1 /* <lambda>null2 * 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.data.repository 19 20 import android.content.Context 21 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerAffordanceModel as AffordanceModel 22 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSelectionModel as SelectionModel 23 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSlotModel as SlotModel 24 import com.android.systemui.shared.customization.data.content.CustomizationProviderClient as Client 25 import com.android.wallpaper.config.BaseFlags 26 import kotlinx.coroutines.CoroutineScope 27 import kotlinx.coroutines.flow.Flow 28 import kotlinx.coroutines.flow.SharingStarted 29 import kotlinx.coroutines.flow.map 30 import kotlinx.coroutines.flow.shareIn 31 32 /** 33 * Abstracts access to application state related to functionality for selecting, picking, or setting 34 * lock screen quick affordances. 35 */ 36 class KeyguardQuickAffordancePickerRepository( 37 private val client: Client, 38 private val scope: CoroutineScope, 39 private val flags: BaseFlags, 40 private val context: Context 41 ) { 42 /** Whether the feature is enabled. */ 43 fun isFeatureEnabled(): Boolean { 44 return flags.isQuickAffordancesEnabled(context) 45 } 46 47 /** List of slots available on the device. */ 48 val slots: Flow<List<SlotModel>> = 49 client.observeSlots().map { slots -> slots.map { slot -> slot.toModel() } } 50 51 /** List of all available quick affordances. */ 52 val affordances: Flow<List<AffordanceModel>> = 53 client 54 .observeAffordances() 55 .map { affordances -> affordances.map { affordance -> affordance.toModel() } } 56 .shareIn(scope, replay = 1, started = SharingStarted.Lazily) 57 58 /** List of slot-affordance pairs, modeling what the user has currently chosen for each slot. */ 59 val selections: Flow<List<SelectionModel>> = 60 client 61 .observeSelections() 62 .map { selections -> selections.map { selection -> selection.toModel() } } 63 .shareIn(scope, replay = 1, started = SharingStarted.Lazily) 64 65 private fun Client.Slot.toModel(): SlotModel { 66 return SlotModel( 67 id = id, 68 maxSelectedQuickAffordances = capacity, 69 ) 70 } 71 72 private fun Client.Affordance.toModel(): AffordanceModel { 73 return AffordanceModel( 74 id = id, 75 name = name, 76 iconResourceId = iconResourceId, 77 isEnabled = isEnabled, 78 enablementExplanation = enablementExplanation ?: "", 79 enablementActionText = enablementActionText, 80 enablementActionIntent = enablementActionIntent, 81 configureIntent = configureIntent, 82 ) 83 } 84 85 private fun Client.Selection.toModel(): SelectionModel { 86 return SelectionModel( 87 slotId = slotId, 88 affordanceId = affordanceId, 89 ) 90 } 91 } 92