1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 package com.android.customization.picker.quickaffordance.domain.interactor
19 
20 import android.graphics.drawable.Drawable
21 import androidx.annotation.DrawableRes
22 import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository
23 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerAffordanceModel as AffordanceModel
24 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSelectionModel as SelectionModel
25 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSlotModel as SlotModel
26 import com.android.systemui.shared.customization.data.content.CustomizationProviderClient as Client
27 import javax.inject.Provider
28 import kotlinx.coroutines.flow.Flow
29 
30 /**
31  * Single entry-point for all application state and business logic related to quick affordances on
32  * the lock screen.
33  */
34 class KeyguardQuickAffordancePickerInteractor(
35     private val repository: KeyguardQuickAffordancePickerRepository,
36     private val client: Client,
37     private val snapshotRestorer: Provider<KeyguardQuickAffordanceSnapshotRestorer>,
38 ) {
39     /** List of slots available on the device. */
40     val slots: Flow<List<SlotModel>> = repository.slots
41 
42     /** List of all available quick affordances. */
43     val affordances: Flow<List<AffordanceModel>> = repository.affordances
44 
45     /** List of slot-affordance pairs, modeling what the user has currently chosen for each slot. */
46     val selections: Flow<List<SelectionModel>> = repository.selections
47 
48     /**
49      * Selects an affordance with the given ID for a slot with the given ID.
50      *
51      * Note that the maximum affordance per slot is automatically managed. If trying to select an
52      * affordance for a slot that's already full, the oldest affordance is removed to make room.
53      *
54      * Note that if an affordance with the given ID is already selected on the slot with the given
55      * ID, that affordance is moved to the newest position on the slot.
56      */
selectnull57     suspend fun select(slotId: String, affordanceId: String) {
58         client.insertSelection(
59             slotId = slotId,
60             affordanceId = affordanceId,
61         )
62 
63         snapshotRestorer.get().storeSnapshot()
64     }
65 
66     /** Unselects all affordances from the slot with the given ID. */
unselectAllFromSlotnull67     suspend fun unselectAllFromSlot(slotId: String) {
68         client.deleteAllSelections(
69             slotId = slotId,
70         )
71 
72         snapshotRestorer.get().storeSnapshot()
73     }
74 
75     /** Unselects all affordances from all slots. */
unselectAllnull76     suspend fun unselectAll() {
77         client.querySlots().forEach { client.deleteAllSelections(it.id) }
78     }
79 
80     /** Returns a [Drawable] for the given resource ID, from the system UI package. */
getAffordanceIconnull81     suspend fun getAffordanceIcon(
82         @DrawableRes iconResourceId: Int,
83     ): Drawable {
84         return client.getAffordanceIcon(iconResourceId)
85     }
86 }
87