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

Commit 6c36c26e authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Store the themed icon enabled state in SharedPreferences" into sc-dev am: 762404e9

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/ThemePicker/+/15109457

Change-Id: I2772727080f7b9b0926748cb847b6631e7b93604
parents 8bb4fdc3 762404e9
Loading
Loading
Loading
Loading
+2 −9
Original line number Diff line number Diff line
@@ -25,9 +25,6 @@ import com.android.wallpaper.R;
import com.android.wallpaper.model.CustomizationSectionController;
import com.android.wallpaper.model.WorkspaceViewModel;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/** The {@link CustomizationSectionController} for themed icon section. */
public class ThemedIconSectionController implements
        CustomizationSectionController<ThemedIconSectionView> {
@@ -35,7 +32,6 @@ public class ThemedIconSectionController implements
    private final ThemedIconSwitchProvider mThemedIconOptionsProvider;
    private final WorkspaceViewModel mWorkspaceViewModel;

    private static ExecutorService sExecutorService = Executors.newSingleThreadExecutor();

    public ThemedIconSectionController(ThemedIconSwitchProvider themedIconOptionsProvider,
            WorkspaceViewModel workspaceViewModel) {
@@ -54,11 +50,8 @@ public class ThemedIconSectionController implements
                (ThemedIconSectionView) LayoutInflater.from(context).inflate(
                        R.layout.themed_icon_section_view, /* root= */ null);
        themedIconColorSectionView.setViewListener(this::onViewActivated);
        sExecutorService.submit(() -> {
            boolean themedIconEnabled = mThemedIconOptionsProvider.fetchThemedIconEnabled();
            themedIconColorSectionView.post(() ->
                    themedIconColorSectionView.getSwitch().setChecked(themedIconEnabled));
        });
        mThemedIconOptionsProvider.fetchThemedIconEnabled(
                enabled -> themedIconColorSectionView.getSwitch().setChecked(enabled));
        return themedIconColorSectionView;
    }

+91 −22
Original line number Diff line number Diff line
@@ -19,49 +19,118 @@ import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.WorkerThread;
import androidx.annotation.Nullable;

import com.android.customization.module.CustomizationPreferences;
import com.android.wallpaper.R;
import com.android.wallpaper.module.InjectorProvider;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Retrieves the themed icon switch by {@link ContentResolver} from the current launcher
 */
public class ThemedIconSwitchProvider {

    private static ThemedIconSwitchProvider sThemedIconSwitchProvider;

    private static final String ICON_THEMED = "icon_themed";
    private static final int ENABLED = 1;
    private static final String COL_ICON_THEMED_VALUE = "boolean_value";
    private static final int ENABLED = 1;
    private static final int RESULT_SUCCESS = 1;

    private final Context mContext;
    private final ExecutorService mExecutorService = Executors.newSingleThreadExecutor();
    private final ContentResolver mContentResolver;
    private final ThemedIconUtils mThemedIconUtils;
    private final CustomizationPreferences mCustomizationPreferences;

    /** Callback for the themed icon enabled state fetching result. */
    public interface FetchThemedIconEnabledCallback {
        /** Gets called when the result is available. */
        void onResult(boolean isEnabled);
    }

    /** Returns the {@link ThemedIconSwitchProvider} instance. */
    public static ThemedIconSwitchProvider getInstance(Context context) {
        if (sThemedIconSwitchProvider == null) {
            Context appContext = context.getApplicationContext();
            sThemedIconSwitchProvider = new ThemedIconSwitchProvider(
                    appContext.getContentResolver(),
                    new ThemedIconUtils(appContext,
                            appContext.getString(R.string.themed_icon_metadata_key)),
                    (CustomizationPreferences) InjectorProvider.getInjector()
                            .getPreferences(appContext));
        }
        return sThemedIconSwitchProvider;
    }

    public ThemedIconSwitchProvider(Context context, ThemedIconUtils themedIconUtils) {
        mContext = context;
    private ThemedIconSwitchProvider(ContentResolver contentResolver,
            ThemedIconUtils themedIconUtils, CustomizationPreferences customizationPreferences) {
        mContentResolver = contentResolver;
        mThemedIconUtils = themedIconUtils;
        mCustomizationPreferences = customizationPreferences;
    }

    /** Returns {@code true} if themed icon feature is available. */
    public boolean isThemedIconAvailable() {
        return mThemedIconUtils.isThemedIconAvailable();
    }

    @WorkerThread
    public boolean fetchThemedIconEnabled() {
        ContentResolver contentResolver = mContext.getContentResolver();
        try (Cursor cursor = contentResolver.query(
                mThemedIconUtils.getUriForPath(ICON_THEMED), /* projection= */
                null, /* selection= */ null, /* selectionArgs= */ null, /* sortOrder= */ null)) {
    /** Gets the themed icon feature enabled state from SharedPreferences. */
    public boolean isThemedIconEnabled() {
        return mCustomizationPreferences.getThemedIconEnabled();
    }

    /**
     * Fetches the themed icon feature enabled state and stores in SharedPreferences, or returns the
     * SharedPreferences result if the fetching failed.
     */
    public void fetchThemedIconEnabled(@Nullable FetchThemedIconEnabledCallback callback) {
        mExecutorService.submit(() -> {
            try (Cursor cursor = mContentResolver.query(
                    mThemedIconUtils.getUriForPath(ICON_THEMED), /* projection= */ null,
                    /* selection= */ null, /* selectionArgs= */ null, /* sortOrder= */ null)) {
                if (cursor != null && cursor.moveToNext()) {
                int themedIconEnabled = cursor.getInt(cursor.getColumnIndex(COL_ICON_THEMED_VALUE));
                return themedIconEnabled == ENABLED;
                    boolean isEnabled = cursor.getInt(cursor.getColumnIndex(COL_ICON_THEMED_VALUE))
                            == ENABLED;
                    if (mCustomizationPreferences.getThemedIconEnabled() != isEnabled) {
                        mCustomizationPreferences.setThemedIconEnabled(isEnabled);
                    }
                    if (callback != null) {
                        postMainThread(() -> callback.onResult(isEnabled));
                    }
                    return;
                }
        return false;
            }
            if (callback != null) {
                postMainThread(
                        () -> callback.onResult(mCustomizationPreferences.getThemedIconEnabled()));
            }
        });
    }

    protected int setThemedIconEnabled(boolean enabled) {
    /**
     * Enables themed icon feature or not.
     *
     * <p>The value would also be stored in SharedPreferences.
     */
    protected void setThemedIconEnabled(boolean enabled) {
        mExecutorService.submit(() -> {
            ContentValues values = new ContentValues();
            values.put(COL_ICON_THEMED_VALUE, enabled);
        return mContext.getContentResolver().update(
                mThemedIconUtils.getUriForPath(ICON_THEMED), values,
                /* where= */ null, /* selectionArgs= */ null);
            int result = mContentResolver.update(mThemedIconUtils.getUriForPath(ICON_THEMED),
                    values, /* where= */ null, /* selectionArgs= */ null);
            if (result == RESULT_SUCCESS) {
                mCustomizationPreferences.setThemedIconEnabled(enabled);
            }
        });
    }

    private void postMainThread(Runnable runnable) {
        new Handler(Looper.getMainLooper()).post(runnable);
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ public interface CustomizationPreferences extends WallpaperPreferences {

    String KEY_CUSTOM_THEME= "themepicker_custom_theme";
    String KEY_VISITED_PREFIX = "themepicker_visited_";
    String KEY_THEMED_ICON_ENABLED = "themepicker_themed_icon_enabled";

    String getSerializedCustomThemes();

@@ -29,4 +30,8 @@ public interface CustomizationPreferences extends WallpaperPreferences {
    boolean getTabVisited(String id);

    void setTabVisited(String id);

    boolean getThemedIconEnabled();

    void setThemedIconEnabled(boolean enabled);
}
+10 −0
Original line number Diff line number Diff line
@@ -46,4 +46,14 @@ public class DefaultCustomizationPreferences extends DefaultWallpaperPreferences
    public void setTabVisited(String id) {
        mSharedPrefs.edit().putBoolean(KEY_VISITED_PREFIX + id, true).apply();
    }

    @Override
    public boolean getThemedIconEnabled() {
        return mSharedPrefs.getBoolean(KEY_THEMED_ICON_ENABLED, false);
    }

    @Override
    public void setThemedIconEnabled(boolean enabled) {
        mSharedPrefs.edit().putBoolean(KEY_THEMED_ICON_ENABLED, enabled).apply();
    }
}
+1 −5
Original line number Diff line number Diff line
@@ -11,8 +11,6 @@ import com.android.customization.model.grid.GridSectionController;
import com.android.customization.model.mode.DarkModeSectionController;
import com.android.customization.model.themedicon.ThemedIconSectionController;
import com.android.customization.model.themedicon.ThemedIconSwitchProvider;
import com.android.customization.model.themedicon.ThemedIconUtils;
import com.android.wallpaper.R;
import com.android.wallpaper.model.CustomizationSectionController;
import com.android.wallpaper.model.CustomizationSectionController.CustomizationSectionNavigationController;
import com.android.wallpaper.model.PermissionRequester;
@@ -49,9 +47,7 @@ public final class DefaultCustomizationSections implements CustomizationSections

        // Themed app icon section.
        sectionControllers.add(new ThemedIconSectionController(
                new ThemedIconSwitchProvider(activity, new ThemedIconUtils(activity,
                        activity.getString(R.string.themed_icon_metadata_key))),
                workspaceViewModel));
                ThemedIconSwitchProvider.getInstance(activity), workspaceViewModel));

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