ToolbarWidgetWrapper.java revision 2aa09a94dd540650d3ebad7363e4fb3aab2ebc95
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package com.android.internal.widget;
19
20import android.animation.Animator;
21import android.animation.AnimatorListenerAdapter;
22import android.app.ActionBar;
23import android.content.Context;
24import android.content.res.TypedArray;
25import android.graphics.drawable.Drawable;
26import android.os.Parcelable;
27import android.text.TextUtils;
28import android.util.Log;
29import android.util.SparseArray;
30import android.view.Gravity;
31import android.view.LayoutInflater;
32import android.view.Menu;
33import android.view.View;
34import android.view.ViewGroup;
35import android.view.Window;
36import android.widget.ActionMenuPresenter;
37import android.widget.AdapterView;
38import android.widget.Spinner;
39import android.widget.SpinnerAdapter;
40import android.widget.Toolbar;
41import com.android.internal.R;
42import com.android.internal.view.menu.ActionMenuItem;
43import com.android.internal.view.menu.MenuBuilder;
44import com.android.internal.view.menu.MenuPresenter;
45
46/**
47 * Internal class used to interact with the Toolbar widget without
48 * exposing interface methods to the public API.
49 *
50 * <p>ToolbarWidgetWrapper manages the differences between Toolbar and ActionBarView
51 * so that either variant acting as a
52 * {@link com.android.internal.app.WindowDecorActionBar WindowDecorActionBar} can behave
53 * in the same way.</p>
54 *
55 * @hide
56 */
57public class ToolbarWidgetWrapper implements DecorToolbar {
58    private static final String TAG = "ToolbarWidgetWrapper";
59
60    private static final int AFFECTS_LOGO_MASK =
61            ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_USE_LOGO;
62
63    private Toolbar mToolbar;
64
65    private int mDisplayOpts;
66    private View mTabView;
67    private Spinner mSpinner;
68    private View mCustomView;
69
70    private Drawable mIcon;
71    private Drawable mLogo;
72    private Drawable mNavIcon;
73
74    private boolean mTitleSet;
75    private CharSequence mTitle;
76    private CharSequence mSubtitle;
77
78    private Window.Callback mWindowCallback;
79    private boolean mMenuPrepared;
80    private ActionMenuPresenter mActionMenuPresenter;
81
82    private int mNavigationMode = ActionBar.NAVIGATION_MODE_STANDARD;
83
84    public ToolbarWidgetWrapper(Toolbar toolbar, boolean style) {
85        mToolbar = toolbar;
86
87        mTitle = toolbar.getTitle();
88        mSubtitle = toolbar.getSubtitle();
89        mTitleSet = !TextUtils.isEmpty(mTitle);
90
91        if (style) {
92            final TypedArray a = toolbar.getContext().obtainStyledAttributes(null,
93                    R.styleable.ActionBar, R.attr.actionBarStyle, 0);
94
95            final CharSequence title = a.getText(R.styleable.ActionBar_title);
96            if (!TextUtils.isEmpty(title)) {
97                setTitle(title);
98            }
99
100            final CharSequence subtitle = a.getText(R.styleable.ActionBar_subtitle);
101            if (!TextUtils.isEmpty(subtitle)) {
102                setSubtitle(subtitle);
103            }
104
105            final Drawable logo = a.getDrawable(R.styleable.ActionBar_logo);
106            if (logo != null) {
107                setLogo(logo);
108            }
109
110            final Drawable icon = a.getDrawable(R.styleable.ActionBar_icon);
111            if (icon != null) {
112                setIcon(icon);
113            }
114
115            final Drawable navIcon = a.getDrawable(R.styleable.ActionBar_homeAsUpIndicator);
116            if (navIcon != null) {
117                setNavigationIcon(navIcon);
118            }
119
120            setDisplayOptions(a.getInt(R.styleable.ActionBar_displayOptions, 0));
121
122            final int customNavId = a.getResourceId(
123                    R.styleable.ActionBar_customNavigationLayout, 0);
124            if (customNavId != 0) {
125                setCustomView(LayoutInflater.from(mToolbar.getContext()).inflate(customNavId,
126                        mToolbar, false));
127                setDisplayOptions(mDisplayOpts | ActionBar.DISPLAY_SHOW_CUSTOM);
128            }
129
130            final int height = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
131            if (height > 0) {
132                final ViewGroup.LayoutParams lp = mToolbar.getLayoutParams();
133                lp.height = height;
134                mToolbar.setLayoutParams(lp);
135            }
136
137            final int contentInsetStart = a.getDimensionPixelOffset(
138                    R.styleable.ActionBar_contentInsetStart, -1);
139            final int contentInsetEnd = a.getDimensionPixelOffset(
140                    R.styleable.ActionBar_contentInsetEnd, -1);
141            if (contentInsetStart >= 0 || contentInsetEnd >= 0) {
142                mToolbar.setContentInsetsRelative(Math.max(contentInsetStart, 0),
143                        Math.max(contentInsetEnd, 0));
144            }
145
146            final int titleTextStyle = a.getResourceId(R.styleable.ActionBar_titleTextStyle, 0);
147            if (titleTextStyle != 0) {
148                mToolbar.setTitleTextAppearance(mToolbar.getContext(), titleTextStyle);
149            }
150
151            final int subtitleTextStyle = a.getResourceId(
152                    R.styleable.ActionBar_subtitleTextStyle, 0);
153            if (subtitleTextStyle != 0) {
154                mToolbar.setSubtitleTextAppearance(mToolbar.getContext(), subtitleTextStyle);
155            }
156
157            a.recycle();
158        }
159
160        if (TextUtils.isEmpty(mToolbar.getNavigationContentDescription())) {
161            mToolbar.setNavigationContentDescription(
162                    getContext().getResources().getText(R.string.action_bar_up_description));
163        }
164
165        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
166            final ActionMenuItem mNavItem = new ActionMenuItem(mToolbar.getContext(),
167                    0, android.R.id.home, 0, 0, mTitle);
168            @Override
169            public void onClick(View v) {
170                if (mWindowCallback != null && mMenuPrepared) {
171                    mWindowCallback.onMenuItemSelected(Window.FEATURE_OPTIONS_PANEL, mNavItem);
172                }
173            }
174        });
175    }
176
177    @Override
178    public ViewGroup getViewGroup() {
179        return mToolbar;
180    }
181
182    @Override
183    public Context getContext() {
184        return mToolbar.getContext();
185    }
186
187    @Override
188    public boolean isSplit() {
189        return false;
190    }
191
192    @Override
193    public boolean hasExpandedActionView() {
194        return mToolbar.hasExpandedActionView();
195    }
196
197    @Override
198    public void collapseActionView() {
199        mToolbar.collapseActionView();
200    }
201
202    @Override
203    public void setWindowCallback(Window.Callback cb) {
204        mWindowCallback = cb;
205    }
206
207    @Override
208    public void setWindowTitle(CharSequence title) {
209        // "Real" title always trumps window title.
210        if (!mTitleSet) {
211            setTitleInt(title);
212        }
213    }
214
215    @Override
216    public CharSequence getTitle() {
217        return mToolbar.getTitle();
218    }
219
220    @Override
221    public void setTitle(CharSequence title) {
222        mTitleSet = true;
223        setTitleInt(title);
224    }
225
226    private void setTitleInt(CharSequence title) {
227        mTitle = title;
228        if ((mDisplayOpts & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
229            mToolbar.setTitle(title);
230        }
231    }
232
233    @Override
234    public CharSequence getSubtitle() {
235        return mToolbar.getSubtitle();
236    }
237
238    @Override
239    public void setSubtitle(CharSequence subtitle) {
240        mSubtitle = subtitle;
241        if ((mDisplayOpts & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
242            mToolbar.setSubtitle(subtitle);
243        }
244    }
245
246    @Override
247    public void initProgress() {
248        Log.i(TAG, "Progress display unsupported");
249    }
250
251    @Override
252    public void initIndeterminateProgress() {
253        Log.i(TAG, "Progress display unsupported");
254    }
255
256    @Override
257    public boolean canSplit() {
258        return false;
259    }
260
261    @Override
262    public void setSplitView(ViewGroup splitView) {
263    }
264
265    @Override
266    public void setSplitToolbar(boolean split) {
267        if (split) {
268            throw new UnsupportedOperationException("Cannot split an android.widget.Toolbar");
269        }
270    }
271
272    @Override
273    public void setSplitWhenNarrow(boolean splitWhenNarrow) {
274        // Ignore.
275    }
276
277    @Override
278    public boolean hasIcon() {
279        return mIcon != null;
280    }
281
282    @Override
283    public boolean hasLogo() {
284        return mLogo != null;
285    }
286
287    @Override
288    public void setIcon(int resId) {
289        setIcon(resId != 0 ? getContext().getDrawable(resId) : null);
290    }
291
292    @Override
293    public void setIcon(Drawable d) {
294        mIcon = d;
295        updateToolbarLogo();
296    }
297
298    @Override
299    public void setLogo(int resId) {
300        setLogo(resId != 0 ? getContext().getDrawable(resId) : null);
301    }
302
303    @Override
304    public void setLogo(Drawable d) {
305        mLogo = d;
306        updateToolbarLogo();
307    }
308
309    private void updateToolbarLogo() {
310        Drawable logo = null;
311        if ((mDisplayOpts & ActionBar.DISPLAY_SHOW_HOME) != 0) {
312            if ((mDisplayOpts & ActionBar.DISPLAY_USE_LOGO) != 0) {
313                logo = mLogo != null ? mLogo : mIcon;
314            } else {
315                logo = mIcon;
316            }
317        }
318        mToolbar.setLogo(logo);
319    }
320
321    @Override
322    public boolean canShowOverflowMenu() {
323        return mToolbar.canShowOverflowMenu();
324    }
325
326    @Override
327    public boolean isOverflowMenuShowing() {
328        return mToolbar.isOverflowMenuShowing();
329    }
330
331    @Override
332    public boolean isOverflowMenuShowPending() {
333        return mToolbar.isOverflowMenuShowPending();
334    }
335
336    @Override
337    public boolean showOverflowMenu() {
338        return mToolbar.showOverflowMenu();
339    }
340
341    @Override
342    public boolean hideOverflowMenu() {
343        return mToolbar.hideOverflowMenu();
344    }
345
346    @Override
347    public void setMenuPrepared() {
348        mMenuPrepared = true;
349    }
350
351    @Override
352    public void setMenu(Menu menu, MenuPresenter.Callback cb) {
353        if (mActionMenuPresenter == null) {
354            mActionMenuPresenter = new ActionMenuPresenter(mToolbar.getContext());
355            mActionMenuPresenter.setId(com.android.internal.R.id.action_menu_presenter);
356        }
357        mActionMenuPresenter.setCallback(cb);
358        mToolbar.setMenu((MenuBuilder) menu, mActionMenuPresenter);
359    }
360
361    @Override
362    public void dismissPopupMenus() {
363        mToolbar.dismissPopupMenus();
364    }
365
366    @Override
367    public int getDisplayOptions() {
368        return mDisplayOpts;
369    }
370
371    @Override
372    public void setDisplayOptions(int newOpts) {
373        final int oldOpts = mDisplayOpts;
374        final int changed = oldOpts ^ newOpts;
375        mDisplayOpts = newOpts;
376        if (changed != 0) {
377            if ((changed & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
378                if ((newOpts & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
379                    mToolbar.setNavigationIcon(mNavIcon);
380                } else {
381                    mToolbar.setNavigationIcon(null);
382                }
383            }
384
385            if ((changed & AFFECTS_LOGO_MASK) != 0) {
386                updateToolbarLogo();
387            }
388
389            if ((changed & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
390                if ((newOpts & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
391                    mToolbar.setTitle(mTitle);
392                    mToolbar.setSubtitle(mSubtitle);
393                } else {
394                    mToolbar.setTitle(null);
395                    mToolbar.setSubtitle(null);
396                }
397            }
398
399            if ((changed & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && mCustomView != null) {
400                if ((newOpts & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) {
401                    mToolbar.addView(mCustomView);
402                } else {
403                    mToolbar.removeView(mCustomView);
404                }
405            }
406        }
407    }
408
409    @Override
410    public void setEmbeddedTabView(ScrollingTabContainerView tabView) {
411        if (mTabView != null && mTabView.getParent() == mToolbar) {
412            mToolbar.removeView(mTabView);
413        }
414        mTabView = tabView;
415        if (tabView != null && mNavigationMode == ActionBar.NAVIGATION_MODE_TABS) {
416            mToolbar.addView(mTabView, 0);
417            Toolbar.LayoutParams lp = (Toolbar.LayoutParams) mTabView.getLayoutParams();
418            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
419            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
420            lp.gravity = Gravity.START | Gravity.BOTTOM;
421            tabView.setAllowCollapse(true);
422        }
423    }
424
425    @Override
426    public boolean hasEmbeddedTabs() {
427        return mTabView != null;
428    }
429
430    @Override
431    public boolean isTitleTruncated() {
432        return mToolbar.isTitleTruncated();
433    }
434
435    @Override
436    public void setCollapsible(boolean collapsible) {
437        mToolbar.setCollapsible(collapsible);
438    }
439
440    @Override
441    public void setHomeButtonEnabled(boolean enable) {
442        // Ignore
443    }
444
445    @Override
446    public int getNavigationMode() {
447        return mNavigationMode;
448    }
449
450    @Override
451    public void setNavigationMode(int mode) {
452        final int oldMode = mNavigationMode;
453        if (mode != oldMode) {
454            switch (oldMode) {
455                case ActionBar.NAVIGATION_MODE_LIST:
456                    if (mSpinner != null && mSpinner.getParent() == mToolbar) {
457                        mToolbar.removeView(mSpinner);
458                    }
459                    break;
460                case ActionBar.NAVIGATION_MODE_TABS:
461                    if (mTabView != null && mTabView.getParent() == mToolbar) {
462                        mToolbar.removeView(mTabView);
463                    }
464                    break;
465            }
466
467            mNavigationMode = mode;
468
469            switch (mode) {
470                case ActionBar.NAVIGATION_MODE_STANDARD:
471                    break;
472                case ActionBar.NAVIGATION_MODE_LIST:
473                    ensureSpinner();
474                    mToolbar.addView(mSpinner, 0);
475                    break;
476                case ActionBar.NAVIGATION_MODE_TABS:
477                    if (mTabView != null) {
478                        mToolbar.addView(mTabView, 0);
479                        Toolbar.LayoutParams lp = (Toolbar.LayoutParams) mTabView.getLayoutParams();
480                        lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
481                        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
482                        lp.gravity = Gravity.START | Gravity.BOTTOM;
483                    }
484                    break;
485                default:
486                    throw new IllegalArgumentException("Invalid navigation mode " + mode);
487            }
488        }
489    }
490
491    private void ensureSpinner() {
492        if (mSpinner == null) {
493            mSpinner = new Spinner(getContext(), null, R.attr.actionDropDownStyle);
494            Toolbar.LayoutParams lp = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
495                    ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.START | Gravity.CENTER_VERTICAL);
496            mSpinner.setLayoutParams(lp);
497        }
498    }
499
500    @Override
501    public void setDropdownParams(SpinnerAdapter adapter,
502            AdapterView.OnItemSelectedListener listener) {
503        ensureSpinner();
504        mSpinner.setAdapter(adapter);
505        mSpinner.setOnItemSelectedListener(listener);
506    }
507
508    @Override
509    public void setDropdownSelectedPosition(int position) {
510        if (mSpinner == null) {
511            throw new IllegalStateException(
512                    "Can't set dropdown selected position without an adapter");
513        }
514        mSpinner.setSelection(position);
515    }
516
517    @Override
518    public int getDropdownSelectedPosition() {
519        return mSpinner != null ? mSpinner.getSelectedItemPosition() : 0;
520    }
521
522    @Override
523    public int getDropdownItemCount() {
524        return mSpinner != null ? mSpinner.getCount() : 0;
525    }
526
527    @Override
528    public void setCustomView(View view) {
529        if (mCustomView != null && (mDisplayOpts & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) {
530            mToolbar.removeView(mCustomView);
531        }
532        mCustomView = view;
533        if (view != null && (mDisplayOpts & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) {
534            mToolbar.addView(mCustomView);
535        }
536    }
537
538    @Override
539    public View getCustomView() {
540        return mCustomView;
541    }
542
543    @Override
544    public void animateToVisibility(int visibility) {
545        if (visibility == View.GONE) {
546            mToolbar.animate().translationY(mToolbar.getHeight()).alpha(0)
547                    .setListener(new AnimatorListenerAdapter() {
548                        private boolean mCanceled = false;
549                        @Override
550                        public void onAnimationEnd(Animator animation) {
551                            if (!mCanceled) {
552                                mToolbar.setVisibility(View.GONE);
553                            }
554                        }
555
556                        @Override
557                        public void onAnimationCancel(Animator animation) {
558                            mCanceled = true;
559                        }
560                    });
561        } else if (visibility == View.VISIBLE) {
562            mToolbar.animate().translationY(0).alpha(1)
563                    .setListener(new AnimatorListenerAdapter() {
564                        @Override
565                        public void onAnimationStart(Animator animation) {
566                            mToolbar.setVisibility(View.VISIBLE);
567                        }
568                    });
569        }
570    }
571
572    @Override
573    public void setNavigationIcon(Drawable icon) {
574        mNavIcon = icon;
575        if ((mDisplayOpts & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
576            mToolbar.setNavigationIcon(icon);
577        }
578    }
579
580    @Override
581    public void setNavigationIcon(int resId) {
582        setNavigationIcon(resId != 0 ? mToolbar.getContext().getDrawable(resId) : null);
583    }
584
585    @Override
586    public void setNavigationContentDescription(CharSequence description) {
587        mToolbar.setNavigationContentDescription(description);
588    }
589
590    @Override
591    public void setNavigationContentDescription(int resId) {
592        mToolbar.setNavigationContentDescription(resId);
593    }
594
595    @Override
596    public void saveHierarchyState(SparseArray<Parcelable> toolbarStates) {
597        mToolbar.saveHierarchyState(toolbarStates);
598    }
599
600    @Override
601    public void restoreHierarchyState(SparseArray<Parcelable> toolbarStates) {
602        mToolbar.restoreHierarchyState(toolbarStates);
603    }
604
605}
606