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

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

Issue 2984453002: Add Browser Actions tab model selector and open a tab through it if ChromeTabbedActivity is not ava…
Patch Set: Sync changes. Created 3 years, 4 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.base.VisibleForTesting;
17 import org.chromium.chrome.R;
18 import org.chromium.chrome.browser.document.ChromeLauncherActivity;
19 import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder;
20 import org.chromium.chrome.browser.notifications.NotificationBuilderFactory;
21 import org.chromium.chrome.browser.notifications.NotificationConstants;
22 import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
23 import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions;
24 import org.chromium.chrome.browser.tab.Tab;
25 import org.chromium.chrome.browser.util.IntentUtils;
26
27 /**
28 * Service responsible for creating background tabs and notifications for Browse r Actions.
29 */
30 public class BrowserActionsService extends Service {
31 public static final String ACTION_TAB_CREATION_START =
32 "org.chromium.chrome.browser.browseractions.ACTION_TAB_CREATION_STAR T";
33 public static final String ACTION_TAB_CREATION_UPDATE =
34 "org.chromium.chrome.browser.browseractions.ACTION_TAB_CREATION_UPDA TE";
35 public static final String ACTION_TAB_CREATION_FINISH =
36 "org.chromium.chrome.browser.browseractions.ACTION_TAB_CREATION_FINI SH";
37 public static final String EXTRA_TAB_ID = "org.chromium.chrome.browser.brows eractions.TAB_ID";
38
39 /**
40 * Action to request open ChromeTabbedActivity in tab switcher mode.
41 */
42 public static final String ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND =
43 "org.chromium.chrome.browser.browseractions.browser_action_open_in_b ackground";
44
45 public static final String PREF_HAS_BROWSER_ACTIONS_NOTIFICATION =
46 "org.chromium.chrome.browser.browseractions.HAS_BROWSER_ACTIONS_NOTI FICATION";
47
48 /**
49 * Extra that indicates whether to show a Tab for single url or the tab swit cher for
50 * multiple urls.
51 */
52 public static final String EXTRA_IS_SINGLE_URL =
53 "org.chromium.chrome.browser.browseractions.is_single_url";
54
55 private static Intent sNotificationIntent;
56
57 private int mTitleResId = R.string.browser_actions_single_link_open_notifica tion_title;
58
59 @Override
60 public IBinder onBind(Intent intent) {
61 return null;
62 }
63
64 @VisibleForTesting
65 static Intent getNotificationIntent() {
66 return sNotificationIntent;
67 }
68
69 @Override
70 public int onStartCommand(Intent intent, int flags, int startId) {
71 if (TextUtils.equals(intent.getAction(), ACTION_TAB_CREATION_START)) {
72 sendBrowserActionsNotification(Tab.INVALID_TAB_ID);
73 ContextUtils.getAppSharedPreferences()
74 .edit()
75 .putBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, true)
76 .apply();
77 NotificationUmaTracker.getInstance().onNotificationShown(
78 NotificationUmaTracker.BROWSER_ACTIONS, ChannelDefinitions.C HANNEL_ID_BROWSER);
79 } else if (TextUtils.equals(intent.getAction(), ACTION_TAB_CREATION_UPDA TE)) {
80 int tabId = IntentUtils.safeGetIntExtra(intent, EXTRA_TAB_ID, Tab.IN VALID_TAB_ID);
81 sendBrowserActionsNotification(tabId);
82 } else if (TextUtils.equals(intent.getAction(), ACTION_TAB_CREATION_FINI SH)) {
83 stopForeground(false);
84 }
85 // The service will not be restarted if Chrome get killed.
86 return START_NOT_STICKY;
87 }
88
89 private void sendBrowserActionsNotification(int tabId) {
90 ChromeNotificationBuilder builder = createNotificationBuilder(tabId);
91 startForeground(NotificationConstants.NOTIFICATION_ID_BROWSER_ACTIONS, b uilder.build());
92 }
93
94 private ChromeNotificationBuilder createNotificationBuilder(int tabId) {
95 ChromeNotificationBuilder builder =
96 NotificationBuilderFactory
97 .createChromeNotificationBuilder(
98 true /* preferCompat */, ChannelDefinitions.CHAN NEL_ID_BROWSER)
99 .setSmallIcon(R.drawable.infobar_chrome)
100 .setLocalOnly(true)
101 .setAutoCancel(true)
102 .setContentText(this.getString(R.string.browser_actions_ notification_text));
103 if (tabId == Tab.INVALID_TAB_ID) {
104 mTitleResId = hasBrowserActionsNotification()
105 ? R.string.browser_actions_multi_links_open_notification_tit le
106 : R.string.browser_actions_single_link_open_notification_tit le;
107 }
108 builder.setContentTitle(this.getString(mTitleResId));
109 sNotificationIntent = buildNotificationIntent(tabId);
110 PendingIntent notifyPendingIntent = PendingIntent.getActivity(
111 this, 0, sNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;
112 builder.setContentIntent(notifyPendingIntent);
113 return builder;
114 }
115
116 private Intent buildNotificationIntent(int tabId) {
117 boolean multipleUrls = hasBrowserActionsNotification();
118 if (!multipleUrls && tabId != Tab.INVALID_TAB_ID) {
119 return Tab.createBringTabToFrontIntent(tabId);
120 }
121 Intent intent = new Intent(this, ChromeLauncherActivity.class);
122 intent.setAction(ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND);
123 intent.putExtra(EXTRA_IS_SINGLE_URL, !multipleUrls);
124 return intent;
125 }
126
127 @VisibleForTesting
128 static boolean hasBrowserActionsNotification() {
129 return ContextUtils.getAppSharedPreferences().getBoolean(
130 PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, false);
131 }
132
133 /**
134 * Cancel Browser Actions notification.
135 */
136 public static void cancelBrowserActionsNotification() {
137 NotificationManager notificationManager =
138 (NotificationManager) ContextUtils.getApplicationContext().getSy stemService(
139 Context.NOTIFICATION_SERVICE);
140 notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_BROWSER _ACTIONS);
141 ContextUtils.getAppSharedPreferences()
142 .edit()
143 .putBoolean(PREF_HAS_BROWSER_ACTIONS_NOTIFICATION, false)
144 .apply();
145 }
146
147 /**
148 * Checks whether Chrome should display tab switcher via Browser Actions Int ent.
149 * @param intent The intent to open the Chrome.
150 * @param isOverviewVisible Whether tab switcher is shown.
151 */
152 public static boolean shouldToggleOverview(Intent intent, boolean isOverview Visible) {
153 boolean fromBrowserActions = isStartedByBrowserActions(intent);
154 boolean isSingleUrl = IntentUtils.safeGetBooleanExtra(intent, EXTRA_IS_S INGLE_URL, false);
155 if (fromBrowserActions) {
156 return isSingleUrl == isOverviewVisible;
157 }
158 return false;
159 }
160
161 private static boolean isStartedByBrowserActions(Intent intent) {
162 if (ACTION_BROWSER_ACTIONS_OPEN_IN_BACKGROUND.equals(intent.getAction()) ) {
163 return true;
164 }
165 return false;
166 }
167
168 /**
169 * Sends a {@link Intent} to update the notification and the status of the { @link
170 * BrowserActionsService}.
171 * @param action The actions of the Intent.
172 * @param tabId The id of the created Tab.
173 */
174 public static void sendIntent(String action, int tabId) {
175 Context context = ContextUtils.getApplicationContext();
176 Intent intent = new Intent(context, BrowserActionsService.class);
177 intent.setAction(action);
178 if (tabId != Tab.INVALID_TAB_ID) {
179 intent.putExtra(EXTRA_TAB_ID, tabId);
180 }
181 context.startService(intent);
182 }
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698