@@ -20,7 +20,6 @@ import android.os.Build
20
20
import java.util.Locale
21
21
import android.os.PowerManager
22
22
import android.provider.Settings
23
- import androidx.annotation.RequiresApi
24
23
import androidx.core.content.ContextCompat
25
24
import androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED
26
25
@@ -51,7 +50,8 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
51
50
override fun onMethodCall (call : MethodCall , result : MethodChannel .Result ) {
52
51
when (call.method) {
53
52
" getBatteryLevel" -> {
54
- val currentBatteryLevel = getBatteryLevel()
53
+ val currentBatteryLevel =
54
+ getBatteryProperty(BatteryManager .BATTERY_PROPERTY_CAPACITY )
55
55
if (currentBatteryLevel != - 1 ) {
56
56
result.success(currentBatteryLevel)
57
57
} else {
@@ -103,78 +103,75 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
103
103
val status: Int = if (VERSION .SDK_INT >= VERSION_CODES .O ) {
104
104
getBatteryProperty(BatteryManager .BATTERY_PROPERTY_STATUS )
105
105
} 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
+ )
107
110
intent?.getIntExtra(BatteryManager .EXTRA_STATUS , - 1 ) ? : - 1
108
111
}
109
112
return convertBatteryStatus(status)
110
113
}
111
114
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
-
123
115
private fun isInPowerSaveMode (): Boolean? {
124
116
val deviceManufacturer = Build .MANUFACTURER .lowercase(Locale .getDefault())
125
117
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()
135
123
}
136
124
}
137
125
138
126
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 ) {
141
132
checkPowerServiceSaveMode()
142
133
} else {
143
134
mode == POWER_SAVE_MODE_SAMSUNG_VALUE
144
135
}
145
136
}
146
137
147
- @RequiresApi(VERSION_CODES .LOLLIPOP )
148
138
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
+ )
150
144
return if (mode != - 1 ) {
151
145
mode == POWER_SAVE_MODE_HUAWEI_VALUE
152
146
} else {
153
147
// 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
155
149
checkPowerServiceSaveMode()
156
150
}
157
151
}
158
152
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
+ )
161
159
return if (mode != - 1 ) {
162
160
mode == POWER_SAVE_MODE_XIAOMI_VALUE
163
161
} else {
164
- null
162
+ checkPowerServiceSaveMode()
165
163
}
166
164
}
167
165
168
- @RequiresApi(api = VERSION_CODES .LOLLIPOP )
169
166
private fun checkPowerServiceSaveMode (): Boolean {
170
167
val powerManager =
171
168
applicationContext!! .getSystemService(Context .POWER_SERVICE ) as PowerManager
172
169
return powerManager.isPowerSaveMode
173
170
}
174
171
175
- @RequiresApi(api = VERSION_CODES .LOLLIPOP )
176
172
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
178
175
return batteryManager.getIntProperty(property)
179
176
}
180
177
0 commit comments