| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.compatui; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.app.TaskInfo; |
| 21 |
import android.app.TaskInfo.CameraCompatControlState; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.res.Configuration; |
| 24 |
import android.hardware.display.DisplayManager; |
| 25 |
import android.util.ArraySet; |
| 26 |
import android.util.Log; |
| 27 |
import android.util.SparseArray; |
| 28 |
import android.view.Display; |
| 29 |
import android.view.InsetsSourceControl; |
| 30 |
import android.view.InsetsState; |
| 31 |
|
| 32 |
import com.android.internal.annotations.VisibleForTesting; |
| 33 |
import com.android.wm.shell.ShellTaskOrganizer; |
| 34 |
import com.android.wm.shell.common.DisplayController; |
| 35 |
import com.android.wm.shell.common.DisplayController.OnDisplaysChangedListener; |
| 36 |
import com.android.wm.shell.common.DisplayImeController; |
| 37 |
import com.android.wm.shell.common.DisplayInsetsController; |
| 38 |
import com.android.wm.shell.common.DisplayInsetsController.OnInsetsChangedListener; |
| 39 |
import com.android.wm.shell.common.DisplayLayout; |
| 40 |
import com.android.wm.shell.common.DockStateReader; |
| 41 |
import com.android.wm.shell.common.ShellExecutor; |
| 42 |
import com.android.wm.shell.common.SyncTransactionQueue; |
| 43 |
import com.android.wm.shell.compatui.CompatUIWindowManager.CompatUIHintsState; |
| 44 |
import com.android.wm.shell.compatui.letterboxedu.LetterboxEduWindowManager; |
| 45 |
import com.android.wm.shell.sysui.KeyguardChangeListener; |
| 46 |
import com.android.wm.shell.sysui.ShellController; |
| 47 |
import com.android.wm.shell.sysui.ShellInit; |
| 48 |
import com.android.wm.shell.transition.Transitions; |
| 49 |
|
| 50 |
import java.lang.ref.WeakReference; |
| 51 |
import java.util.ArrayList; |
| 52 |
import java.util.List; |
| 53 |
import java.util.Set; |
| 54 |
import java.util.function.Consumer; |
| 55 |
import java.util.function.Predicate; |
| 56 |
|
| 57 |
import dagger.Lazy; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public class CompatUIController implements OnDisplaysChangedListener, |
| 64 |
DisplayImeController.ImePositionProcessor, KeyguardChangeListener { |
| 65 |
|
| 66 |
|
| 67 |
public interface CompatUICallback { |
| 68 |
|
| 69 |
void onSizeCompatRestartButtonAppeared(int taskId); |
| 70 |
|
| 71 |
void onSizeCompatRestartButtonClicked(int taskId); |
| 72 |
|
| 73 |
void onCameraControlStateUpdated(int taskId, @CameraCompatControlState int state); |
| 74 |
} |
| 75 |
|
| 76 |
private static final String TAG = "CompatUIController"; |
| 77 |
|
| 78 |
|
| 79 |
private final Set<Integer> mDisplaysWithIme = new ArraySet<>(1); |
| 80 |
|
| 81 |
|
| 82 |
private final SparseArray<PerDisplayOnInsetsChangedListener> mOnInsetsChangedListeners = |
| 83 |
new SparseArray<>(0); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private final SparseArray<CompatUIWindowManager> mActiveCompatLayouts = new SparseArray<>(0); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@Nullable |
| 100 |
private LetterboxEduWindowManager mActiveLetterboxEduLayout; |
| 101 |
|
| 102 |
|
| 103 |
private final SparseArray<WeakReference<Context>> mDisplayContextCache = new SparseArray<>(0); |
| 104 |
|
| 105 |
private final Context mContext; |
| 106 |
private final ShellController mShellController; |
| 107 |
private final DisplayController mDisplayController; |
| 108 |
private final DisplayInsetsController mDisplayInsetsController; |
| 109 |
private final DisplayImeController mImeController; |
| 110 |
private final SyncTransactionQueue mSyncQueue; |
| 111 |
private final ShellExecutor mMainExecutor; |
| 112 |
private final Lazy<Transitions> mTransitionsLazy; |
| 113 |
private final DockStateReader mDockStateReader; |
| 114 |
|
| 115 |
private CompatUICallback mCallback; |
| 116 |
|
| 117 |
|
| 118 |
private final CompatUIHintsState mCompatUIHintsState; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private boolean mKeyguardShowing; |
| 123 |
|
| 124 |
public CompatUIController(Context context, |
| 125 |
ShellInit shellInit, |
| 126 |
ShellController shellController, |
| 127 |
DisplayController displayController, |
| 128 |
DisplayInsetsController displayInsetsController, |
| 129 |
DisplayImeController imeController, |
| 130 |
SyncTransactionQueue syncQueue, |
| 131 |
ShellExecutor mainExecutor, |
| 132 |
Lazy<Transitions> transitionsLazy, |
| 133 |
DockStateReader dockStateReader) { |
| 134 |
mContext = context; |
| 135 |
mShellController = shellController; |
| 136 |
mDisplayController = displayController; |
| 137 |
mDisplayInsetsController = displayInsetsController; |
| 138 |
mImeController = imeController; |
| 139 |
mSyncQueue = syncQueue; |
| 140 |
mMainExecutor = mainExecutor; |
| 141 |
mTransitionsLazy = transitionsLazy; |
| 142 |
mCompatUIHintsState = new CompatUIHintsState(); |
| 143 |
shellInit.addInitCallback(this::onInit, this); |
| 144 |
mDockStateReader = dockStateReader; |
| 145 |
} |
| 146 |
|
| 147 |
private void onInit() { |
| 148 |
mShellController.addKeyguardChangeListener(this); |
| 149 |
mDisplayController.addDisplayWindowListener(this); |
| 150 |
mImeController.addPositionProcessor(this); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
public void setCompatUICallback(CompatUICallback callback) { |
| 155 |
mCallback = callback; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public void onCompatInfoChanged(TaskInfo taskInfo, |
| 166 |
@Nullable ShellTaskOrganizer.TaskListener taskListener) { |
| 167 |
if (taskInfo.configuration == null || taskListener == null) { |
| 168 |
|
| 169 |
removeLayouts(taskInfo.taskId); |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
createOrUpdateCompatLayout(taskInfo, taskListener); |
| 174 |
createOrUpdateLetterboxEduLayout(taskInfo, taskListener); |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public void onDisplayAdded(int displayId) { |
| 179 |
addOnInsetsChangedListener(displayId); |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public void onDisplayRemoved(int displayId) { |
| 184 |
mDisplayContextCache.remove(displayId); |
| 185 |
removeOnInsetsChangedListener(displayId); |
| 186 |
|
| 187 |
|
| 188 |
final List<Integer> toRemoveTaskIds = new ArrayList<>(); |
| 189 |
forAllLayoutsOnDisplay(displayId, layout -> toRemoveTaskIds.add(layout.getTaskId())); |
| 190 |
for (int i = toRemoveTaskIds.size() - 1; i >= 0; i--) { |
| 191 |
removeLayouts(toRemoveTaskIds.get(i)); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private void addOnInsetsChangedListener(int displayId) { |
| 196 |
PerDisplayOnInsetsChangedListener listener = new PerDisplayOnInsetsChangedListener( |
| 197 |
displayId); |
| 198 |
listener.register(); |
| 199 |
mOnInsetsChangedListeners.put(displayId, listener); |
| 200 |
} |
| 201 |
|
| 202 |
private void removeOnInsetsChangedListener(int displayId) { |
| 203 |
PerDisplayOnInsetsChangedListener listener = mOnInsetsChangedListeners.get(displayId); |
| 204 |
if (listener == null) { |
| 205 |
return; |
| 206 |
} |
| 207 |
listener.unregister(); |
| 208 |
mOnInsetsChangedListeners.remove(displayId); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
@Override |
| 213 |
public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) { |
| 214 |
updateDisplayLayout(displayId); |
| 215 |
} |
| 216 |
|
| 217 |
private void updateDisplayLayout(int displayId) { |
| 218 |
final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(displayId); |
| 219 |
forAllLayoutsOnDisplay(displayId, layout -> layout.updateDisplayLayout(displayLayout)); |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public void onImeVisibilityChanged(int displayId, boolean isShowing) { |
| 224 |
if (isShowing) { |
| 225 |
mDisplaysWithIme.add(displayId); |
| 226 |
} else { |
| 227 |
mDisplaysWithIme.remove(displayId); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
forAllLayoutsOnDisplay(displayId, |
| 232 |
layout -> layout.updateVisibility(showOnDisplay(displayId))); |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public void onKeyguardVisibilityChanged(boolean visible, boolean occluded, |
| 237 |
boolean animatingDismiss) { |
| 238 |
mKeyguardShowing = visible; |
| 239 |
|
| 240 |
forAllLayouts(layout -> layout.updateVisibility(showOnDisplay(layout.getDisplayId()))); |
| 241 |
} |
| 242 |
|
| 243 |
private boolean showOnDisplay(int displayId) { |
| 244 |
return !mKeyguardShowing && !isImeShowingOnDisplay(displayId); |
| 245 |
} |
| 246 |
|
| 247 |
private boolean isImeShowingOnDisplay(int displayId) { |
| 248 |
return mDisplaysWithIme.contains(displayId); |
| 249 |
} |
| 250 |
|
| 251 |
private void createOrUpdateCompatLayout(TaskInfo taskInfo, |
| 252 |
ShellTaskOrganizer.TaskListener taskListener) { |
| 253 |
CompatUIWindowManager layout = mActiveCompatLayouts.get(taskInfo.taskId); |
| 254 |
if (layout != null) { |
| 255 |
|
| 256 |
if (!layout.updateCompatInfo(taskInfo, taskListener, |
| 257 |
showOnDisplay(layout.getDisplayId()))) { |
| 258 |
|
| 259 |
mActiveCompatLayouts.remove(taskInfo.taskId); |
| 260 |
} |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
final Context context = getOrCreateDisplayContext(taskInfo.displayId); |
| 266 |
if (context == null) { |
| 267 |
return; |
| 268 |
} |
| 269 |
layout = createCompatUiWindowManager(context, taskInfo, taskListener); |
| 270 |
if (layout.createLayout(showOnDisplay(taskInfo.displayId))) { |
| 271 |
|
| 272 |
mActiveCompatLayouts.put(taskInfo.taskId, layout); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
@VisibleForTesting |
| 277 |
CompatUIWindowManager createCompatUiWindowManager(Context context, TaskInfo taskInfo, |
| 278 |
ShellTaskOrganizer.TaskListener taskListener) { |
| 279 |
return new CompatUIWindowManager(context, |
| 280 |
taskInfo, mSyncQueue, mCallback, taskListener, |
| 281 |
mDisplayController.getDisplayLayout(taskInfo.displayId), mCompatUIHintsState); |
| 282 |
} |
| 283 |
|
| 284 |
private void createOrUpdateLetterboxEduLayout(TaskInfo taskInfo, |
| 285 |
ShellTaskOrganizer.TaskListener taskListener) { |
| 286 |
if (mActiveLetterboxEduLayout != null |
| 287 |
&& mActiveLetterboxEduLayout.getTaskId() == taskInfo.taskId) { |
| 288 |
|
| 289 |
if (!mActiveLetterboxEduLayout.updateCompatInfo(taskInfo, taskListener, |
| 290 |
showOnDisplay(mActiveLetterboxEduLayout.getDisplayId()))) { |
| 291 |
|
| 292 |
mActiveLetterboxEduLayout = null; |
| 293 |
} |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
final Context context = getOrCreateDisplayContext(taskInfo.displayId); |
| 299 |
if (context == null) { |
| 300 |
return; |
| 301 |
} |
| 302 |
LetterboxEduWindowManager newLayout = createLetterboxEduWindowManager(context, taskInfo, |
| 303 |
taskListener); |
| 304 |
if (newLayout.createLayout(showOnDisplay(taskInfo.displayId))) { |
| 305 |
|
| 306 |
if (mActiveLetterboxEduLayout != null) { |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
mActiveLetterboxEduLayout.release(); |
| 311 |
} |
| 312 |
mActiveLetterboxEduLayout = newLayout; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
@VisibleForTesting |
| 317 |
LetterboxEduWindowManager createLetterboxEduWindowManager(Context context, TaskInfo taskInfo, |
| 318 |
ShellTaskOrganizer.TaskListener taskListener) { |
| 319 |
return new LetterboxEduWindowManager(context, taskInfo, |
| 320 |
mSyncQueue, taskListener, mDisplayController.getDisplayLayout(taskInfo.displayId), |
| 321 |
mTransitionsLazy.get(), |
| 322 |
this::onLetterboxEduDismissed, |
| 323 |
mDockStateReader); |
| 324 |
} |
| 325 |
|
| 326 |
private void onLetterboxEduDismissed() { |
| 327 |
mActiveLetterboxEduLayout = null; |
| 328 |
} |
| 329 |
|
| 330 |
private void removeLayouts(int taskId) { |
| 331 |
final CompatUIWindowManager layout = mActiveCompatLayouts.get(taskId); |
| 332 |
if (layout != null) { |
| 333 |
layout.release(); |
| 334 |
mActiveCompatLayouts.remove(taskId); |
| 335 |
} |
| 336 |
|
| 337 |
if (mActiveLetterboxEduLayout != null && mActiveLetterboxEduLayout.getTaskId() == taskId) { |
| 338 |
mActiveLetterboxEduLayout.release(); |
| 339 |
mActiveLetterboxEduLayout = null; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
private Context getOrCreateDisplayContext(int displayId) { |
| 344 |
if (displayId == Display.DEFAULT_DISPLAY) { |
| 345 |
return mContext; |
| 346 |
} |
| 347 |
Context context = null; |
| 348 |
final WeakReference<Context> ref = mDisplayContextCache.get(displayId); |
| 349 |
if (ref != null) { |
| 350 |
context = ref.get(); |
| 351 |
} |
| 352 |
if (context == null) { |
| 353 |
Display display = mContext.getSystemService(DisplayManager.class).getDisplay(displayId); |
| 354 |
if (display != null) { |
| 355 |
context = mContext.createDisplayContext(display); |
| 356 |
mDisplayContextCache.put(displayId, new WeakReference<>(context)); |
| 357 |
} else { |
| 358 |
Log.e(TAG, "Cannot get context for display " + displayId); |
| 359 |
} |
| 360 |
} |
| 361 |
return context; |
| 362 |
} |
| 363 |
|
| 364 |
private void forAllLayoutsOnDisplay(int displayId, |
| 365 |
Consumer<CompatUIWindowManagerAbstract> callback) { |
| 366 |
forAllLayouts(layout -> layout.getDisplayId() == displayId, callback); |
| 367 |
} |
| 368 |
|
| 369 |
private void forAllLayouts(Consumer<CompatUIWindowManagerAbstract> callback) { |
| 370 |
forAllLayouts(layout -> true, callback); |
| 371 |
} |
| 372 |
|
| 373 |
private void forAllLayouts(Predicate<CompatUIWindowManagerAbstract> condition, |
| 374 |
Consumer<CompatUIWindowManagerAbstract> callback) { |
| 375 |
for (int i = 0; i < mActiveCompatLayouts.size(); i++) { |
| 376 |
final int taskId = mActiveCompatLayouts.keyAt(i); |
| 377 |
final CompatUIWindowManager layout = mActiveCompatLayouts.get(taskId); |
| 378 |
if (layout != null && condition.test(layout)) { |
| 379 |
callback.accept(layout); |
| 380 |
} |
| 381 |
} |
| 382 |
if (mActiveLetterboxEduLayout != null && condition.test(mActiveLetterboxEduLayout)) { |
| 383 |
callback.accept(mActiveLetterboxEduLayout); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
private class PerDisplayOnInsetsChangedListener implements OnInsetsChangedListener { |
| 389 |
final int mDisplayId; |
| 390 |
final InsetsState mInsetsState = new InsetsState(); |
| 391 |
|
| 392 |
PerDisplayOnInsetsChangedListener(int displayId) { |
| 393 |
mDisplayId = displayId; |
| 394 |
} |
| 395 |
|
| 396 |
void register() { |
| 397 |
mDisplayInsetsController.addInsetsChangedListener(mDisplayId, this); |
| 398 |
} |
| 399 |
|
| 400 |
void unregister() { |
| 401 |
mDisplayInsetsController.removeInsetsChangedListener(mDisplayId, this); |
| 402 |
} |
| 403 |
|
| 404 |
@Override |
| 405 |
public void insetsChanged(InsetsState insetsState) { |
| 406 |
if (mInsetsState.equals(insetsState)) { |
| 407 |
return; |
| 408 |
} |
| 409 |
mInsetsState.set(insetsState); |
| 410 |
updateDisplayLayout(mDisplayId); |
| 411 |
} |
| 412 |
|
| 413 |
@Override |
| 414 |
public void insetsControlChanged(InsetsState insetsState, |
| 415 |
InsetsSourceControl[] activeControls) { |
| 416 |
insetsChanged(insetsState); |
| 417 |
} |
| 418 |
} |
| 419 |
} |