Add theme information during throwing exception

When an application defines themes that are not inherited from
android:style/Theme, it may cause UnsupportedOperationException.
The cause is that the attributes needed by the application are
defined in neither theme nor layout attribute set.

It takes really a long time to trace all of the themes applied.
This patch logs the basic information about themes to help the
developers to triage issues.

Test: manual test described in b/209708201#comment3

Bug: 209708201
Change-Id: I440aa18d9627adf5e35fd74d7adb1c895f0b169d
This commit is contained in:
Felka Chang
2021-12-09 00:58:28 +08:00
parent 21684a2576
commit dcd3a9cbe9
2 changed files with 40 additions and 18 deletions

View File

@@ -80,6 +80,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
@@ -1942,6 +1943,16 @@ public class Resources {
final Theme other = (Theme) o;
return getKey().equals(other.getKey());
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append('{');
sb.append("id=0x").append(Integer.toHexString(getAppliedStyleResId())).append(", ");
sb.append("themes=").append(Arrays.deepToString(getTheme()));
sb.append('}');
return sb.toString();
}
}
static class ThemeKey implements Cloneable {

View File

@@ -527,11 +527,12 @@ public class TypedArray implements AutoCloseable {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
"Failed to resolve attribute at index " + attrIndex + ": " + value
+ ", theme=" + mTheme);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to color: type=0x" + Integer.toHexString(type));
+ " to color: type=0x" + Integer.toHexString(type) + ", theme=" + mTheme);
}
/**
@@ -561,7 +562,8 @@ public class TypedArray implements AutoCloseable {
if (getValueAt(index * STYLE_NUM_ENTRIES, value)) {
if (value.type == TypedValue.TYPE_ATTRIBUTE) {
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + index + ": " + value);
"Failed to resolve attribute at index " + index + ": " + value
+ ", theme=" + mTheme);
}
return mResources.loadComplexColor(value, value.resourceId, mTheme);
}
@@ -596,7 +598,8 @@ public class TypedArray implements AutoCloseable {
if (getValueAt(index * STYLE_NUM_ENTRIES, value)) {
if (value.type == TypedValue.TYPE_ATTRIBUTE) {
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + index + ": " + value);
"Failed to resolve attribute at index " + index + ": " + value
+ ", theme=" + mTheme);
}
return mResources.loadColorStateList(value, value.resourceId, mTheme);
}
@@ -637,11 +640,12 @@ public class TypedArray implements AutoCloseable {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
"Failed to resolve attribute at index " + attrIndex + ": " + value
+ ", theme=" + mTheme);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to integer: type=0x" + Integer.toHexString(type));
+ " to integer: type=0x" + Integer.toHexString(type) + ", theme=" + mTheme);
}
/**
@@ -684,11 +688,12 @@ public class TypedArray implements AutoCloseable {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
"Failed to resolve attribute at index " + attrIndex + ": " + value
+ ", theme=" + mTheme);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to dimension: type=0x" + Integer.toHexString(type));
+ " to dimension: type=0x" + Integer.toHexString(type) + ", theme=" + mTheme);
}
/**
@@ -732,11 +737,12 @@ public class TypedArray implements AutoCloseable {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
"Failed to resolve attribute at index " + attrIndex + ": " + value
+ ", theme=" + mTheme);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to dimension: type=0x" + Integer.toHexString(type));
+ " to dimension: type=0x" + Integer.toHexString(type) + ", theme=" + mTheme);
}
/**
@@ -781,11 +787,12 @@ public class TypedArray implements AutoCloseable {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
"Failed to resolve attribute at index " + attrIndex + ": " + value
+ ", theme=" + mTheme);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to dimension: type=0x" + Integer.toHexString(type));
+ " to dimension: type=0x" + Integer.toHexString(type) + ", theme=" + mTheme);
}
/**
@@ -825,11 +832,12 @@ public class TypedArray implements AutoCloseable {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
"Failed to resolve attribute at index " + attrIndex + ": " + value
+ ", theme=" + mTheme);
}
throw new UnsupportedOperationException(getPositionDescription()
+ ": You must supply a " + name + " attribute.");
+ ": You must supply a " + name + " attribute." + ", theme=" + mTheme);
}
/**
@@ -900,11 +908,12 @@ public class TypedArray implements AutoCloseable {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
"Failed to resolve attribute at index " + attrIndex + ": " + value
+ ", theme=" + mTheme);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to fraction: type=0x" + Integer.toHexString(type));
+ " to fraction: type=0x" + Integer.toHexString(type) + ", theme=" + mTheme);
}
/**
@@ -996,7 +1005,8 @@ public class TypedArray implements AutoCloseable {
if (getValueAt(index * STYLE_NUM_ENTRIES, value)) {
if (value.type == TypedValue.TYPE_ATTRIBUTE) {
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + index + ": " + value);
"Failed to resolve attribute at index " + index + ": " + value
+ ", theme=" + mTheme);
}
if (density > 0) {
@@ -1032,7 +1042,8 @@ public class TypedArray implements AutoCloseable {
if (getValueAt(index * STYLE_NUM_ENTRIES, value)) {
if (value.type == TypedValue.TYPE_ATTRIBUTE) {
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + index + ": " + value);
"Failed to resolve attribute at index " + index + ": " + value
+ ", theme=" + mTheme);
}
return mResources.getFont(value, value.resourceId);
}