1 /*
2  * Copyright (C) 2017 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 com.android.server.wm;
18 
19 import android.os.RemoteCallbackList;
20 import android.os.RemoteException;
21 import android.util.SparseArray;
22 import android.view.IWallpaperVisibilityListener;
23 
24 /**
25  * Manages and trigger wallpaper visibility listeners.
26  */
27 class WallpaperVisibilityListeners {
28 
29     /**
30      * A map of displayIds and its listeners.
31      */
32     private final SparseArray<RemoteCallbackList<IWallpaperVisibilityListener>> mDisplayListeners =
33             new SparseArray<>();
34 
registerWallpaperVisibilityListener(IWallpaperVisibilityListener listener, int displayId)35     void registerWallpaperVisibilityListener(IWallpaperVisibilityListener listener,
36             int displayId) {
37         RemoteCallbackList<IWallpaperVisibilityListener> listeners =
38                 mDisplayListeners.get(displayId);
39         if (listeners == null) {
40             listeners = new RemoteCallbackList<>();
41             mDisplayListeners.append(displayId, listeners);
42         }
43         listeners.register(listener);
44     }
45 
unregisterWallpaperVisibilityListener(IWallpaperVisibilityListener listener, int displayId)46     void unregisterWallpaperVisibilityListener(IWallpaperVisibilityListener listener,
47             int displayId) {
48         RemoteCallbackList<IWallpaperVisibilityListener> listeners =
49                 mDisplayListeners.get(displayId);
50         if (listeners == null) {
51             return;
52         }
53         listeners.unregister(listener);
54     }
55 
notifyWallpaperVisibilityChanged(DisplayContent displayContent)56     void notifyWallpaperVisibilityChanged(DisplayContent displayContent) {
57         final int displayId = displayContent.getDisplayId();
58         final boolean visible = displayContent.mWallpaperController.isWallpaperVisible();
59         RemoteCallbackList<IWallpaperVisibilityListener> displayListeners =
60                 mDisplayListeners.get(displayId);
61 
62         // No listeners for this display.
63         if (displayListeners == null) {
64             return;
65         }
66 
67         int i = displayListeners.beginBroadcast();
68         while (i > 0) {
69             i--;
70             IWallpaperVisibilityListener listener = displayListeners.getBroadcastItem(i);
71             try {
72                 listener.onWallpaperVisibilityChanged(visible, displayId);
73             } catch (RemoteException e) {
74                 // Nothing to do in here, RemoteCallbackListener will clean it up.
75             }
76         }
77         displayListeners.finishBroadcast();
78     }
79 }
80