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
This commit is contained in:
jasonwshsu
2020-07-19 23:46:54 +08:00
committed by Jason Hsu
parent 26f1b5f65c
commit 48b546eccf
9 changed files with 225 additions and 164 deletions

View File

@@ -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<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)
@@ -273,21 +259,6 @@ public class ToggleScreenMagnificationPreferenceFragment extends
null, joinStrings);
}
@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);
@@ -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()));