ActionMenuItem.java revision 33b974393b6fadcefc896ec4a0f9b66724f61e9f
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
17package com.android.internal.view.menu;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.drawable.Drawable;
22import android.view.MenuItem;
23import android.view.SubMenu;
24import android.view.ContextMenu.ContextMenuInfo;
25
26/**
27 * @hide
28 */
29public class ActionMenuItem implements MenuItem {
30    private final int mId;
31    private final int mGroup;
32    private final int mCategoryOrder;
33    private final int mOrdering;
34
35    private CharSequence mTitle;
36    private CharSequence mTitleCondensed;
37    private Intent mIntent;
38    private char mShortcutNumericChar;
39    private char mShortcutAlphabeticChar;
40
41    private Drawable mIconDrawable;
42    private int mIconResId = NO_ICON;
43
44    private Context mContext;
45
46    private MenuItem.OnMenuItemClickListener mClickListener;
47
48    private static final int NO_ICON = 0;
49
50    private int mFlags = ENABLED;
51    private static final int CHECKABLE      = 0x00000001;
52    private static final int CHECKED        = 0x00000002;
53    private static final int EXCLUSIVE      = 0x00000004;
54    private static final int HIDDEN         = 0x00000008;
55    private static final int ENABLED        = 0x00000010;
56
57    public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering,
58            CharSequence title) {
59        mContext = context;
60        mId = id;
61        mGroup = group;
62        mCategoryOrder = categoryOrder;
63        mOrdering = ordering;
64        mTitle = title;
65    }
66
67    public char getAlphabeticShortcut() {
68        return mShortcutAlphabeticChar;
69    }
70
71    public int getGroupId() {
72        return mGroup;
73    }
74
75    public Drawable getIcon() {
76        return mIconDrawable;
77    }
78
79    public Intent getIntent() {
80        return mIntent;
81    }
82
83    public int getItemId() {
84        return mId;
85    }
86
87    public ContextMenuInfo getMenuInfo() {
88        return null;
89    }
90
91    public char getNumericShortcut() {
92        return mShortcutNumericChar;
93    }
94
95    public int getOrder() {
96        return mOrdering;
97    }
98
99    public SubMenu getSubMenu() {
100        return null;
101    }
102
103    public CharSequence getTitle() {
104        return mTitle;
105    }
106
107    public CharSequence getTitleCondensed() {
108        return mTitleCondensed;
109    }
110
111    public boolean hasSubMenu() {
112        return false;
113    }
114
115    public boolean isCheckable() {
116        return (mFlags & CHECKABLE) != 0;
117    }
118
119    public boolean isChecked() {
120        return (mFlags & CHECKED) != 0;
121    }
122
123    public boolean isEnabled() {
124        return (mFlags & ENABLED) != 0;
125    }
126
127    public boolean isVisible() {
128        return (mFlags & HIDDEN) == 0;
129    }
130
131    public MenuItem setAlphabeticShortcut(char alphaChar) {
132        mShortcutAlphabeticChar = alphaChar;
133        return this;
134    }
135
136    public MenuItem setCheckable(boolean checkable) {
137        mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
138        return this;
139    }
140
141    public ActionMenuItem setExclusiveCheckable(boolean exclusive) {
142        mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
143        return this;
144    }
145
146    public MenuItem setChecked(boolean checked) {
147        mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
148        return this;
149    }
150
151    public MenuItem setEnabled(boolean enabled) {
152        mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0);
153        return this;
154    }
155
156    public MenuItem setIcon(Drawable icon) {
157        mIconDrawable = icon;
158        mIconResId = NO_ICON;
159        return this;
160    }
161
162    public MenuItem setIcon(int iconRes) {
163        mIconResId = iconRes;
164        mIconDrawable = mContext.getResources().getDrawable(iconRes);
165        return this;
166    }
167
168    public MenuItem setIntent(Intent intent) {
169        mIntent = intent;
170        return this;
171    }
172
173    public MenuItem setNumericShortcut(char numericChar) {
174        mShortcutNumericChar = numericChar;
175        return this;
176    }
177
178    public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
179        mClickListener = menuItemClickListener;
180        return this;
181    }
182
183    public MenuItem setShortcut(char numericChar, char alphaChar) {
184        mShortcutNumericChar = numericChar;
185        mShortcutAlphabeticChar = alphaChar;
186        return this;
187    }
188
189    public MenuItem setTitle(CharSequence title) {
190        mTitle = title;
191        return this;
192    }
193
194    public MenuItem setTitle(int title) {
195        mTitle = mContext.getResources().getString(title);
196        return this;
197    }
198
199    public MenuItem setTitleCondensed(CharSequence title) {
200        mTitleCondensed = title;
201        return this;
202    }
203
204    public MenuItem setVisible(boolean visible) {
205        mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN);
206        return this;
207    }
208
209    public boolean invoke() {
210        if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
211            return true;
212        }
213
214        if (mIntent != null) {
215            mContext.startActivity(mIntent);
216            return true;
217        }
218
219        return false;
220    }
221}
222