ActionBarContextView.java revision e021e6ed8931a0a8296af182fc9b0c76b64fb0c4
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 android.widget.ActionMenuPresenter; 20import android.widget.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.TypedArray; 29import android.graphics.drawable.Drawable; 30import android.text.TextUtils; 31import android.util.AttributeSet; 32import android.view.ActionMode; 33import android.view.LayoutInflater; 34import android.view.View; 35import android.view.ViewGroup; 36import android.view.accessibility.AccessibilityEvent; 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 private boolean mTitleOptional; 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 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); 86 setBackground(a.getDrawable( 87 com.android.internal.R.styleable.ActionMode_background)); 88 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); 92 93 mContentHeight = a.getLayoutDimension( 94 com.android.internal.R.styleable.ActionMode_height, 0); 95 96 mSplitBackground = a.getDrawable( 97 com.android.internal.R.styleable.ActionMode_backgroundSplit); 98 99 a.recycle(); 100 } 101 102 @Override 103 public void onDetachedFromWindow() { 104 super.onDetachedFromWindow(); 105 if (mActionMenuPresenter != null) { 106 mActionMenuPresenter.hideOverflowMenu(); 107 mActionMenuPresenter.hideSubMenus(); 108 } 109 } 110 111 @Override 112 public void setSplitToolbar(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.setSplitToolbar(split); 141 } 142 } 143 144 public void setContentHeight(int height) { 145 mContentHeight = height; 146 } 147 148 public void setCustomView(View view) { 149 if (mCustomView != null) { 150 removeView(mCustomView); 151 } 152 mCustomView = view; 153 if (mTitleLayout != null) { 154 removeView(mTitleLayout); 155 mTitleLayout = null; 156 } 157 if (view != null) { 158 addView(view); 159 } 160 requestLayout(); 161 } 162 163 public void setTitle(CharSequence title) { 164 mTitle = title; 165 initTitle(); 166 } 167 168 public void setSubtitle(CharSequence subtitle) { 169 mSubtitle = subtitle; 170 initTitle(); 171 } 172 173 public CharSequence getTitle() { 174 return mTitle; 175 } 176 177 public CharSequence getSubtitle() { 178 return mSubtitle; 179 } 180 181 private void initTitle() { 182 if (mTitleLayout == null) { 183 LayoutInflater inflater = LayoutInflater.from(getContext()); 184 inflater.inflate(R.layout.action_bar_title_item, this); 185 mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1); 186 mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title); 187 mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle); 188 if (mTitleStyleRes != 0) { 189 mTitleView.setTextAppearance(mContext, mTitleStyleRes); 190 } 191 if (mSubtitleStyleRes != 0) { 192 mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes); 193 } 194 } 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); 205 } 206 } 207 208 public void initForMode(final ActionMode mode) { 209 if (mClose == null) { 210 LayoutInflater inflater = LayoutInflater.from(mContext); 211 mClose = inflater.inflate(R.layout.action_mode_close_item, this, false); 212 addView(mClose); 213 } else if (mClose.getParent() == null) { 214 addView(mClose); 215 } 216 217 View closeButton = mClose.findViewById(R.id.action_mode_close_button); 218 closeButton.setOnClickListener(new OnClickListener() { 219 public void onClick(View v) { 220 mode.finish(); 221 } 222 }); 223 224 final MenuBuilder menu = (MenuBuilder) mode.getMenu(); 225 if (mActionMenuPresenter != null) { 226 mActionMenuPresenter.dismissPopupMenus(); 227 } 228 mActionMenuPresenter = new ActionMenuPresenter(mContext); 229 mActionMenuPresenter.setReserveOverflow(true); 230 231 final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, 232 LayoutParams.MATCH_PARENT); 233 if (!mSplitActionBar) { 234 menu.addMenuPresenter(mActionMenuPresenter); 235 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); 236 mMenuView.setBackgroundDrawable(null); 237 addView(mMenuView, layoutParams); 238 } else { 239 // Allow full screen width in split mode. 240 mActionMenuPresenter.setWidthLimit( 241 getContext().getResources().getDisplayMetrics().widthPixels, true); 242 // No limit to the item count; use whatever will fit. 243 mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); 244 // Span the whole width 245 layoutParams.width = LayoutParams.MATCH_PARENT; 246 layoutParams.height = mContentHeight; 247 menu.addMenuPresenter(mActionMenuPresenter); 248 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); 249 mMenuView.setBackgroundDrawable(mSplitBackground); 250 mSplitView.addView(mMenuView, layoutParams); 251 } 252 253 mAnimateInOnLayout = true; 254 } 255 256 public void closeMode() { 257 if (mAnimationMode == ANIMATE_OUT) { 258 // Called again during close; just finish what we were doing. 259 return; 260 } 261 if (mClose == null) { 262 killMode(); 263 return; 264 } 265 266 finishAnimation(); 267 mAnimationMode = ANIMATE_OUT; 268 mCurrentAnimation = makeOutAnimation(); 269 mCurrentAnimation.start(); 270 } 271 272 private void finishAnimation() { 273 final Animator a = mCurrentAnimation; 274 if (a != null) { 275 mCurrentAnimation = null; 276 a.end(); 277 } 278 } 279 280 public void killMode() { 281 finishAnimation(); 282 removeAllViews(); 283 if (mSplitView != null) { 284 mSplitView.removeView(mMenuView); 285 } 286 mCustomView = null; 287 mMenuView = null; 288 mAnimateInOnLayout = false; 289 } 290 291 @Override 292 public boolean showOverflowMenu() { 293 if (mActionMenuPresenter != null) { 294 return mActionMenuPresenter.showOverflowMenu(); 295 } 296 return false; 297 } 298 299 @Override 300 public boolean hideOverflowMenu() { 301 if (mActionMenuPresenter != null) { 302 return mActionMenuPresenter.hideOverflowMenu(); 303 } 304 return false; 305 } 306 307 @Override 308 public boolean isOverflowMenuShowing() { 309 if (mActionMenuPresenter != null) { 310 return mActionMenuPresenter.isOverflowMenuShowing(); 311 } 312 return false; 313 } 314 315 @Override 316 protected ViewGroup.LayoutParams generateDefaultLayoutParams() { 317 // Used by custom views if they don't supply layout params. Everything else 318 // added to an ActionBarContextView should have them already. 319 return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 320 } 321 322 @Override 323 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { 324 return new MarginLayoutParams(getContext(), attrs); 325 } 326 327 @Override 328 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); 336 if (heightMode == MeasureSpec.UNSPECIFIED) { 337 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); 342 343 int maxHeight = mContentHeight > 0 ? 344 mContentHeight : MeasureSpec.getSize(heightMeasureSpec); 345 346 final int verticalPadding = getPaddingTop() + getPaddingBottom(); 347 int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); 348 final int height = maxHeight - verticalPadding; 349 final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); 350 351 if (mClose != null) { 352 availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0); 353 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); 354 availableWidth -= lp.leftMargin + lp.rightMargin; 355 } 356 357 if (mMenuView != null && mMenuView.getParent() == this) { 358 availableWidth = measureChildView(mMenuView, availableWidth, 359 childSpecHeight, 0); 360 } 361 362 if (mTitleLayout != null && mCustomView == null) { 363 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 } 375 } 376 377 if (mCustomView != null) { 378 ViewGroup.LayoutParams lp = mCustomView.getLayoutParams(); 379 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)); 389 } 390 391 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 } 405 } 406 407 private Animator makeInAnimation() { 408 mClose.setTranslationX(-mClose.getWidth() - 409 ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin); 410 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); 425 a.setDuration(300); 426 b.with(a); 427 } 428 } 429 } 430 431 return set; 432 } 433 434 private Animator makeOutAnimation() { 435 ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 436 -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin); 437 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); 450 ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0); 451 a.setDuration(300); 452 b.with(a); 453 } 454 } 455 } 456 457 return set; 458 } 459 460 @Override 461 protected void onLayout(boolean changed, int l, int t, int r, int b) { 462 final boolean isLayoutRtl = isLayoutRtl(); 463 int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft(); 464 final int y = getPaddingTop(); 465 final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); 466 467 if (mClose != null && mClose.getVisibility() != GONE) { 468 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); 469 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); 474 475 if (mAnimateInOnLayout) { 476 mAnimationMode = ANIMATE_IN; 477 mCurrentAnimation = makeInAnimation(); 478 mCurrentAnimation.start(); 479 mAnimateInOnLayout = false; 480 } 481 } 482 483 if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) { 484 x += positionChild(mTitleLayout, x, y, contentHeight, isLayoutRtl); 485 } 486 487 if (mCustomView != null) { 488 x += positionChild(mCustomView, x, y, contentHeight, isLayoutRtl); 489 } 490 491 x = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight(); 492 493 if (mMenuView != null) { 494 x += positionChild(mMenuView, x, y, contentHeight, !isLayoutRtl); 495 } 496 } 497 498 @Override 499 public void onAnimationStart(Animator animation) { 500 } 501 502 @Override 503 public void onAnimationEnd(Animator animation) { 504 if (mAnimationMode == ANIMATE_OUT) { 505 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 } 517 518 @Override 519 public boolean shouldDelayChildPressedState() { 520 return false; 521 } 522 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 } 535 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 } 546} 547