1 /*
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 
17 package android.view;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.XmlRes;
22 import android.compat.annotation.UnsupportedAppUsage;
23 import android.content.Context;
24 import android.content.res.Resources;
25 import android.content.res.TypedArray;
26 import android.content.res.XmlResourceParser;
27 import android.graphics.Bitmap;
28 import android.graphics.Canvas;
29 import android.graphics.Paint;
30 import android.graphics.Rect;
31 import android.graphics.RectF;
32 import android.graphics.drawable.AnimationDrawable;
33 import android.graphics.drawable.BitmapDrawable;
34 import android.graphics.drawable.Drawable;
35 import android.hardware.display.DisplayManager;
36 import android.os.Build;
37 import android.os.Parcel;
38 import android.os.Parcelable;
39 import android.util.Log;
40 import android.util.SparseArray;
41 
42 import com.android.internal.util.XmlUtils;
43 
44 /**
45  * Represents an icon that can be used as a mouse pointer.
46  * <p>
47  * Pointer icons can be provided either by the system using system types,
48  * or by applications using bitmaps or application resources.
49  * </p>
50  */
51 public final class PointerIcon implements Parcelable {
52     private static final String TAG = "PointerIcon";
53 
54     /** {@hide} Type constant: Custom icon with a user-supplied bitmap. */
55     public static final int TYPE_CUSTOM = -1;
56 
57     /** Type constant: Null icon.  It has no bitmap. */
58     public static final int TYPE_NULL = 0;
59 
60     /**
61      * Type constant: no icons are specified. If all views uses this, then the pointer icon falls
62      * back to the default type, but this is helpful to distinguish a view that explicitly wants
63      * to have the default icon.
64      * @hide
65      */
66     public static final int TYPE_NOT_SPECIFIED = 1;
67 
68     /** Type constant: Arrow icon.  (Default mouse pointer) */
69     public static final int TYPE_ARROW = 1000;
70 
71     /** {@hide} Type constant: Spot hover icon for touchpads. */
72     public static final int TYPE_SPOT_HOVER = 2000;
73 
74     /** {@hide} Type constant: Spot touch icon for touchpads. */
75     public static final int TYPE_SPOT_TOUCH = 2001;
76 
77     /** {@hide} Type constant: Spot anchor icon for touchpads. */
78     public static final int TYPE_SPOT_ANCHOR = 2002;
79 
80     // Type constants for additional predefined icons for mice.
81     /** Type constant: context-menu. */
82     public static final int TYPE_CONTEXT_MENU = 1001;
83 
84     /** Type constant: hand. */
85     public static final int TYPE_HAND = 1002;
86 
87     /** Type constant: help. */
88     public static final int TYPE_HELP = 1003;
89 
90     /** Type constant: wait. */
91     public static final int TYPE_WAIT = 1004;
92 
93     /** Type constant: cell. */
94     public static final int TYPE_CELL = 1006;
95 
96     /** Type constant: crosshair. */
97     public static final int TYPE_CROSSHAIR = 1007;
98 
99     /** Type constant: text. */
100     public static final int TYPE_TEXT = 1008;
101 
102     /** Type constant: vertical-text. */
103     public static final int TYPE_VERTICAL_TEXT = 1009;
104 
105     /** Type constant: alias (indicating an alias of/shortcut to something is
106       * to be created. */
107     public static final int TYPE_ALIAS = 1010;
108 
109     /** Type constant: copy. */
110     public static final int TYPE_COPY = 1011;
111 
112     /** Type constant: no-drop. */
113     public static final int TYPE_NO_DROP = 1012;
114 
115     /** Type constant: all-scroll. */
116     public static final int TYPE_ALL_SCROLL = 1013;
117 
118     /** Type constant: horizontal double arrow mainly for resizing. */
119     public static final int TYPE_HORIZONTAL_DOUBLE_ARROW = 1014;
120 
121     /** Type constant: vertical double arrow mainly for resizing. */
122     public static final int TYPE_VERTICAL_DOUBLE_ARROW = 1015;
123 
124     /** Type constant: diagonal double arrow -- top-right to bottom-left. */
125     public static final int TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016;
126 
127     /** Type constant: diagonal double arrow -- top-left to bottom-right. */
128     public static final int TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017;
129 
130     /** Type constant: zoom-in. */
131     public static final int TYPE_ZOOM_IN = 1018;
132 
133     /** Type constant: zoom-out. */
134     public static final int TYPE_ZOOM_OUT = 1019;
135 
136     /** Type constant: grab. */
137     public static final int TYPE_GRAB = 1020;
138 
139     /** Type constant: grabbing. */
140     public static final int TYPE_GRABBING = 1021;
141 
142     /** Type constant: handwriting. */
143     public static final int TYPE_HANDWRITING = 1022;
144 
145     // OEM private types should be defined starting at this range to avoid
146     // conflicts with any system types that may be defined in the future.
147     private static final int TYPE_OEM_FIRST = 10000;
148 
149     /** The default pointer icon. */
150     public static final int TYPE_DEFAULT = TYPE_ARROW;
151 
152     private static final PointerIcon gNullIcon = new PointerIcon(TYPE_NULL);
153     private static final SparseArray<SparseArray<PointerIcon>> gSystemIconsByDisplay =
154             new SparseArray<SparseArray<PointerIcon>>();
155     private static boolean sUseLargeIcons = false;
156 
157     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
158     private final int mType;
159     private int mSystemIconResourceId;
160     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
161     private Bitmap mBitmap;
162     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
163     private float mHotSpotX;
164     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
165     private float mHotSpotY;
166     // The bitmaps for the additional frame of animated pointer icon. Note that the first frame
167     // will be stored in mBitmap.
168     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
169     private Bitmap mBitmapFrames[];
170     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
171     private int mDurationPerFrame;
172 
173     /**
174      * Listener for displays lifecycle.
175      * @hide
176      */
177     private static DisplayManager.DisplayListener sDisplayListener;
178 
PointerIcon(int type)179     private PointerIcon(int type) {
180         mType = type;
181     }
182 
183     /**
184      * Gets a special pointer icon that has no bitmap.
185      *
186      * @return The null pointer icon.
187      *
188      * @see #TYPE_NULL
189      * @hide
190      */
getNullIcon()191     public static @NonNull PointerIcon getNullIcon() {
192         return gNullIcon;
193     }
194 
195     /**
196      * Gets the default pointer icon.
197      *
198      * @param context The context.
199      * @return The default pointer icon.
200      *
201      * @throws IllegalArgumentException if context is null.
202      * @hide
203      */
getDefaultIcon(@onNull Context context)204     public static @NonNull PointerIcon getDefaultIcon(@NonNull Context context) {
205         return getSystemIcon(context, TYPE_DEFAULT);
206     }
207 
208     /**
209      * Gets a system pointer icon for the given type.
210      * If typeis not recognized, returns the default pointer icon.
211      *
212      * @param context The context.
213      * @param type The pointer icon type.
214      * @return The pointer icon.
215      *
216      * @throws IllegalArgumentException if context is null.
217      */
getSystemIcon(@onNull Context context, int type)218     public static @NonNull PointerIcon getSystemIcon(@NonNull Context context, int type) {
219         if (context == null) {
220             throw new IllegalArgumentException("context must not be null");
221         }
222 
223         if (type == TYPE_NULL) {
224             return gNullIcon;
225         }
226 
227         if (sDisplayListener == null) {
228             registerDisplayListener(context);
229         }
230 
231         final int displayId = context.getDisplayId();
232         SparseArray<PointerIcon> systemIcons = gSystemIconsByDisplay.get(displayId);
233         if (systemIcons == null) {
234             systemIcons = new SparseArray<>();
235             gSystemIconsByDisplay.put(displayId, systemIcons);
236         }
237 
238         PointerIcon icon = systemIcons.get(type);
239         // Reload if not in the same display.
240         if (icon != null) {
241             return icon;
242         }
243 
244         int typeIndex = getSystemIconTypeIndex(type);
245         if (typeIndex == 0) {
246             typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
247         }
248 
249         int defStyle = sUseLargeIcons ?
250                 com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
251         TypedArray a = context.obtainStyledAttributes(null,
252                 com.android.internal.R.styleable.Pointer,
253                 0, defStyle);
254         int resourceId = a.getResourceId(typeIndex, -1);
255         a.recycle();
256 
257         if (resourceId == -1) {
258             Log.w(TAG, "Missing theme resources for pointer icon type " + type);
259             return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT);
260         }
261 
262         icon = new PointerIcon(type);
263         if ((resourceId & 0xff000000) == 0x01000000) {
264             icon.mSystemIconResourceId = resourceId;
265         } else {
266             icon.loadResource(context, context.getResources(), resourceId);
267         }
268         systemIcons.append(type, icon);
269         return icon;
270     }
271 
272     /**
273      * Updates wheter accessibility large icons are used or not.
274      * @hide
275      */
setUseLargeIcons(boolean use)276     public static void setUseLargeIcons(boolean use) {
277         sUseLargeIcons = use;
278         gSystemIconsByDisplay.clear();
279     }
280 
281     /**
282      * Creates a custom pointer icon from the given bitmap and hotspot information.
283      *
284      * @param bitmap The bitmap for the icon.
285      * @param hotSpotX The X offset of the pointer icon hotspot in the bitmap.
286      *        Must be within the [0, bitmap.getWidth()) range.
287      * @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap.
288      *        Must be within the [0, bitmap.getHeight()) range.
289      * @return A pointer icon for this bitmap.
290      *
291      * @throws IllegalArgumentException if bitmap is null, or if the x/y hotspot
292      *         parameters are invalid.
293      */
create(@onNull Bitmap bitmap, float hotSpotX, float hotSpotY)294     public static @NonNull PointerIcon create(@NonNull Bitmap bitmap, float hotSpotX,
295             float hotSpotY) {
296         if (bitmap == null) {
297             throw new IllegalArgumentException("bitmap must not be null");
298         }
299         validateHotSpot(bitmap, hotSpotX, hotSpotY);
300 
301         PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
302         icon.mBitmap = bitmap;
303         icon.mHotSpotX = hotSpotX;
304         icon.mHotSpotY = hotSpotY;
305         return icon;
306     }
307 
308     /**
309      * Loads a custom pointer icon from an XML resource.
310      * <p>
311      * The XML resource should have the following form:
312      * <code>
313      * &lt;?xml version="1.0" encoding="utf-8"?&gt;
314      * &lt;pointer-icon xmlns:android="http://schemas.android.com/apk/res/android"
315      *   android:bitmap="@drawable/my_pointer_bitmap"
316      *   android:hotSpotX="24"
317      *   android:hotSpotY="24" /&gt;
318      * </code>
319      * </p>
320      *
321      * @param resources The resources object.
322      * @param resourceId The resource id.
323      * @return The pointer icon.
324      *
325      * @throws IllegalArgumentException if resources is null.
326      * @throws Resources.NotFoundException if the resource was not found or the drawable
327      * linked in the resource was not found.
328      */
load(@onNull Resources resources, @XmlRes int resourceId)329     public static @NonNull PointerIcon load(@NonNull Resources resources, @XmlRes int resourceId) {
330         if (resources == null) {
331             throw new IllegalArgumentException("resources must not be null");
332         }
333 
334         PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
335         icon.loadResource(null, resources, resourceId);
336         return icon;
337     }
338 
339     /**
340      * Loads the bitmap and hotspot information for a pointer icon, if it is not already loaded.
341      * Returns a pointer icon (not necessarily the same instance) with the information filled in.
342      *
343      * @param context The context.
344      * @return The loaded pointer icon.
345      *
346      * @throws IllegalArgumentException if context is null.
347      * @hide
348      */
349     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
load(@onNull Context context)350     public @NonNull PointerIcon load(@NonNull Context context) {
351         if (context == null) {
352             throw new IllegalArgumentException("context must not be null");
353         }
354 
355         if (mSystemIconResourceId == 0 || mBitmap != null) {
356             return this;
357         }
358 
359         PointerIcon result = new PointerIcon(mType);
360         result.mSystemIconResourceId = mSystemIconResourceId;
361         result.loadResource(context, context.getResources(), mSystemIconResourceId);
362         return result;
363     }
364 
365     /** @hide */
getType()366     public int getType() {
367         return mType;
368     }
369 
370     public static final @NonNull Parcelable.Creator<PointerIcon> CREATOR
371             = new Parcelable.Creator<PointerIcon>() {
372         public PointerIcon createFromParcel(Parcel in) {
373             int type = in.readInt();
374             if (type == TYPE_NULL) {
375                 return getNullIcon();
376             }
377 
378             int systemIconResourceId = in.readInt();
379             if (systemIconResourceId != 0) {
380                 PointerIcon icon = new PointerIcon(type);
381                 icon.mSystemIconResourceId = systemIconResourceId;
382                 return icon;
383             }
384 
385             Bitmap bitmap = Bitmap.CREATOR.createFromParcel(in);
386             float hotSpotX = in.readFloat();
387             float hotSpotY = in.readFloat();
388             return PointerIcon.create(bitmap, hotSpotX, hotSpotY);
389         }
390 
391         public PointerIcon[] newArray(int size) {
392             return new PointerIcon[size];
393         }
394     };
395 
describeContents()396     public int describeContents() {
397         return 0;
398     }
399 
writeToParcel(Parcel out, int flags)400     public void writeToParcel(Parcel out, int flags) {
401         out.writeInt(mType);
402 
403         if (mType != TYPE_NULL) {
404             out.writeInt(mSystemIconResourceId);
405             if (mSystemIconResourceId == 0) {
406                 mBitmap.writeToParcel(out, flags);
407                 out.writeFloat(mHotSpotX);
408                 out.writeFloat(mHotSpotY);
409             }
410         }
411     }
412 
413     @Override
equals(@ullable Object other)414     public boolean equals(@Nullable Object other) {
415         if (this == other) {
416             return true;
417         }
418 
419         if (other == null || !(other instanceof PointerIcon)) {
420             return false;
421         }
422 
423         PointerIcon otherIcon = (PointerIcon) other;
424         if (mType != otherIcon.mType
425                 || mSystemIconResourceId != otherIcon.mSystemIconResourceId) {
426             return false;
427         }
428 
429         if (mSystemIconResourceId == 0 && (mBitmap != otherIcon.mBitmap
430                 || mHotSpotX != otherIcon.mHotSpotX
431                 || mHotSpotY != otherIcon.mHotSpotY)) {
432             return false;
433         }
434 
435         return true;
436     }
437 
438     /**
439      *  Get the Bitmap from the Drawable.
440      *
441      *  If the Bitmap needed to be scaled up to account for density, BitmapDrawable
442      *  handles this at draw time. But this class doesn't actually draw the Bitmap;
443      *  it is just a holder for native code to access its SkBitmap. So this needs to
444      *  get a version that is scaled to account for density.
445      */
getBitmapFromDrawable(BitmapDrawable bitmapDrawable)446     private Bitmap getBitmapFromDrawable(BitmapDrawable bitmapDrawable) {
447         Bitmap bitmap = bitmapDrawable.getBitmap();
448         final int scaledWidth  = bitmapDrawable.getIntrinsicWidth();
449         final int scaledHeight = bitmapDrawable.getIntrinsicHeight();
450         if (scaledWidth == bitmap.getWidth() && scaledHeight == bitmap.getHeight()) {
451             return bitmap;
452         }
453 
454         Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
455         RectF dst = new RectF(0, 0, scaledWidth, scaledHeight);
456 
457         Bitmap scaled = Bitmap.createBitmap(scaledWidth, scaledHeight, bitmap.getConfig());
458         Canvas canvas = new Canvas(scaled);
459         Paint paint = new Paint();
460         paint.setFilterBitmap(true);
461         canvas.drawBitmap(bitmap, src, dst, paint);
462         return scaled;
463     }
464 
loadResource(Context context, Resources resources, @XmlRes int resourceId)465     private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
466         final XmlResourceParser parser = resources.getXml(resourceId);
467         final int bitmapRes;
468         final float hotSpotX;
469         final float hotSpotY;
470         try {
471             XmlUtils.beginDocument(parser, "pointer-icon");
472 
473             final TypedArray a = resources.obtainAttributes(
474                     parser, com.android.internal.R.styleable.PointerIcon);
475             bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
476             hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
477             hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
478             a.recycle();
479         } catch (Exception ex) {
480             throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
481         } finally {
482             parser.close();
483         }
484 
485         if (bitmapRes == 0) {
486             throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
487         }
488 
489         Drawable drawable;
490         if (context == null) {
491             drawable = resources.getDrawable(bitmapRes);
492         } else {
493             drawable = context.getDrawable(bitmapRes);
494         }
495         if (drawable instanceof AnimationDrawable) {
496             // Extract animation frame bitmaps.
497             final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
498             final int frames = animationDrawable.getNumberOfFrames();
499             drawable = animationDrawable.getFrame(0);
500             if (frames == 1) {
501                 Log.w(TAG, "Animation icon with single frame -- simply treating the first "
502                         + "frame as a normal bitmap icon.");
503             } else {
504                 // Assumes they have the exact duration.
505                 mDurationPerFrame = animationDrawable.getDuration(0);
506                 mBitmapFrames = new Bitmap[frames - 1];
507                 final int width = drawable.getIntrinsicWidth();
508                 final int height = drawable.getIntrinsicHeight();
509                 for (int i = 1; i < frames; ++i) {
510                     Drawable drawableFrame = animationDrawable.getFrame(i);
511                     if (!(drawableFrame instanceof BitmapDrawable)) {
512                         throw new IllegalArgumentException("Frame of an animated pointer icon "
513                                 + "must refer to a bitmap drawable.");
514                     }
515                     if (drawableFrame.getIntrinsicWidth() != width ||
516                         drawableFrame.getIntrinsicHeight() != height) {
517                         throw new IllegalArgumentException("The bitmap size of " + i + "-th frame "
518                                 + "is different. All frames should have the exact same size and "
519                                 + "share the same hotspot.");
520                     }
521                     BitmapDrawable bitmapDrawableFrame = (BitmapDrawable) drawableFrame;
522                     mBitmapFrames[i - 1] = getBitmapFromDrawable(bitmapDrawableFrame);
523                 }
524             }
525         }
526         if (!(drawable instanceof BitmapDrawable)) {
527             throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
528                     + "refer to a bitmap drawable.");
529         }
530 
531         BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
532         final Bitmap bitmap = getBitmapFromDrawable(bitmapDrawable);
533         validateHotSpot(bitmap, hotSpotX, hotSpotY);
534         // Set the properties now that we have successfully loaded the icon.
535         mBitmap = bitmap;
536         mHotSpotX = hotSpotX;
537         mHotSpotY = hotSpotY;
538     }
539 
540     @Override
toString()541     public String toString() {
542         return "PointerIcon{type=" + typeToString(mType)
543                 + ", hotspotX=" + mHotSpotX + ", hotspotY=" + mHotSpotY
544                 + ", systemIconResourceId=" + mSystemIconResourceId + "}";
545     }
546 
validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY)547     private static void validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY) {
548         if (hotSpotX < 0 || hotSpotX >= bitmap.getWidth()) {
549             throw new IllegalArgumentException("x hotspot lies outside of the bitmap area");
550         }
551         if (hotSpotY < 0 || hotSpotY >= bitmap.getHeight()) {
552             throw new IllegalArgumentException("y hotspot lies outside of the bitmap area");
553         }
554     }
555 
getSystemIconTypeIndex(int type)556     private static int getSystemIconTypeIndex(int type) {
557         switch (type) {
558             case TYPE_ARROW:
559                 return com.android.internal.R.styleable.Pointer_pointerIconArrow;
560             case TYPE_SPOT_HOVER:
561                 return com.android.internal.R.styleable.Pointer_pointerIconSpotHover;
562             case TYPE_SPOT_TOUCH:
563                 return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch;
564             case TYPE_SPOT_ANCHOR:
565                 return com.android.internal.R.styleable.Pointer_pointerIconSpotAnchor;
566             case TYPE_HAND:
567                 return com.android.internal.R.styleable.Pointer_pointerIconHand;
568             case TYPE_CONTEXT_MENU:
569                 return com.android.internal.R.styleable.Pointer_pointerIconContextMenu;
570             case TYPE_HELP:
571                 return com.android.internal.R.styleable.Pointer_pointerIconHelp;
572             case TYPE_WAIT:
573                 return com.android.internal.R.styleable.Pointer_pointerIconWait;
574             case TYPE_CELL:
575                 return com.android.internal.R.styleable.Pointer_pointerIconCell;
576             case TYPE_CROSSHAIR:
577                 return com.android.internal.R.styleable.Pointer_pointerIconCrosshair;
578             case TYPE_TEXT:
579                 return com.android.internal.R.styleable.Pointer_pointerIconText;
580             case TYPE_VERTICAL_TEXT:
581                 return com.android.internal.R.styleable.Pointer_pointerIconVerticalText;
582             case TYPE_ALIAS:
583                 return com.android.internal.R.styleable.Pointer_pointerIconAlias;
584             case TYPE_COPY:
585                 return com.android.internal.R.styleable.Pointer_pointerIconCopy;
586             case TYPE_ALL_SCROLL:
587                 return com.android.internal.R.styleable.Pointer_pointerIconAllScroll;
588             case TYPE_NO_DROP:
589                 return com.android.internal.R.styleable.Pointer_pointerIconNodrop;
590             case TYPE_HORIZONTAL_DOUBLE_ARROW:
591                 return com.android.internal.R.styleable.Pointer_pointerIconHorizontalDoubleArrow;
592             case TYPE_VERTICAL_DOUBLE_ARROW:
593                 return com.android.internal.R.styleable.Pointer_pointerIconVerticalDoubleArrow;
594             case TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW:
595                 return com.android.internal.R.styleable.
596                         Pointer_pointerIconTopRightDiagonalDoubleArrow;
597             case TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW:
598                 return com.android.internal.R.styleable.
599                         Pointer_pointerIconTopLeftDiagonalDoubleArrow;
600             case TYPE_ZOOM_IN:
601                 return com.android.internal.R.styleable.Pointer_pointerIconZoomIn;
602             case TYPE_ZOOM_OUT:
603                 return com.android.internal.R.styleable.Pointer_pointerIconZoomOut;
604             case TYPE_GRAB:
605                 return com.android.internal.R.styleable.Pointer_pointerIconGrab;
606             case TYPE_GRABBING:
607                 return com.android.internal.R.styleable.Pointer_pointerIconGrabbing;
608             case TYPE_HANDWRITING:
609                 return com.android.internal.R.styleable.Pointer_pointerIconHandwriting;
610             default:
611                 return 0;
612         }
613     }
614 
615     /**
616      * Manage system icon cache handled by display lifecycle.
617      * @param context The context.
618      */
registerDisplayListener(@onNull Context context)619     private static void registerDisplayListener(@NonNull Context context) {
620         sDisplayListener = new DisplayManager.DisplayListener() {
621             @Override
622             public void onDisplayAdded(int displayId) {
623             }
624 
625             @Override
626             public void onDisplayRemoved(int displayId) {
627                 gSystemIconsByDisplay.remove(displayId);
628             }
629 
630             @Override
631             public void onDisplayChanged(int displayId) {
632                 gSystemIconsByDisplay.remove(displayId);
633             }
634         };
635 
636         DisplayManager displayManager = context.getSystemService(DisplayManager.class);
637         displayManager.registerDisplayListener(sDisplayListener, null /* handler */);
638     }
639 
640     /**
641      * Convert type constant to string.
642      * @hide
643      */
typeToString(int type)644     public static String typeToString(int type) {
645         switch (type) {
646             case TYPE_CUSTOM: return "CUSTOM";
647             case TYPE_NULL: return "NULL";
648             case TYPE_NOT_SPECIFIED: return "NOT_SPECIFIED";
649             case TYPE_ARROW: return "ARROW";
650             case TYPE_SPOT_HOVER: return "SPOT_HOVER";
651             case TYPE_SPOT_TOUCH: return "SPOT_TOUCH";
652             case TYPE_SPOT_ANCHOR: return "SPOT_ANCHOR";
653             case TYPE_CONTEXT_MENU: return "CONTEXT_MENU";
654             case TYPE_HAND: return "HAND";
655             case TYPE_HELP: return "HELP";
656             case TYPE_WAIT: return "WAIT";
657             case TYPE_CELL: return "CELL";
658             case TYPE_CROSSHAIR: return "CROSSHAIR";
659             case TYPE_TEXT: return "TEXT";
660             case TYPE_VERTICAL_TEXT: return "VERTICAL_TEXT";
661             case TYPE_ALIAS: return "ALIAS";
662             case TYPE_COPY: return "COPY";
663             case TYPE_NO_DROP: return "NO_DROP";
664             case TYPE_ALL_SCROLL: return "ALL_SCROLL";
665             case TYPE_HORIZONTAL_DOUBLE_ARROW: return "HORIZONTAL_DOUBLE_ARROW";
666             case TYPE_VERTICAL_DOUBLE_ARROW: return "VERTICAL_DOUBLE_ARROW";
667             case TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW: return "TOP_RIGHT_DIAGONAL_DOUBLE_ARROW";
668             case TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW: return "TOP_LEFT_DIAGONAL_DOUBLE_ARROW";
669             case TYPE_ZOOM_IN: return "ZOOM_IN";
670             case TYPE_ZOOM_OUT: return "ZOOM_OUT";
671             case TYPE_GRAB: return "GRAB";
672             case TYPE_GRABBING: return "GRABBING";
673             default: return Integer.toString(type);
674         }
675     }
676 }
677