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

Add title and body to disable battery optimization #19

Open
wants to merge 9 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
4 changes: 2 additions & 2 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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":
Expand All @@ -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":
Expand All @@ -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":
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}

Expand All @@ -262,26 +337,28 @@ 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) {
PrefUtils.saveToPrefs(mContext, PrefKeys.IS_MAN_AUTO_START_ACCEPTED, 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());*/
}
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
Loading