2021-06-25 02:43:10 +02:00
|
|
|
package org.telegram.ui.Components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Paint;
|
2021-07-19 17:56:43 +02:00
|
|
|
import android.graphics.RectF;
|
2021-06-25 02:43:10 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
2022-03-24 07:44:40 +01:00
|
|
|
import android.os.Build;
|
2021-06-25 02:43:10 +02:00
|
|
|
import android.text.Layout;
|
|
|
|
import android.text.StaticLayout;
|
|
|
|
import android.text.TextPaint;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2022-03-24 07:44:40 +01:00
|
|
|
import android.view.accessibility.AccessibilityNodeInfo;
|
2021-06-25 02:43:10 +02:00
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2021-09-20 00:10:42 +02:00
|
|
|
import androidx.collection.LongSparseArray;
|
2021-06-25 02:43:10 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
|
|
|
import org.telegram.messenger.LocaleController;
|
|
|
|
import org.telegram.messenger.R;
|
|
|
|
import org.telegram.tgnet.TLRPC;
|
|
|
|
import org.telegram.ui.ActionBar.MenuDrawable;
|
|
|
|
import org.telegram.ui.ActionBar.Theme;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class BotCommandsMenuView extends View {
|
|
|
|
|
2021-07-19 17:56:43 +02:00
|
|
|
final RectF rectTmp = new RectF();
|
2021-06-25 02:43:10 +02:00
|
|
|
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
final MenuDrawable backDrawable = new MenuDrawable() {
|
|
|
|
@Override
|
|
|
|
public void invalidateSelf() {
|
|
|
|
super.invalidateSelf();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
};
|
2022-04-16 16:43:17 +02:00
|
|
|
RLottieDrawable webViewAnimation = new RLottieDrawable(R.raw.bot_webview_sheet_to_cross, String.valueOf(R.raw.bot_webview_sheet_to_cross) + hashCode(), AndroidUtilities.dp(20), AndroidUtilities.dp(20)) {
|
|
|
|
@Override
|
|
|
|
public void invalidateSelf() {
|
|
|
|
super.invalidateSelf();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void invalidateInternal() {
|
|
|
|
super.invalidateInternal();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
};
|
2021-06-25 02:43:10 +02:00
|
|
|
boolean expanded;
|
|
|
|
float expandProgress;
|
|
|
|
|
2022-04-16 16:43:17 +02:00
|
|
|
private String menuText = LocaleController.getString(R.string.BotsMenuTitle);
|
|
|
|
StaticLayout menuTextLayout;
|
2021-06-25 02:43:10 +02:00
|
|
|
boolean isOpened;
|
|
|
|
|
2022-04-16 16:43:17 +02:00
|
|
|
boolean isWebView;
|
|
|
|
boolean isWebViewOpened;
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
Drawable backgroundDrawable;
|
2022-04-16 16:43:17 +02:00
|
|
|
boolean drawBackgroundDrawable = true;
|
2021-06-25 02:43:10 +02:00
|
|
|
|
|
|
|
public BotCommandsMenuView(Context context) {
|
|
|
|
super(context);
|
|
|
|
updateColors();
|
|
|
|
backDrawable.setMiniIcon(true);
|
|
|
|
backDrawable.setRotateToBack(false);
|
|
|
|
backDrawable.setRotation(0f, false);
|
|
|
|
backDrawable.setCallback(this);
|
|
|
|
textPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
|
|
|
|
backDrawable.setRoundCap();
|
2022-04-16 16:43:17 +02:00
|
|
|
backgroundDrawable = Theme.createSimpleSelectorRoundRectDrawable(AndroidUtilities.dp(16), Color.TRANSPARENT, Theme.getColor(Theme.key_featuredStickers_addButtonPressed));
|
2021-06-25 02:43:10 +02:00
|
|
|
backgroundDrawable.setCallback(this);
|
2022-06-21 04:51:00 +02:00
|
|
|
setContentDescription(LocaleController.getString("AccDescrBotMenu", R.string.AccDescrBotMenu));
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 16:43:17 +02:00
|
|
|
public void setDrawBackgroundDrawable(boolean drawBackgroundDrawable) {
|
|
|
|
this.drawBackgroundDrawable = drawBackgroundDrawable;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
|
|
|
|
webViewAnimation.addParentView(this);
|
|
|
|
webViewAnimation.setCurrentParentView(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
|
|
|
|
webViewAnimation.removeParentView(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setWebView(boolean webView) {
|
|
|
|
isWebView = webView;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
private void updateColors() {
|
|
|
|
paint.setColor(Theme.getColor(Theme.key_chat_messagePanelVoiceBackground));
|
2021-09-20 00:10:42 +02:00
|
|
|
int textColor = Theme.getColor(Theme.key_chat_messagePanelVoicePressed);
|
2021-06-25 02:43:10 +02:00
|
|
|
backDrawable.setBackColor(textColor);
|
|
|
|
backDrawable.setIconColor(textColor);
|
|
|
|
textPaint.setColor(textColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lastSize;
|
|
|
|
|
2022-03-24 07:44:40 +01:00
|
|
|
@Override
|
|
|
|
public CharSequence getAccessibilityClassName() {
|
|
|
|
return "android.Widget.Button";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
|
|
|
|
super.onInitializeAccessibilityNodeInfo(info);
|
|
|
|
info.addAction(isOpened()? AccessibilityNodeInfo.AccessibilityAction.ACTION_COLLAPSE : AccessibilityNodeInfo.AccessibilityAction.ACTION_EXPAND);
|
|
|
|
info.setContentDescription(LocaleController.getString("AccDescrBotCommands",R.string.AccDescrBotCommands));
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
@Override
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
int size = MeasureSpec.getSize(widthMeasureSpec) + MeasureSpec.getSize(heightMeasureSpec) << 16;
|
2022-04-16 16:43:17 +02:00
|
|
|
if (lastSize != size || menuTextLayout == null) {
|
2021-06-25 02:43:10 +02:00
|
|
|
backDrawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
|
|
|
|
textPaint.setTextSize(AndroidUtilities.dp(15));
|
|
|
|
lastSize = size;
|
2022-04-16 16:43:17 +02:00
|
|
|
int w = (int) textPaint.measureText(menuText);
|
|
|
|
menuTextLayout = StaticLayoutEx.createStaticLayout(menuText, textPaint, w, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, false, TextUtils.TruncateAt.END, w, 1);
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
2022-04-16 16:43:17 +02:00
|
|
|
onTranslationChanged((menuTextLayout.getWidth() + AndroidUtilities.dp(4)) * expandProgress);
|
2021-06-25 02:43:10 +02:00
|
|
|
int width = AndroidUtilities.dp(40);
|
|
|
|
if (expanded) {
|
2022-04-16 16:43:17 +02:00
|
|
|
width += menuTextLayout.getWidth() + AndroidUtilities.dp(4);
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(32), MeasureSpec.EXACTLY));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void dispatchDraw(Canvas canvas) {
|
2022-04-16 16:43:17 +02:00
|
|
|
if (menuTextLayout != null) {
|
2021-07-19 17:56:43 +02:00
|
|
|
boolean update = false;
|
|
|
|
if (expanded && expandProgress != 1f) {
|
|
|
|
expandProgress += 16f / 150f;
|
|
|
|
if (expandProgress > 1) {
|
|
|
|
expandProgress = 1f;
|
|
|
|
} else {
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
update = true;
|
|
|
|
} else if (!expanded && expandProgress != 0) {
|
|
|
|
expandProgress -= 16f / 150f;
|
|
|
|
if (expandProgress < 0) {
|
|
|
|
expandProgress = 0;
|
|
|
|
} else {
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
update = true;
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 17:56:43 +02:00
|
|
|
float expandProgress = CubicBezierInterpolator.DEFAULT.getInterpolation(this.expandProgress);
|
|
|
|
if (update && expandProgress > 0) {
|
|
|
|
textPaint.setAlpha((int) (255 * expandProgress));
|
|
|
|
}
|
2022-04-16 16:43:17 +02:00
|
|
|
|
|
|
|
if (drawBackgroundDrawable) {
|
|
|
|
rectTmp.set(0, 0, AndroidUtilities.dp(40) + (menuTextLayout.getWidth() + AndroidUtilities.dp(4)) * expandProgress, getMeasuredHeight());
|
|
|
|
canvas.drawRoundRect(rectTmp, AndroidUtilities.dp(16), AndroidUtilities.dp(16), paint);
|
|
|
|
backgroundDrawable.setBounds((int) rectTmp.left, (int) rectTmp.top, (int) rectTmp.right, (int) rectTmp.bottom);
|
|
|
|
backgroundDrawable.draw(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isWebView) {
|
|
|
|
canvas.save();
|
|
|
|
canvas.translate(AndroidUtilities.dp(9.5f), AndroidUtilities.dp(6));
|
|
|
|
RLottieDrawable drawable = webViewAnimation;
|
|
|
|
drawable.setBounds(0, 0, drawable.width, drawable.height);
|
|
|
|
drawable.draw(canvas);
|
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
if (drawable.isRunning()) {
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
canvas.save();
|
|
|
|
canvas.translate(AndroidUtilities.dp(8), AndroidUtilities.dp(4));
|
|
|
|
backDrawable.draw(canvas);
|
|
|
|
canvas.restore();
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
|
2021-07-19 17:56:43 +02:00
|
|
|
if (expandProgress > 0) {
|
|
|
|
canvas.save();
|
2022-04-16 16:43:17 +02:00
|
|
|
canvas.translate(AndroidUtilities.dp(34), (getMeasuredHeight() - menuTextLayout.getHeight()) / 2f);
|
|
|
|
menuTextLayout.draw(canvas);
|
2021-07-19 17:56:43 +02:00
|
|
|
canvas.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update) {
|
2022-04-16 16:43:17 +02:00
|
|
|
onTranslationChanged((menuTextLayout.getWidth() + AndroidUtilities.dp(4)) * expandProgress);
|
2021-07-19 17:56:43 +02:00
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
super.dispatchDraw(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void onTranslationChanged(float translationX) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:51:00 +02:00
|
|
|
public boolean setMenuText(String menuText) {
|
2022-04-21 20:03:20 +02:00
|
|
|
if (menuText == null) {
|
|
|
|
menuText = LocaleController.getString(R.string.BotsMenuTitle);
|
|
|
|
}
|
2022-06-21 04:51:00 +02:00
|
|
|
boolean changed = this.menuText == null || !this.menuText.equals(menuText);
|
2022-04-16 16:43:17 +02:00
|
|
|
this.menuText = menuText;
|
|
|
|
menuTextLayout = null;
|
|
|
|
requestLayout();
|
2022-06-21 04:51:00 +02:00
|
|
|
return changed;
|
2022-04-16 16:43:17 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
public void setExpanded(boolean expanded, boolean animated) {
|
|
|
|
if (this.expanded != expanded) {
|
|
|
|
this.expanded = expanded;
|
|
|
|
if (!animated) {
|
|
|
|
expandProgress = expanded ? 1f : 0f;
|
|
|
|
}
|
|
|
|
requestLayout();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOpened() {
|
|
|
|
return isOpened;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class BotCommandsAdapter extends RecyclerListView.SelectionAdapter {
|
|
|
|
|
|
|
|
ArrayList<String> newResult = new ArrayList<>();
|
|
|
|
ArrayList<String> newResultHelp = new ArrayList<>();
|
|
|
|
|
|
|
|
public BotCommandsAdapter() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled(RecyclerView.ViewHolder holder) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
// FlickerLoadingView flickerLoadingView = new FlickerLoadingView(parent.getContext());
|
|
|
|
// flickerLoadingView.setIsSingleCell(true);
|
|
|
|
// flickerLoadingView.setViewType(FlickerLoadingView.BOTS_MENU_TYPE);
|
|
|
|
// return new RecyclerListView.Holder(flickerLoadingView);
|
|
|
|
BotCommandView view = new BotCommandView(parent.getContext());
|
|
|
|
view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
|
|
|
return new RecyclerListView.Holder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
|
|
|
BotCommandView view = (BotCommandView) holder.itemView;
|
|
|
|
view.command.setText(newResult.get(position));
|
|
|
|
view.description.setText(newResultHelp.get(position));
|
|
|
|
view.commandStr = newResult.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return newResult.size();
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:10:42 +02:00
|
|
|
public void setBotInfo(LongSparseArray<TLRPC.BotInfo> botInfo) {
|
2021-06-25 02:43:10 +02:00
|
|
|
newResult.clear();
|
|
|
|
newResultHelp.clear();
|
|
|
|
for (int b = 0; b < botInfo.size(); b++) {
|
|
|
|
TLRPC.BotInfo info = botInfo.valueAt(b);
|
|
|
|
for (int a = 0; a < info.commands.size(); a++) {
|
|
|
|
TLRPC.TL_botCommand botCommand = info.commands.get(a);
|
|
|
|
if (botCommand != null && botCommand.command != null) {
|
|
|
|
newResult.add("/" + botCommand.command);
|
|
|
|
if (botCommand.description != null && botCommand.description.length() > 1) {
|
|
|
|
newResultHelp.add(botCommand.description.substring(0, 1).toUpperCase() + botCommand.description.substring(1).toLowerCase());
|
|
|
|
} else {
|
|
|
|
newResultHelp.add(botCommand.description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOpened(boolean opened) {
|
|
|
|
if (isOpened != opened) {
|
|
|
|
isOpened = opened;
|
|
|
|
}
|
2022-04-16 16:43:17 +02:00
|
|
|
if (isWebView) {
|
|
|
|
if (isWebViewOpened != opened) {
|
|
|
|
RLottieDrawable drawable = webViewAnimation;
|
|
|
|
if (!drawable.hasParentView()) {
|
|
|
|
drawable.addParentView(this);
|
|
|
|
}
|
|
|
|
drawable.stop();
|
|
|
|
drawable.setPlayInDirectionOfCustomEndFrame(true);
|
|
|
|
drawable.setCustomEndFrame(opened ? drawable.getFramesCount() : 1);
|
|
|
|
drawable.start();
|
|
|
|
isWebViewOpened = opened;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
backDrawable.setRotation(opened ? 1f : 0f, true);
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class BotCommandView extends LinearLayout {
|
|
|
|
|
|
|
|
TextView command;
|
|
|
|
TextView description;
|
|
|
|
String commandStr;
|
|
|
|
|
|
|
|
public BotCommandView(@NonNull Context context) {
|
|
|
|
super(context);
|
|
|
|
setOrientation(HORIZONTAL);
|
|
|
|
setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
|
|
|
|
|
|
|
|
description = new TextView(context);
|
|
|
|
description.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
|
|
|
|
description.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
|
|
|
|
description.setTag(Theme.key_windowBackgroundWhiteBlackText);
|
|
|
|
description.setLines(1);
|
|
|
|
description.setEllipsize(TextUtils.TruncateAt.END);
|
|
|
|
addView(description, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 1f, Gravity.CENTER_VERTICAL, 0, 0, AndroidUtilities.dp(8), 0));
|
|
|
|
|
|
|
|
command = new TextView(context);
|
|
|
|
command.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
|
|
|
|
command.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText));
|
|
|
|
command.setTag(Theme.key_windowBackgroundWhiteGrayText);
|
|
|
|
addView(command, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, 0f, Gravity.CENTER_VERTICAL));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(36), MeasureSpec.EXACTLY));
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getCommand() {
|
|
|
|
return commandStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean verifyDrawable(@NonNull Drawable who) {
|
|
|
|
return super.verifyDrawable(who) || backgroundDrawable == who;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void drawableStateChanged() {
|
|
|
|
super.drawableStateChanged();
|
|
|
|
backgroundDrawable.setState(getDrawableState());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void jumpDrawablesToCurrentState() {
|
|
|
|
super.jumpDrawablesToCurrentState();
|
|
|
|
backgroundDrawable.jumpToCurrentState();
|
|
|
|
}
|
|
|
|
}
|