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