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