blob: 9cba93b00a6191ddefe3fabb32b1ee51ccc3d72a [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 *
Peter Schillerb124d562015-12-11 21:31:17 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Winson Chung303e1ff2014-03-07 15:06:19 -08009 *
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;
18
19import android.app.Activity;
Winson Chungd543c1b2014-06-23 15:06:45 -070020import android.app.ActivityOptions;
Winson2536c7e2015-10-01 15:49:31 -070021import android.app.TaskStackBuilder;
Winson Chung47c4c692014-03-17 10:17:11 -070022import android.content.BroadcastReceiver;
23import android.content.Context;
Winson Chung303e1ff2014-03-07 15:06:19 -080024import android.content.Intent;
Winson Chung47c4c692014-03-17 10:17:11 -070025import android.content.IntentFilter;
Winsona1809852016-03-15 11:41:37 -070026import android.content.res.Configuration;
Winson2536c7e2015-10-01 15:49:31 -070027import android.net.Uri;
Winson Chung303e1ff2014-03-07 15:06:19 -080028import android.os.Bundle;
Winson Chung15a2ba82014-11-18 11:19:24 -080029import android.os.SystemClock;
Winson Chungd543c1b2014-06-23 15:06:45 -070030import android.os.UserHandle;
Winson2536c7e2015-10-01 15:49:31 -070031import android.provider.Settings;
Winson1b585612015-11-06 09:16:26 -080032import android.util.Log;
Winson Chung1e8d71b2014-05-16 17:05:22 -070033import android.view.KeyEvent;
Winson Chung303e1ff2014-03-07 15:06:19 -080034import android.view.View;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -080035import android.view.ViewTreeObserver;
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -080036import android.view.WindowManager;
Jorim Jaggic69bd222016-03-15 14:38:37 +010037import android.view.WindowManager.LayoutParams;
Chris Wrenf6e9228b2016-01-26 18:04:35 -050038
Winson Chung5c9f4b92015-06-25 16:16:46 -070039import com.android.internal.logging.MetricsLogger;
Chris Wrenf6e9228b2016-01-26 18:04:35 -050040import com.android.internal.logging.MetricsProto.MetricsEvent;
Winson Chung47c4c692014-03-17 10:17:11 -070041import com.android.systemui.R;
Winsonb78bb4f2015-09-24 17:22:57 -070042import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080043import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winsone693aaf2016-03-01 12:05:59 -080044import com.android.systemui.recents.events.activity.ConfigurationChangedEvent;
Winsonb1e71d02015-11-23 12:40:23 -080045import com.android.systemui.recents.events.activity.DebugFlagsChangedEvent;
Winsonef064132016-01-05 12:11:31 -080046import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Jorim Jaggi192086e2016-03-11 17:17:03 +010047import com.android.systemui.recents.events.activity.DockedFirstAnimationFrameEvent;
Winson3fb67562015-11-11 10:39:03 -080048import com.android.systemui.recents.events.activity.EnterRecentsWindowAnimationCompletedEvent;
Winson1b585612015-11-06 09:16:26 -080049import com.android.systemui.recents.events.activity.EnterRecentsWindowLastAnimationFrameEvent;
Winsonef064132016-01-05 12:11:31 -080050import com.android.systemui.recents.events.activity.ExitRecentsWindowFirstAnimationFrameEvent;
Winson412e1802015-10-20 16:57:57 -070051import com.android.systemui.recents.events.activity.HideRecentsEvent;
Winson0d14d4d2015-10-26 17:05:04 -070052import com.android.systemui.recents.events.activity.IterateRecentsEvent;
Winson83c1b072015-11-09 10:48:04 -080053import com.android.systemui.recents.events.activity.LaunchTaskFailedEvent;
54import com.android.systemui.recents.events.activity.LaunchTaskSucceededEvent;
Winson88737542016-02-17 13:27:33 -080055import com.android.systemui.recents.events.activity.MultiWindowStateChangedEvent;
Winson412e1802015-10-20 16:57:57 -070056import com.android.systemui.recents.events.activity.ToggleRecentsEvent;
Winson190fe3bf2015-10-20 14:57:24 -070057import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
58import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
Winson397ae742015-11-20 11:27:33 -080059import com.android.systemui.recents.events.ui.AllTaskViewsDismissedEvent;
Winsonef064132016-01-05 12:11:31 -080060import com.android.systemui.recents.events.ui.DeleteTaskDataEvent;
Jorim Jaggi11cc01d2016-01-22 19:39:23 -080061import com.android.systemui.recents.events.ui.RecentsDrawnEvent;
Winson2536c7e2015-10-01 15:49:31 -070062import com.android.systemui.recents.events.ui.ShowApplicationInfoEvent;
Winsone5f1faa2015-11-20 12:26:23 -080063import com.android.systemui.recents.events.ui.StackViewScrolledEvent;
Winsonb1e71d02015-11-23 12:40:23 -080064import com.android.systemui.recents.events.ui.UpdateFreeformTaskViewVisibilityEvent;
Winsone7f138c2015-10-22 16:15:21 -070065import com.android.systemui.recents.events.ui.UserInteractionEvent;
Winson0d14d4d2015-10-26 17:05:04 -070066import com.android.systemui.recents.events.ui.focus.DismissFocusedTaskViewEvent;
67import com.android.systemui.recents.events.ui.focus.FocusNextTaskViewEvent;
68import com.android.systemui.recents.events.ui.focus.FocusPreviousTaskViewEvent;
Winson0d14d4d2015-10-26 17:05:04 -070069import com.android.systemui.recents.misc.DozeTrigger;
Winson Chungffa2ec62014-07-03 15:54:42 -070070import com.android.systemui.recents.misc.SystemServicesProxy;
Winsone6c90732015-09-24 16:06:29 -070071import com.android.systemui.recents.model.RecentsPackageMonitor;
Winson Chunga91c2932014-11-07 15:02:38 -080072import com.android.systemui.recents.model.RecentsTaskLoadPlan;
Winson Chungf1fbd772014-06-24 18:06:58 -070073import com.android.systemui.recents.model.RecentsTaskLoader;
Winson Chungdcfa7972014-07-22 12:27:13 -070074import com.android.systemui.recents.model.Task;
Winson Chung303e1ff2014-03-07 15:06:19 -080075import com.android.systemui.recents.model.TaskStack;
76import com.android.systemui.recents.views.RecentsView;
Winson Chungcdbbb7e2014-06-24 12:11:49 -070077import com.android.systemui.recents.views.SystemBarScrimViews;
Winsond8b1d632016-01-04 17:51:18 -080078import com.android.systemui.statusbar.BaseStatusBar;
Winson Chung303e1ff2014-03-07 15:06:19 -080079
Winson Chung85cfec82014-07-14 14:16:04 -070080/**
81 * The main Recents activity that is started from AlternateRecentsComponent.
82 */
Winson397ae742015-11-20 11:27:33 -080083public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreDrawListener {
Winson Chungd42a6cf2014-06-03 16:24:04 -070084
Winson0d14d4d2015-10-26 17:05:04 -070085 private final static String TAG = "RecentsActivity";
86 private final static boolean DEBUG = false;
87
Winsone6c90732015-09-24 16:06:29 -070088 public final static int EVENT_BUS_PRIORITY = Recents.EVENT_BUS_PRIORITY + 1;
89
Winsone5f1faa2015-11-20 12:26:23 -080090 private RecentsPackageMonitor mPackageMonitor;
91 private long mLastTabKeyEventTime;
92 private boolean mFinishedOnStartup;
93 private boolean mIgnoreAltTabRelease;
Winson88737542016-02-17 13:27:33 -080094 private boolean mIsVisible;
Winson Chung85cfec82014-07-14 14:16:04 -070095
96 // Top level views
Winsone5f1faa2015-11-20 12:26:23 -080097 private RecentsView mRecentsView;
98 private SystemBarScrimViews mScrimViews;
Winson Chungcdbbb7e2014-06-24 12:11:49 -070099
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700100 // Runnables to finish the Recents activity
Winsone693aaf2016-03-01 12:05:59 -0800101 private Intent mHomeIntent;
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700102
Winson0d14d4d2015-10-26 17:05:04 -0700103 // The trigger to automatically launch the current task
Peter Schillerb124d562015-12-11 21:31:17 -0800104 private int mFocusTimerDuration;
105 private DozeTrigger mIterateTrigger;
Winson4b9cded2016-01-26 16:26:47 -0800106 private final UserInteractionEvent mUserInteractionEvent = new UserInteractionEvent();
Winson0d14d4d2015-10-26 17:05:04 -0700107
Winson Chungd543c1b2014-06-23 15:06:45 -0700108 /**
Winson3fb67562015-11-11 10:39:03 -0800109 * A common Runnable to finish Recents by launching Home with an animation depending on the
Peter Schillerb124d562015-12-11 21:31:17 -0800110 * last activity launch state. Generally we always launch home when we exit Recents rather than
Winson3fb67562015-11-11 10:39:03 -0800111 * just finishing the activity since we don't know what is behind Recents in the task stack.
Winson Chungd543c1b2014-06-23 15:06:45 -0700112 */
Winsone693aaf2016-03-01 12:05:59 -0800113 class LaunchHomeRunnable implements Runnable {
Winson49df4202016-01-25 17:33:34 -0800114
Winson Chungd543c1b2014-06-23 15:06:45 -0700115 Intent mLaunchIntent;
Winson49df4202016-01-25 17:33:34 -0800116 ActivityOptions mOpts;
Winson Chungd543c1b2014-06-23 15:06:45 -0700117
Winson Chungd7b2cb12014-06-26 15:08:50 -0700118 /**
Winson3fb67562015-11-11 10:39:03 -0800119 * Creates a finish runnable that starts the specified intent.
Winson Chungd7b2cb12014-06-26 15:08:50 -0700120 */
Winsone693aaf2016-03-01 12:05:59 -0800121 public LaunchHomeRunnable(Intent launchIntent, ActivityOptions opts) {
Winson Chungd543c1b2014-06-23 15:06:45 -0700122 mLaunchIntent = launchIntent;
Winson40a22732016-02-02 18:07:00 -0800123 mOpts = opts;
Winson Chungd543c1b2014-06-23 15:06:45 -0700124 }
125
126 @Override
127 public void run() {
Winson2b9c1d32015-10-05 15:44:23 -0700128 try {
Winson49df4202016-01-25 17:33:34 -0800129 ActivityOptions opts = mOpts;
130 if (opts == null) {
131 opts = ActivityOptions.makeCustomAnimation(RecentsActivity.this,
Winson008ee15f2016-03-18 17:17:25 -0700132 R.anim.recents_to_launcher_enter, R.anim.recents_to_launcher_exit);
Winson49df4202016-01-25 17:33:34 -0800133 }
Winson3fb67562015-11-11 10:39:03 -0800134 startActivityAsUser(mLaunchIntent, opts.toBundle(), UserHandle.CURRENT);
Winson2b9c1d32015-10-05 15:44:23 -0700135 } catch (Exception e) {
Winson1b585612015-11-06 09:16:26 -0800136 Log.e(TAG, getString(R.string.recents_launch_error_message, "Home"), e);
Winson Chungd543c1b2014-06-23 15:06:45 -0700137 }
138 }
139 }
140
Winson Chung85cfec82014-07-14 14:16:04 -0700141 /**
Winson Chung85cfec82014-07-14 14:16:04 -0700142 * Broadcast receiver to handle messages from the system
143 */
Winson Chung31d66c62014-06-30 13:12:54 -0700144 final BroadcastReceiver mSystemBroadcastReceiver = new BroadcastReceiver() {
Winson Chung67369052014-04-07 17:35:48 -0700145 @Override
146 public void onReceive(Context context, Intent intent) {
Winson Chung31d66c62014-06-30 13:12:54 -0700147 String action = intent.getAction();
Winson Chung85cfec82014-07-14 14:16:04 -0700148 if (action.equals(Intent.ACTION_SCREEN_OFF)) {
149 // When the screen turns off, dismiss Recents to Home
Winson2b9c1d32015-10-05 15:44:23 -0700150 dismissRecentsToHomeIfVisible(false);
Winson Chung31d66c62014-06-30 13:12:54 -0700151 }
Winson Chungd42a6cf2014-06-03 16:24:04 -0700152 }
153 };
154
Winson0d14d4d2015-10-26 17:05:04 -0700155 /**
156 * Dismisses recents if we are already visible and the intent is to toggle the recents view.
157 */
Winson42329522016-02-05 10:39:46 -0800158 boolean dismissRecentsToFocusedTask(int logCategory) {
Winson0d14d4d2015-10-26 17:05:04 -0700159 SystemServicesProxy ssp = Recents.getSystemServices();
160 if (ssp.isRecentsTopMost(ssp.getTopMostTask(), null)) {
Winson0d14d4d2015-10-26 17:05:04 -0700161 // If we have a focused Task, launch that Task now
Winson42329522016-02-05 10:39:46 -0800162 if (mRecentsView.launchFocusedTask(logCategory)) return true;
Winson0d14d4d2015-10-26 17:05:04 -0700163 }
164 return false;
165 }
166
167 /**
Winsona0731a12015-12-02 15:10:14 -0800168 * Dismisses recents back to the launch target task.
169 */
170 boolean dismissRecentsToLaunchTargetTaskOrHome() {
171 SystemServicesProxy ssp = Recents.getSystemServices();
172 if (ssp.isRecentsTopMost(ssp.getTopMostTask(), null)) {
173 // If we have a focused Task, launch that Task now
174 if (mRecentsView.launchPreviousTask()) return true;
175 // If none of the other cases apply, then just go Home
Winsonef064132016-01-05 12:11:31 -0800176 dismissRecentsToHome(true /* animateTaskViews */);
Winsona0731a12015-12-02 15:10:14 -0800177 }
178 return false;
179 }
180
181 /**
Winson0d14d4d2015-10-26 17:05:04 -0700182 * Dismisses recents if we are already visible and the intent is to toggle the recents view.
183 */
Winson397ae742015-11-20 11:27:33 -0800184 boolean dismissRecentsToFocusedTaskOrHome() {
Winsone7f138c2015-10-22 16:15:21 -0700185 SystemServicesProxy ssp = Recents.getSystemServices();
Winson Chung2cf8b222015-01-20 11:44:05 -0800186 if (ssp.isRecentsTopMost(ssp.getTopMostTask(), null)) {
Winson Chung85cfec82014-07-14 14:16:04 -0700187 // If we have a focused Task, launch that Task now
Winson42329522016-02-05 10:39:46 -0800188 if (mRecentsView.launchFocusedTask(0 /* logCategory */)) return true;
Winson Chung85cfec82014-07-14 14:16:04 -0700189 // If none of the other cases apply, then just go Home
Winsonef064132016-01-05 12:11:31 -0800190 dismissRecentsToHome(true /* animateTaskViews */);
Winson Chung85cfec82014-07-14 14:16:04 -0700191 return true;
192 }
193 return false;
194 }
195
Winson2b9c1d32015-10-05 15:44:23 -0700196 /**
197 * Dismisses Recents directly to Home without checking whether it is currently visible.
198 */
Winsonef064132016-01-05 12:11:31 -0800199 void dismissRecentsToHome(boolean animateTaskViews) {
Winson49df4202016-01-25 17:33:34 -0800200 dismissRecentsToHome(animateTaskViews, null);
201 }
202
203 /**
204 * Dismisses Recents directly to Home without checking whether it is currently visible.
205 *
206 * @param overrideAnimation If not null, will override the default animation that is based on
207 * how Recents was launched.
208 */
209 void dismissRecentsToHome(boolean animateTaskViews, ActivityOptions overrideAnimation) {
Winsonef064132016-01-05 12:11:31 -0800210 DismissRecentsToHomeAnimationStarted dismissEvent =
211 new DismissRecentsToHomeAnimationStarted(animateTaskViews);
Winsone693aaf2016-03-01 12:05:59 -0800212 dismissEvent.addPostAnimationCallback(new LaunchHomeRunnable(mHomeIntent,
213 overrideAnimation));
Winsonef064132016-01-05 12:11:31 -0800214 dismissEvent.addPostAnimationCallback(new Runnable() {
215 @Override
216 public void run() {
217 Recents.getSystemServices().sendCloseSystemWindows(
218 BaseStatusBar.SYSTEM_DIALOG_REASON_HOME_KEY);
219 }
220 });
221 EventBus.getDefault().send(dismissEvent);
Wale Ogunwaled351ada2015-04-04 11:53:14 -0700222 }
223
Winson Chung85cfec82014-07-14 14:16:04 -0700224 /** Dismisses Recents directly to Home if we currently aren't transitioning. */
Winson2b9c1d32015-10-05 15:44:23 -0700225 boolean dismissRecentsToHomeIfVisible(boolean animated) {
Winsone7f138c2015-10-22 16:15:21 -0700226 SystemServicesProxy ssp = Recents.getSystemServices();
Winson Chung2cf8b222015-01-20 11:44:05 -0800227 if (ssp.isRecentsTopMost(ssp.getTopMostTask(), null)) {
Winson Chung85cfec82014-07-14 14:16:04 -0700228 // Return to Home
Winson2b9c1d32015-10-05 15:44:23 -0700229 dismissRecentsToHome(animated);
Winson Chung47c4c692014-03-17 10:17:11 -0700230 return true;
Winson Chung303e1ff2014-03-07 15:06:19 -0800231 }
232 return false;
233 }
234
235 /** Called with the activity is first created. */
236 @Override
237 public void onCreate(Bundle savedInstanceState) {
238 super.onCreate(savedInstanceState);
Winson3150e572015-10-23 15:07:24 -0700239 mFinishedOnStartup = false;
240
241 // In the case that the activity starts up before the Recents component has initialized
242 // (usually when debugging/pushing the SysUI apk), just finish this activity.
243 SystemServicesProxy ssp = Recents.getSystemServices();
244 if (ssp == null) {
245 mFinishedOnStartup = true;
246 finish();
247 return;
248 }
Winsonb78bb4f2015-09-24 17:22:57 -0700249
250 // Register this activity with the event bus
251 EventBus.getDefault().register(this, EVENT_BUS_PRIORITY);
252
Winson008ee15f2016-03-18 17:17:25 -0700253 // Initialize the package monitor
Winsone7f138c2015-10-22 16:15:21 -0700254 mPackageMonitor = new RecentsPackageMonitor();
255 mPackageMonitor.register(this);
Winson Chungf7bca432014-04-30 17:11:13 -0700256
Winson Chung8e548f72014-06-24 14:40:53 -0700257 // Set the Recents layout
258 setContentView(R.layout.recents);
Winson88737542016-02-17 13:27:33 -0800259 takeKeyEvents(true);
Winson Chung8e548f72014-06-24 14:40:53 -0700260 mRecentsView = (RecentsView) findViewById(R.id.recents_view);
Winson Chung653f70c22014-05-19 14:49:42 -0700261 mRecentsView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
262 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
263 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Winson35f30502015-09-28 11:24:36 -0700264 mScrimViews = new SystemBarScrimViews(this);
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800265 getWindow().getAttributes().privateFlags |=
266 WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY;
Winson Chung303e1ff2014-03-07 15:06:19 -0800267
Peter Schillerb124d562015-12-11 21:31:17 -0800268 mFocusTimerDuration = getResources().getInteger(R.integer.recents_auto_advance_duration);
269 mIterateTrigger = new DozeTrigger(mFocusTimerDuration, new Runnable() {
270 @Override
271 public void run() {
Winson42329522016-02-05 10:39:46 -0800272 dismissRecentsToFocusedTask(MetricsEvent.OVERVIEW_SELECT_TIMEOUT);
Peter Schillerb124d562015-12-11 21:31:17 -0800273 }
274 });
275
Winson3fb67562015-11-11 10:39:03 -0800276 // Create the home intent runnable
Winsone693aaf2016-03-01 12:05:59 -0800277 mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
278 mHomeIntent.addCategory(Intent.CATEGORY_HOME);
279 mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Winson3fb67562015-11-11 10:39:03 -0800280 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson3fb67562015-11-11 10:39:03 -0800281
Winson Chung31d66c62014-06-30 13:12:54 -0700282 // Register the broadcast receiver to handle messages when the screen is turned off
283 IntentFilter filter = new IntentFilter();
284 filter.addAction(Intent.ACTION_SCREEN_OFF);
Winson Chung31d66c62014-06-30 13:12:54 -0700285 registerReceiver(mSystemBroadcastReceiver, filter);
Jorim Jaggic69bd222016-03-15 14:38:37 +0100286
287 getWindow().addPrivateFlags(LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION);
Winson Chung521e7dc2014-06-02 15:31:56 -0700288 }
289
Winson Chung303e1ff2014-03-07 15:06:19 -0800290 @Override
Winson Chung303e1ff2014-03-07 15:06:19 -0800291 protected void onStart() {
Winson Chung303e1ff2014-03-07 15:06:19 -0800292 super.onStart();
Winson Chung303e1ff2014-03-07 15:06:19 -0800293
Winsone7f138c2015-10-22 16:15:21 -0700294 // Notify that recents is now visible
Winson88737542016-02-17 13:27:33 -0800295 EventBus.getDefault().send(new RecentsVisibilityChangedEvent(this, true));
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500296 MetricsLogger.visible(this, MetricsEvent.OVERVIEW_ACTIVITY);
Winson Chung67369052014-04-07 17:35:48 -0700297 }
298
299 @Override
Winson3fb67562015-11-11 10:39:03 -0800300 public void onEnterAnimationComplete() {
301 super.onEnterAnimationComplete();
302 EventBus.getDefault().send(new EnterRecentsWindowAnimationCompletedEvent());
303 }
304
305 @Override
Winson88737542016-02-17 13:27:33 -0800306 protected void onResume() {
307 super.onResume();
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100308
Winson88737542016-02-17 13:27:33 -0800309 // If the Recents component has preloaded a load plan, then use that to prevent
310 // reconstructing the task stack
311 RecentsTaskLoader loader = Recents.getTaskLoader();
312 RecentsTaskLoadPlan loadPlan = RecentsImpl.consumeInstanceLoadPlan();
313 if (loadPlan == null) {
314 loadPlan = loader.createLoadPlan(this);
315 }
316
317 // Start loading tasks according to the load plan
318 RecentsConfiguration config = Recents.getConfiguration();
319 RecentsActivityLaunchState launchState = config.getLaunchState();
320 if (!loadPlan.hasTasks()) {
Winson96e61342016-03-15 16:47:19 -0700321 loader.preloadTasks(loadPlan, launchState.launchedToTaskId,
322 launchState.launchedFromHome);
Winson88737542016-02-17 13:27:33 -0800323 }
324
325 RecentsTaskLoadPlan.Options loadOpts = new RecentsTaskLoadPlan.Options();
326 loadOpts.runningTaskId = launchState.launchedToTaskId;
327 loadOpts.numVisibleTasks = launchState.launchedNumVisibleTasks;
328 loadOpts.numVisibleTaskThumbnails = launchState.launchedNumVisibleThumbnails;
329 loader.loadTasks(this, loadPlan, loadOpts);
330 TaskStack stack = loadPlan.getTaskStack();
Winson59924fe2016-03-17 14:13:18 -0700331 mRecentsView.onResume(mIsVisible, false /* multiWindowChange */, stack);
Winson88737542016-02-17 13:27:33 -0800332
333 // Animate the SystemUI scrims into view
334 Task launchTarget = stack.getLaunchTarget();
335 int taskCount = stack.getTaskCount();
336 int launchTaskIndexInStack = launchTarget != null
337 ? stack.indexOfStackTask(launchTarget)
338 : 0;
339 boolean hasNavBarScrim = (taskCount > 0) && !config.hasTransposedNavBar;
340 boolean animateNavBarScrim = !launchState.launchedWhileDocking;
341 mScrimViews.prepareEnterRecentsAnimation(hasNavBarScrim, animateNavBarScrim);
342
343 // If this is a new instance from a configuration change, then we have to manually trigger
344 // the enter animation state, or if recents was relaunched by AM, without going through
345 // the normal mechanisms
346 boolean wasLaunchedByAm = !launchState.launchedFromHome &&
347 !launchState.launchedFromApp;
348 if (launchState.launchedHasConfigurationChanged || wasLaunchedByAm) {
349 EventBus.getDefault().send(new EnterRecentsWindowAnimationCompletedEvent());
350 }
351
352 mRecentsView.getViewTreeObserver().addOnPreDrawListener(
353 new ViewTreeObserver.OnPreDrawListener() {
354
355 @Override
356 public boolean onPreDraw() {
357 mRecentsView.getViewTreeObserver().removeOnPreDrawListener(this);
358 EventBus.getDefault().post(new RecentsDrawnEvent());
359 return true;
360 }
361 });
362
363 // Keep track of whether we launched from the nav bar button or via alt-tab
364 if (launchState.launchedWithAltTab) {
365 MetricsLogger.count(this, "overview_trigger_alttab", 1);
366 } else {
367 MetricsLogger.count(this, "overview_trigger_nav_btn", 1);
368 }
369
370 // Keep track of whether we launched from an app or from home
371 if (launchState.launchedFromApp) {
372 MetricsLogger.count(this, "overview_source_app", 1);
373 // If from an app, track the stack index of the app in the stack (for affiliated tasks)
374 MetricsLogger.histogram(this, "overview_source_app_index", launchTaskIndexInStack);
375 } else {
376 MetricsLogger.count(this, "overview_source_home", 1);
377 }
378
379 // Keep track of the total stack task count
380 MetricsLogger.histogram(this, "overview_task_count", taskCount);
381
382 // After we have resumed, set the visible state until the next onStop() call
383 mIsVisible = true;
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700384 }
385
386 @Override
Winson88737542016-02-17 13:27:33 -0800387 protected void onPause() {
388 super.onPause();
Winson190fe3bf2015-10-20 14:57:24 -0700389
Winsone5f1faa2015-11-20 12:26:23 -0800390 mIgnoreAltTabRelease = false;
Winson88737542016-02-17 13:27:33 -0800391 mIterateTrigger.stopDozing();
Winson88737542016-02-17 13:27:33 -0800392 }
Winsone7f138c2015-10-22 16:15:21 -0700393
Winson88737542016-02-17 13:27:33 -0800394 @Override
Winsona1809852016-03-15 11:41:37 -0700395 public void onConfigurationChanged(Configuration newConfig) {
396 super.onConfigurationChanged(newConfig);
397
Winson21700932016-03-24 17:26:23 -0700398 EventBus.getDefault().send(new ConfigurationChangedEvent());
Winsona1809852016-03-15 11:41:37 -0700399 }
400
401 @Override
Winson88737542016-02-17 13:27:33 -0800402 protected void onStop() {
403 super.onStop();
404
Winson88737542016-02-17 13:27:33 -0800405 // Notify that recents is now hidden
406 mIsVisible = false;
407 EventBus.getDefault().send(new RecentsVisibilityChangedEvent(this, false));
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500408 MetricsLogger.hidden(this, MetricsEvent.OVERVIEW_ACTIVITY);
Winson73492c52016-03-09 17:44:54 -0800409
410 // Workaround for b/22542869, if the RecentsActivity is started again, but without going
411 // through SystemUI, we need to reset the config launch flags to ensure that we do not
412 // wait on the system to send a signal that was never queued.
413 RecentsConfiguration config = Recents.getConfiguration();
414 RecentsActivityLaunchState launchState = config.getLaunchState();
415 launchState.reset();
Winson Chung47c4c692014-03-17 10:17:11 -0700416 }
417
418 @Override
419 protected void onDestroy() {
Winson Chung47c4c692014-03-17 10:17:11 -0700420 super.onDestroy();
Winson Chung31d66c62014-06-30 13:12:54 -0700421
Winson3150e572015-10-23 15:07:24 -0700422 // In the case that the activity finished on startup, just skip the unregistration below
423 if (mFinishedOnStartup) {
424 return;
425 }
426
Winson Chung85cfec82014-07-14 14:16:04 -0700427 // Unregister the system broadcast receivers
Winson Chung31d66c62014-06-30 13:12:54 -0700428 unregisterReceiver(mSystemBroadcastReceiver);
Winson Chung905950a2014-09-17 09:25:27 +0200429
Winsone7f138c2015-10-22 16:15:21 -0700430 // Unregister any broadcast receivers for the task loader
431 mPackageMonitor.unregister();
432
Winsonb78bb4f2015-09-24 17:22:57 -0700433 EventBus.getDefault().unregister(this);
Winson Chung303e1ff2014-03-07 15:06:19 -0800434 }
435
Winson412e1802015-10-20 16:57:57 -0700436 @Override
437 public void onAttachedToWindow() {
438 super.onAttachedToWindow();
439 EventBus.getDefault().register(mScrimViews, EVENT_BUS_PRIORITY);
440 }
Winson Chungaf3bb692015-06-03 17:31:39 -0700441
Winson412e1802015-10-20 16:57:57 -0700442 @Override
443 public void onDetachedFromWindow() {
444 super.onDetachedFromWindow();
445 EventBus.getDefault().unregister(mScrimViews);
Winson Chung353c0b92014-10-16 17:43:23 -0700446 }
447
448 @Override
Winson Chung4d7b0922014-03-13 17:14:17 -0700449 public void onTrimMemory(int level) {
Winsone7f138c2015-10-22 16:15:21 -0700450 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson Chung4d7b0922014-03-13 17:14:17 -0700451 if (loader != null) {
452 loader.onTrimMemory(level);
453 }
454 }
455
456 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -0800457 public void onMultiWindowChanged(boolean inMultiWindow) {
458 super.onMultiWindowChanged(inMultiWindow);
Winsone693aaf2016-03-01 12:05:59 -0800459 EventBus.getDefault().send(new ConfigurationChangedEvent());
Winson88737542016-02-17 13:27:33 -0800460
461 // Reload the task stack completely
462 RecentsConfiguration config = Recents.getConfiguration();
463 RecentsActivityLaunchState launchState = config.getLaunchState();
Winson931845f2016-02-24 19:38:41 -0800464 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson931845f2016-02-24 19:38:41 -0800465 RecentsTaskLoadPlan loadPlan = loader.createLoadPlan(this);
Winson88737542016-02-17 13:27:33 -0800466 loader.preloadTasks(loadPlan, -1 /* topTaskId */, false /* isTopTaskHome */);
467
468 RecentsTaskLoadPlan.Options loadOpts = new RecentsTaskLoadPlan.Options();
469 loadOpts.numVisibleTasks = launchState.launchedNumVisibleTasks;
470 loadOpts.numVisibleTaskThumbnails = launchState.launchedNumVisibleThumbnails;
471 loader.loadTasks(this, loadPlan, loadOpts);
472
Winson59924fe2016-03-17 14:13:18 -0700473 mRecentsView.onResume(mIsVisible, true /* multiWindowChange */, loadPlan.getTaskStack());
Winson88737542016-02-17 13:27:33 -0800474
475 EventBus.getDefault().send(new MultiWindowStateChangedEvent(inMultiWindow));
Winson Chung06266772015-12-11 10:24:21 -0500476 }
477
478 @Override
Winson Chung1e8d71b2014-05-16 17:05:22 -0700479 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chunga0e88b52014-08-11 19:25:42 -0700480 switch (keyCode) {
481 case KeyEvent.KEYCODE_TAB: {
Winson35f30502015-09-28 11:24:36 -0700482 int altTabKeyDelay = getResources().getInteger(R.integer.recents_alt_tab_key_delay);
Winson Chung15a2ba82014-11-18 11:19:24 -0800483 boolean hasRepKeyTimeElapsed = (SystemClock.elapsedRealtime() -
Winson35f30502015-09-28 11:24:36 -0700484 mLastTabKeyEventTime) > altTabKeyDelay;
Winson Chunga0e88b52014-08-11 19:25:42 -0700485 if (event.getRepeatCount() <= 0 || hasRepKeyTimeElapsed) {
486 // Focus the next task in the stack
487 final boolean backward = event.isShiftPressed();
Winson0d14d4d2015-10-26 17:05:04 -0700488 if (backward) {
489 EventBus.getDefault().send(new FocusPreviousTaskViewEvent());
490 } else {
Peter Schillerb124d562015-12-11 21:31:17 -0800491 EventBus.getDefault().send(
Winson4b9cded2016-01-26 16:26:47 -0800492 new FocusNextTaskViewEvent(0 /* timerIndicatorDuration */));
Winson0d14d4d2015-10-26 17:05:04 -0700493 }
Winson Chung15a2ba82014-11-18 11:19:24 -0800494 mLastTabKeyEventTime = SystemClock.elapsedRealtime();
Winsone5f1faa2015-11-20 12:26:23 -0800495
496 // In the case of another ALT event, don't ignore the next release
497 if (event.isAltPressed()) {
498 mIgnoreAltTabRelease = false;
499 }
Winson Chunga0e88b52014-08-11 19:25:42 -0700500 }
501 return true;
502 }
503 case KeyEvent.KEYCODE_DPAD_UP: {
Peter Schillerb124d562015-12-11 21:31:17 -0800504 EventBus.getDefault().send(
Winson4b9cded2016-01-26 16:26:47 -0800505 new FocusNextTaskViewEvent(0 /* timerIndicatorDuration */));
Winson Chunga0e88b52014-08-11 19:25:42 -0700506 return true;
507 }
508 case KeyEvent.KEYCODE_DPAD_DOWN: {
Winson0d14d4d2015-10-26 17:05:04 -0700509 EventBus.getDefault().send(new FocusPreviousTaskViewEvent());
Winson Chunga0e88b52014-08-11 19:25:42 -0700510 return true;
511 }
512 case KeyEvent.KEYCODE_DEL:
513 case KeyEvent.KEYCODE_FORWARD_DEL: {
Winsondf3012b2016-01-12 15:35:05 -0800514 if (event.getRepeatCount() <= 0) {
515 EventBus.getDefault().send(new DismissFocusedTaskViewEvent());
Winson0d14d4d2015-10-26 17:05:04 -0700516
Winsondf3012b2016-01-12 15:35:05 -0800517 // Keep track of deletions by keyboard
518 MetricsLogger.histogram(this, "overview_task_dismissed_source",
519 Constants.Metrics.DismissSourceKeyboard);
520 return true;
521 }
Winson Chunga0e88b52014-08-11 19:25:42 -0700522 }
523 default:
524 break;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700525 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700526 return super.onKeyDown(keyCode, event);
527 }
528
529 @Override
Winson Chunga26fb782014-06-12 17:52:39 -0700530 public void onUserInteraction() {
Winson4b9cded2016-01-26 16:26:47 -0800531 EventBus.getDefault().send(mUserInteractionEvent);
Winson Chunga26fb782014-06-12 17:52:39 -0700532 }
533
534 @Override
Winson Chung303e1ff2014-03-07 15:06:19 -0800535 public void onBackPressed() {
Winsona0731a12015-12-02 15:10:14 -0800536 // Back behaves like the recents button so just trigger a toggle event
537 EventBus.getDefault().send(new ToggleRecentsEvent());
Winson Chung303e1ff2014-03-07 15:06:19 -0800538 }
Winson Chung47c4c692014-03-17 10:17:11 -0700539
Winsonb78bb4f2015-09-24 17:22:57 -0700540 /**** EventBus events ****/
Winson Chungd543c1b2014-06-23 15:06:45 -0700541
Winson412e1802015-10-20 16:57:57 -0700542 public final void onBusEvent(ToggleRecentsEvent event) {
Winson8f6ee482016-03-18 17:51:48 -0700543 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
544 if (launchState.launchedFromHome) {
545 dismissRecentsToHome(true /* animateTaskViews */);
546 } else {
547 dismissRecentsToLaunchTargetTaskOrHome();
Winsonc29ff002015-11-20 16:00:45 -0800548 }
Winson412e1802015-10-20 16:57:57 -0700549 }
550
Winson0d14d4d2015-10-26 17:05:04 -0700551 public final void onBusEvent(IterateRecentsEvent event) {
Winson8f6ee482016-03-18 17:51:48 -0700552 final RecentsDebugFlags debugFlags = Recents.getDebugFlags();
Peter Schillerb124d562015-12-11 21:31:17 -0800553
Winson8f6ee482016-03-18 17:51:48 -0700554 // Start dozing after the recents button is clicked
555 int timerIndicatorDuration = 0;
556 if (debugFlags.isFastToggleRecentsEnabled()) {
557 timerIndicatorDuration = getResources().getInteger(
558 R.integer.recents_subsequent_auto_advance_duration);
Winson4b9cded2016-01-26 16:26:47 -0800559
Winson8f6ee482016-03-18 17:51:48 -0700560 mIterateTrigger.setDozeDuration(timerIndicatorDuration);
561 if (!mIterateTrigger.isDozing()) {
562 mIterateTrigger.startDozing();
563 } else {
564 mIterateTrigger.poke();
Winson8b1871d2015-11-20 09:56:20 -0800565 }
566 }
Winson8f6ee482016-03-18 17:51:48 -0700567
568 // Focus the next task
569 EventBus.getDefault().send(new FocusNextTaskViewEvent(timerIndicatorDuration));
570
571 MetricsLogger.action(this, MetricsEvent.ACTION_OVERVIEW_PAGE);
Winson0d14d4d2015-10-26 17:05:04 -0700572 }
573
574 public final void onBusEvent(UserInteractionEvent event) {
Winsonb61e6542016-02-04 14:37:18 -0800575 // Stop the fast-toggle dozer
Winson0d14d4d2015-10-26 17:05:04 -0700576 mIterateTrigger.stopDozing();
577 }
578
Winson412e1802015-10-20 16:57:57 -0700579 public final void onBusEvent(HideRecentsEvent event) {
580 if (event.triggeredFromAltTab) {
581 // If we are hiding from releasing Alt-Tab, dismiss Recents to the focused app
Winsone5f1faa2015-11-20 12:26:23 -0800582 if (!mIgnoreAltTabRelease) {
583 dismissRecentsToFocusedTaskOrHome();
584 }
Winson412e1802015-10-20 16:57:57 -0700585 } else if (event.triggeredFromHomeKey) {
Winson8f6ee482016-03-18 17:51:48 -0700586 dismissRecentsToHome(true /* animateTaskViews */);
Winson4b9cded2016-01-26 16:26:47 -0800587
588 // Cancel any pending dozes
589 EventBus.getDefault().send(mUserInteractionEvent);
Winson412e1802015-10-20 16:57:57 -0700590 } else {
591 // Do nothing
592 }
593 }
594
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700595 public final void onBusEvent(EnterRecentsWindowLastAnimationFrameEvent event) {
Winsonb1e71d02015-11-23 12:40:23 -0800596 EventBus.getDefault().send(new UpdateFreeformTaskViewVisibilityEvent(true));
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800597 mRecentsView.getViewTreeObserver().addOnPreDrawListener(this);
Winsonb1e71d02015-11-23 12:40:23 -0800598 mRecentsView.invalidate();
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700599 }
600
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800601 public final void onBusEvent(ExitRecentsWindowFirstAnimationFrameEvent event) {
Winsonb1e71d02015-11-23 12:40:23 -0800602 if (mRecentsView.isLastTaskLaunchedFreeform()) {
603 EventBus.getDefault().send(new UpdateFreeformTaskViewVisibilityEvent(false));
604 }
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800605 mRecentsView.getViewTreeObserver().addOnPreDrawListener(this);
Winsonb1e71d02015-11-23 12:40:23 -0800606 mRecentsView.invalidate();
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800607 }
608
Jorim Jaggi192086e2016-03-11 17:17:03 +0100609 public final void onBusEvent(DockedFirstAnimationFrameEvent event) {
610 mRecentsView.getViewTreeObserver().addOnPreDrawListener(this);
611 mRecentsView.invalidate();
612 }
613
Winson13d30662015-11-06 15:30:29 -0800614 public final void onBusEvent(CancelEnterRecentsWindowAnimationEvent event) {
615 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
616 int launchToTaskId = launchState.launchedToTaskId;
617 if (launchToTaskId != -1 &&
618 (event.launchTask == null || launchToTaskId != event.launchTask.key.id)) {
619 SystemServicesProxy ssp = Recents.getSystemServices();
620 ssp.cancelWindowTransition(launchState.launchedToTaskId);
621 ssp.cancelThumbnailTransition(getTaskId());
622 }
623 }
624
Winson2536c7e2015-10-01 15:49:31 -0700625 public final void onBusEvent(ShowApplicationInfoEvent event) {
626 // Create a new task stack with the application info details activity
Winson2536c7e2015-10-01 15:49:31 -0700627 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Winsone7f138c2015-10-22 16:15:21 -0700628 Uri.fromParts("package", event.task.key.getComponent().getPackageName(), null));
Winson2536c7e2015-10-01 15:49:31 -0700629 intent.setComponent(intent.resolveActivity(getPackageManager()));
630 TaskStackBuilder.create(this)
631 .addNextIntentWithParentStack(intent).startActivities(null,
Peter Schillerb124d562015-12-11 21:31:17 -0800632 new UserHandle(event.task.key.userId));
Winson2536c7e2015-10-01 15:49:31 -0700633
634 // Keep track of app-info invocations
635 MetricsLogger.count(this, "overview_app_info", 1);
636 }
637
Winsonef064132016-01-05 12:11:31 -0800638 public final void onBusEvent(DeleteTaskDataEvent event) {
Winson2536c7e2015-10-01 15:49:31 -0700639 // Remove any stored data from the loader
Winsone7f138c2015-10-22 16:15:21 -0700640 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson2536c7e2015-10-01 15:49:31 -0700641 loader.deleteTaskData(event.task, false);
642
643 // Remove the task from activity manager
Winsone7f138c2015-10-22 16:15:21 -0700644 SystemServicesProxy ssp = Recents.getSystemServices();
645 ssp.removeTask(event.task.key.id);
Winson2536c7e2015-10-01 15:49:31 -0700646 }
647
Winson397ae742015-11-20 11:27:33 -0800648 public final void onBusEvent(AllTaskViewsDismissedEvent event) {
Winson Chung9a742902015-12-11 10:25:40 -0500649 SystemServicesProxy ssp = Recents.getSystemServices();
650 if (ssp.hasDockedTask()) {
Winson20684082016-03-16 17:13:34 -0700651 mRecentsView.showEmptyView(event.msgResId);
Winson Chung9a742902015-12-11 10:25:40 -0500652 } else {
653 // Just go straight home (no animation necessary because there are no more task views)
Winsonef064132016-01-05 12:11:31 -0800654 dismissRecentsToHome(false /* animateTaskViews */);
Winson Chung9a742902015-12-11 10:25:40 -0500655 }
Winson397ae742015-11-20 11:27:33 -0800656
657 // Keep track of all-deletions
658 MetricsLogger.count(this, "overview_task_all_dismissed", 1);
659 }
660
Winson83c1b072015-11-09 10:48:04 -0800661 public final void onBusEvent(LaunchTaskSucceededEvent event) {
662 MetricsLogger.histogram(this, "overview_task_launch_index", event.taskIndexFromStackFront);
663 }
664
665 public final void onBusEvent(LaunchTaskFailedEvent event) {
666 // Return to Home
Winsonef064132016-01-05 12:11:31 -0800667 dismissRecentsToHome(true /* animateTaskViews */);
Winson83c1b072015-11-09 10:48:04 -0800668
669 MetricsLogger.count(this, "overview_task_launch_failed", 1);
670 }
671
Winson412e1802015-10-20 16:57:57 -0700672 public final void onBusEvent(ScreenPinningRequestEvent event) {
673 MetricsLogger.count(this, "overview_screen_pinned", 1);
674 }
675
Winsonc742f972015-11-12 11:32:21 -0800676 public final void onBusEvent(DebugFlagsChangedEvent event) {
677 // Just finish recents so that we can reload the flags anew on the next instantiation
678 finish();
679 }
680
Winsone5f1faa2015-11-20 12:26:23 -0800681 public final void onBusEvent(StackViewScrolledEvent event) {
682 // Once the user has scrolled while holding alt-tab, then we should ignore the release of
683 // the key
684 mIgnoreAltTabRelease = true;
685 }
686
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800687 @Override
688 public boolean onPreDraw() {
689 mRecentsView.getViewTreeObserver().removeOnPreDrawListener(this);
690 // We post to make sure that this information is delivered after this traversals is
691 // finished.
692 mRecentsView.post(new Runnable() {
693 @Override
694 public void run() {
695 Recents.getSystemServices().endProlongedAnimations();
696 }
697 });
698 return true;
699 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800700}