Merge "Make ColorAndMotionFragment fully in Catalyst" into main

This commit is contained in:
Treehugger Robot
2025-02-20 12:39:07 -08:00
committed by Android (Google) Code Review
10 changed files with 456 additions and 21 deletions

View File

@@ -14,6 +14,7 @@
limitations under the License.
-->
<!-- LINT.IfChange -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
@@ -40,16 +41,16 @@
settings:controller="com.android.settings.accessibility.ColorInversionPreferenceController"/>
<!-- DarkModePreference is searchable in the Display & Touch setting.
Therefore, we set searchable = false here to avoid duplicate search results. -->
With catalyst, we're reusing the same preference. Will let the SettingsSearch
to determine how to resolve multi-entry on same preference. -->
<com.android.settings.display.darkmode.DarkModePreference
android:key="dark_ui_mode_accessibility"
android:key="dark_ui_mode"
android:icon="@drawable/ic_dark_ui"
android:title="@string/dark_ui_mode"
android:fragment="com.android.settings.display.darkmode.DarkModeSettingsFragment"
android:widgetLayout="@null"
settings:widgetLayout="@null"
settings:controller="com.android.settings.display.DarkUIPreferenceController"
settings:searchable="false"/>
settings:controller="com.android.settings.display.DarkUIPreferenceController" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_accessibility_animation"
@@ -64,3 +65,4 @@
android:title="@string/experimental_category_title">
</PreferenceCategory>
</PreferenceScreen>
<!-- LINT.ThenChange(/src/com/android/settings/accessibility/ColorAndMotionScreen.kt:ui_hierarchy) -->

View File

