-
Notifications
You must be signed in to change notification settings - Fork 7
/
EmbraceThermalStatusService.kt
81 lines (68 loc) · 2.97 KB
/
EmbraceThermalStatusService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package io.embrace.android.embracesdk.capture.thermalstate
import android.os.Build
import android.os.PowerManager
import androidx.annotation.RequiresApi
import io.embrace.android.embracesdk.internal.Systrace
import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.utils.Provider
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.payload.ThermalState
import io.embrace.android.embracesdk.worker.BackgroundWorker
import io.embrace.android.embracesdk.worker.TaskPriority
import java.util.LinkedList
import java.util.concurrent.Executor
private const val CAPTURE_LIMIT = 100
@RequiresApi(Build.VERSION_CODES.Q)
internal class EmbraceThermalStatusService(
private val backgroundWorker: BackgroundWorker,
private val clock: Clock,
private val logger: InternalEmbraceLogger,
powerManagerProvider: Provider<PowerManager?>
) : ThermalStatusService {
private val thermalStates = LinkedList<ThermalState>()
private val thermalStatusListener = PowerManager.OnThermalStatusChangedListener {
handleThermalStateChange(it)
}
private val powerManager: PowerManager? by lazy(powerManagerProvider)
init {
backgroundWorker.submit(TaskPriority.LOW) {
Systrace.traceSynchronous("thermal-service-registration") {
val pm = powerManager
if (pm != null) {
logger.logDeveloper("ThermalStatusService", "Adding thermal status listener")
// Android API only accepts an executor. We don't want to directly expose those
// to everything in the codebase so we decorate the BackgroundWorker here as an
// alternative
val executor = Executor {
backgroundWorker.submit(runnable = it)
}
pm.addThermalStatusListener(executor, thermalStatusListener)
}
}
}
}
fun handleThermalStateChange(status: Int?) {
if (status == null) {
logger.logDeveloper("ThermalStatusService", "Null thermal status, no-oping.")
return
}
logger.logDeveloper("ThermalStatusService", "Thermal status change: $status")
thermalStates.add(ThermalState(clock.now(), status))
if (thermalStates.size > CAPTURE_LIMIT) {
logger.logDeveloper(
"ThermalStatusService",
"Exceeded capture limit, removing oldest thermal status sample."
)
thermalStates.removeFirst()
}
}
override fun cleanCollections() = thermalStates.clear()
override fun getCapturedData(): List<ThermalState> = thermalStates
override fun close() {
val pm = powerManager
if (pm != null) {
logger.logDeveloper("ThermalStatusService", "Removing thermal status listener")
pm.removeThermalStatusListener(thermalStatusListener)
}
}
}