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
diff --git a/src/com/android/customization/model/mode/ModeSection.java b/src/com/android/customization/model/mode/ModeSection.java
index 493774d..9096e07 100644
--- a/src/com/android/customization/model/mode/ModeSection.java
+++ b/src/com/android/customization/model/mode/ModeSection.java
@@ -39,15 +39,21 @@
 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 @@
     @OnLifecycleEvent(Lifecycle.Event.ON_START)
     @MainThread
     public void onStart() {
-        if (mContext != null) {
-            mContext.registerReceiver(mBatterySaverStateReceiver,
-                    new IntentFilter(ACTION_POWER_SAVE_MODE_CHANGED));
-        }
+        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() {
-        if (mContext != null && mBatterySaverStateReceiver != null) {
-            mContext.unregisterReceiver(mBatterySaverStateReceiver);
-        }
+        sExecutorService.submit(() -> {
+            if (mContext != null && mBatterySaverStateReceiver != null) {
+                mContext.unregisterReceiver(mBatterySaverStateReceiver);
+            }
+        });
     }
 
     @Override
diff --git a/src/com/android/customization/picker/mode/ModeSectionView.java b/src/com/android/customization/picker/mode/ModeSectionView.java
index 090dce1..37e90a5 100644
--- a/src/com/android/customization/picker/mode/ModeSectionView.java
+++ b/src/com/android/customization/picker/mode/ModeSectionView.java
@@ -47,7 +47,8 @@
         switchView.setOnCheckedChangeListener((buttonView, isChecked) ->
                 switchView.setChecked(mIsDarkModeActivated)
         );
-        setOnClickListener(view -> modeToggleClicked());
+        setOnClickListener(
+                view -> switchView.postDelayed(() -> modeToggleClicked(), /* delayMillis= */ 100));
     }
 
     private void modeToggleClicked() {