1 /* <lambda>null2 * 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.systemui.mediaprojection.taskswitcher.data.repository 18 19 import android.app.ActivityManager.RunningTaskInfo 20 import android.app.ActivityOptions 21 import android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED 22 import android.app.ActivityTaskManager 23 import android.app.IActivityTaskManager 24 import android.app.TaskStackListener 25 import android.os.IBinder 26 import android.util.Log 27 import android.view.Display 28 import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging 29 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow 30 import com.android.systemui.dagger.SysUISingleton 31 import com.android.systemui.dagger.qualifiers.Application 32 import com.android.systemui.dagger.qualifiers.Background 33 import javax.inject.Inject 34 import kotlinx.coroutines.CoroutineDispatcher 35 import kotlinx.coroutines.CoroutineScope 36 import kotlinx.coroutines.channels.awaitClose 37 import kotlinx.coroutines.flow.Flow 38 import kotlinx.coroutines.flow.SharingStarted 39 import kotlinx.coroutines.flow.shareIn 40 import kotlinx.coroutines.withContext 41 42 /** Implementation of [TasksRepository] that uses [ActivityTaskManager] as the data source. */ 43 @SysUISingleton 44 class ActivityTaskManagerTasksRepository 45 @Inject 46 constructor( 47 private val activityTaskManager: IActivityTaskManager, 48 @Application private val applicationScope: CoroutineScope, 49 @Background private val backgroundDispatcher: CoroutineDispatcher, 50 ) : TasksRepository { 51 52 override suspend fun launchRecentTask(taskInfo: RunningTaskInfo) { 53 withContext(backgroundDispatcher) { 54 val activityOptions = ActivityOptions.makeBasic() 55 activityOptions.pendingIntentBackgroundActivityStartMode = 56 MODE_BACKGROUND_ACTIVITY_START_ALLOWED 57 activityOptions.launchDisplayId = taskInfo.displayId 58 activityTaskManager.startActivityFromRecents( 59 taskInfo.taskId, 60 activityOptions.toBundle() 61 ) 62 } 63 } 64 65 override suspend fun findRunningTaskFromWindowContainerToken( 66 windowContainerToken: IBinder 67 ): RunningTaskInfo? = 68 getRunningTasks().firstOrNull { taskInfo -> 69 taskInfo.token.asBinder() == windowContainerToken 70 } 71 72 private suspend fun getRunningTasks(): List<RunningTaskInfo> = 73 withContext(backgroundDispatcher) { 74 activityTaskManager.getTasks( 75 /* maxNum = */ Integer.MAX_VALUE, 76 /* filterForVisibleRecents = */ false, 77 /* keepIntentExtra = */ false, 78 /* displayId = */ Display.INVALID_DISPLAY 79 ) 80 } 81 82 override val foregroundTask: Flow<RunningTaskInfo> = 83 conflatedCallbackFlow { 84 val listener = 85 object : TaskStackListener() { 86 override fun onTaskMovedToFront(taskInfo: RunningTaskInfo) { 87 Log.d(TAG, "onTaskMovedToFront: $taskInfo") 88 trySendWithFailureLogging(taskInfo, TAG) 89 } 90 } 91 activityTaskManager.registerTaskStackListener(listener) 92 awaitClose { activityTaskManager.unregisterTaskStackListener(listener) } 93 } 94 .shareIn(applicationScope, SharingStarted.Lazily, replay = 1) 95 96 companion object { 97 private const val TAG = "TasksRepository" 98 } 99 } 100