blob: e10070fb30c0fa86d1e0edac8047b246a9804160 [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;
Adam Powellfa18d182014-01-07 15:56:59 -080019import android.widget.ActionMenuPresenter;
20import android.widget.ActionMenuView;
Adam Powell9ca705e2011-08-11 16:57:59 -070021import com.android.internal.view.menu.MenuBuilder;
22
Adam Powelld8b3f2e2010-12-02 13:37:03 -080023import android.animation.Animator;
24import android.animation.Animator.AnimatorListener;
25import android.animation.AnimatorSet;
26import android.animation.ObjectAnimator;
Adam Powell89e06452010-06-23 20:24:52 -070027import android.content.Context;
28import android.content.res.TypedArray;
Adam Powell9ca705e2011-08-11 16:57:59 -070029import android.graphics.drawable.Drawable;
Gilles Debunnec619e742011-05-31 15:49:51 -070030import android.text.TextUtils;
Adam Powell89e06452010-06-23 20:24:52 -070031import android.util.AttributeSet;
Adam Powell6e346362010-07-23 10:18:23 -070032import android.view.ActionMode;
Adam Powell89e06452010-06-23 20:24:52 -070033import android.view.LayoutInflater;
Adam Powell89e06452010-06-23 20:24:52 -070034import android.view.View;
Adam Powell9a5cc282011-08-28 16:18:16 -070035import android.view.ViewGroup;
Adam Powell86ed4362011-09-14 16:18:53 -070036import android.view.accessibility.AccessibilityEvent;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080037import android.view.animation.DecelerateInterpolator;
Adam Powell0e94b512010-06-29 17:58:20 -070038import android.widget.LinearLayout;
Adam Powell89e06452010-06-23 20:24:52 -070039import android.widget.TextView;
40
41/**
42 * @hide
43 */
Adam Powell640a66e2011-04-29 10:18:53 -070044public class ActionBarContextView extends AbsActionBarView implements AnimatorListener {
Adam Powell77769c72011-01-17 12:05:08 -080045 private static final String TAG = "ActionBarContextView";
46
Adam Powell89e06452010-06-23 20:24:52 -070047 private CharSequence mTitle;
48 private CharSequence mSubtitle;
Adam Powellf16888f2010-10-11 17:05:29 -070049
50 private View mClose;
Adam Powell89e06452010-06-23 20:24:52 -070051 private View mCustomView;
Adam Powell0e94b512010-06-29 17:58:20 -070052 private LinearLayout mTitleLayout;
Adam Powell89e06452010-06-23 20:24:52 -070053 private TextView mTitleView;
Adam Powell0e94b512010-06-29 17:58:20 -070054 private TextView mSubtitleView;
Adam Powellbc234a12010-09-09 17:03:50 -070055 private int mTitleStyleRes;
56 private int mSubtitleStyleRes;
Adam Powell9ca705e2011-08-11 16:57:59 -070057 private Drawable mSplitBackground;
Adam Powellb98a81f2012-02-24 11:09:07 -080058 private boolean mTitleOptional;
Adam Powelld8b3f2e2010-12-02 13:37:03 -080059
60 private Animator mCurrentAnimation;
61 private boolean mAnimateInOnLayout;
62 private int mAnimationMode;
63
64 private static final int ANIMATE_IDLE = 0;
65 private static final int ANIMATE_IN = 1;
66 private static final int ANIMATE_OUT = 2;
Adam Powell89e06452010-06-23 20:24:52 -070067
68 public ActionBarContextView(Context context) {
Adam Powellbc234a12010-09-09 17:03:50 -070069 this(context, null);
Adam Powell89e06452010-06-23 20:24:52 -070070 }
71
72 public ActionBarContextView(Context context, AttributeSet attrs) {
Adam Powellbc234a12010-09-09 17:03:50 -070073 this(context, attrs, com.android.internal.R.attr.actionModeStyle);
Adam Powell89e06452010-06-23 20:24:52 -070074 }
75
Alan Viverette617feb92013-09-09 18:09:13 -070076 public ActionBarContextView(Context context, AttributeSet attrs, int defStyleAttr) {
77 this(context, attrs, defStyleAttr, 0);
78 }
79
80 public ActionBarContextView(
81 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
82 super(context, attrs, defStyleAttr, defStyleRes);
83
84 final TypedArray a = context.obtainStyledAttributes(
85 attrs, R.styleable.ActionMode, defStyleAttr, defStyleRes);
Adam Powell89e06452010-06-23 20:24:52 -070086 setBackgroundDrawable(a.getDrawable(
Adam Powellbc234a12010-09-09 17:03:50 -070087 com.android.internal.R.styleable.ActionMode_background));
Adam Powellbc234a12010-09-09 17:03:50 -070088 mTitleStyleRes = a.getResourceId(
89 com.android.internal.R.styleable.ActionMode_titleTextStyle, 0);
90 mSubtitleStyleRes = a.getResourceId(
91 com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0);
Adam Powelldcdefbb2010-07-19 15:45:22 -070092
Adam Powelle2194442010-08-12 18:13:03 -070093 mContentHeight = a.getLayoutDimension(
Adam Powellbc234a12010-09-09 17:03:50 -070094 com.android.internal.R.styleable.ActionMode_height, 0);
Adam Powell9ca705e2011-08-11 16:57:59 -070095
96 mSplitBackground = a.getDrawable(
97 com.android.internal.R.styleable.ActionMode_backgroundSplit);
98
Adam Powell89e06452010-06-23 20:24:52 -070099 a.recycle();
100 }
Adam Powell9146ac72010-08-16 18:53:38 -0700101
Adam Powella05aba92011-09-23 14:22:49 -0700102 @Override
Adam Powell97e18362011-10-20 15:37:18 -0700103 public void onDetachedFromWindow() {
104 super.onDetachedFromWindow();
105 if (mActionMenuPresenter != null) {
106 mActionMenuPresenter.hideOverflowMenu();
107 mActionMenuPresenter.hideSubMenus();
108 }
109 }
110
111 @Override
Adam Powella05aba92011-09-23 14:22:49 -0700112 public void setSplitActionBar(boolean split) {
113 if (mSplitActionBar != split) {
114 if (mActionMenuPresenter != null) {
115 // Mode is already active; move everything over and adjust the menu itself.
116 final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
117 LayoutParams.MATCH_PARENT);
118 if (!split) {
119 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
120 mMenuView.setBackgroundDrawable(null);
121 final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
122 if (oldParent != null) oldParent.removeView(mMenuView);
123 addView(mMenuView, layoutParams);
124 } else {
125 // Allow full screen width in split mode.
126 mActionMenuPresenter.setWidthLimit(
127 getContext().getResources().getDisplayMetrics().widthPixels, true);
128 // No limit to the item count; use whatever will fit.
129 mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
130 // Span the whole width
131 layoutParams.width = LayoutParams.MATCH_PARENT;
132 layoutParams.height = mContentHeight;
133 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
134 mMenuView.setBackgroundDrawable(mSplitBackground);
135 final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
136 if (oldParent != null) oldParent.removeView(mMenuView);
137 mSplitView.addView(mMenuView, layoutParams);
138 }
139 }
140 super.setSplitActionBar(split);
141 }
142 }
143
Adam Powell425689e2011-09-08 18:09:33 -0700144 public void setContentHeight(int height) {
Adam Powelle2194442010-08-12 18:13:03 -0700145 mContentHeight = height;
146 }
147
Adam Powell89e06452010-06-23 20:24:52 -0700148 public void setCustomView(View view) {
149 if (mCustomView != null) {
150 removeView(mCustomView);
151 }
152 mCustomView = view;
Adam Powell0e94b512010-06-29 17:58:20 -0700153 if (mTitleLayout != null) {
154 removeView(mTitleLayout);
155 mTitleLayout = null;
Adam Powell89e06452010-06-23 20:24:52 -0700156 }
157 if (view != null) {
158 addView(view);
159 }
160 requestLayout();
161 }
Adam Powell0e94b512010-06-29 17:58:20 -0700162
Adam Powell89e06452010-06-23 20:24:52 -0700163 public void setTitle(CharSequence title) {
164 mTitle = title;
Adam Powell0e94b512010-06-29 17:58:20 -0700165 initTitle();
166 }
167
168 public void setSubtitle(CharSequence subtitle) {
169 mSubtitle = subtitle;
170 initTitle();
171 }
172
Adam Powell29ed7572010-07-14 16:24:56 -0700173 public CharSequence getTitle() {
174 return mTitle;
175 }
176
177 public CharSequence getSubtitle() {
178 return mSubtitle;
179 }
180
Adam Powell0e94b512010-06-29 17:58:20 -0700181 private void initTitle() {
182 if (mTitleLayout == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700183 LayoutInflater inflater = LayoutInflater.from(getContext());
Adam Powellf16888f2010-10-11 17:05:29 -0700184 inflater.inflate(R.layout.action_bar_title_item, this);
185 mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
Adam Powell0e94b512010-06-29 17:58:20 -0700186 mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
187 mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
Gilles Debunnec619e742011-05-31 15:49:51 -0700188 if (mTitleStyleRes != 0) {
189 mTitleView.setTextAppearance(mContext, mTitleStyleRes);
Adam Powell89e06452010-06-23 20:24:52 -0700190 }
Gilles Debunnec619e742011-05-31 15:49:51 -0700191 if (mSubtitleStyleRes != 0) {
192 mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes);
Adam Powell0e94b512010-06-29 17:58:20 -0700193 }
Gilles Debunnec619e742011-05-31 15:49:51 -0700194 }
195
196 mTitleView.setText(mTitle);
197 mSubtitleView.setText(mSubtitle);
198
199 final boolean hasTitle = !TextUtils.isEmpty(mTitle);
200 final boolean hasSubtitle = !TextUtils.isEmpty(mSubtitle);
201 mSubtitleView.setVisibility(hasSubtitle ? VISIBLE : GONE);
202 mTitleLayout.setVisibility(hasTitle || hasSubtitle ? VISIBLE : GONE);
203 if (mTitleLayout.getParent() == null) {
204 addView(mTitleLayout);
Adam Powell89e06452010-06-23 20:24:52 -0700205 }
206 }
Adam Powell0e94b512010-06-29 17:58:20 -0700207
Adam Powell6e346362010-07-23 10:18:23 -0700208 public void initForMode(final ActionMode mode) {
Adam Powellf16888f2010-10-11 17:05:29 -0700209 if (mClose == null) {
210 LayoutInflater inflater = LayoutInflater.from(mContext);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800211 mClose = inflater.inflate(R.layout.action_mode_close_item, this, false);
212 addView(mClose);
Adam Powell45f1e082010-12-10 14:20:13 -0800213 } else if (mClose.getParent() == null) {
Adam Powellf16888f2010-10-11 17:05:29 -0700214 addView(mClose);
Adam Powell89e06452010-06-23 20:24:52 -0700215 }
Adam Powellf16888f2010-10-11 17:05:29 -0700216
217 View closeButton = mClose.findViewById(R.id.action_mode_close_button);
218 closeButton.setOnClickListener(new OnClickListener() {
Adam Powell269b8bb2010-07-20 16:46:42 -0700219 public void onClick(View v) {
220 mode.finish();
221 }
222 });
Adam Powell89e06452010-06-23 20:24:52 -0700223
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700224 final MenuBuilder menu = (MenuBuilder) mode.getMenu();
Adam Powell678ed0c2011-10-27 17:46:07 -0700225 if (mActionMenuPresenter != null) {
226 mActionMenuPresenter.dismissPopupMenus();
227 }
Adam Powell538e5652011-10-11 13:47:08 -0700228 mActionMenuPresenter = new ActionMenuPresenter(mContext);
Adam Powell1ab418a2011-06-09 20:49:49 -0700229 mActionMenuPresenter.setReserveOverflow(true);
Adam Powell640a66e2011-04-29 10:18:53 -0700230
231 final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
232 LayoutParams.MATCH_PARENT);
Adam Powella05aba92011-09-23 14:22:49 -0700233 if (!mSplitActionBar) {
Adam Powell1ab418a2011-06-09 20:49:49 -0700234 menu.addMenuPresenter(mActionMenuPresenter);
235 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
Adam Powell9ca705e2011-08-11 16:57:59 -0700236 mMenuView.setBackgroundDrawable(null);
Adam Powell1ab418a2011-06-09 20:49:49 -0700237 addView(mMenuView, layoutParams);
Adam Powell640a66e2011-04-29 10:18:53 -0700238 } else {
239 // Allow full screen width in split mode.
Adam Powell8d02dea2011-05-31 21:35:13 -0700240 mActionMenuPresenter.setWidthLimit(
Adam Powell640a66e2011-04-29 10:18:53 -0700241 getContext().getResources().getDisplayMetrics().widthPixels, true);
242 // No limit to the item count; use whatever will fit.
Adam Powell8d02dea2011-05-31 21:35:13 -0700243 mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
Adam Powell640a66e2011-04-29 10:18:53 -0700244 // Span the whole width
245 layoutParams.width = LayoutParams.MATCH_PARENT;
Adam Powell1ab418a2011-06-09 20:49:49 -0700246 layoutParams.height = mContentHeight;
247 menu.addMenuPresenter(mActionMenuPresenter);
248 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
Adam Powell9ca705e2011-08-11 16:57:59 -0700249 mMenuView.setBackgroundDrawable(mSplitBackground);
Adam Powell1ab418a2011-06-09 20:49:49 -0700250 mSplitView.addView(mMenuView, layoutParams);
Adam Powell640a66e2011-04-29 10:18:53 -0700251 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800252
253 mAnimateInOnLayout = true;
Adam Powell89e06452010-06-23 20:24:52 -0700254 }
Adam Powell0e94b512010-06-29 17:58:20 -0700255
Adam Powell89e06452010-06-23 20:24:52 -0700256 public void closeMode() {
Adam Powell45f1e082010-12-10 14:20:13 -0800257 if (mAnimationMode == ANIMATE_OUT) {
258 // Called again during close; just finish what we were doing.
259 return;
260 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800261 if (mClose == null) {
262 killMode();
263 return;
264 }
265
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800266 finishAnimation();
Adam Powella1e63582011-01-18 16:51:22 -0800267 mAnimationMode = ANIMATE_OUT;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800268 mCurrentAnimation = makeOutAnimation();
269 mCurrentAnimation.start();
270 }
271
272 private void finishAnimation() {
Adam Powell45f1e082010-12-10 14:20:13 -0800273 final Animator a = mCurrentAnimation;
Adam Powella1e63582011-01-18 16:51:22 -0800274 if (a != null) {
Adam Powell45f1e082010-12-10 14:20:13 -0800275 mCurrentAnimation = null;
276 a.end();
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800277 }
278 }
279
280 public void killMode() {
281 finishAnimation();
Adam Powell89e06452010-06-23 20:24:52 -0700282 removeAllViews();
Adam Powell640a66e2011-04-29 10:18:53 -0700283 if (mSplitView != null) {
284 mSplitView.removeView(mMenuView);
285 }
Adam Powell89e06452010-06-23 20:24:52 -0700286 mCustomView = null;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700287 mMenuView = null;
Adam Powell00bba682011-01-08 15:53:38 -0800288 mAnimateInOnLayout = false;
Adam Powell89e06452010-06-23 20:24:52 -0700289 }
Adam Powell0e94b512010-06-29 17:58:20 -0700290
Gilles Debunnec619e742011-05-31 15:49:51 -0700291 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700292 public boolean showOverflowMenu() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700293 if (mActionMenuPresenter != null) {
294 return mActionMenuPresenter.showOverflowMenu();
Adam Powellf6148c52010-08-11 21:10:16 -0700295 }
296 return false;
297 }
298
Gilles Debunnec619e742011-05-31 15:49:51 -0700299 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700300 public boolean hideOverflowMenu() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700301 if (mActionMenuPresenter != null) {
302 return mActionMenuPresenter.hideOverflowMenu();
Adam Powellf6148c52010-08-11 21:10:16 -0700303 }
304 return false;
305 }
306
Gilles Debunnec619e742011-05-31 15:49:51 -0700307 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700308 public boolean isOverflowMenuShowing() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700309 if (mActionMenuPresenter != null) {
310 return mActionMenuPresenter.isOverflowMenuShowing();
Adam Powellf6148c52010-08-11 21:10:16 -0700311 }
312 return false;
313 }
314
Adam Powell89e06452010-06-23 20:24:52 -0700315 @Override
Adam Powell9a5cc282011-08-28 16:18:16 -0700316 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
Adam Powella7db0372010-06-30 17:08:47 -0700317 // Used by custom views if they don't supply layout params. Everything else
318 // added to an ActionBarContextView should have them already.
Adam Powell9ca705e2011-08-11 16:57:59 -0700319 return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
320 }
321
322 @Override
Adam Powell9a5cc282011-08-28 16:18:16 -0700323 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Adam Powell9ca705e2011-08-11 16:57:59 -0700324 return new MarginLayoutParams(getContext(), attrs);
Adam Powella7db0372010-06-30 17:08:47 -0700325 }
326
327 @Override
Adam Powell89e06452010-06-23 20:24:52 -0700328 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
329 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
330 if (widthMode != MeasureSpec.EXACTLY) {
331 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
332 "with android:layout_width=\"match_parent\" (or fill_parent)");
333 }
334
335 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Powell85446e92010-10-22 17:43:56 -0700336 if (heightMode == MeasureSpec.UNSPECIFIED) {
Adam Powell89e06452010-06-23 20:24:52 -0700337 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
338 "with android:layout_height=\"wrap_content\"");
339 }
340
341 final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
Adam Powell89e06452010-06-23 20:24:52 -0700342
Adam Powelle2194442010-08-12 18:13:03 -0700343 int maxHeight = mContentHeight > 0 ?
344 mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
345
346 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Powell89e06452010-06-23 20:24:52 -0700347 int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
Adam Powelle2194442010-08-12 18:13:03 -0700348 final int height = maxHeight - verticalPadding;
Adam Powell89e06452010-06-23 20:24:52 -0700349 final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
350
Adam Powellf16888f2010-10-11 17:05:29 -0700351 if (mClose != null) {
352 availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0);
Adam Powell9ca705e2011-08-11 16:57:59 -0700353 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
354 availableWidth -= lp.leftMargin + lp.rightMargin;
Adam Powell89e06452010-06-23 20:24:52 -0700355 }
356
Adam Powellf6ce6a92011-06-29 10:25:01 -0700357 if (mMenuView != null && mMenuView.getParent() == this) {
Adam Powellb0ff6f92011-01-23 18:07:45 -0800358 availableWidth = measureChildView(mMenuView, availableWidth,
359 childSpecHeight, 0);
Adam Powell89e06452010-06-23 20:24:52 -0700360 }
361
Adam Powellb0ff6f92011-01-23 18:07:45 -0800362 if (mTitleLayout != null && mCustomView == null) {
Adam Powellb98a81f2012-02-24 11:09:07 -0800363 if (mTitleOptional) {
364 final int titleWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
365 mTitleLayout.measure(titleWidthSpec, childSpecHeight);
366 final int titleWidth = mTitleLayout.getMeasuredWidth();
367 final boolean titleFits = titleWidth <= availableWidth;
368 if (titleFits) {
369 availableWidth -= titleWidth;
370 }
371 mTitleLayout.setVisibility(titleFits ? VISIBLE : GONE);
372 } else {
373 availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
374 }
Adam Powell89e06452010-06-23 20:24:52 -0700375 }
376
377 if (mCustomView != null) {
Adam Powell9a5cc282011-08-28 16:18:16 -0700378 ViewGroup.LayoutParams lp = mCustomView.getLayoutParams();
Adam Powella7db0372010-06-30 17:08:47 -0700379 final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ?
380 MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
381 final int customWidth = lp.width >= 0 ?
382 Math.min(lp.width, availableWidth) : availableWidth;
383 final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ?
384 MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
385 final int customHeight = lp.height >= 0 ?
386 Math.min(lp.height, height) : height;
387 mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode),
388 MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
Adam Powell89e06452010-06-23 20:24:52 -0700389 }
390
Adam Powelle2194442010-08-12 18:13:03 -0700391 if (mContentHeight <= 0) {
392 int measuredHeight = 0;
393 final int count = getChildCount();
394 for (int i = 0; i < count; i++) {
395 View v = getChildAt(i);
396 int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
397 if (paddedViewHeight > measuredHeight) {
398 measuredHeight = paddedViewHeight;
399 }
400 }
401 setMeasuredDimension(contentWidth, measuredHeight);
402 } else {
403 setMeasuredDimension(contentWidth, maxHeight);
404 }
Adam Powell89e06452010-06-23 20:24:52 -0700405 }
406
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800407 private Animator makeInAnimation() {
Adam Powell9ca705e2011-08-11 16:57:59 -0700408 mClose.setTranslationX(-mClose.getWidth() -
409 ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800410 ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0);
411 buttonAnimator.setDuration(200);
412 buttonAnimator.addListener(this);
413 buttonAnimator.setInterpolator(new DecelerateInterpolator());
414
415 AnimatorSet set = new AnimatorSet();
416 AnimatorSet.Builder b = set.play(buttonAnimator);
417
418 if (mMenuView != null) {
419 final int count = mMenuView.getChildCount();
420 if (count > 0) {
421 for (int i = count - 1, j = 0; i >= 0; i--, j++) {
422 View child = mMenuView.getChildAt(i);
423 child.setScaleY(0);
424 ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1);
Adam Powell482ae5f2012-02-17 15:18:01 -0800425 a.setDuration(300);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800426 b.with(a);
427 }
428 }
429 }
430
431 return set;
432 }
433
434 private Animator makeOutAnimation() {
435 ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX",
Adam Powell9ca705e2011-08-11 16:57:59 -0700436 -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800437 buttonAnimator.setDuration(200);
438 buttonAnimator.addListener(this);
439 buttonAnimator.setInterpolator(new DecelerateInterpolator());
440
441 AnimatorSet set = new AnimatorSet();
442 AnimatorSet.Builder b = set.play(buttonAnimator);
443
444 if (mMenuView != null) {
445 final int count = mMenuView.getChildCount();
446 if (count > 0) {
447 for (int i = 0; i < 0; i++) {
448 View child = mMenuView.getChildAt(i);
449 child.setScaleY(0);
Adam Powell640a66e2011-04-29 10:18:53 -0700450 ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0);
Adam Powell482ae5f2012-02-17 15:18:01 -0800451 a.setDuration(300);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800452 b.with(a);
453 }
454 }
455 }
456
457 return set;
458 }
459
Adam Powell89e06452010-06-23 20:24:52 -0700460 @Override
461 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700462 final boolean isLayoutRtl = isLayoutRtl();
463 int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft();
Adam Powell89e06452010-06-23 20:24:52 -0700464 final int y = getPaddingTop();
465 final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
Adam Powell89e06452010-06-23 20:24:52 -0700466
Adam Powellf16888f2010-10-11 17:05:29 -0700467 if (mClose != null && mClose.getVisibility() != GONE) {
Adam Powell9ca705e2011-08-11 16:57:59 -0700468 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700469 final int startMargin = (isLayoutRtl ? lp.rightMargin : lp.leftMargin);
470 final int endMargin = (isLayoutRtl ? lp.leftMargin : lp.rightMargin);
471 x = next(x, startMargin, isLayoutRtl);
472 x += positionChild(mClose, x, y, contentHeight, isLayoutRtl);
473 x = next(x, endMargin, isLayoutRtl);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800474
475 if (mAnimateInOnLayout) {
476 mAnimationMode = ANIMATE_IN;
477 mCurrentAnimation = makeInAnimation();
478 mCurrentAnimation.start();
479 mAnimateInOnLayout = false;
480 }
Adam Powell89e06452010-06-23 20:24:52 -0700481 }
Adam Powell640a66e2011-04-29 10:18:53 -0700482
Adam Powellb98a81f2012-02-24 11:09:07 -0800483 if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700484 x += positionChild(mTitleLayout, x, y, contentHeight, isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700485 }
486
487 if (mCustomView != null) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700488 x += positionChild(mCustomView, x, y, contentHeight, isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700489 }
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700490
491 x = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight();
Adam Powell89e06452010-06-23 20:24:52 -0700492
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700493 if (mMenuView != null) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700494 x += positionChild(mMenuView, x, y, contentHeight, !isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700495 }
496 }
497
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800498 @Override
499 public void onAnimationStart(Animator animation) {
500 }
501
502 @Override
503 public void onAnimationEnd(Animator animation) {
Adam Powella1e63582011-01-18 16:51:22 -0800504 if (mAnimationMode == ANIMATE_OUT) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800505 killMode();
506 }
507 mAnimationMode = ANIMATE_IDLE;
508 }
509
510 @Override
511 public void onAnimationCancel(Animator animation) {
512 }
513
514 @Override
515 public void onAnimationRepeat(Animator animation) {
516 }
Patrick Dubroye0a799a2011-05-04 16:19:22 -0700517
518 @Override
519 public boolean shouldDelayChildPressedState() {
520 return false;
521 }
Adam Powell86ed4362011-09-14 16:18:53 -0700522
523 @Override
524 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
525 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
526 // Action mode started
527 event.setSource(this);
528 event.setClassName(getClass().getName());
529 event.setPackageName(getContext().getPackageName());
530 event.setContentDescription(mTitle);
531 } else {
532 super.onInitializeAccessibilityEvent(event);
533 }
534 }
Adam Powellb98a81f2012-02-24 11:09:07 -0800535
536 public void setTitleOptional(boolean titleOptional) {
537 if (titleOptional != mTitleOptional) {
538 requestLayout();
539 }
540 mTitleOptional = titleOptional;
541 }
542
543 public boolean isTitleOptional() {
544 return mTitleOptional;
545 }
Adam Powell89e06452010-06-23 20:24:52 -0700546}