Chromium Code Reviews| 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..8c98353b387bb0412ee3ca196c1a940633842f89 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuHelper.java |
| @@ -0,0 +1,181 @@ |
| +// 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.view.ContextMenu; |
| +import android.view.ContextMenu.ContextMenuInfo; |
| +import android.view.View; |
| +import android.view.View.OnCreateContextMenuListener; |
| +import android.widget.LinearLayout; |
| + |
| +import org.chromium.base.Callback; |
| +import org.chromium.base.CollectionUtil; |
| +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.Collections; |
| +import java.util.HashMap; |
| +import java.util.List; |
| +import java.util.Map; |
| + |
| +/** |
| + * A helper class that handles generating context menus for Browser Actions. |
| + */ |
| +public class BrowserActionsContextMenuHelper |
| + implements OnCreateContextMenuListener, OnCloseContextMenuListener { |
| + private final ContextMenuParams mCurrentContextMenuParams; |
| + private final BrowserActionsContextMenuItemDelegate mDelegate; |
| + private final BrowserActionActivity mActivity; |
|
Ted C
2017/04/19 19:58:41
We should find a way to make this just a vanilla a
ltian
2017/04/21 05:10:54
Done.
|
| + private Callback<Integer> mCallback; |
| + private Runnable mOnMenuShown; |
| + private Runnable mOnMenuClosed; |
| + private Runnable mOnShareClickedRunnable; |
| + |
| + private List<Pair<Integer, List<ContextMenuItem>>> mItems; |
| + |
| + // Items list that could be included in the Browser Actions context menu for type {@code LINK}. |
| + private static final List<? extends ContextMenuItem> BROWSERACTIONS_LINK_GROUP = |
|
Ted C
2017/04/19 19:58:41
ordering nit: statics above local finals above loc
ltian
2017/04/21 05:10:54
Done.
|
| + Collections.unmodifiableList(CollectionUtil.newArrayList( |
| + ChromeContextMenuItem.BROWSERACTIONS_OPEN_IN_BACKGROUND, |
| + ChromeContextMenuItem.BROWSERACTIONS_OPEN_IN_INCOGNITO_TAB, |
| + ChromeContextMenuItem.BROWSERACTION_SAVE_LINK_AS, |
| + ChromeContextMenuItem.BROWSERACTIONS_COPY_ADDRESS, |
| + ChromeContextMenuItem.BROWSERACTIONS_SHARE)); |
| + |
| + private static final List<Integer> BROWSERACTIONS_ID_GROUP = Collections.unmodifiableList( |
| + CollectionUtil.newArrayList(R.id.browseractions_custom_item_one, |
| + R.id.browseractions_custom_item_two, R.id.browseractions_custom_item_three, |
| + R.id.browseractions_custom_item_four, R.id.browseractions_custom_item_five)); |
| + |
| + private static final List<BrowserActionsCustomContextMenuItem> CUSTOM_ITEM_GROUP = |
|
Ted C
2017/04/19 19:58:41
this doesn't look like it should be static.
ltian
2017/04/21 05:10:54
Done.
|
| + new ArrayList<>(); |
| + |
| + // Map the custom items' ids with their PendingIntent actions. |
| + private static final Map<Integer, PendingIntent> CUSTOM_TIME_ACTION_MAP = new HashMap<>(); |
|
Ted C
2017/04/19 19:58:41
Should we use a https://developer.android.com/refe
ltian
2017/04/21 05:10:54
Done.
|
| + |
| + private boolean mIsNewUIEnable = true; |
| + |
| + public BrowserActionsContextMenuHelper(BrowserActionActivity activity, ContextMenuParams params, |
| + List<BrowserActionItem> customItems) { |
| + mActivity = activity; |
| + mCurrentContextMenuParams = params; |
| + mOnMenuShown = new Runnable() { |
| + @Override |
| + public void run() { |
| + mActivity.onMenuShow(); |
| + } |
| + }; |
| + 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); |
| + } |
| + |
| + /** |
| + * Build items for Browser Actions context menu. |
| + */ |
| + private void buildContextMenuItems(List<BrowserActionItem> customItems) { |
| + addBrowserActionItems(customItems); |
| + List<ContextMenuItem> items = new ArrayList<>(); |
| + items.addAll(BROWSERACTIONS_LINK_GROUP); |
| + items.addAll(CUSTOM_ITEM_GROUP); |
| + if (items.isEmpty()) return; |
| + |
| + mItems.add(new Pair<>(R.string.contextmenu_link_title, items)); |
| + } |
| + |
| + /** |
| + * Functions to add List of {@link BrowserActionItem} to the populator |
| + * @param items List of {@link BrowserActionItem}. |
| + */ |
| + private void addBrowserActionItems(List<BrowserActionItem> items) { |
| + for (int i = 0; i < items.size() && i < BrowserActionsIntent.MAX_CUSTOM_ITEMS; i++) { |
| + CUSTOM_ITEM_GROUP.add(new BrowserActionsCustomContextMenuItem( |
| + BROWSERACTIONS_ID_GROUP.get(i), items.get(i))); |
| + CUSTOM_TIME_ACTION_MAP.put(BROWSERACTIONS_ID_GROUP.get(i), items.get(i).getAction()); |
| + } |
| + } |
| + |
| + private void clearCustomItems() { |
| + CUSTOM_ITEM_GROUP.clear(); |
| + CUSTOM_TIME_ACTION_MAP.clear(); |
| + } |
| + |
| + private boolean onItemSelected(ContextMenuParams params, int itemId) { |
| + if (itemId == R.id.browseractions_open_in_background) { |
| + mDelegate.onOpenInBackground(params.getLinkUrl()); |
| + } else if (itemId == R.id.browseractions_open_in_incognito_tab) { |
| + mDelegate.onOpenInIncognitoTab(params.getLinkUrl()); |
| + } else if (itemId == R.id.browseractions_save_link_as) { |
| + mDelegate.startDownload(params.getLinkUrl()); |
| + } else if (itemId == R.id.browseractions_copy_address) { |
| + mDelegate.onSaveToClipboard(params.getLinkUrl()); |
| + } else if (itemId == R.id.browseractions_share) { |
| + mDelegate.share(params.getLinkUrl()); |
| + } else if (CUSTOM_TIME_ACTION_MAP.containsKey(itemId)) { |
| + mDelegate.onCustomItemSelected(CUSTOM_TIME_ACTION_MAP.get(itemId)); |
| + } |
| + return true; |
| + } |
| + |
| + public void displayBrowserActionsMenu(final LinearLayout layout) { |
| + if (mIsNewUIEnable) { |
| + ContextMenuUi menuUi = new TabularContextMenuUi(mOnShareClickedRunnable); |
| + menuUi.displayMenu(mActivity, mCurrentContextMenuParams, mItems, mCallback, |
| + mOnMenuShown, mOnMenuClosed); |
| + } else { |
| + new Handler().postDelayed(new Runnable() { |
| + @Override |
| + public void run() { |
| + layout.setOnCreateContextMenuListener(BrowserActionsContextMenuHelper.this); |
| + mActivity.setOnCloseContextMenuListener(BrowserActionsContextMenuHelper.this); |
| + if (layout.showContextMenu()) { |
| + mOnMenuShown.run(); |
| + } |
| + } |
| + }, 50); |
|
Ted C
2017/04/19 19:58:41
why does this need to be delayed? Why 50ms?
ltian
2017/04/21 05:10:54
The only reason I have this Handler is because if
|
| + } |
| + } |
| + |
| + @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() { |
| + clearCustomItems(); |
|
Ted C
2017/04/19 19:58:41
Should this be mOnMenuClosed.run() ?
ltian
2017/04/21 05:10:54
Done.
|
| + } |
| +} |