Skip to content

Commit

Permalink
obtain attributeset values without hard coding
Browse files Browse the repository at this point in the history
  • Loading branch information
xyczero committed Nov 6, 2017
1 parent 3f25fda commit a80ca4c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
*/
public class AppCompatBackgroundHelper extends AppCompatBaseHelper {

private static final int[] ATTR = {
android.R.attr.background,
R.attr.backgroundTint,
R.attr.backgroundTintMode
};

private TintInfo mBackgroundTintInfo;

private int mBackgroundResId;
Expand All @@ -58,15 +52,15 @@ public AppCompatBackgroundHelper(View view, TintManager tintManager) {
@Override
void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
initPadding();
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTR, defStyleAttr, 0);
if (array.hasValue(1)) {
mBackgroundTintResId = array.getResourceId(1, 0);
if (array.hasValue(2)) {
setSupportBackgroundTintMode(DrawableUtils.parseTintMode(array.getInt(2, 0), null));
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintViewBackgroundHelper, defStyleAttr, 0);
if (array.hasValue(R.styleable.TintViewBackgroundHelper_backgroundTint)) {
mBackgroundTintResId = array.getResourceId(R.styleable.TintViewBackgroundHelper_backgroundTint, 0);
if (array.hasValue(R.styleable.TintViewBackgroundHelper_backgroundTintMode)) {
setSupportBackgroundTintMode(DrawableUtils.parseTintMode(array.getInt(R.styleable.TintViewBackgroundHelper_backgroundTintMode, 0), null));
}
setSupportBackgroundTint(mBackgroundTintResId);
} else {
Drawable drawable = mTintManager.getDrawable(mBackgroundResId = array.getResourceId(0, 0));
Drawable drawable = mTintManager.getDrawable(mBackgroundResId = array.getResourceId(R.styleable.TintViewBackgroundHelper_android_background, 0));
if (drawable != null) {
setBackgroundDrawable(drawable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
* @time 15/11/23
*/
public class AppCompatCompoundButtonHelper extends AppCompatBaseHelper {
private static final int[] ATTRS = {
android.R.attr.button,
R.attr.compoundButtonTint,
R.attr.compoundButtonTintMode
};

private TintInfo mCompoundButtonTintInfo;
private int mCompoundButtonResId;
Expand All @@ -53,15 +48,15 @@ public AppCompatCompoundButtonHelper(CompoundButton view, TintManager tintManage
@SuppressWarnings("ResourceType")
@Override
void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTRS, defStyleAttr, 0);
if (array.hasValue(1)) {
mCompoundButtonTintResId = array.getResourceId(1, 0);
if (array.hasValue(2)) {
setSupportButtonDrawableTintMode(DrawableUtils.parseTintMode(array.getInt(2, 0), null));
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintCompoundButtonHelper, defStyleAttr, 0);
if (array.hasValue(R.styleable.TintCompoundButtonHelper_compoundButtonTint)) {
mCompoundButtonTintResId = array.getResourceId(R.styleable.TintCompoundButtonHelper_compoundButtonTint, 0);
if (array.hasValue(R.styleable.TintCompoundButtonHelper_compoundButtonTintMode)) {
setSupportButtonDrawableTintMode(DrawableUtils.parseTintMode(array.getInt(R.styleable.TintCompoundButtonHelper_compoundButtonTintMode, 0), null));
}
setSupportButtonDrawableTint(mCompoundButtonTintResId);
} else {
Drawable drawable = mTintManager.getDrawable(mCompoundButtonResId = array.getResourceId(0, 0));
Drawable drawable = mTintManager.getDrawable(mCompoundButtonResId = array.getResourceId(R.styleable.TintCompoundButtonHelper_android_button, 0));
if (drawable != null) {
setButtonDrawable(drawable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@
*/
public class AppCompatCompoundDrawableHelper extends AppCompatBaseHelper {

private static final int[] ATTR = {
R.attr.drawableLeftTint,
R.attr.drawableTopTint,
R.attr.drawableRightTint,
R.attr.drawableBottomTint,
R.attr.drawableLeftTintMode,
R.attr.drawableTopTintMode,
R.attr.drawableRightTintMode,
R.attr.drawableBottomTintMode
};

private TintInfo[] mCompoundDrawableTintInfos = new TintInfo[4];

private int[] mCompoundDrawableResIds = new int[4];
Expand All @@ -62,19 +51,27 @@ public AppCompatCompoundDrawableHelper(TextView view, TintManager tintManager) {
@Override
void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
Context context = mView.getContext();
TypedArray a = context.obtainStyledAttributes(attrs, ATTR, defStyleAttr, 0);
for (int tintIndex = 0; tintIndex < 4; tintIndex++) {
int modeIndex = tintIndex + 4;
mCompoundDrawableResIds[tintIndex] = a.getResourceId(tintIndex, 0);
mCompoundDrawableTintResIds[tintIndex] = a.getResourceId(tintIndex, 0);
if (a.hasValue(modeIndex)) {
mCompoundDrawableTintModes[tintIndex] = DrawableUtils.parseTintMode(a.getInt(modeIndex, 0), null);
}
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintCompoundDrawableHelper, defStyleAttr, 0);
mCompoundDrawableResIds[0] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_android_drawableLeft, 0);
mCompoundDrawableTintResIds[0] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_drawableLeftTint, 0);
if (a.hasValue(R.styleable.TintCompoundDrawableHelper_drawableLeftTintMode)) {
mCompoundDrawableTintModes[0] = DrawableUtils.parseTintMode(a.getInt(R.styleable.TintCompoundDrawableHelper_drawableLeftTintMode, 0), null);
}
mCompoundDrawableResIds[1] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_android_drawableTop, 0);
mCompoundDrawableTintResIds[1] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_drawableTopTint, 0);
if (a.hasValue(R.styleable.TintCompoundDrawableHelper_drawableTopTintMode)) {
mCompoundDrawableTintModes[1] = DrawableUtils.parseTintMode(a.getInt(R.styleable.TintCompoundDrawableHelper_drawableTopTintMode, 0), null);
}
mCompoundDrawableResIds[2] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_android_drawableRight, 0);
mCompoundDrawableTintResIds[2] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_drawableRightTint, 0);
if (a.hasValue(R.styleable.TintCompoundDrawableHelper_drawableRightTintMode)) {
mCompoundDrawableTintModes[2] = DrawableUtils.parseTintMode(a.getInt(R.styleable.TintCompoundDrawableHelper_drawableRightTintMode, 0), null);
}
mCompoundDrawableResIds[3] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_android_drawableBottom, 0);
mCompoundDrawableTintResIds[3] = a.getResourceId(R.styleable.TintCompoundDrawableHelper_drawableBottomTint, 0);
if (a.hasValue(R.styleable.TintCompoundDrawableHelper_drawableBottomTintMode)) {
mCompoundDrawableTintModes[3] = DrawableUtils.parseTintMode(a.getInt(R.styleable.TintCompoundDrawableHelper_drawableBottomTintMode, 0), null);
}
mCompoundDrawableResIds[0] = ThemeUtils.getThemeAttrId(context, attrs, android.R.attr.drawableLeft);
mCompoundDrawableResIds[1] = ThemeUtils.getThemeAttrId(context, attrs, android.R.attr.drawableTop);
mCompoundDrawableResIds[2] = ThemeUtils.getThemeAttrId(context, attrs, android.R.attr.drawableRight);
mCompoundDrawableResIds[3] = ThemeUtils.getThemeAttrId(context, attrs, android.R.attr.drawableBottom);
a.recycle();

setCompoundDrawablesWithIntrinsicBounds(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
* @time 16/4/7
*/
public class AppCompatForegroundHelper extends AppCompatBaseHelper {
private static final int[] ATTR = {
android.R.attr.foreground,
R.attr.foregroundTint,
R.attr.foregroundTintMode
};

private TintInfo mForegroundTintInfo;

Expand All @@ -54,15 +49,15 @@ public AppCompatForegroundHelper(View view, TintManager tintManager) {
@SuppressWarnings("ResourceType")
@Override
void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTR, defStyleAttr, 0);
if (array.hasValue(1)) {
mForegroundTintResId = array.getResourceId(1, 0);
if (array.hasValue(2)) {
setSupportForegroundTintMode(DrawableUtils.parseTintMode(array.getInt(2, 0), null));
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintViewForegroundHelper, defStyleAttr, 0);
if (array.hasValue(R.styleable.TintViewForegroundHelper_foregroundTint)) {
mForegroundTintResId = array.getResourceId(R.styleable.TintViewForegroundHelper_foregroundTint, 0);
if (array.hasValue(R.styleable.TintViewForegroundHelper_foregroundTintMode)) {
setSupportForegroundTintMode(DrawableUtils.parseTintMode(array.getInt(R.styleable.TintViewForegroundHelper_foregroundTintMode, 0), null));
}
setSupportForegroundTint(mForegroundTintResId);
} else {
Drawable drawable = mTintManager.getDrawable(mForegroundResId = array.getResourceId(0, 0));
Drawable drawable = mTintManager.getDrawable(mForegroundResId = array.getResourceId(R.styleable.TintViewForegroundHelper_android_foreground, 0));
if (drawable != null) {
setForegroundDrawable(drawable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@
* @time 15/11/15
*/
public class AppCompatImageHelper extends AppCompatBaseHelper {
public static final int[] ATTRS = {
android.R.attr.src,
R.attr.srcCompat,
R.attr.imageTint,
R.attr.imageTintMode
};

private TintInfo mImageTintInfo;
private int mImageResId;
Expand All @@ -53,23 +47,24 @@ public AppCompatImageHelper(View view, TintManager tintManager) {
@SuppressWarnings("ResourceType")
@Override
void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTRS, defStyleAttr, 0);
Drawable image = mTintManager.getDrawable(mImageResId = array.getResourceId(1, 0));
if (image != null) {
setImageDrawable(image);
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintImageHelper, defStyleAttr, 0);
// first resolve srcCompat due to not extending by AppCompatImageView
if (((ImageView) mView).getDrawable() == null) {
Drawable image = mTintManager.getDrawable(mImageResId = array.getResourceId(R.styleable.TintImageHelper_srcCompat, 0));
if (image != null) {
setImageDrawable(image);
}
}
if (array.hasValue(2)) {
mImageTintResId = array.getResourceId(2, 0);
if (array.hasValue(3)) {
setSupportImageTintMode(DrawableUtils.parseTintMode(array.getInt(3, 0), null));
if (array.hasValue(R.styleable.TintImageHelper_imageTint)) {
mImageTintResId = array.getResourceId(R.styleable.TintImageHelper_imageTint, 0);
if (array.hasValue(R.styleable.TintImageHelper_imageTintMode)) {
setSupportImageTintMode(DrawableUtils.parseTintMode(array.getInt(R.styleable.TintImageHelper_imageTintMode, 0), null));
}
setSupportImageTint(mImageTintResId);
} else {
if (image == null) {
image = mTintManager.getDrawable(mImageResId = array.getResourceId(0, 0));
if (image != null) {
setImageDrawable(image);
}
} else if (mImageResId == 0) {
Drawable image = mTintManager.getDrawable(mImageResId = array.getResourceId(R.styleable.TintImageHelper_android_src, 0));
if (image != null) {
setImageDrawable(image);
}
}
array.recycle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
* @time 16/2/4
*/
public class AppCompatProgressBarHelper extends AppCompatBaseHelper {
private static final int ATTR[] = new int[]{
R.attr.progressTint,
R.attr.progressIndeterminateTint
};

private int mProgressTintResId;
private int mIndeterminateTintResId;
Expand All @@ -53,14 +49,14 @@ public AppCompatProgressBarHelper(View view, TintManager tintManager) {
@SuppressWarnings("ResourceType")
@Override
void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTR, defStyleAttr, 0);
if (array.hasValue(0)) {
mProgressTintResId = array.getResourceId(0, 0);
setSupportProgressTint(array.getColorStateList(0));
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintProgressBarHelper, defStyleAttr, 0);
if (array.hasValue(R.styleable.TintProgressBarHelper_progressTint)) {
mProgressTintResId = array.getResourceId(R.styleable.TintProgressBarHelper_progressTint, 0);
setSupportProgressTint(array.getColorStateList(R.styleable.TintProgressBarHelper_progressTint));
}
if (array.hasValue(1)) {
mIndeterminateTintResId = array.getResourceId(1, 0);
setSupportIndeterminateTint(array.getColorStateList(1));
if (array.hasValue(R.styleable.TintProgressBarHelper_progressIndeterminateTint)) {
mIndeterminateTintResId = array.getResourceId(R.styleable.TintProgressBarHelper_progressIndeterminateTint, 0);
setSupportIndeterminateTint(array.getColorStateList(R.styleable.TintProgressBarHelper_progressIndeterminateTint));
}
array.recycle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public class AppCompatTextHelper extends AppCompatBaseHelper {
//If writing like this:
//int[] ATTRS = { R.attr.tintText, android.R.attr.textColor, android.R.attr.textColorLink, ...};
//we can't get textColor value when api is below 20;
private static final int[] ATTRS = {
android.R.attr.textColor,
android.R.attr.textColorLink,
android.R.attr.textAppearance,
};

private int mTextColorId;
private int mTextLinkColorId;
Expand All @@ -55,17 +50,17 @@ public AppCompatTextHelper(View view, TintManager tintManager) {
@SuppressWarnings("ResourceType")
@Override
void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, ATTRS, defStyleAttr, 0);
TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.TintTextHelper , defStyleAttr, 0);

int textColorId = array.getResourceId(0, 0);
int textColorId = array.getResourceId(R.styleable.TintTextHelper_android_textColor, 0);
if (textColorId == 0) {
setTextAppearanceForTextColor(array.getResourceId(2, 0), false);
setTextAppearanceForTextColor(array.getResourceId(R.styleable.TintTextHelper_android_textAppearance, 0), false);
} else {
setTextColor(textColorId);
}

if (array.hasValue(1)) {
setLinkTextColor(array.getResourceId(1, 0));
if (array.hasValue(R.styleable.TintTextHelper_android_textColorLink)) {
setLinkTextColor(array.getResourceId(R.styleable.TintTextHelper_android_textColorLink, 0));
}
array.recycle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TintSwitchCompat(Context context, AttributeSet attrs, int defStyleAttr) {
}
TintManager tintManager = TintManager.get(context);
mThumbHelper = new AppCompatSwitchHelper(this, tintManager,
new int[]{android.R.attr.thumb, R.attr.thumbTint, R.attr.thumbTintMode},
R.styleable.TintSwitchThumb,
new AppCompatSwitchHelper.DrawableCallback() {
@Override
public void setDrawable(Drawable drawable) {
Expand All @@ -63,7 +63,7 @@ public Drawable getDrawable() {
mThumbHelper.loadFromAttribute(attrs, defStyleAttr);

mTrackHelper = new AppCompatSwitchHelper(this, tintManager,
new int[]{R.attr.track, R.attr.trackTint, R.attr.trackTintMode},
R.styleable.TintSwitchTrack,
new AppCompatSwitchHelper.DrawableCallback() {
@Override
public void setDrawable(Drawable drawable) {
Expand Down
Loading

0 comments on commit a80ca4c

Please sign in to comment.