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.XmlRes;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.content.Context;
23 import android.content.res.Resources;
24 import android.content.res.TypedArray;
25 import android.content.res.XmlResourceParser;
26 import android.graphics.Bitmap;
27 import android.graphics.Canvas;
28 import android.graphics.Paint;
29 import android.graphics.Rect;
30 import android.graphics.RectF;
31 import android.graphics.drawable.AnimationDrawable;
32 import android.graphics.drawable.BitmapDrawable;
33 import android.graphics.drawable.Drawable;
34 import android.hardware.display.DisplayManager;
35 import android.os.Build;
36 import android.os.Parcel;
37 import android.os.Parcelable;
38 import android.util.Log;
39 import android.util.SparseArray;
40 
41 import com.android.internal.util.XmlUtils;
42 
43 /**
44  * Represents an icon that can be used as a mouse pointer.
45  * <p>
46  * Pointer icons can be provided either by the system using system types,
47  * or by applications using bitmaps or application resources.
48  * </p>
49  */
50 public final class PointerIcon implements Parcelable {
51     private static final String TAG = "PointerIcon";
52 
53     /** {@hide} Type constant: Custom icon with a user-supplied bitmap. */
54     public static final int TYPE_CUSTOM = -1;
55 
56     /** Type constant: Null icon.  It has no bitmap. */
57     public static final int TYPE_NULL = 0;
58 
59     /** 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
61      * to have the default icon.
62      * @hide
63      */
64     public static final int TYPE_NOT_SPECIFIED = 1;
65 
66     /** Type constant: Arrow icon.  (Default mouse pointer) */
67     public static final int TYPE_ARROW = 1000;
68 
69     /** {@hide} Type constant: Spot hover icon for touchpads. */
70     public static final int TYPE_SPOT_HOVER = 2000;
71 
72     /** {@hide} Type constant: Spot touch icon for touchpads. */
73     public static final int TYPE_SPOT_TOUCH = 2001;
74 
75     /** {@hide} Type constant: Spot anchor icon for touchpads. */
76     public static final int TYPE_SPOT_ANCHOR = 2002;
77 
78     // Type constants for additional predefined icons for mice.
79     /** Type constant: context-menu. */
80     public static final int TYPE_CONTEXT_MENU = 1001;
81 
82     /** Type constant: hand. */
83     public static final int TYPE_HAND = 1002;
84 
85     /** Type constant: help. */
86     public static final int TYPE_HELP = 1003;
87 
88     /** Type constant: wait. */
89     public static final int TYPE_WAIT = 1004;
90 
91     /** Type constant: cell. */
92     public static final int TYPE_CELL = 1006;
93 
94     /** Type constant: crosshair. */
95     public static final int TYPE_CROSSHAIR = 1007;
96 
97     /** Type constant: text. */
98     public static final int TYPE_TEXT = 1008;
99 
100     /** Type constant: vertical-text. */
101     public static final int TYPE_VERTICAL_TEXT = 1009;
102 
103     /** Type constant: alias (indicating an alias of/shortcut to something is
104       * to be created. */
105     public static final int TYPE_ALIAS = 1010;
106 
107     /** Type constant: copy. */
108     public static final int TYPE_COPY = 1011;
109 
110     /** Type constant: no-drop. */
111     public static final int TYPE_NO_DROP = 1012;
112 
113     /** Type constant: all-scroll. */
114     public static final int TYPE_ALL_SCROLL = 1013;
115 
116     /** Type constant: horizontal double arrow mainly for resizing. */
117     public static final int TYPE_HORIZONTAL_DOUBLE_ARROW = 1014;
118 
119     /** Type constant: vertical double arrow mainly for resizing. */
120     public static final int TYPE_VERTICAL_DOUBLE_ARROW = 1015;
121 
122     /** Type constant: diagonal double arrow -- top-right to bottom-left. */
123     public static final int TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW = 1016;
124 
125     /** Type constant: diagonal double arrow -- top-left to bottom-right. */
126     public static final int TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW = 1017;
127 
128     /** Type constant: zoom-in. */
129     public static final int TYPE_ZOOM_IN = 1018;
130 
131     /** Type constant: zoom-out. */
132     public static final int TYPE_ZOOM_OUT = 1019;
133 
134     /** Type constant: grab. */
135     public static final int TYPE_GRAB = 1020;
136 
137     /** Type constant: grabbing. */
138     public static final int TYPE_GRABBING = 1021;
139 
140     // 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;
143 
144     /** The default pointer icon. */
145     public static final int TYPE_DEFAULT = TYPE_ARROW;
146 
147     private static final PointerIcon gNullIcon = new PointerIcon(TYPE_NULL);
148     private static final SparseArray<SparseArray<PointerIcon>> gSystemIconsByDisplay =
149             new SparseArray<SparseArray<PointerIcon>>();
150     private static boolean sUseLargeIcons = false;
151 
152     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
153     private final int mType;
154     private int mSystemIconResourceId;
155     @UnsupportedAppUsage
156     private Bitmap mBitmap;
157     @UnsupportedAppUsage
158     private float mHotSpotX;
159     @UnsupportedAppUsage
160     private float mHotSpotY;
161     // The bitmaps for the additional frame of animated pointer icon. Note that the first frame
162     // will be stored in mBitmap.
163     @UnsupportedAppUsage
164     private Bitmap mBitmapFrames[];
165     @UnsupportedAppUsage
166     private int mDurationPerFrame;
167 
168     /**
169      * Listener for displays lifecycle.
170      * @hide
171      */
172     private static DisplayManager.DisplayListener sDisplayListener;
173 
PointerIcon(int type)174     private PointerIcon(int type) {
175         mType = type;
176     }
177 
178     /**
179      * Gets a special pointer icon that has no bitmap.
180      *
181      * @return The null pointer icon.
182      *
183      * @see #TYPE_NULL
184      * @hide
185      */
getNullIcon()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.
197      * @hide
198      */
getDefaultIcon(@onNull Context context)199     public static PointerIcon getDefaultIcon(@NonNull Context context) {
200         return getSystemIcon(context, TYPE_DEFAULT);
201     }
202 
203     /**
204      * Gets a system pointer icon for the given type.
205      * If typeis not recognized, returns the default pointer icon.
206      *
207      * @param context The context.
208      * @param type The pointer icon type.
209      * @return The pointer icon.
210      *
211      * @throws IllegalArgumentException if context is null.
212      */
getSystemIcon(@onNull Context context, int type)213     public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
214         if (context == null) {
215             throw new IllegalArgumentException("context must not be null");
216         }
217 
218         if (type == TYPE_NULL) {
219             return gNullIcon;
220         }
221 
222         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.
235         if (icon != null) {
236             return icon;
237         }
238 
239         int typeIndex = getSystemIconTypeIndex(type);
240         if (typeIndex == 0) {
241             typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
242         }
243 
244         int defStyle = sUseLargeIcons ?
245                 com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
246         TypedArray a = context.obtainStyledAttributes(null,
247                 com.android.internal.R.styleable.Pointer,
248                 0, defStyle);
249         int resourceId = a.getResourceId(typeIndex, -1);
250         a.recycle();
251 
252         if (resourceId == -1) {
253             Log.w(TAG, "Missing theme resources for pointer icon type " + type);
254             return type == TYPE_DEFAULT ? gNullIcon : getSystemIcon(context, TYPE_DEFAULT);
255         }
256 
257         icon = new PointerIcon(type);
258         if ((resourceId & 0xff000000) == 0x01000000) {
259             icon.mSystemIconResourceId = resourceId;
260         } else {
261             icon.loadResource(context, context.getResources(), resourceId);
262         }
263         systemIcons.append(type, icon);
264         return icon;
265     }
266 
267     /**
268      * Updates wheter accessibility large icons are used or not.
269      * @hide
270      */
setUseLargeIcons(boolean use)271     public static void setUseLargeIcons(boolean use) {
272         sUseLargeIcons = use;
273         gSystemIconsByDisplay.clear();
274     }
275 
276     /**
277      * Creates a custom pointer icon from the given bitmap and hotspot information.
278      *
279      * @param bitmap The bitmap for the icon.
280      * @param hotSpotX The X offset of the pointer icon hotspot in the bitmap.
281      *        Must be within the [0, bitmap.getWidth()) range.
282      * @param hotSpotY The Y offset of the pointer icon hotspot in the bitmap.
283      *        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      */
create(@onNull Bitmap bitmap, float hotSpotX, float hotSpotY)289     public static PointerIcon create(@NonNull Bitmap bitmap, float hotSpotX, float hotSpotY) {
290         if (bitmap == null) {
291             throw new IllegalArgumentException("bitmap must not be null");
292         }
293         validateHotSpot(bitmap, hotSpotX, hotSpotY);
294 
295         PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
296         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      */
load(@onNull Resources resources, @XmlRes int resourceId)323     public static PointerIcon load(@NonNull Resources resources, @XmlRes int resourceId) {
324         if (resources == null) {
325             throw new IllegalArgumentException("resources must not be null");
326         }
327 
328         PointerIcon icon = new PointerIcon(TYPE_CUSTOM);
329         icon.loadResource(null, resources, resourceId);
330         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.
341      * @hide
342      */
343     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
load(@onNull Context context)344     public PointerIcon load(@NonNull Context context) {
345         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 
353         PointerIcon result = new PointerIcon(mType);
354         result.mSystemIconResourceId = mSystemIconResourceId;
355         result.loadResource(context, context.getResources(), mSystemIconResourceId);
356         return result;
357     }
358 
359     /** @hide */
getType()360     public int getType() {
361         return mType;
362     }
363 
364     public static final @android.annotation.NonNull Parcelable.Creator<PointerIcon> CREATOR
365             = new Parcelable.Creator<PointerIcon>() {
366         public PointerIcon createFromParcel(Parcel in) {
367             int type = in.readInt();
368             if (type == TYPE_NULL) {
369                 return getNullIcon();
370             }
371 
372             int systemIconResourceId = in.readInt();
373             if (systemIconResourceId != 0) {
374                 PointerIcon icon = new PointerIcon(type);
375                 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();
382             return PointerIcon.create(bitmap, hotSpotX, hotSpotY);
383         }
384 
385         public PointerIcon[] newArray(int size) {
386             return new PointerIcon[size];
387         }
388     };
389 
describeContents()390     public int describeContents() {
391         return 0;
392     }
393 
writeToParcel(Parcel out, int flags)394     public void writeToParcel(Parcel out, int flags) {
395         out.writeInt(mType);
396 
397         if (mType != TYPE_NULL) {
398             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
equals(Object other)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;
418         if (mType != otherIcon.mType
419                 || 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 
432     /**
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      */
getBitmapFromDrawable(BitmapDrawable bitmapDrawable)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 
loadResource(Context context, Resources resources, @XmlRes int resourceId)459     private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
460         final XmlResourceParser parser = resources.getXml(resourceId);
461         final int bitmapRes;
462         final float hotSpotX;
463         final float hotSpotY;
464         try {
465             XmlUtils.beginDocument(parser, "pointer-icon");
466 
467             final TypedArray a = resources.obtainAttributes(
468                     parser, com.android.internal.R.styleable.PointerIcon);
469             bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
470             hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
471             hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
472             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 
483         Drawable drawable;
484         if (context == null) {
485             drawable = resources.getDrawable(bitmapRes);
486         } else {
487             drawable = context.getDrawable(bitmapRes);
488         }
489         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                     }
515                     BitmapDrawable bitmapDrawableFrame = (BitmapDrawable) drawableFrame;
516                     mBitmapFrames[i - 1] = getBitmapFromDrawable(bitmapDrawableFrame);
517                 }
518             }
519         }
520         if (!(drawable instanceof BitmapDrawable)) {
521             throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
522                     + "refer to a bitmap drawable.");
523         }
524 
525         BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
526         final Bitmap bitmap = getBitmapFromDrawable(bitmapDrawable);
527         validateHotSpot(bitmap, hotSpotX, hotSpotY);
528         // Set the properties now that we have successfully loaded the icon.
529         mBitmap = bitmap;
530         mHotSpotX = hotSpotX;
531         mHotSpotY = hotSpotY;
532     }
533 
validateHotSpot(Bitmap bitmap, float hotSpotX, float hotSpotY)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 
getSystemIconTypeIndex(int type)543     private static int getSystemIconTypeIndex(int type) {
544         switch (type) {
545             case TYPE_ARROW:
546                 return com.android.internal.R.styleable.Pointer_pointerIconArrow;
547             case TYPE_SPOT_HOVER:
548                 return com.android.internal.R.styleable.Pointer_pointerIconSpotHover;
549             case TYPE_SPOT_TOUCH:
550                 return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch;
551             case TYPE_SPOT_ANCHOR:
552                 return com.android.internal.R.styleable.Pointer_pointerIconSpotAnchor;
553             case TYPE_HAND:
554                 return com.android.internal.R.styleable.Pointer_pointerIconHand;
555             case TYPE_CONTEXT_MENU:
556                 return com.android.internal.R.styleable.Pointer_pointerIconContextMenu;
557             case TYPE_HELP:
558                 return com.android.internal.R.styleable.Pointer_pointerIconHelp;
559             case TYPE_WAIT:
560                 return com.android.internal.R.styleable.Pointer_pointerIconWait;
561             case TYPE_CELL:
562                 return com.android.internal.R.styleable.Pointer_pointerIconCell;
563             case TYPE_CROSSHAIR:
564                 return com.android.internal.R.styleable.Pointer_pointerIconCrosshair;
565             case TYPE_TEXT:
566                 return com.android.internal.R.styleable.Pointer_pointerIconText;
567             case TYPE_VERTICAL_TEXT:
568                 return com.android.internal.R.styleable.Pointer_pointerIconVerticalText;
569             case TYPE_ALIAS:
570                 return com.android.internal.R.styleable.Pointer_pointerIconAlias;
571             case TYPE_COPY:
572                 return com.android.internal.R.styleable.Pointer_pointerIconCopy;
573             case TYPE_ALL_SCROLL:
574                 return com.android.internal.R.styleable.Pointer_pointerIconAllScroll;
575             case TYPE_NO_DROP:
576                 return com.android.internal.R.styleable.Pointer_pointerIconNodrop;
577             case TYPE_HORIZONTAL_DOUBLE_ARROW:
578                 return com.android.internal.R.styleable.Pointer_pointerIconHorizontalDoubleArrow;
579             case TYPE_VERTICAL_DOUBLE_ARROW:
580                 return com.android.internal.R.styleable.Pointer_pointerIconVerticalDoubleArrow;
581             case TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW:
582                 return com.android.internal.R.styleable.
583                         Pointer_pointerIconTopRightDiagonalDoubleArrow;
584             case TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW:
585                 return com.android.internal.R.styleable.
586                         Pointer_pointerIconTopLeftDiagonalDoubleArrow;
587             case TYPE_ZOOM_IN:
588                 return com.android.internal.R.styleable.Pointer_pointerIconZoomIn;
589             case TYPE_ZOOM_OUT:
590                 return com.android.internal.R.styleable.Pointer_pointerIconZoomOut;
591             case TYPE_GRAB:
592                 return com.android.internal.R.styleable.Pointer_pointerIconGrab;
593             case TYPE_GRABBING:
594                 return com.android.internal.R.styleable.Pointer_pointerIconGrabbing;
595             default:
596                 return 0;
597         }
598     }
599 
600     /**
601      * Manage system icon cache handled by display lifecycle.
602      * @param context The context.
603      */
registerDisplayListener(@onNull Context context)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 
625 }
626