Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.browseractions; | 5 package org.chromium.chrome.browser.browseractions; |
| 6 | 6 |
| 7 import android.app.NotificationManager; | |
| 7 import android.app.PendingIntent; | 8 import android.app.PendingIntent; |
| 8 import android.app.PendingIntent.CanceledException; | 9 import android.app.PendingIntent.CanceledException; |
| 9 import android.content.ClipData; | 10 import android.content.ClipData; |
| 10 import android.content.ClipboardManager; | 11 import android.content.ClipboardManager; |
| 11 import android.content.Context; | 12 import android.content.Context; |
| 12 import android.content.Intent; | 13 import android.content.Intent; |
| 14 import android.content.SharedPreferences; | |
| 13 import android.net.Uri; | 15 import android.net.Uri; |
| 14 import android.provider.Browser; | 16 import android.provider.Browser; |
| 15 | 17 |
| 16 import org.chromium.base.ContextUtils; | 18 import org.chromium.base.ContextUtils; |
| 17 import org.chromium.base.Log; | 19 import org.chromium.base.Log; |
| 20 import org.chromium.chrome.R; | |
| 18 import org.chromium.chrome.browser.IntentHandler; | 21 import org.chromium.chrome.browser.IntentHandler; |
| 19 import org.chromium.chrome.browser.document.ChromeLauncherActivity; | 22 import org.chromium.chrome.browser.document.ChromeLauncherActivity; |
| 23 import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder; | |
| 24 import org.chromium.chrome.browser.notifications.NotificationBuilderFactory; | |
| 25 import org.chromium.chrome.browser.notifications.NotificationConstants; | |
| 26 import org.chromium.chrome.browser.notifications.NotificationUmaTracker; | |
| 27 import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions; | |
| 20 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; | 28 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
| 21 import org.chromium.chrome.browser.util.IntentUtils; | 29 import org.chromium.chrome.browser.util.IntentUtils; |
| 30 import org.chromium.ui.widget.Toast; | |
| 22 | 31 |
| 23 /** | 32 /** |
| 24 * A delegate responsible for taking actions based on browser action context men u selections. | 33 * A delegate responsible for taking actions based on browser action context men u selections. |
| 25 */ | 34 */ |
| 26 public class BrowserActionsContextMenuItemDelegate { | 35 public class BrowserActionsContextMenuItemDelegate { |
| 27 private static final String TAG = "BrowserActionsItem"; | 36 private static final String TAG = "BrowserActionsItem"; |
| 37 /** | |
| 38 * Action to request open ChromeTabbedActivity in tab switcher mode. | |
| 39 */ | |
| 40 public static final String ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND = | |
| 41 "org.chromium.chrome.browser.browseractions.browser_action_open_in_b ackground"; | |
| 42 | |
| 43 public static final String PREF_HAS_BROWSER_ACTIONS_NOTIFICATION = | |
| 44 "org.chromium.chrome.browser.browseractions.HAS_BROWSER_ACTIONS_NOTI FICATION"; | |
| 45 | |
| 46 /** | |
| 47 * Extra that indicates whether to show a Tab for single url or the tab swic tcher for | |
| 48 * multiple urls. | |
| 49 */ | |
| 50 public static final String EXTRA_IS_SINGLE_URL = | |
| 51 "org.chromium.chrome.browser.browseractions.is_single_url"; | |
| 28 | 52 |
| 29 private final Context mContext; | 53 private final Context mContext; |
| 54 private final NotificationManager mNotificationManager; | |
| 55 private final SharedPreferences mSharedPreferences; | |
| 56 | |
| 57 private void sendBrowserActionsNotification() { | |
| 58 ChromeNotificationBuilder builder = createNotificationBuilder(); | |
| 59 mNotificationManager.notify( | |
| 60 NotificationConstants.NOTIFICATION_ID_BROWSER_ACTIONS, builder.b uild()); | |
| 61 mSharedPreferences.edit().putBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATI ON, true).apply(); | |
| 62 NotificationUmaTracker.getInstance().onNotificationShown( | |
| 63 NotificationUmaTracker.BROWSER_ACTIONS, ChannelDefinitions.CHANN EL_ID_BROWSER); | |
| 64 } | |
| 65 | |
| 66 private ChromeNotificationBuilder createNotificationBuilder() { | |
| 67 ChromeNotificationBuilder builder = | |
| 68 NotificationBuilderFactory | |
| 69 .createChromeNotificationBuilder( | |
| 70 true /* preferCompat */, ChannelDefinitions.CHAN NEL_ID_BROWSER) | |
| 71 .setSmallIcon(R.drawable.infobar_chrome) | |
| 72 .setLocalOnly(true) | |
| 73 .setAutoCancel(true) | |
| 74 .setContentText( | |
| 75 mContext.getString(R.string.browser_actions_noti fication_text)); | |
| 76 if (hasBrowserActionsNotification()) { | |
| 77 builder.setContentTitle(mContext.getString( | |
| 78 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.
| |
| 79 } else { | |
| 80 builder.setContentTitle(mContext.getString( | |
| 81 R.string.browser_actions_single_link_open_notification_title )); | |
| 82 } | |
| 83 Intent intent = buildNotificationIntent(); | |
| 84 PendingIntent notifyPendingIntent = | |
| 85 PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLA G_UPDATE_CURRENT); | |
| 86 builder.setContentIntent(notifyPendingIntent); | |
| 87 return builder; | |
| 88 } | |
| 89 | |
| 90 private Intent buildNotificationIntent() { | |
| 91 Intent intent = new Intent(mContext, ChromeLauncherActivity.class); | |
| 92 intent.setAction(ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND); | |
| 93 intent.putExtra(EXTRA_IS_SINGLE_URL, !hasBrowserActionsNotification()); | |
| 94 return intent; | |
| 95 } | |
| 96 | |
| 97 private boolean hasBrowserActionsNotification() { | |
| 98 return mSharedPreferences.getBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATI ON, false); | |
| 99 } | |
| 30 | 100 |
| 31 /** | 101 /** |
| 32 * Builds a {@link BrowserActionsContextMenuItemDelegate} instance. | 102 * Builds a {@link BrowserActionsContextMenuItemDelegate} instance. |
| 33 */ | 103 */ |
| 34 public BrowserActionsContextMenuItemDelegate() { | 104 public BrowserActionsContextMenuItemDelegate() { |
| 35 mContext = ContextUtils.getApplicationContext(); | 105 mContext = ContextUtils.getApplicationContext(); |
| 106 mNotificationManager = | |
| 107 (NotificationManager) mContext.getSystemService(Context.NOTIFICA TION_SERVICE); | |
| 108 mSharedPreferences = ContextUtils.getAppSharedPreferences(); | |
| 36 } | 109 } |
| 37 | 110 |
| 38 /** | 111 /** |
| 39 * Called when the {@code text} should be saved to the clipboard. | 112 * Called when the {@code text} should be saved to the clipboard. |
| 40 * @param text The text to save to the clipboard. | 113 * @param text The text to save to the clipboard. |
| 41 */ | 114 */ |
| 42 public void onSaveToClipboard(String text) { | 115 public void onSaveToClipboard(String text) { |
| 43 ClipboardManager clipboardManager = | 116 ClipboardManager clipboardManager = |
| 44 (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_S ERVICE); | 117 (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_S ERVICE); |
| 45 ClipData data = ClipData.newPlainText("url", text); | 118 ClipData data = ClipData.newPlainText("url", text); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 59 intent.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName()) ; | 132 intent.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName()) ; |
| 60 IntentHandler.addTrustedIntentExtras(intent); | 133 IntentHandler.addTrustedIntentExtras(intent); |
| 61 IntentHandler.setTabLaunchType(intent, TabLaunchType.FROM_EXTERNAL_APP); | 134 IntentHandler.setTabLaunchType(intent, TabLaunchType.FROM_EXTERNAL_APP); |
| 62 IntentUtils.safeStartActivity(mContext, intent); | 135 IntentUtils.safeStartActivity(mContext, intent); |
| 63 } | 136 } |
| 64 | 137 |
| 65 /** | 138 /** |
| 66 * Called when the {@code linkUrl} should be opened in Chrome in the backgro und. | 139 * Called when the {@code linkUrl} should be opened in Chrome in the backgro und. |
| 67 * @param linkUrl The url to open. | 140 * @param linkUrl The url to open. |
| 68 */ | 141 */ |
| 69 public void onOpenInBackground(String linkUrl) {} | 142 public void onOpenInBackground(String linkUrl) { |
| 143 sendBrowserActionsNotification(); | |
| 144 Toast.makeText(mContext, R.string.browser_actions_open_in_background_toa st_message, | |
| 145 Toast.LENGTH_SHORT) | |
| 146 .show(); | |
| 147 } | |
| 70 | 148 |
| 71 /** | 149 /** |
| 72 * Called when a custom item of Browser action menu is selected. | 150 * Called when a custom item of Browser action menu is selected. |
| 73 * @param action The PendingIntent action to be launched. | 151 * @param action The PendingIntent action to be launched. |
| 74 */ | 152 */ |
| 75 public void onCustomItemSelected(PendingIntent action) { | 153 public void onCustomItemSelected(PendingIntent action) { |
| 76 try { | 154 try { |
| 77 action.send(); | 155 action.send(); |
| 78 } catch (CanceledException e) { | 156 } catch (CanceledException e) { |
| 79 Log.e(TAG, "Browser Action in Chrome failed to send pending intent." ); | 157 Log.e(TAG, "Browser Action in Chrome failed to send pending intent." ); |
| 80 } | 158 } |
| 81 } | 159 } |
| 82 | 160 |
| 83 /** | 161 /** |
| 84 * Called when the page of the {@code linkUrl} should be downloaded. | 162 * Called when the page of the {@code linkUrl} should be downloaded. |
| 85 * @param linkUrl The url of the page to download. | 163 * @param linkUrl The url of the page to download. |
| 86 */ | 164 */ |
| 87 public void startDownload(String linkUrl) {} | 165 public void startDownload(String linkUrl) {} |
| 88 | 166 |
| 89 /** | 167 /** |
| 90 * Called when the {@code linkUrl} should be shared. | 168 * Called when the {@code linkUrl} should be shared. |
| 91 * @param linkUrl The url to share. | 169 * @param linkUrl The url to share. |
| 92 */ | 170 */ |
| 93 public void share(String linkUrl) {} | 171 public void share(String linkUrl) {} |
| 172 | |
| 173 /** | |
| 174 * Cancel Browser Actions notification. | |
| 175 */ | |
| 176 public static void cancelBrowserActionsNotification() { | |
| 177 NotificationManager notificationManager = | |
| 178 (NotificationManager) ContextUtils.getApplicationContext().getSy stemService( | |
| 179 Context.NOTIFICATION_SERVICE); | |
| 180 notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_BROWSER _ACTIONS); | |
| 181 ContextUtils.getAppSharedPreferences() | |
| 182 .edit() | |
| 183 .putBoolean( | |
| 184 BrowserActionsContextMenuItemDelegate.PREF_HAS_BROWSER_A CTIONS_NOTIFICATION, | |
| 185 false) | |
| 186 .apply(); | |
| 187 } | |
| 94 } | 188 } |
| OLD | NEW |