1 /*
2  * Copyright (C) 2020 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.broadcast.logging
18 
19 import android.content.BroadcastReceiver
20 import android.content.Intent
21 import android.content.IntentFilter
22 import com.android.systemui.log.LogBuffer
23 import com.android.systemui.log.LogLevel
24 import com.android.systemui.log.LogLevel.DEBUG
25 import com.android.systemui.log.LogLevel.INFO
26 import com.android.systemui.log.LogMessage
27 import com.android.systemui.log.dagger.BroadcastDispatcherLog
28 import javax.inject.Inject
29 
30 private const val TAG = "BroadcastDispatcherLog"
31 
32 class BroadcastDispatcherLogger @Inject constructor(
33     @BroadcastDispatcherLog private val buffer: LogBuffer
34 ) {
35 
logBroadcastReceivednull36     fun logBroadcastReceived(broadcastId: Int, user: Int, intent: Intent) {
37         val intentString = intent.toString()
38         log(INFO, {
39             int1 = broadcastId
40             int2 = user
41             str1 = intentString
42         }, {
43             "[$int1] Broadcast received for user $int2: $str1"
44         })
45     }
46 
logBroadcastDispatchednull47     fun logBroadcastDispatched(broadcastId: Int, action: String?, receiver: BroadcastReceiver) {
48         val receiverString = receiver.toString()
49         log(DEBUG, {
50             int1 = broadcastId
51             str1 = action
52             str2 = receiverString
53         }, {
54             "Broadcast $int1 ($str1) dispatched to $str2"
55         })
56     }
57 
logReceiverRegisterednull58     fun logReceiverRegistered(user: Int, receiver: BroadcastReceiver) {
59         val receiverString = receiver.toString()
60         log(INFO, {
61             int1 = user
62             str1 = receiverString
63         }, {
64             "Receiver $str1 registered for user $int1"
65         })
66     }
67 
logReceiverUnregisterednull68     fun logReceiverUnregistered(user: Int, receiver: BroadcastReceiver) {
69         val receiverString = receiver.toString()
70         log(INFO, {
71             int1 = user
72             str1 = receiverString
73         }, {
74             "Receiver $str1 unregistered for user $int1"
75         })
76     }
77 
logContextReceiverRegisterednull78     fun logContextReceiverRegistered(user: Int, filter: IntentFilter) {
79         val actions = filter.actionsIterator().asSequence()
80                 .joinToString(separator = ",", prefix = "Actions(", postfix = ")")
81         val categories = if (filter.countCategories() != 0) {
82             filter.categoriesIterator().asSequence()
83                     .joinToString(separator = ",", prefix = "Categories(", postfix = ")")
84         } else {
85             ""
86         }
87         log(INFO, {
88             int1 = user
89             str1 = if (categories != "") {
90                 "${actions}\n$categories"
91             } else {
92                 actions
93             }
94         }, {
95             """
96                 Receiver registered with Context for user $int1.
97                 $str1
98             """.trimIndent()
99         })
100     }
101 
logContextReceiverUnregisterednull102     fun logContextReceiverUnregistered(user: Int, action: String) {
103         log(INFO, {
104             int1 = user
105             str1 = action
106         }, {
107             "Receiver unregistered with Context for user $int1, action $str1"
108         })
109     }
110 
lognull111     private inline fun log(
112         logLevel: LogLevel,
113         initializer: LogMessage.() -> Unit,
114         noinline printer: LogMessage.() -> String
115     ) {
116         buffer.log(TAG, logLevel, initializer, printer)
117     }
118 }