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 package com.android.settingslib.spaprivileged.model.app 18 19 import android.content.pm.ApplicationInfo 20 import android.icu.text.CollationKey 21 import androidx.compose.runtime.Composable 22 import com.android.settingslib.spa.widget.ui.SpinnerOption 23 import com.android.settingslib.spaprivileged.template.app.AppListItem 24 import com.android.settingslib.spaprivileged.template.app.AppListItemModel 25 import kotlinx.coroutines.flow.Flow 26 27 data class AppEntry<T : AppRecord>( 28 val record: T, 29 val label: String, 30 val labelCollationKey: CollationKey, 31 ) 32 33 /** 34 * Implement this interface to build an App List. 35 */ 36 interface AppListModel<T : AppRecord> { 37 /** 38 * Returns the spinner options available to the App List. 39 * 40 * Default no spinner will be shown. 41 */ getSpinnerOptionsnull42 fun getSpinnerOptions(recordList: List<T>): List<SpinnerOption> = emptyList() 43 44 /** 45 * Loads the extra info for the App List, and generates the [AppRecord] List. 46 */ 47 fun transform(userIdFlow: Flow<Int>, appListFlow: Flow<List<ApplicationInfo>>): Flow<List<T>> 48 49 /** 50 * Filters the [AppRecord] list. 51 * 52 * @return the [AppRecord] list which will be displayed. 53 */ 54 fun filter(userIdFlow: Flow<Int>, option: Int, recordListFlow: Flow<List<T>>): Flow<List<T>> = 55 recordListFlow 56 57 /** 58 * This function is called when the App List's loading is finished and displayed to the user. 59 * 60 * Could do some pre-cache here. 61 * 62 * @return true to enable pre-fetching app labels. 63 */ 64 suspend fun onFirstLoaded(recordList: List<T>) = false 65 66 /** 67 * Gets the comparator to sort the App List. 68 * 69 * Default sorting is based on the app label. 70 */ 71 fun getComparator(option: Int): Comparator<AppEntry<T>> = compareBy( 72 { it.labelCollationKey }, <lambda>null73 { it.record.app.packageName }, <lambda>null74 { it.record.app.uid }, 75 ) 76 77 /** 78 * Gets the group title of this item. 79 * 80 * Note: Items should be sorted by group in [getComparator] first, this [getGroupTitle] will not 81 * change the list order. 82 */ getGroupTitlenull83 fun getGroupTitle(option: Int, record: T): String? = null 84 85 /** 86 * Gets the summary for the given app record. 87 * 88 * @return null if no summary should be displayed. 89 */ 90 @Composable 91 fun getSummary(option: Int, record: T): (() -> String)? = null 92 93 @Composable 94 fun AppListItemModel<T>.AppItem() { 95 AppListItem {} 96 } 97 } 98