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

Commit c27b1d38 authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

Wallpaper picker reset support (2/3).

Creates system to support reverting changes made to the home or lock
screen via the wallpaper picker.

The system itself is in WPP2/.../picker/undo.

Bug: 262924056
Test: included unit tests and updated existing ones
Test: manually verified that I can reset changes made to quick
affordances both from the quick affordance screen or the main WPP screen

Change-Id: Id84b27db4264b14436bd73fe6adaac570cffbf37
parent 0ca33a6a
Loading
Loading
Loading
Loading
+45 −8
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import com.android.customization.model.theme.ThemeBundleProvider;
import com.android.customization.model.theme.ThemeManager;
import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository;
import com.android.customization.picker.quickaffordance.domain.interactor.KeyguardQuickAffordancePickerInteractor;
import com.android.customization.picker.quickaffordance.domain.interactor.KeyguardQuickAffordanceSnapshotRestorer;
import com.android.customization.picker.quickaffordance.ui.viewmodel.KeyguardQuickAffordancePickerViewModel;
import com.android.systemui.shared.quickaffordance.data.content.KeyguardQuickAffordanceProviderClient;
import com.android.systemui.shared.quickaffordance.data.content.KeyguardQuickAffordanceProviderClientImpl;
@@ -49,6 +50,9 @@ import com.android.wallpaper.picker.CustomizationPickerActivity;
import com.android.wallpaper.picker.ImagePreviewFragment;
import com.android.wallpaper.picker.LivePreviewFragment;
import com.android.wallpaper.picker.PreviewFragment;
import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer;

import java.util.Map;

import kotlinx.coroutines.Dispatchers;

@@ -66,6 +70,7 @@ public class ThemePickerInjector extends WallpaperPicker2Injector
    private KeyguardQuickAffordanceProviderClient mKeyguardQuickAffordanceProviderClient;
    private FragmentFactory mFragmentFactory;
    private BaseFlags mFlags;
    private KeyguardQuickAffordanceSnapshotRestorer mKeyguardQuickAffordanceSnapshotRestorer;

    @Override
    public CustomizationSections getCustomizationSections(Activity activity) {
@@ -149,7 +154,8 @@ public class ThemePickerInjector extends WallpaperPicker2Injector
                    getKeyguardQuickAffordancePickerProviderClient(context);
            mKeyguardQuickAffordancePickerInteractor = new KeyguardQuickAffordancePickerInteractor(
                    new KeyguardQuickAffordancePickerRepository(client, Dispatchers.getIO()),
                    client);
                    client,
                    () -> getKeyguardQuickAffordanceSnapshotRestorer(context));
        }
        return mKeyguardQuickAffordancePickerInteractor;
    }
@@ -163,7 +169,8 @@ public class ThemePickerInjector extends WallpaperPicker2Injector
            mKeyguardQuickAffordancePickerViewModelFactory =
                    new KeyguardQuickAffordancePickerViewModel.Factory(
                            context,
                            getKeyguardQuickAffordancePickerInteractor(context));
                            getKeyguardQuickAffordancePickerInteractor(context),
                            getUndoInteractor(context));
        }
        return mKeyguardQuickAffordancePickerViewModelFactory;
    }
