blob: 2cbd9cc90287c31be35054ec3b05ab097ab5e637 [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)) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700676 mTopVisibilityView.setAlpha(0);
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700677 mTopVisibilityView.setTranslationY(-mTopVisibilityView.getHeight());
Adam Powell50efbed2011-02-08 16:20:15 -0800678 AnimatorSet anim = new AnimatorSet();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700679 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView, "alpha", 1));
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700680 b.with(ObjectAnimator.ofFloat(mTopVisibilityView, "translationY", 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800681 if (mContentView != null) {
682 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700683 -mTopVisibilityView.getHeight(), 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800684 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700685 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell640a66e2011-04-29 10:18:53 -0700686 mSplitView.setAlpha(0);
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700687 mSplitView.setTranslationY(mSplitView.getHeight());
Adam Powell993a63a2011-08-18 16:35:53 -0700688 mSplitView.setVisibility(View.VISIBLE);
Adam Powell640a66e2011-04-29 10:18:53 -0700689 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 1));
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700690 b.with(ObjectAnimator.ofFloat(mSplitView, "translationY", 0));
Adam Powell640a66e2011-04-29 10:18:53 -0700691 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700692 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
693 com.android.internal.R.interpolator.decelerate_quad));
Dianne Hackborn1cc2bce2012-05-04 18:59:02 -0700694 anim.setDuration(mContext.getResources().getInteger(
695 com.android.internal.R.integer.config_mediumAnimTime));
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700696 // If this is being shown from the system, add a small delay.
697 // This is because we will also be animating in the status bar,
698 // and these two elements can't be done in lock-step. So we give
699 // a little time for the status bar to start its animation before
700 // the action bar animates. (This corresponds to the corresponding
701 // case when hiding, where the status bar has a small delay before
702 // starting.)
703 if (fromSystem) {
704 anim.setStartDelay(100);
705 }
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);
Adam Powelld8145042011-03-24 14:18:27 -0700711 mContainerView.setTranslationY(0);
Adam Powell50efbed2011-02-08 16:20:15 -0800712 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800713 }
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700714 if (mOverlayLayout != null) {
715 mOverlayLayout.requestFitSystemWindows();
716 }
Adam Powell6b336f82010-08-10 20:13:01 -0700717 }
718
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700719 public void doHide(boolean fromSystem) {
Adam Powell07e1f982011-03-24 11:11:15 -0700720 if (mCurrentShowAnim != null) {
721 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800722 }
Adam Powell50efbed2011-02-08 16:20:15 -0800723
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700724 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700725 || fromSystem)) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700726 mTopVisibilityView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800727 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800728 AnimatorSet anim = new AnimatorSet();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700729 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView, "alpha", 0));
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700730 b.with(ObjectAnimator.ofFloat(mTopVisibilityView, "translationY",
731 -mTopVisibilityView.getHeight()));
Adam Powell50efbed2011-02-08 16:20:15 -0800732 if (mContentView != null) {
733 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700734 0, -mTopVisibilityView.getHeight()));
Adam Powell50efbed2011-02-08 16:20:15 -0800735 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700736 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700737 mSplitView.setAlpha(1);
738 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 0));
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700739 b.with(ObjectAnimator.ofFloat(mSplitView, "translationY",
740 mSplitView.getHeight()));
Adam Powell640a66e2011-04-29 10:18:53 -0700741 }
Dianne Hackborn8eedb8b2012-04-16 19:31:58 -0700742 anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
743 com.android.internal.R.interpolator.accelerate_quad));
Dianne Hackborn1cc2bce2012-05-04 18:59:02 -0700744 anim.setDuration(mContext.getResources().getInteger(
745 com.android.internal.R.integer.config_mediumAnimTime));
Adam Powell50efbed2011-02-08 16:20:15 -0800746 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700747 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800748 anim.start();
749 } else {
750 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800751 }
Adam Powell6b336f82010-08-10 20:13:01 -0700752 }
753
754 public boolean isShowing() {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700755 return mNowShowing;
756 }
757
758 public boolean isSystemShowing() {
759 return !mHiddenBySystem;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800760 }
761
Adam Powell640a66e2011-04-29 10:18:53 -0700762 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700763 if (toActionMode) {
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700764 showForActionMode();
765 } else {
766 hideForActionMode();
Adam Powell07e1f982011-03-24 11:11:15 -0700767 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800768
Adam Powell640a66e2011-04-29 10:18:53 -0700769 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
770 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700771 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
772 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
773 }
Adam Powell6b336f82010-08-10 20:13:01 -0700774 }
775
Adam Powell88ab6972011-07-28 11:25:01 -0700776 public Context getThemedContext() {
777 if (mThemedContext == null) {
778 TypedValue outValue = new TypedValue();
779 Resources.Theme currentTheme = mContext.getTheme();
780 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
781 outValue, true);
782 final int targetThemeRes = outValue.resourceId;
783
784 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
785 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
786 } else {
787 mThemedContext = mContext;
788 }
789 }
790 return mThemedContext;
791 }
792
Adam Powell89e06452010-06-23 20:24:52 -0700793 /**
794 * @hide
795 */
Adam Powell5d279772010-07-27 16:34:07 -0700796 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700797 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700798 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700799 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700800
Adam Powell5d279772010-07-27 16:34:07 -0700801 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700802 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700803 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700804 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700805 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700806 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700807
808 @Override
809 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700810 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700811 }
812
Adam Powell89e06452010-06-23 20:24:52 -0700813 @Override
814 public Menu getMenu() {
815 return mMenu;
816 }
817
818 @Override
819 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700820 if (mActionMode != this) {
821 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700822 return;
823 }
824
Dianne Hackborn139e5aa2012-05-05 20:36:38 -0700825 // If this change in state is going to cause the action bar
826 // to be hidden, defer the onDestroy callback until the animation
827 // is finished and associated relayout is about to happen. This lets
828 // apps better anticipate visibility and layout behavior.
829 if (!checkShowingFlags(mHiddenByApp, mHiddenBySystem, false)) {
830 // With the current state but the action bar hidden, our
831 // overall showing state is going to be false.
Adam Powell060e3ca2011-07-19 20:39:16 -0700832 mDeferredDestroyActionMode = this;
833 mDeferredModeDestroyCallback = mCallback;
834 } else {
835 mCallback.onDestroyActionMode(this);
836 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800837 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700838 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700839
840 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700841 mContextView.closeMode();
Adam Powell86ed4362011-09-14 16:18:53 -0700842 mActionView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700843
Adam Powell5d279772010-07-27 16:34:07 -0700844 mActionMode = null;
Adam Powell89e06452010-06-23 20:24:52 -0700845 }
846
847 @Override
848 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700849 mMenu.stopDispatchingItemsChanged();
850 try {
851 mCallback.onPrepareActionMode(this, mMenu);
852 } finally {
853 mMenu.startDispatchingItemsChanged();
854 }
855 }
856
857 public boolean dispatchOnCreate() {
858 mMenu.stopDispatchingItemsChanged();
859 try {
860 return mCallback.onCreateActionMode(this, mMenu);
861 } finally {
862 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700863 }
864 }
865
866 @Override
867 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700868 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700869 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700870 }
871
872 @Override
873 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700874 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700875 }
876
877 @Override
878 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700879 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700880 }
Adam Powell29ed7572010-07-14 16:24:56 -0700881
882 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700883 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800884 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700885 }
886
887 @Override
888 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800889 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700890 }
891
892 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700893 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700894 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700895 }
896
897 @Override
898 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700899 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700900 }
Adam Powell89e06452010-06-23 20:24:52 -0700901
Adam Powell29ed7572010-07-14 16:24:56 -0700902 @Override
Adam Powellb98a81f2012-02-24 11:09:07 -0800903 public void setTitleOptionalHint(boolean titleOptional) {
Adam Powell785c4472012-05-02 21:25:39 -0700904 super.setTitleOptionalHint(titleOptional);
Adam Powellb98a81f2012-02-24 11:09:07 -0800905 mContextView.setTitleOptional(titleOptional);
906 }
907
908 @Override
909 public boolean isTitleOptional() {
910 return mContextView.isTitleOptional();
911 }
912
913 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700914 public View getCustomView() {
915 return mCustomView != null ? mCustomView.get() : null;
916 }
917
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700918 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800919 if (mCallback != null) {
920 return mCallback.onActionItemClicked(this, item);
921 } else {
922 return false;
923 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700924 }
925
926 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
927 }
928
929 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800930 if (mCallback == null) {
931 return false;
932 }
933
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700934 if (!subMenu.hasVisibleItems()) {
935 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700936 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700937
Adam Powellf2423682011-08-11 14:29:45 -0700938 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700939 return true;
940 }
941
942 public void onCloseSubMenu(SubMenuBuilder menu) {
943 }
944
945 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800946 if (mCallback == null) {
947 return;
948 }
Adam Powellf6148c52010-08-11 21:10:16 -0700949 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700950 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700951 }
Adam Powell661c9082010-07-02 10:09:44 -0700952 }
953
954 /**
955 * @hide
956 */
957 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700958 private ActionBar.TabListener mCallback;
959 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700960 private Drawable mIcon;
961 private CharSequence mText;
Adam Powell94e56ef2011-09-06 21:22:22 -0700962 private CharSequence mContentDesc;
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700963 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -0700964 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700965
966 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700967 public Object getTag() {
968 return mTag;
969 }
970
971 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700972 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700973 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700974 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700975 }
976
977 public ActionBar.TabListener getCallback() {
978 return mCallback;
979 }
980
981 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700982 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700983 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700984 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700985 }
986
987 @Override
988 public View getCustomView() {
989 return mCustomView;
990 }
991
992 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700993 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700994 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700995 if (mPosition >= 0) {
996 mTabScrollView.updateTab(mPosition);
997 }
Adam Powell9ab97872010-10-26 21:47:29 -0700998 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700999 }
1000
1001 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001002 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -07001003 return setCustomView(LayoutInflater.from(getThemedContext())
1004 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -08001005 }
1006
1007 @Override
Adam Powell661c9082010-07-02 10:09:44 -07001008 public Drawable getIcon() {
1009 return mIcon;
1010 }
1011
1012 @Override
1013 public int getPosition() {
1014 return mPosition;
1015 }
1016
1017 public void setPosition(int position) {
1018 mPosition = position;
1019 }
1020
1021 @Override
1022 public CharSequence getText() {
1023 return mText;
1024 }
1025
1026 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001027 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -07001028 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001029 if (mPosition >= 0) {
1030 mTabScrollView.updateTab(mPosition);
1031 }
Adam Powell9ab97872010-10-26 21:47:29 -07001032 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001033 }
1034
1035 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001036 public Tab setIcon(int resId) {
1037 return setIcon(mContext.getResources().getDrawable(resId));
1038 }
1039
1040 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001041 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -07001042 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -07001043 if (mPosition >= 0) {
1044 mTabScrollView.updateTab(mPosition);
1045 }
Adam Powell9ab97872010-10-26 21:47:29 -07001046 return this;
Adam Powell661c9082010-07-02 10:09:44 -07001047 }
1048
1049 @Override
Adam Powell32555f32010-11-17 13:49:22 -08001050 public Tab setText(int resId) {
1051 return setText(mContext.getResources().getText(resId));
1052 }
1053
1054 @Override
Adam Powell661c9082010-07-02 10:09:44 -07001055 public void select() {
1056 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -07001057 }
Adam Powell94e56ef2011-09-06 21:22:22 -07001058
1059 @Override
1060 public Tab setContentDescription(int resId) {
1061 return setContentDescription(mContext.getResources().getText(resId));
1062 }
1063
1064 @Override
1065 public Tab setContentDescription(CharSequence contentDesc) {
1066 mContentDesc = contentDesc;
1067 if (mPosition >= 0) {
1068 mTabScrollView.updateTab(mPosition);
1069 }
1070 return this;
1071 }
1072
1073 @Override
1074 public CharSequence getContentDescription() {
1075 return mContentDesc;
1076 }
Adam Powell89e06452010-06-23 20:24:52 -07001077 }
Adam Powell9ab97872010-10-26 21:47:29 -07001078
1079 @Override
1080 public void setCustomView(View view) {
1081 mActionView.setCustomNavigationView(view);
1082 }
1083
1084 @Override
1085 public void setCustomView(View view, LayoutParams layoutParams) {
1086 view.setLayoutParams(layoutParams);
1087 mActionView.setCustomNavigationView(view);
1088 }
1089
1090 @Override
Adam Powell8515ee82010-11-30 14:09:55 -08001091 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -07001092 mActionView.setDropdownAdapter(adapter);
1093 mActionView.setCallback(callback);
1094 }
1095
1096 @Override
1097 public int getSelectedNavigationIndex() {
1098 switch (mActionView.getNavigationMode()) {
1099 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -08001100 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -07001101 case NAVIGATION_MODE_LIST:
1102 return mActionView.getDropdownSelectedPosition();
1103 default:
1104 return -1;
1105 }
1106 }
1107
1108 @Override
1109 public int getNavigationItemCount() {
1110 switch (mActionView.getNavigationMode()) {
1111 case NAVIGATION_MODE_TABS:
1112 return mTabs.size();
1113 case NAVIGATION_MODE_LIST:
1114 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
1115 return adapter != null ? adapter.getCount() : 0;
1116 default:
1117 return 0;
1118 }
1119 }
1120
1121 @Override
Adam Powell0c24a552010-11-03 16:44:11 -07001122 public int getTabCount() {
1123 return mTabs.size();
1124 }
1125
1126 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001127 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -07001128 final int oldMode = mActionView.getNavigationMode();
1129 switch (oldMode) {
1130 case NAVIGATION_MODE_TABS:
1131 mSavedTabPosition = getSelectedNavigationIndex();
1132 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -07001133 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -07001134 break;
1135 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001136 if (oldMode != mode && !mHasEmbeddedTabs) {
1137 if (mOverlayLayout != null) {
1138 mOverlayLayout.requestFitSystemWindows();
1139 }
1140 }
Adam Powell9ab97872010-10-26 21:47:29 -07001141 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -07001142 switch (mode) {
1143 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -07001144 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -07001145 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -07001146 if (mSavedTabPosition != INVALID_POSITION) {
1147 setSelectedNavigationItem(mSavedTabPosition);
1148 mSavedTabPosition = INVALID_POSITION;
1149 }
1150 break;
1151 }
Adam Powelld21aa122011-05-27 13:09:52 -07001152 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -07001153 }
1154
1155 @Override
1156 public Tab getTabAt(int index) {
1157 return mTabs.get(index);
1158 }
Adam Powell0c24a552010-11-03 16:44:11 -07001159
Adam Powell0c24a552010-11-03 16:44:11 -07001160
Adam Powell1969b872011-03-22 11:52:48 -07001161 @Override
1162 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001163 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001164 }
Adam Powell0c24a552010-11-03 16:44:11 -07001165
Adam Powell1969b872011-03-22 11:52:48 -07001166 @Override
1167 public void setIcon(Drawable icon) {
1168 mActionView.setIcon(icon);
1169 }
1170
1171 @Override
1172 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001173 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001174 }
1175
1176 @Override
1177 public void setLogo(Drawable logo) {
1178 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -07001179 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001180
1181 public void setDefaultDisplayHomeAsUpEnabled(boolean enable) {
1182 if (!mDisplayHomeAsUpSet) {
1183 setDisplayHomeAsUpEnabled(enable);
1184 }
1185 }
Adam Powell89e06452010-06-23 20:24:52 -07001186}