From a0f67e8591259099d978ee22a356b3f53f0b1cf2 Mon Sep 17 00:00:00 2001 From: fayey Date: Thu, 16 Nov 2023 07:08:00 +0000 Subject: [PATCH] Add the second toggle for the egress data permission on voice activation apps toggle page. Bug: 309677007 Test: presubmit Change-Id: I219f67bd55d3a38b6e026698ce4cbf24c6fbe503 --- res/values/strings.xml | 4 + .../app/specialaccess/VoiceActivationApps.kt | 90 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/res/values/strings.xml b/res/values/strings.xml index ff59f6a733e..34358429ebd 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9607,6 +9607,10 @@ Allow voice activation Voice activation turns-on approved apps, hands-free, using voice command. Built-in adaptive sensing ensures data stays private only to you.\n\nMore about protected adaptive sensing + + Improve voice activation + + This device uses private intelligence to improve the voice activation model. Apps can receive summarized updates that are aggregated across many users to maintain privacy while improving the model for everyone.\n\nMore about private intelligence Full screen notifications diff --git a/src/com/android/settings/spa/app/specialaccess/VoiceActivationApps.kt b/src/com/android/settings/spa/app/specialaccess/VoiceActivationApps.kt index 12258068319..a7f971418cd 100644 --- a/src/com/android/settings/spa/app/specialaccess/VoiceActivationApps.kt +++ b/src/com/android/settings/spa/app/specialaccess/VoiceActivationApps.kt @@ -20,12 +20,24 @@ import android.Manifest import android.app.AppOpsManager import android.app.settings.SettingsEnums import android.content.Context +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.platform.LocalContext import com.android.settings.R import com.android.settings.overlay.FeatureFactory +import com.android.settingslib.spa.widget.preference.SwitchPreference +import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel +import com.android.settingslib.spaprivileged.model.app.AppOpsController import com.android.settingslib.spaprivileged.model.app.PackageManagers.hasGrantPermission import com.android.settingslib.spaprivileged.template.app.AppOpPermissionListModel import com.android.settingslib.spaprivileged.template.app.AppOpPermissionRecord import com.android.settingslib.spaprivileged.template.app.TogglePermissionAppListProvider +import kotlinx.coroutines.Dispatchers /** * This class builds an App List under voice activation apps and the individual page which @@ -53,6 +65,84 @@ class VoiceActivationAppsListModel(context: Context) : AppOpPermissionListModel( override fun isChangeable(record: AppOpPermissionRecord): Boolean = super.isChangeable(record) && record.app.hasGrantPermission(permission) + @Composable + override fun InfoPageAdditionalContent( + record: AppOpPermissionRecord, + isAllowed: () -> Boolean?, + ) { + SwitchPreference(createReceiveDetectionTrainingDataOpSwitchModel(record, isAllowed)) + } + + @Composable + private fun createReceiveDetectionTrainingDataOpSwitchModel( + record: AppOpPermissionRecord, + isReceiveSandBoxTriggerAudioOpAllowed: () -> Boolean? + ): ReceiveDetectionTrainingDataOpSwitchModel { + val context = LocalContext.current + val ReceiveDetectionTrainingDataOpController = remember { + AppOpsController( + context = context, + app = record.app, + op = AppOpsManager.OP_RECEIVE_SANDBOXED_DETECTION_TRAINING_DATA, + ) + } + val isReceiveDetectionTrainingDataOpAllowed = isReceiveDetectionTrainingDataOpAllowed(record, ReceiveDetectionTrainingDataOpController) + return remember(record) { + ReceiveDetectionTrainingDataOpSwitchModel( + context, + record, + isReceiveSandBoxTriggerAudioOpAllowed, + ReceiveDetectionTrainingDataOpController, + isReceiveDetectionTrainingDataOpAllowed, + ) + }.also { model -> LaunchedEffect(model, Dispatchers.Default) { model.initState() } } + } + + private inner class ReceiveDetectionTrainingDataOpSwitchModel( + context: Context, + private val record: AppOpPermissionRecord, + isReceiveSandBoxTriggerAudioOpAllowed: () -> Boolean?, + receiveDetectionTrainingDataOpController: AppOpsController, + isReceiveDetectionTrainingDataOpAllowed: () -> Boolean?, + ) : SwitchPreferenceModel { + private var appChangeable by mutableStateOf(true) + + override val title: String = context.getString(R.string.permit_receive_sandboxed_detection_training_data) + override val summary: () -> String = { context.getString(R.string.receive_sandboxed_detection_training_data_description) } + override val checked = { isReceiveDetectionTrainingDataOpAllowed() == true && isReceiveSandBoxTriggerAudioOpAllowed() == true } + override val changeable = { appChangeable && isReceiveSandBoxTriggerAudioOpAllowed() == true } + + fun initState() { + appChangeable = isChangeable(record) + } + + override val onCheckedChange: (Boolean) -> Unit = { newChecked -> + receiveDetectionTrainingDataOpController.setAllowed(newChecked) + } + } + + @Composable + private fun isReceiveDetectionTrainingDataOpAllowed( + record: AppOpPermissionRecord, + controller: AppOpsController + ): () -> Boolean? { + if (record.hasRequestBroaderPermission) { + // Broader permission trumps the specific permission. + return { true } + } + + val mode = controller.mode.observeAsState() + return { + when (mode.value) { + null -> null + AppOpsManager.MODE_ALLOWED -> true + AppOpsManager.MODE_DEFAULT -> record.app.hasGrantPermission( + Manifest.permission.RECEIVE_SANDBOXED_DETECTION_TRAINING_DATA) + else -> false + } + } + } + private fun logPermissionChange(newAllowed: Boolean) { val category = when { newAllowed -> SettingsEnums.APP_SPECIAL_PERMISSION_RECEIVE_SANDBOX_TRIGGER_AUDIO_ALLOW