blob: 131f82814aa027a2f03c4dce720cc83298270a36 [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
Adam Powell17809772010-07-21 13:25:11 -0700365 public void setSelectedNavigationItem(int position) {
366 switch (mActionView.getNavigationMode()) {
367 case NAVIGATION_MODE_TABS:
368 selectTab(mTabs.get(position));
369 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700370 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700371 mActionView.setDropdownSelectedPosition(position);
372 break;
373 default:
374 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700375 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700376 }
377 }
378
Adam Powell9ab97872010-10-26 21:47:29 -0700379 public void removeAllTabs() {
380 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700381 }
382
Adam Powell661c9082010-07-02 10:09:44 -0700383 private void cleanupTabs() {
384 if (mSelectedTab != null) {
385 selectTab(null);
386 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700387 mTabs.clear();
Adam Powelld21aa122011-05-27 13:09:52 -0700388 if (mTabScrollView != null) {
389 mTabScrollView.removeAllTabs();
390 }
Adam Powell0c24a552010-11-03 16:44:11 -0700391 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700392 }
393
Adam Powell0e94b512010-06-29 17:58:20 -0700394 public void setTitle(CharSequence title) {
395 mActionView.setTitle(title);
396 }
397
398 public void setSubtitle(CharSequence subtitle) {
399 mActionView.setSubtitle(subtitle);
400 }
401
Adam Powell89e06452010-06-23 20:24:52 -0700402 public void setDisplayOptions(int options) {
Adam Powelldd8fab22012-03-22 17:47:27 -0700403 if ((options & DISPLAY_HOME_AS_UP) != 0) {
404 mDisplayHomeAsUpSet = true;
405 }
Adam Powell89e06452010-06-23 20:24:52 -0700406 mActionView.setDisplayOptions(options);
407 }
Adam Powell0e94b512010-06-29 17:58:20 -0700408
Adam Powell89e06452010-06-23 20:24:52 -0700409 public void setDisplayOptions(int options, int mask) {
410 final int current = mActionView.getDisplayOptions();
Adam Powelldd8fab22012-03-22 17:47:27 -0700411 if ((mask & DISPLAY_HOME_AS_UP) != 0) {
412 mDisplayHomeAsUpSet = true;
413 }
Adam Powell89e06452010-06-23 20:24:52 -0700414 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
415 }
416
417 public void setBackgroundDrawable(Drawable d) {
Adam Powellf88b9152011-09-07 14:54:32 -0700418 mContainerView.setPrimaryBackground(d);
419 }
420
421 public void setStackedBackgroundDrawable(Drawable d) {
422 mContainerView.setStackedBackground(d);
423 }
424
425 public void setSplitBackgroundDrawable(Drawable d) {
426 if (mSplitView != null) {
427 mSplitView.setSplitBackground(d);
428 }
Adam Powell89e06452010-06-23 20:24:52 -0700429 }
430
Adam Powellef704442010-11-16 14:48:22 -0800431 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700432 return mActionView.getCustomNavigationView();
433 }
434
435 public CharSequence getTitle() {
436 return mActionView.getTitle();
437 }
438
439 public CharSequence getSubtitle() {
440 return mActionView.getSubtitle();
441 }
442
443 public int getNavigationMode() {
444 return mActionView.getNavigationMode();
445 }
446
447 public int getDisplayOptions() {
448 return mActionView.getDisplayOptions();
449 }
450
Adam Powell5d279772010-07-27 16:34:07 -0700451 public ActionMode startActionMode(ActionMode.Callback callback) {
452 if (mActionMode != null) {
453 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700454 }
Adam Powell3461b322010-07-14 11:34:43 -0700455
Adam Powell640a66e2011-04-29 10:18:53 -0700456 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700457 ActionModeImpl mode = new ActionModeImpl(callback);
458 if (mode.dispatchOnCreate()) {
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700459 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700460 mContextView.initForMode(mode);
461 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700462 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700463 // TODO animate this
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700464 if (mSplitView.getVisibility() != View.VISIBLE) {
465 mSplitView.setVisibility(View.VISIBLE);
466 if (mOverlayLayout != null) {
467 mOverlayLayout.requestFitSystemWindows();
468 }
469 }
Adam Powell89e06452010-06-23 20:24:52 -0700470 }
Adam Powell86ed4362011-09-14 16:18:53 -0700471 mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powell5d279772010-07-27 16:34:07 -0700472 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700473 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700474 }
Adam Powellac695c62010-07-20 18:19:27 -0700475 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700476 }
477
Adam Powell661c9082010-07-02 10:09:44 -0700478 private void configureTab(Tab tab, int position) {
479 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700480 final ActionBar.TabListener callback = tabi.getCallback();
481
482 if (callback == null) {
483 throw new IllegalStateException("Action Bar Tab must have a Callback");
484 }
Adam Powell661c9082010-07-02 10:09:44 -0700485
486 tabi.setPosition(position);
487 mTabs.add(position, tabi);
488
Adam Powell81b89442010-11-02 17:58:56 -0700489 final int count = mTabs.size();
490 for (int i = position + 1; i < count; i++) {
491 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700492 }
Adam Powell661c9082010-07-02 10:09:44 -0700493 }
494
495 @Override
496 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700497 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700498 }
499
500 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700501 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700502 addTab(tab, position, mTabs.isEmpty());
503 }
504
505 @Override
506 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700507 ensureTabsExist();
508 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700509 configureTab(tab, mTabs.size());
510 if (setSelected) {
511 selectTab(tab);
512 }
513 }
514
515 @Override
516 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700517 ensureTabsExist();
518 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700519 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700520 if (setSelected) {
521 selectTab(tab);
522 }
Adam Powell661c9082010-07-02 10:09:44 -0700523 }
524
525 @Override
526 public Tab newTab() {
527 return new TabImpl();
528 }
529
530 @Override
531 public void removeTab(Tab tab) {
532 removeTabAt(tab.getPosition());
533 }
534
535 @Override
536 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700537 if (mTabScrollView == null) {
538 // No tabs around to remove
539 return;
540 }
541
Adam Powell0c24a552010-11-03 16:44:11 -0700542 int selectedTabPosition = mSelectedTab != null
543 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700544 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700545 TabImpl removedTab = mTabs.remove(position);
546 if (removedTab != null) {
547 removedTab.setPosition(-1);
548 }
Adam Powell661c9082010-07-02 10:09:44 -0700549
550 final int newTabCount = mTabs.size();
551 for (int i = position; i < newTabCount; i++) {
552 mTabs.get(i).setPosition(i);
553 }
554
Adam Powell0c24a552010-11-03 16:44:11 -0700555 if (selectedTabPosition == position) {
556 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
557 }
Adam Powell661c9082010-07-02 10:09:44 -0700558 }
559
560 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700561 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700562 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
563 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
564 return;
565 }
566
Deepanshu Gupta14bf0ce2013-12-12 12:16:24 -0800567 final FragmentTransaction trans = mActionView.isInEditMode() ? null :
568 mActivity.getFragmentManager().beginTransaction().disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700569
570 if (mSelectedTab == tab) {
571 if (mSelectedTab != null) {
572 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700573 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700574 }
575 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700576 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700577 if (mSelectedTab != null) {
578 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
579 }
580 mSelectedTab = (TabImpl) tab;
581 if (mSelectedTab != null) {
582 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
583 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700584 }
585
Deepanshu Gupta14bf0ce2013-12-12 12:16:24 -0800586 if (trans != null && !trans.isEmpty()) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700587 trans.commit();
588 }
589 }
590
591 @Override
592 public Tab getSelectedTab() {
593 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700594 }
595
Adam Powell6b336f82010-08-10 20:13:01 -0700596 @Override
597 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700598 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700599 }
600
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800601 public void enableContentAnimations(boolean enabled) {
602 mContentAnimations = enabled;
603 }
604
Adam Powell6b336f82010-08-10 20:13:01 -0700605 @Override
606 public void show() {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700607 if (mHiddenByApp) {
608 mHiddenByApp = false;
609 updateVisibility(false);
610 }
Adam Powell07e1f982011-03-24 11:11:15 -0700611 }
612
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700613 private void showForActionMode() {
614 if (!mShowingForMode) {
615 mShowingForMode = true;
616 if (mOverlayLayout != null) {
617 mOverlayLayout.setShowingForActionMode(true);
618 }
619 updateVisibility(false);
620 }
621 }
622
623 public void showForSystem() {
624 if (mHiddenBySystem) {
625 mHiddenBySystem = false;
626 updateVisibility(true);
627 }
628 }
629
630 @Override
631 public void hide() {
632 if (!mHiddenByApp) {
633 mHiddenByApp = true;
634 updateVisibility(false);
635 }
636 }
637
638 private void hideForActionMode() {
639 if (mShowingForMode) {
640 mShowingForMode = false;
641 if (mOverlayLayout != null) {
642 mOverlayLayout.setShowingForActionMode(false);
643 }
644 updateVisibility(false);
645 }
646 }
647
648 public void hideForSystem() {
649 if (!mHiddenBySystem) {
650 mHiddenBySystem = true;
651 updateVisibility(true);
652 }
653 }
654
655 private static boolean checkShowingFlags(boolean hiddenByApp, boolean hiddenBySystem,
656 boolean showingForMode) {
657 if (showingForMode) {
658 return true;
659 } else if (hiddenByApp || hiddenBySystem) {
660 return false;
661 } else {
662 return true;
663 }
664 }
665
666 private void updateVisibility(boolean fromSystem) {
667 // Based on the current state, should we be hidden or shown?
668 final boolean shown = checkShowingFlags(mHiddenByApp, mHiddenBySystem,
669 mShowingForMode);
670
671 if (shown) {
672 if (!mNowShowing) {
673 mNowShowing = true;
674 doShow(fromSystem);
675 }
676 } else {
677 if (mNowShowing) {
678 mNowShowing = false;
679 doHide(fromSystem);
680 }
681 }
682 }
683
684 public void doShow(boolean fromSystem) {
Adam Powell07e1f982011-03-24 11:11:15 -0700685 if (mCurrentShowAnim != null) {
686 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800687 }
Adam Powell9b0dc282013-07-31 13:58:43 -0700688 mContainerView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800689
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700690 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700691 || fromSystem)) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700692 mContainerView.setTranslationY(0); // because we're about to ask its window loc
693 float startingY = -mContainerView.getHeight();
Chet Haasee8118e12012-05-30 14:19:02 -0700694 if (fromSystem) {
695 int topLeft[] = {0, 0};
Adam Powell9b0dc282013-07-31 13:58:43 -0700696 mContainerView.getLocationInWindow(topLeft);
Chet Haasee8118e12012-05-30 14:19:02 -0700697 startingY -= topLeft[1];
698 }
Adam Powell9b0dc282013-07-31 13:58:43 -0700699 mContainerView.setTranslationY(startingY);
Adam Powell50efbed2011-02-08 16:20:15 -0800700 AnimatorSet anim = new AnimatorSet();
Adam Powell9b0dc282013-07-31 13:58:43 -0700701 ObjectAnimator a = ObjectAnimator.ofFloat(mContainerView, View.TRANSLATION_Y, 0);
702 a.addUpdateListener(mUpdateListener);
703 AnimatorSet.Builder b = anim.play(a);
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800704 if (mContentAnimations && mContentView != null) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700705 b.with(ObjectAnimator.ofFloat(mContentView, View.TRANSLATION_Y,
Chet Haasee8118e12012-05-30 14:19:02 -0700706 startingY, 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800707 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700708 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700709 mSplitView.setTranslationY(mSplitView.getHeight());
Adam Powell993a63a2011-08-18 16:35:53 -0700710 mSplitView.setVisibility(View.VISIBLE);
Adam Powell9b0dc282013-07-31 13:58:43 -0700711 b.with(ObjectAnimator.ofFloat(mSplitView, View.TRANSLATION_Y, 0));
Adam Powell640a66e2011-04-29 10:18:53 -0700712 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700713 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
Chet Haasee8118e12012-05-30 14:19:02 -0700714 com.android.internal.R.interpolator.decelerate_cubic));
715 anim.setDuration(250);
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700716 // If this is being shown from the system, add a small delay.
717 // This is because we will also be animating in the status bar,
718 // and these two elements can't be done in lock-step. So we give
719 // a little time for the status bar to start its animation before
720 // the action bar animates. (This corresponds to the corresponding
721 // case when hiding, where the status bar has a small delay before
722 // starting.)
Adam Powell50efbed2011-02-08 16:20:15 -0800723 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700724 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800725 anim.start();
726 } else {
Adam Powell9b0dc282013-07-31 13:58:43 -0700727 mContainerView.setAlpha(1);
728 mContainerView.setTranslationY(0);
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800729 if (mContentAnimations && mContentView != null) {
Dianne Hackborn80d55062012-05-22 18:03:20 -0700730 mContentView.setTranslationY(0);
731 }
732 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
733 mSplitView.setAlpha(1);
734 mSplitView.setTranslationY(0);
735 mSplitView.setVisibility(View.VISIBLE);
736 }
Adam Powell50efbed2011-02-08 16:20:15 -0800737 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800738 }
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700739 if (mOverlayLayout != null) {
740 mOverlayLayout.requestFitSystemWindows();
741 }
Adam Powell6b336f82010-08-10 20:13:01 -0700742 }
743
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700744 public void doHide(boolean fromSystem) {
Adam Powell07e1f982011-03-24 11:11:15 -0700745 if (mCurrentShowAnim != null) {
746 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800747 }
Adam Powell50efbed2011-02-08 16:20:15 -0800748
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700749 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700750 || fromSystem)) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700751 mContainerView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800752 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800753 AnimatorSet anim = new AnimatorSet();
Adam Powell9b0dc282013-07-31 13:58:43 -0700754 float endingY = -mContainerView.getHeight();
Chet Haasee8118e12012-05-30 14:19:02 -0700755 if (fromSystem) {
756 int topLeft[] = {0, 0};
Adam Powell9b0dc282013-07-31 13:58:43 -0700757 mContainerView.getLocationInWindow(topLeft);
Chet Haasee8118e12012-05-30 14:19:02 -0700758 endingY -= topLeft[1];
759 }
Adam Powell9b0dc282013-07-31 13:58:43 -0700760 ObjectAnimator a = ObjectAnimator.ofFloat(mContainerView, View.TRANSLATION_Y, endingY);
761 a.addUpdateListener(mUpdateListener);
762 AnimatorSet.Builder b = anim.play(a);
Dianne Hackborndf7221c2013-02-26 14:53:55 -0800763 if (mContentAnimations && mContentView != null) {
Adam Powell9b0dc282013-07-31 13:58:43 -0700764 b.with(ObjectAnimator.ofFloat(mContentView, View.TRANSLATION_Y,
Chet Haasee8118e12012-05-30 14:19:02 -0700765 0, endingY));
Adam Powell50efbed2011-02-08 16:20:15 -0800766 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700767 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700768 mSplitView.setAlpha(1);
Adam Powell9b0dc282013-07-31 13:58:43 -0700769 b.with(ObjectAnimator.ofFloat(mSplitView, View.TRANSLATION_Y,
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700770 mSplitView.getHeight()));
Adam Powell640a66e2011-04-29 10:18:53 -0700771 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700772 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
Chet Haasee8118e12012-05-30 14:19:02 -0700773 com.android.internal.R.interpolator.accelerate_cubic));
774 anim.setDuration(250);
Adam Powell50efbed2011-02-08 16:20:15 -0800775 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700776 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800777 anim.start();
778 } else {
779 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800780 }
Adam Powell6b336f82010-08-10 20:13:01 -0700781 }
782
783 public boolean isShowing() {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700784 return mNowShowing;
785 }
786
787 public boolean isSystemShowing() {
788 return !mHiddenBySystem;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800789 }
790
Adam Powell640a66e2011-04-29 10:18:53 -0700791 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700792 if (toActionMode) {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700793 showForActionMode();
794 } else {
795 hideForActionMode();
Adam Powell07e1f982011-03-24 11:11:15 -0700796 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800797
Adam Powell640a66e2011-04-29 10:18:53 -0700798 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
799 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700800 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
801 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
802 }
Adam Powell6b336f82010-08-10 20:13:01 -0700803 }
804
Adam Powell88ab6972011-07-28 11:25:01 -0700805 public Context getThemedContext() {
806 if (mThemedContext == null) {
807 TypedValue outValue = new TypedValue();
808 Resources.Theme currentTheme = mContext.getTheme();
809 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
810 outValue, true);
811 final int targetThemeRes = outValue.resourceId;
812
813 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
814 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
815 } else {
816 mThemedContext = mContext;
817 }
818 }
819 return mThemedContext;
820 }
821
Adam Powell27cba382013-01-22 18:55:20 -0800822 @Override
823 public boolean isTitleTruncated() {
824 return mActionView != null && mActionView.isTitleTruncated();
825 }
826
Adam Powelle0e2f4f2013-04-05 16:27:35 -0700827 @Override
828 public void setHomeAsUpIndicator(Drawable indicator) {
829 mActionView.setHomeAsUpIndicator(indicator);
830 }
831
832 @Override
833 public void setHomeAsUpIndicator(int resId) {
834 mActionView.setHomeAsUpIndicator(resId);
835 }
836
837 @Override
838 public void setHomeActionContentDescription(CharSequence description) {
839 mActionView.setHomeActionContentDescription(description);
840 }
841
842 @Override
843 public void setHomeActionContentDescription(int resId) {
844 mActionView.setHomeActionContentDescription(resId);
845 }
846
Adam Powell89e06452010-06-23 20:24:52 -0700847 /**
848 * @hide
849 */
Adam Powell5d279772010-07-27 16:34:07 -0700850 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700851 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700852 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700853 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700854
Adam Powell5d279772010-07-27 16:34:07 -0700855 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700856 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700857 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700858 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700859 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700860 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700861
862 @Override
863 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700864 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700865 }
866
Adam Powell89e06452010-06-23 20:24:52 -0700867 @Override
868 public Menu getMenu() {
869 return mMenu;
870 }
871
872 @Override
873 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700874 if (mActionMode != this) {
875 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700876 return;
877 }
878
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700879 // If this change in state is going to cause the action bar
880 // to be hidden, defer the onDestroy callback until the animation
881 // is finished and associated relayout is about to happen. This lets
882 // apps better anticipate visibility and layout behavior.
883 if (!checkShowingFlags(mHiddenByApp, mHiddenBySystem, false)) {
884 // With the current state but the action bar hidden, our
885 // overall showing state is going to be false.
Adam Powell060e3ca2011-07-19 20:39:16 -0700886 mDeferredDestroyActionMode = this;
887 mDeferredModeDestroyCallback = mCallback;
888 } else {
889 mCallback.onDestroyActionMode(this);
890 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800891 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700892 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700893
894 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700895 mContextView.closeMode();
Adam Powell86ed4362011-09-14 16:18:53 -0700896 mActionView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700897
Adam Powell5d279772010-07-27 16:34:07 -0700898 mActionMode = null;
Adam Powell89e06452010-06-23 20:24:52 -0700899 }
900
901 @Override
902 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700903 mMenu.stopDispatchingItemsChanged();
904 try {
905 mCallback.onPrepareActionMode(this, mMenu);
906 } finally {
907 mMenu.startDispatchingItemsChanged();
908 }
909 }
910
911 public boolean dispatchOnCreate() {
912 mMenu.stopDispatchingItemsChanged();
913 try {
914 return mCallback.onCreateActionMode(this, mMenu);
915 } finally {
916 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700917 }
918 }
919
920 @Override
921 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700922 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700923 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700924 }
925
926 @Override
927 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700928 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700929 }
930
931 @Override
932 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700933 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700934 }
Adam Powell29ed7572010-07-14 16:24:56 -0700935
936 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700937 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800938 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700939 }
940
941 @Override
942 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800943 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700944 }
945
946 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700947 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700948 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700949 }
950
951 @Override
952 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700953 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700954 }
Adam Powell89e06452010-06-23 20:24:52 -0700955
Adam Powell29ed7572010-07-14 16:24:56 -0700956 @Override
Adam Powellb98a81f2012-02-24 11:09:07 -0800957 public void setTitleOptionalHint(boolean titleOptional) {
Adam Powell785c4472012-05-02 21:25:39 -0700958 super.setTitleOptionalHint(titleOptional);
Adam Powellb98a81f2012-02-24 11:09:07 -0800959 mContextView.setTitleOptional(titleOptional);
960 }
961
962 @Override
963 public boolean isTitleOptional() {
964 return mContextView.isTitleOptional();
965 }
966
967 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700968 public View getCustomView() {
969 return mCustomView != null ? mCustomView.get() : null;
970 }
971
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700972 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800973 if (mCallback != null) {
974 return mCallback.onActionItemClicked(this, item);
975 } else {
976 return false;
977 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700978 }
979
980 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
981 }
982
983 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800984 if (mCallback == null) {
985 return false;
986 }
987
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700988 if (!subMenu.hasVisibleItems()) {
989 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700990 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700991
Adam Powellf2423682011-08-11 14:29:45 -0700992 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700993 return true;
994 }
995
996 public void onCloseSubMenu(SubMenuBuilder menu) {
997 }
998
999 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -08001000 if (mCallback == null) {
1001 return;
1002 }
Adam Powellf6148c52010-08-11 21:10:16 -07001003 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -07001004 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -07001005 }
Adam Powell661c9082010-07-02 10:09:44 -07001006 }
1007
1008 /**
1009 * @hide
1010 */
1011 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -07001012 private ActionBar.TabListener mCallback;
1013 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -07001014 private Drawable mIcon;
1015 private CharSequence mText;
Adam Powell94e56ef2011-09-06 21:22:22 -07001016 private CharSequence mContentDesc;
Adam Powell0d8ec1d2011-05-03 14:49:13 -07001017 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -07001018 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -07001019
1020 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -07001021 public Object getTag() {
1022 return mTag;
1023 }
1024
1025 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001026 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -07001027 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -07001028 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -07001029 }
1030
1031 public ActionBar.TabListener getCallback() {
1032 return mCallback;
1033 }
1034
1035 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001036 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -07001037 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -07001038 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -07001039 }
1040
1041 @Override
1042 public View getCustomView() {
1043 return mCustomView;
1044 }
1045
1046 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001047 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -07001048 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001049 if (mPosition >= 0) {
1050 mTabScrollView.updateTab(mPosition);
1051 }
Adam Powell9ab97872010-10-26 21:47:29 -07001052 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001053 }
1054
1055 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001056 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -07001057 return setCustomView(LayoutInflater.from(getThemedContext())
1058 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -08001059 }
1060
1061 @Override
Adam Powell661c9082010-07-02 10:09:44 -07001062 public Drawable getIcon() {
1063 return mIcon;
1064 }
1065
1066 @Override
1067 public int getPosition() {
1068 return mPosition;
1069 }
1070
1071 public void setPosition(int position) {
1072 mPosition = position;
1073 }
1074
1075 @Override
1076 public CharSequence getText() {
1077 return mText;
1078 }
1079
1080 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001081 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -07001082 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001083 if (mPosition >= 0) {
1084 mTabScrollView.updateTab(mPosition);
1085 }
Adam Powell9ab97872010-10-26 21:47:29 -07001086 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001087 }
1088
1089 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001090 public Tab setIcon(int resId) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -08001091 return setIcon(mContext.getDrawable(resId));
Adam Powell32555f32010-11-17 13:49:22 -08001092 }
1093
1094 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001095 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -07001096 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001097 if (mPosition >= 0) {
1098 mTabScrollView.updateTab(mPosition);
1099 }
Adam Powell9ab97872010-10-26 21:47:29 -07001100 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001101 }
1102
1103 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001104 public Tab setText(int resId) {
1105 return setText(mContext.getResources().getText(resId));
1106 }
1107
1108 @Override
Adam Powell661c9082010-07-02 10:09:44 -07001109 public void select() {
1110 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -07001111 }
Adam Powell94e56ef2011-09-06 21:22:22 -07001112
1113 @Override
1114 public Tab setContentDescription(int resId) {
1115 return setContentDescription(mContext.getResources().getText(resId));
1116 }
1117
1118 @Override
1119 public Tab setContentDescription(CharSequence contentDesc) {
1120 mContentDesc = contentDesc;
1121 if (mPosition >= 0) {
1122 mTabScrollView.updateTab(mPosition);
1123 }
1124 return this;
1125 }
1126
1127 @Override
1128 public CharSequence getContentDescription() {
1129 return mContentDesc;
1130 }
Adam Powell89e06452010-06-23 20:24:52 -07001131 }
Adam Powell9ab97872010-10-26 21:47:29 -07001132
1133 @Override
1134 public void setCustomView(View view) {
1135 mActionView.setCustomNavigationView(view);
1136 }
1137
1138 @Override
1139 public void setCustomView(View view, LayoutParams layoutParams) {
1140 view.setLayoutParams(layoutParams);
1141 mActionView.setCustomNavigationView(view);
1142 }
1143
1144 @Override
Adam Powell8515ee82010-11-30 14:09:55 -08001145 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -07001146 mActionView.setDropdownAdapter(adapter);
1147 mActionView.setCallback(callback);
1148 }
1149
1150 @Override
1151 public int getSelectedNavigationIndex() {
1152 switch (mActionView.getNavigationMode()) {
1153 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -08001154 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -07001155 case NAVIGATION_MODE_LIST:
1156 return mActionView.getDropdownSelectedPosition();
1157 default:
1158 return -1;
1159 }
1160 }
1161
1162 @Override
1163 public int getNavigationItemCount() {
1164 switch (mActionView.getNavigationMode()) {
1165 case NAVIGATION_MODE_TABS:
1166 return mTabs.size();
1167 case NAVIGATION_MODE_LIST:
1168 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
1169 return adapter != null ? adapter.getCount() : 0;
1170 default:
1171 return 0;
1172 }
1173 }
1174
1175 @Override
Adam Powell0c24a552010-11-03 16:44:11 -07001176 public int getTabCount() {
1177 return mTabs.size();
1178 }
1179
1180 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001181 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -07001182 final int oldMode = mActionView.getNavigationMode();
1183 switch (oldMode) {
1184 case NAVIGATION_MODE_TABS:
1185 mSavedTabPosition = getSelectedNavigationIndex();
1186 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -07001187 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -07001188 break;
1189 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001190 if (oldMode != mode && !mHasEmbeddedTabs) {
1191 if (mOverlayLayout != null) {
1192 mOverlayLayout.requestFitSystemWindows();
1193 }
1194 }
Adam Powell9ab97872010-10-26 21:47:29 -07001195 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -07001196 switch (mode) {
1197 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -07001198 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -07001199 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -07001200 if (mSavedTabPosition != INVALID_POSITION) {
1201 setSelectedNavigationItem(mSavedTabPosition);
1202 mSavedTabPosition = INVALID_POSITION;
1203 }
1204 break;
1205 }
Adam Powelld21aa122011-05-27 13:09:52 -07001206 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -07001207 }
1208
1209 @Override
1210 public Tab getTabAt(int index) {
1211 return mTabs.get(index);
1212 }
Adam Powell0c24a552010-11-03 16:44:11 -07001213
Adam Powell0c24a552010-11-03 16:44:11 -07001214
Adam Powell1969b872011-03-22 11:52:48 -07001215 @Override
1216 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001217 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001218 }
Adam Powell0c24a552010-11-03 16:44:11 -07001219
Adam Powell1969b872011-03-22 11:52:48 -07001220 @Override
1221 public void setIcon(Drawable icon) {
1222 mActionView.setIcon(icon);
1223 }
1224
Adam Powell04fe6eb2013-05-31 14:39:48 -07001225 public boolean hasIcon() {
1226 return mActionView.hasIcon();
1227 }
1228
Adam Powell1969b872011-03-22 11:52:48 -07001229 @Override
1230 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001231 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001232 }
1233
1234 @Override
1235 public void setLogo(Drawable logo) {
1236 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -07001237 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001238
Adam Powell04fe6eb2013-05-31 14:39:48 -07001239 public boolean hasLogo() {
1240 return mActionView.hasLogo();
1241 }
1242
Adam Powelldd8fab22012-03-22 17:47:27 -07001243 public void setDefaultDisplayHomeAsUpEnabled(boolean enable) {
1244 if (!mDisplayHomeAsUpSet) {
1245 setDisplayHomeAsUpEnabled(enable);
1246 }
1247 }
Adam Powell89e06452010-06-23 20:24:52 -07001248}