Chromium Code Reviews| 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..c69a5a5f1c9ebedb0cb6c7357b01a93dc3a59374 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,108 @@ |
| 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 swictcher 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)); |
| + if (hasBrowserActionsNotification()) { |
| + builder.setContentTitle(mContext.getString( |
| + R.string.browser_actions_multi_links_open_notification_title)); |
|
David Trainor- moved to gerrit
2017/05/23 06:16:43
Just pick the title resource based on hasBrowserAc
ltian
2017/05/23 18:36:46
Done.
|
| + } else { |
| + builder.setContentTitle(mContext.getString( |
| + R.string.browser_actions_single_link_open_notification_title)); |
| + } |
| + 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 +139,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 +169,20 @@ 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(); |
| + } |
| } |