create changelog entry
[debian/openrocket] / android-libraries / ActionBarSherlock / src / com / actionbarsherlock / internal / view / menu / ActionMenu.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.actionbarsherlock.internal.view.menu;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.view.KeyEvent;
28
29 import com.actionbarsherlock.view.Menu;
30 import com.actionbarsherlock.view.MenuItem;
31 import com.actionbarsherlock.view.SubMenu;
32
33 /**
34  * @hide
35  */
36 public class ActionMenu implements Menu {
37     private Context mContext;
38
39     private boolean mIsQwerty;
40
41     private ArrayList<ActionMenuItem> mItems;
42
43     public ActionMenu(Context context) {
44         mContext = context;
45         mItems = new ArrayList<ActionMenuItem>();
46     }
47
48     public Context getContext() {
49         return mContext;
50     }
51
52     public MenuItem add(CharSequence title) {
53         return add(0, 0, 0, title);
54     }
55
56     public MenuItem add(int titleRes) {
57         return add(0, 0, 0, titleRes);
58     }
59
60     public MenuItem add(int groupId, int itemId, int order, int titleRes) {
61         return add(groupId, itemId, order, mContext.getResources().getString(titleRes));
62     }
63
64     public MenuItem add(int groupId, int itemId, int order, CharSequence title) {
65         ActionMenuItem item = new ActionMenuItem(getContext(),
66                 groupId, itemId, 0, order, title);
67         mItems.add(order, item);
68         return item;
69     }
70
71     public int addIntentOptions(int groupId, int itemId, int order,
72             ComponentName caller, Intent[] specifics, Intent intent, int flags,
73             MenuItem[] outSpecificItems) {
74         PackageManager pm = mContext.getPackageManager();
75         final List<ResolveInfo> lri =
76                 pm.queryIntentActivityOptions(caller, specifics, intent, 0);
77         final int N = lri != null ? lri.size() : 0;
78
79         if ((flags & FLAG_APPEND_TO_GROUP) == 0) {
80             removeGroup(groupId);
81         }
82
83         for (int i=0; i<N; i++) {
84             final ResolveInfo ri = lri.get(i);
85             Intent rintent = new Intent(
86                 ri.specificIndex < 0 ? intent : specifics[ri.specificIndex]);
87             rintent.setComponent(new ComponentName(
88                     ri.activityInfo.applicationInfo.packageName,
89                     ri.activityInfo.name));
90             final MenuItem item = add(groupId, itemId, order, ri.loadLabel(pm))
91                     .setIcon(ri.loadIcon(pm))
92                     .setIntent(rintent);
93             if (outSpecificItems != null && ri.specificIndex >= 0) {
94                 outSpecificItems[ri.specificIndex] = item;
95             }
96         }
97
98         return N;
99     }
100
101     public SubMenu addSubMenu(CharSequence title) {
102         // TODO Implement submenus
103         return null;
104     }
105
106     public SubMenu addSubMenu(int titleRes) {
107         // TODO Implement submenus
108         return null;
109     }
110
111     public SubMenu addSubMenu(int groupId, int itemId, int order,
112             CharSequence title) {
113         // TODO Implement submenus
114         return null;
115     }
116
117     public SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes) {
118         // TODO Implement submenus
119         return null;
120     }
121
122     public void clear() {
123         mItems.clear();
124     }
125
126     public void close() {
127     }
128
129     private int findItemIndex(int id) {
130         final ArrayList<ActionMenuItem> items = mItems;
131         final int itemCount = items.size();
132         for (int i = 0; i < itemCount; i++) {
133             if (items.get(i).getItemId() == id) {
134                 return i;
135             }
136         }
137
138         return -1;
139     }
140
141     public MenuItem findItem(int id) {
142         return mItems.get(findItemIndex(id));
143     }
144
145     public MenuItem getItem(int index) {
146         return mItems.get(index);
147     }
148
149     public boolean hasVisibleItems() {
150         final ArrayList<ActionMenuItem> items = mItems;
151         final int itemCount = items.size();
152
153         for (int i = 0; i < itemCount; i++) {
154             if (items.get(i).isVisible()) {
155                 return true;
156             }
157         }
158
159         return false;
160     }
161
162     private ActionMenuItem findItemWithShortcut(int keyCode, KeyEvent event) {
163         // TODO Make this smarter.
164         final boolean qwerty = mIsQwerty;
165         final ArrayList<ActionMenuItem> items = mItems;
166         final int itemCount = items.size();
167
168         for (int i = 0; i < itemCount; i++) {
169             ActionMenuItem item = items.get(i);
170             final char shortcut = qwerty ? item.getAlphabeticShortcut() :
171                     item.getNumericShortcut();
172             if (keyCode == shortcut) {
173                 return item;
174             }
175         }
176         return null;
177     }
178
179     public boolean isShortcutKey(int keyCode, KeyEvent event) {
180         return findItemWithShortcut(keyCode, event) != null;
181     }
182
183     public boolean performIdentifierAction(int id, int flags) {
184         final int index = findItemIndex(id);
185         if (index < 0) {
186             return false;
187         }
188
189         return mItems.get(index).invoke();
190     }
191
192     public boolean performShortcut(int keyCode, KeyEvent event, int flags) {
193         ActionMenuItem item = findItemWithShortcut(keyCode, event);
194         if (item == null) {
195             return false;
196         }
197
198         return item.invoke();
199     }
200
201     public void removeGroup(int groupId) {
202         final ArrayList<ActionMenuItem> items = mItems;
203         int itemCount = items.size();
204         int i = 0;
205         while (i < itemCount) {
206             if (items.get(i).getGroupId() == groupId) {
207                 items.remove(i);
208                 itemCount--;
209             } else {
210                 i++;
211             }
212         }
213     }
214
215     public void removeItem(int id) {
216         mItems.remove(findItemIndex(id));
217     }
218
219     public void setGroupCheckable(int group, boolean checkable,
220             boolean exclusive) {
221         final ArrayList<ActionMenuItem> items = mItems;
222         final int itemCount = items.size();
223
224         for (int i = 0; i < itemCount; i++) {
225             ActionMenuItem item = items.get(i);
226             if (item.getGroupId() == group) {
227                 item.setCheckable(checkable);
228                 item.setExclusiveCheckable(exclusive);
229             }
230         }
231     }
232
233     public void setGroupEnabled(int group, boolean enabled) {
234         final ArrayList<ActionMenuItem> items = mItems;
235         final int itemCount = items.size();
236
237         for (int i = 0; i < itemCount; i++) {
238             ActionMenuItem item = items.get(i);
239             if (item.getGroupId() == group) {
240                 item.setEnabled(enabled);
241             }
242         }
243     }
244
245     public void setGroupVisible(int group, boolean visible) {
246         final ArrayList<ActionMenuItem> items = mItems;
247         final int itemCount = items.size();
248
249         for (int i = 0; i < itemCount; i++) {
250             ActionMenuItem item = items.get(i);
251             if (item.getGroupId() == group) {
252                 item.setVisible(visible);
253             }
254         }
255     }
256
257     public void setQwertyMode(boolean isQwerty) {
258         mIsQwerty = isQwerty;
259     }
260
261     public int size() {
262         return mItems.size();
263     }
264 }