Skip to content

fix: fix-xiaomi-battery-safe-mode #3537

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

Closed
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 @@ -103,7 +103,10 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
val status: Int = if (VERSION.SDK_INT >= VERSION_CODES.O) {
getBatteryProperty(BatteryManager.BATTERY_PROPERTY_STATUS)
} else {
val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
val intent = ContextWrapper(applicationContext).registerReceiver(
null,
IntentFilter(Intent.ACTION_BATTERY_CHANGED)
)
intent?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
}
return convertBatteryStatus(status)
Expand All @@ -113,7 +116,10 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
return if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
getBatteryProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
} else {
val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
val intent = ContextWrapper(applicationContext).registerReceiver(
null,
IntentFilter(Intent.ACTION_BATTERY_CHANGED)
)
val level = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
(level * 100 / scale)
Expand All @@ -123,21 +129,25 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
private fun isInPowerSaveMode(): Boolean? {
val deviceManufacturer = Build.MANUFACTURER.lowercase(Locale.getDefault())

return if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
when (deviceManufacturer) {
"xiaomi" -> isXiaomiPowerSaveModeActive()
"huawei" -> isHuaweiPowerSaveModeActive()
"samsung" -> isSamsungPowerSaveModeActive()
else -> checkPowerServiceSaveMode()
}
} else {
null
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
return null
}

return when (deviceManufacturer) {
"xiaomi" -> isXiaomiPowerSaveModeActive()
"huawei" -> isHuaweiPowerSaveModeActive()
"samsung" -> isSamsungPowerSaveModeActive()
else -> checkPowerServiceSaveMode()
}
}

@RequiresApi(VERSION_CODES.LOLLIPOP)
private fun isSamsungPowerSaveModeActive(): Boolean {
val mode = Settings.System.getString(applicationContext!!.contentResolver, POWER_SAVE_MODE_SAMSUNG_NAME)
return if (mode == null && VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
val mode = Settings.System.getString(
applicationContext!!.contentResolver,
POWER_SAVE_MODE_SAMSUNG_NAME
)
return if (mode == null) {
checkPowerServiceSaveMode()
} else {
mode == POWER_SAVE_MODE_SAMSUNG_VALUE
Expand All @@ -146,7 +156,11 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter

@RequiresApi(VERSION_CODES.LOLLIPOP)
private fun isHuaweiPowerSaveModeActive(): Boolean {
val mode = Settings.System.getInt(applicationContext!!.contentResolver, POWER_SAVE_MODE_HUAWEI_NAME, -1)
val mode = Settings.System.getInt(
applicationContext!!.contentResolver,
POWER_SAVE_MODE_HUAWEI_NAME,
-1
)
return if (mode != -1) {
mode == POWER_SAVE_MODE_HUAWEI_VALUE
} else {
Expand All @@ -156,12 +170,17 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
}
}

private fun isXiaomiPowerSaveModeActive(): Boolean? {
val mode = Settings.System.getInt(applicationContext!!.contentResolver, POWER_SAVE_MODE_XIAOMI_NAME, -1)
@RequiresApi(VERSION_CODES.LOLLIPOP)
private fun isXiaomiPowerSaveModeActive(): Boolean {
val mode = Settings.System.getInt(
applicationContext!!.contentResolver,
POWER_SAVE_MODE_XIAOMI_NAME,
-1
)
return if (mode != -1) {
mode == POWER_SAVE_MODE_XIAOMI_VALUE
} else {
null
checkPowerServiceSaveMode()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On some newer android version of xiaomi devices, the power safe mode is not available by querying POWER_SAVE_MODE_XIAOMI_NAME.

This PR fallbacks to the default power safe mode. We are doing something similar for samsung as well.

}
}

Expand All @@ -174,7 +193,8 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter

@RequiresApi(api = VERSION_CODES.LOLLIPOP)
private fun getBatteryProperty(property: Int): Int {
val batteryManager = applicationContext!!.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
val batteryManager =
applicationContext!!.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
return batteryManager.getIntProperty(property)
}

Expand Down
Loading