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

Commit a8a20937 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Clean up DefaultValueStoreDelegate

NO_IFTTT=Catalyst only

Bug: 417340188
Flag: EXEMPT refactor
Test: adb shell settings put/delete
Change-Id: I3ebf11697d4610371ce30a4e8c21372132b9af84
parent 19d4ec0b
Loading
Loading
Loading
Loading
+0 −27
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings

import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.KeyValueStoreDelegate

@Suppress("UNCHECKED_CAST")
class DefaultValueStoreDelegate(
    override val keyValueStoreDelegate: KeyValueStore,
    private val value: Any,
) : KeyValueStoreDelegate {
    override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) = value as T
}
+10 −4
Original line number Diff line number Diff line
@@ -18,10 +18,10 @@ package com.android.settings.notification
import android.app.settings.SettingsEnums.ACTION_CHARGING_SOUND
import android.content.Context
import android.provider.Settings.Secure.CHARGING_SOUNDS_ENABLED
import com.android.settings.DefaultValueStoreDelegate
import com.android.settings.R
import com.android.settings.contract.KEY_CHARGING_SOUNDS
import com.android.settings.metrics.PreferenceActionMetricsProvider
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.ReadWritePermit
@@ -30,7 +30,7 @@ import com.android.settingslib.metadata.SwitchPreference

// LINT.IfChange
class ChargingSoundPreference :
    SwitchPreference(CHARGING_SOUNDS_ENABLED, R.string.charging_sounds_title),
    SwitchPreference(KEY, R.string.charging_sounds_title),
    PreferenceActionMetricsProvider,
    PreferenceAvailabilityProvider {
    override val preferenceActionMetrics: Int
@@ -38,8 +38,7 @@ class ChargingSoundPreference :

    override fun tags(context: Context) = arrayOf(KEY_CHARGING_SOUNDS)

    override fun storage(context: Context) =
        DefaultValueStoreDelegate(SettingsSecureStore.get(context), true)
    override fun storage(context: Context) = context.dataStore

    override fun isAvailable(context: Context) =
        context.resources.getBoolean(R.bool.config_show_charging_sounds)
@@ -56,5 +55,12 @@ class ChargingSoundPreference :

    override val sensitivityLevel
        get() = SensitivityLevel.NO_SENSITIVITY

    companion object {
        const val KEY = CHARGING_SOUNDS_ENABLED

        private val Context.dataStore: KeyValueStore
            get() = SettingsSecureStore.get(this).apply { setDefaultValue(KEY, true) }
    }
}
// LINT.ThenChange(ChargingSoundPreferenceController.java)
+10 −4
Original line number Diff line number Diff line
@@ -18,10 +18,10 @@ package com.android.settings.notification
import android.app.settings.SettingsEnums.ACTION_SCREEN_LOCKING_SOUND
import android.content.Context
import android.provider.Settings.System.LOCKSCREEN_SOUNDS_ENABLED
import com.android.settings.DefaultValueStoreDelegate
import com.android.settings.R
import com.android.settings.contract.KEY_SCREEN_LOCKING_SOUND
import com.android.settings.metrics.PreferenceActionMetricsProvider
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.SettingsSystemStore
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.ReadWritePermit
@@ -30,7 +30,7 @@ import com.android.settingslib.metadata.SwitchPreference

// LINT.IfChange
class ScreenLockSoundPreference :
    SwitchPreference(LOCKSCREEN_SOUNDS_ENABLED, R.string.screen_locking_sounds_title),
    SwitchPreference(KEY, R.string.screen_locking_sounds_title),
    PreferenceActionMetricsProvider,
    PreferenceAvailabilityProvider {
    override val preferenceActionMetrics: Int
@@ -38,8 +38,7 @@ class ScreenLockSoundPreference :

    override fun tags(context: Context) = arrayOf(KEY_SCREEN_LOCKING_SOUND)

    override fun storage(context: Context) =
        DefaultValueStoreDelegate(SettingsSystemStore.get(context), true)
    override fun storage(context: Context) = context.dataStore

    override fun isAvailable(context: Context) =
        context.resources.getBoolean(R.bool.config_show_screen_locking_sounds)
@@ -56,5 +55,12 @@ class ScreenLockSoundPreference :

    override val sensitivityLevel
        get() = SensitivityLevel.NO_SENSITIVITY

    companion object {
        const val KEY = LOCKSCREEN_SOUNDS_ENABLED

        private val Context.dataStore: KeyValueStore
            get() = SettingsSystemStore.get(this).apply { setDefaultValue(KEY, true) }
    }
}
// LINT.ThenChange(ScreenLockSoundPreferenceController.java)
+0 −78
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.datastore.KeyValueStore
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock

@RunWith(AndroidJUnit4::class)
class DefaultValueStoreDelegateTest {
    private val keyValueStore: KeyValueStore = mock()

    @Test
    fun setDefaultValueTrue_shouldReturnTrue() {
        val storeDelegate = DefaultValueStoreDelegate(keyValueStore, true)

        assertThat(storeDelegate.getBoolean(KEY)).isTrue()
    }

    @Test
    fun setDefaultValueFalse_shouldReturnFalse() {
        val storeDelegate = DefaultValueStoreDelegate(keyValueStore, false)

        assertThat(storeDelegate.getBoolean(KEY)).isFalse()
    }

    @Test
    fun setDefaultValueInt_shouldReturnExactInt() {
        val value = 10
        val storeDelegate = DefaultValueStoreDelegate(keyValueStore, value)

        assertThat(storeDelegate.getInt(KEY)).isEqualTo(value)
    }

    @Test
    fun setDefaultValueFloat_shouldReturnExactFloat() {
        val value = 1.1f
        val storeDelegate = DefaultValueStoreDelegate(keyValueStore, value)

        assertThat(storeDelegate.getFloat(KEY)).isEqualTo(value)
    }

    @Test
    fun setDefaultValueLong_shouldReturnExactLong() {
        val value = 10L
        val storeDelegate = DefaultValueStoreDelegate(keyValueStore, value)

        assertThat(storeDelegate.getLong(KEY)).isEqualTo(value)
    }

    @Test
    fun setDefaultValueString_shouldReturnExactString() {
        val value = "abcdef"
        val storeDelegate = DefaultValueStoreDelegate(keyValueStore, value)

        assertThat(storeDelegate.getString(KEY)).isEqualTo(value)
    }

    companion object {
        const val KEY = "test_key"
    }
}