/**
   * 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;
  }
Beispiel #2
0
 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);
 }
Beispiel #9
0
 @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;
 }
Beispiel #10
0
 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);
 }
Beispiel #11
0
  public MenuItem setCheckable(boolean checkable) {
    final int oldFlags = mFlags;
    mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
    if (oldFlags != mFlags) {
      mMenu.onItemsChanged(false);
    }

    return this;
  }
Beispiel #12
0
  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;
  }
Beispiel #13
0
 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;
 }
Beispiel #14
0
  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;
  }
Beispiel #15
0
 char getShortcut()
 {
     if (mMenu.isQwertyMode())
     {
         return mShortcutAlphabeticChar;
     } else
     {
         return mShortcutNumericChar;
     }
 }
Beispiel #16
0
  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;
  }
Beispiel #17
0
  public MenuItem setNumericShortcut(char numericChar) {
    if (mShortcutNumericChar == numericChar) {
      return this;
    }

    mShortcutNumericChar = numericChar;

    mMenu.onItemsChanged(false);

    return this;
  }
Beispiel #18
0
  public Drawable getIcon() {
    if (mIconDrawable != null) {
      return mIconDrawable;
    }

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

    return null;
  }
Beispiel #19
0
  public MenuItem setTitle(CharSequence title) {
    mTitle = title;

    mMenu.onItemsChanged(false);

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

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

    mMenu.onItemsChanged(false);

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

    mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);

    mMenu.onItemsChanged(false);

    return this;
  }
Beispiel #23
0
  @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;
  }
Beispiel #24
0
  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();
    }
  }
Beispiel #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);
        }
      }
    }
Beispiel #27
0
 public boolean collapseActionView()
 {
     if ((mShowAsAction & 8) != 0)
     {
         if (mActionView == null)
         {
             return true;
         }
         if (mOnActionExpandListener == null || mOnActionExpandListener.onMenuItemActionCollapse(this))
         {
             return mMenu.collapseItemActionView(this);
         }
     }
     return false;
 }
Beispiel #28
0
  @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;
  }
Beispiel #29
0
  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);
  }
Beispiel #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();
    }
  }