NekoX/TMessagesProj/src/main/java/org/telegram/ui/Components/BotKeyboardView.java

166 lines
7.4 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.Components;
import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
2022-04-16 16:43:17 +02:00
import android.widget.FrameLayout;
import android.widget.ImageView;
2015-06-29 19:12:11 +02:00
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.Emoji;
2022-04-16 16:43:17 +02:00
import org.telegram.messenger.R;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLRPC;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
2015-06-29 19:12:11 +02:00
import java.util.ArrayList;
public class BotKeyboardView extends LinearLayout {
2021-09-20 00:10:42 +02:00
private final Theme.ResourcesProvider resourcesProvider;
2015-06-29 19:12:11 +02:00
private LinearLayout container;
private TLRPC.TL_replyKeyboardMarkup botButtons;
private BotKeyboardViewDelegate delegate;
private int panelHeight;
private boolean isFullSize;
private int buttonHeight;
private ArrayList<TextView> buttonViews = new ArrayList<>();
2022-04-16 16:43:17 +02:00
private ArrayList<ImageView> buttonIcons = new ArrayList<>();
2017-12-08 18:35:59 +01:00
private ScrollView scrollView;
2015-06-29 19:12:11 +02:00
public interface BotKeyboardViewDelegate {
2016-04-22 15:49:00 +02:00
void didPressedButton(TLRPC.KeyboardButton button);
2015-06-29 19:12:11 +02:00
}
2021-09-20 00:10:42 +02:00
public BotKeyboardView(Context context, Theme.ResourcesProvider resourcesProvider) {
2015-06-29 19:12:11 +02:00
super(context);
2021-09-20 00:10:42 +02:00
this.resourcesProvider = resourcesProvider;
2015-06-29 19:12:11 +02:00
setOrientation(VERTICAL);
2017-12-08 18:35:59 +01:00
scrollView = new ScrollView(context);
2015-06-29 19:12:11 +02:00
addView(scrollView);
container = new LinearLayout(context);
container.setOrientation(VERTICAL);
scrollView.addView(container);
2021-06-25 02:43:10 +02:00
updateColors();
}
2015-06-29 19:12:11 +02:00
2021-06-25 02:43:10 +02:00
public void updateColors() {
2021-09-20 00:10:42 +02:00
AndroidUtilities.setScrollViewEdgeEffectColor(scrollView, getThemedColor(Theme.key_chat_emojiPanelBackground));
setBackgroundColor(getThemedColor(Theme.key_chat_emojiPanelBackground));
2021-06-25 02:43:10 +02:00
for (int i = 0; i < buttonViews.size(); i++) {
2021-09-20 00:10:42 +02:00
buttonViews.get(i).setTextColor(getThemedColor(Theme.key_chat_botKeyboardButtonText));
2022-04-16 16:43:17 +02:00
buttonViews.get(i).setBackground(Theme.createSimpleSelectorRoundRectDrawable(AndroidUtilities.dp(4), getThemedColor(Theme.key_chat_botKeyboardButtonBackground), getThemedColor(Theme.key_chat_botKeyboardButtonBackgroundPressed)));
buttonIcons.get(i).setColorFilter(getThemedColor(Theme.key_chat_botKeyboardButtonText));
2021-06-25 02:43:10 +02:00
}
invalidate();
2015-06-29 19:12:11 +02:00
}
public void setDelegate(BotKeyboardViewDelegate botKeyboardViewDelegate) {
delegate = botKeyboardViewDelegate;
}
public void setPanelHeight(int height) {
panelHeight = height;
2015-07-22 20:56:37 +02:00
if (isFullSize && botButtons != null && botButtons.rows.size() != 0) {
2015-06-29 19:12:11 +02:00
buttonHeight = !isFullSize ? 42 : (int) Math.max(42, (panelHeight - AndroidUtilities.dp(30) - (botButtons.rows.size() - 1) * AndroidUtilities.dp(10)) / botButtons.rows.size() / AndroidUtilities.density);
int count = container.getChildCount();
int newHeight = AndroidUtilities.dp(buttonHeight);
for (int a = 0; a < count; a++) {
View v = container.getChildAt(a);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
if (layoutParams.height != newHeight) {
layoutParams.height = newHeight;
v.setLayoutParams(layoutParams);
}
}
}
}
public void invalidateViews() {
for (int a = 0; a < buttonViews.size(); a++) {
buttonViews.get(a).invalidate();
2022-04-16 16:43:17 +02:00
buttonIcons.get(a).invalidate();
2015-06-29 19:12:11 +02:00
}
}
public boolean isFullSize() {
return isFullSize;
}
public void setButtons(TLRPC.TL_replyKeyboardMarkup buttons) {
botButtons = buttons;
container.removeAllViews();
buttonViews.clear();
2022-04-16 16:43:17 +02:00
buttonIcons.clear();
2017-12-08 18:35:59 +01:00
scrollView.scrollTo(0, 0);
2015-06-29 19:12:11 +02:00
2015-07-22 20:56:37 +02:00
if (buttons != null && botButtons.rows.size() != 0) {
2015-11-26 22:04:02 +01:00
isFullSize = !buttons.resize;
2015-06-29 19:12:11 +02:00
buttonHeight = !isFullSize ? 42 : (int) Math.max(42, (panelHeight - AndroidUtilities.dp(30) - (botButtons.rows.size() - 1) * AndroidUtilities.dp(10)) / botButtons.rows.size() / AndroidUtilities.density);
for (int a = 0; a < buttons.rows.size(); a++) {
TLRPC.TL_keyboardButtonRow row = buttons.rows.get(a);
LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.HORIZONTAL);
container.addView(layout, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, buttonHeight, 15, a == 0 ? 15 : 10, 15, a == buttons.rows.size() - 1 ? 15 : 0));
float weight = 1.0f / row.buttons.size();
for (int b = 0; b < row.buttons.size(); b++) {
2016-04-22 15:49:00 +02:00
TLRPC.KeyboardButton button = row.buttons.get(b);
2021-03-21 11:24:16 +01:00
TextView textView = new EmojiTextView(getContext());
2016-04-22 15:49:00 +02:00
textView.setTag(button);
2021-09-20 00:10:42 +02:00
textView.setTextColor(getThemedColor(Theme.key_chat_botKeyboardButtonText));
2022-04-16 16:43:17 +02:00
textView.setBackground(Theme.createSimpleSelectorRoundRectDrawable(AndroidUtilities.dp(4), getThemedColor(Theme.key_chat_botKeyboardButtonBackground), getThemedColor(Theme.key_chat_botKeyboardButtonBackgroundPressed)));
2015-06-29 19:12:11 +02:00
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setGravity(Gravity.CENTER);
2021-06-25 02:43:10 +02:00
2022-04-16 16:43:17 +02:00
FrameLayout frame = new FrameLayout(getContext());
frame.addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
2015-06-29 19:12:11 +02:00
textView.setPadding(AndroidUtilities.dp(4), 0, AndroidUtilities.dp(4), 0);
2021-03-21 11:24:16 +01:00
textView.setText(button.text);
2022-04-16 16:43:17 +02:00
layout.addView(frame, LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT, weight, 0, 0, b != row.buttons.size() - 1 ? 10 : 0, 0));
2019-12-31 14:08:08 +01:00
textView.setOnClickListener(v -> delegate.didPressedButton((TLRPC.KeyboardButton) v.getTag()));
2015-06-29 19:12:11 +02:00
buttonViews.add(textView);
2022-04-16 16:43:17 +02:00
ImageView icon = new ImageView(getContext());
icon.setColorFilter(getThemedColor(Theme.key_chat_botKeyboardButtonText));
if (button instanceof TLRPC.TL_keyboardButtonWebView || button instanceof TLRPC.TL_keyboardButtonSimpleWebView) {
icon.setImageResource(R.drawable.bot_webview);
icon.setVisibility(VISIBLE);
} else {
icon.setVisibility(GONE);
}
buttonIcons.add(icon);
frame.addView(icon, LayoutHelper.createFrame(12, 12, Gravity.RIGHT | Gravity.TOP, 0, 8, 8, 0));
2015-06-29 19:12:11 +02:00
}
}
}
}
public int getKeyboardHeight() {
2020-09-30 15:48:47 +02:00
if (botButtons == null) {
return 0;
}
2015-06-29 19:12:11 +02:00
return isFullSize ? panelHeight : botButtons.rows.size() * AndroidUtilities.dp(buttonHeight) + AndroidUtilities.dp(30) + (botButtons.rows.size() - 1) * AndroidUtilities.dp(10);
}
2021-09-20 00:10:42 +02:00
private int getThemedColor(String key) {
Integer color = resourcesProvider != null ? resourcesProvider.getColor(key) : null;
return color != null ? color : Theme.getColor(key);
}
2015-06-29 19:12:11 +02:00
}