Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example app #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;

import in.jvapps.disable_battery_optimization.utils.BatteryOptimizationUtil;
import in.jvapps.disable_battery_optimization.utils.BatteryOptimizationUtil.OnBatteryOptimizationDone;
import in.jvapps.disable_battery_optimization.utils.PrefKeys;
import in.jvapps.disable_battery_optimization.utils.PrefUtils;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
Expand All @@ -26,10 +27,12 @@
/**
* DisableBatteryOptimizationPlugin
*/
public class DisableBatteryOptimizationPlugin implements FlutterPlugin, ActivityAware, MethodCallHandler {
public class DisableBatteryOptimizationPlugin implements FlutterPlugin, ActivityAware, MethodCallHandler,
PluginRegistry.ActivityResultListener{

private Context mContext;
private Activity mActivity;
private BatteryOptimizationUtil.OnBatteryOptimizationDone onBatteryOptimizationDone;

// These are null when not using v2 embedding.
private MethodChannel channel;
Expand All @@ -44,6 +47,21 @@ public class DisableBatteryOptimizationPlugin implements FlutterPlugin, Activity
private String manBatteryMessage;


@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent intent){
Log.e(TAG, "onActivityResult " + requestCode +" " + resultCode);
switch (requestCode) {
case REQUEST_DISABLE_BATTERY_OPTIMIZATIONS:
if (onBatteryOptimizationDone != null)
onBatteryOptimizationDone.onBatteryOptimizationDone(true);
else
Log.e(TAG, "no callback for onActivityResult");

onBatteryOptimizationDone = null;
}
return true;
}

// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
Expand Down Expand Up @@ -99,8 +117,9 @@ 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, (res) -> {
result.success(res);
});
} else {
Log.e(TAG, "Unable to request disable manufacturer battery optimization. Arguments are null");
result.success(false);
Expand All @@ -112,8 +131,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
break;
case "showDisableBatteryOptimization":
try {
showIgnoreBatteryPermissions();
result.success(true);
showIgnoreBatteryPermissions((res) -> {
result.success(res);
});
} catch (Exception ex) {
Log.e(TAG, "Exception in showDisableBatteryOptimization. " + ex.toString());
result.success(false);
Expand All @@ -127,8 +147,9 @@ 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((res) -> {
result.success(res);
});
} else {
Log.e(TAG, "Unable to request disable all optimizations. Arguments are null");
result.success(false);
Expand Down Expand Up @@ -157,7 +178,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {

channel = new MethodChannel(binding.getBinaryMessenger(), CHANNEL_NAME);
mContext = binding.getApplicationContext();
}
Expand All @@ -171,6 +191,7 @@ public void onAttachedToActivity(ActivityPluginBinding binding) {
mActivity = binding.getActivity();
mContext = mActivity.getApplicationContext();
channel.setMethodCallHandler(this);
binding.addActivityResultListener(this);
}

@Override
Expand Down Expand Up @@ -201,7 +222,7 @@ private void showAutoStartEnabler(@NonNull final BatteryOptimizationUtil.OnBatte
);
}

private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler) {
private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler, final BatteryOptimizationUtil.OnBatteryOptimizationDone doneCallback) {
BatteryOptimizationUtil.showBatteryOptimizationDialog(
mActivity,
KillerManager.Actions.ACTION_POWERSAVING,
Expand All @@ -210,50 +231,67 @@ private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOp
() -> {
setManBatteryOptimization(true);
if (isRequestNativeBatteryOptimizationDisabler) {
showIgnoreBatteryPermissions();
showIgnoreBatteryPermissions(doneCallback);
} else {
doneCallback.onBatteryOptimizationDone(true);
}
},
() -> {
if (isRequestNativeBatteryOptimizationDisabler) {
showIgnoreBatteryPermissions();
showIgnoreBatteryPermissions(doneCallback);
} else {
doneCallback.onBatteryOptimizationDone(true);
}
}
);
}

