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 | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 34 | import android.view.Menu; |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 35 | import android.view.MenuInflater; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 36 | import android.view.MenuItem; |
| 37 | import android.view.View; |
| 38 | import android.widget.LinearLayout; |
| 39 | import android.widget.SpinnerAdapter; |
| 40 | import android.widget.ViewAnimator; |
| 41 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 42 | import java.lang.ref.WeakReference; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 43 | import java.util.ArrayList; |
| 44 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 45 | /** |
| 46 | * ActionBarImpl is the ActionBar implementation used |
| 47 | * by devices of all screen sizes. If it detects a compatible decor, |
| 48 | * it will split contextual modes across both the ActionBarView at |
| 49 | * the top of the screen and a horizontal LinearLayout at the bottom |
| 50 | * which is normally hidden. |
| 51 | */ |
| 52 | public class ActionBarImpl extends ActionBar { |
| 53 | private static final int NORMAL_VIEW = 0; |
| 54 | private static final int CONTEXT_VIEW = 1; |
| 55 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 56 | private static final int TAB_SWITCH_SHOW_HIDE = 0; |
| 57 | private static final int TAB_SWITCH_ADD_REMOVE = 1; |
| 58 | |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 59 | private Context mContext; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 60 | private Activity mActivity; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 61 | private Dialog mDialog; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 62 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 63 | private ViewAnimator mAnimatorView; |
| 64 | private ActionBarView mActionView; |
| 65 | private ActionBarContextView mUpperContextView; |
| 66 | private LinearLayout mLowerContextView; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 67 | |
| 68 | private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>(); |
| 69 | |
| 70 | private int mTabContainerViewId = android.R.id.content; |
| 71 | private TabImpl mSelectedTab; |
| 72 | private int mTabSwitchMode = TAB_SWITCH_ADD_REMOVE; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 73 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 74 | private ActionMode mActionMode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 75 | |
| 76 | private static final int CONTEXT_DISPLAY_NORMAL = 0; |
| 77 | private static final int CONTEXT_DISPLAY_SPLIT = 1; |
| 78 | |
| 79 | private int mContextDisplayMode; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 80 | |
Adam Powell | 3461b32 | 2010-07-14 11:34:43 -0700 | [diff] [blame] | 81 | private boolean mClosingContext; |
| 82 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 83 | final Handler mHandler = new Handler(); |
| 84 | final Runnable mCloseContext = new Runnable() { |
| 85 | public void run() { |
| 86 | mUpperContextView.closeMode(); |
| 87 | if (mLowerContextView != null) { |
| 88 | mLowerContextView.removeAllViews(); |
| 89 | } |
Adam Powell | 3461b32 | 2010-07-14 11:34:43 -0700 | [diff] [blame] | 90 | mClosingContext = false; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 91 | } |
| 92 | }; |
| 93 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 94 | public ActionBarImpl(Activity activity) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 95 | mActivity = activity; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 96 | init(activity.getWindow().getDecorView()); |
| 97 | } |
| 98 | |
| 99 | public ActionBarImpl(Dialog dialog) { |
| 100 | mDialog = dialog; |
| 101 | init(dialog.getWindow().getDecorView()); |
| 102 | } |
| 103 | |
| 104 | private void init(View decor) { |
| 105 | mContext = decor.getContext(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 106 | mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar); |
| 107 | mUpperContextView = (ActionBarContextView) decor.findViewById( |
| 108 | com.android.internal.R.id.action_context_bar); |
| 109 | mLowerContextView = (LinearLayout) decor.findViewById( |
| 110 | com.android.internal.R.id.lower_action_context_bar); |
| 111 | mAnimatorView = (ViewAnimator) decor.findViewById( |
| 112 | com.android.internal.R.id.action_bar_animator); |
Steve Block | 08f194b | 2010-08-24 18:20:48 +0100 | [diff] [blame^] | 113 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 114 | if (mActionView == null || mUpperContextView == null || mAnimatorView == null) { |
| 115 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 116 | "with a compatible window decor layout"); |
| 117 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 118 | |
Steve Block | 08f194b | 2010-08-24 18:20:48 +0100 | [diff] [blame^] | 119 | mActionView.setContextView(mUpperContextView); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 120 | mContextDisplayMode = mLowerContextView == null ? |
| 121 | CONTEXT_DISPLAY_NORMAL : CONTEXT_DISPLAY_SPLIT; |
| 122 | } |
| 123 | |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public void setStandardNavigationMode(int titleResId, int subtitleResId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 126 | setStandardNavigationMode(mContext.getString(titleResId), |
| 127 | mContext.getString(subtitleResId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void setStandardNavigationMode(int titleResId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 132 | setStandardNavigationMode(mContext.getString(titleResId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void setTitle(int resId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 137 | setTitle(mContext.getString(resId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void setSubtitle(int resId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 142 | setSubtitle(mContext.getString(resId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 145 | public void setCustomNavigationMode(View view) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 146 | cleanupTabs(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 147 | mActionView.setCustomNavigationView(view); |
| 148 | mActionView.setCallback(null); |
| 149 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 150 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 151 | public void setDropdownNavigationMode(SpinnerAdapter adapter, NavigationCallback callback) { |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 152 | setDropdownNavigationMode(adapter, callback, -1); |
| 153 | } |
| 154 | |
| 155 | public void setDropdownNavigationMode(SpinnerAdapter adapter, NavigationCallback callback, |
| 156 | int defaultSelectedPosition) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 157 | cleanupTabs(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 158 | mActionView.setNavigationMode(NAVIGATION_MODE_DROPDOWN_LIST); |
| 159 | mActionView.setDropdownAdapter(adapter); |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 160 | if (defaultSelectedPosition >= 0) { |
| 161 | mActionView.setDropdownSelectedPosition(defaultSelectedPosition); |
| 162 | } |
| 163 | mActionView.setCallback(callback); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 164 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 165 | |
| 166 | public void setStandardNavigationMode() { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 167 | cleanupTabs(); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 168 | mActionView.setNavigationMode(NAVIGATION_MODE_STANDARD); |
| 169 | mActionView.setCallback(null); |
| 170 | } |
| 171 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 172 | public void setStandardNavigationMode(CharSequence title) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 173 | cleanupTabs(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 174 | setStandardNavigationMode(title, null); |
| 175 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 176 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 177 | public void setStandardNavigationMode(CharSequence title, CharSequence subtitle) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 178 | cleanupTabs(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 179 | mActionView.setNavigationMode(NAVIGATION_MODE_STANDARD); |
| 180 | mActionView.setTitle(title); |
| 181 | mActionView.setSubtitle(subtitle); |
| 182 | mActionView.setCallback(null); |
| 183 | } |
| 184 | |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 185 | public void setSelectedNavigationItem(int position) { |
| 186 | switch (mActionView.getNavigationMode()) { |
| 187 | case NAVIGATION_MODE_TABS: |
| 188 | selectTab(mTabs.get(position)); |
| 189 | break; |
| 190 | case NAVIGATION_MODE_DROPDOWN_LIST: |
| 191 | mActionView.setDropdownSelectedPosition(position); |
| 192 | break; |
| 193 | default: |
| 194 | throw new IllegalStateException( |
| 195 | "setSelectedNavigationItem not valid for current navigation mode"); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | public int getSelectedNavigationItem() { |
| 200 | switch (mActionView.getNavigationMode()) { |
| 201 | case NAVIGATION_MODE_TABS: |
| 202 | return mSelectedTab.getPosition(); |
| 203 | case NAVIGATION_MODE_DROPDOWN_LIST: |
| 204 | return mActionView.getDropdownSelectedPosition(); |
| 205 | default: |
| 206 | return -1; |
| 207 | } |
| 208 | } |
| 209 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 210 | private void cleanupTabs() { |
| 211 | if (mSelectedTab != null) { |
| 212 | selectTab(null); |
| 213 | } |
| 214 | if (!mTabs.isEmpty()) { |
| 215 | if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) { |
| 216 | final FragmentTransaction trans = mActivity.openFragmentTransaction(); |
| 217 | final int tabCount = mTabs.size(); |
| 218 | for (int i = 0; i < tabCount; i++) { |
| 219 | trans.remove(mTabs.get(i).getFragment()); |
| 220 | } |
| 221 | trans.commit(); |
| 222 | } |
| 223 | mTabs.clear(); |
| 224 | } |
| 225 | } |
| 226 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 227 | public void setTitle(CharSequence title) { |
| 228 | mActionView.setTitle(title); |
| 229 | } |
| 230 | |
| 231 | public void setSubtitle(CharSequence subtitle) { |
| 232 | mActionView.setSubtitle(subtitle); |
| 233 | } |
| 234 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 235 | public void setDisplayOptions(int options) { |
| 236 | mActionView.setDisplayOptions(options); |
| 237 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 238 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 239 | public void setDisplayOptions(int options, int mask) { |
| 240 | final int current = mActionView.getDisplayOptions(); |
| 241 | mActionView.setDisplayOptions((options & mask) | (current & ~mask)); |
| 242 | } |
| 243 | |
| 244 | public void setBackgroundDrawable(Drawable d) { |
| 245 | mActionView.setBackgroundDrawable(d); |
| 246 | } |
| 247 | |
| 248 | public View getCustomNavigationView() { |
| 249 | return mActionView.getCustomNavigationView(); |
| 250 | } |
| 251 | |
| 252 | public CharSequence getTitle() { |
| 253 | return mActionView.getTitle(); |
| 254 | } |
| 255 | |
| 256 | public CharSequence getSubtitle() { |
| 257 | return mActionView.getSubtitle(); |
| 258 | } |
| 259 | |
| 260 | public int getNavigationMode() { |
| 261 | return mActionView.getNavigationMode(); |
| 262 | } |
| 263 | |
| 264 | public int getDisplayOptions() { |
| 265 | return mActionView.getDisplayOptions(); |
| 266 | } |
| 267 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 268 | public ActionMode startActionMode(ActionMode.Callback callback) { |
| 269 | if (mActionMode != null) { |
| 270 | mActionMode.finish(); |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 271 | } |
Adam Powell | 3461b32 | 2010-07-14 11:34:43 -0700 | [diff] [blame] | 272 | |
| 273 | // Don't wait for the close context mode animation to finish. |
| 274 | if (mClosingContext) { |
| 275 | mAnimatorView.clearAnimation(); |
| 276 | mHandler.removeCallbacks(mCloseContext); |
| 277 | mCloseContext.run(); |
| 278 | } |
| 279 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 280 | ActionMode mode = new ActionModeImpl(callback); |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 281 | if (callback.onCreateActionMode(mode, mode.getMenu())) { |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 282 | mode.invalidate(); |
| 283 | mUpperContextView.initForMode(mode); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 284 | mAnimatorView.setDisplayedChild(CONTEXT_VIEW); |
| 285 | if (mLowerContextView != null) { |
| 286 | // TODO animate this |
| 287 | mLowerContextView.setVisibility(View.VISIBLE); |
| 288 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 289 | mActionMode = mode; |
Adam Powell | ac695c6 | 2010-07-20 18:19:27 -0700 | [diff] [blame] | 290 | return mode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 291 | } |
Adam Powell | ac695c6 | 2010-07-20 18:19:27 -0700 | [diff] [blame] | 292 | return null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 295 | private void configureTab(Tab tab, int position) { |
| 296 | final TabImpl tabi = (TabImpl) tab; |
| 297 | final boolean isFirstTab = mTabs.isEmpty(); |
| 298 | final FragmentTransaction trans = mActivity.openFragmentTransaction(); |
| 299 | final Fragment frag = tabi.getFragment(); |
| 300 | |
| 301 | tabi.setPosition(position); |
| 302 | mTabs.add(position, tabi); |
| 303 | |
| 304 | if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) { |
| 305 | if (!frag.isAdded()) { |
| 306 | trans.add(mTabContainerViewId, frag); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (isFirstTab) { |
| 311 | if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) { |
| 312 | trans.show(frag); |
| 313 | } else if (mTabSwitchMode == TAB_SWITCH_ADD_REMOVE) { |
| 314 | trans.add(mTabContainerViewId, frag); |
| 315 | } |
| 316 | mSelectedTab = tabi; |
| 317 | } else { |
| 318 | if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) { |
| 319 | trans.hide(frag); |
| 320 | } |
| 321 | } |
| 322 | trans.commit(); |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void addTab(Tab tab) { |
| 327 | mActionView.addTab(tab); |
| 328 | configureTab(tab, mTabs.size()); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void insertTab(Tab tab, int position) { |
| 333 | mActionView.insertTab(tab, position); |
| 334 | configureTab(tab, position); |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public Tab newTab() { |
| 339 | return new TabImpl(); |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public void removeTab(Tab tab) { |
| 344 | removeTabAt(tab.getPosition()); |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void removeTabAt(int position) { |
| 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 | |
| 357 | selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1))); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void setTabNavigationMode() { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 362 | if (mActivity == null) { |
| 363 | throw new IllegalStateException( |
| 364 | "Tab navigation mode cannot be used outside of an Activity"); |
| 365 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 366 | mActionView.setNavigationMode(NAVIGATION_MODE_TABS); |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public void setTabNavigationMode(int containerViewId) { |
| 371 | mTabContainerViewId = containerViewId; |
| 372 | setTabNavigationMode(); |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void selectTab(Tab tab) { |
| 377 | if (mSelectedTab == tab) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | mActionView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION); |
| 382 | final FragmentTransaction trans = mActivity.openFragmentTransaction(); |
| 383 | if (mSelectedTab != null) { |
| 384 | if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) { |
| 385 | trans.hide(mSelectedTab.getFragment()); |
| 386 | } else if (mTabSwitchMode == TAB_SWITCH_ADD_REMOVE) { |
| 387 | trans.remove(mSelectedTab.getFragment()); |
| 388 | } |
| 389 | } |
| 390 | if (tab != null) { |
| 391 | if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) { |
| 392 | trans.show(tab.getFragment()); |
| 393 | } else if (mTabSwitchMode == TAB_SWITCH_ADD_REMOVE) { |
| 394 | trans.add(mTabContainerViewId, tab.getFragment()); |
| 395 | } |
| 396 | } |
| 397 | mSelectedTab = (TabImpl) tab; |
| 398 | trans.commit(); |
| 399 | } |
| 400 | |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 401 | @Override |
| 402 | public int getHeight() { |
| 403 | return mActionView.getHeight(); |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public void show() { |
| 408 | // TODO animate! |
| 409 | mAnimatorView.setVisibility(View.VISIBLE); |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void hide() { |
| 414 | // TODO animate! |
| 415 | mAnimatorView.setVisibility(View.GONE); |
| 416 | } |
| 417 | |
| 418 | public boolean isShowing() { |
| 419 | return mAnimatorView.getVisibility() == View.VISIBLE; |
| 420 | } |
| 421 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 422 | /** |
| 423 | * @hide |
| 424 | */ |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 425 | public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 426 | private ActionMode.Callback mCallback; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 427 | private MenuBuilder mMenu; |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 428 | private WeakReference<View> mCustomView; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 429 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 430 | public ActionModeImpl(ActionMode.Callback callback) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 431 | mCallback = callback; |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 432 | mMenu = new MenuBuilder(mActionView.getContext()) |
| 433 | .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 434 | mMenu.setCallback(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 435 | } |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 436 | |
| 437 | @Override |
| 438 | public MenuInflater getMenuInflater() { |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 439 | return new MenuInflater(mContext); |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 442 | @Override |
| 443 | public Menu getMenu() { |
| 444 | return mMenu; |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public void finish() { |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 449 | if (mActionMode != this) { |
| 450 | // Not the active action mode - no-op |
Adam Powell | 93b6bc3 | 2010-07-22 11:36:35 -0700 | [diff] [blame] | 451 | return; |
| 452 | } |
| 453 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 454 | mCallback.onDestroyActionMode(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 455 | mAnimatorView.setDisplayedChild(NORMAL_VIEW); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 456 | |
| 457 | // Clear out the context mode views after the animation finishes |
Adam Powell | 3461b32 | 2010-07-14 11:34:43 -0700 | [diff] [blame] | 458 | mClosingContext = true; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 459 | mHandler.postDelayed(mCloseContext, mAnimatorView.getOutAnimation().getDuration()); |
| 460 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 461 | if (mLowerContextView != null && mLowerContextView.getVisibility() != View.GONE) { |
| 462 | // TODO Animate this |
| 463 | mLowerContextView.setVisibility(View.GONE); |
| 464 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 465 | mActionMode = null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | @Override |
| 469 | public void invalidate() { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 470 | if (mCallback.onPrepareActionMode(this, mMenu)) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 471 | // Refresh content in both context views |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public void setCustomView(View view) { |
| 477 | mUpperContextView.setCustomView(view); |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 478 | mCustomView = new WeakReference<View>(view); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public void setSubtitle(CharSequence subtitle) { |
| 483 | mUpperContextView.setSubtitle(subtitle); |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public void setTitle(CharSequence title) { |
| 488 | mUpperContextView.setTitle(title); |
| 489 | } |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 490 | |
| 491 | @Override |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 492 | public void setTitle(int resId) { |
| 493 | setTitle(mActivity.getString(resId)); |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public void setSubtitle(int resId) { |
| 498 | setSubtitle(mActivity.getString(resId)); |
| 499 | } |
| 500 | |
| 501 | @Override |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 502 | public CharSequence getTitle() { |
| 503 | return mUpperContextView.getTitle(); |
| 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public CharSequence getSubtitle() { |
| 508 | return mUpperContextView.getSubtitle(); |
| 509 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 510 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 511 | @Override |
| 512 | public View getCustomView() { |
| 513 | return mCustomView != null ? mCustomView.get() : null; |
| 514 | } |
| 515 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 516 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 517 | return mCallback.onActionItemClicked(this, item); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 521 | } |
| 522 | |
| 523 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
| 524 | if (!subMenu.hasVisibleItems()) { |
| 525 | return true; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 526 | } |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 527 | |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 528 | new MenuPopupHelper(mContext, subMenu).show(); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 529 | return true; |
| 530 | } |
| 531 | |
| 532 | public void onCloseSubMenu(SubMenuBuilder menu) { |
| 533 | } |
| 534 | |
| 535 | public void onMenuModeChange(MenuBuilder menu) { |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 536 | invalidate(); |
| 537 | mUpperContextView.showOverflowMenu(); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 538 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
| 542 | * @hide |
| 543 | */ |
| 544 | public class TabImpl extends ActionBar.Tab { |
| 545 | private Fragment mFragment; |
| 546 | private Drawable mIcon; |
| 547 | private CharSequence mText; |
| 548 | private int mPosition; |
| 549 | |
| 550 | @Override |
| 551 | public Fragment getFragment() { |
| 552 | return mFragment; |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public Drawable getIcon() { |
| 557 | return mIcon; |
| 558 | } |
| 559 | |
| 560 | @Override |
| 561 | public int getPosition() { |
| 562 | return mPosition; |
| 563 | } |
| 564 | |
| 565 | public void setPosition(int position) { |
| 566 | mPosition = position; |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public CharSequence getText() { |
| 571 | return mText; |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public void setFragment(Fragment fragment) { |
| 576 | mFragment = fragment; |
| 577 | } |
| 578 | |
| 579 | @Override |
| 580 | public void setIcon(Drawable icon) { |
| 581 | mIcon = icon; |
| 582 | } |
| 583 | |
| 584 | @Override |
| 585 | public void setText(CharSequence text) { |
| 586 | mText = text; |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public void select() { |
| 591 | selectTab(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | } |