Extract UserShortcutType functions from ToggleFeaturePreferenceFragment
* Simplify StringJoiner to '+' since it only have one case
* Simplify Collections usage to use removeIf() and findFirst() directly
* Change class name SharedPreferenceUtils to PreferredShortcuts
Bug: 158540780
Test: atest ToggleFeaturePreferenceFragmentTest
Test: atest PreferredShortcutsTest
Change-Id: I02f03ccba09b6d7edaa5c0c8223ab3561a5e976b
diff --git a/src/com/android/settings/accessibility/PreferredShortcuts.java b/src/com/android/settings/accessibility/PreferredShortcuts.java
new file mode 100644
index 0000000..2c9840d
--- /dev/null
+++ b/src/com/android/settings/accessibility/PreferredShortcuts.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2020 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.ComponentName;
+import android.content.Context;
+import android.content.SharedPreferences;
+
+import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/** Static utility methods relating to {@link PreferredShortcut} */
+public final class PreferredShortcuts {
+
+ private static final String ACCESSIBILITY_PERF = "accessibility_prefs";
+ private static final String USER_SHORTCUT_TYPE = "user_shortcut_type";
+
+ /**
+ * Retrieves {@link UserShortcutType} for the given {@code componentName} from
+ * SharedPreferences.
+ *
+ * @param context {@link Context} to access the {@link SharedPreferences}
+ * @param componentName Name of the service or activity, should be the format of {@link
+ * ComponentName#flattenToString()}.
+ * @param defaultType See {@link UserShortcutType}
+ * @return {@link UserShortcutType}
+ */
+ public static int retrieveUserShortcutType(Context context, String componentName,
+ int defaultType) {
+ if (componentName == null) {
+ return defaultType;
+ }
+
+ // Create a mutable set to modify
+ final Set<String> info = new HashSet<>(getFromSharedPreferences(context));
+ info.removeIf(str -> !str.contains(componentName));
+
+ if (info.isEmpty()) {
+ return defaultType;
+ }
+
+ final String str = info.stream().findFirst().get();
+ final PreferredShortcut shortcut = PreferredShortcut.fromString(str);
+ return shortcut.getType();
+ }
+
+ /**
+ * Saves a {@link PreferredShortcut} which containing {@link ComponentName#flattenToString()}
+ * and {@link UserShortcutType} in SharedPreferences.
+ *
+ * @param context {@link Context} to access the {@link SharedPreferences}
+ * @param shortcut Contains {@link ComponentName#flattenToString()} and {@link UserShortcutType}
+ */
+ public static void saveUserShortcutType(Context context, PreferredShortcut shortcut) {
+ final String componentName = shortcut.getComponentName();
+ if (componentName == null) {
+ return;
+ }
+
+ // Create a mutable set to modify
+ final Set<String> info = new HashSet<>(getFromSharedPreferences(context));
+ info.removeIf(str -> str.contains(componentName));
+ info.add(shortcut.toString());
+ saveToSharedPreferences(context, info);
+ }
+
+ /**
+ * Returns a immutable set of {@link PreferredShortcut#toString()} list from
+ * SharedPreferences.
+ */
+ private static Set<String> getFromSharedPreferences(Context context) {
+ return getSharedPreferences(context).getStringSet(USER_SHORTCUT_TYPE, Set.of());
+ }
+
+ /** Sets a set of {@link PreferredShortcut#toString()} list into SharedPreferences. */
+ private static void saveToSharedPreferences(Context context, Set<String> data) {
+ SharedPreferences.Editor editor = getSharedPreferences(context).edit();
+ editor.putStringSet(USER_SHORTCUT_TYPE, data).apply();
+ }
+
+ private static SharedPreferences getSharedPreferences(Context context) {
+ return context.getSharedPreferences(ACCESSIBILITY_PERF, Context.MODE_PRIVATE);
+ }
+
+ private PreferredShortcuts() {}
+}
diff --git a/src/com/android/settings/accessibility/SharedPreferenceUtils.java b/src/com/android/settings/accessibility/SharedPreferenceUtils.java
deleted file mode 100644
index e997291..0000000
--- a/src/com/android/settings/accessibility/SharedPreferenceUtils.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (C) 2020 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 android.content.SharedPreferences;
-
-import com.google.common.collect.ImmutableSet;
-
-import java.util.Set;
-
-/** Utility class for SharedPreferences. */
-public final class SharedPreferenceUtils {
-
- private static final String ACCESSIBILITY_PERF = "accessibility_prefs";
- private static final String USER_SHORTCUT_TYPE = "user_shortcut_type";
- private SharedPreferenceUtils() { }
-
- private static SharedPreferences getSharedPreferences(Context context, String fileName) {
- return context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
- }
-
- /** Returns a set of user shortcuts list to determine user preferred service shortcut. */
- public static Set<String> getUserShortcutTypes(Context context) {
- return getSharedPreferences(context, ACCESSIBILITY_PERF)
- .getStringSet(USER_SHORTCUT_TYPE, ImmutableSet.of());
- }
-
- /** Sets a set of user shortcuts list to determine user preferred service shortcut. */
- public static void setUserShortcutType(Context context, Set<String> data) {
- SharedPreferences.Editor editor = getSharedPreferences(context, ACCESSIBILITY_PERF).edit();
- editor.remove(USER_SHORTCUT_TYPE).apply();
- editor.putStringSet(USER_SHORTCUT_TYPE, data).apply();
- }
-}
diff --git a/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
index 30946d3..c0de313 100644
--- a/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
@@ -17,6 +17,7 @@
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityStatsLogUtils.logAccessibilityServiceEnabled;
+import static com.android.settings.accessibility.PreferredShortcuts.retrieveUserShortcutType;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Activity;
@@ -292,7 +293,8 @@
@Override
public void onToggleClicked(ShortcutPreference preference) {
- final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
+ final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
+ mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
if (preference.isChecked()) {
if (!mToggleServiceDividerSwitchPreference.isChecked()) {
preference.setChecked(false);
@@ -313,7 +315,8 @@
public void onSettingsClicked(ShortcutPreference preference) {
// Do not restore shortcut in shortcut chooser dialog when shortcutPreference is turned off.
mUserShortcutTypesCache = mShortcutPreference.isChecked()
- ? getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE)
+ ? retrieveUserShortcutType(getPrefContext(), mComponentName.flattenToString(),
+ UserShortcutType.SOFTWARE)
: UserShortcutType.EMPTY;
final boolean isServiceOnOrShortcutAdded = mShortcutPreference.isChecked()
@@ -415,7 +418,8 @@
private void onAllowButtonFromShortcutToggleClicked() {
mShortcutPreference.setChecked(true);
- final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
+ final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
+ mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes, mComponentName);
mIsDialogShown.set(false);
diff --git a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
index 0f16136..678ef13 100644
--- a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
@@ -18,6 +18,8 @@
import static com.android.settings.accessibility.AccessibilityUtil.getScreenHeightPixels;
import static com.android.settings.accessibility.AccessibilityUtil.getScreenWidthPixels;
+import static com.android.settings.accessibility.PreferredShortcuts.retrieveUserShortcutType;
+import static com.android.settings.accessibility.PreferredShortcuts.saveUserShortcutType;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
@@ -44,6 +46,7 @@
import android.widget.CheckBox;
import android.widget.ImageView;
+import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
@@ -57,16 +60,11 @@
import com.android.settingslib.accessibility.AccessibilityUtils;
import com.android.settingslib.widget.FooterPreference;
-import com.google.common.annotations.VisibleForTesting;
-
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
-import java.util.HashSet;
import java.util.List;
import java.util.Locale;
-import java.util.Set;
-import java.util.stream.Collectors;
/**
* Base class for accessibility fragments with toggle, shortcut, some helper functions
@@ -525,32 +523,14 @@
if (saveChanges) {
final boolean isChanged = (mUserShortcutTypesCache != UserShortcutType.EMPTY);
if (isChanged) {
- setUserShortcutType(getPrefContext(), mUserShortcutTypesCache);
+ final PreferredShortcut shortcut = new PreferredShortcut(
+ mComponentName.flattenToString(), mUserShortcutTypesCache);
+ saveUserShortcutType(getPrefContext(), shortcut);
}
mUserShortcutTypes = mUserShortcutTypesCache;
}
}
- private void setUserShortcutType(Context context, int type) {
- if (mComponentName == null) {
- return;
- }
-
- Set<String> info = SharedPreferenceUtils.getUserShortcutTypes(context);
- final String componentName = mComponentName.flattenToString();
- if (info.isEmpty()) {
- info = new HashSet<>();
- } else {
- final Set<String> filtered = info.stream()
- .filter(str -> str.contains(componentName))
- .collect(Collectors.toSet());
- info.removeAll(filtered);
- }
- final PreferredShortcut shortcut = new PreferredShortcut(componentName, type);
- info.add(shortcut.toString());
- SharedPreferenceUtils.setUserShortcutType(context, info);
- }
-
protected CharSequence getShortcutTypeSummary(Context context) {
if (!mShortcutPreference.isSettingsEditable()) {
return context.getText(R.string.accessibility_shortcut_edit_dialog_title_hardware);
@@ -560,7 +540,8 @@
return context.getText(R.string.switch_off_text);
}
- final int shortcutTypes = getUserShortcutTypes(context, UserShortcutType.SOFTWARE);
+ final int shortcutTypes = retrieveUserShortcutType(context,
+ mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
int resId = R.string.accessibility_shortcut_edit_summary_software;
if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
resId = AccessibilityUtil.isTouchExploreEnabled(context)
@@ -589,25 +570,6 @@
null, joinStrings);
}
- protected int getUserShortcutTypes(Context context, @UserShortcutType int defaultValue) {
- if (mComponentName == null) {
- return defaultValue;
- }
-
- final Set<String> info = SharedPreferenceUtils.getUserShortcutTypes(context);
- final String componentName = mComponentName.flattenToString();
- final Set<String> filtered = info.stream()
- .filter(str -> str.contains(componentName))
- .collect(Collectors.toSet());
- if (filtered.isEmpty()) {
- return defaultValue;
- }
-
- final String str = (String) filtered.toArray()[0];
- final PreferredShortcut shortcut = PreferredShortcut.fromString(str);
- return shortcut.getType();
- }
-
/**
* This method will be invoked when a button in the edit shortcut dialog is clicked.
*
@@ -638,10 +600,13 @@
mUserShortcutTypes = AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(),
mComponentName);
if (mUserShortcutTypes != UserShortcutType.EMPTY) {
- setUserShortcutType(getPrefContext(), mUserShortcutTypes);
+ final PreferredShortcut shortcut = new PreferredShortcut(
+ mComponentName.flattenToString(), mUserShortcutTypes);
+ saveUserShortcutType(getPrefContext(), shortcut);
} else {
// Get the user shortcut type from shared_prefs if cannot get from settings provider.
- mUserShortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
+ mUserShortcutTypes = retrieveUserShortcutType(getPrefContext(),
+ mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
}
}
@@ -664,10 +629,11 @@
return;
}
- final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
+ final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
+ mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
mShortcutPreference.setChecked(
- AccessibilityUtil.hasValuesInSettings(getPrefContext(), shortcutTypes,
- mComponentName));
+ AccessibilityUtil.hasValuesInSettings(getPrefContext(), shortcutTypes,
+ mComponentName));
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
}
@@ -681,7 +647,8 @@
return;
}
- final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
+ final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
+ mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
if (preference.isChecked()) {
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes,
mComponentName);
@@ -697,7 +664,8 @@
public void onSettingsClicked(ShortcutPreference preference) {
// Do not restore shortcut in shortcut chooser dialog when shortcutPreference is turned off.
mUserShortcutTypesCache = mShortcutPreference.isChecked()
- ? getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE)
+ ? retrieveUserShortcutType(getPrefContext(),
+ mComponentName.flattenToString(), UserShortcutType.SOFTWARE)
: UserShortcutType.EMPTY;
showDialog(DialogEnums.EDIT_SHORTCUT);
}
diff --git a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
index a5a20e1..d470bef 100644
--- a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
@@ -19,6 +19,8 @@
import static com.android.internal.accessibility.AccessibilityShortcutController.MAGNIFICATION_CONTROLLER_NAME;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
+import static com.android.settings.accessibility.PreferredShortcuts.retrieveUserShortcutType;
+import static com.android.settings.accessibility.PreferredShortcuts.saveUserShortcutType;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
@@ -46,12 +48,9 @@
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
import java.util.ArrayList;
-import java.util.HashSet;
import java.util.List;
import java.util.Locale;
-import java.util.Set;
import java.util.StringJoiner;
-import java.util.stream.Collectors;
/**
* Fragment that shows the actual UI for providing basic magnification accessibility service setup
@@ -210,35 +209,22 @@
if (saveChanges) {
final boolean isChanged = (mUserShortcutTypesCache != UserShortcutType.EMPTY);
if (isChanged) {
- setUserShortcutType(getPrefContext(), mUserShortcutTypesCache);
+ final PreferredShortcut shortcut = new PreferredShortcut(
+ MAGNIFICATION_CONTROLLER_NAME, mUserShortcutTypesCache);
+ saveUserShortcutType(getPrefContext(), shortcut);
}
mUserShortcutTypes = mUserShortcutTypesCache;
}
}
- private void setUserShortcutType(Context context, int type) {
- Set<String> info = SharedPreferenceUtils.getUserShortcutTypes(context);
- if (info.isEmpty()) {
- info = new HashSet<>();
- } else {
- final Set<String> filtered = info.stream().filter(
- str -> str.contains(MAGNIFICATION_CONTROLLER_NAME)).collect(
- Collectors.toSet());
- info.removeAll(filtered);
- }
- final PreferredShortcut shortcut = new PreferredShortcut(MAGNIFICATION_CONTROLLER_NAME,
- type);
- info.add(shortcut.toString());
- SharedPreferenceUtils.setUserShortcutType(context, info);
- }
-
@Override
protected CharSequence getShortcutTypeSummary(Context context) {
if (!mShortcutPreference.isChecked()) {
return context.getText(R.string.switch_off_text);
}
- final int shortcutType = getUserShortcutTypes(context, UserShortcutType.EMPTY);
+ final int shortcutType = retrieveUserShortcutType(context, MAGNIFICATION_CONTROLLER_NAME,
+ UserShortcutType.EMPTY);
int resId = R.string.accessibility_shortcut_edit_summary_software;
if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
resId = AccessibilityUtil.isTouchExploreEnabled(context)
@@ -274,21 +260,6 @@
}
@Override
- protected int getUserShortcutTypes(Context context, @UserShortcutType int defaultValue) {
- final Set<String> info = SharedPreferenceUtils.getUserShortcutTypes(context);
- final Set<String> filtered = info.stream().filter(
- str -> str.contains(MAGNIFICATION_CONTROLLER_NAME)).collect(
- Collectors.toSet());
- if (filtered.isEmpty()) {
- return defaultValue;
- }
-
- final String str = (String) filtered.toArray()[0];
- final PreferredShortcut shortcut = PreferredShortcut.fromString(str);
- return shortcut.getType();
- }
-
- @Override
protected void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
updateUserShortcutType(/* saveChanges= */ true);
optInAllMagnificationValuesToSettings(getPrefContext(), mUserShortcutTypes);
@@ -341,7 +312,8 @@
@Override
public void onToggleClicked(ShortcutPreference preference) {
- final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
+ final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
+ MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
if (preference.isChecked()) {
optInAllMagnificationValuesToSettings(getPrefContext(), shortcutTypes);
showDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
@@ -355,7 +327,8 @@
public void onSettingsClicked(ShortcutPreference preference) {
// Do not restore shortcut in shortcut chooser dialog when shortcutPreference is turned off.
mUserShortcutTypesCache = mShortcutPreference.isChecked()
- ? getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE)
+ ? retrieveUserShortcutType(getPrefContext(),
+ MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE)
: UserShortcutType.EMPTY;
showDialog(DialogEnums.MAGNIFICATION_EDIT_SHORTCUT);
}
@@ -365,10 +338,13 @@
// Get the user shortcut type from settings provider.
mUserShortcutTypes = getUserShortcutTypeFromSettings(getPrefContext());
if (mUserShortcutTypes != UserShortcutType.EMPTY) {
- setUserShortcutType(getPrefContext(), mUserShortcutTypes);
+ final PreferredShortcut shortcut = new PreferredShortcut(
+ MAGNIFICATION_CONTROLLER_NAME, mUserShortcutTypes);
+ saveUserShortcutType(getPrefContext(), shortcut);
} else {
// Get the user shortcut type from shared_prefs if cannot get from settings provider.
- mUserShortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
+ mUserShortcutTypes = retrieveUserShortcutType(getPrefContext(),
+ MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
}
}
@@ -389,7 +365,8 @@
@Override
protected void updateShortcutPreference() {
- final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
+ final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
+ MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
mShortcutPreference.setChecked(
hasMagnificationValuesInSettings(getPrefContext(), shortcutTypes));
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
diff --git a/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java
index 0f04b3b..eccfbce 100644
--- a/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java
@@ -24,8 +24,6 @@
import com.android.settings.R;
-import com.google.common.collect.ImmutableSet;
-
/**
* Fragment that only allowed hardware {@link UserShortcutType} for shortcut to open.
*
@@ -70,7 +68,6 @@
final String componentNameString = mComponentName.flattenToString();
final PreferredShortcut shortcut = new PreferredShortcut(componentNameString, type);
- SharedPreferenceUtils.setUserShortcutType(getPrefContext(),
- ImmutableSet.of(shortcut.toString()));
+ PreferredShortcuts.saveUserShortcutType(getPrefContext(), shortcut);
}
}
diff --git a/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutsTest.java b/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutsTest.java
new file mode 100644
index 0000000..a694aa3
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutsTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2020 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.google.common.truth.Truth.assertThat;
+
+import android.content.ComponentName;
+import android.content.Context;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+/** Tests for {@link PreferredShortcuts} */
+@RunWith(RobolectricTestRunner.class)
+public class PreferredShortcutsTest {
+
+ private static final String PACKAGE_NAME_1 = "com.test1.example";
+ private static final String CLASS_NAME_1 = PACKAGE_NAME_1 + ".test1";
+ private static final ComponentName COMPONENT_NAME_1 = new ComponentName(PACKAGE_NAME_1,
+ CLASS_NAME_1);
+ private static final String PACKAGE_NAME_2 = "com.test2.example";
+ private static final String CLASS_NAME_2 = PACKAGE_NAME_2 + ".test2";
+ private static final ComponentName COMPONENT_NAME_2 = new ComponentName(PACKAGE_NAME_2,
+ CLASS_NAME_2);
+
+ private Context mContext = ApplicationProvider.getApplicationContext();
+
+ @Test
+ public void retrieveUserShortcutType_fromSingleData_matchSavedType() {
+ final int type = 1;
+ final PreferredShortcut shortcut = new PreferredShortcut(COMPONENT_NAME_1.flattenToString(),
+ type);
+
+ PreferredShortcuts.saveUserShortcutType(mContext, shortcut);
+ final int retrieveType = PreferredShortcuts.retrieveUserShortcutType(mContext,
+ COMPONENT_NAME_1.flattenToString(), 0);
+
+ assertThat(retrieveType).isEqualTo(type);
+ }
+
+ @Test
+ public void retrieveUserShortcutType_fromMultiData_matchSavedType() {
+ final int type1 = 1;
+ final int type2 = 2;
+ final PreferredShortcut shortcut1 = new PreferredShortcut(
+ COMPONENT_NAME_1.flattenToString(), type1);
+ final PreferredShortcut shortcut2 = new PreferredShortcut(
+ COMPONENT_NAME_2.flattenToString(), type2);
+
+ PreferredShortcuts.saveUserShortcutType(mContext, shortcut1);
+ PreferredShortcuts.saveUserShortcutType(mContext, shortcut2);
+ final int retrieveType = PreferredShortcuts.retrieveUserShortcutType(mContext,
+ COMPONENT_NAME_1.flattenToString(), 0);
+
+ assertThat(retrieveType).isEqualTo(type1);
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java
index f7c9438..9a157fe 100644
--- a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java
@@ -54,10 +54,6 @@
import org.robolectric.annotation.Config;
import org.robolectric.shadows.androidx.fragment.FragmentController;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-
/** Tests for {@link ToggleFeaturePreferenceFragment} */
@RunWith(RobolectricTestRunner.class)
public class ToggleFeaturePreferenceFragmentTest {
@@ -159,9 +155,7 @@
private void putUserShortcutTypeIntoSharedPreference(Context context,
PreferredShortcut shortcut) {
- Set<String> value = new HashSet<>(Collections.singletonList(shortcut.toString()));
-
- SharedPreferenceUtils.setUserShortcutType(context, value);
+ PreferredShortcuts.saveUserShortcutType(context, shortcut);
}
private void callEmptyOnClicked(DialogInterface dialog, int which) {}
diff --git a/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java
index 324ee59..5c0cb6d 100644
--- a/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java
@@ -54,10 +54,6 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-
@RunWith(RobolectricTestRunner.class)
public class ToggleScreenMagnificationPreferenceFragmentTest {
@@ -217,9 +213,7 @@
private void putUserShortcutTypeIntoSharedPreference(Context context,
PreferredShortcut shortcut) {
- Set<String> value = new HashSet<>(Collections.singletonList(shortcut.toString()));
-
- SharedPreferenceUtils.setUserShortcutType(context, value);
+ PreferredShortcuts.saveUserShortcutType(context, shortcut);
}
private void setMagnificationTripleTapEnabled(boolean enabled) {