Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/UIResources.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/UIResources.java b/content/public/android/java/src/org/chromium/content/browser/UIResources.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..73f487a0984b66a495a3bf2f9dcbff09ede0da52 |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/UIResources.java |
| @@ -0,0 +1,85 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser; |
| + |
| +import android.content.Context; |
| +import android.content.res.Resources; |
| +import android.graphics.Bitmap; |
| +import android.graphics.BitmapFactory; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| +import org.chromium.ui.R; |
| +import org.chromium.ui.SystemUIResourceType; |
| + |
| +/** |
| + * Helper class for retrieving both system and embedded UI-related resources. |
| + */ |
| +@JNINamespace("content") |
| +class UIResources { |
| + private static Bitmap getBitmap(Resources res, int resId, int reqWidth, int reqHeight) { |
| + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| + if (reqWidth > 0 && reqHeight > 0) { |
| + options.inJustDecodeBounds = true; |
| + BitmapFactory.decodeResource(res, resId, options); |
| + options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); |
| + } |
| + options.inJustDecodeBounds = false; |
| + options.inPreferredConfig = Bitmap.Config.ARGB_8888; |
| + return BitmapFactory.decodeResource(res, resId, options); |
| + } |
| + |
| + // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html |
| + private static int calculateInSampleSize(BitmapFactory.Options options, |
| + int reqWidth, |
|
Ted C
2014/10/31 23:52:06
this is c++ indenting...
I would put them all on
jdduke (slow)
2014/11/06 00:10:37
Done.
|
| + int reqHeight) { |
| + // Raw height and width of image |
| + final int height = options.outHeight; |
| + final int width = options.outWidth; |
| + int inSampleSize = 1; |
| + |
| + if (height > reqHeight || width > reqWidth) { |
| + |
|
Ted C
2014/10/31 23:52:06
remove this extra line
jdduke (slow)
2014/11/06 00:10:37
Done.
|
| + // Calculate ratios of height and width to requested height and width |
| + final int heightRatio = Math.round((float) height / (float) reqHeight); |
| + final int widthRatio = Math.round((float) width / (float) reqWidth); |
| + |
| + // Choose the smallest ratio as inSampleSize value, this will guarantee |
| + // a final image with both dimensions larger than or equal to the |
| + // requested height and width. |
| + inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; |
| + } |
| + |
| + return inSampleSize; |
| + } |
| + |
| + @CalledByNative |
| + private static Bitmap getBitmap( |
| + Context context, int uiResourceId, int reqWidth, int reqHeight) { |
| + Resources res = null; |
| + int resId = 0; |
| + |
| + if (uiResourceId == SystemUIResourceType.OVERSCROLL_EDGE) { |
| + res = Resources.getSystem(); |
| + resId = res.getIdentifier("android:drawable/overscroll_edge", null, null); |
| + } else if (uiResourceId == SystemUIResourceType.OVERSCROLL_GLOW) { |
| + res = Resources.getSystem(); |
| + resId = res.getIdentifier("android:drawable/overscroll_glow", null, null); |
| + } else if (uiResourceId == SystemUIResourceType.OVERSCROLL_REFRESH_IDLE) { |
| + res = context.getResources(); |
| + resId = R.drawable.refresh_gray; |
| + } else if (uiResourceId == SystemUIResourceType.OVERSCROLL_REFRESH_ACTIVE) { |
| + res = context.getResources(); |
| + resId = R.drawable.refresh_blue; |
| + } else { |
| + assert false; |
| + } |
| + |
| + if (res == null) return null; |
| + if (resId == 0) return null; |
| + |
| + return getBitmap(res, resId, reqWidth, reqHeight); |
| + } |
| +} |