1 /*
2  * Copyright (C) 2024 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.views
18 
19 import com.android.quickstep.ViewUtils
20 import com.android.quickstep.recents.viewmodel.RecentsViewModel
21 import com.android.systemui.shared.recents.model.ThumbnailData
22 import kotlinx.coroutines.CoroutineName
23 import kotlinx.coroutines.CoroutineScope
24 import kotlinx.coroutines.Dispatchers
25 import kotlinx.coroutines.SupervisorJob
26 import kotlinx.coroutines.cancel
27 import kotlinx.coroutines.launch
28 import kotlinx.coroutines.withContext
29 
30 /** Helper for [RecentsView] to interact with the [RecentsViewModel]. */
31 class RecentsViewModelHelper(private val recentsViewModel: RecentsViewModel) {
32     private lateinit var viewAttachedScope: CoroutineScope
33 
onAttachedToWindownull34     fun onAttachedToWindow() {
35         viewAttachedScope =
36             CoroutineScope(SupervisorJob() + Dispatchers.Default + CoroutineName("RecentsView"))
37     }
38 
onDetachedFromWindownull39     fun onDetachedFromWindow() {
40         viewAttachedScope.cancel("RecentsView detaching from window")
41     }
42 
switchToScreenshotnull43     fun switchToScreenshot(
44         taskView: TaskView,
45         updatedThumbnails: Map<Int, ThumbnailData>?,
46         onFinishRunnable: Runnable,
47     ) {
48         // Update recentsViewModel and apply the thumbnailOverride ASAP, before waiting inside
49         // viewAttachedScope.
50         recentsViewModel.setRunningTaskShowScreenshot(true)
51         viewAttachedScope.launch {
52             recentsViewModel.waitForRunningTaskShowScreenshotToUpdate()
53             recentsViewModel.waitForThumbnailsToUpdate(updatedThumbnails)
54             withContext(Dispatchers.Main) { ViewUtils.postFrameDrawn(taskView, onFinishRunnable) }
55         }
56     }
57 }
58