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

Commit e6c8fa86 authored by chihhangchuang's avatar chihhangchuang
Browse files

Make custom theme remeber selections while system config changes and then restore

Issue description is in b/157631127
Video: https://drive.google.com/file/d/1Ys8W1NwcmL5xt7UsGR0KmmPrub_cJVfx/view?usp=sharing

Also fix the gone preview issue:
Before https://drive.google.com/file/d/1y4mxcbatbeykqiMb6UuekrIiC5jYlNRT/view?usp=sharing
After https://drive.google.com/file/d/1nQ-4qIzpvecfAyM31hamKTnPI5QNWwr2/view?usp=sharing

Test: Manually
Bug: 157631127
Change-Id: I39d7040bba0b550119eb7088711502387d6ec82e
parent 7ff9110f
Loading
Loading
Loading
Loading
+33 −1
Original line number Diff line number Diff line
@@ -16,20 +16,29 @@
package com.android.customization.model.theme.custom;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import com.android.customization.model.CustomizationManager;
import com.android.customization.model.theme.ThemeBundle.PreviewInfo;
import com.android.customization.model.theme.ThemeBundleProvider;
import com.android.customization.model.theme.ThemeManager;
import com.android.customization.model.theme.custom.CustomTheme.Builder;

import org.json.JSONException;

import java.util.Map;

public class CustomThemeManager implements CustomizationManager<ThemeComponentOption> {

    private static final String TAG = "CustomThemeManager";
    private static final String EXTRA_CUSTOM_THEME_OPTION = "custom_theme_option";

    private final CustomTheme mOriginalTheme;
    private final CustomTheme.Builder mBuilder;
    private CustomTheme.Builder mBuilder;

    private CustomThemeManager(Map<String, String> overlayPackages,
            @Nullable CustomTheme originalTheme) {
@@ -72,6 +81,29 @@ public class CustomThemeManager implements CustomizationManager<ThemeComponentOp
        return mBuilder.createPreviewInfo(context);
    }

    /** Saves the custom theme selections while system config changes. */
    public void saveCustomTheme(Context context, Bundle savedInstanceState) {
        CustomTheme customTheme =
                buildPartialCustomTheme(context, /* id= */ null, /* title= */ null);
        savedInstanceState.putString(EXTRA_CUSTOM_THEME_OPTION,
                customTheme.getSerializedPackages());
    }

    /** Reads the saved custom theme after system config changed. */
    public void readCustomTheme(ThemeBundleProvider themeBundleProvider,
                                Bundle savedInstanceState) {
        String packages = savedInstanceState.getString(EXTRA_CUSTOM_THEME_OPTION);
        if (!TextUtils.isEmpty(packages)) {
            try {
                mBuilder = themeBundleProvider.parseCustomTheme(packages);
            } catch (JSONException e) {
                Log.w(TAG, "Couldn't parse provided custom theme.");
            }
        } else {
            Log.w(TAG, "No custom theme being restored.");
        }
    }

    public static CustomThemeManager create(
            @Nullable CustomTheme customTheme, ThemeManager themeManager) {
        if (customTheme != null && customTheme.isDefined()) {
+8 −2
Original line number Diff line number Diff line
@@ -85,12 +85,12 @@ public class CustomThemeActivity extends FragmentActivity implements
    protected void onCreate(Bundle savedInstanceState) {
        CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector();
        mUserEventLogger = (ThemesUserEventLogger) injector.getUserEventLogger(this);
        ThemeBundleProvider themeProvider =
                new DefaultThemeProvider(this, injector.getCustomizationPreferences(this));
        Intent intent = getIntent();
        CustomTheme customTheme = null;
        if (intent != null && intent.hasExtra(EXTRA_THEME_PACKAGES)
                && intent.hasExtra(EXTRA_THEME_TITLE) && intent.hasExtra(EXTRA_THEME_ID)) {
            ThemeBundleProvider themeProvider =
                    new DefaultThemeProvider(this, injector.getCustomizationPreferences(this));
            mIsDefinedTheme = intent.getBooleanExtra(CREATE_NEW_THEME, true);
            try {
                CustomTheme.Builder themeBuilder = themeProvider.parseCustomTheme(
@@ -112,6 +112,9 @@ public class CustomThemeActivity extends FragmentActivity implements
                mUserEventLogger);
        mThemeManager.fetchOptions(null, false);
        mCustomThemeManager = CustomThemeManager.create(customTheme, mThemeManager);
        if (savedInstanceState != null) {
            mCustomThemeManager.readCustomTheme(themeProvider, savedInstanceState);
        }

        int currentStep = 0;
        if (savedInstanceState != null) {
@@ -138,6 +141,9 @@ public class CustomThemeActivity extends FragmentActivity implements
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_STATE_CURRENT_STEP, mCurrentStep);
        if (mCustomThemeManager != null) {
            mCustomThemeManager.saveCustomTheme(this, outState);
        }
    }

    private void navigateToStep(int i) {