blob: 42d875df11fb90be012d6005cc1a445d65c99dca [file] [log] [blame]
Adam Powell89e06452010-06-23 20:24:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.internal.widget;
17
Adam Powell9ca705e2011-08-11 16:57:59 -070018import com.android.internal.R;
Alan Viverette3d0f21d2014-07-10 16:15:01 -070019
Adam Powellfa18d182014-01-07 15:56:59 -080020import android.widget.ActionMenuPresenter;
21import android.widget.ActionMenuView;
Adam Powell9ca705e2011-08-11 16:57:59 -070022import com.android.internal.view.menu.MenuBuilder;
23
Adam Powelld8b3f2e2010-12-02 13:37:03 -080024import android.animation.Animator;
25import android.animation.Animator.AnimatorListener;
26import android.animation.AnimatorSet;
27import android.animation.ObjectAnimator;
Adam Powell89e06452010-06-23 20:24:52 -070028import android.content.Context;
29import android.content.res.TypedArray;
Adam Powell9ca705e2011-08-11 16:57:59 -070030import android.graphics.drawable.Drawable;
Gilles Debunnec619e742011-05-31 15:49:51 -070031import android.text.TextUtils;
Adam Powell89e06452010-06-23 20:24:52 -070032import android.util.AttributeSet;
Adam Powell6e346362010-07-23 10:18:23 -070033import android.view.ActionMode;
Adam Powell89e06452010-06-23 20:24:52 -070034import android.view.LayoutInflater;
Adam Powell89e06452010-06-23 20:24:52 -070035import android.view.View;
Adam Powell9a5cc282011-08-28 16:18:16 -070036import android.view.ViewGroup;
Adam Powell86ed4362011-09-14 16:18:53 -070037import android.view.accessibility.AccessibilityEvent;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080038import android.view.animation.DecelerateInterpolator;
Adam Powell0e94b512010-06-29 17:58:20 -070039import android.widget.LinearLayout;
Adam Powell89e06452010-06-23 20:24:52 -070040import android.widget.TextView;
41
42/**
43 * @hide
44 */
Adam Powell640a66e2011-04-29 10:18:53 -070045public class ActionBarContextView extends AbsActionBarView implements AnimatorListener {
Adam Powell77769c72011-01-17 12:05:08 -080046 private static final String TAG = "ActionBarContextView";
47
Adam Powell89e06452010-06-23 20:24:52 -070048 private CharSequence mTitle;
49 private CharSequence mSubtitle;
Adam Powellf16888f2010-10-11 17:05:29 -070050
51 private View mClose;
Adam Powell89e06452010-06-23 20:24:52 -070052 private View mCustomView;
Adam Powell0e94b512010-06-29 17:58:20 -070053 private LinearLayout mTitleLayout;
Adam Powell89e06452010-06-23 20:24:52 -070054 private TextView mTitleView;
Adam Powell0e94b512010-06-29 17:58:20 -070055 private TextView mSubtitleView;
Adam Powellbc234a12010-09-09 17:03:50 -070056 private int mTitleStyleRes;
57 private int mSubtitleStyleRes;
Adam Powell9ca705e2011-08-11 16:57:59 -070058 private Drawable mSplitBackground;
Adam Powellb98a81f2012-02-24 11:09:07 -080059 private boolean mTitleOptional;
Adam Powellb2536cf2014-09-03 17:17:45 -070060 private int mCloseItemLayout;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080061
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 Powell89e06452010-06-23 20:24:52 -070069
70 public ActionBarContextView(Context context) {
Adam Powellbc234a12010-09-09 17:03:50 -070071 this(context, null);
Adam Powell89e06452010-06-23 20:24:52 -070072 }
73
74 public ActionBarContextView(Context context, AttributeSet attrs) {
Adam Powellbc234a12010-09-09 17:03:50 -070075 this(context, attrs, com.android.internal.R.attr.actionModeStyle);
Adam Powell89e06452010-06-23 20:24:52 -070076 }
77
Alan Viverette617feb92013-09-09 18:09:13 -070078 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 Powelle021e6e2014-05-23 17:27:24 -070088 setBackground(a.getDrawable(
Adam Powellbc234a12010-09-09 17:03:50 -070089 com.android.internal.R.styleable.ActionMode_background));
Adam Powellbc234a12010-09-09 17:03:50 -070090 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 Powelldcdefbb2010-07-19 15:45:22 -070094
Adam Powelle2194442010-08-12 18:13:03 -070095 mContentHeight = a.getLayoutDimension(
Adam Powellbc234a12010-09-09 17:03:50 -070096 com.android.internal.R.styleable.ActionMode_height, 0);
Adam Powell9ca705e2011-08-11 16:57:59 -070097
98 mSplitBackground = a.getDrawable(
99 com.android.internal.R.styleable.ActionMode_backgroundSplit);
100
Adam Powellb2536cf2014-09-03 17:17:45 -0700101 mCloseItemLayout = a.getResourceId(
102 com.android.internal.R.styleable.ActionMode_closeItemLayout,
103 R.layout.action_mode_close_item);
104
Adam Powell89e06452010-06-23 20:24:52 -0700105 a.recycle();
106 }
Adam Powell9146ac72010-08-16 18:53:38 -0700107
Adam Powella05aba92011-09-23 14:22:49 -0700108 @Override
Adam Powell97e18362011-10-20 15:37:18 -0700109 public void onDetachedFromWindow() {
110 super.onDetachedFromWindow();
111 if (mActionMenuPresenter != null) {
112 mActionMenuPresenter.hideOverflowMenu();
113 mActionMenuPresenter.hideSubMenus();
114 }
115 }
116
117 @Override
Adam Powelle021e6e2014-05-23 17:27:24 -0700118 public void setSplitToolbar(boolean split) {
Adam Powella05aba92011-09-23 14:22:49 -0700119 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 Powellb2536cf2014-09-03 17:17:45 -0700126 mMenuView.setBackground(null);
Adam Powella05aba92011-09-23 14:22:49 -0700127 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 Powellb2536cf2014-09-03 17:17:45 -0700140 mMenuView.setBackground(mSplitBackground);
Adam Powella05aba92011-09-23 14:22:49 -0700141 final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
142 if (oldParent != null) oldParent.removeView(mMenuView);
143 mSplitView.addView(mMenuView, layoutParams);
144 }
145 }
Adam Powelle021e6e2014-05-23 17:27:24 -0700146 super.setSplitToolbar(split);
Adam Powella05aba92011-09-23 14:22:49 -0700147 }
148 }
149
Adam Powell425689e2011-09-08 18:09:33 -0700150 public void setContentHeight(int height) {
Adam Powelle2194442010-08-12 18:13:03 -0700151 mContentHeight = height;
152 }
153
Adam Powell89e06452010-06-23 20:24:52 -0700154 public void setCustomView(View view) {
155 if (mCustomView != null) {
156 removeView(mCustomView);
157 }
158 mCustomView = view;
Clara Bayarried2a54c2015-02-05 16:58:00 +0000159 if (view != null && mTitleLayout != null) {
Adam Powell0e94b512010-06-29 17:58:20 -0700160 removeView(mTitleLayout);
161 mTitleLayout = null;
Adam Powell89e06452010-06-23 20:24:52 -0700162 }
163 if (view != null) {
164 addView(view);
165 }
166 requestLayout();
167 }
Adam Powell0e94b512010-06-29 17:58:20 -0700168
Adam Powell89e06452010-06-23 20:24:52 -0700169 public void setTitle(CharSequence title) {
170 mTitle = title;
Adam Powell0e94b512010-06-29 17:58:20 -0700171 initTitle();
172 }
173
174 public void setSubtitle(CharSequence subtitle) {
175 mSubtitle = subtitle;
176 initTitle();
177 }
178
Adam Powell29ed7572010-07-14 16:24:56 -0700179 public CharSequence getTitle() {
180 return mTitle;
181 }
182
183 public CharSequence getSubtitle() {
184 return mSubtitle;
185 }
186
Adam Powell0e94b512010-06-29 17:58:20 -0700187 private void initTitle() {
188 if (mTitleLayout == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700189 LayoutInflater inflater = LayoutInflater.from(getContext());
Adam Powellf16888f2010-10-11 17:05:29 -0700190 inflater.inflate(R.layout.action_bar_title_item, this);
191 mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
Adam Powell0e94b512010-06-29 17:58:20 -0700192 mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
193 mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
Gilles Debunnec619e742011-05-31 15:49:51 -0700194 if (mTitleStyleRes != 0) {
195 mTitleView.setTextAppearance(mContext, mTitleStyleRes);
Adam Powell89e06452010-06-23 20:24:52 -0700196 }
Gilles Debunnec619e742011-05-31 15:49:51 -0700197 if (mSubtitleStyleRes != 0) {
198 mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes);
Adam Powell0e94b512010-06-29 17:58:20 -0700199 }
Gilles Debunnec619e742011-05-31 15:49:51 -0700200 }
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 Powell89e06452010-06-23 20:24:52 -0700211 }
212 }
Adam Powell0e94b512010-06-29 17:58:20 -0700213
Adam Powell6e346362010-07-23 10:18:23 -0700214 public void initForMode(final ActionMode mode) {
Adam Powellf16888f2010-10-11 17:05:29 -0700215 if (mClose == null) {
216 LayoutInflater inflater = LayoutInflater.from(mContext);
Adam Powellb2536cf2014-09-03 17:17:45 -0700217 mClose = inflater.inflate(mCloseItemLayout, this, false);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800218 addView(mClose);
Adam Powell45f1e082010-12-10 14:20:13 -0800219 } else if (mClose.getParent() == null) {
Adam Powellf16888f2010-10-11 17:05:29 -0700220 addView(mClose);
Adam Powell89e06452010-06-23 20:24:52 -0700221 }
Adam Powellf16888f2010-10-11 17:05:29 -0700222
223 View closeButton = mClose.findViewById(R.id.action_mode_close_button);
224 closeButton.setOnClickListener(new OnClickListener() {
Adam Powell269b8bb2010-07-20 16:46:42 -0700225 public void onClick(View v) {
226 mode.finish();
227 }
228 });
Adam Powell89e06452010-06-23 20:24:52 -0700229
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700230 final MenuBuilder menu = (MenuBuilder) mode.getMenu();
Adam Powell678ed0c2011-10-27 17:46:07 -0700231 if (mActionMenuPresenter != null) {
232 mActionMenuPresenter.dismissPopupMenus();
233 }
Adam Powell538e5652011-10-11 13:47:08 -0700234 mActionMenuPresenter = new ActionMenuPresenter(mContext);
Adam Powell1ab418a2011-06-09 20:49:49 -0700235 mActionMenuPresenter.setReserveOverflow(true);
Adam Powell640a66e2011-04-29 10:18:53 -0700236
237 final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
238 LayoutParams.MATCH_PARENT);
Adam Powella05aba92011-09-23 14:22:49 -0700239 if (!mSplitActionBar) {
Alan Viverette3d0f21d2014-07-10 16:15:01 -0700240 menu.addMenuPresenter(mActionMenuPresenter, mPopupContext);
Adam Powell1ab418a2011-06-09 20:49:49 -0700241 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
Alan Viverettef6a36e92014-10-28 17:50:59 -0700242 mMenuView.setBackground(null);
Adam Powell1ab418a2011-06-09 20:49:49 -0700243 addView(mMenuView, layoutParams);
Adam Powell640a66e2011-04-29 10:18:53 -0700244 } else {
245 // Allow full screen width in split mode.
Adam Powell8d02dea2011-05-31 21:35:13 -0700246 mActionMenuPresenter.setWidthLimit(
Adam Powell640a66e2011-04-29 10:18:53 -0700247 getContext().getResources().getDisplayMetrics().widthPixels, true);
248 // No limit to the item count; use whatever will fit.
Adam Powell8d02dea2011-05-31 21:35:13 -0700249 mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
Adam Powell640a66e2011-04-29 10:18:53 -0700250 // Span the whole width
251 layoutParams.width = LayoutParams.MATCH_PARENT;
Adam Powell1ab418a2011-06-09 20:49:49 -0700252 layoutParams.height = mContentHeight;
Alan Viverette3d0f21d2014-07-10 16:15:01 -0700253 menu.addMenuPresenter(mActionMenuPresenter, mPopupContext);
Adam Powell1ab418a2011-06-09 20:49:49 -0700254 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
Adam Powell9ca705e2011-08-11 16:57:59 -0700255 mMenuView.setBackgroundDrawable(mSplitBackground);
Adam Powell1ab418a2011-06-09 20:49:49 -0700256 mSplitView.addView(mMenuView, layoutParams);
Adam Powell640a66e2011-04-29 10:18:53 -0700257 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800258
259 mAnimateInOnLayout = true;
Adam Powell89e06452010-06-23 20:24:52 -0700260 }
Adam Powell0e94b512010-06-29 17:58:20 -0700261
Adam Powell89e06452010-06-23 20:24:52 -0700262 public void closeMode() {
Adam Powell45f1e082010-12-10 14:20:13 -0800263 if (mAnimationMode == ANIMATE_OUT) {
264 // Called again during close; just finish what we were doing.
265 return;
266 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800267 if (mClose == null) {
268 killMode();
269 return;
270 }
271
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800272 finishAnimation();
Adam Powella1e63582011-01-18 16:51:22 -0800273 mAnimationMode = ANIMATE_OUT;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800274 mCurrentAnimation = makeOutAnimation();
275 mCurrentAnimation.start();
276 }
277
278 private void finishAnimation() {
Adam Powell45f1e082010-12-10 14:20:13 -0800279 final Animator a = mCurrentAnimation;
Adam Powella1e63582011-01-18 16:51:22 -0800280 if (a != null) {
Adam Powell45f1e082010-12-10 14:20:13 -0800281 mCurrentAnimation = null;
282 a.end();
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800283 }
284 }
285
286 public void killMode() {
287 finishAnimation();
Adam Powell89e06452010-06-23 20:24:52 -0700288 removeAllViews();
Adam Powell640a66e2011-04-29 10:18:53 -0700289 if (mSplitView != null) {
290 mSplitView.removeView(mMenuView);
291 }
Adam Powell89e06452010-06-23 20:24:52 -0700292 mCustomView = null;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700293 mMenuView = null;
Adam Powell00bba682011-01-08 15:53:38 -0800294 mAnimateInOnLayout = false;
Adam Powell89e06452010-06-23 20:24:52 -0700295 }
Adam Powell0e94b512010-06-29 17:58:20 -0700296
Gilles Debunnec619e742011-05-31 15:49:51 -0700297 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700298 public boolean showOverflowMenu() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700299 if (mActionMenuPresenter != null) {
300 return mActionMenuPresenter.showOverflowMenu();
Adam Powellf6148c52010-08-11 21:10:16 -0700301 }
302 return false;
303 }
304
Gilles Debunnec619e742011-05-31 15:49:51 -0700305 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700306 public boolean hideOverflowMenu() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700307 if (mActionMenuPresenter != null) {
308 return mActionMenuPresenter.hideOverflowMenu();
Adam Powellf6148c52010-08-11 21:10:16 -0700309 }
310 return false;
311 }
312
Gilles Debunnec619e742011-05-31 15:49:51 -0700313 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700314 public boolean isOverflowMenuShowing() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700315 if (mActionMenuPresenter != null) {
316 return mActionMenuPresenter.isOverflowMenuShowing();
Adam Powellf6148c52010-08-11 21:10:16 -0700317 }
318 return false;
319 }
320
Adam Powell89e06452010-06-23 20:24:52 -0700321 @Override
Adam Powell9a5cc282011-08-28 16:18:16 -0700322 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
Adam Powella7db0372010-06-30 17:08:47 -0700323 // Used by custom views if they don't supply layout params. Everything else
324 // added to an ActionBarContextView should have them already.
Adam Powell9ca705e2011-08-11 16:57:59 -0700325 return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
326 }
327
328 @Override
Adam Powell9a5cc282011-08-28 16:18:16 -0700329 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Adam Powell9ca705e2011-08-11 16:57:59 -0700330 return new MarginLayoutParams(getContext(), attrs);
Adam Powella7db0372010-06-30 17:08:47 -0700331 }
332
333 @Override
Adam Powell89e06452010-06-23 20:24:52 -0700334 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 Powell85446e92010-10-22 17:43:56 -0700342 if (heightMode == MeasureSpec.UNSPECIFIED) {
Adam Powell89e06452010-06-23 20:24:52 -0700343 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 Powell89e06452010-06-23 20:24:52 -0700348
Adam Powelle2194442010-08-12 18:13:03 -0700349 int maxHeight = mContentHeight > 0 ?
350 mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
351
352 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Powell89e06452010-06-23 20:24:52 -0700353 int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
Adam Powelle2194442010-08-12 18:13:03 -0700354 final int height = maxHeight - verticalPadding;
Adam Powell89e06452010-06-23 20:24:52 -0700355 final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
356
Adam Powellf16888f2010-10-11 17:05:29 -0700357 if (mClose != null) {
358 availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0);
Adam Powell9ca705e2011-08-11 16:57:59 -0700359 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
360 availableWidth -= lp.leftMargin + lp.rightMargin;
Adam Powell89e06452010-06-23 20:24:52 -0700361 }
362
Adam Powellf6ce6a92011-06-29 10:25:01 -0700363 if (mMenuView != null && mMenuView.getParent() == this) {
Adam Powellb0ff6f92011-01-23 18:07:45 -0800364 availableWidth = measureChildView(mMenuView, availableWidth,
365 childSpecHeight, 0);
Adam Powell89e06452010-06-23 20:24:52 -0700366 }
367
Adam Powellb0ff6f92011-01-23 18:07:45 -0800368 if (mTitleLayout != null && mCustomView == null) {
Adam Powellb98a81f2012-02-24 11:09:07 -0800369 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 Powell89e06452010-06-23 20:24:52 -0700381 }
382
383 if (mCustomView != null) {
Adam Powell9a5cc282011-08-28 16:18:16 -0700384 ViewGroup.LayoutParams lp = mCustomView.getLayoutParams();
Adam Powella7db0372010-06-30 17:08:47 -0700385 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 Powell89e06452010-06-23 20:24:52 -0700395 }
396
Adam Powelle2194442010-08-12 18:13:03 -0700397 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 Powell89e06452010-06-23 20:24:52 -0700411 }
412
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800413 private Animator makeInAnimation() {
Adam Powell9ca705e2011-08-11 16:57:59 -0700414 mClose.setTranslationX(-mClose.getWidth() -
415 ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800416 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 Powell482ae5f2012-02-17 15:18:01 -0800431 a.setDuration(300);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800432 b.with(a);
433 }
434 }
435 }
436
437 return set;
438 }
439
440 private Animator makeOutAnimation() {
441 ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX",
Adam Powell9ca705e2011-08-11 16:57:59 -0700442 -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800443 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 Powell640a66e2011-04-29 10:18:53 -0700456 ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0);
Adam Powell482ae5f2012-02-17 15:18:01 -0800457 a.setDuration(300);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800458 b.with(a);
459 }
460 }
461 }
462
463 return set;
464 }
465
Adam Powell89e06452010-06-23 20:24:52 -0700466 @Override
467 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700468 final boolean isLayoutRtl = isLayoutRtl();
469 int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft();
Adam Powell89e06452010-06-23 20:24:52 -0700470 final int y = getPaddingTop();
471 final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
Adam Powell89e06452010-06-23 20:24:52 -0700472
Adam Powellf16888f2010-10-11 17:05:29 -0700473 if (mClose != null && mClose.getVisibility() != GONE) {
Adam Powell9ca705e2011-08-11 16:57:59 -0700474 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700475 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 Powelld8b3f2e2010-12-02 13:37:03 -0800480
481 if (mAnimateInOnLayout) {
482 mAnimationMode = ANIMATE_IN;
483 mCurrentAnimation = makeInAnimation();
484 mCurrentAnimation.start();
485 mAnimateInOnLayout = false;
486 }
Adam Powell89e06452010-06-23 20:24:52 -0700487 }
Adam Powell640a66e2011-04-29 10:18:53 -0700488
Adam Powellb98a81f2012-02-24 11:09:07 -0800489 if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700490 x += positionChild(mTitleLayout, x, y, contentHeight, isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700491 }
492
493 if (mCustomView != null) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700494 x += positionChild(mCustomView, x, y, contentHeight, isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700495 }
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700496
497 x = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight();
Adam Powell89e06452010-06-23 20:24:52 -0700498
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700499 if (mMenuView != null) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700500 x += positionChild(mMenuView, x, y, contentHeight, !isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700501 }
502 }
503
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800504 @Override
505 public void onAnimationStart(Animator animation) {
506 }
507
508 @Override
509 public void onAnimationEnd(Animator animation) {
Adam Powella1e63582011-01-18 16:51:22 -0800510 if (mAnimationMode == ANIMATE_OUT) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800511 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 Dubroye0a799a2011-05-04 16:19:22 -0700523
524 @Override
525 public boolean shouldDelayChildPressedState() {
526 return false;
527 }
Adam Powell86ed4362011-09-14 16:18:53 -0700528
529 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800530 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Adam Powell86ed4362011-09-14 16:18:53 -0700531 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 Viverettea54956a2015-01-07 16:05:02 -0800538 super.onInitializeAccessibilityEventInternal(event);
Adam Powell86ed4362011-09-14 16:18:53 -0700539 }
540 }
Adam Powellb98a81f2012-02-24 11:09:07 -0800541
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 Powell89e06452010-06-23 20:24:52 -0700552}