Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d00cd214 authored by Mark Harman's avatar Mark Harman Committed by Mohammed Althaf T
Browse files

Avoid use of settingsOnBackPressedCallback.

parent 1d923270
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ Version 1.53 (Work in progress)

UPDATED Improvements for popup menu and exposure UI when using large font sizes.
UPDATED Made user's font size preference apply to on-screen text.
UPDATED Changes in preparation for back button behaviour for future Android versions.

Version 1.52 (2023/08/13)

+1 −34
Original line number Diff line number Diff line
@@ -848,8 +848,6 @@ public class MainActivity extends AppCompatActivity {
        }).start();

        // handle on back behaviour
        settingsOnBackPressedCallback = new SettingsOnBackPressedCallback(false);
        this.getOnBackPressedDispatcher().addCallback(this, settingsOnBackPressedCallback);
        popupOnBackPressedCallback = new PopupOnBackPressedCallback(false);
        this.getOnBackPressedDispatcher().addCallback(this, popupOnBackPressedCallback);
        pausePreviewOnBackPressedCallback = new PausePreviewOnBackPressedCallback(false);
@@ -3626,7 +3624,7 @@ public class MainActivity extends AppCompatActivity {

    /** Call when the settings is going to be closed.
     */
    private void settingsClosing() {
    void settingsClosing() {
        if( MyDebug.LOG )
            Log.d(TAG, "close settings");
        setWindowFlagsForCamera();
@@ -3666,37 +3664,6 @@ public class MainActivity extends AppCompatActivity {
        }
    }

    private SettingsOnBackPressedCallback settingsOnBackPressedCallback;

    private class SettingsOnBackPressedCallback extends OnBackPressedCallback {
        public SettingsOnBackPressedCallback(boolean enabled) {
            super(enabled);
            if( MyDebug.LOG )
                Log.d(TAG, "SettingsOnBackPressedCallback, enabled: " + enabled);
        }

        @Override
        public void handleOnBackPressed() {
            if( MyDebug.LOG )
                Log.d(TAG, "SettingsOnBackPressedCallback.handleOnBackPressed");
            if( settingsIsOpen() ) {
                settingsClosing();
                // we still want the back button to take effect (we have a callback to know when the back button is pressed during
                // settings, so we can call settingsClosing())
                this.setEnabled(false); // need to disable this so we don't get this callback just being called again
                MainActivity.this.onBackPressed();
            }
        }
    }

    public void enableSettingsOnBackPressedCallback(boolean enabled) {
        if( MyDebug.LOG )
            Log.d(TAG, "enableSettingsOnBackPressedCallback: " + enabled);
        if( settingsOnBackPressedCallback != null ) {
            settingsOnBackPressedCallback.setEnabled(enabled);
        }
    }

    private PopupOnBackPressedCallback popupOnBackPressedCallback;

    private class PopupOnBackPressedCallback extends OnBackPressedCallback {
+8 −5
Original line number Diff line number Diff line
@@ -1545,8 +1545,6 @@ public class MyPreferenceFragment extends PreferenceFragment implements OnShared
        }

        setupDependencies();

        ((MainActivity)getActivity()).enableSettingsOnBackPressedCallback(true);
    }


@@ -1759,9 +1757,14 @@ public class MyPreferenceFragment extends PreferenceFragment implements OnShared
            Log.d(TAG, "onDestroy");
        super.onDestroy();

        // in theory this shouldn't be needed (if user presses back to exit settings, we'll have already disabled the callback),
        // but just in case the preference fragment is closed for a different reason
        ((MainActivity)getActivity()).enableSettingsOnBackPressedCallback(false);
        if( MyDebug.LOG )
            Log.d(TAG, "isRemoving?: " + isRemoving());

        if( isRemoving() ) {
            // if isRemoving()==true, then it means the fragment is being removed and we are returning to the activity
            // if isRemoving()==false, then it may be that the activity is being destroyed
            ((MainActivity)getActivity()).settingsClosing();
        }

        // dismiss open dialogs - see comment for dialogs for why we do this
        for(AlertDialog dialog : dialogs) {