/**
   * Invokes the item by calling various listeners or callbacks.
   *
   * @return true if the invocation was handled, false otherwise
   */
  public boolean invoke() {
    if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
      return true;
    }

    if (mMenu.dispatchMenuItemSelected(mMenu.getRootMenu(), this)) {
      return true;
    }

    if (mItemCallback != null) {
      mItemCallback.run();
      return true;
    }

    if (mIntent != null) {
      try {
        mMenu.getContext().startActivity(mIntent);
        return true;
      } catch (ActivityNotFoundException e) {
        Log.e(TAG, "Can't find activity to handle intent; ignoring", e);
      }
    }

    if (mActionProvider != null && mActionProvider.onPerformDefaultAction()) {
      return true;
    }

    return false;
  }
 private void configPresenters(MenuBuilder builder) {
   if (builder != null) {
     builder.addMenuPresenter(mActionMenuPresenter);
     builder.addMenuPresenter(mExpandedMenuPresenter);
   } else {
     mActionMenuPresenter.initForMenu(mContext, null);
     mExpandedMenuPresenter.initForMenu(mContext, null);
     mActionMenuPresenter.updateMenuView(true);
     mExpandedMenuPresenter.updateMenuView(true);
   }
 }
  public MenuItem setIcon(Drawable icon) {
    mIconResId = NO_ICON;
    mIconDrawable = icon;
    mMenu.onItemsChanged(false);

    return this;
  }
  @Override
  public boolean dispatchPrepareOptionsMenu(android.view.Menu menu) {
    if (DEBUG) Log.d(TAG, "[dispatchPrepareOptionsMenu] android.view.Menu: " + menu);

    if (mActionMode != null) {
      return false;
    }

    mMenuIsPrepared = false;
    if (!preparePanel()) {
      return false;
    }

    if (isReservingOverflow()) {
      return false;
    }

    if (mNativeItemMap == null) {
      mNativeItemMap = new HashMap<android.view.MenuItem, MenuItemImpl>();
    } else {
      mNativeItemMap.clear();
    }

    if (mMenu == null) {
      return false;
    }

    boolean result = mMenu.bindNativeOverflow(menu, this, mNativeItemMap);
    if (DEBUG) Log.d(TAG, "[dispatchPrepareOptionsMenu] returning " + result);
    return result;
  }
 void setCheckedInt(boolean checked) {
   final int oldFlags = mFlags;
   mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
   if (oldFlags != mFlags) {
     mMenu.onItemsChanged(false);
   }
 }
 public boolean expandActionView()
 {
     while ((mShowAsAction & 8) == 0 || mActionView == null || mOnActionExpandListener != null && !mOnActionExpandListener.onMenuItemActionExpand(this)) 
     {
         return false;
     }
     return mMenu.expandItemActionView(this);
 }
  public MenuItem setShortcut(char numericChar, char alphaChar) {
    mShortcutNumericChar = numericChar;
    mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);

    mMenu.onItemsChanged(false);

    return this;
  }
 @Override
 public void dispatchSaveInstanceState(Bundle outState) {
   if (mMenu != null) {
     mMenuFrozenActionViewState = new Bundle();
     mMenu.saveActionViewStates(mMenuFrozenActionViewState);
   }
   outState.putParcelable(PANELS_TAG, mMenuFrozenActionViewState);
 }
 @Override
 public void initForMenu(Context context, MenuBuilder menu) {
   // Clear the expanded action view when menus change.
   if (mMenu != null && mCurrentExpandedItem != null) {
     mMenu.collapseItemActionView(mCurrentExpandedItem);
   }
   mMenu = menu;
 }
 public PopupMenu(Context context, View anchor) {
   mContext = context;
   mMenu = new MenuBuilder(context);
   mMenu.setCallback(this);
   mAnchor = anchor;
   mPopup = new PopupMenuHelper(context, mMenu, mAnchor);
   mPopup.setCallback(this);
 }
  public MenuItem setCheckable(boolean checkable) {
    final int oldFlags = mFlags;
    mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
    if (oldFlags != mFlags) {
      mMenu.onItemsChanged(false);
    }

    return this;
  }
  public MenuItem setIcon(int iconResId) {
    mIconDrawable = null;
    mIconResId = iconResId;

    // If we have a view, we need to push the Drawable to them
    mMenu.onItemsChanged(false);

    return this;
  }
 public MenuItem setActionView(View view) {
   mActionView = view;
   mActionProvider = null;
   if (view != null && view.getId() == View.NO_ID && mId > 0) {
     view.setId(mId);
   }
   mMenu.onItemActionRequestChanged(this);
   return this;
 }
  public MenuItem setVisible(boolean shown) {
    // Try to set the shown state to the given state. If the shown state was changed
    // (i.e. the previous state isn't the same as given state), notify the parent menu that
    // the shown state has changed for this item
    if (setVisibleInt(shown)) {
      mMenu.onItemVisibleChanged(this);
    }

    return this;
  }
 char getShortcut()
 {
     if (mMenu.isQwertyMode())
     {
         return mShortcutAlphabeticChar;
     } else
     {
         return mShortcutNumericChar;
     }
 }
  public MenuItem setChecked(boolean checked) {
    if ((mFlags & EXCLUSIVE) != 0) {
      // Call the method on the Menu since it knows about the others in this
      // exclusive checkable group
      mMenu.setExclusiveItemChecked(this);
    } else {
      setCheckedInt(checked);
    }

    return this;
  }
  public MenuItem setNumericShortcut(char numericChar) {
    if (mShortcutNumericChar == numericChar) {
      return this;
    }

    mShortcutNumericChar = numericChar;

    mMenu.onItemsChanged(false);

    return this;
  }
  public Drawable getIcon() {
    if (mIconDrawable != null) {
      return mIconDrawable;
    }

    if (mIconResId != NO_ICON) {
      return mMenu.getResources().getDrawable(mIconResId);
    }

    return null;
  }
  public MenuItem setTitle(CharSequence title) {
    mTitle = title;

    mMenu.onItemsChanged(false);

    if (mSubMenu != null) {
      mSubMenu.setHeaderTitle(title);
    }

    return this;
  }
  public MenuItem setEnabled(boolean enabled) {
    if (enabled) {
      mFlags |= ENABLED;
    } else {
      mFlags &= ~ENABLED;
    }

    mMenu.onItemsChanged(false);

    return this;
  }
 @Override
 public boolean onOpenSubMenu(MenuBuilder subMenu) {
   if (subMenu == null) {
     return false;
   }
   if (!subMenu.hasVisibleItems()) {
     return true;
   }
   new MenuPopupHelper(mContext, subMenu, mAnchor).show();
   return true;
 }
  public MenuItem setAlphabeticShortcut(char alphaChar) {
    if (mShortcutAlphabeticChar == alphaChar) {
      return this;
    }

    mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);

    mMenu.onItemsChanged(false);

    return this;
  }
  @Override
  public boolean expandActionView() {
    if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0 || mActionView == null) {
      return false;
    }

    if (mOnActionExpandListener == null || mOnActionExpandListener.onMenuItemActionExpand(this)) {
      return mMenu.expandItemActionView(this);
    }

    return false;
  }
  public MenuItem setTitleCondensed(CharSequence title) {
    mTitleCondensed = title;

    // Could use getTitle() in the loop below, but just cache what it would do here
    if (title == null) {
      title = mTitle;
    }

    mMenu.onItemsChanged(false);

    return this;
  }
  @Override
  public void dispatchInvalidateOptionsMenu() {
    if (DEBUG) Log.d(TAG, "[dispatchInvalidateOptionsMenu]");

    Bundle savedActionViewStates = null;
    if (mMenu != null) {
      savedActionViewStates = new Bundle();
      mMenu.saveActionViewStates(savedActionViewStates);
      if (savedActionViewStates.size() > 0) {
        mMenuFrozenActionViewState = savedActionViewStates;
      }
      // This will be started again when the panel is prepared.
      mMenu.stopDispatchingItemsChanged();
      mMenu.clear();
    }
    mMenuRefreshContent = true;

    // Prepare the options panel if we have an action bar
    if (wActionBar != null) {
      mMenuIsPrepared = false;
      preparePanel();
    }
  }
