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