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

232 lines
10 KiB
Java
Raw Normal View History

2017-12-08 18:35:59 +01:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2017-12-08 18:35:59 +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.
2017-12-08 18:35:59 +01:00
*/
package org.telegram.ui.Cells;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
2022-08-12 17:23:51 +02:00
import android.os.Build;
2018-07-30 04:07:02 +02:00
import android.text.SpannableStringBuilder;
import android.text.Spanned;
2017-12-08 18:35:59 +01:00
import android.text.TextUtils;
2018-07-30 04:07:02 +02:00
import android.text.style.ForegroundColorSpan;
2017-12-08 18:35:59 +01:00
import android.util.TypedValue;
import android.view.Gravity;
2020-04-24 11:21:58 +02:00
import android.view.View;
2017-12-08 18:35:59 +01:00
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import org.telegram.messenger.AndroidUtilities;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.Emoji;
2017-12-08 18:35:59 +01:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.ActionBar.ThemeDescription;
2018-07-30 04:07:02 +02:00
import org.telegram.ui.Components.ColorSpanUnderline;
2017-12-08 18:35:59 +01:00
import org.telegram.ui.Components.LayoutHelper;
import org.telegram.ui.Components.RecyclerListView;
import java.util.List;
2017-12-08 18:35:59 +01:00
public class StickerSetNameCell extends FrameLayout {
2022-08-12 17:23:51 +02:00
public int position;
2017-12-08 18:35:59 +01:00
private TextView textView;
2018-07-30 04:07:02 +02:00
private TextView urlTextView;
2017-12-08 18:35:59 +01:00
private ImageView buttonView;
2018-07-30 04:07:02 +02:00
private boolean empty;
2019-05-14 14:08:05 +02:00
private boolean isEmoji;
2017-12-08 18:35:59 +01:00
private CharSequence stickerSetName;
private int stickerSetNameSearchIndex;
private int stickerSetNameSearchLength;
private CharSequence url;
private int urlSearchLength;
2021-09-20 07:54:41 +02:00
private final Theme.ResourcesProvider resourcesProvider;
2021-09-20 07:54:41 +02:00
public StickerSetNameCell(Context context, boolean emoji, Theme.ResourcesProvider resourcesProvider) {
this(context, emoji, false, resourcesProvider);
2020-04-24 11:21:58 +02:00
}
2021-09-20 07:54:41 +02:00
public StickerSetNameCell(Context context, boolean emoji, boolean supportRtl, Theme.ResourcesProvider resourcesProvider) {
2017-12-08 18:35:59 +01:00
super(context);
2021-09-20 07:54:41 +02:00
this.resourcesProvider = resourcesProvider;
2017-12-08 18:35:59 +01:00
2019-05-14 14:08:05 +02:00
isEmoji = emoji;
2022-08-12 17:23:51 +02:00
LayoutParams lp;
2020-04-24 11:21:58 +02:00
2017-12-08 18:35:59 +01:00
textView = new TextView(context);
2021-09-20 07:54:41 +02:00
textView.setTextColor(getThemedColor(Theme.key_chat_emojiPanelStickerSetName));
2019-05-14 14:08:05 +02:00
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
2017-12-08 18:35:59 +01:00
textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setSingleLine(true);
2022-08-12 17:23:51 +02:00
if (emoji) {
textView.setGravity(Gravity.CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textView.setTextAlignment(TEXT_ALIGNMENT_CENTER);
}
}
2020-04-24 11:21:58 +02:00
if (supportRtl) {
2023-02-03 20:11:36 +01:00
lp = LayoutHelper.createFrameRelatively(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.START | Gravity.TOP, emoji ? 5 : 17, emoji ? 5 : 2, emoji ? 15 : 57, 0);
2020-04-24 11:21:58 +02:00
} else {
2023-02-03 20:11:36 +01:00
lp = LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, emoji ? 5 : 17, emoji ? 5 : 2, emoji ? 15 : 57, 0);
2020-04-24 11:21:58 +02:00
}
addView(textView, lp);
2017-12-08 18:35:59 +01:00
2018-07-30 04:07:02 +02:00
urlTextView = new TextView(context);
2021-09-20 07:54:41 +02:00
urlTextView.setTextColor(getThemedColor(Theme.key_chat_emojiPanelStickerSetName));
2018-07-30 04:07:02 +02:00
urlTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
urlTextView.setEllipsize(TextUtils.TruncateAt.END);
urlTextView.setSingleLine(true);
urlTextView.setVisibility(INVISIBLE);
2020-04-24 11:21:58 +02:00
if (supportRtl) {
2022-08-12 17:23:51 +02:00
lp = LayoutHelper.createFrameRelatively(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.END, 12, 6, 17, 0);
2020-04-24 11:21:58 +02:00
} else {
2022-08-12 17:23:51 +02:00
lp = LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT, 12, 6, 17, 0);
2020-04-24 11:21:58 +02:00
}
addView(urlTextView, lp);
2018-07-30 04:07:02 +02:00
2017-12-08 18:35:59 +01:00
buttonView = new ImageView(context);
buttonView.setScaleType(ImageView.ScaleType.CENTER);
2021-09-20 07:54:41 +02:00
buttonView.setColorFilter(new PorterDuffColorFilter(getThemedColor(Theme.key_chat_emojiPanelStickerSetNameIcon), PorterDuff.Mode.MULTIPLY));
2023-02-03 20:11:36 +01:00
buttonView.setBackground(Theme.createSelectorDrawable(getThemedColor(Theme.key_listSelector), Theme.RIPPLE_MASK_CIRCLE_TO_BOUND_EDGE));
2020-04-24 11:21:58 +02:00
if (supportRtl) {
2023-02-03 20:11:36 +01:00
lp = LayoutHelper.createFrameRelatively(24, 24, Gravity.TOP | Gravity.END, 0, 0, isEmoji ? 0 : 11, 0);
2020-04-24 11:21:58 +02:00
} else {
2023-02-03 20:11:36 +01:00
lp = LayoutHelper.createFrame(24, 24, Gravity.TOP | Gravity.RIGHT, 0, 0, isEmoji ? 0 : 11, 0);
}
if (isEmoji) {
buttonView.setTranslationY(AndroidUtilities.dp(4));
2020-04-24 11:21:58 +02:00
}
addView(buttonView, lp);
2017-12-08 18:35:59 +01:00
}
2018-07-30 04:07:02 +02:00
public void setUrl(CharSequence text, int searchLength) {
url = text;
urlSearchLength = searchLength;
urlTextView.setVisibility(text != null ? VISIBLE : GONE);
updateUrlSearchSpan();
}
private void updateUrlSearchSpan() {
if (url != null) {
SpannableStringBuilder builder = new SpannableStringBuilder(url);
2018-07-30 04:07:02 +02:00
try {
2021-09-20 07:54:41 +02:00
builder.setSpan(new ColorSpanUnderline(getThemedColor(Theme.key_chat_emojiPanelStickerSetNameHighlight)), 0, urlSearchLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(new ColorSpanUnderline(getThemedColor(Theme.key_chat_emojiPanelStickerSetName)), urlSearchLength, url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2018-07-30 04:07:02 +02:00
} catch (Exception ignore) {
}
urlTextView.setText(builder);
}
}
public void setText(CharSequence text, int resId) {
2022-06-21 04:51:00 +02:00
setText(text, resId, null, 0, 0);
}
public void setText(CharSequence text, int resId, CharSequence iconAccDescr) {
setText(text, resId, iconAccDescr, 0, 0);
2018-07-30 04:07:02 +02:00
}
public void setTitleColor(int color) {
textView.setTextColor(color);
}
2018-07-30 04:07:02 +02:00
public void setText(CharSequence text, int resId, int index, int searchLength) {
2022-06-21 04:51:00 +02:00
setText(text, resId, null, index, searchLength);
}
public void setText(CharSequence text, int resId, CharSequence iconAccDescr, int index, int searchLength) {
stickerSetName = text;
stickerSetNameSearchIndex = index;
stickerSetNameSearchLength = searchLength;
2018-07-30 04:07:02 +02:00
if (text == null) {
empty = true;
textView.setText("");
2017-12-08 18:35:59 +01:00
buttonView.setVisibility(INVISIBLE);
2018-07-30 04:07:02 +02:00
} else {
2022-08-12 17:23:51 +02:00
empty = false;
2018-07-30 04:07:02 +02:00
if (searchLength != 0) {
updateTextSearchSpan();
2018-07-30 04:07:02 +02:00
} else {
textView.setText(Emoji.replaceEmoji(text, textView.getPaint().getFontMetricsInt(), AndroidUtilities.dp(14), false));
}
if (resId != 0) {
buttonView.setImageResource(resId);
2022-06-21 04:51:00 +02:00
buttonView.setContentDescription(iconAccDescr);
2018-07-30 04:07:02 +02:00
buttonView.setVisibility(VISIBLE);
} else {
buttonView.setVisibility(INVISIBLE);
}
2017-12-08 18:35:59 +01:00
}
}
private void updateTextSearchSpan() {
if (stickerSetName != null && stickerSetNameSearchLength != 0) {
SpannableStringBuilder builder = new SpannableStringBuilder(stickerSetName);
try {
2021-09-20 07:54:41 +02:00
builder.setSpan(new ForegroundColorSpan(getThemedColor(Theme.key_chat_emojiPanelStickerSetNameHighlight)), stickerSetNameSearchIndex, stickerSetNameSearchIndex + stickerSetNameSearchLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception ignore) {
}
textView.setText(Emoji.replaceEmoji(builder, textView.getPaint().getFontMetricsInt(), AndroidUtilities.dp(14), false));
}
}
2017-12-08 18:35:59 +01:00
public void setOnIconClickListener(OnClickListener onIconClickListener) {
buttonView.setOnClickListener(onIconClickListener);
}
2018-07-30 04:07:02 +02:00
@Override
public void invalidate() {
textView.invalidate();
super.invalidate();
}
2017-12-08 18:35:59 +01:00
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2018-07-30 04:07:02 +02:00
if (empty) {
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(1, MeasureSpec.EXACTLY));
} else {
2023-02-03 20:11:36 +01:00
super.onMeasure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(isEmoji ? 27 : 24), MeasureSpec.EXACTLY));
2018-07-30 04:07:02 +02:00
}
2017-12-08 18:35:59 +01:00
}
2020-04-24 11:21:58 +02:00
@Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
if (child == urlTextView) {
widthUsed += textView.getMeasuredWidth() + AndroidUtilities.dp(16);
}
super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
}
public void updateColors() {
updateTextSearchSpan();
updateUrlSearchSpan();
}
public static void createThemeDescriptions(List<ThemeDescription> descriptions, RecyclerListView listView, ThemeDescription.ThemeDescriptionDelegate delegate) {
descriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_TEXTCOLOR, new Class[]{StickerSetNameCell.class}, new String[]{"textView"}, null, null, null, Theme.key_chat_emojiPanelStickerSetName));
descriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_TEXTCOLOR, new Class[]{StickerSetNameCell.class}, new String[]{"urlTextView"}, null, null, null, Theme.key_chat_emojiPanelStickerSetName));
descriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_IMAGECOLOR, new Class[]{StickerSetNameCell.class}, new String[]{"buttonView"}, null, null, null, Theme.key_chat_emojiPanelStickerSetNameIcon));
descriptions.add(new ThemeDescription(null, 0, null, null, null, delegate, Theme.key_chat_emojiPanelStickerSetNameHighlight));
descriptions.add(new ThemeDescription(null, 0, null, null, null, delegate, Theme.key_chat_emojiPanelStickerSetName));
}
2021-09-20 07:54:41 +02:00
private int getThemedColor(String key) {
Integer color = resourcesProvider != null ? resourcesProvider.getColor(key) : null;
return color != null ? color : Theme.getColor(key);
}
2022-08-12 17:23:51 +02:00
public TextView getTextView() {
return textView;
}
2017-12-08 18:35:59 +01:00
}