NekoX/TMessagesProj/src/main/java/org/telegram/ui/Cells/DrawerActionCheckCell.java

242 lines
9.1 KiB
Java

package org.telegram.ui.Cells;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Property;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.FrameLayout;
import android.widget.TextView;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.R;
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Components.AnimationProperties;
import org.telegram.ui.Components.CubicBezierInterpolator;
import org.telegram.ui.Components.LayoutHelper;
import org.telegram.ui.Components.Switch;
import java.util.ArrayList;
public class DrawerActionCheckCell extends FrameLayout {
private TextView textView;
public Switch checkBox;
private int height = 48;
private int animatedColorBackground;
private float animationProgress;
private Paint animationPaint;
private float lastTouchX;
private ObjectAnimator animator;
private boolean drawCheckRipple;
public static final Property<DrawerActionCheckCell, Float> ANIMATION_PROGRESS = new AnimationProperties.FloatProperty<DrawerActionCheckCell>("animationProgress") {
@Override
public void setValue(DrawerActionCheckCell object, float value) {
object.setAnimationProgress(value);
object.invalidate();
}
@Override
public Float get(DrawerActionCheckCell object) {
return object.animationProgress;
}
};
public DrawerActionCheckCell(Context context) {
this(context, 21);
}
public DrawerActionCheckCell(Context context, int padding) {
super(context);
textView = new TextView(context);
textView.setTextColor(Theme.getColor(Theme.key_chats_menuItemText));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.LEFT | Gravity.TOP, padding, 0, 70, 0));
checkBox = new Switch(context);
checkBox.setColors(Theme.key_switchTrack, Theme.key_switchTrackChecked, Theme.key_windowBackgroundWhite, Theme.key_windowBackgroundWhite);
addView(checkBox, LayoutHelper.createFrame(37, 20, Gravity.RIGHT | Gravity.CENTER_VERTICAL, 22, 0, 22, 0));
setClipChildren(false);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(height), MeasureSpec.EXACTLY));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
lastTouchX = event.getX();
return super.onTouchEvent(event);
}
public void setTextAndCheck(String text, boolean checked, boolean divider) {
textView.setText(text);
checkBox.setChecked(checked, false);
LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
layoutParams.height = LayoutParams.MATCH_PARENT;
layoutParams.topMargin = 0;
textView.setLayoutParams(layoutParams);
setWillNotDraw(!divider);
}
public void setColors(String key, String switchKey, String switchKeyChecked, String switchThumb, String switchThumbChecked) {
textView.setTextColor(Theme.getColor(key));
checkBox.setColors(switchKey, switchKeyChecked, switchThumb, switchThumbChecked);
textView.setTag(key);
}
public void setTypeface(Typeface typeface) {
textView.setTypeface(typeface);
}
public void setHeight(int value) {
height = value;
}
public void setDrawCheckRipple(boolean value) {
drawCheckRipple = value;
}
@Override
public void setPressed(boolean pressed) {
if (drawCheckRipple) {
checkBox.setDrawRipple(pressed);
}
super.setPressed(pressed);
}
public void setOnCheckChangeListener(Switch.OnCheckedChangeListener listener) {
checkBox.setOnCheckedChangeListener(listener);
}
public void setOnCheckClickListener(View.OnClickListener listener) {
checkBox.setOnClickListener(listener);
}
public void setTextAndValueAndCheck(String text, int resId, String value, boolean checked, boolean multiline, boolean divider) {
textView.setText(text);
Drawable drawable = getResources().getDrawable(resId).mutate();
drawable.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chats_menuItemIcon), PorterDuff.Mode.SRC_IN));
textView.setCompoundDrawablePadding(AndroidUtilities.dp(29));
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
checkBox.setChecked(checked, false);
LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
layoutParams.topMargin = AndroidUtilities.dp(10);
textView.setLayoutParams(layoutParams);
setWillNotDraw(!divider);
}
public void setEnabled(boolean value, ArrayList<Animator> animators) {
super.setEnabled(value);
if (animators != null) {
animators.add(ObjectAnimator.ofFloat(textView, "alpha", value ? 1.0f : 0.5f));
animators.add(ObjectAnimator.ofFloat(checkBox, "alpha", value ? 1.0f : 0.5f));
} else {
textView.setAlpha(value ? 1.0f : 0.5f);
checkBox.setAlpha(value ? 1.0f : 0.5f);
}
}
public void setChecked(boolean checked) {
checkBox.setChecked(checked, true);
}
public boolean isChecked() {
return checkBox.isChecked();
}
@Override
public void setBackgroundColor(int color) {
clearAnimation();
animatedColorBackground = 0;
super.setBackgroundColor(color);
}
public void setBackgroundColorAnimated(boolean checked, int color) {
if (animator != null) {
animator.cancel();
animator = null;
}
if (animatedColorBackground != 0) {
setBackgroundColor(animatedColorBackground);
}
if (animationPaint == null) {
animationPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
checkBox.setOverrideColor(checked ? 1 : 2);
animatedColorBackground = color;
animationPaint.setColor(animatedColorBackground);
animationProgress = 0.0f;
animator = ObjectAnimator.ofFloat(this, ANIMATION_PROGRESS, 0.0f, 1.0f);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
setBackgroundColor(animatedColorBackground);
animatedColorBackground = 0;
invalidate();
}
});
animator.setInterpolator(CubicBezierInterpolator.EASE_OUT);
animator.setDuration(240).start();
}
private void setAnimationProgress(float value) {
animationProgress = value;
float rad = Math.max(lastTouchX, getMeasuredWidth() - lastTouchX) + AndroidUtilities.dp(40);
float cx = lastTouchX;
int cy = getMeasuredHeight() / 2;
float animatedRad = rad * animationProgress;
checkBox.setOverrideColorProgress(cx, cy, animatedRad);
}
@Override
protected void onDraw(Canvas canvas) {
if (animatedColorBackground != 0) {
float rad = Math.max(lastTouchX, getMeasuredWidth() - lastTouchX) + AndroidUtilities.dp(40);
float cx = lastTouchX;
int cy = getMeasuredHeight() / 2;
float animatedRad = rad * animationProgress;
canvas.drawCircle(cx, cy, animatedRad, animationPaint);
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
textView.setTextColor(Theme.getColor(Theme.key_chats_menuItemText));
checkBox.setColors(Theme.key_switchTrack, Theme.key_switchTrackChecked, Theme.key_windowBackgroundWhite, Theme.key_windowBackgroundWhite);
}
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName("android.widget.Switch");
info.setCheckable(true);
info.setChecked(checkBox.isChecked());
info.setContentDescription(checkBox.isChecked() ? LocaleController.getString("NotificationsOn", R.string.NotificationsOn) : LocaleController.getString("NotificationsOff", R.string.NotificationsOff));
}
}