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

278 lines
11 KiB
Java

/*
* This is the source code of Telegram for Android v. 5.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013-2018.
*/
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.Typeface;
import android.text.TextUtils;
import android.util.Property;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
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 TextCheckCell extends FrameLayout {
private TextView textView;
private TextView valueTextView;
private Switch checkBox;
private boolean needDivider;
private boolean isMultiline;
private int height = 50;
private int animatedColorBackground;
private float animationProgress;
private Paint animationPaint;
private float lastTouchX;
private ObjectAnimator animator;
private boolean drawCheckRipple;
public static final Property<TextCheckCell, Float> ANIMATION_PROGRESS = new AnimationProperties.FloatProperty<TextCheckCell>("animationProgress") {
@Override
public void setValue(TextCheckCell object, float value) {
object.setAnimationProgress(value);
object.invalidate();
}
@Override
public Float get(TextCheckCell object) {
return object.animationProgress;
}
};
public TextCheckCell(Context context) {
this(context, 21);
}
public TextCheckCell(Context context, int padding) {
this(context, padding, false);
}
public TextCheckCell(Context context, int padding, boolean dialog) {
super(context);
textView = new TextView(context);
textView.setTextColor(Theme.getColor(dialog ? Theme.key_dialogTextBlack : Theme.key_windowBackgroundWhiteBlackText));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL);
textView.setEllipsize(TextUtils.TruncateAt.END);
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 70 : padding, 0, LocaleController.isRTL ? padding : 70, 0));
valueTextView = new TextView(context);
valueTextView.setTextColor(Theme.getColor(dialog ? Theme.key_dialogIcon : Theme.key_windowBackgroundWhiteGrayText2));
valueTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13);
valueTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
valueTextView.setLines(1);
valueTextView.setMaxLines(1);
valueTextView.setSingleLine(true);
valueTextView.setPadding(0, 0, 0, 0);
valueTextView.setEllipsize(TextUtils.TruncateAt.END);
addView(valueTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 64 : padding, 36, LocaleController.isRTL ? padding : 64, 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, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.CENTER_VERTICAL, 22, 0, 22, 0));
setClipChildren(false);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isMultiline) {
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
} else {
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(valueTextView.getVisibility() == VISIBLE ? 64 : height) + (needDivider ? 1 : 0), 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);
isMultiline = false;
checkBox.setChecked(checked, false);
needDivider = divider;
valueTextView.setVisibility(GONE);
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 setTextAndValueAndCheck(String text, String value, boolean checked, boolean multiline, boolean divider) {
textView.setText(text);
valueTextView.setText(value);
checkBox.setChecked(checked, false);
needDivider = divider;
valueTextView.setVisibility(VISIBLE);
isMultiline = multiline;
if (multiline) {
valueTextView.setLines(0);
valueTextView.setMaxLines(0);
valueTextView.setSingleLine(false);
valueTextView.setEllipsize(null);
valueTextView.setPadding(0, 0, 0, AndroidUtilities.dp(11));
} else {
valueTextView.setLines(1);
valueTextView.setMaxLines(1);
valueTextView.setSingleLine(true);
valueTextView.setEllipsize(TextUtils.TruncateAt.END);
valueTextView.setPadding(0, 0, 0, 0);
}
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));
if (valueTextView.getVisibility() == VISIBLE) {
animators.add(ObjectAnimator.ofFloat(valueTextView, "alpha", value ? 1.0f : 0.5f));
}
} else {
textView.setAlpha(value ? 1.0f : 0.5f);
checkBox.setAlpha(value ? 1.0f : 0.5f);
if (valueTextView.getVisibility() == VISIBLE) {
valueTextView.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);
}
if (needDivider) {
canvas.drawLine(LocaleController.isRTL ? 0 : AndroidUtilities.dp(20), getMeasuredHeight() - 1, getMeasuredWidth() - (LocaleController.isRTL ? AndroidUtilities.dp(20) : 0), getMeasuredHeight() - 1, Theme.dividerPaint);
}
}
@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));
}
}