ToolbarActionBar.java revision 31a217290cf376d0573fc36e21c8940987485019
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.app;
19
20import android.annotation.Nullable;
21import android.app.ActionBar;
22import android.content.Context;
23import android.content.res.Configuration;
24import android.graphics.drawable.Drawable;
25import android.view.ActionMode;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.widget.SpinnerAdapter;
29import android.widget.Toolbar;
30
31import java.util.ArrayList;
32import java.util.Map;
33
34public class ToolbarActionBar extends ActionBar {
35    private Toolbar mToolbar;
36    private View mCustomView;
37
38    private int mDisplayOptions;
39
40    private int mNavResId;
41    private int mIconResId;
42    private int mLogoResId;
43    private Drawable mNavDrawable;
44    private Drawable mIconDrawable;
45    private Drawable mLogoDrawable;
46    private int mTitleResId;
47    private int mSubtitleResId;
48    private CharSequence mTitle;
49    private CharSequence mSubtitle;
50
51    private boolean mLastMenuVisibility;
52    private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
53            new ArrayList<OnMenuVisibilityListener>();
54
55    public ToolbarActionBar(Toolbar toolbar) {
56        mToolbar = toolbar;
57    }
58
59    @Override
60    public void setCustomView(View view) {
61        setCustomView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
62    }
63
64    @Override
65    public void setCustomView(View view, LayoutParams layoutParams) {
66        if (mCustomView != null) {
67            mToolbar.removeView(mCustomView);
68        }
69        mCustomView = view;
70        if (view != null) {
71            mToolbar.addView(view, generateLayoutParams(layoutParams));
72        }
73    }
74
75    private Toolbar.LayoutParams generateLayoutParams(LayoutParams lp) {
76        final Toolbar.LayoutParams result = new Toolbar.LayoutParams(lp);
77        result.gravity = lp.gravity;
78        return result;
79    }
80
81    @Override
82    public void setCustomView(int resId) {
83        final LayoutInflater inflater = LayoutInflater.from(mToolbar.getContext());
84        setCustomView(inflater.inflate(resId, mToolbar, false));
85    }
86
87    @Override
88    public void setIcon(int resId) {
89        mIconResId = resId;
90        mIconDrawable = null;
91        updateToolbarLogo();
92    }
93
94    @Override
95    public void setIcon(Drawable icon) {
96        mIconResId = 0;
97        mIconDrawable = icon;
98        updateToolbarLogo();
99    }
100
101    @Override
102    public void setLogo(int resId) {
103        mLogoResId = resId;
104        mLogoDrawable = null;
105        updateToolbarLogo();
106    }
107
108    @Override
109    public void setLogo(Drawable logo) {
110        mLogoResId = 0;
111        mLogoDrawable = logo;
112        updateToolbarLogo();
113    }
114
115    private void updateToolbarLogo() {
116        Drawable drawable = null;
117        if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_HOME) != 0) {
118            final int resId;
119            if ((mDisplayOptions & ActionBar.DISPLAY_USE_LOGO) != 0) {
120                resId = mLogoResId;
121                drawable = mLogoDrawable;
122            } else {
123                resId = mIconResId;
124                drawable = mIconDrawable;
125            }
126            if (resId != 0) {
127                drawable = mToolbar.getContext().getDrawable(resId);
128            }
129        }
130        mToolbar.setLogo(drawable);
131    }
132
133    @Override
134    public void setStackedBackgroundDrawable(Drawable d) {
135        // This space for rent (do nothing)
136    }
137
138    @Override
139    public void setSplitBackgroundDrawable(Drawable d) {
140        // This space for rent (do nothing)
141    }
142
143    @Override
144    public void setHomeButtonEnabled(boolean enabled) {
145        // If the nav button on a Toolbar is present, it's enabled. No-op.
146    }
147
148    @Override
149    public Context getThemedContext() {
150        return mToolbar.getContext();
151    }
152
153    @Override
154    public boolean isTitleTruncated() {
155        return super.isTitleTruncated();
156    }
157
158    @Override
159    public void setHomeAsUpIndicator(Drawable indicator) {
160        mToolbar.setNavigationIcon(indicator);
161    }
162
163    @Override
164    public void setHomeAsUpIndicator(int resId) {
165        mToolbar.setNavigationIcon(resId);
166    }
167
168    @Override
169    public void setHomeActionContentDescription(CharSequence description) {
170        mToolbar.setNavigationDescription(description);
171    }
172
173    @Override
174    public void setDefaultDisplayHomeAsUpEnabled(boolean enabled) {
175        // Do nothing
176    }
177
178    @Override
179    public void setHomeActionContentDescription(int resId) {
180        mToolbar.setNavigationDescription(resId);
181    }
182
183    @Override
184    public void setShowHideAnimationEnabled(boolean enabled) {
185        // This space for rent; no-op.
186    }
187
188    @Override
189    public void onConfigurationChanged(Configuration config) {
190        super.onConfigurationChanged(config);
191    }
192
193    @Override
194    public ActionMode startActionMode(ActionMode.Callback callback) {
195        return mToolbar.startActionMode(callback);
196    }
197
198    @Override
199    public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
200        throw new UnsupportedOperationException(
201                "Navigation modes are not supported in toolbar action bars");
202    }
203
204    @Override
205    public void setSelectedNavigationItem(int position) {
206        throw new UnsupportedOperationException(
207                "Navigation modes are not supported in toolbar action bars");
208    }
209
210    @Override
211    public int getSelectedNavigationIndex() {
212        return -1;
213    }
214
215    @Override
216    public int getNavigationItemCount() {
217        return 0;
218    }
219
220    @Override
221    public void setTitle(CharSequence title) {
222        mTitle = title;
223        mTitleResId = 0;
224        updateToolbarTitle();
225    }
226
227    @Override
228    public void setTitle(int resId) {
229        mTitleResId = resId;
230        mTitle = null;
231        updateToolbarTitle();
232    }
233
234    @Override
235    public void setSubtitle(CharSequence subtitle) {
236        mSubtitle = subtitle;
237        mSubtitleResId = 0;
238        updateToolbarTitle();
239    }
240
241    @Override
242    public void setSubtitle(int resId) {
243        mSubtitleResId = resId;
244        mSubtitle = null;
245        updateToolbarTitle();
246    }
247
248    private void updateToolbarTitle() {
249        final Context context = mToolbar.getContext();
250        CharSequence title = null;
251        CharSequence subtitle = null;
252        if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0) {
253            title = mTitleResId != 0 ? context.getText(mTitleResId) : mTitle;
254            subtitle = mSubtitleResId != 0 ? context.getText(mSubtitleResId) : mSubtitle;
255        }
256        mToolbar.setTitle(title);
257        mToolbar.setSubtitle(subtitle);
258    }
259
260    @Override
261    public void setDisplayOptions(@DisplayOptions int options) {
262        setDisplayOptions(options, 0xffffffff);
263    }
264
265    @Override
266    public void setDisplayOptions(@DisplayOptions int options, @DisplayOptions int mask) {
267        final int oldOptions = mDisplayOptions;
268        mDisplayOptions = (options & mask) | (mDisplayOptions & ~mask);
269        final int optionsChanged = oldOptions ^ mDisplayOptions;
270    }
271
272    @Override
273    public void setDisplayUseLogoEnabled(boolean useLogo) {
274        setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
275    }
276
277    @Override
278    public void setDisplayShowHomeEnabled(boolean showHome) {
279        setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
280    }
281
282    @Override
283    public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
284        setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
285    }
286
287    @Override
288    public void setDisplayShowTitleEnabled(boolean showTitle) {
289        setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
290    }
291
292    @Override
293    public void setDisplayShowCustomEnabled(boolean showCustom) {
294        setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
295    }
296
297    @Override
298    public void setBackgroundDrawable(@Nullable Drawable d) {
299        mToolbar.setBackground(d);
300    }
301
302    @Override
303    public View getCustomView() {
304        return mCustomView;
305    }
306
307    @Override
308    public CharSequence getTitle() {
309        return mToolbar.getTitle();
310    }
311
312    @Override
313    public CharSequence getSubtitle() {
314        return mToolbar.getSubtitle();
315    }
316
317    @Override
318    public int getNavigationMode() {
319        return NAVIGATION_MODE_STANDARD;
320    }
321
322    @Override
323    public void setNavigationMode(@NavigationMode int mode) {
324        throw new UnsupportedOperationException(
325                "Navigation modes are not supported in toolbar action bars");
326    }
327
328    @Override
329    public int getDisplayOptions() {
330        return mDisplayOptions;
331    }
332
333    @Override
334    public Tab newTab() {
335        throw new UnsupportedOperationException(
336                "Navigation modes are not supported in toolbar action bars");
337    }
338
339    @Override
340    public void addTab(Tab tab) {
341        throw new UnsupportedOperationException(
342                "Navigation modes are not supported in toolbar action bars");
343    }
344
345    @Override
346    public void addTab(Tab tab, boolean setSelected) {
347        throw new UnsupportedOperationException(
348                "Navigation modes are not supported in toolbar action bars");
349    }
350
351    @Override
352    public void addTab(Tab tab, int position) {
353        throw new UnsupportedOperationException(
354                "Navigation modes are not supported in toolbar action bars");
355    }
356
357    @Override
358    public void addTab(Tab tab, int position, boolean setSelected) {
359        throw new UnsupportedOperationException(
360                "Navigation modes are not supported in toolbar action bars");
361    }
362
363    @Override
364    public void removeTab(Tab tab) {
365        throw new UnsupportedOperationException(
366                "Navigation modes are not supported in toolbar action bars");
367    }
368
369    @Override
370    public void removeTabAt(int position) {
371        throw new UnsupportedOperationException(
372                "Navigation modes are not supported in toolbar action bars");
373    }
374
375    @Override
376    public void removeAllTabs() {
377        throw new UnsupportedOperationException(
378                "Navigation modes are not supported in toolbar action bars");
379    }
380
381    @Override
382    public void selectTab(Tab tab) {
383        throw new UnsupportedOperationException(
384                "Navigation modes are not supported in toolbar action bars");
385    }
386
387    @Override
388    public Tab getSelectedTab() {
389        throw new UnsupportedOperationException(
390                "Navigation modes are not supported in toolbar action bars");
391    }
392
393    @Override
394    public Tab getTabAt(int index) {
395        throw new UnsupportedOperationException(
396                "Navigation modes are not supported in toolbar action bars");
397    }
398
399    @Override
400    public int getTabCount() {
401        return 0;
402    }
403
404    @Override
405    public int getHeight() {
406        return mToolbar.getHeight();
407    }
408
409    @Override
410    public void show() {
411        // TODO: Consider a better transition for this.
412        // Right now use no automatic transition so that the app can supply one if desired.
413        mToolbar.setVisibility(View.VISIBLE);
414    }
415
416    @Override
417    public void hide() {
418        // TODO: Consider a better transition for this.
419        // Right now use no automatic transition so that the app can supply one if desired.
420        mToolbar.setVisibility(View.GONE);
421    }
422
423    @Override
424    public boolean isShowing() {
425        return mToolbar.getVisibility() == View.VISIBLE;
426    }
427
428    public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
429        mMenuVisibilityListeners.add(listener);
430    }
431
432    public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
433        mMenuVisibilityListeners.remove(listener);
434    }
435
436    public void dispatchMenuVisibilityChanged(boolean isVisible) {
437        if (isVisible == mLastMenuVisibility) {
438            return;
439        }
440        mLastMenuVisibility = isVisible;
441
442        final int count = mMenuVisibilityListeners.size();
443        for (int i = 0; i < count; i++) {
444            mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
445        }
446    }
447}
448