diff --git a/packages/battery_plus/battery_plus/android/build.gradle b/packages/battery_plus/battery_plus/android/build.gradle index c7c0c92c07..14033b8153 100644 --- a/packages/battery_plus/battery_plus/android/build.gradle +++ b/packages/battery_plus/battery_plus/android/build.gradle @@ -38,7 +38,7 @@ android { } defaultConfig { - minSdk 19 + minSdk 21 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt b/packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt index f7618902af..f2b2a68280 100644 --- a/packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt +++ b/packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt @@ -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 @@ -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 { @@ -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) }