diff --git a/res/xml/accessibility_color_and_motion.xml b/res/xml/accessibility_color_and_motion.xml index 6b47a9d62f5..7d5ac4d7f9a 100644 --- a/res/xml/accessibility_color_and_motion.xml +++ b/res/xml/accessibility_color_and_motion.xml @@ -61,7 +61,7 @@ ? = null + + override val icon: Int + @DrawableRes get() = R.drawable.ic_accessibility_animation + + override fun onStart(context: PreferenceLifecycleContext) { + mSettingsKeyedObserver = object : KeyedObserver { + override fun onKeyChanged(key: String?, reason: Int) { + context.notifyPreferenceChange(KEY) + } + } + mSettingsKeyedObserver?.let { + for (key in TOGGLE_ANIMATION_KEYS) { + SettingsGlobalStore.get(context).addObserver(key, it, HandlerExecutor.main) + } + } + } + + override fun onStop(context: PreferenceLifecycleContext) { + mSettingsKeyedObserver?.let { + SettingsGlobalStore.get(context).removeObserver(it) + mSettingsKeyedObserver = null + } + } + + override fun storage(context: Context): KeyValueStore = RemoveAnimationsStorage(context) + + private class RemoveAnimationsStorage(private val context: Context) : + NoOpKeyedObservable(), KeyValueStore { + override fun contains(key: String) = key == KEY + + override fun getValue(key: String, valueType: Class) = + when { + key == KEY && valueType == Boolean::class.javaObjectType -> + !isAnimationEnabled(context) as T + + else -> null + } + + override fun setValue(key: String, valueType: Class, value: T?) { + if (key == KEY && value is Boolean) { + setAnimationEnabled(context, !value) + } + } + } + + companion object { + // This KEY must match the key used in accessibility_color_and_motion.xml for this + // preference, at least until the entire screen is migrated to Catalyst and that XML + // is deleted. Use any key from the set of 3 toggle animation keys. + const val KEY = Settings.Global.ANIMATOR_DURATION_SCALE + + const val ANIMATION_ON_VALUE: Float = 1.0f + const val ANIMATION_OFF_VALUE: Float = 0.0f + + val TOGGLE_ANIMATION_KEYS: List = listOf( + Settings.Global.WINDOW_ANIMATION_SCALE, Settings.Global.TRANSITION_ANIMATION_SCALE, + Settings.Global.ANIMATOR_DURATION_SCALE + ) + + fun isAnimationEnabled(context: Context): Boolean { + // This pref treats animation as enabled if *any* of the animation types are enabled. + for (animationSetting in TOGGLE_ANIMATION_KEYS) { + val animationValue: Float? = + SettingsGlobalStore.get(context).getFloat(animationSetting) + // Animation is enabled by default, so treat null as enabled. + if (animationValue == null || animationValue > ANIMATION_OFF_VALUE) { + return true + } + } + return false + } + + fun setAnimationEnabled(context: Context, enabled: Boolean) { + val value = if (enabled) ANIMATION_ON_VALUE else ANIMATION_OFF_VALUE; + for (animationSetting in TOGGLE_ANIMATION_KEYS) { + SettingsGlobalStore.get(context).setFloat(animationSetting, value) + } + } + } +} \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/accessibility/RemoveAnimationsPreferenceTest.kt b/tests/robotests/src/com/android/settings/accessibility/RemoveAnimationsPreferenceTest.kt new file mode 100644 index 00000000000..dda6c68a0c5 --- /dev/null +++ b/tests/robotests/src/com/android/settings/accessibility/RemoveAnimationsPreferenceTest.kt @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2024 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.accessibility + +import android.content.Context +import androidx.preference.SwitchPreferenceCompat +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.android.settings.accessibility.RemoveAnimationsPreference.Companion.ANIMATION_ON_VALUE +import com.android.settings.accessibility.RemoveAnimationsPreference.Companion.TOGGLE_ANIMATION_KEYS +import com.android.settingslib.datastore.SettingsGlobalStore +import com.android.settingslib.preference.createAndBindWidget +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class RemoveAnimationsPreferenceTest { + + private val appContext: Context = ApplicationProvider.getApplicationContext() + + private val removeAnimationsPreference = + RemoveAnimationsPreference() + + private fun getSwitchPreferenceCompat(): SwitchPreferenceCompat = + removeAnimationsPreference.createAndBindWidget(appContext) + + @Test + fun animationOff_switchPreferenceIsChecked() { + RemoveAnimationsPreference.setAnimationEnabled(appContext, false) + + assertThat(getSwitchPreferenceCompat().isChecked).isTrue() + } + + @Test + fun animationOn_switchPreferenceIsNotChecked() { + RemoveAnimationsPreference.setAnimationEnabled(appContext, true) + + assertThat(getSwitchPreferenceCompat().isChecked).isFalse() + } + + @Test + fun oneAnimationValueOn_switchPreferenceIsNotChecked() { + // Animation is disabled, except for one value. + RemoveAnimationsPreference.setAnimationEnabled(appContext, false) + SettingsGlobalStore.get(appContext).setFloat(TOGGLE_ANIMATION_KEYS[0], ANIMATION_ON_VALUE) + + assertThat(getSwitchPreferenceCompat().isChecked).isFalse() + } + + @Test + fun storageSetTrue_turnsOffAnimation() { + RemoveAnimationsPreference.setAnimationEnabled(appContext, true) + + storageSetValue(true) + + assertThat(RemoveAnimationsPreference.isAnimationEnabled(appContext)).isFalse() + } + + @Test + fun storageSetFalse_turnsOnAnimation() { + RemoveAnimationsPreference.setAnimationEnabled(appContext, false) + + storageSetValue(false) + + assertThat(RemoveAnimationsPreference.isAnimationEnabled(appContext)).isTrue() + } + + private fun storageSetValue(enabled: Boolean) = removeAnimationsPreference.storage(appContext) + .setValue(RemoveAnimationsPreference.KEY, Boolean::class.javaObjectType, enabled) +} \ No newline at end of file