Merge "Add Theme.setNewResourcesImpl() method." into sc-dev

This commit is contained in:
Yuncheol Heo
2021-03-24 15:36:17 +00:00
committed by Android (Google) Code Review
2 changed files with 121 additions and 97 deletions

View File

@@ -360,7 +360,7 @@ public class Resources {
WeakReference<Theme> weakThemeRef = mThemeRefs.get(i); WeakReference<Theme> weakThemeRef = mThemeRefs.get(i);
Theme theme = weakThemeRef != null ? weakThemeRef.get() : null; Theme theme = weakThemeRef != null ? weakThemeRef.get() : null;
if (theme != null) { if (theme != null) {
theme.setImpl(mResourcesImpl.newThemeImpl(theme.getKey())); theme.setNewResourcesImpl(mResourcesImpl);
} }
} }
} }
@@ -1500,6 +1500,9 @@ public class Resources {
* retrieve XML attributes with style and theme information applied. * retrieve XML attributes with style and theme information applied.
*/ */
public final class Theme { public final class Theme {
private final Object mLock = new Object();
@GuardedBy("mLock")
@UnsupportedAppUsage @UnsupportedAppUsage
private ResourcesImpl.ThemeImpl mThemeImpl; private ResourcesImpl.ThemeImpl mThemeImpl;
@@ -1507,7 +1510,15 @@ public class Resources {
} }
void setImpl(ResourcesImpl.ThemeImpl impl) { void setImpl(ResourcesImpl.ThemeImpl impl) {
mThemeImpl = impl; synchronized (mLock) {
mThemeImpl = impl;
}
}
void setNewResourcesImpl(ResourcesImpl resImpl) {
synchronized (mLock) {
mThemeImpl = resImpl.newThemeImpl(mThemeImpl.getKey());
}
} }
/** /**
@@ -1528,7 +1539,9 @@ public class Resources {
* if not already defined in the theme. * if not already defined in the theme.
*/ */
public void applyStyle(int resId, boolean force) { public void applyStyle(int resId, boolean force) {
mThemeImpl.applyStyle(resId, force); synchronized (mLock) {
mThemeImpl.applyStyle(resId, force);
}
} }
/** /**
@@ -1541,7 +1554,11 @@ public class Resources {
* @param other The existing Theme to copy from. * @param other The existing Theme to copy from.
*/ */
public void setTo(Theme other) { public void setTo(Theme other) {
mThemeImpl.setTo(other.mThemeImpl); synchronized (mLock) {
synchronized (other.mLock) {
mThemeImpl.setTo(other.mThemeImpl);
}
}
} }
/** /**
@@ -1566,7 +1583,9 @@ public class Resources {
*/ */
@NonNull @NonNull
public TypedArray obtainStyledAttributes(@NonNull @StyleableRes int[] attrs) { public TypedArray obtainStyledAttributes(@NonNull @StyleableRes int[] attrs) {
return mThemeImpl.obtainStyledAttributes(this, null, attrs, 0, 0); synchronized (mLock) {
return mThemeImpl.obtainStyledAttributes(this, null, attrs, 0, 0);
}
} }
/** /**
@@ -1594,7 +1613,9 @@ public class Resources {
public TypedArray obtainStyledAttributes(@StyleRes int resId, public TypedArray obtainStyledAttributes(@StyleRes int resId,
@NonNull @StyleableRes int[] attrs) @NonNull @StyleableRes int[] attrs)
throws NotFoundException { throws NotFoundException {
return mThemeImpl.obtainStyledAttributes(this, null, attrs, 0, resId); synchronized (mLock) {
return mThemeImpl.obtainStyledAttributes(this, null, attrs, 0, resId);
}
} }
/** /**
@@ -1650,7 +1671,10 @@ public class Resources {
public TypedArray obtainStyledAttributes(@Nullable AttributeSet set, public TypedArray obtainStyledAttributes(@Nullable AttributeSet set,
@NonNull @StyleableRes int[] attrs, @AttrRes int defStyleAttr, @NonNull @StyleableRes int[] attrs, @AttrRes int defStyleAttr,
@StyleRes int defStyleRes) { @StyleRes int defStyleRes) {
return mThemeImpl.obtainStyledAttributes(this, set, attrs, defStyleAttr, defStyleRes); synchronized (mLock) {
return mThemeImpl.obtainStyledAttributes(this, set, attrs, defStyleAttr,
defStyleRes);
}
} }
/** /**
@@ -1671,7 +1695,9 @@ public class Resources {
@NonNull @NonNull
@UnsupportedAppUsage @UnsupportedAppUsage
public TypedArray resolveAttributes(@NonNull int[] values, @NonNull int[] attrs) { public TypedArray resolveAttributes(@NonNull int[] values, @NonNull int[] attrs) {
return mThemeImpl.resolveAttributes(this, values, attrs); synchronized (mLock) {
return mThemeImpl.resolveAttributes(this, values, attrs);
}
} }
/** /**
@@ -1692,7 +1718,9 @@ public class Resources {
* <var>outValue</var> is valid, else false. * <var>outValue</var> is valid, else false.
*/ */
public boolean resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) { public boolean resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) {
return mThemeImpl.resolveAttribute(resid, outValue, resolveRefs); synchronized (mLock) {
return mThemeImpl.resolveAttribute(resid, outValue, resolveRefs);
}
} }
/** /**
@@ -1702,7 +1730,9 @@ public class Resources {
* @hide * @hide
*/ */
public int[] getAllAttributes() { public int[] getAllAttributes() {
return mThemeImpl.getAllAttributes(); synchronized (mLock) {
return mThemeImpl.getAllAttributes();
}
} }
/** /**
@@ -1738,7 +1768,9 @@ public class Resources {
* @see ActivityInfo * @see ActivityInfo
*/ */
public @Config int getChangingConfigurations() { public @Config int getChangingConfigurations() {
return mThemeImpl.getChangingConfigurations(); synchronized (mLock) {
return mThemeImpl.getChangingConfigurations();
}
} }
/** /**
@@ -1749,23 +1781,31 @@ public class Resources {
* @param prefix Text to prefix each line printed. * @param prefix Text to prefix each line printed.
*/ */
public void dump(int priority, String tag, String prefix) { public void dump(int priority, String tag, String prefix) {
mThemeImpl.dump(priority, tag, prefix); synchronized (mLock) {
mThemeImpl.dump(priority, tag, prefix);
}
} }
// Needed by layoutlib. // Needed by layoutlib.
/*package*/ long getNativeTheme() { /*package*/ long getNativeTheme() {
return mThemeImpl.getNativeTheme(); synchronized (mLock) {
return mThemeImpl.getNativeTheme();
}
} }
/*package*/ int getAppliedStyleResId() { /*package*/ int getAppliedStyleResId() {
return mThemeImpl.getAppliedStyleResId(); synchronized (mLock) {
return mThemeImpl.getAppliedStyleResId();
}
} }
/** /**
* @hide * @hide
*/ */
public ThemeKey getKey() { public ThemeKey getKey() {
return mThemeImpl.getKey(); synchronized (mLock) {
return mThemeImpl.getKey();
}
} }
private String getResourceNameFromHexString(String hexString) { private String getResourceNameFromHexString(String hexString) {
@@ -1781,7 +1821,9 @@ public class Resources {
*/ */
@ViewDebug.ExportedProperty(category = "theme", hasAdjacentMapping = true) @ViewDebug.ExportedProperty(category = "theme", hasAdjacentMapping = true)
public String[] getTheme() { public String[] getTheme() {
return mThemeImpl.getTheme(); synchronized (mLock) {
return mThemeImpl.getTheme();
}
} }
/** @hide */ /** @hide */
@@ -1800,7 +1842,9 @@ public class Resources {
* {@link #applyStyle(int, boolean)}. * {@link #applyStyle(int, boolean)}.
*/ */
public void rebase() { public void rebase() {
mThemeImpl.rebase(); synchronized (mLock) {
mThemeImpl.rebase();
}
} }
/** /**
@@ -1862,12 +1906,14 @@ public class Resources {
@NonNull @NonNull
public int[] getAttributeResolutionStack(@AttrRes int defStyleAttr, public int[] getAttributeResolutionStack(@AttrRes int defStyleAttr,
@StyleRes int defStyleRes, @StyleRes int explicitStyleRes) { @StyleRes int defStyleRes, @StyleRes int explicitStyleRes) {
int[] stack = mThemeImpl.getAttributeResolutionStack( synchronized (mLock) {
defStyleAttr, defStyleRes, explicitStyleRes); int[] stack = mThemeImpl.getAttributeResolutionStack(
if (stack == null) { defStyleAttr, defStyleRes, explicitStyleRes);
return new int[0]; if (stack == null) {
} else { return new int[0];
return stack; } else {
return stack;
}
} }
} }
} }

View File

@@ -1314,22 +1314,16 @@ public class ResourcesImpl {
} }
void applyStyle(int resId, boolean force) { void applyStyle(int resId, boolean force) {
synchronized (mKey) { mAssets.applyStyleToTheme(mTheme, resId, force);
mAssets.applyStyleToTheme(mTheme, resId, force); mThemeResId = resId;
mThemeResId = resId; mKey.append(resId, force);
mKey.append(resId, force);
}
} }
void setTo(ThemeImpl other) { void setTo(ThemeImpl other) {
synchronized (mKey) { mAssets.setThemeTo(mTheme, other.mAssets, other.mTheme);
synchronized (other.mKey) {
mAssets.setThemeTo(mTheme, other.mAssets, other.mTheme);
mThemeResId = other.mThemeResId; mThemeResId = other.mThemeResId;
mKey.setTo(other.getKey()); mKey.setTo(other.getKey());
}
}
} }
@NonNull @NonNull
@@ -1338,46 +1332,40 @@ public class ResourcesImpl {
@StyleableRes int[] attrs, @StyleableRes int[] attrs,
@AttrRes int defStyleAttr, @AttrRes int defStyleAttr,
@StyleRes int defStyleRes) { @StyleRes int defStyleRes) {
synchronized (mKey) { final int len = attrs.length;
final int len = attrs.length; final TypedArray array = TypedArray.obtain(wrapper.getResources(), len);
final TypedArray array = TypedArray.obtain(wrapper.getResources(), len);
// XXX note that for now we only work with compiled XML files. // XXX note that for now we only work with compiled XML files.
// To support generic XML files we will need to manually parse // To support generic XML files we will need to manually parse
// out the attributes from the XML file (applying type information // out the attributes from the XML file (applying type information
// contained in the resources and such). // contained in the resources and such).
final XmlBlock.Parser parser = (XmlBlock.Parser) set; final XmlBlock.Parser parser = (XmlBlock.Parser) set;
mAssets.applyStyle(mTheme, defStyleAttr, defStyleRes, parser, attrs, mAssets.applyStyle(mTheme, defStyleAttr, defStyleRes, parser, attrs,
array.mDataAddress, array.mIndicesAddress); array.mDataAddress, array.mIndicesAddress);
array.mTheme = wrapper; array.mTheme = wrapper;
array.mXml = parser; array.mXml = parser;
return array; return array;
}
} }
@NonNull @NonNull
TypedArray resolveAttributes(@NonNull Resources.Theme wrapper, TypedArray resolveAttributes(@NonNull Resources.Theme wrapper,
@NonNull int[] values, @NonNull int[] values,
@NonNull int[] attrs) { @NonNull int[] attrs) {
synchronized (mKey) { final int len = attrs.length;
final int len = attrs.length; if (values == null || len != values.length) {
if (values == null || len != values.length) { throw new IllegalArgumentException(
throw new IllegalArgumentException( "Base attribute values must the same length as attrs");
"Base attribute values must the same length as attrs");
}
final TypedArray array = TypedArray.obtain(wrapper.getResources(), len);
mAssets.resolveAttrs(mTheme, 0, 0, values, attrs, array.mData, array.mIndices);
array.mTheme = wrapper;
array.mXml = null;
return array;
} }
final TypedArray array = TypedArray.obtain(wrapper.getResources(), len);
mAssets.resolveAttrs(mTheme, 0, 0, values, attrs, array.mData, array.mIndices);
array.mTheme = wrapper;
array.mXml = null;
return array;
} }
boolean resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) { boolean resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) {
synchronized (mKey) { return mAssets.getThemeValue(mTheme, resid, outValue, resolveRefs);
return mAssets.getThemeValue(mTheme, resid, outValue, resolveRefs);
}
} }
int[] getAllAttributes() { int[] getAllAttributes() {
@@ -1385,35 +1373,29 @@ public class ResourcesImpl {
} }
@Config int getChangingConfigurations() { @Config int getChangingConfigurations() {
synchronized (mKey) { final @NativeConfig int nativeChangingConfig =
final @NativeConfig int nativeChangingConfig = AssetManager.nativeThemeGetChangingConfigurations(mTheme);
AssetManager.nativeThemeGetChangingConfigurations(mTheme); return ActivityInfo.activityInfoConfigNativeToJava(nativeChangingConfig);
return ActivityInfo.activityInfoConfigNativeToJava(nativeChangingConfig);
}
} }
public void dump(int priority, String tag, String prefix) { public void dump(int priority, String tag, String prefix) {
synchronized (mKey) { mAssets.dumpTheme(mTheme, priority, tag, prefix);
mAssets.dumpTheme(mTheme, priority, tag, prefix);
}
} }
String[] getTheme() { String[] getTheme() {
synchronized (mKey) { final int n = mKey.mCount;
final int N = mKey.mCount; final String[] themes = new String[n * 2];
final String[] themes = new String[N * 2]; for (int i = 0, j = n - 1; i < themes.length; i += 2, --j) {
for (int i = 0, j = N - 1; i < themes.length; i += 2, --j) { final int resId = mKey.mResId[j];
final int resId = mKey.mResId[j]; final boolean forced = mKey.mForce[j];
final boolean forced = mKey.mForce[j]; try {
try { themes[i] = getResourceName(resId);
themes[i] = getResourceName(resId); } catch (NotFoundException e) {
} catch (NotFoundException e) { themes[i] = Integer.toHexString(i);
themes[i] = Integer.toHexString(i);
}
themes[i + 1] = forced ? "forced" : "not forced";
} }
return themes; themes[i + 1] = forced ? "forced" : "not forced";
} }
return themes;
} }
/** /**
@@ -1422,15 +1404,13 @@ public class ResourcesImpl {
* {@link #applyStyle(int, boolean)}. * {@link #applyStyle(int, boolean)}.
*/ */
void rebase() { void rebase() {
synchronized (mKey) { AssetManager.nativeThemeClear(mTheme);
AssetManager.nativeThemeClear(mTheme);
// Reapply the same styles in the same order. // Reapply the same styles in the same order.
for (int i = 0; i < mKey.mCount; i++) { for (int i = 0; i < mKey.mCount; i++) {
final int resId = mKey.mResId[i]; final int resId = mKey.mResId[i];
final boolean force = mKey.mForce[i]; final boolean force = mKey.mForce[i];
mAssets.applyStyleToTheme(mTheme, resId, force); mAssets.applyStyleToTheme(mTheme, resId, force);
}
} }
} }
@@ -1455,10 +1435,8 @@ public class ResourcesImpl {
@Nullable @Nullable
public int[] getAttributeResolutionStack(@AttrRes int defStyleAttr, public int[] getAttributeResolutionStack(@AttrRes int defStyleAttr,
@StyleRes int defStyleRes, @StyleRes int explicitStyleRes) { @StyleRes int defStyleRes, @StyleRes int explicitStyleRes) {
synchronized (mKey) { return mAssets.getAttributeResolutionStack(
return mAssets.getAttributeResolutionStack( mTheme, defStyleAttr, defStyleRes, explicitStyleRes);
mTheme, defStyleAttr, defStyleRes, explicitStyleRes);
}
} }
} }