Bug 1312477 - Add popupmenu mode to AS context menu for use on tablet r?sebastian draft
authorAndrzej Hunt <ahunt@mozilla.com>
Wed, 02 Nov 2016 21:01:43 +0100
changeset 434814 36f95a615362d047c6ef9396f0886912f53cf7d8
parent 434813 c1c5b7111fdbcb91453fc3926e1f4d74168e31ec
child 434815 5d57a8220958517fd5860d8c261e68aaaf51a433
push id34834
push userahunt@mozilla.com
push dateMon, 07 Nov 2016 12:22:00 +0000
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 {
@@ -212,18 +213,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
@@ -435,16 +435,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