blob: ccca22ef2d13c53e6bbbee928bd45cb0bbd38d05 [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 Powellf88b9152011-09-07 14:54:32 -0700349 mContainerView.setPrimaryBackground(d);
350 }
351
352 public void setStackedBackgroundDrawable(Drawable d) {
353 mContainerView.setStackedBackground(d);
354 }
355
356 public void setSplitBackgroundDrawable(Drawable d) {
357 if (mSplitView != null) {
358 mSplitView.setSplitBackground(d);
359 }
Adam Powell89e06452010-06-23 20:24:52 -0700360 }
361
Adam Powellef704442010-11-16 14:48:22 -0800362 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700363 return mActionView.getCustomNavigationView();
364 }
365
366 public CharSequence getTitle() {
367 return mActionView.getTitle();
368 }
369
370 public CharSequence getSubtitle() {
371 return mActionView.getSubtitle();
372 }
373
374 public int getNavigationMode() {
375 return mActionView.getNavigationMode();
376 }
377
378 public int getDisplayOptions() {
379 return mActionView.getDisplayOptions();
380 }
381
Adam Powell5d279772010-07-27 16:34:07 -0700382 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700383 boolean wasHidden = false;
Adam Powell5d279772010-07-27 16:34:07 -0700384 if (mActionMode != null) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700385 wasHidden = mWasHiddenBeforeMode;
Adam Powell5d279772010-07-27 16:34:07 -0700386 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700387 }
Adam Powell3461b322010-07-14 11:34:43 -0700388
Adam Powell640a66e2011-04-29 10:18:53 -0700389 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700390 ActionModeImpl mode = new ActionModeImpl(callback);
391 if (mode.dispatchOnCreate()) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700392 mWasHiddenBeforeMode = !isShowing() || wasHidden;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700393 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700394 mContextView.initForMode(mode);
395 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700396 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700397 // TODO animate this
Adam Powell640a66e2011-04-29 10:18:53 -0700398 mSplitView.setVisibility(View.VISIBLE);
Adam Powell89e06452010-06-23 20:24:52 -0700399 }
Adam Powell5d279772010-07-27 16:34:07 -0700400 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700401 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700402 }
Adam Powellac695c62010-07-20 18:19:27 -0700403 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700404 }
405
Adam Powell661c9082010-07-02 10:09:44 -0700406 private void configureTab(Tab tab, int position) {
407 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700408 final ActionBar.TabListener callback = tabi.getCallback();
409
410 if (callback == null) {
411 throw new IllegalStateException("Action Bar Tab must have a Callback");
412 }
Adam Powell661c9082010-07-02 10:09:44 -0700413
414 tabi.setPosition(position);
415 mTabs.add(position, tabi);
416
Adam Powell81b89442010-11-02 17:58:56 -0700417 final int count = mTabs.size();
418 for (int i = position + 1; i < count; i++) {
419 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700420 }
Adam Powell661c9082010-07-02 10:09:44 -0700421 }
422
423 @Override
424 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700425 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700426 }
427
428 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700429 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700430 addTab(tab, position, mTabs.isEmpty());
431 }
432
433 @Override
434 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700435 ensureTabsExist();
436 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700437 configureTab(tab, mTabs.size());
438 if (setSelected) {
439 selectTab(tab);
440 }
441 }
442
443 @Override
444 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700445 ensureTabsExist();
446 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700447 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700448 if (setSelected) {
449 selectTab(tab);
450 }
Adam Powell661c9082010-07-02 10:09:44 -0700451 }
452
453 @Override
454 public Tab newTab() {
455 return new TabImpl();
456 }
457
458 @Override
459 public void removeTab(Tab tab) {
460 removeTabAt(tab.getPosition());
461 }
462
463 @Override
464 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700465 if (mTabScrollView == null) {
466 // No tabs around to remove
467 return;
468 }
469
Adam Powell0c24a552010-11-03 16:44:11 -0700470 int selectedTabPosition = mSelectedTab != null
471 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700472 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700473 TabImpl removedTab = mTabs.remove(position);
474 if (removedTab != null) {
475 removedTab.setPosition(-1);
476 }
Adam Powell661c9082010-07-02 10:09:44 -0700477
478 final int newTabCount = mTabs.size();
479 for (int i = position; i < newTabCount; i++) {
480 mTabs.get(i).setPosition(i);
481 }
482
Adam Powell0c24a552010-11-03 16:44:11 -0700483 if (selectedTabPosition == position) {
484 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
485 }
Adam Powell661c9082010-07-02 10:09:44 -0700486 }
487
488 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700489 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700490 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
491 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
492 return;
493 }
494
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800495 final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction()
Adam Powell0c24a552010-11-03 16:44:11 -0700496 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700497
498 if (mSelectedTab == tab) {
499 if (mSelectedTab != null) {
500 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700501 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700502 }
503 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700504 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700505 if (mSelectedTab != null) {
506 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
507 }
508 mSelectedTab = (TabImpl) tab;
509 if (mSelectedTab != null) {
510 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
511 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700512 }
513
514 if (!trans.isEmpty()) {
515 trans.commit();
516 }
517 }
518
519 @Override
520 public Tab getSelectedTab() {
521 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700522 }
523
Adam Powell6b336f82010-08-10 20:13:01 -0700524 @Override
525 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700526 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700527 }
528
529 @Override
530 public void show() {
Adam Powell07e1f982011-03-24 11:11:15 -0700531 show(true);
532 }
533
534 void show(boolean markHiddenBeforeMode) {
535 if (mCurrentShowAnim != null) {
536 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800537 }
Adam Powelld76cee22011-01-31 15:05:05 -0800538 if (mContainerView.getVisibility() == View.VISIBLE) {
Adam Powell07e1f982011-03-24 11:11:15 -0700539 if (markHiddenBeforeMode) mWasHiddenBeforeMode = false;
Adam Powelld76cee22011-01-31 15:05:05 -0800540 return;
541 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800542 mContainerView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800543
544 if (mShowHideAnimationEnabled) {
545 mContainerView.setAlpha(0);
546 AnimatorSet anim = new AnimatorSet();
547 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 1));
548 if (mContentView != null) {
549 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
550 -mContainerView.getHeight(), 0));
551 mContainerView.setTranslationY(-mContainerView.getHeight());
552 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY", 0));
553 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700554 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell640a66e2011-04-29 10:18:53 -0700555 mSplitView.setAlpha(0);
Adam Powell993a63a2011-08-18 16:35:53 -0700556 mSplitView.setVisibility(View.VISIBLE);
Adam Powell640a66e2011-04-29 10:18:53 -0700557 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 1));
558 }
Adam Powell50efbed2011-02-08 16:20:15 -0800559 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700560 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800561 anim.start();
562 } else {
Adam Powelld8145042011-03-24 14:18:27 -0700563 mContainerView.setAlpha(1);
564 mContainerView.setTranslationY(0);
Adam Powell50efbed2011-02-08 16:20:15 -0800565 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800566 }
Adam Powell6b336f82010-08-10 20:13:01 -0700567 }
568
569 @Override
570 public void hide() {
Adam Powell07e1f982011-03-24 11:11:15 -0700571 if (mCurrentShowAnim != null) {
572 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800573 }
574 if (mContainerView.getVisibility() == View.GONE) {
575 return;
576 }
Adam Powell50efbed2011-02-08 16:20:15 -0800577
578 if (mShowHideAnimationEnabled) {
579 mContainerView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800580 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800581 AnimatorSet anim = new AnimatorSet();
582 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 0));
583 if (mContentView != null) {
584 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
585 0, -mContainerView.getHeight()));
586 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY",
587 -mContainerView.getHeight()));
588 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700589 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700590 mSplitView.setAlpha(1);
591 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 0));
592 }
Adam Powell50efbed2011-02-08 16:20:15 -0800593 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700594 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800595 anim.start();
596 } else {
597 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800598 }
Adam Powell6b336f82010-08-10 20:13:01 -0700599 }
600
601 public boolean isShowing() {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800602 return mContainerView.getVisibility() == View.VISIBLE;
603 }
604
Adam Powell640a66e2011-04-29 10:18:53 -0700605 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700606 if (toActionMode) {
607 show(false);
608 }
Adam Powell07e1f982011-03-24 11:11:15 -0700609 if (mCurrentModeAnim != null) {
610 mCurrentModeAnim.end();
611 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800612
Adam Powell640a66e2011-04-29 10:18:53 -0700613 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
614 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700615 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
616 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
617 }
Adam Powell6b336f82010-08-10 20:13:01 -0700618 }
619
Adam Powell88ab6972011-07-28 11:25:01 -0700620 public Context getThemedContext() {
621 if (mThemedContext == null) {
622 TypedValue outValue = new TypedValue();
623 Resources.Theme currentTheme = mContext.getTheme();
624 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
625 outValue, true);
626 final int targetThemeRes = outValue.resourceId;
627
628 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
629 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
630 } else {
631 mThemedContext = mContext;
632 }
633 }
634 return mThemedContext;
635 }
636
Adam Powell89e06452010-06-23 20:24:52 -0700637 /**
638 * @hide
639 */
Adam Powell5d279772010-07-27 16:34:07 -0700640 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700641 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700642 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700643 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700644
Adam Powell5d279772010-07-27 16:34:07 -0700645 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700646 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700647 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700648 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700649 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700650 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700651
652 @Override
653 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700654 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700655 }
656
Adam Powell89e06452010-06-23 20:24:52 -0700657 @Override
658 public Menu getMenu() {
659 return mMenu;
660 }
661
662 @Override
663 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700664 if (mActionMode != this) {
665 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700666 return;
667 }
668
Adam Powell060e3ca2011-07-19 20:39:16 -0700669 // If we were hidden before the mode was shown, defer the onDestroy
670 // callback until the animation is finished and associated relayout
671 // is about to happen. This lets apps better anticipate visibility
672 // and layout behavior.
673 if (mWasHiddenBeforeMode) {
674 mDeferredDestroyActionMode = this;
675 mDeferredModeDestroyCallback = mCallback;
676 } else {
677 mCallback.onDestroyActionMode(this);
678 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800679 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700680 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700681
682 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700683 mContextView.closeMode();
Adam Powellf6ce6a92011-06-29 10:25:01 -0700684
Adam Powell5d279772010-07-27 16:34:07 -0700685 mActionMode = null;
Adam Powell07e1f982011-03-24 11:11:15 -0700686
687 if (mWasHiddenBeforeMode) {
688 hide();
689 }
Adam Powell89e06452010-06-23 20:24:52 -0700690 }
691
692 @Override
693 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700694 mMenu.stopDispatchingItemsChanged();
695 try {
696 mCallback.onPrepareActionMode(this, mMenu);
697 } finally {
698 mMenu.startDispatchingItemsChanged();
699 }
700 }
701
702 public boolean dispatchOnCreate() {
703 mMenu.stopDispatchingItemsChanged();
704 try {
705 return mCallback.onCreateActionMode(this, mMenu);
706 } finally {
707 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700708 }
709 }
710
711 @Override
712 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700713 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700714 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700715 }
716
717 @Override
718 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700719 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700720 }
721
722 @Override
723 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700724 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700725 }
Adam Powell29ed7572010-07-14 16:24:56 -0700726
727 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700728 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800729 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700730 }
731
732 @Override
733 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800734 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700735 }
736
737 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700738 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700739 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700740 }
741
742 @Override
743 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700744 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700745 }
Adam Powell89e06452010-06-23 20:24:52 -0700746
Adam Powell29ed7572010-07-14 16:24:56 -0700747 @Override
748 public View getCustomView() {
749 return mCustomView != null ? mCustomView.get() : null;
750 }
751
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700752 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800753 if (mCallback != null) {
754 return mCallback.onActionItemClicked(this, item);
755 } else {
756 return false;
757 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700758 }
759
760 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
761 }
762
763 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800764 if (mCallback == null) {
765 return false;
766 }
767
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700768 if (!subMenu.hasVisibleItems()) {
769 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700770 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700771
Adam Powellf2423682011-08-11 14:29:45 -0700772 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700773 return true;
774 }
775
776 public void onCloseSubMenu(SubMenuBuilder menu) {
777 }
778
779 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800780 if (mCallback == null) {
781 return;
782 }
Adam Powellf6148c52010-08-11 21:10:16 -0700783 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700784 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700785 }
Adam Powell661c9082010-07-02 10:09:44 -0700786 }
787
788 /**
789 * @hide
790 */
791 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700792 private ActionBar.TabListener mCallback;
793 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700794 private Drawable mIcon;
795 private CharSequence mText;
Adam Powell94e56ef2011-09-06 21:22:22 -0700796 private CharSequence mContentDesc;
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700797 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -0700798 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700799
800 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700801 public Object getTag() {
802 return mTag;
803 }
804
805 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700806 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700807 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700808 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700809 }
810
811 public ActionBar.TabListener getCallback() {
812 return mCallback;
813 }
814
815 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700816 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700817 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700818 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700819 }
820
821 @Override
822 public View getCustomView() {
823 return mCustomView;
824 }
825
826 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700827 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700828 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700829 if (mPosition >= 0) {
830 mTabScrollView.updateTab(mPosition);
831 }
Adam Powell9ab97872010-10-26 21:47:29 -0700832 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700833 }
834
835 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800836 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -0700837 return setCustomView(LayoutInflater.from(getThemedContext())
838 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -0800839 }
840
841 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700842 public Drawable getIcon() {
843 return mIcon;
844 }
845
846 @Override
847 public int getPosition() {
848 return mPosition;
849 }
850
851 public void setPosition(int position) {
852 mPosition = position;
853 }
854
855 @Override
856 public CharSequence getText() {
857 return mText;
858 }
859
860 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700861 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -0700862 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700863 if (mPosition >= 0) {
864 mTabScrollView.updateTab(mPosition);
865 }
Adam Powell9ab97872010-10-26 21:47:29 -0700866 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700867 }
868
869 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800870 public Tab setIcon(int resId) {
871 return setIcon(mContext.getResources().getDrawable(resId));
872 }
873
874 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700875 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -0700876 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700877 if (mPosition >= 0) {
878 mTabScrollView.updateTab(mPosition);
879 }
Adam Powell9ab97872010-10-26 21:47:29 -0700880 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700881 }
882
883 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800884 public Tab setText(int resId) {
885 return setText(mContext.getResources().getText(resId));
886 }
887
888 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700889 public void select() {
890 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700891 }
Adam Powell94e56ef2011-09-06 21:22:22 -0700892
893 @Override
894 public Tab setContentDescription(int resId) {
895 return setContentDescription(mContext.getResources().getText(resId));
896 }
897
898 @Override
899 public Tab setContentDescription(CharSequence contentDesc) {
900 mContentDesc = contentDesc;
901 if (mPosition >= 0) {
902 mTabScrollView.updateTab(mPosition);
903 }
904 return this;
905 }
906
907 @Override
908 public CharSequence getContentDescription() {
909 return mContentDesc;
910 }
Adam Powell89e06452010-06-23 20:24:52 -0700911 }
Adam Powell9ab97872010-10-26 21:47:29 -0700912
913 @Override
914 public void setCustomView(View view) {
915 mActionView.setCustomNavigationView(view);
916 }
917
918 @Override
919 public void setCustomView(View view, LayoutParams layoutParams) {
920 view.setLayoutParams(layoutParams);
921 mActionView.setCustomNavigationView(view);
922 }
923
924 @Override
Adam Powell8515ee82010-11-30 14:09:55 -0800925 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -0700926 mActionView.setDropdownAdapter(adapter);
927 mActionView.setCallback(callback);
928 }
929
930 @Override
931 public int getSelectedNavigationIndex() {
932 switch (mActionView.getNavigationMode()) {
933 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -0800934 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -0700935 case NAVIGATION_MODE_LIST:
936 return mActionView.getDropdownSelectedPosition();
937 default:
938 return -1;
939 }
940 }
941
942 @Override
943 public int getNavigationItemCount() {
944 switch (mActionView.getNavigationMode()) {
945 case NAVIGATION_MODE_TABS:
946 return mTabs.size();
947 case NAVIGATION_MODE_LIST:
948 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
949 return adapter != null ? adapter.getCount() : 0;
950 default:
951 return 0;
952 }
953 }
954
955 @Override
Adam Powell0c24a552010-11-03 16:44:11 -0700956 public int getTabCount() {
957 return mTabs.size();
958 }
959
960 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700961 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -0700962 final int oldMode = mActionView.getNavigationMode();
963 switch (oldMode) {
964 case NAVIGATION_MODE_TABS:
965 mSavedTabPosition = getSelectedNavigationIndex();
966 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -0700967 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -0700968 break;
969 }
Adam Powell9ab97872010-10-26 21:47:29 -0700970 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -0700971 switch (mode) {
972 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -0700973 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -0700974 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -0700975 if (mSavedTabPosition != INVALID_POSITION) {
976 setSelectedNavigationItem(mSavedTabPosition);
977 mSavedTabPosition = INVALID_POSITION;
978 }
979 break;
980 }
Adam Powelld21aa122011-05-27 13:09:52 -0700981 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -0700982 }
983
984 @Override
985 public Tab getTabAt(int index) {
986 return mTabs.get(index);
987 }
Adam Powell0c24a552010-11-03 16:44:11 -0700988
Adam Powell0c24a552010-11-03 16:44:11 -0700989
Adam Powell1969b872011-03-22 11:52:48 -0700990 @Override
991 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -0700992 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -0700993 }
Adam Powell0c24a552010-11-03 16:44:11 -0700994
Adam Powell1969b872011-03-22 11:52:48 -0700995 @Override
996 public void setIcon(Drawable icon) {
997 mActionView.setIcon(icon);
998 }
999
1000 @Override
1001 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001002 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001003 }
1004
1005 @Override
1006 public void setLogo(Drawable logo) {
1007 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -07001008 }
Adam Powell89e06452010-06-23 20:24:52 -07001009}