From 49cd50b3fd7e8774dbd15352b3704bb6b9767e8c Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Mon, 29 Mar 2021 15:53:31 +0800 Subject: Introduce WindowProviderService A Window Provider Service is a Window-Context-like Service which handles UI components and is able to obtain the latest configuration. The differences between a Window Context and a Window Provider Service is that: 1. It is always associated with the primary display before attachWindowToken() or WM#addView is called. It is suggested to render UI components after calling the APIs mentioned above. 2. A window context registers the listener in constructor and unregisters it in finalize(), while a window provider service registers the listener in onCreate() and unregisters in onDestroy(). 3. Like the API Context#createWindowContext(int windowType, Bundle options), the users of a Window Provider Service need to override provideWindowType and provideOptions to pass the attributes. 4. When there's a configuration updates from the server side, the Service#onConfigurationChanged callback will be invoked.(TBD) It is suggested to use window context when possible. This class is to migrate the Service to show UI components to the window context concept. We can't migrate them to WindowContext directly because developers are used to use this kind of Service as the container of UI components and may change its property at runtime. An example is that keyboard developers may apply a new theme by InputMethodService#getResources#setTheme(newTheme). Bug: 159767464 Test: atest WindowContextTests#testWindowProviderServiceLifecycle Change-Id: I7d537fd2d128efa28aa6e771d77aa105fb497672 --- core/java/android/app/Service.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'core/java/android/app/Service.java') diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java index 2ceea7f1a6a8..0ab3f2f4be46 100644 --- a/core/java/android/app/Service.java +++ b/core/java/android/app/Service.java @@ -860,6 +860,19 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac setContentCaptureOptions(application.getContentCaptureOptions()); } + /** + * Creates the base {@link Context} of this {@link Service}. + * Users may override this API to create customized base context. + * + * @see android.window.WindowProviderService WindowProviderService class for example + * @see ContextWrapper#attachBaseContext(Context) + * + * @hide + */ + public Context createServiceBaseContext(ActivityThread mainThread, LoadedApk packageInfo) { + return ContextImpl.createAppContext(mainThread, packageInfo); + } + /** * @hide * Clean up any references to avoid leaks. -- cgit v1.2.3