| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 47 |
public class RootTaskDisplayAreaOrganizer extends DisplayAreaOrganizer { |
| 48 |
|
| 49 |
private static final String TAG = RootTaskDisplayAreaOrganizer.class.getSimpleName(); |
| 50 |
|
| 51 |
|
| 52 |
private final SparseArray<DisplayAreaInfo> mDisplayAreasInfo = new SparseArray<>(); |
| 53 |
|
| 54 |
private final SparseArray<SurfaceControl> mLeashes = new SparseArray<>(); |
| 55 |
|
| 56 |
private final SparseArray<ArrayList<RootTaskDisplayAreaListener>> mListeners = |
| 57 |
new SparseArray<>(); |
| 58 |
|
| 59 |
private final SparseArray<DisplayAreaContext> mDisplayAreaContexts = new SparseArray<>(); |
| 60 |
|
| 61 |
private final Context mContext; |
| 62 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 95 |
public void attachToDisplayArea(int displayId, SurfaceControl.Builder b) { |
| 96 |
final SurfaceControl sc = mLeashes.get(displayId); |
| 97 |
b.setParent(sc); |
| 98 |
} |
| 99 |
|
| 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 |
| 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 |
| 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 |
mLeashes.get(displayId).release(); |
| 149 |
mLeashes.remove(displayId); |
| 150 |
|
| 151 |
ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId); |
| 152 |
if (listeners != null) { |
| 153 |
for (int i = listeners.size() - 1; i >= 0; --i) { |
| 154 |
listeners.get(i).onDisplayAreaVanished(displayAreaInfo); |
| 155 |
} |
| 156 |
} |
| 157 |
mDisplayAreaContexts.remove(displayId); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void onDisplayAreaInfoChanged(@NonNull DisplayAreaInfo displayAreaInfo) { |
| 162 |
final int displayId = displayAreaInfo.displayId; |
| 163 |
if (mDisplayAreasInfo.get(displayId) == null) { |
| 164 |
throw new IllegalArgumentException( |
| 165 |
"onDisplayAreaInfoChanged() Unknown DA displayId: " + displayId |
| 166 |
+ " displayAreaInfo:" + displayAreaInfo |
| 167 |
+ " mDisplayAreasInfo.get():" + mDisplayAreasInfo.get(displayId)); |
| 168 |
} |
| 169 |
|
| 170 |
mDisplayAreasInfo.put(displayId, displayAreaInfo); |
| 171 |
|
| 172 |
ArrayList<RootTaskDisplayAreaListener> listeners = mListeners.get(displayId); |
| 173 |
if (listeners != null) { |
| 174 |
for (int i = listeners.size() - 1; i >= 0; --i) { |
| 175 |
listeners.get(i).onDisplayAreaInfoChanged(displayAreaInfo); |
| 176 |
} |
| 177 |
} |
| 178 |
applyConfigChangesToContext(displayAreaInfo); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
@Nullable |
| 185 |
public DisplayAreaInfo getDisplayAreaInfo(int displayId) { |
| 186 |
return mDisplayAreasInfo.get(displayId); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private void applyConfigChangesToContext(@NonNull DisplayAreaInfo displayAreaInfo) { |
| 194 |
final int displayId = displayAreaInfo.displayId; |
| 195 |
final Display display = mContext.getSystemService(DisplayManager.class) |
| 196 |
.getDisplay(displayId); |
| 197 |
if (display == null) { |
| 198 |
ProtoLog.w(WM_SHELL_TASK_ORG, "The display#%d has been removed." |
| 199 |
+ " Skip following steps", displayId); |
| 200 |
return; |
| 201 |
} |
| 202 |
DisplayAreaContext daContext = mDisplayAreaContexts.get(displayId); |
| 203 |
if (daContext == null) { |
| 204 |
daContext = new DisplayAreaContext(mContext, display); |
| 205 |
mDisplayAreaContexts.put(displayId, daContext); |
| 206 |
} |
| 207 |
daContext.updateConfigurationChanges(displayAreaInfo.configuration); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
@Nullable |
| 214 |
@UiContext |
| 215 |
public Context getContext(int displayId) { |
| 216 |
return mDisplayAreaContexts.get(displayId); |
| 217 |
} |
| 218 |
|
| 219 |
public void dump(@NonNull PrintWriter pw, String prefix) { |
| 220 |
final String innerPrefix = prefix + " "; |
| 221 |
final String childPrefix = innerPrefix + " "; |
| 222 |
pw.println(prefix + this); |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public String toString() { |
| 227 |
return TAG + "#" + mDisplayAreasInfo.size(); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
public interface RootTaskDisplayAreaListener { |
| 232 |
default void onDisplayAreaAppeared(DisplayAreaInfo displayAreaInfo) { |
| 233 |
} |
| 234 |
|
| 235 |
default void onDisplayAreaVanished(DisplayAreaInfo displayAreaInfo) { |
| 236 |
} |
| 237 |
|
| 238 |
default void onDisplayAreaInfoChanged(DisplayAreaInfo displayAreaInfo) { |
| 239 |
} |
| 240 |
|
| 241 |
default void dump(@NonNull PrintWriter pw, String prefix) { |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
@UiContext |
| 253 |
public static class DisplayAreaContext extends ContextWrapper { |
| 254 |
private final IBinder mToken = new Binder(); |
| 255 |
private final ResourcesManager mResourcesManager = ResourcesManager.getInstance(); |
| 256 |
|
| 257 |
public DisplayAreaContext(@NonNull Context context, @NonNull Display display) { |
| 258 |
super(null); |
| 259 |
attachBaseContext(context.createTokenContext(mToken, display)); |
| 260 |
} |
| 261 |
|
| 262 |
private void updateConfigurationChanges(@NonNull Configuration newConfig) { |
| 263 |
final Configuration config = getResources().getConfiguration(); |
| 264 |
final boolean configChanged = config.diff(newConfig) != 0; |
| 265 |
if (configChanged) { |
| 266 |
mResourcesManager.updateResourcesForActivity(mToken, newConfig, getDisplayId()); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |