|
|
|
|
@@ -6,17 +6,16 @@ import android.content.res.TypedArray;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.graphics.drawable.StateListDrawable;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
import androidx.core.graphics.drawable.DrawableCompat;
|
|
|
|
|
import androidx.core.view.ViewCompat;
|
|
|
|
|
import com.knightboost.lancet.api.Origin;
|
|
|
|
|
import com.knightboost.lancet.api.Scope;
|
|
|
|
|
import com.knightboost.lancet.api.annotations.Group;
|
|
|
|
|
@@ -32,37 +31,28 @@ import com.mogo.launcher.R;
|
|
|
|
|
@Group("view_pressed_state")
|
|
|
|
|
public class ViewPressedStateLancet {
|
|
|
|
|
|
|
|
|
|
public static class OnClickWrapper implements View.OnClickListener {
|
|
|
|
|
|
|
|
|
|
private final View.OnClickListener delegate;
|
|
|
|
|
|
|
|
|
|
public OnClickWrapper(View.OnClickListener delegate) {
|
|
|
|
|
this.delegate = delegate;
|
|
|
|
|
public static boolean isHasBackground(View view) {
|
|
|
|
|
if (view instanceof ImageView) {
|
|
|
|
|
ImageView image = (ImageView) view;
|
|
|
|
|
return image.getBackground() != null || image.getDrawable() != null;
|
|
|
|
|
}
|
|
|
|
|
if (view instanceof TextView) {
|
|
|
|
|
TextView text = (TextView) view;
|
|
|
|
|
return text.getBackground() != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
Object tag = v.getTag(R.id.click_pressed_attr_id);
|
|
|
|
|
checkSetBgIfNeed(null, v, tag == null ? null : (AttributeSet) tag);
|
|
|
|
|
this.delegate.onClick(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class OnLongClickWrapper implements View.OnLongClickListener {
|
|
|
|
|
|
|
|
|
|
private final View.OnLongClickListener delegate;
|
|
|
|
|
|
|
|
|
|
public OnLongClickWrapper(View.OnLongClickListener delegate) {
|
|
|
|
|
this.delegate = delegate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
|
Object tag = v.getTag(R.id.click_pressed_attr_id);
|
|
|
|
|
checkSetBgIfNeed(null, v, tag == null ? null : (AttributeSet) tag);
|
|
|
|
|
return delegate.onLongClick(v);
|
|
|
|
|
if (view instanceof ViewGroup) {
|
|
|
|
|
ViewGroup group = (ViewGroup) view;
|
|
|
|
|
int count = group.getChildCount();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
View child = group.getChildAt(i);
|
|
|
|
|
if (isHasBackground(child)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return view.getBackground() != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.view.View", scope = Scope.ALL)
|
|
|
|
|
@@ -76,9 +66,72 @@ public class ViewPressedStateLancet {
|
|
|
|
|
view.setOnClickListener(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
view.setOnClickListener(new OnClickWrapper(listener));
|
|
|
|
|
if (isHasBackground(view)) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_id);
|
|
|
|
|
checkSetBgIfNeed(null, view, tag == null ? null : (AttributeSet) tag, true);
|
|
|
|
|
} else if (view instanceof TextView) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_id);
|
|
|
|
|
checkSetBgIfNeed(null, (TextView) view, tag == null ? null : (AttributeSet) tag, true);
|
|
|
|
|
}
|
|
|
|
|
view.setOnClickListener(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.view.View", scope = Scope.ALL)
|
|
|
|
|
@TargetMethod(methodName = "getBackground")
|
|
|
|
|
@ReplaceInvoke
|
|
|
|
|
public static Drawable getBackgroundDrawable(View view) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_replaced);
|
|
|
|
|
Drawable old = view.getBackground();
|
|
|
|
|
if (tag == null) {
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
return (Drawable) tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.widget.ImageView", scope = Scope.ALL)
|
|
|
|
|
@TargetMethod(methodName = "getDrawable")
|
|
|
|
|
@ReplaceInvoke
|
|
|
|
|
public static Drawable getDrawable(ImageView view) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_replaced);
|
|
|
|
|
Drawable old = view.getDrawable();
|
|
|
|
|
if (tag == null) {
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
return (Drawable) tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.widget.TextView", scope = Scope.ALL)
|
|
|
|
|
@TargetMethod(methodName = "getTextColors")
|
|
|
|
|
@ReplaceInvoke
|
|
|
|
|
public static ColorStateList getTextColors(TextView view) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_replaced_color);
|
|
|
|
|
ColorStateList old = view.getTextColors();
|
|
|
|
|
if (tag == null) {
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
return (ColorStateList) tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.widget.CompoundButton", scope = Scope.ALL)
|
|
|
|
|
@TargetMethod(methodName = "setOnCheckedChangeListener")
|
|
|
|
|
@ReplaceInvoke
|
|
|
|
|
public static void setOnCheckedChangeListener(CompoundButton view, CompoundButton.OnCheckedChangeListener listener) {
|
|
|
|
|
if (view == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (listener == null) {
|
|
|
|
|
view.setOnCheckedChangeListener(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (isHasBackground(view)) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_id);
|
|
|
|
|
checkSetBgIfNeed(null, view, tag == null ? null : (AttributeSet) tag, true);
|
|
|
|
|
}
|
|
|
|
|
view.setOnCheckedChangeListener(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.view.View", scope = Scope.ALL)
|
|
|
|
|
@TargetMethod(methodName = "setOnLongClickListener")
|
|
|
|
|
@ReplaceInvoke
|
|
|
|
|
@@ -90,9 +143,130 @@ public class ViewPressedStateLancet {
|
|
|
|
|
view.setOnLongClickListener(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
view.setOnLongClickListener(new OnLongClickWrapper(listener));
|
|
|
|
|
if (isHasBackground(view)) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_id);
|
|
|
|
|
checkSetBgIfNeed(null, view, tag == null ? null : (AttributeSet) tag, true);
|
|
|
|
|
} else if (view instanceof TextView) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_id);
|
|
|
|
|
checkSetBgIfNeed(null, (TextView) view, tag == null ? null : (AttributeSet) tag, true);
|
|
|
|
|
}
|
|
|
|
|
view.setOnLongClickListener(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.view.View", scope = Scope.ALL)
|
|
|
|
|
@TargetMethod(methodName = "setBackground")
|
|
|
|
|
@ReplaceInvoke
|
|
|
|
|
public static void setBackground(View view, Drawable drawable) {
|
|
|
|
|
if (view == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!view.isLongClickable() && !view.isClickable()) {
|
|
|
|
|
view.setBackground(drawable);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
float alpha = getAlpha(view);
|
|
|
|
|
if (alpha >= 0) {
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(drawable, alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_replaced, drawable);
|
|
|
|
|
int paddingLeft = view.getPaddingLeft();
|
|
|
|
|
int paddingRight = view.getPaddingRight();
|
|
|
|
|
int paddingTop = view.getPaddingTop();
|
|
|
|
|
int paddingBottom = view.getPaddingBottom();
|
|
|
|
|
view.setBackground(replaced);
|
|
|
|
|
view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
|
|
|
|
|
} else {
|
|
|
|
|
view.setBackground(drawable);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
view.setBackground(drawable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.widget.ImageView", scope = Scope.ALL)
|
|
|
|
|
@TargetMethod(methodName = "setImageDrawable")
|
|
|
|
|
@ReplaceInvoke
|
|
|
|
|
public static void setImageViewDrawable(ImageView view, Drawable drawable) {
|
|
|
|
|
if (view == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!view.isLongClickable() && !view.isClickable()) {
|
|
|
|
|
view.setImageDrawable(drawable);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
float alpha = getAlpha(view);
|
|
|
|
|
if (alpha >= 0.0) {
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(drawable, alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_replaced, drawable);
|
|
|
|
|
int paddingLeft = view.getPaddingLeft();
|
|
|
|
|
int paddingRight = view.getPaddingRight();
|
|
|
|
|
int paddingTop = view.getPaddingTop();
|
|
|
|
|
int paddingBottom = view.getPaddingBottom();
|
|
|
|
|
view.setImageDrawable(replaced);
|
|
|
|
|
view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
|
|
|
|
|
} else {
|
|
|
|
|
view.setImageDrawable(drawable);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
view.setImageDrawable(drawable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetClass(value = "android.widget.ImageView", scope = Scope.ALL)
|
|
|
|
|
@TargetMethod(methodName = "setImageResource")
|
|
|
|
|
@ReplaceInvoke
|
|
|
|
|
public static void setImageViewResource(ImageView view, int resId) {
|
|
|
|
|
if (view == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!view.isLongClickable() && !view.isClickable()) {
|
|
|
|
|
view.setImageResource(resId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
float alpha = getAlpha(view);
|
|
|
|
|
if (alpha >= 0.0) {
|
|
|
|
|
Drawable drawable = ContextCompat.getDrawable(view.getContext(), resId);
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(drawable, alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_replaced, drawable);
|
|
|
|
|
int paddingLeft = view.getPaddingLeft();
|
|
|
|
|
int paddingRight = view.getPaddingRight();
|
|
|
|
|
int paddingTop = view.getPaddingTop();
|
|
|
|
|
int paddingBottom = view.getPaddingBottom();
|
|
|
|
|
view.setImageDrawable(replaced);
|
|
|
|
|
view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
|
|
|
|
|
} else {
|
|
|
|
|
view.setImageDrawable(drawable);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
view.setImageResource(resId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float getAlpha(View view) {
|
|
|
|
|
Object tag = view.getTag(R.id.click_pressed_attr_enabled);
|
|
|
|
|
if (tag != null && !((Boolean) tag)) {
|
|
|
|
|
return -1.0f;
|
|
|
|
|
}
|
|
|
|
|
tag = view.getTag(R.id.click_pressed_attr_id);
|
|
|
|
|
if (tag instanceof AttributeSet) {
|
|
|
|
|
AttributeSet attr = (AttributeSet) tag;
|
|
|
|
|
TypedArray array = null;
|
|
|
|
|
try {
|
|
|
|
|
array = view.getContext().obtainStyledAttributes(attr, R.styleable.ClickPressedStyle);
|
|
|
|
|
return array.getFloat(R.styleable.ClickPressedStyle_pressed_alpha, 0.6f);
|
|
|
|
|
} finally {
|
|
|
|
|
if (array != null) {
|
|
|
|
|
array.recycle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ImplementedInterface(value = "android.view.LayoutInflater$Factory", scope = Scope.LEAF)
|
|
|
|
|
@Insert(mayCreateSuper = true)
|
|
|
|
|
@TargetMethod(methodName = "onCreateView")
|
|
|
|
|
@@ -101,7 +275,7 @@ public class ViewPressedStateLancet {
|
|
|
|
|
if (view == null && attrs != null) {
|
|
|
|
|
view = tryCreateView(name, context, attrs);
|
|
|
|
|
}
|
|
|
|
|
checkSetBgIfNeed(null, view, attrs);
|
|
|
|
|
checkSetBgIfNeed(null, view, attrs, false);
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -113,7 +287,7 @@ public class ViewPressedStateLancet {
|
|
|
|
|
if (view == null && attrs != null) {
|
|
|
|
|
view = tryCreateView(name, context, attrs);
|
|
|
|
|
}
|
|
|
|
|
checkSetBgIfNeed(parent, view, attrs);
|
|
|
|
|
checkSetBgIfNeed(parent, view, attrs, false);
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -143,7 +317,7 @@ public class ViewPressedStateLancet {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void checkSetBgIfNeed(View parent, View view, AttributeSet attr) {
|
|
|
|
|
private static void checkSetBgIfNeed(View parent, View view, AttributeSet attr, boolean check) {
|
|
|
|
|
try {
|
|
|
|
|
TypedArray t = null;
|
|
|
|
|
float alpha = 0.6f;
|
|
|
|
|
@@ -156,7 +330,6 @@ public class ViewPressedStateLancet {
|
|
|
|
|
if (context == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parent != null) {
|
|
|
|
|
Object tag = parent.getTag(R.id.click_pressed_attr_enabled);
|
|
|
|
|
if (tag != null && !((Boolean) tag)) {
|
|
|
|
|
@@ -182,80 +355,156 @@ public class ViewPressedStateLancet {
|
|
|
|
|
throw new AssertionError("view custom attr \\'alpha\\' must be in [0.0, 1.0]");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_id, attr);
|
|
|
|
|
if (!check) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_id, attr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (t != null) {
|
|
|
|
|
t.recycle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (view instanceof ImageView) {
|
|
|
|
|
ImageView image = (ImageView) view;
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(image.getBackground(), alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
ViewCompat.setBackground(image, replaced);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
replaced = checkAndReplaceDrawable(image.getDrawable(), alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
image.setImageDrawable(replaced);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (view instanceof TextView) {
|
|
|
|
|
TextView text = (TextView) view;
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(text.getBackground(), alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
ViewCompat.setBackground(text, replaced);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ColorStateList textColor = text.getTextColors();
|
|
|
|
|
if (textColor != null) {
|
|
|
|
|
int pressed = textColor.getColorForState(new int[]{ android.R.attr.state_pressed }, Integer.MIN_VALUE);
|
|
|
|
|
if (pressed != Integer.MIN_VALUE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int color = textColor.getColorForState(new int[] { android.R.attr.state_enabled }, Integer.MIN_VALUE);
|
|
|
|
|
int defaultColor = color;
|
|
|
|
|
if (color == Integer.MIN_VALUE) {
|
|
|
|
|
defaultColor = textColor.getDefaultColor();
|
|
|
|
|
}
|
|
|
|
|
int pressedColor = Color.argb((int)(Color.alpha(defaultColor) * alpha), Color.red(defaultColor), Color.green(defaultColor), Color.blue(defaultColor));
|
|
|
|
|
int disableColor = textColor.getColorForState(new int[] { -android.R.attr.state_enabled }, Integer.MIN_VALUE);
|
|
|
|
|
int size = 2;
|
|
|
|
|
if (disableColor != Integer.MIN_VALUE) {
|
|
|
|
|
size += 1;
|
|
|
|
|
}
|
|
|
|
|
int[][] states = new int[size][1];
|
|
|
|
|
int[] colors = new int[size];
|
|
|
|
|
states[0] = new int[] { android.R.attr.state_pressed };
|
|
|
|
|
colors[0] = pressedColor;
|
|
|
|
|
if (size > 2) {
|
|
|
|
|
states[1] = new int[] { -android.R.attr.state_enabled };
|
|
|
|
|
colors[1] = disableColor;
|
|
|
|
|
states[2] = new int[] { android.R.attr.state_enabled };
|
|
|
|
|
colors[2] = defaultColor;
|
|
|
|
|
} else {
|
|
|
|
|
states[1] = new int[] { -android.R.attr.state_pressed };
|
|
|
|
|
colors[1] = defaultColor;
|
|
|
|
|
}
|
|
|
|
|
ColorStateList newColor = new ColorStateList(states, colors);
|
|
|
|
|
text.setTextColor(newColor);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (view != null) {
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(view.getBackground(), alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
ViewCompat.setBackground(view, replaced);
|
|
|
|
|
}
|
|
|
|
|
boolean rst = check(view, alpha);
|
|
|
|
|
if (!rst && view != null) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_enabled, false);
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
t.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean check(View view, float alpha) {
|
|
|
|
|
if (view instanceof ImageView) {
|
|
|
|
|
ImageView image = (ImageView) view;
|
|
|
|
|
Drawable old = image.getBackground();
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(old, alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_replaced, old);
|
|
|
|
|
image.setBackground(replaced);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
old = image.getDrawable();
|
|
|
|
|
replaced = checkAndReplaceDrawable(old, alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_replaced, old);
|
|
|
|
|
int paddingLeft = view.getPaddingLeft();
|
|
|
|
|
int paddingRight = view.getPaddingRight();
|
|
|
|
|
int paddingTop = view.getPaddingTop();
|
|
|
|
|
int paddingBottom = view.getPaddingBottom();
|
|
|
|
|
image.setImageDrawable(replaced);
|
|
|
|
|
image.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (view instanceof TextView) {
|
|
|
|
|
TextView text = (TextView) view;
|
|
|
|
|
Drawable background = text.getBackground();
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(background, alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_replaced, background);
|
|
|
|
|
int paddingLeft = text.getPaddingLeft();
|
|
|
|
|
int paddingRight = text.getPaddingRight();
|
|
|
|
|
int paddingTop = text.getPaddingTop();
|
|
|
|
|
int paddingBottom = text.getPaddingBottom();
|
|
|
|
|
text.setBackground(replaced);
|
|
|
|
|
text.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ColorStateList textColor = text.getTextColors();
|
|
|
|
|
if (textColor != null) {
|
|
|
|
|
int current = text.getCurrentTextColor();
|
|
|
|
|
int pressed = textColor.getColorForState(new int[]{ android.R.attr.state_pressed }, Integer.MIN_VALUE);
|
|
|
|
|
if (pressed != Integer.MIN_VALUE && pressed != current) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
int enableColor = textColor.getColorForState(new int[] { android.R.attr.state_enabled }, Integer.MIN_VALUE);
|
|
|
|
|
int defaultColor = current;
|
|
|
|
|
if (enableColor == Integer.MIN_VALUE && enableColor != current) {
|
|
|
|
|
defaultColor = textColor.getDefaultColor();
|
|
|
|
|
}
|
|
|
|
|
int pressedColor = Color.argb((int)(Color.alpha(defaultColor) * alpha), Color.red(defaultColor), Color.green(defaultColor), Color.blue(defaultColor));
|
|
|
|
|
int disableColor = textColor.getColorForState(new int[] { -android.R.attr.state_enabled }, Integer.MIN_VALUE);
|
|
|
|
|
int size = 2;
|
|
|
|
|
if (disableColor != Integer.MIN_VALUE && disableColor != current) {
|
|
|
|
|
size += 1;
|
|
|
|
|
}
|
|
|
|
|
boolean hasChecked = false;
|
|
|
|
|
int checkedColor = textColor.getColorForState(new int[] { android.R.attr.state_checked }, Integer.MIN_VALUE);
|
|
|
|
|
if (checkedColor != Integer.MIN_VALUE && checkedColor != current) {
|
|
|
|
|
hasChecked = true;
|
|
|
|
|
size += 1;
|
|
|
|
|
}
|
|
|
|
|
boolean hasUnChecked = false;
|
|
|
|
|
int unCheckedColor = textColor.getColorForState(new int[]{ -android.R.attr.state_checked }, Integer.MIN_VALUE);
|
|
|
|
|
if (unCheckedColor != Integer.MIN_VALUE && unCheckedColor != current) {
|
|
|
|
|
hasUnChecked = true;
|
|
|
|
|
size += 1;
|
|
|
|
|
}
|
|
|
|
|
int[][] states = new int[size][1];
|
|
|
|
|
int[] colors = new int[size];
|
|
|
|
|
states[0] = new int[] { android.R.attr.state_pressed };
|
|
|
|
|
colors[0] = pressedColor;
|
|
|
|
|
if (size > 2) {
|
|
|
|
|
states[1] = new int[] { -android.R.attr.state_enabled };
|
|
|
|
|
colors[1] = disableColor;
|
|
|
|
|
states[2] = new int[] { android.R.attr.state_enabled };
|
|
|
|
|
colors[2] = defaultColor;
|
|
|
|
|
if (hasChecked) {
|
|
|
|
|
states[3] = new int[] { android.R.attr.state_checked };
|
|
|
|
|
colors[3] = checkedColor;
|
|
|
|
|
}
|
|
|
|
|
if (hasUnChecked) {
|
|
|
|
|
if (size > 4) {
|
|
|
|
|
states[4] = new int[] { -android.R.attr.state_checked };
|
|
|
|
|
colors[4] = unCheckedColor;
|
|
|
|
|
} else {
|
|
|
|
|
states[3] = new int[] { -android.R.attr.state_checked };
|
|
|
|
|
colors[3] = unCheckedColor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
states[1] = new int[] { -android.R.attr.state_pressed };
|
|
|
|
|
colors[1] = defaultColor;
|
|
|
|
|
}
|
|
|
|
|
ColorStateList newColor = new ColorStateList(states, colors);
|
|
|
|
|
text.setTag(R.id.click_pressed_attr_replaced_color, textColor);
|
|
|
|
|
text.setTextColor(newColor);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (view != null) {
|
|
|
|
|
Drawable old = view.getBackground();
|
|
|
|
|
Drawable replaced = checkAndReplaceDrawable(old, alpha);
|
|
|
|
|
if (replaced != null) {
|
|
|
|
|
view.setTag(R.id.click_pressed_attr_replaced, old);
|
|
|
|
|
int paddingLeft = view.getPaddingLeft();
|
|
|
|
|
int paddingRight = view.getPaddingRight();
|
|
|
|
|
int paddingTop = view.getPaddingTop();
|
|
|
|
|
int paddingBottom = view.getPaddingBottom();
|
|
|
|
|
view.setBackground(replaced);
|
|
|
|
|
view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (view instanceof ViewGroup) {
|
|
|
|
|
ViewGroup group = (ViewGroup) view;
|
|
|
|
|
int count = group.getChildCount();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
View child = group.getChildAt(i);
|
|
|
|
|
if (check(child, alpha)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Drawable checkAndReplaceDrawable(Drawable old, float alpha) {
|
|
|
|
|
if (old == null) {
|
|
|
|
|
return null;
|
|
|
|
|
@@ -264,47 +513,30 @@ public class ViewPressedStateLancet {
|
|
|
|
|
StateListDrawable drawable = (StateListDrawable) old;
|
|
|
|
|
int[] states = drawable.getState();
|
|
|
|
|
boolean hasPressed = false;
|
|
|
|
|
int index = 0;
|
|
|
|
|
int targetIndex = -1;
|
|
|
|
|
for (int state : states) {
|
|
|
|
|
if (!hasPressed && state == android.R.attr.state_pressed) {
|
|
|
|
|
if (state == android.R.attr.state_pressed) {
|
|
|
|
|
hasPressed = true;
|
|
|
|
|
}
|
|
|
|
|
if (targetIndex == -1 && state != -android.R.attr.state_enabled) {
|
|
|
|
|
targetIndex = index;
|
|
|
|
|
}
|
|
|
|
|
if (hasPressed && targetIndex != -1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
index ++;
|
|
|
|
|
}
|
|
|
|
|
if (hasPressed) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (targetIndex != -1) {
|
|
|
|
|
Drawable origin= null;
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
|
|
origin = drawable.getStateDrawable(targetIndex);
|
|
|
|
|
} else {
|
|
|
|
|
origin = drawable.getCurrent();
|
|
|
|
|
}
|
|
|
|
|
if (origin != null) {
|
|
|
|
|
Drawable.ConstantState constantState = origin.getConstantState();
|
|
|
|
|
if (constantState != null) {
|
|
|
|
|
Drawable pressed = DrawableCompat.wrap(constantState.newDrawable().mutate());
|
|
|
|
|
pressed.setAlpha((int)(255 * alpha));
|
|
|
|
|
drawable.addState(new int[] { android.R.attr.state_pressed }, pressed);
|
|
|
|
|
return drawable;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
Drawable origin = drawable.getCurrent();
|
|
|
|
|
Drawable.ConstantState constantState = origin.getConstantState();
|
|
|
|
|
if (constantState != null) {
|
|
|
|
|
Drawable pressed = DrawableCompat.wrap(constantState.newDrawable().mutate());
|
|
|
|
|
pressed.setAlpha((int)(255 * alpha));
|
|
|
|
|
drawable.addState(new int[] { android.R.attr.state_pressed }, pressed);
|
|
|
|
|
return drawable;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Drawable.ConstantState constantState = old.getConstantState();
|
|
|
|
|
if (constantState != null) {
|
|
|
|
|
StateListDrawable result = new StateListDrawable();
|
|
|
|
|
result.addState(new int[] { -android.R.attr.state_pressed }, old);
|
|
|
|
|
int[] state = { -android.R.attr.state_pressed };
|
|
|
|
|
result.addState(state, old);
|
|
|
|
|
Drawable pressed = DrawableCompat.wrap(constantState.newDrawable().mutate());
|
|
|
|
|
pressed.setAlpha((int)(255 * alpha));
|
|
|
|
|
result.addState(new int[] { android.R.attr.state_pressed }, pressed);
|
|
|
|
|
|