Save themed icon switch's chcked state in savedInstanceState

Add section views with tasks in the message queue (view#post()) running
separately would see themed icon switch's checkd state changing quickly.
Save the state to the savedInstanceState and restore it could prevent
this phenomenon.

Bug: 192200516
Test: Manual
Change-Id: I04309bd634e2b9a240570fe304dfa34372c2b6df
diff --git a/src/com/android/customization/model/themedicon/ThemedIconSectionController.java b/src/com/android/customization/model/themedicon/ThemedIconSectionController.java
index c097b6b..a1623d1 100644
--- a/src/com/android/customization/model/themedicon/ThemedIconSectionController.java
+++ b/src/com/android/customization/model/themedicon/ThemedIconSectionController.java
@@ -16,6 +16,7 @@
 package com.android.customization.model.themedicon;
 
 import android.content.Context;
+import android.os.Bundle;
 import android.view.LayoutInflater;
 
 import androidx.annotation.Nullable;
@@ -29,14 +30,24 @@
 public class ThemedIconSectionController implements
         CustomizationSectionController<ThemedIconSectionView> {
 
+    private static final String KEY_THEMED_ICON_ENABLED = "SAVED_THEMED_ICON_ENABLED";
+
     private final ThemedIconSwitchProvider mThemedIconOptionsProvider;
     private final WorkspaceViewModel mWorkspaceViewModel;
 
+    private ThemedIconSectionView mThemedIconSectionView;
+    private boolean mSavedThemedIconEnabled = false;
+
 
     public ThemedIconSectionController(ThemedIconSwitchProvider themedIconOptionsProvider,
-            WorkspaceViewModel workspaceViewModel) {
+            WorkspaceViewModel workspaceViewModel, @Nullable Bundle savedInstanceState) {
         mThemedIconOptionsProvider = themedIconOptionsProvider;
         mWorkspaceViewModel = workspaceViewModel;
+
+        if (savedInstanceState != null) {
+            mSavedThemedIconEnabled = savedInstanceState.getBoolean(
+                    KEY_THEMED_ICON_ENABLED, /* defaultValue= */ false);
+        }
     }
 
     @Override
@@ -46,13 +57,14 @@
 
     @Override
     public ThemedIconSectionView createView(Context context) {
-        ThemedIconSectionView themedIconColorSectionView =
+        mThemedIconSectionView =
                 (ThemedIconSectionView) LayoutInflater.from(context).inflate(
                         R.layout.themed_icon_section_view, /* root= */ null);
-        themedIconColorSectionView.setViewListener(this::onViewActivated);
+        mThemedIconSectionView.setViewListener(this::onViewActivated);
+        mThemedIconSectionView.getSwitch().setChecked(mSavedThemedIconEnabled);
         mThemedIconOptionsProvider.fetchThemedIconEnabled(
-                enabled -> themedIconColorSectionView.getSwitch().setChecked(enabled));
-        return themedIconColorSectionView;
+                enabled -> mThemedIconSectionView.getSwitch().setChecked(enabled));
+        return mThemedIconSectionView;
     }
 
     private void onViewActivated(Context context, boolean viewActivated) {
@@ -62,4 +74,12 @@
         mThemedIconOptionsProvider.setThemedIconEnabled(viewActivated);
         mWorkspaceViewModel.getUpdateWorkspace().setValue(viewActivated);
     }
+
+    @Override
+    public void onSaveInstanceState(Bundle savedInstanceState) {
+        if (mThemedIconSectionView != null) {
+            savedInstanceState.putBoolean(KEY_THEMED_ICON_ENABLED,
+                    mThemedIconSectionView.getSwitch().isChecked());
+        }
+    }
 }
diff --git a/src/com/android/customization/module/DefaultCustomizationSections.java b/src/com/android/customization/module/DefaultCustomizationSections.java
index eb5296d..a4510ea 100644
--- a/src/com/android/customization/module/DefaultCustomizationSections.java
+++ b/src/com/android/customization/module/DefaultCustomizationSections.java
@@ -47,7 +47,8 @@
 
         // Themed app icon section.
         sectionControllers.add(new ThemedIconSectionController(
-                ThemedIconSwitchProvider.getInstance(activity), workspaceViewModel));
+                ThemedIconSwitchProvider.getInstance(activity), workspaceViewModel,
+                savedInstanceState));
 
         // App grid section.
         sectionControllers.add(new GridSectionController(