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 | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 241 | public void setTitle(int resId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 242 | setTitle(mContext.getString(resId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void setSubtitle(int resId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 247 | setSubtitle(mContext.getString(resId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 250 | public void setCustomNavigationMode(View view) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 251 | cleanupTabs(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 252 | setCustomView(view); |
| 253 | setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM | DISPLAY_SHOW_TITLE); |
| 254 | mActionView.setNavigationMode(NAVIGATION_MODE_STANDARD); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 255 | mActionView.setCallback(null); |
| 256 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 257 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 258 | public void setDropdownNavigationMode(SpinnerAdapter adapter, OnNavigationListener callback) { |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 259 | setDropdownNavigationMode(adapter, callback, -1); |
| 260 | } |
| 261 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 262 | public void setDropdownNavigationMode(SpinnerAdapter adapter, OnNavigationListener callback, |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 263 | int defaultSelectedPosition) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 264 | cleanupTabs(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 265 | setDisplayOptions(0, DISPLAY_SHOW_CUSTOM | DISPLAY_SHOW_TITLE); |
| 266 | mActionView.setNavigationMode(NAVIGATION_MODE_LIST); |
| 267 | setListNavigationCallbacks(adapter, callback); |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 268 | if (defaultSelectedPosition >= 0) { |
| 269 | mActionView.setDropdownSelectedPosition(defaultSelectedPosition); |
| 270 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 271 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 272 | |
| 273 | public void setStandardNavigationMode() { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 274 | cleanupTabs(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 275 | setDisplayOptions(DISPLAY_SHOW_TITLE, DISPLAY_SHOW_TITLE | DISPLAY_SHOW_CUSTOM); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 276 | mActionView.setNavigationMode(NAVIGATION_MODE_STANDARD); |
| 277 | mActionView.setCallback(null); |
| 278 | } |
| 279 | |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 280 | public void setSelectedNavigationItem(int position) { |
| 281 | switch (mActionView.getNavigationMode()) { |
| 282 | case NAVIGATION_MODE_TABS: |
| 283 | selectTab(mTabs.get(position)); |
| 284 | break; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 285 | case NAVIGATION_MODE_LIST: |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 286 | mActionView.setDropdownSelectedPosition(position); |
| 287 | break; |
| 288 | default: |
| 289 | throw new IllegalStateException( |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 290 | "setSelectedNavigationIndex not valid for current navigation mode"); |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | public int getSelectedNavigationItem() { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 295 | return getSelectedNavigationIndex(); |
| 296 | } |
| 297 | |
| 298 | public void removeAllTabs() { |
| 299 | cleanupTabs(); |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 302 | private void cleanupTabs() { |
| 303 | if (mSelectedTab != null) { |
| 304 | selectTab(null); |
| 305 | } |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 306 | mTabs.clear(); |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 307 | mActionView.removeAllTabs(); |
| 308 | mSavedTabPosition = INVALID_POSITION; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 311 | public void setTitle(CharSequence title) { |
| 312 | mActionView.setTitle(title); |
| 313 | } |
| 314 | |
| 315 | public void setSubtitle(CharSequence subtitle) { |
| 316 | mActionView.setSubtitle(subtitle); |
| 317 | } |
| 318 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 319 | public void setDisplayOptions(int options) { |
| 320 | mActionView.setDisplayOptions(options); |
| 321 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 322 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 323 | public void setDisplayOptions(int options, int mask) { |
| 324 | final int current = mActionView.getDisplayOptions(); |
| 325 | mActionView.setDisplayOptions((options & mask) | (current & ~mask)); |
| 326 | } |
| 327 | |
| 328 | public void setBackgroundDrawable(Drawable d) { |
| 329 | mActionView.setBackgroundDrawable(d); |
| 330 | } |
| 331 | |
| 332 | public View getCustomNavigationView() { |
Adam Powell | ef70444 | 2010-11-16 14:48:22 -0800 | [diff] [blame] | 333 | return getCustomView(); |
| 334 | } |
| 335 | |
| 336 | public View getCustomView() { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 337 | return mActionView.getCustomNavigationView(); |
| 338 | } |
| 339 | |
| 340 | public CharSequence getTitle() { |
| 341 | return mActionView.getTitle(); |
| 342 | } |
| 343 | |
| 344 | public CharSequence getSubtitle() { |
| 345 | return mActionView.getSubtitle(); |
| 346 | } |
| 347 | |
| 348 | public int getNavigationMode() { |
| 349 | return mActionView.getNavigationMode(); |
| 350 | } |
| 351 | |
| 352 | public int getDisplayOptions() { |
| 353 | return mActionView.getDisplayOptions(); |
| 354 | } |
| 355 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 356 | public ActionMode startActionMode(ActionMode.Callback callback) { |
| 357 | if (mActionMode != null) { |
| 358 | mActionMode.finish(); |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 359 | } |
Adam Powell | 3461b32 | 2010-07-14 11:34:43 -0700 | [diff] [blame] | 360 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 361 | ActionMode mode = new ActionModeImpl(callback); |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 362 | if (callback.onCreateActionMode(mode, mode.getMenu())) { |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 363 | mode.invalidate(); |
| 364 | mUpperContextView.initForMode(mode); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 365 | animateTo(CONTEXT_VIEW); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 366 | if (mLowerContextView != null) { |
| 367 | // TODO animate this |
| 368 | mLowerContextView.setVisibility(View.VISIBLE); |
| 369 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 370 | mActionMode = mode; |
Adam Powell | ac695c6 | 2010-07-20 18:19:27 -0700 | [diff] [blame] | 371 | return mode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 372 | } |
Adam Powell | ac695c6 | 2010-07-20 18:19:27 -0700 | [diff] [blame] | 373 | return null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 376 | private void configureTab(Tab tab, int position) { |
| 377 | final TabImpl tabi = (TabImpl) tab; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 378 | final ActionBar.TabListener callback = tabi.getCallback(); |
| 379 | |
| 380 | if (callback == null) { |
| 381 | throw new IllegalStateException("Action Bar Tab must have a Callback"); |
| 382 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 383 | |
| 384 | tabi.setPosition(position); |
| 385 | mTabs.add(position, tabi); |
| 386 | |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 387 | final int count = mTabs.size(); |
| 388 | for (int i = position + 1; i < count; i++) { |
| 389 | mTabs.get(i).setPosition(i); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 390 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public void addTab(Tab tab) { |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 395 | addTab(tab, mTabs.isEmpty()); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | @Override |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 399 | public void addTab(Tab tab, int position) { |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 400 | addTab(tab, position, mTabs.isEmpty()); |
| 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public void addTab(Tab tab, boolean setSelected) { |
| 405 | mActionView.addTab(tab, setSelected); |
| 406 | configureTab(tab, mTabs.size()); |
| 407 | if (setSelected) { |
| 408 | selectTab(tab); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void addTab(Tab tab, int position, boolean setSelected) { |
| 414 | mActionView.addTab(tab, position, setSelected); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 415 | configureTab(tab, position); |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 416 | if (setSelected) { |
| 417 | selectTab(tab); |
| 418 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public Tab newTab() { |
| 423 | return new TabImpl(); |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public void removeTab(Tab tab) { |
| 428 | removeTabAt(tab.getPosition()); |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void removeTabAt(int position) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 433 | int selectedTabPosition = mSelectedTab != null |
| 434 | ? mSelectedTab.getPosition() : mSavedTabPosition; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 435 | mActionView.removeTabAt(position); |
| 436 | mTabs.remove(position); |
| 437 | |
| 438 | final int newTabCount = mTabs.size(); |
| 439 | for (int i = position; i < newTabCount; i++) { |
| 440 | mTabs.get(i).setPosition(i); |
| 441 | } |
| 442 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 443 | if (selectedTabPosition == position) { |
| 444 | selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1))); |
| 445 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | @Override |
| 449 | public void setTabNavigationMode() { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 450 | if (mActivity == null) { |
| 451 | throw new IllegalStateException( |
| 452 | "Tab navigation mode cannot be used outside of an Activity"); |
| 453 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 454 | setDisplayOptions(0, DISPLAY_SHOW_TITLE | DISPLAY_SHOW_CUSTOM); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 455 | mActionView.setNavigationMode(NAVIGATION_MODE_TABS); |
| 456 | } |
| 457 | |
| 458 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 459 | public void selectTab(Tab tab) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 460 | if (getNavigationMode() != NAVIGATION_MODE_TABS) { |
| 461 | mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION; |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | final FragmentTransaction trans = mActivity.getFragmentManager().openTransaction() |
| 466 | .disallowAddToBackStack(); |
Adam Powell | 7f9b905 | 2010-10-19 16:56:07 -0700 | [diff] [blame] | 467 | |
| 468 | if (mSelectedTab == tab) { |
| 469 | if (mSelectedTab != null) { |
| 470 | mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans); |
| 471 | } |
| 472 | } else { |
| 473 | mActionView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION); |
| 474 | if (mSelectedTab != null) { |
| 475 | mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans); |
| 476 | } |
| 477 | mSelectedTab = (TabImpl) tab; |
| 478 | if (mSelectedTab != null) { |
| 479 | mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans); |
| 480 | } |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | if (!trans.isEmpty()) { |
| 484 | trans.commit(); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public Tab getSelectedTab() { |
| 490 | return mSelectedTab; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 493 | @Override |
| 494 | public int getHeight() { |
| 495 | return mActionView.getHeight(); |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void show() { |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 500 | if (mContainerView.getVisibility() == View.VISIBLE) { |
| 501 | return; |
| 502 | } |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame^] | 503 | if (mCurrentAnim != null) { |
| 504 | mCurrentAnim.end(); |
| 505 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 506 | mContainerView.setVisibility(View.VISIBLE); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 507 | mContainerView.setAlpha(0); |
| 508 | mContainerView.setTranslationY(-mContainerView.getHeight()); |
| 509 | AnimatorSet anim = new AnimatorSet(); |
| 510 | AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mContainerView, "translationY", 0)) |
| 511 | .with(ObjectAnimator.ofFloat(mContainerView, "alpha", 1)); |
| 512 | if (mContentView != null) { |
| 513 | b.with(ObjectAnimator.ofFloat(mContentView, "translationY", -mContainerView.getHeight(), 0)); |
| 514 | } |
| 515 | anim.addListener(mShowListener); |
| 516 | mCurrentAnim = anim; |
| 517 | anim.start(); |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void hide() { |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 522 | if (mCurrentAnim != null) { |
| 523 | mCurrentAnim.end(); |
| 524 | } |
| 525 | if (mContainerView.getVisibility() == View.GONE) { |
| 526 | return; |
| 527 | } |
| 528 | mContainerView.setAlpha(1); |
| 529 | AnimatorSet anim = new AnimatorSet(); |
| 530 | AnimatorSet.Builder b = anim.play( |
| 531 | ObjectAnimator.ofFloat(mContainerView, "translationY", -mContainerView.getHeight())) |
| 532 | .with(ObjectAnimator.ofFloat(mContainerView, "alpha", 0)); |
| 533 | if (mContentView != null) { |
| 534 | b.with(ObjectAnimator.ofFloat(mContentView, "translationY", 0, -mContainerView.getHeight())); |
| 535 | } |
| 536 | anim.addListener(mHideListener); |
| 537 | mCurrentAnim = anim; |
| 538 | anim.start(); |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | public boolean isShowing() { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 542 | return mContainerView.getVisibility() == View.VISIBLE; |
| 543 | } |
| 544 | |
| 545 | private long animateTo(int viewIndex) { |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 546 | show(); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 547 | |
| 548 | AnimatorSet set = new AnimatorSet(); |
| 549 | |
| 550 | final View targetChild = mContainerView.getChildAt(viewIndex); |
| 551 | targetChild.setVisibility(View.VISIBLE); |
| 552 | AnimatorSet.Builder b = set.play(ObjectAnimator.ofFloat(targetChild, "alpha", 1)); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 553 | |
| 554 | final int count = mContainerView.getChildCount(); |
| 555 | for (int i = 0; i < count; i++) { |
| 556 | final View child = mContainerView.getChildAt(i); |
| 557 | if (i == viewIndex) { |
| 558 | continue; |
| 559 | } |
| 560 | |
| 561 | if (child.getVisibility() != View.GONE) { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame^] | 562 | Animator a = ObjectAnimator.ofFloat(child, "alpha", 0); |
| 563 | a.setInterpolator(sFadeOutInterpolator); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 564 | b.with(a); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | set.addListener(mAfterAnimation[viewIndex]); |
| 569 | |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 570 | mCurrentAnim = set; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 571 | set.start(); |
| 572 | return set.getDuration(); |
| 573 | } |
| 574 | |
| 575 | private void hideAllExcept(int viewIndex) { |
| 576 | final int count = mContainerView.getChildCount(); |
| 577 | for (int i = 0; i < count; i++) { |
| 578 | mContainerView.getChildAt(i).setVisibility(i == viewIndex ? View.VISIBLE : View.GONE); |
| 579 | } |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 582 | /** |
| 583 | * @hide |
| 584 | */ |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 585 | public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 586 | private ActionMode.Callback mCallback; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 587 | private MenuBuilder mMenu; |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 588 | private WeakReference<View> mCustomView; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 589 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 590 | public ActionModeImpl(ActionMode.Callback callback) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 591 | mCallback = callback; |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 592 | mMenu = new MenuBuilder(mActionView.getContext()) |
| 593 | .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 594 | mMenu.setCallback(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 595 | } |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 596 | |
| 597 | @Override |
| 598 | public MenuInflater getMenuInflater() { |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 599 | return new MenuInflater(mContext); |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 602 | @Override |
| 603 | public Menu getMenu() { |
| 604 | return mMenu; |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public void finish() { |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 609 | if (mActionMode != this) { |
| 610 | // Not the active action mode - no-op |
Adam Powell | 93b6bc3 | 2010-07-22 11:36:35 -0700 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 614 | mCallback.onDestroyActionMode(this); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 615 | mCallback = null; |
| 616 | animateTo(NORMAL_VIEW); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 617 | |
| 618 | // Clear out the context mode views after the animation finishes |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 619 | mUpperContextView.closeMode(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 620 | if (mLowerContextView != null && mLowerContextView.getVisibility() != View.GONE) { |
| 621 | // TODO Animate this |
| 622 | mLowerContextView.setVisibility(View.GONE); |
| 623 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 624 | mActionMode = null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void invalidate() { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 629 | if (mCallback.onPrepareActionMode(this, mMenu)) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 630 | // Refresh content in both context views |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | @Override |
| 635 | public void setCustomView(View view) { |
| 636 | mUpperContextView.setCustomView(view); |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 637 | mCustomView = new WeakReference<View>(view); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | @Override |
| 641 | public void setSubtitle(CharSequence subtitle) { |
| 642 | mUpperContextView.setSubtitle(subtitle); |
| 643 | } |
| 644 | |
| 645 | @Override |
| 646 | public void setTitle(CharSequence title) { |
| 647 | mUpperContextView.setTitle(title); |
| 648 | } |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 649 | |
| 650 | @Override |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 651 | public void setTitle(int resId) { |
| 652 | setTitle(mActivity.getString(resId)); |
| 653 | } |
| 654 | |
| 655 | @Override |
| 656 | public void setSubtitle(int resId) { |
| 657 | setSubtitle(mActivity.getString(resId)); |
| 658 | } |
| 659 | |
| 660 | @Override |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 661 | public CharSequence getTitle() { |
| 662 | return mUpperContextView.getTitle(); |
| 663 | } |
| 664 | |
| 665 | @Override |
| 666 | public CharSequence getSubtitle() { |
| 667 | return mUpperContextView.getSubtitle(); |
| 668 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 669 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 670 | @Override |
| 671 | public View getCustomView() { |
| 672 | return mCustomView != null ? mCustomView.get() : null; |
| 673 | } |
| 674 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 675 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 676 | if (mCallback != null) { |
| 677 | return mCallback.onActionItemClicked(this, item); |
| 678 | } else { |
| 679 | return false; |
| 680 | } |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 684 | } |
| 685 | |
| 686 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 687 | if (mCallback == null) { |
| 688 | return false; |
| 689 | } |
| 690 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 691 | if (!subMenu.hasVisibleItems()) { |
| 692 | return true; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 693 | } |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 694 | |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 695 | new MenuPopupHelper(mContext, subMenu).show(); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 696 | return true; |
| 697 | } |
| 698 | |
| 699 | public void onCloseSubMenu(SubMenuBuilder menu) { |
| 700 | } |
| 701 | |
| 702 | public void onMenuModeChange(MenuBuilder menu) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 703 | if (mCallback == null) { |
| 704 | return; |
| 705 | } |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 706 | invalidate(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 707 | mUpperContextView.openOverflowMenu(); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 708 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /** |
| 712 | * @hide |
| 713 | */ |
| 714 | public class TabImpl extends ActionBar.Tab { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 715 | private ActionBar.TabListener mCallback; |
| 716 | private Object mTag; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 717 | private Drawable mIcon; |
| 718 | private CharSequence mText; |
| 719 | private int mPosition; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 720 | private View mCustomView; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 721 | |
| 722 | @Override |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 723 | public Object getTag() { |
| 724 | return mTag; |
| 725 | } |
| 726 | |
| 727 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 728 | public Tab setTag(Object tag) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 729 | mTag = tag; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 730 | return this; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | public ActionBar.TabListener getCallback() { |
| 734 | return mCallback; |
| 735 | } |
| 736 | |
| 737 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 738 | public Tab setTabListener(ActionBar.TabListener callback) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 739 | mCallback = callback; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 740 | return this; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | @Override |
| 744 | public View getCustomView() { |
| 745 | return mCustomView; |
| 746 | } |
| 747 | |
| 748 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 749 | public Tab setCustomView(View view) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 750 | mCustomView = view; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 751 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 755 | public Tab setCustomView(int layoutResId) { |
| 756 | return setCustomView(LayoutInflater.from(mContext).inflate(layoutResId, null)); |
| 757 | } |
| 758 | |
| 759 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 760 | public Drawable getIcon() { |
| 761 | return mIcon; |
| 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public int getPosition() { |
| 766 | return mPosition; |
| 767 | } |
| 768 | |
| 769 | public void setPosition(int position) { |
| 770 | mPosition = position; |
| 771 | } |
| 772 | |
| 773 | @Override |
| 774 | public CharSequence getText() { |
| 775 | return mText; |
| 776 | } |
| 777 | |
| 778 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 779 | public Tab setIcon(Drawable icon) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 780 | mIcon = icon; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 781 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 785 | public Tab setIcon(int resId) { |
| 786 | return setIcon(mContext.getResources().getDrawable(resId)); |
| 787 | } |
| 788 | |
| 789 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 790 | public Tab setText(CharSequence text) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 791 | mText = text; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 792 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 796 | public Tab setText(int resId) { |
| 797 | return setText(mContext.getResources().getText(resId)); |
| 798 | } |
| 799 | |
| 800 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 801 | public void select() { |
| 802 | selectTab(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 805 | |
| 806 | @Override |
| 807 | public void setCustomView(View view) { |
| 808 | mActionView.setCustomNavigationView(view); |
| 809 | } |
| 810 | |
| 811 | @Override |
| 812 | public void setCustomView(View view, LayoutParams layoutParams) { |
| 813 | view.setLayoutParams(layoutParams); |
| 814 | mActionView.setCustomNavigationView(view); |
| 815 | } |
| 816 | |
| 817 | @Override |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 818 | public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 819 | mActionView.setDropdownAdapter(adapter); |
| 820 | mActionView.setCallback(callback); |
| 821 | } |
| 822 | |
| 823 | @Override |
| 824 | public int getSelectedNavigationIndex() { |
| 825 | switch (mActionView.getNavigationMode()) { |
| 826 | case NAVIGATION_MODE_TABS: |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 827 | return mSelectedTab != null ? mSelectedTab.getPosition() : -1; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 828 | case NAVIGATION_MODE_LIST: |
| 829 | return mActionView.getDropdownSelectedPosition(); |
| 830 | default: |
| 831 | return -1; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | @Override |
| 836 | public int getNavigationItemCount() { |
| 837 | switch (mActionView.getNavigationMode()) { |
| 838 | case NAVIGATION_MODE_TABS: |
| 839 | return mTabs.size(); |
| 840 | case NAVIGATION_MODE_LIST: |
| 841 | SpinnerAdapter adapter = mActionView.getDropdownAdapter(); |
| 842 | return adapter != null ? adapter.getCount() : 0; |
| 843 | default: |
| 844 | return 0; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | @Override |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 849 | public int getTabCount() { |
| 850 | return mTabs.size(); |
| 851 | } |
| 852 | |
| 853 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 854 | public void setNavigationMode(int mode) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 855 | final int oldMode = mActionView.getNavigationMode(); |
| 856 | switch (oldMode) { |
| 857 | case NAVIGATION_MODE_TABS: |
| 858 | mSavedTabPosition = getSelectedNavigationIndex(); |
| 859 | selectTab(null); |
| 860 | break; |
| 861 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 862 | mActionView.setNavigationMode(mode); |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 863 | switch (mode) { |
| 864 | case NAVIGATION_MODE_TABS: |
| 865 | if (mSavedTabPosition != INVALID_POSITION) { |
| 866 | setSelectedNavigationItem(mSavedTabPosition); |
| 867 | mSavedTabPosition = INVALID_POSITION; |
| 868 | } |
| 869 | break; |
| 870 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | @Override |
| 874 | public Tab getTabAt(int index) { |
| 875 | return mTabs.get(index); |
| 876 | } |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 877 | |
| 878 | /** |
| 879 | * This fragment is added when we're keeping a back stack in a tab switch |
| 880 | * transaction. We use it to change the selected tab in the action bar view |
| 881 | * when we back out. |
| 882 | */ |
| 883 | private class SwitchSelectedTabViewFragment extends Fragment { |
| 884 | private int mSelectedTabIndex; |
| 885 | |
| 886 | public SwitchSelectedTabViewFragment(int oldSelectedTab) { |
| 887 | mSelectedTabIndex = oldSelectedTab; |
| 888 | } |
| 889 | |
| 890 | @Override |
| 891 | public void onDetach() { |
| 892 | if (mSelectedTabIndex >= 0 && mSelectedTabIndex < getTabCount()) { |
| 893 | mActionView.setTabSelected(mSelectedTabIndex); |
| 894 | } |
| 895 | } |
| 896 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 897 | } |