OLD | NEW |
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.app.ActivityManager; | 7 import android.app.ActivityManager; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 /** | 130 /** |
131 * Called when we have to fire an Intent to add a shortcut to the home scree
n. | 131 * Called when we have to fire an Intent to add a shortcut to the home scree
n. |
132 * If the webpage indicated that it was capable of functioning as a webapp,
it is added as a | 132 * If the webpage indicated that it was capable of functioning as a webapp,
it is added as a |
133 * shortcut to a webapp Activity rather than as a general bookmark. User is
sent to the | 133 * shortcut to a webapp Activity rather than as a general bookmark. User is
sent to the |
134 * home screen as soon as the shortcut is created. | 134 * home screen as soon as the shortcut is created. |
135 * | 135 * |
136 * This method must not be called on the UI thread. | 136 * This method must not be called on the UI thread. |
137 */ | 137 */ |
138 @SuppressWarnings("unused") | 138 @SuppressWarnings("unused") |
139 @CalledByNative | 139 @CalledByNative |
140 private static void addShortcut(Context context, String id, String url, fina
l String userTitle, | 140 private static void addShortcut(String id, String url, final String userTitl
e, |
141 String name, String shortName, Bitmap icon, int displayMode, int ori
entation, | 141 String name, String shortName, Bitmap icon, int displayMode, int ori
entation, |
142 int source, long themeColor, long backgroundColor, boolean isIconGen
erated, | 142 int source, long themeColor, long backgroundColor, boolean isIconGen
erated, |
143 final long callbackPointer) { | 143 final long callbackPointer) { |
144 assert !ThreadUtils.runningOnUiThread(); | 144 assert !ThreadUtils.runningOnUiThread(); |
| 145 |
| 146 Context context = ContextUtils.getApplicationContext(); |
145 final Intent shortcutIntent; | 147 final Intent shortcutIntent; |
146 boolean isWebappCapable = (displayMode == WebDisplayMode.Standalone | 148 boolean isWebappCapable = (displayMode == WebDisplayMode.Standalone |
147 || displayMode == WebDisplayMode.Fullscreen); | 149 || displayMode == WebDisplayMode.Fullscreen); |
148 if (isWebappCapable) { | 150 if (isWebappCapable) { |
149 shortcutIntent = createWebappShortcutIntent(id, sDelegate.getFullscr
eenAction(), url, | 151 shortcutIntent = createWebappShortcutIntent(id, sDelegate.getFullscr
eenAction(), url, |
150 getScopeFromUrl(url), name, shortName, icon, WEBAPP_SHORTCUT
_VERSION, | 152 getScopeFromUrl(url), name, shortName, icon, WEBAPP_SHORTCUT
_VERSION, |
151 displayMode, orientation, themeColor, backgroundColor, isIco
nGenerated); | 153 displayMode, orientation, themeColor, backgroundColor, isIco
nGenerated); |
152 shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url)); | 154 shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url)); |
153 } else { | 155 } else { |
154 // Add the shortcut as a launcher icon to open in the browser Activi
ty. | 156 // Add the shortcut as a launcher icon to open in the browser Activi
ty. |
(...skipping 30 matching lines...) Expand all Loading... |
185 String toastText = | 187 String toastText = |
186 applicationContext.getString(R.string.added_to_homescree
n, userTitle); | 188 applicationContext.getString(R.string.added_to_homescree
n, userTitle); |
187 Toast toast = Toast.makeText(applicationContext, toastText, Toas
t.LENGTH_SHORT); | 189 Toast toast = Toast.makeText(applicationContext, toastText, Toas
t.LENGTH_SHORT); |
188 toast.show(); | 190 toast.show(); |
189 } | 191 } |
190 }); | 192 }); |
191 } | 193 } |
192 | 194 |
193 /** | 195 /** |
194 * Creates a storage location and stores the data for a web app using {@link
WebappDataStorage}. | 196 * Creates a storage location and stores the data for a web app using {@link
WebappDataStorage}. |
195 * @param context Context to open the WebappDataStorage with. | |
196 * @param id ID of the webapp which is storing data. | 197 * @param id ID of the webapp which is storing data. |
197 * @param splashImage Image which should be displayed on the splash screen o
f | 198 * @param splashImage Image which should be displayed on the splash screen o
f |
198 * the webapp. This can be null of there is no image to s
how. | 199 * the webapp. This can be null of there is no image to s
how. |
199 */ | 200 */ |
200 @SuppressWarnings("unused") | 201 @SuppressWarnings("unused") |
201 @CalledByNative | 202 @CalledByNative |
202 private static void storeWebappSplashImage(final Context context, final Stri
ng id, | 203 private static void storeWebappSplashImage(final String id, final Bitmap spl
ashImage) { |
203 final Bitmap splashImage) { | 204 WebappRegistry.getWebappDataStorage(ContextUtils.getApplicationContext()
, id, |
204 WebappRegistry.getWebappDataStorage(context, id, | |
205 new WebappRegistry.FetchWebappDataStorageCallback() { | 205 new WebappRegistry.FetchWebappDataStorageCallback() { |
206 @Override | 206 @Override |
207 public void onWebappDataStorageRetrieved(WebappDataStorage s
torage) { | 207 public void onWebappDataStorageRetrieved(WebappDataStorage s
torage) { |
208 if (storage == null) return; | 208 if (storage == null) return; |
209 | 209 |
210 storage.updateSplashScreenImage(splashImage); | 210 storage.updateSplashScreenImage(splashImage); |
211 } | 211 } |
212 | 212 |
213 } | 213 }); |
214 ); | |
215 } | 214 } |
216 | 215 |
217 /** | 216 /** |
218 * Creates an intent that will add a shortcut to the home screen. | 217 * Creates an intent that will add a shortcut to the home screen. |
219 * @param shortcutIntent Intent to fire when the shortcut is activated. | 218 * @param shortcutIntent Intent to fire when the shortcut is activated. |
220 * @param url URL of the shortcut. | 219 * @param url URL of the shortcut. |
221 * @param title Title of the shortcut. | 220 * @param title Title of the shortcut. |
222 * @param icon Image that represents the shortcut. | 221 * @param icon Image that represents the shortcut. |
223 * @return Intent for the shortcut. | 222 * @return Intent for the shortcut. |
224 */ | 223 */ |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 public static boolean isAddToHomeIntentSupported(Context context) { | 302 public static boolean isAddToHomeIntentSupported(Context context) { |
304 PackageManager pm = context.getPackageManager(); | 303 PackageManager pm = context.getPackageManager(); |
305 Intent i = new Intent(INSTALL_SHORTCUT); | 304 Intent i = new Intent(INSTALL_SHORTCUT); |
306 List<ResolveInfo> receivers = pm.queryBroadcastReceivers( | 305 List<ResolveInfo> receivers = pm.queryBroadcastReceivers( |
307 i, PackageManager.GET_INTENT_FILTERS); | 306 i, PackageManager.GET_INTENT_FILTERS); |
308 return !receivers.isEmpty(); | 307 return !receivers.isEmpty(); |
309 } | 308 } |
310 | 309 |
311 /** | 310 /** |
312 * Returns whether the given icon matches the size requirements to be used o
n the home screen. | 311 * Returns whether the given icon matches the size requirements to be used o
n the home screen. |
313 * @param context Context used to create the intent. | |
314 * @param width Icon width, in pixels. | 312 * @param width Icon width, in pixels. |
315 * @param height Icon height, in pixels. | 313 * @param height Icon height, in pixels. |
316 * @return whether the given icon matches the size requirements to be used o
n the home screen. | 314 * @return whether the given icon matches the size requirements to be used o
n the home screen. |
317 */ | 315 */ |
318 @CalledByNative | 316 @CalledByNative |
319 public static boolean isIconLargeEnoughForLauncher(Context context, int widt
h, int height) { | 317 public static boolean isIconLargeEnoughForLauncher(int width, int height) { |
| 318 Context context = ContextUtils.getApplicationContext(); |
320 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); | 319 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); |
321 final int minimalSize = am.getLauncherLargeIconSize() / 2; | 320 final int minimalSize = am.getLauncherLargeIconSize() / 2; |
322 return width >= minimalSize && height >= minimalSize; | 321 return width >= minimalSize && height >= minimalSize; |
323 } | 322 } |
324 | 323 |
325 /** | 324 /** |
326 * Adapts a website's icon (e.g. favicon or touch icon) to the Material desi
gn style guidelines | 325 * Adapts a website's icon (e.g. favicon or touch icon) to the Material desi
gn style guidelines |
327 * for home screen icons. This involves adding some padding and rounding the
corners. | 326 * for home screen icons. This involves adding some padding and rounding the
corners. |
328 * | 327 * |
329 * @param context Context used to create the intent. | 328 * @param context Context used to create the intent. |
330 * @param webIcon The website's favicon or touch icon. | 329 * @param webIcon The website's favicon or touch icon. |
331 * @return Bitmap Either the touch-icon or the newly created favicon. | 330 * @return Bitmap Either the touch-icon or the newly created favicon. |
332 */ | 331 */ |
333 @CalledByNative | 332 @CalledByNative |
334 public static Bitmap createHomeScreenIconFromWebIcon(Context context, Bitmap
webIcon) { | 333 public static Bitmap createHomeScreenIconFromWebIcon(Bitmap webIcon) { |
335 // getLauncherLargeIconSize() is just a guess at the launcher icon size,
and is often | 334 // getLauncherLargeIconSize() is just a guess at the launcher icon size,
and is often |
336 // wrong -- the launcher can show icons at any size it pleases. Instead
of resizing the | 335 // wrong -- the launcher can show icons at any size it pleases. Instead
of resizing the |
337 // icon to the supposed launcher size and then having the launcher resiz
e the icon again, | 336 // icon to the supposed launcher size and then having the launcher resiz
e the icon again, |
338 // just leave the icon at its original size and let the launcher do a si
ngle rescaling. | 337 // just leave the icon at its original size and let the launcher do a si
ngle rescaling. |
339 // Unless the icon is much too big; then scale it down here too. | 338 // Unless the icon is much too big; then scale it down here too. |
| 339 Context context = ContextUtils.getApplicationContext(); |
340 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); | 340 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); |
341 int maxInnerSize = Math.round(am.getLauncherLargeIconSize() * MAX_INNER_
SIZE_RATIO); | 341 int maxInnerSize = Math.round(am.getLauncherLargeIconSize() * MAX_INNER_
SIZE_RATIO); |
342 int innerSize = Math.min(maxInnerSize, Math.max(webIcon.getWidth(), webI
con.getHeight())); | 342 int innerSize = Math.min(maxInnerSize, Math.max(webIcon.getWidth(), webI
con.getHeight())); |
343 int padding = Math.round(ICON_PADDING_RATIO * innerSize); | 343 int padding = Math.round(ICON_PADDING_RATIO * innerSize); |
344 int outerSize = innerSize + 2 * padding; | 344 int outerSize = innerSize + 2 * padding; |
345 | 345 |
346 Bitmap bitmap = null; | 346 Bitmap bitmap = null; |
347 try { | 347 try { |
348 bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARG
B_8888); | 348 bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARG
B_8888); |
349 } catch (OutOfMemoryError e) { | 349 } catch (OutOfMemoryError e) { |
(...skipping 17 matching lines...) Expand all Loading... |
367 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); | 367 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); |
368 canvas.drawPath(path, paint); | 368 canvas.drawPath(path, paint); |
369 | 369 |
370 return bitmap; | 370 return bitmap; |
371 } | 371 } |
372 | 372 |
373 /** | 373 /** |
374 * Generates a generic icon to be used in the launcher. This is just a round
ed rectangle with | 374 * Generates a generic icon to be used in the launcher. This is just a round
ed rectangle with |
375 * a letter in the middle taken from the website's domain name. | 375 * a letter in the middle taken from the website's domain name. |
376 * | 376 * |
377 * @param context Context used to create the intent. | |
378 * @param url URL of the shortcut. | 377 * @param url URL of the shortcut. |
379 * @param red Red component of the dominant icon color. | 378 * @param red Red component of the dominant icon color. |
380 * @param green Green component of the dominant icon color. | 379 * @param green Green component of the dominant icon color. |
381 * @param blue Blue component of the dominant icon color. | 380 * @param blue Blue component of the dominant icon color. |
382 * @return Bitmap Either the touch-icon or the newly created favicon. | 381 * @return Bitmap Either the touch-icon or the newly created favicon. |
383 */ | 382 */ |
384 @CalledByNative | 383 @CalledByNative |
385 public static Bitmap generateHomeScreenIcon(Context context, String url, int
red, int green, | 384 public static Bitmap generateHomeScreenIcon(String url, int red, int green,
int blue) { |
386 int blue) { | 385 Context context = ContextUtils.getApplicationContext(); |
387 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); | 386 ActivityManager am = (ActivityManager) context.getSystemService(Context.
ACTIVITY_SERVICE); |
388 final int outerSize = am.getLauncherLargeIconSize(); | 387 final int outerSize = am.getLauncherLargeIconSize(); |
389 final int iconDensity = am.getLauncherLargeIconDensity(); | 388 final int iconDensity = am.getLauncherLargeIconDensity(); |
390 | 389 |
391 Bitmap bitmap = null; | 390 Bitmap bitmap = null; |
392 try { | 391 try { |
393 bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARG
B_8888); | 392 bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARG
B_8888); |
394 } catch (OutOfMemoryError e) { | 393 } catch (OutOfMemoryError e) { |
395 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); | 394 Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas."
); |
396 return null; | 395 return null; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 builder.fragment(""); | 527 builder.fragment(""); |
529 builder.query(""); | 528 builder.query(""); |
530 return builder.build().toString(); | 529 return builder.build().toString(); |
531 } | 530 } |
532 | 531 |
533 /** | 532 /** |
534 * Returns an array of sizes which describe the ideal size and minimum size
of the Home screen | 533 * Returns an array of sizes which describe the ideal size and minimum size
of the Home screen |
535 * icon and the ideal and minimum sizes of the splash screen image in that o
rder. | 534 * icon and the ideal and minimum sizes of the splash screen image in that o
rder. |
536 */ | 535 */ |
537 @CalledByNative | 536 @CalledByNative |
538 private static int[] getHomeScreenIconAndSplashImageSizes(Context context) { | 537 private static int[] getHomeScreenIconAndSplashImageSizes() { |
| 538 Context context = ContextUtils.getApplicationContext(); |
539 // This ordering must be kept up to date with the C++ ShortcutHelper. | 539 // This ordering must be kept up to date with the C++ ShortcutHelper. |
540 return new int[] { | 540 return new int[] { |
541 getIdealHomescreenIconSizeInDp(context), | 541 getIdealHomescreenIconSizeInDp(context), |
542 getMinimumHomescreenIconSizeInDp(context), | 542 getMinimumHomescreenIconSizeInDp(context), |
543 getIdealSplashImageSizeInDp(context), | 543 getIdealSplashImageSizeInDp(context), |
544 getMinimumSplashImageSizeInDp(context) | 544 getMinimumSplashImageSizeInDp(context) |
545 }; | 545 }; |
546 } | 546 } |
547 | 547 |
548 private static int getIdealSizeFromResourceInDp(Context context, int resourc
e) { | 548 private static int getIdealSizeFromResourceInDp(Context context, int resourc
e) { |
549 float sizeInPx = context.getResources().getDimension(resource); | 549 float sizeInPx = context.getResources().getDimension(resource); |
550 float density = context.getResources().getDisplayMetrics().density; | 550 float density = context.getResources().getDisplayMetrics().density; |
551 return Math.round(sizeInPx / density); | 551 return Math.round(sizeInPx / density); |
552 } | 552 } |
553 | 553 |
554 private static Bitmap getBitmapFromResourceId(Context context, int id, int d
ensity) { | 554 private static Bitmap getBitmapFromResourceId(Context context, int id, int d
ensity) { |
555 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( | 555 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( |
556 context.getResources(), id, density); | 556 context.getResources(), id, density); |
557 | 557 |
558 if (drawable instanceof BitmapDrawable) { | 558 if (drawable instanceof BitmapDrawable) { |
559 BitmapDrawable bd = (BitmapDrawable) drawable; | 559 BitmapDrawable bd = (BitmapDrawable) drawable; |
560 return bd.getBitmap(); | 560 return bd.getBitmap(); |
561 } | 561 } |
562 assert false : "The drawable was not a bitmap drawable as expected"; | 562 assert false : "The drawable was not a bitmap drawable as expected"; |
563 return null; | 563 return null; |
564 } | 564 } |
565 | 565 |
566 private static native void nativeOnWebappDataStored(long callbackPointer); | 566 private static native void nativeOnWebappDataStored(long callbackPointer); |
567 } | 567 } |
OLD | NEW |