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