public void initForMode(final ActionMode mode) {
    if (mClose == null) {
      LayoutInflater inflater = LayoutInflater.from(getContext());
      mClose = inflater.inflate(mCloseItemLayout, this, false);
      addView(mClose);
    } else if (mClose.getParent() == null) {
      addView(mClose);
    }

    View closeButton = mClose.findViewById(R.id.action_mode_close_button);
    closeButton.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            mode.finish();
          }
        });

    final MenuBuilder menu = (MenuBuilder) mode.getMenu();
    if (mActionMenuPresenter != null) {
      mActionMenuPresenter.dismissPopupMenus();
    }
    mActionMenuPresenter = new ActionMenuPresenter(getContext());
    mActionMenuPresenter.setReserveOverflow(true);

    final LayoutParams layoutParams =
        new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    menu.addMenuPresenter(mActionMenuPresenter, mPopupContext);
    mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
    mMenuView.setBackgroundDrawable(null);
    addView(mMenuView, layoutParams);
  }
  public MenuPopupHelper(
      Context context,
      MenuBuilder menu,
      View anchorView,
      boolean overflowOnly,
      int popupStyleAttr,
      int popupStyleRes,
      int direction) {
    mDirection = direction;
    mContext = context;
    mInflater = LayoutInflater.from(context);
    mMenu = menu;
    mAdapter = new MenuAdapter(mMenu);
    mOverflowOnly = overflowOnly;
    mPopupStyleAttr = popupStyleAttr;
    mPopupStyleRes = popupStyleRes;

    final Resources res = context.getResources();
    mPopupMaxWidth =
        Math.max(
            res.getDisplayMetrics().widthPixels / 2,
            res.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));

    mAnchorView = anchorView;

    // Present the menu using our context, not the menu builder's context.
    menu.addMenuPresenter(this, context);
  }
  /**
   * Returns the Menu object that this ActionMenuView is currently presenting.
   *
   * <p>Applications should use this method to obtain the ActionMenuView's Menu object and inflate
   * or add content to it as necessary.
   *
   * @return the Menu presented by this view
   */
  public Menu getMenu() {
    if (mMenu == null) {
      final Context context = getContext();
      mMenu = new MenuBuilder(context);
      mMenu.setCallback(new MenuBuilderCallback());
      mPresenter = new ActionMenuPresenter(context);
      mPresenter.setReserveOverflow(true);
      mPresenter.setCallback(
          mActionMenuPresenterCallback != null
              ? mActionMenuPresenterCallback
              : new ActionMenuPresenterCallback());
      mMenu.addMenuPresenter(mPresenter, mPopupContext);
      mPresenter.setMenuView(this);
    }

    return mMenu;
  }