blob: f1dffa1f859ced160522a206249535740751bf88 [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 Powell2c9c9fe2010-07-16 10:17:57 -070019import com.android.internal.view.menu.MenuBuilder;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070020import com.android.internal.view.menu.MenuPopupHelper;
21import com.android.internal.view.menu.SubMenuBuilder;
Adam Powell01feaee2011-02-10 15:05:05 -080022import com.android.internal.widget.ActionBarContainer;
Adam Powell89e06452010-06-23 20:24:52 -070023import com.android.internal.widget.ActionBarContextView;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070024import com.android.internal.widget.ActionBarOverlayLayout;
Adam Powell89e06452010-06-23 20:24:52 -070025import com.android.internal.widget.ActionBarView;
Adam Powellf8ac6b72011-05-23 18:14:09 -070026import com.android.internal.widget.ScrollingTabContainerView;
Adam Powell89e06452010-06-23 20:24:52 -070027
Adam Powelld8b3f2e2010-12-02 13:37:03 -080028import android.animation.Animator;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080029import android.animation.Animator.AnimatorListener;
Adam Powell07e1f982011-03-24 11:11:15 -070030import android.animation.AnimatorListenerAdapter;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080031import android.animation.AnimatorSet;
Adam Powelle6ec7322010-12-07 15:29:26 -080032import android.animation.ObjectAnimator;
Adam Powell89e06452010-06-23 20:24:52 -070033import android.app.ActionBar;
Adam Powell661c9082010-07-02 10:09:44 -070034import android.app.Activity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070035import android.app.Dialog;
Adam Powell661c9082010-07-02 10:09:44 -070036import android.app.FragmentTransaction;
Adam Powelldec9dfd2010-08-09 15:27:54 -070037import android.content.Context;
Adam Powellf8ac6b72011-05-23 18:14:09 -070038import android.content.res.Configuration;
Adam Powell88ab6972011-07-28 11:25:01 -070039import android.content.res.Resources;
Adam Powell89e06452010-06-23 20:24:52 -070040import android.graphics.drawable.Drawable;
Adam Powellc29f4e52011-07-13 20:40:52 -070041import android.os.Build;
Adam Powell0e94b512010-06-29 17:58:20 -070042import android.os.Handler;
Adam Powell88ab6972011-07-28 11:25:01 -070043import android.util.TypedValue;
Adam Powell6e346362010-07-23 10:18:23 -070044import android.view.ActionMode;
Adam Powell88ab6972011-07-28 11:25:01 -070045import android.view.ContextThemeWrapper;
Adam Powell32555f32010-11-17 13:49:22 -080046import android.view.LayoutInflater;
Adam Powell89e06452010-06-23 20:24:52 -070047import android.view.Menu;
Adam Powell9168f0b2010-08-02 15:46:24 -070048import android.view.MenuInflater;
Adam Powell89e06452010-06-23 20:24:52 -070049import android.view.MenuItem;
50import android.view.View;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070051import android.view.ViewGroup;
Adam Powelle6ec7322010-12-07 15:29:26 -080052import android.view.Window;
Adam Powell86ed4362011-09-14 16:18:53 -070053import android.view.accessibility.AccessibilityEvent;
Adam Powell89e06452010-06-23 20:24:52 -070054import android.widget.SpinnerAdapter;
Adam Powell89e06452010-06-23 20:24:52 -070055
Adam Powell29ed7572010-07-14 16:24:56 -070056import java.lang.ref.WeakReference;
Adam Powell661c9082010-07-02 10:09:44 -070057import java.util.ArrayList;
58
Adam Powell89e06452010-06-23 20:24:52 -070059/**
60 * ActionBarImpl is the ActionBar implementation used
61 * by devices of all screen sizes. If it detects a compatible decor,
62 * it will split contextual modes across both the ActionBarView at
63 * the top of the screen and a horizontal LinearLayout at the bottom
64 * which is normally hidden.
65 */
66public class ActionBarImpl extends ActionBar {
Adam Powelld8145042011-03-24 14:18:27 -070067 private static final String TAG = "ActionBarImpl";
Adam Powell661c9082010-07-02 10:09:44 -070068
Adam Powelldec9dfd2010-08-09 15:27:54 -070069 private Context mContext;
Adam Powell88ab6972011-07-28 11:25:01 -070070 private Context mThemedContext;
Adam Powell661c9082010-07-02 10:09:44 -070071 private Activity mActivity;
Adam Powelldec9dfd2010-08-09 15:27:54 -070072 private Dialog mDialog;
Adam Powell661c9082010-07-02 10:09:44 -070073
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070074 private ActionBarOverlayLayout mOverlayLayout;
Adam Powell01feaee2011-02-10 15:05:05 -080075 private ActionBarContainer mContainerView;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070076 private ViewGroup mTopVisibilityView;
Adam Powell89e06452010-06-23 20:24:52 -070077 private ActionBarView mActionView;
Adam Powell640a66e2011-04-29 10:18:53 -070078 private ActionBarContextView mContextView;
79 private ActionBarContainer mSplitView;
Adam Powelle6ec7322010-12-07 15:29:26 -080080 private View mContentView;
Adam Powellf8ac6b72011-05-23 18:14:09 -070081 private ScrollingTabContainerView mTabScrollView;
Adam Powell661c9082010-07-02 10:09:44 -070082
83 private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>();
84
Adam Powell661c9082010-07-02 10:09:44 -070085 private TabImpl mSelectedTab;
Adam Powell0c24a552010-11-03 16:44:11 -070086 private int mSavedTabPosition = INVALID_POSITION;
Adam Powell89e06452010-06-23 20:24:52 -070087
Adam Powelldd8fab22012-03-22 17:47:27 -070088 private boolean mDisplayHomeAsUpSet;
89
Adam Powell060e3ca2011-07-19 20:39:16 -070090 ActionModeImpl mActionMode;
91 ActionMode mDeferredDestroyActionMode;
92 ActionMode.Callback mDeferredModeDestroyCallback;
Adam Powell89e06452010-06-23 20:24:52 -070093
Adam Powell8515ee82010-11-30 14:09:55 -080094 private boolean mLastMenuVisibility;
95 private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
96 new ArrayList<OnMenuVisibilityListener>();
97
Adam Powell89e06452010-06-23 20:24:52 -070098 private static final int CONTEXT_DISPLAY_NORMAL = 0;
99 private static final int CONTEXT_DISPLAY_SPLIT = 1;
100
Adam Powell0c24a552010-11-03 16:44:11 -0700101 private static final int INVALID_POSITION = -1;
102
Adam Powell89e06452010-06-23 20:24:52 -0700103 private int mContextDisplayMode;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700104 private boolean mHasEmbeddedTabs;
Adam Powell0e94b512010-06-29 17:58:20 -0700105
106 final Handler mHandler = new Handler();
Adam Powellf8ac6b72011-05-23 18:14:09 -0700107 Runnable mTabSelector;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800108
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700109 private int mCurWindowVisibility = View.VISIBLE;
110
Adam Powell07e1f982011-03-24 11:11:15 -0700111 private Animator mCurrentShowAnim;
112 private Animator mCurrentModeAnim;
Adam Powell50efbed2011-02-08 16:20:15 -0800113 private boolean mShowHideAnimationEnabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700114 boolean mWasHiddenBeforeMode;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800115
Adam Powell07e1f982011-03-24 11:11:15 -0700116 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800117 @Override
118 public void onAnimationEnd(Animator animation) {
119 if (mContentView != null) {
120 mContentView.setTranslationY(0);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700121 mTopVisibilityView.setTranslationY(0);
Adam Powelle6ec7322010-12-07 15:29:26 -0800122 }
Adam Powell993a63a2011-08-18 16:35:53 -0700123 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
124 mSplitView.setVisibility(View.GONE);
125 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700126 mTopVisibilityView.setVisibility(View.GONE);
Adam Powell01feaee2011-02-10 15:05:05 -0800127 mContainerView.setTransitioning(false);
Adam Powell07e1f982011-03-24 11:11:15 -0700128 mCurrentShowAnim = null;
Adam Powell060e3ca2011-07-19 20:39:16 -0700129 completeDeferredDestroyActionMode();
Adam Powelle6ec7322010-12-07 15:29:26 -0800130 }
131 };
132
Adam Powell07e1f982011-03-24 11:11:15 -0700133 final AnimatorListener mShowListener = new AnimatorListenerAdapter() {
Adam Powelle6ec7322010-12-07 15:29:26 -0800134 @Override
135 public void onAnimationEnd(Animator animation) {
Adam Powell07e1f982011-03-24 11:11:15 -0700136 mCurrentShowAnim = null;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700137 mTopVisibilityView.requestLayout();
Adam Powelle6ec7322010-12-07 15:29:26 -0800138 }
Adam Powelle6ec7322010-12-07 15:29:26 -0800139 };
140
Adam Powell661c9082010-07-02 10:09:44 -0700141 public ActionBarImpl(Activity activity) {
Adam Powell661c9082010-07-02 10:09:44 -0700142 mActivity = activity;
Adam Powelle6ec7322010-12-07 15:29:26 -0800143 Window window = activity.getWindow();
144 View decor = window.getDecorView();
145 init(decor);
146 if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) {
147 mContentView = decor.findViewById(android.R.id.content);
148 }
Adam Powelldec9dfd2010-08-09 15:27:54 -0700149 }
150
151 public ActionBarImpl(Dialog dialog) {
152 mDialog = dialog;
153 init(dialog.getWindow().getDecorView());
154 }
155
156 private void init(View decor) {
157 mContext = decor.getContext();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700158 mOverlayLayout = (ActionBarOverlayLayout) decor.findViewById(
159 com.android.internal.R.id.action_bar_overlay_layout);
160 if (mOverlayLayout != null) {
161 mOverlayLayout.setActionBar(this);
162 }
Adam Powell89e06452010-06-23 20:24:52 -0700163 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
Adam Powell640a66e2011-04-29 10:18:53 -0700164 mContextView = (ActionBarContextView) decor.findViewById(
Adam Powell89e06452010-06-23 20:24:52 -0700165 com.android.internal.R.id.action_context_bar);
Adam Powell01feaee2011-02-10 15:05:05 -0800166 mContainerView = (ActionBarContainer) decor.findViewById(
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800167 com.android.internal.R.id.action_bar_container);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700168 mTopVisibilityView = (ViewGroup)decor.findViewById(
169 com.android.internal.R.id.top_action_bar);
170 if (mTopVisibilityView == null) {
171 mTopVisibilityView = mContainerView;
172 }
Adam Powell640a66e2011-04-29 10:18:53 -0700173 mSplitView = (ActionBarContainer) decor.findViewById(
174 com.android.internal.R.id.split_action_bar);
Steve Block08f194b2010-08-24 18:20:48 +0100175
Adam Powell640a66e2011-04-29 10:18:53 -0700176 if (mActionView == null || mContextView == null || mContainerView == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700177 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
178 "with a compatible window decor layout");
179 }
Adam Powell0e94b512010-06-29 17:58:20 -0700180
Adam Powell640a66e2011-04-29 10:18:53 -0700181 mActionView.setContextView(mContextView);
Adam Powell9b4bee02011-04-27 19:24:47 -0700182 mContextDisplayMode = mActionView.isSplitActionBar() ?
183 CONTEXT_DISPLAY_SPLIT : CONTEXT_DISPLAY_NORMAL;
Adam Powelldae78242011-04-25 15:23:41 -0700184
Adam Powellc29f4e52011-07-13 20:40:52 -0700185 // Older apps get the home button interaction enabled by default.
186 // Newer apps need to enable it explicitly.
Adam Powell28fa07e2011-07-15 20:17:25 -0700187 setHomeButtonEnabled(mContext.getApplicationInfo().targetSdkVersion <
188 Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Adam Powellf5645cb2011-08-10 22:49:02 -0700189
190 setHasEmbeddedTabs(mContext.getResources().getBoolean(
191 com.android.internal.R.bool.action_bar_embed_tabs));
Adam Powellf8ac6b72011-05-23 18:14:09 -0700192 }
193
194 public void onConfigurationChanged(Configuration newConfig) {
Adam Powellf5645cb2011-08-10 22:49:02 -0700195 setHasEmbeddedTabs(mContext.getResources().getBoolean(
196 com.android.internal.R.bool.action_bar_embed_tabs));
197 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700198
Adam Powellf5645cb2011-08-10 22:49:02 -0700199 private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) {
200 mHasEmbeddedTabs = hasEmbeddedTabs;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700201 // Switch tab layout configuration if needed
202 if (!mHasEmbeddedTabs) {
203 mActionView.setEmbeddedTabView(null);
204 mContainerView.setTabContainer(mTabScrollView);
205 } else {
206 mContainerView.setTabContainer(null);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700207 mActionView.setEmbeddedTabView(mTabScrollView);
208 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700209 final boolean isInTabMode = getNavigationMode() == NAVIGATION_MODE_TABS;
210 if (mTabScrollView != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700211 if (isInTabMode) {
212 mTabScrollView.setVisibility(View.VISIBLE);
213 if (mOverlayLayout != null) {
214 mOverlayLayout.requestFitSystemWindows();
215 }
216 } else {
217 mTabScrollView.setVisibility(View.GONE);
218 }
Adam Powellf5645cb2011-08-10 22:49:02 -0700219 }
220 mActionView.setCollapsable(!mHasEmbeddedTabs && isInTabMode);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700221 }
222
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700223 public boolean hasNonEmbeddedTabs() {
224 return !mHasEmbeddedTabs && getNavigationMode() == NAVIGATION_MODE_TABS;
225 }
226
Adam Powellf8ac6b72011-05-23 18:14:09 -0700227 private void ensureTabsExist() {
228 if (mTabScrollView != null) {
229 return;
230 }
231
Adam Powellf5645cb2011-08-10 22:49:02 -0700232 ScrollingTabContainerView tabScroller = new ScrollingTabContainerView(mContext);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700233
234 if (mHasEmbeddedTabs) {
235 tabScroller.setVisibility(View.VISIBLE);
236 mActionView.setEmbeddedTabView(tabScroller);
237 } else {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700238 if (getNavigationMode() == NAVIGATION_MODE_TABS) {
239 tabScroller.setVisibility(View.VISIBLE);
240 if (mOverlayLayout != null) {
241 mOverlayLayout.requestFitSystemWindows();
242 }
243 } else {
244 tabScroller.setVisibility(View.GONE);
245 }
Adam Powelldae78242011-04-25 15:23:41 -0700246 mContainerView.setTabContainer(tabScroller);
Adam Powelldae78242011-04-25 15:23:41 -0700247 }
Adam Powellf8ac6b72011-05-23 18:14:09 -0700248 mTabScrollView = tabScroller;
Adam Powell89e06452010-06-23 20:24:52 -0700249 }
250
Adam Powell060e3ca2011-07-19 20:39:16 -0700251 void completeDeferredDestroyActionMode() {
252 if (mDeferredModeDestroyCallback != null) {
253 mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode);
254 mDeferredDestroyActionMode = null;
255 mDeferredModeDestroyCallback = null;
256 }
257 }
258
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700259 public void setWindowVisibility(int visibility) {
260 mCurWindowVisibility = visibility;
261 }
262
Adam Powell50efbed2011-02-08 16:20:15 -0800263 /**
264 * Enables or disables animation between show/hide states.
265 * If animation is disabled using this method, animations in progress
266 * will be finished.
267 *
268 * @param enabled true to animate, false to not animate.
269 */
270 public void setShowHideAnimationEnabled(boolean enabled) {
271 mShowHideAnimationEnabled = enabled;
Adam Powell07e1f982011-03-24 11:11:15 -0700272 if (!enabled && mCurrentShowAnim != null) {
273 mCurrentShowAnim.end();
Adam Powell50efbed2011-02-08 16:20:15 -0800274 }
275 }
276
Adam Powell8515ee82010-11-30 14:09:55 -0800277 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
278 mMenuVisibilityListeners.add(listener);
279 }
280
281 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
282 mMenuVisibilityListeners.remove(listener);
283 }
284
285 public void dispatchMenuVisibilityChanged(boolean isVisible) {
286 if (isVisible == mLastMenuVisibility) {
287 return;
288 }
289 mLastMenuVisibility = isVisible;
290
291 final int count = mMenuVisibilityListeners.size();
292 for (int i = 0; i < count; i++) {
293 mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible);
294 }
295 }
296
Adam Powella66c7b02010-07-28 15:28:25 -0700297 @Override
Adam Powell3f476b32011-01-03 19:25:36 -0800298 public void setCustomView(int resId) {
Adam Powellf2423682011-08-11 14:29:45 -0700299 setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId, mActionView, false));
Adam Powell3f476b32011-01-03 19:25:36 -0800300 }
301
302 @Override
303 public void setDisplayUseLogoEnabled(boolean useLogo) {
304 setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO);
305 }
306
307 @Override
308 public void setDisplayShowHomeEnabled(boolean showHome) {
309 setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME);
310 }
311
312 @Override
313 public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
314 setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
315 }
316
317 @Override
318 public void setDisplayShowTitleEnabled(boolean showTitle) {
319 setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE);
320 }
321
322 @Override
323 public void setDisplayShowCustomEnabled(boolean showCustom) {
324 setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM);
325 }
326
327 @Override
Adam Powellc29f4e52011-07-13 20:40:52 -0700328 public void setHomeButtonEnabled(boolean enable) {
329 mActionView.setHomeButtonEnabled(enable);
Adam Powelldae78242011-04-25 15:23:41 -0700330 }
331
332 @Override
Adam Powella66c7b02010-07-28 15:28:25 -0700333 public void setTitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700334 setTitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700335 }
336
337 @Override
338 public void setSubtitle(int resId) {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700339 setSubtitle(mContext.getString(resId));
Adam Powella66c7b02010-07-28 15:28:25 -0700340 }
341
Adam Powell17809772010-07-21 13:25:11 -0700342 public void setSelectedNavigationItem(int position) {
343 switch (mActionView.getNavigationMode()) {
344 case NAVIGATION_MODE_TABS:
345 selectTab(mTabs.get(position));
346 break;
Adam Powell9ab97872010-10-26 21:47:29 -0700347 case NAVIGATION_MODE_LIST:
Adam Powell17809772010-07-21 13:25:11 -0700348 mActionView.setDropdownSelectedPosition(position);
349 break;
350 default:
351 throw new IllegalStateException(
Adam Powell9ab97872010-10-26 21:47:29 -0700352 "setSelectedNavigationIndex not valid for current navigation mode");
Adam Powell17809772010-07-21 13:25:11 -0700353 }
354 }
355
Adam Powell9ab97872010-10-26 21:47:29 -0700356 public void removeAllTabs() {
357 cleanupTabs();
Adam Powell17809772010-07-21 13:25:11 -0700358 }
359
Adam Powell661c9082010-07-02 10:09:44 -0700360 private void cleanupTabs() {
361 if (mSelectedTab != null) {
362 selectTab(null);
363 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700364 mTabs.clear();
Adam Powelld21aa122011-05-27 13:09:52 -0700365 if (mTabScrollView != null) {
366 mTabScrollView.removeAllTabs();
367 }
Adam Powell0c24a552010-11-03 16:44:11 -0700368 mSavedTabPosition = INVALID_POSITION;
Adam Powell661c9082010-07-02 10:09:44 -0700369 }
370
Adam Powell0e94b512010-06-29 17:58:20 -0700371 public void setTitle(CharSequence title) {
372 mActionView.setTitle(title);
373 }
374
375 public void setSubtitle(CharSequence subtitle) {
376 mActionView.setSubtitle(subtitle);
377 }
378
Adam Powell89e06452010-06-23 20:24:52 -0700379 public void setDisplayOptions(int options) {
Adam Powelldd8fab22012-03-22 17:47:27 -0700380 if ((options & DISPLAY_HOME_AS_UP) != 0) {
381 mDisplayHomeAsUpSet = true;
382 }
Adam Powell89e06452010-06-23 20:24:52 -0700383 mActionView.setDisplayOptions(options);
384 }
Adam Powell0e94b512010-06-29 17:58:20 -0700385
Adam Powell89e06452010-06-23 20:24:52 -0700386 public void setDisplayOptions(int options, int mask) {
387 final int current = mActionView.getDisplayOptions();
Adam Powelldd8fab22012-03-22 17:47:27 -0700388 if ((mask & DISPLAY_HOME_AS_UP) != 0) {
389 mDisplayHomeAsUpSet = true;
390 }
Adam Powell89e06452010-06-23 20:24:52 -0700391 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
392 }
393
394 public void setBackgroundDrawable(Drawable d) {
Adam Powellf88b9152011-09-07 14:54:32 -0700395 mContainerView.setPrimaryBackground(d);
396 }
397
398 public void setStackedBackgroundDrawable(Drawable d) {
399 mContainerView.setStackedBackground(d);
400 }
401
402 public void setSplitBackgroundDrawable(Drawable d) {
403 if (mSplitView != null) {
404 mSplitView.setSplitBackground(d);
405 }
Adam Powell89e06452010-06-23 20:24:52 -0700406 }
407
Adam Powellef704442010-11-16 14:48:22 -0800408 public View getCustomView() {
Adam Powell89e06452010-06-23 20:24:52 -0700409 return mActionView.getCustomNavigationView();
410 }
411
412 public CharSequence getTitle() {
413 return mActionView.getTitle();
414 }
415
416 public CharSequence getSubtitle() {
417 return mActionView.getSubtitle();
418 }
419
420 public int getNavigationMode() {
421 return mActionView.getNavigationMode();
422 }
423
424 public int getDisplayOptions() {
425 return mActionView.getDisplayOptions();
426 }
427
Adam Powell5d279772010-07-27 16:34:07 -0700428 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700429 boolean wasHidden = false;
Adam Powell5d279772010-07-27 16:34:07 -0700430 if (mActionMode != null) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700431 wasHidden = mWasHiddenBeforeMode;
Adam Powell5d279772010-07-27 16:34:07 -0700432 mActionMode.finish();
Adam Powell6e346362010-07-23 10:18:23 -0700433 }
Adam Powell3461b322010-07-14 11:34:43 -0700434
Adam Powell640a66e2011-04-29 10:18:53 -0700435 mContextView.killMode();
Adam Powell5ee36c42011-06-02 12:59:43 -0700436 ActionModeImpl mode = new ActionModeImpl(callback);
437 if (mode.dispatchOnCreate()) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700438 mWasHiddenBeforeMode = !isShowing() || wasHidden;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700439 mode.invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700440 mContextView.initForMode(mode);
441 animateToMode(true);
Adam Powell1ab418a2011-06-09 20:49:49 -0700442 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell89e06452010-06-23 20:24:52 -0700443 // TODO animate this
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700444 if (mSplitView.getVisibility() != View.VISIBLE) {
445 mSplitView.setVisibility(View.VISIBLE);
446 if (mOverlayLayout != null) {
447 mOverlayLayout.requestFitSystemWindows();
448 }
449 }
Adam Powell89e06452010-06-23 20:24:52 -0700450 }
Adam Powell86ed4362011-09-14 16:18:53 -0700451 mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powell5d279772010-07-27 16:34:07 -0700452 mActionMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700453 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700454 }
Adam Powellac695c62010-07-20 18:19:27 -0700455 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700456 }
457
Adam Powell661c9082010-07-02 10:09:44 -0700458 private void configureTab(Tab tab, int position) {
459 final TabImpl tabi = (TabImpl) tab;
Adam Powell2b6230e2010-09-07 17:55:25 -0700460 final ActionBar.TabListener callback = tabi.getCallback();
461
462 if (callback == null) {
463 throw new IllegalStateException("Action Bar Tab must have a Callback");
464 }
Adam Powell661c9082010-07-02 10:09:44 -0700465
466 tabi.setPosition(position);
467 mTabs.add(position, tabi);
468
Adam Powell81b89442010-11-02 17:58:56 -0700469 final int count = mTabs.size();
470 for (int i = position + 1; i < count; i++) {
471 mTabs.get(i).setPosition(i);
Adam Powell661c9082010-07-02 10:09:44 -0700472 }
Adam Powell661c9082010-07-02 10:09:44 -0700473 }
474
475 @Override
476 public void addTab(Tab tab) {
Adam Powell81b89442010-11-02 17:58:56 -0700477 addTab(tab, mTabs.isEmpty());
Adam Powell661c9082010-07-02 10:09:44 -0700478 }
479
480 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700481 public void addTab(Tab tab, int position) {
Adam Powell81b89442010-11-02 17:58:56 -0700482 addTab(tab, position, mTabs.isEmpty());
483 }
484
485 @Override
486 public void addTab(Tab tab, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700487 ensureTabsExist();
488 mTabScrollView.addTab(tab, setSelected);
Adam Powell81b89442010-11-02 17:58:56 -0700489 configureTab(tab, mTabs.size());
490 if (setSelected) {
491 selectTab(tab);
492 }
493 }
494
495 @Override
496 public void addTab(Tab tab, int position, boolean setSelected) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700497 ensureTabsExist();
498 mTabScrollView.addTab(tab, position, setSelected);
Adam Powell661c9082010-07-02 10:09:44 -0700499 configureTab(tab, position);
Adam Powell81b89442010-11-02 17:58:56 -0700500 if (setSelected) {
501 selectTab(tab);
502 }
Adam Powell661c9082010-07-02 10:09:44 -0700503 }
504
505 @Override
506 public Tab newTab() {
507 return new TabImpl();
508 }
509
510 @Override
511 public void removeTab(Tab tab) {
512 removeTabAt(tab.getPosition());
513 }
514
515 @Override
516 public void removeTabAt(int position) {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700517 if (mTabScrollView == null) {
518 // No tabs around to remove
519 return;
520 }
521
Adam Powell0c24a552010-11-03 16:44:11 -0700522 int selectedTabPosition = mSelectedTab != null
523 ? mSelectedTab.getPosition() : mSavedTabPosition;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700524 mTabScrollView.removeTabAt(position);
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700525 TabImpl removedTab = mTabs.remove(position);
526 if (removedTab != null) {
527 removedTab.setPosition(-1);
528 }
Adam Powell661c9082010-07-02 10:09:44 -0700529
530 final int newTabCount = mTabs.size();
531 for (int i = position; i < newTabCount; i++) {
532 mTabs.get(i).setPosition(i);
533 }
534
Adam Powell0c24a552010-11-03 16:44:11 -0700535 if (selectedTabPosition == position) {
536 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
537 }
Adam Powell661c9082010-07-02 10:09:44 -0700538 }
539
540 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700541 public void selectTab(Tab tab) {
Adam Powell0c24a552010-11-03 16:44:11 -0700542 if (getNavigationMode() != NAVIGATION_MODE_TABS) {
543 mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
544 return;
545 }
546
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800547 final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction()
Adam Powell0c24a552010-11-03 16:44:11 -0700548 .disallowAddToBackStack();
Adam Powell7f9b9052010-10-19 16:56:07 -0700549
550 if (mSelectedTab == tab) {
551 if (mSelectedTab != null) {
552 mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans);
Adam Powellf8ac6b72011-05-23 18:14:09 -0700553 mTabScrollView.animateToTab(tab.getPosition());
Adam Powell7f9b9052010-10-19 16:56:07 -0700554 }
555 } else {
Adam Powellf8ac6b72011-05-23 18:14:09 -0700556 mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
Adam Powell7f9b9052010-10-19 16:56:07 -0700557 if (mSelectedTab != null) {
558 mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans);
559 }
560 mSelectedTab = (TabImpl) tab;
561 if (mSelectedTab != null) {
562 mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans);
563 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700564 }
565
566 if (!trans.isEmpty()) {
567 trans.commit();
568 }
569 }
570
571 @Override
572 public Tab getSelectedTab() {
573 return mSelectedTab;
Adam Powell661c9082010-07-02 10:09:44 -0700574 }
575
Adam Powell6b336f82010-08-10 20:13:01 -0700576 @Override
577 public int getHeight() {
Adam Powell58c5dc12011-07-14 21:08:10 -0700578 return mContainerView.getHeight();
Adam Powell6b336f82010-08-10 20:13:01 -0700579 }
580
581 @Override
582 public void show() {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700583 show(true, false);
Adam Powell07e1f982011-03-24 11:11:15 -0700584 }
585
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700586 public void show(boolean markHiddenBeforeMode, boolean alwaysAnimate) {
Adam Powell07e1f982011-03-24 11:11:15 -0700587 if (mCurrentShowAnim != null) {
588 mCurrentShowAnim.end();
Adam Powell45f1e082010-12-10 14:20:13 -0800589 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700590 if (mTopVisibilityView.getVisibility() == View.VISIBLE) {
Adam Powell07e1f982011-03-24 11:11:15 -0700591 if (markHiddenBeforeMode) mWasHiddenBeforeMode = false;
Adam Powelld76cee22011-01-31 15:05:05 -0800592 return;
593 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700594 mTopVisibilityView.setVisibility(View.VISIBLE);
Adam Powell50efbed2011-02-08 16:20:15 -0800595
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700596 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
597 || alwaysAnimate)) {
598 mTopVisibilityView.setAlpha(0);
Adam Powell50efbed2011-02-08 16:20:15 -0800599 AnimatorSet anim = new AnimatorSet();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700600 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView, "alpha", 1));
Adam Powell50efbed2011-02-08 16:20:15 -0800601 if (mContentView != null) {
602 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700603 -mTopVisibilityView.getHeight(), 0));
604 mTopVisibilityView.setTranslationY(-mTopVisibilityView.getHeight());
605 b.with(ObjectAnimator.ofFloat(mTopVisibilityView, "translationY", 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800606 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700607 if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) {
Adam Powell640a66e2011-04-29 10:18:53 -0700608 mSplitView.setAlpha(0);
Adam Powell993a63a2011-08-18 16:35:53 -0700609 mSplitView.setVisibility(View.VISIBLE);
Adam Powell640a66e2011-04-29 10:18:53 -0700610 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 1));
611 }
Adam Powell50efbed2011-02-08 16:20:15 -0800612 anim.addListener(mShowListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700613 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800614 anim.start();
615 } else {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700616 mTopVisibilityView.setAlpha(1);
Adam Powelld8145042011-03-24 14:18:27 -0700617 mContainerView.setTranslationY(0);
Adam Powell50efbed2011-02-08 16:20:15 -0800618 mShowListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800619 }
Adam Powell6b336f82010-08-10 20:13:01 -0700620 }
621
622 @Override
623 public void hide() {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700624 hide(false);
625 }
626
627 public void hide(boolean alwaysAnimate) {
Adam Powell07e1f982011-03-24 11:11:15 -0700628 if (mCurrentShowAnim != null) {
629 mCurrentShowAnim.end();
Adam Powelle6ec7322010-12-07 15:29:26 -0800630 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700631 if (mTopVisibilityView.getVisibility() == View.GONE) {
Adam Powelle6ec7322010-12-07 15:29:26 -0800632 return;
633 }
Adam Powell50efbed2011-02-08 16:20:15 -0800634
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700635 if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
636 || alwaysAnimate)) {
637 mTopVisibilityView.setAlpha(1);
Adam Powell01feaee2011-02-10 15:05:05 -0800638 mContainerView.setTransitioning(true);
Adam Powell50efbed2011-02-08 16:20:15 -0800639 AnimatorSet anim = new AnimatorSet();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700640 AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView, "alpha", 0));
Adam Powell50efbed2011-02-08 16:20:15 -0800641 if (mContentView != null) {
642 b.with(ObjectAnimator.ofFloat(mContentView, "translationY",
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700643 0, -mTopVisibilityView.getHeight()));
644 b.with(ObjectAnimator.ofFloat(mTopVisibilityView, "translationY",
645 -mTopVisibilityView.getHeight()));
Adam Powell50efbed2011-02-08 16:20:15 -0800646 }
Adam Powell1ab418a2011-06-09 20:49:49 -0700647 if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
Adam Powell640a66e2011-04-29 10:18:53 -0700648 mSplitView.setAlpha(1);
649 b.with(ObjectAnimator.ofFloat(mSplitView, "alpha", 0));
650 }
Adam Powell50efbed2011-02-08 16:20:15 -0800651 anim.addListener(mHideListener);
Adam Powell07e1f982011-03-24 11:11:15 -0700652 mCurrentShowAnim = anim;
Adam Powell50efbed2011-02-08 16:20:15 -0800653 anim.start();
654 } else {
655 mHideListener.onAnimationEnd(null);
Adam Powelle6ec7322010-12-07 15:29:26 -0800656 }
Adam Powell6b336f82010-08-10 20:13:01 -0700657 }
658
659 public boolean isShowing() {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700660 return mTopVisibilityView.getVisibility() == View.VISIBLE;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800661 }
662
Adam Powell640a66e2011-04-29 10:18:53 -0700663 void animateToMode(boolean toActionMode) {
Adam Powell060e3ca2011-07-19 20:39:16 -0700664 if (toActionMode) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700665 show(false, false);
Adam Powell060e3ca2011-07-19 20:39:16 -0700666 }
Adam Powell07e1f982011-03-24 11:11:15 -0700667 if (mCurrentModeAnim != null) {
668 mCurrentModeAnim.end();
669 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800670
Adam Powell640a66e2011-04-29 10:18:53 -0700671 mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
672 mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700673 if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) {
674 mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE);
675 }
Adam Powell6b336f82010-08-10 20:13:01 -0700676 }
677
Adam Powell88ab6972011-07-28 11:25:01 -0700678 public Context getThemedContext() {
679 if (mThemedContext == null) {
680 TypedValue outValue = new TypedValue();
681 Resources.Theme currentTheme = mContext.getTheme();
682 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
683 outValue, true);
684 final int targetThemeRes = outValue.resourceId;
685
686 if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
687 mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
688 } else {
689 mThemedContext = mContext;
690 }
691 }
692 return mThemedContext;
693 }
694
Adam Powell89e06452010-06-23 20:24:52 -0700695 /**
696 * @hide
697 */
Adam Powell5d279772010-07-27 16:34:07 -0700698 public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback {
Adam Powell6e346362010-07-23 10:18:23 -0700699 private ActionMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700700 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700701 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700702
Adam Powell5d279772010-07-27 16:34:07 -0700703 public ActionModeImpl(ActionMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700704 mCallback = callback;
Adam Powellf2423682011-08-11 14:29:45 -0700705 mMenu = new MenuBuilder(getThemedContext())
Adam Powell4d9861e2010-08-17 11:14:40 -0700706 .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700707 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700708 }
Adam Powell9168f0b2010-08-02 15:46:24 -0700709
710 @Override
711 public MenuInflater getMenuInflater() {
Adam Powellf2423682011-08-11 14:29:45 -0700712 return new MenuInflater(getThemedContext());
Adam Powell9168f0b2010-08-02 15:46:24 -0700713 }
714
Adam Powell89e06452010-06-23 20:24:52 -0700715 @Override
716 public Menu getMenu() {
717 return mMenu;
718 }
719
720 @Override
721 public void finish() {
Adam Powell5d279772010-07-27 16:34:07 -0700722 if (mActionMode != this) {
723 // Not the active action mode - no-op
Adam Powell93b6bc32010-07-22 11:36:35 -0700724 return;
725 }
726
Adam Powell060e3ca2011-07-19 20:39:16 -0700727 // If we were hidden before the mode was shown, defer the onDestroy
728 // callback until the animation is finished and associated relayout
729 // is about to happen. This lets apps better anticipate visibility
730 // and layout behavior.
731 if (mWasHiddenBeforeMode) {
732 mDeferredDestroyActionMode = this;
733 mDeferredModeDestroyCallback = mCallback;
734 } else {
735 mCallback.onDestroyActionMode(this);
736 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800737 mCallback = null;
Adam Powell640a66e2011-04-29 10:18:53 -0700738 animateToMode(false);
Adam Powell0e94b512010-06-29 17:58:20 -0700739
740 // Clear out the context mode views after the animation finishes
Adam Powell640a66e2011-04-29 10:18:53 -0700741 mContextView.closeMode();
Adam Powell86ed4362011-09-14 16:18:53 -0700742 mActionView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adam Powellf6ce6a92011-06-29 10:25:01 -0700743
Adam Powell5d279772010-07-27 16:34:07 -0700744 mActionMode = null;
Adam Powell07e1f982011-03-24 11:11:15 -0700745
746 if (mWasHiddenBeforeMode) {
747 hide();
748 }
Adam Powell89e06452010-06-23 20:24:52 -0700749 }
750
751 @Override
752 public void invalidate() {
Adam Powell5ee36c42011-06-02 12:59:43 -0700753 mMenu.stopDispatchingItemsChanged();
754 try {
755 mCallback.onPrepareActionMode(this, mMenu);
756 } finally {
757 mMenu.startDispatchingItemsChanged();
758 }
759 }
760
761 public boolean dispatchOnCreate() {
762 mMenu.stopDispatchingItemsChanged();
763 try {
764 return mCallback.onCreateActionMode(this, mMenu);
765 } finally {
766 mMenu.startDispatchingItemsChanged();
Adam Powell89e06452010-06-23 20:24:52 -0700767 }
768 }
769
770 @Override
771 public void setCustomView(View view) {
Adam Powell640a66e2011-04-29 10:18:53 -0700772 mContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700773 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700774 }
775
776 @Override
777 public void setSubtitle(CharSequence subtitle) {
Adam Powell640a66e2011-04-29 10:18:53 -0700778 mContextView.setSubtitle(subtitle);
Adam Powell89e06452010-06-23 20:24:52 -0700779 }
780
781 @Override
782 public void setTitle(CharSequence title) {
Adam Powell640a66e2011-04-29 10:18:53 -0700783 mContextView.setTitle(title);
Adam Powell89e06452010-06-23 20:24:52 -0700784 }
Adam Powell29ed7572010-07-14 16:24:56 -0700785
786 @Override
Adam Powellc9ae2a22010-07-28 14:44:21 -0700787 public void setTitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800788 setTitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700789 }
790
791 @Override
792 public void setSubtitle(int resId) {
Adam Powell48e8ac32011-01-14 12:10:24 -0800793 setSubtitle(mContext.getResources().getString(resId));
Adam Powellc9ae2a22010-07-28 14:44:21 -0700794 }
795
796 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700797 public CharSequence getTitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700798 return mContextView.getTitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700799 }
800
801 @Override
802 public CharSequence getSubtitle() {
Adam Powell640a66e2011-04-29 10:18:53 -0700803 return mContextView.getSubtitle();
Adam Powell29ed7572010-07-14 16:24:56 -0700804 }
Adam Powell89e06452010-06-23 20:24:52 -0700805
Adam Powell29ed7572010-07-14 16:24:56 -0700806 @Override
Adam Powellb98a81f2012-02-24 11:09:07 -0800807 public void setTitleOptionalHint(boolean titleOptional) {
808 mContextView.setTitleOptional(titleOptional);
809 }
810
811 @Override
812 public boolean isTitleOptional() {
813 return mContextView.isTitleOptional();
814 }
815
816 @Override
Adam Powell29ed7572010-07-14 16:24:56 -0700817 public View getCustomView() {
818 return mCustomView != null ? mCustomView.get() : null;
819 }
820
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700821 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800822 if (mCallback != null) {
823 return mCallback.onActionItemClicked(this, item);
824 } else {
825 return false;
826 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700827 }
828
829 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
830 }
831
832 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800833 if (mCallback == null) {
834 return false;
835 }
836
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700837 if (!subMenu.hasVisibleItems()) {
838 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700839 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700840
Adam Powellf2423682011-08-11 14:29:45 -0700841 new MenuPopupHelper(getThemedContext(), subMenu).show();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700842 return true;
843 }
844
845 public void onCloseSubMenu(SubMenuBuilder menu) {
846 }
847
848 public void onMenuModeChange(MenuBuilder menu) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800849 if (mCallback == null) {
850 return;
851 }
Adam Powellf6148c52010-08-11 21:10:16 -0700852 invalidate();
Adam Powell640a66e2011-04-29 10:18:53 -0700853 mContextView.showOverflowMenu();
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700854 }
Adam Powell661c9082010-07-02 10:09:44 -0700855 }
856
857 /**
858 * @hide
859 */
860 public class TabImpl extends ActionBar.Tab {
Adam Powell2b6230e2010-09-07 17:55:25 -0700861 private ActionBar.TabListener mCallback;
862 private Object mTag;
Adam Powell661c9082010-07-02 10:09:44 -0700863 private Drawable mIcon;
864 private CharSequence mText;
Adam Powell94e56ef2011-09-06 21:22:22 -0700865 private CharSequence mContentDesc;
Adam Powell0d8ec1d2011-05-03 14:49:13 -0700866 private int mPosition = -1;
Adam Powell2b6230e2010-09-07 17:55:25 -0700867 private View mCustomView;
Adam Powell661c9082010-07-02 10:09:44 -0700868
869 @Override
Adam Powell2b6230e2010-09-07 17:55:25 -0700870 public Object getTag() {
871 return mTag;
872 }
873
874 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700875 public Tab setTag(Object tag) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700876 mTag = tag;
Adam Powell9ab97872010-10-26 21:47:29 -0700877 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700878 }
879
880 public ActionBar.TabListener getCallback() {
881 return mCallback;
882 }
883
884 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700885 public Tab setTabListener(ActionBar.TabListener callback) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700886 mCallback = callback;
Adam Powell9ab97872010-10-26 21:47:29 -0700887 return this;
Adam Powell2b6230e2010-09-07 17:55:25 -0700888 }
889
890 @Override
891 public View getCustomView() {
892 return mCustomView;
893 }
894
895 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700896 public Tab setCustomView(View view) {
Adam Powell2b6230e2010-09-07 17:55:25 -0700897 mCustomView = view;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700898 if (mPosition >= 0) {
899 mTabScrollView.updateTab(mPosition);
900 }
Adam Powell9ab97872010-10-26 21:47:29 -0700901 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700902 }
903
904 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800905 public Tab setCustomView(int layoutResId) {
Adam Powellf2423682011-08-11 14:29:45 -0700906 return setCustomView(LayoutInflater.from(getThemedContext())
907 .inflate(layoutResId, null));
Adam Powell32555f32010-11-17 13:49:22 -0800908 }
909
910 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700911 public Drawable getIcon() {
912 return mIcon;
913 }
914
915 @Override
916 public int getPosition() {
917 return mPosition;
918 }
919
920 public void setPosition(int position) {
921 mPosition = position;
922 }
923
924 @Override
925 public CharSequence getText() {
926 return mText;
927 }
928
929 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700930 public Tab setIcon(Drawable icon) {
Adam Powell661c9082010-07-02 10:09:44 -0700931 mIcon = icon;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700932 if (mPosition >= 0) {
933 mTabScrollView.updateTab(mPosition);
934 }
Adam Powell9ab97872010-10-26 21:47:29 -0700935 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700936 }
937
938 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800939 public Tab setIcon(int resId) {
940 return setIcon(mContext.getResources().getDrawable(resId));
941 }
942
943 @Override
Adam Powell9ab97872010-10-26 21:47:29 -0700944 public Tab setText(CharSequence text) {
Adam Powell661c9082010-07-02 10:09:44 -0700945 mText = text;
Adam Powellf8ac6b72011-05-23 18:14:09 -0700946 if (mPosition >= 0) {
947 mTabScrollView.updateTab(mPosition);
948 }
Adam Powell9ab97872010-10-26 21:47:29 -0700949 return this;
Adam Powell661c9082010-07-02 10:09:44 -0700950 }
951
952 @Override
Adam Powell32555f32010-11-17 13:49:22 -0800953 public Tab setText(int resId) {
954 return setText(mContext.getResources().getText(resId));
955 }
956
957 @Override
Adam Powell661c9082010-07-02 10:09:44 -0700958 public void select() {
959 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700960 }
Adam Powell94e56ef2011-09-06 21:22:22 -0700961
962 @Override
963 public Tab setContentDescription(int resId) {
964 return setContentDescription(mContext.getResources().getText(resId));
965 }
966
967 @Override
968 public Tab setContentDescription(CharSequence contentDesc) {
969 mContentDesc = contentDesc;
970 if (mPosition >= 0) {
971 mTabScrollView.updateTab(mPosition);
972 }
973 return this;
974 }
975
976 @Override
977 public CharSequence getContentDescription() {
978 return mContentDesc;
979 }
Adam Powell89e06452010-06-23 20:24:52 -0700980 }
Adam Powell9ab97872010-10-26 21:47:29 -0700981
982 @Override
983 public void setCustomView(View view) {
984 mActionView.setCustomNavigationView(view);
985 }
986
987 @Override
988 public void setCustomView(View view, LayoutParams layoutParams) {
989 view.setLayoutParams(layoutParams);
990 mActionView.setCustomNavigationView(view);
991 }
992
993 @Override
Adam Powell8515ee82010-11-30 14:09:55 -0800994 public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) {
Adam Powell9ab97872010-10-26 21:47:29 -0700995 mActionView.setDropdownAdapter(adapter);
996 mActionView.setCallback(callback);
997 }
998
999 @Override
1000 public int getSelectedNavigationIndex() {
1001 switch (mActionView.getNavigationMode()) {
1002 case NAVIGATION_MODE_TABS:
Adam Powell04587962010-11-11 22:15:07 -08001003 return mSelectedTab != null ? mSelectedTab.getPosition() : -1;
Adam Powell9ab97872010-10-26 21:47:29 -07001004 case NAVIGATION_MODE_LIST:
1005 return mActionView.getDropdownSelectedPosition();
1006 default:
1007 return -1;
1008 }
1009 }
1010
1011 @Override
1012 public int getNavigationItemCount() {
1013 switch (mActionView.getNavigationMode()) {
1014 case NAVIGATION_MODE_TABS:
1015 return mTabs.size();
1016 case NAVIGATION_MODE_LIST:
1017 SpinnerAdapter adapter = mActionView.getDropdownAdapter();
1018 return adapter != null ? adapter.getCount() : 0;
1019 default:
1020 return 0;
1021 }
1022 }
1023
1024 @Override
Adam Powell0c24a552010-11-03 16:44:11 -07001025 public int getTabCount() {
1026 return mTabs.size();
1027 }
1028
1029 @Override
Adam Powell9ab97872010-10-26 21:47:29 -07001030 public void setNavigationMode(int mode) {
Adam Powell0c24a552010-11-03 16:44:11 -07001031 final int oldMode = mActionView.getNavigationMode();
1032 switch (oldMode) {
1033 case NAVIGATION_MODE_TABS:
1034 mSavedTabPosition = getSelectedNavigationIndex();
1035 selectTab(null);
Adam Powellf5645cb2011-08-10 22:49:02 -07001036 mTabScrollView.setVisibility(View.GONE);
Adam Powell0c24a552010-11-03 16:44:11 -07001037 break;
1038 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001039 if (oldMode != mode && !mHasEmbeddedTabs) {
1040 if (mOverlayLayout != null) {
1041 mOverlayLayout.requestFitSystemWindows();
1042 }
1043 }
Adam Powell9ab97872010-10-26 21:47:29 -07001044 mActionView.setNavigationMode(mode);
Adam Powell0c24a552010-11-03 16:44:11 -07001045 switch (mode) {
1046 case NAVIGATION_MODE_TABS:
Adam Powellf8ac6b72011-05-23 18:14:09 -07001047 ensureTabsExist();
Adam Powellf5645cb2011-08-10 22:49:02 -07001048 mTabScrollView.setVisibility(View.VISIBLE);
Adam Powell0c24a552010-11-03 16:44:11 -07001049 if (mSavedTabPosition != INVALID_POSITION) {
1050 setSelectedNavigationItem(mSavedTabPosition);
1051 mSavedTabPosition = INVALID_POSITION;
1052 }
1053 break;
1054 }
Adam Powelld21aa122011-05-27 13:09:52 -07001055 mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs);
Adam Powell9ab97872010-10-26 21:47:29 -07001056 }
1057
1058 @Override
1059 public Tab getTabAt(int index) {
1060 return mTabs.get(index);
1061 }
Adam Powell0c24a552010-11-03 16:44:11 -07001062
Adam Powell0c24a552010-11-03 16:44:11 -07001063
Adam Powell1969b872011-03-22 11:52:48 -07001064 @Override
1065 public void setIcon(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001066 mActionView.setIcon(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001067 }
Adam Powell0c24a552010-11-03 16:44:11 -07001068
Adam Powell1969b872011-03-22 11:52:48 -07001069 @Override
1070 public void setIcon(Drawable icon) {
1071 mActionView.setIcon(icon);
1072 }
1073
1074 @Override
1075 public void setLogo(int resId) {
Adam Powell45c515b2011-04-21 18:50:20 -07001076 mActionView.setLogo(resId);
Adam Powell1969b872011-03-22 11:52:48 -07001077 }
1078
1079 @Override
1080 public void setLogo(Drawable logo) {
1081 mActionView.setLogo(logo);
Adam Powell0c24a552010-11-03 16:44:11 -07001082 }
Adam Powelldd8fab22012-03-22 17:47:27 -07001083
1084 public void setDefaultDisplayHomeAsUpEnabled(boolean enable) {
1085 if (!mDisplayHomeAsUpSet) {
1086 setDisplayHomeAsUpEnabled(enable);
1087 }
1088 }
Adam Powell89e06452010-06-23 20:24:52 -07001089}