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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionsTabCreationService.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
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.browseractions;
6
7 import android.app.NotificationManager;
8 import android.app.PendingIntent;
9 import android.app.Service;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.os.IBinder;
13 import android.text.TextUtils;
14
15 import org.chromium.base.ContextUtils;
16 import org.chromium.chrome.R;
17 import org.chromium.chrome.browser.document.ChromeLauncherActivity;
18 import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder;
19 import org.chromium.chrome.browser.notifications.NotificationBuilderFactory;
20 import org.chromium.chrome.browser.notifications.NotificationConstants;
21 import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
22 import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions;
23 import org.chromium.chrome.browser.tab.Tab;
24 import org.chromium.chrome.browser.util.IntentUtils;
25
26 /**
27 * Service responsible for creating background tabs and notifications for Browse r Actions.
28 */
29 public class BrowserActionsTabCreationService extends Service {
Yusuf 2017/07/19 00:13:00 BrowserActionsService is cleaner I think.
ltian 2017/08/07 23:24:11 Done.
30 public static final String ACTION_TAB_CREATION_START =
Yusuf 2017/07/19 00:13:00 Some name clarification here: TAB_CREATION_START
ltian 2017/08/07 23:24:11 Yes, TAB_CREATION_START means start creating a tab
31 "org.chromium.chrome.browser.browseractions.ACTION_TAB_CREATION_STAR T";
32 public static final String ACTION_TAB_CREATION_UPDATE =
33 "org.chromium.chrome.browser.browseractions.ACTION_TAB_CREATION_UPDA TE";
34 public static final String ACTION_TAB_CREATION_FINISH =
35 "org.chromium.chrome.browser.browseractions.ACTION_TAB_CREATION_FINI SH";
36
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 swit cher for
48 * multiple urls.
49 */
50 public static final String EXTRA_IS_SINGLE_URL =
51 "org.chromium.chrome.browser.browseractions.is_single_url";
52
53 private int mTitleResId = R.string.browser_actions_single_link_open_notifica tion_title;
54
55 @Override
56 public IBinder onBind(Intent intent) {
57 return null;
58 }
59
60 @Override
61 public int onStartCommand(Intent intent, int flags, int startId) {
Yusuf 2017/07/19 00:13:00 Note that onStartCommand is on the main thread and
ltian 2017/08/07 23:24:11 The problem for IntentService is it is not a foreg
62 if (TextUtils.equals(intent.getAction(), ACTION_TAB_CREATION_START)) {
63 sendBrowserActionsNotification(Tab.INVALID_TAB_ID);
64 ContextUtils.getAppSharedPreferences()
65 .edit()
66 .putBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, true)
67 .apply();
68 NotificationUmaTracker.getInstance().onNotificationShown(
69 NotificationUmaTracker.BROWSER_ACTIONS, ChannelDefinitions.C HANNEL_ID_BROWSER);
70 } else if (TextUtils.equals(intent.getAction(), ACTION_TAB_CREATION_UPDA TE)) {
71 int tabId = IntentUtils.safeGetIntExtra(
72 intent, BrowserActionsContextMenuItemDelegate.EXTRA_TAB_ID, Tab.INVALID_TAB_ID);
73 sendBrowserActionsNotification(tabId);
74 } else if (TextUtils.equals(intent.getAction(), ACTION_TAB_CREATION_FINI SH)) {
75 stopForeground(false);
76 }
77 // The service will not be restarted if Chrome get killed.
78 return START_NOT_STICKY;
79 }
80
81 private void sendBrowserActionsNotification(int tabId) {
82 ChromeNotificationBuilder builder = createNotificationBuilder(tabId);
83 startForeground(NotificationConstants.NOTIFICATION_ID_BROWSER_ACTIONS, b uilder.build());
84 }
85
86 private ChromeNotificationBuilder createNotificationBuilder(int tabId) {
87 ChromeNotificationBuilder builder =
88 NotificationBuilderFactory
89 .createChromeNotificationBuilder(
90 true /* preferCompat */, ChannelDefinitions.CHAN NEL_ID_BROWSER)
91 .setSmallIcon(R.drawable.infobar_chrome)
92 .setLocalOnly(true)
93 .setAutoCancel(true)
94 .setContentText(this.getString(R.string.browser_actions_ notification_text));
95 if (tabId == Tab.INVALID_TAB_ID) {
96 mTitleResId = hasBrowserActionsNotification()
97 ? R.string.browser_actions_multi_links_open_notification_tit le
98 : R.string.browser_actions_single_link_open_notification_tit le;
99 }
100 builder.setContentTitle(this.getString(mTitleResId));
101 Intent intent = buildNotificationIntent(tabId);
102 PendingIntent notifyPendingIntent =
103 PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UP DATE_CURRENT);
104 builder.setContentIntent(notifyPendingIntent);
105 return builder;
106 }
107
108 private Intent buildNotificationIntent(int tabId) {
109 boolean multipleUrls = hasBrowserActionsNotification();
110 if (!multipleUrls && tabId != Tab.INVALID_TAB_ID) {
111 return Tab.createBringTabToFrontIntent(tabId);
112 }
113 Intent intent = new Intent(this, ChromeLauncherActivity.class);
114 intent.setAction(ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND);
115 intent.putExtra(EXTRA_IS_SINGLE_URL, !multipleUrls);
116 return intent;
117 }
118
119 private boolean hasBrowserActionsNotification() {
120 return ContextUtils.getAppSharedPreferences().getBoolean(
121 PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, false);
122 }
123
124 /**
125 * Cancel Browser Actions notification.
126 */
127 public static void cancelBrowserActionsNotification() {
128 NotificationManager notificationManager =
129 (NotificationManager) ContextUtils.getApplicationContext().getSy stemService(
130 Context.NOTIFICATION_SERVICE);
131 notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_BROWSER _ACTIONS);
132 ContextUtils.getAppSharedPreferences()
133 .edit()
134 .putBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, false)
135 .apply();
136 }
137
138 /**
139 * Checks whether Chrome should display tab switcher via Browser Actions Int ent.
140 * @param intent The intent to open the Chrome.
141 * @param isOverviewVisible Whether tab switcher is shown.
142 */
143 public static boolean toggleOverviewByBrowserActions(Intent intent, boolean isOverviewVisible) {
144 boolean fromBrowserActions = isStartedByBrowserActions(intent);
145 boolean isSingleUrl = IntentUtils.safeGetBooleanExtra(intent, EXTRA_IS_S INGLE_URL, false);
146 if (fromBrowserActions) {
147 return isSingleUrl == isOverviewVisible;
148 }
149 return false;
150 }
151
152 private static boolean isStartedByBrowserActions(Intent intent) {
153 if (ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND.equals(intent.getAction()) ) {
154 return true;
155 }
156 return false;
157 }
158
159 /**
160 * Returns {@link Intent} to update the notification and the status of the s ervice.
161 * @param action The actions of the Intent.
162 */
163 public static Intent getTabCreationIntent(String action) {
Yusuf 2017/07/19 00:13:00 this can handle all that is necessary in itself, t
ltian 2017/08/07 23:24:11 Done.
164 Intent intent = new Intent(
165 ContextUtils.getApplicationContext(), BrowserActionsTabCreationS ervice.class);
166 intent.setAction(action);
167 return intent;
168 }
169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698