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);
Theme theme = weakThemeRef != null ? weakThemeRef.get() : 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.
*/
public final class Theme {
private final Object mLock = new Object();
@GuardedBy("mLock")
@UnsupportedAppUsage
private ResourcesImpl.ThemeImpl mThemeImpl;
@@ -1507,7 +1510,15 @@ public class Resources {
}
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.
*/
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.
*/
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
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,
@NonNull @StyleableRes int[] attrs)
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,
@NonNull @StyleableRes int[] attrs, @AttrRes int defStyleAttr,
@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
@UnsupportedAppUsage
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.
*/
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
*/
public int[] getAllAttributes() {
return mThemeImpl.getAllAttributes();
synchronized (mLock) {
return mThemeImpl.getAllAttributes();
}
}
/**
@@ -1738,7 +1768,9 @@ public class Resources {
* @see ActivityInfo
*/
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.
*/
public void dump(int priority, String tag, String prefix) {
mThemeImpl.dump(priority, tag, prefix);
synchronized (mLock) {
mThemeImpl.dump(priority, tag, prefix);
}
}
// Needed by layoutlib.
/*package*/ long getNativeTheme() {
return mThemeImpl.getNativeTheme();
synchronized (mLock) {
return mThemeImpl.getNativeTheme();
}
}
/*package*/ int getAppliedStyleResId() {
return mThemeImpl.getAppliedStyleResId();
synchronized (mLock) {
return mThemeImpl.getAppliedStyleResId();
}
}
/**
* @hide
*/
public ThemeKey getKey() {
return mThemeImpl.getKey();
synchronized (mLock) {
return mThemeImpl.getKey();
}
}
private String getResourceNameFromHexString(String hexString) {
@@ -1781,7 +1821,9 @@ public class Resources {
*/
@ViewDebug.ExportedProperty(category = "theme", hasAdjacentMapping = true)
public String[] getTheme() {
return mThemeImpl.getTheme();
synchronized (mLock) {
return mThemeImpl.getTheme();
}
}
/** @hide */
@@ -1800,7 +1842,9 @@ public class Resources {
* {@link #applyStyle(int, boolean)}.
*/
public void rebase() {
mThemeImpl.rebase();
synchronized (mLock) {
mThemeImpl.rebase();
}
}
/**
@@ -1862,12 +1906,14 @@ public class Resources {
@NonNull
public int[] getAttributeResolutionStack(@AttrRes int defStyleAttr,
@StyleRes int defStyleRes, @StyleRes int explicitStyleRes) {
int[] stack = mThemeImpl.getAttributeResolutionStack(
defStyleAttr, defStyleRes, explicitStyleRes);
if (stack == null) {
return new int[0];
} else {
return stack;
synchronized (mLock) {
int[] stack = mThemeImpl.getAttributeResolutionStack(
defStyleAttr, defStyleRes, explicitStyleRes);
if (stack == null) {
return new int[0];
} else {
return stack;
}
}
}
}

View File

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