diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 33de48b..e56fce8 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Thu Feb 03 20:30:45 IST 2022 +#Fri Sep 27 20:43:45 CEST 2024 distributionBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip diff --git a/android/src/main/java/in/jvapps/disable_battery_optimization/DisableBatteryOptimizationPlugin.java b/android/src/main/java/in/jvapps/disable_battery_optimization/DisableBatteryOptimizationPlugin.java index 24e5465..5b490a4 100644 --- a/android/src/main/java/in/jvapps/disable_battery_optimization/DisableBatteryOptimizationPlugin.java +++ b/android/src/main/java/in/jvapps/disable_battery_optimization/DisableBatteryOptimizationPlugin.java @@ -4,10 +4,12 @@ import android.content.Context; import android.content.Intent; import android.util.Log; +import android.os.PowerManager; import androidx.annotation.NonNull; import in.jvapps.disable_battery_optimization.managers.KillerManager; +import in.jvapps.disable_battery_optimization.managers.DevicesManager; import java.util.List; @@ -76,21 +78,34 @@ public DisableBatteryOptimizationPlugin() { @Override public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { switch (call.method) { + case "callAutoStart": + KillerManager.doActionAutoStart(mContext); + break; + case "callDisableBatteryOptimization": + KillerManager.doActionPowerSaving(mContext); + break; case "showEnableAutoStart": try { List arguments = (List) call.arguments; if (arguments != null) { autoStartTitle = String.valueOf(arguments.get(0)); autoStartMessage = String.valueOf(arguments.get(1)); - showAutoStartEnabler(() -> setManAutoStart(true), () -> setManAutoStart(false)); - result.success(true); + showAutoStartEnabler(() -> { + setManAutoStart(true); + result.success("enabled"); + }, () -> { + setManAutoStart(false); + result.success("disabled"); + }, () -> { + result.success("notavailable"); + }); } else { Log.e(TAG, "Unable to request enableAutoStart. Arguments are null"); - result.success(false); + result.error("E404", "Unable to request enableAutoStart. Arguments are null", null); } } catch (Exception ex) { Log.e(TAG, "Exception in showEnableAutoStart. " + ex.toString()); - result.success(false); + result.error("E100", "Exception in showEnableAutoStart", ex); } break; case "showDisableManBatteryOptimization": @@ -99,24 +114,42 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { if (arguments != null) { manBatteryTitle = String.valueOf(arguments.get(0)); manBatteryMessage = String.valueOf(arguments.get(1)); - showManBatteryOptimizationDisabler(false); - result.success(true); + showManBatteryOptimizationDisabler(false, () -> { + result.success("enabled"); + }, () -> { + result.success("disabled"); + }, () -> { + result.success("notavailable"); + }); } else { Log.e(TAG, "Unable to request disable manufacturer battery optimization. Arguments are null"); - result.success(false); + result.error("E404", "Unable to request disable manufacturer battery optimization. Arguments are null", null); } } catch (Exception ex) { Log.e(TAG, "Exception in showDisableManBatteryOptimization. " + ex.toString()); - result.success(false); + result.error("E100", "Exception during process", ex); } break; case "showDisableBatteryOptimization": try { - showIgnoreBatteryPermissions(); - result.success(true); + List arguments = (List) call.arguments; + if(arguments != null) { + autoStartTitle = String.valueOf(arguments.get(0)); + autoStartMessage = String.valueOf(arguments.get(1)); + showIgnoreBatteryPermissions(() -> { + result.success("enabled"); + }, () -> { + result.success("disabled"); + }, () -> { + result.success("notavailable"); + }); + } else { + Log.e(TAG, "Unable to request disable battery optimization. Arguments are null"); + result.error("E404", "Unable to request disable battery optimization. Arguments are null", null); + } } catch (Exception ex) { Log.e(TAG, "Exception in showDisableBatteryOptimization. " + ex.toString()); - result.success(false); + result.error("E100", "Exception during process", ex); } break; case "disableAllOptimizations": @@ -127,21 +160,28 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { autoStartMessage = String.valueOf(arguments.get(1)); manBatteryTitle = String.valueOf(arguments.get(2)); manBatteryMessage = String.valueOf(arguments.get(3)); - handleIgnoreAllBatteryPermission(); - result.success(true); + handleIgnoreAllBatteryPermission(() -> { + result.success("enabled"); + }, () -> { + result.success("disabled"); + }, () -> { + result.success("notavailable"); + }); } else { Log.e(TAG, "Unable to request disable all optimizations. Arguments are null"); - result.success(false); + result.error("E404", "Unable to request disable all optimizations. Arguments are null", null); } } catch (Exception ex) { Log.e(TAG, "Exception in disableAllOptimizations. " + ex.toString()); - result.success(false); + result.error("E100", "Error during processing", ex); } break; case "isAutoStartEnabled": + // @ToDO result.success(getManAutoStart()); break; case "isBatteryOptimizationDisabled": + // @ToDO result.success(BatteryOptimizationUtil.isIgnoringBatteryOptimizations(mContext)); break; case "isManBatteryOptimizationDisabled": @@ -190,18 +230,24 @@ public void onDetachedFromActivity() { } private void showAutoStartEnabler(@NonNull final BatteryOptimizationUtil.OnBatteryOptimizationAccepted positiveCallback, - @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationCanceled negativeCallback) { + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationCanceled negativeCallback, + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationNotAvailable notAvailableCallback) { BatteryOptimizationUtil.showBatteryOptimizationDialog( mActivity, KillerManager.Actions.ACTION_AUTOSTART, autoStartTitle, autoStartMessage, positiveCallback, - negativeCallback + negativeCallback, + notAvailableCallback ); } - private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler) { + private void showManBatteryOptimizationDisabler( + boolean isRequestNativeBatteryOptimizationDisabler, + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationAccepted positiveCallback, + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationCanceled negativeCallback, + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationNotAvailable notAvailableCallback) { BatteryOptimizationUtil.showBatteryOptimizationDialog( mActivity, KillerManager.Actions.ACTION_POWERSAVING, @@ -210,50 +256,79 @@ private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOp () -> { setManBatteryOptimization(true); if (isRequestNativeBatteryOptimizationDisabler) { - showIgnoreBatteryPermissions(); + showIgnoreBatteryPermissions(positiveCallback, negativeCallback, notAvailableCallback); + } else { + notAvailableCallback.OnBatteryOptimizationNotAvailable(); + } }, () -> { if (isRequestNativeBatteryOptimizationDisabler) { - showIgnoreBatteryPermissions(); + showIgnoreBatteryPermissions(positiveCallback, negativeCallback, notAvailableCallback); + } else { + notAvailableCallback.OnBatteryOptimizationNotAvailable(); } - } + }, + notAvailableCallback ); } - private void showIgnoreBatteryPermissions() { + private void showIgnoreBatteryPermissions( + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationAccepted positiveCallback, + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationCanceled negativeCallback, + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationNotAvailable notAvailableCallback) { if (!BatteryOptimizationUtil.isIgnoringBatteryOptimizations(mContext)) { - final Intent ignoreBatteryOptimizationsIntent = BatteryOptimizationUtil.getIgnoreBatteryOptimizationsIntent(mContext); - if (ignoreBatteryOptimizationsIntent != null) { - mContext.startActivity(ignoreBatteryOptimizationsIntent); - } else { - Log.i(TAG, "Can't ignore the battery optimization as the intent is null"); - } + + BatteryOptimizationUtil.showBatteryOptimizationDialog( + mActivity, + KillerManager.Actions.ACTION_AUTOSTART, + autoStartTitle, + autoStartMessage, + () -> { + final Intent ignoreBatteryOptimizationsIntent = BatteryOptimizationUtil.getIgnoreBatteryOptimizationsIntent(mContext); + if (ignoreBatteryOptimizationsIntent != null) { + mContext.startActivity(ignoreBatteryOptimizationsIntent); + positiveCallback.onBatteryOptimizationAccepted(); + } else { + Log.i(TAG, "Can't ignore the battery optimization as the intent is null"); + negativeCallback.onBatteryOptimizationCanceled(); + } + }, + negativeCallback, + notAvailableCallback + ); + } else { Log.i(TAG, "Battery optimization is already disabled"); + positiveCallback.onBatteryOptimizationAccepted(); } } - private void handleIgnoreAllBatteryPermission() { + private void handleIgnoreAllBatteryPermission( + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationAccepted positiveCallback, + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationCanceled negativeCallback, + @NonNull final BatteryOptimizationUtil.OnBatteryOptimizationNotAvailable notAvailableCallback) { boolean isManBatteryOptimizationDisabled = getManBatteryOptimization(); if (!getManAutoStart()) { showAutoStartEnabler(() -> { setManAutoStart(true); if (!isManBatteryOptimizationDisabled) - showManBatteryOptimizationDisabler(true); + showManBatteryOptimizationDisabler(true, positiveCallback, negativeCallback, notAvailableCallback); else - showIgnoreBatteryPermissions(); + showIgnoreBatteryPermissions(positiveCallback, negativeCallback, notAvailableCallback); }, () -> { if (!isManBatteryOptimizationDisabled) - showManBatteryOptimizationDisabler(true); + showManBatteryOptimizationDisabler(true, positiveCallback, negativeCallback, notAvailableCallback); else - showIgnoreBatteryPermissions(); - }); + showIgnoreBatteryPermissions(positiveCallback, negativeCallback, notAvailableCallback); + }, + () -> {} + ); } else { if (!isManBatteryOptimizationDisabled) - showManBatteryOptimizationDisabler(true); + showManBatteryOptimizationDisabler(true, positiveCallback, negativeCallback, notAvailableCallback); else - showIgnoreBatteryPermissions(); + showIgnoreBatteryPermissions(positiveCallback, negativeCallback, notAvailableCallback); } } @@ -262,13 +337,8 @@ public void setManBatteryOptimization(boolean val) { } public boolean getManBatteryOptimization() { - if (PrefUtils.hasKey(mContext, PrefKeys.IS_MAN_BATTERY_OPTIMIZATION_ACCEPTED)) { - return (boolean) PrefUtils.getFromPrefs(mContext, PrefKeys.IS_MAN_BATTERY_OPTIMIZATION_ACCEPTED, false); - } else { - boolean isManBatteryAvailable = KillerManager.isActionAvailable(mContext, KillerManager.Actions.ACTION_POWERSAVING); - PrefUtils.saveToPrefs(mContext, PrefKeys.IS_MAN_BATTERY_OPTIMIZATION_ACCEPTED, !isManBatteryAvailable); - return !isManBatteryAvailable; - } + boolean isManBatteryAvailable = KillerManager.isActionAvailable(mContext, KillerManager.Actions.ACTION_POWERSAVING); + return !isManBatteryAvailable; } public void setManAutoStart(boolean val) { @@ -276,12 +346,19 @@ public void setManAutoStart(boolean val) { } public boolean getManAutoStart() { - if (PrefUtils.hasKey(mContext, PrefKeys.IS_MAN_AUTO_START_ACCEPTED)) { - return (boolean) PrefUtils.getFromPrefs(mContext, PrefKeys.IS_MAN_AUTO_START_ACCEPTED, false); - } else { - boolean isAutoStartAvailable = KillerManager.isActionAvailable(mContext, KillerManager.Actions.ACTION_AUTOSTART); - PrefUtils.saveToPrefs(mContext, PrefKeys.IS_MAN_AUTO_START_ACCEPTED, !isAutoStartAvailable); - return !isAutoStartAvailable; + PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); + + if (powerManager != null) { + String packageName = mContext.getPackageName(); + boolean isIgnoringBattery = powerManager.isIgnoringBatteryOptimizations(packageName); + Log.i(TAG, "Is ignoring battery: " + isIgnoringBattery); + return !isIgnoringBattery; + } + + if(DevicesManager.getDevice() == null) { + return true; } + boolean isAutoStartAvailable = KillerManager.isActionAvailable(mContext, KillerManager.Actions.ACTION_AUTOSTART); + return !isAutoStartAvailable; } } diff --git a/android/src/main/java/in/jvapps/disable_battery_optimization/managers/KillerManager.java b/android/src/main/java/in/jvapps/disable_battery_optimization/managers/KillerManager.java index 8d09eae..e2fb5cb 100644 --- a/android/src/main/java/in/jvapps/disable_battery_optimization/managers/KillerManager.java +++ b/android/src/main/java/in/jvapps/disable_battery_optimization/managers/KillerManager.java @@ -101,9 +101,9 @@ private static Intent getIntentFromAction(Context context, Actions actions) { } } else { // device not found action failed + LogUtils.e(KillerManager.class.getName(), "DEVICE NOT FOUND" + "SYSTEM UTILS \n" + + SystemUtils.getDefaultDebugInformation()); return null; - /* LogUtils.e(KillerManager.class.getName(), "DEVICE NOT FOUND" + "SYSTEM UTILS \n" + - SystemUtils.getDefaultDebugInformation());*/ } } @@ -126,7 +126,7 @@ public static boolean doAction(Context context, Actions actions) { } catch (Exception e) { // Exception handle action failed - LogUtils.e(KillerManager.class.getName(), e.getMessage()); + LogUtils.e(KillerManager.class.getName(), "INTENT EXCEPTION " + e.getMessage()); return false; } return false; diff --git a/android/src/main/java/in/jvapps/disable_battery_optimization/ui/DialogKillerManagerBuilder.java b/android/src/main/java/in/jvapps/disable_battery_optimization/ui/DialogKillerManagerBuilder.java index 4d84ca7..ab404fe 100644 --- a/android/src/main/java/in/jvapps/disable_battery_optimization/ui/DialogKillerManagerBuilder.java +++ b/android/src/main/java/in/jvapps/disable_battery_optimization/ui/DialogKillerManagerBuilder.java @@ -198,7 +198,6 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { private void initView(View view) { TextView contentTextView = view.findViewById(R.id.md_content); CheckBox doNotShowAgainCheckBox = view.findViewById(R.id.md_promptCheckbox); - ImageView helpImageView = view.findViewById(R.id.md_imageView); if (contentResMessage != -1) { contentTextView.setText(contentResMessage); @@ -221,25 +220,5 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { } }); } - - //TODO add other specific images - int helpImageRes = 0; - switch (mAction) { - case ACTION_AUTOSTART: - helpImageRes = KillerManager.getDevice().getHelpImageAutoStart(); - break; - case ACTION_POWERSAVING: - helpImageRes = KillerManager.getDevice().getHelpImagePowerSaving(); - break; - case ACTION_NOTIFICATIONS: - helpImageRes = KillerManager.getDevice().getHelpImageNotification(); - break; - } - - if (helpImageRes != 0) { - helpImageView.setImageResource(helpImageRes); - }else{ - helpImageView.setVisibility(View.GONE); - } } } \ No newline at end of file diff --git a/android/src/main/java/in/jvapps/disable_battery_optimization/utils/BatteryOptimizationUtil.java b/android/src/main/java/in/jvapps/disable_battery_optimization/utils/BatteryOptimizationUtil.java index 7884408..3cae432 100644 --- a/android/src/main/java/in/jvapps/disable_battery_optimization/utils/BatteryOptimizationUtil.java +++ b/android/src/main/java/in/jvapps/disable_battery_optimization/utils/BatteryOptimizationUtil.java @@ -13,6 +13,7 @@ import androidx.annotation.Nullable; import in.jvapps.disable_battery_optimization.managers.KillerManager; +import in.jvapps.disable_battery_optimization.managers.DevicesManager; import in.jvapps.disable_battery_optimization.ui.DialogKillerManagerBuilder; @@ -33,6 +34,9 @@ public static boolean isIgnoringBatteryOptimizations(Context context) { if (powerManager == null) { return true; } + if(DevicesManager.getDevice() == null) { + return true; + } return powerManager.isIgnoringBatteryOptimizations(packageName); } @@ -53,7 +57,8 @@ public static void showBatteryOptimizationDialog( @Nullable String titleMessage, final String contentMessage, @Nullable final OnBatteryOptimizationAccepted positiveCallback, - @Nullable final OnBatteryOptimizationCanceled negativeCallback) { + @Nullable final OnBatteryOptimizationCanceled negativeCallback, + @Nullable final OnBatteryOptimizationNotAvailable notAvailableCallback) { if (KillerManager.isActionAvailable(context, action)) { if (titleMessage == null) { @@ -78,8 +83,8 @@ public static void showBatteryOptimizationDialog( .setAction(action) .show(); } else { - if (positiveCallback != null) - positiveCallback.onBatteryOptimizationAccepted(); + if (notAvailableCallback != null) + notAvailableCallback.OnBatteryOptimizationNotAvailable(); } } @@ -91,4 +96,8 @@ public interface OnBatteryOptimizationCanceled { void onBatteryOptimizationCanceled(); } + public interface OnBatteryOptimizationNotAvailable { + void OnBatteryOptimizationNotAvailable(); + } + } \ No newline at end of file diff --git a/android/src/main/res/layout/md_dialog_custom_view.xml b/android/src/main/res/layout/md_dialog_custom_view.xml index f356d7d..6c3b105 100644 --- a/android/src/main/res/layout/md_dialog_custom_view.xml +++ b/android/src/main/res/layout/md_dialog_custom_view.xml @@ -4,7 +4,6 @@ android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"> - - - - - - @@ -29,4 +30,4 @@ android:name="flutterEmbedding" android:value="2" /> - + \ No newline at end of file diff --git a/example/android/build.gradle b/example/android/build.gradle index c9e3db0..3a75234 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -1,18 +1,20 @@ buildscript { + ext.kotlin_version = '1.7.10' repositories { google() - jcenter() + mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.0' + classpath 'com.android.tools.build:gradle:7.3.0' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() - jcenter() + mavenCentral() } } @@ -24,6 +26,6 @@ subprojects { project.evaluationDependsOn(':app') } -task clean(type: Delete) { +tasks.register("clean", Delete) { delete rootProject.buildDir -} +} \ No newline at end of file diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties index 0fa2efd..672f73f 100644 --- a/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/example/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip diff --git a/example/android/settings.gradle b/example/android/settings.gradle index 5a2f14f..7d06449 100644 --- a/example/android/settings.gradle +++ b/example/android/settings.gradle @@ -1,15 +1,25 @@ -include ':app' +pluginManagement { + def flutterSdkPath = { + def properties = new Properties() + file("local.properties").withInputStream { properties.load(it) } + def flutterSdkPath = properties.getProperty("flutter.sdk") + assert flutterSdkPath != null, "flutter.sdk not set in local.properties" + return flutterSdkPath + }() -def flutterProjectRoot = rootProject.projectDir.parentFile.toPath() + includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") -def plugins = new Properties() -def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins') -if (pluginsFile.exists()) { - pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) } + repositories { + google() + mavenCentral() + gradlePluginPortal() + } } -plugins.each { name, path -> - def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile() - include ":$name" - project(":$name").projectDir = pluginDirectory +plugins { + id "dev.flutter.flutter-plugin-loader" version "1.0.0" + id "com.android.application" version "7.3.0" apply false + id "org.jetbrains.kotlin.android" version "1.7.10" apply false } + +include ":app" \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index fde994a..a0a6358 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -24,70 +24,79 @@ class _MyAppState extends State { body: Center( child: Column( children: [ + MaterialButton( + child: Text("Call autostart intent"), + onPressed: () { + DisableBatteryOptimization.callAutoStart(); + print("Called auto start"); + }), + MaterialButton( + child: Text("Call battery optimization intent"), + onPressed: () { + DisableBatteryOptimization.callDisableBatteryOptimization(); + print("Called disable battery optimization"); + }), MaterialButton( child: Text("Is Auto Start Enabled"), onPressed: () async { - bool isAutoStartEnabled = - await DisableBatteryOptimization.isAutoStartEnabled; - print( - "Auto start is ${isAutoStartEnabled ? "Enabled" : "Disabled"}"); + final isAutoStartEnabled = await DisableBatteryOptimization.isAutoStartEnabled; + print("Auto start is ${isAutoStartEnabled ? "Enabled" : "Disabled"} - ${isAutoStartEnabled}"); }), MaterialButton( child: Text("Is Battery optimization disabled"), onPressed: () async { - bool isBatteryOptimizationDisabled = - await DisableBatteryOptimization - .isBatteryOptimizationDisabled; + final isBatteryOptimizationDisabled = + await DisableBatteryOptimization.isBatteryOptimizationDisabled; print( - "Battery optimization is ${!isBatteryOptimizationDisabled ? "Enabled" : "Disabled"}"); + "Battery optimization is ${!isBatteryOptimizationDisabled ? "Enabled" : "Disabled"} - ${isBatteryOptimizationDisabled}"); }), MaterialButton( child: Text("Is Manufacturer Battery optimization disabled"), onPressed: () async { - bool isManBatteryOptimizationDisabled = - await DisableBatteryOptimization - .isManufacturerBatteryOptimizationDisabled; + final isManBatteryOptimizationDisabled = + await DisableBatteryOptimization.isManufacturerBatteryOptimizationDisabled; print( "Manufacturer Battery optimization is ${!isManBatteryOptimizationDisabled ? "Enabled" : "Disabled"}"); }), MaterialButton( child: Text("Are All Battery optimizations disabled"), onPressed: () async { - bool isAllBatteryOptimizationDisabled = - await DisableBatteryOptimization - .isAllBatteryOptimizationDisabled; + final isAllBatteryOptimizationDisabled = + await DisableBatteryOptimization.isAllBatteryOptimizationDisabled; print( "All Battery optimizations are disabled ${isAllBatteryOptimizationDisabled ? "True" : "False"}"); }), MaterialButton( child: Text("Enable Auto Start"), - onPressed: () { - DisableBatteryOptimization.showEnableAutoStartSettings( - "Enable Auto Start", - "Follow the steps and enable the auto start of this app"); + onPressed: () async { + final r = await DisableBatteryOptimization.showEnableAutoStartSettings( + "Enable Auto Start", "Follow the steps and enable the auto start of this app"); + print("Result ${r}"); }), MaterialButton( child: Text("Disable Battery Optimizations"), - onPressed: () { - DisableBatteryOptimization - .showDisableBatteryOptimizationSettings(); + onPressed: () async { + final r = await DisableBatteryOptimization.showDisableBatteryOptimizationSettings( + "Disable battery optimization settings", "Follow the steps to disable it"); + print("Result ${r}"); }), MaterialButton( child: Text("Disable Manufacturer Battery Optimizations"), - onPressed: () { - DisableBatteryOptimization - .showDisableManufacturerBatteryOptimizationSettings( - "Your device has additional battery optimization", - "Follow the steps and disable the optimizations to allow smooth functioning of this app"); + onPressed: () async { + final r = await DisableBatteryOptimization.showDisableManufacturerBatteryOptimizationSettings( + "Your device has additional battery optimization", + "Follow the steps and disable the optimizations to allow smooth functioning of this app"); + print("Result ${r}"); }), MaterialButton( child: Text("Disable all Optimizations"), - onPressed: () { - DisableBatteryOptimization.showDisableAllOptimizationsSettings( + onPressed: () async { + final r = await DisableBatteryOptimization.showDisableAllOptimizationsSettings( "Enable Auto Start", "Follow the steps and enable the auto start of this app", "Your device has additional battery optimization", "Follow the steps and disable the optimizations to allow smooth functioning of this app"); + print("Result ${r}"); }) ], ), diff --git a/example/pubspec.lock b/example/pubspec.lock index 34f427c..e6e1c0c 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -37,18 +37,18 @@ packages: dependency: transitive description: name: collection - sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.2" + version: "1.18.0" cupertino_icons: dependency: "direct main" description: name: cupertino_icons - sha256: "1989d917fbe8e6b39806207df5a3fdd3d816cbd090fac2ce26fb45e9a71476e5" + sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6 url: "https://pub.dev" source: hosted - version: "1.0.4" + version: "1.0.8" disable_battery_optimization: dependency: "direct dev" description: @@ -74,38 +74,62 @@ packages: description: flutter source: sdk version: "0.0.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + url: "https://pub.dev" + source: hosted + version: "10.0.4" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + url: "https://pub.dev" + source: hosted + version: "3.0.3" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + url: "https://pub.dev" + source: hosted + version: "3.0.1" matcher: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.12.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" sky_engine: dependency: transitive description: flutter @@ -123,18 +147,18 @@ packages: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -155,10 +179,10 @@ packages: dependency: transitive description: name: test_api - sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.7.0" vector_math: dependency: transitive description: @@ -167,14 +191,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: + vm_service: dependency: transitive description: - name: web - sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + name: vm_service + sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" url: "https://pub.dev" source: hosted - version: "0.1.4-beta" + version: "14.2.1" sdks: - dart: ">=3.1.0-185.0.dev <4.0.0" - flutter: ">=1.12.0" + dart: ">=3.3.0 <4.0.0" + flutter: ">=3.18.0-18.0.pre.54" diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart index 17076f0..d9adc26 100644 --- a/example/test/widget_test.dart +++ b/example/test/widget_test.dart @@ -8,7 +8,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:disable_battery_optimization_example/main.dart'; +import '../lib/main.dart'; void main() { testWidgets('Verify Platform version', (WidgetTester tester) async { @@ -18,8 +18,7 @@ void main() { // Verify that platform version is retrieved. expect( find.byWidgetPredicate( - (Widget widget) => - widget is Text && widget.data.startsWith('Running on:'), + (Widget widget) => widget is Text && widget.data!.startsWith('Running on:'), ), findsOneWidget, ); diff --git a/lib/disable_battery_optimization.dart b/lib/disable_battery_optimization.dart index 8b7f539..85d4f3d 100644 --- a/lib/disable_battery_optimization.dart +++ b/lib/disable_battery_optimization.dart @@ -2,52 +2,71 @@ import 'dart:async'; import 'package:flutter/services.dart'; +enum ReturnValue { + enabled, + disabled, + notAvailable, +} + class DisableBatteryOptimization { - static const MethodChannel _channel = - const MethodChannel('in.jvapps.disable_battery_optimization'); + static const MethodChannel _channel = const MethodChannel('in.jvapps.disable_battery_optimization'); - static Future showEnableAutoStartSettings( - String dialogTitle, String dialogBody) async { - return await _channel.invokeMethod( - 'showEnableAutoStart', [dialogTitle, dialogBody]); + static Future showEnableAutoStartSettings(String dialogTitle, String dialogBody) async { + final value = await _channel.invokeMethod('showEnableAutoStart', [dialogTitle, dialogBody]); + return _parseReturnValue(value); } - static Future showDisableManufacturerBatteryOptimizationSettings( + static Future showDisableManufacturerBatteryOptimizationSettings( String dialogTitle, String dialogBody) async { - return await _channel.invokeMethod('showDisableManBatteryOptimization', - [dialogTitle, dialogBody]); + final value = await _channel.invokeMethod('showDisableManBatteryOptimization', [dialogTitle, dialogBody]); + return _parseReturnValue(value); } - static Future showDisableBatteryOptimizationSettings() async { - return await _channel.invokeMethod('showDisableBatteryOptimization'); + static Future showDisableBatteryOptimizationSettings(String dialogTitle, String dialogBody) async { + final value = await _channel.invokeMethod('showDisableBatteryOptimization', [dialogTitle, dialogBody]); + return _parseReturnValue(value); } - static Future showDisableAllOptimizationsSettings( - String autoStartTitle, - String autoStartBody, - String manBatteryTitle, - String manBatteryBody) async { - return await _channel.invokeMethod('disableAllOptimizations', [ - autoStartTitle, - autoStartBody, - manBatteryTitle, - manBatteryBody - ]); + static Future showDisableAllOptimizationsSettings( + String autoStartTitle, String autoStartBody, String manBatteryTitle, String manBatteryBody) async { + final value = await _channel.invokeMethod( + 'disableAllOptimizations', [autoStartTitle, autoStartBody, manBatteryTitle, manBatteryBody]); + return _parseReturnValue(value); } - static Future get isAutoStartEnabled async { + static Future get isAutoStartEnabled async { return await _channel.invokeMethod("isAutoStartEnabled"); } - static Future get isBatteryOptimizationDisabled async { + static Future get isBatteryOptimizationDisabled async { return await _channel.invokeMethod("isBatteryOptimizationDisabled"); } - static Future get isManufacturerBatteryOptimizationDisabled async { + static Future get isManufacturerBatteryOptimizationDisabled async { return await _channel.invokeMethod("isManBatteryOptimizationDisabled"); } - static Future get isAllBatteryOptimizationDisabled async { + static Future get isAllBatteryOptimizationDisabled async { return await _channel.invokeMethod("isAllOptimizationsDisabled"); } + + static void callAutoStart() { + _channel.invokeMethod("callAutoStart"); + } + + static void callDisableBatteryOptimization() { + _channel.invokeMethod("callDisableBatteryOptimization"); + } + + static ReturnValue _parseReturnValue(String value) { + switch (value) { + case "enabled": + return ReturnValue.enabled; + case "disabled": + return ReturnValue.disabled; + default: + } + print("Value ${value}"); + return ReturnValue.notAvailable; + } } diff --git a/pubspec.lock b/pubspec.lock index 5143cf1..203ecd5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: collection - sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.2" + version: "1.18.0" fake_async: dependency: transitive description: @@ -59,38 +59,62 @@ packages: description: flutter source: sdk version: "0.0.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + url: "https://pub.dev" + source: hosted + version: "10.0.4" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + url: "https://pub.dev" + source: hosted + version: "3.0.3" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + url: "https://pub.dev" + source: hosted + version: "3.0.1" matcher: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.12.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" sky_engine: dependency: transitive description: flutter @@ -108,18 +132,18 @@ packages: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -140,10 +164,10 @@ packages: dependency: transitive description: name: test_api - sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.7.0" vector_math: dependency: transitive description: @@ -152,14 +176,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: + vm_service: dependency: transitive description: - name: web - sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + name: vm_service + sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" url: "https://pub.dev" source: hosted - version: "0.1.4-beta" + version: "14.2.1" sdks: - dart: ">=3.1.0-185.0.dev <4.0.0" - flutter: ">=1.12.0" + dart: ">=3.3.0 <4.0.0" + flutter: ">=3.18.0-18.0.pre.54"