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