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