| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.common; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.res.Configuration; |
| 22 |
import android.graphics.Rect; |
| 23 |
import android.hardware.display.DisplayManager; |
| 24 |
import android.os.RemoteException; |
| 25 |
import android.util.ArraySet; |
| 26 |
import android.util.Slog; |
| 27 |
import android.util.SparseArray; |
| 28 |
import android.view.Display; |
| 29 |
import android.view.IDisplayWindowListener; |
| 30 |
import android.view.IWindowManager; |
| 31 |
import android.view.InsetsState; |
| 32 |
|
| 33 |
import androidx.annotation.BinderThread; |
| 34 |
|
| 35 |
import com.android.wm.shell.common.DisplayChangeController.OnDisplayChangingListener; |
| 36 |
import com.android.wm.shell.common.annotations.ShellMainThread; |
| 37 |
import com.android.wm.shell.sysui.ShellInit; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.List; |
| 41 |
import java.util.Set; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class DisplayController { |
| 50 |
private static final String TAG = "DisplayController"; |
| 51 |
|
| 52 |
private final ShellExecutor mMainExecutor; |
| 53 |
private final Context mContext; |
| 54 |
private final IWindowManager mWmService; |
| 55 |
private final DisplayChangeController mChangeController; |
| 56 |
private final IDisplayWindowListener mDisplayContainerListener; |
| 57 |
|
| 58 |
private final SparseArray<DisplayRecord> mDisplays = new SparseArray<>(); |
| 59 |
private final ArrayList<OnDisplaysChangedListener> mDisplayChangedListeners = new ArrayList<>(); |
| 60 |
|
| 61 |
public DisplayController(Context context, IWindowManager wmService, ShellInit shellInit, |
| 62 |
ShellExecutor mainExecutor) { |
| 63 |
mMainExecutor = mainExecutor; |
| 64 |
mContext = context; |
| 65 |
mWmService = wmService; |
| 66 |
|
| 67 |
mChangeController = new DisplayChangeController(mWmService, shellInit, mainExecutor); |
| 68 |
mDisplayContainerListener = new DisplayWindowListenerImpl(); |
| 69 |
|
| 70 |
|
| 71 |
shellInit.addInitCallback(this::onInit, this); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public void onInit() { |
| 78 |
try { |
| 79 |
int[] displayIds = mWmService.registerDisplayWindowListener(mDisplayContainerListener); |
| 80 |
for (int i = 0; i < displayIds.length; i++) { |
| 81 |
onDisplayAdded(displayIds[i]); |
| 82 |
} |
| 83 |
} catch (RemoteException e) { |
| 84 |
throw new RuntimeException("Unable to register display controller"); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
public DisplayChangeController getChangeController() { |
| 90 |
return mChangeController; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public Display getDisplay(int displayId) { |
| 97 |
final DisplayManager displayManager = mContext.getSystemService(DisplayManager.class); |
| 98 |
return displayManager.getDisplay(displayId); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public @Nullable DisplayLayout getDisplayLayout(int displayId) { |
| 105 |
final DisplayRecord r = mDisplays.get(displayId); |
| 106 |
return r != null ? r.mDisplayLayout : null; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public @Nullable Context getDisplayContext(int displayId) { |
| 113 |
final DisplayRecord r = mDisplays.get(displayId); |
| 114 |
return r != null ? r.mContext : null; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public InsetsState getInsetsState(int displayId) { |
| 121 |
final DisplayRecord r = mDisplays.get(displayId); |
| 122 |
return r != null ? r.mInsetsState : null; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public void updateDisplayInsets(int displayId, InsetsState state) { |
| 129 |
final DisplayRecord r = mDisplays.get(displayId); |
| 130 |
if (r != null) { |
| 131 |
r.setInsets(state); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public void addDisplayWindowListener(OnDisplaysChangedListener listener) { |
| 140 |
synchronized (mDisplays) { |
| 141 |
if (mDisplayChangedListeners.contains(listener)) { |
| 142 |
return; |
| 143 |
} |
| 144 |
mDisplayChangedListeners.add(listener); |
| 145 |
for (int i = 0; i < mDisplays.size(); ++i) { |
| 146 |
listener.onDisplayAdded(mDisplays.keyAt(i)); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public void removeDisplayWindowListener(OnDisplaysChangedListener listener) { |
| 155 |
synchronized (mDisplays) { |
| 156 |
mDisplayChangedListeners.remove(listener); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public void addDisplayChangingController(OnDisplayChangingListener controller) { |
| 164 |
mChangeController.addDisplayChangeListener(controller); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public void removeDisplayChangingController(OnDisplayChangingListener controller) { |
| 171 |
mChangeController.removeDisplayChangeListener(controller); |
| 172 |
} |
| 173 |
|
| 174 |
private void onDisplayAdded(int displayId) { |
| 175 |
synchronized (mDisplays) { |
| 176 |
if (mDisplays.get(displayId) != null) { |
| 177 |
return; |
| 178 |
} |
| 179 |
final Display display = getDisplay(displayId); |
| 180 |
if (display == null) { |
| 181 |
|
| 182 |
|
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
final Context context = (displayId == Display.DEFAULT_DISPLAY) |
| 187 |
? mContext |
| 188 |
: mContext.createDisplayContext(display); |
| 189 |
final DisplayRecord record = new DisplayRecord(displayId); |
| 190 |
record.setDisplayLayout(context, new DisplayLayout(context, display)); |
| 191 |
mDisplays.put(displayId, record); |
| 192 |
for (int i = 0; i < mDisplayChangedListeners.size(); ++i) { |
| 193 |
mDisplayChangedListeners.get(i).onDisplayAdded(displayId); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
private void onDisplayConfigurationChanged(int displayId, Configuration newConfig) { |
| 199 |
synchronized (mDisplays) { |
| 200 |
final DisplayRecord dr = mDisplays.get(displayId); |
| 201 |
if (dr == null) { |
| 202 |
Slog.w(TAG, "Skipping Display Configuration change on non-added" |
| 203 |
+ " display."); |
| 204 |
return; |
| 205 |
} |
| 206 |
final Display display = getDisplay(displayId); |
| 207 |
if (display == null) { |
| 208 |
Slog.w(TAG, "Skipping Display Configuration change on invalid" |
| 209 |
+ " display. It may have been removed."); |
| 210 |
return; |
| 211 |
} |
| 212 |
final Context perDisplayContext = (displayId == Display.DEFAULT_DISPLAY) |
| 213 |
? mContext |
| 214 |
: mContext.createDisplayContext(display); |
| 215 |
final Context context = perDisplayContext.createConfigurationContext(newConfig); |
| 216 |
dr.setDisplayLayout(context, new DisplayLayout(context, display)); |
| 217 |
for (int i = 0; i < mDisplayChangedListeners.size(); ++i) { |
| 218 |
mDisplayChangedListeners.get(i).onDisplayConfigurationChanged( |
| 219 |
displayId, newConfig); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
private void onDisplayRemoved(int displayId) { |
| 225 |
synchronized (mDisplays) { |
| 226 |
if (mDisplays.get(displayId) == null) { |
| 227 |
return; |
| 228 |
} |
| 229 |
for (int i = mDisplayChangedListeners.size() - 1; i >= 0; --i) { |
| 230 |
mDisplayChangedListeners.get(i).onDisplayRemoved(displayId); |
| 231 |
} |
| 232 |
mDisplays.remove(displayId); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
private void onFixedRotationStarted(int displayId, int newRotation) { |
| 237 |
synchronized (mDisplays) { |
| 238 |
if (mDisplays.get(displayId) == null || getDisplay(displayId) == null) { |
| 239 |
Slog.w(TAG, "Skipping onFixedRotationStarted on unknown" |
| 240 |
+ " display, displayId=" + displayId); |
| 241 |
return; |
| 242 |
} |
| 243 |
for (int i = mDisplayChangedListeners.size() - 1; i >= 0; --i) { |
| 244 |
mDisplayChangedListeners.get(i).onFixedRotationStarted( |
| 245 |
displayId, newRotation); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
private void onFixedRotationFinished(int displayId) { |
| 251 |
synchronized (mDisplays) { |
| 252 |
if (mDisplays.get(displayId) == null || getDisplay(displayId) == null) { |
| 253 |
Slog.w(TAG, "Skipping onFixedRotationFinished on unknown" |
| 254 |
+ " display, displayId=" + displayId); |
| 255 |
return; |
| 256 |
} |
| 257 |
for (int i = mDisplayChangedListeners.size() - 1; i >= 0; --i) { |
| 258 |
mDisplayChangedListeners.get(i).onFixedRotationFinished(displayId); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
private void onKeepClearAreasChanged(int displayId, Set<Rect> restricted, |
| 264 |
Set<Rect> unrestricted) { |
| 265 |
synchronized (mDisplays) { |
| 266 |
if (mDisplays.get(displayId) == null || getDisplay(displayId) == null) { |
| 267 |
Slog.w(TAG, "Skipping onKeepClearAreasChanged on unknown" |
| 268 |
+ " display, displayId=" + displayId); |
| 269 |
return; |
| 270 |
} |
| 271 |
for (int i = mDisplayChangedListeners.size() - 1; i >= 0; --i) { |
| 272 |
mDisplayChangedListeners.get(i) |
| 273 |
.onKeepClearAreasChanged(displayId, restricted, unrestricted); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
private static class DisplayRecord { |
| 279 |
private int mDisplayId; |
| 280 |
private Context mContext; |
| 281 |
private DisplayLayout mDisplayLayout; |
| 282 |
private InsetsState mInsetsState = new InsetsState(); |
| 283 |
|
| 284 |
private DisplayRecord(int displayId) { |
| 285 |
mDisplayId = displayId; |
| 286 |
} |
| 287 |
|
| 288 |
private void setDisplayLayout(Context context, DisplayLayout displayLayout) { |
| 289 |
mContext = context; |
| 290 |
mDisplayLayout = displayLayout; |
| 291 |
mDisplayLayout.setInsets(mContext.getResources(), mInsetsState); |
| 292 |
} |
| 293 |
|
| 294 |
private void setInsets(InsetsState state) { |
| 295 |
mInsetsState = state; |
| 296 |
mDisplayLayout.setInsets(mContext.getResources(), state); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
@BinderThread |
| 301 |
private class DisplayWindowListenerImpl extends IDisplayWindowListener.Stub { |
| 302 |
@Override |
| 303 |
public void onDisplayAdded(int displayId) { |
| 304 |
mMainExecutor.execute(() -> { |
| 305 |
DisplayController.this.onDisplayAdded(displayId); |
| 306 |
}); |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) { |
| 311 |
mMainExecutor.execute(() -> { |
| 312 |
DisplayController.this.onDisplayConfigurationChanged(displayId, newConfig); |
| 313 |
}); |
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
public void onDisplayRemoved(int displayId) { |
| 318 |
mMainExecutor.execute(() -> { |
| 319 |
DisplayController.this.onDisplayRemoved(displayId); |
| 320 |
}); |
| 321 |
} |
| 322 |
|
| 323 |
@Override |
| 324 |
public void onFixedRotationStarted(int displayId, int newRotation) { |
| 325 |
mMainExecutor.execute(() -> { |
| 326 |
DisplayController.this.onFixedRotationStarted(displayId, newRotation); |
| 327 |
}); |
| 328 |
} |
| 329 |
|
| 330 |
@Override |
| 331 |
public void onFixedRotationFinished(int displayId) { |
| 332 |
mMainExecutor.execute(() -> { |
| 333 |
DisplayController.this.onFixedRotationFinished(displayId); |
| 334 |
}); |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
public void onKeepClearAreasChanged(int displayId, List<Rect> restricted, |
| 339 |
List<Rect> unrestricted) { |
| 340 |
mMainExecutor.execute(() -> { |
| 341 |
DisplayController.this.onKeepClearAreasChanged(displayId, |
| 342 |
new ArraySet<>(restricted), new ArraySet<>(unrestricted)); |
| 343 |
}); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
@ShellMainThread |
| 354 |
public interface OnDisplaysChangedListener { |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
default void onDisplayAdded(int displayId) {} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
default void onDisplayConfigurationChanged(int displayId, Configuration newConfig) {} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
default void onDisplayRemoved(int displayId) {} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
default void onFixedRotationStarted(int displayId, int newRotation) {} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
default void onFixedRotationFinished(int displayId) {} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
default void onKeepClearAreasChanged(int displayId, Set<Rect> restricted, |
| 384 |
Set<Rect> unrestricted) {} |
| 385 |
} |
| 386 |
} |