Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1886)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuHelper.java

Issue 2815453002: [Android] Show Browser Actions dialog in Chrome (Closed)
Patch Set: Update based on Maria's comments. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuHelper.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuHelper.java
new file mode 100644
index 0000000000000000000000000000000000000000..9c0e33d6e9df0cd52dbff2ddaa5fb9ab9e4765d8
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuHelper.java
@@ -0,0 +1,179 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.browseractions;
+
+import android.app.PendingIntent;
+import android.os.Handler;
+import android.support.customtabs.browseractions.BrowserActionItem;
+import android.support.customtabs.browseractions.BrowserActionsIntent;
+import android.util.Pair;
+import android.util.SparseArray;
+import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
+import android.view.View;
+import android.view.View.OnCreateContextMenuListener;
+
+import org.chromium.base.Callback;
+import org.chromium.chrome.R;
+import org.chromium.chrome.browser.contextmenu.ChromeContextMenuItem;
+import org.chromium.chrome.browser.contextmenu.ContextMenuItem;
+import org.chromium.chrome.browser.contextmenu.ContextMenuParams;
+import org.chromium.chrome.browser.contextmenu.ContextMenuUi;
+import org.chromium.chrome.browser.contextmenu.PlatformContextMenuUi;
+import org.chromium.chrome.browser.contextmenu.TabularContextMenuUi;
+import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * A helper class that handles generating context menus for Browser Actions.
+ */
+public class BrowserActionsContextMenuHelper
+ implements OnCreateContextMenuListener, OnCloseContextMenuListener {
+ // Items list that could be included in the Browser Actions context menu for type {@code LINK}.
+ private static final List<? extends ContextMenuItem> BROWSER_ACTIONS_LINK_GROUP =
+ Arrays.asList(ChromeContextMenuItem.BROWSER_ACTIONS_OPEN_IN_BACKGROUND,
+ ChromeContextMenuItem.BROWSER_ACTIONS_OPEN_IN_INCOGNITO_TAB,
+ ChromeContextMenuItem.BROWSER_ACTION_SAVE_LINK_AS,
+ ChromeContextMenuItem.BROWSER_ACTIONS_COPY_ADDRESS,
+ ChromeContextMenuItem.BROWSER_ACTIONS_SHARE);
+
+ private static final List<Integer> BROWSER_ACTIONS_ID_GROUP =
Ted C 2017/05/12 23:35:26 [CUSTOM_]BROWSER_ACTIONS_ID_GROUP
ltian 2017/05/15 21:47:04 Done.
+ Arrays.asList(R.id.browser_actions_custom_item_one,
+ R.id.browser_actions_custom_item_two, R.id.browser_actions_custom_item_three,
+ R.id.browser_actions_custom_item_four, R.id.browser_actions_custom_item_five);
+
+ private final List<BrowserActionsCustomContextMenuItem> mCustomItemGroup = new ArrayList<>();
+
+ // Map each custom item's id with its PendingIntent action.
+ private final SparseArray<PendingIntent> mCustomItemActionMap = new SparseArray<>();
+
+ private final ContextMenuParams mCurrentContextMenuParams;
+ private final BrowserActionsContextMenuItemDelegate mDelegate;
+ private final BrowserActionActivity mActivity;
+ private Callback<Integer> mCallback;
Ted C 2017/05/12 23:35:26 s/mCallback/mItemSelectedCallback
ltian 2017/05/15 21:47:04 Done.
+ private Runnable mOnMenuShown;
+ private Runnable mOnMenuClosed;
+ private Runnable mOnShareClickedRunnable;
+
+ private List<Pair<Integer, List<ContextMenuItem>>> mItems;
Ted C 2017/05/12 23:35:25 looks like all this and above can be private
ltian 2017/05/15 21:47:04 Do you mean final? I think final is ok for all the
+
+ private boolean mIsNewUiEnabled = true;
Ted C 2017/05/12 23:35:26 this should be a static
ltian 2017/05/15 21:47:04 Done.
+
+ public BrowserActionsContextMenuHelper(BrowserActionActivity activity, ContextMenuParams params,
+ List<BrowserActionItem> customItems) {
+ mActivity = activity;
+ mCurrentContextMenuParams = params;
+ mOnMenuShown = new Runnable() {
+ @Override
+ public void run() {
+ mActivity.onMenuShown();
+ }
+ };
+ mOnMenuClosed = new Runnable() {
+ @Override
+ public void run() {
+ mActivity.finish();
+ clearCustomItems();
+ }
+ };
+ mCallback = new Callback<Integer>() {
+ @Override
+ public void onResult(Integer result) {
+ onItemSelected(mCurrentContextMenuParams, result);
+ }
+ };
+ mOnShareClickedRunnable = new Runnable() {
+ @Override
+ public void run() {}
+ };
+ mDelegate = new BrowserActionsContextMenuItemDelegate(mActivity);
+ mItems = new ArrayList<>();
+ buildContextMenuItems(customItems);
+ }
+
+ /**
+ * Builds items for Browser Actions context menu.
+ */
+ private void buildContextMenuItems(List<BrowserActionItem> customItems) {
Ted C 2017/05/12 23:35:25 maybe this should just return a List<Pair<Integer,
ltian 2017/05/15 21:47:03 Done.
+ addBrowserActionItems(customItems);
+ List<ContextMenuItem> items = new ArrayList<>();
+ items.addAll(BROWSER_ACTIONS_LINK_GROUP);
+ items.addAll(mCustomItemGroup);
+ if (items.isEmpty()) return;
+
+ mItems.add(new Pair<>(R.string.contextmenu_link_title, items));
+ }
+
+ /**
+ * Populates custom action map.
+ * @param items List of {@link BrowserActionItem} to populate the map.
+ */
+ private void addBrowserActionItems(List<BrowserActionItem> items) {
+ for (int i = 0; i < items.size() && i < BrowserActionsIntent.MAX_CUSTOM_ITEMS; i++) {
+ mCustomItemGroup.add(new BrowserActionsCustomContextMenuItem(
Ted C 2017/05/12 23:35:26 I don't think you need mCustomItemGroup at all. Y
ltian 2017/05/15 21:47:04 Done.
+ BROWSER_ACTIONS_ID_GROUP.get(i), items.get(i)));
+ mCustomItemActionMap.put(BROWSER_ACTIONS_ID_GROUP.get(i), items.get(i).getAction());
+ }
+ }
+
+ private void clearCustomItems() {
Ted C 2017/05/12 23:35:26 why do you need to do this?
ltian 2017/05/15 21:47:04 Hmm, I remember I have this because I see the cust
+ mCustomItemGroup.clear();
+ mCustomItemActionMap.clear();
+ }
+
+ private boolean onItemSelected(ContextMenuParams params, int itemId) {
Ted C 2017/05/12 23:35:26 no need to pass in params, you already have access
ltian 2017/05/15 21:47:04 Done.
+ if (itemId == R.id.browser_actions_open_in_background) {
+ mDelegate.onOpenInBackground(params.getLinkUrl());
+ } else if (itemId == R.id.browser_actions_open_in_incognito_tab) {
+ mDelegate.onOpenInIncognitoTab(params.getLinkUrl());
+ } else if (itemId == R.id.browser_actions_save_link_as) {
+ mDelegate.startDownload(params.getLinkUrl());
+ } else if (itemId == R.id.browser_actions_copy_address) {
+ mDelegate.onSaveToClipboard(params.getLinkUrl());
+ } else if (itemId == R.id.browser_actions_share) {
+ mDelegate.share(params.getLinkUrl());
+ } else if (mCustomItemActionMap.indexOfKey(itemId) >= 0) {
+ mDelegate.onCustomItemSelected(mCustomItemActionMap.get(itemId));
+ }
+ return true;
+ }
+
+ /**
+ * Displays the Browser Actions context menu.
+ * @param view The view to show the context menu if old UI is used.
+ */
+ public void displayBrowserActionsMenu(final View view) {
+ if (mIsNewUiEnabled) {
+ ContextMenuUi menuUi = new TabularContextMenuUi(mOnShareClickedRunnable);
+ menuUi.displayMenu(mActivity, mCurrentContextMenuParams, mItems, mCallback,
+ mOnMenuShown, mOnMenuClosed);
+ } else {
+ new Handler().postDelayed(new Runnable() {
Ted C 2017/05/12 23:35:26 maybe add a comment for why this is needed
ltian 2017/05/15 21:47:04 Done. Change the delay to 100 because there is st
+ @Override
+ public void run() {
+ view.setOnCreateContextMenuListener(BrowserActionsContextMenuHelper.this);
+ if (view.showContextMenu()) {
+ mOnMenuShown.run();
+ }
+ }
+ }, 50);
+ }
+ }
+
+ @Override
+ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+ ContextMenuUi menuUi = new PlatformContextMenuUi(menu);
+ menuUi.displayMenu(mActivity, mCurrentContextMenuParams, mItems, mCallback, mOnMenuShown,
+ mOnMenuClosed);
+ }
+
+ @Override
+ public void onContextMenuClosed() {
+ mOnMenuClosed.run();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698