/**
   * Last but not least, try to pull the Font Path from the Theme, which is defined.
   *
   * @param context Activity Context
   * @param styleAttrId Theme style id
   * @param subStyleAttrId the sub style from the theme to look up after the first style
   * @param attributeId if -1 returns null.
   * @return null if no theme or attribute defined.
   */
  static String pullFontPathFromTheme(
      Context context, int styleAttrId, int subStyleAttrId, int attributeId) {
    if (styleAttrId == -1 || attributeId == -1) return null;

    final Resources.Theme theme = context.getTheme();
    final TypedValue value = new TypedValue();

    theme.resolveAttribute(styleAttrId, value, true);
    int subStyleResId = -1;
    final TypedArray parentTypedArray =
        theme.obtainStyledAttributes(value.resourceId, new int[] {subStyleAttrId});
    try {
      subStyleResId = parentTypedArray.getResourceId(0, -1);
    } catch (Exception ignore) {
      // Failed for some reason.
      return null;
    } finally {
      parentTypedArray.recycle();
    }

    if (subStyleResId == -1) return null;
    final TypedArray subTypedArray =
        context.obtainStyledAttributes(subStyleResId, new int[] {attributeId});
    if (subTypedArray != null) {
      try {
        return subTypedArray.getString(0);
      } catch (Exception ignore) {
        // Failed for some reason.
        return null;
      } finally {
        subTypedArray.recycle();
      }
    }
    return null;
  }
  /**
   * Creates a new animation whose parameters come from the specified context and attributes set.
   *
   * @param res The resources
   * @param attrs The set of attributes holding the animation parameters
   * @param anim Null if this is a ValueAnimator, otherwise this is an ObjectAnimator
   */
  private static ValueAnimator loadAnimator(
      Context c,
      Resources res,
      Resources.Theme theme,
      AttributeSet attrs,
      ValueAnimator anim,
      float pathErrorScale)
      throws Resources.NotFoundException {

    TypedArray arrayAnimator = null;
    TypedArray arrayObjectAnimator = null;

    if (theme != null) {
      arrayAnimator = theme.obtainStyledAttributes(attrs, R.styleable.Animator, 0, 0);
    } else {
      arrayAnimator = res.obtainAttributes(attrs, R.styleable.Animator);
    }

    // If anim is not null, then it is an object animator.
    if (anim != null) {
      if (theme != null) {
        arrayObjectAnimator =
            theme.obtainStyledAttributes(attrs, R.styleable.PropertyAnimator, 0, 0);
      } else {
        arrayObjectAnimator = res.obtainAttributes(attrs, R.styleable.PropertyAnimator);
      }
    }

    if (anim == null) {
      anim = new ValueAnimator();
    }

    parseAnimatorFromTypeArray(anim, arrayAnimator, arrayObjectAnimator);

    final int resId = arrayAnimator.getResourceId(R.styleable.Animator_android_interpolator, 0);
    if (resId > 0) {
      anim.setInterpolator(AnimationUtils.loadInterpolator(c, resId));
    }

    arrayAnimator.recycle();
    if (arrayObjectAnimator != null) {
      arrayObjectAnimator.recycle();
    }

    return anim;
  }
  private static int[] getDefaultColors(Context context) {
    Resources.Theme theme = context.getTheme();
    TypedArray toolbarStyle = null;
    TypedArray textAppearances = null;
    TypedArray titleTextAppearance = null;
    TypedArray subtitleTextAppearance = null;

    try {
      toolbarStyle = theme.obtainStyledAttributes(new int[] {R.attr.toolbarStyle});
      int toolbarStyleResId = toolbarStyle.getResourceId(0, 0);
      textAppearances =
          theme.obtainStyledAttributes(
              toolbarStyleResId,
              new int[] {
                R.attr.titleTextAppearance, R.attr.subtitleTextAppearance,
              });
      int titleTextAppearanceResId = textAppearances.getResourceId(0, 0);
      int subtitleTextAppearanceResId = textAppearances.getResourceId(1, 0);

      titleTextAppearance =
          theme.obtainStyledAttributes(
              titleTextAppearanceResId, new int[] {android.R.attr.textColor});
      subtitleTextAppearance =
          theme.obtainStyledAttributes(
              subtitleTextAppearanceResId, new int[] {android.R.attr.textColor});

      int titleTextColor = titleTextAppearance.getColor(0, Color.BLACK);
      int subtitleTextColor = subtitleTextAppearance.getColor(0, Color.BLACK);

      return new int[] {titleTextColor, subtitleTextColor};
    } finally {
      recycleQuietly(toolbarStyle);
      recycleQuietly(textAppearances);
      recycleQuietly(titleTextAppearance);
      recycleQuietly(subtitleTextAppearance);
    }
  }
예제 #4
0
 /**
  * Get a color value from a theme attribute.
  *
  * @param context used for getting the color.
  * @param attribute theme attribute.
  * @param defaultColor default to use.
  * @return color value
  */
 public static int getThemeColor(Context context, int attribute, int defaultColor) {
   int themeColor = 0;
   String packageName = context.getPackageName();
   try {
     Context packageContext = context.createPackageContext(packageName, 0);
     ApplicationInfo applicationInfo =
         context.getPackageManager().getApplicationInfo(packageName, 0);
     packageContext.setTheme(applicationInfo.theme);
     Resources.Theme theme = packageContext.getTheme();
     TypedArray ta = theme.obtainStyledAttributes(new int[] {attribute});
     themeColor = ta.getColor(0, defaultColor);
     ta.recycle();
   } catch (PackageManager.NameNotFoundException e) {
     e.printStackTrace();
   }
   return themeColor;
 }
  /**
   * Last but not least, try to pull the Font Path from the Theme, which is defined.
   *
   * @param context Activity Context
   * @param styleAttrId Theme style id
   * @param attributeId if -1 returns null.
   * @return null if no theme or attribute defined.
   */
  static String pullFontPathFromTheme(Context context, int styleAttrId, int attributeId) {
    if (styleAttrId == -1 || attributeId == -1) return null;

    final Resources.Theme theme = context.getTheme();
    final TypedValue value = new TypedValue();

    theme.resolveAttribute(styleAttrId, value, true);
    final TypedArray typedArray =
        theme.obtainStyledAttributes(value.resourceId, new int[] {attributeId});
    try {
      String font = typedArray.getString(0);
      return font;
    } catch (Exception ignore) {
      // Failed for some reason.
      return null;
    } finally {
      typedArray.recycle();
    }
  }
예제 #6
0
 public void themeObtainTypedArrayAndLeak(Resources.Theme theme) {
   TypedArray array = theme.obtainStyledAttributes(new int[] {});
   ignore(array);
 }