164 lines
6.4 KiB
Java
164 lines
6.4 KiB
Java
package androidx.appcompat.widget;
|
|
|
|
import android.content.Context;
|
|
import android.content.res.ColorStateList;
|
|
import android.graphics.PorterDuff;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.os.Build;
|
|
import android.util.AttributeSet;
|
|
import android.view.View;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.appcompat.R;
|
|
import androidx.core.view.ViewCompat;
|
|
public class AppCompatBackgroundHelper {
|
|
private int mBackgroundResId = -1;
|
|
private TintInfo mBackgroundTint;
|
|
private final AppCompatDrawableManager mDrawableManager;
|
|
private TintInfo mInternalBackgroundTint;
|
|
private TintInfo mTmpInfo;
|
|
@NonNull
|
|
private final View mView;
|
|
|
|
public AppCompatBackgroundHelper(@NonNull View view) {
|
|
this.mView = view;
|
|
this.mDrawableManager = AppCompatDrawableManager.get();
|
|
}
|
|
|
|
private boolean applyFrameworkTintUsingColorFilter(@NonNull Drawable drawable) {
|
|
if (this.mTmpInfo == null) {
|
|
this.mTmpInfo = new TintInfo();
|
|
}
|
|
TintInfo tintInfo = this.mTmpInfo;
|
|
tintInfo.clear();
|
|
ColorStateList backgroundTintList = ViewCompat.getBackgroundTintList(this.mView);
|
|
if (backgroundTintList != null) {
|
|
tintInfo.mHasTintList = true;
|
|
tintInfo.mTintList = backgroundTintList;
|
|
}
|
|
PorterDuff.Mode backgroundTintMode = ViewCompat.getBackgroundTintMode(this.mView);
|
|
if (backgroundTintMode != null) {
|
|
tintInfo.mHasTintMode = true;
|
|
tintInfo.mTintMode = backgroundTintMode;
|
|
}
|
|
if (!tintInfo.mHasTintList && !tintInfo.mHasTintMode) {
|
|
return false;
|
|
}
|
|
AppCompatDrawableManager.tintDrawable(drawable, tintInfo, this.mView.getDrawableState());
|
|
return true;
|
|
}
|
|
|
|
private boolean shouldApplyFrameworkTintUsingColorFilter() {
|
|
int i = Build.VERSION.SDK_INT;
|
|
return i > 21 ? this.mInternalBackgroundTint != null : i == 21;
|
|
}
|
|
|
|
public void applySupportBackgroundTint() {
|
|
Drawable background = this.mView.getBackground();
|
|
if (background == null) {
|
|
return;
|
|
}
|
|
if (!shouldApplyFrameworkTintUsingColorFilter() || !applyFrameworkTintUsingColorFilter(background)) {
|
|
TintInfo tintInfo = this.mBackgroundTint;
|
|
if (tintInfo != null) {
|
|
AppCompatDrawableManager.tintDrawable(background, tintInfo, this.mView.getDrawableState());
|
|
return;
|
|
}
|
|
TintInfo tintInfo2 = this.mInternalBackgroundTint;
|
|
if (tintInfo2 != null) {
|
|
AppCompatDrawableManager.tintDrawable(background, tintInfo2, this.mView.getDrawableState());
|
|
}
|
|
}
|
|
}
|
|
|
|
public ColorStateList getSupportBackgroundTintList() {
|
|
TintInfo tintInfo = this.mBackgroundTint;
|
|
if (tintInfo != null) {
|
|
return tintInfo.mTintList;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public PorterDuff.Mode getSupportBackgroundTintMode() {
|
|
TintInfo tintInfo = this.mBackgroundTint;
|
|
if (tintInfo != null) {
|
|
return tintInfo.mTintMode;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void loadFromAttributes(@Nullable AttributeSet attributeSet, int i) {
|
|
Context context = this.mView.getContext();
|
|
int[] iArr = R.styleable.ViewBackgroundHelper;
|
|
TintTypedArray obtainStyledAttributes = TintTypedArray.obtainStyledAttributes(context, attributeSet, iArr, i, 0);
|
|
View view = this.mView;
|
|
ViewCompat.saveAttributeDataForStyleable(view, view.getContext(), iArr, attributeSet, obtainStyledAttributes.getWrappedTypeArray(), i, 0);
|
|
try {
|
|
int i2 = R.styleable.ViewBackgroundHelper_android_background;
|
|
if (obtainStyledAttributes.hasValue(i2)) {
|
|
this.mBackgroundResId = obtainStyledAttributes.getResourceId(i2, -1);
|
|
ColorStateList tintList = this.mDrawableManager.getTintList(this.mView.getContext(), this.mBackgroundResId);
|
|
if (tintList != null) {
|
|
setInternalBackgroundTint(tintList);
|
|
}
|
|
}
|
|
int i3 = R.styleable.ViewBackgroundHelper_backgroundTint;
|
|
if (obtainStyledAttributes.hasValue(i3)) {
|
|
ViewCompat.setBackgroundTintList(this.mView, obtainStyledAttributes.getColorStateList(i3));
|
|
}
|
|
int i4 = R.styleable.ViewBackgroundHelper_backgroundTintMode;
|
|
if (obtainStyledAttributes.hasValue(i4)) {
|
|
ViewCompat.setBackgroundTintMode(this.mView, DrawableUtils.parseTintMode(obtainStyledAttributes.getInt(i4, -1), null));
|
|
}
|
|
} finally {
|
|
obtainStyledAttributes.recycle();
|
|
}
|
|
}
|
|
|
|
public void onSetBackgroundDrawable(Drawable drawable) {
|
|
this.mBackgroundResId = -1;
|
|
setInternalBackgroundTint(null);
|
|
applySupportBackgroundTint();
|
|
}
|
|
|
|
public void onSetBackgroundResource(int i) {
|
|
this.mBackgroundResId = i;
|
|
AppCompatDrawableManager appCompatDrawableManager = this.mDrawableManager;
|
|
setInternalBackgroundTint(appCompatDrawableManager != null ? appCompatDrawableManager.getTintList(this.mView.getContext(), i) : null);
|
|
applySupportBackgroundTint();
|
|
}
|
|
|
|
public void setInternalBackgroundTint(ColorStateList colorStateList) {
|
|
if (colorStateList != null) {
|
|
if (this.mInternalBackgroundTint == null) {
|
|
this.mInternalBackgroundTint = new TintInfo();
|
|
}
|
|
TintInfo tintInfo = this.mInternalBackgroundTint;
|
|
tintInfo.mTintList = colorStateList;
|
|
tintInfo.mHasTintList = true;
|
|
} else {
|
|
this.mInternalBackgroundTint = null;
|
|
}
|
|
applySupportBackgroundTint();
|
|
}
|
|
|
|
public void setSupportBackgroundTintList(ColorStateList colorStateList) {
|
|
if (this.mBackgroundTint == null) {
|
|
this.mBackgroundTint = new TintInfo();
|
|
}
|
|
TintInfo tintInfo = this.mBackgroundTint;
|
|
tintInfo.mTintList = colorStateList;
|
|
tintInfo.mHasTintList = true;
|
|
applySupportBackgroundTint();
|
|
}
|
|
|
|
public void setSupportBackgroundTintMode(PorterDuff.Mode mode) {
|
|
if (this.mBackgroundTint == null) {
|
|
this.mBackgroundTint = new TintInfo();
|
|
}
|
|
TintInfo tintInfo = this.mBackgroundTint;
|
|
tintInfo.mTintMode = mode;
|
|
tintInfo.mHasTintMode = true;
|
|
applySupportBackgroundTint();
|
|
}
|
|
}
|