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.annotation.TargetApi; | |
| 7 import android.app.Activity; | 8 import android.app.Activity; |
| 9 import android.app.NotificationManager; | |
| 8 import android.app.PendingIntent; | 10 import android.app.PendingIntent; |
| 9 import android.app.PendingIntent.CanceledException; | 11 import android.app.PendingIntent.CanceledException; |
| 10 import android.content.ClipData; | 12 import android.content.ClipData; |
| 11 import android.content.ClipboardManager; | 13 import android.content.ClipboardManager; |
| 12 import android.content.Context; | 14 import android.content.Context; |
| 13 import android.content.Intent; | 15 import android.content.Intent; |
| 14 import android.net.Uri; | 16 import android.net.Uri; |
| 17 import android.os.Build; | |
| 15 import android.provider.Browser; | 18 import android.provider.Browser; |
| 19 import android.service.notification.StatusBarNotification; | |
| 16 | 20 |
| 17 import org.chromium.base.ContextUtils; | 21 import org.chromium.base.ContextUtils; |
| 18 import org.chromium.base.Log; | 22 import org.chromium.base.Log; |
| 23 import org.chromium.chrome.R; | |
| 24 import org.chromium.chrome.browser.ChromeTabbedActivity; | |
| 19 import org.chromium.chrome.browser.IntentHandler; | 25 import org.chromium.chrome.browser.IntentHandler; |
| 20 import org.chromium.chrome.browser.document.ChromeLauncherActivity; | 26 import org.chromium.chrome.browser.document.ChromeLauncherActivity; |
| 27 import org.chromium.chrome.browser.notifications.ChannelDefinitions; | |
| 28 import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder; | |
| 29 import org.chromium.chrome.browser.notifications.NotificationBuilderFactory; | |
| 30 import org.chromium.chrome.browser.notifications.NotificationConstants; | |
| 31 import org.chromium.chrome.browser.notifications.NotificationUmaTracker; | |
| 21 import org.chromium.chrome.browser.util.IntentUtils; | 32 import org.chromium.chrome.browser.util.IntentUtils; |
| 33 import org.chromium.ui.widget.Toast; | |
| 22 | 34 |
| 23 /** | 35 /** |
| 24 * A delegate responsible for taking actions based on browser action context men u selections. | 36 * A delegate responsible for taking actions based on browser action context men u selections. |
| 25 */ | 37 */ |
| 26 public class BrowserActionsContextMenuItemDelegate { | 38 public class BrowserActionsContextMenuItemDelegate { |
| 27 private static final String TAG = "BrowserActionsItem"; | 39 private static final String TAG = "BrowserActionsItem"; |
| 40 /** | |
| 41 * Action to request open ChromeTabbedActivity in tab switcher mode. | |
| 42 */ | |
| 43 public static final String ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND = | |
| 44 "org.chromium.chrome.browser.browseractions.browser_action_open_in_b ackground"; | |
| 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; |
| 30 private final Activity mActivity; | 54 private final Activity mActivity; |
| 55 private final NotificationManager mNotificationManager; | |
| 56 | |
| 57 private void sendBrowserActionsNotification(String linkUrl) { | |
| 58 Intent intent = new Intent(mContext, ChromeTabbedActivity.class); | |
|
David Trainor- moved to gerrit
2017/05/17 16:48:02
ChromeLauncherActivity?
ltian
2017/05/19 21:02:47
Done.
| |
| 59 intent.setAction(ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND); | |
| 60 intent.putExtra(EXTRA_IS_SINGLE_URL, true); | |
|
David Trainor- moved to gerrit
2017/05/17 16:48:02
Can we just pull isSingleUrl() out to a helper tha
ltian
2017/05/19 21:02:47
Talk with Sam and he is ok to display notification
| |
| 61 ChromeNotificationBuilder builder = | |
| 62 NotificationBuilderFactory | |
| 63 .createChromeNotificationBuilder( | |
| 64 true /* preferCompat */, ChannelDefinitions.CHAN NEL_ID_BROWSER) | |
| 65 .setSmallIcon(R.drawable.infobar_chrome) | |
| 66 .setLocalOnly(true) | |
| 67 .setAutoCancel(true) | |
| 68 .setContentText( | |
| 69 mContext.getString(R.string.browser_actions_noti fication_text)) | |
| 70 .setContentTitle(mContext.getString( | |
| 71 R.string.browser_actions_single_link_open_notifi cation_title)); | |
| 72 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && hasBrowserActionsN otification()) { | |
| 73 builder.setContentTitle(mContext.getString( | |
| 74 R.string.browser_actions_multi_links_open_notification_title )); | |
| 75 intent.putExtra(EXTRA_IS_SINGLE_URL, false); | |
| 76 } | |
| 77 PendingIntent notifyPendingIntent = | |
| 78 PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLA G_UPDATE_CURRENT); | |
| 79 builder.setContentIntent(notifyPendingIntent); | |
| 80 mNotificationManager.notify( | |
| 81 NotificationConstants.NOTIFICATION_ID_BROWSER_ACTIONS, builder.b uild()); | |
| 82 NotificationUmaTracker.getInstance().onNotificationShown( | |
| 83 NotificationUmaTracker.BROWSER_ACTIONS, ChannelDefinitions.CHANN EL_ID_BROWSER); | |
| 84 } | |
| 85 | |
| 86 @TargetApi(Build.VERSION_CODES.M) | |
| 87 private boolean hasBrowserActionsNotification() { | |
| 88 StatusBarNotification[] notifications = mNotificationManager.getActiveNo tifications(); | |
| 89 for (StatusBarNotification notification : notifications) { | |
| 90 if (notification.getId() == NotificationConstants.NOTIFICATION_ID_BR OWSER_ACTIONS) { | |
| 91 return true; | |
| 92 } | |
| 93 } | |
| 94 return false; | |
| 95 } | |
| 31 | 96 |
| 32 /** | 97 /** |
| 33 * Builds a {@link BrowserActionsContextMenuItemDelegate} instance. | 98 * Builds a {@link BrowserActionsContextMenuItemDelegate} instance. |
| 34 */ | 99 */ |
| 35 public BrowserActionsContextMenuItemDelegate(Activity activity) { | 100 public BrowserActionsContextMenuItemDelegate(Activity activity) { |
| 36 mContext = ContextUtils.getApplicationContext(); | 101 mContext = ContextUtils.getApplicationContext(); |
| 37 mActivity = activity; | 102 mActivity = activity; |
| 103 mNotificationManager = | |
| 104 (NotificationManager) mContext.getSystemService(Context.NOTIFICA TION_SERVICE); | |
| 38 } | 105 } |
| 39 | 106 |
| 40 /** | 107 /** |
| 41 * Called when the {@code text} should be saved to the clipboard. | 108 * Called when the {@code text} should be saved to the clipboard. |
| 42 * @param text The text to save to the clipboard. | 109 * @param text The text to save to the clipboard. |
| 43 */ | 110 */ |
| 44 public void onSaveToClipboard(String text) { | 111 public void onSaveToClipboard(String text) { |
| 45 ClipboardManager clipboardManager = | 112 ClipboardManager clipboardManager = |
| 46 (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_S ERVICE); | 113 (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_S ERVICE); |
| 47 ClipData data = ClipData.newPlainText("url", text); | 114 ClipData data = ClipData.newPlainText("url", text); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 61 intent.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName()) ; | 128 intent.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName()) ; |
| 62 IntentHandler.addTrustedIntentExtras(intent); | 129 IntentHandler.addTrustedIntentExtras(intent); |
| 63 IntentHandler.setOpenIncognitoFromExternal(intent); | 130 IntentHandler.setOpenIncognitoFromExternal(intent); |
| 64 IntentUtils.safeStartActivity(mContext, intent); | 131 IntentUtils.safeStartActivity(mContext, intent); |
| 65 } | 132 } |
| 66 | 133 |
| 67 /** | 134 /** |
| 68 * Called when the {@code linkUrl} should be opened in Chrome in the backgro und. | 135 * Called when the {@code linkUrl} should be opened in Chrome in the backgro und. |
| 69 * @param linkUrl The url to open. | 136 * @param linkUrl The url to open. |
| 70 */ | 137 */ |
| 71 public void onOpenInBackground(String linkUrl) {} | 138 public void onOpenInBackground(String linkUrl) { |
| 139 sendBrowserActionsNotification(linkUrl); | |
| 140 Toast.makeText(mContext, R.string.browser_actions_open_in_background_toa st_message, | |
| 141 Toast.LENGTH_SHORT) | |
| 142 .show(); | |
| 143 } | |
| 72 | 144 |
| 73 /** | 145 /** |
| 74 * Called when a custom item of Browser action menu is selected. | 146 * Called when a custom item of Browser action menu is selected. |
| 75 * @param action The PendingIntent action to be launched. | 147 * @param action The PendingIntent action to be launched. |
| 76 */ | 148 */ |
| 77 public void onCustomItemSelected(PendingIntent action) { | 149 public void onCustomItemSelected(PendingIntent action) { |
| 78 try { | 150 try { |
| 79 action.send(); | 151 action.send(); |
| 80 } catch (CanceledException e) { | 152 } catch (CanceledException e) { |
| 81 Log.e(TAG, "Browser Action in Chrome failed to send pending intent." ); | 153 Log.e(TAG, "Browser Action in Chrome failed to send pending intent." ); |
| 82 } | 154 } |
| 83 } | 155 } |
| 84 | 156 |
| 85 /** | 157 /** |
| 86 * Called when the page of the {@code linkUrl} should be downloaded. | 158 * Called when the page of the {@code linkUrl} should be downloaded. |
| 87 * @param linkUrl The url of the page to download. | 159 * @param linkUrl The url of the page to download. |
| 88 */ | 160 */ |
| 89 public void startDownload(String linkUrl) {} | 161 public void startDownload(String linkUrl) {} |
| 90 | 162 |
| 91 /** | 163 /** |
| 92 * Called when the {@code linkUrl} should be shared. | 164 * Called when the {@code linkUrl} should be shared. |
| 93 * @param linkUrl The url to share. | 165 * @param linkUrl The url to share. |
| 94 */ | 166 */ |
| 95 public void share(String linkUrl) {} | 167 public void share(String linkUrl) {} |
| 96 } | 168 } |
| OLD | NEW |