diff --git a/src/com/android/settings/accessibility/PreferredShortcut.java b/src/com/android/settings/accessibility/PreferredShortcut.java new file mode 100644 index 00000000000..1654992c8b3 --- /dev/null +++ b/src/com/android/settings/accessibility/PreferredShortcut.java @@ -0,0 +1,96 @@ +/* + * 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.text.TextUtils; + +import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType; + +import com.google.common.base.Objects; + +/** + * A data class for containing {@link ComponentName#flattenToString()} and + * {@link UserShortcutType}. Represents the preferred shortcuts of the service or activity. + */ +public class PreferredShortcut { + + private static final char COMPONENT_NAME_SEPARATOR = ':'; + private static final TextUtils.SimpleStringSplitter sStringColonSplitter = + new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); + + /** + * Creates a {@link PreferredShortcut} from a encoded string described in {@link #toString()}. + * + * @param preferredShortcutString A string conform to the format described in {@link + * #toString()} + * @return A {@link PreferredShortcut} with the specified value + * @throws IllegalArgumentException If preferredShortcutString does not conform to the format + * described in {@link #toString()} + */ + public static PreferredShortcut fromString(String preferredShortcutString) { + sStringColonSplitter.setString(preferredShortcutString); + if (sStringColonSplitter.hasNext()) { + final String componentName = sStringColonSplitter.next(); + final int type = Integer.parseInt(sStringColonSplitter.next()); + return new PreferredShortcut(componentName, type); + } + + throw new IllegalArgumentException( + "Invalid PreferredShortcut string: " + preferredShortcutString); + } + + /** The format of {@link ComponentName#flattenToString()} */ + private String mComponentName; + /** The format of {@link UserShortcutType} */ + private int mType; + + public PreferredShortcut(String componentName, int type) { + mComponentName = componentName; + mType = type; + } + + public String getComponentName() { + return mComponentName; + } + + public int getType() { + return mType; + } + + @Override + public String toString() { + return mComponentName + COMPONENT_NAME_SEPARATOR + mType; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PreferredShortcut that = (PreferredShortcut) o; + return mType == that.mType && Objects.equal(mComponentName, that.mComponentName); + } + + @Override + public int hashCode() { + return Objects.hashCode(mComponentName, mType); + } +} diff --git a/src/com/android/settings/accessibility/PreferredShortcuts.java b/src/com/android/settings/accessibility/PreferredShortcuts.java new file mode 100644 index 00000000000..2c9840d09ec --- /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 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 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 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 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 e9972918054..00000000000 --- 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 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 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 30946d30afb..c0de3131646 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 @@ public class ToggleAccessibilityServicePreferenceFragment extends @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 class ToggleAccessibilityServicePreferenceFragment extends 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 @@ public class ToggleAccessibilityServicePreferenceFragment extends 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 88f58be066c..678ef130e21 100644 --- a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java +++ b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java @@ -18,6 +18,8 @@ package com.android.settings.accessibility; 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.view.accessibility.AccessibilityManager.TouchExplorationStateChan 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,17 +60,11 @@ import com.android.settings.widget.SwitchBar; 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.StringJoiner; -import java.util.stream.Collectors; /** * Base class for accessibility fragments with toggle, shortcut, some helper functions @@ -481,51 +478,6 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference } } - static final class AccessibilityUserShortcutType { - private static final char COMPONENT_NAME_SEPARATOR = ':'; - private static final TextUtils.SimpleStringSplitter sStringColonSplitter = - new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); - - private String mComponentName; - private int mType; - - AccessibilityUserShortcutType(String componentName, int type) { - this.mComponentName = componentName; - this.mType = type; - } - - AccessibilityUserShortcutType(String flattenedString) { - sStringColonSplitter.setString(flattenedString); - if (sStringColonSplitter.hasNext()) { - this.mComponentName = sStringColonSplitter.next(); - this.mType = Integer.parseInt(sStringColonSplitter.next()); - } - } - - String getComponentName() { - return mComponentName; - } - - void setComponentName(String componentName) { - this.mComponentName = componentName; - } - - int getType() { - return mType; - } - - void setType(int type) { - this.mType = type; - } - - String flattenToString() { - final StringJoiner joiner = new StringJoiner(String.valueOf(COMPONENT_NAME_SEPARATOR)); - joiner.add(mComponentName); - joiner.add(String.valueOf(mType)); - return joiner.toString(); - } - } - private void setDialogTextAreaClickListener(View dialogView, CheckBox checkBox) { final View dialogTextArea = dialogView.findViewById(R.id.container); dialogTextArea.setOnClickListener(v -> { @@ -571,33 +523,14 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference 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 info = SharedPreferenceUtils.getUserShortcutTypes(context); - final String componentName = mComponentName.flattenToString(); - if (info.isEmpty()) { - info = new HashSet<>(); - } else { - final Set filtered = info.stream() - .filter(str -> str.contains(componentName)) - .collect(Collectors.toSet()); - info.removeAll(filtered); - } - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - componentName, type); - info.add(shortcut.flattenToString()); - SharedPreferenceUtils.setUserShortcutType(context, info); - } - protected CharSequence getShortcutTypeSummary(Context context) { if (!mShortcutPreference.isSettingsEditable()) { return context.getText(R.string.accessibility_shortcut_edit_dialog_title_hardware); @@ -607,7 +540,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference 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) @@ -636,25 +570,6 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference null, joinStrings); } - protected int getUserShortcutTypes(Context context, @UserShortcutType int defaultValue) { - if (mComponentName == null) { - return defaultValue; - } - - final Set info = SharedPreferenceUtils.getUserShortcutTypes(context); - final String componentName = mComponentName.flattenToString(); - final Set filtered = info.stream() - .filter(str -> str.contains(componentName)) - .collect(Collectors.toSet()); - if (filtered.isEmpty()) { - return defaultValue; - } - - final String str = (String) filtered.toArray()[0]; - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str); - return shortcut.getType(); - } - /** * This method will be invoked when a button in the edit shortcut dialog is clicked. * @@ -685,10 +600,13 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference 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); } } @@ -711,10 +629,11 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference 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())); } @@ -728,7 +647,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference 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); @@ -744,7 +664,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference 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 108c1c0405e..d470bef9524 100644 --- a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java +++ b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java @@ -19,6 +19,8 @@ package com.android.settings.accessibility; 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.R; 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 @@ public class ToggleScreenMagnificationPreferenceFragment extends 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 info = SharedPreferenceUtils.getUserShortcutTypes(context); - if (info.isEmpty()) { - info = new HashSet<>(); - } else { - final Set filtered = info.stream().filter( - str -> str.contains(MAGNIFICATION_CONTROLLER_NAME)).collect( - Collectors.toSet()); - info.removeAll(filtered); - } - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - MAGNIFICATION_CONTROLLER_NAME, type); - info.add(shortcut.flattenToString()); - 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) @@ -273,21 +259,6 @@ public class ToggleScreenMagnificationPreferenceFragment extends null, joinStrings); } - @Override - protected int getUserShortcutTypes(Context context, @UserShortcutType int defaultValue) { - final Set info = SharedPreferenceUtils.getUserShortcutTypes(context); - final Set 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 AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str); - return shortcut.getType(); - } - @Override protected void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) { updateUserShortcutType(/* saveChanges= */ true); @@ -341,7 +312,8 @@ public class ToggleScreenMagnificationPreferenceFragment extends @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 class ToggleScreenMagnificationPreferenceFragment extends 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 @@ public class ToggleScreenMagnificationPreferenceFragment extends // 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 @@ public class ToggleScreenMagnificationPreferenceFragment extends @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 31f97a8f600..eccfbce7813 100644 --- a/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java +++ b/src/com/android/settings/accessibility/VolumeShortcutToggleAccessibilityServicePreferenceFragment.java @@ -24,8 +24,6 @@ import android.view.View; import com.android.settings.R; -import com.google.common.collect.ImmutableSet; - /** * Fragment that only allowed hardware {@link UserShortcutType} for shortcut to open. * @@ -67,10 +65,9 @@ public class VolumeShortcutToggleAccessibilityServicePreferenceFragment extends } private void setAllowedPreferredShortcutType(int type) { - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - mComponentName.flattenToString(), type); + final String componentNameString = mComponentName.flattenToString(); + final PreferredShortcut shortcut = new PreferredShortcut(componentNameString, type); - SharedPreferenceUtils.setUserShortcutType(getPrefContext(), - ImmutableSet.of(shortcut.flattenToString())); + PreferredShortcuts.saveUserShortcutType(getPrefContext(), shortcut); } } diff --git a/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutTest.java b/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutTest.java new file mode 100644 index 00000000000..e7d09963ed4 --- /dev/null +++ b/tests/robotests/src/com/android/settings/accessibility/PreferredShortcutTest.java @@ -0,0 +1,63 @@ +/* + * 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 androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Tests for {@link PreferredShortcut} */ +@RunWith(AndroidJUnit4.class) +public class PreferredShortcutTest { + + private static final String STUB_COMPONENT_NAME = new ComponentName("com.example", + "com.example.testActivity").flattenToString(); + private static final int STUB_TYPE = 3; + + @Test + public void fromString_matchMemberObject() { + final String preferredShortcutString = STUB_COMPONENT_NAME + ":" + STUB_TYPE; + + final PreferredShortcut shortcut = PreferredShortcut.fromString(preferredShortcutString); + + assertThat(shortcut.getComponentName()).isEqualTo(STUB_COMPONENT_NAME); + assertThat(shortcut.getType()).isEqualTo(STUB_TYPE); + } + + @Test + public void toString_matchString() { + final PreferredShortcut shortcut = new PreferredShortcut(STUB_COMPONENT_NAME, STUB_TYPE); + + final String preferredShortcutString = shortcut.toString(); + + assertThat(preferredShortcutString).isEqualTo(STUB_COMPONENT_NAME + ":" + STUB_TYPE); + } + + @Test + public void assertSameObject() { + final String preferredShortcutString = STUB_COMPONENT_NAME + ":" + STUB_TYPE; + final PreferredShortcut targetShortcut = PreferredShortcut.fromString( + preferredShortcutString); + + assertThat(targetShortcut).isEqualTo(new PreferredShortcut(STUB_COMPONENT_NAME, STUB_TYPE)); + } +} 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 00000000000..a694aa3f289 --- /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 f008b03a752..9a157fe1357 100644 --- a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java @@ -43,7 +43,6 @@ import androidx.test.core.app.ApplicationProvider; import com.android.settings.R; import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType; -import com.android.settings.accessibility.ToggleFeaturePreferenceFragment.AccessibilityUserShortcutType; import com.android.settings.testutils.shadow.ShadowFragment; import org.junit.Before; @@ -55,21 +54,10 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.shadows.androidx.fragment.FragmentController; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Collectors; - /** Tests for {@link ToggleFeaturePreferenceFragment} */ @RunWith(RobolectricTestRunner.class) public class ToggleFeaturePreferenceFragmentTest { - private static final String TEST_SERVICE_KEY_1 = "abc:111"; - private static final String TEST_SERVICE_KEY_2 = "mno:222"; - private static final String TEST_SERVICE_KEY_3 = "xyz:333"; - private static final String TEST_SERVICE_NAME_1 = "abc"; - private static final int TEST_SERVICE_VALUE_1 = 111; - private static final String PLACEHOLDER_PACKAGE_NAME = "com.placeholder.example"; private static final String PLACEHOLDER_CLASS_NAME = PLACEHOLDER_PACKAGE_NAME + ".placeholder"; private static final ComponentName PLACEHOLDER_COMPONENT_NAME = new ComponentName( @@ -97,59 +85,6 @@ public class ToggleFeaturePreferenceFragmentTest { doReturn(null).when(mFragment).getPreferenceScreen(); } - @Test - public void a11yUserShortcutType_setConcatString_shouldReturnTargetValue() { - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - TEST_SERVICE_KEY_1); - - assertThat(shortcut.getComponentName()).isEqualTo(TEST_SERVICE_NAME_1); - assertThat(shortcut.getType()).isEqualTo(TEST_SERVICE_VALUE_1); - } - - @Test - public void a11yUserShortcutType_shouldUpdateConcatString() { - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( - TEST_SERVICE_KEY_2); - - shortcut.setComponentName(TEST_SERVICE_NAME_1); - shortcut.setType(TEST_SERVICE_VALUE_1); - - assertThat(shortcut.flattenToString()).isEqualTo(TEST_SERVICE_KEY_1); - } - - @Test - public void stringSet_convertA11yPreferredShortcut_shouldRemoveTarget() { - Set mySet = new HashSet<>(); - mySet.add(TEST_SERVICE_KEY_1); - mySet.add(TEST_SERVICE_KEY_2); - mySet.add(TEST_SERVICE_KEY_3); - - final Set filtered = mySet.stream() - .filter(str -> str.contains(TEST_SERVICE_NAME_1)) - .collect(Collectors.toSet()); - mySet.removeAll(filtered); - - assertThat(mySet).doesNotContain(TEST_SERVICE_KEY_1); - assertThat(mySet).hasSize(/* expectedSize= */2); - } - - @Test - public void stringSet_convertA11yUserShortcutType_shouldReturnPreferredShortcut() { - Set mySet = new HashSet<>(); - mySet.add(TEST_SERVICE_KEY_1); - mySet.add(TEST_SERVICE_KEY_2); - mySet.add(TEST_SERVICE_KEY_3); - - final Set filtered = mySet.stream() - .filter(str -> str.contains(TEST_SERVICE_NAME_1)) - .collect(Collectors.toSet()); - - final String str = (String) filtered.toArray()[0]; - final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str); - final int type = shortcut.getType(); - assertThat(type).isEqualTo(TEST_SERVICE_VALUE_1); - } - @Test public void createFragment_shouldOnlyAddPreferencesOnce() { FragmentController.setupFragment(mFragment, FragmentActivity.class, @@ -184,7 +119,7 @@ public class ToggleFeaturePreferenceFragmentTest { @Test public void updateShortcutPreferenceData_hasValueInSharedPreference_assignToVariable() { mFragment.mComponentName = PLACEHOLDER_COMPONENT_NAME; - final AccessibilityUserShortcutType hardwareShortcut = new AccessibilityUserShortcutType( + final PreferredShortcut hardwareShortcut = new PreferredShortcut( PLACEHOLDER_COMPONENT_NAME.flattenToString(), UserShortcutType.HARDWARE); putUserShortcutTypeIntoSharedPreference(mContext, hardwareShortcut); @@ -219,10 +154,8 @@ public class ToggleFeaturePreferenceFragmentTest { } private void putUserShortcutTypeIntoSharedPreference(Context context, - AccessibilityUserShortcutType shortcut) { - Set value = new HashSet<>(Collections.singletonList(shortcut.flattenToString())); - - SharedPreferenceUtils.setUserShortcutType(context, value); + PreferredShortcut shortcut) { + 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 8bc3009d2fb..5c0cb6d9256 100644 --- a/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentTest.java @@ -44,7 +44,6 @@ import androidx.preference.PreferenceManager; import androidx.test.core.app.ApplicationProvider; import com.android.settings.R; -import com.android.settings.accessibility.ToggleFeaturePreferenceFragment.AccessibilityUserShortcutType; import com.android.settings.testutils.shadow.ShadowFragment; import org.junit.Before; @@ -55,10 +54,6 @@ import org.mockito.MockitoAnnotations; 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 { @@ -183,7 +178,7 @@ public class ToggleScreenMagnificationPreferenceFragmentTest { @Test public void updateShortcutPreferenceData_hasValueInSharedPreference_assignToVariable() { - final AccessibilityUserShortcutType tripleTapShortcut = new AccessibilityUserShortcutType( + final PreferredShortcut tripleTapShortcut = new PreferredShortcut( MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.TRIPLETAP); putUserShortcutTypeIntoSharedPreference(mContext, tripleTapShortcut); @@ -217,10 +212,8 @@ public class ToggleScreenMagnificationPreferenceFragmentTest { } private void putUserShortcutTypeIntoSharedPreference(Context context, - AccessibilityUserShortcutType shortcut) { - Set value = new HashSet<>(Collections.singletonList(shortcut.flattenToString())); - - SharedPreferenceUtils.setUserShortcutType(context, value); + PreferredShortcut shortcut) { + PreferredShortcuts.saveUserShortcutType(context, shortcut); } private void setMagnificationTripleTapEnabled(boolean enabled) {