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