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

238 lines
11 KiB
Java
Raw Normal View History

2015-12-09 19:27:52 +01:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2015-12-09 19:27:52 +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.
2015-12-09 19:27:52 +01:00
*/
package org.telegram.ui.Cells;
import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
2020-04-24 11:21:58 +02:00
import android.view.View;
2019-05-14 14:08:05 +02:00
import android.view.accessibility.AccessibilityNodeInfo;
2015-12-09 19:27:52 +01:00
import android.widget.FrameLayout;
import android.widget.TextView;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
2020-04-24 11:21:58 +02:00
import org.telegram.ui.Components.CheckBox2;
2015-12-09 19:27:52 +01:00
import org.telegram.ui.Components.CheckBoxSquare;
import org.telegram.ui.Components.LayoutHelper;
public class CheckBoxCell extends FrameLayout {
private TextView textView;
private TextView valueTextView;
2020-04-24 11:21:58 +02:00
private View checkBox;
private CheckBoxSquare checkBoxSquare;
private CheckBox2 checkBoxRound;
2015-12-09 19:27:52 +01:00
private boolean needDivider;
2019-06-04 12:14:50 +02:00
private boolean isMultiline;
2019-12-31 14:08:08 +01:00
private int currentType;
2015-12-09 19:27:52 +01:00
2020-04-24 11:21:58 +02:00
private int checkBoxSize = 18;
public final static int TYPE_CHECK_BOX_ROUND = 4;
2018-07-30 04:07:02 +02:00
public CheckBoxCell(Context context, int type) {
2019-01-23 18:03:33 +01:00
this(context, type, 17);
}
public CheckBoxCell(Context context, int type, int padding) {
2015-12-09 19:27:52 +01:00
super(context);
2019-12-31 14:08:08 +01:00
currentType = type;
2015-12-09 19:27:52 +01:00
textView = new TextView(context);
2021-03-19 11:25:58 +01:00
textView.setTextColor(Theme.getColor(type == 1 || type == 5 ? Theme.key_dialogTextBlack : Theme.key_windowBackgroundWhiteBlackText));
textView.setLinkTextColor(Theme.getColor(type == 1 || type == 5 ? Theme.key_dialogTextLink : Theme.key_windowBackgroundWhiteLinkText));
textView.setTag(Theme.getColor(type == 1 || type == 5 ? Theme.key_dialogTextBlack : Theme.key_windowBackgroundWhiteBlackText));
2015-12-09 19:27:52 +01:00
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
2019-12-31 14:08:08 +01:00
if (type == 3) {
textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.LEFT | Gravity.TOP, 29, 0, 0, 0));
textView.setPadding(0, 0, 0, AndroidUtilities.dp(3));
2018-07-30 04:07:02 +02:00
} else {
2019-12-31 14:08:08 +01:00
textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL);
if (type == 2) {
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, (LocaleController.isRTL ? 0 : 29), 0, (LocaleController.isRTL ? 29 : 0), 0));
} else {
2020-04-24 11:21:58 +02:00
int offset = type == 4 ? 56 : 46;
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, (LocaleController.isRTL ? padding : offset + (padding - 17)), 0, (LocaleController.isRTL ? offset + (padding - 17) : padding), 0));
2019-12-31 14:08:08 +01:00
}
2018-07-30 04:07:02 +02:00
}
2015-12-09 19:27:52 +01:00
valueTextView = new TextView(context);
2021-03-19 11:25:58 +01:00
valueTextView.setTextColor(Theme.getColor(type == 1 || type == 5 ? Theme.key_dialogTextBlue : Theme.key_windowBackgroundWhiteValueText));
valueTextView.setTag(type == 1 || type == 5 ? Theme.key_dialogTextBlue : Theme.key_windowBackgroundWhiteValueText);
2015-12-09 19:27:52 +01:00
valueTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
valueTextView.setLines(1);
valueTextView.setMaxLines(1);
valueTextView.setSingleLine(true);
valueTextView.setEllipsize(TextUtils.TruncateAt.END);
valueTextView.setGravity((LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.CENTER_VERTICAL);
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));
2015-12-09 19:27:52 +01:00
2020-04-24 11:21:58 +02:00
if (type == TYPE_CHECK_BOX_ROUND) {
checkBox = checkBoxRound = new CheckBox2(context, 21);
checkBoxRound.setDrawUnchecked(true);
checkBoxRound.setChecked(true, false);
checkBoxRound.setDrawBackgroundAsArc(10);
checkBoxSize = 21;
addView(checkBox, LayoutHelper.createFrame(checkBoxSize, checkBoxSize, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, (LocaleController.isRTL ? 0 : padding), 16, (LocaleController.isRTL ? padding : 0), 0));
2018-07-30 04:07:02 +02:00
} else {
2021-03-19 11:25:58 +01:00
checkBox = checkBoxSquare = new CheckBoxSquare(context, type == 1 || type == 5);
2020-04-24 11:21:58 +02:00
checkBoxSize = 18;
2021-03-19 11:25:58 +01:00
if (type == 5) {
addView(checkBox, LayoutHelper.createFrame(checkBoxSize, checkBoxSize, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL, (LocaleController.isRTL ? 0 : padding), 0, (LocaleController.isRTL ? padding : 0), 0));
} else if (type == 3) {
2020-04-24 11:21:58 +02:00
addView(checkBox, LayoutHelper.createFrame(checkBoxSize, checkBoxSize, Gravity.LEFT | Gravity.TOP, 0, 15, 0, 0));
} else if (type == 2) {
addView(checkBox, LayoutHelper.createFrame(checkBoxSize, checkBoxSize, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, 0, 15, 0, 0));
} else {
addView(checkBox, LayoutHelper.createFrame(checkBoxSize, checkBoxSize, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, (LocaleController.isRTL ? 0 : padding), 16, (LocaleController.isRTL ? padding : 0), 0));
}
2018-07-30 04:07:02 +02:00
}
2015-12-09 19:27:52 +01:00
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2019-12-31 14:08:08 +01:00
if (currentType == 3) {
int width = MeasureSpec.getSize(widthMeasureSpec);
valueTextView.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(10), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(50), MeasureSpec.EXACTLY));
textView.measure(MeasureSpec.makeMeasureSpec(width - AndroidUtilities.dp(34), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(50), MeasureSpec.EXACTLY));
2020-04-24 11:21:58 +02:00
checkBox.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(checkBoxSize), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(checkBoxSize), MeasureSpec.EXACTLY));
2019-12-31 14:08:08 +01:00
setMeasuredDimension(textView.getMeasuredWidth() + AndroidUtilities.dp(29), AndroidUtilities.dp(50));
} else if (isMultiline) {
2019-06-04 12:14:50 +02:00
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
} else {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), AndroidUtilities.dp(50) + (needDivider ? 1 : 0));
2015-12-09 19:27:52 +01:00
2019-06-04 12:14:50 +02:00
int availableWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - AndroidUtilities.dp(34);
2015-12-09 19:27:52 +01:00
2019-06-04 12:14:50 +02:00
valueTextView.measure(MeasureSpec.makeMeasureSpec(availableWidth / 2, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
textView.measure(MeasureSpec.makeMeasureSpec(availableWidth - valueTextView.getMeasuredWidth() - AndroidUtilities.dp(8), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2020-04-24 11:21:58 +02:00
checkBox.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(checkBoxSize), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(checkBoxSize), MeasureSpec.EXACTLY));
2019-06-04 12:14:50 +02:00
}
2015-12-09 19:27:52 +01:00
}
public void setTextColor(int color) {
textView.setTextColor(color);
}
2019-06-04 12:14:50 +02:00
public void setText(CharSequence text, String value, boolean checked, boolean divider) {
2015-12-09 19:27:52 +01:00
textView.setText(text);
2020-04-24 11:21:58 +02:00
if (checkBoxRound != null) {
checkBoxRound.setChecked(checked, false);
} else {
checkBoxSquare.setChecked(checked, false);
}
2015-12-09 19:27:52 +01:00
valueTextView.setText(value);
needDivider = divider;
setWillNotDraw(!divider);
}
2020-04-24 11:21:58 +02:00
public void setNeedDivider(boolean needDivider){
this.needDivider = needDivider;
}
2019-06-04 12:14:50 +02:00
public void setMultiline(boolean value) {
isMultiline = value;
LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
LayoutParams layoutParams1 = (LayoutParams) checkBox.getLayoutParams();
if (isMultiline) {
textView.setLines(0);
textView.setMaxLines(0);
textView.setSingleLine(false);
textView.setEllipsize(null);
2021-03-19 11:25:58 +01:00
if (currentType != 5) {
textView.setPadding(0, 0, 0, AndroidUtilities.dp(5));
layoutParams.height = LayoutParams.WRAP_CONTENT;
layoutParams.topMargin = AndroidUtilities.dp(10);
layoutParams1.topMargin = AndroidUtilities.dp(12);
}
2019-06-04 12:14:50 +02:00
} else {
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setPadding(0, 0, 0, 0);
layoutParams.height = LayoutParams.MATCH_PARENT;
layoutParams.topMargin = 0;
layoutParams1.topMargin = AndroidUtilities.dp(15);
}
textView.setLayoutParams(layoutParams);
checkBox.setLayoutParams(layoutParams1);
}
2017-07-08 18:32:04 +02:00
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
textView.setAlpha(enabled ? 1.0f : 0.5f);
valueTextView.setAlpha(enabled ? 1.0f : 0.5f);
checkBox.setAlpha(enabled ? 1.0f : 0.5f);
}
2015-12-09 19:27:52 +01:00
public void setChecked(boolean checked, boolean animated) {
2020-04-24 11:21:58 +02:00
if (checkBoxRound != null) {
checkBoxRound.setChecked(checked, animated);
} else {
checkBoxSquare.setChecked(checked, animated);
}
2015-12-09 19:27:52 +01:00
}
public boolean isChecked() {
2020-04-24 11:21:58 +02:00
if (checkBoxRound != null) {
return checkBoxRound.isChecked();
} else {
2020-12-23 08:48:30 +01:00
return checkBoxSquare.isChecked();
2020-04-24 11:21:58 +02:00
}
2015-12-09 19:27:52 +01:00
}
2017-03-31 01:58:05 +02:00
public TextView getTextView() {
return textView;
}
public TextView getValueTextView() {
return valueTextView;
}
2020-04-24 11:21:58 +02:00
public View getCheckBoxView() {
2017-03-31 01:58:05 +02:00
return checkBox;
}
2020-04-24 11:21:58 +02:00
public void setCheckBoxColor(String background, String background1, String check) {
if (checkBoxRound != null) {
checkBoxRound.setColor(background,background,check);
}
}
2015-12-09 19:27:52 +01:00
@Override
protected void onDraw(Canvas canvas) {
if (needDivider) {
2020-04-24 11:21:58 +02:00
int offset = currentType == TYPE_CHECK_BOX_ROUND ? 50 : 20;
canvas.drawLine(LocaleController.isRTL ? 0 : AndroidUtilities.dp(offset), getMeasuredHeight() - 1, getMeasuredWidth() - (LocaleController.isRTL ? AndroidUtilities.dp(offset) : 0), getMeasuredHeight() - 1, Theme.dividerPaint);
2015-12-09 19:27:52 +01:00
}
}
2019-05-14 14:08:05 +02:00
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName("android.widget.CheckBox");
info.setCheckable(true);
info.setChecked(isChecked());
}
2015-12-09 19:27:52 +01:00
}