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