Bug 1312467 - Move TouchTarget handling into generic helper class r=sebastian
authorAndrzej Hunt <ahunt@mozilla.com>
Mon, 31 Oct 2016 15:42:30 +0100
changeset 346980 330833c71a86b4c60404f8e16e9a5243f1c95fe1
parent 346979 b8c538f62798ee9dd4acec1974236adcfc19183e
child 346981 a207a3a364654ae584c59a4b37edff1d45d99cef
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1312467
milestone52.0a1
Bug 1312467 - Move TouchTarget handling into generic helper class r=sebastian This will allow us to reuse it for other similar menu buttons. MozReview-Commit-ID: L8pleo8bQp9
mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java
mobile/android/base/java/org/mozilla/gecko/util/TouchTargetUtil.java
mobile/android/base/moz.build
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java
@@ -2,39 +2,36 @@
  * 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.home.activitystream;
 
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.graphics.Color;
-import android.graphics.Rect;
 import android.support.v4.view.ViewPager;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
 import android.text.format.DateUtils;
-import android.util.DisplayMetrics;
-import android.util.TypedValue;
-import android.view.TouchDelegate;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.home.HomePager;
 import org.mozilla.gecko.home.activitystream.menu.ActivityStreamContextMenu;
 import org.mozilla.gecko.home.activitystream.topsites.CirclePageIndicator;
 import org.mozilla.gecko.home.activitystream.topsites.TopSitesPagerAdapter;
 import org.mozilla.gecko.icons.IconCallback;
 import org.mozilla.gecko.icons.IconResponse;
 import org.mozilla.gecko.icons.Icons;
 import org.mozilla.gecko.util.DrawableUtil;
+import org.mozilla.gecko.util.TouchTargetUtil;
 import org.mozilla.gecko.widget.FaviconView;
 
 import java.util.concurrent.Future;
 
 import static org.mozilla.gecko.activitystream.ActivityStream.extractLabel;
 
 public abstract class StreamItem extends RecyclerView.ViewHolder {
     public StreamItem(View itemView) {
@@ -101,36 +98,17 @@ public abstract class StreamItem extends
             vPageView = (TextView) itemView.findViewById(R.id.page);
             vSourceIconView = (ImageView) itemView.findViewById(R.id.source_icon);
 
             final ImageView menuButton = (ImageView) itemView.findViewById(R.id.menu);
 
             menuButton.setImageDrawable(
                     DrawableUtil.tintDrawable(menuButton.getContext(), R.drawable.menu, Color.LTGRAY));
 
-            itemView.post(new Runnable() {
-                @Override
-                public void run() {
-                    Rect delegateArea = new Rect();
-                    menuButton.getHitRect(delegateArea);
-
-                    final int targetHitArea = itemView.getContext().getResources().getDimensionPixelSize(R.dimen.touch_target_size);
-
-                    final int widthDelta = (targetHitArea - delegateArea.width()) / 2;
-                    delegateArea.right += widthDelta;
-                    delegateArea.left -= widthDelta;
-
-                    final int heightDelta = (targetHitArea - delegateArea.height()) / 2;
-                    delegateArea.bottom += heightDelta;
-                    delegateArea.top -= heightDelta;
-
-                    TouchDelegate touchDelegate = new TouchDelegate(delegateArea, menuButton);
-                    itemView.setTouchDelegate(touchDelegate);
-                }
-            });
+            TouchTargetUtil.ensureTargetHitArea(menuButton, itemView);
 
             menuButton.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     ActivityStreamContextMenu.show(v.getContext(), title, url, onUrlOpenListener, onUrlOpenInBackgroundListener, vIconView.getWidth(), vIconView.getHeight());
                 }
             });
         }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/util/TouchTargetUtil.java
@@ -0,0 +1,48 @@
+/* -*- 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.graphics.Rect;
+import android.view.TouchDelegate;
+import android.view.View;
+
+import org.mozilla.gecko.R;
+
+public class TouchTargetUtil {
+    /**
+     * Ensures that a given targetView has a large enough touch area to ensure it can be selected.
+     * A TouchDelegate will be added to the enclosingView as necessary.
+     *
+     * @param targetView
+     * @param enclosingView
+     */
+    public static void ensureTargetHitArea(final View targetView, final View enclosingView) {
+        enclosingView.post(new Runnable() {
+            @Override
+            public void run() {
+                Rect delegateArea = new Rect();
+                targetView.getHitRect(delegateArea);
+
+                final int targetHitArea = enclosingView.getContext().getResources().getDimensionPixelSize(R.dimen.touch_target_size);
+
+                final int widthDelta = (targetHitArea - delegateArea.width()) / 2;
+                delegateArea.right += widthDelta;
+                delegateArea.left -= widthDelta;
+
+                final int heightDelta = (targetHitArea - delegateArea.height()) / 2;
+                delegateArea.bottom += heightDelta;
+                delegateArea.top -= heightDelta;
+
+                if (heightDelta <= 0 && widthDelta <= 0) {
+                    return;
+                }
+
+                TouchDelegate touchDelegate = new TouchDelegate(delegateArea, targetView);
+                enclosingView.setTouchDelegate(touchDelegate);
+            }
+        });
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -723,16 +723,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'toolbar/ToolbarEditLayout.java',
     'toolbar/ToolbarEditText.java',
     'toolbar/ToolbarPrefs.java',
     'toolbar/ToolbarProgressView.java',
     'trackingprotection/TrackingProtectionPrompt.java',
     'updater/PostUpdateHandler.java',
     'updater/UpdateService.java',
     'updater/UpdateServiceHelper.java',
+    'util/TouchTargetUtil.java',
     'widget/ActivityChooserModel.java',
     'widget/AllCapsTextView.java',
     'widget/AnchoredPopup.java',
     'widget/AnimatedHeightLayout.java',
     'widget/BasicColorPicker.java',
     'widget/CheckableLinearLayout.java',
     'widget/ClickableWhenDisabledEditText.java',
     'widget/ContentSecurityDoorHanger.java',