blob: 8cb46b704c18cf41759f52d494a8979de4422cc6 [file] [log] [blame]
Jeff Brown2352b972011-04-12 22:39:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Jun Mukaid4eaef72015-10-30 15:54:33 -070019import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.XmlRes;
Jeff Brown2352b972011-04-12 22:39:53 -070021import android.content.Context;
22import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.content.res.XmlResourceParser;
25import android.graphics.Bitmap;
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -050026import android.graphics.Canvas;
27import android.graphics.Paint;
28import android.graphics.Rect;
29import android.graphics.RectF;
Jun Mukai808196f2015-10-28 16:46:44 -070030import android.graphics.drawable.AnimationDrawable;
Jeff Brown2352b972011-04-12 22:39:53 -070031import android.graphics.drawable.BitmapDrawable;
32import android.graphics.drawable.Drawable;
33import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.Log;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070036import android.util.SparseArray;
37
38import com.android.internal.util.XmlUtils;
Jeff Brown2352b972011-04-12 22:39:53 -070039
40/**
41 * Represents an icon that can be used as a mouse pointer.
42 * <p>
Michael Wrighte051f6f2016-05-13 17:44:16 +010043 * Pointer icons can be provided either by the system using system types,
Jeff Brown2352b972011-04-12 22:39:53 -070044 * or by applications using bitmaps or application resources.
45 * </p>
Jeff Brown2352b972011-04-12 22:39:53 -070046 */
47public final class PointerIcon implements Parcelable {
48 private static final String TAG = "PointerIcon";
49
Michael Wrighte051f6f2016-05-13 17:44:16 +010050 /** {@hide} Type constant: Custom icon with a user-supplied bitmap. */
51 public static final int TYPE_CUSTOM = -1;
Jeff Brown2352b972011-04-12 22:39:53 -070052
Michael Wrighte051f6f2016-05-13 17:44:16 +010053 /** Type constant: Null icon. It has no bitmap. */
54 public static final int TYPE_NULL = 0;
Jeff Brown2352b972011-04-12 22:39:53 -070055
Michael Wrighte051f6f2016-05-13 17:44:16 +010056 /** Type constant: no icons are specified. If all views uses this, then falls back
57 * to the default type, but this is helpful to distinguish a view explicitly want
Jun Mukai1db53972015-09-11 18:08:31 -070058 * to have the default icon.
Jun Mukaid4eaef72015-10-30 15:54:33 -070059 * @hide
Jun Mukai1db53972015-09-11 18:08:31 -070060 */
Michael Wrighte051f6f2016-05-13 17:44:16 +010061 public static final int TYPE_NOT_SPECIFIED = 1;
Jun Mukai1db53972015-09-11 18:08:31 -070062
Michael Wrighte051f6f2016-05-13 17:44:16 +010063 /** Type constant: Arrow icon. (Default mouse pointer) */
64 public static final int TYPE_ARROW = 1000;
Jeff Brown2352b972011-04-12 22:39:53 -070065
Michael Wrighte051f6f2016-05-13 17:44:16 +010066 /** {@hide} Type constant: Spot hover icon for touchpads. */
67 public static final int TYPE_SPOT_HOVER = 2000;
Jeff Brown2352b972011-04-12 22:39:53 -070068
Michael Wrighte051f6f2016-05-13 17:44:16 +010069 /** {@hide} Type constant: Spot touch icon for touchpads. */
70 public static final int TYPE_SPOT_TOUCH = 2001;
Jeff Brown2352b972011-04-12 22:39:53 -070071
Michael Wrighte051f6f2016-05-13 17:44:16 +010072 /** {@hide} Type constant: Spot anchor icon for touchpads. */
73 public static final int TYPE_SPOT_ANCHOR = 2002;
Jeff Brown2352b972011-04-12 22:39:53 -070074
Michael Wrighte051f6f2016-05-13 17:44:16 +010075 // Type constants for additional predefined icons for mice.
76 /** Type constant: context-menu. */
77 public static final int TYPE_CONTEXT_MENU = 1001;
Jun Mukai1db53972015-09-11 18:08:31 -070078
Michael Wrighte051f6f2016-05-13 17:44:16 +010079 /** Type constant: hand. */
80 public static final int TYPE_HAND = 1002;
Jun Mukai1db53972015-09-11 18:08:31 -070081
Michael Wrighte051f6f2016-05-13 17:44:16 +010082 /** Type constant: help. */
83 public static final int TYPE_HELP = 1003;
Jun Mukai1db53972015-09-11 18:08:31 -070084
Michael Wrighte051f6f2016-05-13 17:44:16 +010085 /** Type constant: wait. */
86 public static final int TYPE_WAIT = 1004;
Jun Mukai1db53972015-09-11 18:08:31 -070087
Michael Wrighte051f6f2016-05-13 17:44:16 +010088 /** Type constant: cell. */
89 public static final int TYPE_CELL = 1006;
Jun Mukai1db53972015-09-11 18:08:31 -070090
Michael Wrighte051f6f2016-05-13 17:44:16 +010091 /** Type constant: crosshair. */
92 public static final int TYPE_CROSSHAIR = 1007;
Jun Mukai1db53972015-09-11 18:08:31 -070093
Michael Wrighte051f6f2016-05-13 17:44:16 +010094 /** Type constant: text. */
95 public static final int TYPE_TEXT = 1008;
Jun Mukai1db53972015-09-11 18:08:31 -070096
Michael Wrighte051f6f2016-05-13 17:44:16 +010097 /** Type constant: vertical-text. */
98 public static final int TYPE_VERTICAL_TEXT = 1009;
Jun Mukai1db53972015-09-11 18:08:31 -070099
Michael Wrighte051f6f2016-05-13 17:44:16 +0100100 /** Type constant: alias (indicating an alias of/shortcut to something is
Jun Mukai1db53972015-09-11 18:08:31 -0700101 * to be created. */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100102 public static final int TYPE_ALIAS = 1010;
Jun Mukai1db53972015-09-11 18:08:31 -0700103
Michael Wrighte051f6f2016-05-13 17:44:16 +0100104 /** Type constant: copy. */
105 public static final int TYPE_COPY = 1011;
Jun Mukai1db53972015-09-11 18:08:31 -0700106
Michael Wrighte051f6f2016-05-13 17:44:16 +0100107 /** Type constant: no-drop. */
108 public static final int TYPE_NO_DROP = 1012;
Jun Mukai1db53972015-09-11 18:08:31 -0700109
Michael Wrighte051f6f2016-05-13 17:44:16 +0100110 /** Type constant: all-scroll. */
111 public static final int TYPE_ALL_SCROLL = 1013;
Jun Mukai1db53972015-09-11 18:08:31 -0700112
Michael Wrighte051f6f2016-05-13 17:44:16 +0100113 /** Type constant: horizontal double arrow mainly for resizing. */
114 public static final int TYPE_HORIZONTAL_DOUBLE_ARROW = 1014;
Jun Mukai1db53972015-09-11 18:08:31 -0700115
Michael Wrighte051f6f2016-05-13 17:44:16 +0100116 /** Type constant: vertical double arrow mainly for resizing. */
117 public static final int TYPE_VERTICAL_DOUBLE_ARROW = 1015;
Jun Mukai1db53972015-09-11 18:08:31 -0700118
Michael Wrighte051f6f2016-05-13 17:44:16 +0100119 /** Type constant: diagonal double arrow -- top-right to bottom-left. */
120 public static final int TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016;
Jun Mukai1db53972015-09-11 18:08:31 -0700121
Michael Wrighte051f6f2016-05-13 17:44:16 +0100122 /** Type constant: diagonal double arrow -- top-left to bottom-right. */
123 public static final int TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017;
Jun Mukai1db53972015-09-11 18:08:31 -0700124
Michael Wrighte051f6f2016-05-13 17:44:16 +0100125 /** Type constant: zoom-in. */
126 public static final int TYPE_ZOOM_IN = 1018;
Jun Mukai1db53972015-09-11 18:08:31 -0700127
Michael Wrighte051f6f2016-05-13 17:44:16 +0100128 /** Type constant: zoom-out. */
129 public static final int TYPE_ZOOM_OUT = 1019;
Jun Mukai1db53972015-09-11 18:08:31 -0700130
Michael Wrighte051f6f2016-05-13 17:44:16 +0100131 /** Type constant: grab. */
132 public static final int TYPE_GRAB = 1020;
Jun Mukai1db53972015-09-11 18:08:31 -0700133
Michael Wrighte051f6f2016-05-13 17:44:16 +0100134 /** Type constant: grabbing. */
135 public static final int TYPE_GRABBING = 1021;
Jun Mukai1db53972015-09-11 18:08:31 -0700136
Michael Wrighte051f6f2016-05-13 17:44:16 +0100137 // OEM private types should be defined starting at this range to avoid
138 // conflicts with any system types that may be defined in the future.
139 private static final int TYPE_OEM_FIRST = 10000;
Jeff Brown2352b972011-04-12 22:39:53 -0700140
Jun Mukaid4eaef72015-10-30 15:54:33 -0700141 /** The default pointer icon. */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100142 public static final int TYPE_DEFAULT = TYPE_ARROW;
Jeff Brown2352b972011-04-12 22:39:53 -0700143
Michael Wrighte051f6f2016-05-13 17:44:16 +0100144 private static final PointerIcon gNullIcon = new PointerIcon(TYPE_NULL);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700145 private static final SparseArray<PointerIcon> gSystemIcons = new SparseArray<PointerIcon>();
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800146 private static boolean sUseLargeIcons = false;
Jun Mukaie4e75da2015-12-15 16:19:20 -0800147
Michael Wrighte051f6f2016-05-13 17:44:16 +0100148 private final int mType;
Jeff Brown2352b972011-04-12 22:39:53 -0700149 private int mSystemIconResourceId;
150 private Bitmap mBitmap;
151 private float mHotSpotX;
152 private float mHotSpotY;
Jun Mukai808196f2015-10-28 16:46:44 -0700153 // The bitmaps for the additional frame of animated pointer icon. Note that the first frame
154 // will be stored in mBitmap.
155 private Bitmap mBitmapFrames[];
156 private int mDurationPerFrame;
Jeff Brown2352b972011-04-12 22:39:53 -0700157
Michael Wrighte051f6f2016-05-13 17:44:16 +0100158 private PointerIcon(int type) {
159 mType = type;
Jeff Brown2352b972011-04-12 22:39:53 -0700160 }
161
162 /**
163 * Gets a special pointer icon that has no bitmap.
164 *
165 * @return The null pointer icon.
166 *
Michael Wrighte051f6f2016-05-13 17:44:16 +0100167 * @see #TYPE_NULL
Jun Mukaid4eaef72015-10-30 15:54:33 -0700168 * @hide
Jeff Brown2352b972011-04-12 22:39:53 -0700169 */
170 public static PointerIcon getNullIcon() {
171 return gNullIcon;
172 }
173
174 /**
175 * Gets the default pointer icon.
176 *
177 * @param context The context.
178 * @return The default pointer icon.
179 *
180 * @throws IllegalArgumentException if context is null.
Jun Mukaid4eaef72015-10-30 15:54:33 -0700181 * @hide
Jeff Brown2352b972011-04-12 22:39:53 -0700182 */
Jun Mukaid4eaef72015-10-30 15:54:33 -0700183 public static PointerIcon getDefaultIcon(@NonNull Context context) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100184 return getSystemIcon(context, TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700185 }
186
187 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100188 * Gets a system pointer icon for the given type.
189 * If typeis not recognized, returns the default pointer icon.
Jeff Brown2352b972011-04-12 22:39:53 -0700190 *
191 * @param context The context.
Michael Wrighte051f6f2016-05-13 17:44:16 +0100192 * @param type The pointer icon type.
Jeff Brown2352b972011-04-12 22:39:53 -0700193 * @return The pointer icon.
194 *
195 * @throws IllegalArgumentException if context is null.
196 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100197 public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
Jeff Brown2352b972011-04-12 22:39:53 -0700198 if (context == null) {
199 throw new IllegalArgumentException("context must not be null");
200 }
201
Michael Wrighte051f6f2016-05-13 17:44:16 +0100202 if (type == TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700203 return gNullIcon;
204 }
205
Michael Wrighte051f6f2016-05-13 17:44:16 +0100206 PointerIcon icon = gSystemIcons.get(type);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700207 if (icon != null) {
208 return icon;
209 }
210
Michael Wrighte051f6f2016-05-13 17:44:16 +0100211 int typeIndex = getSystemIconTypeIndex(type);
212 if (typeIndex == 0) {
213 typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700214 }
215
Jun Mukaie4e75da2015-12-15 16:19:20 -0800216 int defStyle = sUseLargeIcons ?
Jun Mukai19a56012015-11-24 11:25:52 -0800217 com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
Jeff Brown2352b972011-04-12 22:39:53 -0700218 TypedArray a = context.obtainStyledAttributes(null,
219 com.android.internal.R.styleable.Pointer,
Jun Mukai19a56012015-11-24 11:25:52 -0800220 0, defStyle);
Michael Wrighte051f6f2016-05-13 17:44:16 +0100221 int resourceId = a.getResourceId(typeIndex, -1);
Jeff Brown2352b972011-04-12 22:39:53 -0700222 a.recycle();
223
224 if (resourceId == -1) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100225 Log.w(TAG, "Missing theme resources for pointer icon type " + type);
226 return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700227 }
228
Michael Wrighte051f6f2016-05-13 17:44:16 +0100229 icon = new PointerIcon(type);
Jeff Brown2352b972011-04-12 22:39:53 -0700230 if ((resourceId & 0xff000000) == 0x01000000) {
231 icon.mSystemIconResourceId = resourceId;
232 } else {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800233 icon.loadResource(context, context.getResources(), resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700234 }
Michael Wrighte051f6f2016-05-13 17:44:16 +0100235 gSystemIcons.append(type, icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700236 return icon;
237 }
238
239 /**
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800240 * Updates wheter accessibility large icons are used or not.
241 * @hide
242 */
243 public static void setUseLargeIcons(boolean use) {
244 sUseLargeIcons = use;
245 gSystemIcons.clear();
246 }
247
248 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100249 * Creates a custom pointer icon from the given bitmap and hotspot information.
Jeff Brown2352b972011-04-12 22:39:53 -0700250 *
251 * @param bitmap The bitmap for the icon.
Alan Viverette376d10e2014-09-02 10:45:30 -0700252 * @param hotSpotX The X offset of the pointer icon hotspot in the bitmap.
Jeff Brown2352b972011-04-12 22:39:53 -0700253 * Must be within the [0, bitmap.getWidth()) range.
Alan Viverette376d10e2014-09-02 10:45:30 -0700254 * @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap.
Jeff Brown2352b972011-04-12 22:39:53 -0700255 * Must be within the [0, bitmap.getHeight()) range.
256 * @return A pointer icon for this bitmap.
257 *
258 * @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot
259 * parameters are invalid.
260 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100261 public static PointerIcon create(@NonNull Bitmap bitmap, float hotSpotX, float hotSpotY) {
Jeff Brown2352b972011-04-12 22:39:53 -0700262 if (bitmap == null) {
263 throw new IllegalArgumentException("bitmap must not be null");
264 }
265 validateHotSpot(bitmap, hotSpotX, hotSpotY);
266
Michael Wrighte051f6f2016-05-13 17:44:16 +0100267 PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
Jeff Brown2352b972011-04-12 22:39:53 -0700268 icon.mBitmap = bitmap;
269 icon.mHotSpotX = hotSpotX;
270 icon.mHotSpotY = hotSpotY;
271 return icon;
272 }
273
274 /**
275 * Loads a custom pointer icon from an XML resource.
276 * <p>
277 * The XML resource should have the following form:
278 * <code>
279 * &lt;?xml version="1.0" encoding="utf-8"?&gt;
280 * &lt;pointer-icon xmlns:android="http://schemas.android.com/apk/res/android"
281 * android:bitmap="@drawable/my_pointer_bitmap"
282 * android:hotSpotX="24"
283 * android:hotSpotY="24" /&gt;
284 * </code>
285 * </p>
286 *
287 * @param resources The resources object.
288 * @param resourceId The resource id.
289 * @return The pointer icon.
290 *
291 * @throws IllegalArgumentException if resources is null.
292 * @throws Resources.NotFoundException if the resource was not found or the drawable
293 * linked in the resource was not found.
294 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100295 public static PointerIcon load(@NonNull Resources resources, @XmlRes int resourceId) {
Jeff Brown2352b972011-04-12 22:39:53 -0700296 if (resources == null) {
297 throw new IllegalArgumentException("resources must not be null");
298 }
299
Michael Wrighte051f6f2016-05-13 17:44:16 +0100300 PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800301 icon.loadResource(null, resources, resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700302 return icon;
303 }
304
305 /**
306 * Loads the bitmap and hotspot information for a pointer icon, if it is not already loaded.
307 * Returns a pointer icon (not necessarily the same instance) with the information filled in.
308 *
309 * @param context The context.
310 * @return The loaded pointer icon.
311 *
312 * @throws IllegalArgumentException if context is null.
Jeff Brown2352b972011-04-12 22:39:53 -0700313 * @hide
314 */
Jun Mukaid4eaef72015-10-30 15:54:33 -0700315 public PointerIcon load(@NonNull Context context) {
Jeff Brown2352b972011-04-12 22:39:53 -0700316 if (context == null) {
317 throw new IllegalArgumentException("context must not be null");
318 }
319
320 if (mSystemIconResourceId == 0 || mBitmap != null) {
321 return this;
322 }
323
Michael Wrighte051f6f2016-05-13 17:44:16 +0100324 PointerIcon result = new PointerIcon(mType);
Jeff Brown2352b972011-04-12 22:39:53 -0700325 result.mSystemIconResourceId = mSystemIconResourceId;
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800326 result.loadResource(context, context.getResources(), mSystemIconResourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700327 return result;
328 }
329
Jun Mukaid4eaef72015-10-30 15:54:33 -0700330 /** @hide */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100331 public int getType() {
332 return mType;
Jeff Brown2352b972011-04-12 22:39:53 -0700333 }
334
Jeff Brown2352b972011-04-12 22:39:53 -0700335 public static final Parcelable.Creator<PointerIcon> CREATOR
336 = new Parcelable.Creator<PointerIcon>() {
337 public PointerIcon createFromParcel(Parcel in) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100338 int type = in.readInt();
339 if (type == TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700340 return getNullIcon();
341 }
342
343 int systemIconResourceId = in.readInt();
344 if (systemIconResourceId != 0) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100345 PointerIcon icon = new PointerIcon(type);
Jeff Brown2352b972011-04-12 22:39:53 -0700346 icon.mSystemIconResourceId = systemIconResourceId;
347 return icon;
348 }
349
350 Bitmap bitmap = Bitmap.CREATOR.createFromParcel(in);
351 float hotSpotX = in.readFloat();
352 float hotSpotY = in.readFloat();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100353 return PointerIcon.create(bitmap, hotSpotX, hotSpotY);
Jeff Brown2352b972011-04-12 22:39:53 -0700354 }
355
356 public PointerIcon[] newArray(int size) {
357 return new PointerIcon[size];
358 }
359 };
360
361 public int describeContents() {
362 return 0;
363 }
364
365 public void writeToParcel(Parcel out, int flags) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100366 out.writeInt(mType);
Jeff Brown2352b972011-04-12 22:39:53 -0700367
Michael Wrighte051f6f2016-05-13 17:44:16 +0100368 if (mType != TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700369 out.writeInt(mSystemIconResourceId);
370 if (mSystemIconResourceId == 0) {
371 mBitmap.writeToParcel(out, flags);
372 out.writeFloat(mHotSpotX);
373 out.writeFloat(mHotSpotY);
374 }
375 }
376 }
377
378 @Override
379 public boolean equals(Object other) {
380 if (this == other) {
381 return true;
382 }
383
384 if (other == null || !(other instanceof PointerIcon)) {
385 return false;
386 }
387
388 PointerIcon otherIcon = (PointerIcon) other;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100389 if (mType != otherIcon.mType
Jeff Brown2352b972011-04-12 22:39:53 -0700390 || mSystemIconResourceId != otherIcon.mSystemIconResourceId) {
391 return false;
392 }
393
394 if (mSystemIconResourceId == 0 && (mBitmap != otherIcon.mBitmap
395 || mHotSpotX != otherIcon.mHotSpotX
396 || mHotSpotY != otherIcon.mHotSpotY)) {
397 return false;
398 }
399
400 return true;
401 }
402
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500403 /**
404 * Get the Bitmap from the Drawable.
405 *
406 * If the Bitmap needed to be scaled up to account for density, BitmapDrawable
407 * handles this at draw time. But this class doesn't actually draw the Bitmap;
408 * it is just a holder for native code to access its SkBitmap. So this needs to
409 * get a version that is scaled to account for density.
410 */
411 private Bitmap getBitmapFromDrawable(BitmapDrawable bitmapDrawable) {
412 Bitmap bitmap = bitmapDrawable.getBitmap();
413 final int scaledWidth = bitmapDrawable.getIntrinsicWidth();
414 final int scaledHeight = bitmapDrawable.getIntrinsicHeight();
415 if (scaledWidth == bitmap.getWidth() && scaledHeight == bitmap.getHeight()) {
416 return bitmap;
417 }
418
419 Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
420 RectF dst = new RectF(0, 0, scaledWidth, scaledHeight);
421
422 Bitmap scaled = Bitmap.createBitmap(scaledWidth, scaledHeight, bitmap.getConfig());
423 Canvas canvas = new Canvas(scaled);
424 Paint paint = new Paint();
425 paint.setFilterBitmap(true);
426 canvas.drawBitmap(bitmap, src, dst, paint);
427 return scaled;
428 }
429
Tor Norbye7b9c9122013-05-30 16:48:33 -0700430 private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
Alan Viverette376d10e2014-09-02 10:45:30 -0700431 final XmlResourceParser parser = resources.getXml(resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700432 final int bitmapRes;
433 final float hotSpotX;
434 final float hotSpotY;
435 try {
436 XmlUtils.beginDocument(parser, "pointer-icon");
437
Alan Viverette376d10e2014-09-02 10:45:30 -0700438 final TypedArray a = resources.obtainAttributes(
Jeff Brown2352b972011-04-12 22:39:53 -0700439 parser, com.android.internal.R.styleable.PointerIcon);
440 bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
Alan Viverette376d10e2014-09-02 10:45:30 -0700441 hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
442 hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
Jeff Brown2352b972011-04-12 22:39:53 -0700443 a.recycle();
444 } catch (Exception ex) {
445 throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
446 } finally {
447 parser.close();
448 }
449
450 if (bitmapRes == 0) {
451 throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
452 }
453
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800454 Drawable drawable;
455 if (context == null) {
456 drawable = resources.getDrawable(bitmapRes);
457 } else {
458 drawable = context.getDrawable(bitmapRes);
459 }
Jun Mukai808196f2015-10-28 16:46:44 -0700460 if (drawable instanceof AnimationDrawable) {
461 // Extract animation frame bitmaps.
462 final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
463 final int frames = animationDrawable.getNumberOfFrames();
464 drawable = animationDrawable.getFrame(0);
465 if (frames == 1) {
466 Log.w(TAG, "Animation icon with single frame -- simply treating the first "
467 + "frame as a normal bitmap icon.");
468 } else {
469 // Assumes they have the exact duration.
470 mDurationPerFrame = animationDrawable.getDuration(0);
471 mBitmapFrames = new Bitmap[frames - 1];
472 final int width = drawable.getIntrinsicWidth();
473 final int height = drawable.getIntrinsicHeight();
474 for (int i = 1; i < frames; ++i) {
475 Drawable drawableFrame = animationDrawable.getFrame(i);
476 if (!(drawableFrame instanceof BitmapDrawable)) {
477 throw new IllegalArgumentException("Frame of an animated pointer icon "
478 + "must refer to a bitmap drawable.");
479 }
480 if (drawableFrame.getIntrinsicWidth() != width ||
481 drawableFrame.getIntrinsicHeight() != height) {
482 throw new IllegalArgumentException("The bitmap size of " + i + "-th frame "
483 + "is different. All frames should have the exact same size and "
484 + "share the same hotspot.");
485 }
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500486 BitmapDrawable bitmapDrawableFrame = (BitmapDrawable) drawableFrame;
487 mBitmapFrames[i - 1] = getBitmapFromDrawable(bitmapDrawableFrame);
Jun Mukai808196f2015-10-28 16:46:44 -0700488 }
489 }
490 }
Jeff Brown2352b972011-04-12 22:39:53 -0700491 if (!(drawable instanceof BitmapDrawable)) {
492 throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
493 + "refer to a bitmap drawable.");
494 }
495
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500496 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
497 final Bitmap bitmap = getBitmapFromDrawable(bitmapDrawable);
Vladislav Kaznacheev44e7de32017-12-20 12:18:03 -0800498 validateHotSpot(bitmap, hotSpotX, hotSpotY);
Jeff Brown2352b972011-04-12 22:39:53 -0700499 // Set the properties now that we have successfully loaded the icon.
Vladislav Kaznacheev44e7de32017-12-20 12:18:03 -0800500 mBitmap = bitmap;
Jeff Brown2352b972011-04-12 22:39:53 -0700501 mHotSpotX = hotSpotX;
502 mHotSpotY = hotSpotY;
503 }
504
505 private static void validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY) {
506 if (hotSpotX < 0 || hotSpotX >= bitmap.getWidth()) {
507 throw new IllegalArgumentException("x hotspot lies outside of the bitmap area");
508 }
509 if (hotSpotY < 0 || hotSpotY >= bitmap.getHeight()) {
510 throw new IllegalArgumentException("y hotspot lies outside of the bitmap area");
511 }
512 }
513
Michael Wrighte051f6f2016-05-13 17:44:16 +0100514 private static int getSystemIconTypeIndex(int type) {
515 switch (type) {
516 case TYPE_ARROW:
Jeff Brown2352b972011-04-12 22:39:53 -0700517 return com.android.internal.R.styleable.Pointer_pointerIconArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100518 case TYPE_SPOT_HOVER:
Jeff Brown2352b972011-04-12 22:39:53 -0700519 return com.android.internal.R.styleable.Pointer_pointerIconSpotHover;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100520 case TYPE_SPOT_TOUCH:
Jeff Brown2352b972011-04-12 22:39:53 -0700521 return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100522 case TYPE_SPOT_ANCHOR:
Jeff Brown2352b972011-04-12 22:39:53 -0700523 return com.android.internal.R.styleable.Pointer_pointerIconSpotAnchor;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100524 case TYPE_HAND:
Jun Mukai1db53972015-09-11 18:08:31 -0700525 return com.android.internal.R.styleable.Pointer_pointerIconHand;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100526 case TYPE_CONTEXT_MENU:
Jun Mukai1db53972015-09-11 18:08:31 -0700527 return com.android.internal.R.styleable.Pointer_pointerIconContextMenu;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100528 case TYPE_HELP:
Jun Mukai1db53972015-09-11 18:08:31 -0700529 return com.android.internal.R.styleable.Pointer_pointerIconHelp;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100530 case TYPE_WAIT:
Jun Mukai808196f2015-10-28 16:46:44 -0700531 return com.android.internal.R.styleable.Pointer_pointerIconWait;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100532 case TYPE_CELL:
Jun Mukai1db53972015-09-11 18:08:31 -0700533 return com.android.internal.R.styleable.Pointer_pointerIconCell;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100534 case TYPE_CROSSHAIR:
Jun Mukai1db53972015-09-11 18:08:31 -0700535 return com.android.internal.R.styleable.Pointer_pointerIconCrosshair;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100536 case TYPE_TEXT:
Jun Mukai1db53972015-09-11 18:08:31 -0700537 return com.android.internal.R.styleable.Pointer_pointerIconText;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100538 case TYPE_VERTICAL_TEXT:
Jun Mukai1db53972015-09-11 18:08:31 -0700539 return com.android.internal.R.styleable.Pointer_pointerIconVerticalText;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100540 case TYPE_ALIAS:
Jun Mukai1db53972015-09-11 18:08:31 -0700541 return com.android.internal.R.styleable.Pointer_pointerIconAlias;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100542 case TYPE_COPY:
Jun Mukai1db53972015-09-11 18:08:31 -0700543 return com.android.internal.R.styleable.Pointer_pointerIconCopy;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100544 case TYPE_ALL_SCROLL:
Jun Mukai1db53972015-09-11 18:08:31 -0700545 return com.android.internal.R.styleable.Pointer_pointerIconAllScroll;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100546 case TYPE_NO_DROP:
Jun Mukai1db53972015-09-11 18:08:31 -0700547 return com.android.internal.R.styleable.Pointer_pointerIconNodrop;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100548 case TYPE_HORIZONTAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700549 return com.android.internal.R.styleable.Pointer_pointerIconHorizontalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100550 case TYPE_VERTICAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700551 return com.android.internal.R.styleable.Pointer_pointerIconVerticalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100552 case TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700553 return com.android.internal.R.styleable.
554 Pointer_pointerIconTopRightDiagonalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100555 case TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700556 return com.android.internal.R.styleable.
557 Pointer_pointerIconTopLeftDiagonalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100558 case TYPE_ZOOM_IN:
Jun Mukai1db53972015-09-11 18:08:31 -0700559 return com.android.internal.R.styleable.Pointer_pointerIconZoomIn;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100560 case TYPE_ZOOM_OUT:
Jun Mukai1db53972015-09-11 18:08:31 -0700561 return com.android.internal.R.styleable.Pointer_pointerIconZoomOut;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100562 case TYPE_GRAB:
Jun Mukai1db53972015-09-11 18:08:31 -0700563 return com.android.internal.R.styleable.Pointer_pointerIconGrab;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100564 case TYPE_GRABBING:
Jun Mukai1db53972015-09-11 18:08:31 -0700565 return com.android.internal.R.styleable.Pointer_pointerIconGrabbing;
Jeff Brown2352b972011-04-12 22:39:53 -0700566 default:
567 return 0;
568 }
569 }
570}