Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static boolean isAttributeInAppCompatv7(String name) {
- switch (name) {
- case "colorPrimary":
- case "colorPrimaryDark":
- case "colorAccent":
- case "colorControlNormal":
- case "colorControlActivated":
- case "colorControlHighlight":
- case "colorSwitchThumbNormal":
- return true;
- default:
- return false;
- }
- }
- private static int fromAppCompat(Theme theme, String name) {
- try {
- String className = App.context().getPackageName() + ".R$attr";
- Class<?> attr = Class.forName(className);
- int attribute = attr.getField(name).getInt(null);
- TypedArray array = theme.obtainStyledAttributes(new int[] {
- attribute
- });
- try {
- int color = array.getColor(0, Integer.MAX_VALUE);
- if (color == Integer.MAX_VALUE) {
- throw new Resources.NotFoundException("attribute " + name
- + " is not defined or not a resource");
- }
- return color;
- } finally {
- array.recycle();
- }
- } catch (Exception e) {
- throw new Resources.NotFoundException(e.getLocalizedMessage());
- }
- }
- private static int getColorFromAttr(Theme theme, String name, int defaultColor) {
- try {
- if (isAttributeInAppCompatv7(name)) {
- try {
- return fromAppCompat(theme, name);
- } catch (Resources.NotFoundException ignored) {
- }
- }
- Field field = android.R.attr.class.getField(name);
- int id = field.getInt(null);
- TypedArray array = theme.obtainStyledAttributes(new int[] {
- id
- });
- try {
- return array.getColor(0, defaultColor);
- } finally {
- array.recycle();
- }
- } catch (Exception e) {
- L.d(TAG, "Failed getting %s from android.R.attr", name);
- return defaultColor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement