From 0680c3fa2fc3d94487120d862e7fbee8624c38a5 Mon Sep 17 00:00:00 2001 From: Andrzej Hunt Date: Tue, 8 Nov 2016 10:11:02 -0500 Subject: [PATCH] Bug 1315717 - Add ripple feedback to AS context menu buttons on Android >= 5 r=sebastian The circular ripple is only available on API >= 21. We can fallback to a different solution for older devices, see following patch. MozReview-Commit-ID: C0aBqsKsuZ5 --HG-- extra : rebase_source : ae5139daca4a61c1dfe78bdca7d686494d36d482 extra : source : 34e9726d1c21fa1d998f8469175e8b91d849b7e7 --- .../gecko/home/activitystream/StreamItem.java | 3 ++ .../activitystream/topsites/TopSitesCard.java | 3 ++ .../java/org/mozilla/gecko/util/ViewUtil.java | 33 +++++++++++++++++++ mobile/android/base/moz.build | 1 + 4 files changed, 40 insertions(+) create mode 100644 mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java diff --git a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java index 9396a53dbcbb..24348dfe0926 100644 --- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java +++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java @@ -27,6 +27,7 @@ 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.ViewUtil; import org.mozilla.gecko.util.TouchTargetUtil; import org.mozilla.gecko.widget.FaviconView; @@ -124,6 +125,8 @@ public abstract class StreamItem extends RecyclerView.ViewHolder { vIconView.getWidth(), vIconView.getHeight()); } }); + + ViewUtil.enableTouchRipple(menuButton); } public void bind(Cursor cursor, int tilesWidth, int tilesHeight) { diff --git a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java index 139d58e893ba..b436a466f7f7 100644 --- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java +++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java @@ -19,6 +19,7 @@ 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.ViewUtil; import org.mozilla.gecko.util.TouchTargetUtil; import org.mozilla.gecko.widget.FaviconView; @@ -53,6 +54,8 @@ class TopSitesCard extends RecyclerView.ViewHolder TouchTargetUtil.ensureTargetHitArea(menuButton, card); menuButton.setOnClickListener(this); + + ViewUtil.enableTouchRipple(menuButton); } void bind(final TopSitesPageAdapter.TopSite topSite) { diff --git a/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java b/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java new file mode 100644 index 000000000000..180e821e7c58 --- /dev/null +++ b/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java @@ -0,0 +1,33 @@ +/* -*- 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.content.res.TypedArray; +import android.view.View; + +import org.mozilla.gecko.AppConstants; +import org.mozilla.gecko.R; + +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) { + 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)); + } +} diff --git a/mobile/android/base/moz.build b/mobile/android/base/moz.build index a71f8f54d2e8..6c88464ab521 100644 --- a/mobile/android/base/moz.build +++ b/mobile/android/base/moz.build @@ -742,6 +742,7 @@ gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [ 'util/DrawableUtil.java', 'util/ResourceDrawableUtils.java', 'util/TouchTargetUtil.java', + 'util/ViewUtil.java', 'widget/ActivityChooserModel.java', 'widget/AllCapsTextView.java', 'widget/AnchoredPopup.java',