NekoX/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/ActionBarMenuItem.java

1024 lines
40 KiB
Java
Raw Normal View History

/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
* 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.
*/
2014-11-13 21:10:14 +01:00
package org.telegram.ui.ActionBar;
2019-12-31 14:08:08 +01:00
import android.animation.Animator;
import android.content.Context;
2017-03-31 01:58:05 +02:00
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
2017-12-08 18:35:59 +01:00
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
2017-12-08 18:35:59 +01:00
import android.text.TextUtils;
import android.text.TextWatcher;
2015-01-02 23:15:07 +01:00
import android.util.TypedValue;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
2019-05-14 14:08:05 +02:00
import android.view.accessibility.AccessibilityNodeInfo;
2019-01-23 18:03:33 +01:00
import android.view.animation.DecelerateInterpolator;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.R;
2017-12-08 18:35:59 +01:00
import org.telegram.ui.Components.CloseProgressDrawable2;
import org.telegram.ui.Components.EditTextBoldCursor;
import org.telegram.ui.Components.LayoutHelper;
public class ActionBarMenuItem extends FrameLayout {
2015-01-02 23:15:07 +01:00
public static class ActionBarMenuItemSearchListener {
2015-05-21 23:27:27 +02:00
public void onSearchExpand() {
}
2015-06-29 19:12:11 +02:00
public boolean canCollapseSearch() {
2015-05-21 23:27:27 +02:00
return true;
}
2015-06-29 19:12:11 +02:00
public void onSearchCollapse() {
}
2015-05-21 23:27:27 +02:00
public void onTextChanged(EditText editText) {
}
public void onSearchPressed(EditText editText) {
}
2017-12-08 18:35:59 +01:00
public void onCaptionCleared() {
}
2019-01-23 18:03:33 +01:00
public boolean forceShowClear() {
return false;
}
2019-12-31 14:08:08 +01:00
public Animator getCustomToggleTransition() {
return null;
}
}
public interface ActionBarSumMenuItemDelegate {
void onShowSubMenu();
2015-05-21 23:27:27 +02:00
}
public interface ActionBarMenuItemDelegate {
void onItemClick(int id);
}
private ActionBarPopupWindow.ActionBarPopupWindowLayout popupLayout;
private ActionBarMenu parentMenu;
private ActionBarPopupWindow popupWindow;
2017-12-08 18:35:59 +01:00
private EditTextBoldCursor searchField;
private TextView searchFieldCaption;
2015-01-02 23:15:07 +01:00
private ImageView clearButton;
2015-02-01 19:51:02 +01:00
protected ImageView iconView;
2019-07-18 15:01:39 +02:00
protected TextView textView;
2015-01-02 23:15:07 +01:00
private FrameLayout searchContainer;
2017-07-08 18:32:04 +02:00
private boolean isSearchField;
2019-12-31 14:08:08 +01:00
protected ActionBarMenuItemSearchListener listener;
private Rect rect;
private int[] location;
private View selectedMenuView;
private Runnable showMenuRunnable;
2017-07-08 18:32:04 +02:00
private int subMenuOpenSide;
2019-01-23 18:03:33 +01:00
private int yOffset;
2015-05-21 23:27:27 +02:00
private ActionBarMenuItemDelegate delegate;
2019-12-31 14:08:08 +01:00
private ActionBarSumMenuItemDelegate subMenuDelegate;
2015-06-29 19:12:11 +02:00
private boolean allowCloseAnimation = true;
2015-07-22 20:56:37 +02:00
protected boolean overrideMenuClick;
private boolean processedPopupClick;
2017-03-31 01:58:05 +02:00
private boolean layoutInScreen;
2017-07-08 18:32:04 +02:00
private boolean animationEnabled = true;
2017-12-08 18:35:59 +01:00
private boolean ignoreOnTextChange;
private CloseProgressDrawable2 progressDrawable;
2019-08-22 01:53:26 +02:00
private int additionalYOffset;
private int additionalXOffset;
2018-07-30 04:07:02 +02:00
private boolean longClickEnabled = true;
2019-01-23 18:03:33 +01:00
private boolean animateClear = true;
2019-12-31 14:08:08 +01:00
private boolean clearsTextOnSearchCollapse = true;
private boolean measurePopup = true;
2017-03-31 01:58:05 +02:00
public ActionBarMenuItem(Context context, ActionBarMenu menu, int backgroundColor, int iconColor) {
2019-07-18 15:01:39 +02:00
this(context, menu, backgroundColor, iconColor, false);
}
public ActionBarMenuItem(Context context, ActionBarMenu menu, int backgroundColor, int iconColor, boolean text) {
super(context);
2016-04-22 15:49:00 +02:00
if (backgroundColor != 0) {
2019-07-18 15:01:39 +02:00
setBackgroundDrawable(Theme.createSelectorDrawable(backgroundColor, text ? 5 : 1));
2016-04-22 15:49:00 +02:00
}
parentMenu = menu;
2015-02-01 19:51:02 +01:00
2019-07-18 15:01:39 +02:00
if (text) {
textView = new TextView(context);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
textView.setGravity(Gravity.CENTER);
textView.setPadding(AndroidUtilities.dp(4), 0, AndroidUtilities.dp(4), 0);
if (iconColor != 0) {
textView.setTextColor(iconColor);
}
addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT));
} else {
iconView = new ImageView(context);
iconView.setScaleType(ImageView.ScaleType.CENTER);
addView(iconView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
if (iconColor != 0) {
iconView.setColorFilter(new PorterDuffColorFilter(iconColor, PorterDuff.Mode.MULTIPLY));
}
2017-03-31 01:58:05 +02:00
}
}
2018-07-30 04:07:02 +02:00
public void setLongClickEnabled(boolean value) {
longClickEnabled = value;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
2018-07-30 04:07:02 +02:00
if (longClickEnabled && hasSubMenu() && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
2018-08-27 10:33:11 +02:00
showMenuRunnable = () -> {
if (getParent() != null) {
getParent().requestDisallowInterceptTouchEvent(true);
}
2018-08-27 10:33:11 +02:00
toggleSubMenu();
};
AndroidUtilities.runOnUIThread(showMenuRunnable, 200);
}
} else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
if (hasSubMenu() && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
if (event.getY() > getHeight()) {
if (getParent() != null) {
getParent().requestDisallowInterceptTouchEvent(true);
}
toggleSubMenu();
return true;
}
} else if (popupWindow != null && popupWindow.isShowing()) {
getLocationOnScreen(location);
float x = event.getX() + location[0];
float y = event.getY() + location[1];
popupLayout.getLocationOnScreen(location);
x -= location[0];
y -= location[1];
selectedMenuView = null;
2015-07-22 20:56:37 +02:00
for (int a = 0; a < popupLayout.getItemsCount(); a++) {
View child = popupLayout.getItemAt(a);
child.getHitRect(rect);
2015-05-21 23:27:27 +02:00
if ((Integer) child.getTag() < 100) {
if (!rect.contains((int) x, (int) y)) {
child.setPressed(false);
child.setSelected(false);
2015-07-22 20:56:37 +02:00
if (Build.VERSION.SDK_INT == 21) {
child.getBackground().setVisible(false, false);
}
} else {
child.setPressed(true);
child.setSelected(true);
if (Build.VERSION.SDK_INT >= 21) {
2015-07-22 20:56:37 +02:00
if (Build.VERSION.SDK_INT == 21) {
child.getBackground().setVisible(true, false);
}
child.drawableHotspotChanged(x, y - child.getTop());
}
selectedMenuView = child;
}
}
}
}
} else if (popupWindow != null && popupWindow.isShowing() && event.getActionMasked() == MotionEvent.ACTION_UP) {
if (selectedMenuView != null) {
selectedMenuView.setSelected(false);
2015-05-21 23:27:27 +02:00
if (parentMenu != null) {
parentMenu.onItemClick((Integer) selectedMenuView.getTag());
} else if (delegate != null) {
delegate.onItemClick((Integer) selectedMenuView.getTag());
}
2015-06-29 19:12:11 +02:00
popupWindow.dismiss(allowCloseAnimation);
} else {
popupWindow.dismiss();
}
} else {
if (selectedMenuView != null) {
selectedMenuView.setSelected(false);
selectedMenuView = null;
}
}
return super.onTouchEvent(event);
}
2019-12-31 14:08:08 +01:00
public void setDelegate(ActionBarMenuItemDelegate actionBarMenuItemDelegate) {
delegate = actionBarMenuItemDelegate;
}
public void setSubMenuDelegate(ActionBarSumMenuItemDelegate actionBarSumMenuItemDelegate) {
subMenuDelegate = actionBarSumMenuItemDelegate;
2014-11-14 16:40:15 +01:00
}
2017-03-31 01:58:05 +02:00
public void setIconColor(int color) {
2019-07-18 15:01:39 +02:00
if (iconView != null) {
iconView.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
}
if (textView != null) {
textView.setTextColor(color);
}
2017-03-31 01:58:05 +02:00
if (clearButton != null) {
clearButton.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
}
}
2015-02-01 19:51:02 +01:00
public void setSubMenuOpenSide(int side) {
subMenuOpenSide = side;
}
2017-03-31 01:58:05 +02:00
public void setLayoutInScreen(boolean value) {
layoutInScreen = value;
}
2017-07-08 18:32:04 +02:00
private void createPopupLayout() {
if (popupLayout != null) {
return;
}
rect = new Rect();
location = new int[2];
popupLayout = new ActionBarPopupWindow.ActionBarPopupWindowLayout(getContext());
2018-08-27 10:33:11 +02:00
popupLayout.setOnTouchListener((v, event) -> {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
if (popupWindow != null && popupWindow.isShowing()) {
v.getHitRect(rect);
if (!rect.contains((int) event.getX(), (int) event.getY())) {
popupWindow.dismiss();
}
}
2017-07-08 18:32:04 +02:00
}
2018-08-27 10:33:11 +02:00
return false;
2017-07-08 18:32:04 +02:00
});
2018-08-27 10:33:11 +02:00
popupLayout.setDispatchKeyEventListener(keyEvent -> {
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0 && popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
2017-07-08 18:32:04 +02:00
}
});
}
2019-08-22 01:53:26 +02:00
public void removeAllSubItems() {
if (popupLayout == null) {
return;
}
popupLayout.removeInnerViews();
}
2017-07-08 18:32:04 +02:00
public void addSubItem(View view, int width, int height) {
createPopupLayout();
popupLayout.addView(view, new LinearLayout.LayoutParams(width, height));
}
2018-07-30 04:07:02 +02:00
public void addSubItem(int id, View view, int width, int height) {
createPopupLayout();
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));
popupLayout.addView(view);
view.setTag(id);
2018-08-27 10:33:11 +02:00
view.setOnClickListener(view1 -> {
if (popupWindow != null && popupWindow.isShowing()) {
if (processedPopupClick) {
return;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
processedPopupClick = true;
popupWindow.dismiss(allowCloseAnimation);
}
if (parentMenu != null) {
parentMenu.onItemClick((Integer) view1.getTag());
} else if (delegate != null) {
delegate.onItemClick((Integer) view1.getTag());
2018-07-30 04:07:02 +02:00
}
});
view.setBackgroundDrawable(Theme.getSelectorDrawable(false));
}
public TextView addSubItem(int id, CharSequence text) {
2017-07-08 18:32:04 +02:00
createPopupLayout();
TextView textView = new TextView(getContext());
2017-03-31 01:58:05 +02:00
textView.setTextColor(Theme.getColor(Theme.key_actionBarDefaultSubmenuItem));
textView.setBackgroundDrawable(Theme.getSelectorDrawable(false));
2014-10-01 00:36:18 +02:00
if (!LocaleController.isRTL) {
textView.setGravity(Gravity.CENTER_VERTICAL);
} else {
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
}
textView.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
2017-12-08 18:35:59 +01:00
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setMinWidth(AndroidUtilities.dp(196));
2019-08-22 01:53:26 +02:00
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setTag(id);
textView.setText(text);
popupLayout.addView(textView);
2015-05-21 23:27:27 +02:00
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
2014-10-01 00:36:18 +02:00
if (LocaleController.isRTL) {
layoutParams.gravity = Gravity.RIGHT;
}
layoutParams.width = LayoutHelper.MATCH_PARENT;
layoutParams.height = AndroidUtilities.dp(48);
textView.setLayoutParams(layoutParams);
2017-03-31 01:58:05 +02:00
2018-08-27 10:33:11 +02:00
textView.setOnClickListener(view -> {
if (popupWindow != null && popupWindow.isShowing()) {
if (processedPopupClick) {
return;
2015-05-21 23:27:27 +02:00
}
2018-08-27 10:33:11 +02:00
processedPopupClick = true;
2019-12-31 14:08:08 +01:00
if (!allowCloseAnimation) {
popupWindow.setAnimationStyle(R.style.PopupAnimation);
}
2018-08-27 10:33:11 +02:00
popupWindow.dismiss(allowCloseAnimation);
}
if (parentMenu != null) {
parentMenu.onItemClick((Integer) view.getTag());
} else if (delegate != null) {
delegate.onItemClick((Integer) view.getTag());
}
});
2014-11-17 03:44:57 +01:00
return textView;
}
2019-05-14 14:08:05 +02:00
public ActionBarMenuSubItem addSubItem(int id, int icon, CharSequence text) {
createPopupLayout();
ActionBarMenuSubItem cell = new ActionBarMenuSubItem(getContext());
cell.setTextAndIcon(text, icon);
cell.setMinimumWidth(AndroidUtilities.dp(196));
cell.setTag(id);
popupLayout.addView(cell);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) cell.getLayoutParams();
if (LocaleController.isRTL) {
layoutParams.gravity = Gravity.RIGHT;
}
layoutParams.width = LayoutHelper.MATCH_PARENT;
layoutParams.height = AndroidUtilities.dp(48);
cell.setLayoutParams(layoutParams);
cell.setOnClickListener(view -> {
if (popupWindow != null && popupWindow.isShowing()) {
if (processedPopupClick) {
return;
}
processedPopupClick = true;
popupWindow.dismiss(allowCloseAnimation);
}
if (parentMenu != null) {
parentMenu.onItemClick((Integer) view.getTag());
} else if (delegate != null) {
delegate.onItemClick((Integer) view.getTag());
}
});
return cell;
}
2017-07-08 18:32:04 +02:00
public void redrawPopup(int color) {
2019-12-31 14:08:08 +01:00
if (popupLayout != null && popupLayout.getBackgroundColor() != color) {
popupLayout.setBackgroundColor(color);
if (popupWindow != null && popupWindow.isShowing()) {
popupLayout.invalidate();
}
2017-03-31 01:58:05 +02:00
}
}
2019-05-14 14:08:05 +02:00
public void setPopupItemsColor(int color, boolean icon) {
2017-03-31 01:58:05 +02:00
if (popupLayout == null) {
return;
}
2019-12-31 14:08:08 +01:00
final LinearLayout layout = popupLayout.linearLayout;
for (int a = 0, count = layout.getChildCount(); a < count; a++) {
final View child = layout.getChildAt(a);
2017-03-31 01:58:05 +02:00
if (child instanceof TextView) {
((TextView) child).setTextColor(color);
2019-05-14 14:08:05 +02:00
} else if (child instanceof ActionBarMenuSubItem) {
if (icon) {
((ActionBarMenuSubItem) child).setIconColor(color);
} else {
((ActionBarMenuSubItem) child).setTextColor(color);
}
2017-03-31 01:58:05 +02:00
}
}
}
2019-12-31 14:08:08 +01:00
public void setPopupItemsSelectorColor(int color) {
if (popupLayout == null) {
return;
}
final LinearLayout layout = popupLayout.linearLayout;
for (int a = 0, count = layout.getChildCount(); a < count; a++) {
final View child = layout.getChildAt(a);
if (child instanceof ActionBarMenuSubItem) {
((ActionBarMenuSubItem) child).setSelectorColor(color);
}
}
}
public boolean hasSubMenu() {
return popupLayout != null;
}
2019-01-23 18:03:33 +01:00
public void setMenuYOffset(int offset) {
yOffset = offset;
}
public void toggleSubMenu() {
2019-05-14 14:08:05 +02:00
if (popupLayout == null || parentMenu != null && parentMenu.isActionMode && parentMenu.parentActionBar != null && !parentMenu.parentActionBar.isActionModeShowed()) {
return;
}
if (showMenuRunnable != null) {
AndroidUtilities.cancelRunOnUIThread(showMenuRunnable);
showMenuRunnable = null;
}
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
return;
}
2019-12-31 14:08:08 +01:00
if (subMenuDelegate != null) {
subMenuDelegate.onShowSubMenu();
}
if (popupWindow == null) {
popupWindow = new ActionBarPopupWindow(popupLayout, LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT);
2017-07-08 18:32:04 +02:00
if (animationEnabled && Build.VERSION.SDK_INT >= 19) {
2015-06-29 19:12:11 +02:00
popupWindow.setAnimationStyle(0);
} else {
popupWindow.setAnimationStyle(R.style.PopupAnimation);
}
2017-07-08 18:32:04 +02:00
if (!animationEnabled) {
popupWindow.setAnimationEnabled(animationEnabled);
}
popupWindow.setOutsideTouchable(true);
popupWindow.setClippingEnabled(true);
2017-03-31 01:58:05 +02:00
if (layoutInScreen) {
2019-05-14 14:08:05 +02:00
popupWindow.setLayoutInScreen(true);
2017-03-31 01:58:05 +02:00
}
popupWindow.setInputMethodMode(ActionBarPopupWindow.INPUT_METHOD_NOT_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
2014-10-01 21:55:24 +02:00
popupWindow.getContentView().setFocusableInTouchMode(true);
2018-08-27 10:33:11 +02:00
popupWindow.getContentView().setOnKeyListener((v, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0 && event.getAction() == KeyEvent.ACTION_UP && popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
return true;
2014-10-01 21:55:24 +02:00
}
2018-08-27 10:33:11 +02:00
return false;
2014-10-01 21:55:24 +02:00
});
2019-12-31 14:08:08 +01:00
popupWindow.setOnDismissListener(this::onDismiss);
}
if (measurePopup) {
popupLayout.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.displaySize.x - AndroidUtilities.dp(40), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.displaySize.y, MeasureSpec.AT_MOST));
measurePopup = false;
}
2015-07-22 20:56:37 +02:00
processedPopupClick = false;
popupWindow.setFocusable(true);
if (popupLayout.getMeasuredWidth() == 0) {
2015-06-29 19:12:11 +02:00
updateOrShowPopup(true, true);
} else {
2015-06-29 19:12:11 +02:00
updateOrShowPopup(true, false);
2014-11-17 03:44:57 +01:00
}
2015-06-29 19:12:11 +02:00
popupWindow.startAnimation();
2014-11-17 03:44:57 +01:00
}
2015-09-24 22:52:02 +02:00
public void openSearch(boolean openKeyboard) {
2015-05-21 23:27:27 +02:00
if (searchContainer == null || searchContainer.getVisibility() == VISIBLE || parentMenu == null) {
2015-01-02 23:15:07 +01:00
return;
}
2015-09-24 22:52:02 +02:00
parentMenu.parentActionBar.onSearchFieldVisibilityChanged(toggleSearch(openKeyboard));
2015-01-02 23:15:07 +01:00
}
2019-12-31 14:08:08 +01:00
protected void onDismiss() {
}
2015-09-24 22:52:02 +02:00
public boolean toggleSearch(boolean openKeyboard) {
2015-01-02 23:15:07 +01:00
if (searchContainer == null) {
return false;
}
2019-12-31 14:08:08 +01:00
if (listener != null) {
Animator animator = listener.getCustomToggleTransition();
if (animator != null) {
searchField.setText("");
animator.start();
return true;
}
}
2015-01-02 23:15:07 +01:00
if (searchContainer.getVisibility() == VISIBLE) {
2019-12-31 14:08:08 +01:00
if (openKeyboard) {
AndroidUtilities.hideKeyboard(searchField);
}
if (clearsTextOnSearchCollapse) {
2019-01-23 18:03:33 +01:00
searchField.setText("");
2019-12-31 14:08:08 +01:00
}
searchContainer.setVisibility(GONE);
searchField.clearFocus();
setVisibility(VISIBLE);
if (listener != null) {
listener.onSearchCollapse();
}
return false;
} else {
2015-01-02 23:15:07 +01:00
searchContainer.setVisibility(VISIBLE);
setVisibility(GONE);
searchField.setText("");
searchField.requestFocus();
2015-09-24 22:52:02 +02:00
if (openKeyboard) {
AndroidUtilities.showKeyboard(searchField);
}
if (listener != null) {
listener.onSearchExpand();
}
return true;
}
}
2019-12-31 14:08:08 +01:00
public void setClearsTextOnSearchCollapse(boolean value) {
clearsTextOnSearchCollapse = value;
}
public void closeSubMenu() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
}
2019-07-18 15:01:39 +02:00
public void setIcon(Drawable drawable) {
if (iconView == null) {
return;
}
iconView.setImageDrawable(drawable);
}
public void setIcon(int resId) {
2019-07-18 15:01:39 +02:00
if (iconView == null) {
return;
}
iconView.setImageResource(resId);
}
2019-07-18 15:01:39 +02:00
public void setText(CharSequence text) {
if (textView == null) {
return;
}
textView.setText(text);
2017-12-08 18:35:59 +01:00
}
2019-07-18 15:01:39 +02:00
public View getContentView() {
return iconView != null ? iconView : textView;
2016-05-25 23:49:47 +02:00
}
2019-01-23 18:03:33 +01:00
public void setSearchFieldHint(CharSequence hint) {
if (searchFieldCaption == null) {
return;
}
searchField.setHint(hint);
2019-05-14 14:08:05 +02:00
setContentDescription(hint);
2019-01-23 18:03:33 +01:00
}
public void setSearchFieldText(CharSequence text, boolean animated) {
if (searchFieldCaption == null) {
return;
}
animateClear = animated;
searchField.setText(text);
if (!TextUtils.isEmpty(text)) {
searchField.setSelection(text.length());
}
}
2019-08-22 01:53:26 +02:00
public void onSearchPressed() {
if (listener != null) {
listener.onSearchPressed(searchField);
}
}
2017-12-08 18:35:59 +01:00
public EditTextBoldCursor getSearchField() {
return searchField;
}
2015-07-22 20:56:37 +02:00
public ActionBarMenuItem setOverrideMenuClick(boolean value) {
overrideMenuClick = value;
return this;
}
public ActionBarMenuItem setIsSearchField(boolean value) {
2015-05-21 23:27:27 +02:00
if (parentMenu == null) {
return this;
}
2015-01-02 23:15:07 +01:00
if (value && searchContainer == null) {
2017-12-08 18:35:59 +01:00
searchContainer = new FrameLayout(getContext()) {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildWithMargins(clearButton, widthMeasureSpec, 0, heightMeasureSpec, 0);
int width;
if (searchFieldCaption.getVisibility() == VISIBLE) {
measureChildWithMargins(searchFieldCaption, widthMeasureSpec, MeasureSpec.getSize(widthMeasureSpec) / 2, heightMeasureSpec, 0);
width = searchFieldCaption.getMeasuredWidth() + AndroidUtilities.dp(4);
} else {
width = 0;
}
measureChildWithMargins(searchField, widthMeasureSpec, width, heightMeasureSpec, 0);
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int x;
if (LocaleController.isRTL) {
x = 0;
} else {
if (searchFieldCaption.getVisibility() == VISIBLE) {
x = searchFieldCaption.getMeasuredWidth() + AndroidUtilities.dp(4);
} else {
x = 0;
}
}
searchField.layout(x, searchField.getTop(), x + searchField.getMeasuredWidth(), searchField.getBottom());
}
};
2017-03-31 01:58:05 +02:00
parentMenu.addView(searchContainer, 0, LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT, 1.0f, 6, 0, 0, 0));
2015-01-02 23:15:07 +01:00
searchContainer.setVisibility(GONE);
2017-12-08 18:35:59 +01:00
searchFieldCaption = new TextView(getContext());
searchFieldCaption.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
searchFieldCaption.setTextColor(Theme.getColor(Theme.key_actionBarDefaultSearch));
searchFieldCaption.setSingleLine(true);
searchFieldCaption.setEllipsize(TextUtils.TruncateAt.END);
searchFieldCaption.setVisibility(GONE);
searchFieldCaption.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
searchField = new EditTextBoldCursor(getContext()) {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL && searchField.length() == 0 && searchFieldCaption.getVisibility() == VISIBLE && searchFieldCaption.length() > 0) {
clearButton.callOnClick();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
2019-01-23 18:03:33 +01:00
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!AndroidUtilities.showKeyboard(this)) {
clearFocus();
requestFocus();
}
}
return super.onTouchEvent(event);
2017-12-08 18:35:59 +01:00
}
};
searchField.setCursorWidth(1.5f);
2019-06-04 12:14:50 +02:00
searchField.setCursorColor(Theme.getColor(Theme.key_actionBarDefaultSearch));
2015-01-02 23:15:07 +01:00
searchField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
2017-03-31 01:58:05 +02:00
searchField.setHintTextColor(Theme.getColor(Theme.key_actionBarDefaultSearchPlaceholder));
searchField.setTextColor(Theme.getColor(Theme.key_actionBarDefaultSearch));
searchField.setSingleLine(true);
2015-01-02 23:15:07 +01:00
searchField.setBackgroundResource(0);
2014-11-14 16:40:15 +01:00
searchField.setPadding(0, 0, 0, 0);
2015-07-22 20:56:37 +02:00
int inputType = searchField.getInputType() | EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
searchField.setInputType(inputType);
2017-12-08 18:35:59 +01:00
if (Build.VERSION.SDK_INT < 23) {
searchField.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
2017-12-08 18:35:59 +01:00
public void onDestroyActionMode(ActionMode mode) {
2015-06-29 19:12:11 +02:00
2017-12-08 18:35:59 +01:00
}
2017-12-08 18:35:59 +01:00
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
2017-12-08 18:35:59 +01:00
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
}
2018-08-27 10:33:11 +02:00
searchField.setOnEditorActionListener((v, actionId, event) -> {
if (event != null && (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_SEARCH || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
AndroidUtilities.hideKeyboard(searchField);
if (listener != null) {
listener.onSearchPressed(searchField);
}
}
2018-08-27 10:33:11 +02:00
return false;
});
searchField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
2017-12-08 18:35:59 +01:00
if (ignoreOnTextChange) {
ignoreOnTextChange = false;
return;
}
if (listener != null) {
listener.onTextChanged(searchField);
}
2015-06-29 19:12:11 +02:00
if (clearButton != null) {
2019-01-23 18:03:33 +01:00
if (TextUtils.isEmpty(s) &&
(listener == null || !listener.forceShowClear()) &&
(searchFieldCaption == null || searchFieldCaption.getVisibility() != VISIBLE)) {
if (clearButton.getTag() != null) {
clearButton.setTag(null);
clearButton.clearAnimation();
if (animateClear) {
clearButton.animate().setInterpolator(new DecelerateInterpolator()).alpha(0.0f).setDuration(180).scaleY(0.0f).scaleX(0.0f).rotation(45).withEndAction(() -> clearButton.setVisibility(INVISIBLE)).start();
} else {
clearButton.setAlpha(0.0f);
clearButton.setRotation(45);
clearButton.setScaleX(0.0f);
clearButton.setScaleY(0.0f);
clearButton.setVisibility(INVISIBLE);
animateClear = true;
}
}
} else {
if (clearButton.getTag() == null) {
clearButton.setTag(1);
clearButton.clearAnimation();
clearButton.setVisibility(VISIBLE);
if (animateClear) {
clearButton.animate().setInterpolator(new DecelerateInterpolator()).alpha(1.0f).setDuration(180).scaleY(1.0f).scaleX(1.0f).rotation(0).start();
} else {
clearButton.setAlpha(1.0f);
clearButton.setRotation(0);
clearButton.setScaleX(1.0f);
clearButton.setScaleY(1.0f);
animateClear = true;
}
}
}
2015-06-29 19:12:11 +02:00
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
searchField.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME_ACTION_SEARCH);
searchField.setTextIsSelectable(false);
2017-12-08 18:35:59 +01:00
if (!LocaleController.isRTL) {
searchContainer.addView(searchFieldCaption, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, 36, Gravity.CENTER_VERTICAL | Gravity.LEFT, 0, 5.5f, 0, 0));
searchContainer.addView(searchField, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 36, Gravity.CENTER_VERTICAL, 0, 0, 48, 0));
} else {
searchContainer.addView(searchField, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 36, Gravity.CENTER_VERTICAL, 0, 0, 48, 0));
searchContainer.addView(searchFieldCaption, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, 36, Gravity.CENTER_VERTICAL | Gravity.RIGHT, 0, 5.5f, 48, 0));
}
2015-01-02 23:15:07 +01:00
2019-01-23 18:03:33 +01:00
clearButton = new ImageView(getContext()) {
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
clearAnimation();
if (getTag() == null) {
clearButton.setVisibility(INVISIBLE);
clearButton.setAlpha(0.0f);
clearButton.setRotation(45);
clearButton.setScaleX(0.0f);
clearButton.setScaleY(0.0f);
} else {
clearButton.setAlpha(1.0f);
clearButton.setRotation(0);
clearButton.setScaleX(1.0f);
clearButton.setScaleY(1.0f);
}
}
};
2017-12-08 18:35:59 +01:00
clearButton.setImageDrawable(progressDrawable = new CloseProgressDrawable2());
2017-03-31 01:58:05 +02:00
clearButton.setColorFilter(new PorterDuffColorFilter(parentMenu.parentActionBar.itemsColor, PorterDuff.Mode.MULTIPLY));
clearButton.setScaleType(ImageView.ScaleType.CENTER);
2019-01-23 18:03:33 +01:00
clearButton.setAlpha(0.0f);
clearButton.setRotation(45);
clearButton.setScaleX(0.0f);
clearButton.setScaleY(0.0f);
2018-08-27 10:33:11 +02:00
clearButton.setOnClickListener(v -> {
if (searchField.length() != 0) {
searchField.setText("");
} else if (searchFieldCaption != null && searchFieldCaption.getVisibility() == VISIBLE) {
searchFieldCaption.setVisibility(GONE);
if (listener != null) {
listener.onCaptionCleared();
2017-12-08 18:35:59 +01:00
}
}
2018-08-27 10:33:11 +02:00
searchField.requestFocus();
AndroidUtilities.showKeyboard(searchField);
});
2019-05-14 14:08:05 +02:00
clearButton.setContentDescription(LocaleController.getString("ClearButton", R.string.ClearButton));
2017-03-31 01:58:05 +02:00
searchContainer.addView(clearButton, LayoutHelper.createFrame(48, LayoutHelper.MATCH_PARENT, Gravity.CENTER_VERTICAL | Gravity.RIGHT));
}
isSearchField = value;
return this;
}
2017-12-08 18:35:59 +01:00
public void setShowSearchProgress(boolean show) {
if (progressDrawable == null) {
return;
}
if (show) {
progressDrawable.startAnimation();
} else {
progressDrawable.stopAnimation();
}
}
public void setSearchFieldCaption(CharSequence caption) {
2019-01-23 18:03:33 +01:00
if (searchFieldCaption == null) {
return;
}
2017-12-08 18:35:59 +01:00
if (TextUtils.isEmpty(caption)) {
searchFieldCaption.setVisibility(GONE);
} else {
searchFieldCaption.setVisibility(VISIBLE);
searchFieldCaption.setText(caption);
}
}
public void setIgnoreOnTextChange() {
ignoreOnTextChange = true;
}
public boolean isSearchField() {
return isSearchField;
}
2017-12-08 18:35:59 +01:00
public void clearSearchText() {
if (searchField == null) {
return;
}
searchField.setText("");
}
2019-12-31 14:08:08 +01:00
public ActionBarMenuItem setActionBarMenuItemSearchListener(ActionBarMenuItemSearchListener actionBarMenuItemSearchListener) {
listener = actionBarMenuItemSearchListener;
2015-01-02 23:15:07 +01:00
return this;
}
2015-06-29 19:12:11 +02:00
public ActionBarMenuItem setAllowCloseAnimation(boolean value) {
allowCloseAnimation = value;
return this;
}
2017-07-08 18:32:04 +02:00
public void setPopupAnimationEnabled(boolean value) {
if (popupWindow != null) {
popupWindow.setAnimationEnabled(value);
}
animationEnabled = value;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (popupWindow != null && popupWindow.isShowing()) {
2015-06-29 19:12:11 +02:00
updateOrShowPopup(false, true);
}
}
2019-08-22 01:53:26 +02:00
public void setAdditionalYOffset(int value) {
additionalYOffset = value;
}
public void setAdditionalXOffset(int value) {
additionalXOffset = value;
2017-12-08 18:35:59 +01:00
}
2015-06-29 19:12:11 +02:00
private void updateOrShowPopup(boolean show, boolean update) {
int offsetY;
2017-07-08 18:32:04 +02:00
if (parentMenu != null) {
2019-05-14 14:08:05 +02:00
offsetY = -parentMenu.parentActionBar.getMeasuredHeight() + parentMenu.getTop() + parentMenu.getPaddingTop();
2015-06-29 19:12:11 +02:00
} else {
2017-07-08 18:32:04 +02:00
float scaleY = getScaleY();
2019-08-22 01:53:26 +02:00
offsetY = -(int) (getMeasuredHeight() * scaleY - (subMenuOpenSide != 2 ? getTranslationY() : 0) / scaleY) + additionalYOffset;
2015-06-29 19:12:11 +02:00
}
2019-01-23 18:03:33 +01:00
offsetY += yOffset;
2015-06-29 19:12:11 +02:00
2015-07-22 20:56:37 +02:00
if (show) {
popupLayout.scrollToTop();
}
2017-07-08 18:32:04 +02:00
if (parentMenu != null) {
View parent = parentMenu.parentActionBar;
if (subMenuOpenSide == 0) {
2015-06-29 19:12:11 +02:00
if (show) {
2017-12-08 18:35:59 +01:00
popupWindow.showAsDropDown(parent, getLeft() + parentMenu.getLeft() + getMeasuredWidth() - popupLayout.getMeasuredWidth() + (int) getTranslationX(), offsetY);
2015-06-29 19:12:11 +02:00
}
if (update) {
2017-12-08 18:35:59 +01:00
popupWindow.update(parent, getLeft() + parentMenu.getLeft() + getMeasuredWidth() - popupLayout.getMeasuredWidth() + (int) getTranslationX(), offsetY, -1, -1);
2015-02-01 19:51:02 +01:00
}
2014-11-14 16:40:15 +01:00
} else {
2017-07-08 18:32:04 +02:00
if (show) {
2017-12-08 18:35:59 +01:00
popupWindow.showAsDropDown(parent, getLeft() - AndroidUtilities.dp(8) + (int) getTranslationX(), offsetY);
2017-07-08 18:32:04 +02:00
}
if (update) {
2017-12-08 18:35:59 +01:00
popupWindow.update(parent, getLeft() - AndroidUtilities.dp(8) + (int) getTranslationX(), offsetY, -1, -1);
2017-07-08 18:32:04 +02:00
}
}
} else {
if (subMenuOpenSide == 0) {
if (getParent() != null) {
2015-06-29 19:12:11 +02:00
View parent = (View) getParent();
if (show) {
2019-08-22 01:53:26 +02:00
popupWindow.showAsDropDown(parent, getLeft() + getMeasuredWidth() - popupLayout.getMeasuredWidth() + additionalXOffset, offsetY);
2015-06-29 19:12:11 +02:00
}
if (update) {
2019-08-22 01:53:26 +02:00
popupWindow.update(parent, getLeft() + getMeasuredWidth() - popupLayout.getMeasuredWidth() + additionalXOffset, offsetY, -1, -1);
2015-06-29 19:12:11 +02:00
}
}
2019-05-14 14:08:05 +02:00
} else if (subMenuOpenSide == 1) {
2017-07-08 18:32:04 +02:00
if (show) {
2019-08-22 01:53:26 +02:00
popupWindow.showAsDropDown(this, -AndroidUtilities.dp(8) + additionalXOffset, offsetY);
2017-07-08 18:32:04 +02:00
}
if (update) {
2019-08-22 01:53:26 +02:00
popupWindow.update(this, -AndroidUtilities.dp(8) + additionalXOffset, offsetY, -1, -1);
2017-07-08 18:32:04 +02:00
}
2019-05-14 14:08:05 +02:00
} else {
if (show) {
2019-08-22 01:53:26 +02:00
popupWindow.showAsDropDown(this, getMeasuredWidth() - popupLayout.getMeasuredWidth() + additionalXOffset, offsetY);
2019-05-14 14:08:05 +02:00
}
if (update) {
2019-08-22 01:53:26 +02:00
popupWindow.update(this, getMeasuredWidth() - popupLayout.getMeasuredWidth() + additionalXOffset, offsetY, -1, -1);
2019-05-14 14:08:05 +02:00
}
2014-11-14 16:40:15 +01:00
}
}
}
public void hideSubItem(int id) {
2019-01-23 18:03:33 +01:00
if (popupLayout == null) {
return;
}
View view = popupLayout.findViewWithTag(id);
2018-07-30 04:07:02 +02:00
if (view != null && view.getVisibility() != GONE) {
view.setVisibility(GONE);
2019-12-31 14:08:08 +01:00
measurePopup = true;
}
}
2018-07-30 04:07:02 +02:00
public boolean isSubItemVisible(int id) {
2019-01-23 18:03:33 +01:00
if (popupLayout == null) {
return false;
}
2018-07-30 04:07:02 +02:00
View view = popupLayout.findViewWithTag(id);
return view != null && view.getVisibility() == VISIBLE;
}
public void showSubItem(int id) {
2019-01-23 18:03:33 +01:00
if (popupLayout == null) {
return;
}
View view = popupLayout.findViewWithTag(id);
2018-07-30 04:07:02 +02:00
if (view != null && view.getVisibility() != VISIBLE) {
view.setVisibility(VISIBLE);
2019-12-31 14:08:08 +01:00
measurePopup = true;
}
}
2019-05-14 14:08:05 +02:00
2019-12-31 14:08:08 +01:00
public void requestFocusOnSearchView() {
if (searchContainer.getWidth() != 0 && !searchField.isFocused()) {
searchField.requestFocus();
AndroidUtilities.showKeyboard(searchField);
}
}
public void clearFocusOnSearchView() {
searchField.clearFocus();
AndroidUtilities.hideKeyboard(searchField);
}
public FrameLayout getSearchContainer() {
return searchContainer;
}
2019-05-14 14:08:05 +02:00
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName("android.widget.ImageButton");
}
}