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