ActionBarContextView.java revision 482ae5f2388a07d60d7f3b54432120172af25eee
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 if (mActionMenuPresenter != null) { 220 mActionMenuPresenter.dismissPopupMenus(); 221 } 222 mActionMenuPresenter = new ActionMenuPresenter(mContext); 223 mActionMenuPresenter.setReserveOverflow(true); 224 225 final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, 226 LayoutParams.MATCH_PARENT); 227 if (!mSplitActionBar) { 228 menu.addMenuPresenter(mActionMenuPresenter); 229 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); 230 mMenuView.setBackgroundDrawable(null); 231 addView(mMenuView, layoutParams); 232 } else { 233 // Allow full screen width in split mode. 234 mActionMenuPresenter.setWidthLimit( 235 getContext().getResources().getDisplayMetrics().widthPixels, true); 236 // No limit to the item count; use whatever will fit. 237 mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); 238 // Span the whole width 239 layoutParams.width = LayoutParams.MATCH_PARENT; 240 layoutParams.height = mContentHeight; 241 menu.addMenuPresenter(mActionMenuPresenter); 242 mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); 243 mMenuView.setBackgroundDrawable(mSplitBackground); 244 mSplitView.addView(mMenuView, layoutParams); 245 } 246 247 mAnimateInOnLayout = true; 248 } 249 250 public void closeMode() { 251 if (mAnimationMode == ANIMATE_OUT) { 252 // Called again during close; just finish what we were doing. 253 return; 254 } 255 if (mClose == null) { 256 killMode(); 257 return; 258 } 259 260 finishAnimation(); 261 mAnimationMode = ANIMATE_OUT; 262 mCurrentAnimation = makeOutAnimation(); 263 mCurrentAnimation.start(); 264 } 265 266 private void finishAnimation() { 267 final Animator a = mCurrentAnimation; 268 if (a != null) { 269 mCurrentAnimation = null; 270 a.end(); 271 } 272 } 273 274 public void killMode() { 275 finishAnimation(); 276 removeAllViews(); 277 if (mSplitView != null) { 278 mSplitView.removeView(mMenuView); 279 } 280 mCustomView = null; 281 mMenuView = null; 282 mAnimateInOnLayout = false; 283 } 284 285 @Override 286 public boolean showOverflowMenu() { 287 if (mActionMenuPresenter != null) { 288 return mActionMenuPresenter.showOverflowMenu(); 289 } 290 return false; 291 } 292 293 @Override 294 public boolean hideOverflowMenu() { 295 if (mActionMenuPresenter != null) { 296 return mActionMenuPresenter.hideOverflowMenu(); 297 } 298 return false; 299 } 300 301 @Override 302 public boolean isOverflowMenuShowing() { 303 if (mActionMenuPresenter != null) { 304 return mActionMenuPresenter.isOverflowMenuShowing(); 305 } 306 return false; 307 } 308 309 @Override 310 protected ViewGroup.LayoutParams generateDefaultLayoutParams() { 311 // Used by custom views if they don't supply layout params. Everything else 312 // added to an ActionBarContextView should have them already. 313 return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 314 } 315 316 @Override 317 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { 318 return new MarginLayoutParams(getContext(), attrs); 319 } 320 321 @Override 322 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 323 final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 324 if (widthMode != MeasureSpec.EXACTLY) { 325 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + 326 "with android:layout_width=\"match_parent\" (or fill_parent)"); 327 } 328 329 final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 330 if (heightMode == MeasureSpec.UNSPECIFIED) { 331 throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + 332 "with android:layout_height=\"wrap_content\""); 333 } 334 335 final int contentWidth = MeasureSpec.getSize(widthMeasureSpec); 336 337 int maxHeight = mContentHeight > 0 ? 338 mContentHeight : MeasureSpec.getSize(heightMeasureSpec); 339 340 final int verticalPadding = getPaddingTop() + getPaddingBottom(); 341 int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); 342 final int height = maxHeight - verticalPadding; 343 final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); 344 345 if (mClose != null) { 346 availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0); 347 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); 348 availableWidth -= lp.leftMargin + lp.rightMargin; 349 } 350 351 if (mMenuView != null && mMenuView.getParent() == this) { 352 availableWidth = measureChildView(mMenuView, availableWidth, 353 childSpecHeight, 0); 354 } 355 356 if (mTitleLayout != null && mCustomView == null) { 357 availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0); 358 } 359 360 if (mCustomView != null) { 361 ViewGroup.LayoutParams lp = mCustomView.getLayoutParams(); 362 final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? 363 MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; 364 final int customWidth = lp.width >= 0 ? 365 Math.min(lp.width, availableWidth) : availableWidth; 366 final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? 367 MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; 368 final int customHeight = lp.height >= 0 ? 369 Math.min(lp.height, height) : height; 370 mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode), 371 MeasureSpec.makeMeasureSpec(customHeight, customHeightMode)); 372 } 373 374 if (mContentHeight <= 0) { 375 int measuredHeight = 0; 376 final int count = getChildCount(); 377 for (int i = 0; i < count; i++) { 378 View v = getChildAt(i); 379 int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; 380 if (paddedViewHeight > measuredHeight) { 381 measuredHeight = paddedViewHeight; 382 } 383 } 384 setMeasuredDimension(contentWidth, measuredHeight); 385 } else { 386 setMeasuredDimension(contentWidth, maxHeight); 387 } 388 } 389 390 private Animator makeInAnimation() { 391 mClose.setTranslationX(-mClose.getWidth() - 392 ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin); 393 ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0); 394 buttonAnimator.setDuration(200); 395 buttonAnimator.addListener(this); 396 buttonAnimator.setInterpolator(new DecelerateInterpolator()); 397 398 AnimatorSet set = new AnimatorSet(); 399 AnimatorSet.Builder b = set.play(buttonAnimator); 400 401 if (mMenuView != null) { 402 final int count = mMenuView.getChildCount(); 403 if (count > 0) { 404 for (int i = count - 1, j = 0; i >= 0; i--, j++) { 405 View child = mMenuView.getChildAt(i); 406 child.setScaleY(0); 407 ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1); 408 a.setDuration(300); 409 b.with(a); 410 } 411 } 412 } 413 414 return set; 415 } 416 417 private Animator makeOutAnimation() { 418 ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 419 -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin); 420 buttonAnimator.setDuration(200); 421 buttonAnimator.addListener(this); 422 buttonAnimator.setInterpolator(new DecelerateInterpolator()); 423 424 AnimatorSet set = new AnimatorSet(); 425 AnimatorSet.Builder b = set.play(buttonAnimator); 426 427 if (mMenuView != null) { 428 final int count = mMenuView.getChildCount(); 429 if (count > 0) { 430 for (int i = 0; i < 0; i++) { 431 View child = mMenuView.getChildAt(i); 432 child.setScaleY(0); 433 ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0); 434 a.setDuration(300); 435 b.with(a); 436 } 437 } 438 } 439 440 return set; 441 } 442 443 @Override 444 protected void onLayout(boolean changed, int l, int t, int r, int b) { 445 int x = getPaddingLeft(); 446 final int y = getPaddingTop(); 447 final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); 448 449 if (mClose != null && mClose.getVisibility() != GONE) { 450 MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); 451 x += lp.leftMargin; 452 x += positionChild(mClose, x, y, contentHeight); 453 x += lp.rightMargin; 454 455 if (mAnimateInOnLayout) { 456 mAnimationMode = ANIMATE_IN; 457 mCurrentAnimation = makeInAnimation(); 458 mCurrentAnimation.start(); 459 mAnimateInOnLayout = false; 460 } 461 } 462 463 if (mTitleLayout != null && mCustomView == null) { 464 x += positionChild(mTitleLayout, x, y, contentHeight); 465 } 466 467 if (mCustomView != null) { 468 x += positionChild(mCustomView, x, y, contentHeight); 469 } 470 471 x = r - l - getPaddingRight(); 472 473 if (mMenuView != null) { 474 x -= positionChildInverse(mMenuView, x, y, contentHeight); 475 } 476 } 477 478 @Override 479 public void onAnimationStart(Animator animation) { 480 } 481 482 @Override 483 public void onAnimationEnd(Animator animation) { 484 if (mAnimationMode == ANIMATE_OUT) { 485 killMode(); 486 } 487 mAnimationMode = ANIMATE_IDLE; 488 } 489 490 @Override 491 public void onAnimationCancel(Animator animation) { 492 } 493 494 @Override 495 public void onAnimationRepeat(Animator animation) { 496 } 497 498 @Override 499 public boolean shouldDelayChildPressedState() { 500 return false; 501 } 502 503 @Override 504 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 505 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { 506 // Action mode started 507 event.setSource(this); 508 event.setClassName(getClass().getName()); 509 event.setPackageName(getContext().getPackageName()); 510 event.setContentDescription(mTitle); 511 } else { 512 super.onInitializeAccessibilityEvent(event); 513 } 514 } 515} 516