1 /*
2  * Copyright (C) 2019 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.qs.logging
18 
19 import android.service.quicksettings.Tile
20 import com.android.systemui.log.LogBuffer
21 import com.android.systemui.log.LogLevel
22 import com.android.systemui.log.LogLevel.DEBUG
23 import com.android.systemui.log.LogLevel.VERBOSE
24 import com.android.systemui.log.LogMessage
25 import com.android.systemui.log.dagger.QSLog
26 import com.android.systemui.plugins.qs.QSTile
27 import com.android.systemui.statusbar.StatusBarState
28 import javax.inject.Inject
29 
30 private const val TAG = "QSLog"
31 
32 class QSLogger @Inject constructor(
33     @QSLog private val buffer: LogBuffer
34 ) {
35 
logTileAddednull36     fun logTileAdded(tileSpec: String) {
37         log(DEBUG, {
38             str1 = tileSpec
39         }, {
40             "[$str1] Tile added"
41         })
42     }
43 
logTileDestroyednull44     fun logTileDestroyed(tileSpec: String, reason: String) {
45         log(DEBUG, {
46             str1 = tileSpec
47             str2 = reason
48         }, {
49             "[$str1] Tile destroyed. Reason: $str2"
50         })
51     }
52 
logTileChangeListeningnull53     fun logTileChangeListening(tileSpec: String, listening: Boolean) {
54         log(VERBOSE, {
55             bool1 = listening
56             str1 = tileSpec
57         }, {
58             "[$str1] Tile listening=$bool1"
59         })
60     }
61 
logAllTilesChangeListeningnull62     fun logAllTilesChangeListening(listening: Boolean, containerName: String, allSpecs: String) {
63         log(DEBUG, {
64             bool1 = listening
65             str1 = containerName
66             str2 = allSpecs
67         }, {
68             "Tiles listening=$bool1 in $str1. $str2"
69         })
70     }
71 
logTileClicknull72     fun logTileClick(tileSpec: String, statusBarState: Int, state: Int) {
73         log(DEBUG, {
74             str1 = tileSpec
75             int1 = statusBarState
76             str2 = StatusBarState.toShortString(statusBarState)
77             str3 = toStateString(state)
78         }, {
79             "[$str1] Tile clicked. StatusBarState=$str2. TileState=$str3"
80         })
81     }
82 
logTileSecondaryClicknull83     fun logTileSecondaryClick(tileSpec: String, statusBarState: Int, state: Int) {
84         log(DEBUG, {
85             str1 = tileSpec
86             int1 = statusBarState
87             str2 = StatusBarState.toShortString(statusBarState)
88             str3 = toStateString(state)
89         }, {
90             "[$str1] Tile long clicked. StatusBarState=$str2. TileState=$str3"
91         })
92     }
93 
logTileLongClicknull94     fun logTileLongClick(tileSpec: String, statusBarState: Int, state: Int) {
95         log(DEBUG, {
96             str1 = tileSpec
97             int1 = statusBarState
98             str2 = StatusBarState.toShortString(statusBarState)
99             str3 = toStateString(state)
100         }, {
101             "[$str1] Tile long clicked. StatusBarState=$str2. TileState=$str3"
102         })
103     }
104 
logTileUpdatednull105     fun logTileUpdated(tileSpec: String, state: QSTile.State) {
106         log(VERBOSE, {
107             str1 = tileSpec
108             str2 = state.label?.toString()
109             str3 = state.icon?.toString()
110             int1 = state.state
111             if (state is QSTile.SignalState) {
112                 bool1 = true
113                 bool2 = state.activityIn
114                 bool3 = state.activityOut
115             }
116         }, {
117             "[$str1] Tile updated. Label=$str2. State=$int1. Icon=$str3." +
118                 if (bool1) " Activity in/out=$bool2/$bool3" else ""
119         })
120     }
121 
logPanelExpandednull122     fun logPanelExpanded(expanded: Boolean, containerName: String) {
123         log(DEBUG, {
124             str1 = containerName
125             bool1 = expanded
126         }, {
127             "$str1 expanded=$bool1"
128         })
129     }
130 
toStateStringnull131     private fun toStateString(state: Int): String {
132         return when (state) {
133             Tile.STATE_ACTIVE -> "active"
134             Tile.STATE_INACTIVE -> "inactive"
135             Tile.STATE_UNAVAILABLE -> "unavailable"
136             else -> "wrong state"
137         }
138     }
139 
lognull140     private inline fun log(
141         logLevel: LogLevel,
142         initializer: LogMessage.() -> Unit,
143         noinline printer: LogMessage.() -> String
144     ) {
145         buffer.log(TAG, logLevel, initializer, printer)
146     }
147 }