blob: cf11fd04efdfe8200f0374c38a3672be98eae6e0 [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;
Mathew Inwoode5ad5982018-08-17 15:07:52 +010020import android.annotation.UnsupportedAppUsage;
Tor Norbye7b9c9122013-05-30 16:48:33 -070021import android.annotation.XmlRes;
Jeff Brown2352b972011-04-12 22:39:53 -070022import android.content.Context;
23import android.content.res.Resources;
24import android.content.res.TypedArray;
25import android.content.res.XmlResourceParser;
26import android.graphics.Bitmap;
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -050027import android.graphics.Canvas;
28import android.graphics.Paint;
29import android.graphics.Rect;
30import android.graphics.RectF;
Jun Mukai808196f2015-10-28 16:46:44 -070031import android.graphics.drawable.AnimationDrawable;
Jeff Brown2352b972011-04-12 22:39:53 -070032import android.graphics.drawable.BitmapDrawable;
33import android.graphics.drawable.Drawable;
Mathew Inwood45d2c252018-09-14 12:35:36 +010034import android.os.Build;
Jeff Brown2352b972011-04-12 22:39:53 -070035import android.os.Parcel;
36import android.os.Parcelable;
37import android.util.Log;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070038import android.util.SparseArray;
39
40import com.android.internal.util.XmlUtils;
Jeff Brown2352b972011-04-12 22:39:53 -070041
42/**
43 * Represents an icon that can be used as a mouse pointer.
44 * <p>
Michael Wrighte051f6f2016-05-13 17:44:16 +010045 * Pointer icons can be provided either by the system using system types,
Jeff Brown2352b972011-04-12 22:39:53 -070046 * or by applications using bitmaps or application resources.
47 * </p>
Jeff Brown2352b972011-04-12 22:39:53 -070048 */
49public final class PointerIcon implements Parcelable {
50 private static final String TAG = "PointerIcon";
51
Michael Wrighte051f6f2016-05-13 17:44:16 +010052 /** {@hide} Type constant: Custom icon with a user-supplied bitmap. */
53 public static final int TYPE_CUSTOM = -1;
Jeff Brown2352b972011-04-12 22:39:53 -070054
Michael Wrighte051f6f2016-05-13 17:44:16 +010055 /** Type constant: Null icon. It has no bitmap. */
56 public static final int TYPE_NULL = 0;
Jeff Brown2352b972011-04-12 22:39:53 -070057
Michael Wrighte051f6f2016-05-13 17:44:16 +010058 /** Type constant: no icons are specified. If all views uses this, then falls back
59 * to the default type, but this is helpful to distinguish a view explicitly want
Jun Mukai1db53972015-09-11 18:08:31 -070060 * to have the default icon.
Jun Mukaid4eaef72015-10-30 15:54:33 -070061 * @hide
Jun Mukai1db53972015-09-11 18:08:31 -070062 */
Michael Wrighte051f6f2016-05-13 17:44:16 +010063 public static final int TYPE_NOT_SPECIFIED = 1;
Jun Mukai1db53972015-09-11 18:08:31 -070064
Michael Wrighte051f6f2016-05-13 17:44:16 +010065 /** Type constant: Arrow icon. (Default mouse pointer) */
66 public static final int TYPE_ARROW = 1000;
Jeff Brown2352b972011-04-12 22:39:53 -070067
Michael Wrighte051f6f2016-05-13 17:44:16 +010068 /** {@hide} Type constant: Spot hover icon for touchpads. */
69 public static final int TYPE_SPOT_HOVER = 2000;
Jeff Brown2352b972011-04-12 22:39:53 -070070
Michael Wrighte051f6f2016-05-13 17:44:16 +010071 /** {@hide} Type constant: Spot touch icon for touchpads. */
72 public static final int TYPE_SPOT_TOUCH = 2001;
Jeff Brown2352b972011-04-12 22:39:53 -070073
Michael Wrighte051f6f2016-05-13 17:44:16 +010074 /** {@hide} Type constant: Spot anchor icon for touchpads. */
75 public static final int TYPE_SPOT_ANCHOR = 2002;
Jeff Brown2352b972011-04-12 22:39:53 -070076
Michael Wrighte051f6f2016-05-13 17:44:16 +010077 // Type constants for additional predefined icons for mice.
78 /** Type constant: context-menu. */
79 public static final int TYPE_CONTEXT_MENU = 1001;
Jun Mukai1db53972015-09-11 18:08:31 -070080
Michael Wrighte051f6f2016-05-13 17:44:16 +010081 /** Type constant: hand. */
82 public static final int TYPE_HAND = 1002;
Jun Mukai1db53972015-09-11 18:08:31 -070083
Michael Wrighte051f6f2016-05-13 17:44:16 +010084 /** Type constant: help. */
85 public static final int TYPE_HELP = 1003;
Jun Mukai1db53972015-09-11 18:08:31 -070086
Michael Wrighte051f6f2016-05-13 17:44:16 +010087 /** Type constant: wait. */
88 public static final int TYPE_WAIT = 1004;
Jun Mukai1db53972015-09-11 18:08:31 -070089
Michael Wrighte051f6f2016-05-13 17:44:16 +010090 /** Type constant: cell. */
91 public static final int TYPE_CELL = 1006;
Jun Mukai1db53972015-09-11 18:08:31 -070092
Michael Wrighte051f6f2016-05-13 17:44:16 +010093 /** Type constant: crosshair. */
94 public static final int TYPE_CROSSHAIR = 1007;
Jun Mukai1db53972015-09-11 18:08:31 -070095
Michael Wrighte051f6f2016-05-13 17:44:16 +010096 /** Type constant: text. */
97 public static final int TYPE_TEXT = 1008;
Jun Mukai1db53972015-09-11 18:08:31 -070098
Michael Wrighte051f6f2016-05-13 17:44:16 +010099 /** Type constant: vertical-text. */
100 public static final int TYPE_VERTICAL_TEXT = 1009;
Jun Mukai1db53972015-09-11 18:08:31 -0700101
Michael Wrighte051f6f2016-05-13 17:44:16 +0100102 /** Type constant: alias (indicating an alias of/shortcut to something is
Jun Mukai1db53972015-09-11 18:08:31 -0700103 * to be created. */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100104 public static final int TYPE_ALIAS = 1010;
Jun Mukai1db53972015-09-11 18:08:31 -0700105
Michael Wrighte051f6f2016-05-13 17:44:16 +0100106 /** Type constant: copy. */
107 public static final int TYPE_COPY = 1011;
Jun Mukai1db53972015-09-11 18:08:31 -0700108
Michael Wrighte051f6f2016-05-13 17:44:16 +0100109 /** Type constant: no-drop. */
110 public static final int TYPE_NO_DROP = 1012;
Jun Mukai1db53972015-09-11 18:08:31 -0700111
Michael Wrighte051f6f2016-05-13 17:44:16 +0100112 /** Type constant: all-scroll. */
113 public static final int TYPE_ALL_SCROLL = 1013;
Jun Mukai1db53972015-09-11 18:08:31 -0700114
Michael Wrighte051f6f2016-05-13 17:44:16 +0100115 /** Type constant: horizontal double arrow mainly for resizing. */
116 public static final int TYPE_HORIZONTAL_DOUBLE_ARROW = 1014;
Jun Mukai1db53972015-09-11 18:08:31 -0700117
Michael Wrighte051f6f2016-05-13 17:44:16 +0100118 /** Type constant: vertical double arrow mainly for resizing. */
119 public static final int TYPE_VERTICAL_DOUBLE_ARROW = 1015;
Jun Mukai1db53972015-09-11 18:08:31 -0700120
Michael Wrighte051f6f2016-05-13 17:44:16 +0100121 /** Type constant: diagonal double arrow -- top-right to bottom-left. */
122 public static final int TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016;
Jun Mukai1db53972015-09-11 18:08:31 -0700123
Michael Wrighte051f6f2016-05-13 17:44:16 +0100124 /** Type constant: diagonal double arrow -- top-left to bottom-right. */
125 public static final int TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017;
Jun Mukai1db53972015-09-11 18:08:31 -0700126
Michael Wrighte051f6f2016-05-13 17:44:16 +0100127 /** Type constant: zoom-in. */
128 public static final int TYPE_ZOOM_IN = 1018;
Jun Mukai1db53972015-09-11 18:08:31 -0700129
Michael Wrighte051f6f2016-05-13 17:44:16 +0100130 /** Type constant: zoom-out. */
131 public static final int TYPE_ZOOM_OUT = 1019;
Jun Mukai1db53972015-09-11 18:08:31 -0700132
Michael Wrighte051f6f2016-05-13 17:44:16 +0100133 /** Type constant: grab. */
134 public static final int TYPE_GRAB = 1020;
Jun Mukai1db53972015-09-11 18:08:31 -0700135
Michael Wrighte051f6f2016-05-13 17:44:16 +0100136 /** Type constant: grabbing. */
137 public static final int TYPE_GRABBING = 1021;
Jun Mukai1db53972015-09-11 18:08:31 -0700138
Michael Wrighte051f6f2016-05-13 17:44:16 +0100139 // OEM private types should be defined starting at this range to avoid
140 // conflicts with any system types that may be defined in the future.
141 private static final int TYPE_OEM_FIRST = 10000;
Jeff Brown2352b972011-04-12 22:39:53 -0700142
Jun Mukaid4eaef72015-10-30 15:54:33 -0700143 /** The default pointer icon. */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100144 public static final int TYPE_DEFAULT = TYPE_ARROW;
Jeff Brown2352b972011-04-12 22:39:53 -0700145
Michael Wrighte051f6f2016-05-13 17:44:16 +0100146 private static final PointerIcon gNullIcon = new PointerIcon(TYPE_NULL);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700147 private static final SparseArray<PointerIcon> gSystemIcons = new SparseArray<PointerIcon>();
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800148 private static boolean sUseLargeIcons = false;
Jun Mukaie4e75da2015-12-15 16:19:20 -0800149
Mathew Inwood45d2c252018-09-14 12:35:36 +0100150 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Michael Wrighte051f6f2016-05-13 17:44:16 +0100151 private final int mType;
Jeff Brown2352b972011-04-12 22:39:53 -0700152 private int mSystemIconResourceId;
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100153 @UnsupportedAppUsage
Jeff Brown2352b972011-04-12 22:39:53 -0700154 private Bitmap mBitmap;
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100155 @UnsupportedAppUsage
Jeff Brown2352b972011-04-12 22:39:53 -0700156 private float mHotSpotX;
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100157 @UnsupportedAppUsage
Jeff Brown2352b972011-04-12 22:39:53 -0700158 private float mHotSpotY;
Jun Mukai808196f2015-10-28 16:46:44 -0700159 // The bitmaps for the additional frame of animated pointer icon. Note that the first frame
160 // will be stored in mBitmap.
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100161 @UnsupportedAppUsage
Jun Mukai808196f2015-10-28 16:46:44 -0700162 private Bitmap mBitmapFrames[];
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100163 @UnsupportedAppUsage
Jun Mukai808196f2015-10-28 16:46:44 -0700164 private int mDurationPerFrame;
Jeff Brown2352b972011-04-12 22:39:53 -0700165
Michael Wrighte051f6f2016-05-13 17:44:16 +0100166 private PointerIcon(int type) {
167 mType = type;
Jeff Brown2352b972011-04-12 22:39:53 -0700168 }
169
170 /**
171 * Gets a special pointer icon that has no bitmap.
172 *
173 * @return The null pointer icon.
174 *
Michael Wrighte051f6f2016-05-13 17:44:16 +0100175 * @see #TYPE_NULL
Jun Mukaid4eaef72015-10-30 15:54:33 -0700176 * @hide
Jeff Brown2352b972011-04-12 22:39:53 -0700177 */
178 public static PointerIcon getNullIcon() {
179 return gNullIcon;
180 }
181
182 /**
183 * Gets the default pointer icon.
184 *
185 * @param context The context.
186 * @return The default pointer icon.
187 *
188 * @throws IllegalArgumentException if context is null.
Jun Mukaid4eaef72015-10-30 15:54:33 -0700189 * @hide
Jeff Brown2352b972011-04-12 22:39:53 -0700190 */
Jun Mukaid4eaef72015-10-30 15:54:33 -0700191 public static PointerIcon getDefaultIcon(@NonNull Context context) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100192 return getSystemIcon(context, TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700193 }
194
195 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100196 * Gets a system pointer icon for the given type.
197 * If typeis not recognized, returns the default pointer icon.
Jeff Brown2352b972011-04-12 22:39:53 -0700198 *
199 * @param context The context.
Michael Wrighte051f6f2016-05-13 17:44:16 +0100200 * @param type The pointer icon type.
Jeff Brown2352b972011-04-12 22:39:53 -0700201 * @return The pointer icon.
202 *
203 * @throws IllegalArgumentException if context is null.
204 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100205 public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
Jeff Brown2352b972011-04-12 22:39:53 -0700206 if (context == null) {
207 throw new IllegalArgumentException("context must not be null");
208 }
209
Michael Wrighte051f6f2016-05-13 17:44:16 +0100210 if (type == TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700211 return gNullIcon;
212 }
213
Michael Wrighte051f6f2016-05-13 17:44:16 +0100214 PointerIcon icon = gSystemIcons.get(type);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700215 if (icon != null) {
216 return icon;
217 }
218
Michael Wrighte051f6f2016-05-13 17:44:16 +0100219 int typeIndex = getSystemIconTypeIndex(type);
220 if (typeIndex == 0) {
221 typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700222 }
223
Jun Mukaie4e75da2015-12-15 16:19:20 -0800224 int defStyle = sUseLargeIcons ?
Jun Mukai19a56012015-11-24 11:25:52 -0800225 com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
Jeff Brown2352b972011-04-12 22:39:53 -0700226 TypedArray a = context.obtainStyledAttributes(null,
227 com.android.internal.R.styleable.Pointer,
Jun Mukai19a56012015-11-24 11:25:52 -0800228 0, defStyle);
Michael Wrighte051f6f2016-05-13 17:44:16 +0100229 int resourceId = a.getResourceId(typeIndex, -1);
Jeff Brown2352b972011-04-12 22:39:53 -0700230 a.recycle();
231
232 if (resourceId == -1) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100233 Log.w(TAG, "Missing theme resources for pointer icon type " + type);
234 return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700235 }
236
Michael Wrighte051f6f2016-05-13 17:44:16 +0100237 icon = new PointerIcon(type);
Jeff Brown2352b972011-04-12 22:39:53 -0700238 if ((resourceId & 0xff000000) == 0x01000000) {
239 icon.mSystemIconResourceId = resourceId;
240 } else {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800241 icon.loadResource(context, context.getResources(), resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700242 }
Michael Wrighte051f6f2016-05-13 17:44:16 +0100243 gSystemIcons.append(type, icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700244 return icon;
245 }
246
247 /**
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800248 * Updates wheter accessibility large icons are used or not.
249 * @hide
250 */
251 public static void setUseLargeIcons(boolean use) {
252 sUseLargeIcons = use;
253 gSystemIcons.clear();
254 }
255
256 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100257 * Creates a custom pointer icon from the given bitmap and hotspot information.
Jeff Brown2352b972011-04-12 22:39:53 -0700258 *
259 * @param bitmap The bitmap for the icon.
Alan Viverette376d10e2014-09-02 10:45:30 -0700260 * @param hotSpotX The X offset of the pointer icon hotspot in the bitmap.
Jeff Brown2352b972011-04-12 22:39:53 -0700261 * Must be within the [0, bitmap.getWidth()) range.
Alan Viverette376d10e2014-09-02 10:45:30 -0700262 * @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap.
Jeff Brown2352b972011-04-12 22:39:53 -0700263 * Must be within the [0, bitmap.getHeight()) range.
264 * @return A pointer icon for this bitmap.
265 *
266 * @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot
267 * parameters are invalid.
268 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100269 public static PointerIcon create(@NonNull Bitmap bitmap, float hotSpotX, float hotSpotY) {
Jeff Brown2352b972011-04-12 22:39:53 -0700270 if (bitmap == null) {
271 throw new IllegalArgumentException("bitmap must not be null");
272 }
273 validateHotSpot(bitmap, hotSpotX, hotSpotY);
274
Michael Wrighte051f6f2016-05-13 17:44:16 +0100275 PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
Jeff Brown2352b972011-04-12 22:39:53 -0700276 icon.mBitmap = bitmap;
277 icon.mHotSpotX = hotSpotX;
278 icon.mHotSpotY = hotSpotY;
279 return icon;
280 }
281
282 /**
283 * Loads a custom pointer icon from an XML resource.
284 * <p>
285 * The XML resource should have the following form:
286 * <code>
287 * &lt;?xml version="1.0" encoding="utf-8"?&gt;
288 * &lt;pointer-icon xmlns:android="http://schemas.android.com/apk/res/android"
289 * android:bitmap="@drawable/my_pointer_bitmap"
290 * android:hotSpotX="24"
291 * android:hotSpotY="24" /&gt;
292 * </code>
293 * </p>
294 *
295 * @param resources The resources object.
296 * @param resourceId The resource id.
297 * @return The pointer icon.
298 *
299 * @throws IllegalArgumentException if resources is null.
300 * @throws Resources.NotFoundException if the resource was not found or the drawable
301 * linked in the resource was not found.
302 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100303 public static PointerIcon load(@NonNull Resources resources, @XmlRes int resourceId) {
Jeff Brown2352b972011-04-12 22:39:53 -0700304 if (resources == null) {
305 throw new IllegalArgumentException("resources must not be null");
306 }
307
Michael Wrighte051f6f2016-05-13 17:44:16 +0100308 PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800309 icon.loadResource(null, resources, resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700310 return icon;
311 }
312
313 /**
314 * Loads the bitmap and hotspot information for a pointer icon, if it is not already loaded.
315 * Returns a pointer icon (not necessarily the same instance) with the information filled in.
316 *
317 * @param context The context.
318 * @return The loaded pointer icon.
319 *
320 * @throws IllegalArgumentException if context is null.
Jeff Brown2352b972011-04-12 22:39:53 -0700321 * @hide
322 */
Mathew Inwood45d2c252018-09-14 12:35:36 +0100323 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jun Mukaid4eaef72015-10-30 15:54:33 -0700324 public PointerIcon load(@NonNull Context context) {
Jeff Brown2352b972011-04-12 22:39:53 -0700325 if (context == null) {
326 throw new IllegalArgumentException("context must not be null");
327 }
328
329 if (mSystemIconResourceId == 0 || mBitmap != null) {
330 return this;
331 }
332
Michael Wrighte051f6f2016-05-13 17:44:16 +0100333 PointerIcon result = new PointerIcon(mType);
Jeff Brown2352b972011-04-12 22:39:53 -0700334 result.mSystemIconResourceId = mSystemIconResourceId;
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800335 result.loadResource(context, context.getResources(), mSystemIconResourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700336 return result;
337 }
338
Jun Mukaid4eaef72015-10-30 15:54:33 -0700339 /** @hide */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100340 public int getType() {
341 return mType;
Jeff Brown2352b972011-04-12 22:39:53 -0700342 }
343
Jeff Brown2352b972011-04-12 22:39:53 -0700344 public static final Parcelable.Creator<PointerIcon> CREATOR
345 = new Parcelable.Creator<PointerIcon>() {
346 public PointerIcon createFromParcel(Parcel in) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100347 int type = in.readInt();
348 if (type == TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700349 return getNullIcon();
350 }
351
352 int systemIconResourceId = in.readInt();
353 if (systemIconResourceId != 0) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100354 PointerIcon icon = new PointerIcon(type);
Jeff Brown2352b972011-04-12 22:39:53 -0700355 icon.mSystemIconResourceId = systemIconResourceId;
356 return icon;
357 }
358
359 Bitmap bitmap = Bitmap.CREATOR.createFromParcel(in);
360 float hotSpotX = in.readFloat();
361 float hotSpotY = in.readFloat();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100362 return PointerIcon.create(bitmap, hotSpotX, hotSpotY);
Jeff Brown2352b972011-04-12 22:39:53 -0700363 }
364
365 public PointerIcon[] newArray(int size) {
366 return new PointerIcon[size];
367 }
368 };
369
370 public int describeContents() {
371 return 0;
372 }
373
374 public void writeToParcel(Parcel out, int flags) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100375 out.writeInt(mType);
Jeff Brown2352b972011-04-12 22:39:53 -0700376
Michael Wrighte051f6f2016-05-13 17:44:16 +0100377 if (mType != TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700378 out.writeInt(mSystemIconResourceId);
379 if (mSystemIconResourceId == 0) {
380 mBitmap.writeToParcel(out, flags);
381 out.writeFloat(mHotSpotX);
382 out.writeFloat(mHotSpotY);
383 }
384 }
385 }
386
387 @Override
388 public boolean equals(Object other) {
389 if (this == other) {
390 return true;
391 }
392
393 if (other == null || !(other instanceof PointerIcon)) {
394 return false;
395 }
396
397 PointerIcon otherIcon = (PointerIcon) other;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100398 if (mType != otherIcon.mType
Jeff Brown2352b972011-04-12 22:39:53 -0700399 || mSystemIconResourceId != otherIcon.mSystemIconResourceId) {
400 return false;
401 }
402
403 if (mSystemIconResourceId == 0 && (mBitmap != otherIcon.mBitmap
404 || mHotSpotX != otherIcon.mHotSpotX
405 || mHotSpotY != otherIcon.mHotSpotY)) {
406 return false;
407 }
408
409 return true;
410 }
411
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500412 /**
413 * Get the Bitmap from the Drawable.
414 *
415 * If the Bitmap needed to be scaled up to account for density, BitmapDrawable
416 * handles this at draw time. But this class doesn't actually draw the Bitmap;
417 * it is just a holder for native code to access its SkBitmap. So this needs to
418 * get a version that is scaled to account for density.
419 */
420 private Bitmap getBitmapFromDrawable(BitmapDrawable bitmapDrawable) {
421 Bitmap bitmap = bitmapDrawable.getBitmap();
422 final int scaledWidth = bitmapDrawable.getIntrinsicWidth();
423 final int scaledHeight = bitmapDrawable.getIntrinsicHeight();
424 if (scaledWidth == bitmap.getWidth() && scaledHeight == bitmap.getHeight()) {
425 return bitmap;
426 }
427
428 Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
429 RectF dst = new RectF(0, 0, scaledWidth, scaledHeight);
430
431 Bitmap scaled = Bitmap.createBitmap(scaledWidth, scaledHeight, bitmap.getConfig());
432 Canvas canvas = new Canvas(scaled);
433 Paint paint = new Paint();
434 paint.setFilterBitmap(true);
435 canvas.drawBitmap(bitmap, src, dst, paint);
436 return scaled;
437 }
438
Tor Norbye7b9c9122013-05-30 16:48:33 -0700439 private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
Alan Viverette376d10e2014-09-02 10:45:30 -0700440 final XmlResourceParser parser = resources.getXml(resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700441 final int bitmapRes;
442 final float hotSpotX;
443 final float hotSpotY;
444 try {
445 XmlUtils.beginDocument(parser, "pointer-icon");
446
Alan Viverette376d10e2014-09-02 10:45:30 -0700447 final TypedArray a = resources.obtainAttributes(
Jeff Brown2352b972011-04-12 22:39:53 -0700448 parser, com.android.internal.R.styleable.PointerIcon);
449 bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
Alan Viverette376d10e2014-09-02 10:45:30 -0700450 hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
451 hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
Jeff Brown2352b972011-04-12 22:39:53 -0700452 a.recycle();
453 } catch (Exception ex) {
454 throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
455 } finally {
456 parser.close();
457 }
458
459 if (bitmapRes == 0) {
460 throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
461 }
462
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800463 Drawable drawable;
464 if (context == null) {
465 drawable = resources.getDrawable(bitmapRes);
466 } else {
467 drawable = context.getDrawable(bitmapRes);
468 }
Jun Mukai808196f2015-10-28 16:46:44 -0700469 if (drawable instanceof AnimationDrawable) {
470 // Extract animation frame bitmaps.
471 final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
472 final int frames = animationDrawable.getNumberOfFrames();
473 drawable = animationDrawable.getFrame(0);
474 if (frames == 1) {
475 Log.w(TAG, "Animation icon with single frame -- simply treating the first "
476 + "frame as a normal bitmap icon.");
477 } else {
478 // Assumes they have the exact duration.
479 mDurationPerFrame = animationDrawable.getDuration(0);
480 mBitmapFrames = new Bitmap[frames - 1];
481 final int width = drawable.getIntrinsicWidth();
482 final int height = drawable.getIntrinsicHeight();
483 for (int i = 1; i < frames; ++i) {
484 Drawable drawableFrame = animationDrawable.getFrame(i);
485 if (!(drawableFrame instanceof BitmapDrawable)) {
486 throw new IllegalArgumentException("Frame of an animated pointer icon "
487 + "must refer to a bitmap drawable.");
488 }
489 if (drawableFrame.getIntrinsicWidth() != width ||
490 drawableFrame.getIntrinsicHeight() != height) {
491 throw new IllegalArgumentException("The bitmap size of " + i + "-th frame "
492 + "is different. All frames should have the exact same size and "
493 + "share the same hotspot.");
494 }
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500495 BitmapDrawable bitmapDrawableFrame = (BitmapDrawable) drawableFrame;
496 mBitmapFrames[i - 1] = getBitmapFromDrawable(bitmapDrawableFrame);
Jun Mukai808196f2015-10-28 16:46:44 -0700497 }
498 }
499 }
Jeff Brown2352b972011-04-12 22:39:53 -0700500 if (!(drawable instanceof BitmapDrawable)) {
501 throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
502 + "refer to a bitmap drawable.");
503 }
504
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500505 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
506 final Bitmap bitmap = getBitmapFromDrawable(bitmapDrawable);
Vladislav Kaznacheev44e7de32017-12-20 12:18:03 -0800507 validateHotSpot(bitmap, hotSpotX, hotSpotY);
Jeff Brown2352b972011-04-12 22:39:53 -0700508 // Set the properties now that we have successfully loaded the icon.
Vladislav Kaznacheev44e7de32017-12-20 12:18:03 -0800509 mBitmap = bitmap;
Jeff Brown2352b972011-04-12 22:39:53 -0700510 mHotSpotX = hotSpotX;
511 mHotSpotY = hotSpotY;
512 }
513
514 private static void validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY) {
515 if (hotSpotX < 0 || hotSpotX >= bitmap.getWidth()) {
516 throw new IllegalArgumentException("x hotspot lies outside of the bitmap area");
517 }
518 if (hotSpotY < 0 || hotSpotY >= bitmap.getHeight()) {
519 throw new IllegalArgumentException("y hotspot lies outside of the bitmap area");
520 }
521 }
522
Michael Wrighte051f6f2016-05-13 17:44:16 +0100523 private static int getSystemIconTypeIndex(int type) {
524 switch (type) {
525 case TYPE_ARROW:
Jeff Brown2352b972011-04-12 22:39:53 -0700526 return com.android.internal.R.styleable.Pointer_pointerIconArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100527 case TYPE_SPOT_HOVER:
Jeff Brown2352b972011-04-12 22:39:53 -0700528 return com.android.internal.R.styleable.Pointer_pointerIconSpotHover;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100529 case TYPE_SPOT_TOUCH:
Jeff Brown2352b972011-04-12 22:39:53 -0700530 return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100531 case TYPE_SPOT_ANCHOR:
Jeff Brown2352b972011-04-12 22:39:53 -0700532 return com.android.internal.R.styleable.Pointer_pointerIconSpotAnchor;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100533 case TYPE_HAND:
Jun Mukai1db53972015-09-11 18:08:31 -0700534 return com.android.internal.R.styleable.Pointer_pointerIconHand;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100535 case TYPE_CONTEXT_MENU:
Jun Mukai1db53972015-09-11 18:08:31 -0700536 return com.android.internal.R.styleable.Pointer_pointerIconContextMenu;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100537 case TYPE_HELP:
Jun Mukai1db53972015-09-11 18:08:31 -0700538 return com.android.internal.R.styleable.Pointer_pointerIconHelp;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100539 case TYPE_WAIT:
Jun Mukai808196f2015-10-28 16:46:44 -0700540 return com.android.internal.R.styleable.Pointer_pointerIconWait;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100541 case TYPE_CELL:
Jun Mukai1db53972015-09-11 18:08:31 -0700542 return com.android.internal.R.styleable.Pointer_pointerIconCell;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100543 case TYPE_CROSSHAIR:
Jun Mukai1db53972015-09-11 18:08:31 -0700544 return com.android.internal.R.styleable.Pointer_pointerIconCrosshair;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100545 case TYPE_TEXT:
Jun Mukai1db53972015-09-11 18:08:31 -0700546 return com.android.internal.R.styleable.Pointer_pointerIconText;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100547 case TYPE_VERTICAL_TEXT:
Jun Mukai1db53972015-09-11 18:08:31 -0700548 return com.android.internal.R.styleable.Pointer_pointerIconVerticalText;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100549 case TYPE_ALIAS:
Jun Mukai1db53972015-09-11 18:08:31 -0700550 return com.android.internal.R.styleable.Pointer_pointerIconAlias;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100551 case TYPE_COPY:
Jun Mukai1db53972015-09-11 18:08:31 -0700552 return com.android.internal.R.styleable.Pointer_pointerIconCopy;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100553 case TYPE_ALL_SCROLL:
Jun Mukai1db53972015-09-11 18:08:31 -0700554 return com.android.internal.R.styleable.Pointer_pointerIconAllScroll;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100555 case TYPE_NO_DROP:
Jun Mukai1db53972015-09-11 18:08:31 -0700556 return com.android.internal.R.styleable.Pointer_pointerIconNodrop;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100557 case TYPE_HORIZONTAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700558 return com.android.internal.R.styleable.Pointer_pointerIconHorizontalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100559 case TYPE_VERTICAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700560 return com.android.internal.R.styleable.Pointer_pointerIconVerticalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100561 case TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700562 return com.android.internal.R.styleable.
563 Pointer_pointerIconTopRightDiagonalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100564 case TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700565 return com.android.internal.R.styleable.
566 Pointer_pointerIconTopLeftDiagonalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100567 case TYPE_ZOOM_IN:
Jun Mukai1db53972015-09-11 18:08:31 -0700568 return com.android.internal.R.styleable.Pointer_pointerIconZoomIn;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100569 case TYPE_ZOOM_OUT:
Jun Mukai1db53972015-09-11 18:08:31 -0700570 return com.android.internal.R.styleable.Pointer_pointerIconZoomOut;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100571 case TYPE_GRAB:
Jun Mukai1db53972015-09-11 18:08:31 -0700572 return com.android.internal.R.styleable.Pointer_pointerIconGrab;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100573 case TYPE_GRABBING:
Jun Mukai1db53972015-09-11 18:08:31 -0700574 return com.android.internal.R.styleable.Pointer_pointerIconGrabbing;
Jeff Brown2352b972011-04-12 22:39:53 -0700575 default:
576 return 0;
577 }
578 }
579}