diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 700d8ff364463..7332701189091 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -2489,6 +2489,20 @@ class ContextImpl extends Context { return new WindowContext(this, display, type, options); } + @NonNull + @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)"); + } + final ContextImpl tokenContext = createBaseWindowContext(token, display); + tokenContext.setResources(createWindowContextResources()); + return tokenContext; + } + + ContextImpl createBaseWindowContext(IBinder token, Display display) { ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mAttributionTag, mSplitName, token, mUser, mFlags, mClassLoader, null); diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index abe7fdae0bf71..29ffa0b70313a 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -6014,10 +6014,13 @@ public abstract class Context { } /** - * A special version of {@link #createWindowContext(int, Bundle)} which also takes - * {@link Display}. The only difference between this API and - * {@link #createWindowContext(int, Bundle)} is that this API can create window context from - * any context even if the context which is not associated to a {@link Display} instance. + * Creates a {@code Context} for a non-{@link android.app.Activity activity} window on the given + * {@link Display}. + * + *
+ * Similar to {@link #createWindowContext(int, Bundle)}, but the {@code display} is passed in, + * instead of implicitly using the {@link #getDisplay() original Context's Display}. + *
* * @param display The {@link Display} to associate with * @param type Window type in {@link WindowManager.LayoutParams} @@ -6122,6 +6125,21 @@ public abstract class Context { @SystemApi public abstract Context createCredentialProtectedStorageContext(); + /** + * Creates a UI context with a {@code token}. The users of this API should handle this context's + * configuration changes. + * + * @param token The token to associate with the {@link Resources} + * @param display The display to associate with the token context + * + * @hide + */ + @UiContext + @NonNull + public Context createTokenContext(@NonNull IBinder token, @NonNull Display display) { + throw new RuntimeException("Not implemented. Must override in a subclass."); + } + /** * Gets the display adjustments holder for this context. This information * is provided on a per-application or activity basis and is used to simulate lower density diff --git a/core/java/android/content/ContextWrapper.java b/core/java/android/content/ContextWrapper.java index e450c08de280c..8e2a8fbcbd2ec 100644 --- a/core/java/android/content/ContextWrapper.java +++ b/core/java/android/content/ContextWrapper.java @@ -21,6 +21,7 @@ import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.TestApi; +import android.annotation.UiContext; import android.app.IApplicationThread; import android.app.IServiceConnection; import android.compat.annotation.UnsupportedAppUsage; @@ -1049,6 +1050,14 @@ public class ContextWrapper extends Context { return mBase.createCredentialProtectedStorageContext(); } + /** @hide */ + @UiContext + @NonNull + @Override + public Context createTokenContext(@NonNull IBinder token, @NonNull Display display) { + return mBase.createTokenContext(token, display); + } + @Override public boolean isDeviceProtectedStorage() { return mBase.isDeviceProtectedStorage(); 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 4a8b450762976..4ef489ffd4802 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/RootTaskDisplayAreaOrganizer.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/RootTaskDisplayAreaOrganizer.java @@ -16,13 +16,23 @@ package com.android.wm.shell; +import android.annotation.UiContext; +import android.app.ResourcesManager; +import android.content.Context; +import android.content.ContextWrapper; +import android.content.res.Configuration; +import android.hardware.display.DisplayManager; +import android.os.Binder; +import android.os.IBinder; import android.util.SparseArray; +import android.view.Display; import android.view.SurfaceControl; import android.window.DisplayAreaAppearedInfo; import android.window.DisplayAreaInfo; import android.window.DisplayAreaOrganizer; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.io.PrintWriter; import java.util.ArrayList; @@ -42,8 +52,13 @@ public class RootTaskDisplayAreaOrganizer extends DisplayAreaOrganizer { private final SparseArray