Skip to content

Commit

Permalink
[M3][Color] Fix Resources Loader bug for color harmonization
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 449926388
(cherry picked from commit 78d2c1f)
  • Loading branch information
Material Design Team authored and pekingme committed May 25, 2022
1 parent b33cf80 commit bef8ca1
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ private ColorResourcesTableCreator() {}
private static final short HEADER_TYPE_TYPE = 0x0201;
private static final short HEADER_TYPE_TYPE_SPEC = 0x0202;

private static final byte TYPE_ID_COLOR = 0x06;

private static final byte ANDROID_PACKAGE_ID = 0x01;
private static final byte APPLICATION_PACKAGE_ID = 0x7F;

private static final String RESOURCE_TYPE_NAME_COLOR = "color";

private static byte typeIdColor;

private static final PackageInfo ANDROID_PACKAGE_INFO =
new PackageInfo(ANDROID_PACKAGE_ID, "android");

Expand All @@ -63,17 +65,24 @@ public int compare(ColorResource res1, ColorResource res2) {
};

static byte[] create(Context context, Map<Integer, Integer> colorMapping) throws IOException {
if (colorMapping.entrySet().isEmpty()) {
throw new IllegalArgumentException("No color resources provided for harmonization.");
}
PackageInfo applicationPackageInfo =
new PackageInfo(APPLICATION_PACKAGE_ID, context.getPackageName());

Map<PackageInfo, List<ColorResource>> colorResourceMap = new HashMap<>();
ColorResource colorResource = null;
for (Map.Entry<Integer, Integer> entry : colorMapping.entrySet()) {
ColorResource colorResource =
colorResource =
new ColorResource(
entry.getKey(),
context.getResources().getResourceName(entry.getKey()),
entry.getValue());
if (colorResource.typeId != TYPE_ID_COLOR) {
if (!context
.getResources()
.getResourceTypeName(entry.getKey())
.equals(RESOURCE_TYPE_NAME_COLOR)) {
throw new IllegalArgumentException(
"Non color resource found: name="
+ colorResource.name
Expand All @@ -94,6 +103,13 @@ static byte[] create(Context context, Map<Integer, Integer> colorMapping) throws
}
colorResourceMap.get(packageInfo).add(colorResource);
}
// Resource Type Ids are assigned by aapt arbitrarily, for each new type the next available
// number is assigned and used. The type id will be the same for resources that are the same
// type.
typeIdColor = colorResource.typeId;
if (typeIdColor == 0) {
throw new IllegalArgumentException("No color resources found for harmonization.");
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
new ResTable(colorResourceMap).writeTo(outputStream);
return outputStream.toByteArray();
Expand Down Expand Up @@ -403,7 +419,7 @@ private static class TypeSpecChunk {

void writeTo(ByteArrayOutputStream outputStream) throws IOException {
header.writeTo(outputStream);
outputStream.write(new byte[] {TYPE_ID_COLOR, 0x00, 0x00, 0x00});
outputStream.write(new byte[] {typeIdColor, 0x00, 0x00, 0x00});
outputStream.write(intToByteArray(entryCount));
for (int entryFlag : entryFlags) {
outputStream.write(intToByteArray(entryFlag));
Expand Down Expand Up @@ -465,7 +481,7 @@ private static class TypeChunk {

void writeTo(ByteArrayOutputStream outputStream) throws IOException {
header.writeTo(outputStream);
outputStream.write(new byte[] {TYPE_ID_COLOR, 0x00, 0x00, 0x00});
outputStream.write(new byte[] {typeIdColor, 0x00, 0x00, 0x00});
outputStream.write(intToByteArray(entryCount));
outputStream.write(intToByteArray(getEntryStart()));
outputStream.write(config);
Expand Down

0 comments on commit bef8ca1

Please sign in to comment.