From 0cbfd2b12cc14e0a7bb052426b6822460fb13920 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 7 Feb 2020 11:19:16 -0800 Subject: [PATCH] Allow activities/services to have app loaders When a service or activity attaches to a base context, add the loaders from the application context to the base context. Activity and service contexts are created before creating the Application context, so in order to add the app loaders before the component onCreate method is called, we must add the app loaders to the component after the app has been initialized. Bug: 148630842 Test: launch AppResourcesLoaders Change-Id: I44aa718779c574094590d25fd839f1d9f9134edb --- core/java/android/app/ActivityThread.java | 12 ++++ tests/AppResourcesLoaders/Android.bp | 22 +++++++ tests/AppResourcesLoaders/AndroidManifest.xml | 31 ++++++++++ tests/AppResourcesLoaders/Overlay/Android.bp | 19 ++++++ .../Overlay/AndroidManifest.xml | 21 +++++++ .../Overlay/res/values/values.xml | 20 +++++++ .../res/layout/activity_isolated.xml | 25 ++++++++ .../res/layout/activity_main.xml | 29 ++++++++++ .../AppResourcesLoaders/res/values/styles.xml | 23 ++++++++ .../AppResourcesLoaders/res/values/values.xml | 20 +++++++ .../example/loaders/LoaderActivity.java | 44 ++++++++++++++ .../loaders/LoaderActivityIsolated.java | 40 +++++++++++++ .../example/loaders/LoadersApplication.java | 58 +++++++++++++++++++ 13 files changed, 364 insertions(+) create mode 100644 tests/AppResourcesLoaders/Android.bp create mode 100644 tests/AppResourcesLoaders/AndroidManifest.xml create mode 100644 tests/AppResourcesLoaders/Overlay/Android.bp create mode 100644 tests/AppResourcesLoaders/Overlay/AndroidManifest.xml create mode 100644 tests/AppResourcesLoaders/Overlay/res/values/values.xml create mode 100644 tests/AppResourcesLoaders/res/layout/activity_isolated.xml create mode 100644 tests/AppResourcesLoaders/res/layout/activity_main.xml create mode 100644 tests/AppResourcesLoaders/res/values/styles.xml create mode 100644 tests/AppResourcesLoaders/res/values/values.xml create mode 100644 tests/AppResourcesLoaders/src/com/android/example/loaders/LoaderActivity.java create mode 100644 tests/AppResourcesLoaders/src/com/android/example/loaders/LoaderActivityIsolated.java create mode 100644 tests/AppResourcesLoaders/src/com/android/example/loaders/LoadersApplication.java diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 0ed5aec58924a..a62f0a6807bb1 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -75,6 +75,7 @@ import android.content.res.CompatibilityInfo; import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.Resources.Theme; +import android.content.res.loader.ResourcesLoader; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDebug; import android.database.sqlite.SQLiteDebug.DbStats; @@ -3283,6 +3284,12 @@ public final class ActivityThread extends ClientTransactionHandler { r.mPendingRemoveWindow = null; r.mPendingRemoveWindowManager = null; } + + // Activity resources must be initialized with the same loaders as the + // application context. + appContext.getResources().addLoaders( + app.getResources().getLoaders().toArray(new ResourcesLoader[0])); + appContext.setOuterContext(activity); activity.attach(appContext, this, getInstrumentation(), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, @@ -4083,6 +4090,11 @@ public final class ActivityThread extends ClientTransactionHandler { java.lang.ClassLoader cl = packageInfo.getClassLoader(); service = packageInfo.getAppFactory() .instantiateService(cl, data.info.name, data.intent); + // Service resources must be initialized with the same loaders as the application + // context. + context.getResources().addLoaders( + app.getResources().getLoaders().toArray(new ResourcesLoader[0])); + context.setOuterContext(service); service.attach(context, this, data.info.name, data.token, app, ActivityManager.getService()); diff --git a/tests/AppResourcesLoaders/Android.bp b/tests/AppResourcesLoaders/Android.bp new file mode 100644 index 0000000000000..e5739dbf181c4 --- /dev/null +++ b/tests/AppResourcesLoaders/Android.bp @@ -0,0 +1,22 @@ +// +// Copyright (C) 2020 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +android_test { + name: "AppResourcesLoaders", + srcs: ["**/*.java"], + sdk_version: "current", + java_resources: [":AppResourcesLoaders_Overlay"] +} diff --git a/tests/AppResourcesLoaders/AndroidManifest.xml b/tests/AppResourcesLoaders/AndroidManifest.xml new file mode 100644 index 0000000000000..cb403b968abf7 --- /dev/null +++ b/tests/AppResourcesLoaders/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + diff --git a/tests/AppResourcesLoaders/Overlay/Android.bp b/tests/AppResourcesLoaders/Overlay/Android.bp new file mode 100644 index 0000000000000..80443f6c3c00a --- /dev/null +++ b/tests/AppResourcesLoaders/Overlay/Android.bp @@ -0,0 +1,19 @@ +// +// Copyright (C) 2020 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +android_test_helper_app { + name: "AppResourcesLoaders_Overlay", +} diff --git a/tests/AppResourcesLoaders/Overlay/AndroidManifest.xml b/tests/AppResourcesLoaders/Overlay/AndroidManifest.xml new file mode 100644 index 0000000000000..083ba37262fdb --- /dev/null +++ b/tests/AppResourcesLoaders/Overlay/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + diff --git a/tests/AppResourcesLoaders/Overlay/res/values/values.xml b/tests/AppResourcesLoaders/Overlay/res/values/values.xml new file mode 100644 index 0000000000000..8f6e462ffcaef --- /dev/null +++ b/tests/AppResourcesLoaders/Overlay/res/values/values.xml @@ -0,0 +1,20 @@ + + + + Loaders present: true + + \ No newline at end of file diff --git a/tests/AppResourcesLoaders/res/layout/activity_isolated.xml b/tests/AppResourcesLoaders/res/layout/activity_isolated.xml new file mode 100644 index 0000000000000..0a13f00633f65 --- /dev/null +++ b/tests/AppResourcesLoaders/res/layout/activity_isolated.xml @@ -0,0 +1,25 @@ + + + + + + \ No newline at end of file diff --git a/tests/AppResourcesLoaders/res/layout/activity_main.xml b/tests/AppResourcesLoaders/res/layout/activity_main.xml new file mode 100644 index 0000000000000..7277700cb14cf --- /dev/null +++ b/tests/AppResourcesLoaders/res/layout/activity_main.xml @@ -0,0 +1,29 @@ + + + + + + +