TaskView.java revision 521e7dc0a6782371a710ee81bf804f7e97aaa292
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.TimeInterpolator; 20import android.animation.ValueAnimator; 21import android.content.Context; 22import android.graphics.Canvas; 23import android.graphics.Outline; 24import android.graphics.Path; 25import android.graphics.Point; 26import android.graphics.Rect; 27import android.graphics.RectF; 28import android.util.AttributeSet; 29import android.view.MotionEvent; 30import android.view.View; 31import android.view.animation.AccelerateInterpolator; 32import android.widget.FrameLayout; 33import com.android.systemui.R; 34import com.android.systemui.recents.Constants; 35import com.android.systemui.recents.RecentsConfiguration; 36import com.android.systemui.recents.model.Task; 37 38 39/* A task view */ 40public class TaskView extends FrameLayout implements Task.TaskCallbacks, View.OnClickListener, 41 View.OnLongClickListener { 42 /** The TaskView callbacks */ 43 interface TaskViewCallbacks { 44 public void onTaskIconClicked(TaskView tv); 45 public void onTaskAppInfoClicked(TaskView tv); 46 public void onTaskFocused(TaskView tv); 47 public void onTaskDismissed(TaskView tv); 48 49 // public void onTaskViewReboundToTask(TaskView tv, Task t); 50 } 51 52 int mDim; 53 int mMaxDim; 54 TimeInterpolator mDimInterpolator = new AccelerateInterpolator(); 55 56 Task mTask; 57 boolean mTaskDataLoaded; 58 boolean mIsFocused; 59 boolean mClipViewInStack; 60 Point mLastTouchDown = new Point(); 61 Path mRoundedRectClipPath = new Path(); 62 63 TaskThumbnailView mThumbnailView; 64 TaskBarView mBarView; 65 TaskViewCallbacks mCb; 66 67 68 public TaskView(Context context) { 69 this(context, null); 70 } 71 72 public TaskView(Context context, AttributeSet attrs) { 73 this(context, attrs, 0); 74 } 75 76 public TaskView(Context context, AttributeSet attrs, int defStyleAttr) { 77 this(context, attrs, defStyleAttr, 0); 78 } 79 80 public TaskView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 81 super(context, attrs, defStyleAttr, defStyleRes); 82 setWillNotDraw(false); 83 } 84 85 @Override 86 protected void onFinishInflate() { 87 RecentsConfiguration config = RecentsConfiguration.getInstance(); 88 mMaxDim = config.taskStackMaxDim; 89 90 // By default, all views are clipped to other views in their stack 91 mClipViewInStack = true; 92 93 // Bind the views 94 mThumbnailView = (TaskThumbnailView) findViewById(R.id.task_view_thumbnail); 95 mBarView = (TaskBarView) findViewById(R.id.task_view_bar); 96 97 if (mTaskDataLoaded) { 98 onTaskDataLoaded(false); 99 } 100 } 101 102 @Override 103 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 104 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 105 106 // Update the rounded rect clip path 107 RecentsConfiguration config = RecentsConfiguration.getInstance(); 108 float radius = config.taskViewRoundedCornerRadiusPx; 109 mRoundedRectClipPath.reset(); 110 mRoundedRectClipPath.addRoundRect(new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()), 111 radius, radius, Path.Direction.CW); 112 113 // Update the outline 114 Outline o = new Outline(); 115 o.setRoundRect(0, 0, getMeasuredWidth(), getMeasuredHeight() - 116 config.taskViewShadowOutlineBottomInsetPx, radius); 117 setOutline(o); 118 } 119 120 @Override 121 public boolean onInterceptTouchEvent(MotionEvent ev) { 122 switch (ev.getAction()) { 123 case MotionEvent.ACTION_DOWN: 124 case MotionEvent.ACTION_MOVE: 125 mLastTouchDown.set((int) ev.getX(), (int) ev.getY()); 126 break; 127 } 128 return super.onInterceptTouchEvent(ev); 129 } 130 131 /** Set callback */ 132 void setCallbacks(TaskViewCallbacks cb) { 133 mCb = cb; 134 } 135 136 /** Gets the task */ 137 Task getTask() { 138 return mTask; 139 } 140 141 /** Synchronizes this view's properties with the task's transform */ 142 void updateViewPropertiesToTaskTransform(TaskViewTransform animateFromTransform, 143 TaskViewTransform toTransform, int duration) { 144 RecentsConfiguration config = RecentsConfiguration.getInstance(); 145 int minZ = config.taskViewTranslationZMinPx; 146 int incZ = config.taskViewTranslationZIncrementPx; 147 148 // Update the bar view 149 mBarView.updateViewPropertiesToTaskTransform(animateFromTransform, toTransform, duration); 150 151 // Update this task view 152 if (duration > 0) { 153 if (animateFromTransform != null) { 154 setTranslationY(animateFromTransform.translationY); 155 if (Constants.DebugFlags.App.EnableShadows) { 156 setTranslationZ(Math.max(minZ, minZ + (animateFromTransform.t * incZ))); 157 } 158 setScaleX(animateFromTransform.scale); 159 setScaleY(animateFromTransform.scale); 160 setAlpha(animateFromTransform.alpha); 161 } 162 if (Constants.DebugFlags.App.EnableShadows) { 163 animate().translationZ(Math.max(minZ, minZ + (toTransform.t * incZ))); 164 } 165 animate().translationY(toTransform.translationY) 166 .scaleX(toTransform.scale) 167 .scaleY(toTransform.scale) 168 .alpha(toTransform.alpha) 169 .setDuration(duration) 170 .setInterpolator(config.fastOutSlowInInterpolator) 171 .withLayer() 172 .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 173 @Override 174 public void onAnimationUpdate(ValueAnimator animation) { 175 updateDimOverlayFromScale(); 176 } 177 }) 178 .start(); 179 } else { 180 setTranslationY(toTransform.translationY); 181 if (Constants.DebugFlags.App.EnableShadows) { 182 setTranslationZ(Math.max(minZ, minZ + (toTransform.t * incZ))); 183 } 184 setScaleX(toTransform.scale); 185 setScaleY(toTransform.scale); 186 setAlpha(toTransform.alpha); 187 } 188 updateDimOverlayFromScale(); 189 invalidate(); 190 } 191 192 /** Resets this view's properties */ 193 void resetViewProperties() { 194 setTranslationX(0f); 195 setTranslationY(0f); 196 if (Constants.DebugFlags.App.EnableShadows) { 197 setTranslationZ(0f); 198 } 199 setScaleX(1f); 200 setScaleY(1f); 201 setAlpha(1f); 202 invalidate(); 203 } 204 205 /** 206 * When we are un/filtering, this method will set up the transform that we are animating to, 207 * in order to hide the task. 208 */ 209 void prepareTaskTransformForFilterTaskHidden(TaskViewTransform toTransform) { 210 // Fade the view out and slide it away 211 toTransform.alpha = 0f; 212 toTransform.translationY += 200; 213 } 214 215 /** 216 * When we are un/filtering, this method will setup the transform that we are animating from, 217 * in order to show the task. 218 */ 219 void prepareTaskTransformForFilterTaskVisible(TaskViewTransform fromTransform) { 220 // Fade the view in 221 fromTransform.alpha = 0f; 222 } 223 224 /** Prepares this task view for the enter-recents animations. This is called earlier in the 225 * first layout because the actual animation into recents may take a long time. */ 226 public void prepareAnimateOnEnterRecents() { 227 mBarView.setVisibility(View.INVISIBLE); 228 } 229 230 /** Animates this task view as it enters recents */ 231 public void animateOnEnterRecents() { 232 RecentsConfiguration config = RecentsConfiguration.getInstance(); 233 mBarView.setVisibility(View.VISIBLE); 234 mBarView.setTranslationY(-mBarView.getMeasuredHeight()); 235 mBarView.animate() 236 .translationY(0) 237 .setStartDelay(config.taskBarEnterAnimDelay) 238 .setInterpolator(config.fastOutSlowInInterpolator) 239 .setDuration(config.taskBarEnterAnimDuration) 240 .withLayer() 241 .start(); 242 } 243 244 /** Animates this task view as it exits recents */ 245 public void animateOnLeavingRecents(final Runnable r) { 246 RecentsConfiguration config = RecentsConfiguration.getInstance(); 247 mBarView.animate() 248 .translationY(-mBarView.getMeasuredHeight()) 249 .setStartDelay(0) 250 .setInterpolator(config.fastOutLinearInInterpolator) 251 .setDuration(config.taskBarExitAnimDuration) 252 .withLayer() 253 .withEndAction(new Runnable() { 254 @Override 255 public void run() { 256 post(r); 257 } 258 }) 259 .start(); 260 } 261 262 /** Animates the deletion of this task view */ 263 public void animateRemoval(final Runnable r) { 264 // Disabling clipping with the stack while the view is animating away 265 setClipViewInStack(false); 266 267 RecentsConfiguration config = RecentsConfiguration.getInstance(); 268 animate().translationX(config.taskViewRemoveAnimTranslationXPx) 269 .alpha(0f) 270 .setStartDelay(0) 271 .setInterpolator(config.fastOutSlowInInterpolator) 272 .setDuration(config.taskViewRemoveAnimDuration) 273 .withLayer() 274 .withEndAction(new Runnable() { 275 @Override 276 public void run() { 277 post(r); 278 279 // Re-enable clipping with the stack (we will reuse this view) 280 setClipViewInStack(false); 281 } 282 }) 283 .start(); 284 } 285 286 /** Returns the rect we want to clip (it may not be the full rect) */ 287 Rect getClippingRect(Rect outRect) { 288 getHitRect(outRect); 289 // XXX: We should get the hit rect of the thumbnail view and intersect, but this is faster 290 outRect.right = outRect.left + mThumbnailView.getRight(); 291 outRect.bottom = outRect.top + mThumbnailView.getBottom(); 292 return outRect; 293 } 294 295 /** Enable the hw layers on this task view */ 296 void enableHwLayers() { 297 mThumbnailView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 298 } 299 300 /** Disable the hw layers on this task view */ 301 void disableHwLayers() { 302 mThumbnailView.setLayerType(View.LAYER_TYPE_NONE, null); 303 } 304 305 /** 306 * Returns whether this view should be clipped, or any views below should clip against this 307 * view. 308 */ 309 boolean shouldClipViewInStack() { 310 return mClipViewInStack; 311 } 312 313 /** Sets whether this view should be clipped, or clipped against. */ 314 void setClipViewInStack(boolean clip) { 315 if (clip != mClipViewInStack) { 316 mClipViewInStack = clip; 317 if (getParent() instanceof View) { 318 Rect r = new Rect(); 319 getHitRect(r); 320 ((View) getParent()).invalidate(r); 321 } 322 } 323 } 324 325 /** Update the dim as a function of the scale of this view. */ 326 void updateDimOverlayFromScale() { 327 float minScale = Constants.Values.TaskStackView.StackPeekMinScale; 328 float scaleRange = 1f - minScale; 329 float dim = (1f - getScaleX()) / scaleRange; 330 dim = mDimInterpolator.getInterpolation(Math.min(dim, 1f)); 331 mDim = Math.max(0, Math.min(mMaxDim, (int) (dim * 255))); 332 } 333 334 @Override 335 public void draw(Canvas canvas) { 336 // Apply the rounded rect clip path on the whole view 337 canvas.clipPath(mRoundedRectClipPath); 338 339 super.draw(canvas); 340 341 // Apply the dim if necessary 342 if (mDim > 0) { 343 canvas.drawColor(mDim << 24); 344 } 345 } 346 347 /** 348 * Sets the focused task explicitly. We need a separate flag because requestFocus() won't happen 349 * if the view is not currently visible, or we are in touch state (where we still want to keep 350 * track of focus). 351 */ 352 public void setFocusedTask() { 353 mIsFocused = true; 354 requestFocus(); 355 invalidate(); 356 mCb.onTaskFocused(this); 357 } 358 359 /** 360 * Updates the explicitly focused state when the view focus changes. 361 */ 362 @Override 363 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { 364 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 365 if (!gainFocus) { 366 mIsFocused = false; 367 invalidate(); 368 } 369 } 370 371 /** 372 * Returns whether we have explicitly been focused. 373 */ 374 public boolean isFocusedTask() { 375 return mIsFocused || isFocused(); 376 } 377 378 /**** TaskCallbacks Implementation ****/ 379 380 /** Binds this task view to the task */ 381 public void onTaskBound(Task t) { 382 mTask = t; 383 mTask.setCallbacks(this); 384 } 385 386 @Override 387 public void onTaskDataLoaded(boolean reloadingTaskData) { 388 if (mThumbnailView != null && mBarView != null) { 389 // Bind each of the views to the new task data 390 mThumbnailView.rebindToTask(mTask, reloadingTaskData); 391 mBarView.rebindToTask(mTask, reloadingTaskData); 392 // Rebind any listeners 393 mBarView.mApplicationIcon.setOnClickListener(this); 394 mBarView.mDismissButton.setOnClickListener(this); 395 if (Constants.DebugFlags.App.EnableDevAppInfoOnLongPress) { 396 RecentsConfiguration config = RecentsConfiguration.getInstance(); 397 if (config.developerOptionsEnabled) { 398 mBarView.mApplicationIcon.setOnLongClickListener(this); 399 } 400 } 401 } 402 mTaskDataLoaded = true; 403 } 404 405 @Override 406 public void onTaskDataUnloaded() { 407 if (mThumbnailView != null && mBarView != null) { 408 // Unbind each of the views from the task data and remove the task callback 409 mTask.setCallbacks(null); 410 mThumbnailView.unbindFromTask(); 411 mBarView.unbindFromTask(); 412 // Unbind any listeners 413 mBarView.mApplicationIcon.setOnClickListener(null); 414 mBarView.mDismissButton.setOnClickListener(null); 415 if (Constants.DebugFlags.App.EnableDevAppInfoOnLongPress) { 416 mBarView.mApplicationIcon.setOnLongClickListener(null); 417 } 418 } 419 mTaskDataLoaded = false; 420 } 421 422 @Override 423 public void onClick(View v) { 424 if (v == mBarView.mApplicationIcon) { 425 mCb.onTaskIconClicked(this); 426 } else if (v == mBarView.mDismissButton) { 427 // Animate out the view and call the callback 428 final TaskView tv = this; 429 animateRemoval(new Runnable() { 430 @Override 431 public void run() { 432 mCb.onTaskDismissed(tv); 433 } 434 }); 435 } 436 } 437 438 @Override 439 public boolean onLongClick(View v) { 440 if (v == mBarView.mApplicationIcon) { 441 mCb.onTaskAppInfoClicked(this); 442 return true; 443 } 444 return false; 445 } 446} 447