Skip to content

fix(battery_plus): Improve battery save mode check on Xiaomi devices #3555

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/battery_plus/battery_plus/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
}

defaultConfig {
minSdk 19
minSdk 21
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import android.os.Build
import java.util.Locale
import android.os.PowerManager
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED

Expand Down Expand Up @@ -51,7 +50,8 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"getBatteryLevel" -> {
val currentBatteryLevel = getBatteryLevel()
val currentBatteryLevel =
getBatteryProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
if (currentBatteryLevel != -1) {
result.success(currentBatteryLevel)
} else {
Expand Down Expand Up @@ -103,78 +103,75 @@ 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)
}

private fun getBatteryLevel(): Int {
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 level = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
(level * 100 / scale)
}
}

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
return when (deviceManufacturer) {
"xiaomi" -> isXiaomiPowerSaveModeActive()
"huawei" -> isHuaweiPowerSaveModeActive()
"samsung" -> isSamsungPowerSaveModeActive()
else -> checkPowerServiceSaveMode()
}
}

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
}
}

@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 {
// On Devices like the P30 lite, we always get an -1 result code.
// Stackoverflow issue: https://stackoverflow.com/a/70500770
// StackOverflow issue: https://stackoverflow.com/a/70500770
checkPowerServiceSaveMode()
}
}

private fun isXiaomiPowerSaveModeActive(): Boolean? {
val mode = Settings.System.getInt(applicationContext!!.contentResolver, POWER_SAVE_MODE_XIAOMI_NAME, -1)
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()
}
}

@RequiresApi(api = VERSION_CODES.LOLLIPOP)
private fun checkPowerServiceSaveMode(): Boolean {
val powerManager =
applicationContext!!.getSystemService(Context.POWER_SERVICE) as PowerManager
return powerManager.isPowerSaveMode
}

@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