blob: 80dcb287f87fdcc087840a10cc713fbfae20d491 [file] [log] [blame]
Winson Chung303e1ff2014-03-07 15:06:19 -08001/*
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;
Winson Chung9f9679d2014-04-11 16:49:09 -070020import android.app.TaskStackBuilder;
Winson Chung303e1ff2014-03-07 15:06:19 -080021import android.content.Context;
22import android.content.Intent;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.Rect;
Winson Chung9f9679d2014-04-11 16:49:09 -070026import android.net.Uri;
Kenny Guy94a07ac2014-08-07 15:34:38 +010027import android.os.UserHandle;
Winson Chung9f9679d2014-04-11 16:49:09 -070028import android.provider.Settings;
Winson Chung8e548f72014-06-24 14:40:53 -070029import android.util.AttributeSet;
Winson Chungecd9b302014-04-16 17:07:18 -070030import android.view.LayoutInflater;
Winson Chung303e1ff2014-03-07 15:06:19 -080031import android.view.View;
Winson Chung653f70c22014-05-19 14:49:42 -070032import android.view.WindowInsets;
Winson Chung303e1ff2014-03-07 15:06:19 -080033import android.widget.FrameLayout;
Winson Chung303e1ff2014-03-07 15:06:19 -080034import com.android.systemui.recents.Constants;
35import com.android.systemui.recents.RecentsConfiguration;
Winson Chung1f24c7e2014-07-11 17:06:48 -070036import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chungff88d7b2014-07-17 12:30:07 -070037import com.android.systemui.recents.misc.Utilities;
Winson Chungf1fbd772014-06-24 18:06:58 -070038import com.android.systemui.recents.model.RecentsPackageMonitor;
39import com.android.systemui.recents.model.RecentsTaskLoader;
Winson Chung303e1ff2014-03-07 15:06:19 -080040import com.android.systemui.recents.model.Task;
41import com.android.systemui.recents.model.TaskStack;
42
43import java.util.ArrayList;
44
Winson Chung303e1ff2014-03-07 15:06:19 -080045/**
46 * This view is the the top level layout that contains TaskStacks (which are laid out according
47 * to their SpaceNode bounds.
48 */
Winson Chung9f49df92014-05-07 18:08:34 -070049public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks,
50 RecentsPackageMonitor.PackageCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070051
52 /** The RecentsView callbacks */
53 public interface RecentsViewCallbacks {
Winson Chung7aceb9a2014-07-03 13:38:01 -070054 public void onTaskViewClicked();
Winson Chung4e96eb72014-09-17 15:16:09 +020055 public void onTaskLaunchFailed();
Winson Chung7aceb9a2014-07-03 13:38:01 -070056 public void onAllTaskViewsDismissed();
Winson Chungcdbbb7e2014-06-24 12:11:49 -070057 public void onExitToHomeAnimationTriggered();
Winson Chung47c4c692014-03-17 10:17:11 -070058 }
59
Winson Chungd42a6cf2014-06-03 16:24:04 -070060 RecentsConfiguration mConfig;
61 LayoutInflater mInflater;
Winson Chungdcfa7972014-07-22 12:27:13 -070062 DebugOverlayView mDebugOverlay;
Winson Chungd42a6cf2014-06-03 16:24:04 -070063
Winson Chungdcfa7972014-07-22 12:27:13 -070064 ArrayList<TaskStack> mStacks;
Winson Chungecd9b302014-04-16 17:07:18 -070065 View mSearchBar;
Winson Chung47c4c692014-03-17 10:17:11 -070066 RecentsViewCallbacks mCb;
Winson Chung02d49272014-08-29 13:57:29 -070067 boolean mAlreadyLaunchingTask;
Winson Chung303e1ff2014-03-07 15:06:19 -080068
69 public RecentsView(Context context) {
70 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -070071 }
72
73 public RecentsView(Context context, AttributeSet attrs) {
74 this(context, attrs, 0);
75 }
76
77 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
78 this(context, attrs, defStyleAttr, 0);
79 }
80
81 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
82 super(context, attrs, defStyleAttr, defStyleRes);
Winson Chungd42a6cf2014-06-03 16:24:04 -070083 mConfig = RecentsConfiguration.getInstance();
Winson Chungecd9b302014-04-16 17:07:18 -070084 mInflater = LayoutInflater.from(context);
Winson Chung303e1ff2014-03-07 15:06:19 -080085 }
86
Winson Chung47c4c692014-03-17 10:17:11 -070087 /** Sets the callbacks */
88 public void setCallbacks(RecentsViewCallbacks cb) {
89 mCb = cb;
90 }
91
Winson Chungdcfa7972014-07-22 12:27:13 -070092 /** Sets the debug overlay */
93 public void setDebugOverlay(DebugOverlayView overlay) {
94 mDebugOverlay = overlay;
95 }
Winson Chung303e1ff2014-03-07 15:06:19 -080096
Winson Chungdcfa7972014-07-22 12:27:13 -070097 /** Set/get the bsp root node */
98 public void setTaskStacks(ArrayList<TaskStack> stacks) {
Winson Chungb0a28ea2014-10-28 15:21:35 -070099 int numStacks = stacks.size();
100
101 // Make a list of the stack view children only
102 ArrayList<TaskStackView> stackViews = new ArrayList<TaskStackView>();
Winson Chung7aceb9a2014-07-03 13:38:01 -0700103 int childCount = getChildCount();
Winson Chungb0a28ea2014-10-28 15:21:35 -0700104 for (int i = 0; i < childCount; i++) {
105 View child = getChildAt(i);
106 if (child != mSearchBar) {
107 stackViews.add((TaskStackView) child);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700108 }
109 }
110
Winson Chungb0a28ea2014-10-28 15:21:35 -0700111 // Remove all/extra stack views
112 int numTaskStacksToKeep = 0; // Keep no tasks if we are recreating the layout
113 if (mConfig.launchedReuseTaskStackViews) {
114 numTaskStacksToKeep = Math.min(childCount, numStacks);
115 }
116 for (int i = stackViews.size() - 1; i >= numTaskStacksToKeep; i--) {
117 removeView(stackViews.get(i));
118 stackViews.remove(i);
119 }
120
121 // Update the stack views that we are keeping
122 for (int i = 0; i < numTaskStacksToKeep; i++) {
123 stackViews.get(i).setStack(stacks.get(i));
124 }
125
126 // Add remaining/recreate stack views
Winson Chungdcfa7972014-07-22 12:27:13 -0700127 mStacks = stacks;
Winson Chungb0a28ea2014-10-28 15:21:35 -0700128 for (int i = stackViews.size(); i < numStacks; i++) {
129 TaskStack stack = stacks.get(i);
Winson Chung303e1ff2014-03-07 15:06:19 -0800130 TaskStackView stackView = new TaskStackView(getContext(), stack);
131 stackView.setCallbacks(this);
132 addView(stackView);
133 }
Winson Chung02d49272014-08-29 13:57:29 -0700134
Winson Chungb0a28ea2014-10-28 15:21:35 -0700135 // Enable debug mode drawing on all the stacks if necessary
136 if (mConfig.debugModeEnabled) {
137 for (int i = childCount - 1; i >= 0; i--) {
138 View v = getChildAt(i);
139 if (v != mSearchBar) {
140 TaskStackView stackView = (TaskStackView) v;
141 stackView.setDebugOverlay(mDebugOverlay);
142 }
143 }
144 }
145
Winson Chung02d49272014-08-29 13:57:29 -0700146 // Reset the launched state
147 mAlreadyLaunchingTask = false;
Winson Chungb0a28ea2014-10-28 15:21:35 -0700148 // Trigger a new layout
149 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700150 }
151
Winson Chung1e8d71b2014-05-16 17:05:22 -0700152 /** Launches the focused task from the first stack if possible */
153 public boolean launchFocusedTask() {
154 // Get the first stack view
155 int childCount = getChildCount();
156 for (int i = 0; i < childCount; i++) {
157 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700158 if (child != mSearchBar) {
Winson Chung1e8d71b2014-05-16 17:05:22 -0700159 TaskStackView stackView = (TaskStackView) child;
160 TaskStack stack = stackView.mStack;
161 // Iterate the stack views and try and find the focused task
162 int taskCount = stackView.getChildCount();
163 for (int j = 0; j < taskCount; j++) {
164 TaskView tv = (TaskView) stackView.getChildAt(j);
165 Task task = tv.getTask();
166 if (tv.isFocusedTask()) {
Winson Chung1f24c7e2014-07-11 17:06:48 -0700167 onTaskViewClicked(stackView, tv, stack, task, false);
Winson Chung1e8d71b2014-05-16 17:05:22 -0700168 return true;
169 }
170 }
171 }
172 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700173 return false;
174 }
175
Winson Chungdcfa7972014-07-22 12:27:13 -0700176 /** Launches the task that Recents was launched from, if possible */
177 public boolean launchPreviousTask() {
Winson Chung47c4c692014-03-17 10:17:11 -0700178 // Get the first stack view
Winson Chung303e1ff2014-03-07 15:06:19 -0800179 int childCount = getChildCount();
180 for (int i = 0; i < childCount; i++) {
Winson Chungecd9b302014-04-16 17:07:18 -0700181 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700182 if (child != mSearchBar) {
Winson Chungecd9b302014-04-16 17:07:18 -0700183 TaskStackView stackView = (TaskStackView) child;
184 TaskStack stack = stackView.mStack;
185 ArrayList<Task> tasks = stack.getTasks();
Winson Chung47c4c692014-03-17 10:17:11 -0700186
Winson Chungdcfa7972014-07-22 12:27:13 -0700187 // Find the launch task in the stack
Winson Chungecd9b302014-04-16 17:07:18 -0700188 if (!tasks.isEmpty()) {
Winson Chungdcfa7972014-07-22 12:27:13 -0700189 int taskCount = tasks.size();
190 for (int j = 0; j < taskCount; j++) {
191 if (tasks.get(j).isLaunchTarget) {
192 Task task = tasks.get(j);
193 TaskView tv = stackView.getChildViewForTask(task);
194 onTaskViewClicked(stackView, tv, stack, task, false);
195 return true;
Winson Chungecd9b302014-04-16 17:07:18 -0700196 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800197 }
198 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800199 }
200 }
201 return false;
202 }
203
Winson Chung24cf1522014-05-29 12:03:33 -0700204 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700205 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung24cf1522014-05-29 12:03:33 -0700206 int childCount = getChildCount();
207 for (int i = 0; i < childCount; i++) {
208 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700209 if (child != mSearchBar) {
Winson Chung24cf1522014-05-29 12:03:33 -0700210 TaskStackView stackView = (TaskStackView) child;
Winson Chung969f5862014-06-16 17:08:24 -0700211 stackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700212 }
213 }
214 }
215
Winson Chungd42a6cf2014-06-03 16:24:04 -0700216 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700217 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chungd7b2cb12014-06-26 15:08:50 -0700218 int childCount = getChildCount();
219 for (int i = 0; i < childCount; i++) {
220 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700221 if (child != mSearchBar) {
Winson Chungd7b2cb12014-06-26 15:08:50 -0700222 TaskStackView stackView = (TaskStackView) child;
223 stackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700224 }
225 }
226
Winson Chung969f5862014-06-16 17:08:24 -0700227 // Notify of the exit animation
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700228 mCb.onExitToHomeAnimationTriggered();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700229 }
230
Winson Chungf7bca432014-04-30 17:11:13 -0700231 /** Adds the search bar */
232 public void setSearchBar(View searchBar) {
233 // Create the search bar (and hide it if we have no recent tasks)
Winson Chung814086d2014-05-07 15:01:14 -0700234 if (Constants.DebugFlags.App.EnableSearchLayout) {
Winson Chungf7bca432014-04-30 17:11:13 -0700235 // Remove the previous search bar if one exists
236 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
237 removeView(mSearchBar);
Winson Chungecd9b302014-04-16 17:07:18 -0700238 }
Winson Chungf7bca432014-04-30 17:11:13 -0700239 // Add the new search bar
240 if (searchBar != null) {
241 mSearchBar = searchBar;
Winson Chungf7bca432014-04-30 17:11:13 -0700242 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700243 }
244 }
Winson Chungecd9b302014-04-16 17:07:18 -0700245 }
246
Winson Chung772b6b12014-07-03 15:54:02 -0700247 /** Returns whether there is currently a search bar */
248 public boolean hasSearchBar() {
249 return mSearchBar != null;
250 }
251
252 /** Sets the visibility of the search bar */
253 public void setSearchBarVisibility(int visibility) {
254 if (mSearchBar != null) {
255 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700256 // Always bring the search bar to the top
257 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700258 }
259 }
260
Winson Chungf7bca432014-04-30 17:11:13 -0700261 /**
262 * This is called with the full size of the window since we are handling our own insets.
263 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800264 @Override
265 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
266 int width = MeasureSpec.getSize(widthMeasureSpec);
267 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700268
Winson Chungf7bca432014-04-30 17:11:13 -0700269 // Get the search bar bounds and measure the search bar layout
Winson Chungecd9b302014-04-16 17:07:18 -0700270 if (mSearchBar != null) {
Winson Chungf7bca432014-04-30 17:11:13 -0700271 Rect searchBarSpaceBounds = new Rect();
Winson Chungdcfa7972014-07-22 12:27:13 -0700272 mConfig.getSearchBarBounds(width, height, mConfig.systemInsets.top, searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700273 mSearchBar.measure(
274 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
275 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700276 }
277
Winson Chungf7bca432014-04-30 17:11:13 -0700278 Rect taskStackBounds = new Rect();
Winson Chungdcfa7972014-07-22 12:27:13 -0700279 mConfig.getTaskStackBounds(width, height, mConfig.systemInsets.top,
280 mConfig.systemInsets.right, taskStackBounds);
Winson Chung303e1ff2014-03-07 15:06:19 -0800281
Winson Chungdcfa7972014-07-22 12:27:13 -0700282 // Measure each TaskStackView with the full width and height of the window since the
283 // transition view is a child of that stack view
Winson Chung303e1ff2014-03-07 15:06:19 -0800284 int childCount = getChildCount();
285 for (int i = 0; i < childCount; i++) {
Winson Chungecd9b302014-04-16 17:07:18 -0700286 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700287 if (child != mSearchBar && child.getVisibility() != GONE) {
Winson Chungdcfa7972014-07-22 12:27:13 -0700288 TaskStackView tsv = (TaskStackView) child;
289 // Set the insets to be the top/left inset + search bounds
290 tsv.setStackInsetRect(taskStackBounds);
291 tsv.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800292 }
293 }
294
295 setMeasuredDimension(width, height);
296 }
297
Winson Chungf7bca432014-04-30 17:11:13 -0700298 /**
299 * This is called with the full size of the window since we are handling our own insets.
300 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800301 @Override
302 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf7bca432014-04-30 17:11:13 -0700303 // Get the search bar bounds so that we lay it out
Winson Chungecd9b302014-04-16 17:07:18 -0700304 if (mSearchBar != null) {
Winson Chungf7bca432014-04-30 17:11:13 -0700305 Rect searchBarSpaceBounds = new Rect();
Winson Chungdcfa7972014-07-22 12:27:13 -0700306 mConfig.getSearchBarBounds(getMeasuredWidth(), getMeasuredHeight(),
307 mConfig.systemInsets.top, searchBarSpaceBounds);
308 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
309 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700310 }
311
Winson Chungdcfa7972014-07-22 12:27:13 -0700312 // Layout each TaskStackView with the full width and height of the window since the
313 // transition view is a child of that stack view
Winson Chung303e1ff2014-03-07 15:06:19 -0800314 int childCount = getChildCount();
315 for (int i = 0; i < childCount; i++) {
Winson Chungecd9b302014-04-16 17:07:18 -0700316 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700317 if (child != mSearchBar && child.getVisibility() != GONE) {
Winson Chungdcfa7972014-07-22 12:27:13 -0700318 child.layout(left, top, left + child.getMeasuredWidth(),
319 top + child.getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800320 }
321 }
322 }
323
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700324 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700325 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700326 // Update the configuration with the latest system insets and trigger a relayout
327 mConfig.updateSystemInsets(insets.getSystemWindowInsets());
328 requestLayout();
Winson Chungdcfa7972014-07-22 12:27:13 -0700329 return insets.consumeSystemWindowInsets();
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700330 }
331
Winson Chunga26fb782014-06-12 17:52:39 -0700332 /** Notifies each task view of the user interaction. */
333 public void onUserInteraction() {
334 // Get the first stack view
Winson Chunga26fb782014-06-12 17:52:39 -0700335 int childCount = getChildCount();
336 for (int i = 0; i < childCount; i++) {
337 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700338 if (child != mSearchBar) {
Winson Chunga0e88b52014-08-11 19:25:42 -0700339 TaskStackView stackView = (TaskStackView) child;
Winson Chunga26fb782014-06-12 17:52:39 -0700340 stackView.onUserInteraction();
341 }
342 }
343 }
344
Winson Chung1e8d71b2014-05-16 17:05:22 -0700345 /** Focuses the next task in the first stack view */
346 public void focusNextTask(boolean forward) {
347 // Get the first stack view
Winson Chung1e8d71b2014-05-16 17:05:22 -0700348 int childCount = getChildCount();
349 for (int i = 0; i < childCount; i++) {
350 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700351 if (child != mSearchBar) {
Winson Chunga0e88b52014-08-11 19:25:42 -0700352 TaskStackView stackView = (TaskStackView) child;
Winson Chungd213a1e2014-10-02 11:18:30 -0700353 stackView.focusNextTask(forward, true);
Winson Chung1e8d71b2014-05-16 17:05:22 -0700354 break;
355 }
356 }
Winson Chunga0e88b52014-08-11 19:25:42 -0700357 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700358
Winson Chunga0e88b52014-08-11 19:25:42 -0700359 /** Dismisses the focused task. */
360 public void dismissFocusedTask() {
361 // Get the first stack view
362 int childCount = getChildCount();
363 for (int i = 0; i < childCount; i++) {
364 View child = getChildAt(i);
365 if (child != mSearchBar) {
366 TaskStackView stackView = (TaskStackView) child;
367 stackView.dismissFocusedTask();
368 break;
369 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700370 }
371 }
372
Winson Chung303e1ff2014-03-07 15:06:19 -0800373 /** Unfilters any filtered stacks */
374 public boolean unfilterFilteredStacks() {
Winson Chungdcfa7972014-07-22 12:27:13 -0700375 if (mStacks != null) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800376 // Check if there are any filtered stacks and unfilter them before we back out of Recents
377 boolean stacksUnfiltered = false;
Winson Chungdcfa7972014-07-22 12:27:13 -0700378 int numStacks = mStacks.size();
379 for (int i = 0; i < numStacks; i++) {
380 TaskStack stack = mStacks.get(i);
Winson Chung303e1ff2014-03-07 15:06:19 -0800381 if (stack.hasFilteredTasks()) {
382 stack.unfilterTasks();
383 stacksUnfiltered = true;
384 }
385 }
386 return stacksUnfiltered;
387 }
388 return false;
389 }
390
Winson Chung47c4c692014-03-17 10:17:11 -0700391 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800392
393 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700394 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Winson Chung1f24c7e2014-07-11 17:06:48 -0700395 final TaskStack stack, final Task task, final boolean lockToTask) {
Winson Chung47c4c692014-03-17 10:17:11 -0700396 // Notify any callbacks of the launching of a new task
397 if (mCb != null) {
Winson Chung7aceb9a2014-07-03 13:38:01 -0700398 mCb.onTaskViewClicked();
Winson Chung47c4c692014-03-17 10:17:11 -0700399 }
Winson Chung02d49272014-08-29 13:57:29 -0700400 // Skip if we are already launching tasks
401 if (mAlreadyLaunchingTask) {
402 return;
403 }
404 mAlreadyLaunchingTask = true;
Winson Chung47c4c692014-03-17 10:17:11 -0700405
Winson Chunge0e45bc2014-06-17 17:56:17 -0700406 // Upfront the processing of the thumbnail
Winson Chungffa2ec62014-07-03 15:54:42 -0700407 TaskViewTransform transform = new TaskViewTransform();
Jorim Jaggicb557032014-09-16 23:09:24 +0200408 View sourceView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700409 int offsetX = 0;
410 int offsetY = 0;
Winson Chung012ef362014-07-31 18:36:25 -0700411 float stackScroll = stackView.getScroller().getStackScroll();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700412 if (tv == null) {
413 // If there is no actual task view, then use the stack view as the source view
414 // and then offset to the expected transform rect, but bound this to just
415 // outside the display rect (to ensure we don't animate from too far away)
416 sourceView = stackView;
Winson Chung012ef362014-07-31 18:36:25 -0700417 transform = stackView.getStackAlgorithm().getStackTransform(task, stackScroll, transform, null);
Winson Chunge0e45bc2014-06-17 17:56:17 -0700418 offsetX = transform.rect.left;
Winson Chunge41ab842014-08-07 15:55:37 -0700419 offsetY = mConfig.displayRect.height();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700420 } else {
Jorim Jaggicb557032014-09-16 23:09:24 +0200421 sourceView = tv.mThumbnailView;
Winson Chung012ef362014-07-31 18:36:25 -0700422 transform = stackView.getStackAlgorithm().getStackTransform(task, stackScroll, transform, null);
Winson Chunge0e45bc2014-06-17 17:56:17 -0700423 }
424
425 // Compute the thumbnail to scale up from
Winson Chung1f24c7e2014-07-11 17:06:48 -0700426 final SystemServicesProxy ssp =
427 RecentsTaskLoader.getInstance().getSystemServicesProxy();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700428 ActivityOptions opts = null;
Winson Chung2e7f3bd2014-09-05 13:17:22 +0200429 if (task.thumbnail != null && task.thumbnail.getWidth() > 0 &&
430 task.thumbnail.getHeight() > 0) {
Winson Chunga4ccb862014-08-22 15:26:27 -0700431 Bitmap b;
432 if (tv != null) {
433 // Disable any focused state before we draw the header
434 if (tv.isFocusedTask()) {
435 tv.unsetFocusedTask();
436 }
437
Winson Chung2e7f3bd2014-09-05 13:17:22 +0200438 float scale = tv.getScaleX();
439 int fromHeaderWidth = (int) (tv.mHeaderView.getMeasuredWidth() * scale);
440 int fromHeaderHeight = (int) (tv.mHeaderView.getMeasuredHeight() * scale);
441 b = Bitmap.createBitmap(fromHeaderWidth, fromHeaderHeight,
442 Bitmap.Config.ARGB_8888);
Winson Chunga4ccb862014-08-22 15:26:27 -0700443 if (Constants.DebugFlags.App.EnableTransitionThumbnailDebugMode) {
444 b.eraseColor(0xFFff0000);
445 } else {
446 Canvas c = new Canvas(b);
447 c.scale(tv.getScaleX(), tv.getScaleY());
448 tv.mHeaderView.draw(c);
449 c.setBitmap(null);
450 }
451 } else {
452 // Notify the system to skip the thumbnail layer by using an ALPHA_8 bitmap
Winson Chung2e7f3bd2014-09-05 13:17:22 +0200453 b = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8);
Winson Chunga4ccb862014-08-22 15:26:27 -0700454 }
Winson Chung1f24c7e2014-07-11 17:06:48 -0700455 ActivityOptions.OnAnimationStartedListener animStartedListener = null;
456 if (lockToTask) {
457 animStartedListener = new ActivityOptions.OnAnimationStartedListener() {
458 boolean mTriggered = false;
459 @Override
460 public void onAnimationStarted() {
461 if (!mTriggered) {
462 postDelayed(new Runnable() {
463 @Override
464 public void run() {
465 ssp.lockCurrentTask();
466 }
467 }, 350);
468 mTriggered = true;
469 }
470 }
471 };
472 }
Winson Chunga4ccb862014-08-22 15:26:27 -0700473 opts = ActivityOptions.makeThumbnailAspectScaleUpAnimation(sourceView,
Winson Chung2e7f3bd2014-09-05 13:17:22 +0200474 b, offsetX, offsetY, transform.rect.width(), transform.rect.height(),
475 animStartedListener);
Winson Chunge0e45bc2014-06-17 17:56:17 -0700476 }
477
478 final ActivityOptions launchOpts = opts;
Winson Chung303e1ff2014-03-07 15:06:19 -0800479 final Runnable launchRunnable = new Runnable() {
480 @Override
481 public void run() {
Winson Chung4be04452014-03-24 17:22:30 -0700482 if (task.isActive) {
483 // Bring an active task to the foreground
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700484 ssp.moveTaskToFront(task.key.id, launchOpts);
Winson Chung4be04452014-03-24 17:22:30 -0700485 } else {
Winson Chung4e96eb72014-09-17 15:16:09 +0200486 if (ssp.startActivityFromRecents(getContext(), task.key.id,
487 task.activityLabel, launchOpts)) {
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700488 if (launchOpts == null && lockToTask) {
489 ssp.lockCurrentTask();
Winson Chung4be04452014-03-24 17:22:30 -0700490 }
Winson Chung4e96eb72014-09-17 15:16:09 +0200491 } else {
492 // Dismiss the task and return the user to home if we fail to
493 // launch the task
494 onTaskViewDismissed(task);
495 if (mCb != null) {
496 mCb.onTaskLaunchFailed();
497 }
Winson Chung4be04452014-03-24 17:22:30 -0700498 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800499 }
500 }
501 };
502
503 // Launch the app right away if there is no task view, otherwise, animate the icon out first
Winson Chung814086d2014-05-07 15:01:14 -0700504 if (tv == null) {
Winson Chung47c4c692014-03-17 10:17:11 -0700505 post(launchRunnable);
Winson Chung303e1ff2014-03-07 15:06:19 -0800506 } else {
Winson Chunga4ccb862014-08-22 15:26:27 -0700507 if (!task.group.isFrontMostTask(task)) {
508 // For affiliated tasks that are behind other tasks, we must animate the front cards
509 // out of view before starting the task transition
Winson Chungebfc6982014-08-26 12:25:34 -0700510 stackView.startLaunchTaskAnimation(tv, launchRunnable, lockToTask);
Winson Chunga4ccb862014-08-22 15:26:27 -0700511 } else {
512 // Otherwise, we can start the task transition immediately
Winson Chungebfc6982014-08-26 12:25:34 -0700513 stackView.startLaunchTaskAnimation(tv, null, lockToTask);
Winson Chunga4ccb862014-08-22 15:26:27 -0700514 postDelayed(launchRunnable, 17);
515 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800516 }
517 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700518
519 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700520 public void onTaskViewAppInfoClicked(Task t) {
Winson Chung9f9679d2014-04-11 16:49:09 -0700521 // Create a new task stack with the application info details activity
522 Intent baseIntent = t.key.baseIntent;
523 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
524 Uri.fromParts("package", baseIntent.getComponent().getPackageName(), null));
525 intent.setComponent(intent.resolveActivity(getContext().getPackageManager()));
526 TaskStackBuilder.create(getContext())
Kenny Guy94a07ac2014-08-07 15:34:38 +0100527 .addNextIntentWithParentStack(intent).startActivities(null,
Winson Chunga4ccb862014-08-22 15:26:27 -0700528 new UserHandle(t.key.userId));
Winson Chung9f9679d2014-04-11 16:49:09 -0700529 }
Winson Chung9f49df92014-05-07 18:08:34 -0700530
Winson Chung5393dff2014-05-08 14:25:43 -0700531 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700532 public void onTaskViewDismissed(Task t) {
Winson Chung5393dff2014-05-08 14:25:43 -0700533 // Remove any stored data from the loader. We currently don't bother notifying the views
Winson Chung7aceb9a2014-07-03 13:38:01 -0700534 // that the data has been unloaded because at the point we call onTaskViewDismissed(), the views
Winson Chung5393dff2014-05-08 14:25:43 -0700535 // either don't need to be updated, or have already been removed.
536 RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
537 loader.deleteTaskData(t, false);
538
539 // Remove the old task from activity manager
Winson Chung5393dff2014-05-08 14:25:43 -0700540 RecentsTaskLoader.getInstance().getSystemServicesProxy().removeTask(t.key.id,
Winson Chungff88d7b2014-07-17 12:30:07 -0700541 Utilities.isDocument(t.key.baseIntent));
Winson Chung5393dff2014-05-08 14:25:43 -0700542 }
543
Winson Chung8e548f72014-06-24 14:40:53 -0700544 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700545 public void onAllTaskViewsDismissed() {
546 mCb.onAllTaskViewsDismissed();
Winson Chungd7b2cb12014-06-26 15:08:50 -0700547 }
548
Winson Chungb0a28ea2014-10-28 15:21:35 -0700549 /** Final callback after Recents is finally hidden. */
550 public void onRecentsHidden() {
551 // Notify each task stack view
552 int childCount = getChildCount();
553 for (int i = 0; i < childCount; i++) {
554 View child = getChildAt(i);
555 if (child != mSearchBar) {
556 TaskStackView stackView = (TaskStackView) child;
557 stackView.onRecentsHidden();
558 }
559 }
560 }
561
Winson Chungd7b2cb12014-06-26 15:08:50 -0700562 @Override
Winson Chung8e548f72014-06-24 14:40:53 -0700563 public void onTaskStackFilterTriggered() {
564 // Hide the search bar
565 if (mSearchBar != null) {
566 mSearchBar.animate()
567 .alpha(0f)
568 .setStartDelay(0)
569 .setInterpolator(mConfig.fastOutSlowInInterpolator)
570 .setDuration(mConfig.filteringCurrentViewsAnimDuration)
571 .withLayer()
572 .start();
573 }
574 }
575
576 @Override
577 public void onTaskStackUnfilterTriggered() {
578 // Show the search bar
579 if (mSearchBar != null) {
580 mSearchBar.animate()
581 .alpha(1f)
582 .setStartDelay(0)
583 .setInterpolator(mConfig.fastOutSlowInInterpolator)
584 .setDuration(mConfig.filteringNewViewsAnimDuration)
585 .withLayer()
586 .start();
587 }
588 }
589
Winson Chung9f49df92014-05-07 18:08:34 -0700590 /**** RecentsPackageMonitor.PackageCallbacks Implementation ****/
591
592 @Override
Winson Chung04400672014-10-17 14:53:30 -0700593 public void onPackagesChanged(RecentsPackageMonitor monitor, String packageName, int userId) {
Winson Chung9f49df92014-05-07 18:08:34 -0700594 // Propagate this event down to each task stack view
595 int childCount = getChildCount();
596 for (int i = 0; i < childCount; i++) {
597 View child = getChildAt(i);
Winson Chung7aceb9a2014-07-03 13:38:01 -0700598 if (child != mSearchBar) {
Winson Chung9f49df92014-05-07 18:08:34 -0700599 TaskStackView stackView = (TaskStackView) child;
Winson Chung04400672014-10-17 14:53:30 -0700600 stackView.onPackagesChanged(monitor, packageName, userId);
Winson Chung9f49df92014-05-07 18:08:34 -0700601 }
602 }
603 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800604}