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