@@ -176,8 +183,26 @@ public class ThemePickerInjector extends WallpaperPicker2Injector
        return mFragmentFactory;
    }

    @Override
    public BaseFlags getFlags() {
        if (mFlags == null) {
            mFlags = new BaseFlags() {};
        }

        return mFlags;
    }

    @Override
    public Map<Integer, SnapshotRestorer> getSnapshotRestorers(Context context) {
        final Map<Integer, SnapshotRestorer> restorers = super.getSnapshotRestorers(context);
        restorers.put(
                KEY_QUICK_AFFORDANCE_SNAPSHOT_RESTORER,
                getKeyguardQuickAffordanceSnapshotRestorer(context));
        return restorers;
    }

    /** Returns the {@link KeyguardQuickAffordanceProviderClient}. */
    public KeyguardQuickAffordanceProviderClient getKeyguardQuickAffordancePickerProviderClient(
    protected KeyguardQuickAffordanceProviderClient getKeyguardQuickAffordancePickerProviderClient(
            Context context) {
        if (mKeyguardQuickAffordanceProviderClient == null) {
            mKeyguardQuickAffordanceProviderClient =
@@ -187,12 +212,24 @@ public class ThemePickerInjector extends WallpaperPicker2Injector
        return mKeyguardQuickAffordanceProviderClient;
    }

    @Override
    public BaseFlags getFlags() {
        if (mFlags == null) {
            mFlags = new BaseFlags() {};
    protected KeyguardQuickAffordanceSnapshotRestorer getKeyguardQuickAffordanceSnapshotRestorer(
            Context context) {
        if (mKeyguardQuickAffordanceSnapshotRestorer == null) {
            mKeyguardQuickAffordanceSnapshotRestorer = new KeyguardQuickAffordanceSnapshotRestorer(
                    getKeyguardQuickAffordancePickerInteractor(context),
                    getKeyguardQuickAffordancePickerProviderClient(context));
        }

        return mFlags;
        return mKeyguardQuickAffordanceSnapshotRestorer;
    }

    private static final int KEY_QUICK_AFFORDANCE_SNAPSHOT_RESTORER =
            WallpaperPicker2Injector.MIN_SNAPSHOT_RESTORER_KEY;

    /**
     * When this injector is overridden, this is the minimal value that should be used by restorers
     * returns in {@link #getSnapshotRestorers(Context)}.
     */
    protected static final int MIN_SNAPSHOT_RESTORER_KEY =
            KEY_QUICK_AFFORDANCE_SNAPSHOT_RESTORER + 1;
}
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.customization.picker.clock.domain.interactor

import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer
import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot

/** Handles state restoration for clocks. */
class ClocksSnapshotRestorer : SnapshotRestorer {
    override suspend fun setUpSnapshotRestorer(
        updater: (RestorableSnapshot) -> Unit,
    ): RestorableSnapshot {
        // TODO(b/262924055): implement as part of the clock settings screen.
        return RestorableSnapshot(mapOf())
    }

    override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) {
        // TODO(b/262924055): implement as part of the clock settings screen.
    }
}
+8 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import com.android.customization.picker.quickaffordance.shared.model.KeyguardQui
import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSelectionModel as SelectionModel
import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSlotModel as SlotModel
import com.android.systemui.shared.quickaffordance.data.content.KeyguardQuickAffordanceProviderClient as Client
import javax.inject.Provider
import kotlinx.coroutines.flow.Flow

/**
@@ -33,10 +34,8 @@ import kotlinx.coroutines.flow.Flow
class KeyguardQuickAffordancePickerInteractor(
    private val repository: KeyguardQuickAffordancePickerRepository,
    private val client: Client,
    private val snapshotRestorer: Provider<KeyguardQuickAffordanceSnapshotRestorer>,
) {
    /** Whether the feature is enabled. */
    val isFeatureEnabled: Flow<Boolean> = repository.isFeatureEnabled

    /** List of slots available on the device. */
    val slots: Flow<List<SlotModel>> = repository.slots

@@ -60,6 +59,8 @@ class KeyguardQuickAffordancePickerInteractor(
            slotId = slotId,
            affordanceId = affordanceId,
        )

        snapshotRestorer.get().storeSnapshot()
    }

    /** Unselects an affordance with the given ID from the slot with the given ID. */
@@ -68,6 +69,8 @@ class KeyguardQuickAffordancePickerInteractor(
            slotId = slotId,
            affordanceId = affordanceId,
        )

        snapshotRestorer.get().storeSnapshot()
    }

    /** Unselects all affordances from the slot with the given ID. */
@@ -75,6 +78,8 @@ class KeyguardQuickAffordancePickerInteractor(
        client.deleteAllSelections(
            slotId = slotId,
        )

        snapshotRestorer.get().storeSnapshot()
    }

    /** Returns a [Drawable] for the given resource ID, from the system UI package. */
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.customization.picker.quickaffordance.domain.interactor

import com.android.systemui.shared.quickaffordance.data.content.KeyguardQuickAffordanceProviderClient
import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer
import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot

/** Handles state restoration for the quick affordances system. */
class KeyguardQuickAffordanceSnapshotRestorer(
    private val interactor: KeyguardQuickAffordancePickerInteractor,
    private val client: KeyguardQuickAffordanceProviderClient,
) : SnapshotRestorer {

    private lateinit var snapshotUpdater: (RestorableSnapshot) -> Unit

    suspend fun storeSnapshot() {
        snapshotUpdater(snapshot())
    }

    override suspend fun setUpSnapshotRestorer(
        updater: (RestorableSnapshot) -> Unit,
    ): RestorableSnapshot {
        snapshotUpdater = updater
        return snapshot()
    }

    override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) {
        val selections: List<Pair<String, String>> =
            checkNotNull(snapshot.args[KEY_SELECTIONS]).split(SELECTION_SEPARATOR).map { selection
                ->
                val (slotId, affordanceId) = selection.split(SLOT_AFFORDANCE_SEPARATOR)
                slotId to affordanceId
            }

        selections.forEach { (slotId, affordanceId) ->
            interactor.select(
                slotId,
                affordanceId,
            )
        }
    }

    private suspend fun snapshot(): RestorableSnapshot {
        return RestorableSnapshot(
            mapOf(
                KEY_SELECTIONS to
                    client.querySelections().joinToString(SELECTION_SEPARATOR) { selection ->
                        "${selection.slotId}${SLOT_AFFORDANCE_SEPARATOR}${selection.affordanceId}"
                    }
            )
        )
    }

    companion object {
        private const val KEY_SELECTIONS = "selections"
        private const val SLOT_AFFORDANCE_SEPARATOR = "->"
        private const val SELECTION_SEPARATOR = "|"
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import com.android.customization.picker.quickaffordance.ui.viewmodel.KeyguardQui
import com.android.wallpaper.R
import com.android.wallpaper.module.InjectorProvider
import com.android.wallpaper.picker.AppbarFragment
import com.android.wallpaper.picker.undo.ui.binder.RevertToolbarButtonBinder
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine

@@ -63,6 +64,13 @@ class KeyguardQuickAffordancePickerFragment : AppbarFragment() {
                    injector.getKeyguardQuickAffordancePickerViewModelFactory(requireContext()),
                )
                .get()
        setUpToolbarMenu(R.menu.undoable_customization_menu)
        RevertToolbarButtonBinder.bind(
            view = view.requireViewById(toolbarId),
            viewModel = viewModel.undo,
            lifecycleOwner = this,
        )

        KeyguardQuickAffordancePreviewBinder.bind(
            activity = requireActivity(),
            previewView = view.requireViewById(R.id.preview),
Loading