pvsvamsi / Disable-Battery-Optimizations

A Flutter plugin to enable auto start and disable battery optimizations. Also shows custom steps to disable the optimizations in devices like mi, xiaomi, samsung, oppo, huawei, oneplus etc
MIT License
28 stars 29 forks source link

xiaomi bug (maybe affects all devices) with somewhat working solution #2

Closed torian257x closed 11 months ago

torian257x commented 3 years ago

old

    private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler) {
        BatteryOptimizationUtil.showBatteryOptimizationDialog(
                mContext,
                KillerManager.Actions.ACTION_POWERSAVING,
                manBatteryTitle,
                manBatteryMessage,
                () -> {
                    setManBatteryOptimization(true);
                    if (isRequestNativeBatteryOptimizationDisabler) {
                        showIgnoreBatteryPermissions();
                    }
                },
                () -> {
                    setManBatteryOptimization(false);
                    if (isRequestNativeBatteryOptimizationDisabler) {
                        showIgnoreBatteryPermissions();
                    }
                }
        );
    }

causes

BadTokenException or InvalidDisplayException, clean up.

Exception in showDisableManBatteryOptimization. com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.

fix according: https://stackoverflow.com/questions/34481525/bad-window-token-you-cannot-show-a-dialog-before-an-activity-is-created-or-afte

    private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler) {
        BatteryOptimizationUtil.showBatteryOptimizationDialog(
                mActivity,
                KillerManager.Actions.ACTION_POWERSAVING,
                manBatteryTitle,
                manBatteryMessage,
                () -> {
                    setManBatteryOptimization(true);
                    if (isRequestNativeBatteryOptimizationDisabler) {
                        showIgnoreBatteryPermissions();
                    }
                },
                () -> {
                    setManBatteryOptimization(false);
                    if (isRequestNativeBatteryOptimizationDisabler) {
                        showIgnoreBatteryPermissions();
                    }
                }
        );
    }

notice I assume there are other functions affected by this peculiarity as well e.g. all that call showBatteryOptimizationDialog

SOMEWHAT WORKING after applying the fix, I get the popup with explanation. What doesn't work if I press on OK to change the actual setting. Nothing opens.

saivarunr commented 3 years ago

Device: OnePlus 9R

Error: Exception in disableAllOptimizations. com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.

Error/Exception on clicking 'disable all optimizations'

saivarunr commented 3 years ago

old

    private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler) {
        BatteryOptimizationUtil.showBatteryOptimizationDialog(
                mContext,
                KillerManager.Actions.ACTION_POWERSAVING,
                manBatteryTitle,
                manBatteryMessage,
                () -> {
                    setManBatteryOptimization(true);
                    if (isRequestNativeBatteryOptimizationDisabler) {
                        showIgnoreBatteryPermissions();
                    }
                },
                () -> {
                    setManBatteryOptimization(false);
                    if (isRequestNativeBatteryOptimizationDisabler) {
                        showIgnoreBatteryPermissions();
                    }
                }
        );
    }

causes

BadTokenException or InvalidDisplayException, clean up.

Exception in showDisableManBatteryOptimization. com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.

fix according: https://stackoverflow.com/questions/34481525/bad-window-token-you-cannot-show-a-dialog-before-an-activity-is-created-or-afte

    private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler) {
        BatteryOptimizationUtil.showBatteryOptimizationDialog(
                mActivity,
                KillerManager.Actions.ACTION_POWERSAVING,
                manBatteryTitle,
                manBatteryMessage,
                () -> {
                    setManBatteryOptimization(true);
                    if (isRequestNativeBatteryOptimizationDisabler) {
                        showIgnoreBatteryPermissions();
                    }
                },
                () -> {
                    setManBatteryOptimization(false);
                    if (isRequestNativeBatteryOptimizationDisabler) {
                        showIgnoreBatteryPermissions();
                    }
                }
        );
    }

notice I assume there are other functions affected by this peculiarity as well e.g. all that call showBatteryOptimizationDialog

SOMEWHAT WORKING after applying the fix, I get the popup with explanation. What doesn't work if I press on OK to change the actual setting. Nothing opens.

@Jossnaz You have a pull request for this or a modified fork?

torian257x commented 3 years ago

No I manually patched it in the code of the project (I'm such a bad bad boi I know)

showBatteryOptimizationDialog is called in 2 places Just change mContext with mActivity in the 2 places

for me it's just a side project so no point in trying to force PRs on someone who doesn't want them (so it seems)

On Sun, 9 May 2021 at 21:58, Sai Varun Reddy Daram @.***> wrote:

old

private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler) {
    BatteryOptimizationUtil.showBatteryOptimizationDialog(
            mContext,
            KillerManager.Actions.ACTION_POWERSAVING,
            manBatteryTitle,
            manBatteryMessage,
            () -> {
                setManBatteryOptimization(true);
                if (isRequestNativeBatteryOptimizationDisabler) {
                    showIgnoreBatteryPermissions();
                }
            },
            () -> {
                setManBatteryOptimization(false);
                if (isRequestNativeBatteryOptimizationDisabler) {
                    showIgnoreBatteryPermissions();
                }
            }
    );
}

causes

BadTokenException or InvalidDisplayException, clean up.

Exception in showDisableManBatteryOptimization. com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.

fix according:

https://stackoverflow.com/questions/34481525/bad-window-token-you-cannot-show-a-dialog-before-an-activity-is-created-or-afte

private void showManBatteryOptimizationDisabler(boolean isRequestNativeBatteryOptimizationDisabler) {
    BatteryOptimizationUtil.showBatteryOptimizationDialog(
            mActivity,
            KillerManager.Actions.ACTION_POWERSAVING,
            manBatteryTitle,
            manBatteryMessage,
            () -> {
                setManBatteryOptimization(true);
                if (isRequestNativeBatteryOptimizationDisabler) {
                    showIgnoreBatteryPermissions();
                }
            },
            () -> {
                setManBatteryOptimization(false);
                if (isRequestNativeBatteryOptimizationDisabler) {
                    showIgnoreBatteryPermissions();
                }
            }
    );
}

notice I assume there are other functions affected by this peculiarity as well e.g. all that call showBatteryOptimizationDialog

SOMEWHAT WORKING after applying the fix, I get the popup with explanation. What doesn't work if I press on OK to change the actual setting. Nothing opens.

@Jossnaz https://github.com/Jossnaz You have a pull request for this or a modified fork?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pvsvamsi/Disable-Battery-Optimizations/issues/2#issuecomment-836098369, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPZ36UPFJXPQX4TOSRKKVDTM5DT3ANCNFSM434EQCKA .

pvsvamsi commented 11 months ago

This is fixed in latest versions