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

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

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
parent 386b180e
Loading
Loading
Loading
Loading
+25 −5
Original line number Diff line number Diff line
@@ -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 @@ import com.android.wallpaper.model.WorkspaceViewModel;
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 @@ public class ThemedIconSectionController implements

    @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 @@ public class ThemedIconSectionController implements
        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());
        }
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -47,7 +47,8 @@ public final class DefaultCustomizationSections implements CustomizationSections

        // Themed app icon section.
        sectionControllers.add(new ThemedIconSectionController(
                ThemedIconSwitchProvider.getInstance(activity), workspaceViewModel));
                ThemedIconSwitchProvider.getInstance(activity), workspaceViewModel,
                savedInstanceState));

        // App grid section.
        sectionControllers.add(new GridSectionController(