pvsvamsi/Disable-Battery-Optimizations

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

Closed this issue · 4 comments

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.

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'

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?

This is fixed in latest versions