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 | |
Yusuf
2017/05/30 17:57:15
switcher
ltian
2017/05/30 19:19:49
Done.
| |
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 int titleResId = hasBrowserActionsNotification() | |
77 ? R.string.browser_actions_multi_links_open_notification_title | |
78 : R.string.browser_actions_single_link_open_notification_title; | |
79 builder.setContentTitle(mContext.getString(titleResId)); | |
80 Intent intent = buildNotificationIntent(); | |
81 PendingIntent notifyPendingIntent = | |
82 PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLA G_UPDATE_CURRENT); | |
83 builder.setContentIntent(notifyPendingIntent); | |
84 return builder; | |
85 } | |
86 | |
87 private Intent buildNotificationIntent() { | |
88 Intent intent = new Intent(mContext, ChromeLauncherActivity.class); | |
89 intent.setAction(ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND); | |
90 intent.putExtra(EXTRA_IS_SINGLE_URL, !hasBrowserActionsNotification()); | |
91 return intent; | |
92 } | |
93 | |
94 private boolean hasBrowserActionsNotification() { | |
95 return mSharedPreferences.getBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATI ON, false); | |
96 } | |
30 | 97 |
31 /** | 98 /** |
32 * Builds a {@link BrowserActionsContextMenuItemDelegate} instance. | 99 * Builds a {@link BrowserActionsContextMenuItemDelegate} instance. |
33 */ | 100 */ |
34 public BrowserActionsContextMenuItemDelegate() { | 101 public BrowserActionsContextMenuItemDelegate() { |
35 mContext = ContextUtils.getApplicationContext(); | 102 mContext = ContextUtils.getApplicationContext(); |
103 mNotificationManager = | |
104 (NotificationManager) mContext.getSystemService(Context.NOTIFICA TION_SERVICE); | |
105 mSharedPreferences = ContextUtils.getAppSharedPreferences(); | |
36 } | 106 } |
37 | 107 |
38 /** | 108 /** |
39 * Called when the {@code text} should be saved to the clipboard. | 109 * Called when the {@code text} should be saved to the clipboard. |
40 * @param text The text to save to the clipboard. | 110 * @param text The text to save to the clipboard. |
41 */ | 111 */ |
42 public void onSaveToClipboard(String text) { | 112 public void onSaveToClipboard(String text) { |
43 ClipboardManager clipboardManager = | 113 ClipboardManager clipboardManager = |
44 (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_S ERVICE); | 114 (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_S ERVICE); |
45 ClipData data = ClipData.newPlainText("url", text); | 115 ClipData data = ClipData.newPlainText("url", text); |
(...skipping 13 matching lines...) Expand all Loading... | |
59 intent.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName()) ; | 129 intent.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName()) ; |
60 IntentHandler.addTrustedIntentExtras(intent); | 130 IntentHandler.addTrustedIntentExtras(intent); |
61 IntentHandler.setTabLaunchType(intent, TabLaunchType.FROM_EXTERNAL_APP); | 131 IntentHandler.setTabLaunchType(intent, TabLaunchType.FROM_EXTERNAL_APP); |
62 IntentUtils.safeStartActivity(mContext, intent); | 132 IntentUtils.safeStartActivity(mContext, intent); |
63 } | 133 } |
64 | 134 |
65 /** | 135 /** |
66 * Called when the {@code linkUrl} should be opened in Chrome in the backgro und. | 136 * Called when the {@code linkUrl} should be opened in Chrome in the backgro und. |
67 * @param linkUrl The url to open. | 137 * @param linkUrl The url to open. |
68 */ | 138 */ |
69 public void onOpenInBackground(String linkUrl) {} | 139 public void onOpenInBackground(String linkUrl) { |
140 sendBrowserActionsNotification(); | |
141 Toast.makeText(mContext, R.string.browser_actions_open_in_background_toa st_message, | |
142 Toast.LENGTH_SHORT) | |
143 .show(); | |
144 } | |
70 | 145 |
71 /** | 146 /** |
72 * Called when a custom item of Browser action menu is selected. | 147 * Called when a custom item of Browser action menu is selected. |
73 * @param action The PendingIntent action to be launched. | 148 * @param action The PendingIntent action to be launched. |
74 */ | 149 */ |
75 public void onCustomItemSelected(PendingIntent action) { | 150 public void onCustomItemSelected(PendingIntent action) { |
76 try { | 151 try { |
77 action.send(); | 152 action.send(); |
78 } catch (CanceledException e) { | 153 } catch (CanceledException e) { |
79 Log.e(TAG, "Browser Action in Chrome failed to send pending intent." ); | 154 Log.e(TAG, "Browser Action in Chrome failed to send pending intent." ); |
80 } | 155 } |
81 } | 156 } |
82 | 157 |
83 /** | 158 /** |
84 * Called when the page of the {@code linkUrl} should be downloaded. | 159 * Called when the page of the {@code linkUrl} should be downloaded. |
85 * @param linkUrl The url of the page to download. | 160 * @param linkUrl The url of the page to download. |
86 */ | 161 */ |
87 public void startDownload(String linkUrl) {} | 162 public void startDownload(String linkUrl) {} |
88 | 163 |
89 /** | 164 /** |
90 * Called when the {@code linkUrl} should be shared. | 165 * Called when the {@code linkUrl} should be shared. |
91 * @param linkUrl The url to share. | 166 * @param linkUrl The url to share. |
92 */ | 167 */ |
93 public void share(String linkUrl) {} | 168 public void share(String linkUrl) {} |
169 | |
170 /** | |
171 * Cancel Browser Actions notification. | |
172 */ | |
173 public static void cancelBrowserActionsNotification() { | |
174 NotificationManager notificationManager = | |
175 (NotificationManager) ContextUtils.getApplicationContext().getSy stemService( | |
176 Context.NOTIFICATION_SERVICE); | |
177 notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_BROWSER _ACTIONS); | |
178 ContextUtils.getAppSharedPreferences() | |
179 .edit() | |
180 .putBoolean( | |
181 BrowserActionsContextMenuItemDelegate.PREF_HAS_BROWSER_A CTIONS_NOTIFICATION, | |
182 false) | |
183 .apply(); | |
184 } | |
94 } | 185 } |
OLD | NEW |