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

Verified Commit 9ba3c413 authored by Manu Suresh's avatar Manu Suresh
Browse files

FP6: parts: make switch service async and transient

parent 6e5335b2
Loading
Loading
Loading
Loading
+24 −5
Original line number Diff line number Diff line
@@ -75,16 +75,27 @@ public class SliderSwitchHandlerService extends Service {
        Log.d(TAG, "Service started");
    }

    private void finishTask(int startId) {
        Log.d(TAG, "Stopping service for startId: " + startId);
        stopSelf(startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent == null) {
            finishTask(startId);
            return START_NOT_STICKY;
        }
        Context context = getApplicationContext();
        String selection = SliderUtils.getCurrentOption(context);
        handleSliderState(selection, SliderUtils.getCurrentFallbackState(intent));
        Log.d(TAG, "Processing startId: " + startId + " for " + selection);
        handleSliderState(selection, SliderUtils.getCurrentFallbackState(intent), startId);
        SliderUtils.notifyChanged(context);
        return START_STICKY;
        return START_REDELIVER_INTENT;
    }

    private void handleSliderState(String selection, boolean state) {
    private void handleSliderState(String selection, boolean state, int startId) {
        boolean isAsync = false;
        try {
            switch (selection) {
                case SliderUtils.KEY_SENSOR_PRIVACY:
@@ -93,7 +104,8 @@ public class SliderSwitchHandlerService extends Service {
                    break;

                case SliderUtils.KEY_FAIRPHONE_MOMENTS:
                    handleMoments(state);
                    isAsync = true;
                    handleMoments(state, startId);
                    break;

                case SliderUtils.KEY_DO_NOT_DISTURB:
@@ -128,6 +140,9 @@ public class SliderSwitchHandlerService extends Service {
        } finally {
            doVibration();
            Log.i(TAG, "Processed slider state: " + state + " for " + selection);
            if (!isAsync) {
                finishTask(startId);
            }
        }
    }

@@ -149,7 +164,7 @@ public class SliderSwitchHandlerService extends Service {
        }
    }

    private void handleMoments(boolean state) {
    private void handleMoments(boolean state, final int startId) {
        LauncherSwitcherService switcherService = new LauncherSwitcherService();

        if (state) {
@@ -157,12 +172,14 @@ public class SliderSwitchHandlerService extends Service {
                @Override
                public void onSuccess() {
                    Log.d(TAG, "Successfully switched to Fairphone Moments");
                    finishTask(startId);
                }

                @Override
                public void onError(Exception exception) {
                    Log.e(TAG, "Failed to switch to Fairphone Moments: " + exception.getMessage(), exception);
                    // Fallback to original behavior or show error to user
                    finishTask(startId);
                }
            });
        } else {
@@ -170,11 +187,13 @@ public class SliderSwitchHandlerService extends Service {
                @Override
                public void onSuccess() {
                    Log.d(TAG, "Successfully switched to user launcher");
                    finishTask(startId);
                }

                @Override
                public void onError(Exception exception) {
                    Log.e(TAG, "Failed to switch to user launcher: " + exception.getMessage(), exception);
                    finishTask(startId);
                }
            });
        }