summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Chaohui Wang <chaohuiw@google.com> 2022-08-30 16:31:08 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2022-08-30 16:31:08 +0000
commit327377b8bc57825a69aaf25a4db4487dba212aff (patch)
treedcbae9be22f0396c4a6565936c7f88d6f3dfb176
parentc085efb7cf4fcfc3ed1ba38a26a9658757cfac31 (diff)
parent9ebe42ffceeab911e115c046b9f3c2e387e6e276 (diff)
Merge "Create TogglePermissionAppListProvider"
-rw-r--r--packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppInfoPage.kt7
-rw-r--r--packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppList.kt (renamed from packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppListModel.kt)36
-rw-r--r--packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppListPage.kt4
3 files changed, 26 insertions, 21 deletions
diff --git a/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppInfoPage.kt b/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppInfoPage.kt
index dac79a0fbb16..eb9ce5e77c93 100644
--- a/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppInfoPage.kt
+++ b/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppInfoPage.kt
@@ -30,7 +30,6 @@ import androidx.navigation.NavType
import androidx.navigation.navArgument
import com.android.settingslib.spa.framework.common.SettingsPageProvider
import com.android.settingslib.spa.framework.compose.navigator
-import com.android.settingslib.spa.framework.compose.rememberContext
import com.android.settingslib.spa.widget.preference.SwitchPreference
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
import com.android.settingslib.spaprivileged.model.app.AppRecord
@@ -44,7 +43,7 @@ private const val PACKAGE_NAME = "packageName"
private const val USER_ID = "userId"
internal class TogglePermissionAppInfoPageProvider(
- private val factory: TogglePermissionAppListModelFactory,
+ private val appListTemplate: TogglePermissionAppListTemplate,
) : SettingsPageProvider {
override val name = NAME
@@ -57,10 +56,10 @@ internal class TogglePermissionAppInfoPageProvider(
@Composable
override fun Page(arguments: Bundle?) {
checkNotNull(arguments)
- val permission = checkNotNull(arguments.getString(PERMISSION))
+ val permissionType = checkNotNull(arguments.getString(PERMISSION))
val packageName = checkNotNull(arguments.getString(PACKAGE_NAME))
val userId = arguments.getInt(USER_ID)
- val listModel = rememberContext { context -> factory.createModel(permission, context) }
+ val listModel = appListTemplate.rememberModel(permissionType)
TogglePermissionAppInfoPage(listModel, packageName, userId)
}
diff --git a/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppListModel.kt b/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppList.kt
index a2848392a2f0..3cc5854b95e4 100644
--- a/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppListModel.kt
+++ b/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppList.kt
@@ -47,20 +47,14 @@ interface TogglePermissionAppListModel<T : AppRecord> {
fun setAllowed(record: T, newAllowed: Boolean)
}
-interface TogglePermissionAppListModelFactory {
- fun createModel(
- permission: String,
- context: Context,
- ): TogglePermissionAppListModel<out AppRecord>
+interface TogglePermissionAppListProvider {
+ val permissionType: String
- fun createPageProviders(): List<SettingsPageProvider> = listOf(
- TogglePermissionAppListPageProvider(this),
- TogglePermissionAppInfoPageProvider(this),
- )
+ fun createModel(context: Context): TogglePermissionAppListModel<out AppRecord>
@Composable
- fun EntryItem(permissionType: String) {
- val listModel = rememberModel(permissionType)
+ fun EntryItem() {
+ val listModel = rememberContext(::createModel)
Preference(
object : PreferenceModel {
override val title = stringResource(listModel.pageTitleResId)
@@ -74,10 +68,22 @@ interface TogglePermissionAppListModelFactory {
*
* Expose route to enable enter from non-SPA pages.
*/
- fun getRoute(permissionType: String): String =
+ fun getRoute(): String =
TogglePermissionAppListPageProvider.getRoute(permissionType)
}
-@Composable
-internal fun TogglePermissionAppListModelFactory.rememberModel(permission: String) =
- rememberContext { context -> createModel(permission, context) }
+class TogglePermissionAppListTemplate(
+ allProviders: List<TogglePermissionAppListProvider>,
+) {
+ private val listModelProviderMap = allProviders.associateBy { it.permissionType }
+
+ fun createPageProviders(): List<SettingsPageProvider> = listOf(
+ TogglePermissionAppListPageProvider(this),
+ TogglePermissionAppInfoPageProvider(this),
+ )
+
+ @Composable
+ internal fun rememberModel(permissionType: String) = rememberContext { context ->
+ listModelProviderMap.getValue(permissionType).createModel(context)
+ }
+}
diff --git a/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppListPage.kt b/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppListPage.kt
index a4d9058fde4d..f2eb96277b9b 100644
--- a/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppListPage.kt
+++ b/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/TogglePermissionAppListPage.kt
@@ -38,7 +38,7 @@ private const val NAME = "TogglePermissionAppList"
private const val PERMISSION = "permission"
internal class TogglePermissionAppListPageProvider(
- private val factory: TogglePermissionAppListModelFactory,
+ private val appListTemplate: TogglePermissionAppListTemplate,
) : SettingsPageProvider {
override val name = NAME
@@ -55,7 +55,7 @@ internal class TogglePermissionAppListPageProvider(
@Composable
private fun TogglePermissionAppList(permissionType: String) {
- val listModel = factory.rememberModel(permissionType)
+ val listModel = appListTemplate.rememberModel(permissionType)
val context = LocalContext.current
val internalListModel = remember {
TogglePermissionInternalAppListModel(context, listModel)