Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 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 | |
Alejandro Nijamkin | abda67b | 2022-11-30 14:34:56 -0800 | [diff] [blame] | 18 | package com.android.customization.picker.quickaffordance.domain.interactor |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 19 | |
| 20 | import android.graphics.drawable.Drawable |
| 21 | import androidx.annotation.DrawableRes |
Alejandro Nijamkin | abda67b | 2022-11-30 14:34:56 -0800 | [diff] [blame] | 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 |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 26 | import com.android.systemui.shared.quickaffordance.data.content.KeyguardQuickAffordanceProviderClient as Client |
Alejandro Nijamkin | c27b1d3 | 2022-12-21 15:27:35 -0800 | [diff] [blame^] | 27 | import javax.inject.Provider |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 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( |
Alejandro Nijamkin | abda67b | 2022-11-30 14:34:56 -0800 | [diff] [blame] | 35 | private val repository: KeyguardQuickAffordancePickerRepository, |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 36 | private val client: Client, |
Alejandro Nijamkin | c27b1d3 | 2022-12-21 15:27:35 -0800 | [diff] [blame^] | 37 | private val snapshotRestorer: Provider<KeyguardQuickAffordanceSnapshotRestorer>, |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 38 | ) { |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 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 | */ |
| 57 | suspend fun select(slotId: String, affordanceId: String) { |
| 58 | client.insertSelection( |
| 59 | slotId = slotId, |
| 60 | affordanceId = affordanceId, |
| 61 | ) |
Alejandro Nijamkin | c27b1d3 | 2022-12-21 15:27:35 -0800 | [diff] [blame^] | 62 | |
| 63 | snapshotRestorer.get().storeSnapshot() |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 64 | } |
| 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 Nijamkin | c27b1d3 | 2022-12-21 15:27:35 -0800 | [diff] [blame^] | 72 | |
| 73 | snapshotRestorer.get().storeSnapshot() |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 74 | } |
| 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 Nijamkin | c27b1d3 | 2022-12-21 15:27:35 -0800 | [diff] [blame^] | 81 | |
| 82 | snapshotRestorer.get().storeSnapshot() |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 83 | } |
| 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 Nijamkin | abda67b | 2022-11-30 14:34:56 -0800 | [diff] [blame] | 91 | |
| 92 | /** Returns `true` if the feature is enabled; `false` otherwise. */ |
| 93 | suspend fun isFeatureEnabled(): Boolean { |
| 94 | return repository.isFeatureEnabled() |
| 95 | } |
Alejandro Nijamkin | 0f02b08 | 2022-11-24 13:43:43 -0800 | [diff] [blame] | 96 | } |