RecentsView.java revision 521e7dc0a6782371a710ee81bf804f7e97aaa292
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.systemui.recents.views; 18 19import android.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.view.LayoutInflater; 32import android.view.View; 33import android.view.WindowInsets; 34import android.widget.FrameLayout; 35import com.android.systemui.recents.Console; 36import com.android.systemui.recents.Constants; 37import com.android.systemui.recents.RecentsConfiguration; 38import com.android.systemui.recents.RecentsPackageMonitor; 39import com.android.systemui.recents.RecentsTaskLoader; 40import com.android.systemui.recents.model.SpaceNode; 41import com.android.systemui.recents.model.Task; 42import com.android.systemui.recents.model.TaskStack; 43 44import java.util.ArrayList; 45import java.util.Set; 46 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 onTaskLaunching(boolean isTaskInStackBounds); 58 public void onEnterAnimationTriggered(); 59 } 60 61 // The space partitioning root of this container 62 SpaceNode mBSP; 63 // Whether there are any tasks 64 boolean mHasTasks; 65 // Search bar view 66 View mSearchBar; 67 // Recents view callbacks 68 RecentsViewCallbacks mCb; 69 70 LayoutInflater mInflater; 71 72 public RecentsView(Context context) { 73 super(context); 74 mInflater = LayoutInflater.from(context); 75 setWillNotDraw(false); 76 } 77 78 /** Sets the callbacks */ 79 public void setCallbacks(RecentsViewCallbacks cb) { 80 mCb = cb; 81 } 82 83 /** Set/get the bsp root node */ 84 public void setBSP(SpaceNode n) { 85 mBSP = n; 86 87 // Create and add all the stacks for this partition of space. 88 mHasTasks = false; 89 removeAllViews(); 90 ArrayList<TaskStack> stacks = mBSP.getStacks(); 91 for (TaskStack stack : stacks) { 92 TaskStackView stackView = new TaskStackView(getContext(), stack); 93 stackView.setCallbacks(this); 94 addView(stackView); 95 mHasTasks |= (stack.getTaskCount() > 0); 96 } 97 } 98 99 /** Launches the focused task from the first stack if possible */ 100 public boolean launchFocusedTask() { 101 // Get the first stack view 102 int childCount = getChildCount(); 103 for (int i = 0; i < childCount; i++) { 104 View child = getChildAt(i); 105 if (child instanceof TaskStackView) { 106 TaskStackView stackView = (TaskStackView) child; 107 TaskStack stack = stackView.mStack; 108 // Iterate the stack views and try and find the focused task 109 int taskCount = stackView.getChildCount(); 110 for (int j = 0; j < taskCount; j++) { 111 TaskView tv = (TaskView) stackView.getChildAt(j); 112 Task task = tv.getTask(); 113 if (tv.isFocusedTask()) { 114 if (Console.Enabled) { 115 Console.log(Constants.Log.UI.Focus, "[RecentsView|launchFocusedTask]", 116 "Found focused Task"); 117 } 118 onTaskLaunched(stackView, tv, stack, task); 119 return true; 120 } 121 } 122 } 123 } 124 if (Console.Enabled) { 125 Console.log(Constants.Log.UI.Focus, "[RecentsView|launchFocusedTask]", 126 "No Tasks focused"); 127 } 128 return false; 129 } 130 131 /** Launches the first task from the first stack if possible */ 132 public boolean launchFirstTask() { 133 // Get the first stack view 134 int childCount = getChildCount(); 135 for (int i = 0; i < childCount; i++) { 136 View child = getChildAt(i); 137 if (child instanceof TaskStackView) { 138 TaskStackView stackView = (TaskStackView) child; 139 TaskStack stack = stackView.mStack; 140 ArrayList<Task> tasks = stack.getTasks(); 141 142 // Get the first task in the stack 143 if (!tasks.isEmpty()) { 144 Task task = tasks.get(tasks.size() - 1); 145 TaskView tv = null; 146 147 // Try and use the first child task view as the source of the launch animation 148 if (stackView.getChildCount() > 0) { 149 TaskView stv = (TaskView) stackView.getChildAt(stackView.getChildCount() - 1); 150 if (stv.getTask() == task) { 151 tv = stv; 152 } 153 } 154 onTaskLaunched(stackView, tv, stack, task); 155 return true; 156 } 157 } 158 } 159 return false; 160 } 161 162 /** Requests all task stacks to start their enter-recents animation */ 163 public void startOnEnterAnimation() { 164 // Notify callbacks that we are starting the enter animation 165 mCb.onEnterAnimationTriggered(); 166 167 int childCount = getChildCount(); 168 for (int i = 0; i < childCount; i++) { 169 View child = getChildAt(i); 170 if (child instanceof TaskStackView) { 171 TaskStackView stackView = (TaskStackView) child; 172 stackView.startOnEnterAnimation(); 173 } 174 } 175 } 176 177 /** Adds the search bar */ 178 public void setSearchBar(View searchBar) { 179 // Create the search bar (and hide it if we have no recent tasks) 180 if (Constants.DebugFlags.App.EnableSearchLayout) { 181 // Remove the previous search bar if one exists 182 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) { 183 removeView(mSearchBar); 184 } 185 // Add the new search bar 186 if (searchBar != null) { 187 mSearchBar = searchBar; 188 mSearchBar.setVisibility(mHasTasks ? View.VISIBLE : View.GONE); 189 addView(mSearchBar); 190 191 if (Console.Enabled) { 192 Console.log(Constants.Log.App.SystemUIHandshake, "[RecentsView|setSearchBar]", 193 "" + (mSearchBar.getVisibility() == View.VISIBLE), 194 Console.AnsiBlue); 195 } 196 } 197 } 198 } 199 200 /** 201 * This is called with the full size of the window since we are handling our own insets. 202 */ 203 @Override 204 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 205 int width = MeasureSpec.getSize(widthMeasureSpec); 206 int widthMode = MeasureSpec.getMode(widthMeasureSpec); 207 int height = MeasureSpec.getSize(heightMeasureSpec); 208 int heightMode = MeasureSpec.getMode(heightMeasureSpec); 209 210 if (Console.Enabled) { 211 Console.log(Constants.Log.UI.MeasureAndLayout, "[RecentsView|measure]", 212 "width: " + width + " height: " + height, Console.AnsiGreen); 213 Console.logTraceTime(Constants.Log.App.TimeRecentsStartup, 214 Constants.Log.App.TimeRecentsStartupKey, "RecentsView.onMeasure"); 215 } 216 217 // Get the search bar bounds and measure the search bar layout 218 RecentsConfiguration config = RecentsConfiguration.getInstance(); 219 if (mSearchBar != null) { 220 Rect searchBarSpaceBounds = new Rect(); 221 config.getSearchBarBounds(width, height - config.systemInsets.top, searchBarSpaceBounds); 222 mSearchBar.measure( 223 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY), 224 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY)); 225 } 226 227 // We give the full width of the space, not including the right nav bar insets in landscape, 228 // to the stack view, since we want the tasks to render under the search bar in landscape. 229 // In addition, we give it the full height, not including the top inset or search bar space, 230 // since we want the tasks to render under the navigation buttons in portrait. 231 Rect taskStackBounds = new Rect(); 232 config.getTaskStackBounds(width, height, taskStackBounds); 233 int childWidth = width - config.systemInsets.right; 234 int childHeight = taskStackBounds.height() - config.systemInsets.top; 235 236 // Measure each TaskStackView 237 int childCount = getChildCount(); 238 for (int i = 0; i < childCount; i++) { 239 View child = getChildAt(i); 240 if (child instanceof TaskStackView && child.getVisibility() != GONE) { 241 child.measure(MeasureSpec.makeMeasureSpec(childWidth, widthMode), 242 MeasureSpec.makeMeasureSpec(childHeight, heightMode)); 243 } 244 } 245 246 setMeasuredDimension(width, height); 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 onLayout(boolean changed, int left, int top, int right, int bottom) { 254 if (Console.Enabled) { 255 Console.log(Constants.Log.UI.MeasureAndLayout, "[RecentsView|layout]", 256 new Rect(left, top, right, bottom) + " changed: " + changed, Console.AnsiGreen); 257 Console.logTraceTime(Constants.Log.App.TimeRecentsStartup, 258 Constants.Log.App.TimeRecentsStartupKey, "RecentsView.onLayout"); 259 } 260 261 // Get the search bar bounds so that we lay it out 262 RecentsConfiguration config = RecentsConfiguration.getInstance(); 263 if (mSearchBar != null) { 264 Rect searchBarSpaceBounds = new Rect(); 265 config.getSearchBarBounds(getMeasuredWidth(), getMeasuredHeight(), searchBarSpaceBounds); 266 mSearchBar.layout(config.systemInsets.left + searchBarSpaceBounds.left, 267 config.systemInsets.top + searchBarSpaceBounds.top, 268 config.systemInsets.left + mSearchBar.getMeasuredWidth(), 269 config.systemInsets.top + mSearchBar.getMeasuredHeight()); 270 } 271 272 // We offset the stack view by the left inset (if any), but lay it out under the search bar. 273 // In addition, we offset our stack views by the top inset and search bar height, but not 274 // the bottom insets because we want it to render under the navigation buttons. 275 Rect taskStackBounds = new Rect(); 276 config.getTaskStackBounds(getMeasuredWidth(), getMeasuredHeight(), taskStackBounds); 277 left += config.systemInsets.left; 278 top += config.systemInsets.top + taskStackBounds.top; 279 280 // Layout each child 281 // XXX: Based on the space node for that task view 282 int childCount = getChildCount(); 283 for (int i = 0; i < childCount; i++) { 284 View child = getChildAt(i); 285 if (child instanceof TaskStackView && child.getVisibility() != GONE) { 286 TaskStackView tsv = (TaskStackView) child; 287 child.layout(left, top, left + tsv.getMeasuredWidth(), top + tsv.getMeasuredHeight()); 288 } 289 } 290 } 291 292 /** Focuses the next task in the first stack view */ 293 public void focusNextTask(boolean forward) { 294 // Get the first stack view 295 TaskStackView stackView = null; 296 int childCount = getChildCount(); 297 for (int i = 0; i < childCount; i++) { 298 View child = getChildAt(i); 299 if (child instanceof TaskStackView) { 300 stackView = (TaskStackView) child; 301 break; 302 } 303 } 304 305 if (stackView != null) { 306 stackView.focusNextTask(forward); 307 } 308 } 309 310 @Override 311 protected void dispatchDraw(Canvas canvas) { 312 if (Console.Enabled) { 313 Console.log(Constants.Log.UI.Draw, "[RecentsView|dispatchDraw]", "", 314 Console.AnsiPurple); 315 } 316 super.dispatchDraw(canvas); 317 } 318 319 @Override 320 public WindowInsets onApplyWindowInsets(WindowInsets insets) { 321 if (Console.Enabled) { 322 Console.log(Constants.Log.UI.MeasureAndLayout, 323 "[RecentsView|fitSystemWindows]", "insets: " + insets, Console.AnsiGreen); 324 } 325 326 // Update the configuration with the latest system insets and trigger a relayout 327 RecentsConfiguration config = RecentsConfiguration.getInstance(); 328 config.updateSystemInsets(insets.getSystemWindowInsets()); 329 requestLayout(); 330 331 return insets.consumeSystemWindowInsets(false, false, false, true); 332 } 333 334 /** Unfilters any filtered stacks */ 335 public boolean unfilterFilteredStacks() { 336 if (mBSP != null) { 337 // Check if there are any filtered stacks and unfilter them before we back out of Recents 338 boolean stacksUnfiltered = false; 339 ArrayList<TaskStack> stacks = mBSP.getStacks(); 340 for (TaskStack stack : stacks) { 341 if (stack.hasFilteredTasks()) { 342 stack.unfilterTasks(); 343 stacksUnfiltered = true; 344 } 345 } 346 return stacksUnfiltered; 347 } 348 return false; 349 } 350 351 /**** TaskStackView.TaskStackCallbacks Implementation ****/ 352 353 @Override 354 public void onTaskLaunched(final TaskStackView stackView, final TaskView tv, 355 final TaskStack stack, final Task task) { 356 // Notify any callbacks of the launching of a new task 357 if (mCb != null) { 358 boolean isTaskInStackBounds = false; 359 if (stackView != null && tv != null) { 360 isTaskInStackBounds = stackView.isTaskInStackBounds(tv); 361 } 362 mCb.onTaskLaunching(isTaskInStackBounds); 363 } 364 365 final Runnable launchRunnable = new Runnable() { 366 @Override 367 public void run() { 368 TaskViewTransform transform; 369 View sourceView = tv; 370 int offsetX = 0; 371 int offsetY = 0; 372 int stackScroll = stackView.getStackScroll(); 373 if (tv == null) { 374 // If there is no actual task view, then use the stack view as the source view 375 // and then offset to the expected transform rect, but bound this to just 376 // outside the display rect (to ensure we don't animate from too far away) 377 RecentsConfiguration config = RecentsConfiguration.getInstance(); 378 sourceView = stackView; 379 transform = stackView.getStackTransform(stack.indexOfTask(task), stackScroll); 380 offsetX = transform.rect.left; 381 offsetY = Math.min(transform.rect.top, config.displayRect.height()); 382 } else { 383 transform = stackView.getStackTransform(stack.indexOfTask(task), stackScroll); 384 } 385 386 // Compute the thumbnail to scale up from 387 ActivityOptions opts = null; 388 int thumbnailWidth = transform.rect.width(); 389 int thumbnailHeight = transform.rect.height(); 390 if (task.thumbnail != null && thumbnailWidth > 0 && thumbnailHeight > 0 && 391 task.thumbnail.getWidth() > 0 && task.thumbnail.getHeight() > 0) { 392 // Resize the thumbnail to the size of the view that we are animating from 393 Bitmap b = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, 394 Bitmap.Config.ARGB_8888); 395 Canvas c = new Canvas(b); 396 c.drawBitmap(task.thumbnail, 397 new Rect(0, 0, task.thumbnail.getWidth(), task.thumbnail.getHeight()), 398 new Rect(0, 0, thumbnailWidth, thumbnailHeight), null); 399 c.setBitmap(null); 400 opts = ActivityOptions.makeThumbnailScaleUpAnimation(sourceView, 401 b, offsetX, offsetY); 402 } 403 404 if (task.isActive) { 405 // Bring an active task to the foreground 406 RecentsTaskLoader.getInstance().getSystemServicesProxy() 407 .moveTaskToFront(task.key.id, opts); 408 } else { 409 // Launch the activity anew with the desired animation 410 Intent i = new Intent(task.key.baseIntent); 411 i.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 412 | Intent.FLAG_ACTIVITY_TASK_ON_HOME 413 | Intent.FLAG_ACTIVITY_NEW_TASK); 414 try { 415 UserHandle taskUser = new UserHandle(task.userId); 416 if (opts != null) { 417 getContext().startActivityAsUser(i, opts.toBundle(), taskUser); 418 } else { 419 getContext().startActivityAsUser(i, taskUser); 420 } 421 } catch (ActivityNotFoundException anfe) { 422 Console.logError(getContext(), "Could not start Activity"); 423 } 424 425 // And clean up the old task 426 onTaskRemoved(task); 427 } 428 429 Console.logTraceTime(Constants.Log.App.TimeRecentsLaunchTask, 430 Constants.Log.App.TimeRecentsLaunchKey, "startActivity"); 431 } 432 }; 433 434 Console.logTraceTime(Constants.Log.App.TimeRecentsLaunchTask, 435 Constants.Log.App.TimeRecentsLaunchKey, "onTaskLaunched"); 436 437 // Launch the app right away if there is no task view, otherwise, animate the icon out first 438 if (tv == null) { 439 post(launchRunnable); 440 } else { 441 tv.animateOnLeavingRecents(launchRunnable); 442 } 443 } 444 445 @Override 446 public void onTaskAppInfoLaunched(Task t) { 447 // Create a new task stack with the application info details activity 448 Intent baseIntent = t.key.baseIntent; 449 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, 450 Uri.fromParts("package", baseIntent.getComponent().getPackageName(), null)); 451 intent.setComponent(intent.resolveActivity(getContext().getPackageManager())); 452 TaskStackBuilder.create(getContext()) 453 .addNextIntentWithParentStack(intent).startActivities(); 454 } 455 456 @Override 457 public void onTaskRemoved(Task t) { 458 // Remove any stored data from the loader. We currently don't bother notifying the views 459 // that the data has been unloaded because at the point we call onTaskRemoved(), the views 460 // either don't need to be updated, or have already been removed. 461 RecentsTaskLoader loader = RecentsTaskLoader.getInstance(); 462 loader.deleteTaskData(t, false); 463 464 // Remove the old task from activity manager 465 int flags = t.key.baseIntent.getFlags(); 466 boolean isDocument = (flags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) == 467 Intent.FLAG_ACTIVITY_NEW_DOCUMENT; 468 RecentsTaskLoader.getInstance().getSystemServicesProxy().removeTask(t.key.id, 469 isDocument); 470 } 471 472 /**** RecentsPackageMonitor.PackageCallbacks Implementation ****/ 473 474 @Override 475 public void onComponentRemoved(Set<ComponentName> cns) { 476 // Propagate this event down to each task stack view 477 int childCount = getChildCount(); 478 for (int i = 0; i < childCount; i++) { 479 View child = getChildAt(i); 480 if (child instanceof TaskStackView) { 481 TaskStackView stackView = (TaskStackView) child; 482 stackView.onComponentRemoved(cns); 483 } 484 } 485 } 486} 487