blob: 8bc1081a3f97f0e115cec43ad3f888ab39895b47 [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;
19import com.android.internal.view.menu.ActionMenuPresenter;
20import com.android.internal.view.menu.ActionMenuView;
21import 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;
Adam Powellbfcdfaf2011-08-19 11:46:59 -070028import android.content.res.Configuration;
Adam Powell89e06452010-06-23 20:24:52 -070029import 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 Powelld8b3f2e2010-12-02 13:37:03 -080060
61 private Animator mCurrentAnimation;
62 private boolean mAnimateInOnLayout;
63 private int mAnimationMode;
64
65 private static final int ANIMATE_IDLE = 0;
66 private static final int ANIMATE_IN = 1;
67 private static final int ANIMATE_OUT = 2;
Adam Powell89e06452010-06-23 20:24:52 -070068
69 public ActionBarContextView(Context context) {
Adam Powellbc234a12010-09-09 17:03:50 -070070 this(context, null);
Adam Powell89e06452010-06-23 20:24:52 -070071 }
72
73 public ActionBarContextView(Context context, AttributeSet attrs) {
Adam Powellbc234a12010-09-09 17:03:50 -070074 this(context, attrs, com.android.internal.R.attr.actionModeStyle);
Adam Powell89e06452010-06-23 20:24:52 -070075 }
76
77 public ActionBarContextView(Context context, AttributeSet attrs, int defStyle) {
78 super(context, attrs, defStyle);
79
Adam Powellbc234a12010-09-09 17:03:50 -070080 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionMode, defStyle, 0);
Adam Powell89e06452010-06-23 20:24:52 -070081 setBackgroundDrawable(a.getDrawable(
Adam Powellbc234a12010-09-09 17:03:50 -070082 com.android.internal.R.styleable.ActionMode_background));
Adam Powellbc234a12010-09-09 17:03:50 -070083 mTitleStyleRes = a.getResourceId(
84 com.android.internal.R.styleable.ActionMode_titleTextStyle, 0);
85 mSubtitleStyleRes = a.getResourceId(
86 com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0);
Adam Powelldcdefbb2010-07-19 15:45:22 -070087
Adam Powelle2194442010-08-12 18:13:03 -070088 mContentHeight = a.getLayoutDimension(
Adam Powellbc234a12010-09-09 17:03:50 -070089 com.android.internal.R.styleable.ActionMode_height, 0);
Adam Powell9ca705e2011-08-11 16:57:59 -070090
91 mSplitBackground = a.getDrawable(
92 com.android.internal.R.styleable.ActionMode_backgroundSplit);
93
Adam Powell89e06452010-06-23 20:24:52 -070094 a.recycle();
95 }
Adam Powell9146ac72010-08-16 18:53:38 -070096
Adam Powella05aba92011-09-23 14:22:49 -070097 @Override
Adam Powell97e18362011-10-20 15:37:18 -070098 public void onDetachedFromWindow() {
99 super.onDetachedFromWindow();
100 if (mActionMenuPresenter != null) {
101 mActionMenuPresenter.hideOverflowMenu();
102 mActionMenuPresenter.hideSubMenus();
103 }
104 }
105
106 @Override
Adam Powella05aba92011-09-23 14:22:49 -0700107 public void setSplitActionBar(boolean split) {
108 if (mSplitActionBar != split) {
109 if (mActionMenuPresenter != null) {
110 // Mode is already active; move everything over and adjust the menu itself.
111 final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
112 LayoutParams.MATCH_PARENT);
113 if (!split) {
114 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
115 mMenuView.setBackgroundDrawable(null);
116 final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
117 if (oldParent != null) oldParent.removeView(mMenuView);
118 addView(mMenuView, layoutParams);
119 } else {
120 // Allow full screen width in split mode.
121 mActionMenuPresenter.setWidthLimit(
122 getContext().getResources().getDisplayMetrics().widthPixels, true);
123 // No limit to the item count; use whatever will fit.
124 mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
125 // Span the whole width
126 layoutParams.width = LayoutParams.MATCH_PARENT;
127 layoutParams.height = mContentHeight;
128 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
129 mMenuView.setBackgroundDrawable(mSplitBackground);
130 final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
131 if (oldParent != null) oldParent.removeView(mMenuView);
132 mSplitView.addView(mMenuView, layoutParams);
133 }
134 }
135 super.setSplitActionBar(split);
136 }
137 }
138
Adam Powell425689e2011-09-08 18:09:33 -0700139 public void setContentHeight(int height) {
Adam Powelle2194442010-08-12 18:13:03 -0700140 mContentHeight = height;
141 }
142
Adam Powell89e06452010-06-23 20:24:52 -0700143 public void setCustomView(View view) {
144 if (mCustomView != null) {
145 removeView(mCustomView);
146 }
147 mCustomView = view;
Adam Powell0e94b512010-06-29 17:58:20 -0700148 if (mTitleLayout != null) {
149 removeView(mTitleLayout);
150 mTitleLayout = null;
Adam Powell89e06452010-06-23 20:24:52 -0700151 }
152 if (view != null) {
153 addView(view);
154 }
155 requestLayout();
156 }
Adam Powell0e94b512010-06-29 17:58:20 -0700157
Adam Powell89e06452010-06-23 20:24:52 -0700158 public void setTitle(CharSequence title) {
159 mTitle = title;
Adam Powell0e94b512010-06-29 17:58:20 -0700160 initTitle();
161 }
162
163 public void setSubtitle(CharSequence subtitle) {
164 mSubtitle = subtitle;
165 initTitle();
166 }
167
Adam Powell29ed7572010-07-14 16:24:56 -0700168 public CharSequence getTitle() {
169 return mTitle;
170 }
171
172 public CharSequence getSubtitle() {
173 return mSubtitle;
174 }
175
Adam Powell0e94b512010-06-29 17:58:20 -0700176 private void initTitle() {
177 if (mTitleLayout == null) {
Adam Powell89e06452010-06-23 20:24:52 -0700178 LayoutInflater inflater = LayoutInflater.from(getContext());
Adam Powellf16888f2010-10-11 17:05:29 -0700179 inflater.inflate(R.layout.action_bar_title_item, this);
180 mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
Adam Powell0e94b512010-06-29 17:58:20 -0700181 mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
182 mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
Gilles Debunnec619e742011-05-31 15:49:51 -0700183 if (mTitleStyleRes != 0) {
184 mTitleView.setTextAppearance(mContext, mTitleStyleRes);
Adam Powell89e06452010-06-23 20:24:52 -0700185 }
Gilles Debunnec619e742011-05-31 15:49:51 -0700186 if (mSubtitleStyleRes != 0) {
187 mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes);
Adam Powell0e94b512010-06-29 17:58:20 -0700188 }
Gilles Debunnec619e742011-05-31 15:49:51 -0700189 }
190
191 mTitleView.setText(mTitle);
192 mSubtitleView.setText(mSubtitle);
193
194 final boolean hasTitle = !TextUtils.isEmpty(mTitle);
195 final boolean hasSubtitle = !TextUtils.isEmpty(mSubtitle);
196 mSubtitleView.setVisibility(hasSubtitle ? VISIBLE : GONE);
197 mTitleLayout.setVisibility(hasTitle || hasSubtitle ? VISIBLE : GONE);
198 if (mTitleLayout.getParent() == null) {
199 addView(mTitleLayout);
Adam Powell89e06452010-06-23 20:24:52 -0700200 }
201 }
Adam Powell0e94b512010-06-29 17:58:20 -0700202
Adam Powell6e346362010-07-23 10:18:23 -0700203 public void initForMode(final ActionMode mode) {
Adam Powellf16888f2010-10-11 17:05:29 -0700204 if (mClose == null) {
205 LayoutInflater inflater = LayoutInflater.from(mContext);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800206 mClose = inflater.inflate(R.layout.action_mode_close_item, this, false);
207 addView(mClose);
Adam Powell45f1e082010-12-10 14:20:13 -0800208 } else if (mClose.getParent() == null) {
Adam Powellf16888f2010-10-11 17:05:29 -0700209 addView(mClose);
Adam Powell89e06452010-06-23 20:24:52 -0700210 }
Adam Powellf16888f2010-10-11 17:05:29 -0700211
212 View closeButton = mClose.findViewById(R.id.action_mode_close_button);
213 closeButton.setOnClickListener(new OnClickListener() {
Adam Powell269b8bb2010-07-20 16:46:42 -0700214 public void onClick(View v) {
215 mode.finish();
216 }
217 });
Adam Powell89e06452010-06-23 20:24:52 -0700218
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700219 final MenuBuilder menu = (MenuBuilder) mode.getMenu();
Adam Powell678ed0c2011-10-27 17:46:07 -0700220 if (mActionMenuPresenter != null) {
221 mActionMenuPresenter.dismissPopupMenus();
222 }
Adam Powell538e5652011-10-11 13:47:08 -0700223 mActionMenuPresenter = new ActionMenuPresenter(mContext);
Adam Powell1ab418a2011-06-09 20:49:49 -0700224 mActionMenuPresenter.setReserveOverflow(true);
Adam Powell640a66e2011-04-29 10:18:53 -0700225
226 final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
227 LayoutParams.MATCH_PARENT);
Adam Powella05aba92011-09-23 14:22:49 -0700228 if (!mSplitActionBar) {
Adam Powell1ab418a2011-06-09 20:49:49 -0700229 menu.addMenuPresenter(mActionMenuPresenter);
230 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
Adam Powell9ca705e2011-08-11 16:57:59 -0700231 mMenuView.setBackgroundDrawable(null);
Adam Powell1ab418a2011-06-09 20:49:49 -0700232 addView(mMenuView, layoutParams);
Adam Powell640a66e2011-04-29 10:18:53 -0700233 } else {
234 // Allow full screen width in split mode.
Adam Powell8d02dea2011-05-31 21:35:13 -0700235 mActionMenuPresenter.setWidthLimit(
Adam Powell640a66e2011-04-29 10:18:53 -0700236 getContext().getResources().getDisplayMetrics().widthPixels, true);
237 // No limit to the item count; use whatever will fit.
Adam Powell8d02dea2011-05-31 21:35:13 -0700238 mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
Adam Powell640a66e2011-04-29 10:18:53 -0700239 // Span the whole width
240 layoutParams.width = LayoutParams.MATCH_PARENT;
Adam Powell1ab418a2011-06-09 20:49:49 -0700241 layoutParams.height = mContentHeight;
242 menu.addMenuPresenter(mActionMenuPresenter);
243 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
Adam Powell9ca705e2011-08-11 16:57:59 -0700244 mMenuView.setBackgroundDrawable(mSplitBackground);
Adam Powell1ab418a2011-06-09 20:49:49 -0700245 mSplitView.addView(mMenuView, layoutParams);
Adam Powell640a66e2011-04-29 10:18:53 -0700246 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800247
248 mAnimateInOnLayout = true;
Adam Powell89e06452010-06-23 20:24:52 -0700249 }
Adam Powell0e94b512010-06-29 17:58:20 -0700250
Adam Powell89e06452010-06-23 20:24:52 -0700251 public void closeMode() {
Adam Powell45f1e082010-12-10 14:20:13 -0800252 if (mAnimationMode == ANIMATE_OUT) {
253 // Called again during close; just finish what we were doing.
254 return;
255 }
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800256 if (mClose == null) {
257 killMode();
258 return;
259 }
260
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800261 finishAnimation();
Adam Powella1e63582011-01-18 16:51:22 -0800262 mAnimationMode = ANIMATE_OUT;
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800263 mCurrentAnimation = makeOutAnimation();
264 mCurrentAnimation.start();
265 }
266
267 private void finishAnimation() {
Adam Powell45f1e082010-12-10 14:20:13 -0800268 final Animator a = mCurrentAnimation;
Adam Powella1e63582011-01-18 16:51:22 -0800269 if (a != null) {
Adam Powell45f1e082010-12-10 14:20:13 -0800270 mCurrentAnimation = null;
271 a.end();
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800272 }
273 }
274
275 public void killMode() {
276 finishAnimation();
Adam Powell89e06452010-06-23 20:24:52 -0700277 removeAllViews();
Adam Powell640a66e2011-04-29 10:18:53 -0700278 if (mSplitView != null) {
279 mSplitView.removeView(mMenuView);
280 }
Adam Powell89e06452010-06-23 20:24:52 -0700281 mCustomView = null;
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700282 mMenuView = null;
Adam Powell00bba682011-01-08 15:53:38 -0800283 mAnimateInOnLayout = false;
Adam Powell89e06452010-06-23 20:24:52 -0700284 }
Adam Powell0e94b512010-06-29 17:58:20 -0700285
Gilles Debunnec619e742011-05-31 15:49:51 -0700286 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700287 public boolean showOverflowMenu() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700288 if (mActionMenuPresenter != null) {
289 return mActionMenuPresenter.showOverflowMenu();
Adam Powellf6148c52010-08-11 21:10:16 -0700290 }
291 return false;
292 }
293
Gilles Debunnec619e742011-05-31 15:49:51 -0700294 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700295 public boolean hideOverflowMenu() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700296 if (mActionMenuPresenter != null) {
297 return mActionMenuPresenter.hideOverflowMenu();
Adam Powellf6148c52010-08-11 21:10:16 -0700298 }
299 return false;
300 }
301
Gilles Debunnec619e742011-05-31 15:49:51 -0700302 @Override
Adam Powellf6148c52010-08-11 21:10:16 -0700303 public boolean isOverflowMenuShowing() {
Adam Powell8d02dea2011-05-31 21:35:13 -0700304 if (mActionMenuPresenter != null) {
305 return mActionMenuPresenter.isOverflowMenuShowing();
Adam Powellf6148c52010-08-11 21:10:16 -0700306 }
307 return false;
308 }
309
Adam Powell89e06452010-06-23 20:24:52 -0700310 @Override
Adam Powell9a5cc282011-08-28 16:18:16 -0700311 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
Adam Powella7db0372010-06-30 17:08:47 -0700312 // Used by custom views if they don't supply layout params. Everything else
313 // added to an ActionBarContextView should have them already.
Adam Powell9ca705e2011-08-11 16:57:59 -0700314 return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
315 }
316
317 @Override
Adam Powell9a5cc282011-08-28 16:18:16 -0700318 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
Adam Powell9ca705e2011-08-11 16:57:59 -0700319 return new MarginLayoutParams(getContext(), attrs);
Adam Powella7db0372010-06-30 17:08:47 -0700320 }
321
322 @Override
Adam Powell89e06452010-06-23 20:24:52 -0700323 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
324 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
325 if (widthMode != MeasureSpec.EXACTLY) {
326 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
327 "with android:layout_width=\"match_parent\" (or fill_parent)");
328 }
329
330 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Powell85446e92010-10-22 17:43:56 -0700331 if (heightMode == MeasureSpec.UNSPECIFIED) {
Adam Powell89e06452010-06-23 20:24:52 -0700332 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
333 "with android:layout_height=\"wrap_content\"");
334 }
335
336 final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
Adam Powell89e06452010-06-23 20:24:52 -0700337
Adam Powelle2194442010-08-12 18:13:03 -0700338 int maxHeight = mContentHeight > 0 ?
339 mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
340
341 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Powell89e06452010-06-23 20:24:52 -0700342 int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
Adam Powelle2194442010-08-12 18:13:03 -0700343 final int height = maxHeight - verticalPadding;
Adam Powell89e06452010-06-23 20:24:52 -0700344 final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
345
Adam Powellf16888f2010-10-11 17:05:29 -0700346 if (mClose != null) {
347 availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0);
Adam Powell9ca705e2011-08-11 16:57:59 -0700348 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
349 availableWidth -= lp.leftMargin + lp.rightMargin;
Adam Powell89e06452010-06-23 20:24:52 -0700350 }
351
Adam Powellf6ce6a92011-06-29 10:25:01 -0700352 if (mMenuView != null && mMenuView.getParent() == this) {
Adam Powellb0ff6f92011-01-23 18:07:45 -0800353 availableWidth = measureChildView(mMenuView, availableWidth,
354 childSpecHeight, 0);
Adam Powell89e06452010-06-23 20:24:52 -0700355 }
356
Adam Powellb0ff6f92011-01-23 18:07:45 -0800357 if (mTitleLayout != null && mCustomView == null) {
Adam Powellb98a81f2012-02-24 11:09:07 -0800358 if (mTitleOptional) {
359 final int titleWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
360 mTitleLayout.measure(titleWidthSpec, childSpecHeight);
361 final int titleWidth = mTitleLayout.getMeasuredWidth();
362 final boolean titleFits = titleWidth <= availableWidth;
363 if (titleFits) {
364 availableWidth -= titleWidth;
365 }
366 mTitleLayout.setVisibility(titleFits ? VISIBLE : GONE);
367 } else {
368 availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
369 }
Adam Powell89e06452010-06-23 20:24:52 -0700370 }
371
372 if (mCustomView != null) {
Adam Powell9a5cc282011-08-28 16:18:16 -0700373 ViewGroup.LayoutParams lp = mCustomView.getLayoutParams();
Adam Powella7db0372010-06-30 17:08:47 -0700374 final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ?
375 MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
376 final int customWidth = lp.width >= 0 ?
377 Math.min(lp.width, availableWidth) : availableWidth;
378 final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ?
379 MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
380 final int customHeight = lp.height >= 0 ?
381 Math.min(lp.height, height) : height;
382 mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode),
383 MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
Adam Powell89e06452010-06-23 20:24:52 -0700384 }
385
Adam Powelle2194442010-08-12 18:13:03 -0700386 if (mContentHeight <= 0) {
387 int measuredHeight = 0;
388 final int count = getChildCount();
389 for (int i = 0; i < count; i++) {
390 View v = getChildAt(i);
391 int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
392 if (paddedViewHeight > measuredHeight) {
393 measuredHeight = paddedViewHeight;
394 }
395 }
396 setMeasuredDimension(contentWidth, measuredHeight);
397 } else {
398 setMeasuredDimension(contentWidth, maxHeight);
399 }
Adam Powell89e06452010-06-23 20:24:52 -0700400 }
401
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800402 private Animator makeInAnimation() {
Adam Powell9ca705e2011-08-11 16:57:59 -0700403 mClose.setTranslationX(-mClose.getWidth() -
404 ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800405 ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0);
406 buttonAnimator.setDuration(200);
407 buttonAnimator.addListener(this);
408 buttonAnimator.setInterpolator(new DecelerateInterpolator());
409
410 AnimatorSet set = new AnimatorSet();
411 AnimatorSet.Builder b = set.play(buttonAnimator);
412
413 if (mMenuView != null) {
414 final int count = mMenuView.getChildCount();
415 if (count > 0) {
416 for (int i = count - 1, j = 0; i >= 0; i--, j++) {
417 View child = mMenuView.getChildAt(i);
418 child.setScaleY(0);
419 ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1);
Adam Powell482ae5f2012-02-17 15:18:01 -0800420 a.setDuration(300);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800421 b.with(a);
422 }
423 }
424 }
425
426 return set;
427 }
428
429 private Animator makeOutAnimation() {
430 ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX",
Adam Powell9ca705e2011-08-11 16:57:59 -0700431 -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800432 buttonAnimator.setDuration(200);
433 buttonAnimator.addListener(this);
434 buttonAnimator.setInterpolator(new DecelerateInterpolator());
435
436 AnimatorSet set = new AnimatorSet();
437 AnimatorSet.Builder b = set.play(buttonAnimator);
438
439 if (mMenuView != null) {
440 final int count = mMenuView.getChildCount();
441 if (count > 0) {
442 for (int i = 0; i < 0; i++) {
443 View child = mMenuView.getChildAt(i);
444 child.setScaleY(0);
Adam Powell640a66e2011-04-29 10:18:53 -0700445 ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0);
Adam Powell482ae5f2012-02-17 15:18:01 -0800446 a.setDuration(300);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800447 b.with(a);
448 }
449 }
450 }
451
452 return set;
453 }
454
Adam Powell89e06452010-06-23 20:24:52 -0700455 @Override
456 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700457 final boolean isLayoutRtl = isLayoutRtl();
458 int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft();
Adam Powell89e06452010-06-23 20:24:52 -0700459 final int y = getPaddingTop();
460 final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
Adam Powell89e06452010-06-23 20:24:52 -0700461
Adam Powellf16888f2010-10-11 17:05:29 -0700462 if (mClose != null && mClose.getVisibility() != GONE) {
Adam Powell9ca705e2011-08-11 16:57:59 -0700463 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700464 final int startMargin = (isLayoutRtl ? lp.rightMargin : lp.leftMargin);
465 final int endMargin = (isLayoutRtl ? lp.leftMargin : lp.rightMargin);
466 x = next(x, startMargin, isLayoutRtl);
467 x += positionChild(mClose, x, y, contentHeight, isLayoutRtl);
468 x = next(x, endMargin, isLayoutRtl);
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800469
470 if (mAnimateInOnLayout) {
471 mAnimationMode = ANIMATE_IN;
472 mCurrentAnimation = makeInAnimation();
473 mCurrentAnimation.start();
474 mAnimateInOnLayout = false;
475 }
Adam Powell89e06452010-06-23 20:24:52 -0700476 }
Adam Powell640a66e2011-04-29 10:18:53 -0700477
Adam Powellb98a81f2012-02-24 11:09:07 -0800478 if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700479 x += positionChild(mTitleLayout, x, y, contentHeight, isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700480 }
481
482 if (mCustomView != null) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700483 x += positionChild(mCustomView, x, y, contentHeight, isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700484 }
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700485
486 x = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight();
Adam Powell89e06452010-06-23 20:24:52 -0700487
Adam Powell2c9c9fe2010-07-16 10:17:57 -0700488 if (mMenuView != null) {
Fabrice Di Megliocf1ba022012-06-25 15:49:11 -0700489 x += positionChild(mMenuView, x, y, contentHeight, !isLayoutRtl);
Adam Powell89e06452010-06-23 20:24:52 -0700490 }
491 }
492
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800493 @Override
494 public void onAnimationStart(Animator animation) {
495 }
496
497 @Override
498 public void onAnimationEnd(Animator animation) {
Adam Powella1e63582011-01-18 16:51:22 -0800499 if (mAnimationMode == ANIMATE_OUT) {
Adam Powelld8b3f2e2010-12-02 13:37:03 -0800500 killMode();
501 }
502 mAnimationMode = ANIMATE_IDLE;
503 }
504
505 @Override
506 public void onAnimationCancel(Animator animation) {
507 }
508
509 @Override
510 public void onAnimationRepeat(Animator animation) {
511 }
Patrick Dubroye0a799a2011-05-04 16:19:22 -0700512
513 @Override
514 public boolean shouldDelayChildPressedState() {
515 return false;
516 }
Adam Powell86ed4362011-09-14 16:18:53 -0700517
518 @Override
519 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
520 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
521 // Action mode started
522 event.setSource(this);
523 event.setClassName(getClass().getName());
524 event.setPackageName(getContext().getPackageName());
525 event.setContentDescription(mTitle);
526 } else {
527 super.onInitializeAccessibilityEvent(event);
528 }
529 }
Adam Powellb98a81f2012-02-24 11:09:07 -0800530
531 public void setTitleOptional(boolean titleOptional) {
532 if (titleOptional != mTitleOptional) {
533 requestLayout();
534 }
535 mTitleOptional = titleOptional;
536 }
537
538 public boolean isTitleOptional() {
539 return mTitleOptional;
540 }
Adam Powell89e06452010-06-23 20:24:52 -0700541}