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 | package com.android.internal.widget; |
| 17 | |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 18 | import com.android.internal.R; |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 19 | |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 20 | import android.widget.ActionMenuPresenter; |
| 21 | import android.widget.ActionMenuView; |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 22 | import com.android.internal.view.menu.MenuBuilder; |
| 23 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 24 | import android.animation.Animator; |
| 25 | import android.animation.Animator.AnimatorListener; |
| 26 | import android.animation.AnimatorSet; |
| 27 | import android.animation.ObjectAnimator; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.content.res.TypedArray; |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 30 | import android.graphics.drawable.Drawable; |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 32 | import android.util.AttributeSet; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 33 | import android.view.ActionMode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 34 | import android.view.LayoutInflater; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 35 | import android.view.View; |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 36 | import android.view.ViewGroup; |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 37 | import android.view.accessibility.AccessibilityEvent; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 38 | import android.view.animation.DecelerateInterpolator; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 39 | import android.widget.LinearLayout; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 40 | import android.widget.TextView; |
| 41 | |
| 42 | /** |
| 43 | * @hide |
| 44 | */ |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 45 | public class ActionBarContextView extends AbsActionBarView implements AnimatorListener { |
Adam Powell | 77769c7 | 2011-01-17 12:05:08 -0800 | [diff] [blame] | 46 | private static final String TAG = "ActionBarContextView"; |
| 47 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 48 | private CharSequence mTitle; |
| 49 | private CharSequence mSubtitle; |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 50 | |
| 51 | private View mClose; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 52 | private View mCustomView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 53 | private LinearLayout mTitleLayout; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 54 | private TextView mTitleView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 55 | private TextView mSubtitleView; |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 56 | private int mTitleStyleRes; |
| 57 | private int mSubtitleStyleRes; |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 58 | private Drawable mSplitBackground; |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 59 | private boolean mTitleOptional; |
Adam Powell | b2536cf | 2014-09-03 17:17:45 -0700 | [diff] [blame] | 60 | private int mCloseItemLayout; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 61 | |
| 62 | private Animator mCurrentAnimation; |
| 63 | private boolean mAnimateInOnLayout; |
| 64 | private int mAnimationMode; |
| 65 | |
| 66 | private static final int ANIMATE_IDLE = 0; |
| 67 | private static final int ANIMATE_IN = 1; |
| 68 | private static final int ANIMATE_OUT = 2; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 69 | |
| 70 | public ActionBarContextView(Context context) { |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 71 | this(context, null); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public ActionBarContextView(Context context, AttributeSet attrs) { |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 75 | this(context, attrs, com.android.internal.R.attr.actionModeStyle); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 78 | public ActionBarContextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 79 | this(context, attrs, defStyleAttr, 0); |
| 80 | } |
| 81 | |
| 82 | public ActionBarContextView( |
| 83 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 84 | super(context, attrs, defStyleAttr, defStyleRes); |
| 85 | |
| 86 | final TypedArray a = context.obtainStyledAttributes( |
| 87 | attrs, R.styleable.ActionMode, defStyleAttr, defStyleRes); |
Adam Powell | e021e6e | 2014-05-23 17:27:24 -0700 | [diff] [blame] | 88 | setBackground(a.getDrawable( |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 89 | com.android.internal.R.styleable.ActionMode_background)); |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 90 | mTitleStyleRes = a.getResourceId( |
| 91 | com.android.internal.R.styleable.ActionMode_titleTextStyle, 0); |
| 92 | mSubtitleStyleRes = a.getResourceId( |
| 93 | com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0); |
Adam Powell | dcdefbb | 2010-07-19 15:45:22 -0700 | [diff] [blame] | 94 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 95 | mContentHeight = a.getLayoutDimension( |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 96 | com.android.internal.R.styleable.ActionMode_height, 0); |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 97 | |
| 98 | mSplitBackground = a.getDrawable( |
| 99 | com.android.internal.R.styleable.ActionMode_backgroundSplit); |
| 100 | |
Adam Powell | b2536cf | 2014-09-03 17:17:45 -0700 | [diff] [blame] | 101 | mCloseItemLayout = a.getResourceId( |
| 102 | com.android.internal.R.styleable.ActionMode_closeItemLayout, |
| 103 | R.layout.action_mode_close_item); |
| 104 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 105 | a.recycle(); |
| 106 | } |
Adam Powell | 9146ac7 | 2010-08-16 18:53:38 -0700 | [diff] [blame] | 107 | |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 108 | @Override |
Adam Powell | 97e1836 | 2011-10-20 15:37:18 -0700 | [diff] [blame] | 109 | public void onDetachedFromWindow() { |
| 110 | super.onDetachedFromWindow(); |
| 111 | if (mActionMenuPresenter != null) { |
| 112 | mActionMenuPresenter.hideOverflowMenu(); |
| 113 | mActionMenuPresenter.hideSubMenus(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
Adam Powell | e021e6e | 2014-05-23 17:27:24 -0700 | [diff] [blame] | 118 | public void setSplitToolbar(boolean split) { |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 119 | if (mSplitActionBar != split) { |
| 120 | if (mActionMenuPresenter != null) { |
| 121 | // Mode is already active; move everything over and adjust the menu itself. |
| 122 | final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 123 | LayoutParams.MATCH_PARENT); |
| 124 | if (!split) { |
| 125 | mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
Adam Powell | b2536cf | 2014-09-03 17:17:45 -0700 | [diff] [blame] | 126 | mMenuView.setBackground(null); |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 127 | final ViewGroup oldParent = (ViewGroup) mMenuView.getParent(); |
| 128 | if (oldParent != null) oldParent.removeView(mMenuView); |
| 129 | addView(mMenuView, layoutParams); |
| 130 | } else { |
| 131 | // Allow full screen width in split mode. |
| 132 | mActionMenuPresenter.setWidthLimit( |
| 133 | getContext().getResources().getDisplayMetrics().widthPixels, true); |
| 134 | // No limit to the item count; use whatever will fit. |
| 135 | mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
| 136 | // Span the whole width |
| 137 | layoutParams.width = LayoutParams.MATCH_PARENT; |
| 138 | layoutParams.height = mContentHeight; |
| 139 | mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
Adam Powell | b2536cf | 2014-09-03 17:17:45 -0700 | [diff] [blame] | 140 | mMenuView.setBackground(mSplitBackground); |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 141 | final ViewGroup oldParent = (ViewGroup) mMenuView.getParent(); |
| 142 | if (oldParent != null) oldParent.removeView(mMenuView); |
| 143 | mSplitView.addView(mMenuView, layoutParams); |
| 144 | } |
| 145 | } |
Adam Powell | e021e6e | 2014-05-23 17:27:24 -0700 | [diff] [blame] | 146 | super.setSplitToolbar(split); |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Adam Powell | 425689e | 2011-09-08 18:09:33 -0700 | [diff] [blame] | 150 | public void setContentHeight(int height) { |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 151 | mContentHeight = height; |
| 152 | } |
| 153 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 154 | public void setCustomView(View view) { |
| 155 | if (mCustomView != null) { |
| 156 | removeView(mCustomView); |
| 157 | } |
| 158 | mCustomView = view; |
Clara Bayarri | ed2a54c | 2015-02-05 16:58:00 +0000 | [diff] [blame] | 159 | if (view != null && mTitleLayout != null) { |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 160 | removeView(mTitleLayout); |
| 161 | mTitleLayout = null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 162 | } |
| 163 | if (view != null) { |
| 164 | addView(view); |
| 165 | } |
| 166 | requestLayout(); |
| 167 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 168 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 169 | public void setTitle(CharSequence title) { |
| 170 | mTitle = title; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 171 | initTitle(); |
| 172 | } |
| 173 | |
| 174 | public void setSubtitle(CharSequence subtitle) { |
| 175 | mSubtitle = subtitle; |
| 176 | initTitle(); |
| 177 | } |
| 178 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 179 | public CharSequence getTitle() { |
| 180 | return mTitle; |
| 181 | } |
| 182 | |
| 183 | public CharSequence getSubtitle() { |
| 184 | return mSubtitle; |
| 185 | } |
| 186 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 187 | private void initTitle() { |
| 188 | if (mTitleLayout == null) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 189 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 190 | inflater.inflate(R.layout.action_bar_title_item, this); |
| 191 | mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 192 | mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title); |
| 193 | mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle); |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 194 | if (mTitleStyleRes != 0) { |
| 195 | mTitleView.setTextAppearance(mContext, mTitleStyleRes); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 196 | } |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 197 | if (mSubtitleStyleRes != 0) { |
| 198 | mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 199 | } |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | mTitleView.setText(mTitle); |
| 203 | mSubtitleView.setText(mSubtitle); |
| 204 | |
| 205 | final boolean hasTitle = !TextUtils.isEmpty(mTitle); |
| 206 | final boolean hasSubtitle = !TextUtils.isEmpty(mSubtitle); |
| 207 | mSubtitleView.setVisibility(hasSubtitle ? VISIBLE : GONE); |
| 208 | mTitleLayout.setVisibility(hasTitle || hasSubtitle ? VISIBLE : GONE); |
| 209 | if (mTitleLayout.getParent() == null) { |
| 210 | addView(mTitleLayout); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 213 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 214 | public void initForMode(final ActionMode mode) { |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 215 | if (mClose == null) { |
| 216 | LayoutInflater inflater = LayoutInflater.from(mContext); |
Adam Powell | b2536cf | 2014-09-03 17:17:45 -0700 | [diff] [blame] | 217 | mClose = inflater.inflate(mCloseItemLayout, this, false); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 218 | addView(mClose); |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 219 | } else if (mClose.getParent() == null) { |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 220 | addView(mClose); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 221 | } |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 222 | |
| 223 | View closeButton = mClose.findViewById(R.id.action_mode_close_button); |
| 224 | closeButton.setOnClickListener(new OnClickListener() { |
Adam Powell | 269b8bb | 2010-07-20 16:46:42 -0700 | [diff] [blame] | 225 | public void onClick(View v) { |
| 226 | mode.finish(); |
| 227 | } |
| 228 | }); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 229 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 230 | final MenuBuilder menu = (MenuBuilder) mode.getMenu(); |
Adam Powell | 678ed0c | 2011-10-27 17:46:07 -0700 | [diff] [blame] | 231 | if (mActionMenuPresenter != null) { |
| 232 | mActionMenuPresenter.dismissPopupMenus(); |
| 233 | } |
Adam Powell | 538e565 | 2011-10-11 13:47:08 -0700 | [diff] [blame] | 234 | mActionMenuPresenter = new ActionMenuPresenter(mContext); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 235 | mActionMenuPresenter.setReserveOverflow(true); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 236 | |
| 237 | final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 238 | LayoutParams.MATCH_PARENT); |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 239 | if (!mSplitActionBar) { |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 240 | menu.addMenuPresenter(mActionMenuPresenter, mPopupContext); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 241 | mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
Alan Viverette | f6a36e9 | 2014-10-28 17:50:59 -0700 | [diff] [blame] | 242 | mMenuView.setBackground(null); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 243 | addView(mMenuView, layoutParams); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 244 | } else { |
| 245 | // Allow full screen width in split mode. |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 246 | mActionMenuPresenter.setWidthLimit( |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 247 | getContext().getResources().getDisplayMetrics().widthPixels, true); |
| 248 | // No limit to the item count; use whatever will fit. |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 249 | mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 250 | // Span the whole width |
| 251 | layoutParams.width = LayoutParams.MATCH_PARENT; |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 252 | layoutParams.height = mContentHeight; |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 253 | menu.addMenuPresenter(mActionMenuPresenter, mPopupContext); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 254 | mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 255 | mMenuView.setBackgroundDrawable(mSplitBackground); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 256 | mSplitView.addView(mMenuView, layoutParams); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 257 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 258 | |
| 259 | mAnimateInOnLayout = true; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 260 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 261 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 262 | public void closeMode() { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 263 | if (mAnimationMode == ANIMATE_OUT) { |
| 264 | // Called again during close; just finish what we were doing. |
| 265 | return; |
| 266 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 267 | if (mClose == null) { |
| 268 | killMode(); |
| 269 | return; |
| 270 | } |
| 271 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 272 | finishAnimation(); |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 273 | mAnimationMode = ANIMATE_OUT; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 274 | mCurrentAnimation = makeOutAnimation(); |
| 275 | mCurrentAnimation.start(); |
| 276 | } |
| 277 | |
| 278 | private void finishAnimation() { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 279 | final Animator a = mCurrentAnimation; |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 280 | if (a != null) { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 281 | mCurrentAnimation = null; |
| 282 | a.end(); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | public void killMode() { |
| 287 | finishAnimation(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 288 | removeAllViews(); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 289 | if (mSplitView != null) { |
| 290 | mSplitView.removeView(mMenuView); |
| 291 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 292 | mCustomView = null; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 293 | mMenuView = null; |
Adam Powell | 00bba68 | 2011-01-08 15:53:38 -0800 | [diff] [blame] | 294 | mAnimateInOnLayout = false; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 295 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 296 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 297 | @Override |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 298 | public boolean showOverflowMenu() { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 299 | if (mActionMenuPresenter != null) { |
| 300 | return mActionMenuPresenter.showOverflowMenu(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 301 | } |
| 302 | return false; |
| 303 | } |
| 304 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 305 | @Override |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 306 | public boolean hideOverflowMenu() { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 307 | if (mActionMenuPresenter != null) { |
| 308 | return mActionMenuPresenter.hideOverflowMenu(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 313 | @Override |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 314 | public boolean isOverflowMenuShowing() { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 315 | if (mActionMenuPresenter != null) { |
| 316 | return mActionMenuPresenter.isOverflowMenuShowing(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 317 | } |
| 318 | return false; |
| 319 | } |
| 320 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 321 | @Override |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 322 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 323 | // Used by custom views if they don't supply layout params. Everything else |
| 324 | // added to an ActionBarContextView should have them already. |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 325 | return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 326 | } |
| 327 | |
| 328 | @Override |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 329 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 330 | return new MarginLayoutParams(getContext(), attrs); |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 334 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 335 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 336 | if (widthMode != MeasureSpec.EXACTLY) { |
| 337 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 338 | "with android:layout_width=\"match_parent\" (or fill_parent)"); |
| 339 | } |
| 340 | |
| 341 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 342 | if (heightMode == MeasureSpec.UNSPECIFIED) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 343 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 344 | "with android:layout_height=\"wrap_content\""); |
| 345 | } |
| 346 | |
| 347 | final int contentWidth = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 348 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 349 | int maxHeight = mContentHeight > 0 ? |
| 350 | mContentHeight : MeasureSpec.getSize(heightMeasureSpec); |
| 351 | |
| 352 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 353 | int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 354 | final int height = maxHeight - verticalPadding; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 355 | final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); |
| 356 | |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 357 | if (mClose != null) { |
| 358 | availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0); |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 359 | MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); |
| 360 | availableWidth -= lp.leftMargin + lp.rightMargin; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 363 | if (mMenuView != null && mMenuView.getParent() == this) { |
Adam Powell | b0ff6f9 | 2011-01-23 18:07:45 -0800 | [diff] [blame] | 364 | availableWidth = measureChildView(mMenuView, availableWidth, |
| 365 | childSpecHeight, 0); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Adam Powell | b0ff6f9 | 2011-01-23 18:07:45 -0800 | [diff] [blame] | 368 | if (mTitleLayout != null && mCustomView == null) { |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 369 | if (mTitleOptional) { |
| 370 | final int titleWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 371 | mTitleLayout.measure(titleWidthSpec, childSpecHeight); |
| 372 | final int titleWidth = mTitleLayout.getMeasuredWidth(); |
| 373 | final boolean titleFits = titleWidth <= availableWidth; |
| 374 | if (titleFits) { |
| 375 | availableWidth -= titleWidth; |
| 376 | } |
| 377 | mTitleLayout.setVisibility(titleFits ? VISIBLE : GONE); |
| 378 | } else { |
| 379 | availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0); |
| 380 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | if (mCustomView != null) { |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 384 | ViewGroup.LayoutParams lp = mCustomView.getLayoutParams(); |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 385 | final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? |
| 386 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 387 | final int customWidth = lp.width >= 0 ? |
| 388 | Math.min(lp.width, availableWidth) : availableWidth; |
| 389 | final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? |
| 390 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 391 | final int customHeight = lp.height >= 0 ? |
| 392 | Math.min(lp.height, height) : height; |
| 393 | mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode), |
| 394 | MeasureSpec.makeMeasureSpec(customHeight, customHeightMode)); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 397 | if (mContentHeight <= 0) { |
| 398 | int measuredHeight = 0; |
| 399 | final int count = getChildCount(); |
| 400 | for (int i = 0; i < count; i++) { |
| 401 | View v = getChildAt(i); |
| 402 | int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; |
| 403 | if (paddedViewHeight > measuredHeight) { |
| 404 | measuredHeight = paddedViewHeight; |
| 405 | } |
| 406 | } |
| 407 | setMeasuredDimension(contentWidth, measuredHeight); |
| 408 | } else { |
| 409 | setMeasuredDimension(contentWidth, maxHeight); |
| 410 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 413 | private Animator makeInAnimation() { |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 414 | mClose.setTranslationX(-mClose.getWidth() - |
| 415 | ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 416 | ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0); |
| 417 | buttonAnimator.setDuration(200); |
| 418 | buttonAnimator.addListener(this); |
| 419 | buttonAnimator.setInterpolator(new DecelerateInterpolator()); |
| 420 | |
| 421 | AnimatorSet set = new AnimatorSet(); |
| 422 | AnimatorSet.Builder b = set.play(buttonAnimator); |
| 423 | |
| 424 | if (mMenuView != null) { |
| 425 | final int count = mMenuView.getChildCount(); |
| 426 | if (count > 0) { |
| 427 | for (int i = count - 1, j = 0; i >= 0; i--, j++) { |
| 428 | View child = mMenuView.getChildAt(i); |
| 429 | child.setScaleY(0); |
| 430 | ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1); |
Adam Powell | 482ae5f | 2012-02-17 15:18:01 -0800 | [diff] [blame] | 431 | a.setDuration(300); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 432 | b.with(a); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return set; |
| 438 | } |
| 439 | |
| 440 | private Animator makeOutAnimation() { |
| 441 | ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 442 | -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 443 | buttonAnimator.setDuration(200); |
| 444 | buttonAnimator.addListener(this); |
| 445 | buttonAnimator.setInterpolator(new DecelerateInterpolator()); |
| 446 | |
| 447 | AnimatorSet set = new AnimatorSet(); |
| 448 | AnimatorSet.Builder b = set.play(buttonAnimator); |
| 449 | |
| 450 | if (mMenuView != null) { |
| 451 | final int count = mMenuView.getChildCount(); |
| 452 | if (count > 0) { |
| 453 | for (int i = 0; i < 0; i++) { |
| 454 | View child = mMenuView.getChildAt(i); |
| 455 | child.setScaleY(0); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 456 | ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0); |
Adam Powell | 482ae5f | 2012-02-17 15:18:01 -0800 | [diff] [blame] | 457 | a.setDuration(300); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 458 | b.with(a); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return set; |
| 464 | } |
| 465 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 466 | @Override |
| 467 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 468 | final boolean isLayoutRtl = isLayoutRtl(); |
| 469 | int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 470 | final int y = getPaddingTop(); |
| 471 | final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 472 | |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 473 | if (mClose != null && mClose.getVisibility() != GONE) { |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 474 | MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 475 | final int startMargin = (isLayoutRtl ? lp.rightMargin : lp.leftMargin); |
| 476 | final int endMargin = (isLayoutRtl ? lp.leftMargin : lp.rightMargin); |
| 477 | x = next(x, startMargin, isLayoutRtl); |
| 478 | x += positionChild(mClose, x, y, contentHeight, isLayoutRtl); |
| 479 | x = next(x, endMargin, isLayoutRtl); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 480 | |
| 481 | if (mAnimateInOnLayout) { |
| 482 | mAnimationMode = ANIMATE_IN; |
| 483 | mCurrentAnimation = makeInAnimation(); |
| 484 | mCurrentAnimation.start(); |
| 485 | mAnimateInOnLayout = false; |
| 486 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 487 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 488 | |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 489 | if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 490 | x += positionChild(mTitleLayout, x, y, contentHeight, isLayoutRtl); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | if (mCustomView != null) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 494 | x += positionChild(mCustomView, x, y, contentHeight, isLayoutRtl); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 495 | } |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 496 | |
| 497 | x = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 498 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 499 | if (mMenuView != null) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 500 | x += positionChild(mMenuView, x, y, contentHeight, !isLayoutRtl); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 504 | @Override |
| 505 | public void onAnimationStart(Animator animation) { |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public void onAnimationEnd(Animator animation) { |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 510 | if (mAnimationMode == ANIMATE_OUT) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 511 | killMode(); |
| 512 | } |
| 513 | mAnimationMode = ANIMATE_IDLE; |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public void onAnimationCancel(Animator animation) { |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void onAnimationRepeat(Animator animation) { |
| 522 | } |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 523 | |
| 524 | @Override |
| 525 | public boolean shouldDelayChildPressedState() { |
| 526 | return false; |
| 527 | } |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 528 | |
| 529 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 530 | public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 531 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 532 | // Action mode started |
| 533 | event.setSource(this); |
| 534 | event.setClassName(getClass().getName()); |
| 535 | event.setPackageName(getContext().getPackageName()); |
| 536 | event.setContentDescription(mTitle); |
| 537 | } else { |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 538 | super.onInitializeAccessibilityEventInternal(event); |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 541 | |
| 542 | public void setTitleOptional(boolean titleOptional) { |
| 543 | if (titleOptional != mTitleOptional) { |
| 544 | requestLayout(); |
| 545 | } |
| 546 | mTitleOptional = titleOptional; |
| 547 | } |
| 548 | |
| 549 | public boolean isTitleOptional() { |
| 550 | return mTitleOptional; |
| 551 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 552 | } |