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

Commit 91fe9256 authored by Chun-Ku Lin's avatar Chun-Ku Lin
Browse files

refactor: migrate ExtraDim screen to xml/preferenceController style

- Updates the fragment test to be closer to integration test that could
  be used to verify the user interactions
- Use expressive style for the intensity slider

Bug: 406052931
Test: atest com.android.settings.accessibility
Test: manually verify the user interactions and UI on the screens
Flag: EXEMPT risk taken
Change-Id: Icbdd40bd1e7596058a74d0ca678508c60064a9d7
parent 0c7a1458
Loading
Loading
Loading
Loading
+10 −164
Original line number Diff line number Diff line
@@ -17,112 +17,33 @@
package com.android.settings.accessibility;

import static com.android.internal.accessibility.AccessibilityShortcutController.REDUCE_BRIGHT_COLORS_COMPONENT_NAME;
import static com.android.internal.accessibility.AccessibilityShortcutController.REDUCE_BRIGHT_COLORS_TILE_SERVICE_COMPONENT_NAME;
import static com.android.settings.accessibility.AccessibilityStatsLogUtils.logAccessibilityServiceEnabled;

import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.hardware.display.ColorDisplayManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceCategory;
import androidx.preference.TwoStatePreference;
import androidx.annotation.NonNull;

import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SeekBarPreference;
import com.android.settings.widget.SettingsMainSwitchPreference;
import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.search.SearchIndexableRaw;

import java.util.ArrayList;
import java.util.List;

