• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.appselector
18 
19 import android.content.ComponentName
20 import android.os.UserHandle
21 import com.android.systemui.flags.FeatureFlags
22 import com.android.systemui.flags.Flags
23 import com.android.systemui.mediaprojection.appselector.data.RecentTask
24 import com.android.systemui.mediaprojection.appselector.data.RecentTaskListProvider
25 import javax.inject.Inject
26 import kotlinx.coroutines.CoroutineScope
27 import kotlinx.coroutines.cancel
28 import kotlinx.coroutines.launch
29 
30 @MediaProjectionAppSelectorScope
31 class MediaProjectionAppSelectorController
32 @Inject
33 constructor(
34     private val recentTaskListProvider: RecentTaskListProvider,
35     private val view: MediaProjectionAppSelectorView,
36     private val flags: FeatureFlags,
37     @HostUserHandle private val hostUserHandle: UserHandle,
38     @MediaProjectionAppSelector private val scope: CoroutineScope,
39     @MediaProjectionAppSelector private val appSelectorComponentName: ComponentName,
40     @MediaProjectionAppSelector private val callerPackageName: String?
41 ) {
42 
initnull43     fun init() {
44         scope.launch {
45             val recentTasks = recentTaskListProvider.loadRecentTasks()
46 
47             val tasks =
48                 recentTasks.filterDevicePolicyRestrictedTasks().filterAppSelector().sortedTasks()
49 
50             view.bind(tasks)
51         }
52     }
53 
destroynull54     fun destroy() {
55         scope.cancel()
56     }
57 
58     /**
59      * Removes all recent tasks that are different from the profile of the host app to avoid any
60      * cross-profile sharing
61      */
Listnull62     private fun List<RecentTask>.filterDevicePolicyRestrictedTasks(): List<RecentTask> =
63         if (flags.isEnabled(Flags.WM_ENABLE_PARTIAL_SCREEN_SHARING_ENTERPRISE_POLICIES)) {
64             // TODO(b/263950746): filter tasks based on the enterprise policies
65             this
66         } else {
<lambda>null67             filter { UserHandle.of(it.userId) == hostUserHandle }
68         }
69 
<lambda>null70     private fun List<RecentTask>.filterAppSelector(): List<RecentTask> = filter {
71         // Only take tasks that is not the app selector
72         it.topActivityComponent != appSelectorComponentName
73     }
74 
<lambda>null75     private fun List<RecentTask>.sortedTasks(): List<RecentTask> = sortedBy {
76         // Show normal tasks first and only then tasks with opened app selector
77         it.topActivityComponent?.packageName == callerPackageName
78     }
79 }
80