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 17package com.example.android.actionbarcompat; 18 19import android.content.Intent; 20import android.graphics.drawable.Drawable; 21import android.view.ActionProvider; 22import android.view.ContextMenu; 23import android.view.MenuItem; 24import android.view.SubMenu; 25import 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 */ 33public 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 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 52 public int getItemId() { 53 return mId; 54 } 55 56 public int getOrder() { 57 return mOrder; 58 } 59 60 public MenuItem setTitle(CharSequence title) { 61 mTitle = title; 62 return this; 63 } 64 65 public MenuItem setTitle(int titleRes) { 66 return setTitle(mMenu.getContext().getString(titleRes)); 67 } 68 69 public CharSequence getTitle() { 70 return mTitle; 71 } 72 73 public MenuItem setTitleCondensed(CharSequence title) { 74 mTitleCondensed = title; 75 return this; 76 } 77 78 public CharSequence getTitleCondensed() { 79 return mTitleCondensed != null ? mTitleCondensed : mTitle; 80 } 81 82 public MenuItem setIcon(Drawable icon) { 83 mIconResId = 0; 84 mIconDrawable = icon; 85 return this; 86 } 87 88 public MenuItem setIcon(int iconResId) { 89 mIconDrawable = null; 90 mIconResId = iconResId; 91 return this; 92 } 93 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 106 public MenuItem setEnabled(boolean enabled) { 107 mEnabled = enabled; 108 return this; 109 } 110 111 public boolean isEnabled() { 112 return mEnabled; 113 } 114 115 // No-op operations. We use no-ops to allow inflation from menu XML. 116 117 public int getGroupId() { 118 // Noop 119 return 0; 120 } 121 122 public View getActionView() { 123 // Noop 124 return null; 125 } 126 127 public MenuItem setActionProvider(ActionProvider actionProvider) { 128 // Noop 129 return this; 130 } 131 132 public ActionProvider getActionProvider() { 133 // Noop 134 return null; 135 } 136 137 public boolean expandActionView() { 138 // Noop 139 return false; 140 } 141 142 public boolean collapseActionView() { 143 // Noop 144 return false; 145 } 146 147 public boolean isActionViewExpanded() { 148 // Noop 149 return false; 150 } 151 152 @Override 153 public MenuItem setOnActionExpandListener(OnActionExpandListener onActionExpandListener) { 154 // Noop 155 return this; 156 } 157 158 public MenuItem setIntent(Intent intent) { 159 // Noop 160 return this; 161 } 162 163 public Intent getIntent() { 164 // Noop 165 return null; 166 } 167 168 public MenuItem setShortcut(char c, char c1) { 169 // Noop 170 return this; 171 } 172 173 public MenuItem setNumericShortcut(char c) { 174 // Noop 175 return this; 176 } 177 178 public char getNumericShortcut() { 179 // Noop 180 return 0; 181 } 182 183 public MenuItem setAlphabeticShortcut(char c) { 184 // Noop 185 return this; 186 } 187 188 public char getAlphabeticShortcut() { 189 // Noop 190 return 0; 191 } 192 193 public MenuItem setCheckable(boolean b) { 194 // Noop 195 return this; 196 } 197 198 public boolean isCheckable() { 199 // Noop 200 return false; 201 } 202 203 public MenuItem setChecked(boolean b) { 204 // Noop 205 return this; 206 } 207 208 public boolean isChecked() { 209 // Noop 210 return false; 211 } 212 213 public MenuItem setVisible(boolean b) { 214 // Noop 215 return this; 216 } 217 218 public boolean isVisible() { 219 // Noop 220 return true; 221 } 222 223 public boolean hasSubMenu() { 224 // Noop 225 return false; 226 } 227 228 public SubMenu getSubMenu() { 229 // Noop 230 return null; 231 } 232 233 public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener) { 234 // Noop 235 return this; 236 } 237 238 public ContextMenu.ContextMenuInfo getMenuInfo() { 239 // Noop 240 return null; 241 } 242 243 public void setShowAsAction(int i) { 244 // Noop 245 } 246 247 public MenuItem setShowAsActionFlags(int i) { 248 // Noop 249 return null; 250 } 251 252 public MenuItem setActionView(View view) { 253 // Noop 254 return this; 255 } 256 257 public MenuItem setActionView(int i) { 258 // Noop 259 return this; 260 } 261} 262