OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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.content.browser.input; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.res.Resources; |
| 9 import android.content.res.TypedArray; |
| 10 import android.graphics.Bitmap; |
| 11 import android.graphics.BitmapFactory; |
| 12 import android.graphics.Canvas; |
| 13 import android.graphics.drawable.BitmapDrawable; |
| 14 import android.graphics.drawable.Drawable; |
| 15 |
| 16 import org.chromium.base.CalledByNative; |
| 17 import org.chromium.base.JNINamespace; |
| 18 |
| 19 /** |
| 20 * Helper class for retrieving resources related to selection handles. |
| 21 */ |
| 22 @JNINamespace("content") |
| 23 public class HandleViewResources { |
| 24 private static final int[] LEFT_HANDLE_ATTRS = { |
| 25 android.R.attr.textSelectHandleLeft, |
| 26 }; |
| 27 |
| 28 private static final int[] CENTER_HANDLE_ATTRS = { |
| 29 android.R.attr.textSelectHandle, |
| 30 }; |
| 31 |
| 32 private static final int[] RIGHT_HANDLE_ATTRS = { |
| 33 android.R.attr.textSelectHandleRight, |
| 34 }; |
| 35 |
| 36 public static Drawable getLeftHandleDrawable(Context context) { |
| 37 return getHandleDrawable(context, LEFT_HANDLE_ATTRS); |
| 38 } |
| 39 |
| 40 public static Drawable getCenterHandleDrawable(Context context) { |
| 41 return getHandleDrawable(context, CENTER_HANDLE_ATTRS); |
| 42 } |
| 43 |
| 44 public static Drawable getRightHandleDrawable(Context context) { |
| 45 return getHandleDrawable(context, RIGHT_HANDLE_ATTRS); |
| 46 } |
| 47 |
| 48 private static Drawable getHandleDrawable(Context context, final int[] attrs
) { |
| 49 TypedArray a = context.getTheme().obtainStyledAttributes(attrs); |
| 50 Drawable drawable = a.getDrawable(0); |
| 51 a.recycle(); |
| 52 return drawable; |
| 53 } |
| 54 |
| 55 private static Bitmap getHandleBitmap(Context context, final int[] attrs) { |
| 56 // TODO(jdduke): Properly derive and apply theme color. |
| 57 TypedArray a = context.getTheme().obtainStyledAttributes(attrs); |
| 58 final int resId = a.getResourceId(a.getIndex(0), 0); |
| 59 final Resources res = a.getResources(); |
| 60 a.recycle(); |
| 61 |
| 62 final Bitmap.Config config = Bitmap.Config.ARGB_8888; |
| 63 final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 64 options.inJustDecodeBounds = false; |
| 65 options.inPreferredConfig = config; |
| 66 final Bitmap bitmap = BitmapFactory.decodeResource(res, resId, options); |
| 67 if (bitmap != null) return bitmap; |
| 68 |
| 69 Drawable drawable = getHandleDrawable(context, attrs); |
| 70 assert drawable != null; |
| 71 if (drawable instanceof BitmapDrawable) { |
| 72 final Bitmap drawableBitmap = ((BitmapDrawable)drawable).getBitmap()
; |
| 73 if (drawableBitmap != null && drawableBitmap.getConfig() == config)
{ |
| 74 return drawableBitmap; |
| 75 } |
| 76 } |
| 77 |
| 78 final int width = drawable.getIntrinsicWidth(); |
| 79 final int height = drawable.getIntrinsicHeight(); |
| 80 Bitmap canvasBitmap = Bitmap.createBitmap(width, height, config); |
| 81 Canvas canvas = new Canvas(canvasBitmap); |
| 82 drawable.draw(canvas); |
| 83 return canvasBitmap; |
| 84 } |
| 85 |
| 86 @CalledByNative |
| 87 private static Bitmap getLeftHandleBitmap(Context context) { |
| 88 return getHandleBitmap(context, LEFT_HANDLE_ATTRS); |
| 89 } |
| 90 |
| 91 @CalledByNative |
| 92 private static Bitmap getCenterHandleBitmap(Context context) { |
| 93 return getHandleBitmap(context, CENTER_HANDLE_ATTRS); |
| 94 } |
| 95 |
| 96 @CalledByNative |
| 97 private static Bitmap getRightHandleBitmap(Context context) { |
| 98 return getHandleBitmap(context, RIGHT_HANDLE_ATTRS); |
| 99 } |
| 100 } |
OLD | NEW |