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 leash.setUnreleasedWarningCallSite( 126 "RootTaskDisplayAreaOrganizer.onDisplayAreaAppeared"); 127 mDisplayAreasInfo.put(displayId, displayAreaInfo); 128 mLeashes.put(displayId, leash); 129 130 ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId); 131 if (listeners != null) { 132 for (int i = listeners.size() - 1; i >= 0; --i) { 133 listeners.get(i).onDisplayAreaAppeared(displayAreaInfo); 134 } 135 } 136 applyConfigChangesToContext(displayAreaInfo); 137 } 138 139 @Override onDisplayAreaVanished(@onNull DisplayAreaInfo displayAreaInfo)140 public void onDisplayAreaVanished(@NonNull DisplayAreaInfo displayAreaInfo) { 141 final int displayId = displayAreaInfo.displayId; 142 if (mDisplayAreasInfo.get(displayId) == null) { 143 throw new IllegalArgumentException( 144 "onDisplayAreaVanished() Unknown DA displayId: " + displayId 145 + " displayAreaInfo:" + displayAreaInfo 146 + " mDisplayAreasInfo.get():" + mDisplayAreasInfo.get(displayId)); 147 } 148 149 mDisplayAreasInfo.remove(displayId); 150 mLeashes.get(displayId).release(); 151 mLeashes.remove(displayId); 152 153 ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId); 154 if (listeners != null) { 155 for (int i = listeners.size() - 1; i >= 0; --i) { 156 listeners.get(i).onDisplayAreaVanished(displayAreaInfo); 157 } 158 } 159 mDisplayAreaContexts.remove(displayId); 160 } 161 162 @Override onDisplayAreaInfoChanged(@onNull DisplayAreaInfo displayAreaInfo)163 public void onDisplayAreaInfoChanged(@NonNull DisplayAreaInfo displayAreaInfo) { 164 final int displayId = displayAreaInfo.displayId; 165 if (mDisplayAreasInfo.get(displayId) == null) { 166 throw new IllegalArgumentException( 167 "onDisplayAreaInfoChanged() Unknown DA displayId: " + displayId 168 + " displayAreaInfo:" + displayAreaInfo 169 + " mDisplayAreasInfo.get():" + mDisplayAreasInfo.get(displayId)); 170 } 171 172 mDisplayAreasInfo.put(displayId, displayAreaInfo); 173 174 ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId); 175 if (listeners != null) { 176 for (int i = listeners.size() - 1; i >= 0; --i) { 177 listeners.get(i).onDisplayAreaInfoChanged(displayAreaInfo); 178 } 179 } 180 applyConfigChangesToContext(displayAreaInfo); 181 } 182 183 /** 184 * Returns the list of display ids that are tracked by a {@link DisplayAreaInfo} 185 */ getDisplayIds()186 public int[] getDisplayIds() { 187 int[] displayIds = new int[mDisplayAreasInfo.size()]; 188 for (int i = 0; i < mDisplayAreasInfo.size(); i++) { 189 displayIds[i] = mDisplayAreasInfo.keyAt(i); 190 } 191 return displayIds; 192 } 193 194 /** 195 * Returns the {@link DisplayAreaInfo} of the {@link DisplayAreaInfo#displayId}. 196 */ 197 @Nullable getDisplayAreaInfo(int displayId)198 public DisplayAreaInfo getDisplayAreaInfo(int displayId) { 199 return mDisplayAreasInfo.get(displayId); 200 } 201 202 /** 203 * Applies the {@link DisplayAreaInfo} to the {@link DisplayAreaContext} specified by 204 * {@link DisplayAreaInfo#displayId}. 205 */ applyConfigChangesToContext(@onNull DisplayAreaInfo displayAreaInfo)206 private void applyConfigChangesToContext(@NonNull DisplayAreaInfo displayAreaInfo) { 207 final int displayId = displayAreaInfo.displayId; 208 final Display display = mContext.getSystemService(DisplayManager.class) 209 .getDisplay(displayId); 210 if (display == null) { 211 ProtoLog.w(WM_SHELL_TASK_ORG, "The display#%d has been removed." 212 + " Skip following steps", displayId); 213 return; 214 } 215 DisplayAreaContext daContext = mDisplayAreaContexts.get(displayId); 216 if (daContext == null) { 217 daContext = new DisplayAreaContext(mContext, display); 218 mDisplayAreaContexts.put(displayId, daContext); 219 } 220 daContext.updateConfigurationChanges(displayAreaInfo.configuration); 221 } 222 223 /** 224 * Returns the UI context associated with RootTaskDisplayArea specified by {@code displayId}. 225 */ 226 @Nullable 227 @UiContext getContext(int displayId)228 public Context getContext(int displayId) { 229 return mDisplayAreaContexts.get(displayId); 230 } 231 dump(@onNull PrintWriter pw, String prefix)232 public void dump(@NonNull PrintWriter pw, String prefix) { 233 final String innerPrefix = prefix + " "; 234 final String childPrefix = innerPrefix + " "; 235 pw.println(prefix + this); 236 } 237 238 @Override toString()239 public String toString() { 240 return TAG + "#" + mDisplayAreasInfo.size(); 241 } 242 243 /** Callbacks for when root task display areas change. */ 244 public interface RootTaskDisplayAreaListener { onDisplayAreaAppeared(DisplayAreaInfo displayAreaInfo)245 default void onDisplayAreaAppeared(DisplayAreaInfo displayAreaInfo) { 246 } 247 onDisplayAreaVanished(DisplayAreaInfo displayAreaInfo)248 default void onDisplayAreaVanished(DisplayAreaInfo displayAreaInfo) { 249 } 250 onDisplayAreaInfoChanged(DisplayAreaInfo displayAreaInfo)251 default void onDisplayAreaInfoChanged(DisplayAreaInfo displayAreaInfo) { 252 } 253 dump(@onNull PrintWriter pw, String prefix)254 default void dump(@NonNull PrintWriter pw, String prefix) { 255 } 256 } 257 258 /** 259 * A UI context to associate with a {@link com.android.server.wm.DisplayArea}. 260 * 261 * This context receives configuration changes through {@link DisplayAreaOrganizer} callbacks 262 * and the core implementation is {@link Context#createTokenContext(IBinder, Display)} to apply 263 * the configuration updates to the {@link android.content.res.Resources}. 264 */ 265 @UiContext 266 public static class DisplayAreaContext extends ContextWrapper { 267 private final IBinder mToken = new Binder(); 268 private final ResourcesManager mResourcesManager = ResourcesManager.getInstance(); 269 DisplayAreaContext(@onNull Context context, @NonNull Display display)270 public DisplayAreaContext(@NonNull Context context, @NonNull Display display) { 271 super(null); 272 attachBaseContext(context.createTokenContext(mToken, display)); 273 } 274 updateConfigurationChanges(@onNull Configuration newConfig)275 private void updateConfigurationChanges(@NonNull Configuration newConfig) { 276 final Configuration config = getResources().getConfiguration(); 277 final boolean configChanged = config.diff(newConfig) != 0; 278 if (configChanged) { 279 mResourcesManager.updateResourcesForActivity(mToken, newConfig, getDisplayId()); 280 } 281 } 282 } 283 }