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.statusbar.policy
18 
19 import android.content.Intent
20 import android.os.BatteryManager.EXTRA_LEVEL
21 import android.os.BatteryManager.EXTRA_SCALE
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.log.LogBuffer
24 import com.android.systemui.log.core.LogLevel
25 import com.android.systemui.statusbar.policy.dagger.BatteryControllerLog
26 import javax.inject.Inject
27 
28 /** Detailed, [LogBuffer]-backed logs for [BatteryControllerImpl] */
29 @SysUISingleton
30 class BatteryControllerLogger
31 @Inject
32 constructor(@BatteryControllerLog private val logBuffer: LogBuffer) {
logBatteryControllerInstancenull33     fun logBatteryControllerInstance(controller: BatteryController) {
34         logBuffer.log(
35             TAG,
36             LogLevel.DEBUG,
37             { int1 = System.identityHashCode(controller) },
38             { "BatteryController CREATE (${Integer.toHexString(int1)})" }
39         )
40     }
41 
logBatteryControllerInitnull42     fun logBatteryControllerInit(controller: BatteryController, hasReceivedBattery: Boolean) {
43         logBuffer.log(
44             TAG,
45             LogLevel.DEBUG,
46             {
47                 int1 = System.identityHashCode(controller)
48                 bool1 = hasReceivedBattery
49             },
50             { "BatteryController INIT (${Integer.toHexString(int1)}) hasReceivedBattery=$bool1" }
51         )
52     }
53 
logIntentReceivednull54     fun logIntentReceived(action: String) {
55         logBuffer.log(TAG, LogLevel.DEBUG, { str1 = action }, { "Received intent $str1" })
56     }
57 
logBatteryChangedIntentnull58     fun logBatteryChangedIntent(intent: Intent) {
59         logBuffer.log(
60             TAG,
61             LogLevel.DEBUG,
62             {
63                 int1 = intent.getIntExtra(EXTRA_LEVEL, DEFAULT)
64                 int2 = intent.getIntExtra(EXTRA_SCALE, DEFAULT)
65             },
66             { "Processing BATTERY_CHANGED intent. level=${int1.report()} scale=${int2.report()}" }
67         )
68     }
69 
logBatteryChangedSkipBecauseTestnull70     fun logBatteryChangedSkipBecauseTest() {
71         logBuffer.log(
72             TAG,
73             LogLevel.DEBUG,
74             {},
75             { "Detected test intent. Will not execute battery level callbacks." }
76         )
77     }
78 
logEnterTestModenull79     fun logEnterTestMode() {
80         logBuffer.log(
81             TAG,
82             LogLevel.DEBUG,
83             {},
84             { "Entering test mode for BATTERY_LEVEL_TEST intent" }
85         )
86     }
87 
logExitTestModenull88     fun logExitTestMode() {
89         logBuffer.log(TAG, LogLevel.DEBUG, {}, { "Exiting test mode" })
90     }
91 
logBatteryLevelChangedCallbacknull92     fun logBatteryLevelChangedCallback(level: Int, plugged: Boolean, charging: Boolean) {
93         logBuffer.log(
94             TAG,
95             LogLevel.DEBUG,
96             {
97                 int1 = level
98                 bool1 = plugged
99                 bool2 = charging
100             },
101             {
102                 "Sending onBatteryLevelChanged callbacks " +
103                     "with level=$int1, plugged=$bool1, charging=$bool2"
104             }
105         )
106     }
107 
logPowerSaveChangedCallbacknull108     fun logPowerSaveChangedCallback(isPowerSave: Boolean) {
109         logBuffer.log(
110             TAG,
111             LogLevel.DEBUG,
112             { bool1 = isPowerSave },
113             { "Sending onPowerSaveChanged callback with powerSave=$bool1" }
114         )
115     }
116 
reportnull117     private fun Int.report(): String =
118         if (this == DEFAULT) {
119             "(missing)"
120         } else {
121             toString()
122         }
123 
124     companion object {
125         const val TAG: String = "BatteryControllerLog"
126     }
127 }
128 
129 // Use a token value so we can determine if we got the default
130 private const val DEFAULT = -11
131