blob: 31360e104390d6fcb08e03f43c7ba6cce61dc2ca [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 }
114 mContainerView.setVisibility(View.GONE);
Adam Powell01feaee2011-02-10 15:05:05 -0800115 mContainerView.setTransitioning(false);
Adam Powell07e1f982011-03-24 11:11:15 -0700116 mCurrentShowAnim = null;
Adam Powell060e3ca2011-07-19 20:39:16 -0700117 completeDeferredDestroyActionMode();
Adam Powelle6ec7322010-12-07 15:29:26 -0800118 }
119 };
120
Adam Powell07e1f982011-03-24 11:11:15 -0700121 final AnimatorListener mShowListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800122 @Override
123 public void onAnimationEnd(Animator animation) {
Adam Powell07e1f982011-03-24 11:11:15 -0700124 mCurrentShowAnim = null;
Adam Powell45f1e082010-12-10 14:20:13 -0800125 mContainerView.requestLayout();
Adam Powelle6ec7322010-12-07 15:29:26 -0800126 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800127 };
128
Adam Powell661c9082010-07-02 10:09:44 -0700129 public ActionBarImpl(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -0700130 mActivity = activity;
Adam Powelle6ec7322010-12-07 15:29:26 -0800131 Window window = activity.getWindow();
132 View decor = window.getDecorView();
133 init(decor);
134 if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) {
135 mContentView = decor.findViewById(android.R.id.content);
136 }
Adam Powelldec9dfd2010-08-09 15:27:54 -0700137 }
138
139 public ActionBarImpl(Dialog dialog) {
140 mDialog = dialog;
141 init(dialog.getWindow().getDecorView());
142 }
143
144 private void init(View decor) {
145 mContext = decor.getContext();
Adam Powell89e06452010-06-23 20:24:52 -0700146 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
Adam Powell640a66e2011-04-29 10:18:53 -0700147 mContextView = (ActionBarContextView) decor.findViewById(
Adam Powell89e06452010-06-23 20:24:52 -0700148 com.android.internal.R.id.action_context_bar);
Adam Powell01feaee2011-02-10 15:05:05 -0800149 mContainerView = (ActionBarContainer) decor.findViewById(
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800150 com.android.internal.R.id.action_bar_container);
Adam Powell640a66e2011-04-29 10:18:53 -0700151 mSplitView = (ActionBarContainer) decor.findViewById(
152 com.android.internal.R.id.split_action_bar);
Steve Block08f194b2010-08-24 18:20:48 +0100153
Adam Powell640a66e2011-04-29 10:18:53 -0700154 if (mActionView == null || mContextView == null || mContainerView == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700155 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
156 "with a compatible window decor layout");
157 }
Adam Powell0e94b512010-06-29 17:58:20 -0700158
Adam Powell640a66e2011-04-29 10:18:53 -0700159 mActionView.setContextView(mContextView);
Adam Powell9b4bee02011-04-27 19:24:47 -0700160 mContextDisplayMode = mActionView.isSplitActionBar() ?
161 CONTEXT_DISPLAY_SPLIT : CONTEXT_DISPLAY_NORMAL;
Adam Powelldae78242011-04-25 15:23:41 -0700162
Adam Powellc29f4e52011-07-13 20:40:52 -0700163 // Older apps get the home button interaction enabled by default.
164 // Newer apps need to enable it explicitly.
Adam Powell28fa07e2011-07-15 20:17:25 -0700165 setHomeButtonEnabled(mContext.getApplicationInfo().targetSdkVersion <
166 Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Adam Powellf5645cb2011-08-10 22:49:02 -0700167
168 setHasEmbeddedTabs(mContext.getResources().getBoolean(
169 com.android.internal.R.bool.action_bar_embed_tabs));
Adam Powellf8ac6b72011-05-23 18:14:09 -0700170 }
171
172 public void onConfigurationChanged(Configuration newConfig) {
Adam Powellf5645cb2011-08-10 22:49:02 -0700173 setHasEmbeddedTabs(mContext.getResources().getBoolean(
174 com.android.internal.R.bool.action_bar_embed_tabs));
175 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700176
Adam Powellf5645cb2011-08-10 22:49:02 -0700177 private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) {
178 mHasEmbeddedTabs = hasEmbeddedTabs;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700179 // Switch tab layout configuration if needed
180 if (!mHasEmbeddedTabs) {
181 mActionView.setEmbeddedTabView(null);
182 mContainerView.setTabContainer(mTabScrollView);
183 } else {
184 mContainerView.setTabContainer(null);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700185 mActionView.setEmbeddedTabView(mTabScrollView);
186 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700187 final boolean isInTabMode = getNavigationMode() == NAVIGATION_MODE_TABS;
188 if (mTabScrollView != null) {
189 mTabScrollView.setVisibility(isInTabMode ? View.VISIBLE : View.GONE);
190 }
191 mActionView.setCollapsable(!mHasEmbeddedTabs && isInTabMode);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700192 }
193
194 private void ensureTabsExist() {
195 if (mTabScrollView != null) {
196 return;
197 }
198
Adam Powellf5645cb2011-08-10 22:49:02 -0700199 ScrollingTabContainerView tabScroller = new ScrollingTabContainerView(mContext);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700200
201 if (mHasEmbeddedTabs) {
202 tabScroller.setVisibility(View.VISIBLE);
203 mActionView.setEmbeddedTabView(tabScroller);
204 } else {
Adam Powelldae78242011-04-25 15:23:41 -0700205 tabScroller.setVisibility(getNavigationMode() == NAVIGATION_MODE_TABS ?
206 View.VISIBLE : View.GONE);
Adam Powelldae78242011-04-25 15:23:41 -0700207 mContainerView.setTabContainer(tabScroller);
Adam Powelldae78242011-04-25 15:23:41 -0700208 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700209 mTabScrollView = tabScroller;
Adam Powell89e06452010-06-23 20:24:52 -0700210 }
211
Adam Powell060e3ca2011-07-19 20:39:16 -0700212 void completeDeferredDestroyActionMode() {
213 if (mDeferredModeDestroyCallback != null) {
214 mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode);
215 mDeferredDestroyActionMode = null;
216 mDeferredModeDestroyCallback = null;
217 }
218 }
219
Adam Powell50efbed2011-02-08 16:20:15 -0800220 /**
221 * Enables or disables animation between show/hide states.
222 * If animation is disabled using this method, animations in progress
223 * will be finished.
224 *
225 * @param enabled true to animate, false to not animate.
226 */
227 public void setShowHideAnimationEnabled(boolean enabled) {
228 mShowHideAnimationEnabled = enabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700229 if (!enabled && mCurrentShowAnim != null) {
230 mCurrentShowAnim.end();
Adam Powell50efbed2011-02-08 16:20:15 -0800231 }
232 }
233
Adam Powell8515ee82010-11-30 14:09:55 -0800234 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
235 mMenuVisibilityListeners.add(listener);
236 }
237
238 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
239 mMenuVisibilityListeners.remove(listener);
240 }
241
242 public void dispatchMenuVisibilityChanged(boolean isVisible) {
243 if (isVisible == mLastMenuVisibility) {
244 return;
245 }
246 mLastMenuVisibility = isVisible;
247
248 final int count = mMenuVisibilityListeners.size();
249 for (int i = 0; i < count; i++) {
250 mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
251 }
252 }
253
Adam Powella66c7b02010-07-28 15:28:25 -0700254 @Override
Adam Powell3f476b32011-01-03 19:25:36 -0800255 public void setCustomView(int resId) {
Adam Powellf2423682011-08-11 14:29:45 -0700256 setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId, mActionView, false));
Adam Powell3f476b32011-01-03 19:25:36 -0800257 }
258
259 @Override
260 public void setDisplayUseLogoEnabled(boolean useLogo) {
261 setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
262 }
263
264 @Override
265 public void setDisplayShowHomeEnabled(boolean showHome) {
266 setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
267 }
268
269 @Override
270 public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
271 setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
272 }
273
274 @Override
275 public void setDisplayShowTitleEnabled(boolean showTitle) {
276 setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
277 }
278
279 @Override
280 public void setDisplayShowCustomEnabled(boolean showCustom) {
281 setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
282 }
283
284 @Override
Adam Powellc29f4e52011-07-13 20:40:52 -0700285 public void setHomeButtonEnabled(boolean enable) {
286 mActionView.setHomeButtonEnabled(enable);
Adam Powelldae78242011-04-25 15:23:41 -0700287 }
288
289 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700290 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700291 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700292 }
293
294 @Override
295 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700296 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700297 }
298
Adam Powell17809772010-07-21 13:25:11 -0700299 public void setSelectedNavigationItem(int position) {
300 switch (mActionView.getNavigationMode()) {
301 case NAVIGATION_MODE_TABS:
302 selectTab(mTabs.get(position));
303 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700304 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700305 mActionView.setDropdownSelectedPosition(position);
306 break;
307 default:
308 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700309 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700310 }
311 }
312
Adam Powell9ab97872010-10-26 21:47:29 -0700313 public void removeAllTabs() {
314 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700315 }
316
Adam Powell661c9082010-07-02 10:09:44 -0700317 private void cleanupTabs() {
318 if (mSelectedTab != null) {
319 selectTab(null);
320 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700321 mTabs.clear();
Adam Powelld21aa122011-05-27 13:09:52 -0700322 if (mTabScrollView != null) {
323 mTabScrollView.removeAllTabs();
324 }
Adam Powell0c24a552010-11-03 16:44:11 -0700325 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700326 }
327
Adam Powell0e94b512010-06-29 17:58:20 -0700328 public void setTitle(CharSequence title) {
329 mActionView.setTitle(title);
330 }
331
332 public void setSubtitle(CharSequence subtitle) {
333 mActionView.setSubtitle(subtitle);
334 }
335
Adam Powell89e06452010-06-23 20:24:52 -0700336 public void setDisplayOptions(int options) {
337 mActionView.setDisplayOptions(options);
338 }
Adam Powell0e94b512010-06-29 17:58:20 -0700339
Adam Powell89e06452010-06-23 20:24:52 -0700340 public void setDisplayOptions(int options, int mask) {
341 final int current = mActionView.getDisplayOptions();
342 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
343 }
344
345 public void setBackgroundDrawable(Drawable d) {
Adam Powelle8c1e5c2010-12-13 10:49:32 -0800346 mContainerView.setBackgroundDrawable(d);
Adam Powell89e06452010-06-23 20:24:52 -0700347 }
348
Adam Powellef704442010-11-16 14:48:22 -0800349 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700350 return mActionView.getCustomNavigationView();
351 }
352
353 public CharSequence getTitle() {
354 return mActionView.getTitle();
355 }
356
357 public CharSequence getSubtitle() {
358 return mActionView.getSubtitle();
359 }
360
361 public int getNavigationMode() {
362 return mActionView.getNavigationMode();
363 }
364
365 public int getDisplayOptions() {
366 return mActionView.getDisplayOptions();
367 }
368
Adam Powell5d279772010-07-27 16:34:07 -0700369 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700370 boolean wasHidden = false;
Adam Powell5d279772010-07-27 16:34:07 -0700371 if (mActionMode != null) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700372 wasHidden = mWasHiddenBeforeMode;
Adam Powell5d279772010-07-27 16:34:07 -0700373 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700374 }
Adam Powell3461b322010-07-14 11:34:43 -0700375
Adam Powell640a66e2011-04-29 10:18:53 -0700376 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700377 ActionModeImpl mode = new ActionModeImpl(callback);
378 if (mode.dispatchOnCreate()) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700379 mWasHiddenBeforeMode = !isShowing() || wasHidden;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700380 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700381 mContextView.initForMode(mode);
382 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700383 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700384 // TODO animate this
Adam Powell640a66e2011-04-29 10:18:53 -0700385 mSplitView.setVisibility(View.VISIBLE);
Adam Powell89e06452010-06-23 20:24:52 -0700386 }
Adam Powell5d279772010-07-27 16:34:07 -0700387 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700388 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700389 }
Adam Powellac695c62010-07-20 18:19:27 -0700390 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700391 }
392
Adam Powell661c9082010-07-02 10:09:44 -0700393 private void configureTab(Tab tab, int position) {
394 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700395 final ActionBar.TabListener callback = tabi.getCallback();
396
397 if (callback == null) {
398 throw new IllegalStateException("Action Bar Tab must have a Callback");
399 }
Adam Powell661c9082010-07-02 10:09:44 -0700400
401 tabi.setPosition(position);
402 mTabs.add(position, tabi);
403
Adam Powell81b89442010-11-02 17:58:56 -0700404 final int count = mTabs.size();
405 for (int i = position + 1; i < count; i++) {
406 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700407 }
Adam Powell661c9082010-07-02 10:09:44 -0700408 }
409
410 @Override
411 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700412 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700413 }
414
415 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700416 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700417 addTab(tab, position, mTabs.isEmpty());
418 }
419
420 @Override
421 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700422 ensureTabsExist();
423 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700424 configureTab(tab, mTabs.size());
425 if (setSelected) {
426 selectTab(tab);
427 }
428 }
429
430 @Override
431 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700432 ensureTabsExist();
433 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700434 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700435 if (setSelected) {
436 selectTab(tab);
437 }
Adam Powell661c9082010-07-02 10:09:44 -0700438 }
439
440 @Override
441 public Tab newTab() {
442 return new TabImpl();
443 }
444
445 @Override
446 public void removeTab(Tab tab) {
447 removeTabAt(tab.getPosition());
448 }
449
450 @Override
451 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700452 if (mTabScrollView == null) {
453 // No tabs around to remove
454 return;
455 }
456
Adam Powell0c24a552010-11-03 16:44:11 -0700457 int selectedTabPosition = mSelectedTab != null
458 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700459 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700460 TabImpl removedTab = mTabs.remove(position);
461 if (removedTab != null) {
462 removedTab.setPosition(-1);
463 }
Adam Powell661c9082010-07-02 10:09:44 -0700464
465 final int newTabCount = mTabs.size();
466 for (int i = position; i < newTabCount; i++) {
467 mTabs.get(i).setPosition(i);
468 }
469
Adam Powell0c24a552010-11-03 16:44:11 -0700470 if (selectedTabPosition == position) {
471 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
472 }
Adam Powell661c9082010-07-02 10:09:44 -0700473 }
474
475 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700476 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700477 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
478 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
479 return;
480 }
481
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800482 final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction()
Adam Powell0c24a552010-11-03 16:44:11 -0700483 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700484
485 if (mSelectedTab == tab) {
486 if (mSelectedTab != null) {
487 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700488 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700489 }
490 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700491 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700492 if (mSelectedTab != null) {
493 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
494 }
495 mSelectedTab = (TabImpl) tab;
496 if (mSelectedTab != null) {
497 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
498 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700499 }
500
501 if (!trans.isEmpty()) {
502 trans.commit();
503 }
504 }
505
506 @Override
507 public Tab getSelectedTab() {
508 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700509 }
510
Adam Powell6b336f82010-08-10 20:13:01 -0700511 @Override
512 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700513 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700514 }
515
516 @Override
517 public void show() {
Adam Powell07e1f982011-03-24 11:11:15 -0700518 show(true);
519 }
520
521 void show(boolean markHiddenBeforeMode) {
522 if (mCurrentShowAnim != null) {
523 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800524 }
Adam Powelld76cee22011-01-31 15:05:05 -0800525 if (mContainerView.getVisibility() == View.VISIBLE) {
Adam Powell07e1f982011-03-24 11:11:15 -0700526 if (markHiddenBeforeMode) mWasHiddenBeforeMode = false;
Adam Powelld76cee22011-01-31 15:05:05 -0800527 return;
528 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800529 mContainerView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800530
531 if (mShowHideAnimationEnabled) {
532 mContainerView.setAlpha(0);
533 AnimatorSet anim = new AnimatorSet();
534 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 1));
535 if (mContentView != null) {
536 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
537 -mContainerView.getHeight(), 0));
538 mContainerView.setTranslationY(-mContainerView.getHeight());
539 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY", 0));
540 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700541 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell640a66e2011-04-29 10:18:53 -0700542 mSplitView.setAlpha(0);
543 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 1));
544 }
Adam Powell50efbed2011-02-08 16:20:15 -0800545 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700546 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800547 anim.start();
548 } else {
Adam Powelld8145042011-03-24 14:18:27 -0700549 mContainerView.setAlpha(1);
550 mContainerView.setTranslationY(0);
Adam Powell50efbed2011-02-08 16:20:15 -0800551 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800552 }
Adam Powell6b336f82010-08-10 20:13:01 -0700553 }
554
555 @Override
556 public void hide() {
Adam Powell07e1f982011-03-24 11:11:15 -0700557 if (mCurrentShowAnim != null) {
558 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800559 }
560 if (mContainerView.getVisibility() == View.GONE) {
561 return;
562 }
Adam Powell50efbed2011-02-08 16:20:15 -0800563
564 if (mShowHideAnimationEnabled) {
565 mContainerView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800566 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800567 AnimatorSet anim = new AnimatorSet();
568 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 0));
569 if (mContentView != null) {
570 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
571 0, -mContainerView.getHeight()));
572 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY",
573 -mContainerView.getHeight()));
574 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700575 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700576 mSplitView.setAlpha(1);
577 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 0));
578 }
Adam Powell50efbed2011-02-08 16:20:15 -0800579 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700580 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800581 anim.start();
582 } else {
583 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800584 }
Adam Powell6b336f82010-08-10 20:13:01 -0700585 }
586
587 public boolean isShowing() {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800588 return mContainerView.getVisibility() == View.VISIBLE;
589 }
590
Adam Powell640a66e2011-04-29 10:18:53 -0700591 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700592 if (toActionMode) {
593 show(false);
594 }
Adam Powell07e1f982011-03-24 11:11:15 -0700595 if (mCurrentModeAnim != null) {
596 mCurrentModeAnim.end();
597 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800598
Adam Powell640a66e2011-04-29 10:18:53 -0700599 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
600 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700601 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
602 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
603 }
Adam Powell6b336f82010-08-10 20:13:01 -0700604 }
605
Adam Powell88ab6972011-07-28 11:25:01 -0700606 public Context getThemedContext() {
607 if (mThemedContext == null) {
608 TypedValue outValue = new TypedValue();
609 Resources.Theme currentTheme = mContext.getTheme();
610 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
611 outValue, true);
612 final int targetThemeRes = outValue.resourceId;
613
614 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
615 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
616 } else {
617 mThemedContext = mContext;
618 }
619 }
620 return mThemedContext;
621 }
622
Adam Powell89e06452010-06-23 20:24:52 -0700623 /**
624 * @hide
625 */
Adam Powell5d279772010-07-27 16:34:07 -0700626 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700627 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700628 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700629 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700630
Adam Powell5d279772010-07-27 16:34:07 -0700631 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700632 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700633 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700634 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700635 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700636 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700637
638 @Override
639 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700640 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700641 }
642
Adam Powell89e06452010-06-23 20:24:52 -0700643 @Override
644 public Menu getMenu() {
645 return mMenu;
646 }
647
648 @Override
649 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700650 if (mActionMode != this) {
651 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700652 return;
653 }
654
Adam Powell060e3ca2011-07-19 20:39:16 -0700655 // If we were hidden before the mode was shown, defer the onDestroy
656 // callback until the animation is finished and associated relayout
657 // is about to happen. This lets apps better anticipate visibility
658 // and layout behavior.
659 if (mWasHiddenBeforeMode) {
660 mDeferredDestroyActionMode = this;
661 mDeferredModeDestroyCallback = mCallback;
662 } else {
663 mCallback.onDestroyActionMode(this);
664 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800665 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700666 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700667
668 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700669 mContextView.closeMode();
Adam Powellf6ce6a92011-06-29 10:25:01 -0700670
Adam Powell5d279772010-07-27 16:34:07 -0700671 mActionMode = null;
Adam Powell07e1f982011-03-24 11:11:15 -0700672
673 if (mWasHiddenBeforeMode) {
674 hide();
675 }
Adam Powell89e06452010-06-23 20:24:52 -0700676 }
677
678 @Override
679 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700680 mMenu.stopDispatchingItemsChanged();
681 try {
682 mCallback.onPrepareActionMode(this, mMenu);
683 } finally {
684 mMenu.startDispatchingItemsChanged();
685 }
686 }
687
688 public boolean dispatchOnCreate() {
689 mMenu.stopDispatchingItemsChanged();
690 try {
691 return mCallback.onCreateActionMode(this, mMenu);
692 } finally {
693 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700694 }
695 }
696
697 @Override
698 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700699 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700700 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700701 }
702
703 @Override
704 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700705 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700706 }
707
708 @Override
709 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700710 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700711 }
Adam Powell29ed7572010-07-14 16:24:56 -0700712
713 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700714 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800715 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700716 }
717
718 @Override
719 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800720 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700721 }
722
723 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700724 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700725 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700726 }
727
728 @Override
729 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700730 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700731 }
Adam Powell89e06452010-06-23 20:24:52 -0700732
Adam Powell29ed7572010-07-14 16:24:56 -0700733 @Override
734 public View getCustomView() {
735 return mCustomView != null ? mCustomView.get() : null;
736 }
737
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700738 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800739 if (mCallback != null) {
740 return mCallback.onActionItemClicked(this, item);
741 } else {
742 return false;
743 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700744 }
745
746 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
747 }
748
749 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800750 if (mCallback == null) {
751 return false;
752 }
753
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700754 if (!subMenu.hasVisibleItems()) {
755 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700756 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700757
Adam Powellf2423682011-08-11 14:29:45 -0700758 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700759 return true;
760 }
761
762 public void onCloseSubMenu(SubMenuBuilder menu) {
763 }
764
765 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800766 if (mCallback == null) {
767 return;
768 }
Adam Powellf6148c52010-08-11 21:10:16 -0700769 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700770 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700771 }
Adam Powell661c9082010-07-02 10:09:44 -0700772 }
773
774 /**
775 * @hide
776 */
777 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700778 private ActionBar.TabListener mCallback;
779 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700780 private Drawable mIcon;
781 private CharSequence mText;
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700782 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -0700783 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700784
785 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700786 public Object getTag() {
787 return mTag;
788 }
789
790 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700791 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700792 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700793 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700794 }
795
796 public ActionBar.TabListener getCallback() {
797 return mCallback;
798 }
799
800 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700801 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700802 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700803 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700804 }
805
806 @Override
807 public View getCustomView() {
808 return mCustomView;
809 }
810
811 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700812 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700813 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700814 if (mPosition >= 0) {
815 mTabScrollView.updateTab(mPosition);
816 }
Adam Powell9ab97872010-10-26 21:47:29 -0700817 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700818 }
819
820 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800821 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -0700822 return setCustomView(LayoutInflater.from(getThemedContext())
823 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -0800824 }
825
826 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700827 public Drawable getIcon() {
828 return mIcon;
829 }
830
831 @Override
832 public int getPosition() {
833 return mPosition;
834 }
835
836 public void setPosition(int position) {
837 mPosition = position;
838 }
839
840 @Override
841 public CharSequence getText() {
842 return mText;
843 }
844
845 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700846 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -0700847 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700848 if (mPosition >= 0) {
849 mTabScrollView.updateTab(mPosition);
850 }
Adam Powell9ab97872010-10-26 21:47:29 -0700851 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700852 }
853
854 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800855 public Tab setIcon(int resId) {
856 return setIcon(mContext.getResources().getDrawable(resId));
857 }
858
859 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700860 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -0700861 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700862 if (mPosition >= 0) {
863 mTabScrollView.updateTab(mPosition);
864 }
Adam Powell9ab97872010-10-26 21:47:29 -0700865 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700866 }
867
868 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800869 public Tab setText(int resId) {
870 return setText(mContext.getResources().getText(resId));
871 }
872
873 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700874 public void select() {
875 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700876 }
877 }
Adam Powell9ab97872010-10-26 21:47:29 -0700878
879 @Override
880 public void setCustomView(View view) {
881 mActionView.setCustomNavigationView(view);
882 }
883
884 @Override
885 public void setCustomView(View view, LayoutParams layoutParams) {
886 view.setLayoutParams(layoutParams);
887 mActionView.setCustomNavigationView(view);
888 }
889
890 @Override
Adam Powell8515ee82010-11-30 14:09:55 -0800891 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -0700892 mActionView.setDropdownAdapter(adapter);
893 mActionView.setCallback(callback);
894 }
895
896 @Override
897 public int getSelectedNavigationIndex() {
898 switch (mActionView.getNavigationMode()) {
899 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -0800900 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -0700901 case NAVIGATION_MODE_LIST:
902 return mActionView.getDropdownSelectedPosition();
903 default:
904 return -1;
905 }
906 }
907
908 @Override
909 public int getNavigationItemCount() {
910 switch (mActionView.getNavigationMode()) {
911 case NAVIGATION_MODE_TABS:
912 return mTabs.size();
913 case NAVIGATION_MODE_LIST:
914 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
915 return adapter != null ? adapter.getCount() : 0;
916 default:
917 return 0;
918 }
919 }
920
921 @Override
Adam Powell0c24a552010-11-03 16:44:11 -0700922 public int getTabCount() {
923 return mTabs.size();
924 }
925
926 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700927 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -0700928 final int oldMode = mActionView.getNavigationMode();
929 switch (oldMode) {
930 case NAVIGATION_MODE_TABS:
931 mSavedTabPosition = getSelectedNavigationIndex();
932 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -0700933 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -0700934 break;
935 }
Adam Powell9ab97872010-10-26 21:47:29 -0700936 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -0700937 switch (mode) {
938 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -0700939 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -0700940 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -0700941 if (mSavedTabPosition != INVALID_POSITION) {
942 setSelectedNavigationItem(mSavedTabPosition);
943 mSavedTabPosition = INVALID_POSITION;
944 }
945 break;
946 }
Adam Powelld21aa122011-05-27 13:09:52 -0700947 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -0700948 }
949
950 @Override
951 public Tab getTabAt(int index) {
952 return mTabs.get(index);
953 }
Adam Powell0c24a552010-11-03 16:44:11 -0700954
Adam Powell0c24a552010-11-03 16:44:11 -0700955
Adam Powell1969b872011-03-22 11:52:48 -0700956 @Override
957 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -0700958 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -0700959 }
Adam Powell0c24a552010-11-03 16:44:11 -0700960
Adam Powell1969b872011-03-22 11:52:48 -0700961 @Override
962 public void setIcon(Drawable icon) {
963 mActionView.setIcon(icon);
964 }
965
966 @Override
967 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -0700968 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -0700969 }
970
971 @Override
972 public void setLogo(Drawable logo) {
973 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -0700974 }
Adam Powell89e06452010-06-23 20:24:52 -0700975}