1 /*
2  * Copyright (C) 2020 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.wm.shell;
18 
19 import static com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_TASK_ORG;
20 
21 import android.annotation.UiContext;
22 import android.app.ResourcesManager;
23 import android.content.Context;
24 import android.content.ContextWrapper;
25 import android.content.res.Configuration;
26 import android.hardware.display.DisplayManager;
27 import android.os.Binder;
28 import android.os.IBinder;
29 import android.util.SparseArray;
30 import android.view.Display;
31 import android.view.SurfaceControl;
32 import android.window.DisplayAreaAppearedInfo;
33 import android.window.DisplayAreaInfo;
34 import android.window.DisplayAreaOrganizer;
35 
36 import androidx.annotation.NonNull;
37 import androidx.annotation.Nullable;
38 
39 import com.android.internal.protolog.common.ProtoLog;
40 
41 import java.io.PrintWriter;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.concurrent.Executor;
45 
46 /** Display area organizer for the root/default TaskDisplayAreas */
47 public class RootTaskDisplayAreaOrganizer extends DisplayAreaOrganizer {
48 
49     private static final String TAG = RootTaskDisplayAreaOrganizer.class.getSimpleName();
50 
51     /** {@link DisplayAreaInfo} list, which is mapped by display IDs. */
52     private final SparseArray<DisplayAreaInfo> mDisplayAreasInfo = new SparseArray<>();
53     /** Display area leashes, which is mapped by display IDs. */
54     private final SparseArray<SurfaceControl> mLeashes = new SparseArray<>();
55 
56     private final SparseArray<ArrayList<RootTaskDisplayAreaListener>> mListeners =
57             new SparseArray<>();
58     /** {@link DisplayAreaContext} list, which is mapped by display IDs. */
59     private final SparseArray<DisplayAreaContext> mDisplayAreaContexts = new SparseArray<>();
60 
61     private final Context mContext;
62 
RootTaskDisplayAreaOrganizer(Executor executor, Context context)63     public RootTaskDisplayAreaOrganizer(Executor executor, Context context) {
64         super(executor);
65         mContext = context;
66         List<DisplayAreaAppearedInfo> infos = registerOrganizer(FEATURE_DEFAULT_TASK_CONTAINER);
67         for (int i = infos.size() - 1; i >= 0; --i) {
68             onDisplayAreaAppeared(infos.get(i).getDisplayAreaInfo(), infos.get(i).getLeash());
69         }
70     }
71 
registerListener(int displayId, RootTaskDisplayAreaListener listener)72     public void registerListener(int displayId, RootTaskDisplayAreaListener listener) {
73         ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId);
74         if (listeners == null) {
75             listeners = new ArrayList<>();
76             mListeners.put(displayId, listeners);
77         }
78 
79         listeners.add(listener);
80 
81         final DisplayAreaInfo info = mDisplayAreasInfo.get(displayId);
82         if (info != null) {
83             listener.onDisplayAreaAppeared(info);
84         }
85     }
86 
unregisterListener(RootTaskDisplayAreaListener listener)87     public void unregisterListener(RootTaskDisplayAreaListener listener) {
88         for (int i = mListeners.size() - 1; i >= 0; --i) {
89             final List<RootTaskDisplayAreaListener> listeners = mListeners.valueAt(i);
90             if (listeners == null) continue;
91             listeners.remove(listener);
92         }
93     }
94 
attachToDisplayArea(int displayId, SurfaceControl.Builder b)95     public void attachToDisplayArea(int displayId, SurfaceControl.Builder b) {
96         final SurfaceControl sc = mLeashes.get(displayId);
97         b.setParent(sc);
98     }
99 
setPosition(@onNull SurfaceControl.Transaction tx, int displayId, int x, int y)100     public void setPosition(@NonNull SurfaceControl.Transaction tx, int displayId, int x, int y) {
101         final SurfaceControl sc = mLeashes.get(displayId);
102         if (sc == null) {
103             throw new IllegalArgumentException("can't find display" + displayId);
104         }
105         tx.setPosition(sc, x, y);
106     }
107 
108     @Override
onDisplayAreaAppeared(@onNull DisplayAreaInfo displayAreaInfo, @NonNull SurfaceControl leash)109     public void onDisplayAreaAppeared(@NonNull DisplayAreaInfo displayAreaInfo,
110             @NonNull SurfaceControl leash) {
111         if (displayAreaInfo.featureId != FEATURE_DEFAULT_TASK_CONTAINER) {
112             throw new IllegalArgumentException(
113                     "Unknown feature: " + displayAreaInfo.featureId
114                             + "displayAreaInfo:" + displayAreaInfo);
115         }
116 
117         final int displayId = displayAreaInfo.displayId;
118         if (mDisplayAreasInfo.get(displayId) != null) {
119             throw new IllegalArgumentException(
120                     "Duplicate DA for displayId: " + displayId
121                             + " displayAreaInfo:" + displayAreaInfo
122                             + " mDisplayAreasInfo.get():" + mDisplayAreasInfo.get(displayId));
123         }
124 
125         mDisplayAreasInfo.put(displayId, displayAreaInfo);
126         mLeashes.put(displayId, leash);
127 
128         ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId);
129         if (listeners != null) {
130             for (int i = listeners.size() - 1; i >= 0; --i) {
131                 listeners.get(i).onDisplayAreaAppeared(displayAreaInfo);
132             }
133         }
134         applyConfigChangesToContext(displayAreaInfo);
135     }
136 
137     @Override
onDisplayAreaVanished(@onNull DisplayAreaInfo displayAreaInfo)138     public void onDisplayAreaVanished(@NonNull DisplayAreaInfo displayAreaInfo) {
139         final int displayId = displayAreaInfo.displayId;
140         if (mDisplayAreasInfo.get(displayId) == null) {
141             throw new IllegalArgumentException(
142                     "onDisplayAreaVanished() Unknown DA displayId: " + displayId
143                             + " displayAreaInfo:" + displayAreaInfo
144                             + " mDisplayAreasInfo.get():" + mDisplayAreasInfo.get(displayId));
145         }
146 
147         mDisplayAreasInfo.remove(displayId);
148 
149         ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId);
150         if (listeners != null) {
151             for (int i = listeners.size() - 1; i >= 0; --i) {
152                 listeners.get(i).onDisplayAreaVanished(displayAreaInfo);
153             }
154         }
155         mDisplayAreaContexts.remove(displayId);
156     }
157 
158     @Override
onDisplayAreaInfoChanged(@onNull DisplayAreaInfo displayAreaInfo)159     public void onDisplayAreaInfoChanged(@NonNull DisplayAreaInfo displayAreaInfo) {
160         final int displayId = displayAreaInfo.displayId;
161         if (mDisplayAreasInfo.get(displayId) == null) {
162             throw new IllegalArgumentException(
163                     "onDisplayAreaInfoChanged() Unknown DA displayId: " + displayId
164                             + " displayAreaInfo:" + displayAreaInfo
165                             + " mDisplayAreasInfo.get():" + mDisplayAreasInfo.get(displayId));
166         }
167 
168         mDisplayAreasInfo.put(displayId, displayAreaInfo);
169 
170         ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId);
171         if (listeners != null) {
172             for (int i = listeners.size() - 1; i >= 0; --i) {
173                 listeners.get(i).onDisplayAreaInfoChanged(displayAreaInfo);
174             }
175         }
176         applyConfigChangesToContext(displayAreaInfo);
177     }
178 
179     /**
180      * Applies the {@link DisplayAreaInfo} to the {@link DisplayAreaContext} specified by
181      * {@link DisplayAreaInfo#displayId}.
182      */
applyConfigChangesToContext(@onNull DisplayAreaInfo displayAreaInfo)183     private void applyConfigChangesToContext(@NonNull DisplayAreaInfo displayAreaInfo) {
184         final int displayId = displayAreaInfo.displayId;
185         final Display display = mContext.getSystemService(DisplayManager.class)
186                 .getDisplay(displayId);
187         if (display == null) {
188             ProtoLog.w(WM_SHELL_TASK_ORG, "The display#%d has been removed."
189                     + " Skip following steps", displayId);
190             return;
191         }
192         DisplayAreaContext daContext = mDisplayAreaContexts.get(displayId);
193         if (daContext == null) {
194             daContext = new DisplayAreaContext(mContext, display);
195             mDisplayAreaContexts.put(displayId, daContext);
196         }
197         daContext.updateConfigurationChanges(displayAreaInfo.configuration);
198     }
199 
200     /**
201      * Returns the UI context associated with RootTaskDisplayArea specified by {@code displayId}.
202      */
203     @Nullable
204     @UiContext
getContext(int displayId)205     public Context getContext(int displayId) {
206         return mDisplayAreaContexts.get(displayId);
207     }
208 
dump(@onNull PrintWriter pw, String prefix)209     public void dump(@NonNull PrintWriter pw, String prefix) {
210         final String innerPrefix = prefix + "  ";
211         final String childPrefix = innerPrefix + "  ";
212         pw.println(prefix + this);
213     }
214 
215     @Override
toString()216     public String toString() {
217         return TAG + "#" + mDisplayAreasInfo.size();
218     }
219 
220     /** Callbacks for when root task display areas change. */
221     public interface RootTaskDisplayAreaListener {
onDisplayAreaAppeared(DisplayAreaInfo displayAreaInfo)222         default void onDisplayAreaAppeared(DisplayAreaInfo displayAreaInfo) {
223         }
224 
onDisplayAreaVanished(DisplayAreaInfo displayAreaInfo)225         default void onDisplayAreaVanished(DisplayAreaInfo displayAreaInfo) {
226         }
227 
onDisplayAreaInfoChanged(DisplayAreaInfo displayAreaInfo)228         default void onDisplayAreaInfoChanged(DisplayAreaInfo displayAreaInfo) {
229         }
230 
dump(@onNull PrintWriter pw, String prefix)231         default void dump(@NonNull PrintWriter pw, String prefix) {
232         }
233     }
234 
235     /**
236      * A UI context to associate with a {@link com.android.server.wm.DisplayArea}.
237      *
238      * This context receives configuration changes through {@link DisplayAreaOrganizer} callbacks
239      * and the core implementation is {@link Context#createTokenContext(IBinder, Display)} to apply
240      * the configuration updates to the {@link android.content.res.Resources}.
241      */
242     @UiContext
243     public static class DisplayAreaContext extends ContextWrapper {
244         private final IBinder mToken = new Binder();
245         private final ResourcesManager mResourcesManager = ResourcesManager.getInstance();
246 
DisplayAreaContext(@onNull Context context, @NonNull Display display)247         public DisplayAreaContext(@NonNull Context context, @NonNull Display display) {
248             super(null);
249             attachBaseContext(context.createTokenContext(mToken, display));
250         }
251 
updateConfigurationChanges(@onNull Configuration newConfig)252         private void updateConfigurationChanges(@NonNull Configuration newConfig) {
253             final Configuration config = getResources().getConfiguration();
254             final boolean configChanged = config.diff(newConfig) != 0;
255             if (configChanged) {
256                 mResourcesManager.updateResourcesForActivity(mToken, newConfig, getDisplayId());
257             }
258         }
259     }
260 }