Bug 1325096 - Post: only use deprecated setBackgroundDrawable on older platforms r?maliu draft
authorAndrzej Hunt <ahunt@mozilla.com>
Mon, 23 Jan 2017 15:59:18 -0800
changeset 468563 c4f1942e541a1f5932c0badd63d237426783cbc8
parent 468562 43c1ac0e6c127352e8f6cd44b3fa8e341d3a5e95
child 543984 15bddb69f234132a5472aad49b06270ec1b8e55b
push id43503
push userahunt@mozilla.com
push dateTue, 31 Jan 2017 18:27:11 +0000
reviewersmaliu
bugs1325096
milestone54.0a1
Bug 1325096 - Post: only use deprecated setBackgroundDrawable on older platforms r?maliu This has no benefit right now, but this lets us avoid forgetting to upgrade to the non-deprecated method once we no longer need to support api 15. MozReview-Commit-ID: FmRoP5PnGDC
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
@@ -1,16 +1,17 @@
 /* -*- 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.util;
 
 import android.annotation.TargetApi;
 import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
 import android.os.Build;
 import android.support.v4.text.TextUtilsCompat;
 import android.support.v4.view.MarginLayoutParamsCompat;
 import android.support.v4.view.ViewCompat;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 
@@ -22,17 +23,28 @@ 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.
      */
+    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
     public static void enableTouchRipple(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 });
+        }
+        final Drawable backgroundDrawable = backgroundDrawableArray.getDrawable(0);
+        backgroundDrawableArray.recycle();
+
+
         // 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;
@@ -41,36 +53,30 @@ public class ViewUtil {
 
         if (!AppConstants.Versions.feature21Plus) {
             paddingLeft = view.getPaddingLeft();
             paddingTop = view.getPaddingTop();
             paddingRight = view.getPaddingRight();
             paddingBottom = view.getPaddingBottom();
         }
 
-        setTouchRipple(view);
+        // This call is deprecated, but the replacement setBackground(Drawable) isn't available
+        // until API 16.
+        if (AppConstants.Versions.feature16Plus) {
+            view.setBackground(backgroundDrawable);
+        } else {
+            view.setBackgroundDrawable(backgroundDrawable);
+        }
 
+        // Restore the padding on certain devices, as explained above:
         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
-        // until API 16.
-        view.setBackgroundDrawable(backgroundDrawableArray.getDrawable(0));
-    }
-
     /**
      * Android framework have a bug margin start/end for RTL between 19~22. We can only use MarginLayoutParamsCompat before 17 and after 23.
      * @param layoutParams
      * @param marginStart
      * @param isLayoutRtl
      */
     public static void setMarginStart(ViewGroup.MarginLayoutParams layoutParams, int marginStart, boolean isLayoutRtl) {
         if (AppConstants.Versions.feature17Plus && AppConstants.Versions.preN) {