/** Settings for reducing brightness. */
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class ToggleReduceBrightColorsPreferenceFragment extends ToggleFeaturePreferenceFragment {

public class ToggleReduceBrightColorsPreferenceFragment extends ShortcutFragment {
    private static final String TAG = "ToggleReduceBrightColorsPreferenceFragment";
    private static final String KEY_INTENSITY = "rbc_intensity";
    private static final String KEY_PERSIST = "rbc_persist";
    @VisibleForTesting
    static final String KEY_SHORTCUT = "rbc_shortcut";
    @VisibleForTesting
    static final String KEY_SWITCH = "rbc_switch";
    private static final String REDUCE_BRIGHT_COLORS_ACTIVATED_KEY =
            Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED;

    private ReduceBrightColorsIntensityPreferenceController mRbcIntensityPreferenceController;
    private ReduceBrightColorsPersistencePreferenceController mRbcPersistencePreferenceController;
    private ColorDisplayManager mColorDisplayManager;

    @NonNull
    @Override
    protected void registerKeysToObserverCallback(
            AccessibilitySettingsContentObserver contentObserver) {
        super.registerKeysToObserverCallback(contentObserver);

        final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1);
        enableServiceFeatureKeys.add(REDUCE_BRIGHT_COLORS_ACTIVATED_KEY);
        contentObserver.registerKeysToObserverCallback(enableServiceFeatureKeys,
                key -> updateSwitchBarToggleSwitch());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mImageUri = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
                .authority(getPrefContext().getPackageName())
                .appendPath(String.valueOf(R.raw.extra_dim_banner))
                .build();
        mComponentName = REDUCE_BRIGHT_COLORS_COMPONENT_NAME;
        mFeatureName = getText(R.string.reduce_bright_colors_preference_title);
        mHtmlDescription = getText(R.string.reduce_bright_colors_preference_subtitle);
        mTopIntroTitle = getText(R.string.reduce_bright_colors_preference_intro_text);
        mRbcIntensityPreferenceController =
                new ReduceBrightColorsIntensityPreferenceController(getContext(), KEY_INTENSITY);
        mRbcPersistencePreferenceController =
                new ReduceBrightColorsPersistencePreferenceController(getContext(), KEY_PERSIST);
        mRbcIntensityPreferenceController.displayPreference(getPreferenceScreen());
        mRbcPersistencePreferenceController.displayPreference(getPreferenceScreen());
        mColorDisplayManager = getContext().getSystemService(ColorDisplayManager.class);
        final View view = super.onCreateView(inflater, container, savedInstanceState);
        // Parent sets the title when creating the view, so set it after calling super
        mToggleServiceSwitchPreference.setTitle(R.string.reduce_bright_colors_switch_title);
        updateGeneralCategoryOrder();
        updateFooterPreference();
        return view;
    }

    private void updateGeneralCategoryOrder() {
        final PreferenceCategory generalCategory = findPreference(KEY_GENERAL_CATEGORY);
        final SeekBarPreference intensity = findPreference(KEY_INTENSITY);
        getPreferenceScreen().removePreference(intensity);
        intensity.setOrder(mShortcutPreference.getOrder() - 2);
        generalCategory.addPreference(intensity);
        final TwoStatePreference persist = findPreference(KEY_PERSIST);
        getPreferenceScreen().removePreference(persist);
        persist.setOrder(mShortcutPreference.getOrder() - 1);
        generalCategory.addPreference(persist);
    }

    private void updateFooterPreference() {
        final String title = getPrefContext().getString(R.string.reduce_bright_colors_about_title);
        mFooterPreferenceController.setIntroductionTitle(title);
        mFooterPreferenceController.displayPreference(getPreferenceScreen());
    public CharSequence getFeatureName() {
        return getString(R.string.reduce_bright_colors_preference_title);
    }

    @NonNull
    @Override
    public void onResume() {
        super.onResume();
        updateSwitchBarToggleSwitch();
    public ComponentName getFeatureComponentName() {
        return REDUCE_BRIGHT_COLORS_COMPONENT_NAME;
    }

    @Override
@@ -138,7 +59,7 @@ public class ToggleReduceBrightColorsPreferenceFragment extends ToggleFeaturePre

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.reduce_bright_colors_settings;
        return R.xml.accessibility_extra_dim_settings;
    }

    @Override
@@ -146,87 +67,12 @@ public class ToggleReduceBrightColorsPreferenceFragment extends ToggleFeaturePre
        return TAG;
    }

    @Override
    protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
        logAccessibilityServiceEnabled(mComponentName, enabled);
        mColorDisplayManager.setReduceBrightColorsActivated(enabled);
    }

    @Override
    protected void onRemoveSwitchPreferenceToggleSwitch() {
        super.onRemoveSwitchPreferenceToggleSwitch();
        mToggleServiceSwitchPreference.setOnPreferenceClickListener(
                /* onPreferenceClickListener= */ null);
    }

    @Override
    protected void updateToggleServiceTitle(SettingsMainSwitchPreference switchPreference) {
        switchPreference.setTitle(R.string.reduce_bright_colors_preference_title);
    }

    @Override
    protected CharSequence getShortcutTitle() {
        return getText(R.string.reduce_bright_colors_shortcut_title);
    }

    @Override
    int getUserShortcutTypes() {
        return AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(),
                mComponentName);
    }

    @Override
    ComponentName getTileComponentName() {
        return REDUCE_BRIGHT_COLORS_TILE_SERVICE_COMPONENT_NAME;
    }

    @Override
    protected void updateSwitchBarToggleSwitch() {
        final boolean checked = mColorDisplayManager.isReduceBrightColorsActivated();
        mRbcIntensityPreferenceController.updateState(getPreferenceScreen()
                .findPreference(KEY_INTENSITY));
        mRbcPersistencePreferenceController.updateState(getPreferenceScreen()
                .findPreference(KEY_PERSIST));
        if (mToggleServiceSwitchPreference.isChecked() != checked) {
            mToggleServiceSwitchPreference.setChecked(checked);
        }
    }

    @Override
    protected String getUseServicePreferenceKey() {
        return KEY_SWITCH;
    }

    @Override
    protected String getShortcutPreferenceKey() {
        return KEY_SHORTCUT;
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider(R.xml.reduce_bright_colors_settings) {
            new BaseSearchIndexProvider(R.xml.accessibility_extra_dim_settings) {
                @Override
                protected boolean isPageSearchEnabled(Context context) {
                    return ColorDisplayManager.isReduceBrightColorsAvailable(context);
                }

                @Override
                public List<SearchIndexableRaw> getRawDataToIndex(Context context,
                        boolean enabled) {
                    final List<SearchIndexableRaw> rawData =
                            super.getRawDataToIndex(context, enabled);

                    SearchIndexableRaw shortcutRaw = new SearchIndexableRaw(context);
                    shortcutRaw.key = KEY_SHORTCUT;
                    shortcutRaw.title = context.getString(
                            R.string.reduce_bright_colors_shortcut_title);
                    rawData.add(shortcutRaw);

                    SearchIndexableRaw mainSwitchRaw = new SearchIndexableRaw(context);
                    mainSwitchRaw.key = KEY_SWITCH;
                    mainSwitchRaw.title = context.getString(
                            R.string.reduce_bright_colors_switch_title);
                    rawData.add(mainSwitchRaw);
                    return rawData;
                }
            };
}
+0 −63
Original line number Diff line number Diff line
/*
 * 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 static com.android.settings.accessibility.ToggleReduceBrightColorsPreferenceFragment.KEY_SHORTCUT;
import static com.android.settings.accessibility.ToggleReduceBrightColorsPreferenceFragment.KEY_SWITCH;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;

import com.android.settings.R;
import com.android.settingslib.search.SearchIndexableRaw;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import java.util.ArrayList;
import java.util.List;

/** Tests for {@link ToggleReduceBrightColorsPreferenceFragment} */
@RunWith(RobolectricTestRunner.class)
public class ToggleReduceBrightColorsPreferenceFragmentTest {
    private final Context mContext = ApplicationProvider.getApplicationContext();

    @Test
    public void getRawDataToIndex_flagOn_returnPreferencesCreatedInCodes() {
        String[] expectedKeys = {KEY_SHORTCUT, KEY_SWITCH};
        String[] expectedTitles = {
                mContext.getString(R.string.reduce_bright_colors_shortcut_title),
                mContext.getString(R.string.reduce_bright_colors_switch_title)};
        List<String> keysResultList = new ArrayList<>();
        List<String> titlesResultList = new ArrayList<>();
        List<SearchIndexableRaw> rawData = ToggleReduceBrightColorsPreferenceFragment
                .SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(mContext, /* enabled= */ true);

        for (SearchIndexableRaw rawDataItem : rawData) {
            keysResultList.add(rawDataItem.key);
            titlesResultList.add(rawDataItem.title);
        }

        // Verify that `getRawDataToIndex` includes the preferences created in codes
        assertThat(keysResultList).containsAtLeastElementsIn(expectedKeys);
        assertThat(titlesResultList).containsAtLeastElementsIn(expectedTitles);
    }
}
+422 −0

File added.

Preview size limit exceeded, changes collapsed.

+37 −0
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.testutils

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder

fun Preference.inflateViewHolder(): PreferenceViewHolder {
    val inflater = LayoutInflater.from(context)
    val view: View = inflater.inflate(layoutResource, null)
    val viewHolder = PreferenceViewHolder.createInstanceForTests(view)
    if (widgetLayoutResource != 0) {
        inflater.inflate(
            widgetLayoutResource,
            viewHolder.itemView.findViewById<ViewGroup?>(android.R.id.widget_frame)
        )
    }
    onBindViewHolder(viewHolder)
    return viewHolder
}