FileDocCategorySizeDatePackage
ToolbarWidgetWrapper.javaAPI DocAndroid 5.1 API22025Thu Mar 12 22:22:10 GMT 2015com.android.internal.widget

ToolbarWidgetWrapper

public class ToolbarWidgetWrapper extends Object implements DecorToolbar
Internal class used to interact with the Toolbar widget without exposing interface methods to the public API.

ToolbarWidgetWrapper manages the differences between Toolbar and ActionBarView so that either variant acting as a {@link com.android.internal.app.WindowDecorActionBar WindowDecorActionBar} can behave in the same way.

hide

Fields Summary
private static final String
TAG
private static final int
AFFECTS_LOGO_MASK
private android.widget.Toolbar
mToolbar
private int
mDisplayOpts
private android.view.View
mTabView
private android.widget.Spinner
mSpinner
private android.view.View
mCustomView
private android.graphics.drawable.Drawable
mIcon
private android.graphics.drawable.Drawable
mLogo
private android.graphics.drawable.Drawable
mNavIcon
private boolean
mTitleSet
private CharSequence
mTitle
private CharSequence
mSubtitle
private CharSequence
mHomeDescription
private Window.Callback
mWindowCallback
private boolean
mMenuPrepared
private android.widget.ActionMenuPresenter
mActionMenuPresenter
private int
mNavigationMode
private int
mDefaultNavigationContentDescription
private android.graphics.drawable.Drawable
mDefaultNavigationIcon
Constructors Summary
public ToolbarWidgetWrapper(android.widget.Toolbar toolbar, boolean style)


         
        this(toolbar, style, R.string.action_bar_up_description);
    
public ToolbarWidgetWrapper(android.widget.Toolbar toolbar, boolean style, int defaultNavigationContentDescription)

        mToolbar = toolbar;

        mTitle = toolbar.getTitle();
        mSubtitle = toolbar.getSubtitle();
        mTitleSet = mTitle != null;
        mNavIcon = mToolbar.getNavigationIcon();
        final TypedArray a = toolbar.getContext().obtainStyledAttributes(null,
                R.styleable.ActionBar, R.attr.actionBarStyle, 0);
        mDefaultNavigationIcon = a.getDrawable(R.styleable.ActionBar_homeAsUpIndicator);
        if (style) {
            final CharSequence title = a.getText(R.styleable.ActionBar_title);
            if (!TextUtils.isEmpty(title)) {
                setTitle(title);
            }

            final CharSequence subtitle = a.getText(R.styleable.ActionBar_subtitle);
            if (!TextUtils.isEmpty(subtitle)) {
                setSubtitle(subtitle);
            }

            final Drawable logo = a.getDrawable(R.styleable.ActionBar_logo);
            if (logo != null) {
                setLogo(logo);
            }

            final Drawable icon = a.getDrawable(R.styleable.ActionBar_icon);
            if (icon != null) {
                setIcon(icon);
            }
            if (mNavIcon == null && mDefaultNavigationIcon != null) {
                setNavigationIcon(mDefaultNavigationIcon);
            }
            setDisplayOptions(a.getInt(R.styleable.ActionBar_displayOptions, 0));

            final int customNavId = a.getResourceId(
                    R.styleable.ActionBar_customNavigationLayout, 0);
            if (customNavId != 0) {
                setCustomView(LayoutInflater.from(mToolbar.getContext()).inflate(customNavId,
                        mToolbar, false));
                setDisplayOptions(mDisplayOpts | ActionBar.DISPLAY_SHOW_CUSTOM);
            }

            final int height = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
            if (height > 0) {
                final ViewGroup.LayoutParams lp = mToolbar.getLayoutParams();
                lp.height = height;
                mToolbar.setLayoutParams(lp);
            }

            final int contentInsetStart = a.getDimensionPixelOffset(
                    R.styleable.ActionBar_contentInsetStart, -1);
            final int contentInsetEnd = a.getDimensionPixelOffset(
                    R.styleable.ActionBar_contentInsetEnd, -1);
            if (contentInsetStart >= 0 || contentInsetEnd >= 0) {
                mToolbar.setContentInsetsRelative(Math.max(contentInsetStart, 0),
                        Math.max(contentInsetEnd, 0));
            }

            final int titleTextStyle = a.getResourceId(R.styleable.ActionBar_titleTextStyle, 0);
            if (titleTextStyle != 0) {
                mToolbar.setTitleTextAppearance(mToolbar.getContext(), titleTextStyle);
            }

            final int subtitleTextStyle = a.getResourceId(
                    R.styleable.ActionBar_subtitleTextStyle, 0);
            if (subtitleTextStyle != 0) {
                mToolbar.setSubtitleTextAppearance(mToolbar.getContext(), subtitleTextStyle);
            }

            final int popupTheme = a.getResourceId(R.styleable.ActionBar_popupTheme, 0);
            if (popupTheme != 0) {
                mToolbar.setPopupTheme(popupTheme);
            }
        } else {
            mDisplayOpts = detectDisplayOptions();
        }
        a.recycle();

        setDefaultNavigationContentDescription(defaultNavigationContentDescription);
        mHomeDescription = mToolbar.getNavigationContentDescription();

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            final ActionMenuItem mNavItem = new ActionMenuItem(mToolbar.getContext(),
                    0, android.R.id.home, 0, 0, mTitle);
            @Override
            public void onClick(View v) {
                if (mWindowCallback != null && mMenuPrepared) {
                    mWindowCallback.onMenuItemSelected(Window.FEATURE_OPTIONS_PANEL, mNavItem);
                }
            }
        });
    
