Advertisement
Guest User

Untitled

a guest
May 25th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1.     private static boolean isAttributeInAppCompatv7(String name) {
  2.         switch (name) {
  3.         case "colorPrimary":
  4.         case "colorPrimaryDark":
  5.         case "colorAccent":
  6.         case "colorControlNormal":
  7.         case "colorControlActivated":
  8.         case "colorControlHighlight":
  9.         case "colorSwitchThumbNormal":
  10.             return true;
  11.         default:
  12.             return false;
  13.         }
  14.     }
  15.  
  16.     private static int fromAppCompat(Theme theme, String name) {
  17.         try {
  18.             String className = App.context().getPackageName() + ".R$attr";
  19.             Class<?> attr = Class.forName(className);
  20.             int attribute = attr.getField(name).getInt(null);
  21.             TypedArray array = theme.obtainStyledAttributes(new int[] {
  22.                 attribute
  23.             });
  24.             try {
  25.                 int color = array.getColor(0, Integer.MAX_VALUE);
  26.                 if (color == Integer.MAX_VALUE) {
  27.                     throw new Resources.NotFoundException("attribute " + name
  28.                             + " is not defined or not a resource");
  29.                 }
  30.                 return color;
  31.             } finally {
  32.                 array.recycle();
  33.             }
  34.         } catch (Exception e) {
  35.             throw new Resources.NotFoundException(e.getLocalizedMessage());
  36.         }
  37.     }
  38.  
  39.     private static int getColorFromAttr(Theme theme, String name, int defaultColor) {
  40.         try {
  41.             if (isAttributeInAppCompatv7(name)) {
  42.                 try {
  43.                     return fromAppCompat(theme, name);
  44.                 } catch (Resources.NotFoundException ignored) {
  45.                 }
  46.             }
  47.             Field field = android.R.attr.class.getField(name);
  48.             int id = field.getInt(null);
  49.             TypedArray array = theme.obtainStyledAttributes(new int[] {
  50.                 id
  51.             });
  52.             try {
  53.                 return array.getColor(0, defaultColor);
  54.             } finally {
  55.                 array.recycle();
  56.             }
  57.         } catch (Exception e) {
  58.             L.d(TAG, "Failed getting %s from android.R.attr", name);
  59.             return defaultColor;
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement