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

350 lines
15 KiB
Java
Raw Normal View History

2014-11-11 23:16:17 +01:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2014-11-11 23:16:17 +01:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2014-11-11 23:16:17 +01:00
*/
package org.telegram.ui.Cells;
2017-03-31 01:58:05 +02:00
import android.animation.Animator;
import android.animation.ObjectAnimator;
2014-11-11 23:16:17 +01:00
import android.content.Context;
import android.graphics.Canvas;
2021-06-25 02:43:10 +02:00
import android.graphics.Paint;
2017-03-31 01:58:05 +02:00
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
2014-11-13 21:10:14 +01:00
import android.text.TextUtils;
2014-11-11 23:16:17 +01:00
import android.util.TypedValue;
import android.view.Gravity;
2021-06-25 02:43:10 +02:00
import android.view.View;
2019-05-14 14:08:05 +02:00
import android.view.accessibility.AccessibilityNodeInfo;
2014-11-11 23:16:17 +01:00
import android.widget.FrameLayout;
2014-11-17 03:44:57 +01:00
import android.widget.ImageView;
2014-11-11 23:16:17 +01:00
import android.widget.TextView;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
2022-09-16 20:48:21 +02:00
import org.telegram.ui.Components.AnimatedEmojiDrawable;
2022-06-21 04:51:00 +02:00
import org.telegram.ui.Components.AnimatedTextView;
2021-12-30 11:52:40 +01:00
import org.telegram.ui.Components.BackupImageView;
2022-06-21 04:51:00 +02:00
import org.telegram.ui.Components.CubicBezierInterpolator;
import org.telegram.ui.Components.LayoutHelper;
2022-06-21 04:51:00 +02:00
import org.telegram.ui.Components.RLottieImageView;
2014-11-11 23:16:17 +01:00
2017-03-31 01:58:05 +02:00
import java.util.ArrayList;
2014-11-11 23:16:17 +01:00
public class TextSettingsCell extends FrameLayout {
2022-06-21 04:51:00 +02:00
private Theme.ResourcesProvider resourcesProvider;
2014-11-11 23:16:17 +01:00
private TextView textView;
2022-06-21 04:51:00 +02:00
private AnimatedTextView valueTextView;
private ImageView imageView;
2021-12-30 11:52:40 +01:00
private BackupImageView valueBackupImageView;
2014-11-17 03:44:57 +01:00
private ImageView valueImageView;
2014-11-11 23:16:17 +01:00
private boolean needDivider;
2019-03-03 21:40:48 +01:00
private boolean canDisable;
2021-06-25 02:43:10 +02:00
private boolean drawLoading;
private int padding;
private boolean incrementLoadingProgress;
private float loadingProgress;
private float drawLoadingProgress;
private int loadingSize;
private boolean measureDelay;
private int changeProgressStartDelay;
Paint paint;
2014-11-11 23:16:17 +01:00
public TextSettingsCell(Context context) {
2019-01-23 18:03:33 +01:00
this(context, 21);
}
2022-06-21 04:51:00 +02:00
public TextSettingsCell(Context context, Theme.ResourcesProvider resourcesProvider) {
this(context, 21, resourcesProvider);
}
2019-01-23 18:03:33 +01:00
public TextSettingsCell(Context context, int padding) {
2022-06-21 04:51:00 +02:00
this(context, padding, null);
}
public TextSettingsCell(Context context, int padding, Theme.ResourcesProvider resourcesProvider) {
2014-11-11 23:16:17 +01:00
super(context);
2022-06-21 04:51:00 +02:00
this.resourcesProvider = resourcesProvider;
2021-06-25 02:43:10 +02:00
this.padding = padding;
2014-11-11 23:16:17 +01:00
textView = new TextView(context);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
2014-11-13 21:10:14 +01:00
textView.setEllipsize(TextUtils.TruncateAt.END);
2014-11-11 23:16:17 +01:00
textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL);
2022-06-21 04:51:00 +02:00
textView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText, resourcesProvider));
2019-01-23 18:03:33 +01:00
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, padding, 0, padding, 0));
2014-11-11 23:16:17 +01:00
2022-06-21 04:51:00 +02:00
valueTextView = new AnimatedTextView(context, true, true, !LocaleController.isRTL);
valueTextView.setAnimationProperties(.55f, 0, 320, CubicBezierInterpolator.EASE_OUT_QUINT);
valueTextView.setTextSize(AndroidUtilities.dp(16));
2014-11-11 23:16:17 +01:00
valueTextView.setGravity((LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.CENTER_VERTICAL);
2022-06-21 04:51:00 +02:00
valueTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteValueText, resourcesProvider));
2019-01-23 18:03:33 +01:00
addView(valueTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.TOP, padding, 0, padding, 0));
2014-11-17 03:44:57 +01:00
2022-06-21 04:51:00 +02:00
imageView = new RLottieImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_windowBackgroundWhiteGrayIcon), PorterDuff.Mode.MULTIPLY));
imageView.setVisibility(GONE);
addView(imageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL, 21, 0, 21, 0));
2014-11-17 03:44:57 +01:00
valueImageView = new ImageView(context);
valueImageView.setScaleType(ImageView.ScaleType.CENTER);
2015-04-09 20:00:14 +02:00
valueImageView.setVisibility(INVISIBLE);
2020-06-25 17:12:58 +02:00
valueImageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_windowBackgroundWhiteGrayIcon), PorterDuff.Mode.SRC_IN));
2019-01-23 18:03:33 +01:00
addView(valueImageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.CENTER_VERTICAL, padding, 0, padding, 0));
2014-11-11 23:16:17 +01:00
}
2022-08-12 17:23:51 +02:00
public ImageView getValueImageView() {
return valueImageView;
}
2014-11-11 23:16:17 +01:00
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2019-01-23 18:03:33 +01:00
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), AndroidUtilities.dp(50) + (needDivider ? 1 : 0));
2014-11-13 21:10:14 +01:00
int availableWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - AndroidUtilities.dp(34);
int width = availableWidth / 2;
2014-11-17 03:44:57 +01:00
if (valueImageView.getVisibility() == VISIBLE) {
valueImageView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
}
2021-12-30 11:52:40 +01:00
2022-06-21 04:51:00 +02:00
if (imageView.getVisibility() == VISIBLE) {
imageView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST));
}
2021-12-30 11:52:40 +01:00
if (valueBackupImageView != null) {
valueBackupImageView.measure(MeasureSpec.makeMeasureSpec(valueBackupImageView.getLayoutParams().height, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(valueBackupImageView.getLayoutParams().width, MeasureSpec.EXACTLY));
}
2014-11-13 21:10:14 +01:00
if (valueTextView.getVisibility() == VISIBLE) {
valueTextView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
width = availableWidth - valueTextView.getMeasuredWidth() - AndroidUtilities.dp(8);
2022-08-12 17:23:51 +02:00
if (valueImageView.getVisibility() == VISIBLE) {
MarginLayoutParams params = (MarginLayoutParams) valueImageView.getLayoutParams();
if (LocaleController.isRTL) {
params.leftMargin = AndroidUtilities.dp(padding + 4) + valueTextView.getMeasuredWidth();
} else {
params.rightMargin = AndroidUtilities.dp(padding + 4) + valueTextView.getMeasuredWidth();
}
}
2014-11-13 21:10:14 +01:00
} else {
width = availableWidth;
}
2014-11-19 16:17:24 +01:00
textView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2014-11-11 23:16:17 +01:00
}
2021-06-25 02:43:10 +02:00
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (measureDelay && getParent() != null) {
changeProgressStartDelay = (int) ((getTop() / (float) ((View) getParent()).getMeasuredHeight()) * 150f);
}
}
2017-03-31 01:58:05 +02:00
public TextView getTextView() {
return textView;
}
2019-03-03 21:40:48 +01:00
public void setCanDisable(boolean value) {
canDisable = value;
}
2022-06-21 04:51:00 +02:00
public AnimatedTextView getValueTextView() {
2017-03-31 01:58:05 +02:00
return valueTextView;
}
2014-11-11 23:16:17 +01:00
public void setTextColor(int color) {
textView.setTextColor(color);
}
2016-03-16 13:26:32 +01:00
public void setTextValueColor(int color) {
valueTextView.setTextColor(color);
}
2022-03-11 17:49:54 +01:00
public void setText(CharSequence text, boolean divider) {
2014-11-11 23:16:17 +01:00
textView.setText(text);
2015-04-09 20:00:14 +02:00
valueTextView.setVisibility(INVISIBLE);
valueImageView.setVisibility(INVISIBLE);
2014-11-11 23:16:17 +01:00
needDivider = divider;
setWillNotDraw(!divider);
}
2022-03-11 17:49:54 +01:00
public void setTextAndValue(CharSequence text, CharSequence value, boolean divider) {
2022-06-21 04:51:00 +02:00
setTextAndValue(text, value, false, divider);
}
public void setTextAndValue(CharSequence text, CharSequence value, boolean animated, boolean divider) {
2014-11-11 23:16:17 +01:00
textView.setText(text);
2015-04-09 20:00:14 +02:00
valueImageView.setVisibility(INVISIBLE);
2014-11-17 03:44:57 +01:00
if (value != null) {
2022-06-21 04:51:00 +02:00
valueTextView.setText(value, animated);
2014-11-17 03:44:57 +01:00
valueTextView.setVisibility(VISIBLE);
} else {
2015-04-09 20:00:14 +02:00
valueTextView.setVisibility(INVISIBLE);
2014-11-17 03:44:57 +01:00
}
needDivider = divider;
setWillNotDraw(!divider);
2015-05-21 23:27:27 +02:00
requestLayout();
2014-11-17 03:44:57 +01:00
}
2022-03-11 17:49:54 +01:00
public void setTextAndIcon(CharSequence text, int resId, boolean divider) {
2014-11-17 03:44:57 +01:00
textView.setText(text);
2015-04-09 20:00:14 +02:00
valueTextView.setVisibility(INVISIBLE);
2014-11-17 03:44:57 +01:00
if (resId != 0) {
valueImageView.setVisibility(VISIBLE);
valueImageView.setImageResource(resId);
} else {
2015-04-09 20:00:14 +02:00
valueImageView.setVisibility(INVISIBLE);
2014-11-17 03:44:57 +01:00
}
2014-11-11 23:16:17 +01:00
needDivider = divider;
setWillNotDraw(!divider);
}
2022-06-21 04:51:00 +02:00
public void setIcon(int resId) {
MarginLayoutParams params = (MarginLayoutParams) textView.getLayoutParams();
if (resId == 0) {
imageView.setVisibility(GONE);
params.leftMargin = 0;
} else {
imageView.setImageResource(resId);
imageView.setVisibility(VISIBLE);
params.leftMargin = AndroidUtilities.dp(71);
}
}
2017-03-31 01:58:05 +02:00
public void setEnabled(boolean value, ArrayList<Animator> animators) {
setEnabled(value);
if (animators != null) {
animators.add(ObjectAnimator.ofFloat(textView, "alpha", value ? 1.0f : 0.5f));
if (valueTextView.getVisibility() == VISIBLE) {
animators.add(ObjectAnimator.ofFloat(valueTextView, "alpha", value ? 1.0f : 0.5f));
}
if (valueImageView.getVisibility() == VISIBLE) {
animators.add(ObjectAnimator.ofFloat(valueImageView, "alpha", value ? 1.0f : 0.5f));
}
} else {
textView.setAlpha(value ? 1.0f : 0.5f);
if (valueTextView.getVisibility() == VISIBLE) {
valueTextView.setAlpha(value ? 1.0f : 0.5f);
}
if (valueImageView.getVisibility() == VISIBLE) {
valueImageView.setAlpha(value ? 1.0f : 0.5f);
}
}
}
2019-03-03 21:40:48 +01:00
@Override
public void setEnabled(boolean value) {
super.setEnabled(value);
textView.setAlpha(value || !canDisable ? 1.0f : 0.5f);
if (valueTextView.getVisibility() == VISIBLE) {
valueTextView.setAlpha(value || !canDisable ? 1.0f : 0.5f);
}
if (valueImageView.getVisibility() == VISIBLE) {
valueImageView.setAlpha(value || !canDisable ? 1.0f : 0.5f);
}
}
2014-11-11 23:16:17 +01:00
@Override
2021-06-25 02:43:10 +02:00
protected void dispatchDraw(Canvas canvas) {
if (drawLoading || drawLoadingProgress != 0) {
if (paint == null) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
2022-06-21 04:51:00 +02:00
paint.setColor(Theme.getColor(Theme.key_dialogSearchBackground, resourcesProvider));
2021-06-25 02:43:10 +02:00
}
//LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT;
if (incrementLoadingProgress) {
loadingProgress += 16 / 1000f;
if (loadingProgress > 1f) {
loadingProgress = 1f;
incrementLoadingProgress = false;
}
} else {
loadingProgress -= 16 / 1000f;
if (loadingProgress < 0) {
loadingProgress = 0;
incrementLoadingProgress = true;
}
}
if (changeProgressStartDelay > 0) {
changeProgressStartDelay -= 15;
} else if (drawLoading && drawLoadingProgress != 1f) {
drawLoadingProgress += 16 / 150f;
if (drawLoadingProgress > 1f) {
drawLoadingProgress = 1f;
}
} else if (!drawLoading && drawLoadingProgress != 0) {
drawLoadingProgress -= 16 / 150f;
if (drawLoadingProgress < 0) {
drawLoadingProgress = 0;
}
}
float alpha = (0.6f + 0.4f * loadingProgress) * drawLoadingProgress;
paint.setAlpha((int) (255 * alpha));
int cy = getMeasuredHeight() >> 1;
AndroidUtilities.rectTmp.set(getMeasuredWidth() - AndroidUtilities.dp(padding) - AndroidUtilities.dp(loadingSize), cy - AndroidUtilities.dp(3), getMeasuredWidth() - AndroidUtilities.dp(padding), cy + AndroidUtilities.dp(3));
if (LocaleController.isRTL) {
AndroidUtilities.rectTmp.left = getMeasuredWidth() - AndroidUtilities.rectTmp.left;
AndroidUtilities.rectTmp.right = getMeasuredWidth() - AndroidUtilities.rectTmp.right;
}
canvas.drawRoundRect(AndroidUtilities.rectTmp, AndroidUtilities.dp(3), AndroidUtilities.dp(3), paint);
invalidate();
}
valueTextView.setAlpha(1f - drawLoadingProgress);
super.dispatchDraw(canvas);
2014-11-11 23:16:17 +01:00
if (needDivider) {
2020-06-26 03:47:42 +02:00
canvas.drawLine(0, getMeasuredHeight() - 1, getMeasuredWidth(), getMeasuredHeight() - 1, Theme.dividerPaint);
2014-11-11 23:16:17 +01:00
}
}
2019-05-14 14:08:05 +02:00
2021-06-25 02:43:10 +02:00
2019-05-14 14:08:05 +02:00
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
2022-06-21 04:51:00 +02:00
info.setText(textView.getText() + (valueTextView != null && valueTextView.getVisibility() == View.VISIBLE ? "\n" + valueTextView.getText() : ""));
2019-05-14 14:08:05 +02:00
info.setEnabled(isEnabled());
}
2021-06-25 02:43:10 +02:00
public void setDrawLoading(boolean drawLoading, int size, boolean animated) {
this.drawLoading = drawLoading;
this.loadingSize = size;
if (!animated) {
drawLoadingProgress = drawLoading ? 1f : 0f;
} else {
measureDelay = true;
}
invalidate();
}
2021-12-30 11:52:40 +01:00
public BackupImageView getValueBackupImageView() {
if (valueBackupImageView == null) {
valueBackupImageView = new BackupImageView(getContext());
addView(valueBackupImageView, LayoutHelper.createFrame(24, 24, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.CENTER_VERTICAL, padding, 0, padding, 0));
}
return valueBackupImageView;
}
2022-09-16 20:48:21 +02:00
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (valueBackupImageView != null && valueBackupImageView.getImageReceiver() != null && valueBackupImageView.getImageReceiver().getDrawable() instanceof AnimatedEmojiDrawable) {
((AnimatedEmojiDrawable) valueBackupImageView.getImageReceiver().getDrawable()).removeView(this);
}
}
2014-11-11 23:16:17 +01:00
}