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