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