| 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
|
| index f97f2d697411f31cfd72983faf4b9d38b4516e06..99b175421537ab6ef81e98cc8027b5d64a5c6d02 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuHelper.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuHelper.java
|
| @@ -4,8 +4,10 @@
|
|
|
| package org.chromium.chrome.browser.browseractions;
|
|
|
| +import android.app.Activity;
|
| import android.app.PendingIntent;
|
| import android.app.PendingIntent.CanceledException;
|
| +import android.app.ProgressDialog;
|
| import android.content.Intent;
|
| import android.net.Uri;
|
| import android.support.customtabs.browseractions.BrowserActionItem;
|
| @@ -21,6 +23,7 @@ import android.view.View.OnCreateContextMenuListener;
|
|
|
| import org.chromium.base.Callback;
|
| import org.chromium.base.Log;
|
| +import org.chromium.base.VisibleForTesting;
|
| import org.chromium.chrome.R;
|
| import org.chromium.chrome.browser.contextmenu.ChromeContextMenuItem;
|
| import org.chromium.chrome.browser.contextmenu.ContextMenuItem;
|
| @@ -41,14 +44,33 @@ import java.util.List;
|
| public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListener,
|
| OnCloseContextMenuListener,
|
| OnAttachStateChangeListener {
|
| - private static final String TAG = "cr_BrowserActions";
|
| - private static final boolean IS_NEW_UI_ENABLED = true;
|
| + /** Notified about events happening for Browser Actions tests. */
|
| + public interface BrowserActionsTestDelegate {
|
| + /** Called when menu is shown. */
|
| + void onBrowserActionsMenuShown();
|
| +
|
| + /** Called when {@link BrowserActionActivity#finishNativeInitialization} is done. */
|
| + void onFinishNativeInitialization();
|
|
|
| - private static final List<Integer> CUSTOM_BROWSER_ACTIONS_ID_GROUP =
|
| + /** Called when Browser Actions start opening a tab in background */
|
| + void onOpenTabInBackgroundStart();
|
| +
|
| + /** Initializes data needed for testing. */
|
| + void initialize(BrowserActionsContextMenuItemDelegate delegate,
|
| + SparseArray<PendingIntent> customActions,
|
| + List<Pair<Integer, List<ContextMenuItem>>> items,
|
| + ProgressDialog progressDialog);
|
| + }
|
| +
|
| +
|
| + static final List<Integer> CUSTOM_BROWSER_ACTIONS_ID_GROUP =
|
| 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 static final String TAG = "cr_BrowserActions";
|
| + private static final boolean IS_NEW_UI_ENABLED = true;
|
| +
|
| // Items list that could be included in the Browser Actions context menu for type {@code LINK}.
|
| private final List<? extends ContextMenuItem> mBrowserActionsLinkGroup;
|
|
|
| @@ -56,31 +78,44 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
|
| private final SparseArray<PendingIntent> mCustomItemActionMap = new SparseArray<>();
|
|
|
| private final ContextMenuParams mCurrentContextMenuParams;
|
| - private final BrowserActionsContextMenuItemDelegate mDelegate;
|
| - private final BrowserActionActivity mActivity;
|
| + private final BrowserActionsContextMenuItemDelegate mMenuItemDelegate;
|
| + private final Activity mActivity;
|
| private final Callback<Integer> mItemSelectedCallback;
|
| private final Runnable mOnMenuShown;
|
| private final Runnable mOnMenuClosed;
|
| + private final Runnable mOnMenuShownListener;
|
| private final Callback<Boolean> mOnShareClickedRunnable;
|
| private final PendingIntent mOnBrowserActionSelectedCallback;
|
|
|
| private final List<Pair<Integer, List<ContextMenuItem>>> mItems;
|
|
|
| - public BrowserActionsContextMenuHelper(BrowserActionActivity activity, ContextMenuParams params,
|
| + private final ProgressDialog mProgressDialog;
|
| +
|
| + private BrowserActionsTestDelegate mTestDelegate;
|
| + private boolean mIsOpenInBackgroundPending;
|
| + private boolean mIsNativeInitialized;
|
| +
|
| + public BrowserActionsContextMenuHelper(Activity activity, ContextMenuParams params,
|
| List<BrowserActionItem> customItems, String sourcePackageName,
|
| - PendingIntent onBrowserActionSelectedCallback) {
|
| + PendingIntent onBrowserActionSelectedCallback, final Runnable listener) {
|
| mActivity = activity;
|
| mCurrentContextMenuParams = params;
|
| + mOnMenuShownListener = listener;
|
| mOnMenuShown = new Runnable() {
|
| @Override
|
| public void run() {
|
| - mActivity.onMenuShown();
|
| + mOnMenuShownListener.run();
|
| + if (mTestDelegate != null) {
|
| + mTestDelegate.onBrowserActionsMenuShown();
|
| + }
|
| }
|
| };
|
| mOnMenuClosed = new Runnable() {
|
| @Override
|
| public void run() {
|
| - mActivity.finish();
|
| + if (!mIsOpenInBackgroundPending) {
|
| + mActivity.finish();
|
| + }
|
| }
|
| };
|
| mItemSelectedCallback = new Callback<Integer>() {
|
| @@ -92,7 +127,7 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
|
| mOnShareClickedRunnable = new Callback<Boolean>() {
|
| @Override
|
| public void onResult(Boolean isShareLink) {
|
| - mDelegate.share(true, mCurrentContextMenuParams.getLinkUrl());
|
| + mMenuItemDelegate.share(true, mCurrentContextMenuParams.getLinkUrl());
|
| }
|
| };
|
| ShareContextMenuItem shareItem = new ShareContextMenuItem(R.drawable.ic_share_white_24dp,
|
| @@ -103,12 +138,23 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
|
| ChromeContextMenuItem.BROWSER_ACTIONS_OPEN_IN_INCOGNITO_TAB,
|
| ChromeContextMenuItem.BROWSER_ACTION_SAVE_LINK_AS,
|
| ChromeContextMenuItem.BROWSER_ACTIONS_COPY_ADDRESS, shareItem);
|
| - mDelegate = new BrowserActionsContextMenuItemDelegate(mActivity, sourcePackageName);
|
| + mMenuItemDelegate = new BrowserActionsContextMenuItemDelegate(mActivity, sourcePackageName);
|
| mOnBrowserActionSelectedCallback = onBrowserActionSelectedCallback;
|
| + mProgressDialog = new ProgressDialog(mActivity);
|
|
|
| mItems = buildContextMenuItems(customItems);
|
| }
|
|
|
| + /**
|
| + * Sets the {@link BrowserActionsTestDelegate} for testing.
|
| + * @param testDelegate The delegate used to notified Browser Actions events.
|
| + */
|
| + @VisibleForTesting
|
| + void setTestDelegateForTesting(BrowserActionsTestDelegate testDelegate) {
|
| + mTestDelegate = testDelegate;
|
| + mTestDelegate.initialize(mMenuItemDelegate, mCustomItemActionMap, mItems, mProgressDialog);
|
| + }
|
| +
|
| /**
|
| * Builds items for Browser Actions context menu.
|
| */
|
| @@ -138,24 +184,28 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
|
| }
|
| }
|
|
|
| - private boolean onItemSelected(int itemId) {
|
| + boolean onItemSelected(int itemId) {
|
| if (itemId == R.id.browser_actions_open_in_background) {
|
| - mDelegate.onOpenInBackground(mCurrentContextMenuParams.getLinkUrl());
|
| - notifyBrowserActionSelected(BrowserActionsIntent.ITEM_OPEN_IN_NEW_TAB);
|
| + if (mIsNativeInitialized) {
|
| + handleOpenInBackground();
|
| + } else {
|
| + mIsOpenInBackgroundPending = true;
|
| + waitNativeInitialized();
|
| + }
|
| } else if (itemId == R.id.browser_actions_open_in_incognito_tab) {
|
| - mDelegate.onOpenInIncognitoTab(mCurrentContextMenuParams.getLinkUrl());
|
| + mMenuItemDelegate.onOpenInIncognitoTab(mCurrentContextMenuParams.getLinkUrl());
|
| notifyBrowserActionSelected(BrowserActionsIntent.ITEM_OPEN_IN_INCOGNITO);
|
| } else if (itemId == R.id.browser_actions_save_link_as) {
|
| - mDelegate.startDownload(mCurrentContextMenuParams.getLinkUrl());
|
| + mMenuItemDelegate.startDownload(mCurrentContextMenuParams.getLinkUrl());
|
| notifyBrowserActionSelected(BrowserActionsIntent.ITEM_DOWNLOAD);
|
| } else if (itemId == R.id.browser_actions_copy_address) {
|
| - mDelegate.onSaveToClipboard(mCurrentContextMenuParams.getLinkUrl());
|
| + mMenuItemDelegate.onSaveToClipboard(mCurrentContextMenuParams.getLinkUrl());
|
| notifyBrowserActionSelected(BrowserActionsIntent.ITEM_COPY);
|
| } else if (itemId == R.id.browser_actions_share) {
|
| - mDelegate.share(false, mCurrentContextMenuParams.getLinkUrl());
|
| + mMenuItemDelegate.share(false, mCurrentContextMenuParams.getLinkUrl());
|
| notifyBrowserActionSelected(BrowserActionsIntent.ITEM_SHARE);
|
| } else if (mCustomItemActionMap.indexOfKey(itemId) >= 0) {
|
| - mDelegate.onCustomItemSelected(mCustomItemActionMap.get(itemId));
|
| + mMenuItemDelegate.onCustomItemSelected(mCustomItemActionMap.get(itemId));
|
| }
|
| return true;
|
| }
|
| @@ -171,6 +221,21 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
|
| }
|
| }
|
|
|
| + /**
|
| + * Display a progress dialog to wait for native libraries initialized.
|
| + */
|
| + private void waitNativeInitialized() {
|
| + mProgressDialog.setMessage(
|
| + mActivity.getString(R.string.browser_actions_loading_native_message));
|
| + mProgressDialog.show();
|
| + }
|
| +
|
| + private void dismissProgressDialog() {
|
| + if (mProgressDialog != null && mProgressDialog.isShowing()) {
|
| + mProgressDialog.dismiss();
|
| + }
|
| + }
|
| +
|
| /**
|
| * Displays the Browser Actions context menu.
|
| * @param view The view to show the context menu if old UI is used.
|
| @@ -208,4 +273,28 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
|
|
|
| @Override
|
| public void onViewDetachedFromWindow(View v) {}
|
| +
|
| + /**
|
| + * Finishes all pending actions which requires Chrome native libraries.
|
| + */
|
| + public void onNativeInitialized() {
|
| + mIsNativeInitialized = true;
|
| + if (mTestDelegate != null) {
|
| + mTestDelegate.onFinishNativeInitialization();
|
| + }
|
| + if (mIsOpenInBackgroundPending) {
|
| + mIsOpenInBackgroundPending = false;
|
| + dismissProgressDialog();
|
| + handleOpenInBackground();
|
| + mActivity.finish();
|
| + }
|
| + }
|
| +
|
| + private void handleOpenInBackground() {
|
| + mMenuItemDelegate.onOpenInBackground(mCurrentContextMenuParams.getLinkUrl());
|
| + if (mTestDelegate != null) {
|
| + mTestDelegate.onOpenTabInBackgroundStart();
|
| + }
|
| + notifyBrowserActionSelected(BrowserActionsIntent.ITEM_OPEN_IN_NEW_TAB);
|
| + }
|
| }
|
|
|