blob: f041f0781768214ee426024e6f37ea13d43a3918 [file] [log] [blame]
Adam Powell89e06452010-06-23 20:24:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.app;
18
Adam Powellb8139af2012-04-19 13:52:46 -070019import com.android.internal.view.ActionBarPolicy;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070020import com.android.internal.view.menu.MenuBuilder;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070021import com.android.internal.view.menu.MenuPopupHelper;
22import com.android.internal.view.menu.SubMenuBuilder;
Adam Powell01feaee2011-02-10 15:05:05 -080023import com.android.internal.widget.ActionBarContainer;
Adam Powell89e06452010-06-23 20:24:52 -070024import com.android.internal.widget.ActionBarContextView;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070025import com.android.internal.widget.ActionBarOverlayLayout;
Adam Powell89e06452010-06-23 20:24:52 -070026import com.android.internal.widget.ActionBarView;
Adam Powellf8ac6b72011-05-23 18:14:09 -070027import com.android.internal.widget.ScrollingTabContainerView;
Adam Powell89e06452010-06-23 20:24:52 -070028
Adam Powelld8b3f2e2010-12-02 13:37:03 -080029import android.animation.Animator;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080030import android.animation.Animator.AnimatorListener;
Adam Powell07e1f982011-03-24 11:11:15 -070031import android.animation.AnimatorListenerAdapter;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080032import android.animation.AnimatorSet;
Adam Powelle6ec7322010-12-07 15:29:26 -080033import android.animation.ObjectAnimator;
Adam Powell89e06452010-06-23 20:24:52 -070034import android.app.ActionBar;
Adam Powell661c9082010-07-02 10:09:44 -070035import android.app.Activity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070036import android.app.Dialog;
Adam Powell661c9082010-07-02 10:09:44 -070037import android.app.FragmentTransaction;
Adam Powelldec9dfd2010-08-09 15:27:54 -070038import android.content.Context;
Adam Powellf8ac6b72011-05-23 18:14:09 -070039import android.content.res.Configuration;
Adam Powell88ab6972011-07-28 11:25:01 -070040import android.content.res.Resources;
Adam Powell89e06452010-06-23 20:24:52 -070041import android.graphics.drawable.Drawable;
Adam Powell0e94b512010-06-29 17:58:20 -070042import android.os.Handler;
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070043import android.util.Log;
Adam Powell88ab6972011-07-28 11:25:01 -070044import android.util.TypedValue;
Adam Powell6e346362010-07-23 10:18:23 -070045import android.view.ActionMode;
Adam Powell88ab6972011-07-28 11:25:01 -070046import android.view.ContextThemeWrapper;
Adam Powell32555f32010-11-17 13:49:22 -080047import android.view.LayoutInflater;
Adam Powell89e06452010-06-23 20:24:52 -070048import android.view.Menu;
Adam Powell9168f0b2010-08-02 15:46:24 -070049import android.view.MenuInflater;
Adam Powell89e06452010-06-23 20:24:52 -070050import android.view.MenuItem;
51import android.view.View;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070052import android.view.ViewGroup;
Adam Powelle6ec7322010-12-07 15:29:26 -080053import android.view.Window;
Adam Powell86ed4362011-09-14 16:18:53 -070054import android.view.accessibility.AccessibilityEvent;
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -070055import android.view.animation.AnimationUtils;
Adam Powell89e06452010-06-23 20:24:52 -070056import android.widget.SpinnerAdapter;
Adam Powell89e06452010-06-23 20:24:52 -070057
Adam Powell29ed7572010-07-14 16:24:56 -070058import java.lang.ref.WeakReference;
Adam Powell661c9082010-07-02 10:09:44 -070059import java.util.ArrayList;
60
Adam Powell89e06452010-06-23 20:24:52 -070061/**
62 * ActionBarImpl is the ActionBar implementation used
63 * by devices of all screen sizes. If it detects a compatible decor,
64 * it will split contextual modes across both the ActionBarView at
65 * the top of the screen and a horizontal LinearLayout at the bottom
66 * which is normally hidden.
67 */
68public class ActionBarImpl extends ActionBar {
Adam Powelld8145042011-03-24 14:18:27 -070069 private static final String TAG = "ActionBarImpl";
Adam Powell661c9082010-07-02 10:09:44 -070070
Adam Powelldec9dfd2010-08-09 15:27:54 -070071 private Context mContext;
Adam Powell88ab6972011-07-28 11:25:01 -070072 private Context mThemedContext;
Adam Powell661c9082010-07-02 10:09:44 -070073 private Activity mActivity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070074 private Dialog mDialog;
Adam Powell661c9082010-07-02 10:09:44 -070075
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070076 private ActionBarOverlayLayout mOverlayLayout;
Adam Powell01feaee2011-02-10 15:05:05 -080077 private ActionBarContainer mContainerView;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070078 private ViewGroup mTopVisibilityView;
Adam Powell89e06452010-06-23 20:24:52 -070079 private ActionBarView mActionView;
Adam Powell640a66e2011-04-29 10:18:53 -070080 private ActionBarContextView mContextView;
81 private ActionBarContainer mSplitView;
Adam Powelle6ec7322010-12-07 15:29:26 -080082 private View mContentView;
Adam Powellf8ac6b72011-05-23 18:14:09 -070083 private ScrollingTabContainerView mTabScrollView;
Adam Powell661c9082010-07-02 10:09:44 -070084
85 private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>();
86
Adam Powell661c9082010-07-02 10:09:44 -070087 private TabImpl mSelectedTab;
Adam Powell0c24a552010-11-03 16:44:11 -070088 private int mSavedTabPosition = INVALID_POSITION;
Adam Powell89e06452010-06-23 20:24:52 -070089
Adam Powelldd8fab22012-03-22 17:47:27 -070090 private boolean mDisplayHomeAsUpSet;
91
Adam Powell060e3ca2011-07-19 20:39:16 -070092 ActionModeImpl mActionMode;
93 ActionMode mDeferredDestroyActionMode;
94 ActionMode.Callback mDeferredModeDestroyCallback;
Adam Powell89e06452010-06-23 20:24:52 -070095
Adam Powell8515ee82010-11-30 14:09:55 -080096 private boolean mLastMenuVisibility;
97 private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
98 new ArrayList<OnMenuVisibilityListener>();
99
Adam Powell89e06452010-06-23 20:24:52 -0700100 private static final int CONTEXT_DISPLAY_NORMAL = 0;
101 private static final int CONTEXT_DISPLAY_SPLIT = 1;
102
Adam Powell0c24a552010-11-03 16:44:11 -0700103 private static final int INVALID_POSITION = -1;
104
Adam Powell89e06452010-06-23 20:24:52 -0700105 private int mContextDisplayMode;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700106 private boolean mHasEmbeddedTabs;
Adam Powell0e94b512010-06-29 17:58:20 -0700107
108 final Handler mHandler = new Handler();
Adam Powellf8ac6b72011-05-23 18:14:09 -0700109 Runnable mTabSelector;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800110
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700111 private int mCurWindowVisibility = View.VISIBLE;
112
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700113 private boolean mHiddenByApp;
114 private boolean mHiddenBySystem;
115 private boolean mShowingForMode;
116
117 private boolean mNowShowing = true;
118
Adam Powell07e1f982011-03-24 11:11:15 -0700119 private Animator mCurrentShowAnim;
Adam Powell50efbed2011-02-08 16:20:15 -0800120 private boolean mShowHideAnimationEnabled;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800121
Adam Powell07e1f982011-03-24 11:11:15 -0700122 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800123 @Override
124 public void onAnimationEnd(Animator animation) {
125 if (mContentView != null) {
126 mContentView.setTranslationY(0);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700127 mTopVisibilityView.setTranslationY(0);
Adam Powelle6ec7322010-12-07 15:29:26 -0800128 }
Adam Powell993a63a2011-08-18 16:35:53 -0700129 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
130 mSplitView.setVisibility(View.GONE);
131 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700132 mTopVisibilityView.setVisibility(View.GONE);
Adam Powell01feaee2011-02-10 15:05:05 -0800133 mContainerView.setTransitioning(false);
Adam Powell07e1f982011-03-24 11:11:15 -0700134 mCurrentShowAnim = null;
Adam Powell060e3ca2011-07-19 20:39:16 -0700135 completeDeferredDestroyActionMode();
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700136 if (mOverlayLayout != null) {
137 mOverlayLayout.requestFitSystemWindows();
138 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800139 }
140 };
141
Adam Powell07e1f982011-03-24 11:11:15 -0700142 final AnimatorListener mShowListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800143 @Override
144 public void onAnimationEnd(Animator animation) {
Adam Powell07e1f982011-03-24 11:11:15 -0700145 mCurrentShowAnim = null;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700146 mTopVisibilityView.requestLayout();
Adam Powelle6ec7322010-12-07 15:29:26 -0800147 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800148 };
149
Adam Powell661c9082010-07-02 10:09:44 -0700150 public ActionBarImpl(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -0700151 mActivity = activity;
Adam Powelle6ec7322010-12-07 15:29:26 -0800152 Window window = activity.getWindow();
153 View decor = window.getDecorView();
154 init(decor);
155 if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) {
156 mContentView = decor.findViewById(android.R.id.content);
157 }
Adam Powelldec9dfd2010-08-09 15:27:54 -0700158 }
159
160 public ActionBarImpl(Dialog dialog) {
161 mDialog = dialog;
162 init(dialog.getWindow().getDecorView());
163 }
164
165 private void init(View decor) {
166 mContext = decor.getContext();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700167 mOverlayLayout = (ActionBarOverlayLayout) decor.findViewById(
168 com.android.internal.R.id.action_bar_overlay_layout);
169 if (mOverlayLayout != null) {
170 mOverlayLayout.setActionBar(this);
171 }
Adam Powell89e06452010-06-23 20:24:52 -0700172 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
Adam Powell640a66e2011-04-29 10:18:53 -0700173 mContextView = (ActionBarContextView) decor.findViewById(
Adam Powell89e06452010-06-23 20:24:52 -0700174 com.android.internal.R.id.action_context_bar);
Adam Powell01feaee2011-02-10 15:05:05 -0800175 mContainerView = (ActionBarContainer) decor.findViewById(
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800176 com.android.internal.R.id.action_bar_container);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700177 mTopVisibilityView = (ViewGroup)decor.findViewById(
178 com.android.internal.R.id.top_action_bar);
179 if (mTopVisibilityView == null) {
180 mTopVisibilityView = mContainerView;
181 }
Adam Powell640a66e2011-04-29 10:18:53 -0700182 mSplitView = (ActionBarContainer) decor.findViewById(
183 com.android.internal.R.id.split_action_bar);
Steve Block08f194b2010-08-24 18:20:48 +0100184
Adam Powell640a66e2011-04-29 10:18:53 -0700185 if (mActionView == null || mContextView == null || mContainerView == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700186 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
187 "with a compatible window decor layout");
188 }
Adam Powell0e94b512010-06-29 17:58:20 -0700189
Adam Powell640a66e2011-04-29 10:18:53 -0700190 mActionView.setContextView(mContextView);
Adam Powell9b4bee02011-04-27 19:24:47 -0700191 mContextDisplayMode = mActionView.isSplitActionBar() ?
192 CONTEXT_DISPLAY_SPLIT : CONTEXT_DISPLAY_NORMAL;
Adam Powelldae78242011-04-25 15:23:41 -0700193
Adam Powelld40423a2012-05-02 14:06:03 -0700194 // This was initially read from the action bar style
195 final int current = mActionView.getDisplayOptions();
196 final boolean homeAsUp = (current & DISPLAY_HOME_AS_UP) != 0;
197 if (homeAsUp) {
198 mDisplayHomeAsUpSet = true;
199 }
200
Adam Powellb8139af2012-04-19 13:52:46 -0700201 ActionBarPolicy abp = ActionBarPolicy.get(mContext);
Adam Powelld40423a2012-05-02 14:06:03 -0700202 setHomeButtonEnabled(abp.enableHomeButtonByDefault() || homeAsUp);
Adam Powellb8139af2012-04-19 13:52:46 -0700203 setHasEmbeddedTabs(abp.hasEmbeddedTabs());
Adam Powellf8ac6b72011-05-23 18:14:09 -0700204 }
205
206 public void onConfigurationChanged(Configuration newConfig) {
Adam Powellb8139af2012-04-19 13:52:46 -0700207 setHasEmbeddedTabs(ActionBarPolicy.get(mContext).hasEmbeddedTabs());
Adam Powellf5645cb2011-08-10 22:49:02 -0700208 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700209
Adam Powellf5645cb2011-08-10 22:49:02 -0700210 private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) {
211 mHasEmbeddedTabs = hasEmbeddedTabs;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700212 // Switch tab layout configuration if needed
213 if (!mHasEmbeddedTabs) {
214 mActionView.setEmbeddedTabView(null);
215 mContainerView.setTabContainer(mTabScrollView);
216 } else {
217 mContainerView.setTabContainer(null);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700218 mActionView.setEmbeddedTabView(mTabScrollView);
219 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700220 final boolean isInTabMode = getNavigationMode() == NAVIGATION_MODE_TABS;
221 if (mTabScrollView != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700222 if (isInTabMode) {
223 mTabScrollView.setVisibility(View.VISIBLE);
224 if (mOverlayLayout != null) {
225 mOverlayLayout.requestFitSystemWindows();
226 }
227 } else {
228 mTabScrollView.setVisibility(View.GONE);
229 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700230 }
231 mActionView.setCollapsable(!mHasEmbeddedTabs && isInTabMode);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700232 }
233
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700234 public boolean hasNonEmbeddedTabs() {
235 return !mHasEmbeddedTabs && getNavigationMode() == NAVIGATION_MODE_TABS;
236 }
237
Adam Powellf8ac6b72011-05-23 18:14:09 -0700238 private void ensureTabsExist() {
239 if (mTabScrollView != null) {
240 return;
241 }
242
Adam Powellf5645cb2011-08-10 22:49:02 -0700243 ScrollingTabContainerView tabScroller = new ScrollingTabContainerView(mContext);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700244
245 if (mHasEmbeddedTabs) {
246 tabScroller.setVisibility(View.VISIBLE);
247 mActionView.setEmbeddedTabView(tabScroller);
248 } else {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700249 if (getNavigationMode() == NAVIGATION_MODE_TABS) {
250 tabScroller.setVisibility(View.VISIBLE);
251 if (mOverlayLayout != null) {
252 mOverlayLayout.requestFitSystemWindows();
253 }
254 } else {
255 tabScroller.setVisibility(View.GONE);
256 }
Adam Powelldae78242011-04-25 15:23:41 -0700257 mContainerView.setTabContainer(tabScroller);
Adam Powelldae78242011-04-25 15:23:41 -0700258 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700259 mTabScrollView = tabScroller;
Adam Powell89e06452010-06-23 20:24:52 -0700260 }
261
Adam Powell060e3ca2011-07-19 20:39:16 -0700262 void completeDeferredDestroyActionMode() {
263 if (mDeferredModeDestroyCallback != null) {
264 mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode);
265 mDeferredDestroyActionMode = null;
266 mDeferredModeDestroyCallback = null;
267 }
268 }
269
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700270 public void setWindowVisibility(int visibility) {
271 mCurWindowVisibility = visibility;
272 }
273
Adam Powell50efbed2011-02-08 16:20:15 -0800274 /**
275 * Enables or disables animation between show/hide states.
276 * If animation is disabled using this method, animations in progress
277 * will be finished.
278 *
279 * @param enabled true to animate, false to not animate.
280 */
281 public void setShowHideAnimationEnabled(boolean enabled) {
282 mShowHideAnimationEnabled = enabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700283 if (!enabled && mCurrentShowAnim != null) {
284 mCurrentShowAnim.end();
Adam Powell50efbed2011-02-08 16:20:15 -0800285 }
286 }
287
Adam Powell8515ee82010-11-30 14:09:55 -0800288 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
289 mMenuVisibilityListeners.add(listener);
290 }
291
292 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
293 mMenuVisibilityListeners.remove(listener);
294 }
295
296 public void dispatchMenuVisibilityChanged(boolean isVisible) {
297 if (isVisible == mLastMenuVisibility) {
298 return;
299 }
300 mLastMenuVisibility = isVisible;
301
302 final int count = mMenuVisibilityListeners.size();
303 for (int i = 0; i < count; i++) {
304 mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
305 }
306 }
307
Adam Powella66c7b02010-07-28 15:28:25 -0700308 @Override
Adam Powell3f476b32011-01-03 19:25:36 -0800309 public void setCustomView(int resId) {
Adam Powellf2423682011-08-11 14:29:45 -0700310 setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId, mActionView, false));
Adam Powell3f476b32011-01-03 19:25:36 -0800311 }
312
313 @Override
314 public void setDisplayUseLogoEnabled(boolean useLogo) {
315 setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
316 }
317
318 @Override
319 public void setDisplayShowHomeEnabled(boolean showHome) {
320 setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
321 }
322
323 @Override
324 public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
325 setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
326 }
327
328 @Override
329 public void setDisplayShowTitleEnabled(boolean showTitle) {
330 setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
331 }
332
333 @Override
334 public void setDisplayShowCustomEnabled(boolean showCustom) {
335 setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
336 }
337
338 @Override
Adam Powellc29f4e52011-07-13 20:40:52 -0700339 public void setHomeButtonEnabled(boolean enable) {
340 mActionView.setHomeButtonEnabled(enable);
Adam Powelldae78242011-04-25 15:23:41 -0700341 }
342
343 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700344 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700345 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700346 }
347
348 @Override
349 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700350 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700351 }
352
Adam Powell17809772010-07-21 13:25:11 -0700353 public void setSelectedNavigationItem(int position) {
354 switch (mActionView.getNavigationMode()) {
355 case NAVIGATION_MODE_TABS:
356 selectTab(mTabs.get(position));
357 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700358 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700359 mActionView.setDropdownSelectedPosition(position);
360 break;
361 default:
362 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700363 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700364 }
365 }
366
Adam Powell9ab97872010-10-26 21:47:29 -0700367 public void removeAllTabs() {
368 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700369 }
370
Adam Powell661c9082010-07-02 10:09:44 -0700371 private void cleanupTabs() {
372 if (mSelectedTab != null) {
373 selectTab(null);
374 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700375 mTabs.clear();
Adam Powelld21aa122011-05-27 13:09:52 -0700376 if (mTabScrollView != null) {
377 mTabScrollView.removeAllTabs();
378 }
Adam Powell0c24a552010-11-03 16:44:11 -0700379 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700380 }
381
Adam Powell0e94b512010-06-29 17:58:20 -0700382 public void setTitle(CharSequence title) {
383 mActionView.setTitle(title);
384 }
385
386 public void setSubtitle(CharSequence subtitle) {
387 mActionView.setSubtitle(subtitle);
388 }
389
Adam Powell89e06452010-06-23 20:24:52 -0700390 public void setDisplayOptions(int options) {
Adam Powelldd8fab22012-03-22 17:47:27 -0700391 if ((options & DISPLAY_HOME_AS_UP) != 0) {
392 mDisplayHomeAsUpSet = true;
393 }
Adam Powell89e06452010-06-23 20:24:52 -0700394 mActionView.setDisplayOptions(options);
395 }
Adam Powell0e94b512010-06-29 17:58:20 -0700396
Adam Powell89e06452010-06-23 20:24:52 -0700397 public void setDisplayOptions(int options, int mask) {
398 final int current = mActionView.getDisplayOptions();
Adam Powelldd8fab22012-03-22 17:47:27 -0700399 if ((mask & DISPLAY_HOME_AS_UP) != 0) {
400 mDisplayHomeAsUpSet = true;
401 }
Adam Powell89e06452010-06-23 20:24:52 -0700402 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
403 }
404
405 public void setBackgroundDrawable(Drawable d) {
Adam Powellf88b9152011-09-07 14:54:32 -0700406 mContainerView.setPrimaryBackground(d);
407 }
408
409 public void setStackedBackgroundDrawable(Drawable d) {
410 mContainerView.setStackedBackground(d);
411 }
412
413 public void setSplitBackgroundDrawable(Drawable d) {
414 if (mSplitView != null) {
415 mSplitView.setSplitBackground(d);
416 }
Adam Powell89e06452010-06-23 20:24:52 -0700417 }
418
Adam Powellef704442010-11-16 14:48:22 -0800419 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700420 return mActionView.getCustomNavigationView();
421 }
422
423 public CharSequence getTitle() {
424 return mActionView.getTitle();
425 }
426
427 public CharSequence getSubtitle() {
428 return mActionView.getSubtitle();
429 }
430
431 public int getNavigationMode() {
432 return mActionView.getNavigationMode();
433 }
434
435 public int getDisplayOptions() {
436 return mActionView.getDisplayOptions();
437 }
438
Adam Powell5d279772010-07-27 16:34:07 -0700439 public ActionMode startActionMode(ActionMode.Callback callback) {
440 if (mActionMode != null) {
441 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700442 }
Adam Powell3461b322010-07-14 11:34:43 -0700443
Adam Powell640a66e2011-04-29 10:18:53 -0700444 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700445 ActionModeImpl mode = new ActionModeImpl(callback);
446 if (mode.dispatchOnCreate()) {
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700447 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700448 mContextView.initForMode(mode);
449 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700450 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700451 // TODO animate this
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700452 if (mSplitView.getVisibility() != View.VISIBLE) {
453 mSplitView.setVisibility(View.VISIBLE);
454 if (mOverlayLayout != null) {
455 mOverlayLayout.requestFitSystemWindows();
456 }
457 }
Adam Powell89e06452010-06-23 20:24:52 -0700458 }
Adam Powell86ed4362011-09-14 16:18:53 -0700459 mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powell5d279772010-07-27 16:34:07 -0700460 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700461 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700462 }
Adam Powellac695c62010-07-20 18:19:27 -0700463 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700464 }
465
Adam Powell661c9082010-07-02 10:09:44 -0700466 private void configureTab(Tab tab, int position) {
467 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700468 final ActionBar.TabListener callback = tabi.getCallback();
469
470 if (callback == null) {
471 throw new IllegalStateException("Action Bar Tab must have a Callback");
472 }
Adam Powell661c9082010-07-02 10:09:44 -0700473
474 tabi.setPosition(position);
475 mTabs.add(position, tabi);
476
Adam Powell81b89442010-11-02 17:58:56 -0700477 final int count = mTabs.size();
478 for (int i = position + 1; i < count; i++) {
479 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700480 }
Adam Powell661c9082010-07-02 10:09:44 -0700481 }
482
483 @Override
484 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700485 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700486 }
487
488 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700489 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700490 addTab(tab, position, mTabs.isEmpty());
491 }
492
493 @Override
494 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700495 ensureTabsExist();
496 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700497 configureTab(tab, mTabs.size());
498 if (setSelected) {
499 selectTab(tab);
500 }
501 }
502
503 @Override
504 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700505 ensureTabsExist();
506 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700507 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700508 if (setSelected) {
509 selectTab(tab);
510 }
Adam Powell661c9082010-07-02 10:09:44 -0700511 }
512
513 @Override
514 public Tab newTab() {
515 return new TabImpl();
516 }
517
518 @Override
519 public void removeTab(Tab tab) {
520 removeTabAt(tab.getPosition());
521 }
522
523 @Override
524 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700525 if (mTabScrollView == null) {
526 // No tabs around to remove
527 return;
528 }
529
Adam Powell0c24a552010-11-03 16:44:11 -0700530 int selectedTabPosition = mSelectedTab != null
531 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700532 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700533 TabImpl removedTab = mTabs.remove(position);
534 if (removedTab != null) {
535 removedTab.setPosition(-1);
536 }
Adam Powell661c9082010-07-02 10:09:44 -0700537
538 final int newTabCount = mTabs.size();
539 for (int i = position; i < newTabCount; i++) {
540 mTabs.get(i).setPosition(i);
541 }
542
Adam Powell0c24a552010-11-03 16:44:11 -0700543 if (selectedTabPosition == position) {
544 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
545 }
Adam Powell661c9082010-07-02 10:09:44 -0700546 }
547
548 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700549 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700550 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
551 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
552 return;
553 }
554
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800555 final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction()
Adam Powell0c24a552010-11-03 16:44:11 -0700556 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700557
558 if (mSelectedTab == tab) {
559 if (mSelectedTab != null) {
560 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700561 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700562 }
563 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700564 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700565 if (mSelectedTab != null) {
566 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
567 }
568 mSelectedTab = (TabImpl) tab;
569 if (mSelectedTab != null) {
570 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
571 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700572 }
573
574 if (!trans.isEmpty()) {
575 trans.commit();
576 }
577 }
578
579 @Override
580 public Tab getSelectedTab() {
581 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700582 }
583
Adam Powell6b336f82010-08-10 20:13:01 -0700584 @Override
585 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700586 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700587 }
588
589 @Override
590 public void show() {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700591 if (mHiddenByApp) {
592 mHiddenByApp = false;
593 updateVisibility(false);
594 }
Adam Powell07e1f982011-03-24 11:11:15 -0700595 }
596
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700597 private void showForActionMode() {
598 if (!mShowingForMode) {
599 mShowingForMode = true;
600 if (mOverlayLayout != null) {
601 mOverlayLayout.setShowingForActionMode(true);
602 }
603 updateVisibility(false);
604 }
605 }
606
607 public void showForSystem() {
608 if (mHiddenBySystem) {
609 mHiddenBySystem = false;
610 updateVisibility(true);
611 }
612 }
613
614 @Override
615 public void hide() {
616 if (!mHiddenByApp) {
617 mHiddenByApp = true;
618 updateVisibility(false);
619 }
620 }
621
622 private void hideForActionMode() {
623 if (mShowingForMode) {
624 mShowingForMode = false;
625 if (mOverlayLayout != null) {
626 mOverlayLayout.setShowingForActionMode(false);
627 }
628 updateVisibility(false);
629 }
630 }
631
632 public void hideForSystem() {
633 if (!mHiddenBySystem) {
634 mHiddenBySystem = true;
635 updateVisibility(true);
636 }
637 }
638
639 private static boolean checkShowingFlags(boolean hiddenByApp, boolean hiddenBySystem,
640 boolean showingForMode) {
641 if (showingForMode) {
642 return true;
643 } else if (hiddenByApp || hiddenBySystem) {
644 return false;
645 } else {
646 return true;
647 }
648 }
649
650 private void updateVisibility(boolean fromSystem) {
651 // Based on the current state, should we be hidden or shown?
652 final boolean shown = checkShowingFlags(mHiddenByApp, mHiddenBySystem,
653 mShowingForMode);
654
655 if (shown) {
656 if (!mNowShowing) {
657 mNowShowing = true;
658 doShow(fromSystem);
659 }
660 } else {
661 if (mNowShowing) {
662 mNowShowing = false;
663 doHide(fromSystem);
664 }
665 }
666 }
667
668 public void doShow(boolean fromSystem) {
Adam Powell07e1f982011-03-24 11:11:15 -0700669 if (mCurrentShowAnim != null) {
670 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800671 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700672 mTopVisibilityView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800673
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700674 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700675 || fromSystem)) {
Chet Haasee8118e12012-05-30 14:19:02 -0700676 mTopVisibilityView.setTranslationY(0); // because we're about to ask its window loc
677 float startingY = -mTopVisibilityView.getHeight();
678 if (fromSystem) {
679 int topLeft[] = {0, 0};
680 mTopVisibilityView.getLocationInWindow(topLeft);
681 startingY -= topLeft[1];
682 }
683 mTopVisibilityView.setTranslationY(startingY);
Adam Powell50efbed2011-02-08 16:20:15 -0800684 AnimatorSet anim = new AnimatorSet();
Chet Haasee8118e12012-05-30 14:19:02 -0700685 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView,
686 "translationY", 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800687 if (mContentView != null) {
688 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
Chet Haasee8118e12012-05-30 14:19:02 -0700689 startingY, 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800690 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700691 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700692 mSplitView.setTranslationY(mSplitView.getHeight());
Adam Powell993a63a2011-08-18 16:35:53 -0700693 mSplitView.setVisibility(View.VISIBLE);
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700694 b.with(ObjectAnimator.ofFloat(mSplitView, "translationY", 0));
Adam Powell640a66e2011-04-29 10:18:53 -0700695 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700696 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
Chet Haasee8118e12012-05-30 14:19:02 -0700697 com.android.internal.R.interpolator.decelerate_cubic));
698 anim.setDuration(250);
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700699 // If this is being shown from the system, add a small delay.
700 // This is because we will also be animating in the status bar,
701 // and these two elements can't be done in lock-step. So we give
702 // a little time for the status bar to start its animation before
703 // the action bar animates. (This corresponds to the corresponding
704 // case when hiding, where the status bar has a small delay before
705 // starting.)
Adam Powell50efbed2011-02-08 16:20:15 -0800706 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700707 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800708 anim.start();
709 } else {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700710 mTopVisibilityView.setAlpha(1);
Dianne Hackborn80d55062012-05-22 18:03:20 -0700711 mTopVisibilityView.setTranslationY(0);
712 if (mContentView != null) {
713 mContentView.setTranslationY(0);
714 }
715 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
716 mSplitView.setAlpha(1);
717 mSplitView.setTranslationY(0);
718 mSplitView.setVisibility(View.VISIBLE);
719 }
Adam Powell50efbed2011-02-08 16:20:15 -0800720 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800721 }
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700722 if (mOverlayLayout != null) {
723 mOverlayLayout.requestFitSystemWindows();
724 }
Adam Powell6b336f82010-08-10 20:13:01 -0700725 }
726
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700727 public void doHide(boolean fromSystem) {
Adam Powell07e1f982011-03-24 11:11:15 -0700728 if (mCurrentShowAnim != null) {
729 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800730 }
Adam Powell50efbed2011-02-08 16:20:15 -0800731
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700732 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700733 || fromSystem)) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700734 mTopVisibilityView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800735 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800736 AnimatorSet anim = new AnimatorSet();
Chet Haasee8118e12012-05-30 14:19:02 -0700737 float endingY = -mTopVisibilityView.getHeight();
738 if (fromSystem) {
739 int topLeft[] = {0, 0};
740 mTopVisibilityView.getLocationInWindow(topLeft);
741 endingY -= topLeft[1];
742 }
743 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView,
744 "translationY", endingY));
Adam Powell50efbed2011-02-08 16:20:15 -0800745 if (mContentView != null) {
746 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
Chet Haasee8118e12012-05-30 14:19:02 -0700747 0, endingY));
Adam Powell50efbed2011-02-08 16:20:15 -0800748 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700749 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700750 mSplitView.setAlpha(1);
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700751 b.with(ObjectAnimator.ofFloat(mSplitView, "translationY",
752 mSplitView.getHeight()));
Adam Powell640a66e2011-04-29 10:18:53 -0700753 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700754 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
Chet Haasee8118e12012-05-30 14:19:02 -0700755 com.android.internal.R.interpolator.accelerate_cubic));
756 anim.setDuration(250);
Adam Powell50efbed2011-02-08 16:20:15 -0800757 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700758 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800759 anim.start();
760 } else {
761 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800762 }
Adam Powell6b336f82010-08-10 20:13:01 -0700763 }
764
765 public boolean isShowing() {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700766 return mNowShowing;
767 }
768
769 public boolean isSystemShowing() {
770 return !mHiddenBySystem;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800771 }
772
Adam Powell640a66e2011-04-29 10:18:53 -0700773 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700774 if (toActionMode) {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700775 showForActionMode();
776 } else {
777 hideForActionMode();
Adam Powell07e1f982011-03-24 11:11:15 -0700778 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800779
Adam Powell640a66e2011-04-29 10:18:53 -0700780 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
781 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700782 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
783 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
784 }
Adam Powell6b336f82010-08-10 20:13:01 -0700785 }
786
Adam Powell88ab6972011-07-28 11:25:01 -0700787 public Context getThemedContext() {
788 if (mThemedContext == null) {
789 TypedValue outValue = new TypedValue();
790 Resources.Theme currentTheme = mContext.getTheme();
791 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
792 outValue, true);
793 final int targetThemeRes = outValue.resourceId;
794
795 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
796 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
797 } else {
798 mThemedContext = mContext;
799 }
800 }
801 return mThemedContext;
802 }
803
Adam Powell27cba382013-01-22 18:55:20 -0800804 @Override
805 public boolean isTitleTruncated() {
806 return mActionView != null && mActionView.isTitleTruncated();
807 }
808
Adam Powell89e06452010-06-23 20:24:52 -0700809 /**
810 * @hide
811 */
Adam Powell5d279772010-07-27 16:34:07 -0700812 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700813 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700814 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700815 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700816
Adam Powell5d279772010-07-27 16:34:07 -0700817 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700818 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700819 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700820 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700821 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700822 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700823
824 @Override
825 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700826 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700827 }
828
Adam Powell89e06452010-06-23 20:24:52 -0700829 @Override
830 public Menu getMenu() {
831 return mMenu;
832 }
833
834 @Override
835 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700836 if (mActionMode != this) {
837 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700838 return;
839 }
840
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700841 // If this change in state is going to cause the action bar
842 // to be hidden, defer the onDestroy callback until the animation
843 // is finished and associated relayout is about to happen. This lets
844 // apps better anticipate visibility and layout behavior.
845 if (!checkShowingFlags(mHiddenByApp, mHiddenBySystem, false)) {
846 // With the current state but the action bar hidden, our
847 // overall showing state is going to be false.
Adam Powell060e3ca2011-07-19 20:39:16 -0700848 mDeferredDestroyActionMode = this;
849 mDeferredModeDestroyCallback = mCallback;
850 } else {
851 mCallback.onDestroyActionMode(this);
852 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800853 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700854 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700855
856 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700857 mContextView.closeMode();
Adam Powell86ed4362011-09-14 16:18:53 -0700858 mActionView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700859
Adam Powell5d279772010-07-27 16:34:07 -0700860 mActionMode = null;
Adam Powell89e06452010-06-23 20:24:52 -0700861 }
862
863 @Override
864 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700865 mMenu.stopDispatchingItemsChanged();
866 try {
867 mCallback.onPrepareActionMode(this, mMenu);
868 } finally {
869 mMenu.startDispatchingItemsChanged();
870 }
871 }
872
873 public boolean dispatchOnCreate() {
874 mMenu.stopDispatchingItemsChanged();
875 try {
876 return mCallback.onCreateActionMode(this, mMenu);
877 } finally {
878 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700879 }
880 }
881
882 @Override
883 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700884 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700885 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700886 }
887
888 @Override
889 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700890 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700891 }
892
893 @Override
894 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700895 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700896 }
Adam Powell29ed7572010-07-14 16:24:56 -0700897
898 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700899 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800900 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700901 }
902
903 @Override
904 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800905 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700906 }
907
908 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700909 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700910 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700911 }
912
913 @Override
914 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700915 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700916 }
Adam Powell89e06452010-06-23 20:24:52 -0700917
Adam Powell29ed7572010-07-14 16:24:56 -0700918 @Override
Adam Powellb98a81f2012-02-24 11:09:07 -0800919 public void setTitleOptionalHint(boolean titleOptional) {
Adam Powell785c4472012-05-02 21:25:39 -0700920 super.setTitleOptionalHint(titleOptional);
Adam Powellb98a81f2012-02-24 11:09:07 -0800921 mContextView.setTitleOptional(titleOptional);
922 }
923
924 @Override
925 public boolean isTitleOptional() {
926 return mContextView.isTitleOptional();
927 }
928
929 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700930 public View getCustomView() {
931 return mCustomView != null ? mCustomView.get() : null;
932 }
933
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700934 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800935 if (mCallback != null) {
936 return mCallback.onActionItemClicked(this, item);
937 } else {
938 return false;
939 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700940 }
941
942 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
943 }
944
945 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800946 if (mCallback == null) {
947 return false;
948 }
949
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700950 if (!subMenu.hasVisibleItems()) {
951 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700952 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700953
Adam Powellf2423682011-08-11 14:29:45 -0700954 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700955 return true;
956 }
957
958 public void onCloseSubMenu(SubMenuBuilder menu) {
959 }
960
961 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800962 if (mCallback == null) {
963 return;
964 }
Adam Powellf6148c52010-08-11 21:10:16 -0700965 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700966 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700967 }
Adam Powell661c9082010-07-02 10:09:44 -0700968 }
969
970 /**
971 * @hide
972 */
973 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700974 private ActionBar.TabListener mCallback;
975 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700976 private Drawable mIcon;
977 private CharSequence mText;
Adam Powell94e56ef2011-09-06 21:22:22 -0700978 private CharSequence mContentDesc;
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700979 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -0700980 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700981
982 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700983 public Object getTag() {
984 return mTag;
985 }
986
987 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700988 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700989 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700990 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700991 }
992
993 public ActionBar.TabListener getCallback() {
994 return mCallback;
995 }
996
997 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700998 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700999 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -07001000 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -07001001 }
1002
1003 @Override
1004 public View getCustomView() {
1005 return mCustomView;
1006 }
1007
1008 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001009 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -07001010 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001011 if (mPosition >= 0) {
1012 mTabScrollView.updateTab(mPosition);
1013 }
Adam Powell9ab97872010-10-26 21:47:29 -07001014 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001015 }
1016
1017 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001018 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -07001019 return setCustomView(LayoutInflater.from(getThemedContext())
1020 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -08001021 }
1022
1023 @Override
Adam Powell661c9082010-07-02 10:09:44 -07001024 public Drawable getIcon() {
1025 return mIcon;
1026 }
1027
1028 @Override
1029 public int getPosition() {
1030 return mPosition;
1031 }
1032
1033 public void setPosition(int position) {
1034 mPosition = position;
1035 }
1036
1037 @Override
1038 public CharSequence getText() {
1039 return mText;
1040 }
1041
1042 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001043 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -07001044 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001045 if (mPosition >= 0) {
1046 mTabScrollView.updateTab(mPosition);
1047 }
Adam Powell9ab97872010-10-26 21:47:29 -07001048 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001049 }
1050
1051 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001052 public Tab setIcon(int resId) {
1053 return setIcon(mContext.getResources().getDrawable(resId));
1054 }
1055
1056 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001057 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -07001058 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001059 if (mPosition >= 0) {
1060 mTabScrollView.updateTab(mPosition);
1061 }
Adam Powell9ab97872010-10-26 21:47:29 -07001062 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001063 }
1064
1065 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001066 public Tab setText(int resId) {
1067 return setText(mContext.getResources().getText(resId));
1068 }
1069
1070 @Override
Adam Powell661c9082010-07-02 10:09:44 -07001071 public void select() {
1072 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -07001073 }
Adam Powell94e56ef2011-09-06 21:22:22 -07001074
1075 @Override
1076 public Tab setContentDescription(int resId) {
1077 return setContentDescription(mContext.getResources().getText(resId));
1078 }
1079
1080 @Override
1081 public Tab setContentDescription(CharSequence contentDesc) {
1082 mContentDesc = contentDesc;
1083 if (mPosition >= 0) {
1084 mTabScrollView.updateTab(mPosition);
1085 }
1086 return this;
1087 }
1088
1089 @Override
1090 public CharSequence getContentDescription() {
1091 return mContentDesc;
1092 }
Adam Powell89e06452010-06-23 20:24:52 -07001093 }
Adam Powell9ab97872010-10-26 21:47:29 -07001094
1095 @Override
1096 public void setCustomView(View view) {
1097 mActionView.setCustomNavigationView(view);
1098 }
1099
1100 @Override
1101 public void setCustomView(View view, LayoutParams layoutParams) {
1102 view.setLayoutParams(layoutParams);
1103 mActionView.setCustomNavigationView(view);
1104 }
1105
1106 @Override
Adam Powell8515ee82010-11-30 14:09:55 -08001107 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -07001108 mActionView.setDropdownAdapter(adapter);
1109 mActionView.setCallback(callback);
1110 }
1111
1112 @Override
1113 public int getSelectedNavigationIndex() {
1114 switch (mActionView.getNavigationMode()) {
1115 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -08001116 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -07001117 case NAVIGATION_MODE_LIST:
1118 return mActionView.getDropdownSelectedPosition();
1119 default:
1120 return -1;
1121 }
1122 }
1123
1124 @Override
1125 public int getNavigationItemCount() {
1126 switch (mActionView.getNavigationMode()) {
1127 case NAVIGATION_MODE_TABS:
1128 return mTabs.size();
1129 case NAVIGATION_MODE_LIST:
1130 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
1131 return adapter != null ? adapter.getCount() : 0;
1132 default:
1133 return 0;
1134 }
1135 }
1136
1137 @Override
Adam Powell0c24a552010-11-03 16:44:11 -07001138 public int getTabCount() {
1139 return mTabs.size();
1140 }
1141
1142 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001143 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -07001144 final int oldMode = mActionView.getNavigationMode();
1145 switch (oldMode) {
1146 case NAVIGATION_MODE_TABS:
1147 mSavedTabPosition = getSelectedNavigationIndex();
1148 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -07001149 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -07001150 break;
1151 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001152 if (oldMode != mode && !mHasEmbeddedTabs) {
1153 if (mOverlayLayout != null) {
1154 mOverlayLayout.requestFitSystemWindows();
1155 }
1156 }
Adam Powell9ab97872010-10-26 21:47:29 -07001157 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -07001158 switch (mode) {
1159 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -07001160 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -07001161 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -07001162 if (mSavedTabPosition != INVALID_POSITION) {
1163 setSelectedNavigationItem(mSavedTabPosition);
1164 mSavedTabPosition = INVALID_POSITION;
1165 }
1166 break;
1167 }
Adam Powelld21aa122011-05-27 13:09:52 -07001168 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -07001169 }
1170
1171 @Override
1172 public Tab getTabAt(int index) {
1173 return mTabs.get(index);
1174 }
Adam Powell0c24a552010-11-03 16:44:11 -07001175
Adam Powell0c24a552010-11-03 16:44:11 -07001176
Adam Powell1969b872011-03-22 11:52:48 -07001177 @Override
1178 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001179 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001180 }
Adam Powell0c24a552010-11-03 16:44:11 -07001181
Adam Powell1969b872011-03-22 11:52:48 -07001182 @Override
1183 public void setIcon(Drawable icon) {
1184 mActionView.setIcon(icon);
1185 }
1186
1187 @Override
1188 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001189 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001190 }
1191
1192 @Override
1193 public void setLogo(Drawable logo) {
1194 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -07001195 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001196
1197 public void setDefaultDisplayHomeAsUpEnabled(boolean enable) {
1198 if (!mDisplayHomeAsUpSet) {
1199 setDisplayHomeAsUpEnabled(enable);
1200 }
1201 }
Adam Powell89e06452010-06-23 20:24:52 -07001202}