@@ -46,11 +46,13 @@ private ColorResourcesTableCreator() {}
4646 private static final short HEADER_TYPE_TYPE = 0x0201 ;
4747 private static final short HEADER_TYPE_TYPE_SPEC = 0x0202 ;
4848
49- private static final byte TYPE_ID_COLOR = 0x06 ;
50-
5149 private static final byte ANDROID_PACKAGE_ID = 0x01 ;
5250 private static final byte APPLICATION_PACKAGE_ID = 0x7F ;
5351
52+ private static final String RESOURCE_TYPE_NAME_COLOR = "color" ;
53+
54+ private static byte typeIdColor ;
55+
5456 private static final PackageInfo ANDROID_PACKAGE_INFO =
5557 new PackageInfo (ANDROID_PACKAGE_ID , "android" );
5658
@@ -63,17 +65,24 @@ public int compare(ColorResource res1, ColorResource res2) {
6365 };
6466
6567 static byte [] create (Context context , Map <Integer , Integer > colorMapping ) throws IOException {
68+ if (colorMapping .entrySet ().isEmpty ()) {
69+ throw new IllegalArgumentException ("No color resources provided for harmonization." );
70+ }
6671 PackageInfo applicationPackageInfo =
6772 new PackageInfo (APPLICATION_PACKAGE_ID , context .getPackageName ());
6873
6974 Map <PackageInfo , List <ColorResource >> colorResourceMap = new HashMap <>();
75+ ColorResource colorResource = null ;
7076 for (Map .Entry <Integer , Integer > entry : colorMapping .entrySet ()) {
71- ColorResource colorResource =
77+ colorResource =
7278 new ColorResource (
7379 entry .getKey (),
7480 context .getResources ().getResourceName (entry .getKey ()),
7581 entry .getValue ());
76- if (colorResource .typeId != TYPE_ID_COLOR ) {
82+ if (!context
83+ .getResources ()
84+ .getResourceTypeName (entry .getKey ())
85+ .equals (RESOURCE_TYPE_NAME_COLOR )) {
7786 throw new IllegalArgumentException (
7887 "Non color resource found: name="
7988 + colorResource .name
@@ -94,6 +103,13 @@ static byte[] create(Context context, Map<Integer, Integer> colorMapping) throws
94103 }
95104 colorResourceMap .get (packageInfo ).add (colorResource );
96105 }
106+ // Resource Type Ids are assigned by aapt arbitrarily, for each new type the next available
107+ // number is assigned and used. The type id will be the same for resources that are the same
108+ // type.
109+ typeIdColor = colorResource .typeId ;
110+ if (typeIdColor == 0 ) {
111+ throw new IllegalArgumentException ("No color resources found for harmonization." );
112+ }
97113 ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
98114 new ResTable (colorResourceMap ).writeTo (outputStream );
99115 return outputStream .toByteArray ();
@@ -403,7 +419,7 @@ private static class TypeSpecChunk {
403419
404420 void writeTo (ByteArrayOutputStream outputStream ) throws IOException {
405421 header .writeTo (outputStream );
406- outputStream .write (new byte [] {TYPE_ID_COLOR , 0x00 , 0x00 , 0x00 });
422+ outputStream .write (new byte [] {typeIdColor , 0x00 , 0x00 , 0x00 });
407423 outputStream .write (intToByteArray (entryCount ));
408424 for (int entryFlag : entryFlags ) {
409425 outputStream .write (intToByteArray (entryFlag ));
@@ -465,7 +481,7 @@ private static class TypeChunk {
465481
466482 void writeTo (ByteArrayOutputStream outputStream ) throws IOException {
467483 header .writeTo (outputStream );
468- outputStream .write (new byte [] {TYPE_ID_COLOR , 0x00 , 0x00 , 0x00 });
484+ outputStream .write (new byte [] {typeIdColor , 0x00 , 0x00 , 0x00 });
469485 outputStream .write (intToByteArray (entryCount ));
470486 outputStream .write (intToByteArray (getEntryStart ()));
471487 outputStream .write (config );
0 commit comments