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

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

Issue 2689993002: Refactor the INSTALL_SHORTCUT broadcast code into ChromeShortcutManager (Closed)
Patch Set: Sync and upload again Created 3 years, 10 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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; 5 package org.chromium.chrome.browser;
6 6
7 import android.annotation.SuppressLint;
8 import android.app.ActivityManager; 7 import android.app.ActivityManager;
9 import android.content.Context; 8 import android.content.Context;
10 import android.content.Intent; 9 import android.content.Intent;
11 import android.content.pm.ApplicationInfo; 10 import android.content.pm.ApplicationInfo;
12 import android.content.pm.PackageInfo; 11 import android.content.pm.PackageInfo;
13 import android.content.pm.PackageManager; 12 import android.content.pm.PackageManager;
14 import android.content.pm.PackageManager.NameNotFoundException; 13 import android.content.pm.PackageManager.NameNotFoundException;
15 import android.content.pm.ResolveInfo;
16 import android.graphics.Bitmap; 14 import android.graphics.Bitmap;
17 import android.graphics.BitmapFactory; 15 import android.graphics.BitmapFactory;
18 import android.graphics.Canvas; 16 import android.graphics.Canvas;
19 import android.graphics.Color; 17 import android.graphics.Color;
20 import android.graphics.Paint; 18 import android.graphics.Paint;
21 import android.graphics.Rect; 19 import android.graphics.Rect;
22 import android.graphics.drawable.BitmapDrawable; 20 import android.graphics.drawable.BitmapDrawable;
23 import android.graphics.drawable.Drawable; 21 import android.graphics.drawable.Drawable;
24 import android.net.Uri; 22 import android.net.Uri;
25 import android.os.AsyncTask; 23 import android.os.AsyncTask;
26 import android.support.annotation.NonNull; 24 import android.support.annotation.NonNull;
27 import android.text.TextUtils; 25 import android.text.TextUtils;
28 import android.util.Base64; 26 import android.util.Base64;
29 27
30 import org.chromium.base.ApiCompatibilityUtils; 28 import org.chromium.base.ApiCompatibilityUtils;
31 import org.chromium.base.ContextUtils; 29 import org.chromium.base.ContextUtils;
32 import org.chromium.base.Log; 30 import org.chromium.base.Log;
33 import org.chromium.base.ThreadUtils; 31 import org.chromium.base.ThreadUtils;
34 import org.chromium.base.VisibleForTesting; 32 import org.chromium.base.VisibleForTesting;
35 import org.chromium.base.annotations.CalledByNative; 33 import org.chromium.base.annotations.CalledByNative;
36 import org.chromium.blink_public.platform.WebDisplayMode; 34 import org.chromium.blink_public.platform.WebDisplayMode;
37 import org.chromium.chrome.R; 35 import org.chromium.chrome.R;
36 import org.chromium.chrome.browser.webapps.ChromeShortcutManager;
38 import org.chromium.chrome.browser.webapps.ChromeWebApkHost; 37 import org.chromium.chrome.browser.webapps.ChromeWebApkHost;
39 import org.chromium.chrome.browser.webapps.WebApkInfo; 38 import org.chromium.chrome.browser.webapps.WebApkInfo;
40 import org.chromium.chrome.browser.webapps.WebappActivity; 39 import org.chromium.chrome.browser.webapps.WebappActivity;
41 import org.chromium.chrome.browser.webapps.WebappAuthenticator; 40 import org.chromium.chrome.browser.webapps.WebappAuthenticator;
42 import org.chromium.chrome.browser.webapps.WebappDataStorage; 41 import org.chromium.chrome.browser.webapps.WebappDataStorage;
43 import org.chromium.chrome.browser.webapps.WebappLauncherActivity; 42 import org.chromium.chrome.browser.webapps.WebappLauncherActivity;
44 import org.chromium.chrome.browser.webapps.WebappRegistry; 43 import org.chromium.chrome.browser.webapps.WebappRegistry;
45 import org.chromium.chrome.browser.widget.RoundedIconGenerator; 44 import org.chromium.chrome.browser.widget.RoundedIconGenerator;
46 import org.chromium.content_public.common.ScreenOrientationConstants; 45 import org.chromium.content_public.common.ScreenOrientationConstants;
47 import org.chromium.ui.widget.Toast; 46 import org.chromium.ui.widget.Toast;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 public static final String EXTRA_WEBAPK_PACKAGE_NAME = 81 public static final String EXTRA_WEBAPK_PACKAGE_NAME =
83 "org.chromium.chrome.browser.webapk_package_name"; 82 "org.chromium.chrome.browser.webapk_package_name";
84 83
85 // When a new field is added to the intent, this version should be increment ed so that it will 84 // When a new field is added to the intent, this version should be increment ed so that it will
86 // be correctly populated into the WebappRegistry/WebappDataStorage. 85 // be correctly populated into the WebappRegistry/WebappDataStorage.
87 public static final int WEBAPP_SHORTCUT_VERSION = 2; 86 public static final int WEBAPP_SHORTCUT_VERSION = 2;
88 87
89 // This value is equal to kInvalidOrMissingColor in the C++ content::Manifes t struct. 88 // This value is equal to kInvalidOrMissingColor in the C++ content::Manifes t struct.
90 public static final long MANIFEST_COLOR_INVALID_OR_MISSING = ((long) Integer .MAX_VALUE) + 1; 89 public static final long MANIFEST_COLOR_INVALID_OR_MISSING = ((long) Integer .MAX_VALUE) + 1;
91 90
91 // This intent category is for the callback intent when using the new shortc ut API
gone 2017/02/15 19:40:18 1) end comment with a period. 2) fix comment up o
Marti Wong 2017/02/17 05:10:49 Done.
92 public static final String SHORTCUT_TOAST_CATEGORY =
93 "com.google.intent.category.SHORTCUT_TOAST";
94
92 private static final String TAG = "ShortcutHelper"; 95 private static final String TAG = "ShortcutHelper";
93 96
94 // There is no public string defining this intent so if Home changes the val ue, we
95 // have to update this string.
96 private static final String INSTALL_SHORTCUT = "com.android.launcher.action. INSTALL_SHORTCUT";
97
98 // The activity class used for launching a WebApk. 97 // The activity class used for launching a WebApk.
99 private static final String WEBAPK_MAIN_ACTIVITY = "org.chromium.webapk.shel l_apk.MainActivity"; 98 private static final String WEBAPK_MAIN_ACTIVITY = "org.chromium.webapk.shel l_apk.MainActivity";
100 99
101 // These sizes are from the Material spec for icons: 100 // These sizes are from the Material spec for icons:
102 // https://www.google.com/design/spec/style/icons.html#icons-product-icons 101 // https://www.google.com/design/spec/style/icons.html#icons-product-icons
103 private static final float MAX_INNER_SIZE_RATIO = 1.25f; 102 private static final float MAX_INNER_SIZE_RATIO = 1.25f;
104 private static final float ICON_PADDING_RATIO = 2.0f / 44.0f; 103 private static final float ICON_PADDING_RATIO = 2.0f / 44.0f;
105 private static final float ICON_CORNER_RADIUS_RATIO = 1.0f / 16.0f; 104 private static final float ICON_CORNER_RADIUS_RATIO = 1.0f / 16.0f;
106 private static final float GENERATED_ICON_PADDING_RATIO = 1.0f / 12.0f; 105 private static final float GENERATED_ICON_PADDING_RATIO = 1.0f / 12.0f;
107 private static final float GENERATED_ICON_FONT_SIZE_RATIO = 1.0f / 3.0f; 106 private static final float GENERATED_ICON_FONT_SIZE_RATIO = 1.0f / 3.0f;
108 107
109 /** Broadcasts Intents out Android for adding the shortcut. */ 108 /** Request Android to add a shortcut to the home screen. */
gone 2017/02/15 19:40:19 This javadoc sounds more like a function comment a
Marti Wong 2017/02/17 05:10:48 Done.
110 public static class Delegate { 109 public static class Delegate {
111 /** 110 /**
112 * Broadcasts an intent to all interested BroadcastReceivers. 111 * Request Android to add a shortcut to the home screen
113 * @param context The Context to use. 112 * @param title Title of the shortcut.
114 * @param intent The intent to broadcast. 113 * @param icon Image that represents the shortcut.
114 * @param intent Intent to fire when the shortcut is activated.
gone 2017/02/15 19:40:18 nit: Line up the @params to match rest of file? @
Marti Wong 2017/02/17 05:10:49 Done.
115 */ 115 */
116 public void sendBroadcast(Context context, Intent intent) { 116 public void addShortcutToHomescreen(String title, Bitmap icon, Intent sh ortcutIntent) {
117 context.sendBroadcast(intent); 117 ChromeShortcutManager.getInstance().addShortcutToHomeScreen(
118 title, icon, shortcutIntent);
118 } 119 }
119 120
120 /** 121 /**
121 * Returns the name of the fullscreen Activity to use when launching sho rtcuts. 122 * Returns the name of the fullscreen Activity to use when launching sho rtcuts.
122 */ 123 */
123 public String getFullscreenAction() { 124 public String getFullscreenAction() {
124 return WebappLauncherActivity.ACTION_START_WEBAPP; 125 return WebappLauncherActivity.ACTION_START_WEBAPP;
125 } 126 }
126 } 127 }
127 128
(...skipping 30 matching lines...) Expand all
158 sDelegate.getFullscreenAction(), url, nonEmptyScopeUrl, name, shortName, 159 sDelegate.getFullscreenAction(), url, nonEmptyScopeUrl, name, shortName,
159 icon, WEBAPP_SHORTCUT_VERSION, displayMode, orientation, themeColor, 160 icon, WEBAPP_SHORTCUT_VERSION, displayMode, orientation, themeColor,
160 backgroundColor, iconUrl.isEmpty()); 161 backgroundColor, iconUrl.isEmpty());
161 shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url)); 162 shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url));
162 shortcutIntent.putExtra(EXTRA_SOURCE, source); 163 shortcutIntent.putExtra(EXTRA_SOURCE, source);
163 shortcutIntent.setPackage(context.getPackageName()); 164 shortcutIntent.setPackage(context.getPackageName());
164 return shortcutIntent; 165 return shortcutIntent;
165 } 166 }
166 @Override 167 @Override
167 protected void onPostExecute(final Intent resultIntent) { 168 protected void onPostExecute(final Intent resultIntent) {
168 Context context = ContextUtils.getApplicationContext(); 169 sDelegate.addShortcutToHomescreen(userTitle, icon, resultIntent) ;
169 sDelegate.sendBroadcast(
170 context, createAddToHomeIntent(userTitle, icon, resultIn tent));
171 170
172 // Store the webapp data so that it is accessible without the in tent. Once this 171 // Store the webapp data so that it is accessible without the in tent. Once this
173 // process is complete, call back to native code to start the sp lash image 172 // process is complete, call back to native code to start the sp lash image
174 // download. 173 // download.
175 WebappRegistry.getInstance().register( 174 WebappRegistry.getInstance().register(
176 id, new WebappRegistry.FetchWebappDataStorageCallback() { 175 id, new WebappRegistry.FetchWebappDataStorageCallback() {
177 @Override 176 @Override
178 public void onWebappDataStorageRetrieved(WebappDataS torage storage) { 177 public void onWebappDataStorageRetrieved(WebappDataS torage storage) {
179 storage.updateFromShortcutIntent(resultIntent); 178 storage.updateFromShortcutIntent(resultIntent);
180 nativeOnWebappDataStored(callbackPointer); 179 nativeOnWebappDataStored(callbackPointer);
181 } 180 }
182 }); 181 });
183 182 if (ChromeShortcutManager.getInstance().shouldShowToastWhenAddin gShortcut()) {
184 showAddedToHomescreenToast(userTitle); 183 showAddedToHomescreenToast(userTitle);
184 }
185 } 185 }
186 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 186 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
187 } 187 }
188 188
189 public static void addWebApkShortcut(Context context, String packageName) { 189 public static void addWebApkShortcut(Context context, String packageName) {
190 PackageManager pm = context.getPackageManager(); 190 PackageManager pm = context.getPackageManager();
191 try { 191 try {
192 ApplicationInfo appInfo = pm.getApplicationInfo( 192 ApplicationInfo appInfo = pm.getApplicationInfo(
193 packageName, PackageManager.GET_META_DATA); 193 packageName, PackageManager.GET_META_DATA);
194 String shortcutTitle = pm.getApplicationLabel(appInfo).toString(); 194 String shortcutTitle = pm.getApplicationLabel(appInfo).toString();
195 Bitmap shortcutIcon = ((BitmapDrawable) pm.getApplicationIcon(packag eName)).getBitmap(); 195 Bitmap shortcutIcon = ((BitmapDrawable) pm.getApplicationIcon(packag eName)).getBitmap();
196 196
197 Bitmap bitmap = createHomeScreenIconFromWebIcon(shortcutIcon); 197 Bitmap bitmap = createHomeScreenIconFromWebIcon(shortcutIcon);
198 Intent i = new Intent(); 198 Intent i = new Intent();
199 i.setClassName(packageName, WEBAPK_MAIN_ACTIVITY); 199 i.setClassName(packageName, WEBAPK_MAIN_ACTIVITY);
200 i.addCategory(Intent.CATEGORY_LAUNCHER); 200 i.addCategory(Intent.CATEGORY_LAUNCHER);
201 context.sendBroadcast(createAddToHomeIntent(shortcutTitle, bitmap, i )); 201 context.sendBroadcast(
202 ChromeShortcutManager.createAddToHomeIntent(shortcutTitle, b itmap, i));
202 } catch (NameNotFoundException e) { 203 } catch (NameNotFoundException e) {
203 e.printStackTrace(); 204 e.printStackTrace();
204 } 205 }
205 } 206 }
206 207
207 /** 208 /**
208 * Adds home screen shortcut which opens in the browser Activity. 209 * Adds home screen shortcut which opens in the browser Activity.
209 */ 210 */
210 @SuppressWarnings("unused") 211 @SuppressWarnings("unused")
211 @CalledByNative 212 @CalledByNative
212 private static void addShortcut(String url, String userTitle, Bitmap icon, i nt source) { 213 private static void addShortcut(
214 String id, String url, String userTitle, Bitmap icon, int source) {
213 Context context = ContextUtils.getApplicationContext(); 215 Context context = ContextUtils.getApplicationContext();
214 final Intent shortcutIntent = createShortcutIntent(url); 216 final Intent shortcutIntent = createShortcutIntent(url);
217 shortcutIntent.putExtra(EXTRA_ID, id);
215 shortcutIntent.putExtra(EXTRA_SOURCE, source); 218 shortcutIntent.putExtra(EXTRA_SOURCE, source);
216 shortcutIntent.setPackage(context.getPackageName()); 219 shortcutIntent.setPackage(context.getPackageName());
217 sDelegate.sendBroadcast( 220 sDelegate.addShortcutToHomescreen(userTitle, icon, shortcutIntent);
218 context, createAddToHomeIntent(userTitle, icon, shortcutIntent)) ; 221 if (ChromeShortcutManager.getInstance().shouldShowToastWhenAddingShortcu t()) {
219 showAddedToHomescreenToast(userTitle); 222 showAddedToHomescreenToast(userTitle);
223 }
220 } 224 }
221 225
222 /** 226 /**
223 * Show toast to alert user that the shortcut was added to the home screen. 227 * Show toast to alert user that the shortcut was added to the home screen.
224 */ 228 */
225 private static void showAddedToHomescreenToast(final String title) { 229 private static void showAddedToHomescreenToast(final String title) {
226 Context applicationContext = ContextUtils.getApplicationContext(); 230 Context applicationContext = ContextUtils.getApplicationContext();
227 String toastText = applicationContext.getString(R.string.added_to_homesc reen, title); 231 String toastText = applicationContext.getString(R.string.added_to_homesc reen, title);
228 showToast(toastText); 232 showToast(toastText);
229 } 233 }
230 234
231 /** 235 /**
236 * Show toast when getting the callback intent by the launcher after adding shortcut by using
237 * the new shortcut API
gone 2017/02/15 19:40:18 End all your comments with periods
Marti Wong 2017/02/17 05:10:48 Done.
238 */
239 public static void showAddedToHomescreenToastFromIntent(Intent intent) {
240 String title = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
gone 2017/02/15 19:40:19 1) Never trust an Intent; use IntentUtils#safeGetS
Marti Wong 2017/02/17 05:10:48 1) Done, thanks~! 2) this function should just sho
241 showAddedToHomescreenToast(title);
242 }
243
244 /**
245 * Determine if it is a callback intent (which requests for a show-toast), u sed in the new
246 * shortcut API
247 */
248 public static boolean isShowtoastIntent(Intent intent) {
gone 2017/02/15 19:40:18 nit: capitalize Toast
Marti Wong 2017/02/17 05:10:48 Done.
249 if (intent == null || intent.getCategories() == null) return false;
250 return intent.getCategories().contains(SHORTCUT_TOAST_CATEGORY);
251 }
252
253 /**
232 * Shows toast notifying user that a WebAPK install is already in progress w hen user tries to 254 * Shows toast notifying user that a WebAPK install is already in progress w hen user tries to
233 * queue a new install for the same WebAPK. 255 * queue a new install for the same WebAPK.
234 */ 256 */
235 @SuppressWarnings("unused") 257 @SuppressWarnings("unused")
236 @CalledByNative 258 @CalledByNative
237 private static void showWebApkInstallInProgressToast() { 259 private static void showWebApkInstallInProgressToast() {
238 Context applicationContext = ContextUtils.getApplicationContext(); 260 Context applicationContext = ContextUtils.getApplicationContext();
239 String toastText = applicationContext.getString(R.string.webapk_install_ in_progress); 261 String toastText = applicationContext.getString(R.string.webapk_install_ in_progress);
240 showToast(toastText); 262 showToast(toastText);
241 } 263 }
(...skipping 24 matching lines...) Expand all
266 288
267 @Override 289 @Override
268 protected void onPostExecute(String encodedImage) { 290 protected void onPostExecute(String encodedImage) {
269 storage.updateSplashScreenImage(encodedImage); 291 storage.updateSplashScreenImage(encodedImage);
270 } 292 }
271 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 293 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
272 } 294 }
273 } 295 }
274 296
275 /** 297 /**
276 * Creates an intent that will add a shortcut to the home screen.
277 * @param title Title of the shortcut.
278 * @param icon Image that represents the shortcut.
279 * @param shortcutIntent Intent to fire when the shortcut is activated.
280 * @return Intent for the shortcut.
281 */
282 public static Intent createAddToHomeIntent(String title, Bitmap icon,
283 Intent shortcutIntent) {
284 Intent i = new Intent(INSTALL_SHORTCUT);
285 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
286 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
287 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
288 return i;
289 }
290
291 /**
292 * Creates a shortcut to launch a web app on the home screen. 298 * Creates a shortcut to launch a web app on the home screen.
293 * @param id Id of the web app. 299 * @param id Id of the web app.
294 * @param action Intent action to open a full screen activity. 300 * @param action Intent action to open a full screen activity.
295 * @param url Url of the web app. 301 * @param url Url of the web app.
296 * @param scope Url scope of the web app. 302 * @param scope Url scope of the web app.
297 * @param name Name of the web app. 303 * @param name Name of the web app.
298 * @param shortName Short name of the web app. 304 * @param shortName Short name of the web app.
299 * @param icon Icon of the web app. 305 * @param icon Icon of the web app.
300 * @param version Version number of the shortcut. 306 * @param version Version number of the shortcut.
301 * @param displayMode Display mode of the web app. 307 * @param displayMode Display mode of the web app.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 360 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
355 shortcutIntent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true); 361 shortcutIntent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true);
356 return shortcutIntent; 362 return shortcutIntent;
357 } 363 }
358 364
359 /** 365 /**
360 * Utility method to check if a shortcut can be added to the home screen. 366 * Utility method to check if a shortcut can be added to the home screen.
361 * @param context Context used to get the package manager. 367 * @param context Context used to get the package manager.
362 * @return if a shortcut can be added to the home screen under the current p rofile. 368 * @return if a shortcut can be added to the home screen under the current p rofile.
363 */ 369 */
364 // TODO(crbug.com/635567): Fix this properly.
365 @SuppressLint("WrongConstant")
366 public static boolean isAddToHomeIntentSupported(Context context) { 370 public static boolean isAddToHomeIntentSupported(Context context) {
gone 2017/02/15 19:40:18 This doesn't use the context anymore. Might as we
Marti Wong 2017/02/17 05:10:48 Done. thanks. Sorry for this careless mistake.
gone 2017/02/17 19:41:43 Pfft, nothing to apologize for.
367 PackageManager pm = context.getPackageManager(); 371 return ChromeShortcutManager.getInstance().canAddShortcutToHomescreen();
368 Intent i = new Intent(INSTALL_SHORTCUT);
369 List<ResolveInfo> receivers = pm.queryBroadcastReceivers(
370 i, PackageManager.GET_INTENT_FILTERS);
371 return !receivers.isEmpty();
372 } 372 }
373 373
374 /** 374 /**
375 * Returns whether the given icon matches the size requirements to be used o n the home screen. 375 * Returns whether the given icon matches the size requirements to be used o n the home screen.
376 * @param width Icon width, in pixels. 376 * @param width Icon width, in pixels.
377 * @param height Icon height, in pixels. 377 * @param height Icon height, in pixels.
378 * @return whether the given icon matches the size requirements to be used o n the home screen. 378 * @return whether the given icon matches the size requirements to be used o n the home screen.
379 */ 379 */
380 @CalledByNative 380 @CalledByNative
381 public static boolean isIconLargeEnoughForLauncher(int width, int height) { 381 public static boolean isIconLargeEnoughForLauncher(int width, int height) {
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 for (int i = 0; i < list.size(); i++) { 700 for (int i = 0; i < list.size(); i++) {
701 array[i] = list.get(i); 701 array[i] = list.get(i);
702 } 702 }
703 return array; 703 return array;
704 } 704 }
705 705
706 private static native void nativeOnWebappDataStored(long callbackPointer); 706 private static native void nativeOnWebappDataStored(long callbackPointer);
707 private static native void nativeOnWebApksRetrieved(long callbackPointer, St ring[] shortNames, 707 private static native void nativeOnWebApksRetrieved(long callbackPointer, St ring[] shortNames,
708 String[] packageName, int[] shellApkVersions, int[] versionCodes); 708 String[] packageName, int[] shellApkVersions, int[] versionCodes);
709 } 709 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698