ActionMenuItem.java revision 6eea0d2a4ce20c9709d2cc9c24c823f7ef795c0a
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.annotation.Nullable;
20import android.content.Context;
21import android.content.Intent;
22import android.content.res.ColorStateList;
23import android.graphics.PorterDuff;
24import android.graphics.drawable.Drawable;
25import android.view.ActionProvider;
26import android.view.ContextMenu.ContextMenuInfo;
27import android.view.KeyEvent;
28import android.view.MenuItem;
29import android.view.SubMenu;
30import android.view.View;
31
32/**
33 * @hide
34 */
35public class ActionMenuItem implements MenuItem {
36    private final int mId;
37    private final int mGroup;
38    private final int mCategoryOrder;
39    private final int mOrdering;
40
41    private CharSequence mTitle;
42    private CharSequence mTitleCondensed;
43    private Intent mIntent;
44    private char mShortcutNumericChar;
45    private int mShortcutNumericModifiers = KeyEvent.META_CTRL_ON;
46    private char mShortcutAlphabeticChar;
47    private int mShortcutAlphabeticModifiers = KeyEvent.META_CTRL_ON;
48
49    private Drawable mIconDrawable;
50    private int mIconResId = NO_ICON;
51    private ColorStateList mIconTintList = null;
52    private PorterDuff.Mode mIconTintMode = null;
53    private boolean mHasIconTint = false;
54    private boolean mHasIconTintMode = false;
55
56    private Context mContext;
57
58    private MenuItem.OnMenuItemClickListener mClickListener;
59
60    private CharSequence mContentDescription;
61    private CharSequence mTooltipText;
62
63    private static final int NO_ICON = 0;
64
65    private int mFlags = ENABLED;
66    private static final int CHECKABLE      = 0x00000001;
67    private static final int CHECKED        = 0x00000002;
68    private static final int EXCLUSIVE      = 0x00000004;
69    private static final int HIDDEN         = 0x00000008;
70    private static final int ENABLED        = 0x00000010;
71
72    public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering,
73            CharSequence title) {
74        mContext = context;
75        mId = id;
76        mGroup = group;
77        mCategoryOrder = categoryOrder;
78        mOrdering = ordering;
79        mTitle = title;
80    }
81
82    public char getAlphabeticShortcut() {
83        return mShortcutAlphabeticChar;
84    }
85
86    public int getAlphabeticModifiers() {
87        return mShortcutAlphabeticModifiers;
88    }
89
90    public int getGroupId() {
91        return mGroup;
92    }
93
94    public Drawable getIcon() {
95        return mIconDrawable;
96    }
97
98    public Intent getIntent() {
99        return mIntent;
100    }
101
102    public int getItemId() {
103        return mId;
104    }
105
106    public ContextMenuInfo getMenuInfo() {
107        return null;
108    }
109
110    public char getNumericShortcut() {
111        return mShortcutNumericChar;
112    }
113
114    public int getNumericModifiers() {
115        return mShortcutNumericModifiers;
116    }
117
118    public int getOrder() {
119        return mOrdering;
120    }
121
122    public SubMenu getSubMenu() {
123        return null;
124    }
125
126    public CharSequence getTitle() {
127        return mTitle;
128    }
129
130    public CharSequence getTitleCondensed() {
131        return mTitleCondensed != null ? mTitleCondensed : mTitle;
132    }
133
134    public boolean hasSubMenu() {
135        return false;
136    }
137
138    public boolean isCheckable() {
139        return (mFlags & CHECKABLE) != 0;
140    }
141
142    public boolean isChecked() {
143        return (mFlags & CHECKED) != 0;
144    }
145
146    public boolean isEnabled() {
147        return (mFlags & ENABLED) != 0;
148    }
149
150    public boolean isVisible() {
151        return (mFlags & HIDDEN) == 0;
152    }
153
154    public MenuItem setAlphabeticShortcut(char alphaChar) {
155        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
156        return this;
157    }
158
159    public MenuItem setAlphabeticShortcut(char alphachar, int alphaModifiers) {
160        mShortcutAlphabeticChar = Character.toLowerCase(alphachar);
161        mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers);
162        return this;
163    }
164
165    public MenuItem setCheckable(boolean checkable) {
166        mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
167        return this;
168    }
169
170    public ActionMenuItem setExclusiveCheckable(boolean exclusive) {
171        mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
172        return this;
173    }
174
175    public MenuItem setChecked(boolean checked) {
176        mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
177        return this;
178    }
179
180    public MenuItem setEnabled(boolean enabled) {
181        mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0);
182        return this;
183    }
184
185    public MenuItem setIcon(Drawable icon) {
186        mIconDrawable = icon;
187        mIconResId = NO_ICON;
188
189        applyIconTint();
190        return this;
191    }
192
193    public MenuItem setIcon(int iconRes) {
194        mIconResId = iconRes;
195        mIconDrawable = mContext.getDrawable(iconRes);
196
197        applyIconTint();
198        return this;
199    }
200
201    @Override
202    public MenuItem setIconTintList(@Nullable ColorStateList iconTintList) {
203        mIconTintList = iconTintList;
204        mHasIconTint = true;
205
206        applyIconTint();
207
208        return this;
209    }
210
211    @Nullable
212    @Override
213    public ColorStateList getIconTintList() {
214        return mIconTintList;
215    }
216
217    @Override
218    public MenuItem setIconTintMode(PorterDuff.Mode iconTintMode) {
219        mIconTintMode = iconTintMode;
220        mHasIconTintMode = true;
221
222        applyIconTint();
223
224        return this;
225    }
226
227    @Nullable
228    @Override
229    public PorterDuff.Mode getIconTintMode() {
230        return mIconTintMode;
231    }
232
233    private void applyIconTint() {
234        if (mIconDrawable != null && (mHasIconTint || mHasIconTintMode)) {
235            mIconDrawable = mIconDrawable.mutate();
236
237            if (mHasIconTint) {
238                mIconDrawable.setTintList(mIconTintList);
239            }
240
241            if (mHasIconTintMode) {
242                mIconDrawable.setTintMode(mIconTintMode);
243            }
244        }
245    }
246
247    public MenuItem setIntent(Intent intent) {
248        mIntent = intent;
249        return this;
250    }
251
252    public MenuItem setNumericShortcut(char numericChar) {
253        mShortcutNumericChar = numericChar;
254        return this;
255    }
256
257    public MenuItem setNumericShortcut(char numericChar, int numericModifiers) {
258        mShortcutNumericChar = numericChar;
259        mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers);
260        return this;
261    }
262
263    public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
264        mClickListener = menuItemClickListener;
265        return this;
266    }
267
268    public MenuItem setShortcut(char numericChar, char alphaChar) {
269        mShortcutNumericChar = numericChar;
270        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
271        return this;
272    }
273
274    public MenuItem setShortcut(char numericChar, char alphaChar, int numericModifiers,
275            int alphaModifiers) {
276        mShortcutNumericChar = numericChar;
277        mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers);
278        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
279        mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers);
280        return this;
281    }
282
283    public MenuItem setTitle(CharSequence title) {
284        mTitle = title;
285        return this;
286    }
287
288    public MenuItem setTitle(int title) {
289        mTitle = mContext.getResources().getString(title);
290        return this;
291    }
292
293    public MenuItem setTitleCondensed(CharSequence title) {
294        mTitleCondensed = title;
295        return this;
296    }
297
298    public MenuItem setVisible(boolean visible) {
299        mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN);
300        return this;
301    }
302
303    public boolean invoke() {
304        if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
305            return true;
306        }
307
308        if (mIntent != null) {
309            mContext.startActivity(mIntent);
310            return true;
311        }
312
313        return false;
314    }
315
316    public void setShowAsAction(int show) {
317        // Do nothing. ActionMenuItems always show as action buttons.
318    }
319
320    public MenuItem setActionView(View actionView) {
321        throw new UnsupportedOperationException();
322    }
323
324    public View getActionView() {
325        return null;
326    }
327
328    @Override
329    public MenuItem setActionView(int resId) {
330        throw new UnsupportedOperationException();
331    }
332
333    @Override
334    public ActionProvider getActionProvider() {
335        return null;
336    }
337
338    @Override
339    public MenuItem setActionProvider(ActionProvider actionProvider) {
340        throw new UnsupportedOperationException();
341    }
342
343    @Override
344    public MenuItem setShowAsActionFlags(int actionEnum) {
345        setShowAsAction(actionEnum);
346        return this;
347    }
348
349    @Override
350    public boolean expandActionView() {
351        return false;
352    }
353
354    @Override
355    public boolean collapseActionView() {
356        return false;
357    }
358
359    @Override
360    public boolean isActionViewExpanded() {
361        return false;
362    }
363
364    @Override
365    public MenuItem setOnActionExpandListener(OnActionExpandListener listener) {
366        // No need to save the listener; ActionMenuItem does not support collapsing items.
367        return this;
368    }
369
370    @Override
371    public MenuItem setContentDescription(CharSequence contentDescription) {
372        mContentDescription = contentDescription;
373        return this;
374    }
375
376    @Override
377    public CharSequence getContentDescription() {
378        return mContentDescription;
379    }
380
381    @Override
382    public MenuItem setTooltipText(CharSequence tooltipText) {
383        mTooltipText = tooltipText;
384        return this;
385    }
386
387    @Override
388    public CharSequence getTooltipText() {
389        return mTooltipText;
390    }
391}
392