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

255 lines
9.6 KiB
Java
Raw Normal View History

2015-06-29 19:12:11 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2015-06-29 19:12:11 +02: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-06-29 19:12:11 +02:00
*/
package org.telegram.ui.Cells;
import android.content.Context;
import android.graphics.Canvas;
2020-01-23 07:15:40 +01:00
import android.graphics.drawable.Drawable;
2015-06-29 19:12:11 +02:00
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.StaticLayout;
import android.text.style.ClickableSpan;
2016-03-06 02:49:31 +01:00
import android.text.style.URLSpan;
2015-06-29 19:12:11 +02:00
import android.view.MotionEvent;
import android.view.View;
2019-05-14 14:08:05 +02:00
import android.view.accessibility.AccessibilityNodeInfo;
2015-06-29 19:12:11 +02:00
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2015-11-26 22:04:02 +01:00
import org.telegram.messenger.Emoji;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessageObject;
2015-06-29 19:12:11 +02:00
import org.telegram.messenger.FileLog;
import org.telegram.messenger.R;
import org.telegram.ui.Components.LinkPath;
2016-04-22 15:49:00 +02:00
import org.telegram.ui.ActionBar.Theme;
2015-06-29 19:12:11 +02:00
import org.telegram.ui.Components.TypefaceSpan;
import org.telegram.ui.Components.URLSpanNoUnderline;
public class BotHelpCell extends View {
private StaticLayout textLayout;
private String oldText;
private int width;
private int height;
private int textX;
private int textY;
public boolean wasDraw;
2015-06-29 19:12:11 +02:00
private ClickableSpan pressedLink;
private LinkPath urlPath = new LinkPath();
private BotHelpCellDelegate delegate;
private boolean animating;
2015-06-29 19:12:11 +02:00
public interface BotHelpCellDelegate {
void didPressUrl(String url);
}
public BotHelpCell(Context context) {
super(context);
}
public void setDelegate(BotHelpCellDelegate botHelpCellDelegate) {
delegate = botHelpCellDelegate;
}
private void resetPressedLink() {
if (pressedLink != null) {
pressedLink = null;
}
invalidate();
}
2020-09-30 15:48:47 +02:00
public void setText(boolean bot, String text) {
2015-06-29 19:12:11 +02:00
if (text == null || text.length() == 0) {
setVisibility(GONE);
return;
}
2019-05-14 14:08:05 +02:00
if (text != null && text.equals(oldText)) {
2015-06-29 19:12:11 +02:00
return;
}
2021-07-30 16:49:55 +02:00
oldText = AndroidUtilities.getSafeString(text);
2015-06-29 19:12:11 +02:00
setVisibility(VISIBLE);
2017-03-31 01:58:05 +02:00
int maxWidth;
2015-06-29 19:12:11 +02:00
if (AndroidUtilities.isTablet()) {
2017-03-31 01:58:05 +02:00
maxWidth = (int) (AndroidUtilities.getMinTabletSide() * 0.7f);
2015-06-29 19:12:11 +02:00
} else {
2017-03-31 01:58:05 +02:00
maxWidth = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.7f);
2015-06-29 19:12:11 +02:00
}
2019-06-04 12:14:50 +02:00
String[] lines = text.split("\n");
2015-06-29 19:12:11 +02:00
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String help = LocaleController.getString("BotInfoTitle", R.string.BotInfoTitle);
2020-09-30 15:48:47 +02:00
if (bot) {
stringBuilder.append(help);
stringBuilder.append("\n\n");
}
2017-03-31 01:58:05 +02:00
for (int a = 0; a < lines.length; a++) {
stringBuilder.append(lines[a].trim());
if (a != lines.length - 1) {
stringBuilder.append("\n");
}
}
MessageObject.addLinks(false, stringBuilder);
2020-09-30 15:48:47 +02:00
if (bot) {
stringBuilder.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), 0, help.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
2017-03-31 01:58:05 +02:00
Emoji.replaceEmoji(stringBuilder, Theme.chat_msgTextPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
2016-03-16 13:26:32 +01:00
try {
2017-03-31 01:58:05 +02:00
textLayout = new StaticLayout(stringBuilder, Theme.chat_msgTextPaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
2016-03-16 13:26:32 +01:00
width = 0;
height = textLayout.getHeight() + AndroidUtilities.dp(4 + 18);
int count = textLayout.getLineCount();
for (int a = 0; a < count; a++) {
width = (int) Math.ceil(Math.max(width, textLayout.getLineWidth(a) + textLayout.getLineLeft(a)));
}
2017-03-31 01:58:05 +02:00
if (width > maxWidth) {
width = maxWidth;
}
2016-03-16 13:26:32 +01:00
} catch (Exception e) {
2018-07-30 04:07:02 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
width += AndroidUtilities.dp(4 + 18);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
boolean result = false;
if (textLayout != null) {
if (event.getAction() == MotionEvent.ACTION_DOWN || pressedLink != null && event.getAction() == MotionEvent.ACTION_UP) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
resetPressedLink();
try {
int x2 = (int) (x - textX);
int y2 = (int) (y - textY);
final int line = textLayout.getLineForVertical(y2);
final int off = textLayout.getOffsetForHorizontal(line, x2);
final float left = textLayout.getLineLeft(line);
if (left <= x2 && left + textLayout.getLineWidth(line) >= x2) {
Spannable buffer = (Spannable) textLayout.getText();
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
resetPressedLink();
pressedLink = link[0];
result = true;
try {
int start = buffer.getSpanStart(pressedLink);
2016-04-22 15:49:00 +02:00
urlPath.setCurrentLayout(textLayout, start, 0);
2015-06-29 19:12:11 +02:00
textLayout.getSelectionPath(start, buffer.getSpanEnd(pressedLink), urlPath);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
} else {
resetPressedLink();
}
} else {
resetPressedLink();
}
} catch (Exception e) {
resetPressedLink();
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
} else if (pressedLink != null) {
try {
if (pressedLink instanceof URLSpanNoUnderline) {
String url = ((URLSpanNoUnderline) pressedLink).getURL();
if (url.startsWith("@") || url.startsWith("#") || url.startsWith("/")) {
if (delegate != null) {
delegate.didPressUrl(url);
}
}
} else {
2016-03-06 02:49:31 +01:00
if (pressedLink instanceof URLSpan) {
if (delegate != null) {
delegate.didPressUrl(((URLSpan) pressedLink).getURL());
}
2016-03-06 02:49:31 +01:00
} else {
pressedLink.onClick(this);
}
2015-06-29 19:12:11 +02:00
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
resetPressedLink();
result = true;
}
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
resetPressedLink();
}
}
return result || super.onTouchEvent(event);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY), height + AndroidUtilities.dp(8));
}
@Override
protected void onDraw(Canvas canvas) {
2019-06-04 12:14:50 +02:00
int x = (getWidth() - width) / 2;
2020-01-23 07:15:40 +01:00
int y = AndroidUtilities.dp(2);
Drawable shadowDrawable = Theme.chat_msgInMediaDrawable.getShadowDrawable();
if (shadowDrawable != null) {
shadowDrawable.setBounds(x, y, width + x, height + y);
shadowDrawable.draw(canvas);
}
2021-08-31 21:06:39 +02:00
int w = AndroidUtilities.displaySize.x;
2020-01-23 07:15:40 +01:00
int h = AndroidUtilities.displaySize.y;
if (getParent() instanceof View) {
View view = (View) getParent();
2021-08-31 21:06:39 +02:00
w = view.getMeasuredWidth();
2020-01-23 07:15:40 +01:00
h = view.getMeasuredHeight();
}
2021-08-31 21:06:39 +02:00
Theme.chat_msgInMediaDrawable.setTop((int) getY(), w, h, false, false);
2017-03-31 01:58:05 +02:00
Theme.chat_msgInMediaDrawable.setBounds(x, y, width + x, height + y);
Theme.chat_msgInMediaDrawable.draw(canvas);
Theme.chat_msgTextPaint.setColor(Theme.getColor(Theme.key_chat_messageTextIn));
Theme.chat_msgTextPaint.linkColor = Theme.getColor(Theme.key_chat_messageLinkIn);
2015-06-29 19:12:11 +02:00
canvas.save();
2015-08-13 11:23:31 +02:00
canvas.translate(textX = AndroidUtilities.dp(2 + 9) + x, textY = AndroidUtilities.dp(2 + 9) + y);
2015-06-29 19:12:11 +02:00
if (pressedLink != null) {
2017-03-31 01:58:05 +02:00
canvas.drawPath(urlPath, Theme.chat_urlPaint);
2015-06-29 19:12:11 +02:00
}
2016-04-22 15:49:00 +02:00
if (textLayout != null) {
textLayout.draw(canvas);
}
2015-06-29 19:12:11 +02:00
canvas.restore();
wasDraw = true;
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
wasDraw = false;
2015-06-29 19:12:11 +02:00
}
2019-05-14 14:08:05 +02:00
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setText(textLayout.getText());
}
public boolean animating() {
return animating;
}
public void setAnimating(boolean animating) {
this.animating = animating;
}
2015-06-29 19:12:11 +02:00
}