2014-06-04 01:31:48 +02:00
|
|
|
/*
|
2015-10-29 18:10:07 +01:00
|
|
|
* This is the source code of Telegram for Android v. 3.x.x.
|
2014-06-04 01:31:48 +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).
|
|
|
|
*
|
2016-01-11 18:19:48 +01:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2016.
|
2014-06-04 01:31:48 +02:00
|
|
|
*/
|
|
|
|
|
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-11-17 03:44:57 +01:00
|
|
|
import android.graphics.drawable.Drawable;
|
2014-06-04 01:31:48 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
2015-05-03 13:48:36 +02:00
|
|
|
import org.telegram.ui.Components.LayoutHelper;
|
2014-06-04 01:31:48 +02:00
|
|
|
|
|
|
|
public class ActionBarMenu extends LinearLayout {
|
|
|
|
|
2014-11-11 23:16:17 +01:00
|
|
|
protected ActionBar parentActionBar;
|
2014-06-04 01:31:48 +02:00
|
|
|
|
2014-11-11 23:16:17 +01:00
|
|
|
public ActionBarMenu(Context context, ActionBar layer) {
|
2014-06-04 01:31:48 +02:00
|
|
|
super(context);
|
|
|
|
setOrientation(LinearLayout.HORIZONTAL);
|
2014-11-11 23:16:17 +01:00
|
|
|
parentActionBar = layer;
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ActionBarMenu(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public View addItemResource(int id, int resourceId) {
|
|
|
|
LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
View view = li.inflate(resourceId, null);
|
|
|
|
view.setTag(id);
|
|
|
|
addView(view);
|
2015-07-22 20:56:37 +02:00
|
|
|
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
|
2015-05-03 13:48:36 +02:00
|
|
|
layoutParams.height = LayoutHelper.MATCH_PARENT;
|
2016-04-22 15:49:00 +02:00
|
|
|
view.setBackgroundDrawable(Theme.createBarSelectorDrawable(parentActionBar.itemsBackgroundColor));
|
2014-06-04 01:31:48 +02:00
|
|
|
view.setLayoutParams(layoutParams);
|
|
|
|
view.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2015-03-19 00:09:45 +01:00
|
|
|
onItemClick((Integer) view.getTag());
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:44:57 +01:00
|
|
|
public ActionBarMenuItem addItem(int id, Drawable drawable) {
|
2016-04-22 15:49:00 +02:00
|
|
|
return addItem(id, 0, parentActionBar.itemsBackgroundColor, drawable, AndroidUtilities.dp(48));
|
2014-11-17 03:44:57 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 01:31:48 +02:00
|
|
|
public ActionBarMenuItem addItem(int id, int icon) {
|
2016-04-22 15:49:00 +02:00
|
|
|
return addItem(id, icon, parentActionBar.itemsBackgroundColor);
|
2014-09-24 04:17:27 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
public ActionBarMenuItem addItem(int id, int icon, int backgroundColor) {
|
|
|
|
return addItem(id, icon, backgroundColor, null, AndroidUtilities.dp(48));
|
2014-11-17 03:44:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public ActionBarMenuItem addItemWithWidth(int id, int icon, int width) {
|
2016-04-22 15:49:00 +02:00
|
|
|
return addItem(id, icon, parentActionBar.itemsBackgroundColor, null, width);
|
2014-11-14 16:40:15 +01:00
|
|
|
}
|
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
public ActionBarMenuItem addItem(int id, int icon, int backgroundColor, Drawable drawable, int width) {
|
|
|
|
ActionBarMenuItem menuItem = new ActionBarMenuItem(getContext(), this, backgroundColor);
|
2014-06-04 01:31:48 +02:00
|
|
|
menuItem.setTag(id);
|
2014-11-17 03:44:57 +01:00
|
|
|
if (drawable != null) {
|
2015-02-01 19:51:02 +01:00
|
|
|
menuItem.iconView.setImageDrawable(drawable);
|
2014-11-17 03:44:57 +01:00
|
|
|
} else {
|
2015-02-01 19:51:02 +01:00
|
|
|
menuItem.iconView.setImageResource(icon);
|
2014-11-17 03:44:57 +01:00
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
addView(menuItem);
|
2015-07-22 20:56:37 +02:00
|
|
|
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) menuItem.getLayoutParams();
|
2015-05-03 13:48:36 +02:00
|
|
|
layoutParams.height = LayoutHelper.MATCH_PARENT;
|
2014-11-14 16:40:15 +01:00
|
|
|
layoutParams.width = width;
|
2014-06-04 01:31:48 +02:00
|
|
|
menuItem.setLayoutParams(layoutParams);
|
|
|
|
menuItem.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2015-07-22 20:56:37 +02:00
|
|
|
ActionBarMenuItem item = (ActionBarMenuItem) view;
|
2014-06-04 01:31:48 +02:00
|
|
|
if (item.hasSubMenu()) {
|
2014-11-11 23:16:17 +01:00
|
|
|
if (parentActionBar.actionBarMenuOnItemClick.canOpenMenu()) {
|
2014-06-11 01:05:54 +02:00
|
|
|
item.toggleSubMenu();
|
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
} else if (item.isSearchField()) {
|
2015-09-24 22:52:02 +02:00
|
|
|
parentActionBar.onSearchFieldVisibilityChanged(item.toggleSearch(true));
|
2014-06-04 01:31:48 +02:00
|
|
|
} else {
|
2015-07-22 20:56:37 +02:00
|
|
|
onItemClick((Integer) view.getTag());
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return menuItem;
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:00:42 +02:00
|
|
|
public void hideAllPopupMenus() {
|
2016-06-24 12:27:15 +02:00
|
|
|
int count = getChildCount();
|
|
|
|
for (int a = 0; a < count; a++) {
|
2014-06-04 01:31:48 +02:00
|
|
|
View view = getChildAt(a);
|
|
|
|
if (view instanceof ActionBarMenuItem) {
|
2015-07-22 20:56:37 +02:00
|
|
|
((ActionBarMenuItem) view).closeSubMenu();
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onItemClick(int id) {
|
2014-11-11 23:16:17 +01:00
|
|
|
if (parentActionBar.actionBarMenuOnItemClick != null) {
|
|
|
|
parentActionBar.actionBarMenuOnItemClick.onItemClick(id);
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearItems() {
|
2016-04-25 21:26:41 +02:00
|
|
|
removeAllViews();
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onMenuButtonPressed() {
|
2016-06-24 12:27:15 +02:00
|
|
|
int count = getChildCount();
|
|
|
|
for (int a = 0; a < count; a++) {
|
2014-06-04 01:31:48 +02:00
|
|
|
View view = getChildAt(a);
|
|
|
|
if (view instanceof ActionBarMenuItem) {
|
2015-07-22 20:56:37 +02:00
|
|
|
ActionBarMenuItem item = (ActionBarMenuItem) view;
|
|
|
|
if (item.getVisibility() != VISIBLE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (item.hasSubMenu()) {
|
2014-06-04 01:31:48 +02:00
|
|
|
item.toggleSubMenu();
|
2014-11-20 15:45:33 +01:00
|
|
|
break;
|
2015-07-22 20:56:37 +02:00
|
|
|
} else if (item.overrideMenuClick) {
|
|
|
|
onItemClick((Integer) item.getTag());
|
|
|
|
break;
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void closeSearchField() {
|
2016-06-24 12:27:15 +02:00
|
|
|
int count = getChildCount();
|
|
|
|
for (int a = 0; a < count; a++) {
|
2014-06-04 01:31:48 +02:00
|
|
|
View view = getChildAt(a);
|
|
|
|
if (view instanceof ActionBarMenuItem) {
|
2015-07-22 20:56:37 +02:00
|
|
|
ActionBarMenuItem item = (ActionBarMenuItem) view;
|
2014-06-04 01:31:48 +02:00
|
|
|
if (item.isSearchField()) {
|
2015-09-24 22:52:02 +02:00
|
|
|
parentActionBar.onSearchFieldVisibilityChanged(item.toggleSearch(false));
|
2015-03-19 00:09:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:34:47 +01:00
|
|
|
public void openSearchField(boolean toggle, String text) {
|
2016-06-24 12:27:15 +02:00
|
|
|
int count = getChildCount();
|
|
|
|
for (int a = 0; a < count; a++) {
|
2015-03-19 00:09:45 +01:00
|
|
|
View view = getChildAt(a);
|
|
|
|
if (view instanceof ActionBarMenuItem) {
|
2015-07-22 20:56:37 +02:00
|
|
|
ActionBarMenuItem item = (ActionBarMenuItem) view;
|
2015-03-19 00:09:45 +01:00
|
|
|
if (item.isSearchField()) {
|
2015-03-26 18:34:47 +01:00
|
|
|
if (toggle) {
|
2015-09-24 22:52:02 +02:00
|
|
|
parentActionBar.onSearchFieldVisibilityChanged(item.toggleSearch(true));
|
2015-03-26 18:34:47 +01:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
item.getSearchField().setText(text);
|
|
|
|
item.getSearchField().setSelection(text.length());
|
|
|
|
break;
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActionBarMenuItem getItem(int id) {
|
|
|
|
View v = findViewWithTag(id);
|
|
|
|
if (v instanceof ActionBarMenuItem) {
|
2015-07-22 20:56:37 +02:00
|
|
|
return (ActionBarMenuItem) v;
|
2014-06-04 01:31:48 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|