Make Extra Dim main toggle and shortcut searchable

Since the Extra Dim main toggle and shortcut preferences are created in
codes, we override `getRawDataToIndex` to return the corresponding index raw data.

Bug: 354778552
Test: atest ToggleReduceBrightColorsPreferenceTest
Test: Manually check the preferences are searchable in Settings search
Flag: com.android.settings.accessibility.fix_a11y_settings_search
Change-Id: I1642e96273cd16f35b23350af0ede3b1247ef067
diff --git a/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java
index 2f7005c..ffb8d39 100644
--- a/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java
@@ -32,6 +32,7 @@
 import android.view.View;
 import android.view.ViewGroup;
 
+import androidx.annotation.VisibleForTesting;
 import androidx.preference.PreferenceCategory;
 import androidx.preference.TwoStatePreference;
 
@@ -41,6 +42,7 @@
 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;
@@ -52,6 +54,10 @@
     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;
 
@@ -197,11 +203,43 @@
         }
     }
 
+    @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) {
                 @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);
+
+                    if (Flags.fixA11ySettingsSearch()) {
+                        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;
+                }
             };
 }
diff --git a/tests/robotests/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragmentTest.java
new file mode 100644
index 0000000..7450d94
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragmentTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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 android.platform.test.annotations.EnableFlags;
+import android.platform.test.flag.junit.SetFlagsRule;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.R;
+import com.android.settingslib.search.SearchIndexableRaw;
+
+import org.junit.Rule;
+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 {
+
+    @Rule
+    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+
+    @Test
+    @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
+    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);
+    }
+}