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 | |
| 18 | import com.android.internal.R; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 19 | import com.android.internal.view.menu.ActionMenuPresenter; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 20 | import com.android.internal.view.menu.ActionMenuView; |
| 21 | import com.android.internal.view.menu.MenuBuilder; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 22 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 23 | import android.animation.Animator; |
| 24 | import android.animation.Animator.AnimatorListener; |
| 25 | import android.animation.AnimatorSet; |
| 26 | import android.animation.ObjectAnimator; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.res.TypedArray; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 30 | import android.view.ActionMode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 31 | import android.view.LayoutInflater; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 32 | import android.view.View; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 33 | import android.view.ViewGroup.LayoutParams; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 34 | import android.view.animation.DecelerateInterpolator; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 35 | import android.widget.LinearLayout; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 36 | import android.widget.TextView; |
| 37 | |
| 38 | /** |
| 39 | * @hide |
| 40 | */ |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 41 | public class ActionBarContextView extends AbsActionBarView implements AnimatorListener { |
Adam Powell | 77769c7 | 2011-01-17 12:05:08 -0800 | [diff] [blame] | 42 | private static final String TAG = "ActionBarContextView"; |
| 43 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 44 | private int mContentHeight; |
| 45 | |
| 46 | private CharSequence mTitle; |
| 47 | private CharSequence mSubtitle; |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 48 | |
| 49 | private View mClose; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 50 | private View mCustomView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 51 | private LinearLayout mTitleLayout; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 52 | private TextView mTitleView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 53 | private TextView mSubtitleView; |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 54 | private int mTitleStyleRes; |
| 55 | private int mSubtitleStyleRes; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 56 | |
| 57 | private Animator mCurrentAnimation; |
| 58 | private boolean mAnimateInOnLayout; |
| 59 | private int mAnimationMode; |
| 60 | |
| 61 | private static final int ANIMATE_IDLE = 0; |
| 62 | private static final int ANIMATE_IN = 1; |
| 63 | private static final int ANIMATE_OUT = 2; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 64 | |
| 65 | public ActionBarContextView(Context context) { |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 66 | this(context, null); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public ActionBarContextView(Context context, AttributeSet attrs) { |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 70 | this(context, attrs, com.android.internal.R.attr.actionModeStyle); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public ActionBarContextView(Context context, AttributeSet attrs, int defStyle) { |
| 74 | super(context, attrs, defStyle); |
| 75 | |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 76 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionMode, defStyle, 0); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 77 | setBackgroundDrawable(a.getDrawable( |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 78 | com.android.internal.R.styleable.ActionMode_background)); |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 79 | mTitleStyleRes = a.getResourceId( |
| 80 | com.android.internal.R.styleable.ActionMode_titleTextStyle, 0); |
| 81 | mSubtitleStyleRes = a.getResourceId( |
| 82 | com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0); |
Adam Powell | dcdefbb | 2010-07-19 15:45:22 -0700 | [diff] [blame] | 83 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 84 | mContentHeight = a.getLayoutDimension( |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 85 | com.android.internal.R.styleable.ActionMode_height, 0); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 86 | a.recycle(); |
| 87 | } |
Adam Powell | 9146ac7 | 2010-08-16 18:53:38 -0700 | [diff] [blame] | 88 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 89 | public void setHeight(int height) { |
| 90 | mContentHeight = height; |
| 91 | } |
| 92 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 93 | public void setCustomView(View view) { |
| 94 | if (mCustomView != null) { |
| 95 | removeView(mCustomView); |
| 96 | } |
| 97 | mCustomView = view; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 98 | if (mTitleLayout != null) { |
| 99 | removeView(mTitleLayout); |
| 100 | mTitleLayout = null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 101 | } |
| 102 | if (view != null) { |
| 103 | addView(view); |
| 104 | } |
| 105 | requestLayout(); |
| 106 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 107 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 108 | public void setTitle(CharSequence title) { |
| 109 | mTitle = title; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 110 | initTitle(); |
| 111 | } |
| 112 | |
| 113 | public void setSubtitle(CharSequence subtitle) { |
| 114 | mSubtitle = subtitle; |
| 115 | initTitle(); |
| 116 | } |
| 117 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 118 | public CharSequence getTitle() { |
| 119 | return mTitle; |
| 120 | } |
| 121 | |
| 122 | public CharSequence getSubtitle() { |
| 123 | return mSubtitle; |
| 124 | } |
| 125 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 126 | private void initTitle() { |
| 127 | if (mTitleLayout == null) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 128 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 129 | inflater.inflate(R.layout.action_bar_title_item, this); |
| 130 | mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 131 | mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title); |
| 132 | mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle); |
| 133 | if (mTitle != null) { |
| 134 | mTitleView.setText(mTitle); |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 135 | if (mTitleStyleRes != 0) { |
| 136 | mTitleView.setTextAppearance(mContext, mTitleStyleRes); |
| 137 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 138 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 139 | if (mSubtitle != null) { |
| 140 | mSubtitleView.setText(mSubtitle); |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 141 | if (mSubtitleStyleRes != 0) { |
| 142 | mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes); |
| 143 | } |
Adam Powell | 8350f7d | 2010-07-28 14:27:28 -0700 | [diff] [blame] | 144 | mSubtitleView.setVisibility(VISIBLE); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 145 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 146 | } else { |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 147 | mTitleView.setText(mTitle); |
| 148 | mSubtitleView.setText(mSubtitle); |
Adam Powell | 8350f7d | 2010-07-28 14:27:28 -0700 | [diff] [blame] | 149 | mSubtitleView.setVisibility(mSubtitle != null ? VISIBLE : GONE); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 150 | if (mTitleLayout.getParent() == null) { |
| 151 | addView(mTitleLayout); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 155 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 156 | public void initForMode(final ActionMode mode) { |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 157 | if (mClose == null) { |
| 158 | LayoutInflater inflater = LayoutInflater.from(mContext); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 159 | mClose = inflater.inflate(R.layout.action_mode_close_item, this, false); |
| 160 | addView(mClose); |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 161 | } else if (mClose.getParent() == null) { |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 162 | addView(mClose); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 163 | } |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 164 | |
| 165 | View closeButton = mClose.findViewById(R.id.action_mode_close_button); |
| 166 | closeButton.setOnClickListener(new OnClickListener() { |
Adam Powell | 269b8bb | 2010-07-20 16:46:42 -0700 | [diff] [blame] | 167 | public void onClick(View v) { |
| 168 | mode.finish(); |
| 169 | } |
| 170 | }); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 171 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 172 | final MenuBuilder menu = (MenuBuilder) mode.getMenu(); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 173 | mMenuPresenter = new ActionMenuPresenter(); |
| 174 | menu.addMenuPresenter(mMenuPresenter); |
| 175 | mMenuView = (ActionMenuView) mMenuPresenter.getMenuView(this); |
| 176 | |
| 177 | final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 178 | LayoutParams.MATCH_PARENT); |
| 179 | mMenuView.setLayoutParams(layoutParams); |
| 180 | if (mSplitView == null) { |
| 181 | addView(mMenuView); |
| 182 | } else { |
| 183 | // Allow full screen width in split mode. |
| 184 | mMenuPresenter.setWidthLimit( |
| 185 | getContext().getResources().getDisplayMetrics().widthPixels, true); |
| 186 | // No limit to the item count; use whatever will fit. |
| 187 | mMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
| 188 | // Span the whole width |
| 189 | layoutParams.width = LayoutParams.MATCH_PARENT; |
| 190 | mSplitView.addView(mMenuView); |
| 191 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 192 | |
| 193 | mAnimateInOnLayout = true; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 194 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 195 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 196 | public void closeMode() { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 197 | if (mAnimationMode == ANIMATE_OUT) { |
| 198 | // Called again during close; just finish what we were doing. |
| 199 | return; |
| 200 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 201 | if (mClose == null) { |
| 202 | killMode(); |
| 203 | return; |
| 204 | } |
| 205 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 206 | finishAnimation(); |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 207 | mAnimationMode = ANIMATE_OUT; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 208 | mCurrentAnimation = makeOutAnimation(); |
| 209 | mCurrentAnimation.start(); |
| 210 | } |
| 211 | |
| 212 | private void finishAnimation() { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 213 | final Animator a = mCurrentAnimation; |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 214 | if (a != null) { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 215 | mCurrentAnimation = null; |
| 216 | a.end(); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | public void killMode() { |
| 221 | finishAnimation(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 222 | removeAllViews(); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 223 | if (mSplitView != null) { |
| 224 | mSplitView.removeView(mMenuView); |
| 225 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 226 | mCustomView = null; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 227 | mMenuView = null; |
Adam Powell | 00bba68 | 2011-01-08 15:53:38 -0800 | [diff] [blame] | 228 | mAnimateInOnLayout = false; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 229 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 230 | |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 231 | public boolean showOverflowMenu() { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 232 | if (mMenuPresenter != null) { |
| 233 | return mMenuPresenter.showOverflowMenu(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | public boolean hideOverflowMenu() { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 239 | if (mMenuPresenter != null) { |
| 240 | return mMenuPresenter.hideOverflowMenu(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 241 | } |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | public boolean isOverflowMenuShowing() { |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 246 | if (mMenuPresenter != null) { |
| 247 | return mMenuPresenter.isOverflowMenuShowing(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 252 | @Override |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 253 | protected LayoutParams generateDefaultLayoutParams() { |
| 254 | // Used by custom views if they don't supply layout params. Everything else |
| 255 | // added to an ActionBarContextView should have them already. |
| 256 | return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 257 | } |
| 258 | |
| 259 | @Override |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 260 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 261 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 262 | if (widthMode != MeasureSpec.EXACTLY) { |
| 263 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 264 | "with android:layout_width=\"match_parent\" (or fill_parent)"); |
| 265 | } |
| 266 | |
| 267 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 268 | if (heightMode == MeasureSpec.UNSPECIFIED) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 269 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 270 | "with android:layout_height=\"wrap_content\""); |
| 271 | } |
| 272 | |
| 273 | final int contentWidth = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 274 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 275 | int maxHeight = mContentHeight > 0 ? |
| 276 | mContentHeight : MeasureSpec.getSize(heightMeasureSpec); |
| 277 | |
| 278 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 279 | int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 280 | final int height = maxHeight - verticalPadding; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 281 | final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); |
| 282 | |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 283 | if (mClose != null) { |
| 284 | availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Adam Powell | b0ff6f9 | 2011-01-23 18:07:45 -0800 | [diff] [blame] | 287 | if (mMenuView != null) { |
| 288 | availableWidth = measureChildView(mMenuView, availableWidth, |
| 289 | childSpecHeight, 0); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Adam Powell | b0ff6f9 | 2011-01-23 18:07:45 -0800 | [diff] [blame] | 292 | if (mTitleLayout != null && mCustomView == null) { |
| 293 | availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if (mCustomView != null) { |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 297 | LayoutParams lp = mCustomView.getLayoutParams(); |
| 298 | final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? |
| 299 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 300 | final int customWidth = lp.width >= 0 ? |
| 301 | Math.min(lp.width, availableWidth) : availableWidth; |
| 302 | final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? |
| 303 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 304 | final int customHeight = lp.height >= 0 ? |
| 305 | Math.min(lp.height, height) : height; |
| 306 | mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode), |
| 307 | MeasureSpec.makeMeasureSpec(customHeight, customHeightMode)); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 310 | if (mContentHeight <= 0) { |
| 311 | int measuredHeight = 0; |
| 312 | final int count = getChildCount(); |
| 313 | for (int i = 0; i < count; i++) { |
| 314 | View v = getChildAt(i); |
| 315 | int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; |
| 316 | if (paddedViewHeight > measuredHeight) { |
| 317 | measuredHeight = paddedViewHeight; |
| 318 | } |
| 319 | } |
| 320 | setMeasuredDimension(contentWidth, measuredHeight); |
| 321 | } else { |
| 322 | setMeasuredDimension(contentWidth, maxHeight); |
| 323 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 326 | private Animator makeInAnimation() { |
| 327 | mClose.setTranslationX(-mClose.getWidth()); |
| 328 | ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0); |
| 329 | buttonAnimator.setDuration(200); |
| 330 | buttonAnimator.addListener(this); |
| 331 | buttonAnimator.setInterpolator(new DecelerateInterpolator()); |
| 332 | |
| 333 | AnimatorSet set = new AnimatorSet(); |
| 334 | AnimatorSet.Builder b = set.play(buttonAnimator); |
| 335 | |
| 336 | if (mMenuView != null) { |
| 337 | final int count = mMenuView.getChildCount(); |
| 338 | if (count > 0) { |
| 339 | for (int i = count - 1, j = 0; i >= 0; i--, j++) { |
| 340 | View child = mMenuView.getChildAt(i); |
| 341 | child.setScaleY(0); |
| 342 | ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1); |
| 343 | a.setDuration(100); |
| 344 | a.setStartDelay(j * 70); |
| 345 | b.with(a); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return set; |
| 351 | } |
| 352 | |
| 353 | private Animator makeOutAnimation() { |
| 354 | ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 355 | -mClose.getWidth()); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 356 | buttonAnimator.setDuration(200); |
| 357 | buttonAnimator.addListener(this); |
| 358 | buttonAnimator.setInterpolator(new DecelerateInterpolator()); |
| 359 | |
| 360 | AnimatorSet set = new AnimatorSet(); |
| 361 | AnimatorSet.Builder b = set.play(buttonAnimator); |
| 362 | |
| 363 | if (mMenuView != null) { |
| 364 | final int count = mMenuView.getChildCount(); |
| 365 | if (count > 0) { |
| 366 | for (int i = 0; i < 0; i++) { |
| 367 | View child = mMenuView.getChildAt(i); |
| 368 | child.setScaleY(0); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 369 | ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 370 | a.setDuration(100); |
| 371 | a.setStartDelay(i * 70); |
| 372 | b.with(a); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return set; |
| 378 | } |
| 379 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 380 | @Override |
| 381 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 382 | int x = getPaddingLeft(); |
| 383 | final int y = getPaddingTop(); |
| 384 | final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 385 | |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 386 | if (mClose != null && mClose.getVisibility() != GONE) { |
| 387 | x += positionChild(mClose, x, y, contentHeight); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 388 | |
| 389 | if (mAnimateInOnLayout) { |
| 390 | mAnimationMode = ANIMATE_IN; |
| 391 | mCurrentAnimation = makeInAnimation(); |
| 392 | mCurrentAnimation.start(); |
| 393 | mAnimateInOnLayout = false; |
| 394 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 395 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 396 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 397 | if (mTitleLayout != null && mCustomView == null) { |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 398 | x += positionChild(mTitleLayout, x, y, contentHeight); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (mCustomView != null) { |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 402 | x += positionChild(mCustomView, x, y, contentHeight); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | x = r - l - getPaddingRight(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 406 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 407 | if (mMenuView != null) { |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 408 | x -= positionChildInverse(mMenuView, x, y, contentHeight); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 412 | @Override |
| 413 | public void onAnimationStart(Animator animation) { |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public void onAnimationEnd(Animator animation) { |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 418 | if (mAnimationMode == ANIMATE_OUT) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 419 | killMode(); |
| 420 | } |
| 421 | mAnimationMode = ANIMATE_IDLE; |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void onAnimationCancel(Animator animation) { |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public void onAnimationRepeat(Animator animation) { |
| 430 | } |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 431 | |
| 432 | @Override |
| 433 | public boolean shouldDelayChildPressedState() { |
| 434 | return false; |
| 435 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 436 | } |