TaskStackView.java revision e41ab847a5c12cb7a6a5b2d0f40e101bc9ea6b59
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.content.ComponentName; 20import android.content.Context; 21import android.graphics.Rect; 22import android.view.LayoutInflater; 23import android.view.MotionEvent; 24import android.view.View; 25import android.widget.FrameLayout; 26import com.android.systemui.R; 27import com.android.systemui.recents.Constants; 28import com.android.systemui.recents.RecentsConfiguration; 29import com.android.systemui.recents.misc.DozeTrigger; 30import com.android.systemui.recents.model.RecentsPackageMonitor; 31import com.android.systemui.recents.model.RecentsTaskLoader; 32import com.android.systemui.recents.model.Task; 33import com.android.systemui.recents.model.TaskStack; 34 35import java.util.ArrayList; 36import java.util.HashMap; 37import java.util.HashSet; 38 39 40/* The visual representation of a task stack view */ 41public class TaskStackView extends FrameLayout implements TaskStack.TaskStackCallbacks, 42 TaskView.TaskViewCallbacks, TaskStackViewScroller.TaskStackViewScrollerCallbacks, 43 ViewPool.ViewPoolConsumer<TaskView, Task>, RecentsPackageMonitor.PackageCallbacks { 44 45 /** The TaskView callbacks */ 46 interface TaskStackViewCallbacks { 47 public void onTaskViewClicked(TaskStackView stackView, TaskView tv, TaskStack stack, Task t, 48 boolean lockToTask); 49 public void onTaskViewAppInfoClicked(Task t); 50 public void onTaskViewDismissed(Task t); 51 public void onAllTaskViewsDismissed(); 52 public void onTaskStackFilterTriggered(); 53 public void onTaskStackUnfilterTriggered(); 54 } 55 56 RecentsConfiguration mConfig; 57 58 TaskStack mStack; 59 TaskStackViewLayoutAlgorithm mLayoutAlgorithm; 60 TaskStackViewFilterAlgorithm mFilterAlgorithm; 61 TaskStackViewScroller mStackScroller; 62 TaskStackViewTouchHandler mTouchHandler; 63 TaskStackViewCallbacks mCb; 64 ViewPool<TaskView, Task> mViewPool; 65 ArrayList<TaskViewTransform> mCurrentTaskTransforms = new ArrayList<TaskViewTransform>(); 66 DozeTrigger mUIDozeTrigger; 67 DebugOverlayView mDebugOverlay; 68 Rect mTaskStackBounds = new Rect(); 69 int mFocusedTaskIndex = -1; 70 71 // Optimizations 72 int mStackViewsAnimationDuration; 73 boolean mStackViewsDirty = true; 74 boolean mAwaitingFirstLayout = true; 75 boolean mStartEnterAnimationRequestedAfterLayout; 76 ViewAnimation.TaskViewEnterContext mStartEnterAnimationContext; 77 int[] mTmpVisibleRange = new int[2]; 78 TaskViewTransform mTmpTransform = new TaskViewTransform(); 79 HashMap<Task, TaskView> mTmpTaskViewMap = new HashMap<Task, TaskView>(); 80 LayoutInflater mInflater; 81 82 // A convenience runnable to return all views to the pool 83 Runnable mReturnAllViewsToPoolRunnable = new Runnable() { 84 @Override 85 public void run() { 86 int childCount = getChildCount(); 87 for (int i = childCount - 1; i >= 0; i--) { 88 TaskView tv = (TaskView) getChildAt(i); 89 mViewPool.returnViewToPool(tv); 90 // Also hide the view since we don't need it anymore 91 tv.setVisibility(View.INVISIBLE); 92 } 93 } 94 }; 95 96 public TaskStackView(Context context, TaskStack stack) { 97 super(context); 98 mConfig = RecentsConfiguration.getInstance(); 99 mStack = stack; 100 mStack.setCallbacks(this); 101 mViewPool = new ViewPool<TaskView, Task>(context, this); 102 mInflater = LayoutInflater.from(context); 103 mLayoutAlgorithm = new TaskStackViewLayoutAlgorithm(mConfig); 104 mFilterAlgorithm = new TaskStackViewFilterAlgorithm(mConfig, this, mViewPool); 105 mStackScroller = new TaskStackViewScroller(context, mConfig, mLayoutAlgorithm); 106 mStackScroller.setCallbacks(this); 107 mTouchHandler = new TaskStackViewTouchHandler(context, this, mStackScroller); 108 mUIDozeTrigger = new DozeTrigger(mConfig.taskBarDismissDozeDelaySeconds, new Runnable() { 109 @Override 110 public void run() { 111 // Show the task bar dismiss buttons 112 int childCount = getChildCount(); 113 for (int i = 0; i < childCount; i++) { 114 TaskView tv = (TaskView) getChildAt(i); 115 tv.startNoUserInteractionAnimation(); 116 } 117 } 118 }); 119 } 120 121 /** Sets the callbacks */ 122 void setCallbacks(TaskStackViewCallbacks cb) { 123 mCb = cb; 124 } 125 126 /** Sets the debug overlay */ 127 public void setDebugOverlay(DebugOverlayView overlay) { 128 mDebugOverlay = overlay; 129 } 130 131 /** Requests that the views be synchronized with the model */ 132 void requestSynchronizeStackViewsWithModel() { 133 requestSynchronizeStackViewsWithModel(0); 134 } 135 void requestSynchronizeStackViewsWithModel(int duration) { 136 if (!mStackViewsDirty) { 137 invalidate(); 138 mStackViewsDirty = true; 139 } 140 if (mAwaitingFirstLayout) { 141 // Skip the animation if we are awaiting first layout 142 mStackViewsAnimationDuration = 0; 143 } else { 144 mStackViewsAnimationDuration = Math.max(mStackViewsAnimationDuration, duration); 145 } 146 } 147 148 /** Finds the child view given a specific task. */ 149 public TaskView getChildViewForTask(Task t) { 150 int childCount = getChildCount(); 151 for (int i = 0; i < childCount; i++) { 152 TaskView tv = (TaskView) getChildAt(i); 153 if (tv.getTask() == t) { 154 return tv; 155 } 156 } 157 return null; 158 } 159 160 /** Returns the stack algorithm for this task stack. */ 161 public TaskStackViewLayoutAlgorithm getStackAlgorithm() { 162 return mLayoutAlgorithm; 163 } 164 165 /** 166 * Gets the stack transforms of a list of tasks, and returns the visible range of tasks. 167 */ 168 private boolean updateStackTransforms(ArrayList<TaskViewTransform> taskTransforms, 169 ArrayList<Task> tasks, 170 float stackScroll, 171 int[] visibleRangeOut, 172 boolean boundTranslationsToRect) { 173 // XXX: We should be intelligent about where to look for the visible stack range using the 174 // current stack scroll. 175 // XXX: We should log extra cases like the ones below where we don't expect to hit very often 176 // XXX: Print out approximately how many indices we have to go through to find the first visible transform 177 178 int taskTransformCount = taskTransforms.size(); 179 int taskCount = tasks.size(); 180 int frontMostVisibleIndex = -1; 181 int backMostVisibleIndex = -1; 182 183 // We can reuse the task transforms where possible to reduce object allocation 184 if (taskTransformCount < taskCount) { 185 // If there are less transforms than tasks, then add as many transforms as necessary 186 for (int i = taskTransformCount; i < taskCount; i++) { 187 taskTransforms.add(new TaskViewTransform()); 188 } 189 } else if (taskTransformCount > taskCount) { 190 // If there are more transforms than tasks, then just subset the transform list 191 taskTransforms.subList(0, taskCount); 192 } 193 194 // Update the stack transforms 195 TaskViewTransform prevTransform = null; 196 for (int i = taskCount - 1; i >= 0; i--) { 197 TaskViewTransform transform = mLayoutAlgorithm.getStackTransform(tasks.get(i), 198 stackScroll, taskTransforms.get(i), prevTransform); 199 if (transform.visible) { 200 if (frontMostVisibleIndex < 0) { 201 frontMostVisibleIndex = i; 202 } 203 backMostVisibleIndex = i; 204 } else { 205 if (backMostVisibleIndex != -1) { 206 // We've reached the end of the visible range, so going down the rest of the 207 // stack, we can just reset the transforms accordingly 208 while (i >= 0) { 209 taskTransforms.get(i).reset(); 210 i--; 211 } 212 break; 213 } 214 } 215 216 if (boundTranslationsToRect) { 217 transform.translationY = Math.min(transform.translationY, 218 mLayoutAlgorithm.mViewRect.bottom); 219 } 220 prevTransform = transform; 221 } 222 if (visibleRangeOut != null) { 223 visibleRangeOut[0] = frontMostVisibleIndex; 224 visibleRangeOut[1] = backMostVisibleIndex; 225 } 226 return frontMostVisibleIndex != -1 && backMostVisibleIndex != -1; 227 } 228 229 /** 230 * Gets the stack transforms of a list of tasks, and returns the visible range of tasks. This 231 * call is less optimal than calling updateStackTransforms directly. 232 */ 233 private ArrayList<TaskViewTransform> getStackTransforms(ArrayList<Task> tasks, 234 float stackScroll, 235 int[] visibleRangeOut, 236 boolean boundTranslationsToRect) { 237 ArrayList<TaskViewTransform> taskTransforms = new ArrayList<TaskViewTransform>(); 238 updateStackTransforms(taskTransforms, tasks, stackScroll, visibleRangeOut, 239 boundTranslationsToRect); 240 return taskTransforms; 241 } 242 243 /** Synchronizes the views with the model */ 244 boolean synchronizeStackViewsWithModel() { 245 if (mStackViewsDirty) { 246 // Get all the task transforms 247 ArrayList<Task> tasks = mStack.getTasks(); 248 float stackScroll = mStackScroller.getStackScroll(); 249 int[] visibleRange = mTmpVisibleRange; 250 boolean isValidVisibleRange = updateStackTransforms(mCurrentTaskTransforms, tasks, 251 stackScroll, visibleRange, false); 252 if (mDebugOverlay != null) { 253 mDebugOverlay.setText("vis[" + visibleRange[1] + "-" + visibleRange[0] + "]"); 254 } 255 256 // Return all the invisible children to the pool 257 mTmpTaskViewMap.clear(); 258 int childCount = getChildCount(); 259 for (int i = childCount - 1; i >= 0; i--) { 260 TaskView tv = (TaskView) getChildAt(i); 261 Task task = tv.getTask(); 262 int taskIndex = mStack.indexOfTask(task); 263 if (visibleRange[1] <= taskIndex && taskIndex <= visibleRange[0]) { 264 mTmpTaskViewMap.put(task, tv); 265 } else { 266 mViewPool.returnViewToPool(tv); 267 } 268 } 269 270 // Pick up all the newly visible children and update all the existing children 271 for (int i = visibleRange[0]; isValidVisibleRange && i >= visibleRange[1]; i--) { 272 Task task = tasks.get(i); 273 TaskViewTransform transform = mCurrentTaskTransforms.get(i); 274 TaskView tv = mTmpTaskViewMap.get(task); 275 int taskIndex = mStack.indexOfTask(task); 276 277 if (tv == null) { 278 tv = mViewPool.pickUpViewFromPool(task, task); 279 280 if (mStackViewsAnimationDuration > 0) { 281 // For items in the list, put them in start animating them from the 282 // approriate ends of the list where they are expected to appear 283 if (Float.compare(transform.p, 0f) <= 0) { 284 mLayoutAlgorithm.getStackTransform(0f, 0f, mTmpTransform, null); 285 } else { 286 mLayoutAlgorithm.getStackTransform(1f, 0f, mTmpTransform, null); 287 } 288 tv.updateViewPropertiesToTaskTransform(mTmpTransform, 0); 289 } 290 } 291 292 // Animate the task into place 293 tv.updateViewPropertiesToTaskTransform(mCurrentTaskTransforms.get(taskIndex), 294 mStackViewsAnimationDuration); 295 } 296 297 // Reset the request-synchronize params 298 mStackViewsAnimationDuration = 0; 299 mStackViewsDirty = false; 300 return true; 301 } 302 return false; 303 } 304 305 /** Updates the clip for each of the task views. */ 306 void clipTaskViews() { 307 // Update the clip on each task child 308 if (Constants.DebugFlags.App.EnableTaskStackClipping) { 309 int childCount = getChildCount(); 310 for (int i = 0; i < childCount - 1; i++) { 311 TaskView tv = (TaskView) getChildAt(i); 312 TaskView nextTv = null; 313 TaskView tmpTv = null; 314 int clipBottom = 0; 315 if (tv.shouldClipViewInStack()) { 316 // Find the next view to clip against 317 int nextIndex = i; 318 while (nextIndex < getChildCount()) { 319 tmpTv = (TaskView) getChildAt(++nextIndex); 320 if (tmpTv != null && tmpTv.shouldClipViewInStack()) { 321 nextTv = tmpTv; 322 break; 323 } 324 } 325 326 // Clip against the next view, this is just an approximation since we are 327 // stacked and we can make assumptions about the visibility of the this 328 // task relative to the ones in front of it. 329 if (nextTv != null) { 330 // We can reuse the current task transforms to find the task rects 331 TaskViewTransform transform = mCurrentTaskTransforms.get(mStack.indexOfTask(tv.getTask())); 332 TaskViewTransform nextTransform = mCurrentTaskTransforms.get(mStack.indexOfTask(nextTv.getTask())); 333 clipBottom = transform.rect.bottom - nextTransform.rect.top; 334 } 335 } 336 tv.getViewBounds().setClipBottom(clipBottom); 337 } 338 if (getChildCount() > 0) { 339 // The front most task should never be clipped 340 TaskView tv = (TaskView) getChildAt(getChildCount() - 1); 341 tv.getViewBounds().setClipBottom(0); 342 } 343 } 344 } 345 346 /** The stack insets to apply to the stack contents */ 347 public void setStackInsetRect(Rect r) { 348 mTaskStackBounds.set(r); 349 } 350 351 /** Updates the min and max virtual scroll bounds */ 352 void updateMinMaxScroll(boolean boundScrollToNewMinMax, boolean launchedWithAltTab) { 353 // Compute the min and max scroll values 354 mLayoutAlgorithm.computeMinMaxScroll(mStack.getTasks(), launchedWithAltTab); 355 356 // Debug logging 357 if (boundScrollToNewMinMax) { 358 mStackScroller.boundScroll(); 359 } 360 } 361 362 /** Returns the scroller. */ 363 public TaskStackViewScroller getScroller() { 364 return mStackScroller; 365 } 366 367 /** Focuses the task at the specified index in the stack */ 368 void focusTask(int taskIndex, boolean scrollToNewPosition) { 369 if (0 <= taskIndex && taskIndex < mStack.getTaskCount()) { 370 mFocusedTaskIndex = taskIndex; 371 372 // Focus the view if possible, otherwise, focus the view after we scroll into position 373 Task t = mStack.getTasks().get(taskIndex); 374 TaskView tv = getChildViewForTask(t); 375 Runnable postScrollRunnable = null; 376 if (tv != null) { 377 tv.setFocusedTask(); 378 } else { 379 postScrollRunnable = new Runnable() { 380 @Override 381 public void run() { 382 Task t = mStack.getTasks().get(mFocusedTaskIndex); 383 TaskView tv = getChildViewForTask(t); 384 if (tv != null) { 385 tv.setFocusedTask(); 386 } 387 } 388 }; 389 } 390 391 // Scroll the view into position (just center it in the curve) 392 if (scrollToNewPosition) { 393 float newScroll = mLayoutAlgorithm.getStackScrollForTaskIndex(t) - 0.5f; 394 newScroll = mStackScroller.getBoundedStackScroll(newScroll); 395 mStackScroller.animateScroll(mStackScroller.getStackScroll(), newScroll, postScrollRunnable); 396 } else { 397 if (postScrollRunnable != null) { 398 postScrollRunnable.run(); 399 } 400 } 401 402 } 403 } 404 405 /** Focuses the next task in the stack */ 406 void focusNextTask(boolean forward) { 407 // Find the next index to focus 408 int numTasks = mStack.getTaskCount(); 409 if (mFocusedTaskIndex < 0) { 410 mFocusedTaskIndex = numTasks - 1; 411 } 412 if (0 <= mFocusedTaskIndex && mFocusedTaskIndex < numTasks) { 413 mFocusedTaskIndex = Math.max(0, Math.min(numTasks - 1, 414 mFocusedTaskIndex + (forward ? -1 : 1))); 415 } 416 focusTask(mFocusedTaskIndex, true); 417 } 418 419 @Override 420 public boolean onInterceptTouchEvent(MotionEvent ev) { 421 return mTouchHandler.onInterceptTouchEvent(ev); 422 } 423 424 @Override 425 public boolean onTouchEvent(MotionEvent ev) { 426 return mTouchHandler.onTouchEvent(ev); 427 } 428 429 @Override 430 public void computeScroll() { 431 mStackScroller.computeScroll(); 432 // Synchronize the views 433 synchronizeStackViewsWithModel(); 434 clipTaskViews(); 435 } 436 437 /** Computes the stack and task rects */ 438 public void computeRects(int windowWidth, int windowHeight, Rect taskStackBounds, 439 boolean launchedWithAltTab) { 440 // Compute the rects in the stack algorithm 441 mLayoutAlgorithm.computeRects(windowWidth, windowHeight, taskStackBounds); 442 443 // Update the scroll bounds 444 updateMinMaxScroll(false, launchedWithAltTab); 445 } 446 447 /** 448 * This is called with the full window width and height to allow stack view children to 449 * perform the full screen transition down. 450 */ 451 @Override 452 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 453 int width = MeasureSpec.getSize(widthMeasureSpec); 454 int height = MeasureSpec.getSize(heightMeasureSpec); 455 456 // Compute our stack/task rects 457 Rect taskStackBounds = new Rect(mTaskStackBounds); 458 taskStackBounds.bottom -= mConfig.systemInsets.bottom; 459 computeRects(width, height, taskStackBounds, mConfig.launchedWithAltTab); 460 461 // If this is the first layout, then scroll to the front of the stack and synchronize the 462 // stack views immediately to load all the views 463 if (mAwaitingFirstLayout) { 464 mStackScroller.setStackScrollToInitialState(); 465 requestSynchronizeStackViewsWithModel(); 466 synchronizeStackViewsWithModel(); 467 } 468 469 // Measure each of the TaskViews 470 int childCount = getChildCount(); 471 for (int i = 0; i < childCount; i++) { 472 TaskView tv = (TaskView) getChildAt(i); 473 if (tv.isFullScreenView()) { 474 tv.measure(widthMeasureSpec, heightMeasureSpec); 475 } else { 476 tv.measure( 477 MeasureSpec.makeMeasureSpec(mLayoutAlgorithm.mTaskRect.width(), 478 MeasureSpec.EXACTLY), 479 MeasureSpec.makeMeasureSpec(mLayoutAlgorithm.mTaskRect.height() + 480 tv.getMaxFooterHeight(), MeasureSpec.EXACTLY)); 481 } 482 } 483 484 setMeasuredDimension(width, height); 485 } 486 487 /** 488 * This is called with the size of the space not including the top or right insets, or the 489 * search bar height in portrait (but including the search bar width in landscape, since we want 490 * to draw under it. 491 */ 492 @Override 493 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 494 // Layout each of the children 495 int childCount = getChildCount(); 496 for (int i = 0; i < childCount; i++) { 497 TaskView tv = (TaskView) getChildAt(i); 498 if (tv.isFullScreenView()) { 499 tv.layout(left, top, left + tv.getMeasuredWidth(), top + tv.getMeasuredHeight()); 500 } else { 501 tv.layout(mLayoutAlgorithm.mTaskRect.left, mLayoutAlgorithm.mTaskRect.top, 502 mLayoutAlgorithm.mTaskRect.right, mLayoutAlgorithm.mTaskRect.bottom + 503 tv.getMaxFooterHeight()); 504 } 505 } 506 507 if (mAwaitingFirstLayout) { 508 mAwaitingFirstLayout = false; 509 onFirstLayout(); 510 } 511 } 512 513 /** Handler for the first layout. */ 514 void onFirstLayout() { 515 int offscreenY = mLayoutAlgorithm.mViewRect.bottom - 516 (mLayoutAlgorithm.mTaskRect.top - mLayoutAlgorithm.mViewRect.top); 517 518 // Find the launch target task 519 Task launchTargetTask = null; 520 int childCount = getChildCount(); 521 for (int i = childCount - 1; i >= 0; i--) { 522 TaskView tv = (TaskView) getChildAt(i); 523 Task task = tv.getTask(); 524 if (task.isLaunchTarget) { 525 launchTargetTask = task; 526 break; 527 } 528 } 529 530 // Prepare the first view for its enter animation 531 for (int i = childCount - 1; i >= 0; i--) { 532 TaskView tv = (TaskView) getChildAt(i); 533 Task task = tv.getTask(); 534 boolean occludesLaunchTarget = (launchTargetTask != null) && 535 launchTargetTask.group.isTaskAboveTask(task, launchTargetTask); 536 tv.prepareEnterRecentsAnimation(task.isLaunchTarget, occludesLaunchTarget, offscreenY); 537 } 538 539 // If the enter animation started already and we haven't completed a layout yet, do the 540 // enter animation now 541 if (mStartEnterAnimationRequestedAfterLayout) { 542 startEnterRecentsAnimation(mStartEnterAnimationContext); 543 mStartEnterAnimationRequestedAfterLayout = false; 544 mStartEnterAnimationContext = null; 545 } 546 547 // When Alt-Tabbing, we scroll to and focus the previous task 548 if (mConfig.launchedWithAltTab) { 549 focusTask(Math.max(0, mStack.getTaskCount() - 2), false); 550 } 551 } 552 553 /** Requests this task stacks to start it's enter-recents animation */ 554 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) { 555 // If we are still waiting to layout, then just defer until then 556 if (mAwaitingFirstLayout) { 557 mStartEnterAnimationRequestedAfterLayout = true; 558 mStartEnterAnimationContext = ctx; 559 return; 560 } 561 562 if (mStack.getTaskCount() > 0) { 563 // Find the launch target task 564 Task launchTargetTask = null; 565 int childCount = getChildCount(); 566 for (int i = childCount - 1; i >= 0; i--) { 567 TaskView tv = (TaskView) getChildAt(i); 568 Task task = tv.getTask(); 569 if (task.isLaunchTarget) { 570 launchTargetTask = task; 571 break; 572 } 573 } 574 575 // Animate all the task views into view 576 for (int i = childCount - 1; i >= 0; i--) { 577 TaskView tv = (TaskView) getChildAt(i); 578 Task task = tv.getTask(); 579 ctx.currentTaskTransform = new TaskViewTransform(); 580 ctx.currentStackViewIndex = i; 581 ctx.currentStackViewCount = childCount; 582 ctx.currentTaskRect = mLayoutAlgorithm.mTaskRect; 583 ctx.currentTaskOccludesLaunchTarget = (launchTargetTask != null) && 584 launchTargetTask.group.isTaskAboveTask(task, launchTargetTask); 585 mLayoutAlgorithm.getStackTransform(task, mStackScroller.getStackScroll(), ctx.currentTaskTransform, null); 586 tv.startEnterRecentsAnimation(ctx); 587 } 588 589 // Add a runnable to the post animation ref counter to clear all the views 590 ctx.postAnimationTrigger.addLastDecrementRunnable(new Runnable() { 591 @Override 592 public void run() { 593 // Start dozing 594 mUIDozeTrigger.startDozing(); 595 } 596 }); 597 } 598 } 599 600 /** Requests this task stacks to start it's exit-recents animation. */ 601 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) { 602 // Stop any scrolling 603 mStackScroller.stopScroller(); 604 mStackScroller.stopBoundScrollAnimation(); 605 // Animate all the task views out of view 606 ctx.offscreenTranslationY = mLayoutAlgorithm.mViewRect.bottom - 607 (mLayoutAlgorithm.mTaskRect.top - mLayoutAlgorithm.mViewRect.top); 608 int childCount = getChildCount(); 609 for (int i = 0; i < childCount; i++) { 610 TaskView tv = (TaskView) getChildAt(i); 611 tv.startExitToHomeAnimation(ctx); 612 } 613 614 // Add a runnable to the post animation ref counter to clear all the views 615 ctx.postAnimationTrigger.addLastDecrementRunnable(mReturnAllViewsToPoolRunnable); 616 } 617 618 /** Animates a task view in this stack as it launches. */ 619 public void startLaunchTaskAnimation(TaskView tv, final Runnable r) { 620 Task launchTargetTask = tv.getTask(); 621 int childCount = getChildCount(); 622 for (int i = 0; i < childCount; i++) { 623 TaskView t = (TaskView) getChildAt(i); 624 if (t == tv) { 625 t.startLaunchTaskAnimation(r, true, true); 626 } else { 627 boolean occludesLaunchTarget = launchTargetTask.group.isTaskAboveTask(t.getTask(), 628 launchTargetTask); 629 t.startLaunchTaskAnimation(null, false, occludesLaunchTarget); 630 } 631 } 632 } 633 634 public boolean isTransformedTouchPointInView(float x, float y, View child) { 635 return isTransformedTouchPointInView(x, y, child, null); 636 } 637 638 /** Pokes the dozer on user interaction. */ 639 void onUserInteraction() { 640 // Poke the doze trigger if it is dozing 641 mUIDozeTrigger.poke(); 642 } 643 644 /**** TaskStackCallbacks Implementation ****/ 645 646 @Override 647 public void onStackTaskAdded(TaskStack stack, Task t) { 648 requestSynchronizeStackViewsWithModel(); 649 } 650 651 @Override 652 public void onStackTaskRemoved(TaskStack stack, Task removedTask, Task newFrontMostTask) { 653 // Remove the view associated with this task, we can't rely on updateTransforms 654 // to work here because the task is no longer in the list 655 TaskView tv = getChildViewForTask(removedTask); 656 if (tv != null) { 657 mViewPool.returnViewToPool(tv); 658 } 659 660 // Notify the callback that we've removed the task and it can clean up after it 661 mCb.onTaskViewDismissed(removedTask); 662 663 // Update the min/max scroll and animate other task views into their new positions 664 updateMinMaxScroll(true, mConfig.launchedWithAltTab); 665 requestSynchronizeStackViewsWithModel(200); 666 667 // Update the new front most task 668 if (newFrontMostTask != null) { 669 TaskView frontTv = getChildViewForTask(newFrontMostTask); 670 if (frontTv != null) { 671 frontTv.onTaskBound(newFrontMostTask); 672 } 673 } 674 675 // If there are no remaining tasks, then either unfilter the current stack, or just close 676 // the activity if there are no filtered stacks 677 if (mStack.getTaskCount() == 0) { 678 boolean shouldFinishActivity = true; 679 if (mStack.hasFilteredTasks()) { 680 mStack.unfilterTasks(); 681 shouldFinishActivity = (mStack.getTaskCount() == 0); 682 } 683 if (shouldFinishActivity) { 684 mCb.onAllTaskViewsDismissed(); 685 } 686 } 687 } 688 689 @Override 690 public void onStackFiltered(TaskStack newStack, final ArrayList<Task> curTasks, 691 Task filteredTask) { 692 /* 693 // Stash the scroll and filtered task for us to restore to when we unfilter 694 mStashedScroll = getStackScroll(); 695 696 // Calculate the current task transforms 697 ArrayList<TaskViewTransform> curTaskTransforms = 698 getStackTransforms(curTasks, getStackScroll(), null, true); 699 700 // Update the task offsets 701 mLayoutAlgorithm.updateTaskOffsets(mStack.getTasks()); 702 703 // Scroll the item to the top of the stack (sans-peek) rect so that we can see it better 704 updateMinMaxScroll(false); 705 float overlapHeight = mLayoutAlgorithm.getTaskOverlapHeight(); 706 setStackScrollRaw((int) (newStack.indexOfTask(filteredTask) * overlapHeight)); 707 boundScrollRaw(); 708 709 // Compute the transforms of the items in the new stack after setting the new scroll 710 final ArrayList<Task> tasks = mStack.getTasks(); 711 final ArrayList<TaskViewTransform> taskTransforms = 712 getStackTransforms(mStack.getTasks(), getStackScroll(), null, true); 713 714 // Animate 715 mFilterAlgorithm.startFilteringAnimation(curTasks, curTaskTransforms, tasks, taskTransforms); 716 717 // Notify any callbacks 718 mCb.onTaskStackFilterTriggered(); 719 */ 720 } 721 722 @Override 723 public void onStackUnfiltered(TaskStack newStack, final ArrayList<Task> curTasks) { 724 /* 725 // Calculate the current task transforms 726 final ArrayList<TaskViewTransform> curTaskTransforms = 727 getStackTransforms(curTasks, getStackScroll(), null, true); 728 729 // Update the task offsets 730 mLayoutAlgorithm.updateTaskOffsets(mStack.getTasks()); 731 732 // Restore the stashed scroll 733 updateMinMaxScroll(false); 734 setStackScrollRaw(mStashedScroll); 735 boundScrollRaw(); 736 737 // Compute the transforms of the items in the new stack after restoring the stashed scroll 738 final ArrayList<Task> tasks = mStack.getTasks(); 739 final ArrayList<TaskViewTransform> taskTransforms = 740 getStackTransforms(tasks, getStackScroll(), null, true); 741 742 // Animate 743 mFilterAlgorithm.startFilteringAnimation(curTasks, curTaskTransforms, tasks, taskTransforms); 744 745 // Clear the saved vars 746 mStashedScroll = 0; 747 748 // Notify any callbacks 749 mCb.onTaskStackUnfilterTriggered(); 750 */ 751 } 752 753 /**** ViewPoolConsumer Implementation ****/ 754 755 @Override 756 public TaskView createView(Context context) { 757 return (TaskView) mInflater.inflate(R.layout.recents_task_view, this, false); 758 } 759 760 @Override 761 public void prepareViewToEnterPool(TaskView tv) { 762 Task task = tv.getTask(); 763 764 // Report that this tasks's data is no longer being used 765 RecentsTaskLoader.getInstance().unloadTaskData(task); 766 767 // Detach the view from the hierarchy 768 detachViewFromParent(tv); 769 770 // Reset the view properties 771 tv.resetViewProperties(); 772 } 773 774 @Override 775 public void prepareViewToLeavePool(TaskView tv, Task task, boolean isNewView) { 776 // Rebind the task and request that this task's data be filled into the TaskView 777 tv.onTaskBound(task); 778 779 // Mark the launch task as fullscreen 780 if (Constants.DebugFlags.App.EnableScreenshotAppTransition && mAwaitingFirstLayout) { 781 if (task.isLaunchTarget) { 782 tv.setIsFullScreen(true); 783 } 784 } 785 786 // Load the task data 787 RecentsTaskLoader.getInstance().loadTaskData(task); 788 789 // Sanity check, the task view should always be clipping against the stack at this point, 790 // but just in case, re-enable it here 791 tv.setClipViewInStack(true); 792 793 // If the doze trigger has already fired, then update the state for this task view 794 if (mUIDozeTrigger.hasTriggered()) { 795 tv.setNoUserInteractionState(); 796 } 797 798 // Find the index where this task should be placed in the stack 799 int insertIndex = -1; 800 int taskIndex = mStack.indexOfTask(task); 801 if (taskIndex != -1) { 802 int childCount = getChildCount(); 803 for (int i = 0; i < childCount; i++) { 804 Task tvTask = ((TaskView) getChildAt(i)).getTask(); 805 if (taskIndex < mStack.indexOfTask(tvTask)) { 806 insertIndex = i; 807 break; 808 } 809 } 810 } 811 812 // Add/attach the view to the hierarchy 813 if (isNewView) { 814 addView(tv, insertIndex); 815 816 // Set the callbacks and listeners for this new view 817 tv.setTouchEnabled(true); 818 tv.setCallbacks(this); 819 } else { 820 attachViewToParent(tv, insertIndex, tv.getLayoutParams()); 821 } 822 } 823 824 @Override 825 public boolean hasPreferredData(TaskView tv, Task preferredData) { 826 return (tv.getTask() == preferredData); 827 } 828 829 /**** TaskViewCallbacks Implementation ****/ 830 831 @Override 832 public void onTaskViewAppIconClicked(TaskView tv) { 833 if (Constants.DebugFlags.App.EnableTaskFiltering) { 834 if (mStack.hasFilteredTasks()) { 835 mStack.unfilterTasks(); 836 } else { 837 mStack.filterTasks(tv.getTask()); 838 } 839 } 840 } 841 842 @Override 843 public void onTaskViewAppInfoClicked(TaskView tv) { 844 if (mCb != null) { 845 mCb.onTaskViewAppInfoClicked(tv.getTask()); 846 } 847 } 848 849 @Override 850 public void onTaskViewClicked(TaskView tv, Task task, boolean lockToTask) { 851 // Cancel any doze triggers 852 mUIDozeTrigger.stopDozing(); 853 854 if (mCb != null) { 855 mCb.onTaskViewClicked(this, tv, mStack, task, lockToTask); 856 } 857 } 858 859 @Override 860 public void onTaskViewDismissed(TaskView tv) { 861 Task task = tv.getTask(); 862 // Announce for accessibility 863 tv.announceForAccessibility(getContext().getString(R.string.accessibility_recents_item_dismissed, 864 tv.getTask().activityLabel)); 865 // Remove the task from the view 866 mStack.removeTask(task); 867 } 868 869 @Override 870 public void onTaskViewClipStateChanged(TaskView tv) { 871 invalidate(); 872 } 873 874 @Override 875 public void onTaskViewFullScreenTransitionCompleted() { 876 requestSynchronizeStackViewsWithModel(); 877 } 878 879 /**** TaskStackViewScroller.TaskStackViewScrollerCallbacks ****/ 880 881 @Override 882 public void onScrollChanged(float p) { 883 mUIDozeTrigger.poke(); 884 requestSynchronizeStackViewsWithModel(); 885 postInvalidateOnAnimation(); 886 } 887 888 /**** RecentsPackageMonitor.PackageCallbacks Implementation ****/ 889 890 @Override 891 public void onComponentRemoved(HashSet<ComponentName> cns) { 892 // For other tasks, just remove them directly if they no longer exist 893 ArrayList<Task> tasks = mStack.getTasks(); 894 for (int i = tasks.size() - 1; i >= 0; i--) { 895 final Task t = tasks.get(i); 896 if (cns.contains(t.key.baseIntent.getComponent())) { 897 TaskView tv = getChildViewForTask(t); 898 if (tv != null) { 899 // For visible children, defer removing the task until after the animation 900 tv.startDeleteTaskAnimation(new Runnable() { 901 @Override 902 public void run() { 903 mStack.removeTask(t); 904 } 905 }); 906 } else { 907 // Otherwise, remove the task from the stack immediately 908 mStack.removeTask(t); 909 } 910 } 911 } 912 } 913} 914