Skip to content

Commit

Permalink
[sdk][andr] Don't use PowerManager if not available (#35)
Browse files Browse the repository at this point in the history
* [sdk][andr] Don't use PowerManager if not available

* Add comment
  • Loading branch information
murki authored Sep 10, 2024
1 parent 261815b commit d888daa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import android.os.PowerManager
import androidx.annotation.RequiresApi

internal class PowerMonitor(context: Context) {
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
// Customers have reported encountering certain devices (particularly the Caterpillar S48C phone)
// where this returns null on Android 8.1.0. Even though it should always be available on API level >= 21.
val powerManager = context.getSystemService(Context.POWER_SERVICE) as? PowerManager

@RequiresApi(Build.VERSION_CODES.Q)
private val thermalStatusMap = hashMapOf(
Expand All @@ -32,6 +34,6 @@ internal class PowerMonitor(context: Context) {
}

fun isPowerSaveModeEnabledAttribute(): Pair<String, String> {
return Pair("_low_power_enabled", if (powerManager.isPowerSaveMode) "1" else "0")
return Pair("_low_power_enabled", if (powerManager?.isPowerSaveMode == true) "1" else "0")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class DeviceStateListenerLogger(
context.registerComponentCallbacks(this)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
powerMonitor.powerManager.addThermalStatusListener(executor, thermalCallback)
powerMonitor.powerManager?.addThermalStatusListener(executor, thermalCallback)
}
}

Expand All @@ -75,7 +75,7 @@ internal class DeviceStateListenerLogger(
context.unregisterComponentCallbacks(this)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
powerMonitor.powerManager.removeThermalStatusListener(thermalCallback)
powerMonitor.powerManager?.removeThermalStatusListener(thermalCallback)
}
}

Expand Down

0 comments on commit d888daa

Please sign in to comment.