From 81210a6ca8913d68eb52b574ce10b16a6b719efb Mon Sep 17 00:00:00 2001 From: Julian Bissekkou Date: Fri, 21 Mar 2025 17:13:05 +0100 Subject: [PATCH 1/4] fallback to default system check on xiaomi devices --- .../plus/battery/BatteryPlusPlugin.kt | 26 ++++++++++--------- .../battery_plus/example/pubspec.yaml | 11 +++++++- 2 files changed, 24 insertions(+), 13 deletions(-) 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..cfdb1dff78 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 @@ -123,21 +123,22 @@ 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) { + return if (mode == null) { checkPowerServiceSaveMode() } else { mode == POWER_SAVE_MODE_SAMSUNG_VALUE @@ -156,12 +157,13 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter } } - private fun isXiaomiPowerSaveModeActive(): Boolean? { + @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() } } diff --git a/packages/battery_plus/battery_plus/example/pubspec.yaml b/packages/battery_plus/battery_plus/example/pubspec.yaml index a3a6148c32..d2b430401b 100644 --- a/packages/battery_plus/battery_plus/example/pubspec.yaml +++ b/packages/battery_plus/battery_plus/example/pubspec.yaml @@ -8,7 +8,10 @@ environment: dependencies: flutter: sdk: flutter - battery_plus: ^6.2.1 + battery_plus_platform_interface: + path: ../../battery_plus_platform_interface + battery_plus: + path: ../ dev_dependencies: flutter_driver: @@ -19,5 +22,11 @@ dev_dependencies: sdk: flutter flutter_lints: ">=4.0.0 <6.0.0" +dependency_overrides: + battery_plus_platform_interface: + path: ../../battery_plus_platform_interface + battery_plus: + path: ../ + flutter: uses-material-design: true From 87f838893dd38b524a8d200f73ba51e977b3fb82 Mon Sep 17 00:00:00 2001 From: Julian Bissekkou Date: Tue, 25 Mar 2025 13:00:44 +0100 Subject: [PATCH 2/4] add debug code --- .../plus/battery/BatteryPlusPlugin.kt | 58 +++++++++++++++++-- 1 file changed, 52 insertions(+), 6 deletions(-) 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 cfdb1dff78..ad66d137e1 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,6 +20,7 @@ import android.os.Build import java.util.Locale import android.os.PowerManager import android.provider.Settings +import android.util.Log import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED @@ -103,7 +104,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) @@ -113,7 +117,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) @@ -137,7 +144,10 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter @RequiresApi(VERSION_CODES.LOLLIPOP) private fun isSamsungPowerSaveModeActive(): Boolean { - val mode = Settings.System.getString(applicationContext!!.contentResolver, POWER_SAVE_MODE_SAMSUNG_NAME) + val mode = Settings.System.getString( + applicationContext!!.contentResolver, + POWER_SAVE_MODE_SAMSUNG_NAME + ) return if (mode == null) { checkPowerServiceSaveMode() } else { @@ -147,7 +157,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 { @@ -159,7 +173,14 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter @RequiresApi(VERSION_CODES.LOLLIPOP) private fun isXiaomiPowerSaveModeActive(): Boolean { - val mode = Settings.System.getInt(applicationContext!!.contentResolver, POWER_SAVE_MODE_XIAOMI_NAME, -1) + val allSettings = dumpSystemSettings(applicationContext!!) + Log.e("JB", allSettings.toString()) + Log.e("JB", "------------") + val mode = Settings.System.getInt( + applicationContext!!.contentResolver, + POWER_SAVE_MODE_XIAOMI_NAME, + -1 + ) return if (mode != -1) { mode == POWER_SAVE_MODE_XIAOMI_VALUE } else { @@ -167,6 +188,30 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter } } + private fun dumpSystemSettings(context: Context): Map { + val settingsMap = mutableMapOf() + + try { + val contentResolver = context.contentResolver + val uri = Settings.System.CONTENT_URI + + contentResolver.query(uri, null, null, null, null)?.use { cursor -> + val nameIndex = cursor.getColumnIndex("name") + val valueIndex = cursor.getColumnIndex("value") + + while (cursor.moveToNext()) { + val name = cursor.getString(nameIndex) + val value = cursor.getString(valueIndex) + settingsMap[name] = value + } + } + } catch (e: Exception) { + e.printStackTrace() + } + + return settingsMap + } + @RequiresApi(api = VERSION_CODES.LOLLIPOP) private fun checkPowerServiceSaveMode(): Boolean { val powerManager = @@ -176,7 +221,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) } From 5e0484944d458776fd72502526da876b525ff7c2 Mon Sep 17 00:00:00 2001 From: Julian Bissekkou Date: Wed, 2 Apr 2025 08:28:20 +0200 Subject: [PATCH 3/4] undo debug changes --- .../plus/battery/BatteryPlusPlugin.kt | 27 ------------------- .../battery_plus/example/pubspec.yaml | 11 +------- 2 files changed, 1 insertion(+), 37 deletions(-) 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 ad66d137e1..1cee477998 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 @@ -173,9 +173,6 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter @RequiresApi(VERSION_CODES.LOLLIPOP) private fun isXiaomiPowerSaveModeActive(): Boolean { - val allSettings = dumpSystemSettings(applicationContext!!) - Log.e("JB", allSettings.toString()) - Log.e("JB", "------------") val mode = Settings.System.getInt( applicationContext!!.contentResolver, POWER_SAVE_MODE_XIAOMI_NAME, @@ -188,30 +185,6 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter } } - private fun dumpSystemSettings(context: Context): Map { - val settingsMap = mutableMapOf() - - try { - val contentResolver = context.contentResolver - val uri = Settings.System.CONTENT_URI - - contentResolver.query(uri, null, null, null, null)?.use { cursor -> - val nameIndex = cursor.getColumnIndex("name") - val valueIndex = cursor.getColumnIndex("value") - - while (cursor.moveToNext()) { - val name = cursor.getString(nameIndex) - val value = cursor.getString(valueIndex) - settingsMap[name] = value - } - } - } catch (e: Exception) { - e.printStackTrace() - } - - return settingsMap - } - @RequiresApi(api = VERSION_CODES.LOLLIPOP) private fun checkPowerServiceSaveMode(): Boolean { val powerManager = diff --git a/packages/battery_plus/battery_plus/example/pubspec.yaml b/packages/battery_plus/battery_plus/example/pubspec.yaml index d2b430401b..a3a6148c32 100644 --- a/packages/battery_plus/battery_plus/example/pubspec.yaml +++ b/packages/battery_plus/battery_plus/example/pubspec.yaml @@ -8,10 +8,7 @@ environment: dependencies: flutter: sdk: flutter - battery_plus_platform_interface: - path: ../../battery_plus_platform_interface - battery_plus: - path: ../ + battery_plus: ^6.2.1 dev_dependencies: flutter_driver: @@ -22,11 +19,5 @@ dev_dependencies: sdk: flutter flutter_lints: ">=4.0.0 <6.0.0" -dependency_overrides: - battery_plus_platform_interface: - path: ../../battery_plus_platform_interface - battery_plus: - path: ../ - flutter: uses-material-design: true From 47911853d1eb52e784773fcf0c6a25b99a03672e Mon Sep 17 00:00:00 2001 From: Julian Bissekkou Date: Wed, 2 Apr 2025 08:28:44 +0200 Subject: [PATCH 4/4] organize imports --- .../dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt | 1 - 1 file changed, 1 deletion(-) 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 1cee477998..d78e426416 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 android.util.Log import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED