Bug 1325096 - Fix menu button padding on select devices r?maliu draft
authorAndrzej Hunt <ahunt@mozilla.com>
Mon, 23 Jan 2017 15:53:49 -0800
changeset 468561 63f194ed7621a76fa0e37e5562a7df439a595970
parent 468498 ee975d32deb9eaa5641f45428cd6a4b5b555a8f5
child 468562 43c1ac0e6c127352e8f6cd44b3fa8e341d3a5e95
push id43503
push userahunt@mozilla.com
push dateTue, 31 Jan 2017 18:27:11 +0000
reviewersmaliu
bugs1325096
milestone54.0a1
Bug 1325096 - Fix menu button padding on select devices r?maliu On some devices, ImageView appears to lose it's padding when calling setBackground[Drawable](). We therefore need to save and restore the padding on those devices. Known affected devices are various Asus Transformer tablets, and also the Motorola Droid 4, running various Android 4.X versions. We therefore make this workaround conditional on Android 4 to minimise the number of devices where this workaround is used. (Having the conditional should also make it easy to notice when we can remove the workaround, i.e. whenever Android 5 becomes the baseline.) MozReview-Commit-ID: B88gWwEx48x
mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java
--- a/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java
@@ -22,17 +22,43 @@ import java.util.Locale;
 public class ViewUtil {
 
     /**
      * Enable a circular touch ripple for a given view. This is intended for borderless views,
      * such as (3-dot) menu buttons.
      *
      * Because of platform limitations a square ripple is used on Android 4.
      */
-    public static void enableTouchRipple(View view) {
+    public static void enableTouchRipple(final View view) {
+        // On certain older devices (e.g. ASUS TF200T, Motorola Droid 4), setting a background
+        // drawable results in the padding getting wiped. We work around this by saving the pre-existing
+        // padding, followed by restoring it at the end in view.setPadding().
+
+        // Unfortunately the IDE and compiler aren't clever enough for us to be able to make
+        // these final (and uninitialised). So we just use garbage values instead:
+        int paddingLeft = -1;
+        int paddingTop = -1;
+        int paddingRight = -1;
+        int paddingBottom = -1;
+
+        if (!AppConstants.Versions.feature21Plus) {
+            paddingLeft = view.getPaddingLeft();
+            paddingTop = view.getPaddingTop();
+            paddingRight = view.getPaddingRight();
+            paddingBottom = view.getPaddingBottom();
+        }
+
+        setTouchRipple(view);
+
+        if (!AppConstants.Versions.feature21Plus) {
+            view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
+        }
+    }
+
+    private static void setTouchRipple(final View view) {
         final TypedArray backgroundDrawableArray;
         if (AppConstants.Versions.feature21Plus) {
             backgroundDrawableArray = view.getContext().obtainStyledAttributes(new int[] { R.attr.selectableItemBackgroundBorderless });
         } else {
             backgroundDrawableArray = view.getContext().obtainStyledAttributes(new int[] { R.attr.selectableItemBackground });
         }
 
         // This call is deprecated, but the replacement setBackground(Drawable) isn't available