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

Commit f8bb3ed5 authored by Josh's avatar Josh
Browse files

Added early verification of selected key combination

Test: ShortcutCustomizationViewModelTest
CustomShortcutCategoriesRepositoryTest CustomInputGesturesRepositoryTest
Flag: com.android.systemui.keyboard_shortcut_helper_shortcut_customizer
Fix: 381063978

Change-Id: I6dedfcf0ecccfeb44d6752af7cbb3202ae1ba385
parent 092cb7e6
Loading
Loading
Loading
Loading
+23 −20
Original line number Diff line number Diff line
@@ -32,18 +32,19 @@ import com.android.systemui.keyboard.shared.model.ShortcutCustomizationRequestRe
import com.android.systemui.keyboard.shared.model.ShortcutCustomizationRequestResult.ERROR_RESERVED_COMBINATION
import com.android.systemui.keyboard.shared.model.ShortcutCustomizationRequestResult.SUCCESS
import com.android.systemui.settings.UserTracker
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.withContext
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext

@SysUISingleton
class CustomInputGesturesRepository
@Inject
constructor(private val userTracker: UserTracker,
    @Background private val bgCoroutineContext: CoroutineContext)
{
constructor(
    private val userTracker: UserTracker,
    @Background private val bgCoroutineContext: CoroutineContext,
) {

    private val userContext: Context
        get() = userTracker.createCurrentUserContext(userTracker.userContext)
@@ -55,8 +56,7 @@ constructor(private val userTracker: UserTracker,

    private val _customInputGesture = MutableStateFlow<List<InputGestureData>>(emptyList())

    val customInputGestures =
        _customInputGesture.onStart { refreshCustomInputGestures() }
    val customInputGestures = _customInputGesture.onStart { refreshCustomInputGestures() }

    fun refreshCustomInputGestures() {
        setCustomInputGestures(inputGestures = retrieveCustomInputGestures())
@@ -72,18 +72,18 @@ constructor(private val userTracker: UserTracker,
        } else emptyList()
    }

    suspend fun addCustomInputGesture(inputGesture: InputGestureData): ShortcutCustomizationRequestResult {
    suspend fun addCustomInputGesture(
        inputGesture: InputGestureData
    ): ShortcutCustomizationRequestResult {
        return withContext(bgCoroutineContext) {
            when (val result = inputManager.addCustomInputGesture(inputGesture)) {
                CUSTOM_INPUT_GESTURE_RESULT_SUCCESS -> {
                    refreshCustomInputGestures()
                    SUCCESS
                }
                CUSTOM_INPUT_GESTURE_RESULT_ERROR_ALREADY_EXISTS ->
                    ERROR_RESERVED_COMBINATION
                CUSTOM_INPUT_GESTURE_RESULT_ERROR_ALREADY_EXISTS -> ERROR_RESERVED_COMBINATION

                CUSTOM_INPUT_GESTURE_RESULT_ERROR_RESERVED_GESTURE ->
                    ERROR_RESERVED_COMBINATION
                CUSTOM_INPUT_GESTURE_RESULT_ERROR_RESERVED_GESTURE -> ERROR_RESERVED_COMBINATION

                else -> {
                    Log.w(
@@ -97,11 +97,11 @@ constructor(private val userTracker: UserTracker,
        }
    }

    suspend fun deleteCustomInputGesture(inputGesture: InputGestureData): ShortcutCustomizationRequestResult {
    suspend fun deleteCustomInputGesture(
        inputGesture: InputGestureData
    ): ShortcutCustomizationRequestResult {
        return withContext(bgCoroutineContext) {
            when (
                val result = inputManager.removeCustomInputGesture(inputGesture)
            ) {
            when (val result = inputManager.removeCustomInputGesture(inputGesture)) {
                CUSTOM_INPUT_GESTURE_RESULT_SUCCESS -> {
                    refreshCustomInputGestures()
                    SUCCESS
@@ -134,6 +134,9 @@ constructor(private val userTracker: UserTracker,
        }
    }

    suspend fun getInputGestureByTrigger(trigger: InputGestureData.Trigger): InputGestureData? =
        withContext(bgCoroutineContext) { inputManager.getInputGesture(trigger) }

    private companion object {
        private const val TAG = "CustomInputGesturesRepository"
    }
+16 −9
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.keyboard.shortcut.data.repository

import android.hardware.input.InputGestureData
import android.hardware.input.InputGestureData.Builder
import android.hardware.input.InputGestureData.Trigger
import android.hardware.input.InputGestureData.createKeyTrigger
import android.hardware.input.InputManager
import android.hardware.input.KeyGestureEvent.KeyGestureType
@@ -175,6 +176,11 @@ constructor(
        return customInputGesturesRepository.resetAllCustomInputGestures()
    }

    suspend fun isSelectedKeyCombinationAvailable(): Boolean {
        val trigger = buildTriggerFromSelectedKeyCombination() ?: return false
        return customInputGesturesRepository.getInputGestureByTrigger(trigger) == null
    }

    private fun Builder.addKeyGestureTypeForShortcutBeingCustomized(): Builder {
        val keyGestureType = getKeyGestureTypeForShortcutBeingCustomized()

@@ -222,7 +228,10 @@ constructor(
        )
    }

    private fun Builder.addTriggerFromSelectedKeyCombination(): Builder {
    private fun Builder.addTriggerFromSelectedKeyCombination(): Builder =
        setTrigger(buildTriggerFromSelectedKeyCombination())

    private fun buildTriggerFromSelectedKeyCombination(): Trigger? {
        val selectedKeyCombination = _selectedKeyCombination.value
        if (selectedKeyCombination?.keyCode == null) {
            Log.w(
@@ -230,17 +239,15 @@ constructor(
                "User requested to set shortcut but selected key combination is " +
                    "$selectedKeyCombination",
            )
            return this
            return null
        }

        return setTrigger(
            createKeyTrigger(
        return createKeyTrigger(
            /* keycode= */ selectedKeyCombination.keyCode,
            /* modifierState= */ shortcutCategoriesUtils.removeUnsupportedModifiers(
                selectedKeyCombination.modifiers
            ),
        )
        )
    }

    @VisibleForTesting
+3 −0
Original line number Diff line number Diff line
@@ -53,4 +53,7 @@ constructor(private val customShortcutRepository: CustomShortcutCategoriesReposi
    suspend fun resetAllCustomShortcuts(): ShortcutCustomizationRequestResult {
        return customShortcutRepository.resetAllCustomShortcuts()
    }

    suspend fun isSelectedKeyCombinationAvailable(): Boolean =
        customShortcutRepository.isSelectedKeyCombinationAvailable()
}
+17 −11
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import com.android.systemui.statusbar.phone.create
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

class ShortcutCustomizationDialogStarter
@@ -57,6 +58,8 @@ constructor(
    private val viewModel = viewModelFactory.create()

    override suspend fun onActivated(): Nothing {
        coroutineScope {
            launch {
                viewModel.shortcutCustomizationUiState.collect { uiState ->
                    when (uiState) {
                        is AddShortcutDialog,
@@ -72,6 +75,9 @@ constructor(
                        }
                    }
                }
            }
            launch { viewModel.activate() }
        }
        awaitCancellation()
    }

+7 −5
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.os.UserHandle
import android.provider.Settings
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
@@ -36,6 +35,7 @@ import com.android.systemui.keyboard.shortcut.ui.composable.ShortcutHelper
import com.android.systemui.keyboard.shortcut.ui.composable.ShortcutHelperBottomSheet
import com.android.systemui.keyboard.shortcut.ui.composable.getWidth
import com.android.systemui.keyboard.shortcut.ui.viewmodel.ShortcutHelperViewModel
import com.android.systemui.lifecycle.rememberActivated
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.res.R
import com.android.systemui.statusbar.phone.SystemUIDialogFactory
@@ -51,14 +51,13 @@ class ShortcutHelperDialogStarter
constructor(
    @Application private val applicationScope: CoroutineScope,
    private val shortcutHelperViewModel: ShortcutHelperViewModel,
    shortcutCustomizationDialogStarterFactory: ShortcutCustomizationDialogStarter.Factory,
    private val shortcutCustomizationDialogStarterFactory:
        ShortcutCustomizationDialogStarter.Factory,
    private val dialogFactory: SystemUIDialogFactory,
    private val activityStarter: ActivityStarter,
) : CoreStartable {

    @VisibleForTesting var dialog: Dialog? = null
    private val shortcutCustomizationDialogStarter =
        shortcutCustomizationDialogStarterFactory.create()

    override fun start() {
        shortcutHelperViewModel.shouldShow
@@ -77,7 +76,10 @@ constructor(
            content = { dialog ->
                val shortcutsUiState by
                    shortcutHelperViewModel.shortcutsUiState.collectAsStateWithLifecycle()
                LaunchedEffect(Unit) { shortcutCustomizationDialogStarter.activate() }
                val shortcutCustomizationDialogStarter =
                    rememberActivated(traceName = "shortcutCustomizationDialogStarter") {
                        shortcutCustomizationDialogStarterFactory.create()
                    }
                ShortcutHelper(
                    modifier = Modifier.width(getWidth()),
                    shortcutsUiState = shortcutsUiState,
Loading