blob: 992fdee5e839b073466d3d87452f55544232acee [file] [log] [blame]
Adam Powell89e06452010-06-23 20:24:52 -07001/*
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
17package com.android.internal.app;
18
Adam Powell2c9c9fe2010-07-16 10:17:57 -070019import com.android.internal.view.menu.MenuBuilder;
Adam Powell2c9c9fe2010-07-16 10:17:57 -070020import com.android.internal.view.menu.MenuPopupHelper;
21import com.android.internal.view.menu.SubMenuBuilder;
Adam Powell89e06452010-06-23 20:24:52 -070022import com.android.internal.widget.ActionBarContextView;
23import com.android.internal.widget.ActionBarView;
24
25import android.app.ActionBar;
Adam Powell661c9082010-07-02 10:09:44 -070026import android.app.Activity;
Adam Powellac695c62010-07-20 18:19:27 -070027import android.app.ContextualMode;
Adam Powell661c9082010-07-02 10:09:44 -070028import android.app.Fragment;
29import android.app.FragmentTransaction;
Adam Powell89e06452010-06-23 20:24:52 -070030import android.graphics.drawable.Drawable;
Adam Powell0e94b512010-06-29 17:58:20 -070031import android.os.Handler;
Adam Powell89e06452010-06-23 20:24:52 -070032import android.view.Menu;
33import android.view.MenuItem;
34import android.view.View;
35import android.widget.LinearLayout;
36import android.widget.SpinnerAdapter;
37import android.widget.ViewAnimator;
38
Adam Powell29ed7572010-07-14 16:24:56 -070039import java.lang.ref.WeakReference;
Adam Powell661c9082010-07-02 10:09:44 -070040import java.util.ArrayList;
41
Adam Powell89e06452010-06-23 20:24:52 -070042/**
43 * ActionBarImpl is the ActionBar implementation used
44 * by devices of all screen sizes. If it detects a compatible decor,
45 * it will split contextual modes across both the ActionBarView at
46 * the top of the screen and a horizontal LinearLayout at the bottom
47 * which is normally hidden.
48 */
49public class ActionBarImpl extends ActionBar {
50 private static final int NORMAL_VIEW = 0;
51 private static final int CONTEXT_VIEW = 1;
52
Adam Powell661c9082010-07-02 10:09:44 -070053 private static final int TAB_SWITCH_SHOW_HIDE = 0;
54 private static final int TAB_SWITCH_ADD_REMOVE = 1;
55
56 private Activity mActivity;
57
Adam Powell89e06452010-06-23 20:24:52 -070058 private ViewAnimator mAnimatorView;
59 private ActionBarView mActionView;
60 private ActionBarContextView mUpperContextView;
61 private LinearLayout mLowerContextView;
Adam Powell661c9082010-07-02 10:09:44 -070062
63 private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>();
64
65 private int mTabContainerViewId = android.R.id.content;
66 private TabImpl mSelectedTab;
67 private int mTabSwitchMode = TAB_SWITCH_ADD_REMOVE;
Adam Powell89e06452010-06-23 20:24:52 -070068
69 private ContextMode mContextMode;
70
71 private static final int CONTEXT_DISPLAY_NORMAL = 0;
72 private static final int CONTEXT_DISPLAY_SPLIT = 1;
73
74 private int mContextDisplayMode;
Adam Powell0e94b512010-06-29 17:58:20 -070075
Adam Powell3461b322010-07-14 11:34:43 -070076 private boolean mClosingContext;
77
Adam Powell0e94b512010-06-29 17:58:20 -070078 final Handler mHandler = new Handler();
79 final Runnable mCloseContext = new Runnable() {
80 public void run() {
81 mUpperContextView.closeMode();
82 if (mLowerContextView != null) {
83 mLowerContextView.removeAllViews();
84 }
Adam Powell3461b322010-07-14 11:34:43 -070085 mClosingContext = false;
Adam Powell0e94b512010-06-29 17:58:20 -070086 }
87 };
88
Adam Powell661c9082010-07-02 10:09:44 -070089 public ActionBarImpl(Activity activity) {
90 final View decor = activity.getWindow().getDecorView();
91 mActivity = activity;
Adam Powell89e06452010-06-23 20:24:52 -070092 mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar);
93 mUpperContextView = (ActionBarContextView) decor.findViewById(
94 com.android.internal.R.id.action_context_bar);
95 mLowerContextView = (LinearLayout) decor.findViewById(
96 com.android.internal.R.id.lower_action_context_bar);
97 mAnimatorView = (ViewAnimator) decor.findViewById(
98 com.android.internal.R.id.action_bar_animator);
99
100 if (mActionView == null || mUpperContextView == null || mAnimatorView == null) {
101 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
102 "with a compatible window decor layout");
103 }
Adam Powell0e94b512010-06-29 17:58:20 -0700104
Adam Powell89e06452010-06-23 20:24:52 -0700105 mContextDisplayMode = mLowerContextView == null ?
106 CONTEXT_DISPLAY_NORMAL : CONTEXT_DISPLAY_SPLIT;
107 }
108
109 public void setCustomNavigationMode(View view) {
Adam Powell661c9082010-07-02 10:09:44 -0700110 cleanupTabs();
Adam Powell89e06452010-06-23 20:24:52 -0700111 mActionView.setCustomNavigationView(view);
112 mActionView.setCallback(null);
113 }
Adam Powell0e94b512010-06-29 17:58:20 -0700114
Adam Powell89e06452010-06-23 20:24:52 -0700115 public void setDropdownNavigationMode(SpinnerAdapter adapter, NavigationCallback callback) {
Adam Powell17809772010-07-21 13:25:11 -0700116 setDropdownNavigationMode(adapter, callback, -1);
117 }
118
119 public void setDropdownNavigationMode(SpinnerAdapter adapter, NavigationCallback callback,
120 int defaultSelectedPosition) {
Adam Powell661c9082010-07-02 10:09:44 -0700121 cleanupTabs();
Adam Powell89e06452010-06-23 20:24:52 -0700122 mActionView.setNavigationMode(NAVIGATION_MODE_DROPDOWN_LIST);
123 mActionView.setDropdownAdapter(adapter);
Adam Powell17809772010-07-21 13:25:11 -0700124 if (defaultSelectedPosition >= 0) {
125 mActionView.setDropdownSelectedPosition(defaultSelectedPosition);
126 }
127 mActionView.setCallback(callback);
Adam Powell89e06452010-06-23 20:24:52 -0700128 }
Adam Powell0e94b512010-06-29 17:58:20 -0700129
130 public void setStandardNavigationMode() {
Adam Powell661c9082010-07-02 10:09:44 -0700131 cleanupTabs();
Adam Powell0e94b512010-06-29 17:58:20 -0700132 mActionView.setNavigationMode(NAVIGATION_MODE_STANDARD);
133 mActionView.setCallback(null);
134 }
135
Adam Powell89e06452010-06-23 20:24:52 -0700136 public void setStandardNavigationMode(CharSequence title) {
Adam Powell661c9082010-07-02 10:09:44 -0700137 cleanupTabs();
Adam Powell89e06452010-06-23 20:24:52 -0700138 setStandardNavigationMode(title, null);
139 }
Adam Powell0e94b512010-06-29 17:58:20 -0700140
Adam Powell89e06452010-06-23 20:24:52 -0700141 public void setStandardNavigationMode(CharSequence title, CharSequence subtitle) {
Adam Powell661c9082010-07-02 10:09:44 -0700142 cleanupTabs();
Adam Powell89e06452010-06-23 20:24:52 -0700143 mActionView.setNavigationMode(NAVIGATION_MODE_STANDARD);
144 mActionView.setTitle(title);
145 mActionView.setSubtitle(subtitle);
146 mActionView.setCallback(null);
147 }
148
Adam Powell17809772010-07-21 13:25:11 -0700149 public void setSelectedNavigationItem(int position) {
150 switch (mActionView.getNavigationMode()) {
151 case NAVIGATION_MODE_TABS:
152 selectTab(mTabs.get(position));
153 break;
154 case NAVIGATION_MODE_DROPDOWN_LIST:
155 mActionView.setDropdownSelectedPosition(position);
156 break;
157 default:
158 throw new IllegalStateException(
159 "setSelectedNavigationItem not valid for current navigation mode");
160 }
161 }
162
163 public int getSelectedNavigationItem() {
164 switch (mActionView.getNavigationMode()) {
165 case NAVIGATION_MODE_TABS:
166 return mSelectedTab.getPosition();
167 case NAVIGATION_MODE_DROPDOWN_LIST:
168 return mActionView.getDropdownSelectedPosition();
169 default:
170 return -1;
171 }
172 }
173
Adam Powell661c9082010-07-02 10:09:44 -0700174 private void cleanupTabs() {
175 if (mSelectedTab != null) {
176 selectTab(null);
177 }
178 if (!mTabs.isEmpty()) {
179 if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) {
180 final FragmentTransaction trans = mActivity.openFragmentTransaction();
181 final int tabCount = mTabs.size();
182 for (int i = 0; i < tabCount; i++) {
183 trans.remove(mTabs.get(i).getFragment());
184 }
185 trans.commit();
186 }
187 mTabs.clear();
188 }
189 }
190
Adam Powell0e94b512010-06-29 17:58:20 -0700191 public void setTitle(CharSequence title) {
192 mActionView.setTitle(title);
193 }
194
195 public void setSubtitle(CharSequence subtitle) {
196 mActionView.setSubtitle(subtitle);
197 }
198
Adam Powell89e06452010-06-23 20:24:52 -0700199 public void setDisplayOptions(int options) {
200 mActionView.setDisplayOptions(options);
201 }
Adam Powell0e94b512010-06-29 17:58:20 -0700202
Adam Powell89e06452010-06-23 20:24:52 -0700203 public void setDisplayOptions(int options, int mask) {
204 final int current = mActionView.getDisplayOptions();
205 mActionView.setDisplayOptions((options & mask) | (current & ~mask));
206 }
207
208 public void setBackgroundDrawable(Drawable d) {
209 mActionView.setBackgroundDrawable(d);
210 }
211
212 public View getCustomNavigationView() {
213 return mActionView.getCustomNavigationView();
214 }
215
216 public CharSequence getTitle() {
217 return mActionView.getTitle();
218 }
219
220 public CharSequence getSubtitle() {
221 return mActionView.getSubtitle();
222 }
223
224 public int getNavigationMode() {
225 return mActionView.getNavigationMode();
226 }
227
228 public int getDisplayOptions() {
229 return mActionView.getDisplayOptions();
230 }
231
Adam Powellac695c62010-07-20 18:19:27 -0700232 public ContextualMode startContextualMode(ContextualMode.Callback callback) {
233 finishContextualMode();
Adam Powell3461b322010-07-14 11:34:43 -0700234
235 // Don't wait for the close context mode animation to finish.
236 if (mClosingContext) {
237 mAnimatorView.clearAnimation();
238 mHandler.removeCallbacks(mCloseContext);
239 mCloseContext.run();
240 }
241
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700242 ContextMode mode = new ContextMode(callback);
243 if (callback.onCreateContextMode(mode, mode.getMenu())) {
244 mode.invalidate();
245 mUpperContextView.initForMode(mode);
Adam Powell89e06452010-06-23 20:24:52 -0700246 mAnimatorView.setDisplayedChild(CONTEXT_VIEW);
247 if (mLowerContextView != null) {
248 // TODO animate this
249 mLowerContextView.setVisibility(View.VISIBLE);
250 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700251 mContextMode = mode;
Adam Powellac695c62010-07-20 18:19:27 -0700252 return mode;
Adam Powell89e06452010-06-23 20:24:52 -0700253 }
Adam Powellac695c62010-07-20 18:19:27 -0700254 return null;
Adam Powell89e06452010-06-23 20:24:52 -0700255 }
256
Adam Powellac695c62010-07-20 18:19:27 -0700257 public void finishContextualMode() {
Adam Powell89e06452010-06-23 20:24:52 -0700258 if (mContextMode != null) {
259 mContextMode.finish();
260 }
261 }
262
Adam Powell661c9082010-07-02 10:09:44 -0700263 private void configureTab(Tab tab, int position) {
264 final TabImpl tabi = (TabImpl) tab;
265 final boolean isFirstTab = mTabs.isEmpty();
266 final FragmentTransaction trans = mActivity.openFragmentTransaction();
267 final Fragment frag = tabi.getFragment();
268
269 tabi.setPosition(position);
270 mTabs.add(position, tabi);
271
272 if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) {
273 if (!frag.isAdded()) {
274 trans.add(mTabContainerViewId, frag);
275 }
276 }
277
278 if (isFirstTab) {
279 if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) {
280 trans.show(frag);
281 } else if (mTabSwitchMode == TAB_SWITCH_ADD_REMOVE) {
282 trans.add(mTabContainerViewId, frag);
283 }
284 mSelectedTab = tabi;
285 } else {
286 if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) {
287 trans.hide(frag);
288 }
289 }
290 trans.commit();
291 }
292
293 @Override
294 public void addTab(Tab tab) {
295 mActionView.addTab(tab);
296 configureTab(tab, mTabs.size());
297 }
298
299 @Override
300 public void insertTab(Tab tab, int position) {
301 mActionView.insertTab(tab, position);
302 configureTab(tab, position);
303 }
304
305 @Override
306 public Tab newTab() {
307 return new TabImpl();
308 }
309
310 @Override
311 public void removeTab(Tab tab) {
312 removeTabAt(tab.getPosition());
313 }
314
315 @Override
316 public void removeTabAt(int position) {
317 mActionView.removeTabAt(position);
318 mTabs.remove(position);
319
320 final int newTabCount = mTabs.size();
321 for (int i = position; i < newTabCount; i++) {
322 mTabs.get(i).setPosition(i);
323 }
324
325 selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1)));
326 }
327
328 @Override
329 public void setTabNavigationMode() {
330 mActionView.setNavigationMode(NAVIGATION_MODE_TABS);
331 }
332
333 @Override
334 public void setTabNavigationMode(int containerViewId) {
335 mTabContainerViewId = containerViewId;
336 setTabNavigationMode();
337 }
338
339 @Override
340 public void selectTab(Tab tab) {
341 if (mSelectedTab == tab) {
342 return;
343 }
344
345 mActionView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION);
346 final FragmentTransaction trans = mActivity.openFragmentTransaction();
347 if (mSelectedTab != null) {
348 if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) {
349 trans.hide(mSelectedTab.getFragment());
350 } else if (mTabSwitchMode == TAB_SWITCH_ADD_REMOVE) {
351 trans.remove(mSelectedTab.getFragment());
352 }
353 }
354 if (tab != null) {
355 if (mTabSwitchMode == TAB_SWITCH_SHOW_HIDE) {
356 trans.show(tab.getFragment());
357 } else if (mTabSwitchMode == TAB_SWITCH_ADD_REMOVE) {
358 trans.add(mTabContainerViewId, tab.getFragment());
359 }
360 }
361 mSelectedTab = (TabImpl) tab;
362 trans.commit();
363 }
364
Adam Powell89e06452010-06-23 20:24:52 -0700365 /**
366 * @hide
367 */
Adam Powellac695c62010-07-20 18:19:27 -0700368 public class ContextMode extends ContextualMode implements MenuBuilder.Callback {
369 private ContextualMode.Callback mCallback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700370 private MenuBuilder mMenu;
Adam Powell29ed7572010-07-14 16:24:56 -0700371 private WeakReference<View> mCustomView;
Adam Powell89e06452010-06-23 20:24:52 -0700372
Adam Powellac695c62010-07-20 18:19:27 -0700373 public ContextMode(ContextualMode.Callback callback) {
Adam Powell89e06452010-06-23 20:24:52 -0700374 mCallback = callback;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700375 mMenu = new MenuBuilder(mActionView.getContext());
376 mMenu.setCallback(this);
Adam Powell89e06452010-06-23 20:24:52 -0700377 }
378
379 @Override
380 public Menu getMenu() {
381 return mMenu;
382 }
383
384 @Override
385 public void finish() {
386 mCallback.onDestroyContextMode(this);
Adam Powell89e06452010-06-23 20:24:52 -0700387 mAnimatorView.setDisplayedChild(NORMAL_VIEW);
Adam Powell0e94b512010-06-29 17:58:20 -0700388
389 // Clear out the context mode views after the animation finishes
Adam Powell3461b322010-07-14 11:34:43 -0700390 mClosingContext = true;
Adam Powell0e94b512010-06-29 17:58:20 -0700391 mHandler.postDelayed(mCloseContext, mAnimatorView.getOutAnimation().getDuration());
392
Adam Powell89e06452010-06-23 20:24:52 -0700393 if (mLowerContextView != null && mLowerContextView.getVisibility() != View.GONE) {
394 // TODO Animate this
395 mLowerContextView.setVisibility(View.GONE);
396 }
397 mContextMode = null;
398 }
399
400 @Override
401 public void invalidate() {
402 if (mCallback.onPrepareContextMode(this, mMenu)) {
403 // Refresh content in both context views
404 }
405 }
406
407 @Override
408 public void setCustomView(View view) {
409 mUpperContextView.setCustomView(view);
Adam Powell29ed7572010-07-14 16:24:56 -0700410 mCustomView = new WeakReference<View>(view);
Adam Powell89e06452010-06-23 20:24:52 -0700411 }
412
413 @Override
414 public void setSubtitle(CharSequence subtitle) {
415 mUpperContextView.setSubtitle(subtitle);
416 }
417
418 @Override
419 public void setTitle(CharSequence title) {
420 mUpperContextView.setTitle(title);
421 }
Adam Powell29ed7572010-07-14 16:24:56 -0700422
423 @Override
424 public CharSequence getTitle() {
425 return mUpperContextView.getTitle();
426 }
427
428 @Override
429 public CharSequence getSubtitle() {
430 return mUpperContextView.getSubtitle();
431 }
Adam Powell89e06452010-06-23 20:24:52 -0700432
Adam Powell29ed7572010-07-14 16:24:56 -0700433 @Override
434 public View getCustomView() {
435 return mCustomView != null ? mCustomView.get() : null;
436 }
437
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700438 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
439 return mCallback.onContextItemClicked(this, item);
440 }
441
442 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
443 }
444
445 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
446 if (!subMenu.hasVisibleItems()) {
447 return true;
Adam Powell89e06452010-06-23 20:24:52 -0700448 }
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700449
450 new MenuPopupHelper(mActivity, subMenu).show();
451 return true;
452 }
453
454 public void onCloseSubMenu(SubMenuBuilder menu) {
455 }
456
457 public void onMenuModeChange(MenuBuilder menu) {
458 }
Adam Powell661c9082010-07-02 10:09:44 -0700459 }
460
461 /**
462 * @hide
463 */
464 public class TabImpl extends ActionBar.Tab {
465 private Fragment mFragment;
466 private Drawable mIcon;
467 private CharSequence mText;
468 private int mPosition;
469
470 @Override
471 public Fragment getFragment() {
472 return mFragment;
473 }
474
475 @Override
476 public Drawable getIcon() {
477 return mIcon;
478 }
479
480 @Override
481 public int getPosition() {
482 return mPosition;
483 }
484
485 public void setPosition(int position) {
486 mPosition = position;
487 }
488
489 @Override
490 public CharSequence getText() {
491 return mText;
492 }
493
494 @Override
495 public void setFragment(Fragment fragment) {
496 mFragment = fragment;
497 }
498
499 @Override
500 public void setIcon(Drawable icon) {
501 mIcon = icon;
502 }
503
504 @Override
505 public void setText(CharSequence text) {
506 mText = text;
507 }
508
509 @Override
510 public void select() {
511 selectTab(this);
Adam Powell89e06452010-06-23 20:24:52 -0700512 }
513 }
514}