blob: 8f1354b566f1f9db86bd5cb9683477389096ada6 [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 Powell01feaee2011-02-10 15:05:05 -080022import com.android.internal.widget.ActionBarContainer;
Adam Powell89e06452010-06-23 20:24:52 -070023import com.android.internal.widget.ActionBarContextView;
24import com.android.internal.widget.ActionBarView;
25
Adam Powelld8b3f2e2010-12-02 13:37:03 -080026import android.animation.Animator;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080027import android.animation.Animator.AnimatorListener;
28import android.animation.AnimatorSet;
Adam Powelle6ec7322010-12-07 15:29:26 -080029import android.animation.ObjectAnimator;
Adam Powell45f1e082010-12-10 14:20:13 -080030import android.animation.TimeInterpolator;
Adam Powell89e06452010-06-23 20:24:52 -070031import android.app.ActionBar;
Adam Powell661c9082010-07-02 10:09:44 -070032import android.app.Activity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070033import android.app.Dialog;
Adam Powell661c9082010-07-02 10:09:44 -070034import android.app.Fragment;
35import android.app.FragmentTransaction;
Adam Powelldec9dfd2010-08-09 15:27:54 -070036import android.content.Context;
Adam Powell89e06452010-06-23 20:24:52 -070037import android.graphics.drawable.Drawable;
Adam Powell0e94b512010-06-29 17:58:20 -070038import android.os.Handler;
Adam Powell6e346362010-07-23 10:18:23 -070039import android.view.ActionMode;
Adam Powell32555f32010-11-17 13:49:22 -080040import android.view.LayoutInflater;
Adam Powell89e06452010-06-23 20:24:52 -070041import android.view.Menu;
Adam Powell9168f0b2010-08-02 15:46:24 -070042import android.view.MenuInflater;
Adam Powell89e06452010-06-23 20:24:52 -070043import android.view.MenuItem;
44import android.view.View;
Adam Powelle6ec7322010-12-07 15:29:26 -080045import android.view.Window;
Adam Powell45f1e082010-12-10 14:20:13 -080046import android.view.animation.DecelerateInterpolator;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080047import android.widget.FrameLayout;
Adam Powell89e06452010-06-23 20:24:52 -070048import android.widget.LinearLayout;
49import android.widget.SpinnerAdapter;
Adam Powell89e06452010-06-23 20:24:52 -070050
Adam Powell29ed7572010-07-14 16:24:56 -070051import java.lang.ref.WeakReference;
Adam Powell661c9082010-07-02 10:09:44 -070052import java.util.ArrayList;
53
Adam Powell89e06452010-06-23 20:24:52 -070054/**
55 * ActionBarImpl is the ActionBar implementation used
56 * by devices of all screen sizes. If it detects a compatible decor,
57 * it will split contextual modes across both the ActionBarView at
58 * the top of the screen and a horizontal LinearLayout at the bottom
59 * which is normally hidden.
60 */
61public class ActionBarImpl extends ActionBar {
62 private static final int NORMAL_VIEW = 0;
63 private static final int CONTEXT_VIEW = 1;
Adam Powell661c9082010-07-02 10:09:44 -070064
Adam Powelldec9dfd2010-08-09 15:27:54 -070065 private Context mContext;
Adam Powell661c9082010-07-02 10:09:44 -070066 private Activity mActivity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070067 private Dialog mDialog;
Adam Powell661c9082010-07-02 10:09:44 -070068
Adam Powell01feaee2011-02-10 15:05:05 -080069 private ActionBarContainer mContainerView;
Adam Powell89e06452010-06-23 20:24:52 -070070 private ActionBarView mActionView;
71 private ActionBarContextView mUpperContextView;
72 private LinearLayout mLowerContextView;
Adam Powelle6ec7322010-12-07 15:29:26 -080073 private View mContentView;
Adam Powell661c9082010-07-02 10:09:44 -070074
75 private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>();
76
Adam Powell661c9082010-07-02 10:09:44 -070077 private TabImpl mSelectedTab;
Adam Powell0c24a552010-11-03 16:44:11 -070078 private int mSavedTabPosition = INVALID_POSITION;
Adam Powell89e06452010-06-23 20:24:52 -070079
Adam Powell5d279772010-07-27 16:34:07 -070080 private ActionMode mActionMode;
Adam Powell89e06452010-06-23 20:24:52 -070081
Adam Powell8515ee82010-11-30 14:09:55 -080082 private boolean mLastMenuVisibility;
83 private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
84 new ArrayList<OnMenuVisibilityListener>();
85
Adam Powell89e06452010-06-23 20:24:52 -070086 private static final int CONTEXT_DISPLAY_NORMAL = 0;
87 private static final int CONTEXT_DISPLAY_SPLIT = 1;
88
Adam Powell0c24a552010-11-03 16:44:11 -070089 private static final int INVALID_POSITION = -1;
90
Adam Powell89e06452010-06-23 20:24:52 -070091 private int mContextDisplayMode;
Adam Powell0e94b512010-06-29 17:58:20 -070092
93 final Handler mHandler = new Handler();
Adam Powelld8b3f2e2010-12-02 13:37:03 -080094
Adam Powelle6ec7322010-12-07 15:29:26 -080095 private Animator mCurrentAnim;
Adam Powell50efbed2011-02-08 16:20:15 -080096 private boolean mShowHideAnimationEnabled;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080097
Adam Powell45f1e082010-12-10 14:20:13 -080098 private static final TimeInterpolator sFadeOutInterpolator = new DecelerateInterpolator();
99
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800100 final AnimatorListener[] mAfterAnimation = new AnimatorListener[] {
101 new AnimatorListener() { // NORMAL_VIEW
102 @Override
103 public void onAnimationStart(Animator animation) {
104 }
105
106 @Override
107 public void onAnimationEnd(Animator animation) {
108 if (mLowerContextView != null) {
109 mLowerContextView.removeAllViews();
110 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800111 mCurrentAnim = null;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800112 hideAllExcept(NORMAL_VIEW);
113 }
114
115 @Override
116 public void onAnimationCancel(Animator animation) {
117 }
118
119 @Override
120 public void onAnimationRepeat(Animator animation) {
121 }
122 },
123 new AnimatorListener() { // CONTEXT_VIEW
124 @Override
125 public void onAnimationStart(Animator animation) {
126 }
127
128 @Override
129 public void onAnimationEnd(Animator animation) {
Adam Powelle6ec7322010-12-07 15:29:26 -0800130 mCurrentAnim = null;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800131 hideAllExcept(CONTEXT_VIEW);
132 }
133
134 @Override
135 public void onAnimationCancel(Animator animation) {
136 }
137
138 @Override
139 public void onAnimationRepeat(Animator animation) {
140 }
Adam Powell0e94b512010-06-29 17:58:20 -0700141 }
Adam Powell0e94b512010-06-29 17:58:20 -0700142 };
143
Adam Powelle6ec7322010-12-07 15:29:26 -0800144 final AnimatorListener mHideListener = new AnimatorListener() {
145 @Override
146 public void onAnimationStart(Animator animation) {
147 }
148
149 @Override
150 public void onAnimationEnd(Animator animation) {
151 if (mContentView != null) {
152 mContentView.setTranslationY(0);
153 }
154 mContainerView.setVisibility(View.GONE);
Adam Powell01feaee2011-02-10 15:05:05 -0800155 mContainerView.setTransitioning(false);
Adam Powelle6ec7322010-12-07 15:29:26 -0800156 mCurrentAnim = null;
157 }
158
159 @Override
160 public void onAnimationCancel(Animator animation) {
161 }
162
163 @Override
164 public void onAnimationRepeat(Animator animation) {
165 }
166 };
167
168 final AnimatorListener mShowListener = new AnimatorListener() {
169 @Override
170 public void onAnimationStart(Animator animation) {
171 }
172
173 @Override
174 public void onAnimationEnd(Animator animation) {
175 mCurrentAnim = null;
Adam Powell45f1e082010-12-10 14:20:13 -0800176 mContainerView.requestLayout();
Adam Powelle6ec7322010-12-07 15:29:26 -0800177 }
178
179 @Override
180 public void onAnimationCancel(Animator animation) {
181 }
182
183 @Override
184 public void onAnimationRepeat(Animator animation) {
185 }
186 };
187
Adam Powell661c9082010-07-02 10:09:44 -0700188 public ActionBarImpl(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -0700189 mActivity = activity;
Adam Powelle6ec7322010-12-07 15:29:26 -0800190 Window window = activity.getWindow();
191 View decor = window.getDecorView();
192 init(decor);
193 if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) {
194 mContentView = decor.findViewById(android.R.id.content);
195 }
Adam Powelldec9dfd2010-08-09 15:27:54 -0700196 }
197
198 public ActionBarImpl(Dialog dialog) {
199 mDialog = dialog;
200 init(dialog.getWindow().getDecorView());
201 }
202
203 private void init(View decor) {
204 mContext = decor.getContext();
Adam Powell89e06452010-06-23 20:24:52 -0700205 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
206 mUpperContextView = (ActionBarContextView) decor.findViewById(
207 com.android.internal.R.id.action_context_bar);
208 mLowerContextView = (LinearLayout) decor.findViewById(
209 com.android.internal.R.id.lower_action_context_bar);
Adam Powell01feaee2011-02-10 15:05:05 -0800210 mContainerView = (ActionBarContainer) decor.findViewById(
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800211 com.android.internal.R.id.action_bar_container);
Steve Block08f194b2010-08-24 18:20:48 +0100212
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800213 if (mActionView == null || mUpperContextView == null || mContainerView == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700214 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
215 "with a compatible window decor layout");
216 }
Adam Powell0e94b512010-06-29 17:58:20 -0700217
Steve Block08f194b2010-08-24 18:20:48 +0100218 mActionView.setContextView(mUpperContextView);
Adam Powell89e06452010-06-23 20:24:52 -0700219 mContextDisplayMode = mLowerContextView == null ?
220 CONTEXT_DISPLAY_NORMAL : CONTEXT_DISPLAY_SPLIT;
221 }
222
Adam Powell50efbed2011-02-08 16:20:15 -0800223 /**
224 * Enables or disables animation between show/hide states.
225 * If animation is disabled using this method, animations in progress
226 * will be finished.
227 *
228 * @param enabled true to animate, false to not animate.
229 */
230 public void setShowHideAnimationEnabled(boolean enabled) {
231 mShowHideAnimationEnabled = enabled;
232 if (!enabled && mCurrentAnim != null) {
233 mCurrentAnim.end();
234 }
235 }
236
Adam Powell8515ee82010-11-30 14:09:55 -0800237 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
238 mMenuVisibilityListeners.add(listener);
239 }
240
241 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
242 mMenuVisibilityListeners.remove(listener);
243 }
244
245 public void dispatchMenuVisibilityChanged(boolean isVisible) {
246 if (isVisible == mLastMenuVisibility) {
247 return;
248 }
249 mLastMenuVisibility = isVisible;
250
251 final int count = mMenuVisibilityListeners.size();
252 for (int i = 0; i < count; i++) {
253 mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
254 }
255 }
256
Adam Powella66c7b02010-07-28 15:28:25 -0700257 @Override
Adam Powell3f476b32011-01-03 19:25:36 -0800258 public void setCustomView(int resId) {
259 setCustomView(LayoutInflater.from(mContext).inflate(resId, mActionView, false));
260 }
261
262 @Override
263 public void setDisplayUseLogoEnabled(boolean useLogo) {
264 setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
265 }
266
267 @Override
268 public void setDisplayShowHomeEnabled(boolean showHome) {
269 setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
270 }
271
272 @Override
273 public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
274 setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
275 }
276
277 @Override
278 public void setDisplayShowTitleEnabled(boolean showTitle) {
279 setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
280 }
281
282 @Override
283 public void setDisplayShowCustomEnabled(boolean showCustom) {
284 setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
285 }
286
287 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700288 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700289 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700290 }
291
292 @Override
293 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700294 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700295 }
296
Adam Powell17809772010-07-21 13:25:11 -0700297 public void setSelectedNavigationItem(int position) {
298 switch (mActionView.getNavigationMode()) {
299 case NAVIGATION_MODE_TABS:
300 selectTab(mTabs.get(position));
301 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700302 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700303 mActionView.setDropdownSelectedPosition(position);
304 break;
305 default:
306 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700307 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700308 }
309 }
310
Adam Powell9ab97872010-10-26 21:47:29 -0700311 public void removeAllTabs() {
312 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700313 }
314
Adam Powell661c9082010-07-02 10:09:44 -0700315 private void cleanupTabs() {
316 if (mSelectedTab != null) {
317 selectTab(null);
318 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700319 mTabs.clear();
Adam Powell0c24a552010-11-03 16:44:11 -0700320 mActionView.removeAllTabs();
321 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700322 }
323
Adam Powell0e94b512010-06-29 17:58:20 -0700324 public void setTitle(CharSequence title) {
325 mActionView.setTitle(title);
326 }
327
328 public void setSubtitle(CharSequence subtitle) {
329 mActionView.setSubtitle(subtitle);
330 }
331
Adam Powell89e06452010-06-23 20:24:52 -0700332 public void setDisplayOptions(int options) {
333 mActionView.setDisplayOptions(options);
334 }
Adam Powell0e94b512010-06-29 17:58:20 -0700335
Adam Powell89e06452010-06-23 20:24:52 -0700336 public void setDisplayOptions(int options, int mask) {
337 final int current = mActionView.getDisplayOptions();
338 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
339 }
340
341 public void setBackgroundDrawable(Drawable d) {
Adam Powelle8c1e5c2010-12-13 10:49:32 -0800342 mContainerView.setBackgroundDrawable(d);
Adam Powell89e06452010-06-23 20:24:52 -0700343 }
344
Adam Powellef704442010-11-16 14:48:22 -0800345 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700346 return mActionView.getCustomNavigationView();
347 }
348
349 public CharSequence getTitle() {
350 return mActionView.getTitle();
351 }
352
353 public CharSequence getSubtitle() {
354 return mActionView.getSubtitle();
355 }
356
357 public int getNavigationMode() {
358 return mActionView.getNavigationMode();
359 }
360
361 public int getDisplayOptions() {
362 return mActionView.getDisplayOptions();
363 }
364
Adam Powell5d279772010-07-27 16:34:07 -0700365 public ActionMode startActionMode(ActionMode.Callback callback) {
366 if (mActionMode != null) {
367 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700368 }
Adam Powell3461b322010-07-14 11:34:43 -0700369
Adam Powella1e63582011-01-18 16:51:22 -0800370 mUpperContextView.killMode();
Adam Powell5d279772010-07-27 16:34:07 -0700371 ActionMode mode = new ActionModeImpl(callback);
Adam Powell6e346362010-07-23 10:18:23 -0700372 if (callback.onCreateActionMode(mode, mode.getMenu())) {
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700373 mode.invalidate();
374 mUpperContextView.initForMode(mode);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800375 animateTo(CONTEXT_VIEW);
Adam Powell89e06452010-06-23 20:24:52 -0700376 if (mLowerContextView != null) {
377 // TODO animate this
378 mLowerContextView.setVisibility(View.VISIBLE);
379 }
Adam Powell5d279772010-07-27 16:34:07 -0700380 mActionMode = mode;
Adam Powell50efbed2011-02-08 16:20:15 -0800381 show();
Adam Powellac695c62010-07-20 18:19:27 -0700382 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700383 }
Adam Powellac695c62010-07-20 18:19:27 -0700384 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700385 }
386
Adam Powell661c9082010-07-02 10:09:44 -0700387 private void configureTab(Tab tab, int position) {
388 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700389 final ActionBar.TabListener callback = tabi.getCallback();
390
391 if (callback == null) {
392 throw new IllegalStateException("Action Bar Tab must have a Callback");
393 }
Adam Powell661c9082010-07-02 10:09:44 -0700394
395 tabi.setPosition(position);
396 mTabs.add(position, tabi);
397
Adam Powell81b89442010-11-02 17:58:56 -0700398 final int count = mTabs.size();
399 for (int i = position + 1; i < count; i++) {
400 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700401 }
Adam Powell661c9082010-07-02 10:09:44 -0700402 }
403
404 @Override
405 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700406 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700407 }
408
409 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700410 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700411 addTab(tab, position, mTabs.isEmpty());
412 }
413
414 @Override
415 public void addTab(Tab tab, boolean setSelected) {
416 mActionView.addTab(tab, setSelected);
417 configureTab(tab, mTabs.size());
418 if (setSelected) {
419 selectTab(tab);
420 }
421 }
422
423 @Override
424 public void addTab(Tab tab, int position, boolean setSelected) {
425 mActionView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700426 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700427 if (setSelected) {
428 selectTab(tab);
429 }
Adam Powell661c9082010-07-02 10:09:44 -0700430 }
431
432 @Override
433 public Tab newTab() {
434 return new TabImpl();
435 }
436
437 @Override
438 public void removeTab(Tab tab) {
439 removeTabAt(tab.getPosition());
440 }
441
442 @Override
443 public void removeTabAt(int position) {
Adam Powell0c24a552010-11-03 16:44:11 -0700444 int selectedTabPosition = mSelectedTab != null
445 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powell661c9082010-07-02 10:09:44 -0700446 mActionView.removeTabAt(position);
447 mTabs.remove(position);
448
449 final int newTabCount = mTabs.size();
450 for (int i = position; i < newTabCount; i++) {
451 mTabs.get(i).setPosition(i);
452 }
453
Adam Powell0c24a552010-11-03 16:44:11 -0700454 if (selectedTabPosition == position) {
455 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
456 }
Adam Powell661c9082010-07-02 10:09:44 -0700457 }
458
459 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700460 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700461 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
462 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
463 return;
464 }
465
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800466 final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction()
Adam Powell0c24a552010-11-03 16:44:11 -0700467 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700468
469 if (mSelectedTab == tab) {
470 if (mSelectedTab != null) {
471 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
472 }
473 } else {
474 mActionView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
475 if (mSelectedTab != null) {
476 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
477 }
478 mSelectedTab = (TabImpl) tab;
479 if (mSelectedTab != null) {
480 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
481 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700482 }
483
484 if (!trans.isEmpty()) {
485 trans.commit();
486 }
487 }
488
489 @Override
490 public Tab getSelectedTab() {
491 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700492 }
493
Adam Powell6b336f82010-08-10 20:13:01 -0700494 @Override
495 public int getHeight() {
496 return mActionView.getHeight();
497 }
498
499 @Override
500 public void show() {
Adam Powell45f1e082010-12-10 14:20:13 -0800501 if (mCurrentAnim != null) {
502 mCurrentAnim.end();
503 }
Adam Powelld76cee22011-01-31 15:05:05 -0800504 if (mContainerView.getVisibility() == View.VISIBLE) {
505 return;
506 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800507 mContainerView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800508
509 if (mShowHideAnimationEnabled) {
510 mContainerView.setAlpha(0);
511 AnimatorSet anim = new AnimatorSet();
512 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 1));
513 if (mContentView != null) {
514 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
515 -mContainerView.getHeight(), 0));
516 mContainerView.setTranslationY(-mContainerView.getHeight());
517 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY", 0));
518 }
519 anim.addListener(mShowListener);
520 mCurrentAnim = anim;
521 anim.start();
522 } else {
523 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800524 }
Adam Powell6b336f82010-08-10 20:13:01 -0700525 }
526
527 @Override
528 public void hide() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800529 if (mCurrentAnim != null) {
530 mCurrentAnim.end();
531 }
532 if (mContainerView.getVisibility() == View.GONE) {
533 return;
534 }
Adam Powell50efbed2011-02-08 16:20:15 -0800535
536 if (mShowHideAnimationEnabled) {
537 mContainerView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800538 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800539 AnimatorSet anim = new AnimatorSet();
540 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 0));
541 if (mContentView != null) {
542 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
543 0, -mContainerView.getHeight()));
544 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY",
545 -mContainerView.getHeight()));
546 }
547 anim.addListener(mHideListener);
548 mCurrentAnim = anim;
549 anim.start();
550 } else {
551 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800552 }
Adam Powell6b336f82010-08-10 20:13:01 -0700553 }
554
555 public boolean isShowing() {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800556 return mContainerView.getVisibility() == View.VISIBLE;
557 }
558
559 private long animateTo(int viewIndex) {
Adam Powelle6ec7322010-12-07 15:29:26 -0800560 show();
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800561
562 AnimatorSet set = new AnimatorSet();
563
564 final View targetChild = mContainerView.getChildAt(viewIndex);
565 targetChild.setVisibility(View.VISIBLE);
566 AnimatorSet.Builder b = set.play(ObjectAnimator.ofFloat(targetChild, "alpha", 1));
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800567
568 final int count = mContainerView.getChildCount();
569 for (int i = 0; i < count; i++) {
570 final View child = mContainerView.getChildAt(i);
571 if (i == viewIndex) {
572 continue;
573 }
574
575 if (child.getVisibility() != View.GONE) {
Adam Powell45f1e082010-12-10 14:20:13 -0800576 Animator a = ObjectAnimator.ofFloat(child, "alpha", 0);
577 a.setInterpolator(sFadeOutInterpolator);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800578 b.with(a);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800579 }
580 }
581
582 set.addListener(mAfterAnimation[viewIndex]);
583
Adam Powelle6ec7322010-12-07 15:29:26 -0800584 mCurrentAnim = set;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800585 set.start();
586 return set.getDuration();
587 }
588
589 private void hideAllExcept(int viewIndex) {
590 final int count = mContainerView.getChildCount();
591 for (int i = 0; i < count; i++) {
592 mContainerView.getChildAt(i).setVisibility(i == viewIndex ? View.VISIBLE : View.GONE);
593 }
Adam Powell6b336f82010-08-10 20:13:01 -0700594 }
595
Adam Powell89e06452010-06-23 20:24:52 -0700596 /**
597 * @hide
598 */
Adam Powell5d279772010-07-27 16:34:07 -0700599 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700600 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700601 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700602 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700603
Adam Powell5d279772010-07-27 16:34:07 -0700604 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700605 mCallback = callback;
Adam Powell4d9861e2010-08-17 11:14:40 -0700606 mMenu = new MenuBuilder(mActionView.getContext())
607 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700608 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700609 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700610
611 @Override
612 public MenuInflater getMenuInflater() {
Adam Powell4d9861e2010-08-17 11:14:40 -0700613 return new MenuInflater(mContext);
Adam Powell9168f0b2010-08-02 15:46:24 -0700614 }
615
Adam Powell89e06452010-06-23 20:24:52 -0700616 @Override
617 public Menu getMenu() {
618 return mMenu;
619 }
620
621 @Override
622 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700623 if (mActionMode != this) {
624 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700625 return;
626 }
627
Adam Powell6e346362010-07-23 10:18:23 -0700628 mCallback.onDestroyActionMode(this);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800629 mCallback = null;
630 animateTo(NORMAL_VIEW);
Adam Powell0e94b512010-06-29 17:58:20 -0700631
632 // Clear out the context mode views after the animation finishes
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800633 mUpperContextView.closeMode();
Adam Powell89e06452010-06-23 20:24:52 -0700634 if (mLowerContextView != null && mLowerContextView.getVisibility() != View.GONE) {
635 // TODO Animate this
636 mLowerContextView.setVisibility(View.GONE);
637 }
Adam Powell5d279772010-07-27 16:34:07 -0700638 mActionMode = null;
Adam Powell89e06452010-06-23 20:24:52 -0700639 }
640
641 @Override
642 public void invalidate() {
Adam Powell6e346362010-07-23 10:18:23 -0700643 if (mCallback.onPrepareActionMode(this, mMenu)) {
Adam Powell89e06452010-06-23 20:24:52 -0700644 // Refresh content in both context views
645 }
646 }
647
648 @Override
649 public void setCustomView(View view) {
650 mUpperContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700651 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700652 }
653
654 @Override
655 public void setSubtitle(CharSequence subtitle) {
656 mUpperContextView.setSubtitle(subtitle);
657 }
658
659 @Override
660 public void setTitle(CharSequence title) {
661 mUpperContextView.setTitle(title);
662 }
Adam Powell29ed7572010-07-14 16:24:56 -0700663
664 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700665 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800666 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700667 }
668
669 @Override
670 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800671 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700672 }
673
674 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700675 public CharSequence getTitle() {
676 return mUpperContextView.getTitle();
677 }
678
679 @Override
680 public CharSequence getSubtitle() {
681 return mUpperContextView.getSubtitle();
682 }
Adam Powell89e06452010-06-23 20:24:52 -0700683
Adam Powell29ed7572010-07-14 16:24:56 -0700684 @Override
685 public View getCustomView() {
686 return mCustomView != null ? mCustomView.get() : null;
687 }
688
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700689 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800690 if (mCallback != null) {
691 return mCallback.onActionItemClicked(this, item);
692 } else {
693 return false;
694 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700695 }
696
697 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
698 }
699
700 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800701 if (mCallback == null) {
702 return false;
703 }
704
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700705 if (!subMenu.hasVisibleItems()) {
706 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700707 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700708
Adam Powelldec9dfd2010-08-09 15:27:54 -0700709 new MenuPopupHelper(mContext, subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700710 return true;
711 }
712
713 public void onCloseSubMenu(SubMenuBuilder menu) {
714 }
715
716 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800717 if (mCallback == null) {
718 return;
719 }
Adam Powellf6148c52010-08-11 21:10:16 -0700720 invalidate();
Adam Powell8515ee82010-11-30 14:09:55 -0800721 mUpperContextView.openOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700722 }
Adam Powell661c9082010-07-02 10:09:44 -0700723 }
724
725 /**
726 * @hide
727 */
728 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700729 private ActionBar.TabListener mCallback;
730 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700731 private Drawable mIcon;
732 private CharSequence mText;
733 private int mPosition;
Adam Powell2b6230e2010-09-07 17:55:25 -0700734 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700735
736 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700737 public Object getTag() {
738 return mTag;
739 }
740
741 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700742 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700743 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700744 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700745 }
746
747 public ActionBar.TabListener getCallback() {
748 return mCallback;
749 }
750
751 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700752 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700753 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700754 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700755 }
756
757 @Override
758 public View getCustomView() {
759 return mCustomView;
760 }
761
762 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700763 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700764 mCustomView = view;
Adam Powell9ab97872010-10-26 21:47:29 -0700765 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700766 }
767
768 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800769 public Tab setCustomView(int layoutResId) {
770 return setCustomView(LayoutInflater.from(mContext).inflate(layoutResId, null));
771 }
772
773 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700774 public Drawable getIcon() {
775 return mIcon;
776 }
777
778 @Override
779 public int getPosition() {
780 return mPosition;
781 }
782
783 public void setPosition(int position) {
784 mPosition = position;
785 }
786
787 @Override
788 public CharSequence getText() {
789 return mText;
790 }
791
792 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700793 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -0700794 mIcon = icon;
Adam Powell9ab97872010-10-26 21:47:29 -0700795 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700796 }
797
798 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800799 public Tab setIcon(int resId) {
800 return setIcon(mContext.getResources().getDrawable(resId));
801 }
802
803 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700804 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -0700805 mText = text;
Adam Powell9ab97872010-10-26 21:47:29 -0700806 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700807 }
808
809 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800810 public Tab setText(int resId) {
811 return setText(mContext.getResources().getText(resId));
812 }
813
814 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700815 public void select() {
816 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700817 }
818 }
Adam Powell9ab97872010-10-26 21:47:29 -0700819
820 @Override
821 public void setCustomView(View view) {
822 mActionView.setCustomNavigationView(view);
823 }
824
825 @Override
826 public void setCustomView(View view, LayoutParams layoutParams) {
827 view.setLayoutParams(layoutParams);
828 mActionView.setCustomNavigationView(view);
829 }
830
831 @Override
Adam Powell8515ee82010-11-30 14:09:55 -0800832 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -0700833 mActionView.setDropdownAdapter(adapter);
834 mActionView.setCallback(callback);
835 }
836
837 @Override
838 public int getSelectedNavigationIndex() {
839 switch (mActionView.getNavigationMode()) {
840 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -0800841 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -0700842 case NAVIGATION_MODE_LIST:
843 return mActionView.getDropdownSelectedPosition();
844 default:
845 return -1;
846 }
847 }
848
849 @Override
850 public int getNavigationItemCount() {
851 switch (mActionView.getNavigationMode()) {
852 case NAVIGATION_MODE_TABS:
853 return mTabs.size();
854 case NAVIGATION_MODE_LIST:
855 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
856 return adapter != null ? adapter.getCount() : 0;
857 default:
858 return 0;
859 }
860 }
861
862 @Override
Adam Powell0c24a552010-11-03 16:44:11 -0700863 public int getTabCount() {
864 return mTabs.size();
865 }
866
867 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700868 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -0700869 final int oldMode = mActionView.getNavigationMode();
870 switch (oldMode) {
871 case NAVIGATION_MODE_TABS:
872 mSavedTabPosition = getSelectedNavigationIndex();
873 selectTab(null);
874 break;
875 }
Adam Powell9ab97872010-10-26 21:47:29 -0700876 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -0700877 switch (mode) {
878 case NAVIGATION_MODE_TABS:
879 if (mSavedTabPosition != INVALID_POSITION) {
880 setSelectedNavigationItem(mSavedTabPosition);
881 mSavedTabPosition = INVALID_POSITION;
882 }
883 break;
884 }
Adam Powell9ab97872010-10-26 21:47:29 -0700885 }
886
887 @Override
888 public Tab getTabAt(int index) {
889 return mTabs.get(index);
890 }
Adam Powell0c24a552010-11-03 16:44:11 -0700891
892 /**
893 * This fragment is added when we're keeping a back stack in a tab switch
894 * transaction. We use it to change the selected tab in the action bar view
895 * when we back out.
896 */
897 private class SwitchSelectedTabViewFragment extends Fragment {
898 private int mSelectedTabIndex;
899
900 public SwitchSelectedTabViewFragment(int oldSelectedTab) {
901 mSelectedTabIndex = oldSelectedTab;
902 }
903
904 @Override
905 public void onDetach() {
906 if (mSelectedTabIndex >= 0 && mSelectedTabIndex < getTabCount()) {
907 mActionView.setTabSelected(mSelectedTabIndex);
908 }
909 }
910 }
Adam Powell89e06452010-06-23 20:24:52 -0700911}