Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 59935e2a authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Add RestrictionKeys to TogglePermissionAppList

To support the "Disabled by admin" feature.

Bug: 235727273
Test: Manual with Settings App
Change-Id: Iccbfeb4b3e3e0b4f8b67b4eea177879361c44d87
parent 63190ceb
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -34,11 +34,12 @@ import com.android.settingslib.spa.framework.common.SettingsEntryBuilder
import com.android.settingslib.spa.framework.common.SettingsPage
import com.android.settingslib.spa.framework.common.SettingsPageProvider
import com.android.settingslib.spa.framework.compose.navigator
import com.android.settingslib.spa.widget.preference.SwitchPreference
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
import com.android.settingslib.spaprivileged.model.app.AppRecord
import com.android.settingslib.spaprivileged.model.app.PackageManagers
import com.android.settingslib.spaprivileged.model.app.toRoute
import com.android.settingslib.spaprivileged.model.enterprise.Restrictions
import com.android.settingslib.spaprivileged.template.preference.RestrictedSwitchPreference
import kotlinx.coroutines.Dispatchers

private const val ENTRY_NAME = "AllowControl"
@@ -105,7 +106,7 @@ private fun TogglePermissionAppInfoPage(
        LaunchedEffect(model, Dispatchers.Default) {
            model.initState()
        }
        SwitchPreference(model)
        RestrictedSwitchPreference(model, Restrictions(userId, listModel.switchRestrictionKeys))
    }
}

+2 −0
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ interface TogglePermissionAppListModel<T : AppRecord> {
    val pageTitleResId: Int
    val switchTitleResId: Int
    val footerResId: Int
    val switchRestrictionKeys: List<String>
        get() = emptyList()

    /**
     * Loads the extra info for the App List, and generates the [AppRecord] List.
+27 −5
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
@@ -37,6 +38,10 @@ import com.android.settingslib.spa.framework.util.getStringArg
import com.android.settingslib.spaprivileged.R
import com.android.settingslib.spaprivileged.model.app.AppListModel
import com.android.settingslib.spaprivileged.model.app.AppRecord
import com.android.settingslib.spaprivileged.model.app.userId
import com.android.settingslib.spaprivileged.model.enterprise.Restrictions
import com.android.settingslib.spaprivileged.model.enterprise.RestrictionsProvider
import com.android.settingslib.spaprivileged.template.preference.RestrictedSwitchPreference
import kotlinx.coroutines.flow.Flow

private const val ENTRY_NAME = "AppList"
@@ -127,15 +132,32 @@ private class TogglePermissionInternalAppListModel<T : AppRecord>(

    @Composable
    override fun getSummary(option: Int, record: T): State<String> {
        val restrictionsProvider = remember {
            val restrictions = Restrictions(
                userId = record.app.userId,
                keys = listModel.switchRestrictionKeys,
            )
            RestrictionsProvider(context, restrictions)
        }
        val restrictedMode = restrictionsProvider.restrictedMode.observeAsState()
        val allowed = listModel.isAllowed(record)
        return remember {
            derivedStateOf {
                RestrictedSwitchPreference.getSummary(
                    context = context,
                    restrictedMode = restrictedMode.value,
                    noRestrictedSummary = getNoRestrictedSummary(allowed),
                    checked = allowed,
                ).value
            }
        }
    }

    private fun getNoRestrictedSummary(allowed: State<Boolean?>) = derivedStateOf {
        when (allowed.value) {
            true -> context.getString(R.string.app_permission_summary_allowed)
            false -> context.getString(R.string.app_permission_summary_not_allowed)
                    else -> ""
                }
            }
            else -> context.getString(R.string.summary_placeholder)
        }
    }
}
+21 −5
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.Context
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
@@ -55,6 +56,20 @@ fun RestrictedSwitchPreference(model: SwitchPreferenceModel, restrictions: Restr
    }
}

object RestrictedSwitchPreference {
    fun getSummary(
        context: Context,
        restrictedMode: RestrictedMode?,
        noRestrictedSummary: State<String>,
        checked: State<Boolean?>,
    ): State<String> = when (restrictedMode) {
        is NoRestricted -> noRestrictedSummary
        is BaseUserRestricted -> stateOf(context.getString(R.string.disabled))
        is BlockedByAdmin -> derivedStateOf { restrictedMode.getSummary(checked.value) }
        null -> stateOf(context.getString(R.string.summary_placeholder))
    }
}

private class RestrictedSwitchPreferenceModel(
    private val context: Context,
    model: SwitchPreferenceModel,
@@ -62,11 +77,12 @@ private class RestrictedSwitchPreferenceModel(
) : SwitchPreferenceModel {
    override val title = model.title

    override val summary = when (restrictedMode) {
        is NoRestricted -> model.summary
        is BaseUserRestricted -> stateOf(context.getString(R.string.disabled))
        is BlockedByAdmin -> derivedStateOf { restrictedMode.getSummary(model.checked.value) }
    }
    override val summary = RestrictedSwitchPreference.getSummary(
        context = context,
        restrictedMode = restrictedMode,
        noRestrictedSummary = model.summary,
        checked = model.checked,
    )

    override val checked = when (restrictedMode) {
        is NoRestricted -> model.checked