Merge changes Iad13fbe1,I6eed7598 into main

* changes:
  Makes Use Color inversion searchable.
  Create a method to allow child classes to define it's main switch toggle's pref key
This commit is contained in:
Jean Chen
2024-10-09 03:25:55 +00:00
committed by Android (Google) Code Review
7 changed files with 87 additions and 32 deletions

View File

@@ -67,7 +67,7 @@ public class LaunchAccessibilityActivityPreferenceFragment extends ToggleFeature
initLaunchPreference(); initLaunchPreference();
final View view = super.onCreateView(inflater, container, savedInstanceState); final View view = super.onCreateView(inflater, container, savedInstanceState);
removePreference(KEY_USE_SERVICE_PREFERENCE); removePreference(getUseServicePreferenceKey());
return view; return view;
} }

View File

@@ -33,6 +33,8 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.QuickSettingsTooltipType; import com.android.settings.accessibility.AccessibilityUtil.QuickSettingsTooltipType;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
@@ -52,7 +54,10 @@ public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePrefere
private static final String TAG = "ToggleColorInversionPreferenceFragment"; private static final String TAG = "ToggleColorInversionPreferenceFragment";
private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED; private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED;
private static final String KEY_SHORTCUT_PREFERENCE = "color_inversion_shortcut_key"; @VisibleForTesting
static final String KEY_SHORTCUT_PREFERENCE = "color_inversion_shortcut_key";
@VisibleForTesting
static final String KEY_SWITCH_PREFERENCE = "color_inversion_switch_preference_key";
@Override @Override
protected void registerKeysToObserverCallback( protected void registerKeysToObserverCallback(
@@ -131,6 +136,11 @@ public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePrefere
switchPreference.setTitle(R.string.accessibility_display_inversion_switch_title); switchPreference.setTitle(R.string.accessibility_display_inversion_switch_title);
} }
@Override
protected String getUseServicePreferenceKey() {
return KEY_SWITCH_PREFERENCE;
}
@Override @Override
protected CharSequence getShortcutTitle() { protected CharSequence getShortcutTitle() {
return getText(R.string.accessibility_display_inversion_shortcut_title); return getText(R.string.accessibility_display_inversion_shortcut_title);
@@ -194,12 +204,22 @@ public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePrefere
@Override @Override
public List<SearchIndexableRaw> getRawDataToIndex(Context context, public List<SearchIndexableRaw> getRawDataToIndex(Context context,
boolean enabled) { boolean enabled) {
final List<SearchIndexableRaw> rawData = new ArrayList<>(); final List<SearchIndexableRaw> rawData =
super.getRawDataToIndex(context, enabled);
SearchIndexableRaw raw = new SearchIndexableRaw(context); SearchIndexableRaw raw = new SearchIndexableRaw(context);
raw.key = KEY_SHORTCUT_PREFERENCE; raw.key = KEY_SHORTCUT_PREFERENCE;
raw.title = context.getString( raw.title = context.getString(
R.string.accessibility_display_inversion_shortcut_title); R.string.accessibility_display_inversion_shortcut_title);
rawData.add(raw); rawData.add(raw);
if (Flags.fixA11ySettingsSearch()) {
SearchIndexableRaw mainPreferenceRaw = new SearchIndexableRaw(context);
mainPreferenceRaw.key = KEY_SWITCH_PREFERENCE;
mainPreferenceRaw.title = context.getString(
R.string.accessibility_display_inversion_switch_title);
rawData.add(mainPreferenceRaw);
}
return rawData; return rawData;
} }
}; };

View File

@@ -106,7 +106,7 @@ public class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceF
final List<String> lists = new ArrayList<>(); final List<String> lists = new ArrayList<>();
lists.add(KEY_TOP_INTRO_PREFERENCE); lists.add(KEY_TOP_INTRO_PREFERENCE);
lists.add(KEY_PREVIEW); lists.add(KEY_PREVIEW);
lists.add(KEY_USE_SERVICE_PREFERENCE); lists.add(getUseServicePreferenceKey());
// Putting saturation level close to the preview so users can see what is changing. // Putting saturation level close to the preview so users can see what is changing.
lists.add(KEY_SATURATION); lists.add(KEY_SATURATION);
lists.add(KEY_DEUTERANOMALY); lists.add(KEY_DEUTERANOMALY);

View File

