blob: cfecca5188e388083e11ebfc8f855515096cdd31 [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;
Adam Powellf8ac6b72011-05-23 18:14:09 -070025import com.android.internal.widget.ScrollingTabContainerView;
Adam Powell89e06452010-06-23 20:24:52 -070026
Adam Powelld8b3f2e2010-12-02 13:37:03 -080027import android.animation.Animator;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080028import android.animation.Animator.AnimatorListener;
Adam Powell07e1f982011-03-24 11:11:15 -070029import android.animation.AnimatorListenerAdapter;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080030import android.animation.AnimatorSet;
Adam Powelle6ec7322010-12-07 15:29:26 -080031import android.animation.ObjectAnimator;
Adam Powell89e06452010-06-23 20:24:52 -070032import android.app.ActionBar;
Adam Powell661c9082010-07-02 10:09:44 -070033import android.app.Activity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070034import android.app.Dialog;
Adam Powell661c9082010-07-02 10:09:44 -070035import android.app.FragmentTransaction;
Adam Powelldec9dfd2010-08-09 15:27:54 -070036import android.content.Context;
Adam Powellf8ac6b72011-05-23 18:14:09 -070037import android.content.res.Configuration;
Adam Powell88ab6972011-07-28 11:25:01 -070038import android.content.res.Resources;
Adam Powell89e06452010-06-23 20:24:52 -070039import android.graphics.drawable.Drawable;
Adam Powellc29f4e52011-07-13 20:40:52 -070040import android.os.Build;
Adam Powell0e94b512010-06-29 17:58:20 -070041import android.os.Handler;
Adam Powell88ab6972011-07-28 11:25:01 -070042import android.util.TypedValue;
Adam Powell6e346362010-07-23 10:18:23 -070043import android.view.ActionMode;
Adam Powell88ab6972011-07-28 11:25:01 -070044import android.view.ContextThemeWrapper;
Adam Powell32555f32010-11-17 13:49:22 -080045import android.view.LayoutInflater;
Adam Powell89e06452010-06-23 20:24:52 -070046import android.view.Menu;
Adam Powell9168f0b2010-08-02 15:46:24 -070047import android.view.MenuInflater;
Adam Powell89e06452010-06-23 20:24:52 -070048import android.view.MenuItem;
49import android.view.View;
Adam Powelle6ec7322010-12-07 15:29:26 -080050import android.view.Window;
Adam Powell89e06452010-06-23 20:24:52 -070051import android.widget.SpinnerAdapter;
Adam Powell89e06452010-06-23 20:24:52 -070052
Adam Powell29ed7572010-07-14 16:24:56 -070053import java.lang.ref.WeakReference;
Adam Powell661c9082010-07-02 10:09:44 -070054import java.util.ArrayList;
55
Adam Powell89e06452010-06-23 20:24:52 -070056/**
57 * ActionBarImpl is the ActionBar implementation used
58 * by devices of all screen sizes. If it detects a compatible decor,
59 * it will split contextual modes across both the ActionBarView at
60 * the top of the screen and a horizontal LinearLayout at the bottom
61 * which is normally hidden.
62 */
63public class ActionBarImpl extends ActionBar {
Adam Powelld8145042011-03-24 14:18:27 -070064 private static final String TAG = "ActionBarImpl";
Adam Powell661c9082010-07-02 10:09:44 -070065
Adam Powelldec9dfd2010-08-09 15:27:54 -070066 private Context mContext;
Adam Powell88ab6972011-07-28 11:25:01 -070067 private Context mThemedContext;
Adam Powell661c9082010-07-02 10:09:44 -070068 private Activity mActivity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070069 private Dialog mDialog;
Adam Powell661c9082010-07-02 10:09:44 -070070
Adam Powell01feaee2011-02-10 15:05:05 -080071 private ActionBarContainer mContainerView;
Adam Powell89e06452010-06-23 20:24:52 -070072 private ActionBarView mActionView;
Adam Powell640a66e2011-04-29 10:18:53 -070073 private ActionBarContextView mContextView;
74 private ActionBarContainer mSplitView;
Adam Powelle6ec7322010-12-07 15:29:26 -080075 private View mContentView;
Adam Powellf8ac6b72011-05-23 18:14:09 -070076 private ScrollingTabContainerView mTabScrollView;
Adam Powell661c9082010-07-02 10:09:44 -070077
78 private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>();
79
Adam Powell661c9082010-07-02 10:09:44 -070080 private TabImpl mSelectedTab;
Adam Powell0c24a552010-11-03 16:44:11 -070081 private int mSavedTabPosition = INVALID_POSITION;
Adam Powell89e06452010-06-23 20:24:52 -070082
Adam Powell060e3ca2011-07-19 20:39:16 -070083 ActionModeImpl mActionMode;
84 ActionMode mDeferredDestroyActionMode;
85 ActionMode.Callback mDeferredModeDestroyCallback;
Adam Powell89e06452010-06-23 20:24:52 -070086
Adam Powell8515ee82010-11-30 14:09:55 -080087 private boolean mLastMenuVisibility;
88 private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
89 new ArrayList<OnMenuVisibilityListener>();
90
Adam Powell89e06452010-06-23 20:24:52 -070091 private static final int CONTEXT_DISPLAY_NORMAL = 0;
92 private static final int CONTEXT_DISPLAY_SPLIT = 1;
93
Adam Powell0c24a552010-11-03 16:44:11 -070094 private static final int INVALID_POSITION = -1;
95
Adam Powell89e06452010-06-23 20:24:52 -070096 private int mContextDisplayMode;
Adam Powellf8ac6b72011-05-23 18:14:09 -070097 private boolean mHasEmbeddedTabs;
Adam Powell0e94b512010-06-29 17:58:20 -070098
99 final Handler mHandler = new Handler();
Adam Powellf8ac6b72011-05-23 18:14:09 -0700100 Runnable mTabSelector;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800101
Adam Powell07e1f982011-03-24 11:11:15 -0700102 private Animator mCurrentShowAnim;
103 private Animator mCurrentModeAnim;
Adam Powell50efbed2011-02-08 16:20:15 -0800104 private boolean mShowHideAnimationEnabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700105 boolean mWasHiddenBeforeMode;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800106
Adam Powell07e1f982011-03-24 11:11:15 -0700107 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800108 @Override
109 public void onAnimationEnd(Animator animation) {
110 if (mContentView != null) {
111 mContentView.setTranslationY(0);
Adam Powell060e3ca2011-07-19 20:39:16 -0700112 mContainerView.setTranslationY(0);
Adam Powelle6ec7322010-12-07 15:29:26 -0800113 }
Adam Powell993a63a2011-08-18 16:35:53 -0700114 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
115 mSplitView.setVisibility(View.GONE);
116 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800117 mContainerView.setVisibility(View.GONE);
Adam Powell01feaee2011-02-10 15:05:05 -0800118 mContainerView.setTransitioning(false);
Adam Powell07e1f982011-03-24 11:11:15 -0700119 mCurrentShowAnim = null;
Adam Powell060e3ca2011-07-19 20:39:16 -0700120 completeDeferredDestroyActionMode();
Adam Powelle6ec7322010-12-07 15:29:26 -0800121 }
122 };
123
Adam Powell07e1f982011-03-24 11:11:15 -0700124 final AnimatorListener mShowListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800125 @Override
126 public void onAnimationEnd(Animator animation) {
Adam Powell07e1f982011-03-24 11:11:15 -0700127 mCurrentShowAnim = null;
Adam Powell45f1e082010-12-10 14:20:13 -0800128 mContainerView.requestLayout();
Adam Powelle6ec7322010-12-07 15:29:26 -0800129 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800130 };
131
Adam Powell661c9082010-07-02 10:09:44 -0700132 public ActionBarImpl(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -0700133 mActivity = activity;
Adam Powelle6ec7322010-12-07 15:29:26 -0800134 Window window = activity.getWindow();
135 View decor = window.getDecorView();
136 init(decor);
137 if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) {
138 mContentView = decor.findViewById(android.R.id.content);
139 }
Adam Powelldec9dfd2010-08-09 15:27:54 -0700140 }
141
142 public ActionBarImpl(Dialog dialog) {
143 mDialog = dialog;
144 init(dialog.getWindow().getDecorView());
145 }
146
147 private void init(View decor) {
148 mContext = decor.getContext();
Adam Powell89e06452010-06-23 20:24:52 -0700149 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
Adam Powell640a66e2011-04-29 10:18:53 -0700150 mContextView = (ActionBarContextView) decor.findViewById(
Adam Powell89e06452010-06-23 20:24:52 -0700151 com.android.internal.R.id.action_context_bar);
Adam Powell01feaee2011-02-10 15:05:05 -0800152 mContainerView = (ActionBarContainer) decor.findViewById(
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800153 com.android.internal.R.id.action_bar_container);
Adam Powell640a66e2011-04-29 10:18:53 -0700154 mSplitView = (ActionBarContainer) decor.findViewById(
155 com.android.internal.R.id.split_action_bar);
Steve Block08f194b2010-08-24 18:20:48 +0100156
Adam Powell640a66e2011-04-29 10:18:53 -0700157 if (mActionView == null || mContextView == null || mContainerView == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700158 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
159 "with a compatible window decor layout");
160 }
Adam Powell0e94b512010-06-29 17:58:20 -0700161
Adam Powell640a66e2011-04-29 10:18:53 -0700162 mActionView.setContextView(mContextView);
Adam Powell9b4bee02011-04-27 19:24:47 -0700163 mContextDisplayMode = mActionView.isSplitActionBar() ?
164 CONTEXT_DISPLAY_SPLIT : CONTEXT_DISPLAY_NORMAL;
Adam Powelldae78242011-04-25 15:23:41 -0700165
Adam Powellc29f4e52011-07-13 20:40:52 -0700166 // Older apps get the home button interaction enabled by default.
167 // Newer apps need to enable it explicitly.
Adam Powell28fa07e2011-07-15 20:17:25 -0700168 setHomeButtonEnabled(mContext.getApplicationInfo().targetSdkVersion <
169 Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Adam Powellf5645cb2011-08-10 22:49:02 -0700170
171 setHasEmbeddedTabs(mContext.getResources().getBoolean(
172 com.android.internal.R.bool.action_bar_embed_tabs));
Adam Powellf8ac6b72011-05-23 18:14:09 -0700173 }
174
175 public void onConfigurationChanged(Configuration newConfig) {
Adam Powellf5645cb2011-08-10 22:49:02 -0700176 setHasEmbeddedTabs(mContext.getResources().getBoolean(
177 com.android.internal.R.bool.action_bar_embed_tabs));
178 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700179
Adam Powellf5645cb2011-08-10 22:49:02 -0700180 private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) {
181 mHasEmbeddedTabs = hasEmbeddedTabs;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700182 // Switch tab layout configuration if needed
183 if (!mHasEmbeddedTabs) {
184 mActionView.setEmbeddedTabView(null);
185 mContainerView.setTabContainer(mTabScrollView);
186 } else {
187 mContainerView.setTabContainer(null);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700188 mActionView.setEmbeddedTabView(mTabScrollView);
189 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700190 final boolean isInTabMode = getNavigationMode() == NAVIGATION_MODE_TABS;
191 if (mTabScrollView != null) {
192 mTabScrollView.setVisibility(isInTabMode ? View.VISIBLE : View.GONE);
193 }
194 mActionView.setCollapsable(!mHasEmbeddedTabs && isInTabMode);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700195 }
196
197 private void ensureTabsExist() {
198 if (mTabScrollView != null) {
199 return;
200 }
201
Adam Powellf5645cb2011-08-10 22:49:02 -0700202 ScrollingTabContainerView tabScroller = new ScrollingTabContainerView(mContext);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700203
204 if (mHasEmbeddedTabs) {
205 tabScroller.setVisibility(View.VISIBLE);
206 mActionView.setEmbeddedTabView(tabScroller);
207 } else {
Adam Powelldae78242011-04-25 15:23:41 -0700208 tabScroller.setVisibility(getNavigationMode() == NAVIGATION_MODE_TABS ?
209 View.VISIBLE : View.GONE);
Adam Powelldae78242011-04-25 15:23:41 -0700210 mContainerView.setTabContainer(tabScroller);
Adam Powelldae78242011-04-25 15:23:41 -0700211 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700212 mTabScrollView = tabScroller;
Adam Powell89e06452010-06-23 20:24:52 -0700213 }
214
Adam Powell060e3ca2011-07-19 20:39:16 -0700215 void completeDeferredDestroyActionMode() {
216 if (mDeferredModeDestroyCallback != null) {
217 mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode);
218 mDeferredDestroyActionMode = null;
219 mDeferredModeDestroyCallback = null;
220 }
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;
Adam Powell07e1f982011-03-24 11:11:15 -0700232 if (!enabled && mCurrentShowAnim != null) {
233 mCurrentShowAnim.end();
Adam Powell50efbed2011-02-08 16:20:15 -0800234 }
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) {
Adam Powellf2423682011-08-11 14:29:45 -0700259 setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId, mActionView, false));
Adam Powell3f476b32011-01-03 19:25:36 -0800260 }
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 Powellc29f4e52011-07-13 20:40:52 -0700288 public void setHomeButtonEnabled(boolean enable) {
289 mActionView.setHomeButtonEnabled(enable);
Adam Powelldae78242011-04-25 15:23:41 -0700290 }
291
292 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700293 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700294 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700295 }
296
297 @Override
298 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700299 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700300 }
301
Adam Powell17809772010-07-21 13:25:11 -0700302 public void setSelectedNavigationItem(int position) {
303 switch (mActionView.getNavigationMode()) {
304 case NAVIGATION_MODE_TABS:
305 selectTab(mTabs.get(position));
306 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700307 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700308 mActionView.setDropdownSelectedPosition(position);
309 break;
310 default:
311 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700312 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700313 }
314 }
315
Adam Powell9ab97872010-10-26 21:47:29 -0700316 public void removeAllTabs() {
317 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700318 }
319
Adam Powell661c9082010-07-02 10:09:44 -0700320 private void cleanupTabs() {
321 if (mSelectedTab != null) {
322 selectTab(null);
323 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700324 mTabs.clear();
Adam Powelld21aa122011-05-27 13:09:52 -0700325 if (mTabScrollView != null) {
326 mTabScrollView.removeAllTabs();
327 }
Adam Powell0c24a552010-11-03 16:44:11 -0700328 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700329 }
330
Adam Powell0e94b512010-06-29 17:58:20 -0700331 public void setTitle(CharSequence title) {
332 mActionView.setTitle(title);
333 }
334
335 public void setSubtitle(CharSequence subtitle) {
336 mActionView.setSubtitle(subtitle);
337 }
338
Adam Powell89e06452010-06-23 20:24:52 -0700339 public void setDisplayOptions(int options) {
340 mActionView.setDisplayOptions(options);
341 }
Adam Powell0e94b512010-06-29 17:58:20 -0700342
Adam Powell89e06452010-06-23 20:24:52 -0700343 public void setDisplayOptions(int options, int mask) {
344 final int current = mActionView.getDisplayOptions();
345 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
346 }
347
348 public void setBackgroundDrawable(Drawable d) {
Adam Powelle8c1e5c2010-12-13 10:49:32 -0800349 mContainerView.setBackgroundDrawable(d);
Adam Powell89e06452010-06-23 20:24:52 -0700350 }
351
Adam Powellef704442010-11-16 14:48:22 -0800352 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700353 return mActionView.getCustomNavigationView();
354 }
355
356 public CharSequence getTitle() {
357 return mActionView.getTitle();
358 }
359
360 public CharSequence getSubtitle() {
361 return mActionView.getSubtitle();
362 }
363
364 public int getNavigationMode() {
365 return mActionView.getNavigationMode();
366 }
367
368 public int getDisplayOptions() {
369 return mActionView.getDisplayOptions();
370 }
371
Adam Powell5d279772010-07-27 16:34:07 -0700372 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700373 boolean wasHidden = false;
Adam Powell5d279772010-07-27 16:34:07 -0700374 if (mActionMode != null) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700375 wasHidden = mWasHiddenBeforeMode;
Adam Powell5d279772010-07-27 16:34:07 -0700376 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700377 }
Adam Powell3461b322010-07-14 11:34:43 -0700378
Adam Powell640a66e2011-04-29 10:18:53 -0700379 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700380 ActionModeImpl mode = new ActionModeImpl(callback);
381 if (mode.dispatchOnCreate()) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700382 mWasHiddenBeforeMode = !isShowing() || wasHidden;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700383 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700384 mContextView.initForMode(mode);
385 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700386 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700387 // TODO animate this
Adam Powell640a66e2011-04-29 10:18:53 -0700388 mSplitView.setVisibility(View.VISIBLE);
Adam Powell89e06452010-06-23 20:24:52 -0700389 }
Adam Powell5d279772010-07-27 16:34:07 -0700390 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700391 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700392 }
Adam Powellac695c62010-07-20 18:19:27 -0700393 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700394 }
395
Adam Powell661c9082010-07-02 10:09:44 -0700396 private void configureTab(Tab tab, int position) {
397 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700398 final ActionBar.TabListener callback = tabi.getCallback();
399
400 if (callback == null) {
401 throw new IllegalStateException("Action Bar Tab must have a Callback");
402 }
Adam Powell661c9082010-07-02 10:09:44 -0700403
404 tabi.setPosition(position);
405 mTabs.add(position, tabi);
406
Adam Powell81b89442010-11-02 17:58:56 -0700407 final int count = mTabs.size();
408 for (int i = position + 1; i < count; i++) {
409 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700410 }
Adam Powell661c9082010-07-02 10:09:44 -0700411 }
412
413 @Override
414 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700415 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700416 }
417
418 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700419 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700420 addTab(tab, position, mTabs.isEmpty());
421 }
422
423 @Override
424 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700425 ensureTabsExist();
426 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700427 configureTab(tab, mTabs.size());
428 if (setSelected) {
429 selectTab(tab);
430 }
431 }
432
433 @Override
434 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700435 ensureTabsExist();
436 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700437 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700438 if (setSelected) {
439 selectTab(tab);
440 }
Adam Powell661c9082010-07-02 10:09:44 -0700441 }
442
443 @Override
444 public Tab newTab() {
445 return new TabImpl();
446 }
447
448 @Override
449 public void removeTab(Tab tab) {
450 removeTabAt(tab.getPosition());
451 }
452
453 @Override
454 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700455 if (mTabScrollView == null) {
456 // No tabs around to remove
457 return;
458 }
459
Adam Powell0c24a552010-11-03 16:44:11 -0700460 int selectedTabPosition = mSelectedTab != null
461 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700462 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700463 TabImpl removedTab = mTabs.remove(position);
464 if (removedTab != null) {
465 removedTab.setPosition(-1);
466 }
Adam Powell661c9082010-07-02 10:09:44 -0700467
468 final int newTabCount = mTabs.size();
469 for (int i = position; i < newTabCount; i++) {
470 mTabs.get(i).setPosition(i);
471 }
472
Adam Powell0c24a552010-11-03 16:44:11 -0700473 if (selectedTabPosition == position) {
474 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
475 }
Adam Powell661c9082010-07-02 10:09:44 -0700476 }
477
478 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700479 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700480 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
481 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
482 return;
483 }
484
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800485 final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction()
Adam Powell0c24a552010-11-03 16:44:11 -0700486 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700487
488 if (mSelectedTab == tab) {
489 if (mSelectedTab != null) {
490 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700491 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700492 }
493 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700494 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700495 if (mSelectedTab != null) {
496 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
497 }
498 mSelectedTab = (TabImpl) tab;
499 if (mSelectedTab != null) {
500 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
501 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700502 }
503
504 if (!trans.isEmpty()) {
505 trans.commit();
506 }
507 }
508
509 @Override
510 public Tab getSelectedTab() {
511 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700512 }
513
Adam Powell6b336f82010-08-10 20:13:01 -0700514 @Override
515 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700516 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700517 }
518
519 @Override
520 public void show() {
Adam Powell07e1f982011-03-24 11:11:15 -0700521 show(true);
522 }
523
524 void show(boolean markHiddenBeforeMode) {
525 if (mCurrentShowAnim != null) {
526 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800527 }
Adam Powelld76cee22011-01-31 15:05:05 -0800528 if (mContainerView.getVisibility() == View.VISIBLE) {
Adam Powell07e1f982011-03-24 11:11:15 -0700529 if (markHiddenBeforeMode) mWasHiddenBeforeMode = false;
Adam Powelld76cee22011-01-31 15:05:05 -0800530 return;
531 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800532 mContainerView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800533
534 if (mShowHideAnimationEnabled) {
535 mContainerView.setAlpha(0);
536 AnimatorSet anim = new AnimatorSet();
537 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 1));
538 if (mContentView != null) {
539 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
540 -mContainerView.getHeight(), 0));
541 mContainerView.setTranslationY(-mContainerView.getHeight());
542 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY", 0));
543 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700544 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell640a66e2011-04-29 10:18:53 -0700545 mSplitView.setAlpha(0);
Adam Powell993a63a2011-08-18 16:35:53 -0700546 mSplitView.setVisibility(View.VISIBLE);
Adam Powell640a66e2011-04-29 10:18:53 -0700547 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 1));
548 }
Adam Powell50efbed2011-02-08 16:20:15 -0800549 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700550 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800551 anim.start();
552 } else {
Adam Powelld8145042011-03-24 14:18:27 -0700553 mContainerView.setAlpha(1);
554 mContainerView.setTranslationY(0);
Adam Powell50efbed2011-02-08 16:20:15 -0800555 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800556 }
Adam Powell6b336f82010-08-10 20:13:01 -0700557 }
558
559 @Override
560 public void hide() {
Adam Powell07e1f982011-03-24 11:11:15 -0700561 if (mCurrentShowAnim != null) {
562 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800563 }
564 if (mContainerView.getVisibility() == View.GONE) {
565 return;
566 }
Adam Powell50efbed2011-02-08 16:20:15 -0800567
568 if (mShowHideAnimationEnabled) {
569 mContainerView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800570 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800571 AnimatorSet anim = new AnimatorSet();
572 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 0));
573 if (mContentView != null) {
574 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
575 0, -mContainerView.getHeight()));
576 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY",
577 -mContainerView.getHeight()));
578 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700579 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700580 mSplitView.setAlpha(1);
581 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 0));
582 }
Adam Powell50efbed2011-02-08 16:20:15 -0800583 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700584 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800585 anim.start();
586 } else {
587 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800588 }
Adam Powell6b336f82010-08-10 20:13:01 -0700589 }
590
591 public boolean isShowing() {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800592 return mContainerView.getVisibility() == View.VISIBLE;
593 }
594
Adam Powell640a66e2011-04-29 10:18:53 -0700595 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700596 if (toActionMode) {
597 show(false);
598 }
Adam Powell07e1f982011-03-24 11:11:15 -0700599 if (mCurrentModeAnim != null) {
600 mCurrentModeAnim.end();
601 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800602
Adam Powell640a66e2011-04-29 10:18:53 -0700603 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
604 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700605 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
606 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
607 }
Adam Powell6b336f82010-08-10 20:13:01 -0700608 }
609
Adam Powell88ab6972011-07-28 11:25:01 -0700610 public Context getThemedContext() {
611 if (mThemedContext == null) {
612 TypedValue outValue = new TypedValue();
613 Resources.Theme currentTheme = mContext.getTheme();
614 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
615 outValue, true);
616 final int targetThemeRes = outValue.resourceId;
617
618 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
619 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
620 } else {
621 mThemedContext = mContext;
622 }
623 }
624 return mThemedContext;
625 }
626
Adam Powell89e06452010-06-23 20:24:52 -0700627 /**
628 * @hide
629 */
Adam Powell5d279772010-07-27 16:34:07 -0700630 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700631 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700632 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700633 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700634
Adam Powell5d279772010-07-27 16:34:07 -0700635 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700636 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700637 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700638 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700639 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700640 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700641
642 @Override
643 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700644 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700645 }
646
Adam Powell89e06452010-06-23 20:24:52 -0700647 @Override
648 public Menu getMenu() {
649 return mMenu;
650 }
651
652 @Override
653 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700654 if (mActionMode != this) {
655 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700656 return;
657 }
658
Adam Powell060e3ca2011-07-19 20:39:16 -0700659 // If we were hidden before the mode was shown, defer the onDestroy
660 // callback until the animation is finished and associated relayout
661 // is about to happen. This lets apps better anticipate visibility
662 // and layout behavior.
663 if (mWasHiddenBeforeMode) {
664 mDeferredDestroyActionMode = this;
665 mDeferredModeDestroyCallback = mCallback;
666 } else {
667 mCallback.onDestroyActionMode(this);
668 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800669 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700670 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700671
672 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700673 mContextView.closeMode();
Adam Powellf6ce6a92011-06-29 10:25:01 -0700674
Adam Powell5d279772010-07-27 16:34:07 -0700675 mActionMode = null;
Adam Powell07e1f982011-03-24 11:11:15 -0700676
677 if (mWasHiddenBeforeMode) {
678 hide();
679 }
Adam Powell89e06452010-06-23 20:24:52 -0700680 }
681
682 @Override
683 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700684 mMenu.stopDispatchingItemsChanged();
685 try {
686 mCallback.onPrepareActionMode(this, mMenu);
687 } finally {
688 mMenu.startDispatchingItemsChanged();
689 }
690 }
691
692 public boolean dispatchOnCreate() {
693 mMenu.stopDispatchingItemsChanged();
694 try {
695 return mCallback.onCreateActionMode(this, mMenu);
696 } finally {
697 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700698 }
699 }
700
701 @Override
702 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700703 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700704 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700705 }
706
707 @Override
708 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700709 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700710 }
711
712 @Override
713 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700714 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700715 }
Adam Powell29ed7572010-07-14 16:24:56 -0700716
717 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700718 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800719 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700720 }
721
722 @Override
723 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800724 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700725 }
726
727 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700728 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700729 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700730 }
731
732 @Override
733 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700734 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700735 }
Adam Powell89e06452010-06-23 20:24:52 -0700736
Adam Powell29ed7572010-07-14 16:24:56 -0700737 @Override
738 public View getCustomView() {
739 return mCustomView != null ? mCustomView.get() : null;
740 }
741
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700742 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800743 if (mCallback != null) {
744 return mCallback.onActionItemClicked(this, item);
745 } else {
746 return false;
747 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700748 }
749
750 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
751 }
752
753 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800754 if (mCallback == null) {
755 return false;
756 }
757
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700758 if (!subMenu.hasVisibleItems()) {
759 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700760 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700761
Adam Powellf2423682011-08-11 14:29:45 -0700762 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700763 return true;
764 }
765
766 public void onCloseSubMenu(SubMenuBuilder menu) {
767 }
768
769 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800770 if (mCallback == null) {
771 return;
772 }
Adam Powellf6148c52010-08-11 21:10:16 -0700773 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700774 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700775 }
Adam Powell661c9082010-07-02 10:09:44 -0700776 }
777
778 /**
779 * @hide
780 */
781 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700782 private ActionBar.TabListener mCallback;
783 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700784 private Drawable mIcon;
785 private CharSequence mText;
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700786 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -0700787 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700788
789 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700790 public Object getTag() {
791 return mTag;
792 }
793
794 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700795 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700796 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700797 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700798 }
799
800 public ActionBar.TabListener getCallback() {
801 return mCallback;
802 }
803
804 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700805 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700806 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700807 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700808 }
809
810 @Override
811 public View getCustomView() {
812 return mCustomView;
813 }
814
815 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700816 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700817 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700818 if (mPosition >= 0) {
819 mTabScrollView.updateTab(mPosition);
820 }
Adam Powell9ab97872010-10-26 21:47:29 -0700821 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700822 }
823
824 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800825 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -0700826 return setCustomView(LayoutInflater.from(getThemedContext())
827 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -0800828 }
829
830 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700831 public Drawable getIcon() {
832 return mIcon;
833 }
834
835 @Override
836 public int getPosition() {
837 return mPosition;
838 }
839
840 public void setPosition(int position) {
841 mPosition = position;
842 }
843
844 @Override
845 public CharSequence getText() {
846 return mText;
847 }
848
849 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700850 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -0700851 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700852 if (mPosition >= 0) {
853 mTabScrollView.updateTab(mPosition);
854 }
Adam Powell9ab97872010-10-26 21:47:29 -0700855 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700856 }
857
858 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800859 public Tab setIcon(int resId) {
860 return setIcon(mContext.getResources().getDrawable(resId));
861 }
862
863 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700864 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -0700865 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700866 if (mPosition >= 0) {
867 mTabScrollView.updateTab(mPosition);
868 }
Adam Powell9ab97872010-10-26 21:47:29 -0700869 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700870 }
871
872 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800873 public Tab setText(int resId) {
874 return setText(mContext.getResources().getText(resId));
875 }
876
877 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700878 public void select() {
879 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700880 }
881 }
Adam Powell9ab97872010-10-26 21:47:29 -0700882
883 @Override
884 public void setCustomView(View view) {
885 mActionView.setCustomNavigationView(view);
886 }
887
888 @Override
889 public void setCustomView(View view, LayoutParams layoutParams) {
890 view.setLayoutParams(layoutParams);
891 mActionView.setCustomNavigationView(view);
892 }
893
894 @Override
Adam Powell8515ee82010-11-30 14:09:55 -0800895 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -0700896 mActionView.setDropdownAdapter(adapter);
897 mActionView.setCallback(callback);
898 }
899
900 @Override
901 public int getSelectedNavigationIndex() {
902 switch (mActionView.getNavigationMode()) {
903 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -0800904 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -0700905 case NAVIGATION_MODE_LIST:
906 return mActionView.getDropdownSelectedPosition();
907 default:
908 return -1;
909 }
910 }
911
912 @Override
913 public int getNavigationItemCount() {
914 switch (mActionView.getNavigationMode()) {
915 case NAVIGATION_MODE_TABS:
916 return mTabs.size();
917 case NAVIGATION_MODE_LIST:
918 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
919 return adapter != null ? adapter.getCount() : 0;
920 default:
921 return 0;
922 }
923 }
924
925 @Override
Adam Powell0c24a552010-11-03 16:44:11 -0700926 public int getTabCount() {
927 return mTabs.size();
928 }
929
930 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700931 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -0700932 final int oldMode = mActionView.getNavigationMode();
933 switch (oldMode) {
934 case NAVIGATION_MODE_TABS:
935 mSavedTabPosition = getSelectedNavigationIndex();
936 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -0700937 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -0700938 break;
939 }
Adam Powell9ab97872010-10-26 21:47:29 -0700940 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -0700941 switch (mode) {
942 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -0700943 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -0700944 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -0700945 if (mSavedTabPosition != INVALID_POSITION) {
946 setSelectedNavigationItem(mSavedTabPosition);
947 mSavedTabPosition = INVALID_POSITION;
948 }
949 break;
950 }
Adam Powelld21aa122011-05-27 13:09:52 -0700951 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -0700952 }
953
954 @Override
955 public Tab getTabAt(int index) {
956 return mTabs.get(index);
957 }
Adam Powell0c24a552010-11-03 16:44:11 -0700958
Adam Powell0c24a552010-11-03 16:44:11 -0700959
Adam Powell1969b872011-03-22 11:52:48 -0700960 @Override
961 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -0700962 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -0700963 }
Adam Powell0c24a552010-11-03 16:44:11 -0700964
Adam Powell1969b872011-03-22 11:52:48 -0700965 @Override
966 public void setIcon(Drawable icon) {
967 mActionView.setIcon(icon);
968 }
969
970 @Override
971 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -0700972 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -0700973 }
974
975 @Override
976 public void setLogo(Drawable logo) {
977 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -0700978 }
Adam Powell89e06452010-06-23 20:24:52 -0700979}