Bug 1312477 - Add popupmenu mode to AS context menu for use on tablet r=sebastian
authorAndrzej Hunt <ahunt@mozilla.com>
Wed, 02 Nov 2016 21:01:43 +0100
changeset 348240 ffdf65db6dba6bbc5eb3e62cdc869746371fdca6
parent 348239 522f6a80deadca7229e77a39babe6224e6bc8b07
child 348241 ee67814f9015f33e893caefe9c8bad0b4daa46f4
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
bugs1312477
milestone52.0a1
Bug 1312477 - Add popupmenu mode to AS context menu for use on tablet r=sebastian MozReview-Commit-ID: HCym4G8RtcU
mobile/android/base/java/org/mozilla/gecko/home/activitystream/menu/ActivityStreamContextMenu.java
mobile/android/base/java/org/mozilla/gecko/home/activitystream/menu/PopupContextMenu.java
mobile/android/base/moz.build
mobile/android/base/resources/layout/activity_stream_contextmenu_popupmenu.xml
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/menu/ActivityStreamContextMenu.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/menu/ActivityStreamContextMenu.java
@@ -16,16 +16,17 @@ import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.IntentHelper;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.annotation.RobocopTarget;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.home.HomePager;
 import org.mozilla.gecko.util.Clipboard;
+import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.util.UIAsyncTask;
 
 import java.util.EnumSet;
 
 @RobocopTarget
 public abstract class ActivityStreamContextMenu
         implements NavigationView.OnNavigationItemSelectedListener {
@@ -213,18 +214,26 @@ public abstract class ActivityStreamCont
                                                       View anchor,
                                                       final MenuMode menuMode,
                                                       final String title, @NonNull final String url,
                                                       HomePager.OnUrlOpenListener onUrlOpenListener,
                                                       HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener,
                                                       final int tilesWidth, final int tilesHeight) {
         final ActivityStreamContextMenu menu;
 
-        menu = new BottomSheetContextMenu(context,
-                menuMode,
-                title, url,
-                onUrlOpenListener, onUrlOpenInBackgroundListener,
-                tilesWidth, tilesHeight);
+        if (!HardwareUtils.isTablet()) {
+            menu = new BottomSheetContextMenu(context,
+                    menuMode,
+                    title, url,
+                    onUrlOpenListener, onUrlOpenInBackgroundListener,
+                    tilesWidth, tilesHeight);
+        } else {
+            menu = new PopupContextMenu(context,
+                    anchor,
+                    menuMode,
+                    title, url,
+                    onUrlOpenListener, onUrlOpenInBackgroundListener);
+        }
 
         menu.show();
         return menu;
     }
 }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/menu/PopupContextMenu.java
@@ -0,0 +1,76 @@
+/* -*- 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.home.activitystream.menu;
+
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.Color;
+import android.graphics.drawable.ColorDrawable;
+import android.support.annotation.NonNull;
+import android.support.design.widget.NavigationView;
+import android.view.LayoutInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.PopupWindow;
+
+import org.mozilla.gecko.R;
+import org.mozilla.gecko.home.HomePager;
+
+/* package-private */ class PopupContextMenu
+        extends ActivityStreamContextMenu {
+
+    private final PopupWindow popupWindow;
+    private final NavigationView navigationView;
+
+    private final View anchor;
+
+    public PopupContextMenu(final Context context,
+                            View anchor,
+                            final MenuMode mode,
+                            final String title,
+                            @NonNull final String url,
+                            HomePager.OnUrlOpenListener onUrlOpenListener,
+                            HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
+        super(context,
+                mode,
+                title,
+                url,
+                onUrlOpenListener,
+                onUrlOpenInBackgroundListener);
+
+        this.anchor = anchor;
+
+        final LayoutInflater inflater = LayoutInflater.from(context);
+
+        View card = inflater.inflate(R.layout.activity_stream_contextmenu_popupmenu, null);
+        navigationView = (NavigationView) card.findViewById(R.id.menu);
+        navigationView.setNavigationItemSelectedListener(this);
+
+        popupWindow = new PopupWindow(context);
+        popupWindow.setContentView(card);
+        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
+        popupWindow.setFocusable(true);
+
+        super.postInit();
+    }
+
+    @Override
+    public MenuItem getItemByID(int id) {
+        return navigationView.getMenu().findItem(id);
+    }
+
+    @Override
+    public void show() {
+        // By default popupWindow follows the pre-material convention of displaying the popup
+        // below a View. We need to shift it over the view:
+        popupWindow.showAsDropDown(anchor,
+                0,
+                -(anchor.getHeight() + anchor.getPaddingBottom()));
+    }
+
+    public void dismiss() {
+        popupWindow.dismiss();
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -436,16 +436,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'health/HealthRecorder.java',
     'health/SessionInformation.java',
     'health/StubbedHealthRecorder.java',
     'home/activitystream/ActivityStream.java',
     'home/activitystream/ActivityStreamHomeFragment.java',
     'home/activitystream/ActivityStreamHomeScreen.java',
     'home/activitystream/menu/ActivityStreamContextMenu.java',
     'home/activitystream/menu/BottomSheetContextMenu.java',
+    'home/activitystream/menu/PopupContextMenu.java',
     'home/activitystream/StreamItem.java',
     'home/activitystream/StreamRecyclerAdapter.java',
     'home/activitystream/topsites/CirclePageIndicator.java',
     'home/activitystream/topsites/TopSitesCard.java',
     'home/activitystream/topsites/TopSitesPage.java',
     'home/activitystream/topsites/TopSitesPageAdapter.java',
     'home/activitystream/topsites/TopSitesPagerAdapter.java',
     'home/BookmarkFolderView.java',
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/resources/layout/activity_stream_contextmenu_popupmenu.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
+                                    xmlns:app="http://schemas.android.com/apk/res-auto"
+                                    android:layout_width="32dp"
+                                    android:layout_height="200dp"
+                                    app:cardElevation="5dp"
+                                    app:cardUseCompatPadding="true">
+
+    <!-- This is mostly a copy of the same menu in activity_stream_contextmenu_bottomsheet.xml,
+         however for the popup menu we don't need to override the dividers, hence we omit the
+         android:theme override -->
+    <android.support.design.widget.NavigationView
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        xmlns:app="http://schemas.android.com/apk/res-auto"
+        android:id="@+id/menu"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:itemTextAppearance="@style/ActivityStreamContextMenuText"
+        app:menu="@menu/activitystream_contextmenu"/>
+
+</android.support.v7.widget.CardView>
\ No newline at end of file