private void showIgnoreBatteryPermissions() {
private void showIgnoreBatteryPermissions(@NonNull final BatteryOptimizationUtil.OnBatteryOptimizationDone doneCallback) {
boolean result = false;
if (!BatteryOptimizationUtil.isIgnoringBatteryOptimizations(mContext)) {
final Intent ignoreBatteryOptimizationsIntent = BatteryOptimizationUtil.getIgnoreBatteryOptimizationsIntent(mContext);
if (ignoreBatteryOptimizationsIntent != null) {
mContext.startActivity(ignoreBatteryOptimizationsIntent);
final Intent ignoreBatteryOptimizationsIntent = BatteryOptimizationUtil
.getIgnoreBatteryOptimizationsIntent(mContext);
if (ignoreBatteryOptimizationsIntent != null && onBatteryOptimizationDone == null) {
onBatteryOptimizationDone = doneCallback;
mActivity.startActivityForResult(ignoreBatteryOptimizationsIntent, REQUEST_DISABLE_BATTERY_OPTIMIZATIONS);
return;
} else if ( onBatteryOptimizationDone != null ){
Log.w(TAG, "Can't ignore the battery optimization as another battery system dialog is already running");
result = false;
} else {
Log.i(TAG, "Can't ignore the battery optimization as the intent is null");
Log.w(TAG, "Can't ignore the battery optimization as the intent is null");
result = false;
}
} else {
Log.i(TAG, "Battery optimization is already disabled");
result = true;
}

if (doneCallback != null)
doneCallback.onBatteryOptimizationDone(result);
}

private void handleIgnoreAllBatteryPermission() {
private void handleIgnoreAllBatteryPermission(@NonNull final BatteryOptimizationUtil.OnBatteryOptimizationDone doneCallback) {
boolean isManBatteryOptimizationDisabled = getManBatteryOptimization();
if (!getManAutoStart()) {
showAutoStartEnabler(() -> {
setManAutoStart(true);
if (!isManBatteryOptimizationDisabled)
showManBatteryOptimizationDisabler(true);
showManBatteryOptimizationDisabler(true, doneCallback);
else
showIgnoreBatteryPermissions();
showIgnoreBatteryPermissions(doneCallback);
}, () -> {
if (!isManBatteryOptimizationDisabled)
showManBatteryOptimizationDisabler(true);
else
showIgnoreBatteryPermissions();
showManBatteryOptimizationDisabler(true, doneCallback);
else {
showIgnoreBatteryPermissions(doneCallback);
}
});
} else {
if (!isManBatteryOptimizationDisabled)
showManBatteryOptimizationDisabler(true);
showManBatteryOptimizationDisabler(true, doneCallback);
else
showIgnoreBatteryPermissions();
showIgnoreBatteryPermissions(doneCallback);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public void show() {
negativeBtnStr = mContext.getText(android.R.string.cancel).toString();
}

builder.cancelable(false);

builder.positiveText(positiveBtnStr)
.customView(R.layout.md_dialog_custom_view, false)
.onPositive(new MaterialDialog.SingleButtonCallback() {
Expand Down Expand Up @@ -242,4 +244,4 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
helpImageView.setVisibility(View.GONE);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public static Intent getIgnoreBatteryOptimizationsIntent(Context context) {
String sb = "package:" +
context.getApplicationContext().getPackageName();
@SuppressLint("BatteryLife") Intent intent = new Intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse(sb));
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
// NOT using FLAG_ACTIVITY_NEW_TASK to allow having an activity result callback at the end of the activity
// according to Gemini, this should be ok security wise for this particular intent
return intent.resolveActivity(context.getPackageManager()) == null ? getAppSettingsIntent(context) : intent;
}

Expand Down Expand Up @@ -91,4 +92,8 @@ public interface OnBatteryOptimizationCanceled {
void onBatteryOptimizationCanceled();
}

}
public interface OnBatteryOptimizationDone {
void onBatteryOptimizationDone(boolean result);
}

}
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {}
public class MainActivity extends FlutterActivity {
}
21 changes: 10 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,31 @@ class _MyAppState extends State<MyApp> {
child: Text("Is Auto Start Enabled"),
onPressed: () async {
bool isAutoStartEnabled =
await DisableBatteryOptimization.isAutoStartEnabled;
await DisableBatteryOptimization.isAutoStartEnabled ?? false;
print(
"Auto start is ${isAutoStartEnabled ? "Enabled" : "Disabled"}");
}),
MaterialButton(
child: Text("Is Battery optimization disabled"),
onPressed: () async {
bool isBatteryOptimizationDisabled =
await DisableBatteryOptimization
.isBatteryOptimizationDisabled;
await DisableBatteryOptimization.isBatteryOptimizationDisabled ?? false;
print(
"Battery optimization is ${!isBatteryOptimizationDisabled ? "Enabled" : "Disabled"}");
}),
MaterialButton(
child: Text("Is Manufacturer Battery optimization disabled"),
onPressed: () async {
bool isManBatteryOptimizationDisabled =
await DisableBatteryOptimization
.isManufacturerBatteryOptimizationDisabled;
await DisableBatteryOptimization.isManufacturerBatteryOptimizationDisabled ?? false;
print(
"Manufacturer Battery optimization is ${!isManBatteryOptimizationDisabled ? "Enabled" : "Disabled"}");
}),
MaterialButton(
child: Text("Are All Battery optimizations disabled"),
onPressed: () async {
bool isAllBatteryOptimizationDisabled =
await DisableBatteryOptimization
.isAllBatteryOptimizationDisabled;
await DisableBatteryOptimization.isAllBatteryOptimizationDisabled ?? false;
print(
"All Battery optimizations are disabled ${isAllBatteryOptimizationDisabled ? "True" : "False"}");
}),
Expand All @@ -68,22 +65,24 @@ class _MyAppState extends State<MyApp> {
}),
MaterialButton(
child: Text("Disable Battery Optimizations"),
onPressed: () {
onPressed: () async {
DisableBatteryOptimization
.showDisableBatteryOptimizationSettings();
await DisableBatteryOptimization.showDisableBatteryOptimizationSettings();
}),
MaterialButton(
child: Text("Disable Manufacturer Battery Optimizations"),
onPressed: () {
DisableBatteryOptimization
onPressed: () async {
await DisableBatteryOptimization
.showDisableManufacturerBatteryOptimizationSettings(
"Your device has additional battery optimization",
"Follow the steps and disable the optimizations to allow smooth functioning of this app");
}),
MaterialButton(
child: Text("Disable all Optimizations"),
onPressed: () {
onPressed: () async {
DisableBatteryOptimization.showDisableAllOptimizationsSettings(
await DisableBatteryOptimization.showDisableAllOptimizationsSettings(
"Enable Auto Start",
"Follow the steps and enable the auto start of this app",
"Your device has additional battery optimization",
Expand Down