Search in sources :

Example 1 with OnDismissListener

use of android.widget.PopupWindow.OnDismissListener in project AndroidChromium by JackyAndroid.

the class AppMenu method show.

/**
     * Creates and shows the app menu anchored to the specified view.
     *
     * @param context The context of the AppMenu (ensure the proper theme is set on this context).
     * @param anchorView The anchor {@link View} of the {@link ListPopupWindow}.
     * @param isByPermanentButton Whether or not permanent hardware button triggered it. (oppose to
     *                            software button or keyboard).
     * @param screenRotation Current device screen rotation.
     * @param visibleDisplayFrame The display area rect in which AppMenu is supposed to fit in.
     * @param screenHeight Current device screen height.
     * @param footerResourceId The resource id for a view to add to the end of the menu list.
     *                         Can be 0 if no such view is required.
     */
void show(Context context, View anchorView, boolean isByPermanentButton, int screenRotation, Rect visibleDisplayFrame, int screenHeight, int footerResourceId) {
    mPopup = new ListPopupWindow(context, null, android.R.attr.popupMenuStyle);
    mPopup.setModal(true);
    mPopup.setAnchorView(anchorView);
    mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
    int footerHeight = 0;
    if (footerResourceId != 0) {
        mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
        View promptView = LayoutInflater.from(context).inflate(footerResourceId, null);
        mPopup.setPromptView(promptView);
        int measureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        promptView.measure(measureSpec, measureSpec);
        footerHeight = promptView.getMeasuredHeight();
    }
    mPopup.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss() {
            if (mPopup.getAnchorView() instanceof ImageButton) {
                ((ImageButton) mPopup.getAnchorView()).setSelected(false);
            }
            if (mMenuItemEnterAnimator != null)
                mMenuItemEnterAnimator.cancel();
            mHandler.appMenuDismissed();
            mHandler.onMenuVisibilityChanged(false);
        }
    });
    // Some OEMs don't actually let us change the background... but they still return the
    // padding of the new background, which breaks the menu height.  If we still have a
    // drawable here even though our style says @null we should use this padding instead...
    Drawable originalBgDrawable = mPopup.getBackground();
    // an incorrectly drawn background.
    if (isByPermanentButton) {
        mPopup.setBackgroundDrawable(ApiCompatibilityUtils.getDrawable(context.getResources(), R.drawable.menu_bg));
    } else {
        mPopup.setBackgroundDrawable(ApiCompatibilityUtils.getDrawable(context.getResources(), R.drawable.edge_menu_bg));
        mPopup.setAnimationStyle(R.style.OverflowMenuAnim);
    }
    // animations.
    if (SysUtils.isLowEndDevice() || Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mPopup.setAnimationStyle(0);
    }
    Rect bgPadding = new Rect();
    mPopup.getBackground().getPadding(bgPadding);
    int popupWidth = context.getResources().getDimensionPixelSize(R.dimen.menu_width) + bgPadding.left + bgPadding.right;
    mPopup.setWidth(popupWidth);
    mCurrentScreenRotation = screenRotation;
    mIsByPermanentButton = isByPermanentButton;
    // Extract visible items from the Menu.
    int numItems = mMenu.size();
    List<MenuItem> menuItems = new ArrayList<MenuItem>();
    for (int i = 0; i < numItems; ++i) {
        MenuItem item = mMenu.getItem(i);
        if (item.isVisible()) {
            menuItems.add(item);
        }
    }
    Rect sizingPadding = new Rect(bgPadding);
    if (isByPermanentButton && originalBgDrawable != null) {
        Rect originalPadding = new Rect();
        originalBgDrawable.getPadding(originalPadding);
        sizingPadding.top = originalPadding.top;
        sizingPadding.bottom = originalPadding.bottom;
    }
    // A List adapter for visible items in the Menu. The first row is added as a header to the
    // list view.
    mAdapter = new AppMenuAdapter(this, menuItems, LayoutInflater.from(context));
    mPopup.setAdapter(mAdapter);
    setMenuHeight(menuItems.size(), visibleDisplayFrame, screenHeight, sizingPadding, footerHeight);
    setPopupOffset(mPopup, mCurrentScreenRotation, visibleDisplayFrame, sizingPadding);
    mPopup.setOnItemClickListener(this);
    mPopup.show();
    mPopup.getListView().setItemsCanFocus(true);
    mPopup.getListView().setOnKeyListener(this);
    mHandler.onMenuVisibilityChanged(true);
    if (mVerticalFadeDistance > 0) {
        mPopup.getListView().setVerticalFadingEdgeEnabled(true);
        mPopup.getListView().setFadingEdgeLength(mVerticalFadeDistance);
    }
    // Don't animate the menu items for low end devices.
    if (!SysUtils.isLowEndDevice()) {
        mPopup.getListView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                mPopup.getListView().removeOnLayoutChangeListener(this);
                runMenuItemEnterAnimations();
            }
        });
    }
}
Also used : Rect(android.graphics.Rect) OnDismissListener(android.widget.PopupWindow.OnDismissListener) Drawable(android.graphics.drawable.Drawable) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) ImageButton(android.widget.ImageButton) ListPopupWindow(android.widget.ListPopupWindow)

Example 2 with OnDismissListener

use of android.widget.PopupWindow.OnDismissListener in project AndroidChromium by JackyAndroid.

the class ToolbarPhone method showTabSwitcherCalloutIfNecessary.

private void showTabSwitcherCalloutIfNecessary() {
    assert mTabSwitcherCallout == null;
    mTabSwitcherCallout = TabSwitcherCallout.showIfNecessary(getContext(), mToggleTabStackButton);
    if (mTabSwitcherCallout == null)
        return;
    mTabSwitcherCallout.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss() {
            if (mFullscreenManager != null) {
                mFullscreenManager.hideControlsPersistent(mFullscreenCalloutToken);
                mFullscreenCalloutToken = FullscreenManager.INVALID_TOKEN;
            }
            mTabSwitcherCallout = null;
        }
    });
    if (mFullscreenManager != null) {
        mFullscreenCalloutToken = mFullscreenManager.showControlsPersistentAndClearOldToken(mFullscreenCalloutToken);
    }
}
Also used : OnDismissListener(android.widget.PopupWindow.OnDismissListener)

Aggregations

OnDismissListener (android.widget.PopupWindow.OnDismissListener)2 Rect (android.graphics.Rect)1 Drawable (android.graphics.drawable.Drawable)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ImageButton (android.widget.ImageButton)1 ListPopupWindow (android.widget.ListPopupWindow)1 ListView (android.widget.ListView)1 ArrayList (java.util.ArrayList)1