Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 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 | |
| 18 | package com.android.internal.app; |
| 19 | |
Vadim Tryshev | 1abe85c | 2017-01-06 19:30:00 -0800 | [diff] [blame] | 20 | import com.android.internal.view.menu.MenuBuilder; |
| 21 | import com.android.internal.view.menu.MenuPresenter; |
| 22 | import com.android.internal.widget.DecorToolbar; |
| 23 | import com.android.internal.widget.ToolbarWidgetWrapper; |
| 24 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 25 | import android.annotation.Nullable; |
| 26 | import android.app.ActionBar; |
| 27 | import android.content.Context; |
| 28 | import android.content.res.Configuration; |
| 29 | import android.graphics.drawable.Drawable; |
| 30 | import android.view.ActionMode; |
Alan Viverette | 36a5c8d | 2015-08-26 12:48:17 -0400 | [diff] [blame] | 31 | import android.view.KeyCharacterMap; |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 32 | import android.view.KeyEvent; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 34 | import android.view.Menu; |
| 35 | import android.view.MenuItem; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 36 | import android.view.View; |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 37 | import android.view.Window; |
Adam Powell | 14e1afe | 2014-08-18 15:58:23 -0700 | [diff] [blame] | 38 | import android.view.WindowCallbackWrapper; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 39 | import android.widget.SpinnerAdapter; |
| 40 | import android.widget.Toolbar; |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 41 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 43 | |
| 44 | public class ToolbarActionBar extends ActionBar { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 45 | private DecorToolbar mDecorToolbar; |
Adam Powell | 14e1afe | 2014-08-18 15:58:23 -0700 | [diff] [blame] | 46 | private boolean mToolbarMenuPrepared; |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 47 | private Window.Callback mWindowCallback; |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 48 | private boolean mMenuCallbackSet; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 49 | |
| 50 | private boolean mLastMenuVisibility; |
| 51 | private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners = |
| 52 | new ArrayList<OnMenuVisibilityListener>(); |
| 53 | |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 54 | 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 Powell | 2aa09a9 | 2014-07-19 16:01:12 -0700 | [diff] [blame] | 70 | mDecorToolbar = new ToolbarWidgetWrapper(toolbar, false); |
Adam Powell | 14e1afe | 2014-08-18 15:58:23 -0700 | [diff] [blame] | 71 | mWindowCallback = new ToolbarCallbackWrapper(windowCallback); |
Adam Powell | b40484b | 2014-07-11 17:17:33 -0700 | [diff] [blame] | 72 | mDecorToolbar.setWindowCallback(mWindowCallback); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 73 | toolbar.setOnMenuItemClickListener(mMenuClicker); |
| 74 | mDecorToolbar.setWindowTitle(title); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Adam Powell | 14e1afe | 2014-08-18 15:58:23 -0700 | [diff] [blame] | 77 | public Window.Callback getWrappedWindowCallback() { |
| 78 | return mWindowCallback; |
| 79 | } |
| 80 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 81 | @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 Powell | c5ed395 | 2015-06-15 14:11:42 -0700 | [diff] [blame] | 88 | if (view != null) { |
| 89 | view.setLayoutParams(layoutParams); |
| 90 | } |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 91 | mDecorToolbar.setCustomView(view); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void setCustomView(int resId) { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 96 | final LayoutInflater inflater = LayoutInflater.from(mDecorToolbar.getContext()); |
| 97 | setCustomView(inflater.inflate(resId, mDecorToolbar.getViewGroup(), false)); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void setIcon(int resId) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 102 | mDecorToolbar.setIcon(resId); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void setIcon(Drawable icon) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 107 | mDecorToolbar.setIcon(icon); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void setLogo(int resId) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 112 | mDecorToolbar.setLogo(resId); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void setLogo(Drawable logo) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 117 | mDecorToolbar.setLogo(logo); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 118 | } |
| 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 Powell | 14d1fa4 | 2014-07-10 16:23:39 -0700 | [diff] [blame] | 136 | public void setElevation(float elevation) { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 137 | mDecorToolbar.getViewGroup().setElevation(elevation); |
Adam Powell | 14d1fa4 | 2014-07-10 16:23:39 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public float getElevation() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 142 | return mDecorToolbar.getViewGroup().getElevation(); |
Adam Powell | 14d1fa4 | 2014-07-10 16:23:39 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 146 | public Context getThemedContext() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 147 | return mDecorToolbar.getContext(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean isTitleTruncated() { |
| 152 | return super.isTitleTruncated(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void setHomeAsUpIndicator(Drawable indicator) { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 157 | mDecorToolbar.setNavigationIcon(indicator); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void setHomeAsUpIndicator(int resId) { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 162 | mDecorToolbar.setNavigationIcon(resId); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void setHomeActionContentDescription(CharSequence description) { |
Yigit Boyar | e91f7c0 | 2014-09-10 17:12:28 -0700 | [diff] [blame] | 167 | mDecorToolbar.setNavigationContentDescription(description); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void setDefaultDisplayHomeAsUpEnabled(boolean enabled) { |
| 172 | // Do nothing |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void setHomeActionContentDescription(int resId) { |
Yigit Boyar | e91f7c0 | 2014-09-10 17:12:28 -0700 | [diff] [blame] | 177 | mDecorToolbar.setNavigationContentDescription(resId); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 178 | } |
| 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 Powell | 1dcedba | 2014-08-05 16:38:53 -0700 | [diff] [blame] | 192 | return null; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) { |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 197 | mDecorToolbar.setDropdownParams(adapter, new NavItemSelectedListener(callback)); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void setSelectedNavigationItem(int position) { |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 202 | 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 Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 210 | } |
| 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 Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 224 | mDecorToolbar.setTitle(title); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void setTitle(int resId) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 229 | mDecorToolbar.setTitle(resId != 0 ? mDecorToolbar.getContext().getText(resId) : null); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
Adam Powell | af2d859 | 2014-08-26 18:06:40 -0700 | [diff] [blame] | 233 | public void setWindowTitle(CharSequence title) { |
| 234 | mDecorToolbar.setWindowTitle(title); |
| 235 | } |
| 236 | |
| 237 | @Override |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 238 | public void setSubtitle(CharSequence subtitle) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 239 | mDecorToolbar.setSubtitle(subtitle); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void setSubtitle(int resId) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 244 | mDecorToolbar.setSubtitle(resId != 0 ? mDecorToolbar.getContext().getText(resId) : null); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 245 | } |
| 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 Powell | 12b0321 | 2014-09-02 15:59:28 -0700 | [diff] [blame] | 254 | final int currentOptions = mDecorToolbar.getDisplayOptions(); |
Adam Powell | 12b0321 | 2014-09-02 15:59:28 -0700 | [diff] [blame] | 255 | mDecorToolbar.setDisplayOptions(options & mask | currentOptions & ~mask); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 256 | } |
| 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 Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 285 | mDecorToolbar.setBackgroundDrawable(d); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public View getCustomView() { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 290 | return mDecorToolbar.getCustomView(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public CharSequence getTitle() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 295 | return mDecorToolbar.getTitle(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public CharSequence getSubtitle() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 300 | return mDecorToolbar.getSubtitle(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public int getNavigationMode() { |
| 305 | return NAVIGATION_MODE_STANDARD; |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void setNavigationMode(@NavigationMode int mode) { |
Adam Powell | e002c2f | 2014-06-03 17:54:34 -0700 | [diff] [blame] | 310 | if (mode == ActionBar.NAVIGATION_MODE_TABS) { |
| 311 | throw new IllegalArgumentException("Tabs not supported in this configuration"); |
| 312 | } |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 313 | mDecorToolbar.setNavigationMode(mode); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public int getDisplayOptions() { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 318 | return mDecorToolbar.getDisplayOptions(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public Tab newTab() { |
| 323 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 324 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void addTab(Tab tab) { |
| 329 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 330 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void addTab(Tab tab, boolean setSelected) { |
| 335 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 336 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public void addTab(Tab tab, int position) { |
| 341 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 342 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void addTab(Tab tab, int position, boolean setSelected) { |
| 347 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 348 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void removeTab(Tab tab) { |
| 353 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 354 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public void removeTabAt(int position) { |
| 359 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 360 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public void removeAllTabs() { |
| 365 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 366 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public void selectTab(Tab tab) { |
| 371 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 372 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public Tab getSelectedTab() { |
| 377 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 378 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public Tab getTabAt(int index) { |
| 383 | throw new UnsupportedOperationException( |
Adam Powell | 6790b05 | 2014-06-02 13:30:11 -0700 | [diff] [blame] | 384 | "Tabs are not supported in toolbar action bars"); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public int getTabCount() { |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public int getHeight() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 394 | return mDecorToolbar.getHeight(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 395 | } |
| 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 Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 401 | mDecorToolbar.setVisibility(View.VISIBLE); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 402 | } |
| 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 Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 408 | mDecorToolbar.setVisibility(View.GONE); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public boolean isShowing() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 413 | return mDecorToolbar.getVisibility() == View.VISIBLE; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 416 | @Override |
| 417 | public boolean openOptionsMenu() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 418 | return mDecorToolbar.showOverflowMenu(); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public boolean invalidateOptionsMenu() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 423 | mDecorToolbar.getViewGroup().removeCallbacks(mMenuInvalidator); |
| 424 | mDecorToolbar.getViewGroup().postOnAnimation(mMenuInvalidator); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public boolean collapseActionView() { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 430 | if (mDecorToolbar.hasExpandedActionView()) { |
| 431 | mDecorToolbar.collapseActionView(); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 432 | return true; |
| 433 | } |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | void populateOptionsMenu() { |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 438 | if (!mMenuCallbackSet) { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 439 | mDecorToolbar.setMenuCallbacks(new ActionMenuPresenterCallback(), new MenuBuilderCallback()); |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 440 | mMenuCallbackSet = true; |
| 441 | } |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 442 | final Menu menu = mDecorToolbar.getMenu(); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 443 | final MenuBuilder mb = menu instanceof MenuBuilder ? (MenuBuilder) menu : null; |
| 444 | if (mb != null) { |
| 445 | mb.stopDispatchingItemsChanged(); |
| 446 | } |
| 447 | try { |
| 448 | menu.clear(); |
| 449 | if (!mWindowCallback.onCreatePanelMenu(Window.FEATURE_OPTIONS_PANEL, menu) || |
| 450 | !mWindowCallback.onPreparePanel(Window.FEATURE_OPTIONS_PANEL, null, menu)) { |
| 451 | menu.clear(); |
| 452 | } |
| 453 | } finally { |
| 454 | if (mb != null) { |
| 455 | mb.startDispatchingItemsChanged(); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public boolean onMenuKeyEvent(KeyEvent event) { |
| 462 | if (event.getAction() == KeyEvent.ACTION_UP) { |
| 463 | openOptionsMenu(); |
| 464 | } |
| 465 | return true; |
| 466 | } |
| 467 | |
Abodunrinwa Toki | a04b7ad | 2015-06-30 17:44:04 -0700 | [diff] [blame] | 468 | @Override |
| 469 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
| 470 | Menu menu = mDecorToolbar.getMenu(); |
| 471 | if (menu != null) { |
Alan Viverette | 36a5c8d | 2015-08-26 12:48:17 -0400 | [diff] [blame] | 472 | final KeyCharacterMap kmap = KeyCharacterMap.load( |
| 473 | event != null ? event.getDeviceId() : KeyCharacterMap.VIRTUAL_KEYBOARD); |
| 474 | menu.setQwertyMode(kmap.getKeyboardType() != KeyCharacterMap.NUMERIC); |
Abodunrinwa Toki | a04b7ad | 2015-06-30 17:44:04 -0700 | [diff] [blame] | 475 | menu.performShortcut(keyCode, event, 0); |
| 476 | } |
| 477 | // This action bar always returns true for handling keyboard shortcuts. |
| 478 | // This will block the window from preparing a temporary panel to handle |
| 479 | // keyboard shortcuts. |
| 480 | return true; |
| 481 | } |
| 482 | |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 483 | @Override |
| 484 | public void onDestroy() { |
| 485 | // Remove any invalidation callbacks |
| 486 | mDecorToolbar.getViewGroup().removeCallbacks(mMenuInvalidator); |
| 487 | } |
| 488 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 489 | public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) { |
| 490 | mMenuVisibilityListeners.add(listener); |
| 491 | } |
| 492 | |
| 493 | public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) { |
| 494 | mMenuVisibilityListeners.remove(listener); |
| 495 | } |
| 496 | |
| 497 | public void dispatchMenuVisibilityChanged(boolean isVisible) { |
| 498 | if (isVisible == mLastMenuVisibility) { |
| 499 | return; |
| 500 | } |
| 501 | mLastMenuVisibility = isVisible; |
| 502 | |
| 503 | final int count = mMenuVisibilityListeners.size(); |
| 504 | for (int i = 0; i < count; i++) { |
| 505 | mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible); |
| 506 | } |
| 507 | } |
Adam Powell | 14e1afe | 2014-08-18 15:58:23 -0700 | [diff] [blame] | 508 | |
| 509 | private class ToolbarCallbackWrapper extends WindowCallbackWrapper { |
| 510 | public ToolbarCallbackWrapper(Window.Callback wrapped) { |
| 511 | super(wrapped); |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 516 | final boolean result = super.onPreparePanel(featureId, view, menu); |
| 517 | if (result && !mToolbarMenuPrepared) { |
| 518 | mDecorToolbar.setMenuPrepared(); |
| 519 | mToolbarMenuPrepared = true; |
| 520 | } |
| 521 | return result; |
| 522 | } |
| 523 | } |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 524 | |
| 525 | private final class ActionMenuPresenterCallback implements MenuPresenter.Callback { |
| 526 | private boolean mClosingActionMenu; |
| 527 | |
| 528 | @Override |
| 529 | public boolean onOpenSubMenu(MenuBuilder subMenu) { |
| 530 | if (mWindowCallback != null) { |
| 531 | mWindowCallback.onMenuOpened(Window.FEATURE_ACTION_BAR, subMenu); |
| 532 | return true; |
| 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 539 | if (mClosingActionMenu) { |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | mClosingActionMenu = true; |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 544 | mDecorToolbar.dismissPopupMenus(); |
Adam Powell | 04c0d46 | 2014-09-04 16:10:24 -0700 | [diff] [blame] | 545 | if (mWindowCallback != null) { |
| 546 | mWindowCallback.onPanelClosed(Window.FEATURE_ACTION_BAR, menu); |
| 547 | } |
| 548 | mClosingActionMenu = false; |
| 549 | } |
| 550 | } |
Adam Powell | c461250 | 2014-09-04 19:16:39 -0700 | [diff] [blame] | 551 | |
| 552 | private final class MenuBuilderCallback implements MenuBuilder.Callback { |
| 553 | |
| 554 | @Override |
| 555 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public void onMenuModeChange(MenuBuilder menu) { |
| 561 | if (mWindowCallback != null) { |
Yigit Boyar | fe6d57c | 2014-10-24 11:33:36 -0700 | [diff] [blame] | 562 | if (mDecorToolbar.isOverflowMenuShowing()) { |
Adam Powell | c461250 | 2014-09-04 19:16:39 -0700 | [diff] [blame] | 563 | mWindowCallback.onPanelClosed(Window.FEATURE_ACTION_BAR, menu); |
| 564 | } else if (mWindowCallback.onPreparePanel(Window.FEATURE_OPTIONS_PANEL, |
| 565 | null, menu)) { |
| 566 | mWindowCallback.onMenuOpened(Window.FEATURE_ACTION_BAR, menu); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 571 | } |