• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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.example.android.actionbarcompat;
18 
19 import android.content.Intent;
20 import android.graphics.drawable.Drawable;
21 import android.view.ActionProvider;
22 import android.view.ContextMenu;
23 import android.view.MenuItem;
24 import android.view.SubMenu;
25 import android.view.View;
26 
27 /**
28  * A <em>really</em> dumb implementation of the {@link android.view.MenuItem} interface, that's only
29  * useful for our actionbar-compat purposes. See
30  * <code>com.android.internal.view.menu.MenuItemImpl</code> in AOSP for a more complete
31  * implementation.
32  */
33 public class SimpleMenuItem implements MenuItem {
34 
35     private SimpleMenu mMenu;
36 
37     private final int mId;
38     private final int mOrder;
39     private CharSequence mTitle;
40     private CharSequence mTitleCondensed;
41     private Drawable mIconDrawable;
42     private int mIconResId = 0;
43     private boolean mEnabled = true;
44 
SimpleMenuItem(SimpleMenu menu, int id, int order, CharSequence title)45     public SimpleMenuItem(SimpleMenu menu, int id, int order, CharSequence title) {
46         mMenu = menu;
47         mId = id;
48         mOrder = order;
49         mTitle = title;
50     }
51 
getItemId()52     public int getItemId() {
53         return mId;
54     }
55 
getOrder()56     public int getOrder() {
57         return mOrder;
58     }
59 
setTitle(CharSequence title)60     public MenuItem setTitle(CharSequence title) {
61         mTitle = title;
62         return this;
63     }
64 
setTitle(int titleRes)65     public MenuItem setTitle(int titleRes) {
66         return setTitle(mMenu.getContext().getString(titleRes));
67     }
68 
getTitle()69     public CharSequence getTitle() {
70         return mTitle;
71     }
72 
setTitleCondensed(CharSequence title)73     public MenuItem setTitleCondensed(CharSequence title) {
74         mTitleCondensed = title;
75         return this;
76     }
77 
getTitleCondensed()78     public CharSequence getTitleCondensed() {
79         return mTitleCondensed != null ? mTitleCondensed : mTitle;
80     }
81 
setIcon(Drawable icon)82     public MenuItem setIcon(Drawable icon) {
83         mIconResId = 0;
84         mIconDrawable = icon;
85         return this;
86     }
87 
setIcon(int iconResId)88     public MenuItem setIcon(int iconResId) {
89         mIconDrawable = null;
90         mIconResId = iconResId;
91         return this;
92     }
93 
getIcon()94     public Drawable getIcon() {
95         if (mIconDrawable != null) {
96             return mIconDrawable;
97         }
98 
99         if (mIconResId != 0) {
100             return mMenu.getResources().getDrawable(mIconResId);
101         }
102 
103         return null;
104     }
105 
setEnabled(boolean enabled)106     public MenuItem setEnabled(boolean enabled) {
107         mEnabled = enabled;
108         return this;
109     }
110 
isEnabled()111     public boolean isEnabled() {
112         return mEnabled;
113     }
114 
115     // No-op operations. We use no-ops to allow inflation from menu XML.
116 
getGroupId()117     public int getGroupId() {
118         // Noop
119         return 0;
120     }
121 
getActionView()122     public View getActionView() {
123         // Noop
124         return null;
125     }
126 
setActionProvider(ActionProvider actionProvider)127     public MenuItem setActionProvider(ActionProvider actionProvider) {
128         // Noop
129         return this;
130     }
131 
getActionProvider()132     public ActionProvider getActionProvider() {
133         // Noop
134         return null;
135     }
136 
expandActionView()137     public boolean expandActionView() {
138         // Noop
139         return false;
140     }
141 
collapseActionView()142     public boolean collapseActionView() {
143         // Noop
144         return false;
145     }
146 
isActionViewExpanded()147     public boolean isActionViewExpanded() {
148         // Noop
149         return false;
150     }
151 
152     @Override
setOnActionExpandListener(OnActionExpandListener onActionExpandListener)153     public MenuItem setOnActionExpandListener(OnActionExpandListener onActionExpandListener) {
154         // Noop
155         return this;
156     }
157 
setIntent(Intent intent)158     public MenuItem setIntent(Intent intent) {
159         // Noop
160         return this;
161     }
162 
getIntent()163     public Intent getIntent() {
164         // Noop
165         return null;
166     }
167 
setShortcut(char c, char c1)168     public MenuItem setShortcut(char c, char c1) {
169         // Noop
170         return this;
171     }
172 
setNumericShortcut(char c)173     public MenuItem setNumericShortcut(char c) {
174         // Noop
175         return this;
176     }
177 
getNumericShortcut()178     public char getNumericShortcut() {
179         // Noop
180         return 0;
181     }
182 
setAlphabeticShortcut(char c)183     public MenuItem setAlphabeticShortcut(char c) {
184         // Noop
185         return this;
186     }
187 
getAlphabeticShortcut()188     public char getAlphabeticShortcut() {
189         // Noop
190         return 0;
191     }
192 
setCheckable(boolean b)193     public MenuItem setCheckable(boolean b) {
194         // Noop
195         return this;
196     }
197 
isCheckable()198     public boolean isCheckable() {
199         // Noop
200         return false;
201     }
202 
setChecked(boolean b)203     public MenuItem setChecked(boolean b) {
204         // Noop
205         return this;
206     }
207 
isChecked()208     public boolean isChecked() {
209         // Noop
210         return false;
211     }
212 
setVisible(boolean b)213     public MenuItem setVisible(boolean b) {
214         // Noop
215         return this;
216     }
217 
isVisible()218     public boolean isVisible() {
219         // Noop
220         return true;
221     }
222 
hasSubMenu()223     public boolean hasSubMenu() {
224         // Noop
225         return false;
226     }
227 
getSubMenu()228     public SubMenu getSubMenu() {
229         // Noop
230         return null;
231     }
232 
setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener)233     public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener) {
234         // Noop
235         return this;
236     }
237 
getMenuInfo()238     public ContextMenu.ContextMenuInfo getMenuInfo() {
239         // Noop
240         return null;
241     }
242 
setShowAsAction(int i)243     public void setShowAsAction(int i) {
244         // Noop
245     }
246 
setShowAsActionFlags(int i)247     public MenuItem setShowAsActionFlags(int i) {
248         // Noop
249         return null;
250     }
251 
setActionView(View view)252     public MenuItem setActionView(View view) {
253         // Noop
254         return this;
255     }
256 
setActionView(int i)257     public MenuItem setActionView(int i) {
258         // Noop
259         return this;
260     }
261 }
262