TaskViewHeader.java revision 88f3db9e2180be5e2d3c524b9f1dd99e05465041
1/* 2 * Copyright (C) 2014 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 */ 16 17package com.android.systemui.recents.views; 18 19import android.animation.Animator; 20import android.animation.AnimatorListenerAdapter; 21import android.animation.AnimatorSet; 22import android.animation.ArgbEvaluator; 23import android.animation.ObjectAnimator; 24import android.animation.ValueAnimator; 25import android.content.Context; 26import android.content.res.ColorStateList; 27import android.graphics.Canvas; 28import android.graphics.Color; 29import android.graphics.drawable.Drawable; 30import android.graphics.drawable.GradientDrawable; 31import android.graphics.drawable.RippleDrawable; 32import android.graphics.Outline; 33import android.graphics.Paint; 34import android.graphics.PorterDuff; 35import android.graphics.PorterDuffColorFilter; 36import android.graphics.PorterDuffXfermode; 37import android.graphics.Rect; 38import android.util.AttributeSet; 39import android.view.View; 40import android.view.ViewOutlineProvider; 41import android.widget.FrameLayout; 42import android.widget.ImageView; 43import android.widget.TextView; 44import com.android.systemui.R; 45import com.android.systemui.recents.Constants; 46import com.android.systemui.recents.RecentsConfiguration; 47import com.android.systemui.recents.misc.SystemServicesProxy; 48import com.android.systemui.recents.misc.Utilities; 49import com.android.systemui.recents.model.RecentsTaskLoader; 50import com.android.systemui.recents.model.Task; 51 52 53/* The task bar view */ 54public class TaskViewHeader extends FrameLayout { 55 56 RecentsConfiguration mConfig; 57 private SystemServicesProxy mSsp; 58 59 // Header views 60 ImageView mMoveTaskButton; 61 ImageView mDismissButton; 62 ImageView mApplicationIcon; 63 TextView mActivityDescription; 64 65 // Header drawables 66 boolean mCurrentPrimaryColorIsDark; 67 int mCurrentPrimaryColor; 68 int mBackgroundColor; 69 Drawable mLightDismissDrawable; 70 Drawable mDarkDismissDrawable; 71 RippleDrawable mBackground; 72 GradientDrawable mBackgroundColorDrawable; 73 AnimatorSet mFocusAnimator; 74 String mDismissContentDescription; 75 76 // Static highlight that we draw at the top of each view 77 static Paint sHighlightPaint; 78 79 // Header dim, which is only used when task view hardware layers are not used 80 Paint mDimLayerPaint = new Paint(); 81 PorterDuffColorFilter mDimColorFilter = new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_ATOP); 82 83 boolean mLayersDisabled; 84 85 public TaskViewHeader(Context context) { 86 this(context, null); 87 } 88 89 public TaskViewHeader(Context context, AttributeSet attrs) { 90 this(context, attrs, 0); 91 } 92 93 public TaskViewHeader(Context context, AttributeSet attrs, int defStyleAttr) { 94 this(context, attrs, defStyleAttr, 0); 95 } 96 97 public TaskViewHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 98 super(context, attrs, defStyleAttr, defStyleRes); 99 mConfig = RecentsConfiguration.getInstance(); 100 mSsp = RecentsTaskLoader.getInstance().getSystemServicesProxy(); 101 setWillNotDraw(false); 102 setClipToOutline(true); 103 setOutlineProvider(new ViewOutlineProvider() { 104 @Override 105 public void getOutline(View view, Outline outline) { 106 outline.setRect(0, 0, getMeasuredWidth(), getMeasuredHeight()); 107 } 108 }); 109 110 // Load the dismiss resources 111 mLightDismissDrawable = context.getDrawable(R.drawable.recents_dismiss_light); 112 mDarkDismissDrawable = context.getDrawable(R.drawable.recents_dismiss_dark); 113 mDismissContentDescription = 114 context.getString(R.string.accessibility_recents_item_will_be_dismissed); 115 116 // Configure the highlight paint 117 if (sHighlightPaint == null) { 118 sHighlightPaint = new Paint(); 119 sHighlightPaint.setStyle(Paint.Style.STROKE); 120 sHighlightPaint.setStrokeWidth(mConfig.taskViewHighlightPx); 121 sHighlightPaint.setColor(mConfig.taskBarViewHighlightColor); 122 sHighlightPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD)); 123 sHighlightPaint.setAntiAlias(true); 124 } 125 } 126 127 @Override 128 protected void onFinishInflate() { 129 // Initialize the icon and description views 130 mApplicationIcon = (ImageView) findViewById(R.id.application_icon); 131 mActivityDescription = (TextView) findViewById(R.id.activity_description); 132 mDismissButton = (ImageView) findViewById(R.id.dismiss_task); 133 mMoveTaskButton = (ImageView) findViewById(R.id.move_task); 134 135 // Hide the backgrounds if they are ripple drawables 136 if (!Constants.DebugFlags.App.EnableTaskFiltering) { 137 if (mApplicationIcon.getBackground() instanceof RippleDrawable) { 138 mApplicationIcon.setBackground(null); 139 } 140 } 141 142 mBackgroundColorDrawable = (GradientDrawable) getContext().getDrawable(R.drawable 143 .recents_task_view_header_bg_color); 144 // Copy the ripple drawable since we are going to be manipulating it 145 mBackground = (RippleDrawable) 146 getContext().getDrawable(R.drawable.recents_task_view_header_bg); 147 mBackground = (RippleDrawable) mBackground.mutate().getConstantState().newDrawable(); 148 mBackground.setColor(ColorStateList.valueOf(0)); 149 mBackground.setDrawableByLayerId(mBackground.getId(0), mBackgroundColorDrawable); 150 setBackground(mBackground); 151 } 152 153 @Override 154 protected void onDraw(Canvas canvas) { 155 // Draw the highlight at the top edge (but put the bottom edge just out of view) 156 float offset = (float) Math.ceil(mConfig.taskViewHighlightPx / 2f); 157 float radius = mConfig.taskViewRoundedCornerRadiusPx; 158 int count = canvas.save(Canvas.CLIP_SAVE_FLAG); 159 canvas.clipRect(0, 0, getMeasuredWidth(), getMeasuredHeight()); 160 canvas.drawRoundRect(-offset, 0f, (float) getMeasuredWidth() + offset, 161 getMeasuredHeight() + radius, radius, radius, sHighlightPaint); 162 canvas.restoreToCount(count); 163 } 164 165 @Override 166 public boolean hasOverlappingRendering() { 167 return false; 168 } 169 170 /** 171 * Sets the dim alpha, only used when we are not using hardware layers. 172 * (see RecentsConfiguration.useHardwareLayers) 173 */ 174 void setDimAlpha(int alpha) { 175 mDimColorFilter.setColor(Color.argb(alpha, 0, 0, 0)); 176 mDimLayerPaint.setColorFilter(mDimColorFilter); 177 if (!mLayersDisabled) { 178 setLayerType(LAYER_TYPE_HARDWARE, mDimLayerPaint); 179 } 180 } 181 182 /** Returns the secondary color for a primary color. */ 183 int getSecondaryColor(int primaryColor, boolean useLightOverlayColor) { 184 int overlayColor = useLightOverlayColor ? Color.WHITE : Color.BLACK; 185 return Utilities.getColorWithOverlay(primaryColor, overlayColor, 0.8f); 186 } 187 188 /** Binds the bar view to the task */ 189 public void rebindToTask(Task t) { 190 // If an activity icon is defined, then we use that as the primary icon to show in the bar, 191 // otherwise, we fall back to the application icon 192 if (t.activityIcon != null) { 193 mApplicationIcon.setImageDrawable(t.activityIcon); 194 } else if (t.applicationIcon != null) { 195 mApplicationIcon.setImageDrawable(t.applicationIcon); 196 } 197 mApplicationIcon.setContentDescription(t.contentDescription); 198 if (!mActivityDescription.getText().toString().equals(t.activityLabel)) { 199 mActivityDescription.setText(t.activityLabel); 200 } 201 mActivityDescription.setContentDescription(t.contentDescription); 202 203 // Try and apply the system ui tint 204 int existingBgColor = getBackgroundColor(); 205 if (existingBgColor != t.colorPrimary) { 206 mBackgroundColorDrawable.setColor(t.colorPrimary); 207 mBackgroundColor = t.colorPrimary; 208 } 209 mCurrentPrimaryColor = t.colorPrimary; 210 mCurrentPrimaryColorIsDark = t.useLightOnPrimaryColor; 211 mActivityDescription.setTextColor(t.useLightOnPrimaryColor ? 212 mConfig.taskBarViewLightTextColor : mConfig.taskBarViewDarkTextColor); 213 mDismissButton.setImageDrawable(t.useLightOnPrimaryColor ? 214 mLightDismissDrawable : mDarkDismissDrawable); 215 mDismissButton.setContentDescription(String.format(mDismissContentDescription, 216 t.contentDescription)); 217 mMoveTaskButton.setVisibility((mConfig.multiStackEnabled) ? View.VISIBLE : View.INVISIBLE); 218 if (mConfig.multiStackEnabled) { 219 updateResizeTaskBarIcon(t); 220 } 221 } 222 223 /** Updates the resize task bar button. */ 224 void updateResizeTaskBarIcon(Task t) { 225 Rect display = mSsp.getWindowRect(); 226 Rect taskRect = mSsp.getTaskBounds(t.key.stackId); 227 int resId = R.drawable.star; 228 if (display.equals(taskRect) || taskRect.isEmpty()) { 229 resId = R.drawable.vector_drawable_place_fullscreen; 230 } else { 231 boolean top = display.top == taskRect.top; 232 boolean bottom = display.bottom == taskRect.bottom; 233 boolean left = display.left == taskRect.left; 234 boolean right = display.right == taskRect.right; 235 if (top && bottom && left) { 236 resId = R.drawable.vector_drawable_place_left; 237 } else if (top && bottom && right) { 238 resId = R.drawable.vector_drawable_place_right; 239 } else if (top && left && right) { 240 resId = R.drawable.vector_drawable_place_top; 241 } else if (bottom && left && right) { 242 resId = R.drawable.vector_drawable_place_bottom; 243 } else if (top && right) { 244 resId = R.drawable.vector_drawable_place_top_right; 245 } else if (top && left) { 246 resId = R.drawable.vector_drawable_place_top_left; 247 } else if (bottom && right) { 248 resId = R.drawable.vector_drawable_place_bottom_right; 249 } else if (bottom && left) { 250 resId = R.drawable.vector_drawable_place_bottom_left; 251 } 252 } 253 mMoveTaskButton.setImageResource(resId); 254 } 255 256 /** Unbinds the bar view from the task */ 257 void unbindFromTask() { 258 mApplicationIcon.setImageDrawable(null); 259 } 260 261 /** Animates this task bar dismiss button when launching a task. */ 262 void startLaunchTaskDismissAnimation() { 263 if (mDismissButton.getVisibility() == View.VISIBLE) { 264 mDismissButton.animate().cancel(); 265 mDismissButton.animate() 266 .alpha(0f) 267 .setStartDelay(0) 268 .setInterpolator(mConfig.fastOutSlowInInterpolator) 269 .setDuration(mConfig.taskViewExitToAppDuration) 270 .start(); 271 } 272 } 273 274 /** Animates this task bar if the user does not interact with the stack after a certain time. */ 275 void startNoUserInteractionAnimation() { 276 if (mDismissButton.getVisibility() != View.VISIBLE) { 277 mDismissButton.setVisibility(View.VISIBLE); 278 mDismissButton.setAlpha(0f); 279 mDismissButton.animate() 280 .alpha(1f) 281 .setStartDelay(0) 282 .setInterpolator(mConfig.fastOutLinearInInterpolator) 283 .setDuration(mConfig.taskViewEnterFromAppDuration) 284 .start(); 285 } 286 } 287 288 /** Mark this task view that the user does has not interacted with the stack after a certain time. */ 289 void setNoUserInteractionState() { 290 if (mDismissButton.getVisibility() != View.VISIBLE) { 291 mDismissButton.animate().cancel(); 292 mDismissButton.setVisibility(View.VISIBLE); 293 mDismissButton.setAlpha(1f); 294 } 295 } 296 297 /** Resets the state tracking that the user has not interacted with the stack after a certain time. */ 298 void resetNoUserInteractionState() { 299 mDismissButton.setVisibility(View.INVISIBLE); 300 } 301 302 @Override 303 protected int[] onCreateDrawableState(int extraSpace) { 304 305 // Don't forward our state to the drawable - we do it manually in onTaskViewFocusChanged. 306 // This is to prevent layer trashing when the view is pressed. 307 return new int[] {}; 308 } 309 310 @Override 311 protected void dispatchDraw(Canvas canvas) { 312 super.dispatchDraw(canvas); 313 if (mLayersDisabled) { 314 mLayersDisabled = false; 315 postOnAnimation(new Runnable() { 316 @Override 317 public void run() { 318 mLayersDisabled = false; 319 setLayerType(LAYER_TYPE_HARDWARE, mDimLayerPaint); 320 } 321 }); 322 } 323 } 324 325 public void disableLayersForOneFrame() { 326 mLayersDisabled = true; 327 328 // Disable layer for a frame so we can draw our first frame faster. 329 setLayerType(LAYER_TYPE_NONE, null); 330 } 331 332 /** Notifies the associated TaskView has been focused. */ 333 void onTaskViewFocusChanged(boolean focused, boolean animateFocusedState) { 334 // If we are not animating the visible state, just return 335 if (!animateFocusedState) return; 336 337 boolean isRunning = false; 338 if (mFocusAnimator != null) { 339 isRunning = mFocusAnimator.isRunning(); 340 Utilities.cancelAnimationWithoutCallbacks(mFocusAnimator); 341 } 342 343 if (focused) { 344 int currentColor = mBackgroundColor; 345 int secondaryColor = getSecondaryColor(mCurrentPrimaryColor, mCurrentPrimaryColorIsDark); 346 int[][] states = new int[][] { 347 new int[] {}, 348 new int[] { android.R.attr.state_enabled }, 349 new int[] { android.R.attr.state_pressed } 350 }; 351 int[] newStates = new int[]{ 352 0, 353 android.R.attr.state_enabled, 354 android.R.attr.state_pressed 355 }; 356 int[] colors = new int[] { 357 currentColor, 358 secondaryColor, 359 secondaryColor 360 }; 361 mBackground.setColor(new ColorStateList(states, colors)); 362 mBackground.setState(newStates); 363 // Pulse the background color 364 int lightPrimaryColor = getSecondaryColor(mCurrentPrimaryColor, mCurrentPrimaryColorIsDark); 365 ValueAnimator backgroundColor = ValueAnimator.ofObject(new ArgbEvaluator(), 366 currentColor, lightPrimaryColor); 367 backgroundColor.addListener(new AnimatorListenerAdapter() { 368 @Override 369 public void onAnimationStart(Animator animation) { 370 mBackground.setState(new int[]{}); 371 } 372 }); 373 backgroundColor.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 374 @Override 375 public void onAnimationUpdate(ValueAnimator animation) { 376 int color = (int) animation.getAnimatedValue(); 377 mBackgroundColorDrawable.setColor(color); 378 mBackgroundColor = color; 379 } 380 }); 381 backgroundColor.setRepeatCount(ValueAnimator.INFINITE); 382 backgroundColor.setRepeatMode(ValueAnimator.REVERSE); 383 // Pulse the translation 384 ObjectAnimator translation = ObjectAnimator.ofFloat(this, "translationZ", 15f); 385 translation.setRepeatCount(ValueAnimator.INFINITE); 386 translation.setRepeatMode(ValueAnimator.REVERSE); 387 388 mFocusAnimator = new AnimatorSet(); 389 mFocusAnimator.playTogether(backgroundColor, translation); 390 mFocusAnimator.setStartDelay(150); 391 mFocusAnimator.setDuration(750); 392 mFocusAnimator.start(); 393 } else { 394 if (isRunning) { 395 // Restore the background color 396 int currentColor = mBackgroundColor; 397 ValueAnimator backgroundColor = ValueAnimator.ofObject(new ArgbEvaluator(), 398 currentColor, mCurrentPrimaryColor); 399 backgroundColor.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 400 @Override 401 public void onAnimationUpdate(ValueAnimator animation) { 402 int color = (int) animation.getAnimatedValue(); 403 mBackgroundColorDrawable.setColor(color); 404 mBackgroundColor = color; 405 } 406 }); 407 // Restore the translation 408 ObjectAnimator translation = ObjectAnimator.ofFloat(this, "translationZ", 0f); 409 410 mFocusAnimator = new AnimatorSet(); 411 mFocusAnimator.playTogether(backgroundColor, translation); 412 mFocusAnimator.setDuration(150); 413 mFocusAnimator.start(); 414 } else { 415 mBackground.setState(new int[] {}); 416 setTranslationZ(0f); 417 } 418 } 419 } 420} 421