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 11854ef3f297b614085d0d269f24ae49c6e91a45..7c2bd87ed9534cc83b34a00fb5a175c4d90a9d36 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 |
@@ -4,35 +4,105 @@ |
package org.chromium.chrome.browser.browseractions; |
+import android.app.NotificationManager; |
import android.app.PendingIntent; |
import android.app.PendingIntent.CanceledException; |
import android.content.ClipData; |
import android.content.ClipboardManager; |
import android.content.Context; |
import android.content.Intent; |
+import android.content.SharedPreferences; |
import android.net.Uri; |
import android.provider.Browser; |
import org.chromium.base.ContextUtils; |
import org.chromium.base.Log; |
+import org.chromium.chrome.R; |
import org.chromium.chrome.browser.IntentHandler; |
import org.chromium.chrome.browser.document.ChromeLauncherActivity; |
+import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder; |
+import org.chromium.chrome.browser.notifications.NotificationBuilderFactory; |
+import org.chromium.chrome.browser.notifications.NotificationConstants; |
+import org.chromium.chrome.browser.notifications.NotificationUmaTracker; |
+import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions; |
import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
import org.chromium.chrome.browser.util.IntentUtils; |
+import org.chromium.ui.widget.Toast; |
/** |
* A delegate responsible for taking actions based on browser action context menu selections. |
*/ |
public class BrowserActionsContextMenuItemDelegate { |
private static final String TAG = "BrowserActionsItem"; |
+ /** |
+ * Action to request open ChromeTabbedActivity in tab switcher mode. |
+ */ |
+ public static final String ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND = |
+ "org.chromium.chrome.browser.browseractions.browser_action_open_in_background"; |
+ |
+ public static final String PREF_HAS_BROWSER_ACTIONS_NOTIFICATION = |
+ "org.chromium.chrome.browser.browseractions.HAS_BROWSER_ACTIONS_NOTIFICATION"; |
+ |
+ /** |
+ * Extra that indicates whether to show a Tab for single url or the tab switcher for |
+ * multiple urls. |
+ */ |
+ public static final String EXTRA_IS_SINGLE_URL = |
+ "org.chromium.chrome.browser.browseractions.is_single_url"; |
private final Context mContext; |
+ private final NotificationManager mNotificationManager; |
+ private final SharedPreferences mSharedPreferences; |
+ |
+ private void sendBrowserActionsNotification() { |
+ ChromeNotificationBuilder builder = createNotificationBuilder(); |
+ mNotificationManager.notify( |
+ NotificationConstants.NOTIFICATION_ID_BROWSER_ACTIONS, builder.build()); |
+ mSharedPreferences.edit().putBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, true).apply(); |
+ NotificationUmaTracker.getInstance().onNotificationShown( |
+ NotificationUmaTracker.BROWSER_ACTIONS, ChannelDefinitions.CHANNEL_ID_BROWSER); |
+ } |
+ |
+ private ChromeNotificationBuilder createNotificationBuilder() { |
+ ChromeNotificationBuilder builder = |
+ NotificationBuilderFactory |
+ .createChromeNotificationBuilder( |
+ true /* preferCompat */, ChannelDefinitions.CHANNEL_ID_BROWSER) |
+ .setSmallIcon(R.drawable.infobar_chrome) |
+ .setLocalOnly(true) |
+ .setAutoCancel(true) |
+ .setContentText( |
+ mContext.getString(R.string.browser_actions_notification_text)); |
+ int titleResId = hasBrowserActionsNotification() |
+ ? R.string.browser_actions_multi_links_open_notification_title |
+ : R.string.browser_actions_single_link_open_notification_title; |
+ builder.setContentTitle(mContext.getString(titleResId)); |
+ Intent intent = buildNotificationIntent(); |
+ PendingIntent notifyPendingIntent = |
+ PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
+ builder.setContentIntent(notifyPendingIntent); |
+ return builder; |
+ } |
+ |
+ private Intent buildNotificationIntent() { |
+ Intent intent = new Intent(mContext, ChromeLauncherActivity.class); |
+ intent.setAction(ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND); |
+ intent.putExtra(EXTRA_IS_SINGLE_URL, !hasBrowserActionsNotification()); |
+ return intent; |
+ } |
+ |
+ private boolean hasBrowserActionsNotification() { |
+ return mSharedPreferences.getBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, false); |
+ } |
/** |
* Builds a {@link BrowserActionsContextMenuItemDelegate} instance. |
*/ |
public BrowserActionsContextMenuItemDelegate() { |
mContext = ContextUtils.getApplicationContext(); |
+ mNotificationManager = |
+ (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
+ mSharedPreferences = ContextUtils.getAppSharedPreferences(); |
} |
/** |
@@ -66,7 +136,12 @@ public class BrowserActionsContextMenuItemDelegate { |
* Called when the {@code linkUrl} should be opened in Chrome in the background. |
* @param linkUrl The url to open. |
*/ |
- public void onOpenInBackground(String linkUrl) {} |
+ public void onOpenInBackground(String linkUrl) { |
+ sendBrowserActionsNotification(); |
+ Toast.makeText(mContext, R.string.browser_actions_open_in_background_toast_message, |
+ Toast.LENGTH_SHORT) |
+ .show(); |
+ } |
/** |
* Called when a custom item of Browser action menu is selected. |
@@ -91,4 +166,43 @@ public class BrowserActionsContextMenuItemDelegate { |
* @param linkUrl The url to share. |
*/ |
public void share(String linkUrl) {} |
+ |
+ /** |
+ * Cancel Browser Actions notification. |
+ */ |
+ public static void cancelBrowserActionsNotification() { |
+ NotificationManager notificationManager = |
+ (NotificationManager) ContextUtils.getApplicationContext().getSystemService( |
+ Context.NOTIFICATION_SERVICE); |
+ notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_BROWSER_ACTIONS); |
+ ContextUtils.getAppSharedPreferences() |
+ .edit() |
+ .putBoolean( |
+ BrowserActionsContextMenuItemDelegate.PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, |
+ false) |
+ .apply(); |
+ } |
+ |
+ /** |
+ * Checks whether Chrome should display tab switcher via Browser Actions Intent. |
+ * @param intent The intent to open the Chrome. |
+ * @param isOverviewVisible Whether tab switcher is shown. |
+ */ |
+ public static boolean toggleOverviewByBrowserActions(Intent intent, boolean isOverviewVisible) { |
+ boolean fromBrowserActions = isStartedByBrowserActions(intent); |
+ boolean isSingleUrl = IntentUtils.safeGetBooleanExtra( |
+ intent, BrowserActionsContextMenuItemDelegate.EXTRA_IS_SINGLE_URL, false); |
+ if (fromBrowserActions) { |
+ return isSingleUrl == isOverviewVisible; |
+ } |
+ return false; |
+ } |
+ |
+ private static boolean isStartedByBrowserActions(Intent intent) { |
+ if (BrowserActionsContextMenuItemDelegate.ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND.equals( |
+ intent.getAction())) { |
+ return true; |
+ } |
+ return false; |
+ } |
} |