blob: c2d2e5a0474cd34fafc03051165fb3008570d817 [file] [log] [blame]
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -08001/*
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
Alejandro Nijamkinabda67b2022-11-30 14:34:56 -080018package com.android.customization.picker.quickaffordance.domain.interactor
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080019
20import android.graphics.drawable.Drawable
21import androidx.annotation.DrawableRes
Alejandro Nijamkinabda67b2022-11-30 14:34:56 -080022import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository
23import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerAffordanceModel as AffordanceModel
24import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSelectionModel as SelectionModel
25import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSlotModel as SlotModel
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080026import com.android.systemui.shared.quickaffordance.data.content.KeyguardQuickAffordanceProviderClient as Client
Alejandro Nijamkinc27b1d32022-12-21 15:27:35 -080027import javax.inject.Provider
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080028import 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 */
34class KeyguardQuickAffordancePickerInteractor(
Alejandro Nijamkinabda67b2022-11-30 14:34:56 -080035 private val repository: KeyguardQuickAffordancePickerRepository,
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080036 private val client: Client,
Alejandro Nijamkinc27b1d32022-12-21 15:27:35 -080037 private val snapshotRestorer: Provider<KeyguardQuickAffordanceSnapshotRestorer>,
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080038) {
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080039 /** 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 */
57 suspend fun select(slotId: String, affordanceId: String) {
58 client.insertSelection(
59 slotId = slotId,
60 affordanceId = affordanceId,
61 )
Alejandro Nijamkinc27b1d32022-12-21 15:27:35 -080062
63 snapshotRestorer.get().storeSnapshot()
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080064 }
65
66 /** Unselects an affordance with the given ID from the slot with the given ID. */
67 suspend fun unselect(slotId: String, affordanceId: String) {
68 client.deleteSelection(
69 slotId = slotId,
70 affordanceId = affordanceId,
71 )
Alejandro Nijamkinc27b1d32022-12-21 15:27:35 -080072
73 snapshotRestorer.get().storeSnapshot()
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080074 }
75
76 /** Unselects all affordances from the slot with the given ID. */
77 suspend fun unselectAll(slotId: String) {
78 client.deleteAllSelections(
79 slotId = slotId,
80 )
Alejandro Nijamkinc27b1d32022-12-21 15:27:35 -080081
82 snapshotRestorer.get().storeSnapshot()
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080083 }
84
85 /** Returns a [Drawable] for the given resource ID, from the system UI package. */
86 suspend fun getAffordanceIcon(
87 @DrawableRes iconResourceId: Int,
88 ): Drawable {
89 return client.getAffordanceIcon(iconResourceId)
90 }
Alejandro Nijamkinabda67b2022-11-30 14:34:56 -080091
92 /** Returns `true` if the feature is enabled; `false` otherwise. */
93 suspend fun isFeatureEnabled(): Boolean {
94 return repository.isFeatureEnabled()
95 }
Alejandro Nijamkin0f02b082022-11-24 13:43:43 -080096}