blob: 7cf369f4993a14543c97224dd3fab4c5472e16af [file] [log] [blame]
Adam Powell89e06452010-06-23 20:24:52 -07001/*
2 * Copyright (C) 2010 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
17package com.android.internal.app;
18
Adam Powell2c9c9fe2010-07-16 10:17:57 -070019import com.android.internal.view.menu.MenuBuilder;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070020import com.android.internal.view.menu.MenuPopupHelper;
21import com.android.internal.view.menu.SubMenuBuilder;
Adam Powell89e06452010-06-23 20:24:52 -070022import com.android.internal.widget.ActionBarContextView;
23import com.android.internal.widget.ActionBarView;
24
25import android.app.ActionBar;
Adam Powell661c9082010-07-02 10:09:44 -070026import android.app.Activity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070027import android.app.Dialog;
Adam Powell661c9082010-07-02 10:09:44 -070028import android.app.Fragment;
29import android.app.FragmentTransaction;
Adam Powelldec9dfd2010-08-09 15:27:54 -070030import android.content.Context;
Adam Powell89e06452010-06-23 20:24:52 -070031import android.graphics.drawable.Drawable;
Adam Powell0e94b512010-06-29 17:58:20 -070032import android.os.Handler;
Adam Powell6e346362010-07-23 10:18:23 -070033import android.view.ActionMode;
Adam Powell89e06452010-06-23 20:24:52 -070034import android.view.Menu;
Adam Powell9168f0b2010-08-02 15:46:24 -070035import android.view.MenuInflater;
Adam Powell89e06452010-06-23 20:24:52 -070036import android.view.MenuItem;
37import android.view.View;
38import android.widget.LinearLayout;
39import android.widget.SpinnerAdapter;
40import android.widget.ViewAnimator;
41
Adam Powell29ed7572010-07-14 16:24:56 -070042import java.lang.ref.WeakReference;
Adam Powell661c9082010-07-02 10:09:44 -070043import java.util.ArrayList;
44
Adam Powell89e06452010-06-23 20:24:52 -070045/**
46 * ActionBarImpl is the ActionBar implementation used
47 * by devices of all screen sizes. If it detects a compatible decor,
48 * it will split contextual modes across both the ActionBarView at
49 * the top of the screen and a horizontal LinearLayout at the bottom
50 * which is normally hidden.
51 */
52public class ActionBarImpl extends ActionBar {
53 private static final int NORMAL_VIEW = 0;
54 private static final int CONTEXT_VIEW = 1;
Adam Powell661c9082010-07-02 10:09:44 -070055
Adam Powelldec9dfd2010-08-09 15:27:54 -070056 private Context mContext;
Adam Powell661c9082010-07-02 10:09:44 -070057 private Activity mActivity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070058 private Dialog mDialog;
Adam Powell661c9082010-07-02 10:09:44 -070059
Adam Powell89e06452010-06-23 20:24:52 -070060 private ViewAnimator mAnimatorView;
61 private ActionBarView mActionView;
62 private ActionBarContextView mUpperContextView;
63 private LinearLayout mLowerContextView;
Adam Powell661c9082010-07-02 10:09:44 -070064
65 private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>();
66
Adam Powell661c9082010-07-02 10:09:44 -070067 private TabImpl mSelectedTab;
Adam Powell0c24a552010-11-03 16:44:11 -070068 private int mSavedTabPosition = INVALID_POSITION;
Adam Powell89e06452010-06-23 20:24:52 -070069
Adam Powell5d279772010-07-27 16:34:07 -070070 private ActionMode mActionMode;
Adam Powell89e06452010-06-23 20:24:52 -070071
72 private static final int CONTEXT_DISPLAY_NORMAL = 0;
73 private static final int CONTEXT_DISPLAY_SPLIT = 1;
74
Adam Powell0c24a552010-11-03 16:44:11 -070075 private static final int INVALID_POSITION = -1;
76
Adam Powell89e06452010-06-23 20:24:52 -070077 private int mContextDisplayMode;
Adam Powell0e94b512010-06-29 17:58:20 -070078
Adam Powell3461b322010-07-14 11:34:43 -070079 private boolean mClosingContext;
80
Adam Powell0e94b512010-06-29 17:58:20 -070081 final Handler mHandler = new Handler();
82 final Runnable mCloseContext = new Runnable() {
83 public void run() {
84 mUpperContextView.closeMode();
85 if (mLowerContextView != null) {
86 mLowerContextView.removeAllViews();
87 }
Adam Powell3461b322010-07-14 11:34:43 -070088 mClosingContext = false;
Adam Powell0e94b512010-06-29 17:58:20 -070089 }
90 };
91
Adam Powell661c9082010-07-02 10:09:44 -070092 public ActionBarImpl(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -070093 mActivity = activity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070094 init(activity.getWindow().getDecorView());
95 }
96
97 public ActionBarImpl(Dialog dialog) {
98 mDialog = dialog;
99 init(dialog.getWindow().getDecorView());
100 }
101
102 private void init(View decor) {
103 mContext = decor.getContext();
Adam Powell89e06452010-06-23 20:24:52 -0700104 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
105 mUpperContextView = (ActionBarContextView) decor.findViewById(
106 com.android.internal.R.id.action_context_bar);
107 mLowerContextView = (LinearLayout) decor.findViewById(
108 com.android.internal.R.id.lower_action_context_bar);
109 mAnimatorView = (ViewAnimator) decor.findViewById(
110 com.android.internal.R.id.action_bar_animator);
Steve Block08f194b2010-08-24 18:20:48 +0100111
Adam Powell89e06452010-06-23 20:24:52 -0700112 if (mActionView == null || mUpperContextView == null || mAnimatorView == null) {
113 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
114 "with a compatible window decor layout");
115 }
Adam Powell0e94b512010-06-29 17:58:20 -0700116
Steve Block08f194b2010-08-24 18:20:48 +0100117 mActionView.setContextView(mUpperContextView);
Adam Powell89e06452010-06-23 20:24:52 -0700118 mContextDisplayMode = mLowerContextView == null ?
119 CONTEXT_DISPLAY_NORMAL : CONTEXT_DISPLAY_SPLIT;
120 }
121
Adam Powella66c7b02010-07-28 15:28:25 -0700122 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700123 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700124 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700125 }
126
127 @Override
128 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700129 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700130 }
131
Adam Powell89e06452010-06-23 20:24:52 -0700132 public void setCustomNavigationMode(View view) {
Adam Powell661c9082010-07-02 10:09:44 -0700133 cleanupTabs();
Adam Powell9ab97872010-10-26 21:47:29 -0700134 setCustomView(view);
135 setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM | DISPLAY_SHOW_TITLE);
136 mActionView.setNavigationMode(NAVIGATION_MODE_STANDARD);
Adam Powell89e06452010-06-23 20:24:52 -0700137 mActionView.setCallback(null);
138 }
Adam Powell0e94b512010-06-29 17:58:20 -0700139
Adam Powell89e06452010-06-23 20:24:52 -0700140 public void setDropdownNavigationMode(SpinnerAdapter adapter, NavigationCallback callback) {
Adam Powell17809772010-07-21 13:25:11 -0700141 setDropdownNavigationMode(adapter, callback, -1);
142 }
143
144 public void setDropdownNavigationMode(SpinnerAdapter adapter, NavigationCallback callback,
145 int defaultSelectedPosition) {
Adam Powell661c9082010-07-02 10:09:44 -0700146 cleanupTabs();
Adam Powell9ab97872010-10-26 21:47:29 -0700147 setDisplayOptions(0, DISPLAY_SHOW_CUSTOM | DISPLAY_SHOW_TITLE);
148 mActionView.setNavigationMode(NAVIGATION_MODE_LIST);
149 setListNavigationCallbacks(adapter, callback);
Adam Powell17809772010-07-21 13:25:11 -0700150 if (defaultSelectedPosition >= 0) {
151 mActionView.setDropdownSelectedPosition(defaultSelectedPosition);
152 }
Adam Powell89e06452010-06-23 20:24:52 -0700153 }
Adam Powell0e94b512010-06-29 17:58:20 -0700154
155 public void setStandardNavigationMode() {
Adam Powell661c9082010-07-02 10:09:44 -0700156 cleanupTabs();
Adam Powell9ab97872010-10-26 21:47:29 -0700157 setDisplayOptions(DISPLAY_SHOW_TITLE, DISPLAY_SHOW_TITLE | DISPLAY_SHOW_CUSTOM);
Adam Powell0e94b512010-06-29 17:58:20 -0700158 mActionView.setNavigationMode(NAVIGATION_MODE_STANDARD);
159 mActionView.setCallback(null);
160 }
161
Adam Powell17809772010-07-21 13:25:11 -0700162 public void setSelectedNavigationItem(int position) {
163 switch (mActionView.getNavigationMode()) {
164 case NAVIGATION_MODE_TABS:
165 selectTab(mTabs.get(position));
166 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700167 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700168 mActionView.setDropdownSelectedPosition(position);
169 break;
170 default:
171 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700172 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700173 }
174 }
175
176 public int getSelectedNavigationItem() {
Adam Powell9ab97872010-10-26 21:47:29 -0700177 return getSelectedNavigationIndex();
178 }
179
180 public void removeAllTabs() {
181 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700182 }
183
Adam Powell661c9082010-07-02 10:09:44 -0700184 private void cleanupTabs() {
185 if (mSelectedTab != null) {
186 selectTab(null);
187 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700188 mTabs.clear();
Adam Powell0c24a552010-11-03 16:44:11 -0700189 mActionView.removeAllTabs();
190 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700191 }
192
Adam Powell0e94b512010-06-29 17:58:20 -0700193 public void setTitle(CharSequence title) {
194 mActionView.setTitle(title);
195 }
196
197 public void setSubtitle(CharSequence subtitle) {
198 mActionView.setSubtitle(subtitle);
199 }
200
Adam Powell89e06452010-06-23 20:24:52 -0700201 public void setDisplayOptions(int options) {
202 mActionView.setDisplayOptions(options);
203 }
Adam Powell0e94b512010-06-29 17:58:20 -0700204
Adam Powell89e06452010-06-23 20:24:52 -0700205 public void setDisplayOptions(int options, int mask) {
206 final int current = mActionView.getDisplayOptions();
207 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
208 }
209
210 public void setBackgroundDrawable(Drawable d) {
211 mActionView.setBackgroundDrawable(d);
212 }
213
214 public View getCustomNavigationView() {
215 return mActionView.getCustomNavigationView();
216 }
217
218 public CharSequence getTitle() {
219 return mActionView.getTitle();
220 }
221
222 public CharSequence getSubtitle() {
223 return mActionView.getSubtitle();
224 }
225
226 public int getNavigationMode() {
227 return mActionView.getNavigationMode();
228 }
229
230 public int getDisplayOptions() {
231 return mActionView.getDisplayOptions();
232 }
233
Adam Powell5d279772010-07-27 16:34:07 -0700234 public ActionMode startActionMode(ActionMode.Callback callback) {
235 if (mActionMode != null) {
236 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700237 }
Adam Powell3461b322010-07-14 11:34:43 -0700238
239 // Don't wait for the close context mode animation to finish.
240 if (mClosingContext) {
241 mAnimatorView.clearAnimation();
242 mHandler.removeCallbacks(mCloseContext);
243 mCloseContext.run();
244 }
245
Adam Powell5d279772010-07-27 16:34:07 -0700246 ActionMode mode = new ActionModeImpl(callback);
Adam Powell6e346362010-07-23 10:18:23 -0700247 if (callback.onCreateActionMode(mode, mode.getMenu())) {
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700248 mode.invalidate();
249 mUpperContextView.initForMode(mode);
Adam Powell89e06452010-06-23 20:24:52 -0700250 mAnimatorView.setDisplayedChild(CONTEXT_VIEW);
251 if (mLowerContextView != null) {
252 // TODO animate this
253 mLowerContextView.setVisibility(View.VISIBLE);
254 }
Adam Powell5d279772010-07-27 16:34:07 -0700255 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700256 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700257 }
Adam Powellac695c62010-07-20 18:19:27 -0700258 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700259 }
260
Adam Powell661c9082010-07-02 10:09:44 -0700261 private void configureTab(Tab tab, int position) {
262 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700263 final ActionBar.TabListener callback = tabi.getCallback();
264
265 if (callback == null) {
266 throw new IllegalStateException("Action Bar Tab must have a Callback");
267 }
Adam Powell661c9082010-07-02 10:09:44 -0700268
269 tabi.setPosition(position);
270 mTabs.add(position, tabi);
271
Adam Powell81b89442010-11-02 17:58:56 -0700272 final int count = mTabs.size();
273 for (int i = position + 1; i < count; i++) {
274 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700275 }
Adam Powell661c9082010-07-02 10:09:44 -0700276 }
277
278 @Override
279 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700280 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700281 }
282
283 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700284 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700285 addTab(tab, position, mTabs.isEmpty());
286 }
287
288 @Override
289 public void addTab(Tab tab, boolean setSelected) {
290 mActionView.addTab(tab, setSelected);
291 configureTab(tab, mTabs.size());
292 if (setSelected) {
293 selectTab(tab);
294 }
295 }
296
297 @Override
298 public void addTab(Tab tab, int position, boolean setSelected) {
299 mActionView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700300 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700301 if (setSelected) {
302 selectTab(tab);
303 }
Adam Powell661c9082010-07-02 10:09:44 -0700304 }
305
306 @Override
307 public Tab newTab() {
308 return new TabImpl();
309 }
310
311 @Override
312 public void removeTab(Tab tab) {
313 removeTabAt(tab.getPosition());
314 }
315
316 @Override
317 public void removeTabAt(int position) {
Adam Powell0c24a552010-11-03 16:44:11 -0700318 int selectedTabPosition = mSelectedTab != null
319 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powell661c9082010-07-02 10:09:44 -0700320 mActionView.removeTabAt(position);
321 mTabs.remove(position);
322
323 final int newTabCount = mTabs.size();
324 for (int i = position; i < newTabCount; i++) {
325 mTabs.get(i).setPosition(i);
326 }
327
Adam Powell0c24a552010-11-03 16:44:11 -0700328 if (selectedTabPosition == position) {
329 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
330 }
Adam Powell661c9082010-07-02 10:09:44 -0700331 }
332
333 @Override
334 public void setTabNavigationMode() {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700335 if (mActivity == null) {
336 throw new IllegalStateException(
337 "Tab navigation mode cannot be used outside of an Activity");
338 }
Adam Powell9ab97872010-10-26 21:47:29 -0700339 setDisplayOptions(0, DISPLAY_SHOW_TITLE | DISPLAY_SHOW_CUSTOM);
Adam Powell661c9082010-07-02 10:09:44 -0700340 mActionView.setNavigationMode(NAVIGATION_MODE_TABS);
341 }
342
343 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700344 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700345 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
346 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
347 return;
348 }
349
350 final FragmentTransaction trans = mActivity.getFragmentManager().openTransaction()
351 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700352
353 if (mSelectedTab == tab) {
354 if (mSelectedTab != null) {
355 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
356 }
357 } else {
358 mActionView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
359 if (mSelectedTab != null) {
360 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
361 }
362 mSelectedTab = (TabImpl) tab;
363 if (mSelectedTab != null) {
364 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
365 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700366 }
367
368 if (!trans.isEmpty()) {
369 trans.commit();
370 }
371 }
372
373 @Override
374 public Tab getSelectedTab() {
375 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700376 }
377
Adam Powell6b336f82010-08-10 20:13:01 -0700378 @Override
379 public int getHeight() {
380 return mActionView.getHeight();
381 }
382
383 @Override
384 public void show() {
385 // TODO animate!
386 mAnimatorView.setVisibility(View.VISIBLE);
387 }
388
389 @Override
390 public void hide() {
391 // TODO animate!
392 mAnimatorView.setVisibility(View.GONE);
393 }
394
395 public boolean isShowing() {
396 return mAnimatorView.getVisibility() == View.VISIBLE;
397 }
398
Adam Powell89e06452010-06-23 20:24:52 -0700399 /**
400 * @hide
401 */
Adam Powell5d279772010-07-27 16:34:07 -0700402 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700403 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700404 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700405 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700406
Adam Powell5d279772010-07-27 16:34:07 -0700407 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700408 mCallback = callback;
Adam Powell4d9861e2010-08-17 11:14:40 -0700409 mMenu = new MenuBuilder(mActionView.getContext())
410 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700411 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700412 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700413
414 @Override
415 public MenuInflater getMenuInflater() {
Adam Powell4d9861e2010-08-17 11:14:40 -0700416 return new MenuInflater(mContext);
Adam Powell9168f0b2010-08-02 15:46:24 -0700417 }
418
Adam Powell89e06452010-06-23 20:24:52 -0700419 @Override
420 public Menu getMenu() {
421 return mMenu;
422 }
423
424 @Override
425 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700426 if (mActionMode != this) {
427 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700428 return;
429 }
430
Adam Powell6e346362010-07-23 10:18:23 -0700431 mCallback.onDestroyActionMode(this);
Adam Powell89e06452010-06-23 20:24:52 -0700432 mAnimatorView.setDisplayedChild(NORMAL_VIEW);
Adam Powell0e94b512010-06-29 17:58:20 -0700433
434 // Clear out the context mode views after the animation finishes
Adam Powell3461b322010-07-14 11:34:43 -0700435 mClosingContext = true;
Adam Powell0e94b512010-06-29 17:58:20 -0700436 mHandler.postDelayed(mCloseContext, mAnimatorView.getOutAnimation().getDuration());
437
Adam Powell89e06452010-06-23 20:24:52 -0700438 if (mLowerContextView != null && mLowerContextView.getVisibility() != View.GONE) {
439 // TODO Animate this
440 mLowerContextView.setVisibility(View.GONE);
441 }
Adam Powell5d279772010-07-27 16:34:07 -0700442 mActionMode = null;
Adam Powell89e06452010-06-23 20:24:52 -0700443 }
444
445 @Override
446 public void invalidate() {
Adam Powell6e346362010-07-23 10:18:23 -0700447 if (mCallback.onPrepareActionMode(this, mMenu)) {
Adam Powell89e06452010-06-23 20:24:52 -0700448 // Refresh content in both context views
449 }
450 }
451
452 @Override
453 public void setCustomView(View view) {
454 mUpperContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700455 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700456 }
457
458 @Override
459 public void setSubtitle(CharSequence subtitle) {
460 mUpperContextView.setSubtitle(subtitle);
461 }
462
463 @Override
464 public void setTitle(CharSequence title) {
465 mUpperContextView.setTitle(title);
466 }
Adam Powell29ed7572010-07-14 16:24:56 -0700467
468 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700469 public void setTitle(int resId) {
470 setTitle(mActivity.getString(resId));
471 }
472
473 @Override
474 public void setSubtitle(int resId) {
475 setSubtitle(mActivity.getString(resId));
476 }
477
478 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700479 public CharSequence getTitle() {
480 return mUpperContextView.getTitle();
481 }
482
483 @Override
484 public CharSequence getSubtitle() {
485 return mUpperContextView.getSubtitle();
486 }
Adam Powell89e06452010-06-23 20:24:52 -0700487
Adam Powell29ed7572010-07-14 16:24:56 -0700488 @Override
489 public View getCustomView() {
490 return mCustomView != null ? mCustomView.get() : null;
491 }
492
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700493 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powell6e346362010-07-23 10:18:23 -0700494 return mCallback.onActionItemClicked(this, item);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700495 }
496
497 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
498 }
499
500 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
501 if (!subMenu.hasVisibleItems()) {
502 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700503 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700504
Adam Powelldec9dfd2010-08-09 15:27:54 -0700505 new MenuPopupHelper(mContext, subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700506 return true;
507 }
508
509 public void onCloseSubMenu(SubMenuBuilder menu) {
510 }
511
512 public void onMenuModeChange(MenuBuilder menu) {
Adam Powellf6148c52010-08-11 21:10:16 -0700513 invalidate();
514 mUpperContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700515 }
Adam Powell661c9082010-07-02 10:09:44 -0700516 }
517
518 /**
519 * @hide
520 */
521 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700522 private ActionBar.TabListener mCallback;
523 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700524 private Drawable mIcon;
525 private CharSequence mText;
526 private int mPosition;
Adam Powell2b6230e2010-09-07 17:55:25 -0700527 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700528
529 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700530 public Object getTag() {
531 return mTag;
532 }
533
534 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700535 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700536 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700537 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700538 }
539
540 public ActionBar.TabListener getCallback() {
541 return mCallback;
542 }
543
544 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700545 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700546 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700547 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700548 }
549
550 @Override
551 public View getCustomView() {
552 return mCustomView;
553 }
554
555 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700556 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700557 mCustomView = view;
Adam Powell9ab97872010-10-26 21:47:29 -0700558 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700559 }
560
561 @Override
562 public Drawable getIcon() {
563 return mIcon;
564 }
565
566 @Override
567 public int getPosition() {
568 return mPosition;
569 }
570
571 public void setPosition(int position) {
572 mPosition = position;
573 }
574
575 @Override
576 public CharSequence getText() {
577 return mText;
578 }
579
580 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700581 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -0700582 mIcon = icon;
Adam Powell9ab97872010-10-26 21:47:29 -0700583 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700584 }
585
586 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700587 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -0700588 mText = text;
Adam Powell9ab97872010-10-26 21:47:29 -0700589 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700590 }
591
592 @Override
593 public void select() {
594 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700595 }
596 }
Adam Powell9ab97872010-10-26 21:47:29 -0700597
598 @Override
599 public void setCustomView(View view) {
600 mActionView.setCustomNavigationView(view);
601 }
602
603 @Override
604 public void setCustomView(View view, LayoutParams layoutParams) {
605 view.setLayoutParams(layoutParams);
606 mActionView.setCustomNavigationView(view);
607 }
608
609 @Override
610 public void setListNavigationCallbacks(SpinnerAdapter adapter, NavigationCallback callback) {
611 mActionView.setDropdownAdapter(adapter);
612 mActionView.setCallback(callback);
613 }
614
615 @Override
616 public int getSelectedNavigationIndex() {
617 switch (mActionView.getNavigationMode()) {
618 case NAVIGATION_MODE_TABS:
619 return mSelectedTab.getPosition();
620 case NAVIGATION_MODE_LIST:
621 return mActionView.getDropdownSelectedPosition();
622 default:
623 return -1;
624 }
625 }
626
627 @Override
628 public int getNavigationItemCount() {
629 switch (mActionView.getNavigationMode()) {
630 case NAVIGATION_MODE_TABS:
631 return mTabs.size();
632 case NAVIGATION_MODE_LIST:
633 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
634 return adapter != null ? adapter.getCount() : 0;
635 default:
636 return 0;
637 }
638 }
639
640 @Override
Adam Powell0c24a552010-11-03 16:44:11 -0700641 public int getTabCount() {
642 return mTabs.size();
643 }
644
645 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700646 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -0700647 final int oldMode = mActionView.getNavigationMode();
648 switch (oldMode) {
649 case NAVIGATION_MODE_TABS:
650 mSavedTabPosition = getSelectedNavigationIndex();
651 selectTab(null);
652 break;
653 }
Adam Powell9ab97872010-10-26 21:47:29 -0700654 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -0700655 switch (mode) {
656 case NAVIGATION_MODE_TABS:
657 if (mSavedTabPosition != INVALID_POSITION) {
658 setSelectedNavigationItem(mSavedTabPosition);
659 mSavedTabPosition = INVALID_POSITION;
660 }
661 break;
662 }
Adam Powell9ab97872010-10-26 21:47:29 -0700663 }
664
665 @Override
666 public Tab getTabAt(int index) {
667 return mTabs.get(index);
668 }
Adam Powell0c24a552010-11-03 16:44:11 -0700669
670 /**
671 * This fragment is added when we're keeping a back stack in a tab switch
672 * transaction. We use it to change the selected tab in the action bar view
673 * when we back out.
674 */
675 private class SwitchSelectedTabViewFragment extends Fragment {
676 private int mSelectedTabIndex;
677
678 public SwitchSelectedTabViewFragment(int oldSelectedTab) {
679 mSelectedTabIndex = oldSelectedTab;
680 }
681
682 @Override
683 public void onDetach() {
684 if (mSelectedTabIndex >= 0 && mSelectedTabIndex < getTabCount()) {
685 mActionView.setTabSelected(mSelectedTabIndex);
686 }
687 }
688 }
Adam Powell89e06452010-06-23 20:24:52 -0700689}