Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(548)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsContextMenuItemDelegate.java

Issue 2984453002: Add Browser Actions tab model selector and open a tab through it if ChromeTabbedActivity is not ava…
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.Activity; 7 import android.app.Activity;
8 import android.app.NotificationManager;
9 import android.app.PendingIntent; 8 import android.app.PendingIntent;
10 import android.app.PendingIntent.CanceledException; 9 import android.app.PendingIntent.CanceledException;
11 import android.content.ClipData; 10 import android.content.ClipData;
12 import android.content.ClipboardManager; 11 import android.content.ClipboardManager;
13 import android.content.Context; 12 import android.content.Context;
14 import android.content.Intent; 13 import android.content.Intent;
15 import android.content.SharedPreferences;
16 import android.net.Uri; 14 import android.net.Uri;
17 import android.provider.Browser; 15 import android.provider.Browser;
18 16
19 import org.chromium.base.ApplicationStatus; 17 import org.chromium.base.ApplicationStatus;
20 import org.chromium.base.ContextUtils;
21 import org.chromium.base.Log; 18 import org.chromium.base.Log;
22 import org.chromium.chrome.R; 19 import org.chromium.chrome.R;
23 import org.chromium.chrome.browser.ChromeTabbedActivity; 20 import org.chromium.chrome.browser.ChromeTabbedActivity;
24 import org.chromium.chrome.browser.IntentHandler; 21 import org.chromium.chrome.browser.IntentHandler;
22 import org.chromium.chrome.browser.browseractions.BrowserActionsTabCreatorManage r.BrowserActionsTabCreator;
25 import org.chromium.chrome.browser.document.ChromeLauncherActivity; 23 import org.chromium.chrome.browser.document.ChromeLauncherActivity;
26 import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder;
27 import org.chromium.chrome.browser.notifications.NotificationBuilderFactory;
28 import org.chromium.chrome.browser.notifications.NotificationConstants;
29 import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
30 import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions;
31 import org.chromium.chrome.browser.share.ShareHelper; 24 import org.chromium.chrome.browser.share.ShareHelper;
32 import org.chromium.chrome.browser.share.ShareParams; 25 import org.chromium.chrome.browser.share.ShareParams;
33 import org.chromium.chrome.browser.tab.Tab; 26 import org.chromium.chrome.browser.tab.Tab;
34 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; 27 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType;
35 import org.chromium.chrome.browser.util.IntentUtils; 28 import org.chromium.chrome.browser.util.IntentUtils;
36 import org.chromium.content_public.browser.LoadUrlParams; 29 import org.chromium.content_public.browser.LoadUrlParams;
37 import org.chromium.content_public.common.Referrer; 30 import org.chromium.content_public.common.Referrer;
38 import org.chromium.ui.widget.Toast; 31 import org.chromium.ui.widget.Toast;
39 32
40 import java.lang.ref.WeakReference; 33 import java.lang.ref.WeakReference;
41 34
42 /** 35 /**
43 * 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.
44 */ 37 */
45 public class BrowserActionsContextMenuItemDelegate { 38 public class BrowserActionsContextMenuItemDelegate {
46 private static final String TAG = "BrowserActionsItem"; 39 private static final String TAG = "BrowserActionsItem";
47 /**
48 * Action to request open ChromeTabbedActivity in tab switcher mode.
49 */
50 public static final String ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND =
51 "org.chromium.chrome.browser.browseractions.browser_action_open_in_b ackground";
52 40
53 public static final String PREF_HAS_BROWSER_ACTIONS_NOTIFICATION = 41 public static final String EXTRA_TAB_ID = "org.chromium.chrome.browser.brows eractions.TAB_ID";
54 "org.chromium.chrome.browser.browseractions.HAS_BROWSER_ACTIONS_NOTI FICATION";
55
56 /**
57 * Extra that indicates whether to show a Tab for single url or the tab swit cher for
58 * multiple urls.
59 */
60 public static final String EXTRA_IS_SINGLE_URL =
61 "org.chromium.chrome.browser.browseractions.is_single_url";
62 42
63 private final Activity mActivity; 43 private final Activity mActivity;
64 private final NotificationManager mNotificationManager;
65 private final SharedPreferences mSharedPreferences;
66 private final String mSourcePackageName; 44 private final String mSourcePackageName;
45 private final BrowserActionsTabCreatorManager mTabCreatorManager;
67 46
68 /** 47 /**
69 * Builds a {@link BrowserActionsContextMenuItemDelegate} instance. 48 * Builds a {@link BrowserActionsContextMenuItemDelegate} instance.
70 * @param activity The activity displays the context menu. 49 * @param activity The activity displays the context menu.
71 * @param sourcePackageName The package name of the app which requests the B rowser Actions. 50 * @param sourcePackageName The package name of the app which requests the B rowser Actions.
72 */ 51 */
73 public BrowserActionsContextMenuItemDelegate(Activity activity, String sourc ePackageName) { 52 public BrowserActionsContextMenuItemDelegate(Activity activity, String sourc ePackageName) {
74 mActivity = activity; 53 mActivity = activity;
75 mNotificationManager =
76 (NotificationManager) activity.getSystemService(Context.NOTIFICA TION_SERVICE);
77 mSharedPreferences = ContextUtils.getAppSharedPreferences();
78 mSourcePackageName = sourcePackageName; 54 mSourcePackageName = sourcePackageName;
79 } 55 mTabCreatorManager = new BrowserActionsTabCreatorManager();
80
81 private void sendBrowserActionsNotification(int tabId) {
82 ChromeNotificationBuilder builder = createNotificationBuilder(tabId);
83 mNotificationManager.notify(
84 NotificationConstants.NOTIFICATION_ID_BROWSER_ACTIONS, builder.b uild());
85 mSharedPreferences.edit().putBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATI ON, true).apply();
86 NotificationUmaTracker.getInstance().onNotificationShown(
87 NotificationUmaTracker.BROWSER_ACTIONS, ChannelDefinitions.CHANN EL_ID_BROWSER);
88 }
89
90 private ChromeNotificationBuilder createNotificationBuilder(int tabId) {
91 ChromeNotificationBuilder builder =
92 NotificationBuilderFactory
93 .createChromeNotificationBuilder(
94 true /* preferCompat */, ChannelDefinitions.CHAN NEL_ID_BROWSER)
95 .setSmallIcon(R.drawable.infobar_chrome)
96 .setLocalOnly(true)
97 .setAutoCancel(true)
98 .setContentText(
99 mActivity.getString(R.string.browser_actions_not ification_text));
100 int titleResId = hasBrowserActionsNotification()
101 ? R.string.browser_actions_multi_links_open_notification_title
102 : R.string.browser_actions_single_link_open_notification_title;
103 builder.setContentTitle(mActivity.getString(titleResId));
104 Intent intent = buildNotificationIntent(tabId);
105 PendingIntent notifyPendingIntent =
106 PendingIntent.getActivity(mActivity, 0, intent, PendingIntent.FL AG_UPDATE_CURRENT);
107 builder.setContentIntent(notifyPendingIntent);
108 return builder;
109 }
110
111 private Intent buildNotificationIntent(int tabId) {
112 boolean multipleUrls = hasBrowserActionsNotification();
113 if (!multipleUrls && tabId != Tab.INVALID_TAB_ID) {
114 return Tab.createBringTabToFrontIntent(tabId);
115 }
116 Intent intent = new Intent(mActivity, ChromeLauncherActivity.class);
117 intent.setAction(ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND);
118 intent.putExtra(EXTRA_IS_SINGLE_URL, !multipleUrls);
119 return intent;
120 }
121
122 private boolean hasBrowserActionsNotification() {
123 return mSharedPreferences.getBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATI ON, false);
124 } 56 }
125 57
126 private int openTabInBackground(String linkUrl) { 58 private int openTabInBackground(String linkUrl) {
127 int tabId = Tab.INVALID_TAB_ID;
128 Referrer referrer = IntentHandler.constructValidReferrerForAuthority(mSo urcePackageName); 59 Referrer referrer = IntentHandler.constructValidReferrerForAuthority(mSo urcePackageName);
129 LoadUrlParams loadUrlParams = new LoadUrlParams(linkUrl); 60 LoadUrlParams loadUrlParams = new LoadUrlParams(linkUrl);
130 loadUrlParams.setReferrer(referrer); 61 loadUrlParams.setReferrer(referrer);
131 for (WeakReference<Activity> ref : ApplicationStatus.getRunningActivitie s()) { 62 for (WeakReference<Activity> ref : ApplicationStatus.getRunningActivitie s()) {
132 if (!(ref.get() instanceof ChromeTabbedActivity)) continue; 63 if (!(ref.get() instanceof ChromeTabbedActivity)) continue;
133 64
134 ChromeTabbedActivity activity = (ChromeTabbedActivity) ref.get(); 65 ChromeTabbedActivity activity = (ChromeTabbedActivity) ref.get();
135 if (activity == null) continue; 66 if (activity == null) continue;
136 if (activity.getTabModelSelector() != null) { 67 if (activity.getTabModelSelector() != null) {
137 Tab tab = activity.getTabModelSelector().openNewTab( 68 Tab tab = activity.getTabModelSelector().openNewTab(
138 loadUrlParams, TabLaunchType.FROM_BROWSER_ACTIONS, null, false); 69 loadUrlParams, TabLaunchType.FROM_BROWSER_ACTIONS, null, false);
139 if (tab != null) { 70 if (tab != null) {
140 tabId = tab.getId(); 71 return tab.getId();
141 } 72 }
142 } 73 }
143 } 74 }
144 return tabId; 75 BrowsreActionsTabModelSelector selector =
76 BrowsreActionsTabModelSelector.getInstance(mActivity, mTabCreato rManager);
77 if (!selector.isActiveState()) {
78 selector.initializeSelector();
79 ((BrowserActionsTabCreator) mTabCreatorManager.getTabCreator(false))
80 .setTabModel(selector.getCurrentModel());
81 selector.loadState(true);
82 selector.restoreTabs(false);
83 }
84 selector.openNewTab(loadUrlParams, TabLaunchType.FROM_BROWSER_ACTIONS, n ull, false);
85 return Tab.INVALID_TAB_ID;
145 } 86 }
146 87
147 /** 88 /**
148 * Called when the {@code text} should be saved to the clipboard. 89 * Called when the {@code text} should be saved to the clipboard.
149 * @param text The text to save to the clipboard. 90 * @param text The text to save to the clipboard.
150 */ 91 */
151 public void onSaveToClipboard(String text) { 92 public void onSaveToClipboard(String text) {
152 ClipboardManager clipboardManager = 93 ClipboardManager clipboardManager =
153 (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_ SERVICE); 94 (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_ SERVICE);
154 ClipData data = ClipData.newPlainText("url", text); 95 ClipData data = ClipData.newPlainText("url", text);
(...skipping 14 matching lines...) Expand all
169 IntentHandler.addTrustedIntentExtras(intent); 110 IntentHandler.addTrustedIntentExtras(intent);
170 IntentHandler.setTabLaunchType(intent, TabLaunchType.FROM_EXTERNAL_APP); 111 IntentHandler.setTabLaunchType(intent, TabLaunchType.FROM_EXTERNAL_APP);
171 IntentUtils.safeStartActivity(mActivity, intent); 112 IntentUtils.safeStartActivity(mActivity, intent);
172 } 113 }
173 114
174 /** 115 /**
175 * Called when the {@code linkUrl} should be opened in Chrome in the backgro und. 116 * Called when the {@code linkUrl} should be opened in Chrome in the backgro und.
176 * @param linkUrl The url to open. 117 * @param linkUrl The url to open.
177 */ 118 */
178 public void onOpenInBackground(String linkUrl) { 119 public void onOpenInBackground(String linkUrl) {
120 Intent startIntent = BrowserActionsTabCreationService.getTabCreationInte nt(
121 BrowserActionsTabCreationService.ACTION_TAB_CREATION_START);
122 mActivity.startService(startIntent);
123
179 int tabId = openTabInBackground(linkUrl); 124 int tabId = openTabInBackground(linkUrl);
180 if (tabId != Tab.INVALID_TAB_ID) { 125 if (tabId != Tab.INVALID_TAB_ID) {
181 sendBrowserActionsNotification(tabId); 126 Intent updateIntent = BrowserActionsTabCreationService.getTabCreatio nIntent(
182 Toast.makeText(mActivity, R.string.browser_actions_open_in_backgroun d_toast_message, 127 BrowserActionsTabCreationService.ACTION_TAB_CREATION_UPDATE) ;
183 Toast.LENGTH_SHORT) 128 updateIntent.putExtra(EXTRA_TAB_ID, tabId);
184 .show(); 129 mActivity.startService(updateIntent);
185 } else {
186 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkUrl));
187 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
188 intent.setClass(mActivity, ChromeLauncherActivity.class);
189 intent.putExtra(ChromeLauncherActivity.EXTRA_IS_ALLOWED_TO_RETURN_TO _PARENT, false);
190 IntentUtils.safeStartActivity(mActivity, intent);
191 } 130 }
131 Toast.makeText(mActivity, R.string.browser_actions_open_in_background_to ast_message,
132 Toast.LENGTH_SHORT)
133 .show();
192 } 134 }
193 135
194 /** 136 /**
195 * Called when a custom item of Browser action menu is selected. 137 * Called when a custom item of Browser action menu is selected.
196 * @param action The PendingIntent action to be launched. 138 * @param action The PendingIntent action to be launched.
197 */ 139 */
198 public void onCustomItemSelected(PendingIntent action) { 140 public void onCustomItemSelected(PendingIntent action) {
199 try { 141 try {
200 action.send(); 142 action.send();
201 } catch (CanceledException e) { 143 } catch (CanceledException e) {
(...skipping 14 matching lines...) Expand all
216 */ 158 */
217 public void share(Boolean shareDirectly, String linkUrl) { 159 public void share(Boolean shareDirectly, String linkUrl) {
218 ShareParams params = new ShareParams.Builder(mActivity, linkUrl, linkUrl ) 160 ShareParams params = new ShareParams.Builder(mActivity, linkUrl, linkUrl )
219 .setShareDirectly(shareDirectly) 161 .setShareDirectly(shareDirectly)
220 .setSaveLastUsed(!shareDirectly) 162 .setSaveLastUsed(!shareDirectly)
221 .setSourcePackageName(mSourcePackageName) 163 .setSourcePackageName(mSourcePackageName)
222 .setIsExternalUrl(true) 164 .setIsExternalUrl(true)
223 .build(); 165 .build();
224 ShareHelper.share(params); 166 ShareHelper.share(params);
225 } 167 }
226
227 /**
228 * Cancel Browser Actions notification.
229 */
230 public static void cancelBrowserActionsNotification() {
231 NotificationManager notificationManager =
232 (NotificationManager) ContextUtils.getApplicationContext().getSy stemService(
233 Context.NOTIFICATION_SERVICE);
234 notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_BROWSER _ACTIONS);
235 ContextUtils.getAppSharedPreferences()
236 .edit()
237 .putBoolean(
238 BrowserActionsContextMenuItemDelegate.PREF_HAS_BROWSER_A CTIONS_NOTIFICATION,
239 false)
240 .apply();
241 }
242
243 /**
244 * Checks whether Chrome should display tab switcher via Browser Actions Int ent.
245 * @param intent The intent to open the Chrome.
246 * @param isOverviewVisible Whether tab switcher is shown.
247 */
248 public static boolean toggleOverviewByBrowserActions(Intent intent, boolean isOverviewVisible) {
249 boolean fromBrowserActions = isStartedByBrowserActions(intent);
250 boolean isSingleUrl = IntentUtils.safeGetBooleanExtra(
251 intent, BrowserActionsContextMenuItemDelegate.EXTRA_IS_SINGLE_UR L, false);
252 if (fromBrowserActions) {
253 return isSingleUrl == isOverviewVisible;
254 }
255 return false;
256 }
257
258 private static boolean isStartedByBrowserActions(Intent intent) {
259 if (BrowserActionsContextMenuItemDelegate.ACTION_BROWSER_ACTIONS_OPEN_IN _BACKGROUND.equals(
260 intent.getAction())) {
261 return true;
262 }
263 return false;
264 }
265 } 168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698