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.systemui.common.data.repository
18 
19 import android.os.UserHandle
20 import com.android.systemui.common.shared.model.PackageChangeModel
21 import com.android.systemui.common.shared.model.PackageInstallSession
22 import com.android.systemui.util.time.SystemClock
23 import kotlinx.coroutines.flow.Flow
24 import kotlinx.coroutines.flow.MutableSharedFlow
25 import kotlinx.coroutines.flow.MutableStateFlow
26 import kotlinx.coroutines.flow.asStateFlow
27 import kotlinx.coroutines.flow.filter
28 
29 class FakePackageChangeRepository(private val systemClock: SystemClock) : PackageChangeRepository {
30 
31     private var _packageChanged = MutableSharedFlow<PackageChangeModel>()
32 
packageChangednull33     override fun packageChanged(user: UserHandle) =
34         _packageChanged.filter {
35             user == UserHandle.ALL || user == UserHandle.getUserHandleForUid(it.packageUid)
36         }
37 
38     private val _packageInstallSessions = MutableStateFlow<List<PackageInstallSession>>(emptyList())
39 
40     override val packageInstallSessionsForPrimaryUser: Flow<List<PackageInstallSession>> =
41         _packageInstallSessions.asStateFlow()
42 
setInstallSessionsnull43     fun setInstallSessions(sessions: List<PackageInstallSession>) {
44         _packageInstallSessions.value = sessions
45     }
46 
notifyChangenull47     suspend fun notifyChange(model: PackageChangeModel) {
48         _packageChanged.emit(model)
49     }
50 
notifyInstallnull51     suspend fun notifyInstall(packageName: String, user: UserHandle) {
52         notifyChange(
53             PackageChangeModel.Installed(
54                 packageName = packageName,
55                 packageUid =
56                     UserHandle.getUid(
57                         /* userId = */ user.identifier,
58                         /* appId = */ packageName.hashCode(),
59                     ),
60                 timeMillis = systemClock.currentTimeMillis(),
61             )
62         )
63     }
64 
notifyUpdateStartednull65     suspend fun notifyUpdateStarted(packageName: String, user: UserHandle) {
66         notifyChange(
67             PackageChangeModel.UpdateStarted(
68                 packageName = packageName,
69                 packageUid =
70                     UserHandle.getUid(
71                         /* userId = */ user.identifier,
72                         /* appId = */ packageName.hashCode(),
73                     ),
74                 timeMillis = systemClock.currentTimeMillis(),
75             )
76         )
77     }
78 
notifyUpdateFinishednull79     suspend fun notifyUpdateFinished(packageName: String, user: UserHandle) {
80         notifyChange(
81             PackageChangeModel.UpdateFinished(
82                 packageName = packageName,
83                 packageUid =
84                     UserHandle.getUid(
85                         /* userId = */ user.identifier,
86                         /* appId = */ packageName.hashCode(),
87                     ),
88                 timeMillis = systemClock.currentTimeMillis(),
89             )
90         )
91     }
92 
notifyUninstallnull93     suspend fun notifyUninstall(packageName: String, user: UserHandle) {
94         notifyChange(
95             PackageChangeModel.Uninstalled(
96                 packageName = packageName,
97                 packageUid =
98                     UserHandle.getUid(
99                         /* userId = */ user.identifier,
100                         /* appId = */ packageName.hashCode(),
101                     ),
102                 timeMillis = systemClock.currentTimeMillis(),
103             )
104         )
105     }
106 }
107