blob: 7684bc52745ec1911c6aa5e32c3d4dc54a47e78f [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;
98 private int mContentHeight;
Adam Powell0e94b512010-06-29 17:58:20 -070099
100 final Handler mHandler = new Handler();
Adam Powellf8ac6b72011-05-23 18:14:09 -0700101 Runnable mTabSelector;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800102
Adam Powell07e1f982011-03-24 11:11:15 -0700103 private Animator mCurrentShowAnim;
104 private Animator mCurrentModeAnim;
Adam Powell50efbed2011-02-08 16:20:15 -0800105 private boolean mShowHideAnimationEnabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700106 boolean mWasHiddenBeforeMode;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800107
Adam Powell07e1f982011-03-24 11:11:15 -0700108 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800109 @Override
110 public void onAnimationEnd(Animator animation) {
111 if (mContentView != null) {
112 mContentView.setTranslationY(0);
Adam Powell060e3ca2011-07-19 20:39:16 -0700113 mContainerView.setTranslationY(0);
Adam Powelle6ec7322010-12-07 15:29:26 -0800114 }
115 mContainerView.setVisibility(View.GONE);
Adam Powell01feaee2011-02-10 15:05:05 -0800116 mContainerView.setTransitioning(false);
Adam Powell07e1f982011-03-24 11:11:15 -0700117 mCurrentShowAnim = null;
Adam Powell060e3ca2011-07-19 20:39:16 -0700118 completeDeferredDestroyActionMode();
Adam Powelle6ec7322010-12-07 15:29:26 -0800119 }
120 };
121
Adam Powell07e1f982011-03-24 11:11:15 -0700122 final AnimatorListener mShowListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800123 @Override
124 public void onAnimationEnd(Animator animation) {
Adam Powell07e1f982011-03-24 11:11:15 -0700125 mCurrentShowAnim = null;
Adam Powell45f1e082010-12-10 14:20:13 -0800126 mContainerView.requestLayout();
Adam Powelle6ec7322010-12-07 15:29:26 -0800127 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800128 };
129
Adam Powell661c9082010-07-02 10:09:44 -0700130 public ActionBarImpl(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -0700131 mActivity = activity;
Adam Powelle6ec7322010-12-07 15:29:26 -0800132 Window window = activity.getWindow();
133 View decor = window.getDecorView();
134 init(decor);
135 if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) {
136 mContentView = decor.findViewById(android.R.id.content);
137 }
Adam Powelldec9dfd2010-08-09 15:27:54 -0700138 }
139
140 public ActionBarImpl(Dialog dialog) {
141 mDialog = dialog;
142 init(dialog.getWindow().getDecorView());
143 }
144
145 private void init(View decor) {
146 mContext = decor.getContext();
Adam Powell89e06452010-06-23 20:24:52 -0700147 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
Adam Powell640a66e2011-04-29 10:18:53 -0700148 mContextView = (ActionBarContextView) decor.findViewById(
Adam Powell89e06452010-06-23 20:24:52 -0700149 com.android.internal.R.id.action_context_bar);
Adam Powell01feaee2011-02-10 15:05:05 -0800150 mContainerView = (ActionBarContainer) decor.findViewById(
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800151 com.android.internal.R.id.action_bar_container);
Adam Powell640a66e2011-04-29 10:18:53 -0700152 mSplitView = (ActionBarContainer) decor.findViewById(
153 com.android.internal.R.id.split_action_bar);
Steve Block08f194b2010-08-24 18:20:48 +0100154
Adam Powell640a66e2011-04-29 10:18:53 -0700155 if (mActionView == null || mContextView == null || mContainerView == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700156 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
157 "with a compatible window decor layout");
158 }
Adam Powell0e94b512010-06-29 17:58:20 -0700159
Adam Powellf8ac6b72011-05-23 18:14:09 -0700160 mHasEmbeddedTabs = mContext.getResources().getBoolean(
161 com.android.internal.R.bool.action_bar_embed_tabs);
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 Powellcc2e5be2011-06-10 11:55:06 -0700166 mContentHeight = mActionView.getContentHeight();
Adam Powellc29f4e52011-07-13 20:40:52 -0700167
168 // Older apps get the home button interaction enabled by default.
169 // Newer apps need to enable it explicitly.
Adam Powell28fa07e2011-07-15 20:17:25 -0700170 setHomeButtonEnabled(mContext.getApplicationInfo().targetSdkVersion <
171 Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700172 }
173
174 public void onConfigurationChanged(Configuration newConfig) {
175 mHasEmbeddedTabs = mContext.getResources().getBoolean(
176 com.android.internal.R.bool.action_bar_embed_tabs);
177
178 // Switch tab layout configuration if needed
179 if (!mHasEmbeddedTabs) {
180 mActionView.setEmbeddedTabView(null);
181 mContainerView.setTabContainer(mTabScrollView);
182 } else {
183 mContainerView.setTabContainer(null);
184 if (mTabScrollView != null) {
185 mTabScrollView.setVisibility(View.VISIBLE);
186 }
187 mActionView.setEmbeddedTabView(mTabScrollView);
188 }
Adam Powelld21aa122011-05-27 13:09:52 -0700189 mActionView.setCollapsable(!mHasEmbeddedTabs &&
190 getNavigationMode() == NAVIGATION_MODE_TABS);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700191
Adam Powellcc2e5be2011-06-10 11:55:06 -0700192 mContentHeight = mActionView.getContentHeight();
Adam Powellf8ac6b72011-05-23 18:14:09 -0700193
194 if (mTabScrollView != null) {
195 mTabScrollView.getLayoutParams().height = mContentHeight;
196 mTabScrollView.requestLayout();
197 }
198 }
199
200 private void ensureTabsExist() {
201 if (mTabScrollView != null) {
202 return;
203 }
204
205 ScrollingTabContainerView tabScroller = mActionView.createTabContainer();
206
207 if (mHasEmbeddedTabs) {
208 tabScroller.setVisibility(View.VISIBLE);
209 mActionView.setEmbeddedTabView(tabScroller);
210 } else {
Adam Powelldae78242011-04-25 15:23:41 -0700211 tabScroller.setVisibility(getNavigationMode() == NAVIGATION_MODE_TABS ?
212 View.VISIBLE : View.GONE);
Adam Powelldae78242011-04-25 15:23:41 -0700213 mContainerView.setTabContainer(tabScroller);
Adam Powelldae78242011-04-25 15:23:41 -0700214 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700215 mTabScrollView = tabScroller;
Adam Powell89e06452010-06-23 20:24:52 -0700216 }
217
Adam Powell060e3ca2011-07-19 20:39:16 -0700218 void completeDeferredDestroyActionMode() {
219 if (mDeferredModeDestroyCallback != null) {
220 mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode);
221 mDeferredDestroyActionMode = null;
222 mDeferredModeDestroyCallback = null;
223 }
224 }
225
Adam Powell50efbed2011-02-08 16:20:15 -0800226 /**
227 * Enables or disables animation between show/hide states.
228 * If animation is disabled using this method, animations in progress
229 * will be finished.
230 *
231 * @param enabled true to animate, false to not animate.
232 */
233 public void setShowHideAnimationEnabled(boolean enabled) {
234 mShowHideAnimationEnabled = enabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700235 if (!enabled && mCurrentShowAnim != null) {
236 mCurrentShowAnim.end();
Adam Powell50efbed2011-02-08 16:20:15 -0800237 }
238 }
239
Adam Powell8515ee82010-11-30 14:09:55 -0800240 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
241 mMenuVisibilityListeners.add(listener);
242 }
243
244 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
245 mMenuVisibilityListeners.remove(listener);
246 }
247
248 public void dispatchMenuVisibilityChanged(boolean isVisible) {
249 if (isVisible == mLastMenuVisibility) {
250 return;
251 }
252 mLastMenuVisibility = isVisible;
253
254 final int count = mMenuVisibilityListeners.size();
255 for (int i = 0; i < count; i++) {
256 mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
257 }
258 }
259
Adam Powella66c7b02010-07-28 15:28:25 -0700260 @Override
Adam Powell3f476b32011-01-03 19:25:36 -0800261 public void setCustomView(int resId) {
262 setCustomView(LayoutInflater.from(mContext).inflate(resId, mActionView, false));
263 }
264
265 @Override
266 public void setDisplayUseLogoEnabled(boolean useLogo) {
267 setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
268 }
269
270 @Override
271 public void setDisplayShowHomeEnabled(boolean showHome) {
272 setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
273 }
274
275 @Override
276 public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
277 setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
278 }
279
280 @Override
281 public void setDisplayShowTitleEnabled(boolean showTitle) {
282 setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
283 }
284
285 @Override
286 public void setDisplayShowCustomEnabled(boolean showCustom) {
287 setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
288 }
289
290 @Override
Adam Powellc29f4e52011-07-13 20:40:52 -0700291 public void setHomeButtonEnabled(boolean enable) {
292 mActionView.setHomeButtonEnabled(enable);
Adam Powelldae78242011-04-25 15:23:41 -0700293 }
294
295 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700296 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700297 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700298 }
299
300 @Override
301 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700302 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700303 }
304
Adam Powell17809772010-07-21 13:25:11 -0700305 public void setSelectedNavigationItem(int position) {
306 switch (mActionView.getNavigationMode()) {
307 case NAVIGATION_MODE_TABS:
308 selectTab(mTabs.get(position));
309 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700310 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700311 mActionView.setDropdownSelectedPosition(position);
312 break;
313 default:
314 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700315 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700316 }
317 }
318
Adam Powell9ab97872010-10-26 21:47:29 -0700319 public void removeAllTabs() {
320 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700321 }
322
Adam Powell661c9082010-07-02 10:09:44 -0700323 private void cleanupTabs() {
324 if (mSelectedTab != null) {
325 selectTab(null);
326 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700327 mTabs.clear();
Adam Powelld21aa122011-05-27 13:09:52 -0700328 if (mTabScrollView != null) {
329 mTabScrollView.removeAllTabs();
330 }
Adam Powell0c24a552010-11-03 16:44:11 -0700331 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700332 }
333
Adam Powell0e94b512010-06-29 17:58:20 -0700334 public void setTitle(CharSequence title) {
335 mActionView.setTitle(title);
336 }
337
338 public void setSubtitle(CharSequence subtitle) {
339 mActionView.setSubtitle(subtitle);
340 }
341
Adam Powell89e06452010-06-23 20:24:52 -0700342 public void setDisplayOptions(int options) {
343 mActionView.setDisplayOptions(options);
344 }
Adam Powell0e94b512010-06-29 17:58:20 -0700345
Adam Powell89e06452010-06-23 20:24:52 -0700346 public void setDisplayOptions(int options, int mask) {
347 final int current = mActionView.getDisplayOptions();
348 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
349 }
350
351 public void setBackgroundDrawable(Drawable d) {
Adam Powelle8c1e5c2010-12-13 10:49:32 -0800352 mContainerView.setBackgroundDrawable(d);
Adam Powell89e06452010-06-23 20:24:52 -0700353 }
354
Adam Powellef704442010-11-16 14:48:22 -0800355 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700356 return mActionView.getCustomNavigationView();
357 }
358
359 public CharSequence getTitle() {
360 return mActionView.getTitle();
361 }
362
363 public CharSequence getSubtitle() {
364 return mActionView.getSubtitle();
365 }
366
367 public int getNavigationMode() {
368 return mActionView.getNavigationMode();
369 }
370
371 public int getDisplayOptions() {
372 return mActionView.getDisplayOptions();
373 }
374
Adam Powell5d279772010-07-27 16:34:07 -0700375 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700376 boolean wasHidden = false;
Adam Powell5d279772010-07-27 16:34:07 -0700377 if (mActionMode != null) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700378 wasHidden = mWasHiddenBeforeMode;
Adam Powell5d279772010-07-27 16:34:07 -0700379 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700380 }
Adam Powell3461b322010-07-14 11:34:43 -0700381
Adam Powell640a66e2011-04-29 10:18:53 -0700382 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700383 ActionModeImpl mode = new ActionModeImpl(callback);
384 if (mode.dispatchOnCreate()) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700385 mWasHiddenBeforeMode = !isShowing() || wasHidden;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700386 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700387 mContextView.initForMode(mode);
388 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700389 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700390 // TODO animate this
Adam Powell640a66e2011-04-29 10:18:53 -0700391 mSplitView.setVisibility(View.VISIBLE);
Adam Powell89e06452010-06-23 20:24:52 -0700392 }
Adam Powell5d279772010-07-27 16:34:07 -0700393 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700394 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700395 }
Adam Powellac695c62010-07-20 18:19:27 -0700396 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700397 }
398
Adam Powell661c9082010-07-02 10:09:44 -0700399 private void configureTab(Tab tab, int position) {
400 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700401 final ActionBar.TabListener callback = tabi.getCallback();
402
403 if (callback == null) {
404 throw new IllegalStateException("Action Bar Tab must have a Callback");
405 }
Adam Powell661c9082010-07-02 10:09:44 -0700406
407 tabi.setPosition(position);
408 mTabs.add(position, tabi);
409
Adam Powell81b89442010-11-02 17:58:56 -0700410 final int count = mTabs.size();
411 for (int i = position + 1; i < count; i++) {
412 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700413 }
Adam Powell661c9082010-07-02 10:09:44 -0700414 }
415
416 @Override
417 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700418 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700419 }
420
421 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700422 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700423 addTab(tab, position, mTabs.isEmpty());
424 }
425
426 @Override
427 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700428 ensureTabsExist();
429 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700430 configureTab(tab, mTabs.size());
431 if (setSelected) {
432 selectTab(tab);
433 }
434 }
435
436 @Override
437 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700438 ensureTabsExist();
439 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700440 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700441 if (setSelected) {
442 selectTab(tab);
443 }
Adam Powell661c9082010-07-02 10:09:44 -0700444 }
445
446 @Override
447 public Tab newTab() {
448 return new TabImpl();
449 }
450
451 @Override
452 public void removeTab(Tab tab) {
453 removeTabAt(tab.getPosition());
454 }
455
456 @Override
457 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700458 if (mTabScrollView == null) {
459 // No tabs around to remove
460 return;
461 }
462
Adam Powell0c24a552010-11-03 16:44:11 -0700463 int selectedTabPosition = mSelectedTab != null
464 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700465 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700466 TabImpl removedTab = mTabs.remove(position);
467 if (removedTab != null) {
468 removedTab.setPosition(-1);
469 }
Adam Powell661c9082010-07-02 10:09:44 -0700470
471 final int newTabCount = mTabs.size();
472 for (int i = position; i < newTabCount; i++) {
473 mTabs.get(i).setPosition(i);
474 }
475
Adam Powell0c24a552010-11-03 16:44:11 -0700476 if (selectedTabPosition == position) {
477 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
478 }
Adam Powell661c9082010-07-02 10:09:44 -0700479 }
480
481 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700482 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700483 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
484 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
485 return;
486 }
487
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800488 final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction()
Adam Powell0c24a552010-11-03 16:44:11 -0700489 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700490
491 if (mSelectedTab == tab) {
492 if (mSelectedTab != null) {
493 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700494 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700495 }
496 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700497 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700498 if (mSelectedTab != null) {
499 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
500 }
501 mSelectedTab = (TabImpl) tab;
502 if (mSelectedTab != null) {
503 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
504 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700505 }
506
507 if (!trans.isEmpty()) {
508 trans.commit();
509 }
510 }
511
512 @Override
513 public Tab getSelectedTab() {
514 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700515 }
516
Adam Powell6b336f82010-08-10 20:13:01 -0700517 @Override
518 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700519 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700520 }
521
522 @Override
523 public void show() {
Adam Powell07e1f982011-03-24 11:11:15 -0700524 show(true);
525 }
526
527 void show(boolean markHiddenBeforeMode) {
528 if (mCurrentShowAnim != null) {
529 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800530 }
Adam Powelld76cee22011-01-31 15:05:05 -0800531 if (mContainerView.getVisibility() == View.VISIBLE) {
Adam Powell07e1f982011-03-24 11:11:15 -0700532 if (markHiddenBeforeMode) mWasHiddenBeforeMode = false;
Adam Powelld76cee22011-01-31 15:05:05 -0800533 return;
534 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800535 mContainerView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800536
537 if (mShowHideAnimationEnabled) {
538 mContainerView.setAlpha(0);
539 AnimatorSet anim = new AnimatorSet();
540 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 1));
541 if (mContentView != null) {
542 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
543 -mContainerView.getHeight(), 0));
544 mContainerView.setTranslationY(-mContainerView.getHeight());
545 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY", 0));
546 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700547 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell640a66e2011-04-29 10:18:53 -0700548 mSplitView.setAlpha(0);
549 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 1));
550 }
Adam Powell50efbed2011-02-08 16:20:15 -0800551 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700552 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800553 anim.start();
554 } else {
Adam Powelld8145042011-03-24 14:18:27 -0700555 mContainerView.setAlpha(1);
556 mContainerView.setTranslationY(0);
Adam Powell50efbed2011-02-08 16:20:15 -0800557 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800558 }
Adam Powell6b336f82010-08-10 20:13:01 -0700559 }
560
561 @Override
562 public void hide() {
Adam Powell07e1f982011-03-24 11:11:15 -0700563 if (mCurrentShowAnim != null) {
564 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800565 }
566 if (mContainerView.getVisibility() == View.GONE) {
567 return;
568 }
Adam Powell50efbed2011-02-08 16:20:15 -0800569
570 if (mShowHideAnimationEnabled) {
571 mContainerView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800572 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800573 AnimatorSet anim = new AnimatorSet();
574 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 0));
575 if (mContentView != null) {
576 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
577 0, -mContainerView.getHeight()));
578 b.with(ObjectAnimator.ofFloat(mContainerView, "translationY",
579 -mContainerView.getHeight()));
580 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700581 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700582 mSplitView.setAlpha(1);
583 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 0));
584 }
Adam Powell50efbed2011-02-08 16:20:15 -0800585 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700586 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800587 anim.start();
588 } else {
589 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800590 }
Adam Powell6b336f82010-08-10 20:13:01 -0700591 }
592
593 public boolean isShowing() {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800594 return mContainerView.getVisibility() == View.VISIBLE;
595 }
596
Adam Powell640a66e2011-04-29 10:18:53 -0700597 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700598 if (toActionMode) {
599 show(false);
600 }
Adam Powell07e1f982011-03-24 11:11:15 -0700601 if (mCurrentModeAnim != null) {
602 mCurrentModeAnim.end();
603 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800604
Adam Powell640a66e2011-04-29 10:18:53 -0700605 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
606 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700607 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
608 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
609 }
Adam Powell6b336f82010-08-10 20:13:01 -0700610 }
611
Adam Powell88ab6972011-07-28 11:25:01 -0700612 public Context getThemedContext() {
613 if (mThemedContext == null) {
614 TypedValue outValue = new TypedValue();
615 Resources.Theme currentTheme = mContext.getTheme();
616 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
617 outValue, true);
618 final int targetThemeRes = outValue.resourceId;
619
620 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
621 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
622 } else {
623 mThemedContext = mContext;
624 }
625 }
626 return mThemedContext;
627 }
628
Adam Powell89e06452010-06-23 20:24:52 -0700629 /**
630 * @hide
631 */
Adam Powell5d279772010-07-27 16:34:07 -0700632 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700633 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700634 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700635 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700636
Adam Powell5d279772010-07-27 16:34:07 -0700637 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700638 mCallback = callback;
Adam Powell4d9861e2010-08-17 11:14:40 -0700639 mMenu = new MenuBuilder(mActionView.getContext())
640 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700641 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700642 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700643
644 @Override
645 public MenuInflater getMenuInflater() {
Adam Powell4d9861e2010-08-17 11:14:40 -0700646 return new MenuInflater(mContext);
Adam Powell9168f0b2010-08-02 15:46:24 -0700647 }
648
Adam Powell89e06452010-06-23 20:24:52 -0700649 @Override
650 public Menu getMenu() {
651 return mMenu;
652 }
653
654 @Override
655 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700656 if (mActionMode != this) {
657 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700658 return;
659 }
660
Adam Powell060e3ca2011-07-19 20:39:16 -0700661 // If we were hidden before the mode was shown, defer the onDestroy
662 // callback until the animation is finished and associated relayout
663 // is about to happen. This lets apps better anticipate visibility
664 // and layout behavior.
665 if (mWasHiddenBeforeMode) {
666 mDeferredDestroyActionMode = this;
667 mDeferredModeDestroyCallback = mCallback;
668 } else {
669 mCallback.onDestroyActionMode(this);
670 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800671 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700672 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700673
674 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700675 mContextView.closeMode();
Adam Powellf6ce6a92011-06-29 10:25:01 -0700676
Adam Powell5d279772010-07-27 16:34:07 -0700677 mActionMode = null;
Adam Powell07e1f982011-03-24 11:11:15 -0700678
679 if (mWasHiddenBeforeMode) {
680 hide();
681 }
Adam Powell89e06452010-06-23 20:24:52 -0700682 }
683
684 @Override
685 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700686 mMenu.stopDispatchingItemsChanged();
687 try {
688 mCallback.onPrepareActionMode(this, mMenu);
689 } finally {
690 mMenu.startDispatchingItemsChanged();
691 }
692 }
693
694 public boolean dispatchOnCreate() {
695 mMenu.stopDispatchingItemsChanged();
696 try {
697 return mCallback.onCreateActionMode(this, mMenu);
698 } finally {
699 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700700 }
701 }
702
703 @Override
704 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700705 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700706 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700707 }
708
709 @Override
710 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700711 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700712 }
713
714 @Override
715 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700716 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700717 }
Adam Powell29ed7572010-07-14 16:24:56 -0700718
719 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700720 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800721 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700722 }
723
724 @Override
725 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800726 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700727 }
728
729 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700730 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700731 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700732 }
733
734 @Override
735 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700736 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700737 }
Adam Powell89e06452010-06-23 20:24:52 -0700738
Adam Powell29ed7572010-07-14 16:24:56 -0700739 @Override
740 public View getCustomView() {
741 return mCustomView != null ? mCustomView.get() : null;
742 }
743
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700744 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800745 if (mCallback != null) {
746 return mCallback.onActionItemClicked(this, item);
747 } else {
748 return false;
749 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700750 }
751
752 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
753 }
754
755 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800756 if (mCallback == null) {
757 return false;
758 }
759
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700760 if (!subMenu.hasVisibleItems()) {
761 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700762 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700763
Adam Powelldec9dfd2010-08-09 15:27:54 -0700764 new MenuPopupHelper(mContext, subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700765 return true;
766 }
767
768 public void onCloseSubMenu(SubMenuBuilder menu) {
769 }
770
771 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800772 if (mCallback == null) {
773 return;
774 }
Adam Powellf6148c52010-08-11 21:10:16 -0700775 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700776 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700777 }
Adam Powell661c9082010-07-02 10:09:44 -0700778 }
779
780 /**
781 * @hide
782 */
783 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700784 private ActionBar.TabListener mCallback;
785 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700786 private Drawable mIcon;
787 private CharSequence mText;
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700788 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -0700789 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700790
791 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700792 public Object getTag() {
793 return mTag;
794 }
795
796 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700797 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700798 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700799 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700800 }
801
802 public ActionBar.TabListener getCallback() {
803 return mCallback;
804 }
805
806 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700807 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700808 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700809 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700810 }
811
812 @Override
813 public View getCustomView() {
814 return mCustomView;
815 }
816
817 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700818 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700819 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700820 if (mPosition >= 0) {
821 mTabScrollView.updateTab(mPosition);
822 }
Adam Powell9ab97872010-10-26 21:47:29 -0700823 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700824 }
825
826 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800827 public Tab setCustomView(int layoutResId) {
828 return setCustomView(LayoutInflater.from(mContext).inflate(layoutResId, null));
829 }
830
831 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700832 public Drawable getIcon() {
833 return mIcon;
834 }
835
836 @Override
837 public int getPosition() {
838 return mPosition;
839 }
840
841 public void setPosition(int position) {
842 mPosition = position;
843 }
844
845 @Override
846 public CharSequence getText() {
847 return mText;
848 }
849
850 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700851 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -0700852 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700853 if (mPosition >= 0) {
854 mTabScrollView.updateTab(mPosition);
855 }
Adam Powell9ab97872010-10-26 21:47:29 -0700856 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700857 }
858
859 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800860 public Tab setIcon(int resId) {
861 return setIcon(mContext.getResources().getDrawable(resId));
862 }
863
864 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700865 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -0700866 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700867 if (mPosition >= 0) {
868 mTabScrollView.updateTab(mPosition);
869 }
Adam Powell9ab97872010-10-26 21:47:29 -0700870 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700871 }
872
873 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800874 public Tab setText(int resId) {
875 return setText(mContext.getResources().getText(resId));
876 }
877
878 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700879 public void select() {
880 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700881 }
882 }
Adam Powell9ab97872010-10-26 21:47:29 -0700883
884 @Override
885 public void setCustomView(View view) {
886 mActionView.setCustomNavigationView(view);
887 }
888
889 @Override
890 public void setCustomView(View view, LayoutParams layoutParams) {
891 view.setLayoutParams(layoutParams);
892 mActionView.setCustomNavigationView(view);
893 }
894
895 @Override
Adam Powell8515ee82010-11-30 14:09:55 -0800896 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -0700897 mActionView.setDropdownAdapter(adapter);
898 mActionView.setCallback(callback);
899 }
900
901 @Override
902 public int getSelectedNavigationIndex() {
903 switch (mActionView.getNavigationMode()) {
904 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -0800905 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -0700906 case NAVIGATION_MODE_LIST:
907 return mActionView.getDropdownSelectedPosition();
908 default:
909 return -1;
910 }
911 }
912
913 @Override
914 public int getNavigationItemCount() {
915 switch (mActionView.getNavigationMode()) {
916 case NAVIGATION_MODE_TABS:
917 return mTabs.size();
918 case NAVIGATION_MODE_LIST:
919 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
920 return adapter != null ? adapter.getCount() : 0;
921 default:
922 return 0;
923 }
924 }
925
926 @Override
Adam Powell0c24a552010-11-03 16:44:11 -0700927 public int getTabCount() {
928 return mTabs.size();
929 }
930
931 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700932 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -0700933 final int oldMode = mActionView.getNavigationMode();
934 switch (oldMode) {
935 case NAVIGATION_MODE_TABS:
936 mSavedTabPosition = getSelectedNavigationIndex();
937 selectTab(null);
Adam Powelldae78242011-04-25 15:23:41 -0700938 if (!mActionView.hasEmbeddedTabs()) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700939 mTabScrollView.setVisibility(View.GONE);
Adam Powelldae78242011-04-25 15:23:41 -0700940 }
Adam Powell0c24a552010-11-03 16:44:11 -0700941 break;
942 }
Adam Powell9ab97872010-10-26 21:47:29 -0700943 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -0700944 switch (mode) {
945 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -0700946 ensureTabsExist();
Adam Powelldae78242011-04-25 15:23:41 -0700947 if (!mActionView.hasEmbeddedTabs()) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700948 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powelldae78242011-04-25 15:23:41 -0700949 }
Adam Powell0c24a552010-11-03 16:44:11 -0700950 if (mSavedTabPosition != INVALID_POSITION) {
951 setSelectedNavigationItem(mSavedTabPosition);
952 mSavedTabPosition = INVALID_POSITION;
953 }
954 break;
955 }
Adam Powelld21aa122011-05-27 13:09:52 -0700956 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -0700957 }
958
959 @Override
960 public Tab getTabAt(int index) {
961 return mTabs.get(index);
962 }
Adam Powell0c24a552010-11-03 16:44:11 -0700963
Adam Powell0c24a552010-11-03 16:44:11 -0700964
Adam Powell1969b872011-03-22 11:52:48 -0700965 @Override
966 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -0700967 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -0700968 }
Adam Powell0c24a552010-11-03 16:44:11 -0700969
Adam Powell1969b872011-03-22 11:52:48 -0700970 @Override
971 public void setIcon(Drawable icon) {
972 mActionView.setIcon(icon);
973 }
974
975 @Override
976 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -0700977 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -0700978 }
979
980 @Override
981 public void setLogo(Drawable logo) {
982 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -0700983 }
Adam Powell89e06452010-06-23 20:24:52 -0700984}