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