TaskView.java revision bc571a980704dc767838935e83c6aed231c406e9
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.ObjectAnimator; 21import android.animation.ValueAnimator; 22import android.content.Context; 23import android.graphics.*; 24import android.util.AttributeSet; 25import android.view.View; 26import android.view.ViewOutlineProvider; 27import android.view.animation.AccelerateInterpolator; 28import android.widget.FrameLayout; 29import com.android.systemui.R; 30import com.android.systemui.recents.Constants; 31import com.android.systemui.recents.RecentsConfiguration; 32import com.android.systemui.recents.misc.Utilities; 33import com.android.systemui.recents.model.Task; 34import com.android.systemui.statusbar.phone.PhoneStatusBar; 35 36/* A task view */ 37public class TaskView extends FrameLayout implements Task.TaskCallbacks, 38 View.OnClickListener, View.OnLongClickListener { 39 40 /** The TaskView callbacks */ 41 interface TaskViewCallbacks { 42 public void onTaskViewAppIconClicked(TaskView tv); 43 public void onTaskViewAppInfoClicked(TaskView tv); 44 public void onTaskViewClicked(TaskView tv, Task task, boolean lockToTask); 45 public void onTaskViewDismissed(TaskView tv); 46 public void onTaskViewClipStateChanged(TaskView tv); 47 public void onTaskViewFocusChanged(TaskView tv, boolean focused); 48 } 49 50 RecentsConfiguration mConfig; 51 52 float mTaskProgress; 53 ObjectAnimator mTaskProgressAnimator; 54 float mMaxDimScale; 55 int mDimAlpha; 56 AccelerateInterpolator mDimInterpolator = new AccelerateInterpolator(1f); 57 PorterDuffColorFilter mDimColorFilter = new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_ATOP); 58 Paint mDimLayerPaint = new Paint(); 59 float mActionButtonTranslationZ; 60 61 Task mTask; 62 boolean mTaskDataLoaded; 63 boolean mIsFocused; 64 boolean mFocusAnimationsEnabled; 65 boolean mClipViewInStack; 66 AnimateableViewBounds mViewBounds; 67 68 View mContent; 69 TaskViewThumbnail mThumbnailView; 70 TaskViewHeader mHeaderView; 71 View mActionButtonView; 72 TaskViewCallbacks mCb; 73 74 // Optimizations 75 ValueAnimator.AnimatorUpdateListener mUpdateDimListener = 76 new ValueAnimator.AnimatorUpdateListener() { 77 @Override 78 public void onAnimationUpdate(ValueAnimator animation) { 79 setTaskProgress((Float) animation.getAnimatedValue()); 80 } 81 }; 82 83 84 public TaskView(Context context) { 85 this(context, null); 86 } 87 88 public TaskView(Context context, AttributeSet attrs) { 89 this(context, attrs, 0); 90 } 91 92 public TaskView(Context context, AttributeSet attrs, int defStyleAttr) { 93 this(context, attrs, defStyleAttr, 0); 94 } 95 96 public TaskView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 97 super(context, attrs, defStyleAttr, defStyleRes); 98 mConfig = RecentsConfiguration.getInstance(); 99 mMaxDimScale = mConfig.taskStackMaxDim / 255f; 100 mClipViewInStack = true; 101 mViewBounds = new AnimateableViewBounds(this, mConfig.taskViewRoundedCornerRadiusPx); 102 setTaskProgress(getTaskProgress()); 103 setDim(getDim()); 104 if (mConfig.fakeShadows) { 105 setBackground(new FakeShadowDrawable(context.getResources(), mConfig)); 106 } 107 setOutlineProvider(mViewBounds); 108 } 109 110 /** Set callback */ 111 void setCallbacks(TaskViewCallbacks cb) { 112 mCb = cb; 113 } 114 115 /** Resets this TaskView for reuse. */ 116 void reset() { 117 resetViewProperties(); 118 resetNoUserInteractionState(); 119 setClipViewInStack(false); 120 setCallbacks(null); 121 } 122 123 /** Gets the task */ 124 Task getTask() { 125 return mTask; 126 } 127 128 /** Returns the view bounds. */ 129 AnimateableViewBounds getViewBounds() { 130 return mViewBounds; 131 } 132 133 @Override 134 protected void onFinishInflate() { 135 // Bind the views 136 mContent = findViewById(R.id.task_view_content); 137 mHeaderView = (TaskViewHeader) findViewById(R.id.task_view_bar); 138 mThumbnailView = (TaskViewThumbnail) findViewById(R.id.task_view_thumbnail); 139 mThumbnailView.updateClipToTaskBar(mHeaderView); 140 mActionButtonView = findViewById(R.id.lock_to_app_fab); 141 mActionButtonView.setOutlineProvider(new ViewOutlineProvider() { 142 @Override 143 public void getOutline(View view, Outline outline) { 144 // Set the outline to match the FAB background 145 outline.setOval(0, 0, mActionButtonView.getWidth(), mActionButtonView.getHeight()); 146 } 147 }); 148 mActionButtonTranslationZ = mActionButtonView.getTranslationZ(); 149 } 150 151 @Override 152 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 153 int width = MeasureSpec.getSize(widthMeasureSpec); 154 int height = MeasureSpec.getSize(heightMeasureSpec); 155 156 int widthWithoutPadding = width - mPaddingLeft - mPaddingRight; 157 int heightWithoutPadding = height - mPaddingTop - mPaddingBottom; 158 159 // Measure the content 160 mContent.measure(MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY), 161 MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY)); 162 163 // Measure the bar view, and action button 164 mHeaderView.measure(MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY), 165 MeasureSpec.makeMeasureSpec(mConfig.taskBarHeight, MeasureSpec.EXACTLY)); 166 mActionButtonView.measure( 167 MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.AT_MOST), 168 MeasureSpec.makeMeasureSpec(heightWithoutPadding, MeasureSpec.AT_MOST)); 169 // Measure the thumbnail to be square 170 mThumbnailView.measure( 171 MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY), 172 MeasureSpec.makeMeasureSpec(widthWithoutPadding, MeasureSpec.EXACTLY)); 173 setMeasuredDimension(width, height); 174 invalidateOutline(); 175 } 176 177 /** Synchronizes this view's properties with the task's transform */ 178 void updateViewPropertiesToTaskTransform(TaskViewTransform toTransform, int duration) { 179 updateViewPropertiesToTaskTransform(toTransform, duration, null); 180 } 181 182 void updateViewPropertiesToTaskTransform(TaskViewTransform toTransform, int duration, 183 ValueAnimator.AnimatorUpdateListener updateCallback) { 184 // Apply the transform 185 toTransform.applyToTaskView(this, duration, mConfig.fastOutSlowInInterpolator, false, 186 !mConfig.fakeShadows, updateCallback); 187 188 // Update the task progress 189 Utilities.cancelAnimationWithoutCallbacks(mTaskProgressAnimator); 190 if (duration <= 0) { 191 setTaskProgress(toTransform.p); 192 } else { 193 mTaskProgressAnimator = ObjectAnimator.ofFloat(this, "taskProgress", toTransform.p); 194 mTaskProgressAnimator.setDuration(duration); 195 mTaskProgressAnimator.addUpdateListener(mUpdateDimListener); 196 mTaskProgressAnimator.start(); 197 } 198 } 199 200 /** Resets this view's properties */ 201 void resetViewProperties() { 202 setDim(0); 203 setLayerType(View.LAYER_TYPE_NONE, null); 204 TaskViewTransform.reset(this); 205 if (mActionButtonView != null) { 206 mActionButtonView.setScaleX(1f); 207 mActionButtonView.setScaleY(1f); 208 mActionButtonView.setAlpha(1f); 209 mActionButtonView.setTranslationZ(mActionButtonTranslationZ); 210 } 211 } 212 213 /** 214 * When we are un/filtering, this method will set up the transform that we are animating to, 215 * in order to hide the task. 216 */ 217 void prepareTaskTransformForFilterTaskHidden(TaskViewTransform toTransform) { 218 // Fade the view out and slide it away 219 toTransform.alpha = 0f; 220 toTransform.translationY += 200; 221 toTransform.translationZ = 0; 222 } 223 224 /** 225 * When we are un/filtering, this method will setup the transform that we are animating from, 226 * in order to show the task. 227 */ 228 void prepareTaskTransformForFilterTaskVisible(TaskViewTransform fromTransform) { 229 // Fade the view in 230 fromTransform.alpha = 0f; 231 } 232 233 /** Prepares this task view for the enter-recents animations. This is called earlier in the 234 * first layout because the actual animation into recents may take a long time. */ 235 void prepareEnterRecentsAnimation(boolean isTaskViewLaunchTargetTask, 236 boolean occludesLaunchTarget, int offscreenY) { 237 int initialDim = getDim(); 238 if (mConfig.launchedFromAppWithThumbnail) { 239 if (isTaskViewLaunchTargetTask) { 240 // Set the dim to 0 so we can animate it in 241 initialDim = 0; 242 // Hide the action button 243 mActionButtonView.setAlpha(0f); 244 } else if (occludesLaunchTarget) { 245 // Move the task view off screen (below) so we can animate it in 246 setTranslationY(offscreenY); 247 } 248 249 } else if (mConfig.launchedFromHome) { 250 // Move the task view off screen (below) so we can animate it in 251 setTranslationY(offscreenY); 252 setTranslationZ(0); 253 setScaleX(1f); 254 setScaleY(1f); 255 } 256 // Apply the current dim 257 setDim(initialDim); 258 // Prepare the thumbnail view alpha 259 mThumbnailView.prepareEnterRecentsAnimation(isTaskViewLaunchTargetTask); 260 } 261 262 /** Animates this task view as it enters recents */ 263 void startEnterRecentsAnimation(final ViewAnimation.TaskViewEnterContext ctx) { 264 final TaskViewTransform transform = ctx.currentTaskTransform; 265 int startDelay = 0; 266 267 if (mConfig.launchedFromAppWithThumbnail) { 268 if (mTask.isLaunchTarget) { 269 // Animate the dim/overlay 270 if (Constants.DebugFlags.App.EnableThumbnailAlphaOnFrontmost) { 271 // Animate the thumbnail alpha before the dim animation (to prevent updating the 272 // hardware layer) 273 mThumbnailView.startEnterRecentsAnimation(mConfig.transitionEnterFromAppDelay, 274 new Runnable() { 275 @Override 276 public void run() { 277 animateDimToProgress(0, mConfig.taskViewEnterFromAppDuration, 278 ctx.postAnimationTrigger.decrementOnAnimationEnd()); 279 } 280 }); 281 } else { 282 // Immediately start the dim animation 283 animateDimToProgress(mConfig.transitionEnterFromAppDelay, 284 mConfig.taskViewEnterFromAppDuration, 285 ctx.postAnimationTrigger.decrementOnAnimationEnd()); 286 } 287 ctx.postAnimationTrigger.increment(); 288 289 // Animate the action button in 290 fadeInActionButton(mConfig.transitionEnterFromAppDelay, 291 mConfig.taskViewEnterFromAppDuration); 292 } else { 293 // Animate the task up if it was occluding the launch target 294 if (ctx.currentTaskOccludesLaunchTarget) { 295 setTranslationY(transform.translationY + mConfig.taskViewAffiliateGroupEnterOffsetPx); 296 setAlpha(0f); 297 animate().alpha(1f) 298 .translationY(transform.translationY) 299 .setStartDelay(mConfig.transitionEnterFromAppDelay) 300 .setUpdateListener(null) 301 .setInterpolator(mConfig.fastOutSlowInInterpolator) 302 .setDuration(mConfig.taskViewEnterFromHomeDuration) 303 .withEndAction(new Runnable() { 304 @Override 305 public void run() { 306 // Decrement the post animation trigger 307 ctx.postAnimationTrigger.decrement(); 308 } 309 }) 310 .start(); 311 ctx.postAnimationTrigger.increment(); 312 } 313 } 314 startDelay = mConfig.transitionEnterFromAppDelay; 315 316 } else if (mConfig.launchedFromHome) { 317 // Animate the tasks up 318 int frontIndex = (ctx.currentStackViewCount - ctx.currentStackViewIndex - 1); 319 int delay = mConfig.transitionEnterFromHomeDelay + 320 frontIndex * mConfig.taskViewEnterFromHomeStaggerDelay; 321 322 setScaleX(transform.scale); 323 setScaleY(transform.scale); 324 if (!mConfig.fakeShadows) { 325 animate().translationZ(transform.translationZ); 326 } 327 animate() 328 .translationY(transform.translationY) 329 .setStartDelay(delay) 330 .setUpdateListener(ctx.updateListener) 331 .setInterpolator(mConfig.quintOutInterpolator) 332 .setDuration(mConfig.taskViewEnterFromHomeDuration + 333 frontIndex * mConfig.taskViewEnterFromHomeStaggerDelay) 334 .withEndAction(new Runnable() { 335 @Override 336 public void run() { 337 // Decrement the post animation trigger 338 ctx.postAnimationTrigger.decrement(); 339 } 340 }) 341 .start(); 342 ctx.postAnimationTrigger.increment(); 343 startDelay = delay; 344 } 345 346 // Enable the focus animations from this point onwards so that they aren't affected by the 347 // window transitions 348 postDelayed(new Runnable() { 349 @Override 350 public void run() { 351 enableFocusAnimations(); 352 } 353 }, startDelay); 354 } 355 356 public void fadeInActionButton(int delay, int duration) { 357 // Hide the action button 358 mActionButtonView.setAlpha(0f); 359 360 // Animate the action button in 361 mActionButtonView.animate().alpha(1f) 362 .setStartDelay(delay) 363 .setDuration(duration) 364 .setInterpolator(PhoneStatusBar.ALPHA_IN) 365 .withLayer() 366 .start(); 367 } 368 369 /** Animates this task view as it leaves recents by pressing home. */ 370 void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) { 371 animate() 372 .translationY(ctx.offscreenTranslationY) 373 .setStartDelay(0) 374 .setUpdateListener(null) 375 .setInterpolator(mConfig.fastOutLinearInInterpolator) 376 .setDuration(mConfig.taskViewExitToHomeDuration) 377 .withEndAction(ctx.postAnimationTrigger.decrementAsRunnable()) 378 .start(); 379 ctx.postAnimationTrigger.increment(); 380 } 381 382 /** Animates this task view as it exits recents */ 383 void startLaunchTaskAnimation(final Runnable postAnimRunnable, boolean isLaunchingTask, 384 boolean occludesLaunchTarget, boolean lockToTask) { 385 if (isLaunchingTask) { 386 // Animate the thumbnail alpha back into full opacity for the window animation out 387 mThumbnailView.startLaunchTaskAnimation(postAnimRunnable); 388 389 // Animate the dim 390 if (mDimAlpha > 0) { 391 ObjectAnimator anim = ObjectAnimator.ofInt(this, "dim", 0); 392 anim.setDuration(mConfig.taskViewExitToAppDuration); 393 anim.setInterpolator(mConfig.fastOutLinearInInterpolator); 394 anim.start(); 395 } 396 397 // Animate the action button away 398 if (!lockToTask) { 399 float toScale = 0.9f; 400 mActionButtonView.animate() 401 .scaleX(toScale) 402 .scaleY(toScale); 403 } 404 mActionButtonView.animate() 405 .alpha(0f) 406 .setStartDelay(0) 407 .setDuration(mConfig.taskViewExitToAppDuration) 408 .setInterpolator(mConfig.fastOutLinearInInterpolator) 409 .withLayer() 410 .start(); 411 } else { 412 // Hide the dismiss button 413 mHeaderView.startLaunchTaskDismissAnimation(); 414 // If this is another view in the task grouping and is in front of the launch task, 415 // animate it away first 416 if (occludesLaunchTarget) { 417 animate().alpha(0f) 418 .translationY(getTranslationY() + mConfig.taskViewAffiliateGroupEnterOffsetPx) 419 .setStartDelay(0) 420 .setUpdateListener(null) 421 .setInterpolator(mConfig.fastOutLinearInInterpolator) 422 .setDuration(mConfig.taskViewExitToAppDuration) 423 .start(); 424 } 425 } 426 } 427 428 /** Animates the deletion of this task view */ 429 void startDeleteTaskAnimation(final Runnable r) { 430 // Disabling clipping with the stack while the view is animating away 431 setClipViewInStack(false); 432 433 animate().translationX(mConfig.taskViewRemoveAnimTranslationXPx) 434 .alpha(0f) 435 .setStartDelay(0) 436 .setUpdateListener(null) 437 .setInterpolator(mConfig.fastOutSlowInInterpolator) 438 .setDuration(mConfig.taskViewRemoveAnimDuration) 439 .withEndAction(new Runnable() { 440 @Override 441 public void run() { 442 // We just throw this into a runnable because starting a view property 443 // animation using layers can cause inconsisten results if we try and 444 // update the layers while the animation is running. In some cases, 445 // the runnabled passed in may start an animation which also uses layers 446 // so we defer all this by posting this. 447 r.run(); 448 449 // Re-enable clipping with the stack (we will reuse this view) 450 setClipViewInStack(true); 451 } 452 }) 453 .start(); 454 } 455 456 /** Animates this task view if the user does not interact with the stack after a certain time. */ 457 void startNoUserInteractionAnimation() { 458 mHeaderView.startNoUserInteractionAnimation(); 459 } 460 461 /** Mark this task view that the user does has not interacted with the stack after a certain time. */ 462 void setNoUserInteractionState() { 463 mHeaderView.setNoUserInteractionState(); 464 } 465 466 /** Resets the state tracking that the user has not interacted with the stack after a certain time. */ 467 void resetNoUserInteractionState() { 468 mHeaderView.resetNoUserInteractionState(); 469 } 470 471 /** Dismisses this task. */ 472 void dismissTask() { 473 // Animate out the view and call the callback 474 final TaskView tv = this; 475 startDeleteTaskAnimation(new Runnable() { 476 @Override 477 public void run() { 478 if (mCb != null) { 479 mCb.onTaskViewDismissed(tv); 480 } 481 } 482 }); 483 } 484 485 /** 486 * Returns whether this view should be clipped, or any views below should clip against this 487 * view. 488 */ 489 boolean shouldClipViewInStack() { 490 return mClipViewInStack && (getVisibility() == View.VISIBLE); 491 } 492 493 /** Sets whether this view should be clipped, or clipped against. */ 494 void setClipViewInStack(boolean clip) { 495 if (clip != mClipViewInStack) { 496 mClipViewInStack = clip; 497 if (mCb != null) { 498 mCb.onTaskViewClipStateChanged(this); 499 } 500 } 501 } 502 503 /** Sets the current task progress. */ 504 public void setTaskProgress(float p) { 505 mTaskProgress = p; 506 mViewBounds.setAlpha(p); 507 updateDimFromTaskProgress(); 508 } 509 510 /** Returns the current task progress. */ 511 public float getTaskProgress() { 512 return mTaskProgress; 513 } 514 515 /** Returns the current dim. */ 516 public void setDim(int dim) { 517 mDimAlpha = dim; 518 if (mConfig.useHardwareLayers) { 519 // Defer setting hardware layers if we have not yet measured, or there is no dim to draw 520 if (getMeasuredWidth() > 0 && getMeasuredHeight() > 0) { 521 mDimColorFilter.setColor(Color.argb(mDimAlpha, 0, 0, 0)); 522 mDimLayerPaint.setColorFilter(mDimColorFilter); 523 mContent.setLayerType(LAYER_TYPE_HARDWARE, mDimLayerPaint); 524 } 525 } else { 526 float dimAlpha = mDimAlpha / 255.0f; 527 if (mThumbnailView != null) { 528 mThumbnailView.setDimAlpha(dimAlpha); 529 } 530 if (mHeaderView != null) { 531 mHeaderView.setDimAlpha(dim); 532 } 533 } 534 } 535 536 /** Returns the current dim. */ 537 public int getDim() { 538 return mDimAlpha; 539 } 540 541 /** Animates the dim to the task progress. */ 542 void animateDimToProgress(int delay, int duration, Animator.AnimatorListener postAnimRunnable) { 543 // Animate the dim into view as well 544 int toDim = getDimFromTaskProgress(); 545 if (toDim != getDim()) { 546 ObjectAnimator anim = ObjectAnimator.ofInt(TaskView.this, "dim", toDim); 547 anim.setStartDelay(delay); 548 anim.setDuration(duration); 549 if (postAnimRunnable != null) { 550 anim.addListener(postAnimRunnable); 551 } 552 anim.start(); 553 } 554 } 555 556 /** Compute the dim as a function of the scale of this view. */ 557 int getDimFromTaskProgress() { 558 float dim = mMaxDimScale * mDimInterpolator.getInterpolation(1f - mTaskProgress); 559 return (int) (dim * 255); 560 } 561 562 /** Update the dim as a function of the scale of this view. */ 563 void updateDimFromTaskProgress() { 564 setDim(getDimFromTaskProgress()); 565 } 566 567 /**** View focus state ****/ 568 569 /** 570 * Sets the focused task explicitly. We need a separate flag because requestFocus() won't happen 571 * if the view is not currently visible, or we are in touch state (where we still want to keep 572 * track of focus). 573 */ 574 public void setFocusedTask(boolean animateFocusedState) { 575 mIsFocused = true; 576 if (mFocusAnimationsEnabled) { 577 // Focus the header bar 578 mHeaderView.onTaskViewFocusChanged(true, animateFocusedState); 579 } 580 // Update the thumbnail alpha with the focus 581 mThumbnailView.onFocusChanged(true); 582 // Call the callback 583 if (mCb != null) { 584 mCb.onTaskViewFocusChanged(this, true); 585 } 586 // Workaround, we don't always want it focusable in touch mode, but we want the first task 587 // to be focused after the enter-recents animation, which can be triggered from either touch 588 // or keyboard 589 setFocusableInTouchMode(true); 590 requestFocus(); 591 setFocusableInTouchMode(false); 592 invalidate(); 593 } 594 595 /** 596 * Unsets the focused task explicitly. 597 */ 598 void unsetFocusedTask() { 599 mIsFocused = false; 600 if (mFocusAnimationsEnabled) { 601 // Un-focus the header bar 602 mHeaderView.onTaskViewFocusChanged(false, true); 603 } 604 605 // Update the thumbnail alpha with the focus 606 mThumbnailView.onFocusChanged(false); 607 // Call the callback 608 if (mCb != null) { 609 mCb.onTaskViewFocusChanged(this, false); 610 } 611 invalidate(); 612 } 613 614 /** 615 * Updates the explicitly focused state when the view focus changes. 616 */ 617 @Override 618 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { 619 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 620 if (!gainFocus) { 621 unsetFocusedTask(); 622 } 623 } 624 625 /** 626 * Returns whether we have explicitly been focused. 627 */ 628 public boolean isFocusedTask() { 629 return mIsFocused || isFocused(); 630 } 631 632 /** Enables all focus animations. */ 633 void enableFocusAnimations() { 634 boolean wasFocusAnimationsEnabled = mFocusAnimationsEnabled; 635 mFocusAnimationsEnabled = true; 636 if (mIsFocused && !wasFocusAnimationsEnabled) { 637 // Re-notify the header if we were focused and animations were not previously enabled 638 mHeaderView.onTaskViewFocusChanged(true, true); 639 } 640 } 641 642 /**** TaskCallbacks Implementation ****/ 643 644 /** Binds this task view to the task */ 645 public void onTaskBound(Task t) { 646 mTask = t; 647 mTask.setCallbacks(this); 648 649 // Hide the action button if lock to app is disabled for this view 650 int lockButtonVisibility = (!t.lockToTaskEnabled || !t.lockToThisTask) ? GONE : VISIBLE; 651 if (mActionButtonView.getVisibility() != lockButtonVisibility) { 652 mActionButtonView.setVisibility(lockButtonVisibility); 653 requestLayout(); 654 } 655 } 656 657 @Override 658 public void onTaskDataLoaded() { 659 if (mThumbnailView != null && mHeaderView != null) { 660 // Bind each of the views to the new task data 661 mThumbnailView.rebindToTask(mTask); 662 mHeaderView.rebindToTask(mTask); 663 // Rebind any listeners 664 mHeaderView.mApplicationIcon.setOnClickListener(this); 665 mHeaderView.mDismissButton.setOnClickListener(this); 666 mActionButtonView.setOnClickListener(this); 667 if (Constants.DebugFlags.App.EnableDevAppInfoOnLongPress) { 668 if (mConfig.developerOptionsEnabled) { 669 mHeaderView.mApplicationIcon.setOnLongClickListener(this); 670 } 671 } 672 } 673 mTaskDataLoaded = true; 674 } 675 676 @Override 677 public void onTaskDataUnloaded() { 678 if (mThumbnailView != null && mHeaderView != null) { 679 // Unbind each of the views from the task data and remove the task callback 680 mTask.setCallbacks(null); 681 mThumbnailView.unbindFromTask(); 682 mHeaderView.unbindFromTask(); 683 // Unbind any listeners 684 mHeaderView.mApplicationIcon.setOnClickListener(null); 685 mHeaderView.mDismissButton.setOnClickListener(null); 686 mActionButtonView.setOnClickListener(null); 687 if (Constants.DebugFlags.App.EnableDevAppInfoOnLongPress) { 688 mHeaderView.mApplicationIcon.setOnLongClickListener(null); 689 } 690 } 691 mTaskDataLoaded = false; 692 } 693 694 /** Enables/disables handling touch on this task view. */ 695 void setTouchEnabled(boolean enabled) { 696 setOnClickListener(enabled ? this : null); 697 } 698 699 /**** View.OnClickListener Implementation ****/ 700 701 @Override 702 public void onClick(final View v) { 703 final TaskView tv = this; 704 final boolean delayViewClick = (v != this) && (v != mActionButtonView); 705 if (delayViewClick) { 706 // We purposely post the handler delayed to allow for the touch feedback to draw 707 postDelayed(new Runnable() { 708 @Override 709 public void run() { 710 if (Constants.DebugFlags.App.EnableTaskFiltering && v == mHeaderView.mApplicationIcon) { 711 if (mCb != null) { 712 mCb.onTaskViewAppIconClicked(tv); 713 } 714 } else if (v == mHeaderView.mDismissButton) { 715 dismissTask(); 716 } 717 } 718 }, 125); 719 } else { 720 if (v == mActionButtonView) { 721 // Reset the translation of the action button before we animate it out 722 mActionButtonView.setTranslationZ(0f); 723 } 724 if (mCb != null) { 725 mCb.onTaskViewClicked(tv, tv.getTask(), (v == mActionButtonView)); 726 } 727 } 728 } 729 730 /**** View.OnLongClickListener Implementation ****/ 731 732 @Override 733 public boolean onLongClick(View v) { 734 if (v == mHeaderView.mApplicationIcon) { 735 if (mCb != null) { 736 mCb.onTaskViewAppInfoClicked(this); 737 return true; 738 } 739 } 740 return false; 741 } 742} 743