2012-05-31 16:01:50 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import android.content.ComponentName;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.content.Context;
|
2012-05-31 16:01:50 -07:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.SubMenu;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.LinearLayout.LayoutParams;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
public class GeckoMenu extends LinearLayout
|
|
|
|
implements Menu, GeckoMenuItem.OnShowAsActionChangedListener {
|
2012-05-31 16:01:50 -07:00
|
|
|
private static final String LOGTAG = "GeckoMenu";
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
public static interface ActionItemBarPresenter {
|
2012-05-31 16:01:50 -07:00
|
|
|
public void addActionItem(View actionItem);
|
2012-06-02 23:09:55 -07:00
|
|
|
public void removeActionItem(int index);
|
2012-05-31 16:01:50 -07:00
|
|
|
public int getActionItemsCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final int NO_ID = 0;
|
|
|
|
|
|
|
|
// Default list of items.
|
|
|
|
private List<GeckoMenuItem> mItems;
|
|
|
|
|
2012-06-02 23:09:55 -07:00
|
|
|
// List of items in action-bar.
|
|
|
|
private List<GeckoMenuItem> mActionItems;
|
2012-05-31 16:01:50 -07:00
|
|
|
|
|
|
|
// Reference to action-items bar in action-bar.
|
|
|
|
private ActionItemBarPresenter mActionItemBarPresenter;
|
|
|
|
|
|
|
|
public GeckoMenu(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
|
|
|
mContext = context;
|
|
|
|
|
|
|
|
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
|
|
|
|
LayoutParams.WRAP_CONTENT));
|
|
|
|
setOrientation(VERTICAL);
|
|
|
|
|
|
|
|
mItems = new ArrayList<GeckoMenuItem>();
|
2012-06-02 23:09:55 -07:00
|
|
|
mActionItems = new ArrayList<GeckoMenuItem>();
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MenuItem add(CharSequence title) {
|
|
|
|
GeckoMenuItem menuItem = new GeckoMenuItem(mContext, NO_ID);
|
|
|
|
menuItem.setTitle(title);
|
|
|
|
addItem(menuItem);
|
|
|
|
return menuItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MenuItem add(int groupId, int itemId, int order, int titleRes) {
|
|
|
|
GeckoMenuItem menuItem = new GeckoMenuItem(mContext, itemId, order);
|
|
|
|
menuItem.setTitle(titleRes);
|
|
|
|
addItem(menuItem);
|
|
|
|
return menuItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MenuItem add(int titleRes) {
|
|
|
|
GeckoMenuItem menuItem = new GeckoMenuItem(mContext, NO_ID);
|
|
|
|
menuItem.setTitle(titleRes);
|
|
|
|
addItem(menuItem);
|
|
|
|
return menuItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MenuItem add(int groupId, int itemId, int order, CharSequence title) {
|
|
|
|
GeckoMenuItem menuItem = new GeckoMenuItem(mContext, itemId, order);
|
|
|
|
menuItem.setTitle(title);
|
|
|
|
addItem(menuItem);
|
|
|
|
return menuItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addItem(GeckoMenuItem menuItem) {
|
|
|
|
menuItem.setOnShowAsActionChangedListener(this);
|
|
|
|
|
|
|
|
// Insert it in proper order.
|
|
|
|
int index = 0;
|
|
|
|
for (GeckoMenuItem item : mItems) {
|
|
|
|
if (item.getOrder() > menuItem.getOrder()) {
|
|
|
|
mItems.add(index, menuItem);
|
|
|
|
|
|
|
|
// Account for the items in the action-bar.
|
|
|
|
if (mActionItemBarPresenter != null)
|
|
|
|
addView(menuItem.getLayout(), index - mActionItemBarPresenter.getActionItemsCount());
|
|
|
|
else
|
|
|
|
addView(menuItem.getLayout(), index);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the menuItem at the end.
|
|
|
|
mItems.add(menuItem);
|
|
|
|
addView(menuItem.getLayout());
|
|
|
|
}
|
|
|
|
|
2012-06-02 23:09:55 -07:00
|
|
|
private void addActionItem(GeckoMenuItem menuItem) {
|
|
|
|
menuItem.setOnShowAsActionChangedListener(this);
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
for (GeckoMenuItem item : mItems) {
|
|
|
|
if (item.getOrder() <= menuItem.getOrder())
|
|
|
|
index++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mActionItems.add(menuItem);
|
|
|
|
mItems.add(index, menuItem);
|
2012-05-31 16:01:50 -07:00
|
|
|
mActionItemBarPresenter.addActionItem(menuItem.getLayout());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SubMenu addSubMenu(int groupId, int itemId, int order, CharSequence title) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SubMenu addSubMenu(CharSequence title) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SubMenu addSubMenu(int titleRes) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MenuItem findItem(int id) {
|
|
|
|
for (GeckoMenuItem menuItem : mItems) {
|
|
|
|
if (menuItem.getItemId() == id)
|
|
|
|
return menuItem;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MenuItem getItem(int index) {
|
|
|
|
if (index < mItems.size())
|
|
|
|
return mItems.get(index);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasVisibleItems() {
|
|
|
|
for (GeckoMenuItem menuItem : mItems) {
|
|
|
|
if (menuItem.isVisible())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isShortcutKey(int keyCode, KeyEvent event) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean performIdentifierAction(int id, int flags) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean performShortcut(int keyCode, KeyEvent event, int flags) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeGroup(int groupId) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeItem(int id) {
|
|
|
|
for (GeckoMenuItem menuItem : mItems) {
|
|
|
|
if (menuItem.getItemId() == id) {
|
2012-06-02 23:09:55 -07:00
|
|
|
if (mActionItems.contains(menuItem)) {
|
|
|
|
if (mActionItemBarPresenter != null)
|
|
|
|
mActionItemBarPresenter.removeActionItem(mActionItems.indexOf(menuItem));
|
2012-05-31 16:01:50 -07:00
|
|
|
|
2012-06-02 23:09:55 -07:00
|
|
|
mActionItems.remove(menuItem);
|
|
|
|
} else {
|
|
|
|
removeView(findViewById(id));
|
|
|
|
}
|
2012-05-31 16:01:50 -07:00
|
|
|
|
|
|
|
mItems.remove(menuItem);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setGroupCheckable(int group, boolean checkable, boolean exclusive) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setGroupEnabled(int group, boolean enabled) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setGroupVisible(int group, boolean visible) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setQwertyMode(boolean isQwerty) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int size() {
|
|
|
|
return mItems.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-02 23:09:55 -07:00
|
|
|
public boolean hasActionItemBar() {
|
|
|
|
return (mActionItemBarPresenter != null);
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
2012-06-02 23:09:55 -07:00
|
|
|
@Override
|
|
|
|
public void onShowAsActionChanged(GeckoMenuItem item, boolean isActionItem) {
|
2012-05-31 16:01:50 -07:00
|
|
|
removeItem(item.getItemId());
|
2012-06-02 23:09:55 -07:00
|
|
|
|
|
|
|
if (isActionItem)
|
|
|
|
addActionItem(item);
|
|
|
|
else
|
|
|
|
addItem(item);
|
2012-05-31 16:01:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setActionItemBarPresenter(ActionItemBarPresenter presenter) {
|
|
|
|
mActionItemBarPresenter = presenter;
|
|
|
|
}
|
|
|
|
}
|