Skip to content

Commit 32ba69b

Browse files
authored
fix(battery_plus): Improve battery save mode check on Xiaomi devices (#3555)
1 parent c8c1a14 commit 32ba69b

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

packages/battery_plus/battery_plus/android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ android {
3838
}
3939

4040
defaultConfig {
41-
minSdk 19
41+
minSdk 21
4242
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
4343
}
4444

packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt

+31-34
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import android.os.Build
2020
import java.util.Locale
2121
import android.os.PowerManager
2222
import android.provider.Settings
23-
import androidx.annotation.RequiresApi
2423
import androidx.core.content.ContextCompat
2524
import androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED
2625

@@ -51,7 +50,8 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
5150
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
5251
when (call.method) {
5352
"getBatteryLevel" -> {
54-
val currentBatteryLevel = getBatteryLevel()
53+
val currentBatteryLevel =
54+
getBatteryProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
5555
if (currentBatteryLevel != -1) {
5656
result.success(currentBatteryLevel)
5757
} else {
@@ -103,78 +103,75 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
103103
val status: Int = if (VERSION.SDK_INT >= VERSION_CODES.O) {
104104
getBatteryProperty(BatteryManager.BATTERY_PROPERTY_STATUS)
105105
} else {
106-
val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
106+
val intent = ContextWrapper(applicationContext).registerReceiver(
107+
null,
108+
IntentFilter(Intent.ACTION_BATTERY_CHANGED)
109+
)
107110
intent?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
108111
}
109112
return convertBatteryStatus(status)
110113
}
111114

112-
private fun getBatteryLevel(): Int {
113-
return if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
114-
getBatteryProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
115-
} else {
116-
val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
117-
val level = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
118-
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
119-
(level * 100 / scale)
120-
}
121-
}
122-
123115
private fun isInPowerSaveMode(): Boolean? {
124116
val deviceManufacturer = Build.MANUFACTURER.lowercase(Locale.getDefault())
125117

126-
return if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
127-
when (deviceManufacturer) {
128-
"xiaomi" -> isXiaomiPowerSaveModeActive()
129-
"huawei" -> isHuaweiPowerSaveModeActive()
130-
"samsung" -> isSamsungPowerSaveModeActive()
131-
else -> checkPowerServiceSaveMode()
132-
}
133-
} else {
134-
null
118+
return when (deviceManufacturer) {
119+
"xiaomi" -> isXiaomiPowerSaveModeActive()
120+
"huawei" -> isHuaweiPowerSaveModeActive()
121+
"samsung" -> isSamsungPowerSaveModeActive()
122+
else -> checkPowerServiceSaveMode()
135123
}
136124
}
137125

138126
private fun isSamsungPowerSaveModeActive(): Boolean {
139-
val mode = Settings.System.getString(applicationContext!!.contentResolver, POWER_SAVE_MODE_SAMSUNG_NAME)
140-
return if (mode == null && VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
127+
val mode = Settings.System.getString(
128+
applicationContext!!.contentResolver,
129+
POWER_SAVE_MODE_SAMSUNG_NAME
130+
)
131+
return if (mode == null) {
141132
checkPowerServiceSaveMode()
142133
} else {
143134
mode == POWER_SAVE_MODE_SAMSUNG_VALUE
144135
}
145136
}
146137

147-
@RequiresApi(VERSION_CODES.LOLLIPOP)
148138
private fun isHuaweiPowerSaveModeActive(): Boolean {
149-
val mode = Settings.System.getInt(applicationContext!!.contentResolver, POWER_SAVE_MODE_HUAWEI_NAME, -1)
139+
val mode = Settings.System.getInt(
140+
applicationContext!!.contentResolver,
141+
POWER_SAVE_MODE_HUAWEI_NAME,
142+
-1
143+
)
150144
return if (mode != -1) {
151145
mode == POWER_SAVE_MODE_HUAWEI_VALUE
152146
} else {
153147
// On Devices like the P30 lite, we always get an -1 result code.
154-
// Stackoverflow issue: https://stackoverflow.com/a/70500770
148+
// StackOverflow issue: https://stackoverflow.com/a/70500770
155149
checkPowerServiceSaveMode()
156150
}
157151
}
158152

159-
private fun isXiaomiPowerSaveModeActive(): Boolean? {
160-
val mode = Settings.System.getInt(applicationContext!!.contentResolver, POWER_SAVE_MODE_XIAOMI_NAME, -1)
153+
private fun isXiaomiPowerSaveModeActive(): Boolean {
154+
val mode = Settings.System.getInt(
155+
applicationContext!!.contentResolver,
156+
POWER_SAVE_MODE_XIAOMI_NAME,
157+
-1
158+
)
161159
return if (mode != -1) {
162160
mode == POWER_SAVE_MODE_XIAOMI_VALUE
163161
} else {
164-
null
162+
checkPowerServiceSaveMode()
165163
}
166164
}
167165

168-
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
169166
private fun checkPowerServiceSaveMode(): Boolean {
170167
val powerManager =
171168
applicationContext!!.getSystemService(Context.POWER_SERVICE) as PowerManager
172169
return powerManager.isPowerSaveMode
173170
}
174171

175-
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
176172
private fun getBatteryProperty(property: Int): Int {
177-
val batteryManager = applicationContext!!.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
173+
val batteryManager =
174+
applicationContext!!.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
178175
return batteryManager.getIntProperty(property)
179176
}
180177

0 commit comments

Comments
 (0)