2 * Copyright 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package net.sf.openrocket.android.actionbarcompat;
19 import java.util.ArrayList;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.res.Resources;
25 import android.view.KeyEvent;
26 import android.view.Menu;
27 import android.view.MenuItem;
28 import android.view.SubMenu;
31 * A <em>really</em> dumb implementation of the {@link android.view.Menu} interface, that's only
32 * useful for our actionbar-compat purposes. See
33 * <code>com.android.internal.view.menu.MenuBuilder</code> in AOSP for a more complete
36 public class SimpleMenu implements Menu {
38 private Context mContext;
39 private Resources mResources;
41 private ArrayList<SimpleMenuItem> mItems;
43 public SimpleMenu(Context context) {
45 mResources = context.getResources();
46 mItems = new ArrayList<SimpleMenuItem>();
49 public Context getContext() {
53 public Resources getResources() {
57 public MenuItem add(CharSequence title) {
58 return addInternal(0, 0, title);
61 public MenuItem add(int titleRes) {
62 return addInternal(0, 0, mResources.getString(titleRes));
65 public MenuItem add(int groupId, int itemId, int order, CharSequence title) {
66 return addInternal(itemId, order, title);
69 public MenuItem add(int groupId, int itemId, int order, int titleRes) {
70 return addInternal(itemId, order, mResources.getString(titleRes));
74 * Adds an item to the menu. The other add methods funnel to this.
76 private MenuItem addInternal(int itemId, int order, CharSequence title) {
77 final SimpleMenuItem item = new SimpleMenuItem(this, itemId, order, title);
78 mItems.add(findInsertIndex(mItems, order), item);
82 private static int findInsertIndex(ArrayList<? extends MenuItem> items, int order) {
83 for (int i = items.size() - 1; i >= 0; i--) {
84 MenuItem item = items.get(i);
85 if (item.getOrder() <= order) {
93 public int findItemIndex(int id) {
94 final int size = size();
96 for (int i = 0; i < size; i++) {
97 SimpleMenuItem item = mItems.get(i);
98 if (item.getItemId() == id) {
106 public void removeItem(int itemId) {
107 removeItemAtInt(findItemIndex(itemId));
110 private void removeItemAtInt(int index) {
111 if ((index < 0) || (index >= mItems.size())) {
114 mItems.remove(index);
117 public void clear() {
121 public MenuItem findItem(int id) {
122 final int size = size();
123 for (int i = 0; i < size; i++) {
124 SimpleMenuItem item = mItems.get(i);
125 if (item.getItemId() == id) {
134 return mItems.size();
137 public MenuItem getItem(int index) {
138 return mItems.get(index);
141 // Unsupported operations.
143 public SubMenu addSubMenu(CharSequence charSequence) {
144 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
147 public SubMenu addSubMenu(int titleRes) {
148 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
151 public SubMenu addSubMenu(int groupId, int itemId, int order, CharSequence title) {
152 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
155 public SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes) {
156 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
159 public int addIntentOptions(int i, int i1, int i2, ComponentName componentName,
160 Intent[] intents, Intent intent, int i3, MenuItem[] menuItems) {
161 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
164 public void removeGroup(int i) {
165 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
168 public void setGroupCheckable(int i, boolean b, boolean b1) {
169 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
172 public void setGroupVisible(int i, boolean b) {
173 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
176 public void setGroupEnabled(int i, boolean b) {
177 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
180 public boolean hasVisibleItems() {
181 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
184 public void close() {
185 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
188 public boolean performShortcut(int i, KeyEvent keyEvent, int i1) {
189 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
192 public boolean isShortcutKey(int i, KeyEvent keyEvent) {
193 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
196 public boolean performIdentifierAction(int i, int i1) {
197 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");
200 public void setQwertyMode(boolean b) {
201 throw new UnsupportedOperationException("This operation is not supported for SimpleMenu");