From 5e393507ffde6a7171504533166371952e4d2bb4 Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 5 Mar 2021 15:16:56 +0800 Subject: [PATCH] Add more logs in RootTDAOrganizer There is an issue that obtaining a display specified by DisplayAreaInfo#displayId in onDisplayAreaAppeared failed and caused an Exception in Shell and System UI restarted. It is very odd that the info is directly from WMS and the display should be initialized already. This CL tries to add more logs to help debug the root cause. Test: manual Bug: 180818790 Change-Id: I0ea6387f80a2f19a985815419fae3602963c6c84 --- core/java/android/app/ContextImpl.java | 4 +-- .../shell/RootTaskDisplayAreaOrganizer.java | 29 ++++++++++--------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index bc798136f2e3f..1d1496f2b5cec 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -2566,9 +2566,7 @@ class ContextImpl extends Context { @Override public Context createTokenContext(@NonNull IBinder token, @NonNull Display display) { if (display == null) { - throw new UnsupportedOperationException("Token context can only be created from " - + "other visual contexts, such as Activity or one created with " - + "Context#createDisplayContext(Display)"); + throw new IllegalArgumentException("Display must not be null"); } final ContextImpl tokenContext = createBaseWindowContext(token, display); tokenContext.setResources(createWindowContextResources()); diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/RootTaskDisplayAreaOrganizer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/RootTaskDisplayAreaOrganizer.java index cb54021d7a237..3708e151c3726 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/RootTaskDisplayAreaOrganizer.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/RootTaskDisplayAreaOrganizer.java @@ -119,7 +119,7 @@ public class RootTaskDisplayAreaOrganizer extends DisplayAreaOrganizer { listeners.get(i).onDisplayAreaAppeared(displayAreaInfo); } } - applyConfigChangesToContext(displayId, displayAreaInfo.configuration); + applyConfigChangesToContext(displayAreaInfo); } @Override @@ -161,24 +161,27 @@ public class RootTaskDisplayAreaOrganizer extends DisplayAreaOrganizer { listeners.get(i).onDisplayAreaInfoChanged(displayAreaInfo); } } - applyConfigChangesToContext(displayId, displayAreaInfo.configuration); + applyConfigChangesToContext(displayAreaInfo); } /** - * Applies the {@link Configuration} to the {@link DisplayAreaContext} specified by - * {@code displayId}. - * - * @param displayId The ID of the {@link Display} which the {@link DisplayAreaContext} is - * associated with - * @param newConfig The propagated configuration + * Applies the {@link DisplayAreaInfo} to the {@link DisplayAreaContext} specified by + * {@link DisplayAreaInfo#displayId}. */ - private void applyConfigChangesToContext(int displayId, @NonNull Configuration newConfig) { + private void applyConfigChangesToContext(@NonNull DisplayAreaInfo displayAreaInfo) { + final int displayId = displayAreaInfo.displayId; + final Display display = mContext.getSystemService(DisplayManager.class) + .getDisplay(displayId); + if (display == null) { + throw new UnsupportedOperationException("The display #" + displayId + " is invalid." + + "displayAreaInfo:" + displayAreaInfo); + } DisplayAreaContext daContext = mDisplayAreaContexts.get(displayId); if (daContext == null) { - daContext = new DisplayAreaContext(mContext, displayId); + daContext = new DisplayAreaContext(mContext, display); mDisplayAreaContexts.put(displayId, daContext); } - daContext.updateConfigurationChanges(newConfig); + daContext.updateConfigurationChanges(displayAreaInfo.configuration); } /** @@ -228,10 +231,8 @@ public class RootTaskDisplayAreaOrganizer extends DisplayAreaOrganizer { private final IBinder mToken = new Binder(); private final ResourcesManager mResourcesManager = ResourcesManager.getInstance(); - public DisplayAreaContext(@NonNull Context context, int displayId) { + public DisplayAreaContext(@NonNull Context context, @NonNull Display display) { super(null); - final Display display = context.getSystemService(DisplayManager.class) - .getDisplay(displayId); attachBaseContext(context.createTokenContext(mToken, display)); }