@@ -63,9 +63,10 @@ public class ColorAndMotionFragment extends DashboardFragment {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (!isCatalystEnabled()) {
initializeAllPreferences();
updateSystemPreferences();
mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
mShortcutFeatureKeys.add(Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED);
@@ -73,6 +74,7 @@ public class ColorAndMotionFragment extends DashboardFragment {
mSettingsContentObserver.registerKeysToObserverCallback(mShortcutFeatureKeys,
key -> updatePreferencesState());
}
}
private void updatePreferencesState() {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
@@ -84,16 +86,18 @@ public class ColorAndMotionFragment extends DashboardFragment {
@Override
public void onStart() {
super.onStart();
if (!isCatalystEnabled()) {
mSettingsContentObserver.register(getContentResolver());
}
}
@Override
public void onStop() {
super.onStop();
if (!isCatalystEnabled()) {
mSettingsContentObserver.unregister(getContentResolver());
}
}
@Override
protected int getPreferenceScreenResId() {
@@ -116,9 +120,11 @@ public class ColorAndMotionFragment extends DashboardFragment {
/**
* Updates preferences related to system configurations.
*/
// LINT.IfChange(ui_hierarchy)
private void updateSystemPreferences() {
final PreferenceCategory experimentalCategory = getPreferenceScreen().findPreference(
CATEGORY_EXPERIMENTAL);
if (ColorDisplayManager.isColorTransformAccelerated(getContext())) {
getPreferenceScreen().removePreference(experimentalCategory);
} else {
@@ -130,6 +136,7 @@ public class ColorAndMotionFragment extends DashboardFragment {
experimentalCategory.addPreference(mToggleDisableAnimationsPreference);
}
}
// LINT.ThenChange(/src/com/android/settings/accessibility/ColorAndMotionScreen.kt:ui_hierarchy)
@Nullable
@Override

View File

@@ -17,10 +17,15 @@
package com.android.settings.accessibility
import android.content.Context
import android.hardware.display.ColorDisplayManager
import com.android.settings.R
import com.android.settings.Settings.ColorAndMotionActivity
import com.android.settings.display.darkmode.DarkModeScreen
import com.android.settings.flags.Flags
import com.android.settings.utils.makeLaunchIntent
import com.android.settingslib.metadata.PreferenceCategory
import com.android.settingslib.metadata.PreferenceGroup
import com.android.settingslib.metadata.PreferenceHierarchy
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.ProvidePreferenceScreen
import com.android.settingslib.metadata.preferenceHierarchy
@@ -39,14 +44,34 @@ class ColorAndMotionScreen : PreferenceScreenCreator {
override fun isFlagEnabled(context: Context) = Flags.catalystAccessibilityColorAndMotion()
override fun hasCompleteHierarchy(): Boolean = false
override fun hasCompleteHierarchy(): Boolean = true
override fun fragmentClass() = ColorAndMotionFragment::class.java
override fun getPreferenceHierarchy(context: Context) =
preferenceHierarchy(context, this) {
override fun getPreferenceHierarchy(context: Context): PreferenceHierarchy {
// LINT.IfChange(ui_hierarchy)
if (ColorDisplayManager.isColorTransformAccelerated(context)) {
return preferenceHierarchy(context, this) {
+DaltonizerPreference()
+ColorInversionPreference()
+DarkModeScreen.KEY
+RemoveAnimationsPreference()
}
} else {
return preferenceHierarchy(context, this) {
+ColorInversionPreference()
+DarkModeScreen.KEY
+PreferenceCategory(
"experimental_category",
R.string.experimental_category_title
) += {
+DaltonizerPreference()
+RemoveAnimationsPreference()
}
}
}
// LINT.ThenChange(/res/xml/accessibility_color_and_motion.xml, /src/com/android/settings/accessibility/ColorAndMotionFragment.java:ui_hierarchy)
}
override fun getLaunchIntent(context: Context, metadata: PreferenceMetadata?) =
makeLaunchIntent(context, ColorAndMotionActivity::class.java, metadata?.key)

View File

@@ -0,0 +1,81 @@
/*
* 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.accessibility
import android.app.settings.SettingsEnums
import android.content.Context
import android.provider.Settings
import com.android.settings.R
import com.android.settings.core.SubSettingLauncher
import com.android.settingslib.datastore.HandlerExecutor
import com.android.settingslib.datastore.KeyedObserver
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.metadata.PreferenceLifecycleContext
import com.android.settingslib.metadata.PreferenceLifecycleProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.PreferenceSummaryProvider
class ColorInversionPreference : PreferenceMetadata, PreferenceSummaryProvider,
PreferenceLifecycleProvider {
override val key: String
get() = PREFERENCE_KEY
override val title: Int
get() = R.string.accessibility_display_inversion_preference_title
override val icon: Int
get() = R.drawable.ic_color_inversion
override val keywords: Int
get() = R.string.keywords_color_inversion
private var mSettingsKeyedObserver: KeyedObserver<String>? = null
override fun intent(context: Context) =
SubSettingLauncher(context)
.setDestination(ToggleColorInversionPreferenceFragment::class.java.name)
.setSourceMetricsCategory(SettingsEnums.ACCESSIBILITY_COLOR_AND_MOTION)
.toIntent()
override fun getSummary(context: Context): CharSequence? {
return AccessibilityUtil.getSummary(
context,
SETTING_KEY,
R.string.color_inversion_state_on, R.string.color_inversion_state_off
)
}
override fun onStart(context: PreferenceLifecycleContext) {
val observer =
KeyedObserver<String> { _, _ -> context.notifyPreferenceChange(PREFERENCE_KEY) }
mSettingsKeyedObserver = observer
val storage = SettingsSecureStore.get(context)
storage.addObserver(SETTING_KEY, observer, HandlerExecutor.main)
}
override fun onStop(context: PreferenceLifecycleContext) {
mSettingsKeyedObserver?.let {
val storage = SettingsSecureStore.get(context)
storage.removeObserver(SETTING_KEY, it)
mSettingsKeyedObserver = null
}
}
companion object {
const val PREFERENCE_KEY = "toggle_inversion_preference"
const val SETTING_KEY = Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED
}
}

View File

@@ -22,6 +22,7 @@ import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
// LINT.IfChange
/** Controller that shows the color inversion summary. */
public class ColorInversionPreferenceController extends BasePreferenceController {
@@ -45,3 +46,4 @@ public class ColorInversionPreferenceController extends BasePreferenceController
return AVAILABLE;
}
}
// LINT.ThenChange(/src/com/android/settings/accessibility/ColorInversionPreference.kt)

View File

@@ -0,0 +1,82 @@
/*
* 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.accessibility
import android.app.settings.SettingsEnums
import android.content.Context
import android.provider.Settings
import com.android.settings.R
import com.android.settings.core.SubSettingLauncher
import com.android.settingslib.datastore.HandlerExecutor
import com.android.settingslib.datastore.KeyedObserver
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.metadata.PreferenceLifecycleContext
import com.android.settingslib.metadata.PreferenceLifecycleProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.PreferenceSummaryProvider
class DaltonizerPreference : PreferenceMetadata, PreferenceSummaryProvider,
PreferenceLifecycleProvider {
override val key: String
get() = PREFERENCE_KEY
override val title: Int
get() = com.android.settingslib.R.string.accessibility_display_daltonizer_preference_title
override val icon: Int
get() = R.drawable.ic_daltonizer
override val keywords: Int
get() = R.string.keywords_color_correction
private var mSettingsKeyedObserver: KeyedObserver<String>? = null
override fun intent(context: Context) =
SubSettingLauncher(context)
.setDestination(ToggleDaltonizerPreferenceFragment::class.java.name)
.setSourceMetricsCategory(SettingsEnums.ACCESSIBILITY_COLOR_AND_MOTION)
.toIntent()
override fun getSummary(context: Context): CharSequence? {
return AccessibilityUtil.getSummary(
context,
SETTING_KEY,
R.string.daltonizer_state_on, R.string.daltonizer_state_off
)
}
override fun onStart(context: PreferenceLifecycleContext) {
val observer =
KeyedObserver<String> { _, _ -> context.notifyPreferenceChange(PREFERENCE_KEY) }
mSettingsKeyedObserver = observer
val storage = SettingsSecureStore.get(context)
storage.addObserver(SETTING_KEY, observer, HandlerExecutor.main)
}
override fun onStop(context: PreferenceLifecycleContext) {
mSettingsKeyedObserver?.let {
val storage = SettingsSecureStore.get(context)
storage.removeObserver(SETTING_KEY, it)
mSettingsKeyedObserver = null
}
}
companion object {
const val PREFERENCE_KEY = "daltonizer_preference"
const val SETTING_KEY = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED
}
}

View File

@@ -22,6 +22,7 @@ import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
// LINT.IfChange
/** Controller that shows and updates the color correction summary. */
public class DaltonizerPreferenceController extends BasePreferenceController {
private static final String DALTONIZER_ENABLED =
@@ -44,3 +45,4 @@ public class DaltonizerPreferenceController extends BasePreferenceController {
R.string.daltonizer_state_on, R.string.daltonizer_state_off);
}
}
// LINT.ThenChange(/src/com/android/settings/accessibility/DaltonizerPreference.kt)

View File

@@ -308,7 +308,10 @@ public abstract class ToggleFeaturePreferenceFragment extends DashboardFragment
getActivity().setTitle(title);
}
protected void onProcessArguments(Bundle arguments) {
protected void onProcessArguments(@Nullable Bundle arguments) {
if (arguments == null) {
return;
}
// Key.
mPreferenceKey = arguments.getString(AccessibilitySettings.EXTRA_PREFERENCE_KEY);
@@ -432,7 +435,8 @@ public abstract class ToggleFeaturePreferenceFragment extends DashboardFragment
private void initToggleServiceSwitchPreference() {
mToggleServiceSwitchPreference = new SettingsMainSwitchPreference(getPrefContext());
mToggleServiceSwitchPreference.setKey(getUseServicePreferenceKey());
if (getArguments().containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
if (getArguments() != null
&& getArguments().containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
final boolean enabled = getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED);
mToggleServiceSwitchPreference.setChecked(enabled);
}

View File

@@ -0,0 +1,115 @@
/*
* 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.accessibility
import android.app.settings.SettingsEnums
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.android.settings.SettingsActivity
import com.android.settings.SubSettings
import com.android.settings.accessibility.ColorInversionPreference.Companion.PREFERENCE_KEY
import com.android.settings.accessibility.ColorInversionPreference.Companion.SETTING_KEY
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.metadata.PreferenceLifecycleContext
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.robolectric.shadows.ShadowLooper
@RunWith(AndroidJUnit4::class)
class ColorInversionPreferenceTest {
private val mockLifecycleContext = mock<PreferenceLifecycleContext>()
private val appContext: Context = ApplicationProvider.getApplicationContext()
private val colorInversionPreference = ColorInversionPreference()
@Before
fun setUp() {
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.OFF)
}
@Test
fun getIntent_returnColorInversionScreenIntent() {
val intent = colorInversionPreference.intent(appContext)
assertThat(intent).isNotNull()
assertThat(intent!!.action).isEqualTo(Intent.ACTION_MAIN)
assertThat(intent.component).isEqualTo(ComponentName(appContext, SubSettings::class.java))
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
ToggleColorInversionPreferenceFragment::class.java.name
)
assertThat(
intent.getIntExtra(
MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY,
0
)
).isEqualTo(
SettingsEnums.ACCESSIBILITY_COLOR_AND_MOTION
)
}
@Test
fun onStart_settingChanges_notifyPrefChange() {
colorInversionPreference.onStart(mockLifecycleContext)
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.ON)
ShadowLooper.runUiThreadTasksIncludingDelayedTasks()
verify(mockLifecycleContext, times(1)).notifyPreferenceChange(PREFERENCE_KEY)
}
@Test
fun onStop_settingChanges_doNotNotifyPrefChange() {
colorInversionPreference.onStop(mockLifecycleContext)
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.ON)
ShadowLooper.runUiThreadTasksIncludingDelayedTasks()
verify(mockLifecycleContext, never()).notifyPreferenceChange(any())
}
@Test
fun getSummary_colorInversionOn_verifySummary() {
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.ON)
assertThat(colorInversionPreference.getPreferenceSummary(appContext)).isEqualTo(
appContext.getText(
R.string.color_inversion_state_on
)
)
}
@Test
fun getSummary_colorInversionOff_verifySummary() {
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.OFF)
assertThat(colorInversionPreference.getPreferenceSummary(appContext)).isEqualTo(
appContext.getText(
R.string.color_inversion_state_off
)
)
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.accessibility
import android.app.settings.SettingsEnums
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.android.settings.SettingsActivity
import com.android.settings.SubSettings
import com.android.settings.accessibility.DaltonizerPreference.Companion.PREFERENCE_KEY
import com.android.settings.accessibility.DaltonizerPreference.Companion.SETTING_KEY
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.metadata.PreferenceLifecycleContext
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.robolectric.shadows.ShadowLooper
@RunWith(AndroidJUnit4::class)
class DaltonizerPreferenceTest {
private val mockLifecycleContext = mock<PreferenceLifecycleContext>()
private val appContext: Context = ApplicationProvider.getApplicationContext()
private val daltonizerPreference = DaltonizerPreference()
@Before
fun setUp() {
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.OFF)
}
@Test
fun getIntent_returnDaltonizerScreenIntent() {
val intent = daltonizerPreference.intent(appContext)
assertThat(intent).isNotNull()
assertThat(intent!!.action).isEqualTo(Intent.ACTION_MAIN)
assertThat(intent.component).isEqualTo(ComponentName(appContext, SubSettings::class.java))
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
ToggleDaltonizerPreferenceFragment::class.java.name
)
assertThat(
intent.getIntExtra(
MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY,
0
)
).isEqualTo(
SettingsEnums.ACCESSIBILITY_COLOR_AND_MOTION
)
}
@Test
fun onStart_settingChanges_notifyPrefChange() {
daltonizerPreference.onStart(mockLifecycleContext)
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.ON)
ShadowLooper.runUiThreadTasksIncludingDelayedTasks()
verify(mockLifecycleContext, times(1)).notifyPreferenceChange(PREFERENCE_KEY)
}
@Test
fun onStop_settingChanges_doNotNotifyPrefChange() {
daltonizerPreference.onStop(mockLifecycleContext)
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.ON)
ShadowLooper.runUiThreadTasksIncludingDelayedTasks()
verify(mockLifecycleContext, never()).notifyPreferenceChange(any())
}
@Test
fun getSummary_daltonizerOn_verifySummary() {
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.ON)
assertThat(daltonizerPreference.getPreferenceSummary(appContext)).isEqualTo(
appContext.getText(
R.string.daltonizer_state_on
)
)
}
@Test
fun getSummary_daltonizerOff_verifySummary() {
SettingsSecureStore.get(appContext).setInt(SETTING_KEY, AccessibilityUtil.State.OFF)
assertThat(daltonizerPreference.getPreferenceSummary(appContext)).isEqualTo(
appContext.getText(
R.string.daltonizer_state_off
)
)
}
}