RecentsView.java revision 2e7f3bdcc9ec0b3e95b565b943ecee2210f4b937
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.app.ActivityOptions; 20import android.app.TaskStackBuilder; 21import android.content.ActivityNotFoundException; 22import android.content.ComponentName; 23import android.content.Context; 24import android.content.Intent; 25import android.graphics.Bitmap; 26import android.graphics.Canvas; 27import android.graphics.Rect; 28import android.net.Uri; 29import android.os.UserHandle; 30import android.provider.Settings; 31import android.util.AttributeSet; 32import android.view.LayoutInflater; 33import android.view.View; 34import android.view.WindowInsets; 35import android.widget.FrameLayout; 36import com.android.systemui.recents.Constants; 37import com.android.systemui.recents.RecentsConfiguration; 38import com.android.systemui.recents.misc.Console; 39import com.android.systemui.recents.misc.SystemServicesProxy; 40import com.android.systemui.recents.misc.Utilities; 41import com.android.systemui.recents.model.RecentsPackageMonitor; 42import com.android.systemui.recents.model.RecentsTaskLoader; 43import com.android.systemui.recents.model.Task; 44import com.android.systemui.recents.model.TaskStack; 45 46import java.util.ArrayList; 47import java.util.HashSet; 48 49/** 50 * This view is the the top level layout that contains TaskStacks (which are laid out according 51 * to their SpaceNode bounds. 52 */ 53public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks, 54 RecentsPackageMonitor.PackageCallbacks { 55 56 /** The RecentsView callbacks */ 57 public interface RecentsViewCallbacks { 58 public void onTaskViewClicked(); 59 public void onAllTaskViewsDismissed(); 60 public void onExitToHomeAnimationTriggered(); 61 } 62 63 RecentsConfiguration mConfig; 64 LayoutInflater mInflater; 65 DebugOverlayView mDebugOverlay; 66 67 ArrayList<TaskStack> mStacks; 68 View mSearchBar; 69 RecentsViewCallbacks mCb; 70 boolean mAlreadyLaunchingTask; 71 72 public RecentsView(Context context) { 73 super(context); 74 } 75 76 public RecentsView(Context context, AttributeSet attrs) { 77 this(context, attrs, 0); 78 } 79 80 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) { 81 this(context, attrs, defStyleAttr, 0); 82 } 83 84 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 85 super(context, attrs, defStyleAttr, defStyleRes); 86 mConfig = RecentsConfiguration.getInstance(); 87 mInflater = LayoutInflater.from(context); 88 } 89 90 /** Sets the callbacks */ 91 public void setCallbacks(RecentsViewCallbacks cb) { 92 mCb = cb; 93 } 94 95 /** Sets the debug overlay */ 96 public void setDebugOverlay(DebugOverlayView overlay) { 97 mDebugOverlay = overlay; 98 } 99 100 /** Set/get the bsp root node */ 101 public void setTaskStacks(ArrayList<TaskStack> stacks) { 102 // Remove all TaskStackViews (but leave the search bar) 103 int childCount = getChildCount(); 104 for (int i = childCount - 1; i >= 0; i--) { 105 View v = getChildAt(i); 106 if (v != mSearchBar) { 107 removeViewAt(i); 108 } 109 } 110 111 // Create and add all the stacks for this partition of space. 112 mStacks = stacks; 113 int numStacks = mStacks.size(); 114 for (int i = 0; i < numStacks; i++) { 115 TaskStack stack = mStacks.get(i); 116 TaskStackView stackView = new TaskStackView(getContext(), stack); 117 stackView.setCallbacks(this); 118 // Enable debug mode drawing 119 if (mConfig.debugModeEnabled) { 120 stackView.setDebugOverlay(mDebugOverlay); 121 } 122 addView(stackView); 123 } 124 125 // Reset the launched state 126 mAlreadyLaunchingTask = false; 127 } 128 129 /** Removes all the task stack views from this recents view. */ 130 public void removeAllTaskStacks() { 131 int childCount = getChildCount(); 132 for (int i = childCount - 1; i >= 0; i--) { 133 View child = getChildAt(i); 134 if (child != mSearchBar) { 135 removeViewAt(i); 136 } 137 } 138 } 139 140 /** Launches the focused task from the first stack if possible */ 141 public boolean launchFocusedTask() { 142 // Get the first stack view 143 int childCount = getChildCount(); 144 for (int i = 0; i < childCount; i++) { 145 View child = getChildAt(i); 146 if (child != mSearchBar) { 147 TaskStackView stackView = (TaskStackView) child; 148 TaskStack stack = stackView.mStack; 149 // Iterate the stack views and try and find the focused task 150 int taskCount = stackView.getChildCount(); 151 for (int j = 0; j < taskCount; j++) { 152 TaskView tv = (TaskView) stackView.getChildAt(j); 153 Task task = tv.getTask(); 154 if (tv.isFocusedTask()) { 155 onTaskViewClicked(stackView, tv, stack, task, false); 156 return true; 157 } 158 } 159 } 160 } 161 return false; 162 } 163 164 /** Launches the task that Recents was launched from, if possible */ 165 public boolean launchPreviousTask() { 166 // Get the first stack view 167 int childCount = getChildCount(); 168 for (int i = 0; i < childCount; i++) { 169 View child = getChildAt(i); 170 if (child != mSearchBar) { 171 TaskStackView stackView = (TaskStackView) child; 172 TaskStack stack = stackView.mStack; 173 ArrayList<Task> tasks = stack.getTasks(); 174 175 // Find the launch task in the stack 176 if (!tasks.isEmpty()) { 177 int taskCount = tasks.size(); 178 for (int j = 0; j < taskCount; j++) { 179 if (tasks.get(j).isLaunchTarget) { 180 Task task = tasks.get(j); 181 TaskView tv = stackView.getChildViewForTask(task); 182 onTaskViewClicked(stackView, tv, stack, task, false); 183 return true; 184 } 185 } 186 } 187 } 188 } 189 return false; 190 } 191 192 /** Requests all task stacks to start their enter-recents animation */ 193 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) { 194 int childCount = getChildCount(); 195 for (int i = 0; i < childCount; i++) { 196 View child = getChildAt(i); 197 if (child != mSearchBar) { 198 TaskStackView stackView = (TaskStackView) child; 199 stackView.startEnterRecentsAnimation(ctx); 200 } 201 } 202 } 203 204 /** Requests all task stacks to start their exit-recents animation */ 205 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) { 206 int childCount = getChildCount(); 207 for (int i = 0; i < childCount; i++) { 208 View child = getChildAt(i); 209 if (child != mSearchBar) { 210 TaskStackView stackView = (TaskStackView) child; 211 stackView.startExitToHomeAnimation(ctx); 212 } 213 } 214 215 // Notify of the exit animation 216 mCb.onExitToHomeAnimationTriggered(); 217 } 218 219 /** Adds the search bar */ 220 public void setSearchBar(View searchBar) { 221 // Create the search bar (and hide it if we have no recent tasks) 222 if (Constants.DebugFlags.App.EnableSearchLayout) { 223 // Remove the previous search bar if one exists 224 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) { 225 removeView(mSearchBar); 226 } 227 // Add the new search bar 228 if (searchBar != null) { 229 mSearchBar = searchBar; 230 addView(mSearchBar); 231 } 232 } 233 } 234 235 /** Returns whether there is currently a search bar */ 236 public boolean hasSearchBar() { 237 return mSearchBar != null; 238 } 239 240 /** Sets the visibility of the search bar */ 241 public void setSearchBarVisibility(int visibility) { 242 if (mSearchBar != null) { 243 mSearchBar.setVisibility(visibility); 244 // Always bring the search bar to the top 245 mSearchBar.bringToFront(); 246 } 247 } 248 249 /** 250 * This is called with the full size of the window since we are handling our own insets. 251 */ 252 @Override 253 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 254 int width = MeasureSpec.getSize(widthMeasureSpec); 255 int height = MeasureSpec.getSize(heightMeasureSpec); 256 257 // Get the search bar bounds and measure the search bar layout 258 if (mSearchBar != null) { 259 Rect searchBarSpaceBounds = new Rect(); 260 mConfig.getSearchBarBounds(width, height, mConfig.systemInsets.top, searchBarSpaceBounds); 261 mSearchBar.measure( 262 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY), 263 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY)); 264 } 265 266 Rect taskStackBounds = new Rect(); 267 mConfig.getTaskStackBounds(width, height, mConfig.systemInsets.top, 268 mConfig.systemInsets.right, taskStackBounds); 269 270 // Measure each TaskStackView with the full width and height of the window since the 271 // transition view is a child of that stack view 272 int childCount = getChildCount(); 273 for (int i = 0; i < childCount; i++) { 274 View child = getChildAt(i); 275 if (child != mSearchBar && child.getVisibility() != GONE) { 276 TaskStackView tsv = (TaskStackView) child; 277 // Set the insets to be the top/left inset + search bounds 278 tsv.setStackInsetRect(taskStackBounds); 279 tsv.measure(widthMeasureSpec, heightMeasureSpec); 280 } 281 } 282 283 setMeasuredDimension(width, height); 284 } 285 286 /** 287 * This is called with the full size of the window since we are handling our own insets. 288 */ 289 @Override 290 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 291 // Get the search bar bounds so that we lay it out 292 if (mSearchBar != null) { 293 Rect searchBarSpaceBounds = new Rect(); 294 mConfig.getSearchBarBounds(getMeasuredWidth(), getMeasuredHeight(), 295 mConfig.systemInsets.top, searchBarSpaceBounds); 296 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top, 297 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom); 298 } 299 300 // Layout each TaskStackView with the full width and height of the window since the 301 // transition view is a child of that stack view 302 int childCount = getChildCount(); 303 for (int i = 0; i < childCount; i++) { 304 View child = getChildAt(i); 305 if (child != mSearchBar && child.getVisibility() != GONE) { 306 child.layout(left, top, left + child.getMeasuredWidth(), 307 top + child.getMeasuredHeight()); 308 } 309 } 310 } 311 312 @Override 313 public WindowInsets onApplyWindowInsets(WindowInsets insets) { 314 // Update the configuration with the latest system insets and trigger a relayout 315 mConfig.updateSystemInsets(insets.getSystemWindowInsets()); 316 requestLayout(); 317 return insets.consumeSystemWindowInsets(); 318 } 319 320 /** Notifies each task view of the user interaction. */ 321 public void onUserInteraction() { 322 // Get the first stack view 323 int childCount = getChildCount(); 324 for (int i = 0; i < childCount; i++) { 325 View child = getChildAt(i); 326 if (child != mSearchBar) { 327 TaskStackView stackView = (TaskStackView) child; 328 stackView.onUserInteraction(); 329 } 330 } 331 } 332 333 /** Focuses the next task in the first stack view */ 334 public void focusNextTask(boolean forward) { 335 // Get the first stack view 336 int childCount = getChildCount(); 337 for (int i = 0; i < childCount; i++) { 338 View child = getChildAt(i); 339 if (child != mSearchBar) { 340 TaskStackView stackView = (TaskStackView) child; 341 stackView.focusNextTask(forward); 342 break; 343 } 344 } 345 } 346 347 /** Dismisses the focused task. */ 348 public void dismissFocusedTask() { 349 // Get the first stack view 350 int childCount = getChildCount(); 351 for (int i = 0; i < childCount; i++) { 352 View child = getChildAt(i); 353 if (child != mSearchBar) { 354 TaskStackView stackView = (TaskStackView) child; 355 stackView.dismissFocusedTask(); 356 break; 357 } 358 } 359 } 360 361 /** Unfilters any filtered stacks */ 362 public boolean unfilterFilteredStacks() { 363 if (mStacks != null) { 364 // Check if there are any filtered stacks and unfilter them before we back out of Recents 365 boolean stacksUnfiltered = false; 366 int numStacks = mStacks.size(); 367 for (int i = 0; i < numStacks; i++) { 368 TaskStack stack = mStacks.get(i); 369 if (stack.hasFilteredTasks()) { 370 stack.unfilterTasks(); 371 stacksUnfiltered = true; 372 } 373 } 374 return stacksUnfiltered; 375 } 376 return false; 377 } 378 379 /**** TaskStackView.TaskStackCallbacks Implementation ****/ 380 381 @Override 382 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv, 383 final TaskStack stack, final Task task, final boolean lockToTask) { 384 // Notify any callbacks of the launching of a new task 385 if (mCb != null) { 386 mCb.onTaskViewClicked(); 387 } 388 // Skip if we are already launching tasks 389 if (mAlreadyLaunchingTask) { 390 return; 391 } 392 mAlreadyLaunchingTask = true; 393 394 // Upfront the processing of the thumbnail 395 TaskViewTransform transform = new TaskViewTransform(); 396 View sourceView = tv; 397 int offsetX = 0; 398 int offsetY = 0; 399 float stackScroll = stackView.getScroller().getStackScroll(); 400 if (tv == null) { 401 // If there is no actual task view, then use the stack view as the source view 402 // and then offset to the expected transform rect, but bound this to just 403 // outside the display rect (to ensure we don't animate from too far away) 404 sourceView = stackView; 405 transform = stackView.getStackAlgorithm().getStackTransform(task, stackScroll, transform, null); 406 offsetX = transform.rect.left; 407 offsetY = mConfig.displayRect.height(); 408 } else { 409 transform = stackView.getStackAlgorithm().getStackTransform(task, stackScroll, transform, null); 410 } 411 412 // Compute the thumbnail to scale up from 413 final SystemServicesProxy ssp = 414 RecentsTaskLoader.getInstance().getSystemServicesProxy(); 415 ActivityOptions opts = null; 416 if (task.thumbnail != null && task.thumbnail.getWidth() > 0 && 417 task.thumbnail.getHeight() > 0) { 418 Bitmap b; 419 if (tv != null) { 420 // Disable any focused state before we draw the header 421 if (tv.isFocusedTask()) { 422 tv.unsetFocusedTask(); 423 } 424 425 float scale = tv.getScaleX(); 426 int fromHeaderWidth = (int) (tv.mHeaderView.getMeasuredWidth() * scale); 427 int fromHeaderHeight = (int) (tv.mHeaderView.getMeasuredHeight() * scale); 428 b = Bitmap.createBitmap(fromHeaderWidth, fromHeaderHeight, 429 Bitmap.Config.ARGB_8888); 430 if (Constants.DebugFlags.App.EnableTransitionThumbnailDebugMode) { 431 b.eraseColor(0xFFff0000); 432 } else { 433 Canvas c = new Canvas(b); 434 c.scale(tv.getScaleX(), tv.getScaleY()); 435 tv.mHeaderView.draw(c); 436 c.setBitmap(null); 437 } 438 } else { 439 // Notify the system to skip the thumbnail layer by using an ALPHA_8 bitmap 440 b = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8); 441 } 442 ActivityOptions.OnAnimationStartedListener animStartedListener = null; 443 if (lockToTask) { 444 animStartedListener = new ActivityOptions.OnAnimationStartedListener() { 445 boolean mTriggered = false; 446 @Override 447 public void onAnimationStarted() { 448 if (!mTriggered) { 449 postDelayed(new Runnable() { 450 @Override 451 public void run() { 452 ssp.lockCurrentTask(); 453 } 454 }, 350); 455 mTriggered = true; 456 } 457 } 458 }; 459 } 460 opts = ActivityOptions.makeThumbnailAspectScaleUpAnimation(sourceView, 461 b, offsetX, offsetY, transform.rect.width(), transform.rect.height(), 462 animStartedListener); 463 } 464 465 final ActivityOptions launchOpts = opts; 466 final Runnable launchRunnable = new Runnable() { 467 @Override 468 public void run() { 469 if (task.isActive) { 470 // Bring an active task to the foreground 471 ssp.moveTaskToFront(task.key.id, launchOpts); 472 } else { 473 try { 474 ssp.startActivityFromRecents(task.key.id, launchOpts); 475 if (launchOpts == null && lockToTask) { 476 ssp.lockCurrentTask(); 477 } 478 } catch (ActivityNotFoundException anfe) { 479 Console.logError(getContext(), "Could not start Activity"); 480 } 481 } 482 } 483 }; 484 485 // Launch the app right away if there is no task view, otherwise, animate the icon out first 486 if (tv == null) { 487 post(launchRunnable); 488 } else { 489 if (!task.group.isFrontMostTask(task)) { 490 // For affiliated tasks that are behind other tasks, we must animate the front cards 491 // out of view before starting the task transition 492 stackView.startLaunchTaskAnimation(tv, launchRunnable, lockToTask); 493 } else { 494 // Otherwise, we can start the task transition immediately 495 stackView.startLaunchTaskAnimation(tv, null, lockToTask); 496 postDelayed(launchRunnable, 17); 497 } 498 } 499 } 500 501 @Override 502 public void onTaskViewAppInfoClicked(Task t) { 503 // Create a new task stack with the application info details activity 504 Intent baseIntent = t.key.baseIntent; 505 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, 506 Uri.fromParts("package", baseIntent.getComponent().getPackageName(), null)); 507 intent.setComponent(intent.resolveActivity(getContext().getPackageManager())); 508 TaskStackBuilder.create(getContext()) 509 .addNextIntentWithParentStack(intent).startActivities(null, 510 new UserHandle(t.key.userId)); 511 } 512 513 @Override 514 public void onTaskViewDismissed(Task t) { 515 // Remove any stored data from the loader. We currently don't bother notifying the views 516 // that the data has been unloaded because at the point we call onTaskViewDismissed(), the views 517 // either don't need to be updated, or have already been removed. 518 RecentsTaskLoader loader = RecentsTaskLoader.getInstance(); 519 loader.deleteTaskData(t, false); 520 521 // Remove the old task from activity manager 522 RecentsTaskLoader.getInstance().getSystemServicesProxy().removeTask(t.key.id, 523 Utilities.isDocument(t.key.baseIntent)); 524 } 525 526 @Override 527 public void onAllTaskViewsDismissed() { 528 mCb.onAllTaskViewsDismissed(); 529 } 530 531 @Override 532 public void onTaskStackFilterTriggered() { 533 // Hide the search bar 534 if (mSearchBar != null) { 535 mSearchBar.animate() 536 .alpha(0f) 537 .setStartDelay(0) 538 .setInterpolator(mConfig.fastOutSlowInInterpolator) 539 .setDuration(mConfig.filteringCurrentViewsAnimDuration) 540 .withLayer() 541 .start(); 542 } 543 } 544 545 @Override 546 public void onTaskStackUnfilterTriggered() { 547 // Show the search bar 548 if (mSearchBar != null) { 549 mSearchBar.animate() 550 .alpha(1f) 551 .setStartDelay(0) 552 .setInterpolator(mConfig.fastOutSlowInInterpolator) 553 .setDuration(mConfig.filteringNewViewsAnimDuration) 554 .withLayer() 555 .start(); 556 } 557 } 558 559 /**** RecentsPackageMonitor.PackageCallbacks Implementation ****/ 560 561 @Override 562 public void onComponentRemoved(HashSet<ComponentName> cns) { 563 // Propagate this event down to each task stack view 564 int childCount = getChildCount(); 565 for (int i = 0; i < childCount; i++) { 566 View child = getChildAt(i); 567 if (child != mSearchBar) { 568 TaskStackView stackView = (TaskStackView) child; 569 stackView.onComponentRemoved(cns); 570 } 571 } 572 } 573} 574