@@ -83,7 +83,6 @@ public abstract class ToggleFeaturePreferenceFragment extends DashboardFragment
public static final String KEY_GENERAL_CATEGORY = "general_categories"; public static final String KEY_GENERAL_CATEGORY = "general_categories";
public static final String KEY_SHORTCUT_PREFERENCE = "shortcut_preference"; public static final String KEY_SHORTCUT_PREFERENCE = "shortcut_preference";
protected static final String KEY_TOP_INTRO_PREFERENCE = "top_intro"; protected static final String KEY_TOP_INTRO_PREFERENCE = "top_intro";
protected static final String KEY_USE_SERVICE_PREFERENCE = "use_service";
protected static final String KEY_HTML_DESCRIPTION_PREFERENCE = "html_description"; protected static final String KEY_HTML_DESCRIPTION_PREFERENCE = "html_description";
protected static final String KEY_SAVED_QS_TOOLTIP_RESHOW = "qs_tooltip_reshow"; protected static final String KEY_SAVED_QS_TOOLTIP_RESHOW = "qs_tooltip_reshow";
protected static final String KEY_SAVED_QS_TOOLTIP_TYPE = "qs_tooltip_type"; protected static final String KEY_SAVED_QS_TOOLTIP_TYPE = "qs_tooltip_type";
@@ -325,6 +324,10 @@ public abstract class ToggleFeaturePreferenceFragment extends DashboardFragment
switchPreference.setTitle(title); switchPreference.setTitle(title);
} }
protected String getUseServicePreferenceKey() {
return "use_service";
}
protected CharSequence getShortcutTitle() { protected CharSequence getShortcutTitle() {
return getString(R.string.accessibility_shortcut_title, mPackageName); return getString(R.string.accessibility_shortcut_title, mPackageName);
} }
@@ -411,7 +414,7 @@ public abstract class ToggleFeaturePreferenceFragment extends DashboardFragment
final List<String> lists = new ArrayList<>(); final List<String> lists = new ArrayList<>();
lists.add(KEY_TOP_INTRO_PREFERENCE); lists.add(KEY_TOP_INTRO_PREFERENCE);
lists.add(KEY_ANIMATED_IMAGE); lists.add(KEY_ANIMATED_IMAGE);
lists.add(KEY_USE_SERVICE_PREFERENCE); lists.add(getUseServicePreferenceKey());
lists.add(KEY_GENERAL_CATEGORY); lists.add(KEY_GENERAL_CATEGORY);
lists.add(KEY_HTML_DESCRIPTION_PREFERENCE); lists.add(KEY_HTML_DESCRIPTION_PREFERENCE);
return lists; return lists;
@@ -476,7 +479,7 @@ public abstract class ToggleFeaturePreferenceFragment extends DashboardFragment
private void initToggleServiceSwitchPreference() { private void initToggleServiceSwitchPreference() {
mToggleServiceSwitchPreference = new SettingsMainSwitchPreference(getPrefContext()); mToggleServiceSwitchPreference = new SettingsMainSwitchPreference(getPrefContext());
mToggleServiceSwitchPreference.setKey(KEY_USE_SERVICE_PREFERENCE); mToggleServiceSwitchPreference.setKey(getUseServicePreferenceKey());
if (getArguments().containsKey(AccessibilitySettings.EXTRA_CHECKED)) { if (getArguments().containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
final boolean enabled = getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED); final boolean enabled = getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED);
mToggleServiceSwitchPreference.setChecked(enabled); mToggleServiceSwitchPreference.setChecked(enabled);

View File

@@ -18,7 +18,8 @@ package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON; import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.android.settings.accessibility.ToggleColorInversionPreferenceFragment.KEY_USE_SERVICE_PREFERENCE; import static com.android.settings.accessibility.ToggleColorInversionPreferenceFragment.KEY_SHORTCUT_PREFERENCE;
import static com.android.settings.accessibility.ToggleColorInversionPreferenceFragment.KEY_SWITCH_PREFERENCE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@@ -32,12 +33,12 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.platform.test.annotations.DisableFlags; import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule; import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings; import android.provider.Settings;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.accessibility.Flags;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
@@ -50,6 +51,7 @@ import com.android.settings.accessibility.AccessibilityUtil.QuickSettingsTooltip
import com.android.settings.testutils.XmlTestUtils; import com.android.settings.testutils.XmlTestUtils;
import com.android.settings.testutils.shadow.ShadowFragment; import com.android.settings.testutils.shadow.ShadowFragment;
import com.android.settings.widget.SettingsMainSwitchPreference; import com.android.settings.widget.SettingsMainSwitchPreference;
import com.android.settingslib.search.SearchIndexableRaw;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
@@ -63,6 +65,7 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow; import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** Tests for {@link ToggleColorInversionPreferenceFragment} */ /** Tests for {@link ToggleColorInversionPreferenceFragment} */
@@ -93,10 +96,10 @@ public class ToggleColorInversionPreferenceFragmentTest {
when(mActivity.getContentResolver()).thenReturn(mContext.getContentResolver()); when(mActivity.getContentResolver()).thenReturn(mContext.getContentResolver());
mScreen = spy(new PreferenceScreen(mContext, /* attrs= */ null)); mScreen = spy(new PreferenceScreen(mContext, /* attrs= */ null));
when(mScreen.findPreference(KEY_USE_SERVICE_PREFERENCE)) when(mScreen.findPreference(mFragment.getUseServicePreferenceKey()))
.thenReturn(mFragment.mToggleServiceSwitchPreference); .thenReturn(mFragment.mToggleServiceSwitchPreference);
doReturn(mScreen).when(mFragment).getPreferenceScreen(); doReturn(mScreen).when(mFragment).getPreferenceScreen();
mSwitchPreference = mScreen.findPreference(KEY_USE_SERVICE_PREFERENCE); mSwitchPreference = mScreen.findPreference(mFragment.getUseServicePreferenceKey());
} }
@Test @Test
@@ -137,7 +140,7 @@ public class ToggleColorInversionPreferenceFragmentTest {
} }
@Test @Test
@DisableFlags(Flags.FLAG_A11Y_QS_SHORTCUT) @DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
public void onPreferenceToggled_colorCorrectDisabled_shouldReturnTrueAndShowTooltipView() { public void onPreferenceToggled_colorCorrectDisabled_shouldReturnTrueAndShowTooltipView() {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, OFF); Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, OFF);
@@ -199,6 +202,41 @@ public class ToggleColorInversionPreferenceFragmentTest {
assertThat(keys).containsAtLeastElementsIn(niks); assertThat(keys).containsAtLeastElementsIn(niks);
} }
@Test
@DisableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
public void getRawDataToIndex_flagOff_returnShortcutIndexablePreferences() {
List<SearchIndexableRaw> rawData = ToggleColorInversionPreferenceFragment
.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(mContext, /* enabled= */ true);
assertThat(rawData).hasSize(1);
assertThat(rawData.get(0).key).isEqualTo(KEY_SHORTCUT_PREFERENCE);
assertThat(rawData.get(0).title).isEqualTo(mContext.getString(
R.string.accessibility_display_inversion_shortcut_title));
}
@Test
@EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
public void getRawDataToIndex_flagOn_returnAllIndexablePreferences() {
String[] expectedKeys = {KEY_SHORTCUT_PREFERENCE, KEY_SWITCH_PREFERENCE};
String[] expectedTitles = {
mContext.getString(R.string.accessibility_display_inversion_shortcut_title),
mContext.getString(R.string.accessibility_display_inversion_switch_title)};
List<String> keysResultList = new ArrayList<>();
List<String> titlesResultList = new ArrayList<>();
List<SearchIndexableRaw> rawData = ToggleColorInversionPreferenceFragment
.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(mContext, /* enabled= */ true);
for (SearchIndexableRaw rawDataItem : rawData) {
keysResultList.add(rawDataItem.key);
titlesResultList.add(rawDataItem.title);
}
assertThat(rawData).hasSize(2);
assertThat(keysResultList).containsExactly(expectedKeys);
assertThat(titlesResultList).containsExactly(expectedTitles);
}
private static PopupWindow getLatestPopupWindow() { private static PopupWindow getLatestPopupWindow() {
final ShadowApplication shadowApplication = final ShadowApplication shadowApplication =
Shadow.extract(ApplicationProvider.getApplicationContext()); Shadow.extract(ApplicationProvider.getApplicationContext());
@@ -220,7 +258,7 @@ public class ToggleColorInversionPreferenceFragmentTest {
mComponentName = PLACEHOLDER_COMPONENT_NAME; mComponentName = PLACEHOLDER_COMPONENT_NAME;
final SettingsMainSwitchPreference switchPreference = final SettingsMainSwitchPreference switchPreference =
new SettingsMainSwitchPreference(context); new SettingsMainSwitchPreference(context);
switchPreference.setKey(KEY_USE_SERVICE_PREFERENCE); switchPreference.setKey(getUseServicePreferenceKey());
mToggleServiceSwitchPreference = switchPreference; mToggleServiceSwitchPreference = switchPreference;
setArguments(new Bundle()); setArguments(new Bundle());
} }

View File

@@ -18,7 +18,6 @@ package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON; import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.KEY_USE_SERVICE_PREFERENCE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@@ -189,6 +188,6 @@ public class ToggleDaltonizerPreferenceFragmentTest {
private SettingsMainSwitchPreference getMainFeatureToggle( private SettingsMainSwitchPreference getMainFeatureToggle(
ToggleDaltonizerPreferenceFragment fragment) { ToggleDaltonizerPreferenceFragment fragment) {
return fragment.findPreference(KEY_USE_SERVICE_PREFERENCE); return fragment.findPreference(fragment.getUseServicePreferenceKey());
} }
} }

