Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[M3][Color] Add support for color resources harmonization in XML
PiperOrigin-RevId: 430757816
- Loading branch information
1 parent
43114c4
commit 869d943
Showing
9 changed files
with
559 additions
and
45 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
lib/java/com/google/android/material/color/ColorResourcesLoaderCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (C) 2022 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.material.color; | ||
|
||
import android.content.Context; | ||
import android.content.res.loader.ResourcesLoader; | ||
import android.content.res.loader.ResourcesProvider; | ||
import android.os.Build.VERSION_CODES; | ||
import android.os.ParcelFileDescriptor; | ||
import android.system.Os; | ||
import android.util.Log; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.RequiresApi; | ||
import java.io.FileDescriptor; | ||
import java.io.FileOutputStream; | ||
import java.io.OutputStream; | ||
import java.util.Map; | ||
|
||
/** This class creates a Resources Table at runtime and helps replace color Resources on the fly. */ | ||
@RequiresApi(VERSION_CODES.R) | ||
final class ColorResourcesLoaderCreator { | ||
|
||
private ColorResourcesLoaderCreator() {} | ||
|
||
private static final String TAG = ColorResourcesLoaderCreator.class.getSimpleName(); | ||
|
||
@Nullable | ||
static ResourcesLoader create( | ||
@NonNull Context context, @NonNull Map<Integer, Integer> colorMapping) { | ||
try { | ||
byte[] contentBytes = ColorResourcesTableCreator.create(context, colorMapping); | ||
Log.i(TAG, "Table created, length: " + contentBytes.length); | ||
if (contentBytes.length == 0) { | ||
return null; | ||
} | ||
FileDescriptor arscFile = null; | ||
try { | ||
arscFile = Os.memfd_create("temp.arsc", /* flags= */ 0); | ||
// Note: This must not be closed through the OutputStream. | ||
try (OutputStream pipeWriter = new FileOutputStream(arscFile)) { | ||
pipeWriter.write(contentBytes); | ||
|
||
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(arscFile)) { | ||
ResourcesLoader colorsLoader = new ResourcesLoader(); | ||
colorsLoader.addProvider( | ||
ResourcesProvider.loadFromTable(pfd, /* assetsProvider= */ null)); | ||
return colorsLoader; | ||
} | ||
} | ||
} finally { | ||
if (arscFile != null) { | ||
Os.close(arscFile); | ||
} | ||
} | ||
} catch (Exception e) { | ||
Log.e(TAG, "Failed to create the ColorResourcesTableCreator.", e); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
lib/java/com/google/android/material/color/HarmonizedColorAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (C) 2022 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.material.color; | ||
|
||
import com.google.android.material.R; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.StyleRes; | ||
|
||
/** | ||
* A class for specifying color attributes for harmonization, which would contain an int array of | ||
* color attributes, with the option to specify a custom theme overlay. | ||
*/ | ||
public final class HarmonizedColorAttributes { | ||
|
||
private final int[] attributes; | ||
@StyleRes private final int themeOverlay; | ||
|
||
private static final int[] HARMONIZED_MATERIAL_ATTRIBUTES = | ||
new int[] { | ||
R.attr.colorError, | ||
R.attr.colorOnError, | ||
R.attr.colorErrorContainer, | ||
R.attr.colorOnErrorContainer | ||
}; | ||
|
||
/** Create HarmonizedColorAttributes with an int array of color attributes. */ | ||
@NonNull | ||
public static HarmonizedColorAttributes create(@NonNull int[] attributes) { | ||
return new HarmonizedColorAttributes(attributes, 0); | ||
} | ||
|
||
/** | ||
* Create HarmonizedColorAttributes with a theme overlay, along with an int array of attributes in | ||
* the theme overlay. | ||
*/ | ||
@NonNull | ||
public static HarmonizedColorAttributes create( | ||
@NonNull int[] attributes, @StyleRes int themeOverlay) { | ||
return new HarmonizedColorAttributes(attributes, themeOverlay); | ||
} | ||
|
||
/** Create HarmonizedColorAttributes with Material default, with Error colors being harmonized. */ | ||
@NonNull | ||
public static HarmonizedColorAttributes createMaterialDefaults() { | ||
return create(HARMONIZED_MATERIAL_ATTRIBUTES, R.style.ThemeOverlay_Material3_HarmonizedColors); | ||
} | ||
|
||
private HarmonizedColorAttributes(@NonNull int[] attributes, @StyleRes int themeOverlay) { | ||
if (themeOverlay != 0 && attributes.length == 0) { | ||
throw new IllegalArgumentException( | ||
"Theme overlay should be used with the accompanying int[] attributes."); | ||
} | ||
this.attributes = attributes; | ||
this.themeOverlay = themeOverlay; | ||
} | ||
|
||
/** Returns the int array of color attributes for harmonization. */ | ||
@NonNull | ||
public int[] getAttributes() { | ||
return attributes; | ||
} | ||
|
||
/** Returns the custom theme overlay for harmonization, default is 0 if not specified. */ | ||
@StyleRes | ||
public int getThemeOverlay() { | ||
return themeOverlay; | ||
} | ||
} |
Oops, something went wrong.