blob: 387527f95033a2ae351b52915a1c7d0f23e9c636 [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;
Winsona4fab412016-09-15 16:28:22 -070020import android.app.ActivityManager;
Winson Chungd543c1b2014-06-23 15:06:45 -070021import android.app.ActivityOptions;
Winson2536c7e2015-10-01 15:49:31 -070022import android.app.TaskStackBuilder;
Lucas Dupine17ce522017-07-17 15:45:06 -070023import android.app.WallpaperManager;
Winson Chung47c4c692014-03-17 10:17:11 -070024import android.content.BroadcastReceiver;
25import android.content.Context;
Winson Chung303e1ff2014-03-07 15:06:19 -080026import android.content.Intent;
Winson Chung47c4c692014-03-17 10:17:11 -070027import android.content.IntentFilter;
Winsona1809852016-03-15 11:41:37 -070028import android.content.res.Configuration;
Winson2536c7e2015-10-01 15:49:31 -070029import android.net.Uri;
Winson Chung303e1ff2014-03-07 15:06:19 -080030import android.os.Bundle;
Winson3f32e7e2016-04-20 17:18:08 -070031import android.os.Handler;
Winson Chung15a2ba82014-11-18 11:19:24 -080032import android.os.SystemClock;
Winson Chungd543c1b2014-06-23 15:06:45 -070033import android.os.UserHandle;
Winson2536c7e2015-10-01 15:49:31 -070034import android.provider.Settings;
Winson Chung28217a42017-02-15 13:46:52 -080035import android.provider.Settings.Secure;
Winson1b585612015-11-06 09:16:26 -080036import android.util.Log;
Winson Chung1e8d71b2014-05-16 17:05:22 -070037import android.view.KeyEvent;
Winson Chung303e1ff2014-03-07 15:06:19 -080038import android.view.View;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -080039import android.view.ViewTreeObserver;
Jorim Jaggi3273f312016-05-05 17:32:44 -070040import android.view.ViewTreeObserver.OnPreDrawListener;
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -080041import android.view.WindowManager;
Jorim Jaggic69bd222016-03-15 14:38:37 +010042import android.view.WindowManager.LayoutParams;
Chris Wrenf6e9228b2016-01-26 18:04:35 -050043
Lucas Dupine17ce522017-07-17 15:45:06 -070044import com.android.internal.colorextraction.ColorExtractor;
Winson Chung5c9f4b92015-06-25 16:16:46 -070045import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010046import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Lucas Dupin3ceaa0a2017-06-27 18:17:40 -070047import com.android.keyguard.LatencyTracker;
Jorim Jaggie05256e2016-09-08 14:58:20 -070048import com.android.systemui.DejankUtils;
Lucas Dupine17ce522017-07-17 15:45:06 -070049import com.android.systemui.Dependency;
Winson9b001392016-04-08 14:54:02 -070050import com.android.systemui.Interpolators;
Winson Chung47c4c692014-03-17 10:17:11 -070051import com.android.systemui.R;
Lucas Dupine17ce522017-07-17 15:45:06 -070052import com.android.systemui.colorextraction.SysuiColorExtractor;
Winsonb78bb4f2015-09-24 17:22:57 -070053import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080054import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winsone693aaf2016-03-01 12:05:59 -080055import com.android.systemui.recents.events.activity.ConfigurationChangedEvent;
Winsonb1e71d02015-11-23 12:40:23 -080056import com.android.systemui.recents.events.activity.DebugFlagsChangedEvent;
Winsonef064132016-01-05 12:11:31 -080057import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Jorim Jaggi192086e2016-03-11 17:17:03 +010058import com.android.systemui.recents.events.activity.DockedFirstAnimationFrameEvent;
Jorim Jaggi3273f312016-05-05 17:32:44 -070059import com.android.systemui.recents.events.activity.DockedTopTaskEvent;
Winson3fb67562015-11-11 10:39:03 -080060import com.android.systemui.recents.events.activity.EnterRecentsWindowAnimationCompletedEvent;
Winson1b585612015-11-06 09:16:26 -080061import com.android.systemui.recents.events.activity.EnterRecentsWindowLastAnimationFrameEvent;
Winsonef064132016-01-05 12:11:31 -080062import com.android.systemui.recents.events.activity.ExitRecentsWindowFirstAnimationFrameEvent;
Winson412e1802015-10-20 16:57:57 -070063import com.android.systemui.recents.events.activity.HideRecentsEvent;
Winson0d14d4d2015-10-26 17:05:04 -070064import com.android.systemui.recents.events.activity.IterateRecentsEvent;
Winson83c1b072015-11-09 10:48:04 -080065import com.android.systemui.recents.events.activity.LaunchTaskFailedEvent;
66import com.android.systemui.recents.events.activity.LaunchTaskSucceededEvent;
Winson88737542016-02-17 13:27:33 -080067import com.android.systemui.recents.events.activity.MultiWindowStateChangedEvent;
Winson412e1802015-10-20 16:57:57 -070068import com.android.systemui.recents.events.activity.ToggleRecentsEvent;
Winson Chungb5026902017-05-03 12:45:13 -070069import com.android.systemui.recents.events.component.ActivityUnpinnedEvent;
Winson190fe3bf2015-10-20 14:57:24 -070070import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
71import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
Winson Chunga9c41272017-08-14 16:44:20 -070072import com.android.systemui.recents.events.component.SetWaitingForTransitionStartEvent;
Winson397ae742015-11-20 11:27:33 -080073import com.android.systemui.recents.events.ui.AllTaskViewsDismissedEvent;
Winsonef064132016-01-05 12:11:31 -080074import com.android.systemui.recents.events.ui.DeleteTaskDataEvent;
Winson9b001392016-04-08 14:54:02 -070075import com.android.systemui.recents.events.ui.HideIncompatibleAppOverlayEvent;
Jorim Jaggi11cc01d2016-01-22 19:39:23 -080076import com.android.systemui.recents.events.ui.RecentsDrawnEvent;
Winson2536c7e2015-10-01 15:49:31 -070077import com.android.systemui.recents.events.ui.ShowApplicationInfoEvent;
Winson9b001392016-04-08 14:54:02 -070078import com.android.systemui.recents.events.ui.ShowIncompatibleAppOverlayEvent;
Winsone5f1faa2015-11-20 12:26:23 -080079import com.android.systemui.recents.events.ui.StackViewScrolledEvent;
Lucas Dupinae90ba82017-06-16 16:45:59 -070080import com.android.systemui.recents.events.ui.TaskViewDismissedEvent;
Winsonb1e71d02015-11-23 12:40:23 -080081import com.android.systemui.recents.events.ui.UpdateFreeformTaskViewVisibilityEvent;
Winsone7f138c2015-10-22 16:15:21 -070082import com.android.systemui.recents.events.ui.UserInteractionEvent;
Winson0d14d4d2015-10-26 17:05:04 -070083import com.android.systemui.recents.events.ui.focus.DismissFocusedTaskViewEvent;
84import com.android.systemui.recents.events.ui.focus.FocusNextTaskViewEvent;
85import com.android.systemui.recents.events.ui.focus.FocusPreviousTaskViewEvent;
Jiaquan Hec0e18132017-01-11 14:47:00 -080086import com.android.systemui.recents.events.ui.focus.NavigateTaskViewEvent;
87import com.android.systemui.recents.events.ui.focus.NavigateTaskViewEvent.Direction;
Winson0d14d4d2015-10-26 17:05:04 -070088import com.android.systemui.recents.misc.DozeTrigger;
Winson Chungffa2ec62014-07-03 15:54:42 -070089import com.android.systemui.recents.misc.SystemServicesProxy;
Winson9b001392016-04-08 14:54:02 -070090import com.android.systemui.recents.misc.Utilities;
Winsone6c90732015-09-24 16:06:29 -070091import com.android.systemui.recents.model.RecentsPackageMonitor;
Winson Chunga91c2932014-11-07 15:02:38 -080092import com.android.systemui.recents.model.RecentsTaskLoadPlan;
Winson Chungf1fbd772014-06-24 18:06:58 -070093import com.android.systemui.recents.model.RecentsTaskLoader;
Winson Chungdcfa7972014-07-22 12:27:13 -070094import com.android.systemui.recents.model.Task;
Winson Chung303e1ff2014-03-07 15:06:19 -080095import com.android.systemui.recents.model.TaskStack;
96import com.android.systemui.recents.views.RecentsView;
Winson Chungcdbbb7e2014-06-24 12:11:49 -070097import com.android.systemui.recents.views.SystemBarScrimViews;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050098import com.android.systemui.statusbar.phone.StatusBar;
Winson Chung303e1ff2014-03-07 15:06:19 -080099
Winsond72c3152016-04-05 15:33:35 -0700100import java.io.FileDescriptor;
101import java.io.PrintWriter;
Winsona4fab412016-09-15 16:28:22 -0700102import java.util.List;
Winsond72c3152016-04-05 15:33:35 -0700103
Winson Chung85cfec82014-07-14 14:16:04 -0700104/**
Winson27c28f82016-05-05 16:16:50 -0700105 * The main Recents activity that is started from RecentsComponent.
Winson Chung85cfec82014-07-14 14:16:04 -0700106 */
Lucas Dupine17ce522017-07-17 15:45:06 -0700107public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreDrawListener,
108 ColorExtractor.OnColorsChangedListener {
Winson Chungd42a6cf2014-06-03 16:24:04 -0700109
Winson0d14d4d2015-10-26 17:05:04 -0700110 private final static String TAG = "RecentsActivity";
111 private final static boolean DEBUG = false;
112
Winsone6c90732015-09-24 16:06:29 -0700113 public final static int EVENT_BUS_PRIORITY = Recents.EVENT_BUS_PRIORITY + 1;
Winson9b001392016-04-08 14:54:02 -0700114 public final static int INCOMPATIBLE_APP_ALPHA_DURATION = 150;
Winsone6c90732015-09-24 16:06:29 -0700115
Winsone5f1faa2015-11-20 12:26:23 -0800116 private RecentsPackageMonitor mPackageMonitor;
Winson3f32e7e2016-04-20 17:18:08 -0700117 private Handler mHandler = new Handler();
Winsone5f1faa2015-11-20 12:26:23 -0800118 private long mLastTabKeyEventTime;
119 private boolean mFinishedOnStartup;
120 private boolean mIgnoreAltTabRelease;
Winson88737542016-02-17 13:27:33 -0800121 private boolean mIsVisible;
Lucas Dupin3ceaa0a2017-06-27 18:17:40 -0700122 private Configuration mLastConfig;
Winson Chung85cfec82014-07-14 14:16:04 -0700123
124 // Top level views
Winsone5f1faa2015-11-20 12:26:23 -0800125 private RecentsView mRecentsView;
126 private SystemBarScrimViews mScrimViews;
Winson9b001392016-04-08 14:54:02 -0700127 private View mIncompatibleAppOverlay;
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700128
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700129 // Runnables to finish the Recents activity
Winsone693aaf2016-03-01 12:05:59 -0800130 private Intent mHomeIntent;
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700131
Winson0d14d4d2015-10-26 17:05:04 -0700132 // The trigger to automatically launch the current task
Peter Schillerb124d562015-12-11 21:31:17 -0800133 private int mFocusTimerDuration;
134 private DozeTrigger mIterateTrigger;
Winson4b9cded2016-01-26 16:26:47 -0800135 private final UserInteractionEvent mUserInteractionEvent = new UserInteractionEvent();
Winson0d14d4d2015-10-26 17:05:04 -0700136
Lucas Dupine17ce522017-07-17 15:45:06 -0700137 // Theme and colors
138 private SysuiColorExtractor mColorExtractor;
139 private boolean mUsingDarkText;
140
Winson Chungd543c1b2014-06-23 15:06:45 -0700141 /**
Winson3fb67562015-11-11 10:39:03 -0800142 * A common Runnable to finish Recents by launching Home with an animation depending on the
Peter Schillerb124d562015-12-11 21:31:17 -0800143 * last activity launch state. Generally we always launch home when we exit Recents rather than
Winson3fb67562015-11-11 10:39:03 -0800144 * just finishing the activity since we don't know what is behind Recents in the task stack.
Winson Chungd543c1b2014-06-23 15:06:45 -0700145 */
Winsone693aaf2016-03-01 12:05:59 -0800146 class LaunchHomeRunnable implements Runnable {
Winson49df4202016-01-25 17:33:34 -0800147
Winson Chungd543c1b2014-06-23 15:06:45 -0700148 Intent mLaunchIntent;
Winson49df4202016-01-25 17:33:34 -0800149 ActivityOptions mOpts;
Winson Chungd543c1b2014-06-23 15:06:45 -0700150
Winson Chungd7b2cb12014-06-26 15:08:50 -0700151 /**
Winson3fb67562015-11-11 10:39:03 -0800152 * Creates a finish runnable that starts the specified intent.
Winson Chungd7b2cb12014-06-26 15:08:50 -0700153 */
Winsone693aaf2016-03-01 12:05:59 -0800154 public LaunchHomeRunnable(Intent launchIntent, ActivityOptions opts) {
Winson Chungd543c1b2014-06-23 15:06:45 -0700155 mLaunchIntent = launchIntent;
Winson40a22732016-02-02 18:07:00 -0800156 mOpts = opts;
Winson Chungd543c1b2014-06-23 15:06:45 -0700157 }
158
159 @Override
160 public void run() {
Winson2b9c1d32015-10-05 15:44:23 -0700161 try {
Winson3f32e7e2016-04-20 17:18:08 -0700162 mHandler.post(() -> {
163 ActivityOptions opts = mOpts;
164 if (opts == null) {
165 opts = ActivityOptions.makeCustomAnimation(RecentsActivity.this,
166 R.anim.recents_to_launcher_enter, R.anim.recents_to_launcher_exit);
167 }
168 startActivityAsUser(mLaunchIntent, opts.toBundle(), UserHandle.CURRENT);
169 });
Winson2b9c1d32015-10-05 15:44:23 -0700170 } catch (Exception e) {
Winson1b585612015-11-06 09:16:26 -0800171 Log.e(TAG, getString(R.string.recents_launch_error_message, "Home"), e);
Winson Chungd543c1b2014-06-23 15:06:45 -0700172 }
173 }
174 }
175
Winson Chung85cfec82014-07-14 14:16:04 -0700176 /**
Winson Chung85cfec82014-07-14 14:16:04 -0700177 * Broadcast receiver to handle messages from the system
178 */
Winson Chung31d66c62014-06-30 13:12:54 -0700179 final BroadcastReceiver mSystemBroadcastReceiver = new BroadcastReceiver() {
Winson Chung67369052014-04-07 17:35:48 -0700180 @Override
Winsona4fab412016-09-15 16:28:22 -0700181 public void onReceive(Context ctx, Intent intent) {
Winson Chung31d66c62014-06-30 13:12:54 -0700182 String action = intent.getAction();
Winson Chung85cfec82014-07-14 14:16:04 -0700183 if (action.equals(Intent.ACTION_SCREEN_OFF)) {
184 // When the screen turns off, dismiss Recents to Home
Winson2b9c1d32015-10-05 15:44:23 -0700185 dismissRecentsToHomeIfVisible(false);
Winson Chung36f3f032016-09-08 23:29:43 +0000186 } else if (action.equals(Intent.ACTION_TIME_CHANGED)) {
Winsona4fab412016-09-15 16:28:22 -0700187 // If the time shifts but the currentTime >= lastStackActiveTime, then that boundary
188 // is still valid. Otherwise, we need to reset the lastStackactiveTime to the
189 // currentTime and remove the old tasks in between which would not be previously
190 // visible, but currently would be in the new currentTime
Winson Chung28217a42017-02-15 13:46:52 -0800191 int currentUser = SystemServicesProxy.getInstance(RecentsActivity.this)
192 .getCurrentUser();
193 long oldLastStackActiveTime = Settings.Secure.getLongForUser(getContentResolver(),
194 Secure.OVERVIEW_LAST_STACK_ACTIVE_TIME, -1, currentUser);
Winsona4fab412016-09-15 16:28:22 -0700195 if (oldLastStackActiveTime != -1) {
196 long currentTime = System.currentTimeMillis();
197 if (currentTime < oldLastStackActiveTime) {
198 // We are only removing tasks that are between the new current time
199 // and the old last stack active time, they were not visible and in the
200 // TaskStack so we don't need to remove any associated TaskViews but we do
201 // need to load the task id's from the system
Winson Chungf3cfa892017-04-24 12:23:47 -0700202 RecentsTaskLoader loader = Recents.getTaskLoader();
203 RecentsTaskLoadPlan loadPlan = loader.createLoadPlan(ctx);
204 loader.preloadRawTasks(loadPlan, false /* includeFrontMostExcludedTask */);
Winsona4fab412016-09-15 16:28:22 -0700205 List<ActivityManager.RecentTaskInfo> tasks = loadPlan.getRawTasks();
206 for (int i = tasks.size() - 1; i >= 0; i--) {
207 ActivityManager.RecentTaskInfo task = tasks.get(i);
208 if (currentTime <= task.lastActiveTime && task.lastActiveTime <
209 oldLastStackActiveTime) {
210 Recents.getSystemServices().removeTask(task.persistentId);
211 }
212 }
Jorim Jaggi6b460382017-05-13 23:16:44 +0200213 Recents.getSystemServices().updateOverviewLastStackActiveTimeAsync(
214 currentTime, currentUser);
Winson Chungb5026902017-05-03 12:45:13 -0700215
216 // Clear the last PiP task time, it's an edge case and we'd rather it
217 // not relaunch the PiP task if the user double taps
218 RecentsImpl.clearLastPipTime();
Winsona4fab412016-09-15 16:28:22 -0700219 }
220 }
Winson Chung31d66c62014-06-30 13:12:54 -0700221 }
Winson Chungd42a6cf2014-06-03 16:24:04 -0700222 }
223 };
224
Jorim Jaggi3273f312016-05-05 17:32:44 -0700225 private final OnPreDrawListener mRecentsDrawnEventListener =
226 new ViewTreeObserver.OnPreDrawListener() {
227 @Override
228 public boolean onPreDraw() {
229 mRecentsView.getViewTreeObserver().removeOnPreDrawListener(this);
230 EventBus.getDefault().post(new RecentsDrawnEvent());
Jorim Jaggie05256e2016-09-08 14:58:20 -0700231 if (LatencyTracker.isEnabled(getApplicationContext())) {
232 DejankUtils.postAfterTraversal(() -> LatencyTracker.getInstance(
233 getApplicationContext()).onActionEnd(
234 LatencyTracker.ACTION_TOGGLE_RECENTS));
235 }
Jorim Jaggi44f4bcb2017-05-12 15:40:43 +0200236 DejankUtils.postAfterTraversal(() -> {
237 Recents.getTaskLoader().startLoader(RecentsActivity.this);
238 Recents.getTaskLoader().getHighResThumbnailLoader().setVisible(true);
239 });
Jorim Jaggi3273f312016-05-05 17:32:44 -0700240 return true;
241 }
242 };
243
Winson0d14d4d2015-10-26 17:05:04 -0700244 /**
245 * Dismisses recents if we are already visible and the intent is to toggle the recents view.
246 */
Winson42329522016-02-05 10:39:46 -0800247 boolean dismissRecentsToFocusedTask(int logCategory) {
Winson0d14d4d2015-10-26 17:05:04 -0700248 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700249 if (ssp.isRecentsActivityVisible()) {
Winson0d14d4d2015-10-26 17:05:04 -0700250 // If we have a focused Task, launch that Task now
Winson42329522016-02-05 10:39:46 -0800251 if (mRecentsView.launchFocusedTask(logCategory)) return true;
Winson0d14d4d2015-10-26 17:05:04 -0700252 }
253 return false;
254 }
255
256 /**
Winsona0731a12015-12-02 15:10:14 -0800257 * Dismisses recents back to the launch target task.
258 */
259 boolean dismissRecentsToLaunchTargetTaskOrHome() {
260 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700261 if (ssp.isRecentsActivityVisible()) {
Winsona0731a12015-12-02 15:10:14 -0800262 // If we have a focused Task, launch that Task now
263 if (mRecentsView.launchPreviousTask()) return true;
264 // If none of the other cases apply, then just go Home
Winsonef064132016-01-05 12:11:31 -0800265 dismissRecentsToHome(true /* animateTaskViews */);
Winsona0731a12015-12-02 15:10:14 -0800266 }
267 return false;
268 }
269
270 /**
Winson0d14d4d2015-10-26 17:05:04 -0700271 * Dismisses recents if we are already visible and the intent is to toggle the recents view.
272 */
Winson397ae742015-11-20 11:27:33 -0800273 boolean dismissRecentsToFocusedTaskOrHome() {
Winsone7f138c2015-10-22 16:15:21 -0700274 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700275 if (ssp.isRecentsActivityVisible()) {
Winson Chung85cfec82014-07-14 14:16:04 -0700276 // If we have a focused Task, launch that Task now
Winson42329522016-02-05 10:39:46 -0800277 if (mRecentsView.launchFocusedTask(0 /* logCategory */)) return true;
Winson Chung85cfec82014-07-14 14:16:04 -0700278 // If none of the other cases apply, then just go Home
Winsonef064132016-01-05 12:11:31 -0800279 dismissRecentsToHome(true /* animateTaskViews */);
Winson Chung85cfec82014-07-14 14:16:04 -0700280 return true;
281 }
282 return false;
283 }
284
Winson2b9c1d32015-10-05 15:44:23 -0700285 /**
286 * Dismisses Recents directly to Home without checking whether it is currently visible.
287 */
Winsonef064132016-01-05 12:11:31 -0800288 void dismissRecentsToHome(boolean animateTaskViews) {
Winson49df4202016-01-25 17:33:34 -0800289 dismissRecentsToHome(animateTaskViews, null);
290 }
291
292 /**
293 * Dismisses Recents directly to Home without checking whether it is currently visible.
294 *
295 * @param overrideAnimation If not null, will override the default animation that is based on
296 * how Recents was launched.
297 */
298 void dismissRecentsToHome(boolean animateTaskViews, ActivityOptions overrideAnimation) {
Winsonef064132016-01-05 12:11:31 -0800299 DismissRecentsToHomeAnimationStarted dismissEvent =
300 new DismissRecentsToHomeAnimationStarted(animateTaskViews);
Winsone693aaf2016-03-01 12:05:59 -0800301 dismissEvent.addPostAnimationCallback(new LaunchHomeRunnable(mHomeIntent,
302 overrideAnimation));
Winson3ca10282016-04-21 15:20:21 -0700303 Recents.getSystemServices().sendCloseSystemWindows(
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500304 StatusBar.SYSTEM_DIALOG_REASON_HOME_KEY);
Winsonef064132016-01-05 12:11:31 -0800305 EventBus.getDefault().send(dismissEvent);
Wale Ogunwaled351ada2015-04-04 11:53:14 -0700306 }
307
Winson Chung85cfec82014-07-14 14:16:04 -0700308 /** Dismisses Recents directly to Home if we currently aren't transitioning. */
Winson2b9c1d32015-10-05 15:44:23 -0700309 boolean dismissRecentsToHomeIfVisible(boolean animated) {
Winsone7f138c2015-10-22 16:15:21 -0700310 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700311 if (ssp.isRecentsActivityVisible()) {
Winson Chung85cfec82014-07-14 14:16:04 -0700312 // Return to Home
Winson2b9c1d32015-10-05 15:44:23 -0700313 dismissRecentsToHome(animated);
Winson Chung47c4c692014-03-17 10:17:11 -0700314 return true;
Winson Chung303e1ff2014-03-07 15:06:19 -0800315 }
316 return false;
317 }
318
319 /** Called with the activity is first created. */
320 @Override
321 public void onCreate(Bundle savedInstanceState) {
322 super.onCreate(savedInstanceState);
Winson3150e572015-10-23 15:07:24 -0700323 mFinishedOnStartup = false;
324
325 // In the case that the activity starts up before the Recents component has initialized
326 // (usually when debugging/pushing the SysUI apk), just finish this activity.
327 SystemServicesProxy ssp = Recents.getSystemServices();
328 if (ssp == null) {
329 mFinishedOnStartup = true;
330 finish();
331 return;
332 }
Winsonb78bb4f2015-09-24 17:22:57 -0700333
334 // Register this activity with the event bus
335 EventBus.getDefault().register(this, EVENT_BUS_PRIORITY);
336
Winson008ee15f2016-03-18 17:17:25 -0700337 // Initialize the package monitor
Winsone7f138c2015-10-22 16:15:21 -0700338 mPackageMonitor = new RecentsPackageMonitor();
339 mPackageMonitor.register(this);
Winson Chungf7bca432014-04-30 17:11:13 -0700340
Lucas Dupine17ce522017-07-17 15:45:06 -0700341 // Select theme based on wallpaper colors
342 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
343 mColorExtractor.addOnColorsChangedListener(this);
344 mUsingDarkText = mColorExtractor.getColors(ColorExtractor.TYPE_DARK,
345 WallpaperManager.FLAG_SYSTEM, true).supportsDarkText();
346 setTheme(mUsingDarkText ? R.style.RecentsTheme_Wallpaper_Light
347 : R.style.RecentsTheme_Wallpaper);
348
Winson Chung8e548f72014-06-24 14:40:53 -0700349 // Set the Recents layout
350 setContentView(R.layout.recents);
Winson88737542016-02-17 13:27:33 -0800351 takeKeyEvents(true);
Alan Viverette51efddb2017-04-05 10:00:01 -0400352 mRecentsView = findViewById(R.id.recents_view);
Winson35f30502015-09-28 11:24:36 -0700353 mScrimViews = new SystemBarScrimViews(this);
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800354 getWindow().getAttributes().privateFlags |=
355 WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY;
Winson Chung303e1ff2014-03-07 15:06:19 -0800356
Winson Chung427cec42017-07-17 11:35:39 -0700357 mLastConfig = new Configuration(Utilities.getAppConfiguration(this));
Peter Schillerb124d562015-12-11 21:31:17 -0800358 mFocusTimerDuration = getResources().getInteger(R.integer.recents_auto_advance_duration);
359 mIterateTrigger = new DozeTrigger(mFocusTimerDuration, new Runnable() {
360 @Override
361 public void run() {
Winson42329522016-02-05 10:39:46 -0800362 dismissRecentsToFocusedTask(MetricsEvent.OVERVIEW_SELECT_TIMEOUT);
Peter Schillerb124d562015-12-11 21:31:17 -0800363 }
364 });
365
Winsone8a4eff2016-03-28 16:20:35 -0700366 // Set the window background
367 getWindow().setBackgroundDrawable(mRecentsView.getBackgroundScrim());
368
Winson3fb67562015-11-11 10:39:03 -0800369 // Create the home intent runnable
Winsone693aaf2016-03-01 12:05:59 -0800370 mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
371 mHomeIntent.addCategory(Intent.CATEGORY_HOME);
372 mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Winson3fb67562015-11-11 10:39:03 -0800373 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson3fb67562015-11-11 10:39:03 -0800374
Winson Chung31d66c62014-06-30 13:12:54 -0700375 // Register the broadcast receiver to handle messages when the screen is turned off
376 IntentFilter filter = new IntentFilter();
377 filter.addAction(Intent.ACTION_SCREEN_OFF);
Winson Chung36f3f032016-09-08 23:29:43 +0000378 filter.addAction(Intent.ACTION_TIME_CHANGED);
Winson Chung31d66c62014-06-30 13:12:54 -0700379 registerReceiver(mSystemBroadcastReceiver, filter);
Jorim Jaggic69bd222016-03-15 14:38:37 +0100380
381 getWindow().addPrivateFlags(LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION);
Winsona1ededd2016-03-25 12:23:12 -0700382
383 // Reload the stack view
384 reloadStackView();
Winson Chung521e7dc2014-06-02 15:31:56 -0700385 }
386
Winson Chung303e1ff2014-03-07 15:06:19 -0800387 @Override
Winson Chung303e1ff2014-03-07 15:06:19 -0800388 protected void onStart() {
Winson Chung303e1ff2014-03-07 15:06:19 -0800389 super.onStart();
Winson Chung303e1ff2014-03-07 15:06:19 -0800390
Winsone7f138c2015-10-22 16:15:21 -0700391 // Notify that recents is now visible
Winson88737542016-02-17 13:27:33 -0800392 EventBus.getDefault().send(new RecentsVisibilityChangedEvent(this, true));
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500393 MetricsLogger.visible(this, MetricsEvent.OVERVIEW_ACTIVITY);
Jorim Jaggid6a39492016-04-25 17:31:22 -0700394
Lucas Dupine17ce522017-07-17 15:45:06 -0700395 // Getting system scrim colors ignoring wallpaper visibility since it should never be grey.
396 ColorExtractor.GradientColors systemColors = mColorExtractor.getColors(
397 ColorExtractor.TYPE_DARK, WallpaperManager.FLAG_SYSTEM, true);
398 // We don't want to interpolate colors because we're defining the initial state.
399 // Gradient should be set/ready when you open "Recents".
400 mRecentsView.setScrimColors(systemColors, false);
401
Jorim Jaggid6a39492016-04-25 17:31:22 -0700402 // Notify of the next draw
Jorim Jaggi3273f312016-05-05 17:32:44 -0700403 mRecentsView.getViewTreeObserver().addOnPreDrawListener(mRecentsDrawnEventListener);
Winson Chung67369052014-04-07 17:35:48 -0700404 }
405
406 @Override
Lucas Dupine17ce522017-07-17 15:45:06 -0700407 public void onColorsChanged(ColorExtractor colorExtractor, int which) {
408 if ((which & WallpaperManager.FLAG_SYSTEM) != 0) {
409 // Recents doesn't care about the wallpaper being visible or not, it always
410 // wants to scrim with wallpaper colors
411 ColorExtractor.GradientColors colors = mColorExtractor.getColors(
412 WallpaperManager.FLAG_SYSTEM,
413 ColorExtractor.TYPE_DARK, true /* ignoreVis */);
414 boolean darkText = colors.supportsDarkText();
415 if (darkText != mUsingDarkText) {
416 mUsingDarkText = darkText;
417 setTheme(mUsingDarkText ? R.style.RecentsTheme_Wallpaper_Light
418 : R.style.RecentsTheme_Wallpaper);
419 mRecentsView.reevaluateStyles();
420 }
421 mRecentsView.setScrimColors(colors, true /* animated */);
422 }
423 }
424
425 @Override
Winsona1ededd2016-03-25 12:23:12 -0700426 protected void onNewIntent(Intent intent) {
427 super.onNewIntent(intent);
428
429 // Reload the stack view
430 reloadStackView();
Winson3fb67562015-11-11 10:39:03 -0800431 }
432
Winsona1ededd2016-03-25 12:23:12 -0700433 /**
434 * Reloads the stack views upon launching Recents.
435 */
436 private void reloadStackView() {
Winson Chungb5026902017-05-03 12:45:13 -0700437
Winson88737542016-02-17 13:27:33 -0800438 // If the Recents component has preloaded a load plan, then use that to prevent
439 // reconstructing the task stack
440 RecentsTaskLoader loader = Recents.getTaskLoader();
441 RecentsTaskLoadPlan loadPlan = RecentsImpl.consumeInstanceLoadPlan();
442 if (loadPlan == null) {
443 loadPlan = loader.createLoadPlan(this);
444 }
445
446 // Start loading tasks according to the load plan
447 RecentsConfiguration config = Recents.getConfiguration();
448 RecentsActivityLaunchState launchState = config.getLaunchState();
449 if (!loadPlan.hasTasks()) {
Winson96e61342016-03-15 16:47:19 -0700450 loader.preloadTasks(loadPlan, launchState.launchedToTaskId,
Yoshinori Hirano7bccd432016-11-21 15:42:20 +0900451 !launchState.launchedFromHome && !launchState.launchedViaDockGesture);
Winson88737542016-02-17 13:27:33 -0800452 }
453
454 RecentsTaskLoadPlan.Options loadOpts = new RecentsTaskLoadPlan.Options();
455 loadOpts.runningTaskId = launchState.launchedToTaskId;
456 loadOpts.numVisibleTasks = launchState.launchedNumVisibleTasks;
457 loadOpts.numVisibleTaskThumbnails = launchState.launchedNumVisibleThumbnails;
458 loader.loadTasks(this, loadPlan, loadOpts);
459 TaskStack stack = loadPlan.getTaskStack();
Lucas Dupinae90ba82017-06-16 16:45:59 -0700460 mRecentsView.onReload(stack, mIsVisible);
Winson88737542016-02-17 13:27:33 -0800461
Winsona1ededd2016-03-25 12:23:12 -0700462 // Update the nav bar scrim, but defer the animation until the enter-window event
Winsonc69249f2016-03-28 13:38:39 -0700463 boolean animateNavBarScrim = !launchState.launchedViaDockGesture;
Winson500ba752016-03-30 11:59:22 -0700464 mScrimViews.updateNavBarScrim(animateNavBarScrim, stack.getTaskCount() > 0, null);
Winson88737542016-02-17 13:27:33 -0800465
Winsona1ededd2016-03-25 12:23:12 -0700466 // If this is a new instance relaunched by AM, without going through the normal mechanisms,
467 // then we have to manually trigger the enter animation state
Winson88737542016-02-17 13:27:33 -0800468 boolean wasLaunchedByAm = !launchState.launchedFromHome &&
469 !launchState.launchedFromApp;
Winsona1ededd2016-03-25 12:23:12 -0700470 if (wasLaunchedByAm) {
Winson88737542016-02-17 13:27:33 -0800471 EventBus.getDefault().send(new EnterRecentsWindowAnimationCompletedEvent());
472 }
473
Winson88737542016-02-17 13:27:33 -0800474 // Keep track of whether we launched from the nav bar button or via alt-tab
475 if (launchState.launchedWithAltTab) {
476 MetricsLogger.count(this, "overview_trigger_alttab", 1);
477 } else {
478 MetricsLogger.count(this, "overview_trigger_nav_btn", 1);
479 }
480
481 // Keep track of whether we launched from an app or from home
482 if (launchState.launchedFromApp) {
Winsona1ededd2016-03-25 12:23:12 -0700483 Task launchTarget = stack.getLaunchTarget();
484 int launchTaskIndexInStack = launchTarget != null
485 ? stack.indexOfStackTask(launchTarget)
486 : 0;
Winson88737542016-02-17 13:27:33 -0800487 MetricsLogger.count(this, "overview_source_app", 1);
488 // If from an app, track the stack index of the app in the stack (for affiliated tasks)
489 MetricsLogger.histogram(this, "overview_source_app_index", launchTaskIndexInStack);
490 } else {
491 MetricsLogger.count(this, "overview_source_home", 1);
492 }
493
494 // Keep track of the total stack task count
Winsona1ededd2016-03-25 12:23:12 -0700495 int taskCount = mRecentsView.getStack().getTaskCount();
Winson88737542016-02-17 13:27:33 -0800496 MetricsLogger.histogram(this, "overview_task_count", taskCount);
497
498 // After we have resumed, set the visible state until the next onStop() call
499 mIsVisible = true;
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700500 }
501
502 @Override
Winsona1ededd2016-03-25 12:23:12 -0700503 public void onEnterAnimationComplete() {
504 super.onEnterAnimationComplete();
Winson Chunged0e2b42017-05-18 23:12:27 -0700505 EventBus.getDefault().send(new EnterRecentsWindowAnimationCompletedEvent());
Winson Chunga9c41272017-08-14 16:44:20 -0700506
507 // Workaround for b/64694148: The animation started callback is not made (see
508 // RecentsImpl.getThumbnailTransitionActivityOptions) so reset the transition-waiting state
509 // once the enter animation has completed.
510 EventBus.getDefault().send(new SetWaitingForTransitionStartEvent(false));
Winsona1ededd2016-03-25 12:23:12 -0700511 }
512
513 @Override
Winson88737542016-02-17 13:27:33 -0800514 protected void onPause() {
515 super.onPause();
Winson190fe3bf2015-10-20 14:57:24 -0700516
Winsone5f1faa2015-11-20 12:26:23 -0800517 mIgnoreAltTabRelease = false;
Winson88737542016-02-17 13:27:33 -0800518 mIterateTrigger.stopDozing();
Winson88737542016-02-17 13:27:33 -0800519 }
Winsone7f138c2015-10-22 16:15:21 -0700520
Winson88737542016-02-17 13:27:33 -0800521 @Override
Winsona1809852016-03-15 11:41:37 -0700522 public void onConfigurationChanged(Configuration newConfig) {
523 super.onConfigurationChanged(newConfig);
524
Winson44849b82016-03-29 10:45:45 -0700525 // Notify of the config change
Jorim Jaggi25160db2016-04-18 16:03:36 -0700526 Configuration newDeviceConfiguration = Utilities.getAppConfiguration(this);
Winson500ba752016-03-30 11:59:22 -0700527 int numStackTasks = mRecentsView.getStack().getStackTaskCount();
Winson44849b82016-03-29 10:45:45 -0700528 EventBus.getDefault().send(new ConfigurationChangedEvent(false /* fromMultiWindow */,
Lucas Dupin3ceaa0a2017-06-27 18:17:40 -0700529 mLastConfig.orientation != newDeviceConfiguration.orientation,
530 mLastConfig.densityDpi != newDeviceConfiguration.densityDpi, numStackTasks > 0));
531
Lucas Dupine17ce522017-07-17 15:45:06 -0700532 mLastConfig.updateFrom(newDeviceConfiguration);
Winsona1809852016-03-15 11:41:37 -0700533 }
534
535 @Override
Andrii Kulian933076d2016-03-29 17:04:42 -0700536 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
537 super.onMultiWindowModeChanged(isInMultiWindowMode);
Winson500ba752016-03-30 11:59:22 -0700538
Winson Chungb5026902017-05-03 12:45:13 -0700539 reloadTaskStack(isInMultiWindowMode, true /* sendConfigChangedEvent */);
Winsona1ededd2016-03-25 12:23:12 -0700540 }
541
542 @Override
Winson88737542016-02-17 13:27:33 -0800543 protected void onStop() {
544 super.onStop();
545
Winson88737542016-02-17 13:27:33 -0800546 // Notify that recents is now hidden
547 mIsVisible = false;
548 EventBus.getDefault().send(new RecentsVisibilityChangedEvent(this, false));
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500549 MetricsLogger.hidden(this, MetricsEvent.OVERVIEW_ACTIVITY);
Jorim Jaggi6f9dbcb2017-03-17 17:22:47 +0100550 Recents.getTaskLoader().getHighResThumbnailLoader().setVisible(false);
Winson73492c52016-03-09 17:44:54 -0800551
Yoshinori Hirano7bccd432016-11-21 15:42:20 +0900552 if (!isChangingConfigurations()) {
553 // Workaround for b/22542869, if the RecentsActivity is started again, but without going
554 // through SystemUI, we need to reset the config launch flags to ensure that we do not
555 // wait on the system to send a signal that was never queued.
556 RecentsConfiguration config = Recents.getConfiguration();
557 RecentsActivityLaunchState launchState = config.getLaunchState();
558 launchState.reset();
559 }
Winson Chung69a9c172017-06-06 14:01:29 -0700560
561 // Force a gc to attempt to clean up bitmap references more quickly (b/38258699)
562 Recents.getSystemServices().gc();
Winson Chung47c4c692014-03-17 10:17:11 -0700563 }
564
565 @Override
566 protected void onDestroy() {
Winson Chung47c4c692014-03-17 10:17:11 -0700567 super.onDestroy();
Winson Chung31d66c62014-06-30 13:12:54 -0700568
Winson3150e572015-10-23 15:07:24 -0700569 // In the case that the activity finished on startup, just skip the unregistration below
570 if (mFinishedOnStartup) {
571 return;
572 }
573
Winson Chung85cfec82014-07-14 14:16:04 -0700574 // Unregister the system broadcast receivers
Winson Chung31d66c62014-06-30 13:12:54 -0700575 unregisterReceiver(mSystemBroadcastReceiver);
Winson Chung905950a2014-09-17 09:25:27 +0200576
Winsone7f138c2015-10-22 16:15:21 -0700577 // Unregister any broadcast receivers for the task loader
578 mPackageMonitor.unregister();
579
Winsonb78bb4f2015-09-24 17:22:57 -0700580 EventBus.getDefault().unregister(this);
Winson Chung303e1ff2014-03-07 15:06:19 -0800581 }
582
Winson412e1802015-10-20 16:57:57 -0700583 @Override
584 public void onAttachedToWindow() {
585 super.onAttachedToWindow();
586 EventBus.getDefault().register(mScrimViews, EVENT_BUS_PRIORITY);
587 }
Winson Chungaf3bb692015-06-03 17:31:39 -0700588
Winson412e1802015-10-20 16:57:57 -0700589 @Override
590 public void onDetachedFromWindow() {
591 super.onDetachedFromWindow();
592 EventBus.getDefault().unregister(mScrimViews);
Winson Chung353c0b92014-10-16 17:43:23 -0700593 }
594
595 @Override
Winson Chung4d7b0922014-03-13 17:14:17 -0700596 public void onTrimMemory(int level) {
Winsone7f138c2015-10-22 16:15:21 -0700597 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson Chung4d7b0922014-03-13 17:14:17 -0700598 if (loader != null) {
599 loader.onTrimMemory(level);
600 }
601 }
602
603 @Override
Winson Chung1e8d71b2014-05-16 17:05:22 -0700604 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chunga0e88b52014-08-11 19:25:42 -0700605 switch (keyCode) {
606 case KeyEvent.KEYCODE_TAB: {
Winson35f30502015-09-28 11:24:36 -0700607 int altTabKeyDelay = getResources().getInteger(R.integer.recents_alt_tab_key_delay);
Winson Chung15a2ba82014-11-18 11:19:24 -0800608 boolean hasRepKeyTimeElapsed = (SystemClock.elapsedRealtime() -
Winson35f30502015-09-28 11:24:36 -0700609 mLastTabKeyEventTime) > altTabKeyDelay;
Winson Chunga0e88b52014-08-11 19:25:42 -0700610 if (event.getRepeatCount() <= 0 || hasRepKeyTimeElapsed) {
611 // Focus the next task in the stack
612 final boolean backward = event.isShiftPressed();
Winson0d14d4d2015-10-26 17:05:04 -0700613 if (backward) {
614 EventBus.getDefault().send(new FocusPreviousTaskViewEvent());
615 } else {
Peter Schillerb124d562015-12-11 21:31:17 -0800616 EventBus.getDefault().send(
Winson4b9cded2016-01-26 16:26:47 -0800617 new FocusNextTaskViewEvent(0 /* timerIndicatorDuration */));
Winson0d14d4d2015-10-26 17:05:04 -0700618 }
Winson Chung15a2ba82014-11-18 11:19:24 -0800619 mLastTabKeyEventTime = SystemClock.elapsedRealtime();
Winsone5f1faa2015-11-20 12:26:23 -0800620
621 // In the case of another ALT event, don't ignore the next release
622 if (event.isAltPressed()) {
623 mIgnoreAltTabRelease = false;
624 }
Winson Chunga0e88b52014-08-11 19:25:42 -0700625 }
626 return true;
627 }
Jiaquan Hec0e18132017-01-11 14:47:00 -0800628 case KeyEvent.KEYCODE_DPAD_UP:
629 case KeyEvent.KEYCODE_DPAD_DOWN:
630 case KeyEvent.KEYCODE_DPAD_LEFT:
631 case KeyEvent.KEYCODE_DPAD_RIGHT: {
632 final Direction direction = NavigateTaskViewEvent.getDirectionFromKeyCode(keyCode);
633 EventBus.getDefault().send(new NavigateTaskViewEvent(direction));
Winson Chunga0e88b52014-08-11 19:25:42 -0700634 return true;
635 }
636 case KeyEvent.KEYCODE_DEL:
637 case KeyEvent.KEYCODE_FORWARD_DEL: {
Winsondf3012b2016-01-12 15:35:05 -0800638 if (event.getRepeatCount() <= 0) {
639 EventBus.getDefault().send(new DismissFocusedTaskViewEvent());
Winson0d14d4d2015-10-26 17:05:04 -0700640
Winsondf3012b2016-01-12 15:35:05 -0800641 // Keep track of deletions by keyboard
642 MetricsLogger.histogram(this, "overview_task_dismissed_source",
643 Constants.Metrics.DismissSourceKeyboard);
644 return true;
645 }
Winson Chunga0e88b52014-08-11 19:25:42 -0700646 }
647 default:
648 break;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700649 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700650 return super.onKeyDown(keyCode, event);
651 }
652
653 @Override
Winson Chunga26fb782014-06-12 17:52:39 -0700654 public void onUserInteraction() {
Winson4b9cded2016-01-26 16:26:47 -0800655 EventBus.getDefault().send(mUserInteractionEvent);
Winson Chunga26fb782014-06-12 17:52:39 -0700656 }
657
658 @Override
Winson Chung303e1ff2014-03-07 15:06:19 -0800659 public void onBackPressed() {
Winsona0731a12015-12-02 15:10:14 -0800660 // Back behaves like the recents button so just trigger a toggle event
661 EventBus.getDefault().send(new ToggleRecentsEvent());
Winson Chung303e1ff2014-03-07 15:06:19 -0800662 }
Winson Chung47c4c692014-03-17 10:17:11 -0700663
Winsonb78bb4f2015-09-24 17:22:57 -0700664 /**** EventBus events ****/
Winson Chungd543c1b2014-06-23 15:06:45 -0700665
Winson412e1802015-10-20 16:57:57 -0700666 public final void onBusEvent(ToggleRecentsEvent event) {
Winson8f6ee482016-03-18 17:51:48 -0700667 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
668 if (launchState.launchedFromHome) {
669 dismissRecentsToHome(true /* animateTaskViews */);
670 } else {
671 dismissRecentsToLaunchTargetTaskOrHome();
Winsonc29ff002015-11-20 16:00:45 -0800672 }
Winson412e1802015-10-20 16:57:57 -0700673 }
674
Winson0d14d4d2015-10-26 17:05:04 -0700675 public final void onBusEvent(IterateRecentsEvent event) {
Winson8f6ee482016-03-18 17:51:48 -0700676 final RecentsDebugFlags debugFlags = Recents.getDebugFlags();
Peter Schillerb124d562015-12-11 21:31:17 -0800677
Winson8f6ee482016-03-18 17:51:48 -0700678 // Start dozing after the recents button is clicked
679 int timerIndicatorDuration = 0;
680 if (debugFlags.isFastToggleRecentsEnabled()) {
681 timerIndicatorDuration = getResources().getInteger(
682 R.integer.recents_subsequent_auto_advance_duration);
Winson4b9cded2016-01-26 16:26:47 -0800683
Winson8f6ee482016-03-18 17:51:48 -0700684 mIterateTrigger.setDozeDuration(timerIndicatorDuration);
685 if (!mIterateTrigger.isDozing()) {
686 mIterateTrigger.startDozing();
687 } else {
688 mIterateTrigger.poke();
Winson8b1871d2015-11-20 09:56:20 -0800689 }
690 }
Winson8f6ee482016-03-18 17:51:48 -0700691
692 // Focus the next task
693 EventBus.getDefault().send(new FocusNextTaskViewEvent(timerIndicatorDuration));
694
695 MetricsLogger.action(this, MetricsEvent.ACTION_OVERVIEW_PAGE);
Winson0d14d4d2015-10-26 17:05:04 -0700696 }
697
698 public final void onBusEvent(UserInteractionEvent event) {
Winsonb61e6542016-02-04 14:37:18 -0800699 // Stop the fast-toggle dozer
Winson0d14d4d2015-10-26 17:05:04 -0700700 mIterateTrigger.stopDozing();
701 }
702
Winson412e1802015-10-20 16:57:57 -0700703 public final void onBusEvent(HideRecentsEvent event) {
704 if (event.triggeredFromAltTab) {
705 // If we are hiding from releasing Alt-Tab, dismiss Recents to the focused app
Winsone5f1faa2015-11-20 12:26:23 -0800706 if (!mIgnoreAltTabRelease) {
707 dismissRecentsToFocusedTaskOrHome();
708 }
Winson412e1802015-10-20 16:57:57 -0700709 } else if (event.triggeredFromHomeKey) {
Winson8f6ee482016-03-18 17:51:48 -0700710 dismissRecentsToHome(true /* animateTaskViews */);
Winson4b9cded2016-01-26 16:26:47 -0800711
712 // Cancel any pending dozes
713 EventBus.getDefault().send(mUserInteractionEvent);
Winson412e1802015-10-20 16:57:57 -0700714 } else {
715 // Do nothing
716 }
717 }
718
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700719 public final void onBusEvent(EnterRecentsWindowLastAnimationFrameEvent event) {
Winsonb1e71d02015-11-23 12:40:23 -0800720 EventBus.getDefault().send(new UpdateFreeformTaskViewVisibilityEvent(true));
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800721 mRecentsView.getViewTreeObserver().addOnPreDrawListener(this);
Winsonb1e71d02015-11-23 12:40:23 -0800722 mRecentsView.invalidate();
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700723 }
724
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800725 public final void onBusEvent(ExitRecentsWindowFirstAnimationFrameEvent event) {
Winsonb1e71d02015-11-23 12:40:23 -0800726 if (mRecentsView.isLastTaskLaunchedFreeform()) {
727 EventBus.getDefault().send(new UpdateFreeformTaskViewVisibilityEvent(false));
728 }
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800729 mRecentsView.getViewTreeObserver().addOnPreDrawListener(this);
Winsonb1e71d02015-11-23 12:40:23 -0800730 mRecentsView.invalidate();
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800731 }
732
Jorim Jaggi192086e2016-03-11 17:17:03 +0100733 public final void onBusEvent(DockedFirstAnimationFrameEvent event) {
734 mRecentsView.getViewTreeObserver().addOnPreDrawListener(this);
735 mRecentsView.invalidate();
736 }
737
Winson13d30662015-11-06 15:30:29 -0800738 public final void onBusEvent(CancelEnterRecentsWindowAnimationEvent event) {
739 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
740 int launchToTaskId = launchState.launchedToTaskId;
741 if (launchToTaskId != -1 &&
742 (event.launchTask == null || launchToTaskId != event.launchTask.key.id)) {
743 SystemServicesProxy ssp = Recents.getSystemServices();
744 ssp.cancelWindowTransition(launchState.launchedToTaskId);
745 ssp.cancelThumbnailTransition(getTaskId());
746 }
747 }
748
Winson2536c7e2015-10-01 15:49:31 -0700749 public final void onBusEvent(ShowApplicationInfoEvent event) {
750 // Create a new task stack with the application info details activity
Winson2536c7e2015-10-01 15:49:31 -0700751 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Winsone7f138c2015-10-22 16:15:21 -0700752 Uri.fromParts("package", event.task.key.getComponent().getPackageName(), null));
Winson2536c7e2015-10-01 15:49:31 -0700753 intent.setComponent(intent.resolveActivity(getPackageManager()));
754 TaskStackBuilder.create(this)
755 .addNextIntentWithParentStack(intent).startActivities(null,
Peter Schillerb124d562015-12-11 21:31:17 -0800756 new UserHandle(event.task.key.userId));
Winson2536c7e2015-10-01 15:49:31 -0700757
758 // Keep track of app-info invocations
759 MetricsLogger.count(this, "overview_app_info", 1);
760 }
761
Winson9b001392016-04-08 14:54:02 -0700762 public final void onBusEvent(ShowIncompatibleAppOverlayEvent event) {
763 if (mIncompatibleAppOverlay == null) {
764 mIncompatibleAppOverlay = Utilities.findViewStubById(this,
765 R.id.incompatible_app_overlay_stub).inflate();
766 mIncompatibleAppOverlay.setWillNotDraw(false);
767 mIncompatibleAppOverlay.setVisibility(View.VISIBLE);
768 }
769 mIncompatibleAppOverlay.animate()
770 .alpha(1f)
771 .setDuration(INCOMPATIBLE_APP_ALPHA_DURATION)
772 .setInterpolator(Interpolators.ALPHA_IN)
773 .start();
774 }
775
776 public final void onBusEvent(HideIncompatibleAppOverlayEvent event) {
777 if (mIncompatibleAppOverlay != null) {
778 mIncompatibleAppOverlay.animate()
779 .alpha(0f)
780 .setDuration(INCOMPATIBLE_APP_ALPHA_DURATION)
781 .setInterpolator(Interpolators.ALPHA_OUT)
782 .start();
783 }
784 }
785
Winsonef064132016-01-05 12:11:31 -0800786 public final void onBusEvent(DeleteTaskDataEvent event) {
Winson2536c7e2015-10-01 15:49:31 -0700787 // Remove any stored data from the loader
Winsone7f138c2015-10-22 16:15:21 -0700788 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson2536c7e2015-10-01 15:49:31 -0700789 loader.deleteTaskData(event.task, false);
790
791 // Remove the task from activity manager
Winsone7f138c2015-10-22 16:15:21 -0700792 SystemServicesProxy ssp = Recents.getSystemServices();
793 ssp.removeTask(event.task.key.id);
Winson2536c7e2015-10-01 15:49:31 -0700794 }
795
Lucas Dupinae90ba82017-06-16 16:45:59 -0700796 public final void onBusEvent(TaskViewDismissedEvent event) {
797 mRecentsView.updateScrimOpacity();
798 }
799
Winson397ae742015-11-20 11:27:33 -0800800 public final void onBusEvent(AllTaskViewsDismissedEvent event) {
Winson Chung9a742902015-12-11 10:25:40 -0500801 SystemServicesProxy ssp = Recents.getSystemServices();
802 if (ssp.hasDockedTask()) {
Winson20684082016-03-16 17:13:34 -0700803 mRecentsView.showEmptyView(event.msgResId);
Winson Chung9a742902015-12-11 10:25:40 -0500804 } else {
805 // Just go straight home (no animation necessary because there are no more task views)
Winsonef064132016-01-05 12:11:31 -0800806 dismissRecentsToHome(false /* animateTaskViews */);
Winson Chung9a742902015-12-11 10:25:40 -0500807 }
Winson397ae742015-11-20 11:27:33 -0800808
809 // Keep track of all-deletions
810 MetricsLogger.count(this, "overview_task_all_dismissed", 1);
811 }
812
Winson83c1b072015-11-09 10:48:04 -0800813 public final void onBusEvent(LaunchTaskSucceededEvent event) {
814 MetricsLogger.histogram(this, "overview_task_launch_index", event.taskIndexFromStackFront);
815 }
816
817 public final void onBusEvent(LaunchTaskFailedEvent event) {
818 // Return to Home
Winsonef064132016-01-05 12:11:31 -0800819 dismissRecentsToHome(true /* animateTaskViews */);
Winson83c1b072015-11-09 10:48:04 -0800820
821 MetricsLogger.count(this, "overview_task_launch_failed", 1);
822 }
823
Winson412e1802015-10-20 16:57:57 -0700824 public final void onBusEvent(ScreenPinningRequestEvent event) {
825 MetricsLogger.count(this, "overview_screen_pinned", 1);
826 }
827
Winsonc742f972015-11-12 11:32:21 -0800828 public final void onBusEvent(DebugFlagsChangedEvent event) {
829 // Just finish recents so that we can reload the flags anew on the next instantiation
830 finish();
831 }
832
Winsone5f1faa2015-11-20 12:26:23 -0800833 public final void onBusEvent(StackViewScrolledEvent event) {
834 // Once the user has scrolled while holding alt-tab, then we should ignore the release of
835 // the key
836 mIgnoreAltTabRelease = true;
837 }
838
Jorim Jaggi3273f312016-05-05 17:32:44 -0700839 public final void onBusEvent(final DockedTopTaskEvent event) {
840 mRecentsView.getViewTreeObserver().addOnPreDrawListener(mRecentsDrawnEventListener);
841 mRecentsView.invalidate();
842 }
843
Winson Chungb5026902017-05-03 12:45:13 -0700844 public final void onBusEvent(final ActivityUnpinnedEvent event) {
845 if (mIsVisible) {
846 // Skip the configuration change event as the PiP activity does not actually affect the
847 // config of recents
848 reloadTaskStack(isInMultiWindowMode(), false /* sendConfigChangedEvent */);
849 }
850 }
851
852 private void reloadTaskStack(boolean isInMultiWindowMode, boolean sendConfigChangedEvent) {
853 // Reload the task stack completely
854 RecentsConfiguration config = Recents.getConfiguration();
855 RecentsActivityLaunchState launchState = config.getLaunchState();
856 RecentsTaskLoader loader = Recents.getTaskLoader();
857 RecentsTaskLoadPlan loadPlan = loader.createLoadPlan(this);
858 loader.preloadTasks(loadPlan, -1 /* runningTaskId */,
859 false /* includeFrontMostExcludedTask */);
860
861 RecentsTaskLoadPlan.Options loadOpts = new RecentsTaskLoadPlan.Options();
862 loadOpts.numVisibleTasks = launchState.launchedNumVisibleTasks;
863 loadOpts.numVisibleTaskThumbnails = launchState.launchedNumVisibleThumbnails;
864 loader.loadTasks(this, loadPlan, loadOpts);
865
866 TaskStack stack = loadPlan.getTaskStack();
867 int numStackTasks = stack.getStackTaskCount();
868 boolean showDeferredAnimation = numStackTasks > 0;
869
870 if (sendConfigChangedEvent) {
871 EventBus.getDefault().send(new ConfigurationChangedEvent(true /* fromMultiWindow */,
872 false /* fromDeviceOrientationChange */, false /* fromDisplayDensityChange */,
873 numStackTasks > 0));
874 }
875 EventBus.getDefault().send(new MultiWindowStateChangedEvent(isInMultiWindowMode,
876 showDeferredAnimation, stack));
877 }
878
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800879 @Override
880 public boolean onPreDraw() {
881 mRecentsView.getViewTreeObserver().removeOnPreDrawListener(this);
882 // We post to make sure that this information is delivered after this traversals is
883 // finished.
884 mRecentsView.post(new Runnable() {
885 @Override
886 public void run() {
887 Recents.getSystemServices().endProlongedAnimations();
888 }
889 });
890 return true;
891 }
Winsond72c3152016-04-05 15:33:35 -0700892
893 @Override
894 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
895 super.dump(prefix, fd, writer, args);
Winson67c79572016-04-13 14:02:18 -0700896 EventBus.getDefault().dump(prefix, writer);
Winson29a763b2016-04-20 10:50:40 -0700897 Recents.getTaskLoader().dump(prefix, writer);
Winson67c79572016-04-13 14:02:18 -0700898
Winsond72c3152016-04-05 15:33:35 -0700899 String id = Integer.toHexString(System.identityHashCode(this));
Winson Chung28217a42017-02-15 13:46:52 -0800900 long lastStackActiveTime = Settings.Secure.getLongForUser(getContentResolver(),
901 Secure.OVERVIEW_LAST_STACK_ACTIVE_TIME, -1,
902 SystemServicesProxy.getInstance(this).getCurrentUser());
Winsond72c3152016-04-05 15:33:35 -0700903
904 writer.print(prefix); writer.print(TAG);
905 writer.print(" visible="); writer.print(mIsVisible ? "Y" : "N");
Winson Chung36f3f032016-09-08 23:29:43 +0000906 writer.print(" lastStackTaskActiveTime="); writer.print(lastStackActiveTime);
907 writer.print(" currentTime="); writer.print(System.currentTimeMillis());
Winsond72c3152016-04-05 15:33:35 -0700908 writer.print(" [0x"); writer.print(id); writer.print("]");
909 writer.println();
910
911 if (mRecentsView != null) {
912 mRecentsView.dump(prefix, writer);
913 }
Winsond72c3152016-04-05 15:33:35 -0700914 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800915}