View File

@@ -235,8 +235,7 @@ public class ToggleFeaturePreferenceFragmentTest {
@Test @Test
@Config(shadows = ShadowFragment.class) @Config(shadows = ShadowFragment.class)
public void onPreferenceToggledOnDisabledService_notShowTooltipView() { public void onPreferenceToggledOnDisabledService_notShowTooltipView() {
mFragment.onPreferenceToggled( mFragment.onPreferenceToggled(mFragment.getUseServicePreferenceKey(), /* enabled= */ false);
ToggleFeaturePreferenceFragment.KEY_USE_SERVICE_PREFERENCE, /* enabled= */ false);
assertThat(getLatestPopupWindow()).isNull(); assertThat(getLatestPopupWindow()).isNull();
} }
@@ -245,8 +244,7 @@ public class ToggleFeaturePreferenceFragmentTest {
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT) @DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
@Config(shadows = ShadowFragment.class) @Config(shadows = ShadowFragment.class)
public void onPreferenceToggledOnEnabledService_showTooltipView() { public void onPreferenceToggledOnEnabledService_showTooltipView() {
mFragment.onPreferenceToggled( mFragment.onPreferenceToggled(mFragment.getUseServicePreferenceKey(), /* enabled= */ true);
ToggleFeaturePreferenceFragment.KEY_USE_SERVICE_PREFERENCE, /* enabled= */ true);
assertThat(getLatestPopupWindow().isShowing()).isTrue(); assertThat(getLatestPopupWindow().isShowing()).isTrue();
} }
@@ -258,8 +256,7 @@ public class ToggleFeaturePreferenceFragmentTest {
suwIntent.putExtra(WizardManagerHelper.EXTRA_IS_SETUP_FLOW, true); suwIntent.putExtra(WizardManagerHelper.EXTRA_IS_SETUP_FLOW, true);
when(mActivity.getIntent()).thenReturn(suwIntent); when(mActivity.getIntent()).thenReturn(suwIntent);
mFragment.onPreferenceToggled( mFragment.onPreferenceToggled(mFragment.getUseServicePreferenceKey(), /* enabled= */ true);
ToggleFeaturePreferenceFragment.KEY_USE_SERVICE_PREFERENCE, /* enabled= */ true);
assertThat(getLatestPopupWindow()).isNull(); assertThat(getLatestPopupWindow()).isNull();
} }
@@ -268,12 +265,10 @@ public class ToggleFeaturePreferenceFragmentTest {
@DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT) @DisableFlags(android.view.accessibility.Flags.FLAG_A11Y_QS_SHORTCUT)
@Config(shadows = ShadowFragment.class) @Config(shadows = ShadowFragment.class)
public void onPreferenceToggledOnEnabledService_tooltipViewShown_notShowTooltipView() { public void onPreferenceToggledOnEnabledService_tooltipViewShown_notShowTooltipView() {
mFragment.onPreferenceToggled( mFragment.onPreferenceToggled(mFragment.getUseServicePreferenceKey(), /* enabled= */ true);
ToggleFeaturePreferenceFragment.KEY_USE_SERVICE_PREFERENCE, /* enabled= */ true);
getLatestPopupWindow().dismiss(); getLatestPopupWindow().dismiss();
mFragment.onPreferenceToggled( mFragment.onPreferenceToggled(mFragment.getUseServicePreferenceKey(), /* enabled= */ true);
ToggleFeaturePreferenceFragment.KEY_USE_SERVICE_PREFERENCE, /* enabled= */ true);
assertThat(getLatestPopupWindow().isShowing()).isFalse(); assertThat(getLatestPopupWindow().isShowing()).isFalse();
} }