Merge "[3/n] Move new picker logic to AOSP" into sc-dev
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/model/themedicon/ThemedIconSectionController.java b/src/com/android/customization/model/themedicon/ThemedIconSectionController.java
index 20f3746..32fbfa8 100644
--- a/src/com/android/customization/model/themedicon/ThemedIconSectionController.java
+++ b/src/com/android/customization/model/themedicon/ThemedIconSectionController.java
@@ -25,12 +25,17 @@
 import com.android.wallpaper.model.HubSectionController;
 import com.android.wallpaper.model.WorkspaceViewModel;
 
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
 /** The {@link HubSectionController} for themed icon section. */
 public class ThemedIconSectionController implements HubSectionController<ThemedIconSectionView> {
 
     private final ThemedIconSwitchProvider mThemedIconOptionsProvider;
     private final WorkspaceViewModel mWorkspaceViewModel;
 
+    private static ExecutorService sExecutorService = Executors.newSingleThreadExecutor();
+
     public ThemedIconSectionController(ThemedIconSwitchProvider themedIconOptionsProvider,
             WorkspaceViewModel workspaceViewModel) {
         mThemedIconOptionsProvider = themedIconOptionsProvider;
@@ -48,8 +53,11 @@
                 (ThemedIconSectionView) LayoutInflater.from(context).inflate(
                         R.layout.themed_icon_section_view, /* root= */ null);
         themedIconColorSectionView.setViewListener(this::onViewActivated);
-        themedIconColorSectionView.getSwitch()
-                .setChecked(mThemedIconOptionsProvider.fetchThemedIconEnabled());
+        sExecutorService.submit(() -> {
+            boolean themedIconEnabled = mThemedIconOptionsProvider.fetchThemedIconEnabled();
+            themedIconColorSectionView.post(() ->
+                    themedIconColorSectionView.getSwitch().setChecked(themedIconEnabled));
+        });
         return themedIconColorSectionView;
     }
 
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() {