blob: 805c0a93ee498cdcbdccebb29e985a3e383773b2 [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 Powellb8139af2012-04-19 13:52:46 -070019import com.android.internal.view.ActionBarPolicy;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070020import com.android.internal.view.menu.MenuBuilder;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070021import com.android.internal.view.menu.MenuPopupHelper;
22import com.android.internal.view.menu.SubMenuBuilder;
Adam Powell01feaee2011-02-10 15:05:05 -080023import com.android.internal.widget.ActionBarContainer;
Adam Powell89e06452010-06-23 20:24:52 -070024import com.android.internal.widget.ActionBarContextView;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070025import com.android.internal.widget.ActionBarOverlayLayout;
Adam Powell89e06452010-06-23 20:24:52 -070026import com.android.internal.widget.ActionBarView;
Adam Powellf8ac6b72011-05-23 18:14:09 -070027import com.android.internal.widget.ScrollingTabContainerView;
Adam Powell89e06452010-06-23 20:24:52 -070028
Adam Powelld8b3f2e2010-12-02 13:37:03 -080029import android.animation.Animator;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080030import android.animation.Animator.AnimatorListener;
Adam Powell07e1f982011-03-24 11:11:15 -070031import android.animation.AnimatorListenerAdapter;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080032import android.animation.AnimatorSet;
Adam Powelle6ec7322010-12-07 15:29:26 -080033import android.animation.ObjectAnimator;
Adam Powell89e06452010-06-23 20:24:52 -070034import android.app.ActionBar;
Adam Powell661c9082010-07-02 10:09:44 -070035import android.app.Activity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070036import android.app.Dialog;
Adam Powell661c9082010-07-02 10:09:44 -070037import android.app.FragmentTransaction;
Adam Powelldec9dfd2010-08-09 15:27:54 -070038import android.content.Context;
Adam Powellf8ac6b72011-05-23 18:14:09 -070039import android.content.res.Configuration;
Adam Powell88ab6972011-07-28 11:25:01 -070040import android.content.res.Resources;
Adam Powell89e06452010-06-23 20:24:52 -070041import android.graphics.drawable.Drawable;
Adam Powellc29f4e52011-07-13 20:40:52 -070042import android.os.Build;
Adam Powell0e94b512010-06-29 17:58:20 -070043import android.os.Handler;
Adam Powell88ab6972011-07-28 11:25:01 -070044import android.util.TypedValue;
Adam Powell6e346362010-07-23 10:18:23 -070045import android.view.ActionMode;
Adam Powell88ab6972011-07-28 11:25:01 -070046import android.view.ContextThemeWrapper;
Adam Powell32555f32010-11-17 13:49:22 -080047import android.view.LayoutInflater;
Adam Powell89e06452010-06-23 20:24:52 -070048import android.view.Menu;
Adam Powell9168f0b2010-08-02 15:46:24 -070049import android.view.MenuInflater;
Adam Powell89e06452010-06-23 20:24:52 -070050import android.view.MenuItem;
51import android.view.View;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070052import android.view.ViewGroup;
Adam Powelle6ec7322010-12-07 15:29:26 -080053import android.view.Window;
Adam Powell86ed4362011-09-14 16:18:53 -070054import android.view.accessibility.AccessibilityEvent;
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -070055import android.view.animation.AnimationUtils;
Adam Powell89e06452010-06-23 20:24:52 -070056import android.widget.SpinnerAdapter;
Adam Powell89e06452010-06-23 20:24:52 -070057
Adam Powell29ed7572010-07-14 16:24:56 -070058import java.lang.ref.WeakReference;
Adam Powell661c9082010-07-02 10:09:44 -070059import java.util.ArrayList;
60
Adam Powell89e06452010-06-23 20:24:52 -070061/**
62 * ActionBarImpl is the ActionBar implementation used
63 * by devices of all screen sizes. If it detects a compatible decor,
64 * it will split contextual modes across both the ActionBarView at
65 * the top of the screen and a horizontal LinearLayout at the bottom
66 * which is normally hidden.
67 */
68public class ActionBarImpl extends ActionBar {
Adam Powelld8145042011-03-24 14:18:27 -070069 private static final String TAG = "ActionBarImpl";
Adam Powell661c9082010-07-02 10:09:44 -070070
Adam Powelldec9dfd2010-08-09 15:27:54 -070071 private Context mContext;
Adam Powell88ab6972011-07-28 11:25:01 -070072 private Context mThemedContext;
Adam Powell661c9082010-07-02 10:09:44 -070073 private Activity mActivity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070074 private Dialog mDialog;
Adam Powell661c9082010-07-02 10:09:44 -070075
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070076 private ActionBarOverlayLayout mOverlayLayout;
Adam Powell01feaee2011-02-10 15:05:05 -080077 private ActionBarContainer mContainerView;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070078 private ViewGroup mTopVisibilityView;
Adam Powell89e06452010-06-23 20:24:52 -070079 private ActionBarView mActionView;
Adam Powell640a66e2011-04-29 10:18:53 -070080 private ActionBarContextView mContextView;
81 private ActionBarContainer mSplitView;
Adam Powelle6ec7322010-12-07 15:29:26 -080082 private View mContentView;
Adam Powellf8ac6b72011-05-23 18:14:09 -070083 private ScrollingTabContainerView mTabScrollView;
Adam Powell661c9082010-07-02 10:09:44 -070084
85 private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>();
86
Adam Powell661c9082010-07-02 10:09:44 -070087 private TabImpl mSelectedTab;
Adam Powell0c24a552010-11-03 16:44:11 -070088 private int mSavedTabPosition = INVALID_POSITION;
Adam Powell89e06452010-06-23 20:24:52 -070089
Adam Powelldd8fab22012-03-22 17:47:27 -070090 private boolean mDisplayHomeAsUpSet;
91
Adam Powell060e3ca2011-07-19 20:39:16 -070092 ActionModeImpl mActionMode;
93 ActionMode mDeferredDestroyActionMode;
94 ActionMode.Callback mDeferredModeDestroyCallback;
Adam Powell89e06452010-06-23 20:24:52 -070095
Adam Powell8515ee82010-11-30 14:09:55 -080096 private boolean mLastMenuVisibility;
97 private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
98 new ArrayList<OnMenuVisibilityListener>();
99
Adam Powell89e06452010-06-23 20:24:52 -0700100 private static final int CONTEXT_DISPLAY_NORMAL = 0;
101 private static final int CONTEXT_DISPLAY_SPLIT = 1;
102
Adam Powell0c24a552010-11-03 16:44:11 -0700103 private static final int INVALID_POSITION = -1;
104
Adam Powell89e06452010-06-23 20:24:52 -0700105 private int mContextDisplayMode;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700106 private boolean mHasEmbeddedTabs;
Adam Powell0e94b512010-06-29 17:58:20 -0700107
108 final Handler mHandler = new Handler();
Adam Powellf8ac6b72011-05-23 18:14:09 -0700109 Runnable mTabSelector;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800110
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700111 private int mCurWindowVisibility = View.VISIBLE;
112
Adam Powell07e1f982011-03-24 11:11:15 -0700113 private Animator mCurrentShowAnim;
114 private Animator mCurrentModeAnim;
Adam Powell50efbed2011-02-08 16:20:15 -0800115 private boolean mShowHideAnimationEnabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700116 boolean mWasHiddenBeforeMode;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800117
Adam Powell07e1f982011-03-24 11:11:15 -0700118 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800119 @Override
120 public void onAnimationEnd(Animator animation) {
121 if (mContentView != null) {
122 mContentView.setTranslationY(0);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700123 mTopVisibilityView.setTranslationY(0);
Adam Powelle6ec7322010-12-07 15:29:26 -0800124 }
Adam Powell993a63a2011-08-18 16:35:53 -0700125 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
126 mSplitView.setVisibility(View.GONE);
127 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700128 mTopVisibilityView.setVisibility(View.GONE);
Adam Powell01feaee2011-02-10 15:05:05 -0800129 mContainerView.setTransitioning(false);
Adam Powell07e1f982011-03-24 11:11:15 -0700130 mCurrentShowAnim = null;
Adam Powell060e3ca2011-07-19 20:39:16 -0700131 completeDeferredDestroyActionMode();
Adam Powelle6ec7322010-12-07 15:29:26 -0800132 }
133 };
134
Adam Powell07e1f982011-03-24 11:11:15 -0700135 final AnimatorListener mShowListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800136 @Override
137 public void onAnimationEnd(Animator animation) {
Adam Powell07e1f982011-03-24 11:11:15 -0700138 mCurrentShowAnim = null;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700139 mTopVisibilityView.requestLayout();
Adam Powelle6ec7322010-12-07 15:29:26 -0800140 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800141 };
142
Adam Powell661c9082010-07-02 10:09:44 -0700143 public ActionBarImpl(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -0700144 mActivity = activity;
Adam Powelle6ec7322010-12-07 15:29:26 -0800145 Window window = activity.getWindow();
146 View decor = window.getDecorView();
147 init(decor);
148 if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) {
149 mContentView = decor.findViewById(android.R.id.content);
150 }
Adam Powelldec9dfd2010-08-09 15:27:54 -0700151 }
152
153 public ActionBarImpl(Dialog dialog) {
154 mDialog = dialog;
155 init(dialog.getWindow().getDecorView());
156 }
157
158 private void init(View decor) {
159 mContext = decor.getContext();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700160 mOverlayLayout = (ActionBarOverlayLayout) decor.findViewById(
161 com.android.internal.R.id.action_bar_overlay_layout);
162 if (mOverlayLayout != null) {
163 mOverlayLayout.setActionBar(this);
164 }
Adam Powell89e06452010-06-23 20:24:52 -0700165 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
Adam Powell640a66e2011-04-29 10:18:53 -0700166 mContextView = (ActionBarContextView) decor.findViewById(
Adam Powell89e06452010-06-23 20:24:52 -0700167 com.android.internal.R.id.action_context_bar);
Adam Powell01feaee2011-02-10 15:05:05 -0800168 mContainerView = (ActionBarContainer) decor.findViewById(
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800169 com.android.internal.R.id.action_bar_container);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700170 mTopVisibilityView = (ViewGroup)decor.findViewById(
171 com.android.internal.R.id.top_action_bar);
172 if (mTopVisibilityView == null) {
173 mTopVisibilityView = mContainerView;
174 }
Adam Powell640a66e2011-04-29 10:18:53 -0700175 mSplitView = (ActionBarContainer) decor.findViewById(
176 com.android.internal.R.id.split_action_bar);
Steve Block08f194b2010-08-24 18:20:48 +0100177
Adam Powell640a66e2011-04-29 10:18:53 -0700178 if (mActionView == null || mContextView == null || mContainerView == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700179 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
180 "with a compatible window decor layout");
181 }
Adam Powell0e94b512010-06-29 17:58:20 -0700182
Adam Powell640a66e2011-04-29 10:18:53 -0700183 mActionView.setContextView(mContextView);
Adam Powell9b4bee02011-04-27 19:24:47 -0700184 mContextDisplayMode = mActionView.isSplitActionBar() ?
185 CONTEXT_DISPLAY_SPLIT : CONTEXT_DISPLAY_NORMAL;
Adam Powelldae78242011-04-25 15:23:41 -0700186
Adam Powelld40423a2012-05-02 14:06:03 -0700187 // This was initially read from the action bar style
188 final int current = mActionView.getDisplayOptions();
189 final boolean homeAsUp = (current & DISPLAY_HOME_AS_UP) != 0;
190 if (homeAsUp) {
191 mDisplayHomeAsUpSet = true;
192 }
193
Adam Powellb8139af2012-04-19 13:52:46 -0700194 ActionBarPolicy abp = ActionBarPolicy.get(mContext);
Adam Powelld40423a2012-05-02 14:06:03 -0700195 setHomeButtonEnabled(abp.enableHomeButtonByDefault() || homeAsUp);
Adam Powellb8139af2012-04-19 13:52:46 -0700196 setHasEmbeddedTabs(abp.hasEmbeddedTabs());
Adam Powellf8ac6b72011-05-23 18:14:09 -0700197 }
198
199 public void onConfigurationChanged(Configuration newConfig) {
Adam Powellb8139af2012-04-19 13:52:46 -0700200 setHasEmbeddedTabs(ActionBarPolicy.get(mContext).hasEmbeddedTabs());
Adam Powellf5645cb2011-08-10 22:49:02 -0700201 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700202
Adam Powellf5645cb2011-08-10 22:49:02 -0700203 private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) {
204 mHasEmbeddedTabs = hasEmbeddedTabs;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700205 // Switch tab layout configuration if needed
206 if (!mHasEmbeddedTabs) {
207 mActionView.setEmbeddedTabView(null);
208 mContainerView.setTabContainer(mTabScrollView);
209 } else {
210 mContainerView.setTabContainer(null);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700211 mActionView.setEmbeddedTabView(mTabScrollView);
212 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700213 final boolean isInTabMode = getNavigationMode() == NAVIGATION_MODE_TABS;
214 if (mTabScrollView != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700215 if (isInTabMode) {
216 mTabScrollView.setVisibility(View.VISIBLE);
217 if (mOverlayLayout != null) {
218 mOverlayLayout.requestFitSystemWindows();
219 }
220 } else {
221 mTabScrollView.setVisibility(View.GONE);
222 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700223 }
224 mActionView.setCollapsable(!mHasEmbeddedTabs && isInTabMode);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700225 }
226
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700227 public boolean hasNonEmbeddedTabs() {
228 return !mHasEmbeddedTabs && getNavigationMode() == NAVIGATION_MODE_TABS;
229 }
230
Adam Powellf8ac6b72011-05-23 18:14:09 -0700231 private void ensureTabsExist() {
232 if (mTabScrollView != null) {
233 return;
234 }
235
Adam Powellf5645cb2011-08-10 22:49:02 -0700236 ScrollingTabContainerView tabScroller = new ScrollingTabContainerView(mContext);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700237
238 if (mHasEmbeddedTabs) {
239 tabScroller.setVisibility(View.VISIBLE);
240 mActionView.setEmbeddedTabView(tabScroller);
241 } else {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700242 if (getNavigationMode() == NAVIGATION_MODE_TABS) {
243 tabScroller.setVisibility(View.VISIBLE);
244 if (mOverlayLayout != null) {
245 mOverlayLayout.requestFitSystemWindows();
246 }
247 } else {
248 tabScroller.setVisibility(View.GONE);
249 }
Adam Powelldae78242011-04-25 15:23:41 -0700250 mContainerView.setTabContainer(tabScroller);
Adam Powelldae78242011-04-25 15:23:41 -0700251 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700252 mTabScrollView = tabScroller;
Adam Powell89e06452010-06-23 20:24:52 -0700253 }
254
Adam Powell060e3ca2011-07-19 20:39:16 -0700255 void completeDeferredDestroyActionMode() {
256 if (mDeferredModeDestroyCallback != null) {
257 mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode);
258 mDeferredDestroyActionMode = null;
259 mDeferredModeDestroyCallback = null;
260 }
261 }
262
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700263 public void setWindowVisibility(int visibility) {
264 mCurWindowVisibility = visibility;
265 }
266
Adam Powell50efbed2011-02-08 16:20:15 -0800267 /**
268 * Enables or disables animation between show/hide states.
269 * If animation is disabled using this method, animations in progress
270 * will be finished.
271 *
272 * @param enabled true to animate, false to not animate.
273 */
274 public void setShowHideAnimationEnabled(boolean enabled) {
275 mShowHideAnimationEnabled = enabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700276 if (!enabled && mCurrentShowAnim != null) {
277 mCurrentShowAnim.end();
Adam Powell50efbed2011-02-08 16:20:15 -0800278 }
279 }
280
Adam Powell8515ee82010-11-30 14:09:55 -0800281 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
282 mMenuVisibilityListeners.add(listener);
283 }
284
285 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
286 mMenuVisibilityListeners.remove(listener);
287 }
288
289 public void dispatchMenuVisibilityChanged(boolean isVisible) {
290 if (isVisible == mLastMenuVisibility) {
291 return;
292 }
293 mLastMenuVisibility = isVisible;
294
295 final int count = mMenuVisibilityListeners.size();
296 for (int i = 0; i < count; i++) {
297 mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
298 }
299 }
300
Adam Powella66c7b02010-07-28 15:28:25 -0700301 @Override
Adam Powell3f476b32011-01-03 19:25:36 -0800302 public void setCustomView(int resId) {
Adam Powellf2423682011-08-11 14:29:45 -0700303 setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId, mActionView, false));
Adam Powell3f476b32011-01-03 19:25:36 -0800304 }
305
306 @Override
307 public void setDisplayUseLogoEnabled(boolean useLogo) {
308 setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
309 }
310
311 @Override
312 public void setDisplayShowHomeEnabled(boolean showHome) {
313 setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
314 }
315
316 @Override
317 public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
318 setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
319 }
320
321 @Override
322 public void setDisplayShowTitleEnabled(boolean showTitle) {
323 setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
324 }
325
326 @Override
327 public void setDisplayShowCustomEnabled(boolean showCustom) {
328 setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
329 }
330
331 @Override
Adam Powellc29f4e52011-07-13 20:40:52 -0700332 public void setHomeButtonEnabled(boolean enable) {
333 mActionView.setHomeButtonEnabled(enable);
Adam Powelldae78242011-04-25 15:23:41 -0700334 }
335
336 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700337 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700338 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700339 }
340
341 @Override
342 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700343 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700344 }
345
Adam Powell17809772010-07-21 13:25:11 -0700346 public void setSelectedNavigationItem(int position) {
347 switch (mActionView.getNavigationMode()) {
348 case NAVIGATION_MODE_TABS:
349 selectTab(mTabs.get(position));
350 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700351 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700352 mActionView.setDropdownSelectedPosition(position);
353 break;
354 default:
355 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700356 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700357 }
358 }
359
Adam Powell9ab97872010-10-26 21:47:29 -0700360 public void removeAllTabs() {
361 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700362 }
363
Adam Powell661c9082010-07-02 10:09:44 -0700364 private void cleanupTabs() {
365 if (mSelectedTab != null) {
366 selectTab(null);
367 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700368 mTabs.clear();
Adam Powelld21aa122011-05-27 13:09:52 -0700369 if (mTabScrollView != null) {
370 mTabScrollView.removeAllTabs();
371 }
Adam Powell0c24a552010-11-03 16:44:11 -0700372 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700373 }
374
Adam Powell0e94b512010-06-29 17:58:20 -0700375 public void setTitle(CharSequence title) {
376 mActionView.setTitle(title);
377 }
378
379 public void setSubtitle(CharSequence subtitle) {
380 mActionView.setSubtitle(subtitle);
381 }
382
Adam Powell89e06452010-06-23 20:24:52 -0700383 public void setDisplayOptions(int options) {
Adam Powelldd8fab22012-03-22 17:47:27 -0700384 if ((options & DISPLAY_HOME_AS_UP) != 0) {
385 mDisplayHomeAsUpSet = true;
386 }
Adam Powell89e06452010-06-23 20:24:52 -0700387 mActionView.setDisplayOptions(options);
388 }
Adam Powell0e94b512010-06-29 17:58:20 -0700389
Adam Powell89e06452010-06-23 20:24:52 -0700390 public void setDisplayOptions(int options, int mask) {
391 final int current = mActionView.getDisplayOptions();
Adam Powelldd8fab22012-03-22 17:47:27 -0700392 if ((mask & DISPLAY_HOME_AS_UP) != 0) {
393 mDisplayHomeAsUpSet = true;
394 }
Adam Powell89e06452010-06-23 20:24:52 -0700395 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
396 }
397
398 public void setBackgroundDrawable(Drawable d) {
Adam Powellf88b9152011-09-07 14:54:32 -0700399 mContainerView.setPrimaryBackground(d);
400 }
401
402 public void setStackedBackgroundDrawable(Drawable d) {
403 mContainerView.setStackedBackground(d);
404 }
405
406 public void setSplitBackgroundDrawable(Drawable d) {
407 if (mSplitView != null) {
408 mSplitView.setSplitBackground(d);
409 }
Adam Powell89e06452010-06-23 20:24:52 -0700410 }
411
Adam Powellef704442010-11-16 14:48:22 -0800412 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700413 return mActionView.getCustomNavigationView();
414 }
415
416 public CharSequence getTitle() {
417 return mActionView.getTitle();
418 }
419
420 public CharSequence getSubtitle() {
421 return mActionView.getSubtitle();
422 }
423
424 public int getNavigationMode() {
425 return mActionView.getNavigationMode();
426 }
427
428 public int getDisplayOptions() {
429 return mActionView.getDisplayOptions();
430 }
431
Adam Powell5d279772010-07-27 16:34:07 -0700432 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700433 boolean wasHidden = false;
Adam Powell5d279772010-07-27 16:34:07 -0700434 if (mActionMode != null) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700435 wasHidden = mWasHiddenBeforeMode;
Adam Powell5d279772010-07-27 16:34:07 -0700436 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700437 }
Adam Powell3461b322010-07-14 11:34:43 -0700438
Adam Powell640a66e2011-04-29 10:18:53 -0700439 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700440 ActionModeImpl mode = new ActionModeImpl(callback);
441 if (mode.dispatchOnCreate()) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700442 mWasHiddenBeforeMode = !isShowing() || wasHidden;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700443 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700444 mContextView.initForMode(mode);
445 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700446 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700447 // TODO animate this
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700448 if (mSplitView.getVisibility() != View.VISIBLE) {
449 mSplitView.setVisibility(View.VISIBLE);
450 if (mOverlayLayout != null) {
451 mOverlayLayout.requestFitSystemWindows();
452 }
453 }
Adam Powell89e06452010-06-23 20:24:52 -0700454 }
Adam Powell86ed4362011-09-14 16:18:53 -0700455 mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powell5d279772010-07-27 16:34:07 -0700456 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700457 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700458 }
Adam Powellac695c62010-07-20 18:19:27 -0700459 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700460 }
461
Adam Powell661c9082010-07-02 10:09:44 -0700462 private void configureTab(Tab tab, int position) {
463 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700464 final ActionBar.TabListener callback = tabi.getCallback();
465
466 if (callback == null) {
467 throw new IllegalStateException("Action Bar Tab must have a Callback");
468 }
Adam Powell661c9082010-07-02 10:09:44 -0700469
470 tabi.setPosition(position);
471 mTabs.add(position, tabi);
472
Adam Powell81b89442010-11-02 17:58:56 -0700473 final int count = mTabs.size();
474 for (int i = position + 1; i < count; i++) {
475 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700476 }
Adam Powell661c9082010-07-02 10:09:44 -0700477 }
478
479 @Override
480 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700481 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700482 }
483
484 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700485 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700486 addTab(tab, position, mTabs.isEmpty());
487 }
488
489 @Override
490 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700491 ensureTabsExist();
492 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700493 configureTab(tab, mTabs.size());
494 if (setSelected) {
495 selectTab(tab);
496 }
497 }
498
499 @Override
500 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700501 ensureTabsExist();
502 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700503 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700504 if (setSelected) {
505 selectTab(tab);
506 }
Adam Powell661c9082010-07-02 10:09:44 -0700507 }
508
509 @Override
510 public Tab newTab() {
511 return new TabImpl();
512 }
513
514 @Override
515 public void removeTab(Tab tab) {
516 removeTabAt(tab.getPosition());
517 }
518
519 @Override
520 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700521 if (mTabScrollView == null) {
522 // No tabs around to remove
523 return;
524 }
525
Adam Powell0c24a552010-11-03 16:44:11 -0700526 int selectedTabPosition = mSelectedTab != null
527 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700528 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700529 TabImpl removedTab = mTabs.remove(position);
530 if (removedTab != null) {
531 removedTab.setPosition(-1);
532 }
Adam Powell661c9082010-07-02 10:09:44 -0700533
534 final int newTabCount = mTabs.size();
535 for (int i = position; i < newTabCount; i++) {
536 mTabs.get(i).setPosition(i);
537 }
538
Adam Powell0c24a552010-11-03 16:44:11 -0700539 if (selectedTabPosition == position) {
540 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
541 }
Adam Powell661c9082010-07-02 10:09:44 -0700542 }
543
544 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700545 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700546 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
547 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
548 return;
549 }
550
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800551 final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction()
Adam Powell0c24a552010-11-03 16:44:11 -0700552 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700553
554 if (mSelectedTab == tab) {
555 if (mSelectedTab != null) {
556 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700557 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700558 }
559 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700560 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700561 if (mSelectedTab != null) {
562 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
563 }
564 mSelectedTab = (TabImpl) tab;
565 if (mSelectedTab != null) {
566 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
567 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700568 }
569
570 if (!trans.isEmpty()) {
571 trans.commit();
572 }
573 }
574
575 @Override
576 public Tab getSelectedTab() {
577 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700578 }
579
Adam Powell6b336f82010-08-10 20:13:01 -0700580 @Override
581 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700582 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700583 }
584
585 @Override
586 public void show() {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700587 show(true, false);
Adam Powell07e1f982011-03-24 11:11:15 -0700588 }
589
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700590 public void show(boolean markHiddenBeforeMode, boolean alwaysAnimate) {
Adam Powell07e1f982011-03-24 11:11:15 -0700591 if (mCurrentShowAnim != null) {
592 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800593 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700594 if (mTopVisibilityView.getVisibility() == View.VISIBLE) {
Adam Powell07e1f982011-03-24 11:11:15 -0700595 if (markHiddenBeforeMode) mWasHiddenBeforeMode = false;
Adam Powelld76cee22011-01-31 15:05:05 -0800596 return;
597 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700598 mTopVisibilityView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800599
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700600 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
601 || alwaysAnimate)) {
602 mTopVisibilityView.setAlpha(0);
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700603 mTopVisibilityView.setTranslationY(-mTopVisibilityView.getHeight());
Adam Powell50efbed2011-02-08 16:20:15 -0800604 AnimatorSet anim = new AnimatorSet();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700605 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView, "alpha", 1));
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700606 b.with(ObjectAnimator.ofFloat(mTopVisibilityView, "translationY", 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800607 if (mContentView != null) {
608 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700609 -mTopVisibilityView.getHeight(), 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800610 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700611 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell640a66e2011-04-29 10:18:53 -0700612 mSplitView.setAlpha(0);
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700613 mSplitView.setTranslationY(mSplitView.getHeight());
Adam Powell993a63a2011-08-18 16:35:53 -0700614 mSplitView.setVisibility(View.VISIBLE);
Adam Powell640a66e2011-04-29 10:18:53 -0700615 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 1));
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700616 b.with(ObjectAnimator.ofFloat(mSplitView, "translationY", 0));
Adam Powell640a66e2011-04-29 10:18:53 -0700617 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700618 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
619 com.android.internal.R.interpolator.decelerate_quad));
Adam Powell50efbed2011-02-08 16:20:15 -0800620 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700621 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800622 anim.start();
623 } else {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700624 mTopVisibilityView.setAlpha(1);
Adam Powelld8145042011-03-24 14:18:27 -0700625 mContainerView.setTranslationY(0);
Adam Powell50efbed2011-02-08 16:20:15 -0800626 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800627 }
Adam Powell6b336f82010-08-10 20:13:01 -0700628 }
629
630 @Override
631 public void hide() {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700632 hide(false);
633 }
634
635 public void hide(boolean alwaysAnimate) {
Adam Powell07e1f982011-03-24 11:11:15 -0700636 if (mCurrentShowAnim != null) {
637 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800638 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700639 if (mTopVisibilityView.getVisibility() == View.GONE) {
Adam Powelle6ec7322010-12-07 15:29:26 -0800640 return;
641 }
Adam Powell50efbed2011-02-08 16:20:15 -0800642
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700643 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
644 || alwaysAnimate)) {
645 mTopVisibilityView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800646 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800647 AnimatorSet anim = new AnimatorSet();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700648 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView, "alpha", 0));
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700649 b.with(ObjectAnimator.ofFloat(mTopVisibilityView, "translationY",
650 -mTopVisibilityView.getHeight()));
Adam Powell50efbed2011-02-08 16:20:15 -0800651 if (mContentView != null) {
652 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700653 0, -mTopVisibilityView.getHeight()));
Adam Powell50efbed2011-02-08 16:20:15 -0800654 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700655 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700656 mSplitView.setAlpha(1);
657 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 0));
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700658 b.with(ObjectAnimator.ofFloat(mSplitView, "translationY",
659 mSplitView.getHeight()));
Adam Powell640a66e2011-04-29 10:18:53 -0700660 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700661 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
662 com.android.internal.R.interpolator.accelerate_quad));
Adam Powell50efbed2011-02-08 16:20:15 -0800663 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700664 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800665 anim.start();
666 } else {
667 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800668 }
Adam Powell6b336f82010-08-10 20:13:01 -0700669 }
670
671 public boolean isShowing() {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700672 return mTopVisibilityView.getVisibility() == View.VISIBLE;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800673 }
674
Adam Powell640a66e2011-04-29 10:18:53 -0700675 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700676 if (toActionMode) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700677 show(false, false);
Adam Powell060e3ca2011-07-19 20:39:16 -0700678 }
Adam Powell07e1f982011-03-24 11:11:15 -0700679 if (mCurrentModeAnim != null) {
680 mCurrentModeAnim.end();
681 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800682
Adam Powell640a66e2011-04-29 10:18:53 -0700683 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
684 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700685 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
686 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
687 }
Adam Powell6b336f82010-08-10 20:13:01 -0700688 }
689
Adam Powell88ab6972011-07-28 11:25:01 -0700690 public Context getThemedContext() {
691 if (mThemedContext == null) {
692 TypedValue outValue = new TypedValue();
693 Resources.Theme currentTheme = mContext.getTheme();
694 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
695 outValue, true);
696 final int targetThemeRes = outValue.resourceId;
697
698 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
699 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
700 } else {
701 mThemedContext = mContext;
702 }
703 }
704 return mThemedContext;
705 }
706
Adam Powell89e06452010-06-23 20:24:52 -0700707 /**
708 * @hide
709 */
Adam Powell5d279772010-07-27 16:34:07 -0700710 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700711 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700712 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700713 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700714
Adam Powell5d279772010-07-27 16:34:07 -0700715 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700716 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700717 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700718 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700719 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700720 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700721
722 @Override
723 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700724 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700725 }
726
Adam Powell89e06452010-06-23 20:24:52 -0700727 @Override
728 public Menu getMenu() {
729 return mMenu;
730 }
731
732 @Override
733 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700734 if (mActionMode != this) {
735 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700736 return;
737 }
738
Adam Powell060e3ca2011-07-19 20:39:16 -0700739 // If we were hidden before the mode was shown, defer the onDestroy
740 // callback until the animation is finished and associated relayout
741 // is about to happen. This lets apps better anticipate visibility
742 // and layout behavior.
743 if (mWasHiddenBeforeMode) {
744 mDeferredDestroyActionMode = this;
745 mDeferredModeDestroyCallback = mCallback;
746 } else {
747 mCallback.onDestroyActionMode(this);
748 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800749 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700750 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700751
752 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700753 mContextView.closeMode();
Adam Powell86ed4362011-09-14 16:18:53 -0700754 mActionView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700755
Adam Powell5d279772010-07-27 16:34:07 -0700756 mActionMode = null;
Adam Powell07e1f982011-03-24 11:11:15 -0700757
758 if (mWasHiddenBeforeMode) {
759 hide();
760 }
Adam Powell89e06452010-06-23 20:24:52 -0700761 }
762
763 @Override
764 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700765 mMenu.stopDispatchingItemsChanged();
766 try {
767 mCallback.onPrepareActionMode(this, mMenu);
768 } finally {
769 mMenu.startDispatchingItemsChanged();
770 }
771 }
772
773 public boolean dispatchOnCreate() {
774 mMenu.stopDispatchingItemsChanged();
775 try {
776 return mCallback.onCreateActionMode(this, mMenu);
777 } finally {
778 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700779 }
780 }
781
782 @Override
783 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700784 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700785 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700786 }
787
788 @Override
789 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700790 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700791 }
792
793 @Override
794 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700795 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700796 }
Adam Powell29ed7572010-07-14 16:24:56 -0700797
798 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700799 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800800 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700801 }
802
803 @Override
804 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800805 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700806 }
807
808 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700809 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700810 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700811 }
812
813 @Override
814 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700815 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700816 }
Adam Powell89e06452010-06-23 20:24:52 -0700817
Adam Powell29ed7572010-07-14 16:24:56 -0700818 @Override
Adam Powellb98a81f2012-02-24 11:09:07 -0800819 public void setTitleOptionalHint(boolean titleOptional) {
Adam Powell785c4472012-05-02 21:25:39 -0700820 super.setTitleOptionalHint(titleOptional);
Adam Powellb98a81f2012-02-24 11:09:07 -0800821 mContextView.setTitleOptional(titleOptional);
822 }
823
824 @Override
825 public boolean isTitleOptional() {
826 return mContextView.isTitleOptional();
827 }
828
829 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700830 public View getCustomView() {
831 return mCustomView != null ? mCustomView.get() : null;
832 }
833
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700834 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800835 if (mCallback != null) {
836 return mCallback.onActionItemClicked(this, item);
837 } else {
838 return false;
839 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700840 }
841
842 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
843 }
844
845 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800846 if (mCallback == null) {
847 return false;
848 }
849
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700850 if (!subMenu.hasVisibleItems()) {
851 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700852 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700853
Adam Powellf2423682011-08-11 14:29:45 -0700854 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700855 return true;
856 }
857
858 public void onCloseSubMenu(SubMenuBuilder menu) {
859 }
860
861 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800862 if (mCallback == null) {
863 return;
864 }
Adam Powellf6148c52010-08-11 21:10:16 -0700865 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700866 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700867 }
Adam Powell661c9082010-07-02 10:09:44 -0700868 }
869
870 /**
871 * @hide
872 */
873 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700874 private ActionBar.TabListener mCallback;
875 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700876 private Drawable mIcon;
877 private CharSequence mText;
Adam Powell94e56ef2011-09-06 21:22:22 -0700878 private CharSequence mContentDesc;
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700879 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -0700880 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700881
882 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700883 public Object getTag() {
884 return mTag;
885 }
886
887 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700888 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700889 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700890 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700891 }
892
893 public ActionBar.TabListener getCallback() {
894 return mCallback;
895 }
896
897 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700898 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700899 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700900 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700901 }
902
903 @Override
904 public View getCustomView() {
905 return mCustomView;
906 }
907
908 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700909 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700910 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700911 if (mPosition >= 0) {
912 mTabScrollView.updateTab(mPosition);
913 }
Adam Powell9ab97872010-10-26 21:47:29 -0700914 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700915 }
916
917 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800918 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -0700919 return setCustomView(LayoutInflater.from(getThemedContext())
920 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -0800921 }
922
923 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700924 public Drawable getIcon() {
925 return mIcon;
926 }
927
928 @Override
929 public int getPosition() {
930 return mPosition;
931 }
932
933 public void setPosition(int position) {
934 mPosition = position;
935 }
936
937 @Override
938 public CharSequence getText() {
939 return mText;
940 }
941
942 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700943 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -0700944 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700945 if (mPosition >= 0) {
946 mTabScrollView.updateTab(mPosition);
947 }
Adam Powell9ab97872010-10-26 21:47:29 -0700948 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700949 }
950
951 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800952 public Tab setIcon(int resId) {
953 return setIcon(mContext.getResources().getDrawable(resId));
954 }
955
956 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700957 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -0700958 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700959 if (mPosition >= 0) {
960 mTabScrollView.updateTab(mPosition);
961 }
Adam Powell9ab97872010-10-26 21:47:29 -0700962 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700963 }
964
965 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800966 public Tab setText(int resId) {
967 return setText(mContext.getResources().getText(resId));
968 }
969
970 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700971 public void select() {
972 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700973 }
Adam Powell94e56ef2011-09-06 21:22:22 -0700974
975 @Override
976 public Tab setContentDescription(int resId) {
977 return setContentDescription(mContext.getResources().getText(resId));
978 }
979
980 @Override
981 public Tab setContentDescription(CharSequence contentDesc) {
982 mContentDesc = contentDesc;
983 if (mPosition >= 0) {
984 mTabScrollView.updateTab(mPosition);
985 }
986 return this;
987 }
988
989 @Override
990 public CharSequence getContentDescription() {
991 return mContentDesc;
992 }
Adam Powell89e06452010-06-23 20:24:52 -0700993 }
Adam Powell9ab97872010-10-26 21:47:29 -0700994
995 @Override
996 public void setCustomView(View view) {
997 mActionView.setCustomNavigationView(view);
998 }
999
1000 @Override
1001 public void setCustomView(View view, LayoutParams layoutParams) {
1002 view.setLayoutParams(layoutParams);
1003 mActionView.setCustomNavigationView(view);
1004 }
1005
1006 @Override
Adam Powell8515ee82010-11-30 14:09:55 -08001007 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -07001008 mActionView.setDropdownAdapter(adapter);
1009 mActionView.setCallback(callback);
1010 }
1011
1012 @Override
1013 public int getSelectedNavigationIndex() {
1014 switch (mActionView.getNavigationMode()) {
1015 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -08001016 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -07001017 case NAVIGATION_MODE_LIST:
1018 return mActionView.getDropdownSelectedPosition();
1019 default:
1020 return -1;
1021 }
1022 }
1023
1024 @Override
1025 public int getNavigationItemCount() {
1026 switch (mActionView.getNavigationMode()) {
1027 case NAVIGATION_MODE_TABS:
1028 return mTabs.size();
1029 case NAVIGATION_MODE_LIST:
1030 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
1031 return adapter != null ? adapter.getCount() : 0;
1032 default:
1033 return 0;
1034 }
1035 }
1036
1037 @Override
Adam Powell0c24a552010-11-03 16:44:11 -07001038 public int getTabCount() {
1039 return mTabs.size();
1040 }
1041
1042 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001043 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -07001044 final int oldMode = mActionView.getNavigationMode();
1045 switch (oldMode) {
1046 case NAVIGATION_MODE_TABS:
1047 mSavedTabPosition = getSelectedNavigationIndex();
1048 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -07001049 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -07001050 break;
1051 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001052 if (oldMode != mode && !mHasEmbeddedTabs) {
1053 if (mOverlayLayout != null) {
1054 mOverlayLayout.requestFitSystemWindows();
1055 }
1056 }
Adam Powell9ab97872010-10-26 21:47:29 -07001057 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -07001058 switch (mode) {
1059 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -07001060 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -07001061 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -07001062 if (mSavedTabPosition != INVALID_POSITION) {
1063 setSelectedNavigationItem(mSavedTabPosition);
1064 mSavedTabPosition = INVALID_POSITION;
1065 }
1066 break;
1067 }
Adam Powelld21aa122011-05-27 13:09:52 -07001068 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -07001069 }
1070
1071 @Override
1072 public Tab getTabAt(int index) {
1073 return mTabs.get(index);
1074 }
Adam Powell0c24a552010-11-03 16:44:11 -07001075
Adam Powell0c24a552010-11-03 16:44:11 -07001076
Adam Powell1969b872011-03-22 11:52:48 -07001077 @Override
1078 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001079 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001080 }
Adam Powell0c24a552010-11-03 16:44:11 -07001081
Adam Powell1969b872011-03-22 11:52:48 -07001082 @Override
1083 public void setIcon(Drawable icon) {
1084 mActionView.setIcon(icon);
1085 }
1086
1087 @Override
1088 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001089 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001090 }
1091
1092 @Override
1093 public void setLogo(Drawable logo) {
1094 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -07001095 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001096
1097 public void setDefaultDisplayHomeAsUpEnabled(boolean enable) {
1098 if (!mDisplayHomeAsUpSet) {
1099 setDisplayHomeAsUpEnabled(enable);
1100 }
1101 }
Adam Powell89e06452010-06-23 20:24:52 -07001102}