blob: b3904f4577084b5bdbb58321854bc16386d36184 [file] [log] [blame]
Adam Powelle43340c2014-03-17 19:10:43 -07001/*
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
Vadim Tryshev1abe85c2017-01-06 19:30:00 -080020import com.android.internal.view.menu.MenuBuilder;
21import com.android.internal.view.menu.MenuPresenter;
22import com.android.internal.widget.DecorToolbar;
23import com.android.internal.widget.ToolbarWidgetWrapper;
24
Adam Powelle43340c2014-03-17 19:10:43 -070025import android.annotation.Nullable;
26import android.app.ActionBar;
27import android.content.Context;
28import android.content.res.Configuration;
29import android.graphics.drawable.Drawable;
30import android.view.ActionMode;
Alan Viverette36a5c8d2015-08-26 12:48:17 -040031import android.view.KeyCharacterMap;
Adam Powell07a74542014-05-30 15:52:44 -070032import android.view.KeyEvent;
Adam Powelle43340c2014-03-17 19:10:43 -070033import android.view.LayoutInflater;
Adam Powell07a74542014-05-30 15:52:44 -070034import android.view.Menu;
35import android.view.MenuItem;
Adam Powelle43340c2014-03-17 19:10:43 -070036import android.view.View;
Adam Powell07a74542014-05-30 15:52:44 -070037import android.view.Window;
Adam Powell14e1afe2014-08-18 15:58:23 -070038import android.view.WindowCallbackWrapper;
Adam Powelle43340c2014-03-17 19:10:43 -070039import android.widget.SpinnerAdapter;
40import android.widget.Toolbar;
George Mount5beb26172015-12-15 13:36:01 -080041
Adam Powelle43340c2014-03-17 19:10:43 -070042import java.util.ArrayList;
Adam Powelle43340c2014-03-17 19:10:43 -070043
44public class ToolbarActionBar extends ActionBar {
Adam Powell07a74542014-05-30 15:52:44 -070045 private DecorToolbar mDecorToolbar;
Adam Powell14e1afe2014-08-18 15:58:23 -070046 private boolean mToolbarMenuPrepared;
Adam Powell07a74542014-05-30 15:52:44 -070047 private Window.Callback mWindowCallback;
Adam Powell04c0d462014-09-04 16:10:24 -070048 private boolean mMenuCallbackSet;
Adam Powelle43340c2014-03-17 19:10:43 -070049
50 private boolean mLastMenuVisibility;
51 private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
52 new ArrayList<OnMenuVisibilityListener>();
53
Adam Powell07a74542014-05-30 15:52:44 -070054 private final Runnable mMenuInvalidator = new Runnable() {
55 @Override
56 public void run() {
57 populateOptionsMenu();
58 }
59 };
60
61 private final Toolbar.OnMenuItemClickListener mMenuClicker =
62 new Toolbar.OnMenuItemClickListener() {
63 @Override
64 public boolean onMenuItemClick(MenuItem item) {
65 return mWindowCallback.onMenuItemSelected(Window.FEATURE_OPTIONS_PANEL, item);
66 }
67 };
68
69 public ToolbarActionBar(Toolbar toolbar, CharSequence title, Window.Callback windowCallback) {
Adam Powell2aa09a92014-07-19 16:01:12 -070070 mDecorToolbar = new ToolbarWidgetWrapper(toolbar, false);
Adam Powell14e1afe2014-08-18 15:58:23 -070071 mWindowCallback = new ToolbarCallbackWrapper(windowCallback);
Adam Powellb40484b2014-07-11 17:17:33 -070072 mDecorToolbar.setWindowCallback(mWindowCallback);
Adam Powell07a74542014-05-30 15:52:44 -070073 toolbar.setOnMenuItemClickListener(mMenuClicker);
74 mDecorToolbar.setWindowTitle(title);
Adam Powelle43340c2014-03-17 19:10:43 -070075 }
76
Adam Powell14e1afe2014-08-18 15:58:23 -070077 public Window.Callback getWrappedWindowCallback() {
78 return mWindowCallback;
79 }
80
Adam Powelle43340c2014-03-17 19:10:43 -070081 @Override
82 public void setCustomView(View view) {
83 setCustomView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
84 }
85
86 @Override
87 public void setCustomView(View view, LayoutParams layoutParams) {
Adam Powellc5ed3952015-06-15 14:11:42 -070088 if (view != null) {
89 view.setLayoutParams(layoutParams);
90 }
Adam Powell07a74542014-05-30 15:52:44 -070091 mDecorToolbar.setCustomView(view);
Adam Powelle43340c2014-03-17 19:10:43 -070092 }
93
94 @Override
95 public void setCustomView(int resId) {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -070096 final LayoutInflater inflater = LayoutInflater.from(mDecorToolbar.getContext());
97 setCustomView(inflater.inflate(resId, mDecorToolbar.getViewGroup(), false));
Adam Powelle43340c2014-03-17 19:10:43 -070098 }
99
100 @Override
101 public void setIcon(int resId) {
Adam Powell07a74542014-05-30 15:52:44 -0700102 mDecorToolbar.setIcon(resId);
Adam Powelle43340c2014-03-17 19:10:43 -0700103 }
104
105 @Override
106 public void setIcon(Drawable icon) {
Adam Powell07a74542014-05-30 15:52:44 -0700107 mDecorToolbar.setIcon(icon);
Adam Powelle43340c2014-03-17 19:10:43 -0700108 }
109
110 @Override
111 public void setLogo(int resId) {
Adam Powell07a74542014-05-30 15:52:44 -0700112 mDecorToolbar.setLogo(resId);
Adam Powelle43340c2014-03-17 19:10:43 -0700113 }
114
115 @Override
116 public void setLogo(Drawable logo) {
Adam Powell07a74542014-05-30 15:52:44 -0700117 mDecorToolbar.setLogo(logo);
Adam Powelle43340c2014-03-17 19:10:43 -0700118 }
119
120 @Override
121 public void setStackedBackgroundDrawable(Drawable d) {
122 // This space for rent (do nothing)
123 }
124
125 @Override
126 public void setSplitBackgroundDrawable(Drawable d) {
127 // This space for rent (do nothing)
128 }
129
130 @Override
131 public void setHomeButtonEnabled(boolean enabled) {
132 // If the nav button on a Toolbar is present, it's enabled. No-op.
133 }
134
135 @Override
Adam Powell14d1fa42014-07-10 16:23:39 -0700136 public void setElevation(float elevation) {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700137 mDecorToolbar.getViewGroup().setElevation(elevation);
Adam Powell14d1fa42014-07-10 16:23:39 -0700138 }
139
140 @Override
141 public float getElevation() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700142 return mDecorToolbar.getViewGroup().getElevation();
Adam Powell14d1fa42014-07-10 16:23:39 -0700143 }
144
145 @Override
Adam Powelle43340c2014-03-17 19:10:43 -0700146 public Context getThemedContext() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700147 return mDecorToolbar.getContext();
Adam Powelle43340c2014-03-17 19:10:43 -0700148 }
149
150 @Override
151 public boolean isTitleTruncated() {
152 return super.isTitleTruncated();
153 }
154
155 @Override
156 public void setHomeAsUpIndicator(Drawable indicator) {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700157 mDecorToolbar.setNavigationIcon(indicator);
Adam Powelle43340c2014-03-17 19:10:43 -0700158 }
159
160 @Override
161 public void setHomeAsUpIndicator(int resId) {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700162 mDecorToolbar.setNavigationIcon(resId);
Adam Powelle43340c2014-03-17 19:10:43 -0700163 }
164
165 @Override
166 public void setHomeActionContentDescription(CharSequence description) {
Yigit Boyare91f7c02014-09-10 17:12:28 -0700167 mDecorToolbar.setNavigationContentDescription(description);
Adam Powelle43340c2014-03-17 19:10:43 -0700168 }
169
170 @Override
171 public void setDefaultDisplayHomeAsUpEnabled(boolean enabled) {
172 // Do nothing
173 }
174
175 @Override
176 public void setHomeActionContentDescription(int resId) {
Yigit Boyare91f7c02014-09-10 17:12:28 -0700177 mDecorToolbar.setNavigationContentDescription(resId);
Adam Powelle43340c2014-03-17 19:10:43 -0700178 }
179
180 @Override
181 public void setShowHideAnimationEnabled(boolean enabled) {
182 // This space for rent; no-op.
183 }
184
185 @Override
186 public void onConfigurationChanged(Configuration config) {
187 super.onConfigurationChanged(config);
188 }
189
190 @Override
191 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell1dcedba2014-08-05 16:38:53 -0700192 return null;
Adam Powelle43340c2014-03-17 19:10:43 -0700193 }
194
195 @Override
196 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell6790b052014-06-02 13:30:11 -0700197 mDecorToolbar.setDropdownParams(adapter, new NavItemSelectedListener(callback));
Adam Powelle43340c2014-03-17 19:10:43 -0700198 }
199
200 @Override
201 public void setSelectedNavigationItem(int position) {
Adam Powell6790b052014-06-02 13:30:11 -0700202 switch (mDecorToolbar.getNavigationMode()) {
203 case NAVIGATION_MODE_LIST:
204 mDecorToolbar.setDropdownSelectedPosition(position);
205 break;
206 default:
207 throw new IllegalStateException(
208 "setSelectedNavigationIndex not valid for current navigation mode");
209 }
Adam Powelle43340c2014-03-17 19:10:43 -0700210 }
211
212 @Override
213 public int getSelectedNavigationIndex() {
214 return -1;
215 }
216
217 @Override
218 public int getNavigationItemCount() {
219 return 0;
220 }
221
222 @Override
223 public void setTitle(CharSequence title) {
Adam Powell07a74542014-05-30 15:52:44 -0700224 mDecorToolbar.setTitle(title);
Adam Powelle43340c2014-03-17 19:10:43 -0700225 }
226
227 @Override
228 public void setTitle(int resId) {
Adam Powell07a74542014-05-30 15:52:44 -0700229 mDecorToolbar.setTitle(resId != 0 ? mDecorToolbar.getContext().getText(resId) : null);
Adam Powelle43340c2014-03-17 19:10:43 -0700230 }
231
232 @Override
Adam Powellaf2d8592014-08-26 18:06:40 -0700233 public void setWindowTitle(CharSequence title) {
234 mDecorToolbar.setWindowTitle(title);
235 }
236
237 @Override
Adam Powelle43340c2014-03-17 19:10:43 -0700238 public void setSubtitle(CharSequence subtitle) {
Adam Powell07a74542014-05-30 15:52:44 -0700239 mDecorToolbar.setSubtitle(subtitle);
Adam Powelle43340c2014-03-17 19:10:43 -0700240 }
241
242 @Override
243 public void setSubtitle(int resId) {
Adam Powell07a74542014-05-30 15:52:44 -0700244 mDecorToolbar.setSubtitle(resId != 0 ? mDecorToolbar.getContext().getText(resId) : null);
Adam Powelle43340c2014-03-17 19:10:43 -0700245 }
246
247 @Override
248 public void setDisplayOptions(@DisplayOptions int options) {
249 setDisplayOptions(options, 0xffffffff);
250 }
251
252 @Override
253 public void setDisplayOptions(@DisplayOptions int options, @DisplayOptions int mask) {
Adam Powell12b03212014-09-02 15:59:28 -0700254 final int currentOptions = mDecorToolbar.getDisplayOptions();
Adam Powell12b03212014-09-02 15:59:28 -0700255 mDecorToolbar.setDisplayOptions(options & mask | currentOptions & ~mask);
Adam Powelle43340c2014-03-17 19:10:43 -0700256 }
257
258 @Override
259 public void setDisplayUseLogoEnabled(boolean useLogo) {
260 setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
261 }
262
263 @Override
264 public void setDisplayShowHomeEnabled(boolean showHome) {
265 setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
266 }
267
268 @Override
269 public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
270 setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
271 }
272
273 @Override
274 public void setDisplayShowTitleEnabled(boolean showTitle) {
275 setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
276 }
277
278 @Override
279 public void setDisplayShowCustomEnabled(boolean showCustom) {
280 setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
281 }
282
283 @Override
284 public void setBackgroundDrawable(@Nullable Drawable d) {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700285 mDecorToolbar.setBackgroundDrawable(d);
Adam Powelle43340c2014-03-17 19:10:43 -0700286 }
287
288 @Override
289 public View getCustomView() {
Adam Powell07a74542014-05-30 15:52:44 -0700290 return mDecorToolbar.getCustomView();
Adam Powelle43340c2014-03-17 19:10:43 -0700291 }
292
293 @Override
294 public CharSequence getTitle() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700295 return mDecorToolbar.getTitle();
Adam Powelle43340c2014-03-17 19:10:43 -0700296 }
297
298 @Override
299 public CharSequence getSubtitle() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700300 return mDecorToolbar.getSubtitle();
Adam Powelle43340c2014-03-17 19:10:43 -0700301 }
302
303 @Override
304 public int getNavigationMode() {
305 return NAVIGATION_MODE_STANDARD;
306 }
307
308 @Override
309 public void setNavigationMode(@NavigationMode int mode) {
Adam Powelle002c2f2014-06-03 17:54:34 -0700310 if (mode == ActionBar.NAVIGATION_MODE_TABS) {
311 throw new IllegalArgumentException("Tabs not supported in this configuration");
312 }
Adam Powell6790b052014-06-02 13:30:11 -0700313 mDecorToolbar.setNavigationMode(mode);
Adam Powelle43340c2014-03-17 19:10:43 -0700314 }
315
316 @Override
317 public int getDisplayOptions() {
Adam Powell07a74542014-05-30 15:52:44 -0700318 return mDecorToolbar.getDisplayOptions();
Adam Powelle43340c2014-03-17 19:10:43 -0700319 }
320
321 @Override
322 public Tab newTab() {
323 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700324 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700325 }
326
327 @Override
328 public void addTab(Tab tab) {
329 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700330 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700331 }
332
333 @Override
334 public void addTab(Tab tab, boolean setSelected) {
335 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700336 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700337 }
338
339 @Override
340 public void addTab(Tab tab, int position) {
341 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700342 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700343 }
344
345 @Override
346 public void addTab(Tab tab, int position, boolean setSelected) {
347 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700348 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700349 }
350
351 @Override
352 public void removeTab(Tab tab) {
353 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700354 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700355 }
356
357 @Override
358 public void removeTabAt(int position) {
359 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700360 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700361 }
362
363 @Override
364 public void removeAllTabs() {
365 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700366 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700367 }
368
369 @Override
370 public void selectTab(Tab tab) {
371 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700372 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700373 }
374
375 @Override
376 public Tab getSelectedTab() {
377 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700378 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700379 }
380
381 @Override
382 public Tab getTabAt(int index) {
383 throw new UnsupportedOperationException(
Adam Powell6790b052014-06-02 13:30:11 -0700384 "Tabs are not supported in toolbar action bars");
Adam Powelle43340c2014-03-17 19:10:43 -0700385 }
386
387 @Override
388 public int getTabCount() {
389 return 0;
390 }
391
392 @Override
393 public int getHeight() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700394 return mDecorToolbar.getHeight();
Adam Powelle43340c2014-03-17 19:10:43 -0700395 }
396
397 @Override
398 public void show() {
399 // TODO: Consider a better transition for this.
400 // Right now use no automatic transition so that the app can supply one if desired.
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700401 mDecorToolbar.setVisibility(View.VISIBLE);
Adam Powelle43340c2014-03-17 19:10:43 -0700402 }
403
404 @Override
405 public void hide() {
406 // TODO: Consider a better transition for this.
407 // Right now use no automatic transition so that the app can supply one if desired.
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700408 mDecorToolbar.setVisibility(View.GONE);
Adam Powelle43340c2014-03-17 19:10:43 -0700409 }
410
411 @Override
412 public boolean isShowing() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700413 return mDecorToolbar.getVisibility() == View.VISIBLE;
Adam Powelle43340c2014-03-17 19:10:43 -0700414 }
415
Adam Powell07a74542014-05-30 15:52:44 -0700416 @Override
417 public boolean openOptionsMenu() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700418 return mDecorToolbar.showOverflowMenu();
Adam Powell07a74542014-05-30 15:52:44 -0700419 }
420
421 @Override
Evan Rosky41823d12017-03-21 14:42:35 -0700422 public boolean closeOptionsMenu() {
423 return mDecorToolbar.hideOverflowMenu();
424 }
425
426 @Override
Adam Powell07a74542014-05-30 15:52:44 -0700427 public boolean invalidateOptionsMenu() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700428 mDecorToolbar.getViewGroup().removeCallbacks(mMenuInvalidator);
429 mDecorToolbar.getViewGroup().postOnAnimation(mMenuInvalidator);
Adam Powell07a74542014-05-30 15:52:44 -0700430 return true;
431 }
432
433 @Override
434 public boolean collapseActionView() {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700435 if (mDecorToolbar.hasExpandedActionView()) {
436 mDecorToolbar.collapseActionView();
Adam Powell07a74542014-05-30 15:52:44 -0700437 return true;
438 }
439 return false;
440 }
441
442 void populateOptionsMenu() {
Adam Powell04c0d462014-09-04 16:10:24 -0700443 if (!mMenuCallbackSet) {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700444 mDecorToolbar.setMenuCallbacks(new ActionMenuPresenterCallback(), new MenuBuilderCallback());
Adam Powell04c0d462014-09-04 16:10:24 -0700445 mMenuCallbackSet = true;
446 }
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700447 final Menu menu = mDecorToolbar.getMenu();
Adam Powell07a74542014-05-30 15:52:44 -0700448 final MenuBuilder mb = menu instanceof MenuBuilder ? (MenuBuilder) menu : null;
449 if (mb != null) {
450 mb.stopDispatchingItemsChanged();
451 }
452 try {
453 menu.clear();
454 if (!mWindowCallback.onCreatePanelMenu(Window.FEATURE_OPTIONS_PANEL, menu) ||
455 !mWindowCallback.onPreparePanel(Window.FEATURE_OPTIONS_PANEL, null, menu)) {
456 menu.clear();
457 }
458 } finally {
459 if (mb != null) {
460 mb.startDispatchingItemsChanged();
461 }
462 }
463 }
464
465 @Override
466 public boolean onMenuKeyEvent(KeyEvent event) {
467 if (event.getAction() == KeyEvent.ACTION_UP) {
468 openOptionsMenu();
469 }
470 return true;
471 }
472
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -0700473 @Override
474 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
475 Menu menu = mDecorToolbar.getMenu();
476 if (menu != null) {
Alan Viverette36a5c8d2015-08-26 12:48:17 -0400477 final KeyCharacterMap kmap = KeyCharacterMap.load(
478 event != null ? event.getDeviceId() : KeyCharacterMap.VIRTUAL_KEYBOARD);
479 menu.setQwertyMode(kmap.getKeyboardType() != KeyCharacterMap.NUMERIC);
Evan Roskyb22faf52017-02-17 13:11:53 -0800480 return menu.performShortcut(keyCode, event, 0);
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -0700481 }
Evan Roskyb22faf52017-02-17 13:11:53 -0800482 return false;
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -0700483 }
484
Chris Banes21b25772016-01-04 20:41:59 +0000485 @Override
486 public void onDestroy() {
487 // Remove any invalidation callbacks
488 mDecorToolbar.getViewGroup().removeCallbacks(mMenuInvalidator);
489 }
490
Adam Powelle43340c2014-03-17 19:10:43 -0700491 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
492 mMenuVisibilityListeners.add(listener);
493 }
494
495 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
496 mMenuVisibilityListeners.remove(listener);
497 }
498
499 public void dispatchMenuVisibilityChanged(boolean isVisible) {
500 if (isVisible == mLastMenuVisibility) {
501 return;
502 }
503 mLastMenuVisibility = isVisible;
504
505 final int count = mMenuVisibilityListeners.size();
506 for (int i = 0; i < count; i++) {
507 mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
508 }
509 }
Adam Powell14e1afe2014-08-18 15:58:23 -0700510
511 private class ToolbarCallbackWrapper extends WindowCallbackWrapper {
512 public ToolbarCallbackWrapper(Window.Callback wrapped) {
513 super(wrapped);
514 }
515
516 @Override
517 public boolean onPreparePanel(int featureId, View view, Menu menu) {
518 final boolean result = super.onPreparePanel(featureId, view, menu);
519 if (result && !mToolbarMenuPrepared) {
520 mDecorToolbar.setMenuPrepared();
521 mToolbarMenuPrepared = true;
522 }
523 return result;
524 }
Evan Roskyb22faf52017-02-17 13:11:53 -0800525
526 @Override
527 public View onCreatePanelView(int featureId) {
528 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
529 // This gets called by PhoneWindow.preparePanel. Since this already manages
530 // its own panel, we return a dummy view here to prevent PhoneWindow from
531 // preparing a default one.
532 return new View(mDecorToolbar.getContext());
533 }
534 return super.onCreatePanelView(featureId);
535 }
Adam Powell14e1afe2014-08-18 15:58:23 -0700536 }
Adam Powell04c0d462014-09-04 16:10:24 -0700537
538 private final class ActionMenuPresenterCallback implements MenuPresenter.Callback {
539 private boolean mClosingActionMenu;
540
541 @Override
542 public boolean onOpenSubMenu(MenuBuilder subMenu) {
543 if (mWindowCallback != null) {
544 mWindowCallback.onMenuOpened(Window.FEATURE_ACTION_BAR, subMenu);
545 return true;
546 }
547 return false;
548 }
549
550 @Override
551 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
552 if (mClosingActionMenu) {
553 return;
554 }
555
556 mClosingActionMenu = true;
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700557 mDecorToolbar.dismissPopupMenus();
Adam Powell04c0d462014-09-04 16:10:24 -0700558 if (mWindowCallback != null) {
559 mWindowCallback.onPanelClosed(Window.FEATURE_ACTION_BAR, menu);
560 }
561 mClosingActionMenu = false;
562 }
563 }
Adam Powellc4612502014-09-04 19:16:39 -0700564
565 private final class MenuBuilderCallback implements MenuBuilder.Callback {
566
567 @Override
568 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
569 return false;
570 }
571
572 @Override
573 public void onMenuModeChange(MenuBuilder menu) {
574 if (mWindowCallback != null) {
Yigit Boyarfe6d57c2014-10-24 11:33:36 -0700575 if (mDecorToolbar.isOverflowMenuShowing()) {
Adam Powellc4612502014-09-04 19:16:39 -0700576 mWindowCallback.onPanelClosed(Window.FEATURE_ACTION_BAR, menu);
577 } else if (mWindowCallback.onPreparePanel(Window.FEATURE_OPTIONS_PANEL,
578 null, menu)) {
579 mWindowCallback.onMenuOpened(Window.FEATURE_ACTION_BAR, menu);
580 }
581 }
582 }
583 }
Adam Powelle43340c2014-03-17 19:10:43 -0700584}