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