blob: fb93ddda7c2c2c2166864c48424c8db43a5f3c63 [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 Powell9b0dc282013-07-31 13:58:43 -070019import android.animation.ValueAnimator;
20import android.view.ViewParent;
Adam Powellb8139af2012-04-19 13:52:46 -070021import com.android.internal.view.ActionBarPolicy;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070022import com.android.internal.view.menu.MenuBuilder;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070023import com.android.internal.view.menu.MenuPopupHelper;
24import com.android.internal.view.menu.SubMenuBuilder;
Adam Powell01feaee2011-02-10 15:05:05 -080025import com.android.internal.widget.ActionBarContainer;
Adam Powell89e06452010-06-23 20:24:52 -070026import com.android.internal.widget.ActionBarContextView;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070027import com.android.internal.widget.ActionBarOverlayLayout;
Adam Powell89e06452010-06-23 20:24:52 -070028import com.android.internal.widget.ActionBarView;
Adam Powellf8ac6b72011-05-23 18:14:09 -070029import com.android.internal.widget.ScrollingTabContainerView;
Adam Powell89e06452010-06-23 20:24:52 -070030
Adam Powelld8b3f2e2010-12-02 13:37:03 -080031import android.animation.Animator;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080032import android.animation.Animator.AnimatorListener;
Adam Powell07e1f982011-03-24 11:11:15 -070033import android.animation.AnimatorListenerAdapter;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080034import android.animation.AnimatorSet;
Adam Powelle6ec7322010-12-07 15:29:26 -080035import android.animation.ObjectAnimator;
Adam Powell89e06452010-06-23 20:24:52 -070036import android.app.ActionBar;
Adam Powell661c9082010-07-02 10:09:44 -070037import android.app.Activity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070038import android.app.Dialog;
Adam Powell661c9082010-07-02 10:09:44 -070039import android.app.FragmentTransaction;
Adam Powelldec9dfd2010-08-09 15:27:54 -070040import android.content.Context;
Adam Powellf8ac6b72011-05-23 18:14:09 -070041import android.content.res.Configuration;
Adam Powell88ab6972011-07-28 11:25:01 -070042import android.content.res.Resources;
Adam Powell89e06452010-06-23 20:24:52 -070043import android.graphics.drawable.Drawable;
Adam Powell0e94b512010-06-29 17:58:20 -070044import android.os.Handler;
Adam Powell88ab6972011-07-28 11:25:01 -070045import android.util.TypedValue;
Adam Powell6e346362010-07-23 10:18:23 -070046import android.view.ActionMode;
Adam Powell88ab6972011-07-28 11:25:01 -070047import android.view.ContextThemeWrapper;
Adam Powell32555f32010-11-17 13:49:22 -080048import android.view.LayoutInflater;
Adam Powell89e06452010-06-23 20:24:52 -070049import android.view.Menu;
Adam Powell9168f0b2010-08-02 15:46:24 -070050import android.view.MenuInflater;
Adam Powell89e06452010-06-23 20:24:52 -070051import android.view.MenuItem;
52import android.view.View;
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;
George Mounte1803372014-02-26 19:00:52 +000060import java.util.Map;
Adam Powell661c9082010-07-02 10:09:44 -070061
Adam Powell89e06452010-06-23 20:24:52 -070062/**
Adam Powelle43340c2014-03-17 19:10:43 -070063 * WindowDecorActionBar is the ActionBar implementation used
64 * by devices of all screen sizes as part of the window decor layout.
65 * If it detects a compatible decor, it will split contextual modes
66 * across both the ActionBarView at the top of the screen and
67 * a horizontal LinearLayout at the bottom which is normally hidden.
Adam Powell89e06452010-06-23 20:24:52 -070068 */
Adam Powelle43340c2014-03-17 19:10:43 -070069public class WindowDecorActionBar extends ActionBar {
70 private static final String TAG = "WindowDecorActionBar";
Adam Powell661c9082010-07-02 10:09:44 -070071
Adam Powelldec9dfd2010-08-09 15:27:54 -070072 private Context mContext;
Adam Powell88ab6972011-07-28 11:25:01 -070073 private Context mThemedContext;
Adam Powell661c9082010-07-02 10:09:44 -070074 private Activity mActivity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070075 private Dialog mDialog;
Adam Powell661c9082010-07-02 10:09:44 -070076
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070077 private ActionBarOverlayLayout mOverlayLayout;
Adam Powell01feaee2011-02-10 15:05:05 -080078 private ActionBarContainer mContainerView;
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
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700108 private int mCurWindowVisibility = View.VISIBLE;
109
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800110 private boolean mContentAnimations = true;
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700111 private boolean mHiddenByApp;
112 private boolean mHiddenBySystem;
113 private boolean mShowingForMode;
114
115 private boolean mNowShowing = true;
116
Adam Powell07e1f982011-03-24 11:11:15 -0700117 private Animator mCurrentShowAnim;
Adam Powell50efbed2011-02-08 16:20:15 -0800118 private boolean mShowHideAnimationEnabled;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800119
Adam Powell07e1f982011-03-24 11:11:15 -0700120 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800121 @Override
122 public void onAnimationEnd(Animator animation) {
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800123 if (mContentAnimations && mContentView != null) {
Adam Powelle6ec7322010-12-07 15:29:26 -0800124 mContentView.setTranslationY(0);
Adam Powell9b0dc282013-07-31 13:58:43 -0700125 mContainerView.setTranslationY(0);
Adam Powelle6ec7322010-12-07 15:29:26 -0800126 }
Adam Powell993a63a2011-08-18 16:35:53 -0700127 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
128 mSplitView.setVisibility(View.GONE);
129 }
Adam Powell9b0dc282013-07-31 13:58:43 -0700130 mContainerView.setVisibility(View.GONE);
Adam Powell01feaee2011-02-10 15:05:05 -0800131 mContainerView.setTransitioning(false);
Adam Powell07e1f982011-03-24 11:11:15 -0700132 mCurrentShowAnim = null;
Adam Powell060e3ca2011-07-19 20:39:16 -0700133 completeDeferredDestroyActionMode();
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700134 if (mOverlayLayout != null) {
135 mOverlayLayout.requestFitSystemWindows();
136 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800137 }
138 };
139
Adam Powell07e1f982011-03-24 11:11:15 -0700140 final AnimatorListener mShowListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800141 @Override
142 public void onAnimationEnd(Animator animation) {
Adam Powell07e1f982011-03-24 11:11:15 -0700143 mCurrentShowAnim = null;
Adam Powell9b0dc282013-07-31 13:58:43 -0700144 mContainerView.requestLayout();
145 }
146 };
147
148 final ValueAnimator.AnimatorUpdateListener mUpdateListener =
149 new ValueAnimator.AnimatorUpdateListener() {
150 @Override
151 public void onAnimationUpdate(ValueAnimator animation) {
152 final ViewParent parent = mContainerView.getParent();
153 ((View) parent).invalidate();
Adam Powelle6ec7322010-12-07 15:29:26 -0800154 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800155 };
156
Adam Powelle43340c2014-03-17 19:10:43 -0700157 public WindowDecorActionBar(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -0700158 mActivity = activity;
Adam Powelle6ec7322010-12-07 15:29:26 -0800159 Window window = activity.getWindow();
160 View decor = window.getDecorView();
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800161 boolean overlayMode = mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
Adam Powell9b0dc282013-07-31 13:58:43 -0700162 init(decor);
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800163 if (!overlayMode) {
Adam Powelle6ec7322010-12-07 15:29:26 -0800164 mContentView = decor.findViewById(android.R.id.content);
165 }
Adam Powelldec9dfd2010-08-09 15:27:54 -0700166 }
167
Adam Powelle43340c2014-03-17 19:10:43 -0700168 public WindowDecorActionBar(Dialog dialog) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700169 mDialog = dialog;
Adam Powell9b0dc282013-07-31 13:58:43 -0700170 init(dialog.getWindow().getDecorView());
Adam Powelldec9dfd2010-08-09 15:27:54 -0700171 }
172
Deepanshu Gupta14bf0ce2013-12-12 12:16:24 -0800173 /**
174 * Only for edit mode.
175 * @hide
176 */
177 public WindowDecorActionBar(View layout) {
178 assert layout.isInEditMode();
179 init(layout);
180 }
181
Adam Powell9b0dc282013-07-31 13:58:43 -0700182 private void init(View decor) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700183 mOverlayLayout = (ActionBarOverlayLayout) decor.findViewById(
184 com.android.internal.R.id.action_bar_overlay_layout);
185 if (mOverlayLayout != null) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700186 mOverlayLayout.setActionBar(this);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700187 }
Adam Powell89e06452010-06-23 20:24:52 -0700188 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
Adam Powell640a66e2011-04-29 10:18:53 -0700189 mContextView = (ActionBarContextView) decor.findViewById(
Adam Powell89e06452010-06-23 20:24:52 -0700190 com.android.internal.R.id.action_context_bar);
Adam Powell01feaee2011-02-10 15:05:05 -0800191 mContainerView = (ActionBarContainer) decor.findViewById(
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800192 com.android.internal.R.id.action_bar_container);
Adam Powell640a66e2011-04-29 10:18:53 -0700193 mSplitView = (ActionBarContainer) decor.findViewById(
194 com.android.internal.R.id.split_action_bar);
Steve Block08f194b2010-08-24 18:20:48 +0100195
Adam Powell640a66e2011-04-29 10:18:53 -0700196 if (mActionView == null || mContextView == null || mContainerView == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700197 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
198 "with a compatible window decor layout");
199 }
Adam Powell0e94b512010-06-29 17:58:20 -0700200
Alan Viverette62599332014-04-01 14:57:39 -0700201 mContext = mActionView.getContext();
Adam Powell640a66e2011-04-29 10:18:53 -0700202 mActionView.setContextView(mContextView);
Adam Powell9b4bee02011-04-27 19:24:47 -0700203 mContextDisplayMode = mActionView.isSplitActionBar() ?
204 CONTEXT_DISPLAY_SPLIT : CONTEXT_DISPLAY_NORMAL;
Adam Powelldae78242011-04-25 15:23:41 -0700205
Adam Powelld40423a2012-05-02 14:06:03 -0700206 // This was initially read from the action bar style
207 final int current = mActionView.getDisplayOptions();
208 final boolean homeAsUp = (current & DISPLAY_HOME_AS_UP) != 0;
209 if (homeAsUp) {
210 mDisplayHomeAsUpSet = true;
211 }
212
Adam Powellb8139af2012-04-19 13:52:46 -0700213 ActionBarPolicy abp = ActionBarPolicy.get(mContext);
Adam Powelld40423a2012-05-02 14:06:03 -0700214 setHomeButtonEnabled(abp.enableHomeButtonByDefault() || homeAsUp);
Adam Powellb8139af2012-04-19 13:52:46 -0700215 setHasEmbeddedTabs(abp.hasEmbeddedTabs());
Adam Powellf8ac6b72011-05-23 18:14:09 -0700216 }
217
218 public void onConfigurationChanged(Configuration newConfig) {
Adam Powellb8139af2012-04-19 13:52:46 -0700219 setHasEmbeddedTabs(ActionBarPolicy.get(mContext).hasEmbeddedTabs());
Adam Powellf5645cb2011-08-10 22:49:02 -0700220 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700221
Adam Powellf5645cb2011-08-10 22:49:02 -0700222 private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) {
223 mHasEmbeddedTabs = hasEmbeddedTabs;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700224 // Switch tab layout configuration if needed
225 if (!mHasEmbeddedTabs) {
226 mActionView.setEmbeddedTabView(null);
227 mContainerView.setTabContainer(mTabScrollView);
228 } else {
229 mContainerView.setTabContainer(null);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700230 mActionView.setEmbeddedTabView(mTabScrollView);
231 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700232 final boolean isInTabMode = getNavigationMode() == NAVIGATION_MODE_TABS;
233 if (mTabScrollView != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700234 if (isInTabMode) {
235 mTabScrollView.setVisibility(View.VISIBLE);
236 if (mOverlayLayout != null) {
237 mOverlayLayout.requestFitSystemWindows();
238 }
239 } else {
240 mTabScrollView.setVisibility(View.GONE);
241 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700242 }
243 mActionView.setCollapsable(!mHasEmbeddedTabs && isInTabMode);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700244 }
245
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700246 public boolean hasNonEmbeddedTabs() {
247 return !mHasEmbeddedTabs && getNavigationMode() == NAVIGATION_MODE_TABS;
248 }
249
Adam Powellf8ac6b72011-05-23 18:14:09 -0700250 private void ensureTabsExist() {
251 if (mTabScrollView != null) {
252 return;
253 }
254
Adam Powellf5645cb2011-08-10 22:49:02 -0700255 ScrollingTabContainerView tabScroller = new ScrollingTabContainerView(mContext);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700256
257 if (mHasEmbeddedTabs) {
258 tabScroller.setVisibility(View.VISIBLE);
259 mActionView.setEmbeddedTabView(tabScroller);
260 } else {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700261 if (getNavigationMode() == NAVIGATION_MODE_TABS) {
262 tabScroller.setVisibility(View.VISIBLE);
263 if (mOverlayLayout != null) {
264 mOverlayLayout.requestFitSystemWindows();
265 }
266 } else {
267 tabScroller.setVisibility(View.GONE);
268 }
Adam Powelldae78242011-04-25 15:23:41 -0700269 mContainerView.setTabContainer(tabScroller);
Adam Powelldae78242011-04-25 15:23:41 -0700270 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700271 mTabScrollView = tabScroller;
Adam Powell89e06452010-06-23 20:24:52 -0700272 }
273
Adam Powell060e3ca2011-07-19 20:39:16 -0700274 void completeDeferredDestroyActionMode() {
275 if (mDeferredModeDestroyCallback != null) {
276 mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode);
277 mDeferredDestroyActionMode = null;
278 mDeferredModeDestroyCallback = null;
279 }
280 }
281
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700282 public void setWindowVisibility(int visibility) {
283 mCurWindowVisibility = visibility;
284 }
285
Adam Powell50efbed2011-02-08 16:20:15 -0800286 /**
287 * Enables or disables animation between show/hide states.
288 * If animation is disabled using this method, animations in progress
289 * will be finished.
290 *
291 * @param enabled true to animate, false to not animate.
292 */
293 public void setShowHideAnimationEnabled(boolean enabled) {
294 mShowHideAnimationEnabled = enabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700295 if (!enabled && mCurrentShowAnim != null) {
296 mCurrentShowAnim.end();
Adam Powell50efbed2011-02-08 16:20:15 -0800297 }
298 }
299
Adam Powell8515ee82010-11-30 14:09:55 -0800300 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
301 mMenuVisibilityListeners.add(listener);
302 }
303
304 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
305 mMenuVisibilityListeners.remove(listener);
306 }
307
308 public void dispatchMenuVisibilityChanged(boolean isVisible) {
309 if (isVisible == mLastMenuVisibility) {
310 return;
311 }
312 mLastMenuVisibility = isVisible;
313
314 final int count = mMenuVisibilityListeners.size();
315 for (int i = 0; i < count; i++) {
316 mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
317 }
318 }
319
Adam Powella66c7b02010-07-28 15:28:25 -0700320 @Override
Adam Powell3f476b32011-01-03 19:25:36 -0800321 public void setCustomView(int resId) {
Adam Powellf2423682011-08-11 14:29:45 -0700322 setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId, mActionView, false));
Adam Powell3f476b32011-01-03 19:25:36 -0800323 }
324
325 @Override
326 public void setDisplayUseLogoEnabled(boolean useLogo) {
327 setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
328 }
329
330 @Override
331 public void setDisplayShowHomeEnabled(boolean showHome) {
332 setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
333 }
334
335 @Override
336 public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
337 setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
338 }
339
340 @Override
341 public void setDisplayShowTitleEnabled(boolean showTitle) {
342 setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
343 }
344
345 @Override
346 public void setDisplayShowCustomEnabled(boolean showCustom) {
347 setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
348 }
349
350 @Override
Adam Powellc29f4e52011-07-13 20:40:52 -0700351 public void setHomeButtonEnabled(boolean enable) {
352 mActionView.setHomeButtonEnabled(enable);
Adam Powelldae78242011-04-25 15:23:41 -0700353 }
354
355 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700356 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700357 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700358 }
359
360 @Override
361 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700362 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700363 }
364
George Mounte1803372014-02-26 19:00:52 +0000365 public void captureSharedElements(Map<String, View> sharedElements) {
366 mContainerView.findSharedElements(sharedElements);
367 }
368
Adam Powell17809772010-07-21 13:25:11 -0700369 public void setSelectedNavigationItem(int position) {
370 switch (mActionView.getNavigationMode()) {
371 case NAVIGATION_MODE_TABS:
372 selectTab(mTabs.get(position));
373 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700374 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700375 mActionView.setDropdownSelectedPosition(position);
376 break;
377 default:
378 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700379 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700380 }
381 }
382
Adam Powell9ab97872010-10-26 21:47:29 -0700383 public void removeAllTabs() {
384 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700385 }
386
Adam Powell661c9082010-07-02 10:09:44 -0700387 private void cleanupTabs() {
388 if (mSelectedTab != null) {
389 selectTab(null);
390 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700391 mTabs.clear();
Adam Powelld21aa122011-05-27 13:09:52 -0700392 if (mTabScrollView != null) {
393 mTabScrollView.removeAllTabs();
394 }
Adam Powell0c24a552010-11-03 16:44:11 -0700395 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700396 }
397
Adam Powell0e94b512010-06-29 17:58:20 -0700398 public void setTitle(CharSequence title) {
399 mActionView.setTitle(title);
400 }
401
402 public void setSubtitle(CharSequence subtitle) {
403 mActionView.setSubtitle(subtitle);
404 }
405
Adam Powell89e06452010-06-23 20:24:52 -0700406 public void setDisplayOptions(int options) {
Adam Powelldd8fab22012-03-22 17:47:27 -0700407 if ((options & DISPLAY_HOME_AS_UP) != 0) {
408 mDisplayHomeAsUpSet = true;
409 }
Adam Powell89e06452010-06-23 20:24:52 -0700410 mActionView.setDisplayOptions(options);
411 }
Adam Powell0e94b512010-06-29 17:58:20 -0700412
Adam Powell89e06452010-06-23 20:24:52 -0700413 public void setDisplayOptions(int options, int mask) {
414 final int current = mActionView.getDisplayOptions();
Adam Powelldd8fab22012-03-22 17:47:27 -0700415 if ((mask & DISPLAY_HOME_AS_UP) != 0) {
416 mDisplayHomeAsUpSet = true;
417 }
Adam Powell89e06452010-06-23 20:24:52 -0700418 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
419 }
420
421 public void setBackgroundDrawable(Drawable d) {
Adam Powellf88b9152011-09-07 14:54:32 -0700422 mContainerView.setPrimaryBackground(d);
423 }
424
425 public void setStackedBackgroundDrawable(Drawable d) {
426 mContainerView.setStackedBackground(d);
427 }
428
429 public void setSplitBackgroundDrawable(Drawable d) {
430 if (mSplitView != null) {
431 mSplitView.setSplitBackground(d);
432 }
Adam Powell89e06452010-06-23 20:24:52 -0700433 }
434
Adam Powellef704442010-11-16 14:48:22 -0800435 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700436 return mActionView.getCustomNavigationView();
437 }
438
439 public CharSequence getTitle() {
440 return mActionView.getTitle();
441 }
442
443 public CharSequence getSubtitle() {
444 return mActionView.getSubtitle();
445 }
446
447 public int getNavigationMode() {
448 return mActionView.getNavigationMode();
449 }
450
451 public int getDisplayOptions() {
452 return mActionView.getDisplayOptions();
453 }
454
Adam Powell5d279772010-07-27 16:34:07 -0700455 public ActionMode startActionMode(ActionMode.Callback callback) {
456 if (mActionMode != null) {
457 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700458 }
Adam Powell3461b322010-07-14 11:34:43 -0700459
Adam Powell640a66e2011-04-29 10:18:53 -0700460 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700461 ActionModeImpl mode = new ActionModeImpl(callback);
462 if (mode.dispatchOnCreate()) {
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700463 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700464 mContextView.initForMode(mode);
465 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700466 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700467 // TODO animate this
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700468 if (mSplitView.getVisibility() != View.VISIBLE) {
469 mSplitView.setVisibility(View.VISIBLE);
470 if (mOverlayLayout != null) {
471 mOverlayLayout.requestFitSystemWindows();
472 }
473 }
Adam Powell89e06452010-06-23 20:24:52 -0700474 }
Adam Powell86ed4362011-09-14 16:18:53 -0700475 mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powell5d279772010-07-27 16:34:07 -0700476 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700477 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700478 }
Adam Powellac695c62010-07-20 18:19:27 -0700479 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700480 }
481
Adam Powell661c9082010-07-02 10:09:44 -0700482 private void configureTab(Tab tab, int position) {
483 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700484 final ActionBar.TabListener callback = tabi.getCallback();
485
486 if (callback == null) {
487 throw new IllegalStateException("Action Bar Tab must have a Callback");
488 }
Adam Powell661c9082010-07-02 10:09:44 -0700489
490 tabi.setPosition(position);
491 mTabs.add(position, tabi);
492
Adam Powell81b89442010-11-02 17:58:56 -0700493 final int count = mTabs.size();
494 for (int i = position + 1; i < count; i++) {
495 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700496 }
Adam Powell661c9082010-07-02 10:09:44 -0700497 }
498
499 @Override
500 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700501 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700502 }
503
504 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700505 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700506 addTab(tab, position, mTabs.isEmpty());
507 }
508
509 @Override
510 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700511 ensureTabsExist();
512 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700513 configureTab(tab, mTabs.size());
514 if (setSelected) {
515 selectTab(tab);
516 }
517 }
518
519 @Override
520 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700521 ensureTabsExist();
522 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700523 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700524 if (setSelected) {
525 selectTab(tab);
526 }
Adam Powell661c9082010-07-02 10:09:44 -0700527 }
528
529 @Override
530 public Tab newTab() {
531 return new TabImpl();
532 }
533
534 @Override
535 public void removeTab(Tab tab) {
536 removeTabAt(tab.getPosition());
537 }
538
539 @Override
540 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700541 if (mTabScrollView == null) {
542 // No tabs around to remove
543 return;
544 }
545
Adam Powell0c24a552010-11-03 16:44:11 -0700546 int selectedTabPosition = mSelectedTab != null
547 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700548 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700549 TabImpl removedTab = mTabs.remove(position);
550 if (removedTab != null) {
551 removedTab.setPosition(-1);
552 }
Adam Powell661c9082010-07-02 10:09:44 -0700553
554 final int newTabCount = mTabs.size();
555 for (int i = position; i < newTabCount; i++) {
556 mTabs.get(i).setPosition(i);
557 }
558
Adam Powell0c24a552010-11-03 16:44:11 -0700559 if (selectedTabPosition == position) {
560 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
561 }
Adam Powell661c9082010-07-02 10:09:44 -0700562 }
563
564 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700565 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700566 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
567 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
568 return;
569 }
570
Deepanshu Gupta14bf0ce2013-12-12 12:16:24 -0800571 final FragmentTransaction trans = mActionView.isInEditMode() ? null :
572 mActivity.getFragmentManager().beginTransaction().disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700573
574 if (mSelectedTab == tab) {
575 if (mSelectedTab != null) {
576 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700577 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700578 }
579 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700580 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700581 if (mSelectedTab != null) {
582 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
583 }
584 mSelectedTab = (TabImpl) tab;
585 if (mSelectedTab != null) {
586 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
587 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700588 }
589
Deepanshu Gupta14bf0ce2013-12-12 12:16:24 -0800590 if (trans != null && !trans.isEmpty()) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700591 trans.commit();
592 }
593 }
594
595 @Override
596 public Tab getSelectedTab() {
597 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700598 }
599
Adam Powell6b336f82010-08-10 20:13:01 -0700600 @Override
601 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700602 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700603 }
604
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800605 public void enableContentAnimations(boolean enabled) {
606 mContentAnimations = enabled;
607 }
608
Adam Powell6b336f82010-08-10 20:13:01 -0700609 @Override
610 public void show() {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700611 if (mHiddenByApp) {
612 mHiddenByApp = false;
613 updateVisibility(false);
614 }
Adam Powell07e1f982011-03-24 11:11:15 -0700615 }
616
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700617 private void showForActionMode() {
618 if (!mShowingForMode) {
619 mShowingForMode = true;
620 if (mOverlayLayout != null) {
621 mOverlayLayout.setShowingForActionMode(true);
622 }
623 updateVisibility(false);
624 }
625 }
626
627 public void showForSystem() {
628 if (mHiddenBySystem) {
629 mHiddenBySystem = false;
630 updateVisibility(true);
631 }
632 }
633
634 @Override
635 public void hide() {
636 if (!mHiddenByApp) {
637 mHiddenByApp = true;
638 updateVisibility(false);
639 }
640 }
641
642 private void hideForActionMode() {
643 if (mShowingForMode) {
644 mShowingForMode = false;
645 if (mOverlayLayout != null) {
646 mOverlayLayout.setShowingForActionMode(false);
647 }
648 updateVisibility(false);
649 }
650 }
651
652 public void hideForSystem() {
653 if (!mHiddenBySystem) {
654 mHiddenBySystem = true;
655 updateVisibility(true);
656 }
657 }
658
659 private static boolean checkShowingFlags(boolean hiddenByApp, boolean hiddenBySystem,
660 boolean showingForMode) {
661 if (showingForMode) {
662 return true;
663 } else if (hiddenByApp || hiddenBySystem) {
664 return false;
665 } else {
666 return true;
667 }
668 }
669
670 private void updateVisibility(boolean fromSystem) {
671 // Based on the current state, should we be hidden or shown?
672 final boolean shown = checkShowingFlags(mHiddenByApp, mHiddenBySystem,
673 mShowingForMode);
674
675 if (shown) {
676 if (!mNowShowing) {
677 mNowShowing = true;
678 doShow(fromSystem);
679 }
680 } else {
681 if (mNowShowing) {
682 mNowShowing = false;
683 doHide(fromSystem);
684 }
685 }
686 }
687
688 public void doShow(boolean fromSystem) {
Adam Powell07e1f982011-03-24 11:11:15 -0700689 if (mCurrentShowAnim != null) {
690 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800691 }
Adam Powell9b0dc282013-07-31 13:58:43 -0700692 mContainerView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800693
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700694 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700695 || fromSystem)) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700696 mContainerView.setTranslationY(0); // because we're about to ask its window loc
697 float startingY = -mContainerView.getHeight();
Chet Haasee8118e12012-05-30 14:19:02 -0700698 if (fromSystem) {
699 int topLeft[] = {0, 0};
Adam Powell9b0dc282013-07-31 13:58:43 -0700700 mContainerView.getLocationInWindow(topLeft);
Chet Haasee8118e12012-05-30 14:19:02 -0700701 startingY -= topLeft[1];
702 }
Adam Powell9b0dc282013-07-31 13:58:43 -0700703 mContainerView.setTranslationY(startingY);
Adam Powell50efbed2011-02-08 16:20:15 -0800704 AnimatorSet anim = new AnimatorSet();
Adam Powell9b0dc282013-07-31 13:58:43 -0700705 ObjectAnimator a = ObjectAnimator.ofFloat(mContainerView, View.TRANSLATION_Y, 0);
706 a.addUpdateListener(mUpdateListener);
707 AnimatorSet.Builder b = anim.play(a);
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800708 if (mContentAnimations && mContentView != null) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700709 b.with(ObjectAnimator.ofFloat(mContentView, View.TRANSLATION_Y,
Chet Haasee8118e12012-05-30 14:19:02 -0700710 startingY, 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800711 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700712 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700713 mSplitView.setTranslationY(mSplitView.getHeight());
Adam Powell993a63a2011-08-18 16:35:53 -0700714 mSplitView.setVisibility(View.VISIBLE);
Adam Powell9b0dc282013-07-31 13:58:43 -0700715 b.with(ObjectAnimator.ofFloat(mSplitView, View.TRANSLATION_Y, 0));
Adam Powell640a66e2011-04-29 10:18:53 -0700716 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700717 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
Chet Haasee8118e12012-05-30 14:19:02 -0700718 com.android.internal.R.interpolator.decelerate_cubic));
719 anim.setDuration(250);
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700720 // If this is being shown from the system, add a small delay.
721 // This is because we will also be animating in the status bar,
722 // and these two elements can't be done in lock-step. So we give
723 // a little time for the status bar to start its animation before
724 // the action bar animates. (This corresponds to the corresponding
725 // case when hiding, where the status bar has a small delay before
726 // starting.)
Adam Powell50efbed2011-02-08 16:20:15 -0800727 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700728 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800729 anim.start();
730 } else {
Adam Powell9b0dc282013-07-31 13:58:43 -0700731 mContainerView.setAlpha(1);
732 mContainerView.setTranslationY(0);
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800733 if (mContentAnimations && mContentView != null) {
Dianne Hackborn80d55062012-05-22 18:03:20 -0700734 mContentView.setTranslationY(0);
735 }
736 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
737 mSplitView.setAlpha(1);
738 mSplitView.setTranslationY(0);
739 mSplitView.setVisibility(View.VISIBLE);
740 }
Adam Powell50efbed2011-02-08 16:20:15 -0800741 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800742 }
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700743 if (mOverlayLayout != null) {
744 mOverlayLayout.requestFitSystemWindows();
745 }
Adam Powell6b336f82010-08-10 20:13:01 -0700746 }
747
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700748 public void doHide(boolean fromSystem) {
Adam Powell07e1f982011-03-24 11:11:15 -0700749 if (mCurrentShowAnim != null) {
750 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800751 }
Adam Powell50efbed2011-02-08 16:20:15 -0800752
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700753 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700754 || fromSystem)) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700755 mContainerView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800756 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800757 AnimatorSet anim = new AnimatorSet();
Adam Powell9b0dc282013-07-31 13:58:43 -0700758 float endingY = -mContainerView.getHeight();
Chet Haasee8118e12012-05-30 14:19:02 -0700759 if (fromSystem) {
760 int topLeft[] = {0, 0};
Adam Powell9b0dc282013-07-31 13:58:43 -0700761 mContainerView.getLocationInWindow(topLeft);
Chet Haasee8118e12012-05-30 14:19:02 -0700762 endingY -= topLeft[1];
763 }
Adam Powell9b0dc282013-07-31 13:58:43 -0700764 ObjectAnimator a = ObjectAnimator.ofFloat(mContainerView, View.TRANSLATION_Y, endingY);
765 a.addUpdateListener(mUpdateListener);
766 AnimatorSet.Builder b = anim.play(a);
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800767 if (mContentAnimations && mContentView != null) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700768 b.with(ObjectAnimator.ofFloat(mContentView, View.TRANSLATION_Y,
Chet Haasee8118e12012-05-30 14:19:02 -0700769 0, endingY));
Adam Powell50efbed2011-02-08 16:20:15 -0800770 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700771 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700772 mSplitView.setAlpha(1);
Adam Powell9b0dc282013-07-31 13:58:43 -0700773 b.with(ObjectAnimator.ofFloat(mSplitView, View.TRANSLATION_Y,
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700774 mSplitView.getHeight()));
Adam Powell640a66e2011-04-29 10:18:53 -0700775 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700776 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
Chet Haasee8118e12012-05-30 14:19:02 -0700777 com.android.internal.R.interpolator.accelerate_cubic));
778 anim.setDuration(250);
Adam Powell50efbed2011-02-08 16:20:15 -0800779 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700780 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800781 anim.start();
782 } else {
783 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800784 }
Adam Powell6b336f82010-08-10 20:13:01 -0700785 }
786
787 public boolean isShowing() {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700788 return mNowShowing;
789 }
790
791 public boolean isSystemShowing() {
792 return !mHiddenBySystem;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800793 }
794
Adam Powell640a66e2011-04-29 10:18:53 -0700795 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700796 if (toActionMode) {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700797 showForActionMode();
798 } else {
799 hideForActionMode();
Adam Powell07e1f982011-03-24 11:11:15 -0700800 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800801
Adam Powell640a66e2011-04-29 10:18:53 -0700802 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
803 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700804 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
805 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
806 }
Adam Powell6b336f82010-08-10 20:13:01 -0700807 }
808
Adam Powell88ab6972011-07-28 11:25:01 -0700809 public Context getThemedContext() {
810 if (mThemedContext == null) {
811 TypedValue outValue = new TypedValue();
812 Resources.Theme currentTheme = mContext.getTheme();
813 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
814 outValue, true);
815 final int targetThemeRes = outValue.resourceId;
816
817 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
818 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
819 } else {
820 mThemedContext = mContext;
821 }
822 }
823 return mThemedContext;
824 }
825
Adam Powell27cba382013-01-22 18:55:20 -0800826 @Override
827 public boolean isTitleTruncated() {
828 return mActionView != null && mActionView.isTitleTruncated();
829 }
830
Adam Powelle0e2f4f2013-04-05 16:27:35 -0700831 @Override
832 public void setHomeAsUpIndicator(Drawable indicator) {
833 mActionView.setHomeAsUpIndicator(indicator);
834 }
835
836 @Override
837 public void setHomeAsUpIndicator(int resId) {
838 mActionView.setHomeAsUpIndicator(resId);
839 }
840
841 @Override
842 public void setHomeActionContentDescription(CharSequence description) {
843 mActionView.setHomeActionContentDescription(description);
844 }
845
846 @Override
847 public void setHomeActionContentDescription(int resId) {
848 mActionView.setHomeActionContentDescription(resId);
849 }
850
Adam Powell89e06452010-06-23 20:24:52 -0700851 /**
852 * @hide
853 */
Adam Powell5d279772010-07-27 16:34:07 -0700854 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700855 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700856 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700857 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700858
Adam Powell5d279772010-07-27 16:34:07 -0700859 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700860 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700861 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700862 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700863 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700864 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700865
866 @Override
867 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700868 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700869 }
870
Adam Powell89e06452010-06-23 20:24:52 -0700871 @Override
872 public Menu getMenu() {
873 return mMenu;
874 }
875
876 @Override
877 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700878 if (mActionMode != this) {
879 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700880 return;
881 }
882
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700883 // If this change in state is going to cause the action bar
884 // to be hidden, defer the onDestroy callback until the animation
885 // is finished and associated relayout is about to happen. This lets
886 // apps better anticipate visibility and layout behavior.
887 if (!checkShowingFlags(mHiddenByApp, mHiddenBySystem, false)) {
888 // With the current state but the action bar hidden, our
889 // overall showing state is going to be false.
Adam Powell060e3ca2011-07-19 20:39:16 -0700890 mDeferredDestroyActionMode = this;
891 mDeferredModeDestroyCallback = mCallback;
892 } else {
893 mCallback.onDestroyActionMode(this);
894 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800895 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700896 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700897
898 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700899 mContextView.closeMode();
Adam Powell86ed4362011-09-14 16:18:53 -0700900 mActionView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700901
Adam Powell5d279772010-07-27 16:34:07 -0700902 mActionMode = null;
Adam Powell89e06452010-06-23 20:24:52 -0700903 }
904
905 @Override
906 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700907 mMenu.stopDispatchingItemsChanged();
908 try {
909 mCallback.onPrepareActionMode(this, mMenu);
910 } finally {
911 mMenu.startDispatchingItemsChanged();
912 }
913 }
914
915 public boolean dispatchOnCreate() {
916 mMenu.stopDispatchingItemsChanged();
917 try {
918 return mCallback.onCreateActionMode(this, mMenu);
919 } finally {
920 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700921 }
922 }
923
924 @Override
925 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700926 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700927 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700928 }
929
930 @Override
931 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700932 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700933 }
934
935 @Override
936 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700937 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700938 }
Adam Powell29ed7572010-07-14 16:24:56 -0700939
940 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700941 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800942 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700943 }
944
945 @Override
946 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800947 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700948 }
949
950 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700951 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700952 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700953 }
954
955 @Override
956 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700957 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700958 }
Adam Powell89e06452010-06-23 20:24:52 -0700959
Adam Powell29ed7572010-07-14 16:24:56 -0700960 @Override
Adam Powellb98a81f2012-02-24 11:09:07 -0800961 public void setTitleOptionalHint(boolean titleOptional) {
Adam Powell785c4472012-05-02 21:25:39 -0700962 super.setTitleOptionalHint(titleOptional);
Adam Powellb98a81f2012-02-24 11:09:07 -0800963 mContextView.setTitleOptional(titleOptional);
964 }
965
966 @Override
967 public boolean isTitleOptional() {
968 return mContextView.isTitleOptional();
969 }
970
971 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700972 public View getCustomView() {
973 return mCustomView != null ? mCustomView.get() : null;
974 }
975
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700976 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800977 if (mCallback != null) {
978 return mCallback.onActionItemClicked(this, item);
979 } else {
980 return false;
981 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700982 }
983
984 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
985 }
986
987 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800988 if (mCallback == null) {
989 return false;
990 }
991
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700992 if (!subMenu.hasVisibleItems()) {
993 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700994 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700995
Adam Powellf2423682011-08-11 14:29:45 -0700996 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700997 return true;
998 }
999
1000 public void onCloseSubMenu(SubMenuBuilder menu) {
1001 }
1002
1003 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -08001004 if (mCallback == null) {
1005 return;
1006 }
Adam Powellf6148c52010-08-11 21:10:16 -07001007 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -07001008 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -07001009 }
Adam Powell661c9082010-07-02 10:09:44 -07001010 }
1011
1012 /**
1013 * @hide
1014 */
1015 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -07001016 private ActionBar.TabListener mCallback;
1017 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -07001018 private Drawable mIcon;
1019 private CharSequence mText;
Adam Powell94e56ef2011-09-06 21:22:22 -07001020 private CharSequence mContentDesc;
Adam Powell0d8ec1d2011-05-03 14:49:13 -07001021 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -07001022 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -07001023
1024 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -07001025 public Object getTag() {
1026 return mTag;
1027 }
1028
1029 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001030 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -07001031 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -07001032 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -07001033 }
1034
1035 public ActionBar.TabListener getCallback() {
1036 return mCallback;
1037 }
1038
1039 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001040 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -07001041 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -07001042 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -07001043 }
1044
1045 @Override
1046 public View getCustomView() {
1047 return mCustomView;
1048 }
1049
1050 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001051 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -07001052 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001053 if (mPosition >= 0) {
1054 mTabScrollView.updateTab(mPosition);
1055 }
Adam Powell9ab97872010-10-26 21:47:29 -07001056 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001057 }
1058
1059 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001060 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -07001061 return setCustomView(LayoutInflater.from(getThemedContext())
1062 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -08001063 }
1064
1065 @Override
Adam Powell661c9082010-07-02 10:09:44 -07001066 public Drawable getIcon() {
1067 return mIcon;
1068 }
1069
1070 @Override
1071 public int getPosition() {
1072 return mPosition;
1073 }
1074
1075 public void setPosition(int position) {
1076 mPosition = position;
1077 }
1078
1079 @Override
1080 public CharSequence getText() {
1081 return mText;
1082 }
1083
1084 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001085 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -07001086 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001087 if (mPosition >= 0) {
1088 mTabScrollView.updateTab(mPosition);
1089 }
Adam Powell9ab97872010-10-26 21:47:29 -07001090 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001091 }
1092
1093 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001094 public Tab setIcon(int resId) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -08001095 return setIcon(mContext.getDrawable(resId));
Adam Powell32555f32010-11-17 13:49:22 -08001096 }
1097
1098 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001099 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -07001100 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001101 if (mPosition >= 0) {
1102 mTabScrollView.updateTab(mPosition);
1103 }
Adam Powell9ab97872010-10-26 21:47:29 -07001104 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001105 }
1106
1107 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001108 public Tab setText(int resId) {
1109 return setText(mContext.getResources().getText(resId));
1110 }
1111
1112 @Override
Adam Powell661c9082010-07-02 10:09:44 -07001113 public void select() {
1114 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -07001115 }
Adam Powell94e56ef2011-09-06 21:22:22 -07001116
1117 @Override
1118 public Tab setContentDescription(int resId) {
1119 return setContentDescription(mContext.getResources().getText(resId));
1120 }
1121
1122 @Override
1123 public Tab setContentDescription(CharSequence contentDesc) {
1124 mContentDesc = contentDesc;
1125 if (mPosition >= 0) {
1126 mTabScrollView.updateTab(mPosition);
1127 }
1128 return this;
1129 }
1130
1131 @Override
1132 public CharSequence getContentDescription() {
1133 return mContentDesc;
1134 }
Adam Powell89e06452010-06-23 20:24:52 -07001135 }
Adam Powell9ab97872010-10-26 21:47:29 -07001136
1137 @Override
1138 public void setCustomView(View view) {
1139 mActionView.setCustomNavigationView(view);
1140 }
1141
1142 @Override
1143 public void setCustomView(View view, LayoutParams layoutParams) {
1144 view.setLayoutParams(layoutParams);
1145 mActionView.setCustomNavigationView(view);
1146 }
1147
1148 @Override
Adam Powell8515ee82010-11-30 14:09:55 -08001149 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -07001150 mActionView.setDropdownAdapter(adapter);
1151 mActionView.setCallback(callback);
1152 }
1153
1154 @Override
1155 public int getSelectedNavigationIndex() {
1156 switch (mActionView.getNavigationMode()) {
1157 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -08001158 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -07001159 case NAVIGATION_MODE_LIST:
1160 return mActionView.getDropdownSelectedPosition();
1161 default:
1162 return -1;
1163 }
1164 }
1165
1166 @Override
1167 public int getNavigationItemCount() {
1168 switch (mActionView.getNavigationMode()) {
1169 case NAVIGATION_MODE_TABS:
1170 return mTabs.size();
1171 case NAVIGATION_MODE_LIST:
1172 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
1173 return adapter != null ? adapter.getCount() : 0;
1174 default:
1175 return 0;
1176 }
1177 }
1178
1179 @Override
Adam Powell0c24a552010-11-03 16:44:11 -07001180 public int getTabCount() {
1181 return mTabs.size();
1182 }
1183
1184 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001185 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -07001186 final int oldMode = mActionView.getNavigationMode();
1187 switch (oldMode) {
1188 case NAVIGATION_MODE_TABS:
1189 mSavedTabPosition = getSelectedNavigationIndex();
1190 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -07001191 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -07001192 break;
1193 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001194 if (oldMode != mode && !mHasEmbeddedTabs) {
1195 if (mOverlayLayout != null) {
1196 mOverlayLayout.requestFitSystemWindows();
1197 }
1198 }
Adam Powell9ab97872010-10-26 21:47:29 -07001199 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -07001200 switch (mode) {
1201 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -07001202 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -07001203 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -07001204 if (mSavedTabPosition != INVALID_POSITION) {
1205 setSelectedNavigationItem(mSavedTabPosition);
1206 mSavedTabPosition = INVALID_POSITION;
1207 }
1208 break;
1209 }
Adam Powelld21aa122011-05-27 13:09:52 -07001210 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -07001211 }
1212
1213 @Override
1214 public Tab getTabAt(int index) {
1215 return mTabs.get(index);
1216 }
Adam Powell0c24a552010-11-03 16:44:11 -07001217
Adam Powell0c24a552010-11-03 16:44:11 -07001218
Adam Powell1969b872011-03-22 11:52:48 -07001219 @Override
1220 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001221 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001222 }
Adam Powell0c24a552010-11-03 16:44:11 -07001223
Adam Powell1969b872011-03-22 11:52:48 -07001224 @Override
1225 public void setIcon(Drawable icon) {
1226 mActionView.setIcon(icon);
1227 }
1228
Adam Powell04fe6eb2013-05-31 14:39:48 -07001229 public boolean hasIcon() {
1230 return mActionView.hasIcon();
1231 }
1232
Adam Powell1969b872011-03-22 11:52:48 -07001233 @Override
1234 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001235 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001236 }
1237
1238 @Override
1239 public void setLogo(Drawable logo) {
1240 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -07001241 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001242
Adam Powell04fe6eb2013-05-31 14:39:48 -07001243 public boolean hasLogo() {
1244 return mActionView.hasLogo();
1245 }
1246
Adam Powelldd8fab22012-03-22 17:47:27 -07001247 public void setDefaultDisplayHomeAsUpEnabled(boolean enable) {
1248 if (!mDisplayHomeAsUpSet) {
1249 setDisplayHomeAsUpEnabled(enable);
1250 }
1251 }
Adam Powell89e06452010-06-23 20:24:52 -07001252}