1 /*
<lambda>null2  * 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.statusbar.pipeline.mobile.ui
18 
19 import android.view.View
20 import com.android.systemui.Dumpable
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.dump.DumpManager
23 import com.android.systemui.log.LogBuffer
24 import com.android.systemui.log.core.LogLevel
25 import com.android.systemui.statusbar.pipeline.dagger.MobileViewLog
26 import com.android.systemui.statusbar.pipeline.mobile.ui.viewmodel.LocationBasedMobileViewModel
27 import java.io.PrintWriter
28 import javax.inject.Inject
29 
30 /** Logs for changes with the new mobile views. */
31 @SysUISingleton
32 class MobileViewLogger
33 @Inject
34 constructor(
35     @MobileViewLog private val buffer: LogBuffer,
36     dumpManager: DumpManager,
37 ) : Dumpable {
38     init {
39         dumpManager.registerNormalDumpable(this)
40     }
41 
42     private val collectionStatuses = mutableMapOf<String, Boolean>()
43 
44     fun logUiAdapterSubIdsSentToIconController(subs: List<Int>) {
45         buffer.log(
46             TAG,
47             LogLevel.INFO,
48             { str1 = subs.toString() },
49             { "Sub IDs in MobileUiAdapter being sent to icon controller: $str1" },
50         )
51     }
52 
53     fun logNewViewBinding(view: View, viewModel: LocationBasedMobileViewModel) {
54         buffer.log(
55             TAG,
56             LogLevel.INFO,
57             {
58                 str1 = view.getIdForLogging()
59                 str2 = viewModel.getIdForLogging()
60                 str3 = viewModel.location.name
61             },
62             { "New view binding. viewId=$str1, viewModelId=$str2, viewModelLocation=$str3" },
63         )
64     }
65 
66     fun logCollectionStarted(view: View, viewModel: LocationBasedMobileViewModel) {
67         collectionStatuses[view.getIdForLogging()] = true
68         buffer.log(
69             TAG,
70             LogLevel.INFO,
71             {
72                 str1 = view.getIdForLogging()
73                 str2 = viewModel.getIdForLogging()
74                 str3 = viewModel.location.name
75             },
76             { "Collection started. viewId=$str1, viewModelId=$str2, viewModelLocation=$str3" },
77         )
78     }
79 
80     fun logCollectionStopped(view: View, viewModel: LocationBasedMobileViewModel) {
81         collectionStatuses[view.getIdForLogging()] = false
82         buffer.log(
83             TAG,
84             LogLevel.INFO,
85             {
86                 str1 = view.getIdForLogging()
87                 str2 = viewModel.getIdForLogging()
88                 str3 = viewModel.location.name
89             },
90             { "Collection stopped. viewId=$str1, viewModelId=$str2, viewModelLocation=$str3" },
91         )
92     }
93 
94     override fun dump(pw: PrintWriter, args: Array<out String>) {
95         pw.println("Collection statuses per view:---")
96         collectionStatuses.forEach { viewId, isCollecting ->
97             pw.println("viewId=$viewId, isCollecting=$isCollecting")
98         }
99     }
100 
101     companion object {
102         fun Any.getIdForLogging(): String {
103             // The identityHashCode is guaranteed to be constant for the lifetime of the object.
104             return Integer.toHexString(System.identityHashCode(this))
105         }
106     }
107 }
108 
109 private const val TAG = "MobileViewLogger"
110