Acquire partial wake lock only if there is active registrations or calls
This commit is contained in:
@ -100,6 +100,7 @@ class BaresipService: Service() {
|
||||
private lateinit var tm: TelecomManager
|
||||
private lateinit var btm: BluetoothManager
|
||||
private lateinit var vibrator: Vibrator
|
||||
private lateinit var partialWakeLock: PowerManager.WakeLock
|
||||
private lateinit var proximityWakeLock: PowerManager.WakeLock
|
||||
private lateinit var wifiLock: WifiManager.WifiLock
|
||||
private lateinit var bluetoothReceiver: BroadcastReceiver
|
||||
@ -160,6 +161,12 @@ class BaresipService: Service() {
|
||||
applicationContext.getSystemService(VIBRATOR_SERVICE) as Vibrator
|
||||
}
|
||||
|
||||
// This is needed to keep service running also in Doze Mode
|
||||
partialWakeLock = pm.newWakeLock(
|
||||
PowerManager.PARTIAL_WAKE_LOCK,
|
||||
"com.tutpro.baresip:wakelock"
|
||||
).apply { setReferenceCounted(false) }
|
||||
|
||||
networkCallback = object : ConnectivityManager.NetworkCallback() {
|
||||
|
||||
override fun onAvailable(network: Network) {
|
||||
@ -1414,7 +1421,8 @@ class BaresipService: Service() {
|
||||
callActionUri = ""
|
||||
Log.d(TAG, "Battery optimizations are ignored: " +
|
||||
"${pm.isIgnoringBatteryOptimizations(packageName)}")
|
||||
Log.d(TAG, "Wifi lock is held: ${wifiLock.isHeld}")
|
||||
Log.d(TAG, "Partial wake lock/wifi lock is held: " +
|
||||
"${partialWakeLock.isHeld}/${wifiLock.isHeld}")
|
||||
updateStatusNotification()
|
||||
}
|
||||
|
||||
@ -1536,6 +1544,25 @@ class BaresipService: Service() {
|
||||
Timer().schedule(250) {
|
||||
nm.notify(STATUS_NOTIFICATION_ID, buildStatusNotification())
|
||||
}
|
||||
updatePartialWakeLock()
|
||||
}
|
||||
|
||||
@SuppressLint("WakelockTimeout")
|
||||
private fun updatePartialWakeLock() {
|
||||
val isAnyUaActive = uasStatus.value.values.any { it != R.drawable.circle_white }
|
||||
val isAnyCallActive = calls.isNotEmpty()
|
||||
val needsToStayAwake = isAnyUaActive || isAnyCallActive
|
||||
if (needsToStayAwake) {
|
||||
if (!partialWakeLock.isHeld) {
|
||||
Log.d(TAG, "Acquiring Partial Wake Lock")
|
||||
partialWakeLock.acquire()
|
||||
}
|
||||
} else {
|
||||
if (partialWakeLock.isHeld) {
|
||||
Log.d(TAG, "Releasing Partial Wake Lock")
|
||||
partialWakeLock.release()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun toast(message: String, length: Int = Toast.LENGTH_SHORT) {
|
||||
@ -1879,6 +1906,8 @@ class BaresipService: Service() {
|
||||
messages = emptyList()
|
||||
if (this::nm.isInitialized)
|
||||
nm.cancelAll()
|
||||
if (this::partialWakeLock.isInitialized && partialWakeLock.isHeld)
|
||||
partialWakeLock.release()
|
||||
if (this::proximityWakeLock.isInitialized && proximityWakeLock.isHeld)
|
||||
proximityWakeLock.release()
|
||||
if (this::wifiLock.isInitialized)
|
||||
|
||||
Reference in New Issue
Block a user