• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
27 import javax.inject.Inject
28 import javax.inject.Singleton
29 import kotlinx.coroutines.flow.Flow
30 
31 /**
32  * Single entry-point for all application state and business logic related to quick affordances on
33  * the lock screen.
34  */
35 @Singleton
36 class KeyguardQuickAffordancePickerInteractor
37 @Inject
38 constructor(
39     repository: KeyguardQuickAffordancePickerRepository,
40     private val client: CustomizationProviderClient,
41     private val snapshotRestorer: KeyguardQuickAffordanceSnapshotRestorer,
42 ) {
43     /** List of slots available on the device. */
44     val slots: Flow<List<SlotModel>> = repository.slots
45 
46     /** List of all available quick affordances. */
47     val affordances: Flow<List<AffordanceModel>> = repository.affordances
48 
49     /** List of slot-affordance pairs, modeling what the user has currently chosen for each slot. */
50     val selections: Flow<List<SelectionModel>> = repository.selections
51 
52     /**
53      * Selects an affordance with the given ID for a slot with the given ID.
54      *
55      * Note that the maximum affordance per slot is automatically managed. If trying to select an
56      * affordance for a slot that's already full, the oldest affordance is removed to make room.
57      *
58      * Note that if an affordance with the given ID is already selected on the slot with the given
59      * ID, that affordance is moved to the newest position on the slot.
60      */
selectnull61     suspend fun select(slotId: String, affordanceId: String) {
62         client.insertSelection(
63             slotId = slotId,
64             affordanceId = affordanceId,
65         )
66 
67         snapshotRestorer.storeSnapshot()
68     }
69 
70     /** Unselects all affordances from the slot with the given ID. */
unselectAllFromSlotnull71     suspend fun unselectAllFromSlot(slotId: String) {
72         client.deleteAllSelections(
73             slotId = slotId,
74         )
75 
76         snapshotRestorer.storeSnapshot()
77     }
78 
79     /** Unselects all affordances from all slots. */
unselectAllnull80     suspend fun unselectAll() {
81         client.querySlots().forEach { client.deleteAllSelections(it.id) }
82     }
83 
84     /** Returns a [Drawable] for the given resource ID, from the system UI package. */
getAffordanceIconnull85     suspend fun getAffordanceIcon(
86         @DrawableRes iconResourceId: Int,
87     ): Drawable {
88         return client.getAffordanceIcon(iconResourceId)
89     }
90 }
91