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