blob: 998fd0195a986984a7ecf53c989e5e95dd94dbd3 [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;
Jun Mukai808196f2015-10-28 16:46:44 -070026import android.graphics.drawable.AnimationDrawable;
Jeff Brown2352b972011-04-12 22:39:53 -070027import android.graphics.drawable.BitmapDrawable;
28import android.graphics.drawable.Drawable;
29import android.os.Parcel;
30import android.os.Parcelable;
31import android.util.Log;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070032import android.util.SparseArray;
33
34import com.android.internal.util.XmlUtils;
Jeff Brown2352b972011-04-12 22:39:53 -070035
36/**
37 * Represents an icon that can be used as a mouse pointer.
38 * <p>
Michael Wrighte051f6f2016-05-13 17:44:16 +010039 * Pointer icons can be provided either by the system using system types,
Jeff Brown2352b972011-04-12 22:39:53 -070040 * or by applications using bitmaps or application resources.
41 * </p>
Jeff Brown2352b972011-04-12 22:39:53 -070042 */
43public final class PointerIcon implements Parcelable {
44 private static final String TAG = "PointerIcon";
45
Michael Wrighte051f6f2016-05-13 17:44:16 +010046 /** {@hide} Type constant: Custom icon with a user-supplied bitmap. */
47 public static final int TYPE_CUSTOM = -1;
Jeff Brown2352b972011-04-12 22:39:53 -070048
Michael Wrighte051f6f2016-05-13 17:44:16 +010049 /** Type constant: Null icon. It has no bitmap. */
50 public static final int TYPE_NULL = 0;
Jeff Brown2352b972011-04-12 22:39:53 -070051
Michael Wrighte051f6f2016-05-13 17:44:16 +010052 /** Type constant: no icons are specified. If all views uses this, then falls back
53 * to the default type, but this is helpful to distinguish a view explicitly want
Jun Mukai1db53972015-09-11 18:08:31 -070054 * to have the default icon.
Jun Mukaid4eaef72015-10-30 15:54:33 -070055 * @hide
Jun Mukai1db53972015-09-11 18:08:31 -070056 */
Michael Wrighte051f6f2016-05-13 17:44:16 +010057 public static final int TYPE_NOT_SPECIFIED = 1;
Jun Mukai1db53972015-09-11 18:08:31 -070058
Michael Wrighte051f6f2016-05-13 17:44:16 +010059 /** Type constant: Arrow icon. (Default mouse pointer) */
60 public static final int TYPE_ARROW = 1000;
Jeff Brown2352b972011-04-12 22:39:53 -070061
Michael Wrighte051f6f2016-05-13 17:44:16 +010062 /** {@hide} Type constant: Spot hover icon for touchpads. */
63 public static final int TYPE_SPOT_HOVER = 2000;
Jeff Brown2352b972011-04-12 22:39:53 -070064
Michael Wrighte051f6f2016-05-13 17:44:16 +010065 /** {@hide} Type constant: Spot touch icon for touchpads. */
66 public static final int TYPE_SPOT_TOUCH = 2001;
Jeff Brown2352b972011-04-12 22:39:53 -070067
Michael Wrighte051f6f2016-05-13 17:44:16 +010068 /** {@hide} Type constant: Spot anchor icon for touchpads. */
69 public static final int TYPE_SPOT_ANCHOR = 2002;
Jeff Brown2352b972011-04-12 22:39:53 -070070
Michael Wrighte051f6f2016-05-13 17:44:16 +010071 // Type constants for additional predefined icons for mice.
72 /** Type constant: context-menu. */
73 public static final int TYPE_CONTEXT_MENU = 1001;
Jun Mukai1db53972015-09-11 18:08:31 -070074
Michael Wrighte051f6f2016-05-13 17:44:16 +010075 /** Type constant: hand. */
76 public static final int TYPE_HAND = 1002;
Jun Mukai1db53972015-09-11 18:08:31 -070077
Michael Wrighte051f6f2016-05-13 17:44:16 +010078 /** Type constant: help. */
79 public static final int TYPE_HELP = 1003;
Jun Mukai1db53972015-09-11 18:08:31 -070080
Michael Wrighte051f6f2016-05-13 17:44:16 +010081 /** Type constant: wait. */
82 public static final int TYPE_WAIT = 1004;
Jun Mukai1db53972015-09-11 18:08:31 -070083
Michael Wrighte051f6f2016-05-13 17:44:16 +010084 /** Type constant: cell. */
85 public static final int TYPE_CELL = 1006;
Jun Mukai1db53972015-09-11 18:08:31 -070086
Michael Wrighte051f6f2016-05-13 17:44:16 +010087 /** Type constant: crosshair. */
88 public static final int TYPE_CROSSHAIR = 1007;
Jun Mukai1db53972015-09-11 18:08:31 -070089
Michael Wrighte051f6f2016-05-13 17:44:16 +010090 /** Type constant: text. */
91 public static final int TYPE_TEXT = 1008;
Jun Mukai1db53972015-09-11 18:08:31 -070092
Michael Wrighte051f6f2016-05-13 17:44:16 +010093 /** Type constant: vertical-text. */
94 public static final int TYPE_VERTICAL_TEXT = 1009;
Jun Mukai1db53972015-09-11 18:08:31 -070095
Michael Wrighte051f6f2016-05-13 17:44:16 +010096 /** Type constant: alias (indicating an alias of/shortcut to something is
Jun Mukai1db53972015-09-11 18:08:31 -070097 * to be created. */
Michael Wrighte051f6f2016-05-13 17:44:16 +010098 public static final int TYPE_ALIAS = 1010;
Jun Mukai1db53972015-09-11 18:08:31 -070099
Michael Wrighte051f6f2016-05-13 17:44:16 +0100100 /** Type constant: copy. */
101 public static final int TYPE_COPY = 1011;
Jun Mukai1db53972015-09-11 18:08:31 -0700102
Michael Wrighte051f6f2016-05-13 17:44:16 +0100103 /** Type constant: no-drop. */
104 public static final int TYPE_NO_DROP = 1012;
Jun Mukai1db53972015-09-11 18:08:31 -0700105
Michael Wrighte051f6f2016-05-13 17:44:16 +0100106 /** Type constant: all-scroll. */
107 public static final int TYPE_ALL_SCROLL = 1013;
Jun Mukai1db53972015-09-11 18:08:31 -0700108
Michael Wrighte051f6f2016-05-13 17:44:16 +0100109 /** Type constant: horizontal double arrow mainly for resizing. */
110 public static final int TYPE_HORIZONTAL_DOUBLE_ARROW = 1014;
Jun Mukai1db53972015-09-11 18:08:31 -0700111
Michael Wrighte051f6f2016-05-13 17:44:16 +0100112 /** Type constant: vertical double arrow mainly for resizing. */
113 public static final int TYPE_VERTICAL_DOUBLE_ARROW = 1015;
Jun Mukai1db53972015-09-11 18:08:31 -0700114
Michael Wrighte051f6f2016-05-13 17:44:16 +0100115 /** Type constant: diagonal double arrow -- top-right to bottom-left. */
116 public static final int TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016;
Jun Mukai1db53972015-09-11 18:08:31 -0700117
Michael Wrighte051f6f2016-05-13 17:44:16 +0100118 /** Type constant: diagonal double arrow -- top-left to bottom-right. */
119 public static final int TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017;
Jun Mukai1db53972015-09-11 18:08:31 -0700120
Michael Wrighte051f6f2016-05-13 17:44:16 +0100121 /** Type constant: zoom-in. */
122 public static final int TYPE_ZOOM_IN = 1018;
Jun Mukai1db53972015-09-11 18:08:31 -0700123
Michael Wrighte051f6f2016-05-13 17:44:16 +0100124 /** Type constant: zoom-out. */
125 public static final int TYPE_ZOOM_OUT = 1019;
Jun Mukai1db53972015-09-11 18:08:31 -0700126
Michael Wrighte051f6f2016-05-13 17:44:16 +0100127 /** Type constant: grab. */
128 public static final int TYPE_GRAB = 1020;
Jun Mukai1db53972015-09-11 18:08:31 -0700129
Michael Wrighte051f6f2016-05-13 17:44:16 +0100130 /** Type constant: grabbing. */
131 public static final int TYPE_GRABBING = 1021;
Jun Mukai1db53972015-09-11 18:08:31 -0700132
Michael Wrighte051f6f2016-05-13 17:44:16 +0100133 // OEM private types should be defined starting at this range to avoid
134 // conflicts with any system types that may be defined in the future.
135 private static final int TYPE_OEM_FIRST = 10000;
Jeff Brown2352b972011-04-12 22:39:53 -0700136
Jun Mukaid4eaef72015-10-30 15:54:33 -0700137 /** The default pointer icon. */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100138 public static final int TYPE_DEFAULT = TYPE_ARROW;
Jeff Brown2352b972011-04-12 22:39:53 -0700139
Michael Wrighte051f6f2016-05-13 17:44:16 +0100140 private static final PointerIcon gNullIcon = new PointerIcon(TYPE_NULL);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700141 private static final SparseArray<PointerIcon> gSystemIcons = new SparseArray<PointerIcon>();
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800142 private static boolean sUseLargeIcons = false;
Jun Mukaie4e75da2015-12-15 16:19:20 -0800143
Michael Wrighte051f6f2016-05-13 17:44:16 +0100144 private final int mType;
Jeff Brown2352b972011-04-12 22:39:53 -0700145 private int mSystemIconResourceId;
146 private Bitmap mBitmap;
147 private float mHotSpotX;
148 private float mHotSpotY;
Jun Mukai808196f2015-10-28 16:46:44 -0700149 // The bitmaps for the additional frame of animated pointer icon. Note that the first frame
150 // will be stored in mBitmap.
151 private Bitmap mBitmapFrames[];
152 private int mDurationPerFrame;
Jeff Brown2352b972011-04-12 22:39:53 -0700153
Michael Wrighte051f6f2016-05-13 17:44:16 +0100154 private PointerIcon(int type) {
155 mType = type;
Jeff Brown2352b972011-04-12 22:39:53 -0700156 }
157
158 /**
159 * Gets a special pointer icon that has no bitmap.
160 *
161 * @return The null pointer icon.
162 *
Michael Wrighte051f6f2016-05-13 17:44:16 +0100163 * @see #TYPE_NULL
Jun Mukaid4eaef72015-10-30 15:54:33 -0700164 * @hide
Jeff Brown2352b972011-04-12 22:39:53 -0700165 */
166 public static PointerIcon getNullIcon() {
167 return gNullIcon;
168 }
169
170 /**
171 * Gets the default pointer icon.
172 *
173 * @param context The context.
174 * @return The default pointer icon.
175 *
176 * @throws IllegalArgumentException if context is null.
Jun Mukaid4eaef72015-10-30 15:54:33 -0700177 * @hide
Jeff Brown2352b972011-04-12 22:39:53 -0700178 */
Jun Mukaid4eaef72015-10-30 15:54:33 -0700179 public static PointerIcon getDefaultIcon(@NonNull Context context) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100180 return getSystemIcon(context, TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700181 }
182
183 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100184 * Gets a system pointer icon for the given type.
185 * If typeis not recognized, returns the default pointer icon.
Jeff Brown2352b972011-04-12 22:39:53 -0700186 *
187 * @param context The context.
Michael Wrighte051f6f2016-05-13 17:44:16 +0100188 * @param type The pointer icon type.
Jeff Brown2352b972011-04-12 22:39:53 -0700189 * @return The pointer icon.
190 *
191 * @throws IllegalArgumentException if context is null.
192 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100193 public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
Jeff Brown2352b972011-04-12 22:39:53 -0700194 if (context == null) {
195 throw new IllegalArgumentException("context must not be null");
196 }
197
Michael Wrighte051f6f2016-05-13 17:44:16 +0100198 if (type == TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700199 return gNullIcon;
200 }
201
Michael Wrighte051f6f2016-05-13 17:44:16 +0100202 PointerIcon icon = gSystemIcons.get(type);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700203 if (icon != null) {
204 return icon;
205 }
206
Michael Wrighte051f6f2016-05-13 17:44:16 +0100207 int typeIndex = getSystemIconTypeIndex(type);
208 if (typeIndex == 0) {
209 typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700210 }
211
Jun Mukaie4e75da2015-12-15 16:19:20 -0800212 int defStyle = sUseLargeIcons ?
Jun Mukai19a56012015-11-24 11:25:52 -0800213 com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
Jeff Brown2352b972011-04-12 22:39:53 -0700214 TypedArray a = context.obtainStyledAttributes(null,
215 com.android.internal.R.styleable.Pointer,
Jun Mukai19a56012015-11-24 11:25:52 -0800216 0, defStyle);
Michael Wrighte051f6f2016-05-13 17:44:16 +0100217 int resourceId = a.getResourceId(typeIndex, -1);
Jeff Brown2352b972011-04-12 22:39:53 -0700218 a.recycle();
219
220 if (resourceId == -1) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100221 Log.w(TAG, "Missing theme resources for pointer icon type " + type);
222 return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700223 }
224
Michael Wrighte051f6f2016-05-13 17:44:16 +0100225 icon = new PointerIcon(type);
Jeff Brown2352b972011-04-12 22:39:53 -0700226 if ((resourceId & 0xff000000) == 0x01000000) {
227 icon.mSystemIconResourceId = resourceId;
228 } else {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800229 icon.loadResource(context, context.getResources(), resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700230 }
Michael Wrighte051f6f2016-05-13 17:44:16 +0100231 gSystemIcons.append(type, icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700232 return icon;
233 }
234
235 /**
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800236 * Updates wheter accessibility large icons are used or not.
237 * @hide
238 */
239 public static void setUseLargeIcons(boolean use) {
240 sUseLargeIcons = use;
241 gSystemIcons.clear();
242 }
243
244 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100245 * Creates a custom pointer icon from the given bitmap and hotspot information.
Jeff Brown2352b972011-04-12 22:39:53 -0700246 *
247 * @param bitmap The bitmap for the icon.
Alan Viverette376d10e2014-09-02 10:45:30 -0700248 * @param hotSpotX The X offset of the pointer icon hotspot in the bitmap.
Jeff Brown2352b972011-04-12 22:39:53 -0700249 * Must be within the [0, bitmap.getWidth()) range.
Alan Viverette376d10e2014-09-02 10:45:30 -0700250 * @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap.
Jeff Brown2352b972011-04-12 22:39:53 -0700251 * Must be within the [0, bitmap.getHeight()) range.
252 * @return A pointer icon for this bitmap.
253 *
254 * @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot
255 * parameters are invalid.
256 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100257 public static PointerIcon create(@NonNull Bitmap bitmap, float hotSpotX, float hotSpotY) {
Jeff Brown2352b972011-04-12 22:39:53 -0700258 if (bitmap == null) {
259 throw new IllegalArgumentException("bitmap must not be null");
260 }
261 validateHotSpot(bitmap, hotSpotX, hotSpotY);
262
Michael Wrighte051f6f2016-05-13 17:44:16 +0100263 PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
Jeff Brown2352b972011-04-12 22:39:53 -0700264 icon.mBitmap = bitmap;
265 icon.mHotSpotX = hotSpotX;
266 icon.mHotSpotY = hotSpotY;
267 return icon;
268 }
269
270 /**
271 * Loads a custom pointer icon from an XML resource.
272 * <p>
273 * The XML resource should have the following form:
274 * <code>
275 * &lt;?xml version="1.0" encoding="utf-8"?&gt;
276 * &lt;pointer-icon xmlns:android="http://schemas.android.com/apk/res/android"
277 * android:bitmap="@drawable/my_pointer_bitmap"
278 * android:hotSpotX="24"
279 * android:hotSpotY="24" /&gt;
280 * </code>
281 * </p>
282 *
283 * @param resources The resources object.
284 * @param resourceId The resource id.
285 * @return The pointer icon.
286 *
287 * @throws IllegalArgumentException if resources is null.
288 * @throws Resources.NotFoundException if the resource was not found or the drawable
289 * linked in the resource was not found.
290 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100291 public static PointerIcon load(@NonNull Resources resources, @XmlRes int resourceId) {
Jeff Brown2352b972011-04-12 22:39:53 -0700292 if (resources == null) {
293 throw new IllegalArgumentException("resources must not be null");
294 }
295
Michael Wrighte051f6f2016-05-13 17:44:16 +0100296 PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800297 icon.loadResource(null, resources, resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700298 return icon;
299 }
300
301 /**
302 * Loads the bitmap and hotspot information for a pointer icon, if it is not already loaded.
303 * Returns a pointer icon (not necessarily the same instance) with the information filled in.
304 *
305 * @param context The context.
306 * @return The loaded pointer icon.
307 *
308 * @throws IllegalArgumentException if context is null.
Jeff Brown2352b972011-04-12 22:39:53 -0700309 * @hide
310 */
Jun Mukaid4eaef72015-10-30 15:54:33 -0700311 public PointerIcon load(@NonNull Context context) {
Jeff Brown2352b972011-04-12 22:39:53 -0700312 if (context == null) {
313 throw new IllegalArgumentException("context must not be null");
314 }
315
316 if (mSystemIconResourceId == 0 || mBitmap != null) {
317 return this;
318 }
319
Michael Wrighte051f6f2016-05-13 17:44:16 +0100320 PointerIcon result = new PointerIcon(mType);
Jeff Brown2352b972011-04-12 22:39:53 -0700321 result.mSystemIconResourceId = mSystemIconResourceId;
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800322 result.loadResource(context, context.getResources(), mSystemIconResourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700323 return result;
324 }
325
Jun Mukaid4eaef72015-10-30 15:54:33 -0700326 /** @hide */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100327 public int getType() {
328 return mType;
Jeff Brown2352b972011-04-12 22:39:53 -0700329 }
330
Jeff Brown2352b972011-04-12 22:39:53 -0700331 public static final Parcelable.Creator<PointerIcon> CREATOR
332 = new Parcelable.Creator<PointerIcon>() {
333 public PointerIcon createFromParcel(Parcel in) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100334 int type = in.readInt();
335 if (type == TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700336 return getNullIcon();
337 }
338
339 int systemIconResourceId = in.readInt();
340 if (systemIconResourceId != 0) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100341 PointerIcon icon = new PointerIcon(type);
Jeff Brown2352b972011-04-12 22:39:53 -0700342 icon.mSystemIconResourceId = systemIconResourceId;
343 return icon;
344 }
345
346 Bitmap bitmap = Bitmap.CREATOR.createFromParcel(in);
347 float hotSpotX = in.readFloat();
348 float hotSpotY = in.readFloat();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100349 return PointerIcon.create(bitmap, hotSpotX, hotSpotY);
Jeff Brown2352b972011-04-12 22:39:53 -0700350 }
351
352 public PointerIcon[] newArray(int size) {
353 return new PointerIcon[size];
354 }
355 };
356
357 public int describeContents() {
358 return 0;
359 }
360
361 public void writeToParcel(Parcel out, int flags) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100362 out.writeInt(mType);
Jeff Brown2352b972011-04-12 22:39:53 -0700363
Michael Wrighte051f6f2016-05-13 17:44:16 +0100364 if (mType != TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700365 out.writeInt(mSystemIconResourceId);
366 if (mSystemIconResourceId == 0) {
367 mBitmap.writeToParcel(out, flags);
368 out.writeFloat(mHotSpotX);
369 out.writeFloat(mHotSpotY);
370 }
371 }
372 }
373
374 @Override
375 public boolean equals(Object other) {
376 if (this == other) {
377 return true;
378 }
379
380 if (other == null || !(other instanceof PointerIcon)) {
381 return false;
382 }
383
384 PointerIcon otherIcon = (PointerIcon) other;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100385 if (mType != otherIcon.mType
Jeff Brown2352b972011-04-12 22:39:53 -0700386 || mSystemIconResourceId != otherIcon.mSystemIconResourceId) {
387 return false;
388 }
389
390 if (mSystemIconResourceId == 0 && (mBitmap != otherIcon.mBitmap
391 || mHotSpotX != otherIcon.mHotSpotX
392 || mHotSpotY != otherIcon.mHotSpotY)) {
393 return false;
394 }
395
396 return true;
397 }
398
Tor Norbye7b9c9122013-05-30 16:48:33 -0700399 private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
Alan Viverette376d10e2014-09-02 10:45:30 -0700400 final XmlResourceParser parser = resources.getXml(resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700401 final int bitmapRes;
402 final float hotSpotX;
403 final float hotSpotY;
404 try {
405 XmlUtils.beginDocument(parser, "pointer-icon");
406
Alan Viverette376d10e2014-09-02 10:45:30 -0700407 final TypedArray a = resources.obtainAttributes(
Jeff Brown2352b972011-04-12 22:39:53 -0700408 parser, com.android.internal.R.styleable.PointerIcon);
409 bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
Alan Viverette376d10e2014-09-02 10:45:30 -0700410 hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
411 hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
Jeff Brown2352b972011-04-12 22:39:53 -0700412 a.recycle();
413 } catch (Exception ex) {
414 throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
415 } finally {
416 parser.close();
417 }
418
419 if (bitmapRes == 0) {
420 throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
421 }
422
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800423 Drawable drawable;
424 if (context == null) {
425 drawable = resources.getDrawable(bitmapRes);
426 } else {
427 drawable = context.getDrawable(bitmapRes);
428 }
Jun Mukai808196f2015-10-28 16:46:44 -0700429 if (drawable instanceof AnimationDrawable) {
430 // Extract animation frame bitmaps.
431 final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
432 final int frames = animationDrawable.getNumberOfFrames();
433 drawable = animationDrawable.getFrame(0);
434 if (frames == 1) {
435 Log.w(TAG, "Animation icon with single frame -- simply treating the first "
436 + "frame as a normal bitmap icon.");
437 } else {
438 // Assumes they have the exact duration.
439 mDurationPerFrame = animationDrawable.getDuration(0);
440 mBitmapFrames = new Bitmap[frames - 1];
441 final int width = drawable.getIntrinsicWidth();
442 final int height = drawable.getIntrinsicHeight();
443 for (int i = 1; i < frames; ++i) {
444 Drawable drawableFrame = animationDrawable.getFrame(i);
445 if (!(drawableFrame instanceof BitmapDrawable)) {
446 throw new IllegalArgumentException("Frame of an animated pointer icon "
447 + "must refer to a bitmap drawable.");
448 }
449 if (drawableFrame.getIntrinsicWidth() != width ||
450 drawableFrame.getIntrinsicHeight() != height) {
451 throw new IllegalArgumentException("The bitmap size of " + i + "-th frame "
452 + "is different. All frames should have the exact same size and "
453 + "share the same hotspot.");
454 }
455 mBitmapFrames[i - 1] = ((BitmapDrawable)drawableFrame).getBitmap();
456 }
457 }
458 }
Jeff Brown2352b972011-04-12 22:39:53 -0700459 if (!(drawable instanceof BitmapDrawable)) {
460 throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
461 + "refer to a bitmap drawable.");
462 }
463
464 // Set the properties now that we have successfully loaded the icon.
465 mBitmap = ((BitmapDrawable)drawable).getBitmap();
466 mHotSpotX = hotSpotX;
467 mHotSpotY = hotSpotY;
468 }
469
470 private static void validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY) {
471 if (hotSpotX < 0 || hotSpotX >= bitmap.getWidth()) {
472 throw new IllegalArgumentException("x hotspot lies outside of the bitmap area");
473 }
474 if (hotSpotY < 0 || hotSpotY >= bitmap.getHeight()) {
475 throw new IllegalArgumentException("y hotspot lies outside of the bitmap area");
476 }
477 }
478
Michael Wrighte051f6f2016-05-13 17:44:16 +0100479 private static int getSystemIconTypeIndex(int type) {
480 switch (type) {
481 case TYPE_ARROW:
Jeff Brown2352b972011-04-12 22:39:53 -0700482 return com.android.internal.R.styleable.Pointer_pointerIconArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100483 case TYPE_SPOT_HOVER:
Jeff Brown2352b972011-04-12 22:39:53 -0700484 return com.android.internal.R.styleable.Pointer_pointerIconSpotHover;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100485 case TYPE_SPOT_TOUCH:
Jeff Brown2352b972011-04-12 22:39:53 -0700486 return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100487 case TYPE_SPOT_ANCHOR:
Jeff Brown2352b972011-04-12 22:39:53 -0700488 return com.android.internal.R.styleable.Pointer_pointerIconSpotAnchor;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100489 case TYPE_HAND:
Jun Mukai1db53972015-09-11 18:08:31 -0700490 return com.android.internal.R.styleable.Pointer_pointerIconHand;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100491 case TYPE_CONTEXT_MENU:
Jun Mukai1db53972015-09-11 18:08:31 -0700492 return com.android.internal.R.styleable.Pointer_pointerIconContextMenu;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100493 case TYPE_HELP:
Jun Mukai1db53972015-09-11 18:08:31 -0700494 return com.android.internal.R.styleable.Pointer_pointerIconHelp;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100495 case TYPE_WAIT:
Jun Mukai808196f2015-10-28 16:46:44 -0700496 return com.android.internal.R.styleable.Pointer_pointerIconWait;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100497 case TYPE_CELL:
Jun Mukai1db53972015-09-11 18:08:31 -0700498 return com.android.internal.R.styleable.Pointer_pointerIconCell;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100499 case TYPE_CROSSHAIR:
Jun Mukai1db53972015-09-11 18:08:31 -0700500 return com.android.internal.R.styleable.Pointer_pointerIconCrosshair;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100501 case TYPE_TEXT:
Jun Mukai1db53972015-09-11 18:08:31 -0700502 return com.android.internal.R.styleable.Pointer_pointerIconText;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100503 case TYPE_VERTICAL_TEXT:
Jun Mukai1db53972015-09-11 18:08:31 -0700504 return com.android.internal.R.styleable.Pointer_pointerIconVerticalText;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100505 case TYPE_ALIAS:
Jun Mukai1db53972015-09-11 18:08:31 -0700506 return com.android.internal.R.styleable.Pointer_pointerIconAlias;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100507 case TYPE_COPY:
Jun Mukai1db53972015-09-11 18:08:31 -0700508 return com.android.internal.R.styleable.Pointer_pointerIconCopy;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100509 case TYPE_ALL_SCROLL:
Jun Mukai1db53972015-09-11 18:08:31 -0700510 return com.android.internal.R.styleable.Pointer_pointerIconAllScroll;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100511 case TYPE_NO_DROP:
Jun Mukai1db53972015-09-11 18:08:31 -0700512 return com.android.internal.R.styleable.Pointer_pointerIconNodrop;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100513 case TYPE_HORIZONTAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700514 return com.android.internal.R.styleable.Pointer_pointerIconHorizontalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100515 case TYPE_VERTICAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700516 return com.android.internal.R.styleable.Pointer_pointerIconVerticalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100517 case TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700518 return com.android.internal.R.styleable.
519 Pointer_pointerIconTopRightDiagonalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100520 case TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700521 return com.android.internal.R.styleable.
522 Pointer_pointerIconTopLeftDiagonalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100523 case TYPE_ZOOM_IN:
Jun Mukai1db53972015-09-11 18:08:31 -0700524 return com.android.internal.R.styleable.Pointer_pointerIconZoomIn;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100525 case TYPE_ZOOM_OUT:
Jun Mukai1db53972015-09-11 18:08:31 -0700526 return com.android.internal.R.styleable.Pointer_pointerIconZoomOut;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100527 case TYPE_GRAB:
Jun Mukai1db53972015-09-11 18:08:31 -0700528 return com.android.internal.R.styleable.Pointer_pointerIconGrab;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100529 case TYPE_GRABBING:
Jun Mukai1db53972015-09-11 18:08:31 -0700530 return com.android.internal.R.styleable.Pointer_pointerIconGrabbing;
Jeff Brown2352b972011-04-12 22:39:53 -0700531 default:
532 return 0;
533 }
534 }
535}