Skip to content

Commit 57f4b77

Browse files
amhkKenneth Andersson
authored andcommitted
Runtime resource overlay, iteration 1.
Runtime resource overlay allows unmodified applications to appear as if they had been compiled with additional resources defined. See libs/utils/README for more information. This commit is the first iteration of runtime resource overlay. It provides the actual overlay modifications and loading of trusted overlay packages (ie residing in /vendor) targeting framework-res.apk. This commit loads exactly one overlay package. The overlay, if present, must target framework-res.apk and be located at /vendor/overlay/framework/framework-res.apk. Change-Id: If26ee7754813004a96c043dba37fbe99fa3919db
1 parent c6b22e2 commit 57f4b77

File tree

21 files changed

+1202
-61
lines changed

21 files changed

+1202
-61
lines changed

core/tests/overlaytests/Android.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Dummy makefile to halt recursive directory traversal.
2+
3+
LOCAL_PATH := $(call my-dir)
4+
include $(CLEAR_VARS)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LOCAL_PATH := $(call my-dir)
2+
include $(CLEAR_VARS)
3+
4+
LOCAL_MODULE_TAGS := tests
5+
6+
LOCAL_PACKAGE_NAME := OverlayTest
7+
8+
LOCAL_SRC_FILES := $(call all-java-files-under, src)
9+
10+
include $(BUILD_PACKAGE)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.android.overlaytest">
3+
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION"/>
4+
<application>
5+
<uses-library android:name="android.test.runner"/>
6+
</application>
7+
<instrumentation android:name="android.test.InstrumentationTestRunner"
8+
android:targetPackage="com.android.overlaytest"
9+
android:label="Runtime resource overlay tests"/>
10+
</manifest>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.android.overlaytest;
2+
3+
import android.content.res.Configuration;
4+
import android.content.res.Resources;
5+
import android.test.AndroidTestCase;
6+
import java.io.InputStream;
7+
import java.util.Locale;
8+
9+
public abstract class OverlayBaseTest extends AndroidTestCase {
10+
private Resources mResources;
11+
protected boolean mWithOverlay; // will be set by subclasses
12+
13+
protected void setUp() {
14+
mResources = getContext().getResources();
15+
}
16+
17+
private int calculateRawResourceChecksum(int resId) throws Throwable {
18+
InputStream input = null;
19+
try {
20+
input = mResources.openRawResource(resId);
21+
int ch, checksum = 0;
22+
while ((ch = input.read()) != -1) {
23+
checksum = (checksum + ch) % 0xffddbb00;
24+
}
25+
return checksum;
26+
} finally {
27+
input.close();
28+
}
29+
}
30+
31+
private void setLocale(String code) {
32+
Locale locale = new Locale(code);
33+
Locale.setDefault(locale);
34+
Configuration config = new Configuration();
35+
config.locale = locale;
36+
mResources.updateConfiguration(config, mResources.getDisplayMetrics());
37+
}
38+
39+
private void assertResource(int resId, boolean ewo, boolean ew) throws Throwable {
40+
boolean expected = mWithOverlay ? ew : ewo;
41+
boolean actual = mResources.getBoolean(resId);
42+
assertEquals(expected, actual);
43+
}
44+
45+
private void assertResource(int resId, String ewo, String ew) throws Throwable {
46+
String expected = mWithOverlay ? ew : ewo;
47+
String actual = mResources.getString(resId);
48+
assertEquals(expected, actual);
49+
}
50+
51+
private void assertResource(int resId, int[] ewo, int[] ew) throws Throwable {
52+
int[] expected = mWithOverlay ? ew : ewo;
53+
int[] actual = mResources.getIntArray(resId);
54+
assertEquals("length:", expected.length, actual.length);
55+
for (int i = 0; i < actual.length; ++i) {
56+
assertEquals("index " + i + ":", actual[i], expected[i]);
57+
}
58+
}
59+
60+
public void testBooleanOverlay() throws Throwable {
61+
// config_automatic_brightness_available has overlay (default config)
62+
final int resId = com.android.internal.R.bool.config_automatic_brightness_available;
63+
assertResource(resId, false, true);
64+
}
65+
66+
public void testBoolean() throws Throwable {
67+
// config_bypass_keyguard_if_slider_open has no overlay
68+
final int resId = com.android.internal.R.bool.config_bypass_keyguard_if_slider_open;
69+
assertResource(resId, true, true);
70+
}
71+
72+
public void testStringOverlay() throws Throwable {
73+
// phoneTypeCar has an overlay (default config), which shouldn't shadow
74+
// the Swedish translation
75+
final int resId = com.android.internal.R.string.phoneTypeCar;
76+
setLocale("sv_SE");
77+
assertResource(resId, "Bil", "Bil");
78+
}
79+
80+
public void testStringSwedishOverlay() throws Throwable {
81+
// phoneTypeWork has overlay (no default config, only for lang=sv)
82+
final int resId = com.android.internal.R.string.phoneTypeWork;
83+
setLocale("en_US");
84+
assertResource(resId, "Work", "Work");
85+
setLocale("sv_SE");
86+
assertResource(resId, "Arbete", "Jobb");
87+
}
88+
89+
public void testString() throws Throwable {
90+
// phoneTypeHome has no overlay
91+
final int resId = com.android.internal.R.string.phoneTypeHome;
92+
setLocale("en_US");
93+
assertResource(resId, "Home", "Home");
94+
setLocale("sv_SE");
95+
assertResource(resId, "Hem", "Hem");
96+
}
97+
98+
public void testIntegerArrayOverlay() throws Throwable {
99+
// config_scrollBarrierVibePattern has overlay (default config)
100+
final int resId = com.android.internal.R.array.config_scrollBarrierVibePattern;
101+
assertResource(resId, new int[]{0, 15, 10, 10}, new int[]{100, 200, 300});
102+
}
103+
104+
public void testIntegerArray() throws Throwable {
105+
// config_virtualKeyVibePattern has no overlay
106+
final int resId = com.android.internal.R.array.config_virtualKeyVibePattern;
107+
final int[] expected = {0, 10, 20, 30};
108+
assertResource(resId, expected, expected);
109+
}
110+
111+
public void testAsset() throws Throwable {
112+
// drawable/default_background.jpg has overlay (default config)
113+
final int resId = com.android.internal.R.drawable.default_wallpaper;
114+
int actual = calculateRawResourceChecksum(resId);
115+
int expected = mWithOverlay ? 0x000051da : 0x0014ebce;
116+
assertEquals(expected, actual);
117+
}
118+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.android.overlaytest;
2+
3+
public class WithOverlayTest extends OverlayBaseTest {
4+
public WithOverlayTest() {
5+
mWithOverlay = true;
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.android.overlaytest;
2+
3+
public class WithoutOverlayTest extends OverlayBaseTest {
4+
public WithoutOverlayTest() {
5+
mWithOverlay = false;
6+
}
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
LOCAL_PATH := $(call my-dir)
2+
include $(CLEAR_VARS)
3+
4+
LOCAL_MODULE_TAGS := tests
5+
6+
LOCAL_SRC_FILES := $(call all-java-files-under, src)
7+
8+
LOCAL_SDK_VERSION := current
9+
10+
LOCAL_PACKAGE_NAME := com.android.overlaytest.overlay
11+
12+
LOCAL_AAPT_FLAGS := -o
13+
14+
include $(BUILD_PACKAGE)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.android.overlaytest.overlay"
3+
android:versionCode="1"
4+
android:versionName="1.0">
5+
<overlay-package android:name="android"/>
6+
</manifest>
399 Bytes
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="phoneTypeWork">Jobb</string>
4+
</resources>

0 commit comments

Comments
 (0)