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

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

Issue 1308533006: webapps: allow callers of icon downloader/selector to specify a minimum size (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webapps-splashscreen-icon
Patch Set: Address review comments Created 5 years, 3 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.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;
11 import android.content.pm.ResolveInfo; 11 import android.content.pm.ResolveInfo;
12 import android.content.res.Resources;
13 import android.graphics.Bitmap; 12 import android.graphics.Bitmap;
14 import android.graphics.BitmapFactory; 13 import android.graphics.BitmapFactory;
15 import android.graphics.Canvas; 14 import android.graphics.Canvas;
16 import android.graphics.Color; 15 import android.graphics.Color;
17 import android.graphics.Paint; 16 import android.graphics.Paint;
18 import android.graphics.Path; 17 import android.graphics.Path;
19 import android.graphics.PorterDuff; 18 import android.graphics.PorterDuff;
20 import android.graphics.PorterDuffXfermode; 19 import android.graphics.PorterDuffXfermode;
21 import android.graphics.Rect; 20 import android.graphics.Rect;
22 import android.graphics.RectF; 21 import android.graphics.RectF;
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 * @param encodedString the Base64 String to decode. 288 * @param encodedString the Base64 String to decode.
290 * @return the Bitmap which was encoded by the String. 289 * @return the Bitmap which was encoded by the String.
291 */ 290 */
292 public static Bitmap decodeBitmapFromString(String encodedString) { 291 public static Bitmap decodeBitmapFromString(String encodedString) {
293 if (TextUtils.isEmpty(encodedString)) return null; 292 if (TextUtils.isEmpty(encodedString)) return null;
294 byte[] decoded = Base64.decode(encodedString, Base64.DEFAULT); 293 byte[] decoded = Base64.decode(encodedString, Base64.DEFAULT);
295 return BitmapFactory.decodeByteArray(decoded, 0, decoded.length); 294 return BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
296 } 295 }
297 296
298 /** 297 /**
299 * Returns the ideal size for an image displayed on a web app's splash scree n.
300 * @param resources Resources to retrieve the dimension from.
301 * @return the dimensions in dp which the image should have.
302 */
303 public static int getIdealSplashImageSizeInDp(Resources resources) {
304 return getIdealSizeFromResource(resources, R.dimen.webapp_splash_image_s ize);
305 }
306
307 /**
308 * Returns the ideal size for an icon representing a web app. This size is used on app banners, 298 * Returns the ideal size for an icon representing a web app. This size is used on app banners,
309 * the Android Home screen, and in Android's recent tasks list, among other places. 299 * the Android Home screen, and in Android's recent tasks list, among other places.
310 * @param resources Resources to retrieve the dimension from. 300 * @param resources Resources to retrieve the dimension from.
311 * @return the dimensions in dp which the icon should have. 301 * @return the dimensions in dp which the icon should have.
312 */ 302 */
313 public static int getIdealIconSizeInDp(Resources resources) { 303 public static int getIdealIconSizeInDp(Context context) {
314 return getIdealSizeFromResource(resources, R.dimen.webapp_home_screen_ic on_size); 304 return getIdealSizeFromResourceInDp(context, R.dimen.webapp_home_screen_ icon_size);
315 } 305 }
316 306
317 /** 307 /**
308 * Returns the minimum size for an icon representing a web app. This size i s used on app
309 * banners, the Android Home screen, and in Android's recent tasks list, amo ng other places.
310 * @param resources Resources to retrieve the dimension from.
311 * @return the lower bound of the size which the icon should have in dp.
312 */
313 public static int getMinimumIconSizeInDp(Context context) {
314 float sizeInPx = context.getResources().getDimension(R.dimen.webapp_home _screen_icon_size);
315 float density = context.getResources().getDisplayMetrics().density;
316 float idealIconSizeInDp = sizeInPx / density;
317
318 float minimumIconSizeInPx = idealIconSizeInDp * (density - 1);
319 return Math.round(minimumIconSizeInPx / density);
320 }
321
322 /**
323 * Returns the ideal size for an image displayed on a web app's splash scree n.
324 * @param resources Resources to retrieve the dimension from.
325 * @return the dimensions in dp which the image should have.
326 */
327 public static int getIdealSplashImageSizeInDp(Context context) {
328 return getIdealSizeFromResourceInDp(context, R.dimen.webapp_splash_image _size);
329 }
330
331 /**
332 * Returns the minimum size for an image displayed on a web app's splash scr een.
333 * @param resources Resources to retrieve the dimension from.
334 * @return the lower bound of the size which the image should have in dp.
335 */
336 public static int getMinimumSplashImageSizeInDp(Context context) {
337 return getIdealSizeFromResourceInDp(context, R.dimen.webapp_splash_image _min_size);
338 }
339
340 /**
318 * @return String that can be used to verify that a WebappActivity is being started by Chrome. 341 * @return String that can be used to verify that a WebappActivity is being started by Chrome.
319 */ 342 */
320 public static String getEncodedMac(Context context, String url) { 343 public static String getEncodedMac(Context context, String url) {
321 // The only reason we convert to a String here is because Android inexpl icably eats a 344 // The only reason we convert to a String here is because Android inexpl icably eats a
322 // byte[] when adding the shortcut -- the Bundle received by the launche d Activity even 345 // byte[] when adding the shortcut -- the Bundle received by the launche d Activity even
323 // lacks the key for the extra. 346 // lacks the key for the extra.
324 byte[] mac = WebappAuthenticator.getMacForUrl(context, url); 347 byte[] mac = WebappAuthenticator.getMacForUrl(context, url);
325 return Base64.encodeToString(mac, Base64.DEFAULT); 348 return Base64.encodeToString(mac, Base64.DEFAULT);
326 } 349 }
327 350
328 private static int getIdealSizeFromResource(Resources resources, int resourc e) { 351 /**
329 float sizeInPx = resources.getDimension(resource); 352 * Returns an array of sizes which describe the ideal size and minimum size of the home screen
330 float density = resources.getDisplayMetrics().density; 353 * icon and the ideal and minimum sizes of the splash screen image in that o rder.
354 */
355 @CalledByNative
356 private static int[] getIconAndSplashImageSizes(Context context) {
357 // This ordering must be kept up to date with the C++ ShortcutHelper.
358 return new int[] {
359 getIdealIconSizeInDp(context),
360 getMinimumIconSizeInDp(context),
361 getIdealSplashImageSizeInDp(context),
362 getMinimumSplashImageSizeInDp(context)
363 };
364 }
365
366 private static int getIdealSizeFromResourceInDp(Context context, int resourc e) {
367 float sizeInPx = context.getResources().getDimension(resource);
368 float density = context.getResources().getDisplayMetrics().density;
331 return Math.round(sizeInPx / density); 369 return Math.round(sizeInPx / density);
332 } 370 }
333 371
334 private static Bitmap getBitmapFromResourceId(Context context, int id, int d ensity) { 372 private static Bitmap getBitmapFromResourceId(Context context, int id, int d ensity) {
335 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( 373 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity(
336 context.getResources(), id, density); 374 context.getResources(), id, density);
337 375
338 if (drawable instanceof BitmapDrawable) { 376 if (drawable instanceof BitmapDrawable) {
339 BitmapDrawable bd = (BitmapDrawable) drawable; 377 BitmapDrawable bd = (BitmapDrawable) drawable;
340 return bd.getBitmap(); 378 return bd.getBitmap();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 int iconFontSize = (int) (GENERATED_ICON_FONT_SIZE_DP * density); 428 int iconFontSize = (int) (GENERATED_ICON_FONT_SIZE_DP * density);
391 429
392 RoundedIconGenerator generator = new RoundedIconGenerator( 430 RoundedIconGenerator generator = new RoundedIconGenerator(
393 iconSize, iconSize, iconRoundedEdge, color, iconFontSize); 431 iconSize, iconSize, iconRoundedEdge, color, iconFontSize);
394 Bitmap icon = generator.generateIconForUrl(url); 432 Bitmap icon = generator.generateIconForUrl(url);
395 if (icon == null) return; // Bookmark URL does not have a domain. 433 if (icon == null) return; // Bookmark URL does not have a domain.
396 canvas.drawBitmap(icon, iconBounds.exactCenterX() - icon.getWidth() / 2. 0f, 434 canvas.drawBitmap(icon, iconBounds.exactCenterX() - icon.getWidth() / 2. 0f,
397 iconBounds.exactCenterY() - icon.getHeight() / 2.0f, null); 435 iconBounds.exactCenterY() - icon.getHeight() / 2.0f, null);
398 } 436 }
399 } 437 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698