blob: 18d0d7b98a4256df4d37d6a6d62e4ecda5928fc9 [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;
Artur Satayevad9254c2019-12-10 17:47:54 +000021import android.compat.annotation.UnsupportedAppUsage;
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;
Andrii Kulianfd8666d2018-10-05 16:58:39 -070034import android.hardware.display.DisplayManager;
Mathew Inwood8c854f82018-09-14 12:35:36 +010035import android.os.Build;
Jeff Brown2352b972011-04-12 22:39:53 -070036import android.os.Parcel;
37import android.os.Parcelable;
38import android.util.Log;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070039import android.util.SparseArray;
40
41import com.android.internal.util.XmlUtils;
Jeff Brown2352b972011-04-12 22:39:53 -070042
43/**
44 * Represents an icon that can be used as a mouse pointer.
45 * <p>
Michael Wrighte051f6f2016-05-13 17:44:16 +010046 * Pointer icons can be provided either by the system using system types,
Jeff Brown2352b972011-04-12 22:39:53 -070047 * or by applications using bitmaps or application resources.
48 * </p>
Jeff Brown2352b972011-04-12 22:39:53 -070049 */
50public final class PointerIcon implements Parcelable {
51 private static final String TAG = "PointerIcon";
52
Michael Wrighte051f6f2016-05-13 17:44:16 +010053 /** {@hide} Type constant: Custom icon with a user-supplied bitmap. */
54 public static final int TYPE_CUSTOM = -1;
Jeff Brown2352b972011-04-12 22:39:53 -070055
Michael Wrighte051f6f2016-05-13 17:44:16 +010056 /** Type constant: Null icon. It has no bitmap. */
57 public static final int TYPE_NULL = 0;
Jeff Brown2352b972011-04-12 22:39:53 -070058
Michael Wrighte051f6f2016-05-13 17:44:16 +010059 /** Type constant: no icons are specified. If all views uses this, then falls back
60 * to the default type, but this is helpful to distinguish a view explicitly want
Jun Mukai1db53972015-09-11 18:08:31 -070061 * to have the default icon.
Jun Mukaid4eaef72015-10-30 15:54:33 -070062 * @hide
Jun Mukai1db53972015-09-11 18:08:31 -070063 */
Michael Wrighte051f6f2016-05-13 17:44:16 +010064 public static final int TYPE_NOT_SPECIFIED = 1;
Jun Mukai1db53972015-09-11 18:08:31 -070065
Michael Wrighte051f6f2016-05-13 17:44:16 +010066 /** Type constant: Arrow icon. (Default mouse pointer) */
67 public static final int TYPE_ARROW = 1000;
Jeff Brown2352b972011-04-12 22:39:53 -070068
Michael Wrighte051f6f2016-05-13 17:44:16 +010069 /** {@hide} Type constant: Spot hover icon for touchpads. */
70 public static final int TYPE_SPOT_HOVER = 2000;
Jeff Brown2352b972011-04-12 22:39:53 -070071
Michael Wrighte051f6f2016-05-13 17:44:16 +010072 /** {@hide} Type constant: Spot touch icon for touchpads. */
73 public static final int TYPE_SPOT_TOUCH = 2001;
Jeff Brown2352b972011-04-12 22:39:53 -070074
Michael Wrighte051f6f2016-05-13 17:44:16 +010075 /** {@hide} Type constant: Spot anchor icon for touchpads. */
76 public static final int TYPE_SPOT_ANCHOR = 2002;
Jeff Brown2352b972011-04-12 22:39:53 -070077
Michael Wrighte051f6f2016-05-13 17:44:16 +010078 // Type constants for additional predefined icons for mice.
79 /** Type constant: context-menu. */
80 public static final int TYPE_CONTEXT_MENU = 1001;
Jun Mukai1db53972015-09-11 18:08:31 -070081
Michael Wrighte051f6f2016-05-13 17:44:16 +010082 /** Type constant: hand. */
83 public static final int TYPE_HAND = 1002;
Jun Mukai1db53972015-09-11 18:08:31 -070084
Michael Wrighte051f6f2016-05-13 17:44:16 +010085 /** Type constant: help. */
86 public static final int TYPE_HELP = 1003;
Jun Mukai1db53972015-09-11 18:08:31 -070087
Michael Wrighte051f6f2016-05-13 17:44:16 +010088 /** Type constant: wait. */
89 public static final int TYPE_WAIT = 1004;
Jun Mukai1db53972015-09-11 18:08:31 -070090
Michael Wrighte051f6f2016-05-13 17:44:16 +010091 /** Type constant: cell. */
92 public static final int TYPE_CELL = 1006;
Jun Mukai1db53972015-09-11 18:08:31 -070093
Michael Wrighte051f6f2016-05-13 17:44:16 +010094 /** Type constant: crosshair. */
95 public static final int TYPE_CROSSHAIR = 1007;
Jun Mukai1db53972015-09-11 18:08:31 -070096
Michael Wrighte051f6f2016-05-13 17:44:16 +010097 /** Type constant: text. */
98 public static final int TYPE_TEXT = 1008;
Jun Mukai1db53972015-09-11 18:08:31 -070099
Michael Wrighte051f6f2016-05-13 17:44:16 +0100100 /** Type constant: vertical-text. */
101 public static final int TYPE_VERTICAL_TEXT = 1009;
Jun Mukai1db53972015-09-11 18:08:31 -0700102
Michael Wrighte051f6f2016-05-13 17:44:16 +0100103 /** Type constant: alias (indicating an alias of/shortcut to something is
Jun Mukai1db53972015-09-11 18:08:31 -0700104 * to be created. */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100105 public static final int TYPE_ALIAS = 1010;
Jun Mukai1db53972015-09-11 18:08:31 -0700106
Michael Wrighte051f6f2016-05-13 17:44:16 +0100107 /** Type constant: copy. */
108 public static final int TYPE_COPY = 1011;
Jun Mukai1db53972015-09-11 18:08:31 -0700109
Michael Wrighte051f6f2016-05-13 17:44:16 +0100110 /** Type constant: no-drop. */
111 public static final int TYPE_NO_DROP = 1012;
Jun Mukai1db53972015-09-11 18:08:31 -0700112
Michael Wrighte051f6f2016-05-13 17:44:16 +0100113 /** Type constant: all-scroll. */
114 public static final int TYPE_ALL_SCROLL = 1013;
Jun Mukai1db53972015-09-11 18:08:31 -0700115
Michael Wrighte051f6f2016-05-13 17:44:16 +0100116 /** Type constant: horizontal double arrow mainly for resizing. */
117 public static final int TYPE_HORIZONTAL_DOUBLE_ARROW = 1014;
Jun Mukai1db53972015-09-11 18:08:31 -0700118
Michael Wrighte051f6f2016-05-13 17:44:16 +0100119 /** Type constant: vertical double arrow mainly for resizing. */
120 public static final int TYPE_VERTICAL_DOUBLE_ARROW = 1015;
Jun Mukai1db53972015-09-11 18:08:31 -0700121
Michael Wrighte051f6f2016-05-13 17:44:16 +0100122 /** Type constant: diagonal double arrow -- top-right to bottom-left. */
123 public static final int TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016;
Jun Mukai1db53972015-09-11 18:08:31 -0700124
Michael Wrighte051f6f2016-05-13 17:44:16 +0100125 /** Type constant: diagonal double arrow -- top-left to bottom-right. */
126 public static final int TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017;
Jun Mukai1db53972015-09-11 18:08:31 -0700127
Michael Wrighte051f6f2016-05-13 17:44:16 +0100128 /** Type constant: zoom-in. */
129 public static final int TYPE_ZOOM_IN = 1018;
Jun Mukai1db53972015-09-11 18:08:31 -0700130
Michael Wrighte051f6f2016-05-13 17:44:16 +0100131 /** Type constant: zoom-out. */
132 public static final int TYPE_ZOOM_OUT = 1019;
Jun Mukai1db53972015-09-11 18:08:31 -0700133
Michael Wrighte051f6f2016-05-13 17:44:16 +0100134 /** Type constant: grab. */
135 public static final int TYPE_GRAB = 1020;
Jun Mukai1db53972015-09-11 18:08:31 -0700136
Michael Wrighte051f6f2016-05-13 17:44:16 +0100137 /** Type constant: grabbing. */
138 public static final int TYPE_GRABBING = 1021;
Jun Mukai1db53972015-09-11 18:08:31 -0700139
Michael Wrighte051f6f2016-05-13 17:44:16 +0100140 // OEM private types should be defined starting at this range to avoid
141 // conflicts with any system types that may be defined in the future.
142 private static final int TYPE_OEM_FIRST = 10000;
Jeff Brown2352b972011-04-12 22:39:53 -0700143
Jun Mukaid4eaef72015-10-30 15:54:33 -0700144 /** The default pointer icon. */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100145 public static final int TYPE_DEFAULT = TYPE_ARROW;
Jeff Brown2352b972011-04-12 22:39:53 -0700146
Michael Wrighte051f6f2016-05-13 17:44:16 +0100147 private static final PointerIcon gNullIcon = new PointerIcon(TYPE_NULL);
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700148 private static final SparseArray<SparseArray<PointerIcon>> gSystemIconsByDisplay =
149 new SparseArray<SparseArray<PointerIcon>>();
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800150 private static boolean sUseLargeIcons = false;
Jun Mukaie4e75da2015-12-15 16:19:20 -0800151
Mathew Inwood8c854f82018-09-14 12:35:36 +0100152 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Michael Wrighte051f6f2016-05-13 17:44:16 +0100153 private final int mType;
Jeff Brown2352b972011-04-12 22:39:53 -0700154 private int mSystemIconResourceId;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100155 @UnsupportedAppUsage
Jeff Brown2352b972011-04-12 22:39:53 -0700156 private Bitmap mBitmap;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100157 @UnsupportedAppUsage
Jeff Brown2352b972011-04-12 22:39:53 -0700158 private float mHotSpotX;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100159 @UnsupportedAppUsage
Jeff Brown2352b972011-04-12 22:39:53 -0700160 private float mHotSpotY;
Jun Mukai808196f2015-10-28 16:46:44 -0700161 // The bitmaps for the additional frame of animated pointer icon. Note that the first frame
162 // will be stored in mBitmap.
Mathew Inwooda570dee2018-08-17 14:56:00 +0100163 @UnsupportedAppUsage
Jun Mukai808196f2015-10-28 16:46:44 -0700164 private Bitmap mBitmapFrames[];
Mathew Inwooda570dee2018-08-17 14:56:00 +0100165 @UnsupportedAppUsage
Jun Mukai808196f2015-10-28 16:46:44 -0700166 private int mDurationPerFrame;
Jeff Brown2352b972011-04-12 22:39:53 -0700167
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700168 /**
169 * Listener for displays lifecycle.
170 * @hide
171 */
172 private static DisplayManager.DisplayListener sDisplayListener;
173
Michael Wrighte051f6f2016-05-13 17:44:16 +0100174 private PointerIcon(int type) {
175 mType = type;
Jeff Brown2352b972011-04-12 22:39:53 -0700176 }
177
178 /**
179 * Gets a special pointer icon that has no bitmap.
180 *
181 * @return The null pointer icon.
182 *
Michael Wrighte051f6f2016-05-13 17:44:16 +0100183 * @see #TYPE_NULL
Jun Mukaid4eaef72015-10-30 15:54:33 -0700184 * @hide
Jeff Brown2352b972011-04-12 22:39:53 -0700185 */
186 public static PointerIcon getNullIcon() {
187 return gNullIcon;
188 }
189
190 /**
191 * Gets the default pointer icon.
192 *
193 * @param context The context.
194 * @return The default pointer icon.
195 *
196 * @throws IllegalArgumentException if context is null.
Jun Mukaid4eaef72015-10-30 15:54:33 -0700197 * @hide
Jeff Brown2352b972011-04-12 22:39:53 -0700198 */
Jun Mukaid4eaef72015-10-30 15:54:33 -0700199 public static PointerIcon getDefaultIcon(@NonNull Context context) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100200 return getSystemIcon(context, TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700201 }
202
203 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100204 * Gets a system pointer icon for the given type.
205 * If typeis not recognized, returns the default pointer icon.
Jeff Brown2352b972011-04-12 22:39:53 -0700206 *
207 * @param context The context.
Michael Wrighte051f6f2016-05-13 17:44:16 +0100208 * @param type The pointer icon type.
Jeff Brown2352b972011-04-12 22:39:53 -0700209 * @return The pointer icon.
210 *
211 * @throws IllegalArgumentException if context is null.
212 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100213 public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
Jeff Brown2352b972011-04-12 22:39:53 -0700214 if (context == null) {
215 throw new IllegalArgumentException("context must not be null");
216 }
217
Michael Wrighte051f6f2016-05-13 17:44:16 +0100218 if (type == TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700219 return gNullIcon;
220 }
221
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700222 if (sDisplayListener == null) {
223 registerDisplayListener(context);
224 }
225
226 final int displayId = context.getDisplayId();
227 SparseArray<PointerIcon> systemIcons = gSystemIconsByDisplay.get(displayId);
228 if (systemIcons == null) {
229 systemIcons = new SparseArray<>();
230 gSystemIconsByDisplay.put(displayId, systemIcons);
231 }
232
233 PointerIcon icon = systemIcons.get(type);
234 // Reload if not in the same display.
Jun Mukaid4eaef72015-10-30 15:54:33 -0700235 if (icon != null) {
236 return icon;
237 }
238
Michael Wrighte051f6f2016-05-13 17:44:16 +0100239 int typeIndex = getSystemIconTypeIndex(type);
240 if (typeIndex == 0) {
241 typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700242 }
243
Jun Mukaie4e75da2015-12-15 16:19:20 -0800244 int defStyle = sUseLargeIcons ?
Jun Mukai19a56012015-11-24 11:25:52 -0800245 com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
Jeff Brown2352b972011-04-12 22:39:53 -0700246 TypedArray a = context.obtainStyledAttributes(null,
247 com.android.internal.R.styleable.Pointer,
Jun Mukai19a56012015-11-24 11:25:52 -0800248 0, defStyle);
Michael Wrighte051f6f2016-05-13 17:44:16 +0100249 int resourceId = a.getResourceId(typeIndex, -1);
Jeff Brown2352b972011-04-12 22:39:53 -0700250 a.recycle();
251
252 if (resourceId == -1) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100253 Log.w(TAG, "Missing theme resources for pointer icon type " + type);
254 return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT);
Jeff Brown2352b972011-04-12 22:39:53 -0700255 }
256
Michael Wrighte051f6f2016-05-13 17:44:16 +0100257 icon = new PointerIcon(type);
Jeff Brown2352b972011-04-12 22:39:53 -0700258 if ((resourceId & 0xff000000) == 0x01000000) {
259 icon.mSystemIconResourceId = resourceId;
260 } else {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800261 icon.loadResource(context, context.getResources(), resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700262 }
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700263 systemIcons.append(type, icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700264 return icon;
265 }
266
267 /**
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800268 * Updates wheter accessibility large icons are used or not.
269 * @hide
270 */
271 public static void setUseLargeIcons(boolean use) {
272 sUseLargeIcons = use;
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700273 gSystemIconsByDisplay.clear();
Jun Mukai1f3dbff2015-12-16 14:41:25 -0800274 }
275
276 /**
Michael Wrighte051f6f2016-05-13 17:44:16 +0100277 * Creates a custom pointer icon from the given bitmap and hotspot information.
Jeff Brown2352b972011-04-12 22:39:53 -0700278 *
279 * @param bitmap The bitmap for the icon.
Alan Viverette376d10e2014-09-02 10:45:30 -0700280 * @param hotSpotX The X offset of the pointer icon hotspot in the bitmap.
Jeff Brown2352b972011-04-12 22:39:53 -0700281 * Must be within the [0, bitmap.getWidth()) range.
Alan Viverette376d10e2014-09-02 10:45:30 -0700282 * @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap.
Jeff Brown2352b972011-04-12 22:39:53 -0700283 * Must be within the [0, bitmap.getHeight()) range.
284 * @return A pointer icon for this bitmap.
285 *
286 * @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot
287 * parameters are invalid.
288 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100289 public static PointerIcon create(@NonNull Bitmap bitmap, float hotSpotX, float hotSpotY) {
Jeff Brown2352b972011-04-12 22:39:53 -0700290 if (bitmap == null) {
291 throw new IllegalArgumentException("bitmap must not be null");
292 }
293 validateHotSpot(bitmap, hotSpotX, hotSpotY);
294
Michael Wrighte051f6f2016-05-13 17:44:16 +0100295 PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
Jeff Brown2352b972011-04-12 22:39:53 -0700296 icon.mBitmap = bitmap;
297 icon.mHotSpotX = hotSpotX;
298 icon.mHotSpotY = hotSpotY;
299 return icon;
300 }
301
302 /**
303 * Loads a custom pointer icon from an XML resource.
304 * <p>
305 * The XML resource should have the following form:
306 * <code>
307 * &lt;?xml version="1.0" encoding="utf-8"?&gt;
308 * &lt;pointer-icon xmlns:android="http://schemas.android.com/apk/res/android"
309 * android:bitmap="@drawable/my_pointer_bitmap"
310 * android:hotSpotX="24"
311 * android:hotSpotY="24" /&gt;
312 * </code>
313 * </p>
314 *
315 * @param resources The resources object.
316 * @param resourceId The resource id.
317 * @return The pointer icon.
318 *
319 * @throws IllegalArgumentException if resources is null.
320 * @throws Resources.NotFoundException if the resource was not found or the drawable
321 * linked in the resource was not found.
322 */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100323 public static PointerIcon load(@NonNull Resources resources, @XmlRes int resourceId) {
Jeff Brown2352b972011-04-12 22:39:53 -0700324 if (resources == null) {
325 throw new IllegalArgumentException("resources must not be null");
326 }
327
Michael Wrighte051f6f2016-05-13 17:44:16 +0100328 PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800329 icon.loadResource(null, resources, resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700330 return icon;
331 }
332
333 /**
334 * Loads the bitmap and hotspot information for a pointer icon, if it is not already loaded.
335 * Returns a pointer icon (not necessarily the same instance) with the information filled in.
336 *
337 * @param context The context.
338 * @return The loaded pointer icon.
339 *
340 * @throws IllegalArgumentException if context is null.
Jeff Brown2352b972011-04-12 22:39:53 -0700341 * @hide
342 */
Mathew Inwood8c854f82018-09-14 12:35:36 +0100343 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jun Mukaid4eaef72015-10-30 15:54:33 -0700344 public PointerIcon load(@NonNull Context context) {
Jeff Brown2352b972011-04-12 22:39:53 -0700345 if (context == null) {
346 throw new IllegalArgumentException("context must not be null");
347 }
348
349 if (mSystemIconResourceId == 0 || mBitmap != null) {
350 return this;
351 }
352
Michael Wrighte051f6f2016-05-13 17:44:16 +0100353 PointerIcon result = new PointerIcon(mType);
Jeff Brown2352b972011-04-12 22:39:53 -0700354 result.mSystemIconResourceId = mSystemIconResourceId;
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800355 result.loadResource(context, context.getResources(), mSystemIconResourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700356 return result;
357 }
358
Jun Mukaid4eaef72015-10-30 15:54:33 -0700359 /** @hide */
Michael Wrighte051f6f2016-05-13 17:44:16 +0100360 public int getType() {
361 return mType;
Jeff Brown2352b972011-04-12 22:39:53 -0700362 }
363
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700364 public static final @android.annotation.NonNull Parcelable.Creator<PointerIcon> CREATOR
Jeff Brown2352b972011-04-12 22:39:53 -0700365 = new Parcelable.Creator<PointerIcon>() {
366 public PointerIcon createFromParcel(Parcel in) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100367 int type = in.readInt();
368 if (type == TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700369 return getNullIcon();
370 }
371
372 int systemIconResourceId = in.readInt();
373 if (systemIconResourceId != 0) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100374 PointerIcon icon = new PointerIcon(type);
Jeff Brown2352b972011-04-12 22:39:53 -0700375 icon.mSystemIconResourceId = systemIconResourceId;
376 return icon;
377 }
378
379 Bitmap bitmap = Bitmap.CREATOR.createFromParcel(in);
380 float hotSpotX = in.readFloat();
381 float hotSpotY = in.readFloat();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100382 return PointerIcon.create(bitmap, hotSpotX, hotSpotY);
Jeff Brown2352b972011-04-12 22:39:53 -0700383 }
384
385 public PointerIcon[] newArray(int size) {
386 return new PointerIcon[size];
387 }
388 };
389
390 public int describeContents() {
391 return 0;
392 }
393
394 public void writeToParcel(Parcel out, int flags) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100395 out.writeInt(mType);
Jeff Brown2352b972011-04-12 22:39:53 -0700396
Michael Wrighte051f6f2016-05-13 17:44:16 +0100397 if (mType != TYPE_NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700398 out.writeInt(mSystemIconResourceId);
399 if (mSystemIconResourceId == 0) {
400 mBitmap.writeToParcel(out, flags);
401 out.writeFloat(mHotSpotX);
402 out.writeFloat(mHotSpotY);
403 }
404 }
405 }
406
407 @Override
408 public boolean equals(Object other) {
409 if (this == other) {
410 return true;
411 }
412
413 if (other == null || !(other instanceof PointerIcon)) {
414 return false;
415 }
416
417 PointerIcon otherIcon = (PointerIcon) other;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100418 if (mType != otherIcon.mType
Jeff Brown2352b972011-04-12 22:39:53 -0700419 || mSystemIconResourceId != otherIcon.mSystemIconResourceId) {
420 return false;
421 }
422
423 if (mSystemIconResourceId == 0 && (mBitmap != otherIcon.mBitmap
424 || mHotSpotX != otherIcon.mHotSpotX
425 || mHotSpotY != otherIcon.mHotSpotY)) {
426 return false;
427 }
428
429 return true;
430 }
431
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500432 /**
433 * Get the Bitmap from the Drawable.
434 *
435 * If the Bitmap needed to be scaled up to account for density, BitmapDrawable
436 * handles this at draw time. But this class doesn't actually draw the Bitmap;
437 * it is just a holder for native code to access its SkBitmap. So this needs to
438 * get a version that is scaled to account for density.
439 */
440 private Bitmap getBitmapFromDrawable(BitmapDrawable bitmapDrawable) {
441 Bitmap bitmap = bitmapDrawable.getBitmap();
442 final int scaledWidth = bitmapDrawable.getIntrinsicWidth();
443 final int scaledHeight = bitmapDrawable.getIntrinsicHeight();
444 if (scaledWidth == bitmap.getWidth() && scaledHeight == bitmap.getHeight()) {
445 return bitmap;
446 }
447
448 Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
449 RectF dst = new RectF(0, 0, scaledWidth, scaledHeight);
450
451 Bitmap scaled = Bitmap.createBitmap(scaledWidth, scaledHeight, bitmap.getConfig());
452 Canvas canvas = new Canvas(scaled);
453 Paint paint = new Paint();
454 paint.setFilterBitmap(true);
455 canvas.drawBitmap(bitmap, src, dst, paint);
456 return scaled;
457 }
458
Tor Norbye7b9c9122013-05-30 16:48:33 -0700459 private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
Alan Viverette376d10e2014-09-02 10:45:30 -0700460 final XmlResourceParser parser = resources.getXml(resourceId);
Jeff Brown2352b972011-04-12 22:39:53 -0700461 final int bitmapRes;
462 final float hotSpotX;
463 final float hotSpotY;
464 try {
465 XmlUtils.beginDocument(parser, "pointer-icon");
466
Alan Viverette376d10e2014-09-02 10:45:30 -0700467 final TypedArray a = resources.obtainAttributes(
Jeff Brown2352b972011-04-12 22:39:53 -0700468 parser, com.android.internal.R.styleable.PointerIcon);
469 bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
Alan Viverette376d10e2014-09-02 10:45:30 -0700470 hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
471 hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
Jeff Brown2352b972011-04-12 22:39:53 -0700472 a.recycle();
473 } catch (Exception ex) {
474 throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
475 } finally {
476 parser.close();
477 }
478
479 if (bitmapRes == 0) {
480 throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
481 }
482
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800483 Drawable drawable;
484 if (context == null) {
485 drawable = resources.getDrawable(bitmapRes);
486 } else {
487 drawable = context.getDrawable(bitmapRes);
488 }
Jun Mukai808196f2015-10-28 16:46:44 -0700489 if (drawable instanceof AnimationDrawable) {
490 // Extract animation frame bitmaps.
491 final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
492 final int frames = animationDrawable.getNumberOfFrames();
493 drawable = animationDrawable.getFrame(0);
494 if (frames == 1) {
495 Log.w(TAG, "Animation icon with single frame -- simply treating the first "
496 + "frame as a normal bitmap icon.");
497 } else {
498 // Assumes they have the exact duration.
499 mDurationPerFrame = animationDrawable.getDuration(0);
500 mBitmapFrames = new Bitmap[frames - 1];
501 final int width = drawable.getIntrinsicWidth();
502 final int height = drawable.getIntrinsicHeight();
503 for (int i = 1; i < frames; ++i) {
504 Drawable drawableFrame = animationDrawable.getFrame(i);
505 if (!(drawableFrame instanceof BitmapDrawable)) {
506 throw new IllegalArgumentException("Frame of an animated pointer icon "
507 + "must refer to a bitmap drawable.");
508 }
509 if (drawableFrame.getIntrinsicWidth() != width ||
510 drawableFrame.getIntrinsicHeight() != height) {
511 throw new IllegalArgumentException("The bitmap size of " + i + "-th frame "
512 + "is different. All frames should have the exact same size and "
513 + "share the same hotspot.");
514 }
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500515 BitmapDrawable bitmapDrawableFrame = (BitmapDrawable) drawableFrame;
516 mBitmapFrames[i - 1] = getBitmapFromDrawable(bitmapDrawableFrame);
Jun Mukai808196f2015-10-28 16:46:44 -0700517 }
518 }
519 }
Jeff Brown2352b972011-04-12 22:39:53 -0700520 if (!(drawable instanceof BitmapDrawable)) {
521 throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
522 + "refer to a bitmap drawable.");
523 }
524
Leon Scroggins III2f1b06b2018-02-15 10:40:33 -0500525 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
526 final Bitmap bitmap = getBitmapFromDrawable(bitmapDrawable);
Vladislav Kaznacheev44e7de32017-12-20 12:18:03 -0800527 validateHotSpot(bitmap, hotSpotX, hotSpotY);
Jeff Brown2352b972011-04-12 22:39:53 -0700528 // Set the properties now that we have successfully loaded the icon.
Vladislav Kaznacheev44e7de32017-12-20 12:18:03 -0800529 mBitmap = bitmap;
Jeff Brown2352b972011-04-12 22:39:53 -0700530 mHotSpotX = hotSpotX;
531 mHotSpotY = hotSpotY;
532 }
533
534 private static void validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY) {
535 if (hotSpotX < 0 || hotSpotX >= bitmap.getWidth()) {
536 throw new IllegalArgumentException("x hotspot lies outside of the bitmap area");
537 }
538 if (hotSpotY < 0 || hotSpotY >= bitmap.getHeight()) {
539 throw new IllegalArgumentException("y hotspot lies outside of the bitmap area");
540 }
541 }
542
Michael Wrighte051f6f2016-05-13 17:44:16 +0100543 private static int getSystemIconTypeIndex(int type) {
544 switch (type) {
545 case TYPE_ARROW:
Jeff Brown2352b972011-04-12 22:39:53 -0700546 return com.android.internal.R.styleable.Pointer_pointerIconArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100547 case TYPE_SPOT_HOVER:
Jeff Brown2352b972011-04-12 22:39:53 -0700548 return com.android.internal.R.styleable.Pointer_pointerIconSpotHover;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100549 case TYPE_SPOT_TOUCH:
Jeff Brown2352b972011-04-12 22:39:53 -0700550 return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100551 case TYPE_SPOT_ANCHOR:
Jeff Brown2352b972011-04-12 22:39:53 -0700552 return com.android.internal.R.styleable.Pointer_pointerIconSpotAnchor;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100553 case TYPE_HAND:
Jun Mukai1db53972015-09-11 18:08:31 -0700554 return com.android.internal.R.styleable.Pointer_pointerIconHand;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100555 case TYPE_CONTEXT_MENU:
Jun Mukai1db53972015-09-11 18:08:31 -0700556 return com.android.internal.R.styleable.Pointer_pointerIconContextMenu;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100557 case TYPE_HELP:
Jun Mukai1db53972015-09-11 18:08:31 -0700558 return com.android.internal.R.styleable.Pointer_pointerIconHelp;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100559 case TYPE_WAIT:
Jun Mukai808196f2015-10-28 16:46:44 -0700560 return com.android.internal.R.styleable.Pointer_pointerIconWait;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100561 case TYPE_CELL:
Jun Mukai1db53972015-09-11 18:08:31 -0700562 return com.android.internal.R.styleable.Pointer_pointerIconCell;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100563 case TYPE_CROSSHAIR:
Jun Mukai1db53972015-09-11 18:08:31 -0700564 return com.android.internal.R.styleable.Pointer_pointerIconCrosshair;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100565 case TYPE_TEXT:
Jun Mukai1db53972015-09-11 18:08:31 -0700566 return com.android.internal.R.styleable.Pointer_pointerIconText;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100567 case TYPE_VERTICAL_TEXT:
Jun Mukai1db53972015-09-11 18:08:31 -0700568 return com.android.internal.R.styleable.Pointer_pointerIconVerticalText;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100569 case TYPE_ALIAS:
Jun Mukai1db53972015-09-11 18:08:31 -0700570 return com.android.internal.R.styleable.Pointer_pointerIconAlias;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100571 case TYPE_COPY:
Jun Mukai1db53972015-09-11 18:08:31 -0700572 return com.android.internal.R.styleable.Pointer_pointerIconCopy;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100573 case TYPE_ALL_SCROLL:
Jun Mukai1db53972015-09-11 18:08:31 -0700574 return com.android.internal.R.styleable.Pointer_pointerIconAllScroll;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100575 case TYPE_NO_DROP:
Jun Mukai1db53972015-09-11 18:08:31 -0700576 return com.android.internal.R.styleable.Pointer_pointerIconNodrop;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100577 case TYPE_HORIZONTAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700578 return com.android.internal.R.styleable.Pointer_pointerIconHorizontalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100579 case TYPE_VERTICAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700580 return com.android.internal.R.styleable.Pointer_pointerIconVerticalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100581 case TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700582 return com.android.internal.R.styleable.
583 Pointer_pointerIconTopRightDiagonalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100584 case TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW:
Jun Mukai1db53972015-09-11 18:08:31 -0700585 return com.android.internal.R.styleable.
586 Pointer_pointerIconTopLeftDiagonalDoubleArrow;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100587 case TYPE_ZOOM_IN:
Jun Mukai1db53972015-09-11 18:08:31 -0700588 return com.android.internal.R.styleable.Pointer_pointerIconZoomIn;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100589 case TYPE_ZOOM_OUT:
Jun Mukai1db53972015-09-11 18:08:31 -0700590 return com.android.internal.R.styleable.Pointer_pointerIconZoomOut;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100591 case TYPE_GRAB:
Jun Mukai1db53972015-09-11 18:08:31 -0700592 return com.android.internal.R.styleable.Pointer_pointerIconGrab;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100593 case TYPE_GRABBING:
Jun Mukai1db53972015-09-11 18:08:31 -0700594 return com.android.internal.R.styleable.Pointer_pointerIconGrabbing;
Jeff Brown2352b972011-04-12 22:39:53 -0700595 default:
596 return 0;
597 }
598 }
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700599
600 /**
601 * Manage system icon cache handled by display lifecycle.
602 * @param context The context.
603 */
604 private static void registerDisplayListener(@NonNull Context context) {
605 sDisplayListener = new DisplayManager.DisplayListener() {
606 @Override
607 public void onDisplayAdded(int displayId) {
608 }
609
610 @Override
611 public void onDisplayRemoved(int displayId) {
612 gSystemIconsByDisplay.remove(displayId);
613 }
614
615 @Override
616 public void onDisplayChanged(int displayId) {
617 gSystemIconsByDisplay.remove(displayId);
618 }
619 };
620
621 DisplayManager displayManager = context.getSystemService(DisplayManager.class);
622 displayManager.registerDisplayListener(sDisplayListener, null /* handler */);
623 }
624
Jeff Brown2352b972011-04-12 22:39:53 -0700625}