mobile/android/base/menu/GeckoMenuItem.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.menu;
michael@0 6
michael@0 7 import org.mozilla.gecko.R;
michael@0 8 import org.mozilla.gecko.widget.GeckoActionProvider;
michael@0 9
michael@0 10 import android.content.Intent;
michael@0 11 import android.graphics.drawable.Drawable;
michael@0 12 import android.os.Build;
michael@0 13 import android.view.ActionProvider;
michael@0 14 import android.view.ContextMenu;
michael@0 15 import android.view.MenuItem;
michael@0 16 import android.view.SubMenu;
michael@0 17 import android.view.View;
michael@0 18
michael@0 19 public class GeckoMenuItem implements MenuItem {
michael@0 20 private static final String LOGTAG = "GeckoMenuItem";
michael@0 21
michael@0 22 public static final int SHOW_AS_ACTION_NEVER = 0;
michael@0 23 public static final int SHOW_AS_ACTION_IF_ROOM = 1;
michael@0 24 public static final int SHOW_AS_ACTION_ALWAYS = 2;
michael@0 25
michael@0 26 // A View that can show a MenuItem should be able to initialize from
michael@0 27 // the properties of the MenuItem.
michael@0 28 public static interface Layout {
michael@0 29 public void initialize(GeckoMenuItem item);
michael@0 30 public void setShowIcon(boolean show);
michael@0 31 }
michael@0 32
michael@0 33 public static interface OnShowAsActionChangedListener {
michael@0 34 public boolean hasActionItemBar();
michael@0 35 public void onShowAsActionChanged(GeckoMenuItem item);
michael@0 36 }
michael@0 37
michael@0 38 private int mId;
michael@0 39 private int mOrder;
michael@0 40 private View mActionView;
michael@0 41 private int mActionEnum;
michael@0 42 private CharSequence mTitle;
michael@0 43 private CharSequence mTitleCondensed;
michael@0 44 private boolean mCheckable = false;
michael@0 45 private boolean mChecked = false;
michael@0 46 private boolean mVisible = true;
michael@0 47 private boolean mEnabled = true;
michael@0 48 private Drawable mIcon;
michael@0 49 private int mIconRes;
michael@0 50 private GeckoActionProvider mActionProvider;
michael@0 51 private GeckoMenu mMenu;
michael@0 52 private GeckoSubMenu mSubMenu;
michael@0 53 private MenuItem.OnMenuItemClickListener mMenuItemClickListener = null;
michael@0 54 private OnShowAsActionChangedListener mShowAsActionChangedListener;
michael@0 55
michael@0 56 public GeckoMenuItem(GeckoMenu menu, int id, int order, int titleRes) {
michael@0 57 mMenu = menu;
michael@0 58 mId = id;
michael@0 59 mOrder = order;
michael@0 60 setTitle(titleRes);
michael@0 61 }
michael@0 62
michael@0 63 public GeckoMenuItem(GeckoMenu menu, int id, int order, CharSequence title) {
michael@0 64 mMenu = menu;
michael@0 65 mId = id;
michael@0 66 mOrder = order;
michael@0 67 setTitle(title);
michael@0 68 }
michael@0 69
michael@0 70 @Override
michael@0 71 public boolean collapseActionView() {
michael@0 72 return false;
michael@0 73 }
michael@0 74
michael@0 75 @Override
michael@0 76 public boolean expandActionView() {
michael@0 77 return false;
michael@0 78 }
michael@0 79
michael@0 80 public boolean hasActionProvider() {
michael@0 81 if (Build.VERSION.SDK_INT < 14) {
michael@0 82 return false;
michael@0 83 }
michael@0 84
michael@0 85 return (mActionProvider != null);
michael@0 86 }
michael@0 87
michael@0 88 public int getActionEnum() {
michael@0 89 return mActionEnum;
michael@0 90 }
michael@0 91
michael@0 92 public GeckoActionProvider getGeckoActionProvider() {
michael@0 93 return mActionProvider;
michael@0 94 }
michael@0 95
michael@0 96 @Override
michael@0 97 public ActionProvider getActionProvider() {
michael@0 98 return null;
michael@0 99 }
michael@0 100
michael@0 101 @Override
michael@0 102 public View getActionView() {
michael@0 103 if (mActionProvider != null) {
michael@0 104 return mActionProvider.getView();
michael@0 105 }
michael@0 106
michael@0 107 return mActionView;
michael@0 108 }
michael@0 109
michael@0 110 @Override
michael@0 111 public char getAlphabeticShortcut() {
michael@0 112 return 0;
michael@0 113 }
michael@0 114
michael@0 115 @Override
michael@0 116 public int getGroupId() {
michael@0 117 return 0;
michael@0 118 }
michael@0 119
michael@0 120 @Override
michael@0 121 public Drawable getIcon() {
michael@0 122 if (mIcon == null) {
michael@0 123 if (mIconRes != 0)
michael@0 124 return mMenu.getResources().getDrawable(mIconRes);
michael@0 125 else
michael@0 126 return null;
michael@0 127 } else {
michael@0 128 return mIcon;
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 @Override
michael@0 133 public Intent getIntent() {
michael@0 134 return null;
michael@0 135 }
michael@0 136
michael@0 137 @Override
michael@0 138 public int getItemId() {
michael@0 139 return mId;
michael@0 140 }
michael@0 141
michael@0 142 @Override
michael@0 143 public ContextMenu.ContextMenuInfo getMenuInfo() {
michael@0 144 return null;
michael@0 145 }
michael@0 146
michael@0 147 @Override
michael@0 148 public char getNumericShortcut() {
michael@0 149 return 0;
michael@0 150 }
michael@0 151
michael@0 152 @Override
michael@0 153 public int getOrder() {
michael@0 154 return mOrder;
michael@0 155 }
michael@0 156
michael@0 157 @Override
michael@0 158 public SubMenu getSubMenu() {
michael@0 159 return mSubMenu;
michael@0 160 }
michael@0 161
michael@0 162 @Override
michael@0 163 public CharSequence getTitle() {
michael@0 164 return mTitle;
michael@0 165 }
michael@0 166
michael@0 167 @Override
michael@0 168 public CharSequence getTitleCondensed() {
michael@0 169 return mTitleCondensed;
michael@0 170 }
michael@0 171
michael@0 172 @Override
michael@0 173 public boolean hasSubMenu() {
michael@0 174 if (mActionProvider != null)
michael@0 175 return mActionProvider.hasSubMenu();
michael@0 176
michael@0 177 return (mSubMenu != null);
michael@0 178 }
michael@0 179
michael@0 180 public boolean isActionItem() {
michael@0 181 return (mActionEnum > 0);
michael@0 182 }
michael@0 183
michael@0 184 @Override
michael@0 185 public boolean isActionViewExpanded() {
michael@0 186 return false;
michael@0 187 }
michael@0 188
michael@0 189 @Override
michael@0 190 public boolean isCheckable() {
michael@0 191 return mCheckable;
michael@0 192 }
michael@0 193
michael@0 194 @Override
michael@0 195 public boolean isChecked() {
michael@0 196 return mChecked;
michael@0 197 }
michael@0 198
michael@0 199 @Override
michael@0 200 public boolean isEnabled() {
michael@0 201 return mEnabled;
michael@0 202 }
michael@0 203
michael@0 204 @Override
michael@0 205 public boolean isVisible() {
michael@0 206 return mVisible;
michael@0 207 }
michael@0 208
michael@0 209 @Override
michael@0 210 public MenuItem setActionProvider(ActionProvider actionProvider) {
michael@0 211 return this;
michael@0 212 }
michael@0 213
michael@0 214 public MenuItem setActionProvider(GeckoActionProvider actionProvider) {
michael@0 215 mActionProvider = actionProvider;
michael@0 216 if (mActionProvider != null) {
michael@0 217 actionProvider.setOnTargetSelectedListener(new GeckoActionProvider.OnTargetSelectedListener() {
michael@0 218 @Override
michael@0 219 public void onTargetSelected() {
michael@0 220 mMenu.close();
michael@0 221
michael@0 222 // Refresh the menu item to show the high frequency apps.
michael@0 223 mShowAsActionChangedListener.onShowAsActionChanged(GeckoMenuItem.this);
michael@0 224 }
michael@0 225 });
michael@0 226 }
michael@0 227
michael@0 228 mShowAsActionChangedListener.onShowAsActionChanged(this);
michael@0 229 return this;
michael@0 230 }
michael@0 231
michael@0 232 @Override
michael@0 233 public MenuItem setActionView(int resId) {
michael@0 234 return this;
michael@0 235 }
michael@0 236
michael@0 237 @Override
michael@0 238 public MenuItem setActionView(View view) {
michael@0 239 return this;
michael@0 240 }
michael@0 241
michael@0 242 @Override
michael@0 243 public MenuItem setAlphabeticShortcut(char alphaChar) {
michael@0 244 return this;
michael@0 245 }
michael@0 246
michael@0 247 @Override
michael@0 248 public MenuItem setCheckable(boolean checkable) {
michael@0 249 mCheckable = checkable;
michael@0 250 mMenu.onItemChanged(this);
michael@0 251 return this;
michael@0 252 }
michael@0 253
michael@0 254 @Override
michael@0 255 public MenuItem setChecked(boolean checked) {
michael@0 256 mChecked = checked;
michael@0 257 mMenu.onItemChanged(this);
michael@0 258 return this;
michael@0 259 }
michael@0 260
michael@0 261 @Override
michael@0 262 public MenuItem setEnabled(boolean enabled) {
michael@0 263 mEnabled = enabled;
michael@0 264 mMenu.onItemChanged(this);
michael@0 265 return this;
michael@0 266 }
michael@0 267
michael@0 268 @Override
michael@0 269 public MenuItem setIcon(Drawable icon) {
michael@0 270 mIcon = icon;
michael@0 271 mMenu.onItemChanged(this);
michael@0 272 return this;
michael@0 273 }
michael@0 274
michael@0 275 @Override
michael@0 276 public MenuItem setIcon(int iconRes) {
michael@0 277 mIconRes = iconRes;
michael@0 278 mMenu.onItemChanged(this);
michael@0 279 return this;
michael@0 280 }
michael@0 281
michael@0 282 @Override
michael@0 283 public MenuItem setIntent(Intent intent) {
michael@0 284 return this;
michael@0 285 }
michael@0 286
michael@0 287 @Override
michael@0 288 public MenuItem setNumericShortcut(char numericChar) {
michael@0 289 return this;
michael@0 290 }
michael@0 291
michael@0 292 @Override
michael@0 293 public MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener) {
michael@0 294 return this;
michael@0 295 }
michael@0 296
michael@0 297 @Override
michael@0 298 public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener) {
michael@0 299 mMenuItemClickListener = menuItemClickListener;
michael@0 300 return this;
michael@0 301 }
michael@0 302
michael@0 303 @Override
michael@0 304 public MenuItem setShortcut(char numericChar, char alphaChar) {
michael@0 305 return this;
michael@0 306 }
michael@0 307
michael@0 308 @Override
michael@0 309 public void setShowAsAction(int actionEnum) {
michael@0 310 setShowAsAction(actionEnum, 0);
michael@0 311 }
michael@0 312
michael@0 313 public void setShowAsAction(int actionEnum, int style) {
michael@0 314 if (mShowAsActionChangedListener == null)
michael@0 315 return;
michael@0 316
michael@0 317 if (mActionEnum == actionEnum)
michael@0 318 return;
michael@0 319
michael@0 320 if (actionEnum > 0) {
michael@0 321 if (!mShowAsActionChangedListener.hasActionItemBar())
michael@0 322 return;
michael@0 323
michael@0 324 if (!hasActionProvider()) {
michael@0 325 // Change the type to just an icon
michael@0 326 MenuItemActionBar actionView;
michael@0 327 if (style != 0) {
michael@0 328 actionView = new MenuItemActionBar(mMenu.getContext(), null, style);
michael@0 329 } else {
michael@0 330 if (actionEnum == SHOW_AS_ACTION_ALWAYS) {
michael@0 331 actionView = new MenuItemActionBar(mMenu.getContext());
michael@0 332 } else {
michael@0 333 actionView = new MenuItemActionBar(mMenu.getContext(), null, R.attr.menuItemSecondaryActionBarStyle);
michael@0 334 }
michael@0 335 }
michael@0 336
michael@0 337 actionView.initialize(this);
michael@0 338 mActionView = actionView;
michael@0 339 }
michael@0 340
michael@0 341 mActionEnum = actionEnum;
michael@0 342 }
michael@0 343
michael@0 344 mShowAsActionChangedListener.onShowAsActionChanged(this);
michael@0 345 }
michael@0 346
michael@0 347 @Override
michael@0 348 public MenuItem setShowAsActionFlags(int actionEnum) {
michael@0 349 return this;
michael@0 350 }
michael@0 351
michael@0 352 public MenuItem setSubMenu(GeckoSubMenu subMenu) {
michael@0 353 mSubMenu = subMenu;
michael@0 354 return this;
michael@0 355 }
michael@0 356
michael@0 357 @Override
michael@0 358 public MenuItem setTitle(CharSequence title) {
michael@0 359 mTitle = title;
michael@0 360 mMenu.onItemChanged(this);
michael@0 361 return this;
michael@0 362 }
michael@0 363
michael@0 364 @Override
michael@0 365 public MenuItem setTitle(int title) {
michael@0 366 mTitle = mMenu.getResources().getString(title);
michael@0 367 mMenu.onItemChanged(this);
michael@0 368 return this;
michael@0 369 }
michael@0 370
michael@0 371 @Override
michael@0 372 public MenuItem setTitleCondensed(CharSequence title) {
michael@0 373 mTitleCondensed = title;
michael@0 374 return this;
michael@0 375 }
michael@0 376
michael@0 377 @Override
michael@0 378 public MenuItem setVisible(boolean visible) {
michael@0 379 mVisible = visible;
michael@0 380 mMenu.onItemChanged(this);
michael@0 381 return this;
michael@0 382 }
michael@0 383
michael@0 384 public boolean invoke() {
michael@0 385 if (mMenuItemClickListener != null)
michael@0 386 return mMenuItemClickListener.onMenuItemClick(this);
michael@0 387 else
michael@0 388 return false;
michael@0 389 }
michael@0 390
michael@0 391 public void setOnShowAsActionChangedListener(OnShowAsActionChangedListener listener) {
michael@0 392 mShowAsActionChangedListener = listener;
michael@0 393 }
michael@0 394 }

mercurial