Merge "[Catalyst] Extract datastore for VibrationMainSwitchPreference" into main
This commit is contained in:
@@ -26,7 +26,6 @@ import com.android.settings.contract.KEY_VIBRATION_HAPTICS
|
|||||||
import com.android.settings.metrics.PreferenceActionMetricsProvider
|
import com.android.settings.metrics.PreferenceActionMetricsProvider
|
||||||
import com.android.settingslib.datastore.KeyValueStore
|
import com.android.settingslib.datastore.KeyValueStore
|
||||||
import com.android.settingslib.datastore.KeyedObservableDelegate
|
import com.android.settingslib.datastore.KeyedObservableDelegate
|
||||||
import com.android.settingslib.datastore.SettingsStore
|
|
||||||
import com.android.settingslib.datastore.SettingsSystemStore
|
import com.android.settingslib.datastore.SettingsSystemStore
|
||||||
import com.android.settingslib.metadata.BooleanValuePreference
|
import com.android.settingslib.metadata.BooleanValuePreference
|
||||||
import com.android.settingslib.metadata.PreferenceMetadata
|
import com.android.settingslib.metadata.PreferenceMetadata
|
||||||
@@ -56,8 +55,7 @@ class VibrationMainSwitchPreference :
|
|||||||
|
|
||||||
override fun tags(context: Context) = arrayOf(KEY_VIBRATION_HAPTICS)
|
override fun tags(context: Context) = arrayOf(KEY_VIBRATION_HAPTICS)
|
||||||
|
|
||||||
override fun storage(context: Context): KeyValueStore =
|
override fun storage(context: Context): KeyValueStore = VibrationMainSwitchStore(context)
|
||||||
VibrationMainSwitchToggleStorage(SettingsSystemStore.get(context))
|
|
||||||
|
|
||||||
override fun getReadPermissions(context: Context) = SettingsSystemStore.getReadPermissions()
|
override fun getReadPermissions(context: Context) = SettingsSystemStore.getReadPermissions()
|
||||||
|
|
||||||
@@ -81,34 +79,41 @@ class VibrationMainSwitchPreference :
|
|||||||
if (newValue == true) {
|
if (newValue == true) {
|
||||||
// Play a haptic as preview for the main toggle only when touch feedback is enabled.
|
// Play a haptic as preview for the main toggle only when touch feedback is enabled.
|
||||||
VibrationPreferenceConfig.playVibrationPreview(
|
VibrationPreferenceConfig.playVibrationPreview(
|
||||||
preference.context.getSystemService(Vibrator::class.java),
|
preference.context.vibrator,
|
||||||
VibrationAttributes.USAGE_TOUCH,
|
VibrationAttributes.USAGE_TOUCH,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Provides SettingsStore for vibration main switch with custom default value. */
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
private class VibrationMainSwitchToggleStorage(private val settingsStore: SettingsStore) :
|
|
||||||
KeyedObservableDelegate<String>(settingsStore), KeyValueStore {
|
|
||||||
|
|
||||||
override fun contains(key: String) = settingsStore.contains(key)
|
|
||||||
|
|
||||||
override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) =
|
|
||||||
DEFAULT_VALUE as T
|
|
||||||
|
|
||||||
override fun <T : Any> getValue(key: String, valueType: Class<T>) =
|
|
||||||
(settingsStore.getBoolean(key) ?: DEFAULT_VALUE) as T
|
|
||||||
|
|
||||||
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
|
|
||||||
settingsStore.setBoolean(key, value as Boolean?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val KEY = Settings.System.VIBRATE_ON
|
const val KEY = Settings.System.VIBRATE_ON
|
||||||
const val DEFAULT_VALUE = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Provides SettingsStore for vibration main switch with custom default value. */
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
class VibrationMainSwitchStore(
|
||||||
|
context: Context,
|
||||||
|
private val settingsStore: KeyValueStore = SettingsSystemStore.get(context),
|
||||||
|
) : KeyedObservableDelegate<String>(settingsStore), KeyValueStore {
|
||||||
|
|
||||||
|
override fun contains(key: String) = settingsStore.contains(key)
|
||||||
|
|
||||||
|
override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) = DEFAULT_VALUE as T
|
||||||
|
|
||||||
|
override fun <T : Any> getValue(key: String, valueType: Class<T>) =
|
||||||
|
settingsStore.getValue(key, valueType) ?: getDefaultValue(key, valueType)
|
||||||
|
|
||||||
|
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) =
|
||||||
|
settingsStore.setValue(key, valueType, value)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val DEFAULT_VALUE = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val Context.vibrator: Vibrator
|
||||||
|
get() = getSystemService(Vibrator::class.java)!!
|
||||||
|
|
||||||
// LINT.ThenChange(VibrationMainSwitchPreferenceController.java)
|
// LINT.ThenChange(VibrationMainSwitchPreferenceController.java)
|
||||||
|
@@ -21,8 +21,8 @@ import android.content.res.Resources
|
|||||||
import android.os.Vibrator
|
import android.os.Vibrator
|
||||||
import androidx.test.core.app.ApplicationProvider
|
import androidx.test.core.app.ApplicationProvider
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
import com.android.settings.flags.Flags
|
|
||||||
import com.android.settings.R
|
import com.android.settings.R
|
||||||
|
import com.android.settings.flags.Flags
|
||||||
import com.android.settingslib.preference.CatalystScreenTestCase
|
import com.android.settingslib.preference.CatalystScreenTestCase
|
||||||
import com.google.common.truth.Truth.assertThat
|
import com.google.common.truth.Truth.assertThat
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
@@ -35,7 +35,7 @@ import org.mockito.kotlin.stub
|
|||||||
// LINT.IfChange
|
// LINT.IfChange
|
||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(AndroidJUnit4::class)
|
||||||
class VibrationIntensityScreenTest : CatalystScreenTestCase() {
|
class VibrationIntensityScreenTest : CatalystScreenTestCase() {
|
||||||
private lateinit var vibrator: Vibrator
|
private lateinit var mockVibrator: Vibrator
|
||||||
|
|
||||||
private val resourcesSpy: Resources =
|
private val resourcesSpy: Resources =
|
||||||
spy((ApplicationProvider.getApplicationContext() as Context).resources)
|
spy((ApplicationProvider.getApplicationContext() as Context).resources)
|
||||||
@@ -44,9 +44,10 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
|
|||||||
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
|
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
|
||||||
override fun getSystemService(name: String): Any? =
|
override fun getSystemService(name: String): Any? =
|
||||||
when {
|
when {
|
||||||
name == getSystemServiceName(Vibrator::class.java) -> vibrator
|
name == VIBRATOR_SERVICE -> mockVibrator
|
||||||
else -> super.getSystemService(name)
|
else -> super.getSystemService(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getResources(): Resources = resourcesSpy
|
override fun getResources(): Resources = resourcesSpy
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +63,7 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun isAvailable_noVibrator_unavailable() {
|
fun isAvailable_noVibrator_unavailable() {
|
||||||
vibrator = mock { on { hasVibrator() } doReturn false }
|
mockVibrator = mock { on { hasVibrator() } doReturn false }
|
||||||
resourcesSpy.stub {
|
resourcesSpy.stub {
|
||||||
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 3
|
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 3
|
||||||
}
|
}
|
||||||
@@ -71,7 +72,7 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun isAvailable_hasVibratorAndSingleIntensityLevel_unavailable() {
|
fun isAvailable_hasVibratorAndSingleIntensityLevel_unavailable() {
|
||||||
vibrator = mock { on { hasVibrator() } doReturn true }
|
mockVibrator = mock { on { hasVibrator() } doReturn true }
|
||||||
resourcesSpy.stub {
|
resourcesSpy.stub {
|
||||||
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
|
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
|
||||||
}
|
}
|
||||||
@@ -80,7 +81,7 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun isAvailable_hasVibratorAndMultipleIntensityLevels_available() {
|
fun isAvailable_hasVibratorAndMultipleIntensityLevels_available() {
|
||||||
vibrator = mock { on { hasVibrator() } doReturn true }
|
mockVibrator = mock { on { hasVibrator() } doReturn true }
|
||||||
resourcesSpy.stub {
|
resourcesSpy.stub {
|
||||||
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 2
|
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 2
|
||||||
}
|
}
|
||||||
@@ -88,4 +89,3 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// LINT.ThenChange(VibrationPreferenceControllerTest.java)
|
// LINT.ThenChange(VibrationPreferenceControllerTest.java)
|
||||||
|
|
||||||
|
@@ -21,8 +21,8 @@ import android.content.res.Resources
|
|||||||
import android.os.Vibrator
|
import android.os.Vibrator
|
||||||
import androidx.test.core.app.ApplicationProvider
|
import androidx.test.core.app.ApplicationProvider
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
import com.android.settings.flags.Flags
|
|
||||||
import com.android.settings.R
|
import com.android.settings.R
|
||||||
|
import com.android.settings.flags.Flags
|
||||||
import com.android.settingslib.preference.CatalystScreenTestCase
|
import com.android.settingslib.preference.CatalystScreenTestCase
|
||||||
import com.google.common.truth.Truth.assertThat
|
import com.google.common.truth.Truth.assertThat
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
@@ -35,7 +35,7 @@ import org.mockito.kotlin.stub
|
|||||||
// LINT.IfChange
|
// LINT.IfChange
|
||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(AndroidJUnit4::class)
|
||||||
class VibrationScreenTest : CatalystScreenTestCase() {
|
class VibrationScreenTest : CatalystScreenTestCase() {
|
||||||
private lateinit var vibrator: Vibrator
|
private lateinit var mockVibrator: Vibrator
|
||||||
|
|
||||||
private val resourcesSpy: Resources =
|
private val resourcesSpy: Resources =
|
||||||
spy((ApplicationProvider.getApplicationContext() as Context).resources)
|
spy((ApplicationProvider.getApplicationContext() as Context).resources)
|
||||||
@@ -44,9 +44,10 @@ class VibrationScreenTest : CatalystScreenTestCase() {
|
|||||||
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
|
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
|
||||||
override fun getSystemService(name: String): Any? =
|
override fun getSystemService(name: String): Any? =
|
||||||
when {
|
when {
|
||||||
name == getSystemServiceName(Vibrator::class.java) -> vibrator
|
name == VIBRATOR_SERVICE -> mockVibrator
|
||||||
else -> super.getSystemService(name)
|
else -> super.getSystemService(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getResources(): Resources = resourcesSpy
|
override fun getResources(): Resources = resourcesSpy
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +63,7 @@ class VibrationScreenTest : CatalystScreenTestCase() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun isAvailable_noVibrator_unavailable() {
|
fun isAvailable_noVibrator_unavailable() {
|
||||||
vibrator = mock { on { hasVibrator() } doReturn false }
|
mockVibrator = mock { on { hasVibrator() } doReturn false }
|
||||||
resourcesSpy.stub {
|
resourcesSpy.stub {
|
||||||
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
|
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
|
||||||
}
|
}
|
||||||
@@ -71,7 +72,7 @@ class VibrationScreenTest : CatalystScreenTestCase() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun isAvailable_hasVibratorAndMultipleIntensityLevels_unavailable() {
|
fun isAvailable_hasVibratorAndMultipleIntensityLevels_unavailable() {
|
||||||
vibrator = mock { on { hasVibrator() } doReturn true }
|
mockVibrator = mock { on { hasVibrator() } doReturn true }
|
||||||
resourcesSpy.stub {
|
resourcesSpy.stub {
|
||||||
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 3
|
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 3
|
||||||
}
|
}
|
||||||
@@ -80,7 +81,7 @@ class VibrationScreenTest : CatalystScreenTestCase() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun isAvailable_hasVibratorAndSingleIntensityLevel_available() {
|
fun isAvailable_hasVibratorAndSingleIntensityLevel_available() {
|
||||||
vibrator = mock { on { hasVibrator() } doReturn true }
|
mockVibrator = mock { on { hasVibrator() } doReturn true }
|
||||||
resourcesSpy.stub {
|
resourcesSpy.stub {
|
||||||
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
|
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user