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

Commit d9ee34df authored by Ching-Sung Li's avatar Ching-Sung Li Committed by Ching Sung Li
Browse files

Register power-save broadcast receiver without blocking UI thread

Register power-save broadcast receiver in the background thread to not
block UI thread as workaround while system is busy.

Bug: 190333611
Test: Manual
Change-Id: I097bcf8baa9776462f62b8fcaa35d40b5de0a188
parent b780674c
Loading
Loading
Loading
Loading
+20 −9
Original line number Diff line number Diff line
@@ -39,15 +39,21 @@ import com.android.wallpaper.R;
import com.android.wallpaper.model.HubSectionController;
import com.android.wallpaper.model.HubSectionController.HubSectionBatterySaverListener;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Section for dark theme toggle that controls if this section will be shown visually
 */
public class ModeSection implements HubSectionController<ModeSectionView>, LifecycleObserver,
        HubSectionBatterySaverListener {

    private final Lifecycle mLifecycle;
    private final BatterySaverStateReceiver mBatterySaverStateReceiver;

    private static ExecutorService sExecutorService = Executors.newSingleThreadExecutor();

    private Context mContext;
    private Lifecycle mLifecycle;
    private BatterySaverStateReceiver mBatterySaverStateReceiver;
    private ModeSectionView mModeSectionView;

    public ModeSection(Context context, Lifecycle lifecycle) {
@@ -60,18 +66,23 @@ public class ModeSection implements HubSectionController<ModeSectionView>, Lifec
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    @MainThread
    public void onStart() {
        if (mContext != null) {
        sExecutorService.submit(() -> {
            if (mContext != null && mLifecycle.getCurrentState().isAtLeast(
                    Lifecycle.State.STARTED)) {
                mContext.registerReceiver(mBatterySaverStateReceiver,
                        new IntentFilter(ACTION_POWER_SAVE_MODE_CHANGED));
            }
        });
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    @MainThread
    public void onStop() {
        sExecutorService.submit(() -> {
            if (mContext != null && mBatterySaverStateReceiver != null) {
                mContext.unregisterReceiver(mBatterySaverStateReceiver);
            }
        });
    }

    @Override
+2 −1
Original line number Diff line number Diff line
@@ -47,7 +47,8 @@ public final class ModeSectionView extends SectionView {
        switchView.setOnCheckedChangeListener((buttonView, isChecked) ->
                switchView.setChecked(mIsDarkModeActivated)
        );
        setOnClickListener(view -> modeToggleClicked());
        setOnClickListener(
                view -> switchView.postDelayed(() -> modeToggleClicked(), /* delayMillis= */ 100));
    }

    private void modeToggleClicked() {