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
diff --git a/tests/AppResourcesLoaders/Android.bp b/tests/AppResourcesLoaders/Android.bp
new file mode 100644
index 0000000..e5739db
--- /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 0000000..cb403b9
--- /dev/null
+++ b/tests/AppResourcesLoaders/AndroidManifest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.example.loaders">
+ <application android:label="AppResourcesLoaders"
+ android:name=".LoadersApplication">
+ <activity android:name=".LoaderActivity">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ <activity android:name=".LoaderActivityIsolated"
+ android:process="com.android.phone" />
+ </application>
+</manifest>
diff --git a/tests/AppResourcesLoaders/Overlay/Android.bp b/tests/AppResourcesLoaders/Overlay/Android.bp
new file mode 100644
index 0000000..80443f6
--- /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 0000000..083ba37
--- /dev/null
+++ b/tests/AppResourcesLoaders/Overlay/AndroidManifest.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.example.loaders">
+ <application android:hasCode="false" />
+</manifest>
diff --git a/tests/AppResourcesLoaders/Overlay/res/values/values.xml b/tests/AppResourcesLoaders/Overlay/res/values/values.xml
new file mode 100644
index 0000000..8f6e462
--- /dev/null
+++ b/tests/AppResourcesLoaders/Overlay/res/values/values.xml
@@ -0,0 +1,20 @@
+<!--
+ ~ 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.
+ -->
+
+<resources>
+ <string name="loader_present">Loaders present: true</string>
+ <public type="string" name="loader_present" id="0x7f010000" />
+</resources>
\ 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 0000000..0a13f00
--- /dev/null
+++ b/tests/AppResourcesLoaders/res/layout/activity_isolated.xml
@@ -0,0 +1,25 @@
+<!--
+ ~ 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.
+ -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_height="match_parent"
+ android:layout_width="match_parent"
+ android:gravity="center"
+ android:orientation="vertical">
+
+ <TextView android:text="@string/loader_present"
+ style="@style/ButtonStyle"/>
+</LinearLayout>
\ 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 0000000..7277700
--- /dev/null
+++ b/tests/AppResourcesLoaders/res/layout/activity_main.xml
@@ -0,0 +1,29 @@
+<!--
+ ~ 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.
+ -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_height="match_parent"
+ android:layout_width="match_parent"
+ android:gravity="center"
+ android:orientation="vertical">
+
+ <TextView android:text="@string/loader_present"
+ style="@style/ButtonStyle"/>
+
+ <Button android:id="@+id/btn_isolated_activity"
+ android:text="Launch Isolated Activity"
+ style="@style/ButtonStyle"/>
+</LinearLayout>
\ No newline at end of file
diff --git a/tests/AppResourcesLoaders/res/values/styles.xml b/tests/AppResourcesLoaders/res/values/styles.xml
new file mode 100644
index 0000000..ee73d65
--- /dev/null
+++ b/tests/AppResourcesLoaders/res/values/styles.xml
@@ -0,0 +1,23 @@
+<!--
+ ~ 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.
+ -->
+
+<resources>
+ <style name="ButtonStyle" >
+ <item name="android:layout_width">wrap_content</item>
+ <item name="android:layout_height">40dp</item>
+ <item name="android:layout_centerInParent">true</item>
+ </style>
+</resources>
\ No newline at end of file
diff --git a/tests/AppResourcesLoaders/res/values/values.xml b/tests/AppResourcesLoaders/res/values/values.xml
new file mode 100644
index 0000000..af128b6
--- /dev/null
+++ b/tests/AppResourcesLoaders/res/values/values.xml
@@ -0,0 +1,20 @@
+<!--
+ ~ 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.
+ -->
+
+<resources>
+ <string name="loader_present">Loaders present: false</string>
+ <public type="string" name="loader_present" id="0x7f010000" />
+</resources>
\ No newline at end of file
diff --git a/tests/AppResourcesLoaders/src/com/android/example/loaders/LoaderActivity.java b/tests/AppResourcesLoaders/src/com/android/example/loaders/LoaderActivity.java
new file mode 100644
index 0000000..49ef46f
--- /dev/null
+++ b/tests/AppResourcesLoaders/src/com/android/example/loaders/LoaderActivity.java
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+package com.android.example.loaders;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+
+public class LoaderActivity extends Activity {
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(newBase);
+ final String loaderPresentOnAttach =
+ newBase.getResources().getString(R.string.loader_present);
+ if (loaderPresentOnAttach == null || !loaderPresentOnAttach.endsWith("true")) {
+ throw new AssertionError("Loader not present in attachBaseContext");
+ }
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+ findViewById(R.id.btn_isolated_activity).setOnClickListener((v) -> {
+ startActivity(new Intent(this, LoaderActivityIsolated.class));
+ });
+ }
+}
diff --git a/tests/AppResourcesLoaders/src/com/android/example/loaders/LoaderActivityIsolated.java b/tests/AppResourcesLoaders/src/com/android/example/loaders/LoaderActivityIsolated.java
new file mode 100644
index 0000000..04550b9
--- /dev/null
+++ b/tests/AppResourcesLoaders/src/com/android/example/loaders/LoaderActivityIsolated.java
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+
+package com.android.example.loaders;
+
+import android.app.Activity;
+import android.content.Context;
+import android.os.Bundle;
+
+public class LoaderActivityIsolated extends Activity {
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(newBase);
+ final String loaderPresentOnAttach =
+ newBase.getResources().getString(R.string.loader_present);
+ if (loaderPresentOnAttach == null || !loaderPresentOnAttach.endsWith("true")) {
+ throw new AssertionError("Loader not present in attachBaseContext");
+ }
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_isolated);
+ }
+}
diff --git a/tests/AppResourcesLoaders/src/com/android/example/loaders/LoadersApplication.java b/tests/AppResourcesLoaders/src/com/android/example/loaders/LoadersApplication.java
new file mode 100644
index 0000000..709c208
--- /dev/null
+++ b/tests/AppResourcesLoaders/src/com/android/example/loaders/LoadersApplication.java
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+
+package com.android.example.loaders;
+
+import android.app.Application;
+import android.content.res.Resources;
+import android.content.res.loader.ResourcesLoader;
+import android.content.res.loader.ResourcesProvider;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class LoadersApplication extends Application {
+ private static final String TAG = "LoadersApplication";
+ private static final String LOADER_RESOURCES_APK = "AppResourcesLoaders_Overlay.apk";
+
+ @Override
+ public void onCreate() {
+ try {
+ final Resources resources = getResources();
+ final ResourcesLoader loader = new ResourcesLoader();
+ loader.addProvider(ResourcesProvider.loadFromApk(copyResource(LOADER_RESOURCES_APK)));
+ resources.addLoaders(loader);
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to load loader resources ", e);
+ }
+ }
+
+ private ParcelFileDescriptor copyResource(String fileName) throws IOException {
+ final File apkFile = new File(getFilesDir(), fileName);
+ final InputStream is = getClassLoader().getResourceAsStream(LOADER_RESOURCES_APK);
+ final FileOutputStream os = new FileOutputStream(apkFile);
+ byte[] buffer = new byte[8192];
+ int count;
+ while ((count = is.read(buffer)) != -1) {
+ os.write(buffer, 0, count);
+ }
+ return ParcelFileDescriptor.open(apkFile, ParcelFileDescriptor.MODE_READ_ONLY);
+ }
+}