1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.quickstep.util; 18 19 import android.app.ActivityManager; 20 import android.util.Log; 21 22 import androidx.annotation.NonNull; 23 24 import com.android.systemui.shared.system.TaskStackChangeListener; 25 import com.android.systemui.shared.system.TaskStackChangeListeners; 26 27 /** 28 * This class tracks the failure of a task launch through the Launcher.startActivitySafely() call, 29 * in an edge case in which a task may already be visible on screen (ie. in PIP) and no transition 30 * will be run in WM, which results in expected callbacks to not be processed. 31 * 32 * We transiently register a task stack listener during a task launch and if the restart signal is 33 * received, then the registered callback will be notified. 34 */ 35 public class TaskRestartedDuringLaunchListener implements TaskStackChangeListener { 36 37 private static final String TAG = "TaskRestartedDuringLaunchListener"; 38 39 private @NonNull Runnable mTaskRestartedCallback = null; 40 41 /** 42 * Registers a failure listener callback if it detects a scenario in which an app launch 43 * resulted in an already existing task to be "restarted". 44 */ register(@onNull Runnable taskRestartedCallback)45 public void register(@NonNull Runnable taskRestartedCallback) { 46 TaskStackChangeListeners.getInstance().registerTaskStackListener(this); 47 mTaskRestartedCallback = taskRestartedCallback; 48 } 49 50 /** 51 * Unregisters the failure listener. 52 */ unregister()53 public void unregister() { 54 TaskStackChangeListeners.getInstance().unregisterTaskStackListener(this); 55 mTaskRestartedCallback = null; 56 } 57 58 @Override onActivityRestartAttempt(ActivityManager.RunningTaskInfo task, boolean homeTaskVisible, boolean clearedTask, boolean wasVisible)59 public void onActivityRestartAttempt(ActivityManager.RunningTaskInfo task, 60 boolean homeTaskVisible, boolean clearedTask, boolean wasVisible) { 61 if (wasVisible) { 62 Log.d(TAG, "Detected activity restart during launch for task=" + task.taskId); 63 mTaskRestartedCallback.run(); 64 unregister(); 65 } 66 } 67 } 68