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

465 lines
19 KiB
Java
Raw Normal View History

2019-05-14 14:08:05 +02:00
package org.telegram.ui.Components;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
2021-03-19 11:25:58 +01:00
import android.graphics.drawable.Drawable;
2019-05-14 14:08:05 +02:00
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ImageReceiver;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessageObject;
import org.telegram.messenger.R;
2020-04-24 11:21:58 +02:00
import org.telegram.tgnet.TLRPC;
2019-12-31 14:08:08 +01:00
import org.telegram.ui.ActionBar.SimpleTextView;
2019-05-14 14:08:05 +02:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Cells.ChatMessageCell;
@SuppressWarnings("FieldCanBeLocal")
public class HintView extends FrameLayout {
2019-12-31 14:08:08 +01:00
public static final int TYPE_SEARCH_AS_LIST = 3;
2020-01-23 07:15:40 +01:00
public static final int TYPE_POLL_VOTE = 5;
2019-12-31 14:08:08 +01:00
2019-05-14 14:08:05 +02:00
private TextView textView;
private ImageView imageView;
private ImageView arrowImageView;
private ChatMessageCell messageCell;
2019-08-22 01:53:26 +02:00
private View currentView;
2019-05-14 14:08:05 +02:00
private AnimatorSet animatorSet;
private Runnable hideRunnable;
private int currentType;
private boolean isTopArrow;
private String overrideText;
2020-01-23 07:15:40 +01:00
private int shownY;
2020-10-30 11:26:29 +01:00
private float translationY;
private float extraTranslationY;
2020-01-23 07:15:40 +01:00
2020-08-14 18:58:22 +02:00
private int bottomOffset;
2020-01-23 07:15:40 +01:00
private long showingDuration = 2000;
2019-05-14 14:08:05 +02:00
public HintView(Context context, int type) {
this(context, type, false);
}
public HintView(Context context, int type, boolean topArrow) {
super(context);
currentType = type;
isTopArrow = topArrow;
textView = new CorrectlyMeasuringTextView(context);
textView.setTextColor(Theme.getColor(Theme.key_chat_gifSaveHintText));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
textView.setMaxLines(2);
2021-03-19 11:25:58 +01:00
if (type == 7 || type == 8 || type == 9) {
2021-02-23 12:53:38 +01:00
textView.setMaxWidth(AndroidUtilities.dp(310));
} else if (type == 4) {
textView.setMaxWidth(AndroidUtilities.dp(280));
} else {
textView.setMaxWidth(AndroidUtilities.dp(250));
}
2019-12-31 14:08:08 +01:00
if (currentType == TYPE_SEARCH_AS_LIST) {
textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
2020-01-23 07:15:40 +01:00
textView.setBackground(Theme.createRoundRectDrawable(AndroidUtilities.dp(5), Theme.getColor(Theme.key_chat_gifSaveHintBackground)));
2019-12-31 14:08:08 +01:00
textView.setPadding(AndroidUtilities.dp(10), 0, AndroidUtilities.dp(10), 0);
addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, 30, Gravity.LEFT | Gravity.TOP, 0, topArrow ? 6 : 0, 0, topArrow ? 0 : 6));
2019-08-22 01:53:26 +02:00
} else {
2019-12-31 14:08:08 +01:00
textView.setGravity(Gravity.LEFT | Gravity.TOP);
2021-03-19 11:25:58 +01:00
textView.setBackground(Theme.createRoundRectDrawable(AndroidUtilities.dp(currentType == 7 || currentType == 8 || currentType == 9 ? 6 : 3), Theme.getColor(Theme.key_chat_gifSaveHintBackground)));
2020-01-23 07:15:40 +01:00
if (currentType == TYPE_POLL_VOTE || currentType == 4) {
textView.setPadding(AndroidUtilities.dp(9), AndroidUtilities.dp(6), AndroidUtilities.dp(9), AndroidUtilities.dp(7));
} else if (currentType == 2) {
2019-12-31 14:08:08 +01:00
textView.setPadding(AndroidUtilities.dp(7), AndroidUtilities.dp(6), AndroidUtilities.dp(7), AndroidUtilities.dp(7));
2021-03-19 11:25:58 +01:00
} else if (currentType == 7 || currentType == 8 || currentType == 9) {
2021-02-23 12:53:38 +01:00
textView.setPadding(AndroidUtilities.dp(8), AndroidUtilities.dp(7), AndroidUtilities.dp(8), AndroidUtilities.dp(8));
2019-12-31 14:08:08 +01:00
} else {
textView.setPadding(AndroidUtilities.dp(currentType == 0 ? 54 : 5), AndroidUtilities.dp(6), AndroidUtilities.dp(5), AndroidUtilities.dp(7));
}
addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, 0, topArrow ? 6 : 0, 0, topArrow ? 0 : 6));
2019-08-22 01:53:26 +02:00
}
2019-05-14 14:08:05 +02:00
if (type == 0) {
textView.setText(LocaleController.getString("AutoplayVideoInfo", R.string.AutoplayVideoInfo));
imageView = new ImageView(context);
imageView.setImageResource(R.drawable.tooltip_sound);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chat_gifSaveHintText), PorterDuff.Mode.MULTIPLY));
addView(imageView, LayoutHelper.createFrame(38, 34, Gravity.LEFT | Gravity.TOP, 7, 7, 0, 0));
}
arrowImageView = new ImageView(context);
arrowImageView.setImageResource(topArrow ? R.drawable.tooltip_arrow_up : R.drawable.tooltip_arrow);
arrowImageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chat_gifSaveHintBackground), PorterDuff.Mode.MULTIPLY));
addView(arrowImageView, LayoutHelper.createFrame(14, 6, Gravity.LEFT | (topArrow ? Gravity.TOP : Gravity.BOTTOM), 0, 0, 0, 0));
}
2021-03-19 11:25:58 +01:00
public void setBackgroundColor(int background, int text) {
textView.setTextColor(text);
arrowImageView.setColorFilter(new PorterDuffColorFilter(background, PorterDuff.Mode.MULTIPLY));
textView.setBackground(Theme.createRoundRectDrawable(AndroidUtilities.dp(currentType == 7 || currentType == 8 ? 6 : 3), background));
}
2019-05-14 14:08:05 +02:00
public void setOverrideText(String text) {
overrideText = text;
textView.setText(text);
if (messageCell != null) {
ChatMessageCell cell = messageCell;
messageCell = null;
showForMessageCell(cell, false);
}
}
2020-10-30 11:26:29 +01:00
public void setExtraTranslationY(float value) {
extraTranslationY = value;
setTranslationY(extraTranslationY + translationY);
}
public float getBaseTranslationY() {
return translationY;
}
2019-05-14 14:08:05 +02:00
public boolean showForMessageCell(ChatMessageCell cell, boolean animated) {
2020-01-23 07:15:40 +01:00
return showForMessageCell(cell, null, 0, 0, animated);
}
public boolean showForMessageCell(ChatMessageCell cell, Object object, int x, int y, boolean animated) {
if (currentType == TYPE_POLL_VOTE && y == shownY && messageCell == cell || currentType != TYPE_POLL_VOTE && (currentType == 0 && getTag() != null || messageCell == cell)) {
2019-05-14 14:08:05 +02:00
return false;
}
if (hideRunnable != null) {
AndroidUtilities.cancelRunOnUIThread(hideRunnable);
hideRunnable = null;
}
2020-01-23 07:15:40 +01:00
int[] position = new int[2];
cell.getLocationInWindow(position);
int top = position[1];
View p = (View) getParent();
p.getLocationInWindow(position);
top -= position[1];
2019-05-14 14:08:05 +02:00
View parentView = (View) cell.getParent();
2020-01-23 07:15:40 +01:00
int centerX;
2019-05-14 14:08:05 +02:00
if (currentType == 0) {
ImageReceiver imageReceiver = cell.getPhotoImage();
top += imageReceiver.getImageY();
int height = (int) imageReceiver.getImageHeight();
2019-05-14 14:08:05 +02:00
int bottom = top + height;
int parentHeight = parentView.getMeasuredHeight();
if (top <= getMeasuredHeight() + AndroidUtilities.dp(10) || bottom > parentHeight + height / 4) {
return false;
}
centerX = cell.getNoSoundIconCenterX();
2020-01-23 07:15:40 +01:00
} else if (currentType == TYPE_POLL_VOTE) {
Integer count = (Integer) object;
centerX = x;
top += y;
shownY = y;
if (count == -1) {
textView.setText(LocaleController.getString("PollSelectOption", R.string.PollSelectOption));
} else {
if (cell.getMessageObject().isQuiz()) {
if (count == 0) {
textView.setText(LocaleController.getString("NoVotesQuiz", R.string.NoVotesQuiz));
} else {
textView.setText(LocaleController.formatPluralString("Answer", count));
}
} else {
if (count == 0) {
textView.setText(LocaleController.getString("NoVotes", R.string.NoVotes));
} else {
textView.setText(LocaleController.formatPluralString("Vote", count));
}
}
}
measure(MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST));
2019-05-14 14:08:05 +02:00
} else {
MessageObject messageObject = cell.getMessageObject();
if (overrideText == null) {
textView.setText(LocaleController.getString("HidAccount", R.string.HidAccount));
} else {
textView.setText(overrideText);
}
measure(MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST));
2020-04-24 11:21:58 +02:00
TLRPC.User user = cell.getCurrentUser();
if (user != null && user.id == 0) {
top += (cell.getMeasuredHeight() - Math.max(0, cell.getBottom() - parentView.getMeasuredHeight()) - AndroidUtilities.dp(50));
} else {
top += AndroidUtilities.dp(22);
if (!messageObject.isOutOwner() && cell.isDrawNameLayout()) {
top += AndroidUtilities.dp(20);
}
2019-05-14 14:08:05 +02:00
}
if (!isTopArrow && top <= getMeasuredHeight() + AndroidUtilities.dp(10)) {
return false;
}
centerX = cell.getForwardNameCenterX();
}
int parentWidth = parentView.getMeasuredWidth();
if (isTopArrow) {
2020-10-30 11:26:29 +01:00
setTranslationY(extraTranslationY + (translationY = AndroidUtilities.dp(44)));
2019-05-14 14:08:05 +02:00
} else {
2020-10-30 11:26:29 +01:00
setTranslationY(extraTranslationY + (translationY = top - getMeasuredHeight()));
2019-05-14 14:08:05 +02:00
}
int iconX = cell.getLeft() + centerX;
int left = AndroidUtilities.dp(19);
2020-01-23 07:15:40 +01:00
if (currentType == TYPE_POLL_VOTE) {
int offset = Math.max(0, centerX - getMeasuredWidth() / 2 - AndroidUtilities.dp(19.1f));
setTranslationX(offset);
left += offset;
} else if (iconX > parentView.getMeasuredWidth() / 2) {
2019-05-14 14:08:05 +02:00
int offset = parentWidth - getMeasuredWidth() - AndroidUtilities.dp(38);
setTranslationX(offset);
left += offset;
} else {
setTranslationX(0);
}
float arrowX = cell.getLeft() + centerX - left - arrowImageView.getMeasuredWidth() / 2;
arrowImageView.setTranslationX(arrowX);
if (iconX > parentView.getMeasuredWidth() / 2) {
if (arrowX < AndroidUtilities.dp(10)) {
float diff = arrowX - AndroidUtilities.dp(10);
setTranslationX(getTranslationX() + diff);
arrowImageView.setTranslationX(arrowX - diff);
}
} else {
if (arrowX > getMeasuredWidth() - AndroidUtilities.dp(14 + 10)) {
float diff = arrowX - getMeasuredWidth() + AndroidUtilities.dp(14 + 10);
setTranslationX(diff);
arrowImageView.setTranslationX(arrowX - diff);
} else if (arrowX < AndroidUtilities.dp(10)) {
float diff = arrowX - AndroidUtilities.dp(10);
setTranslationX(getTranslationX() + diff);
arrowImageView.setTranslationX(arrowX - diff);
}
}
messageCell = cell;
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
setTag(1);
setVisibility(VISIBLE);
if (animated) {
animatorSet = new AnimatorSet();
animatorSet.playTogether(
2020-01-23 07:15:40 +01:00
ObjectAnimator.ofFloat(this, View.ALPHA, 0.0f, 1.0f)
2019-05-14 14:08:05 +02:00
);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animatorSet = null;
AndroidUtilities.runOnUIThread(hideRunnable = () -> hide(), currentType == 0 ? 10000 : 2000);
}
});
animatorSet.setDuration(300);
animatorSet.start();
} else {
setAlpha(1.0f);
}
return true;
}
2019-08-22 01:53:26 +02:00
public boolean showForView(View view, boolean animated) {
if (currentView == view || getTag() != null) {
2021-03-19 11:25:58 +01:00
if (getTag() != null) {
updatePosition(view);
}
2019-08-22 01:53:26 +02:00
return false;
}
if (hideRunnable != null) {
AndroidUtilities.cancelRunOnUIThread(hideRunnable);
hideRunnable = null;
}
2021-03-19 11:25:58 +01:00
updatePosition(view);
currentView = view;
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
setTag(1);
setVisibility(VISIBLE);
if (animated) {
animatorSet = new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(this, View.ALPHA, 0.0f, 1.0f)
);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animatorSet = null;
AndroidUtilities.runOnUIThread(hideRunnable = () -> hide(), showingDuration);
}
});
animatorSet.setDuration(300);
animatorSet.start();
} else {
setAlpha(1.0f);
}
return true;
}
private void updatePosition(View view) {
2020-01-23 07:15:40 +01:00
measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.displaySize.x, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.displaySize.x, MeasureSpec.AT_MOST));
2019-08-22 01:53:26 +02:00
int[] position = new int[2];
view.getLocationInWindow(position);
int top = position[1] - AndroidUtilities.dp(4);
2020-01-23 07:15:40 +01:00
if (currentType == 4) {
top += AndroidUtilities.dp(4);
2020-10-30 11:26:29 +01:00
} else if (currentType == 6) {
top += view.getMeasuredHeight() + getMeasuredHeight() + AndroidUtilities.dp(10);
2021-04-14 03:44:46 +02:00
} else if (currentType == 7 || currentType == 8 && isTopArrow) {
2021-02-23 12:53:38 +01:00
top += view.getMeasuredHeight() + getMeasuredHeight() + AndroidUtilities.dp(8);
2021-04-14 03:44:46 +02:00
} else if (currentType == 8) {
top -= AndroidUtilities.dp(10);
2020-01-23 07:15:40 +01:00
}
2019-12-31 14:08:08 +01:00
int centerX;
2021-04-14 03:44:46 +02:00
if (currentType == 8 && isTopArrow) {
2021-03-19 11:25:58 +01:00
if (view instanceof SimpleTextView) {
SimpleTextView textView = (SimpleTextView) view;
Drawable drawable = textView.getRightDrawable();
centerX = position[0] + (drawable != null ? drawable.getBounds().centerX() : textView.getTextWidth() / 2) - AndroidUtilities.dp(8);
} else if (view instanceof TextView) {
TextView textView = (TextView) view;
centerX = position[0] + textView.getMeasuredWidth() - AndroidUtilities.dp(16.5f);
} else {
centerX = position[0];
}
} else if (currentType == TYPE_SEARCH_AS_LIST) {
2021-02-23 12:53:38 +01:00
centerX = position[0];
2019-12-31 14:08:08 +01:00
} else {
centerX = position[0] + view.getMeasuredWidth() / 2;
}
2019-08-22 01:53:26 +02:00
View parentView = (View) getParent();
parentView.getLocationInWindow(position);
centerX -= position[0];
top -= position[1];
2020-08-14 18:58:22 +02:00
top -= bottomOffset;
2019-08-22 01:53:26 +02:00
int parentWidth = parentView.getMeasuredWidth();
2021-03-19 11:25:58 +01:00
if (isTopArrow && currentType != 6 && currentType != 7 && currentType != 8) {
2020-10-30 11:26:29 +01:00
setTranslationY(extraTranslationY + (translationY = AndroidUtilities.dp(44)));
2019-08-22 01:53:26 +02:00
} else {
2020-10-30 11:26:29 +01:00
setTranslationY(extraTranslationY + (translationY = top - getMeasuredHeight()));
2019-08-22 01:53:26 +02:00
}
2019-12-31 14:08:08 +01:00
final int offset;
2020-01-23 07:15:40 +01:00
int leftMargin = 0;
int rightMargin = 0;
if (getLayoutParams() instanceof MarginLayoutParams) {
leftMargin = ((MarginLayoutParams) getLayoutParams()).leftMargin;
rightMargin = ((MarginLayoutParams) getLayoutParams()).rightMargin;
}
2021-04-14 03:44:46 +02:00
if (currentType == 8 && !isTopArrow) {
offset = (parentWidth - leftMargin - rightMargin - getMeasuredWidth()) / 2;
} else if (centerX > parentView.getMeasuredWidth() / 2) {
2019-12-31 14:08:08 +01:00
if (currentType == TYPE_SEARCH_AS_LIST) {
offset = (int) (parentWidth - getMeasuredWidth() * 1.5f);
} else {
2020-01-23 07:15:40 +01:00
offset = parentWidth - getMeasuredWidth() - (leftMargin + rightMargin);
2019-12-31 14:08:08 +01:00
}
2019-08-22 01:53:26 +02:00
} else {
2019-12-31 14:08:08 +01:00
if (currentType == TYPE_SEARCH_AS_LIST) {
offset = centerX - getMeasuredWidth() / 2 - arrowImageView.getMeasuredWidth();
} else {
offset = 0;
}
2019-08-22 01:53:26 +02:00
}
2019-12-31 14:08:08 +01:00
setTranslationX(offset);
2020-01-23 07:15:40 +01:00
float arrowX = centerX - (leftMargin + offset) - arrowImageView.getMeasuredWidth() / 2;
2021-02-23 12:53:38 +01:00
if (currentType == 7) {
arrowX += AndroidUtilities.dp(2);
}
2019-08-22 01:53:26 +02:00
arrowImageView.setTranslationX(arrowX);
2019-12-31 14:08:08 +01:00
if (centerX > parentView.getMeasuredWidth() / 2) {
2019-08-22 01:53:26 +02:00
if (arrowX < AndroidUtilities.dp(10)) {
float diff = arrowX - AndroidUtilities.dp(10);
setTranslationX(getTranslationX() + diff);
arrowImageView.setTranslationX(arrowX - diff);
}
} else {
if (arrowX > getMeasuredWidth() - AndroidUtilities.dp(14 + 10)) {
float diff = arrowX - getMeasuredWidth() + AndroidUtilities.dp(14 + 10);
setTranslationX(diff);
arrowImageView.setTranslationX(arrowX - diff);
} else if (arrowX < AndroidUtilities.dp(10)) {
float diff = arrowX - AndroidUtilities.dp(10);
setTranslationX(getTranslationX() + diff);
arrowImageView.setTranslationX(arrowX - diff);
}
}
}
2019-05-14 14:08:05 +02:00
public void hide() {
if (getTag() == null) {
return;
}
setTag(null);
if (hideRunnable != null) {
AndroidUtilities.cancelRunOnUIThread(hideRunnable);
hideRunnable = null;
}
if (animatorSet != null) {
animatorSet.cancel();
animatorSet = null;
}
animatorSet = new AnimatorSet();
animatorSet.playTogether(
2020-01-23 07:15:40 +01:00
ObjectAnimator.ofFloat(this, View.ALPHA, 0.0f)
2019-05-14 14:08:05 +02:00
);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
setVisibility(View.INVISIBLE);
2019-08-22 01:53:26 +02:00
currentView = null;
2019-05-14 14:08:05 +02:00
messageCell = null;
animatorSet = null;
}
});
animatorSet.setDuration(300);
animatorSet.start();
}
2019-08-22 01:53:26 +02:00
public void setText(CharSequence text) {
textView.setText(text);
}
2019-05-14 14:08:05 +02:00
public ChatMessageCell getMessageCell() {
return messageCell;
}
2020-01-23 07:15:40 +01:00
public void setShowingDuration(long showingDuration) {
this.showingDuration = showingDuration;
}
2020-08-14 18:58:22 +02:00
public void setBottomOffset(int offset) {
this.bottomOffset = offset;
}
2019-05-14 14:08:05 +02:00
}