Bug 1460074 - Still cannot switch between years in date picker; r?jchen draft
authorPetru Lingurar <petru.lingurar@softvision.ro>
Mon, 16 Jul 2018 18:54:26 +0300
changeset 818817 d525fdd74ddf45bc967009352dd9e4231d7a04ee
parent 818682 2ed1506d1dc7db3d70a3feed95f1456bce05bbee
push id116353
push userplingurar@mozilla.com
push dateMon, 16 Jul 2018 15:59:58 +0000
reviewersjchen
bugs1460074
milestone63.0a1
Bug 1460074 - Still cannot switch between years in date picker; r?jchen The problem stemmed from having the DatePicker inside a ScrollView which was receiving the swipe events. To avoid this I've created the new FocusableDatePicker which has the ability to prevent it's parent from receiving touch events. MozReview-Commit-ID: 6VntjE5A0ec
mobile/android/base/java/org/mozilla/gecko/prompts/PromptInput.java
mobile/android/base/java/org/mozilla/gecko/widget/FocusableDatePicker.java
--- a/mobile/android/base/java/org/mozilla/gecko/prompts/PromptInput.java
+++ b/mobile/android/base/java/org/mozilla/gecko/prompts/PromptInput.java
@@ -7,16 +7,17 @@ package org.mozilla.gecko.prompts;
 
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
 
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.widget.AllCapsTextView;
+import org.mozilla.gecko.widget.FocusableDatePicker;
 import org.mozilla.gecko.widget.DateTimePicker;
 
 import android.content.Context;
 import android.content.res.Configuration;
 import android.support.design.widget.TextInputLayout;
 import android.text.Html;
 import android.text.InputType;
 import android.text.TextUtils;
@@ -173,17 +174,18 @@ public abstract class PromptInput {
             super(obj);
         }
 
         // Will use platform's DatePicker and TimePicker to let users input date and time using the fancy widgets.
         // For the other input types our custom DateTimePicker will offer spinners.
         @Override
         public View getView(Context context) throws UnsupportedOperationException {
             if (mType.equals("date")) {
-                DatePicker input = new DatePicker(context);
+                // FocusableDatePicker allow us to have priority in responding to scroll events.
+                DatePicker input = new FocusableDatePicker(context);
                 try {
                     if (!TextUtils.isEmpty(mValue)) {
                         GregorianCalendar calendar = new GregorianCalendar();
                         calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(mValue));
                         input.updateDate(calendar.get(Calendar.YEAR),
                                          calendar.get(Calendar.MONTH),
                                          calendar.get(Calendar.DAY_OF_MONTH));
                     }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/widget/FocusableDatePicker.java
@@ -0,0 +1,49 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.widget;
+
+import android.annotation.SuppressLint;
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.view.ViewParent;
+import android.widget.DatePicker;
+
+/**
+ * This is based on the platform's {@link DatePicker}.<br>
+ * The only difference is that it will prevent it's parent from receiving touch events.
+ */
+public class FocusableDatePicker extends DatePicker {
+    public FocusableDatePicker(Context context) {
+        super(context);
+    }
+
+    public FocusableDatePicker(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public FocusableDatePicker(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    @SuppressLint("NewApi")
+    public FocusableDatePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+    }
+
+    @Override
+    public boolean onInterceptTouchEvent(MotionEvent ev) {
+        ViewParent parentView = getParent();
+
+        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
+            if (parentView != null) {
+                parentView.requestDisallowInterceptTouchEvent(true);
+            }
+        }
+
+        return false;
+    }
+}