1 /*
2  * Copyright (C) 2019 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.content.res.Configuration;
20 import android.os.RemoteCallbackList;
21 import android.os.RemoteException;
22 import android.view.IDisplayWindowListener;
23 
24 /**
25  * Manages dispatch of relevant hierarchy changes to interested listeners. Listeners are assumed
26  * to be remote.
27  */
28 class DisplayWindowListenerController {
29     RemoteCallbackList<IDisplayWindowListener> mDisplayListeners = new RemoteCallbackList<>();
30 
31 //    private final ArrayList<DisplayContainerListener> mDisplayListeners = new ArrayList<>();
32     private final WindowManagerService mService;
33 
DisplayWindowListenerController(WindowManagerService service)34     DisplayWindowListenerController(WindowManagerService service) {
35         mService = service;
36     }
37 
registerListener(IDisplayWindowListener listener)38     void registerListener(IDisplayWindowListener listener) {
39         synchronized (mService.mGlobalLock) {
40             mDisplayListeners.register(listener);
41             try {
42                 for (int i = 0; i < mService.mAtmService.mRootWindowContainer.getChildCount();
43                         ++i) {
44                     DisplayContent d = mService.mAtmService.mRootWindowContainer.getChildAt(i);
45                     listener.onDisplayAdded(d.mDisplayId);
46                 }
47             } catch (RemoteException e) { }
48         }
49     }
50 
unregisterListener(IDisplayWindowListener listener)51     void unregisterListener(IDisplayWindowListener listener) {
52         mDisplayListeners.unregister(listener);
53     }
54 
dispatchDisplayAdded(DisplayContent display)55     void dispatchDisplayAdded(DisplayContent display) {
56         int count = mDisplayListeners.beginBroadcast();
57         for (int i = 0; i < count; ++i) {
58             try {
59                 mDisplayListeners.getBroadcastItem(i).onDisplayAdded(display.mDisplayId);
60             } catch (RemoteException e) {
61             }
62         }
63         mDisplayListeners.finishBroadcast();
64     }
65 
dispatchDisplayChanged(DisplayContent display, Configuration newConfig)66     void dispatchDisplayChanged(DisplayContent display, Configuration newConfig) {
67         // Only report changed if this has actually been added to the hierarchy already.
68         boolean isInHierarchy = false;
69         for (int i = 0; i < display.getParent().getChildCount(); ++i) {
70             if (display.getParent().getChildAt(i) == display) {
71                 isInHierarchy = true;
72             }
73         }
74         if (!isInHierarchy) {
75             return;
76         }
77         int count = mDisplayListeners.beginBroadcast();
78         for (int i = 0; i < count; ++i) {
79             try {
80                 mDisplayListeners.getBroadcastItem(i).onDisplayConfigurationChanged(
81                         display.getDisplayId(), newConfig);
82             } catch (RemoteException e) {
83             }
84         }
85         mDisplayListeners.finishBroadcast();
86     }
87 
dispatchDisplayRemoved(DisplayContent display)88     void dispatchDisplayRemoved(DisplayContent display) {
89         int count = mDisplayListeners.beginBroadcast();
90         for (int i = 0; i < count; ++i) {
91             try {
92                 mDisplayListeners.getBroadcastItem(i).onDisplayRemoved(display.mDisplayId);
93             } catch (RemoteException e) {
94             }
95         }
96         mDisplayListeners.finishBroadcast();
97     }
98 
dispatchFixedRotationStarted(DisplayContent display, int newRotation)99     void dispatchFixedRotationStarted(DisplayContent display, int newRotation) {
100         int count = mDisplayListeners.beginBroadcast();
101         for (int i = 0; i < count; ++i) {
102             try {
103                 mDisplayListeners.getBroadcastItem(i).onFixedRotationStarted(
104                         display.mDisplayId, newRotation);
105             } catch (RemoteException e) {
106             }
107         }
108         mDisplayListeners.finishBroadcast();
109     }
110 
dispatchFixedRotationFinished(DisplayContent display)111     void dispatchFixedRotationFinished(DisplayContent display) {
112         int count = mDisplayListeners.beginBroadcast();
113         for (int i = 0; i < count; ++i) {
114             try {
115                 mDisplayListeners.getBroadcastItem(i).onFixedRotationFinished(display.mDisplayId);
116             } catch (RemoteException e) {
117             }
118         }
119         mDisplayListeners.finishBroadcast();
120     }
121 }
122