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

Commit e98fcc0f authored by Jason Chiu's avatar Jason Chiu
Browse files

[Catalyst] Refactor TouchSoundPreference

NO_IFTTT=Refactor only

Bug: 389131951
Test: atest TouchSoundPreferenceTest
Flag: EXEMPT simple code refactor
Change-Id: Ie901cf10300c8901eb0b9c47513b4b676facdcbe
parent 8cb24b2d
Loading
Loading
Loading
Loading
+19 −21
Original line number Diff line number Diff line
@@ -48,25 +48,7 @@ class TouchSoundPreference :
    override fun isAvailable(context: Context) =
        context.resources.getBoolean(R.bool.config_show_touch_sounds)

    override fun storage(context: Context): KeyValueStore =
        object : KeyValueStoreDelegate {
            override val keyValueStoreDelegate
                get() = context.dataStore

            override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
                super.setValue(key, valueType, value)
                val isChecked = getValue(key, valueType) as Boolean
                val coroutineScope = CoroutineScope(Dispatchers.Default)
                coroutineScope.launch {
                    val audioManager = context.getSystemService(AudioManager::class.java)
                    if (isChecked) {
                        audioManager.loadSoundEffects()
                    } else {
                        audioManager.unloadSoundEffects()
                    }
                }
            }
        }
    override fun storage(context: Context): KeyValueStore = TouchSoundStorage(context)

    override fun getReadPermissions(context: Context) = SettingsSystemStore.getReadPermissions()

@@ -83,9 +65,25 @@ class TouchSoundPreference :

    companion object {
        const val KEY = SOUND_EFFECTS_ENABLED
    }
}

private class TouchSoundStorage(private val context: Context) : KeyValueStoreDelegate {
    override val keyValueStoreDelegate: KeyValueStore =
        SettingsSystemStore.get(context).apply { setDefaultValue(TouchSoundPreference.KEY, true) }

        private val Context.dataStore: KeyValueStore
            get() = SettingsSystemStore.get(this).apply { setDefaultValue(KEY, true) }
    override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
        super.setValue(key, valueType, value)
        val isChecked = getBoolean(key)
        val coroutineScope = CoroutineScope(Dispatchers.Default.limitedParallelism(1))
        coroutineScope.launch {
            val audioManager = context.getSystemService(AudioManager::class.java)
            if (isChecked == true) {
                audioManager.loadSoundEffects()
            } else {
                audioManager.unloadSoundEffects()
            }
        }
    }
}
// LINT.ThenChange(TouchSoundPreferenceController.java)