| Index: chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuItemDelegate.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuItemDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuItemDelegate.java
|
| index 7d3f8bad7587e2e710ba81af656d94a708fd1f40..5c1d5b56e0b164930efff40b4c68479f4fa65d9e 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuItemDelegate.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuItemDelegate.java
|
| @@ -16,9 +16,12 @@ import android.content.SharedPreferences;
|
| import android.net.Uri;
|
| import android.provider.Browser;
|
|
|
| +import org.chromium.base.ApplicationStatus;
|
| import org.chromium.base.ContextUtils;
|
| import org.chromium.base.Log;
|
| +import org.chromium.base.VisibleForTesting;
|
| import org.chromium.chrome.R;
|
| +import org.chromium.chrome.browser.ChromeTabbedActivity;
|
| import org.chromium.chrome.browser.IntentHandler;
|
| import org.chromium.chrome.browser.document.ChromeLauncherActivity;
|
| import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder;
|
| @@ -28,10 +31,15 @@ import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
|
| import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions;
|
| import org.chromium.chrome.browser.share.ShareHelper;
|
| import org.chromium.chrome.browser.share.ShareParams;
|
| +import org.chromium.chrome.browser.tab.Tab;
|
| import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType;
|
| import org.chromium.chrome.browser.util.IntentUtils;
|
| +import org.chromium.content_public.browser.LoadUrlParams;
|
| +import org.chromium.content_public.common.Referrer;
|
| import org.chromium.ui.widget.Toast;
|
|
|
| +import java.lang.ref.WeakReference;
|
| +
|
| /**
|
| * A delegate responsible for taking actions based on browser action context menu selections.
|
| */
|
| @@ -58,6 +66,8 @@ public class BrowserActionsContextMenuItemDelegate {
|
| private final SharedPreferences mSharedPreferences;
|
| private final String mSourcePackageName;
|
|
|
| + private Intent mNotificationIntent;
|
| +
|
| /**
|
| * Builds a {@link BrowserActionsContextMenuItemDelegate} instance.
|
| * @param activity The activity displays the context menu.
|
| @@ -71,8 +81,13 @@ public class BrowserActionsContextMenuItemDelegate {
|
| mSourcePackageName = sourcePackageName;
|
| }
|
|
|
| - private void sendBrowserActionsNotification() {
|
| - ChromeNotificationBuilder builder = createNotificationBuilder();
|
| + @VisibleForTesting
|
| + Intent getNotificationIntent() {
|
| + return mNotificationIntent;
|
| + }
|
| +
|
| + private void sendBrowserActionsNotification(int tabId) {
|
| + ChromeNotificationBuilder builder = createNotificationBuilder(tabId);
|
| mNotificationManager.notify(
|
| NotificationConstants.NOTIFICATION_ID_BROWSER_ACTIONS, builder.build());
|
| mSharedPreferences.edit().putBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, true).apply();
|
| @@ -80,7 +95,7 @@ public class BrowserActionsContextMenuItemDelegate {
|
| NotificationUmaTracker.BROWSER_ACTIONS, ChannelDefinitions.CHANNEL_ID_BROWSER);
|
| }
|
|
|
| - private ChromeNotificationBuilder createNotificationBuilder() {
|
| + private ChromeNotificationBuilder createNotificationBuilder(int tabId) {
|
| ChromeNotificationBuilder builder =
|
| NotificationBuilderFactory
|
| .createChromeNotificationBuilder(
|
| @@ -94,24 +109,48 @@ public class BrowserActionsContextMenuItemDelegate {
|
| ? R.string.browser_actions_multi_links_open_notification_title
|
| : R.string.browser_actions_single_link_open_notification_title;
|
| builder.setContentTitle(mActivity.getString(titleResId));
|
| - Intent intent = buildNotificationIntent();
|
| - PendingIntent notifyPendingIntent =
|
| - PendingIntent.getActivity(mActivity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
| + mNotificationIntent = buildNotificationIntent(tabId);
|
| + PendingIntent notifyPendingIntent = PendingIntent.getActivity(
|
| + mActivity, 0, mNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
| builder.setContentIntent(notifyPendingIntent);
|
| return builder;
|
| }
|
|
|
| - private Intent buildNotificationIntent() {
|
| + private Intent buildNotificationIntent(int tabId) {
|
| + boolean multipleUrls = hasBrowserActionsNotification();
|
| + if (!multipleUrls && tabId != Tab.INVALID_TAB_ID) {
|
| + return Tab.createBringTabToFrontIntent(tabId);
|
| + }
|
| Intent intent = new Intent(mActivity, ChromeLauncherActivity.class);
|
| intent.setAction(ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND);
|
| - intent.putExtra(EXTRA_IS_SINGLE_URL, !hasBrowserActionsNotification());
|
| + intent.putExtra(EXTRA_IS_SINGLE_URL, !multipleUrls);
|
| return intent;
|
| }
|
|
|
| - private boolean hasBrowserActionsNotification() {
|
| + boolean hasBrowserActionsNotification() {
|
| return mSharedPreferences.getBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, false);
|
| }
|
|
|
| + private int openTabInBackground(String linkUrl) {
|
| + int tabId = Tab.INVALID_TAB_ID;
|
| + Referrer referrer = IntentHandler.constructValidReferrerForAuthority(mSourcePackageName);
|
| + LoadUrlParams loadUrlParams = new LoadUrlParams(linkUrl);
|
| + loadUrlParams.setReferrer(referrer);
|
| + for (WeakReference<Activity> ref : ApplicationStatus.getRunningActivities()) {
|
| + if (!(ref.get() instanceof ChromeTabbedActivity)) continue;
|
| +
|
| + ChromeTabbedActivity activity = (ChromeTabbedActivity) ref.get();
|
| + if (activity == null) continue;
|
| + if (activity.getTabModelSelector() != null) {
|
| + Tab tab = activity.getTabModelSelector().openNewTab(
|
| + loadUrlParams, TabLaunchType.FROM_BROWSER_ACTIONS, null, false);
|
| + assert tab != null;
|
| + tabId = tab.getId();
|
| + }
|
| + }
|
| + return tabId;
|
| + }
|
| +
|
| /**
|
| * Called when the {@code text} should be saved to the clipboard.
|
| * @param text The text to save to the clipboard.
|
| @@ -144,10 +183,19 @@ public class BrowserActionsContextMenuItemDelegate {
|
| * @param linkUrl The url to open.
|
| */
|
| public void onOpenInBackground(String linkUrl) {
|
| - sendBrowserActionsNotification();
|
| - Toast.makeText(mActivity, R.string.browser_actions_open_in_background_toast_message,
|
| - Toast.LENGTH_SHORT)
|
| - .show();
|
| + int tabId = openTabInBackground(linkUrl);
|
| + if (tabId != Tab.INVALID_TAB_ID) {
|
| + sendBrowserActionsNotification(tabId);
|
| + Toast.makeText(mActivity, R.string.browser_actions_open_in_background_toast_message,
|
| + Toast.LENGTH_SHORT)
|
| + .show();
|
| + } else {
|
| + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkUrl));
|
| + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
| + intent.setClass(mActivity, ChromeLauncherActivity.class);
|
| + intent.putExtra(ChromeLauncherActivity.EXTRA_IS_ALLOWED_TO_RETURN_TO_PARENT, false);
|
| + IntentUtils.safeStartActivity(mActivity, intent);
|
| + }
|
| }
|
|
|
| /**
|
|
|