Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sdk][andr] Don't use PowerManager if not available #35

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading