Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 19 | import com.android.internal.view.menu.MenuBuilder; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 20 | import com.android.internal.view.menu.MenuPopupHelper; |
| 21 | import com.android.internal.view.menu.SubMenuBuilder; |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 22 | import com.android.internal.widget.ActionBarContainer; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 23 | import com.android.internal.widget.ActionBarContextView; |
| 24 | import com.android.internal.widget.ActionBarView; |
| 25 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 26 | import android.animation.Animator; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 27 | import android.animation.Animator.AnimatorListener; |
| 28 | import android.animation.AnimatorSet; |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 29 | import android.animation.ObjectAnimator; |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 30 | import android.animation.TimeInterpolator; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 31 | import android.app.ActionBar; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 32 | import android.app.Activity; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 33 | import android.app.Dialog; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 34 | import android.app.Fragment; |
| 35 | import android.app.FragmentTransaction; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 36 | import android.content.Context; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 37 | import android.graphics.drawable.Drawable; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 38 | import android.os.Handler; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 39 | import android.view.ActionMode; |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 40 | import android.view.LayoutInflater; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 41 | import android.view.Menu; |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 42 | import android.view.MenuInflater; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 43 | import android.view.MenuItem; |
| 44 | import android.view.View; |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 45 | import android.view.Window; |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 46 | import android.view.animation.DecelerateInterpolator; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 47 | import android.widget.FrameLayout; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 48 | import android.widget.LinearLayout; |
| 49 | import android.widget.SpinnerAdapter; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 50 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 51 | import java.lang.ref.WeakReference; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 54 | /** |
| 55 | * ActionBarImpl is the ActionBar implementation used |
| 56 | * by devices of all screen sizes. If it detects a compatible decor, |
| 57 | * it will split contextual modes across both the ActionBarView at |
| 58 | * the top of the screen and a horizontal LinearLayout at the bottom |
| 59 | * which is normally hidden. |
| 60 | */ |
| 61 | public class ActionBarImpl extends ActionBar { |
| 62 | private static final int NORMAL_VIEW = 0; |
| 63 | private static final int CONTEXT_VIEW = 1; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 64 | |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 65 | private Context mContext; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 66 | private Activity mActivity; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 67 | private Dialog mDialog; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 68 | |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 69 | private ActionBarContainer mContainerView; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 70 | private ActionBarView mActionView; |
| 71 | private ActionBarContextView mUpperContextView; |
| 72 | private LinearLayout mLowerContextView; |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 73 | private View mContentView; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 74 | |
| 75 | private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>(); |
| 76 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 77 | private TabImpl mSelectedTab; |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 78 | private int mSavedTabPosition = INVALID_POSITION; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 79 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 80 | private ActionMode mActionMode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 81 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 82 | private boolean mLastMenuVisibility; |
| 83 | private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners = |
| 84 | new ArrayList<OnMenuVisibilityListener>(); |
| 85 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 86 | private static final int CONTEXT_DISPLAY_NORMAL = 0; |
| 87 | private static final int CONTEXT_DISPLAY_SPLIT = 1; |
| 88 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 89 | private static final int INVALID_POSITION = -1; |
| 90 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 91 | private int mContextDisplayMode; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 92 | |
| 93 | final Handler mHandler = new Handler(); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 94 | |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 95 | private Animator mCurrentAnim; |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 96 | private boolean mShowHideAnimationEnabled; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 97 | |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 98 | private static final TimeInterpolator sFadeOutInterpolator = new DecelerateInterpolator(); |
| 99 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 100 | final AnimatorListener[] mAfterAnimation = new AnimatorListener[] { |
| 101 | new AnimatorListener() { // NORMAL_VIEW |
| 102 | @Override |
| 103 | public void onAnimationStart(Animator animation) { |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onAnimationEnd(Animator animation) { |
| 108 | if (mLowerContextView != null) { |
| 109 | mLowerContextView.removeAllViews(); |
| 110 | } |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 111 | mCurrentAnim = null; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 112 | hideAllExcept(NORMAL_VIEW); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void onAnimationCancel(Animator animation) { |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onAnimationRepeat(Animator animation) { |
| 121 | } |
| 122 | }, |
| 123 | new AnimatorListener() { // CONTEXT_VIEW |
| 124 | @Override |
| 125 | public void onAnimationStart(Animator animation) { |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void onAnimationEnd(Animator animation) { |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 130 | mCurrentAnim = null; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 131 | hideAllExcept(CONTEXT_VIEW); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void onAnimationCancel(Animator animation) { |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void onAnimationRepeat(Animator animation) { |
| 140 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 141 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 144 | final AnimatorListener mHideListener = new AnimatorListener() { |
| 145 | @Override |
| 146 | public void onAnimationStart(Animator animation) { |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void onAnimationEnd(Animator animation) { |
| 151 | if (mContentView != null) { |
| 152 | mContentView.setTranslationY(0); |
| 153 | } |
| 154 | mContainerView.setVisibility(View.GONE); |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 155 | mContainerView.setTransitioning(false); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 156 | mCurrentAnim = null; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onAnimationCancel(Animator animation) { |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void onAnimationRepeat(Animator animation) { |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | final AnimatorListener mShowListener = new AnimatorListener() { |
| 169 | @Override |
| 170 | public void onAnimationStart(Animator animation) { |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onAnimationEnd(Animator animation) { |
| 175 | mCurrentAnim = null; |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 176 | mContainerView.requestLayout(); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void onAnimationCancel(Animator animation) { |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void onAnimationRepeat(Animator animation) { |
| 185 | } |
| 186 | }; |
| 187 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 188 | public ActionBarImpl(Activity activity) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 189 | mActivity = activity; |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 190 | Window window = activity.getWindow(); |
| 191 | View decor = window.getDecorView(); |
| 192 | init(decor); |
| 193 | if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) { |
| 194 | mContentView = decor.findViewById(android.R.id.content); |
| 195 | } |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | public ActionBarImpl(Dialog dialog) { |
| 199 | mDialog = dialog; |
| 200 | init(dialog.getWindow().getDecorView()); |
| 201 | } |
| 202 | |
| 203 | private void init(View decor) { |
| 204 | mContext = decor.getContext(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 205 | mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar); |
| 206 | mUpperContextView = (ActionBarContextView) decor.findViewById( |
| 207 | com.android.internal.R.id.action_context_bar); |
| 208 | mLowerContextView = (LinearLayout) decor.findViewById( |
| 209 | com.android.internal.R.id.lower_action_context_bar); |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 210 | mContainerView = (ActionBarContainer) decor.findViewById( |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 211 | com.android.internal.R.id.action_bar_container); |
Steve Block | 08f194b | 2010-08-24 18:20:48 +0100 | [diff] [blame] | 212 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 213 | if (mActionView == null || mUpperContextView == null || mContainerView == null) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 214 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 215 | "with a compatible window decor layout"); |
| 216 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 217 | |
Steve Block | 08f194b | 2010-08-24 18:20:48 +0100 | [diff] [blame] | 218 | mActionView.setContextView(mUpperContextView); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 219 | mContextDisplayMode = mLowerContextView == null ? |
| 220 | CONTEXT_DISPLAY_NORMAL : CONTEXT_DISPLAY_SPLIT; |
| 221 | } |
| 222 | |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 223 | /** |
| 224 | * Enables or disables animation between show/hide states. |
| 225 | * If animation is disabled using this method, animations in progress |
| 226 | * will be finished. |
| 227 | * |
| 228 | * @param enabled true to animate, false to not animate. |
| 229 | */ |
| 230 | public void setShowHideAnimationEnabled(boolean enabled) { |
| 231 | mShowHideAnimationEnabled = enabled; |
| 232 | if (!enabled && mCurrentAnim != null) { |
| 233 | mCurrentAnim.end(); |
| 234 | } |
| 235 | } |
| 236 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 237 | public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) { |
| 238 | mMenuVisibilityListeners.add(listener); |
| 239 | } |
| 240 | |
| 241 | public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) { |
| 242 | mMenuVisibilityListeners.remove(listener); |
| 243 | } |
| 244 | |
| 245 | public void dispatchMenuVisibilityChanged(boolean isVisible) { |
| 246 | if (isVisible == mLastMenuVisibility) { |
| 247 | return; |
| 248 | } |
| 249 | mLastMenuVisibility = isVisible; |
| 250 | |
| 251 | final int count = mMenuVisibilityListeners.size(); |
| 252 | for (int i = 0; i < count; i++) { |
| 253 | mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible); |
| 254 | } |
| 255 | } |
| 256 | |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 257 | @Override |
Adam Powell | 3f476b3 | 2011-01-03 19:25:36 -0800 | [diff] [blame] | 258 | public void setCustomView(int resId) { |
| 259 | setCustomView(LayoutInflater.from(mContext).inflate(resId, mActionView, false)); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void setDisplayUseLogoEnabled(boolean useLogo) { |
| 264 | setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO); |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void setDisplayShowHomeEnabled(boolean showHome) { |
| 269 | setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) { |
| 274 | setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void setDisplayShowTitleEnabled(boolean showTitle) { |
| 279 | setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void setDisplayShowCustomEnabled(boolean showCustom) { |
| 284 | setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM); |
| 285 | } |
| 286 | |
| 287 | @Override |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 288 | public void setTitle(int resId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 289 | setTitle(mContext.getString(resId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void setSubtitle(int resId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 294 | setSubtitle(mContext.getString(resId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 297 | public void setSelectedNavigationItem(int position) { |
| 298 | switch (mActionView.getNavigationMode()) { |
| 299 | case NAVIGATION_MODE_TABS: |
| 300 | selectTab(mTabs.get(position)); |
| 301 | break; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 302 | case NAVIGATION_MODE_LIST: |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 303 | mActionView.setDropdownSelectedPosition(position); |
| 304 | break; |
| 305 | default: |
| 306 | throw new IllegalStateException( |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 307 | "setSelectedNavigationIndex not valid for current navigation mode"); |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 311 | public void removeAllTabs() { |
| 312 | cleanupTabs(); |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 315 | private void cleanupTabs() { |
| 316 | if (mSelectedTab != null) { |
| 317 | selectTab(null); |
| 318 | } |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 319 | mTabs.clear(); |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 320 | mActionView.removeAllTabs(); |
| 321 | mSavedTabPosition = INVALID_POSITION; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 324 | public void setTitle(CharSequence title) { |
| 325 | mActionView.setTitle(title); |
| 326 | } |
| 327 | |
| 328 | public void setSubtitle(CharSequence subtitle) { |
| 329 | mActionView.setSubtitle(subtitle); |
| 330 | } |
| 331 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 332 | public void setDisplayOptions(int options) { |
| 333 | mActionView.setDisplayOptions(options); |
| 334 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 335 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 336 | public void setDisplayOptions(int options, int mask) { |
| 337 | final int current = mActionView.getDisplayOptions(); |
| 338 | mActionView.setDisplayOptions((options & mask) | (current & ~mask)); |
| 339 | } |
| 340 | |
| 341 | public void setBackgroundDrawable(Drawable d) { |
Adam Powell | e8c1e5c | 2010-12-13 10:49:32 -0800 | [diff] [blame] | 342 | mContainerView.setBackgroundDrawable(d); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Adam Powell | ef70444 | 2010-11-16 14:48:22 -0800 | [diff] [blame] | 345 | public View getCustomView() { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 346 | return mActionView.getCustomNavigationView(); |
| 347 | } |
| 348 | |
| 349 | public CharSequence getTitle() { |
| 350 | return mActionView.getTitle(); |
| 351 | } |
| 352 | |
| 353 | public CharSequence getSubtitle() { |
| 354 | return mActionView.getSubtitle(); |
| 355 | } |
| 356 | |
| 357 | public int getNavigationMode() { |
| 358 | return mActionView.getNavigationMode(); |
| 359 | } |
| 360 | |
| 361 | public int getDisplayOptions() { |
| 362 | return mActionView.getDisplayOptions(); |
| 363 | } |
| 364 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 365 | public ActionMode startActionMode(ActionMode.Callback callback) { |
| 366 | if (mActionMode != null) { |
| 367 | mActionMode.finish(); |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 368 | } |
Adam Powell | 3461b32 | 2010-07-14 11:34:43 -0700 | [diff] [blame] | 369 | |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 370 | mUpperContextView.killMode(); |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 371 | ActionMode mode = new ActionModeImpl(callback); |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 372 | if (callback.onCreateActionMode(mode, mode.getMenu())) { |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 373 | mode.invalidate(); |
| 374 | mUpperContextView.initForMode(mode); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 375 | animateTo(CONTEXT_VIEW); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 376 | if (mLowerContextView != null) { |
| 377 | // TODO animate this |
| 378 | mLowerContextView.setVisibility(View.VISIBLE); |
| 379 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 380 | mActionMode = mode; |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 381 | show(); |
Adam Powell | ac695c6 | 2010-07-20 18:19:27 -0700 | [diff] [blame] | 382 | return mode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 383 | } |
Adam Powell | ac695c6 | 2010-07-20 18:19:27 -0700 | [diff] [blame] | 384 | return null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 387 | private void configureTab(Tab tab, int position) { |
| 388 | final TabImpl tabi = (TabImpl) tab; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 389 | final ActionBar.TabListener callback = tabi.getCallback(); |
| 390 | |
| 391 | if (callback == null) { |
| 392 | throw new IllegalStateException("Action Bar Tab must have a Callback"); |
| 393 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 394 | |
| 395 | tabi.setPosition(position); |
| 396 | mTabs.add(position, tabi); |
| 397 | |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 398 | final int count = mTabs.size(); |
| 399 | for (int i = position + 1; i < count; i++) { |
| 400 | mTabs.get(i).setPosition(i); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 401 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public void addTab(Tab tab) { |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 406 | addTab(tab, mTabs.isEmpty()); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | @Override |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 410 | public void addTab(Tab tab, int position) { |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 411 | addTab(tab, position, mTabs.isEmpty()); |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public void addTab(Tab tab, boolean setSelected) { |
| 416 | mActionView.addTab(tab, setSelected); |
| 417 | configureTab(tab, mTabs.size()); |
| 418 | if (setSelected) { |
| 419 | selectTab(tab); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public void addTab(Tab tab, int position, boolean setSelected) { |
| 425 | mActionView.addTab(tab, position, setSelected); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 426 | configureTab(tab, position); |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 427 | if (setSelected) { |
| 428 | selectTab(tab); |
| 429 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public Tab newTab() { |
| 434 | return new TabImpl(); |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public void removeTab(Tab tab) { |
| 439 | removeTabAt(tab.getPosition()); |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public void removeTabAt(int position) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 444 | int selectedTabPosition = mSelectedTab != null |
| 445 | ? mSelectedTab.getPosition() : mSavedTabPosition; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 446 | mActionView.removeTabAt(position); |
| 447 | mTabs.remove(position); |
| 448 | |
| 449 | final int newTabCount = mTabs.size(); |
| 450 | for (int i = position; i < newTabCount; i++) { |
| 451 | mTabs.get(i).setPosition(i); |
| 452 | } |
| 453 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 454 | if (selectedTabPosition == position) { |
| 455 | selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1))); |
| 456 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 460 | public void selectTab(Tab tab) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 461 | if (getNavigationMode() != NAVIGATION_MODE_TABS) { |
| 462 | mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION; |
| 463 | return; |
| 464 | } |
| 465 | |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 466 | final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction() |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 467 | .disallowAddToBackStack(); |
Adam Powell | 7f9b905 | 2010-10-19 16:56:07 -0700 | [diff] [blame] | 468 | |
| 469 | if (mSelectedTab == tab) { |
| 470 | if (mSelectedTab != null) { |
| 471 | mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans); |
| 472 | } |
| 473 | } else { |
| 474 | mActionView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION); |
| 475 | if (mSelectedTab != null) { |
| 476 | mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans); |
| 477 | } |
| 478 | mSelectedTab = (TabImpl) tab; |
| 479 | if (mSelectedTab != null) { |
| 480 | mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans); |
| 481 | } |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (!trans.isEmpty()) { |
| 485 | trans.commit(); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public Tab getSelectedTab() { |
| 491 | return mSelectedTab; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 494 | @Override |
| 495 | public int getHeight() { |
| 496 | return mActionView.getHeight(); |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public void show() { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 501 | if (mCurrentAnim != null) { |
| 502 | mCurrentAnim.end(); |
| 503 | } |
Adam Powell | d76cee2 | 2011-01-31 15:05:05 -0800 | [diff] [blame] | 504 | if (mContainerView.getVisibility() == View.VISIBLE) { |
| 505 | return; |
| 506 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 507 | mContainerView.setVisibility(View.VISIBLE); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 508 | |
| 509 | if (mShowHideAnimationEnabled) { |
| 510 | mContainerView.setAlpha(0); |
| 511 | AnimatorSet anim = new AnimatorSet(); |
| 512 | AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 1)); |
| 513 | if (mContentView != null) { |
| 514 | b.with(ObjectAnimator.ofFloat(mContentView, "translationY", |
| 515 | -mContainerView.getHeight(), 0)); |
| 516 | mContainerView.setTranslationY(-mContainerView.getHeight()); |
| 517 | b.with(ObjectAnimator.ofFloat(mContainerView, "translationY", 0)); |
| 518 | } |
| 519 | anim.addListener(mShowListener); |
| 520 | mCurrentAnim = anim; |
| 521 | anim.start(); |
| 522 | } else { |
| 523 | mShowListener.onAnimationEnd(null); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 524 | } |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | @Override |
| 528 | public void hide() { |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 529 | if (mCurrentAnim != null) { |
| 530 | mCurrentAnim.end(); |
| 531 | } |
| 532 | if (mContainerView.getVisibility() == View.GONE) { |
| 533 | return; |
| 534 | } |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 535 | |
| 536 | if (mShowHideAnimationEnabled) { |
| 537 | mContainerView.setAlpha(1); |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 538 | mContainerView.setTransitioning(true); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 539 | AnimatorSet anim = new AnimatorSet(); |
| 540 | AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "alpha", 0)); |
| 541 | if (mContentView != null) { |
| 542 | b.with(ObjectAnimator.ofFloat(mContentView, "translationY", |
| 543 | 0, -mContainerView.getHeight())); |
| 544 | b.with(ObjectAnimator.ofFloat(mContainerView, "translationY", |
| 545 | -mContainerView.getHeight())); |
| 546 | } |
| 547 | anim.addListener(mHideListener); |
| 548 | mCurrentAnim = anim; |
| 549 | anim.start(); |
| 550 | } else { |
| 551 | mHideListener.onAnimationEnd(null); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 552 | } |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | public boolean isShowing() { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 556 | return mContainerView.getVisibility() == View.VISIBLE; |
| 557 | } |
| 558 | |
| 559 | private long animateTo(int viewIndex) { |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 560 | show(); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 561 | |
| 562 | AnimatorSet set = new AnimatorSet(); |
| 563 | |
| 564 | final View targetChild = mContainerView.getChildAt(viewIndex); |
| 565 | targetChild.setVisibility(View.VISIBLE); |
| 566 | AnimatorSet.Builder b = set.play(ObjectAnimator.ofFloat(targetChild, "alpha", 1)); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 567 | |
| 568 | final int count = mContainerView.getChildCount(); |
| 569 | for (int i = 0; i < count; i++) { |
| 570 | final View child = mContainerView.getChildAt(i); |
| 571 | if (i == viewIndex) { |
| 572 | continue; |
| 573 | } |
| 574 | |
| 575 | if (child.getVisibility() != View.GONE) { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 576 | Animator a = ObjectAnimator.ofFloat(child, "alpha", 0); |
| 577 | a.setInterpolator(sFadeOutInterpolator); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 578 | b.with(a); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | set.addListener(mAfterAnimation[viewIndex]); |
| 583 | |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 584 | mCurrentAnim = set; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 585 | set.start(); |
| 586 | return set.getDuration(); |
| 587 | } |
| 588 | |
| 589 | private void hideAllExcept(int viewIndex) { |
| 590 | final int count = mContainerView.getChildCount(); |
| 591 | for (int i = 0; i < count; i++) { |
| 592 | mContainerView.getChildAt(i).setVisibility(i == viewIndex ? View.VISIBLE : View.GONE); |
| 593 | } |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 596 | /** |
| 597 | * @hide |
| 598 | */ |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 599 | public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 600 | private ActionMode.Callback mCallback; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 601 | private MenuBuilder mMenu; |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 602 | private WeakReference<View> mCustomView; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 603 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 604 | public ActionModeImpl(ActionMode.Callback callback) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 605 | mCallback = callback; |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 606 | mMenu = new MenuBuilder(mActionView.getContext()) |
| 607 | .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 608 | mMenu.setCallback(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 609 | } |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 610 | |
| 611 | @Override |
| 612 | public MenuInflater getMenuInflater() { |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 613 | return new MenuInflater(mContext); |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 616 | @Override |
| 617 | public Menu getMenu() { |
| 618 | return mMenu; |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public void finish() { |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 623 | if (mActionMode != this) { |
| 624 | // Not the active action mode - no-op |
Adam Powell | 93b6bc3 | 2010-07-22 11:36:35 -0700 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 628 | mCallback.onDestroyActionMode(this); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 629 | mCallback = null; |
| 630 | animateTo(NORMAL_VIEW); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 631 | |
| 632 | // Clear out the context mode views after the animation finishes |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 633 | mUpperContextView.closeMode(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 634 | if (mLowerContextView != null && mLowerContextView.getVisibility() != View.GONE) { |
| 635 | // TODO Animate this |
| 636 | mLowerContextView.setVisibility(View.GONE); |
| 637 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 638 | mActionMode = null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public void invalidate() { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 643 | if (mCallback.onPrepareActionMode(this, mMenu)) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 644 | // Refresh content in both context views |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | @Override |
| 649 | public void setCustomView(View view) { |
| 650 | mUpperContextView.setCustomView(view); |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 651 | mCustomView = new WeakReference<View>(view); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | @Override |
| 655 | public void setSubtitle(CharSequence subtitle) { |
| 656 | mUpperContextView.setSubtitle(subtitle); |
| 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public void setTitle(CharSequence title) { |
| 661 | mUpperContextView.setTitle(title); |
| 662 | } |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 663 | |
| 664 | @Override |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 665 | public void setTitle(int resId) { |
Adam Powell | 48e8ac3 | 2011-01-14 12:10:24 -0800 | [diff] [blame] | 666 | setTitle(mContext.getResources().getString(resId)); |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | @Override |
| 670 | public void setSubtitle(int resId) { |
Adam Powell | 48e8ac3 | 2011-01-14 12:10:24 -0800 | [diff] [blame] | 671 | setSubtitle(mContext.getResources().getString(resId)); |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | @Override |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 675 | public CharSequence getTitle() { |
| 676 | return mUpperContextView.getTitle(); |
| 677 | } |
| 678 | |
| 679 | @Override |
| 680 | public CharSequence getSubtitle() { |
| 681 | return mUpperContextView.getSubtitle(); |
| 682 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 683 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 684 | @Override |
| 685 | public View getCustomView() { |
| 686 | return mCustomView != null ? mCustomView.get() : null; |
| 687 | } |
| 688 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 689 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 690 | if (mCallback != null) { |
| 691 | return mCallback.onActionItemClicked(this, item); |
| 692 | } else { |
| 693 | return false; |
| 694 | } |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 698 | } |
| 699 | |
| 700 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 701 | if (mCallback == null) { |
| 702 | return false; |
| 703 | } |
| 704 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 705 | if (!subMenu.hasVisibleItems()) { |
| 706 | return true; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 707 | } |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 708 | |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 709 | new MenuPopupHelper(mContext, subMenu).show(); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 710 | return true; |
| 711 | } |
| 712 | |
| 713 | public void onCloseSubMenu(SubMenuBuilder menu) { |
| 714 | } |
| 715 | |
| 716 | public void onMenuModeChange(MenuBuilder menu) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 717 | if (mCallback == null) { |
| 718 | return; |
| 719 | } |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 720 | invalidate(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 721 | mUpperContextView.openOverflowMenu(); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 722 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /** |
| 726 | * @hide |
| 727 | */ |
| 728 | public class TabImpl extends ActionBar.Tab { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 729 | private ActionBar.TabListener mCallback; |
| 730 | private Object mTag; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 731 | private Drawable mIcon; |
| 732 | private CharSequence mText; |
| 733 | private int mPosition; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 734 | private View mCustomView; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 735 | |
| 736 | @Override |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 737 | public Object getTag() { |
| 738 | return mTag; |
| 739 | } |
| 740 | |
| 741 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 742 | public Tab setTag(Object tag) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 743 | mTag = tag; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 744 | return this; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | public ActionBar.TabListener getCallback() { |
| 748 | return mCallback; |
| 749 | } |
| 750 | |
| 751 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 752 | public Tab setTabListener(ActionBar.TabListener callback) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 753 | mCallback = callback; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 754 | return this; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | @Override |
| 758 | public View getCustomView() { |
| 759 | return mCustomView; |
| 760 | } |
| 761 | |
| 762 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 763 | public Tab setCustomView(View view) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 764 | mCustomView = view; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 765 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 769 | public Tab setCustomView(int layoutResId) { |
| 770 | return setCustomView(LayoutInflater.from(mContext).inflate(layoutResId, null)); |
| 771 | } |
| 772 | |
| 773 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 774 | public Drawable getIcon() { |
| 775 | return mIcon; |
| 776 | } |
| 777 | |
| 778 | @Override |
| 779 | public int getPosition() { |
| 780 | return mPosition; |
| 781 | } |
| 782 | |
| 783 | public void setPosition(int position) { |
| 784 | mPosition = position; |
| 785 | } |
| 786 | |
| 787 | @Override |
| 788 | public CharSequence getText() { |
| 789 | return mText; |
| 790 | } |
| 791 | |
| 792 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 793 | public Tab setIcon(Drawable icon) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 794 | mIcon = icon; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 795 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 799 | public Tab setIcon(int resId) { |
| 800 | return setIcon(mContext.getResources().getDrawable(resId)); |
| 801 | } |
| 802 | |
| 803 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 804 | public Tab setText(CharSequence text) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 805 | mText = text; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 806 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 810 | public Tab setText(int resId) { |
| 811 | return setText(mContext.getResources().getText(resId)); |
| 812 | } |
| 813 | |
| 814 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 815 | public void select() { |
| 816 | selectTab(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 819 | |
| 820 | @Override |
| 821 | public void setCustomView(View view) { |
| 822 | mActionView.setCustomNavigationView(view); |
| 823 | } |
| 824 | |
| 825 | @Override |
| 826 | public void setCustomView(View view, LayoutParams layoutParams) { |
| 827 | view.setLayoutParams(layoutParams); |
| 828 | mActionView.setCustomNavigationView(view); |
| 829 | } |
| 830 | |
| 831 | @Override |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 832 | public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 833 | mActionView.setDropdownAdapter(adapter); |
| 834 | mActionView.setCallback(callback); |
| 835 | } |
| 836 | |
| 837 | @Override |
| 838 | public int getSelectedNavigationIndex() { |
| 839 | switch (mActionView.getNavigationMode()) { |
| 840 | case NAVIGATION_MODE_TABS: |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 841 | return mSelectedTab != null ? mSelectedTab.getPosition() : -1; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 842 | case NAVIGATION_MODE_LIST: |
| 843 | return mActionView.getDropdownSelectedPosition(); |
| 844 | default: |
| 845 | return -1; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | @Override |
| 850 | public int getNavigationItemCount() { |
| 851 | switch (mActionView.getNavigationMode()) { |
| 852 | case NAVIGATION_MODE_TABS: |
| 853 | return mTabs.size(); |
| 854 | case NAVIGATION_MODE_LIST: |
| 855 | SpinnerAdapter adapter = mActionView.getDropdownAdapter(); |
| 856 | return adapter != null ? adapter.getCount() : 0; |
| 857 | default: |
| 858 | return 0; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | @Override |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 863 | public int getTabCount() { |
| 864 | return mTabs.size(); |
| 865 | } |
| 866 | |
| 867 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 868 | public void setNavigationMode(int mode) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 869 | final int oldMode = mActionView.getNavigationMode(); |
| 870 | switch (oldMode) { |
| 871 | case NAVIGATION_MODE_TABS: |
| 872 | mSavedTabPosition = getSelectedNavigationIndex(); |
| 873 | selectTab(null); |
| 874 | break; |
| 875 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 876 | mActionView.setNavigationMode(mode); |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 877 | switch (mode) { |
| 878 | case NAVIGATION_MODE_TABS: |
| 879 | if (mSavedTabPosition != INVALID_POSITION) { |
| 880 | setSelectedNavigationItem(mSavedTabPosition); |
| 881 | mSavedTabPosition = INVALID_POSITION; |
| 882 | } |
| 883 | break; |
| 884 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | @Override |
| 888 | public Tab getTabAt(int index) { |
| 889 | return mTabs.get(index); |
| 890 | } |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 891 | |
| 892 | /** |
| 893 | * This fragment is added when we're keeping a back stack in a tab switch |
| 894 | * transaction. We use it to change the selected tab in the action bar view |
| 895 | * when we back out. |
| 896 | */ |
| 897 | private class SwitchSelectedTabViewFragment extends Fragment { |
| 898 | private int mSelectedTabIndex; |
| 899 | |
| 900 | public SwitchSelectedTabViewFragment(int oldSelectedTab) { |
| 901 | mSelectedTabIndex = oldSelectedTab; |
| 902 | } |
| 903 | |
| 904 | @Override |
| 905 | public void onDetach() { |
| 906 | if (mSelectedTabIndex >= 0 && mSelectedTabIndex < getTabCount()) { |
| 907 | mActionView.setTabSelected(mSelectedTabIndex); |
| 908 | } |
| 909 | } |
| 910 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 911 | } |