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