Methods Summary
public voidanimateToVisibility(int visibility)

        if (visibility == View.GONE) {
            mToolbar.animate().alpha(0)
                    .setListener(new AnimatorListenerAdapter() {
                        private boolean mCanceled = false;
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            if (!mCanceled) {
                                mToolbar.setVisibility(View.GONE);
                            }
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {
                            mCanceled = true;
                        }
                    });
        } else if (visibility == View.VISIBLE) {
            mToolbar.animate().alpha(1)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationStart(Animator animation) {
                            mToolbar.setVisibility(View.VISIBLE);
                        }
                    });
        }
    
public booleancanShowOverflowMenu()

        return mToolbar.canShowOverflowMenu();
    
public booleancanSplit()

        return false;
    
public voidcollapseActionView()

        mToolbar.collapseActionView();
    
private intdetectDisplayOptions()

        int opts = ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME |
                ActionBar.DISPLAY_USE_LOGO;
        if (mToolbar.getNavigationIcon() != null) {
            opts |= ActionBar.DISPLAY_HOME_AS_UP;
            mDefaultNavigationIcon = mToolbar.getNavigationIcon();
        }
        return opts;
    
public voiddismissPopupMenus()

        mToolbar.dismissPopupMenus();
    
private voidensureSpinner()

        if (mSpinner == null) {
            mSpinner = new Spinner(getContext(), null, R.attr.actionDropDownStyle);
            Toolbar.LayoutParams lp = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.START | Gravity.CENTER_VERTICAL);
            mSpinner.setLayoutParams(lp);
        }
    
public android.content.ContextgetContext()

        return mToolbar.getContext();
    
public android.view.ViewgetCustomView()

        return mCustomView;
    
public intgetDisplayOptions()

        return mDisplayOpts;
    
public intgetDropdownItemCount()

        return mSpinner != null ? mSpinner.getCount() : 0;
    
public intgetDropdownSelectedPosition()

        return mSpinner != null ? mSpinner.getSelectedItemPosition() : 0;
    
public intgetHeight()

        return mToolbar.getHeight();
    
public android.view.MenugetMenu()

        return mToolbar.getMenu();
    
public intgetNavigationMode()

        return mNavigationMode;
    
public java.lang.CharSequencegetSubtitle()

        return mToolbar.getSubtitle();
    
public java.lang.CharSequencegetTitle()

        return mToolbar.getTitle();
    
public android.view.ViewGroupgetViewGroup()

        return mToolbar;
    
public intgetVisibility()

        return mToolbar.getVisibility();
    
public booleanhasEmbeddedTabs()

        return mTabView != null;
    
public booleanhasExpandedActionView()

        return mToolbar.hasExpandedActionView();
    
public booleanhasIcon()

        return mIcon != null;
    
public booleanhasLogo()

        return mLogo != null;
    
public booleanhideOverflowMenu()

        return mToolbar.hideOverflowMenu();
    
public voidinitIndeterminateProgress()

        Log.i(TAG, "Progress display unsupported");
    
public voidinitProgress()

        Log.i(TAG, "Progress display unsupported");
    
public booleanisOverflowMenuShowPending()

        return mToolbar.isOverflowMenuShowPending();
    
public booleanisOverflowMenuShowing()

        return mToolbar.isOverflowMenuShowing();
    
public booleanisSplit()

        return false;
    
public booleanisTitleTruncated()

        return mToolbar.isTitleTruncated();
    
public voidrestoreHierarchyState(android.util.SparseArray toolbarStates)

        mToolbar.restoreHierarchyState(toolbarStates);
    
public voidsaveHierarchyState(android.util.SparseArray toolbarStates)

        mToolbar.saveHierarchyState(toolbarStates);
    
public voidsetBackgroundDrawable(android.graphics.drawable.Drawable d)

        //noinspection deprecation
        mToolbar.setBackgroundDrawable(d);
    
public voidsetCollapsible(boolean collapsible)

        mToolbar.setCollapsible(collapsible);
    
public voidsetCustomView(android.view.View view)

        if (mCustomView != null && (mDisplayOpts & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) {
            mToolbar.removeView(mCustomView);
        }
        mCustomView = view;
        if (view != null && (mDisplayOpts & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) {
            mToolbar.addView(mCustomView);
        }
    
public voidsetDefaultNavigationContentDescription(int defaultNavigationContentDescription)

        if (defaultNavigationContentDescription == mDefaultNavigationContentDescription) {
            return;
        }
        mDefaultNavigationContentDescription = defaultNavigationContentDescription;
        if (TextUtils.isEmpty(mToolbar.getNavigationContentDescription())) {
            setNavigationContentDescription(mDefaultNavigationContentDescription);
        }
    
public voidsetDefaultNavigationIcon(android.graphics.drawable.Drawable defaultNavigationIcon)

        if (mDefaultNavigationIcon != defaultNavigationIcon) {
            mDefaultNavigationIcon = defaultNavigationIcon;
            updateNavigationIcon();
        }
    
public voidsetDisplayOptions(int newOpts)

        final int oldOpts = mDisplayOpts;
        final int changed = oldOpts ^ newOpts;
        mDisplayOpts = newOpts;
        if (changed != 0) {
            if ((changed & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
                if ((newOpts & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
                    updateHomeAccessibility();
                }
                updateNavigationIcon();
            }

            if ((changed & AFFECTS_LOGO_MASK) != 0) {
                updateToolbarLogo();
            }

            if ((changed & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
                if ((newOpts & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
                    mToolbar.setTitle(mTitle);
                    mToolbar.setSubtitle(mSubtitle);
                } else {
                    mToolbar.setTitle(null);
                    mToolbar.setSubtitle(null);
                }
            }

            if ((changed & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && mCustomView != null) {
                if ((newOpts & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) {
                    mToolbar.addView(mCustomView);
                } else {
                    mToolbar.removeView(mCustomView);
                }
            }
        }
    
public voidsetDropdownParams(android.widget.SpinnerAdapter adapter, AdapterView.OnItemSelectedListener listener)

        ensureSpinner();
        mSpinner.setAdapter(adapter);
        mSpinner.setOnItemSelectedListener(listener);
    
public voidsetDropdownSelectedPosition(int position)

        if (mSpinner == null) {
            throw new IllegalStateException(
                    "Can't set dropdown selected position without an adapter");
        }
        mSpinner.setSelection(position);
    
public voidsetEmbeddedTabView(ScrollingTabContainerView tabView)

        if (mTabView != null && mTabView.getParent() == mToolbar) {
            mToolbar.removeView(mTabView);
        }
        mTabView = tabView;
        if (tabView != null && mNavigationMode == ActionBar.NAVIGATION_MODE_TABS) {
            mToolbar.addView(mTabView, 0);
            Toolbar.LayoutParams lp = (Toolbar.LayoutParams) mTabView.getLayoutParams();
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.START | Gravity.BOTTOM;
            tabView.setAllowCollapse(true);
        }
    
public voidsetHomeButtonEnabled(boolean enable)

        // Ignore
    
public voidsetIcon(int resId)

        setIcon(resId != 0 ? getContext().getDrawable(resId) : null);
    
public voidsetIcon(android.graphics.drawable.Drawable d)

        mIcon = d;
        updateToolbarLogo();
    
public voidsetLogo(int resId)

        setLogo(resId != 0 ? getContext().getDrawable(resId) : null);
    
public voidsetLogo(android.graphics.drawable.Drawable d)

        mLogo = d;
        updateToolbarLogo();
    
public voidsetMenu(android.view.Menu menu, MenuPresenter.Callback cb)

        if (mActionMenuPresenter == null) {
            mActionMenuPresenter = new ActionMenuPresenter(mToolbar.getContext());
            mActionMenuPresenter.setId(com.android.internal.R.id.action_menu_presenter);
        }
        mActionMenuPresenter.setCallback(cb);
        mToolbar.setMenu((MenuBuilder) menu, mActionMenuPresenter);
    
public voidsetMenuCallbacks(MenuPresenter.Callback presenterCallback, MenuBuilder.Callback menuBuilderCallback)

        mToolbar.setMenuCallbacks(presenterCallback, menuBuilderCallback);
    
public voidsetMenuPrepared()

        mMenuPrepared = true;
    
public voidsetNavigationContentDescription(java.lang.CharSequence description)

        mHomeDescription = description;
        updateHomeAccessibility();
    
public voidsetNavigationContentDescription(int resId)

        setNavigationContentDescription(resId == 0 ? null : getContext().getString(resId));
    
public voidsetNavigationIcon(android.graphics.drawable.Drawable icon)

        mNavIcon = icon;
        updateNavigationIcon();
    
public voidsetNavigationIcon(int resId)

        setNavigationIcon(resId != 0 ? mToolbar.getContext().getDrawable(resId) : null);
    
public voidsetNavigationMode(int mode)

        final int oldMode = mNavigationMode;
        if (mode != oldMode) {
            switch (oldMode) {
                case ActionBar.NAVIGATION_MODE_LIST:
                    if (mSpinner != null && mSpinner.getParent() == mToolbar) {
                        mToolbar.removeView(mSpinner);
                    }
                    break;
                case ActionBar.NAVIGATION_MODE_TABS:
                    if (mTabView != null && mTabView.getParent() == mToolbar) {
                        mToolbar.removeView(mTabView);
                    }
                    break;
            }

            mNavigationMode = mode;

            switch (mode) {
                case ActionBar.NAVIGATION_MODE_STANDARD:
                    break;
                case ActionBar.NAVIGATION_MODE_LIST:
                    ensureSpinner();
                    mToolbar.addView(mSpinner, 0);
                    break;
                case ActionBar.NAVIGATION_MODE_TABS:
                    if (mTabView != null) {
                        mToolbar.addView(mTabView, 0);
                        Toolbar.LayoutParams lp = (Toolbar.LayoutParams) mTabView.getLayoutParams();
                        lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                        lp.gravity = Gravity.START | Gravity.BOTTOM;
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Invalid navigation mode " + mode);
            }
        }
    
public voidsetSplitToolbar(boolean split)

        if (split) {
            throw new UnsupportedOperationException("Cannot split an android.widget.Toolbar");
        }
    
public voidsetSplitView(android.view.ViewGroup splitView)

    
public voidsetSplitWhenNarrow(boolean splitWhenNarrow)

        // Ignore.
    
public voidsetSubtitle(java.lang.CharSequence subtitle)

        mSubtitle = subtitle;
        if ((mDisplayOpts & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
            mToolbar.setSubtitle(subtitle);
        }
    
public voidsetTitle(java.lang.CharSequence title)

        mTitleSet = true;
        setTitleInt(title);
    
private voidsetTitleInt(java.lang.CharSequence title)

        mTitle = title;
        if ((mDisplayOpts & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
            mToolbar.setTitle(title);
        }
    
public voidsetVisibility(int visible)

        mToolbar.setVisibility(visible);
    
public voidsetWindowCallback(Window.Callback cb)

        mWindowCallback = cb;
    
public voidsetWindowTitle(java.lang.CharSequence title)

        // "Real" title always trumps window title.
        if (!mTitleSet) {
            setTitleInt(title);
        }
    
public booleanshowOverflowMenu()

        return mToolbar.showOverflowMenu();
    
private voidupdateHomeAccessibility()

        if ((mDisplayOpts & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
            if (TextUtils.isEmpty(mHomeDescription)) {
                mToolbar.setNavigationContentDescription(mDefaultNavigationContentDescription);
            } else {
                mToolbar.setNavigationContentDescription(mHomeDescription);
            }
        }
    
private voidupdateNavigationIcon()

        if ((mDisplayOpts & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
            mToolbar.setNavigationIcon(mNavIcon != null ? mNavIcon : mDefaultNavigationIcon);
        } else {
            mToolbar.setNavigationIcon(null);
        }
    
private voidupdateToolbarLogo()

        Drawable logo = null;
        if ((mDisplayOpts & ActionBar.DISPLAY_SHOW_HOME) != 0) {
            if ((mDisplayOpts & ActionBar.DISPLAY_USE_LOGO) != 0) {
                logo = mLogo != null ? mLogo : mIcon;
            } else {
                logo = mIcon;
            }
        }
        mToolbar.setLogo(logo);