1 /*
<lambda>null2  * 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.systemui.common.domain.interactor
18 
19 import android.os.UserHandle
20 import com.android.systemui.common.data.repository.PackageChangeRepository
21 import com.android.systemui.common.shared.model.PackageChangeModel
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.user.domain.interactor.SelectedUserInteractor
24 import javax.inject.Inject
25 import kotlinx.coroutines.ExperimentalCoroutinesApi
26 import kotlinx.coroutines.flow.Flow
27 import kotlinx.coroutines.flow.filter
28 import kotlinx.coroutines.flow.flatMapLatest
29 
30 /**
31  * Allows listening to package updates. This is recommended over registering broadcasts directly as
32  * it avoids the delay imposed by broadcasts, and provides more structured updates.
33  */
34 @OptIn(ExperimentalCoroutinesApi::class)
35 @SysUISingleton
36 class PackageChangeInteractor
37 @Inject
38 constructor(
39     private val packageChangeRepository: PackageChangeRepository,
40     private val userInteractor: SelectedUserInteractor,
41 ) {
42     /**
43      * Emits values when packages for the specified user are changed. See supported modifications in
44      * [PackageChangeModel]
45      *
46      * @param user The user to listen to. [UserHandle.USER_ALL] may be used to listen to all users.
47      *   [UserHandle.USER_CURRENT] can be used to listen to the currently active user, and
48      *   automatically handles user switching.
49      * @param packageName An optional package name to filter updates by. If not specified, will
50      *   receive updates for all packages.
51      */
52     fun packageChanged(user: UserHandle, packageName: String? = null): Flow<PackageChangeModel> {
53         if (user == UserHandle.CURRENT) {
54             return userInteractor.selectedUser.flatMapLatest { userId ->
55                 packageChangedInternal(UserHandle.of(userId), packageName)
56             }
57         }
58         return packageChangedInternal(user, packageName)
59     }
60 
61     private fun packageChangedInternal(
62         user: UserHandle,
63         packageName: String?,
64     ): Flow<PackageChangeModel> =
65         packageChangeRepository.packageChanged(user).filter { model ->
66             (model.packageName == (packageName ?: model.packageName))
67         }
68 }
69