Exemple #26
0
    @Override
    public void updateMenuView(boolean cleared) {
      // Make sure the expanded item we have is still there.
      if (mCurrentExpandedItem != null) {
        boolean found = false;

        if (mMenu != null) {
          final int count = mMenu.size();
          for (int i = 0; i < count; i++) {
            final MenuItem item = mMenu.getItem(i);
            if (item == mCurrentExpandedItem) {
              found = true;
              break;
            }
          }
        }

        if (!found) {
          // The item we had expanded disappeared. Collapse.
          collapseItemActionView(mMenu, mCurrentExpandedItem);
        }
      }
    }
 public boolean collapseActionView()
 {
     if ((mShowAsAction & 8) != 0)
     {
         if (mActionView == null)
         {
             return true;
         }
         if (mOnActionExpandListener == null || mOnActionExpandListener.onMenuItemActionCollapse(this))
         {
             return mMenu.collapseItemActionView(this);
         }
     }
     return false;
 }
  @Override
  public boolean collapseActionView() {
    if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0) {
      return false;
    }
    if (mActionView == null) {
      // We're already collapsed if we have no action view.
      return true;
    }

    if (mOnActionExpandListener == null || mOnActionExpandListener.onMenuItemActionCollapse(this)) {
      return mMenu.collapseItemActionView(this);
    }

    return false;
  }
  public void setShowAsAction(int actionEnum) {
    switch (actionEnum & SHOW_AS_ACTION_MASK) {
      case SHOW_AS_ACTION_ALWAYS:
      case SHOW_AS_ACTION_IF_ROOM:
      case SHOW_AS_ACTION_NEVER:
        // Looks good!
        break;

      default:
        // Mutually exclusive options selected!
        throw new IllegalArgumentException(
            "SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM,"
                + " and SHOW_AS_ACTION_NEVER are mutually exclusive.");
    }
    mShowAsAction = actionEnum;
    mMenu.onItemActionRequestChanged(this);
  }
Exemple #30
0
  @Override
  public void onRestoreInstanceState(Parcelable p) {
    SavedState state = (SavedState) p;

    super.onRestoreInstanceState(state.getSuperState());

    if (state.expandedMenuItemId != 0 && mExpandedMenuPresenter != null && mOptionsMenu != null) {
      final MenuItem item = mOptionsMenu.findItem(state.expandedMenuItemId);
      if (item != null) {
        item.expandActionView();
      }
    }

    if (state.isOverflowOpen) {
      postShowOverflowMenu();
    }
  }