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.dagger.SysUISingleton
22 import com.android.systemui.log.LogBuffer
23 import com.android.systemui.log.core.LogLevel
24 import com.android.systemui.log.dagger.PackageChangeRepoLog
25 import javax.inject.Inject
26 
getChangeStringnull27 private fun getChangeString(model: PackageChangeModel) =
28     when (model) {
29         is PackageChangeModel.Installed -> "installed"
30         is PackageChangeModel.Uninstalled -> "uninstalled"
31         is PackageChangeModel.UpdateStarted -> "started updating"
32         is PackageChangeModel.UpdateFinished -> "finished updating"
33         is PackageChangeModel.Changed -> "changed"
34         is PackageChangeModel.Empty -> throw IllegalStateException("Unexpected empty value: $model")
35     }
36 
37 /** A debug logger for [PackageChangeRepository]. */
38 @SysUISingleton
39 class PackageUpdateLogger @Inject constructor(@PackageChangeRepoLog private val buffer: LogBuffer) {
40 
logChangenull41     fun logChange(model: PackageChangeModel) {
42         buffer.log(
43             TAG,
44             LogLevel.DEBUG,
45             {
46                 str1 = model.packageName
47                 str2 = getChangeString(model)
48                 int1 = model.packageUid
49             },
50             {
51                 val user = UserHandle.getUserHandleForUid(int1)
52                 "Package $str1 ($int1) $str2 on user $user"
53             }
54         )
55     }
56 }
57 
58 private const val TAG = "PackageChangeRepoLog"
59