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-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;
|
|
|
|
|
2014-07-03 00:39:05 +02:00
|
|
|
import org.telegram.android.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;
|
2014-11-11 23:16:17 +01:00
|
|
|
view.setBackgroundResource(parentActionBar.itemsBackgroundResourceId);
|
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) {
|
|
|
|
return addItem(id, 0, parentActionBar.itemsBackgroundResourceId, drawable, AndroidUtilities.dp(48));
|
|
|
|
}
|
|
|
|
|
2014-06-04 01:31:48 +02:00
|
|
|
public ActionBarMenuItem addItem(int id, int icon) {
|
2014-11-11 23:16:17 +01:00
|
|
|
return addItem(id, icon, parentActionBar.itemsBackgroundResourceId);
|
2014-09-24 04:17:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ActionBarMenuItem addItem(int id, int icon, int backgroundResource) {
|
2014-11-17 03:44:57 +01:00
|
|
|
return addItem(id, icon, backgroundResource, null, AndroidUtilities.dp(48));
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActionBarMenuItem addItemWithWidth(int id, int icon, int width) {
|
|
|
|
return addItem(id, icon, parentActionBar.itemsBackgroundResourceId, null, width);
|
2014-11-14 16:40:15 +01:00
|
|
|
}
|
|
|
|
|
2014-11-17 03:44:57 +01:00
|
|
|
public ActionBarMenuItem addItem(int id, int icon, int backgroundResource, Drawable drawable, int width) {
|
2014-11-11 23:16:17 +01:00
|
|
|
ActionBarMenuItem menuItem = new ActionBarMenuItem(getContext(), this, backgroundResource);
|
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()) {
|
2014-11-11 23:16:17 +01:00
|
|
|
parentActionBar.onSearchFieldVisibilityChanged(item.toggleSearch());
|
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() {
|
2014-06-04 01:31:48 +02:00
|
|
|
for (int a = 0; a < getChildCount(); a++) {
|
|
|
|
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() {
|
|
|
|
for (int a = 0; a < getChildCount(); a++) {
|
|
|
|
View view = getChildAt(a);
|
|
|
|
removeView(view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onMenuButtonPressed() {
|
|
|
|
for (int a = 0; a < getChildCount(); a++) {
|
|
|
|
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() {
|
|
|
|
for (int a = 0; a < getChildCount(); a++) {
|
|
|
|
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()) {
|
2014-11-11 23:16:17 +01:00
|
|
|
parentActionBar.onSearchFieldVisibilityChanged(item.toggleSearch());
|
2015-03-19 00:09:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:34:47 +01:00
|
|
|
public void openSearchField(boolean toggle, String text) {
|
2015-03-19 00:09:45 +01:00
|
|
|
for (int a = 0; a < getChildCount(); a++) {
|
|
|
|
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) {
|
|
|
|
parentActionBar.onSearchFieldVisibilityChanged(item.toggleSearch());
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|