2014-06-04 01:31:48 +02:00
|
|
|
/*
|
|
|
|
* This is the source code of Telegram for Android v. 1.4.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).
|
|
|
|
*
|
|
|
|
* Copyright Nikolai Kudashov, 2013-2014.
|
|
|
|
*/
|
|
|
|
|
2014-11-13 21:10:14 +01:00
|
|
|
package org.telegram.ui.ActionBar;
|
2014-06-04 01:31:48 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
2014-06-17 16:45:21 +02:00
|
|
|
import android.graphics.Rect;
|
2014-06-04 01:31:48 +02:00
|
|
|
import android.os.Build;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
2015-01-02 23:15:07 +01:00
|
|
|
import android.util.TypedValue;
|
2014-09-24 04:17:27 +02:00
|
|
|
import android.view.ActionMode;
|
|
|
|
import android.view.ContextMenu;
|
2014-06-04 01:31:48 +02:00
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.KeyEvent;
|
2014-09-24 04:17:27 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2014-06-17 16:45:21 +02:00
|
|
|
import android.view.MotionEvent;
|
2014-06-04 01:31:48 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.WindowManager;
|
|
|
|
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;
|
|
|
|
|
2014-07-03 00:39:05 +02:00
|
|
|
import org.telegram.android.AndroidUtilities;
|
2014-10-01 00:36:18 +02:00
|
|
|
import org.telegram.android.LocaleController;
|
2014-06-04 01:31:48 +02:00
|
|
|
import org.telegram.messenger.R;
|
2015-01-02 23:15:07 +01:00
|
|
|
import org.telegram.ui.AnimationCompat.ViewProxy;
|
2015-02-01 19:51:02 +01:00
|
|
|
import org.telegram.ui.Components.FrameLayoutFixed;
|
2014-06-04 01:31:48 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
public class ActionBarMenuItem extends FrameLayoutFixed {
|
2014-06-04 01:31:48 +02:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
public static class ActionBarMenuItemSearchListener {
|
|
|
|
public void onSearchExpand() { }
|
2015-03-19 00:09:45 +01:00
|
|
|
public boolean onSearchCollapse() { return true; }
|
2015-01-02 23:15:07 +01:00
|
|
|
public void onTextChanged(EditText editText) { }
|
|
|
|
public void onSearchPressed(EditText editText) { }
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 16:45:21 +02:00
|
|
|
private ActionBarPopupWindow.ActionBarPopupWindowLayout popupLayout;
|
2014-06-04 01:31:48 +02:00
|
|
|
private ActionBarMenu parentMenu;
|
2014-06-04 18:00:42 +02:00
|
|
|
private ActionBarPopupWindow popupWindow;
|
2014-06-04 01:31:48 +02:00
|
|
|
private EditText searchField;
|
2015-01-02 23:15:07 +01:00
|
|
|
private ImageView clearButton;
|
2015-02-01 19:51:02 +01:00
|
|
|
protected ImageView iconView;
|
2015-01-02 23:15:07 +01:00
|
|
|
private FrameLayout searchContainer;
|
2014-06-04 01:31:48 +02:00
|
|
|
private boolean isSearchField = false;
|
|
|
|
private ActionBarMenuItemSearchListener listener;
|
2014-11-06 22:34:47 +01:00
|
|
|
private Rect rect;
|
|
|
|
private int[] location;
|
|
|
|
private View selectedMenuView;
|
|
|
|
private Runnable showMenuRunnable;
|
2014-11-14 16:40:15 +01:00
|
|
|
private boolean showFromBottom;
|
2014-11-17 03:44:57 +01:00
|
|
|
private int menuHeight = AndroidUtilities.dp(16);
|
2014-12-01 18:56:31 +01:00
|
|
|
private boolean needOffset = Build.VERSION.SDK_INT >= 21;
|
2015-02-01 19:51:02 +01:00
|
|
|
private int subMenuOpenSide = 0;
|
2014-06-04 01:31:48 +02:00
|
|
|
|
2014-11-11 23:16:17 +01:00
|
|
|
public ActionBarMenuItem(Context context, ActionBarMenu menu, int background) {
|
2014-06-04 01:31:48 +02:00
|
|
|
super(context);
|
2014-06-12 03:13:15 +02:00
|
|
|
setBackgroundResource(background);
|
2014-06-04 01:31:48 +02:00
|
|
|
parentMenu = menu;
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
iconView = new ImageView(context);
|
|
|
|
iconView.setScaleType(ImageView.ScaleType.CENTER);
|
|
|
|
addView(iconView);
|
|
|
|
LayoutParams layoutParams = (LayoutParams) iconView.getLayoutParams();
|
|
|
|
layoutParams.width = LayoutParams.MATCH_PARENT;
|
|
|
|
layoutParams.height = LayoutParams.MATCH_PARENT;
|
|
|
|
iconView.setLayoutParams(layoutParams);
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 16:45:21 +02:00
|
|
|
@Override
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
2014-11-06 22:34:47 +01:00
|
|
|
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
|
|
|
if (hasSubMenu() && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
|
|
|
|
showMenuRunnable = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (getParent() != null) {
|
|
|
|
getParent().requestDisallowInterceptTouchEvent(true);
|
|
|
|
}
|
|
|
|
toggleSubMenu();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
AndroidUtilities.runOnUIThread(showMenuRunnable, 200);
|
|
|
|
}
|
|
|
|
} else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
|
2014-06-17 16:45:21 +02:00
|
|
|
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;
|
|
|
|
for (int a = 0; a < popupLayout.getChildCount(); a++) {
|
|
|
|
View child = popupLayout.getChildAt(a);
|
|
|
|
child.getHitRect(rect);
|
|
|
|
if ((Integer)child.getTag() < 100) {
|
|
|
|
if (!rect.contains((int)x, (int)y)) {
|
2014-11-06 22:34:47 +01:00
|
|
|
child.setPressed(false);
|
2014-06-17 16:45:21 +02:00
|
|
|
child.setSelected(false);
|
2014-11-06 22:34:47 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
|
|
child.getBackground().setVisible(false, false);
|
|
|
|
}
|
2014-06-17 16:45:21 +02:00
|
|
|
} else {
|
2014-11-06 22:34:47 +01:00
|
|
|
child.setPressed(true);
|
2014-06-17 16:45:21 +02:00
|
|
|
child.setSelected(true);
|
2014-11-06 22:34:47 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
|
|
child.getBackground().setVisible(true, false);
|
|
|
|
child.drawableHotspotChanged(x, y - child.getTop());
|
|
|
|
}
|
2014-06-17 16:45:21 +02:00
|
|
|
selectedMenuView = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (popupWindow != null && popupWindow.isShowing() && event.getActionMasked() == MotionEvent.ACTION_UP) {
|
|
|
|
if (selectedMenuView != null) {
|
|
|
|
selectedMenuView.setSelected(false);
|
|
|
|
parentMenu.onItemClick((Integer) selectedMenuView.getTag());
|
|
|
|
}
|
|
|
|
popupWindow.dismiss();
|
|
|
|
} else {
|
|
|
|
if (selectedMenuView != null) {
|
|
|
|
selectedMenuView.setSelected(false);
|
|
|
|
selectedMenuView = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
}
|
|
|
|
|
2014-11-14 16:40:15 +01:00
|
|
|
public void setShowFromBottom(boolean value) {
|
|
|
|
showFromBottom = value;
|
|
|
|
}
|
|
|
|
|
2014-12-01 18:56:31 +01:00
|
|
|
public void setNeedOffset(boolean value) {
|
|
|
|
needOffset = Build.VERSION.SDK_INT >= 21 && value;
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
public void setSubMenuOpenSide(int side) {
|
|
|
|
subMenuOpenSide = side;
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:44:57 +01:00
|
|
|
public TextView addSubItem(int id, String text, int icon) {
|
2014-06-04 01:31:48 +02:00
|
|
|
if (popupLayout == null) {
|
2014-06-17 16:45:21 +02:00
|
|
|
rect = new Rect();
|
|
|
|
location = new int[2];
|
|
|
|
popupLayout = new ActionBarPopupWindow.ActionBarPopupWindowLayout(getContext());
|
2014-06-04 01:31:48 +02:00
|
|
|
popupLayout.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
popupLayout.setBackgroundResource(R.drawable.popup_fixed);
|
2014-06-17 16:45:21 +02:00
|
|
|
popupLayout.setOnTouchListener(new OnTouchListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
popupLayout.setDispatchKeyEventListener(new ActionBarPopupWindow.OnDispatchKeyEventListener() {
|
|
|
|
@Override
|
|
|
|
public void onDispatchKeyEvent(KeyEvent keyEvent) {
|
|
|
|
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0 && popupWindow != null && popupWindow.isShowing()) {
|
|
|
|
popupWindow.dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
TextView textView = new TextView(getContext());
|
2014-11-17 03:44:57 +01:00
|
|
|
textView.setTextColor(0xff212121);
|
2014-06-04 01:31:48 +02:00
|
|
|
textView.setBackgroundResource(R.drawable.list_selector);
|
2014-10-01 00:36:18 +02:00
|
|
|
if (!LocaleController.isRTL) {
|
|
|
|
textView.setGravity(Gravity.CENTER_VERTICAL);
|
|
|
|
} else {
|
|
|
|
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
textView.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
|
2014-06-04 01:31:48 +02:00
|
|
|
textView.setTextSize(18);
|
2014-07-03 00:39:05 +02:00
|
|
|
textView.setMinWidth(AndroidUtilities.dp(196));
|
2014-06-04 01:31:48 +02:00
|
|
|
textView.setTag(id);
|
|
|
|
textView.setText(text);
|
|
|
|
if (icon != 0) {
|
2014-07-03 00:39:05 +02:00
|
|
|
textView.setCompoundDrawablePadding(AndroidUtilities.dp(12));
|
2014-10-01 00:36:18 +02:00
|
|
|
if (!LocaleController.isRTL) {
|
|
|
|
textView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(icon), null, null, null);
|
|
|
|
} else {
|
|
|
|
textView.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(icon), null);
|
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
popupLayout.addView(textView);
|
|
|
|
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)textView.getLayoutParams();
|
2014-10-01 00:36:18 +02:00
|
|
|
if (LocaleController.isRTL) {
|
|
|
|
layoutParams.gravity = Gravity.RIGHT;
|
|
|
|
}
|
2014-11-12 23:16:59 +01:00
|
|
|
layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
|
2014-07-03 00:39:05 +02:00
|
|
|
layoutParams.height = AndroidUtilities.dp(48);
|
2014-06-04 01:31:48 +02:00
|
|
|
textView.setLayoutParams(layoutParams);
|
|
|
|
textView.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
parentMenu.onItemClick((Integer) view.getTag());
|
|
|
|
if (popupWindow != null && popupWindow.isShowing()) {
|
|
|
|
popupWindow.dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-11-17 03:44:57 +01:00
|
|
|
menuHeight += layoutParams.height;
|
|
|
|
return textView;
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasSubMenu() {
|
|
|
|
return popupLayout != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void toggleSubMenu() {
|
2014-11-06 22:34:47 +01:00
|
|
|
if (popupLayout == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (showMenuRunnable != null) {
|
|
|
|
AndroidUtilities.cancelRunOnUIThread(showMenuRunnable);
|
|
|
|
showMenuRunnable = null;
|
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
if (popupWindow != null && popupWindow.isShowing()) {
|
|
|
|
popupWindow.dismiss();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (popupWindow == null) {
|
2014-06-04 18:00:42 +02:00
|
|
|
popupWindow = new ActionBarPopupWindow(popupLayout, FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
|
2014-06-17 16:45:21 +02:00
|
|
|
//popupWindow.setBackgroundDrawable(new BitmapDrawable());
|
2014-11-06 22:34:47 +01:00
|
|
|
popupWindow.setAnimationStyle(R.style.PopupAnimation);
|
2014-06-04 01:31:48 +02:00
|
|
|
popupWindow.setOutsideTouchable(true);
|
|
|
|
popupWindow.setClippingEnabled(true);
|
2014-06-04 18:00:42 +02:00
|
|
|
popupWindow.setInputMethodMode(ActionBarPopupWindow.INPUT_METHOD_NOT_NEEDED);
|
2014-06-04 01:31:48 +02:00
|
|
|
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
|
2014-07-03 00:39:05 +02:00
|
|
|
popupLayout.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), MeasureSpec.AT_MOST));
|
2014-10-01 21:55:24 +02:00
|
|
|
popupWindow.getContentView().setFocusableInTouchMode(true);
|
|
|
|
popupWindow.getContentView().setOnKeyListener(new OnKeyListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0 && event.getAction() == KeyEvent.ACTION_UP && popupWindow != null && popupWindow.isShowing()) {
|
|
|
|
popupWindow.dismiss();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
2014-06-13 20:26:05 +02:00
|
|
|
popupWindow.setFocusable(true);
|
2014-06-04 01:31:48 +02:00
|
|
|
if (popupLayout.getMeasuredWidth() == 0) {
|
2015-02-01 19:51:02 +01:00
|
|
|
if (subMenuOpenSide == 0) {
|
|
|
|
if (showFromBottom) {
|
|
|
|
popupWindow.showAsDropDown(this, -popupLayout.getMeasuredWidth() + getMeasuredWidth(), getOffsetY());
|
|
|
|
popupWindow.update(this, -popupLayout.getMeasuredWidth() + getMeasuredWidth(), getOffsetY(), -1, -1);
|
|
|
|
} else {
|
|
|
|
popupWindow.showAsDropDown(this, parentMenu.parentActionBar.getMeasuredWidth() - popupLayout.getMeasuredWidth() - getLeft() - parentMenu.getLeft(), getOffsetY());
|
|
|
|
popupWindow.update(this, parentMenu.parentActionBar.getMeasuredWidth() - popupLayout.getMeasuredWidth() - getLeft() - parentMenu.getLeft(), getOffsetY(), -1, -1);
|
|
|
|
}
|
2014-11-14 16:40:15 +01:00
|
|
|
} else {
|
2015-02-01 19:51:02 +01:00
|
|
|
popupWindow.showAsDropDown(this, -AndroidUtilities.dp(8), getOffsetY());
|
|
|
|
popupWindow.update(this, -AndroidUtilities.dp(8), getOffsetY(), -1, -1);
|
2014-11-14 16:40:15 +01:00
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
} else {
|
2015-02-01 19:51:02 +01:00
|
|
|
if (subMenuOpenSide == 0) {
|
|
|
|
if (showFromBottom) {
|
|
|
|
popupWindow.showAsDropDown(this, -popupLayout.getMeasuredWidth() + getMeasuredWidth(), getOffsetY());
|
|
|
|
} else {
|
|
|
|
popupWindow.showAsDropDown(this, parentMenu.parentActionBar.getMeasuredWidth() - popupLayout.getMeasuredWidth() - getLeft() - parentMenu.getLeft(), getOffsetY());
|
|
|
|
}
|
2014-11-14 16:40:15 +01:00
|
|
|
} else {
|
2015-02-01 19:51:02 +01:00
|
|
|
popupWindow.showAsDropDown(this, -AndroidUtilities.dp(8), getOffsetY());
|
2014-11-14 16:40:15 +01:00
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 18:56:31 +01:00
|
|
|
private int getOffsetY() {
|
|
|
|
if (showFromBottom) {
|
|
|
|
getLocationOnScreen(location);
|
|
|
|
int diff = location[1] - AndroidUtilities.statusBarHeight + getMeasuredHeight() - menuHeight;
|
2015-01-02 23:15:07 +01:00
|
|
|
int y = -menuHeight;
|
2014-12-01 18:56:31 +01:00
|
|
|
if (diff < 0) {
|
|
|
|
y -= diff;
|
|
|
|
}
|
|
|
|
return y - (needOffset ? AndroidUtilities.statusBarHeight : 0);
|
|
|
|
} else {
|
|
|
|
return -getMeasuredHeight() - (needOffset ? AndroidUtilities.statusBarHeight : 0);
|
2014-11-17 03:44:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
public void openSearch() {
|
|
|
|
if (searchContainer == null || searchContainer.getVisibility() == VISIBLE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parentMenu.parentActionBar.onSearchFieldVisibilityChanged(toggleSearch());
|
|
|
|
}
|
|
|
|
|
2014-06-04 01:31:48 +02:00
|
|
|
public boolean toggleSearch() {
|
2015-01-02 23:15:07 +01:00
|
|
|
if (searchContainer == null) {
|
2014-06-04 01:31:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
if (searchContainer.getVisibility() == VISIBLE) {
|
2015-03-19 00:09:45 +01:00
|
|
|
if (listener == null || listener != null && listener.onSearchCollapse()) {
|
|
|
|
searchContainer.setVisibility(GONE);
|
|
|
|
setVisibility(VISIBLE);
|
|
|
|
AndroidUtilities.hideKeyboard(searchField);
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
2015-01-02 23:15:07 +01:00
|
|
|
searchContainer.setVisibility(VISIBLE);
|
2014-06-04 01:31:48 +02:00
|
|
|
setVisibility(GONE);
|
|
|
|
searchField.setText("");
|
|
|
|
searchField.requestFocus();
|
2014-07-03 00:39:05 +02:00
|
|
|
AndroidUtilities.showKeyboard(searchField);
|
2014-06-04 01:31:48 +02:00
|
|
|
if (listener != null) {
|
|
|
|
listener.onSearchExpand();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void closeSubMenu() {
|
|
|
|
if (popupWindow != null && popupWindow.isShowing()) {
|
|
|
|
popupWindow.dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:32:51 +01:00
|
|
|
public void setIcon(int resId) {
|
|
|
|
iconView.setImageResource(resId);
|
|
|
|
}
|
|
|
|
|
2014-06-04 01:31:48 +02:00
|
|
|
public EditText getSearchField() {
|
|
|
|
return searchField;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActionBarMenuItem setIsSearchField(boolean value) {
|
2015-01-02 23:15:07 +01:00
|
|
|
if (value && searchContainer == null) {
|
|
|
|
searchContainer = new FrameLayout(getContext());
|
|
|
|
parentMenu.addView(searchContainer, 0);
|
|
|
|
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)searchContainer.getLayoutParams();
|
|
|
|
layoutParams.weight = 1;
|
|
|
|
layoutParams.width = 0;
|
|
|
|
layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
layoutParams.leftMargin = AndroidUtilities.dp(6);
|
|
|
|
searchContainer.setLayoutParams(layoutParams);
|
|
|
|
searchContainer.setVisibility(GONE);
|
|
|
|
|
2014-06-04 01:31:48 +02:00
|
|
|
searchField = new EditText(getContext());
|
2015-01-02 23:15:07 +01:00
|
|
|
searchField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
|
|
|
|
searchField.setHintTextColor(0x88ffffff);
|
2014-06-04 01:31:48 +02:00
|
|
|
searchField.setTextColor(0xffffffff);
|
|
|
|
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);
|
2014-06-04 01:31:48 +02:00
|
|
|
searchField.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
|
2014-09-24 04:17:27 +02:00
|
|
|
if (android.os.Build.VERSION.SDK_INT < 11) {
|
|
|
|
searchField.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
|
|
|
menu.clear();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
searchField.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
|
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
searchField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
2015-02-26 02:32:51 +01:00
|
|
|
if (actionId == EditorInfo.IME_ACTION_SEARCH || event != null && (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_SEARCH || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
|
2014-07-03 00:39:05 +02:00
|
|
|
AndroidUtilities.hideKeyboard(searchField);
|
2015-01-02 23:15:07 +01:00
|
|
|
if (listener != null) {
|
|
|
|
listener.onSearchPressed(searchField);
|
|
|
|
}
|
2014-06-04 01:31:48 +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) {
|
|
|
|
if (listener != null) {
|
|
|
|
listener.onTextChanged(searchField);
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
ViewProxy.setAlpha(clearButton, s == null || s.length() == 0 ? 0.6f : 1.0f);
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
|
|
|
|
mCursorDrawableRes.setAccessible(true);
|
|
|
|
mCursorDrawableRes.set(searchField, R.drawable.search_carret);
|
|
|
|
} catch (Exception e) {
|
2014-06-04 18:00:42 +02:00
|
|
|
//nothing to do
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
|
|
|
searchField.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME_ACTION_SEARCH);
|
2014-06-04 18:00:42 +02:00
|
|
|
searchField.setTextIsSelectable(false);
|
2014-06-04 01:31:48 +02:00
|
|
|
} else {
|
|
|
|
searchField.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
searchContainer.addView(searchField);
|
|
|
|
FrameLayout.LayoutParams layoutParams2 = (FrameLayout.LayoutParams) searchField.getLayoutParams();
|
|
|
|
layoutParams2.width = FrameLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
layoutParams2.gravity = Gravity.CENTER_VERTICAL;
|
|
|
|
layoutParams2.height = AndroidUtilities.dp(36);
|
|
|
|
layoutParams2.rightMargin = AndroidUtilities.dp(48);
|
|
|
|
searchField.setLayoutParams(layoutParams2);
|
|
|
|
|
|
|
|
clearButton = new ImageView(getContext());
|
|
|
|
clearButton.setImageResource(R.drawable.ic_close_white);
|
2015-02-01 19:51:02 +01:00
|
|
|
clearButton.setScaleType(ImageView.ScaleType.CENTER);
|
2015-01-02 23:15:07 +01:00
|
|
|
clearButton.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
searchField.setText("");
|
|
|
|
AndroidUtilities.showKeyboard(searchField);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
searchContainer.addView(clearButton);
|
|
|
|
layoutParams2 = (FrameLayout.LayoutParams) clearButton.getLayoutParams();
|
|
|
|
layoutParams2.width = AndroidUtilities.dp(48);
|
|
|
|
layoutParams2.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
|
|
|
|
layoutParams2.height = FrameLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
clearButton.setLayoutParams(layoutParams2);
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
isSearchField = value;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSearchField() {
|
|
|
|
return isSearchField;
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
public ActionBarMenuItem setActionBarMenuItemSearchListener(ActionBarMenuItemSearchListener listener) {
|
2014-06-04 01:31:48 +02:00
|
|
|
this.listener = listener;
|
2015-01-02 23:15:07 +01:00
|
|
|
return this;
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-06 22:34:47 +01:00
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
|
|
super.onLayout(changed, left, top, right, bottom);
|
2014-06-04 01:31:48 +02:00
|
|
|
if (popupWindow != null && popupWindow.isShowing()) {
|
2015-02-01 19:51:02 +01:00
|
|
|
if (subMenuOpenSide == 0) {
|
|
|
|
if (showFromBottom) {
|
|
|
|
popupWindow.update(this, -popupLayout.getMeasuredWidth() + getMeasuredWidth(), getOffsetY(), -1, -1);
|
|
|
|
} else {
|
|
|
|
popupWindow.update(this, parentMenu.parentActionBar.getMeasuredWidth() - popupLayout.getMeasuredWidth() - getLeft() - parentMenu.getLeft(), getOffsetY(), -1, -1);
|
|
|
|
}
|
2014-11-14 16:40:15 +01:00
|
|
|
} else {
|
2015-02-01 19:51:02 +01:00
|
|
|
popupWindow.update(this, -AndroidUtilities.dp(8), getOffsetY(), -1, -1);
|
2014-11-14 16:40:15 +01:00
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-11 01:05:54 +02:00
|
|
|
|
|
|
|
public void hideSubItem(int id) {
|
|
|
|
View view = popupLayout.findViewWithTag(id);
|
|
|
|
if (view != null) {
|
|
|
|
view.setVisibility(GONE);
|
|
|
|
}
|
|
|
|
view = popupLayout.findViewWithTag(100 + id);
|
|
|
|
if (view != null) {
|
|
|
|
view.setVisibility(GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showSubItem(int id) {
|
|
|
|
View view = popupLayout.findViewWithTag(id);
|
|
|
|
if (view != null) {
|
|
|
|
view.setVisibility(VISIBLE);
|
|
|
|
}
|
|
|
|
view = popupLayout.findViewWithTag(100 + id);
|
|
|
|
if (view != null) {
|
|
|
|
view.setVisibility(VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|