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 | b8139af | 2012-04-19 13:52:46 -0700 | [diff] [blame] | 19 | import com.android.internal.view.ActionBarPolicy; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 20 | import com.android.internal.view.menu.MenuBuilder; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 21 | import com.android.internal.view.menu.MenuPopupHelper; |
| 22 | import com.android.internal.view.menu.SubMenuBuilder; |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 23 | import com.android.internal.widget.ActionBarContainer; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 24 | import com.android.internal.widget.ActionBarContextView; |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 25 | import com.android.internal.widget.ActionBarOverlayLayout; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 26 | import com.android.internal.widget.ActionBarView; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 27 | import com.android.internal.widget.ScrollingTabContainerView; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 28 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 29 | import android.animation.Animator; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 30 | import android.animation.Animator.AnimatorListener; |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 31 | import android.animation.AnimatorListenerAdapter; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 32 | import android.animation.AnimatorSet; |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 33 | import android.animation.ObjectAnimator; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 34 | import android.app.ActionBar; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 35 | import android.app.Activity; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 36 | import android.app.Dialog; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 37 | import android.app.FragmentTransaction; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 38 | import android.content.Context; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 39 | import android.content.res.Configuration; |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 40 | import android.content.res.Resources; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 41 | import android.graphics.drawable.Drawable; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 42 | import android.os.Handler; |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 43 | import android.util.Log; |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 44 | import android.util.TypedValue; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 45 | import android.view.ActionMode; |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 46 | import android.view.ContextThemeWrapper; |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 47 | import android.view.LayoutInflater; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 48 | import android.view.Menu; |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 49 | import android.view.MenuInflater; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 50 | import android.view.MenuItem; |
| 51 | import android.view.View; |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 52 | import android.view.ViewGroup; |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 53 | import android.view.Window; |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 54 | import android.view.accessibility.AccessibilityEvent; |
Dianne Hackborn | 8eedb8b | 2012-04-16 19:31:58 -0700 | [diff] [blame] | 55 | import android.view.animation.AnimationUtils; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 56 | import android.widget.SpinnerAdapter; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 57 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 58 | import java.lang.ref.WeakReference; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 59 | import java.util.ArrayList; |
| 60 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 61 | /** |
| 62 | * ActionBarImpl is the ActionBar implementation used |
| 63 | * by devices of all screen sizes. If it detects a compatible decor, |
| 64 | * it will split contextual modes across both the ActionBarView at |
| 65 | * the top of the screen and a horizontal LinearLayout at the bottom |
| 66 | * which is normally hidden. |
| 67 | */ |
| 68 | public class ActionBarImpl extends ActionBar { |
Adam Powell | d814504 | 2011-03-24 14:18:27 -0700 | [diff] [blame] | 69 | private static final String TAG = "ActionBarImpl"; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 70 | |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 71 | private Context mContext; |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 72 | private Context mThemedContext; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 73 | private Activity mActivity; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 74 | private Dialog mDialog; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 75 | |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 76 | private ActionBarOverlayLayout mOverlayLayout; |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 77 | private ActionBarContainer mContainerView; |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 78 | private ViewGroup mTopVisibilityView; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 79 | private ActionBarView mActionView; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 80 | private ActionBarContextView mContextView; |
| 81 | private ActionBarContainer mSplitView; |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 82 | private View mContentView; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 83 | private ScrollingTabContainerView mTabScrollView; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 84 | |
| 85 | private ArrayList<TabImpl> mTabs = new ArrayList<TabImpl>(); |
| 86 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 87 | private TabImpl mSelectedTab; |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 88 | private int mSavedTabPosition = INVALID_POSITION; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 89 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 90 | private boolean mDisplayHomeAsUpSet; |
| 91 | |
Adam Powell | 060e3ca | 2011-07-19 20:39:16 -0700 | [diff] [blame] | 92 | ActionModeImpl mActionMode; |
| 93 | ActionMode mDeferredDestroyActionMode; |
| 94 | ActionMode.Callback mDeferredModeDestroyCallback; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 95 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 96 | private boolean mLastMenuVisibility; |
| 97 | private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners = |
| 98 | new ArrayList<OnMenuVisibilityListener>(); |
| 99 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 100 | private static final int CONTEXT_DISPLAY_NORMAL = 0; |
| 101 | private static final int CONTEXT_DISPLAY_SPLIT = 1; |
| 102 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 103 | private static final int INVALID_POSITION = -1; |
| 104 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 105 | private int mContextDisplayMode; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 106 | private boolean mHasEmbeddedTabs; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 107 | |
| 108 | final Handler mHandler = new Handler(); |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 109 | Runnable mTabSelector; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 110 | |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 111 | private int mCurWindowVisibility = View.VISIBLE; |
| 112 | |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 113 | private boolean mHiddenByApp; |
| 114 | private boolean mHiddenBySystem; |
| 115 | private boolean mShowingForMode; |
| 116 | |
| 117 | private boolean mNowShowing = true; |
| 118 | |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 119 | private Animator mCurrentShowAnim; |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 120 | private boolean mShowHideAnimationEnabled; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 121 | |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 122 | final AnimatorListener mHideListener = new AnimatorListenerAdapter() { |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 123 | @Override |
| 124 | public void onAnimationEnd(Animator animation) { |
| 125 | if (mContentView != null) { |
| 126 | mContentView.setTranslationY(0); |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 127 | mTopVisibilityView.setTranslationY(0); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 128 | } |
Adam Powell | 993a63a | 2011-08-18 16:35:53 -0700 | [diff] [blame] | 129 | if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) { |
| 130 | mSplitView.setVisibility(View.GONE); |
| 131 | } |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 132 | mTopVisibilityView.setVisibility(View.GONE); |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 133 | mContainerView.setTransitioning(false); |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 134 | mCurrentShowAnim = null; |
Adam Powell | 060e3ca | 2011-07-19 20:39:16 -0700 | [diff] [blame] | 135 | completeDeferredDestroyActionMode(); |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 136 | if (mOverlayLayout != null) { |
| 137 | mOverlayLayout.requestFitSystemWindows(); |
| 138 | } |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 139 | } |
| 140 | }; |
| 141 | |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 142 | final AnimatorListener mShowListener = new AnimatorListenerAdapter() { |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 143 | @Override |
| 144 | public void onAnimationEnd(Animator animation) { |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 145 | mCurrentShowAnim = null; |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 146 | mTopVisibilityView.requestLayout(); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 147 | } |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 150 | public ActionBarImpl(Activity activity) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 151 | mActivity = activity; |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 152 | Window window = activity.getWindow(); |
| 153 | View decor = window.getDecorView(); |
| 154 | init(decor); |
| 155 | if (!mActivity.getWindow().hasFeature(Window.FEATURE_ACTION_BAR_OVERLAY)) { |
| 156 | mContentView = decor.findViewById(android.R.id.content); |
| 157 | } |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | public ActionBarImpl(Dialog dialog) { |
| 161 | mDialog = dialog; |
| 162 | init(dialog.getWindow().getDecorView()); |
| 163 | } |
| 164 | |
| 165 | private void init(View decor) { |
| 166 | mContext = decor.getContext(); |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 167 | mOverlayLayout = (ActionBarOverlayLayout) decor.findViewById( |
| 168 | com.android.internal.R.id.action_bar_overlay_layout); |
| 169 | if (mOverlayLayout != null) { |
| 170 | mOverlayLayout.setActionBar(this); |
| 171 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 172 | mActionView = (ActionBarView) decor.findViewById(com.android.internal.R.id.action_bar); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 173 | mContextView = (ActionBarContextView) decor.findViewById( |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 174 | com.android.internal.R.id.action_context_bar); |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 175 | mContainerView = (ActionBarContainer) decor.findViewById( |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 176 | com.android.internal.R.id.action_bar_container); |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 177 | mTopVisibilityView = (ViewGroup)decor.findViewById( |
| 178 | com.android.internal.R.id.top_action_bar); |
| 179 | if (mTopVisibilityView == null) { |
| 180 | mTopVisibilityView = mContainerView; |
| 181 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 182 | mSplitView = (ActionBarContainer) decor.findViewById( |
| 183 | com.android.internal.R.id.split_action_bar); |
Steve Block | 08f194b | 2010-08-24 18:20:48 +0100 | [diff] [blame] | 184 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 185 | if (mActionView == null || mContextView == null || mContainerView == null) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 186 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 187 | "with a compatible window decor layout"); |
| 188 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 189 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 190 | mActionView.setContextView(mContextView); |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 191 | mContextDisplayMode = mActionView.isSplitActionBar() ? |
| 192 | CONTEXT_DISPLAY_SPLIT : CONTEXT_DISPLAY_NORMAL; |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 193 | |
Adam Powell | d40423a | 2012-05-02 14:06:03 -0700 | [diff] [blame] | 194 | // This was initially read from the action bar style |
| 195 | final int current = mActionView.getDisplayOptions(); |
| 196 | final boolean homeAsUp = (current & DISPLAY_HOME_AS_UP) != 0; |
| 197 | if (homeAsUp) { |
| 198 | mDisplayHomeAsUpSet = true; |
| 199 | } |
| 200 | |
Adam Powell | b8139af | 2012-04-19 13:52:46 -0700 | [diff] [blame] | 201 | ActionBarPolicy abp = ActionBarPolicy.get(mContext); |
Adam Powell | d40423a | 2012-05-02 14:06:03 -0700 | [diff] [blame] | 202 | setHomeButtonEnabled(abp.enableHomeButtonByDefault() || homeAsUp); |
Adam Powell | b8139af | 2012-04-19 13:52:46 -0700 | [diff] [blame] | 203 | setHasEmbeddedTabs(abp.hasEmbeddedTabs()); |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | public void onConfigurationChanged(Configuration newConfig) { |
Adam Powell | b8139af | 2012-04-19 13:52:46 -0700 | [diff] [blame] | 207 | setHasEmbeddedTabs(ActionBarPolicy.get(mContext).hasEmbeddedTabs()); |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 208 | } |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 209 | |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 210 | private void setHasEmbeddedTabs(boolean hasEmbeddedTabs) { |
| 211 | mHasEmbeddedTabs = hasEmbeddedTabs; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 212 | // Switch tab layout configuration if needed |
| 213 | if (!mHasEmbeddedTabs) { |
| 214 | mActionView.setEmbeddedTabView(null); |
| 215 | mContainerView.setTabContainer(mTabScrollView); |
| 216 | } else { |
| 217 | mContainerView.setTabContainer(null); |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 218 | mActionView.setEmbeddedTabView(mTabScrollView); |
| 219 | } |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 220 | final boolean isInTabMode = getNavigationMode() == NAVIGATION_MODE_TABS; |
| 221 | if (mTabScrollView != null) { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 222 | if (isInTabMode) { |
| 223 | mTabScrollView.setVisibility(View.VISIBLE); |
| 224 | if (mOverlayLayout != null) { |
| 225 | mOverlayLayout.requestFitSystemWindows(); |
| 226 | } |
| 227 | } else { |
| 228 | mTabScrollView.setVisibility(View.GONE); |
| 229 | } |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 230 | } |
| 231 | mActionView.setCollapsable(!mHasEmbeddedTabs && isInTabMode); |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 234 | public boolean hasNonEmbeddedTabs() { |
| 235 | return !mHasEmbeddedTabs && getNavigationMode() == NAVIGATION_MODE_TABS; |
| 236 | } |
| 237 | |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 238 | private void ensureTabsExist() { |
| 239 | if (mTabScrollView != null) { |
| 240 | return; |
| 241 | } |
| 242 | |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 243 | ScrollingTabContainerView tabScroller = new ScrollingTabContainerView(mContext); |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 244 | |
| 245 | if (mHasEmbeddedTabs) { |
| 246 | tabScroller.setVisibility(View.VISIBLE); |
| 247 | mActionView.setEmbeddedTabView(tabScroller); |
| 248 | } else { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 249 | if (getNavigationMode() == NAVIGATION_MODE_TABS) { |
| 250 | tabScroller.setVisibility(View.VISIBLE); |
| 251 | if (mOverlayLayout != null) { |
| 252 | mOverlayLayout.requestFitSystemWindows(); |
| 253 | } |
| 254 | } else { |
| 255 | tabScroller.setVisibility(View.GONE); |
| 256 | } |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 257 | mContainerView.setTabContainer(tabScroller); |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 258 | } |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 259 | mTabScrollView = tabScroller; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Adam Powell | 060e3ca | 2011-07-19 20:39:16 -0700 | [diff] [blame] | 262 | void completeDeferredDestroyActionMode() { |
| 263 | if (mDeferredModeDestroyCallback != null) { |
| 264 | mDeferredModeDestroyCallback.onDestroyActionMode(mDeferredDestroyActionMode); |
| 265 | mDeferredDestroyActionMode = null; |
| 266 | mDeferredModeDestroyCallback = null; |
| 267 | } |
| 268 | } |
| 269 | |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 270 | public void setWindowVisibility(int visibility) { |
| 271 | mCurWindowVisibility = visibility; |
| 272 | } |
| 273 | |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 274 | /** |
| 275 | * Enables or disables animation between show/hide states. |
| 276 | * If animation is disabled using this method, animations in progress |
| 277 | * will be finished. |
| 278 | * |
| 279 | * @param enabled true to animate, false to not animate. |
| 280 | */ |
| 281 | public void setShowHideAnimationEnabled(boolean enabled) { |
| 282 | mShowHideAnimationEnabled = enabled; |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 283 | if (!enabled && mCurrentShowAnim != null) { |
| 284 | mCurrentShowAnim.end(); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 288 | public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) { |
| 289 | mMenuVisibilityListeners.add(listener); |
| 290 | } |
| 291 | |
| 292 | public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) { |
| 293 | mMenuVisibilityListeners.remove(listener); |
| 294 | } |
| 295 | |
| 296 | public void dispatchMenuVisibilityChanged(boolean isVisible) { |
| 297 | if (isVisible == mLastMenuVisibility) { |
| 298 | return; |
| 299 | } |
| 300 | mLastMenuVisibility = isVisible; |
| 301 | |
| 302 | final int count = mMenuVisibilityListeners.size(); |
| 303 | for (int i = 0; i < count; i++) { |
| 304 | mMenuVisibilityListeners.get(i).onMenuVisibilityChanged(isVisible); |
| 305 | } |
| 306 | } |
| 307 | |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 308 | @Override |
Adam Powell | 3f476b3 | 2011-01-03 19:25:36 -0800 | [diff] [blame] | 309 | public void setCustomView(int resId) { |
Adam Powell | f242368 | 2011-08-11 14:29:45 -0700 | [diff] [blame] | 310 | setCustomView(LayoutInflater.from(getThemedContext()).inflate(resId, mActionView, false)); |
Adam Powell | 3f476b3 | 2011-01-03 19:25:36 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void setDisplayUseLogoEnabled(boolean useLogo) { |
| 315 | setDisplayOptions(useLogo ? DISPLAY_USE_LOGO : 0, DISPLAY_USE_LOGO); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public void setDisplayShowHomeEnabled(boolean showHome) { |
| 320 | setDisplayOptions(showHome ? DISPLAY_SHOW_HOME : 0, DISPLAY_SHOW_HOME); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) { |
| 325 | setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void setDisplayShowTitleEnabled(boolean showTitle) { |
| 330 | setDisplayOptions(showTitle ? DISPLAY_SHOW_TITLE : 0, DISPLAY_SHOW_TITLE); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void setDisplayShowCustomEnabled(boolean showCustom) { |
| 335 | setDisplayOptions(showCustom ? DISPLAY_SHOW_CUSTOM : 0, DISPLAY_SHOW_CUSTOM); |
| 336 | } |
| 337 | |
| 338 | @Override |
Adam Powell | c29f4e5 | 2011-07-13 20:40:52 -0700 | [diff] [blame] | 339 | public void setHomeButtonEnabled(boolean enable) { |
| 340 | mActionView.setHomeButtonEnabled(enable); |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 344 | public void setTitle(int resId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 345 | setTitle(mContext.getString(resId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void setSubtitle(int resId) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 350 | setSubtitle(mContext.getString(resId)); |
Adam Powell | a66c7b0 | 2010-07-28 15:28:25 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 353 | public void setSelectedNavigationItem(int position) { |
| 354 | switch (mActionView.getNavigationMode()) { |
| 355 | case NAVIGATION_MODE_TABS: |
| 356 | selectTab(mTabs.get(position)); |
| 357 | break; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 358 | case NAVIGATION_MODE_LIST: |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 359 | mActionView.setDropdownSelectedPosition(position); |
| 360 | break; |
| 361 | default: |
| 362 | throw new IllegalStateException( |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 363 | "setSelectedNavigationIndex not valid for current navigation mode"); |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 367 | public void removeAllTabs() { |
| 368 | cleanupTabs(); |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 371 | private void cleanupTabs() { |
| 372 | if (mSelectedTab != null) { |
| 373 | selectTab(null); |
| 374 | } |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 375 | mTabs.clear(); |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 376 | if (mTabScrollView != null) { |
| 377 | mTabScrollView.removeAllTabs(); |
| 378 | } |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 379 | mSavedTabPosition = INVALID_POSITION; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 382 | public void setTitle(CharSequence title) { |
| 383 | mActionView.setTitle(title); |
| 384 | } |
| 385 | |
| 386 | public void setSubtitle(CharSequence subtitle) { |
| 387 | mActionView.setSubtitle(subtitle); |
| 388 | } |
| 389 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 390 | public void setDisplayOptions(int options) { |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 391 | if ((options & DISPLAY_HOME_AS_UP) != 0) { |
| 392 | mDisplayHomeAsUpSet = true; |
| 393 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 394 | mActionView.setDisplayOptions(options); |
| 395 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 396 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 397 | public void setDisplayOptions(int options, int mask) { |
| 398 | final int current = mActionView.getDisplayOptions(); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 399 | if ((mask & DISPLAY_HOME_AS_UP) != 0) { |
| 400 | mDisplayHomeAsUpSet = true; |
| 401 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 402 | mActionView.setDisplayOptions((options & mask) | (current & ~mask)); |
| 403 | } |
| 404 | |
| 405 | public void setBackgroundDrawable(Drawable d) { |
Adam Powell | f88b915 | 2011-09-07 14:54:32 -0700 | [diff] [blame] | 406 | mContainerView.setPrimaryBackground(d); |
| 407 | } |
| 408 | |
| 409 | public void setStackedBackgroundDrawable(Drawable d) { |
| 410 | mContainerView.setStackedBackground(d); |
| 411 | } |
| 412 | |
| 413 | public void setSplitBackgroundDrawable(Drawable d) { |
| 414 | if (mSplitView != null) { |
| 415 | mSplitView.setSplitBackground(d); |
| 416 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Adam Powell | ef70444 | 2010-11-16 14:48:22 -0800 | [diff] [blame] | 419 | public View getCustomView() { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 420 | return mActionView.getCustomNavigationView(); |
| 421 | } |
| 422 | |
| 423 | public CharSequence getTitle() { |
| 424 | return mActionView.getTitle(); |
| 425 | } |
| 426 | |
| 427 | public CharSequence getSubtitle() { |
| 428 | return mActionView.getSubtitle(); |
| 429 | } |
| 430 | |
| 431 | public int getNavigationMode() { |
| 432 | return mActionView.getNavigationMode(); |
| 433 | } |
| 434 | |
| 435 | public int getDisplayOptions() { |
| 436 | return mActionView.getDisplayOptions(); |
| 437 | } |
| 438 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 439 | public ActionMode startActionMode(ActionMode.Callback callback) { |
| 440 | if (mActionMode != null) { |
| 441 | mActionMode.finish(); |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 442 | } |
Adam Powell | 3461b32 | 2010-07-14 11:34:43 -0700 | [diff] [blame] | 443 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 444 | mContextView.killMode(); |
Adam Powell | 5ee36c4 | 2011-06-02 12:59:43 -0700 | [diff] [blame] | 445 | ActionModeImpl mode = new ActionModeImpl(callback); |
| 446 | if (mode.dispatchOnCreate()) { |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 447 | mode.invalidate(); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 448 | mContextView.initForMode(mode); |
| 449 | animateToMode(true); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 450 | if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 451 | // TODO animate this |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 452 | if (mSplitView.getVisibility() != View.VISIBLE) { |
| 453 | mSplitView.setVisibility(View.VISIBLE); |
| 454 | if (mOverlayLayout != null) { |
| 455 | mOverlayLayout.requestFitSystemWindows(); |
| 456 | } |
| 457 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 458 | } |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 459 | mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 460 | mActionMode = mode; |
Adam Powell | ac695c6 | 2010-07-20 18:19:27 -0700 | [diff] [blame] | 461 | return mode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 462 | } |
Adam Powell | ac695c6 | 2010-07-20 18:19:27 -0700 | [diff] [blame] | 463 | return null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 466 | private void configureTab(Tab tab, int position) { |
| 467 | final TabImpl tabi = (TabImpl) tab; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 468 | final ActionBar.TabListener callback = tabi.getCallback(); |
| 469 | |
| 470 | if (callback == null) { |
| 471 | throw new IllegalStateException("Action Bar Tab must have a Callback"); |
| 472 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 473 | |
| 474 | tabi.setPosition(position); |
| 475 | mTabs.add(position, tabi); |
| 476 | |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 477 | final int count = mTabs.size(); |
| 478 | for (int i = position + 1; i < count; i++) { |
| 479 | mTabs.get(i).setPosition(i); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 480 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public void addTab(Tab tab) { |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 485 | addTab(tab, mTabs.isEmpty()); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | @Override |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 489 | public void addTab(Tab tab, int position) { |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 490 | addTab(tab, position, mTabs.isEmpty()); |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public void addTab(Tab tab, boolean setSelected) { |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 495 | ensureTabsExist(); |
| 496 | mTabScrollView.addTab(tab, setSelected); |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 497 | configureTab(tab, mTabs.size()); |
| 498 | if (setSelected) { |
| 499 | selectTab(tab); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void addTab(Tab tab, int position, boolean setSelected) { |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 505 | ensureTabsExist(); |
| 506 | mTabScrollView.addTab(tab, position, setSelected); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 507 | configureTab(tab, position); |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 508 | if (setSelected) { |
| 509 | selectTab(tab); |
| 510 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public Tab newTab() { |
| 515 | return new TabImpl(); |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public void removeTab(Tab tab) { |
| 520 | removeTabAt(tab.getPosition()); |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public void removeTabAt(int position) { |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 525 | if (mTabScrollView == null) { |
| 526 | // No tabs around to remove |
| 527 | return; |
| 528 | } |
| 529 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 530 | int selectedTabPosition = mSelectedTab != null |
| 531 | ? mSelectedTab.getPosition() : mSavedTabPosition; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 532 | mTabScrollView.removeTabAt(position); |
Adam Powell | 0d8ec1d | 2011-05-03 14:49:13 -0700 | [diff] [blame] | 533 | TabImpl removedTab = mTabs.remove(position); |
| 534 | if (removedTab != null) { |
| 535 | removedTab.setPosition(-1); |
| 536 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 537 | |
| 538 | final int newTabCount = mTabs.size(); |
| 539 | for (int i = position; i < newTabCount; i++) { |
| 540 | mTabs.get(i).setPosition(i); |
| 541 | } |
| 542 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 543 | if (selectedTabPosition == position) { |
| 544 | selectTab(mTabs.isEmpty() ? null : mTabs.get(Math.max(0, position - 1))); |
| 545 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 549 | public void selectTab(Tab tab) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 550 | if (getNavigationMode() != NAVIGATION_MODE_TABS) { |
| 551 | mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION; |
| 552 | return; |
| 553 | } |
| 554 | |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 555 | final FragmentTransaction trans = mActivity.getFragmentManager().beginTransaction() |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 556 | .disallowAddToBackStack(); |
Adam Powell | 7f9b905 | 2010-10-19 16:56:07 -0700 | [diff] [blame] | 557 | |
| 558 | if (mSelectedTab == tab) { |
| 559 | if (mSelectedTab != null) { |
| 560 | mSelectedTab.getCallback().onTabReselected(mSelectedTab, trans); |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 561 | mTabScrollView.animateToTab(tab.getPosition()); |
Adam Powell | 7f9b905 | 2010-10-19 16:56:07 -0700 | [diff] [blame] | 562 | } |
| 563 | } else { |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 564 | mTabScrollView.setTabSelected(tab != null ? tab.getPosition() : Tab.INVALID_POSITION); |
Adam Powell | 7f9b905 | 2010-10-19 16:56:07 -0700 | [diff] [blame] | 565 | if (mSelectedTab != null) { |
| 566 | mSelectedTab.getCallback().onTabUnselected(mSelectedTab, trans); |
| 567 | } |
| 568 | mSelectedTab = (TabImpl) tab; |
| 569 | if (mSelectedTab != null) { |
| 570 | mSelectedTab.getCallback().onTabSelected(mSelectedTab, trans); |
| 571 | } |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | if (!trans.isEmpty()) { |
| 575 | trans.commit(); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | @Override |
| 580 | public Tab getSelectedTab() { |
| 581 | return mSelectedTab; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 584 | @Override |
| 585 | public int getHeight() { |
Adam Powell | 58c5dc1 | 2011-07-14 21:08:10 -0700 | [diff] [blame] | 586 | return mContainerView.getHeight(); |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public void show() { |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 591 | if (mHiddenByApp) { |
| 592 | mHiddenByApp = false; |
| 593 | updateVisibility(false); |
| 594 | } |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 597 | private void showForActionMode() { |
| 598 | if (!mShowingForMode) { |
| 599 | mShowingForMode = true; |
| 600 | if (mOverlayLayout != null) { |
| 601 | mOverlayLayout.setShowingForActionMode(true); |
| 602 | } |
| 603 | updateVisibility(false); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | public void showForSystem() { |
| 608 | if (mHiddenBySystem) { |
| 609 | mHiddenBySystem = false; |
| 610 | updateVisibility(true); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | @Override |
| 615 | public void hide() { |
| 616 | if (!mHiddenByApp) { |
| 617 | mHiddenByApp = true; |
| 618 | updateVisibility(false); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | private void hideForActionMode() { |
| 623 | if (mShowingForMode) { |
| 624 | mShowingForMode = false; |
| 625 | if (mOverlayLayout != null) { |
| 626 | mOverlayLayout.setShowingForActionMode(false); |
| 627 | } |
| 628 | updateVisibility(false); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | public void hideForSystem() { |
| 633 | if (!mHiddenBySystem) { |
| 634 | mHiddenBySystem = true; |
| 635 | updateVisibility(true); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | private static boolean checkShowingFlags(boolean hiddenByApp, boolean hiddenBySystem, |
| 640 | boolean showingForMode) { |
| 641 | if (showingForMode) { |
| 642 | return true; |
| 643 | } else if (hiddenByApp || hiddenBySystem) { |
| 644 | return false; |
| 645 | } else { |
| 646 | return true; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | private void updateVisibility(boolean fromSystem) { |
| 651 | // Based on the current state, should we be hidden or shown? |
| 652 | final boolean shown = checkShowingFlags(mHiddenByApp, mHiddenBySystem, |
| 653 | mShowingForMode); |
| 654 | |
| 655 | if (shown) { |
| 656 | if (!mNowShowing) { |
| 657 | mNowShowing = true; |
| 658 | doShow(fromSystem); |
| 659 | } |
| 660 | } else { |
| 661 | if (mNowShowing) { |
| 662 | mNowShowing = false; |
| 663 | doHide(fromSystem); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | public void doShow(boolean fromSystem) { |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 669 | if (mCurrentShowAnim != null) { |
| 670 | mCurrentShowAnim.end(); |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 671 | } |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 672 | mTopVisibilityView.setVisibility(View.VISIBLE); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 673 | |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 674 | if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 675 | || fromSystem)) { |
Chet Haase | e8118e1 | 2012-05-30 14:19:02 -0700 | [diff] [blame] | 676 | mTopVisibilityView.setTranslationY(0); // because we're about to ask its window loc |
| 677 | float startingY = -mTopVisibilityView.getHeight(); |
| 678 | if (fromSystem) { |
| 679 | int topLeft[] = {0, 0}; |
| 680 | mTopVisibilityView.getLocationInWindow(topLeft); |
| 681 | startingY -= topLeft[1]; |
| 682 | } |
| 683 | mTopVisibilityView.setTranslationY(startingY); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 684 | AnimatorSet anim = new AnimatorSet(); |
Chet Haase | e8118e1 | 2012-05-30 14:19:02 -0700 | [diff] [blame] | 685 | AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView, |
| 686 | "translationY", 0)); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 687 | if (mContentView != null) { |
| 688 | b.with(ObjectAnimator.ofFloat(mContentView, "translationY", |
Chet Haase | e8118e1 | 2012-05-30 14:19:02 -0700 | [diff] [blame] | 689 | startingY, 0)); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 690 | } |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 691 | if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) { |
Dianne Hackborn | 8eedb8b | 2012-04-16 19:31:58 -0700 | [diff] [blame] | 692 | mSplitView.setTranslationY(mSplitView.getHeight()); |
Adam Powell | 993a63a | 2011-08-18 16:35:53 -0700 | [diff] [blame] | 693 | mSplitView.setVisibility(View.VISIBLE); |
Dianne Hackborn | 8eedb8b | 2012-04-16 19:31:58 -0700 | [diff] [blame] | 694 | b.with(ObjectAnimator.ofFloat(mSplitView, "translationY", 0)); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 695 | } |
Dianne Hackborn | 8eedb8b | 2012-04-16 19:31:58 -0700 | [diff] [blame] | 696 | anim.setInterpolator(AnimationUtils.loadInterpolator(mContext, |
Chet Haase | e8118e1 | 2012-05-30 14:19:02 -0700 | [diff] [blame] | 697 | com.android.internal.R.interpolator.decelerate_cubic)); |
| 698 | anim.setDuration(250); |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 699 | // If this is being shown from the system, add a small delay. |
| 700 | // This is because we will also be animating in the status bar, |
| 701 | // and these two elements can't be done in lock-step. So we give |
| 702 | // a little time for the status bar to start its animation before |
| 703 | // the action bar animates. (This corresponds to the corresponding |
| 704 | // case when hiding, where the status bar has a small delay before |
| 705 | // starting.) |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 706 | anim.addListener(mShowListener); |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 707 | mCurrentShowAnim = anim; |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 708 | anim.start(); |
| 709 | } else { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 710 | mTopVisibilityView.setAlpha(1); |
Dianne Hackborn | 80d5506 | 2012-05-22 18:03:20 -0700 | [diff] [blame] | 711 | mTopVisibilityView.setTranslationY(0); |
| 712 | if (mContentView != null) { |
| 713 | mContentView.setTranslationY(0); |
| 714 | } |
| 715 | if (mSplitView != null && mContextDisplayMode == CONTEXT_DISPLAY_SPLIT) { |
| 716 | mSplitView.setAlpha(1); |
| 717 | mSplitView.setTranslationY(0); |
| 718 | mSplitView.setVisibility(View.VISIBLE); |
| 719 | } |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 720 | mShowListener.onAnimationEnd(null); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 721 | } |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 722 | if (mOverlayLayout != null) { |
| 723 | mOverlayLayout.requestFitSystemWindows(); |
| 724 | } |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 727 | public void doHide(boolean fromSystem) { |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 728 | if (mCurrentShowAnim != null) { |
| 729 | mCurrentShowAnim.end(); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 730 | } |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 731 | |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 732 | if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 733 | || fromSystem)) { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 734 | mTopVisibilityView.setAlpha(1); |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 735 | mContainerView.setTransitioning(true); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 736 | AnimatorSet anim = new AnimatorSet(); |
Chet Haase | e8118e1 | 2012-05-30 14:19:02 -0700 | [diff] [blame] | 737 | float endingY = -mTopVisibilityView.getHeight(); |
| 738 | if (fromSystem) { |
| 739 | int topLeft[] = {0, 0}; |
| 740 | mTopVisibilityView.getLocationInWindow(topLeft); |
| 741 | endingY -= topLeft[1]; |
| 742 | } |
| 743 | AnimatorSet.Builder b = anim.play(ObjectAnimator.ofFloat(mTopVisibilityView, |
| 744 | "translationY", endingY)); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 745 | if (mContentView != null) { |
| 746 | b.with(ObjectAnimator.ofFloat(mContentView, "translationY", |
Chet Haase | e8118e1 | 2012-05-30 14:19:02 -0700 | [diff] [blame] | 747 | 0, endingY)); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 748 | } |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 749 | if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 750 | mSplitView.setAlpha(1); |
Dianne Hackborn | 8eedb8b | 2012-04-16 19:31:58 -0700 | [diff] [blame] | 751 | b.with(ObjectAnimator.ofFloat(mSplitView, "translationY", |
| 752 | mSplitView.getHeight())); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 753 | } |
Dianne Hackborn | 8eedb8b | 2012-04-16 19:31:58 -0700 | [diff] [blame] | 754 | anim.setInterpolator(AnimationUtils.loadInterpolator(mContext, |
Chet Haase | e8118e1 | 2012-05-30 14:19:02 -0700 | [diff] [blame] | 755 | com.android.internal.R.interpolator.accelerate_cubic)); |
| 756 | anim.setDuration(250); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 757 | anim.addListener(mHideListener); |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 758 | mCurrentShowAnim = anim; |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 759 | anim.start(); |
| 760 | } else { |
| 761 | mHideListener.onAnimationEnd(null); |
Adam Powell | e6ec732 | 2010-12-07 15:29:26 -0800 | [diff] [blame] | 762 | } |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | public boolean isShowing() { |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 766 | return mNowShowing; |
| 767 | } |
| 768 | |
| 769 | public boolean isSystemShowing() { |
| 770 | return !mHiddenBySystem; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 773 | void animateToMode(boolean toActionMode) { |
Adam Powell | 060e3ca | 2011-07-19 20:39:16 -0700 | [diff] [blame] | 774 | if (toActionMode) { |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 775 | showForActionMode(); |
| 776 | } else { |
| 777 | hideForActionMode(); |
Adam Powell | 07e1f98 | 2011-03-24 11:11:15 -0700 | [diff] [blame] | 778 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 779 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 780 | mActionView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE); |
| 781 | mContextView.animateToVisibility(toActionMode ? View.VISIBLE : View.GONE); |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 782 | if (mTabScrollView != null && !mActionView.hasEmbeddedTabs() && mActionView.isCollapsed()) { |
| 783 | mTabScrollView.animateToVisibility(toActionMode ? View.GONE : View.VISIBLE); |
| 784 | } |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 787 | public Context getThemedContext() { |
| 788 | if (mThemedContext == null) { |
| 789 | TypedValue outValue = new TypedValue(); |
| 790 | Resources.Theme currentTheme = mContext.getTheme(); |
| 791 | currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme, |
| 792 | outValue, true); |
| 793 | final int targetThemeRes = outValue.resourceId; |
| 794 | |
| 795 | if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) { |
| 796 | mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes); |
| 797 | } else { |
| 798 | mThemedContext = mContext; |
| 799 | } |
| 800 | } |
| 801 | return mThemedContext; |
| 802 | } |
| 803 | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 804 | @Override |
| 805 | public boolean isTitleTruncated() { |
| 806 | return mActionView != null && mActionView.isTitleTruncated(); |
| 807 | } |
| 808 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 809 | /** |
| 810 | * @hide |
| 811 | */ |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 812 | public class ActionModeImpl extends ActionMode implements MenuBuilder.Callback { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 813 | private ActionMode.Callback mCallback; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 814 | private MenuBuilder mMenu; |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 815 | private WeakReference<View> mCustomView; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 816 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 817 | public ActionModeImpl(ActionMode.Callback callback) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 818 | mCallback = callback; |
Adam Powell | f242368 | 2011-08-11 14:29:45 -0700 | [diff] [blame] | 819 | mMenu = new MenuBuilder(getThemedContext()) |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 820 | .setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 821 | mMenu.setCallback(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 822 | } |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 823 | |
| 824 | @Override |
| 825 | public MenuInflater getMenuInflater() { |
Adam Powell | f242368 | 2011-08-11 14:29:45 -0700 | [diff] [blame] | 826 | return new MenuInflater(getThemedContext()); |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 829 | @Override |
| 830 | public Menu getMenu() { |
| 831 | return mMenu; |
| 832 | } |
| 833 | |
| 834 | @Override |
| 835 | public void finish() { |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 836 | if (mActionMode != this) { |
| 837 | // Not the active action mode - no-op |
Adam Powell | 93b6bc3 | 2010-07-22 11:36:35 -0700 | [diff] [blame] | 838 | return; |
| 839 | } |
| 840 | |
Dianne Hackborn | 139e5aa | 2012-05-05 20:36:38 -0700 | [diff] [blame] | 841 | // If this change in state is going to cause the action bar |
| 842 | // to be hidden, defer the onDestroy callback until the animation |
| 843 | // is finished and associated relayout is about to happen. This lets |
| 844 | // apps better anticipate visibility and layout behavior. |
| 845 | if (!checkShowingFlags(mHiddenByApp, mHiddenBySystem, false)) { |
| 846 | // With the current state but the action bar hidden, our |
| 847 | // overall showing state is going to be false. |
Adam Powell | 060e3ca | 2011-07-19 20:39:16 -0700 | [diff] [blame] | 848 | mDeferredDestroyActionMode = this; |
| 849 | mDeferredModeDestroyCallback = mCallback; |
| 850 | } else { |
| 851 | mCallback.onDestroyActionMode(this); |
| 852 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 853 | mCallback = null; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 854 | animateToMode(false); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 855 | |
| 856 | // Clear out the context mode views after the animation finishes |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 857 | mContextView.closeMode(); |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 858 | mActionView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 859 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 860 | mActionMode = null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | @Override |
| 864 | public void invalidate() { |
Adam Powell | 5ee36c4 | 2011-06-02 12:59:43 -0700 | [diff] [blame] | 865 | mMenu.stopDispatchingItemsChanged(); |
| 866 | try { |
| 867 | mCallback.onPrepareActionMode(this, mMenu); |
| 868 | } finally { |
| 869 | mMenu.startDispatchingItemsChanged(); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | public boolean dispatchOnCreate() { |
| 874 | mMenu.stopDispatchingItemsChanged(); |
| 875 | try { |
| 876 | return mCallback.onCreateActionMode(this, mMenu); |
| 877 | } finally { |
| 878 | mMenu.startDispatchingItemsChanged(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | @Override |
| 883 | public void setCustomView(View view) { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 884 | mContextView.setCustomView(view); |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 885 | mCustomView = new WeakReference<View>(view); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | @Override |
| 889 | public void setSubtitle(CharSequence subtitle) { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 890 | mContextView.setSubtitle(subtitle); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | @Override |
| 894 | public void setTitle(CharSequence title) { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 895 | mContextView.setTitle(title); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 896 | } |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 897 | |
| 898 | @Override |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 899 | public void setTitle(int resId) { |
Adam Powell | 48e8ac3 | 2011-01-14 12:10:24 -0800 | [diff] [blame] | 900 | setTitle(mContext.getResources().getString(resId)); |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | @Override |
| 904 | public void setSubtitle(int resId) { |
Adam Powell | 48e8ac3 | 2011-01-14 12:10:24 -0800 | [diff] [blame] | 905 | setSubtitle(mContext.getResources().getString(resId)); |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | @Override |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 909 | public CharSequence getTitle() { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 910 | return mContextView.getTitle(); |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | @Override |
| 914 | public CharSequence getSubtitle() { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 915 | return mContextView.getSubtitle(); |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 916 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 917 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 918 | @Override |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 919 | public void setTitleOptionalHint(boolean titleOptional) { |
Adam Powell | 785c447 | 2012-05-02 21:25:39 -0700 | [diff] [blame] | 920 | super.setTitleOptionalHint(titleOptional); |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 921 | mContextView.setTitleOptional(titleOptional); |
| 922 | } |
| 923 | |
| 924 | @Override |
| 925 | public boolean isTitleOptional() { |
| 926 | return mContextView.isTitleOptional(); |
| 927 | } |
| 928 | |
| 929 | @Override |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 930 | public View getCustomView() { |
| 931 | return mCustomView != null ? mCustomView.get() : null; |
| 932 | } |
| 933 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 934 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 935 | if (mCallback != null) { |
| 936 | return mCallback.onActionItemClicked(this, item); |
| 937 | } else { |
| 938 | return false; |
| 939 | } |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 943 | } |
| 944 | |
| 945 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 946 | if (mCallback == null) { |
| 947 | return false; |
| 948 | } |
| 949 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 950 | if (!subMenu.hasVisibleItems()) { |
| 951 | return true; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 952 | } |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 953 | |
Adam Powell | f242368 | 2011-08-11 14:29:45 -0700 | [diff] [blame] | 954 | new MenuPopupHelper(getThemedContext(), subMenu).show(); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 955 | return true; |
| 956 | } |
| 957 | |
| 958 | public void onCloseSubMenu(SubMenuBuilder menu) { |
| 959 | } |
| 960 | |
| 961 | public void onMenuModeChange(MenuBuilder menu) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 962 | if (mCallback == null) { |
| 963 | return; |
| 964 | } |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 965 | invalidate(); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 966 | mContextView.showOverflowMenu(); |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 967 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /** |
| 971 | * @hide |
| 972 | */ |
| 973 | public class TabImpl extends ActionBar.Tab { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 974 | private ActionBar.TabListener mCallback; |
| 975 | private Object mTag; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 976 | private Drawable mIcon; |
| 977 | private CharSequence mText; |
Adam Powell | 94e56ef | 2011-09-06 21:22:22 -0700 | [diff] [blame] | 978 | private CharSequence mContentDesc; |
Adam Powell | 0d8ec1d | 2011-05-03 14:49:13 -0700 | [diff] [blame] | 979 | private int mPosition = -1; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 980 | private View mCustomView; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 981 | |
| 982 | @Override |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 983 | public Object getTag() { |
| 984 | return mTag; |
| 985 | } |
| 986 | |
| 987 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 988 | public Tab setTag(Object tag) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 989 | mTag = tag; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 990 | return this; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | public ActionBar.TabListener getCallback() { |
| 994 | return mCallback; |
| 995 | } |
| 996 | |
| 997 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 998 | public Tab setTabListener(ActionBar.TabListener callback) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 999 | mCallback = callback; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1000 | return this; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | @Override |
| 1004 | public View getCustomView() { |
| 1005 | return mCustomView; |
| 1006 | } |
| 1007 | |
| 1008 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1009 | public Tab setCustomView(View view) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 1010 | mCustomView = view; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 1011 | if (mPosition >= 0) { |
| 1012 | mTabScrollView.updateTab(mPosition); |
| 1013 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1014 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 1018 | public Tab setCustomView(int layoutResId) { |
Adam Powell | f242368 | 2011-08-11 14:29:45 -0700 | [diff] [blame] | 1019 | return setCustomView(LayoutInflater.from(getThemedContext()) |
| 1020 | .inflate(layoutResId, null)); |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 1024 | public Drawable getIcon() { |
| 1025 | return mIcon; |
| 1026 | } |
| 1027 | |
| 1028 | @Override |
| 1029 | public int getPosition() { |
| 1030 | return mPosition; |
| 1031 | } |
| 1032 | |
| 1033 | public void setPosition(int position) { |
| 1034 | mPosition = position; |
| 1035 | } |
| 1036 | |
| 1037 | @Override |
| 1038 | public CharSequence getText() { |
| 1039 | return mText; |
| 1040 | } |
| 1041 | |
| 1042 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1043 | public Tab setIcon(Drawable icon) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 1044 | mIcon = icon; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 1045 | if (mPosition >= 0) { |
| 1046 | mTabScrollView.updateTab(mPosition); |
| 1047 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1048 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 1052 | public Tab setIcon(int resId) { |
| 1053 | return setIcon(mContext.getResources().getDrawable(resId)); |
| 1054 | } |
| 1055 | |
| 1056 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1057 | public Tab setText(CharSequence text) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 1058 | mText = text; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 1059 | if (mPosition >= 0) { |
| 1060 | mTabScrollView.updateTab(mPosition); |
| 1061 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1062 | return this; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | @Override |
Adam Powell | 32555f3 | 2010-11-17 13:49:22 -0800 | [diff] [blame] | 1066 | public Tab setText(int resId) { |
| 1067 | return setText(mContext.getResources().getText(resId)); |
| 1068 | } |
| 1069 | |
| 1070 | @Override |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 1071 | public void select() { |
| 1072 | selectTab(this); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 1073 | } |
Adam Powell | 94e56ef | 2011-09-06 21:22:22 -0700 | [diff] [blame] | 1074 | |
| 1075 | @Override |
| 1076 | public Tab setContentDescription(int resId) { |
| 1077 | return setContentDescription(mContext.getResources().getText(resId)); |
| 1078 | } |
| 1079 | |
| 1080 | @Override |
| 1081 | public Tab setContentDescription(CharSequence contentDesc) { |
| 1082 | mContentDesc = contentDesc; |
| 1083 | if (mPosition >= 0) { |
| 1084 | mTabScrollView.updateTab(mPosition); |
| 1085 | } |
| 1086 | return this; |
| 1087 | } |
| 1088 | |
| 1089 | @Override |
| 1090 | public CharSequence getContentDescription() { |
| 1091 | return mContentDesc; |
| 1092 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 1093 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1094 | |
| 1095 | @Override |
| 1096 | public void setCustomView(View view) { |
| 1097 | mActionView.setCustomNavigationView(view); |
| 1098 | } |
| 1099 | |
| 1100 | @Override |
| 1101 | public void setCustomView(View view, LayoutParams layoutParams) { |
| 1102 | view.setLayoutParams(layoutParams); |
| 1103 | mActionView.setCustomNavigationView(view); |
| 1104 | } |
| 1105 | |
| 1106 | @Override |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 1107 | public void setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1108 | mActionView.setDropdownAdapter(adapter); |
| 1109 | mActionView.setCallback(callback); |
| 1110 | } |
| 1111 | |
| 1112 | @Override |
| 1113 | public int getSelectedNavigationIndex() { |
| 1114 | switch (mActionView.getNavigationMode()) { |
| 1115 | case NAVIGATION_MODE_TABS: |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 1116 | return mSelectedTab != null ? mSelectedTab.getPosition() : -1; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1117 | case NAVIGATION_MODE_LIST: |
| 1118 | return mActionView.getDropdownSelectedPosition(); |
| 1119 | default: |
| 1120 | return -1; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | @Override |
| 1125 | public int getNavigationItemCount() { |
| 1126 | switch (mActionView.getNavigationMode()) { |
| 1127 | case NAVIGATION_MODE_TABS: |
| 1128 | return mTabs.size(); |
| 1129 | case NAVIGATION_MODE_LIST: |
| 1130 | SpinnerAdapter adapter = mActionView.getDropdownAdapter(); |
| 1131 | return adapter != null ? adapter.getCount() : 0; |
| 1132 | default: |
| 1133 | return 0; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | @Override |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1138 | public int getTabCount() { |
| 1139 | return mTabs.size(); |
| 1140 | } |
| 1141 | |
| 1142 | @Override |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1143 | public void setNavigationMode(int mode) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1144 | final int oldMode = mActionView.getNavigationMode(); |
| 1145 | switch (oldMode) { |
| 1146 | case NAVIGATION_MODE_TABS: |
| 1147 | mSavedTabPosition = getSelectedNavigationIndex(); |
| 1148 | selectTab(null); |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 1149 | mTabScrollView.setVisibility(View.GONE); |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1150 | break; |
| 1151 | } |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1152 | if (oldMode != mode && !mHasEmbeddedTabs) { |
| 1153 | if (mOverlayLayout != null) { |
| 1154 | mOverlayLayout.requestFitSystemWindows(); |
| 1155 | } |
| 1156 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1157 | mActionView.setNavigationMode(mode); |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1158 | switch (mode) { |
| 1159 | case NAVIGATION_MODE_TABS: |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 1160 | ensureTabsExist(); |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 1161 | mTabScrollView.setVisibility(View.VISIBLE); |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1162 | if (mSavedTabPosition != INVALID_POSITION) { |
| 1163 | setSelectedNavigationItem(mSavedTabPosition); |
| 1164 | mSavedTabPosition = INVALID_POSITION; |
| 1165 | } |
| 1166 | break; |
| 1167 | } |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 1168 | mActionView.setCollapsable(mode == NAVIGATION_MODE_TABS && !mHasEmbeddedTabs); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | @Override |
| 1172 | public Tab getTabAt(int index) { |
| 1173 | return mTabs.get(index); |
| 1174 | } |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1175 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1176 | |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 1177 | @Override |
| 1178 | public void setIcon(int resId) { |
Adam Powell | 45c515b | 2011-04-21 18:50:20 -0700 | [diff] [blame] | 1179 | mActionView.setIcon(resId); |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 1180 | } |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1181 | |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 1182 | @Override |
| 1183 | public void setIcon(Drawable icon) { |
| 1184 | mActionView.setIcon(icon); |
| 1185 | } |
| 1186 | |
| 1187 | @Override |
| 1188 | public void setLogo(int resId) { |
Adam Powell | 45c515b | 2011-04-21 18:50:20 -0700 | [diff] [blame] | 1189 | mActionView.setLogo(resId); |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | @Override |
| 1193 | public void setLogo(Drawable logo) { |
| 1194 | mActionView.setLogo(logo); |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 1195 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 1196 | |
| 1197 | public void setDefaultDisplayHomeAsUpEnabled(boolean enable) { |
| 1198 | if (!mDisplayHomeAsUpSet) { |
| 1199 | setDisplayHomeAsUpEnabled(enable); |
| 1200 | } |
| 1201 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 1202 | } |