Support accessibility shortcut secondary action (7/n)

Implements sharedPreference to keep the user preferred shortcut for each
service.

Bug: 142530063
Bug: 142531156
Test: make RunSettingsRoboTests ROBOTEST_FILTER=ToggleFeaturePreferenceFragmentTest
Test: make RunSettingsRoboTests2
Change-Id: I2a7c54ddc32764f6d88f2efed87982d469acc183
This commit is contained in:
menghanli
2020-01-09 14:27:47 +08:00
parent b642f3cb10
commit 334dc4f20b
9 changed files with 461 additions and 249 deletions

View File

@@ -38,8 +38,8 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.PreferredShortcutType;
import com.android.settings.accessibility.AccessibilityUtil.State;
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -48,7 +48,10 @@ import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.widget.RadioButtonPreference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@SearchIndexable
public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceFragment
@@ -57,14 +60,12 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED;
private static final String PREFERENCE_KEY = "daltonizer_mode_deuteranomaly";
private static final String EXTRA_SHORTCUT_TYPE = "shortcutType";
// TODO(b/142530063): Check the new setting key to decide which summary should be shown.
private static final String KEY_SHORTCUT_TYPE = Settings.System.MASTER_MONO;
private static final String EXTRA_SHORTCUT_TYPE = "shortcut_type";
private static final String KEY_SHORTCUT_PREFERENCE = "shortcut_preference";
private static final int DIALOG_ID_EDIT_SHORTCUT = 1;
private static final List<AbstractPreferenceController> sControllers = new ArrayList<>();
private ShortcutPreference mShortcutPreference;
private int mPreferredShortcutType = PreferredShortcutType.DEFAULT;
private int mUserShortcutType = UserShortcutType.DEFAULT;
private CheckBox mSoftwareTypeCheckBox;
private CheckBox mHardwareTypeCheckBox;
@@ -99,7 +100,7 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(EXTRA_SHORTCUT_TYPE, mPreferredShortcutType);
outState.putInt(EXTRA_SHORTCUT_TYPE, mUserShortcutType);
super.onSaveInstanceState(outState);
}
@@ -148,8 +149,8 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
}
private void updateAlertDialogCheckState() {
updateCheckStatus(mSoftwareTypeCheckBox, PreferredShortcutType.SOFTWARE);
updateCheckStatus(mHardwareTypeCheckBox, PreferredShortcutType.HARDWARE);
updateCheckStatus(mSoftwareTypeCheckBox, UserShortcutType.SOFTWARE);
updateCheckStatus(mHardwareTypeCheckBox, UserShortcutType.HARDWARE);
}
private void updateAlertDialogEnableState() {
@@ -163,48 +164,55 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
}
}
private void updateCheckStatus(CheckBox checkBox, @PreferredShortcutType int type) {
checkBox.setChecked((mPreferredShortcutType & type) == type);
private void updateCheckStatus(CheckBox checkBox, @UserShortcutType int type) {
checkBox.setChecked((mUserShortcutType & type) == type);
checkBox.setOnClickListener(v -> {
updatePreferredShortcutType(false);
updateUserShortcutType(/* saveChanges= */ false);
updateAlertDialogEnableState();
});
}
private void updatePreferredShortcutType(boolean saveToDB) {
mPreferredShortcutType = PreferredShortcutType.DEFAULT;
private void updateUserShortcutType(boolean saveChanges) {
mUserShortcutType = UserShortcutType.DEFAULT;
if (mSoftwareTypeCheckBox.isChecked()) {
mPreferredShortcutType |= PreferredShortcutType.SOFTWARE;
mUserShortcutType |= UserShortcutType.SOFTWARE;
}
if (mHardwareTypeCheckBox.isChecked()) {
mPreferredShortcutType |= PreferredShortcutType.HARDWARE;
mUserShortcutType |= UserShortcutType.HARDWARE;
}
if (saveToDB) {
setPreferredShortcutType(mPreferredShortcutType);
if (saveChanges) {
setUserShortcutType(getPrefContext(), mUserShortcutType);
}
}
private void setSecureIntValue(String key, @PreferredShortcutType int value) {
Settings.Secure.putIntForUser(getPrefContext().getContentResolver(),
key, value, getPrefContext().getContentResolver().getUserId());
}
private void setPreferredShortcutType(@PreferredShortcutType int type) {
setSecureIntValue(KEY_SHORTCUT_TYPE, type);
private void setUserShortcutType(Context context, int type) {
Set<String> info = SharedPreferenceUtils.getUserShortcutType(context);
if (info.isEmpty()) {
info = new HashSet<>();
} else {
final Set<String> filtered = info.stream().filter(
str -> str.contains(getComponentName().flattenToString())).collect(
Collectors.toSet());
info.removeAll(filtered);
}
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
getComponentName().flattenToString(), type);
info.add(shortcut.flattenToString());
SharedPreferenceUtils.setUserShortcutType(context, info);
}
private String getShortcutTypeSummary(Context context) {
final int shortcutType = getPreferredShortcutType(context);
final int shortcutType = getUserShortcutType(context, UserShortcutType.SOFTWARE);
final CharSequence softwareTitle =
context.getText(AccessibilityUtil.isGestureNavigateEnabled(context)
? R.string.accessibility_shortcut_edit_dialog_title_software_gesture
: R.string.accessibility_shortcut_edit_dialog_title_software);
List<CharSequence> list = new ArrayList<>();
if ((shortcutType & PreferredShortcutType.SOFTWARE) == PreferredShortcutType.SOFTWARE) {
if ((shortcutType & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE) {
list.add(softwareTitle);
}
if ((shortcutType & PreferredShortcutType.HARDWARE) == PreferredShortcutType.HARDWARE) {
if ((shortcutType & UserShortcutType.HARDWARE) == UserShortcutType.HARDWARE) {
final CharSequence hardwareTitle = context.getText(
R.string.accessibility_shortcut_edit_dialog_title_hardware);
list.add(hardwareTitle);
@@ -218,21 +226,23 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
return AccessibilityUtil.capitalize(joinStrings);
}
@PreferredShortcutType
private int getPreferredShortcutType(Context context) {
return getSecureIntValue(context, KEY_SHORTCUT_TYPE, PreferredShortcutType.SOFTWARE);
}
private int getUserShortcutType(Context context, @UserShortcutType int defaultValue) {
final Set<String> info = SharedPreferenceUtils.getUserShortcutType(context);
final String componentName = getComponentName().flattenToString();
final Set<String> filtered = info.stream().filter(
str -> str.contains(componentName)).collect(
Collectors.toSet());
if (filtered.isEmpty()) {
return defaultValue;
}
@PreferredShortcutType
private int getSecureIntValue(Context context, String key,
@PreferredShortcutType int defaultValue) {
return Settings.Secure.getIntForUser(
context.getContentResolver(),
key, defaultValue, context.getContentResolver().getUserId());
final String str = (String) filtered.toArray()[0];
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str);
return shortcut.getUserShortcutType();
}
private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
updatePreferredShortcutType(true);
updateUserShortcutType(/* saveChanges= */ true);
mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext()));
}
@@ -294,32 +304,33 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
@Override
public void onCheckboxClicked(ShortcutPreference preference) {
if (preference.getChecked()) {
// TODO(b/142531156): Replace PreferredShortcutType.SOFTWARE value with dialog shortcut
// TODO(b/142531156): Replace UserShortcutType.SOFTWARE value with dialog shortcut
// preferred key.
AccessibilityUtil.optInValueToSettings(getContext(), PreferredShortcutType.SOFTWARE,
AccessibilityUtil.optInValueToSettings(getContext(), UserShortcutType.SOFTWARE,
getComponentName());
} else {
// TODO(b/142531156): Replace PreferredShortcutType.SOFTWARE value with dialog shortcut
// TODO(b/142531156): Replace UserShortcutType.SOFTWARE value with dialog shortcut
// preferred key.
AccessibilityUtil.optOutValueFromSettings(getContext(), PreferredShortcutType.SOFTWARE,
AccessibilityUtil.optOutValueFromSettings(getContext(), UserShortcutType.SOFTWARE,
getComponentName());
}
}
@Override
public void onSettingsClicked(ShortcutPreference preference) {
mPreferredShortcutType = getPreferredShortcutType(getPrefContext());
mUserShortcutType = getUserShortcutType(getPrefContext(),
UserShortcutType.SOFTWARE);
showDialog(DIALOG_ID_EDIT_SHORTCUT);
}
private void initShortcutPreference(Bundle savedInstanceState) {
// Restore the PreferredShortcut type
if (savedInstanceState != null) {
mPreferredShortcutType = savedInstanceState.getInt(EXTRA_SHORTCUT_TYPE,
PreferredShortcutType.DEFAULT);
}
if (mPreferredShortcutType == PreferredShortcutType.DEFAULT) {
mPreferredShortcutType = getPreferredShortcutType(getPrefContext());
mUserShortcutType = getUserShortcutType(getPrefContext(),
UserShortcutType.SOFTWARE);
// Restore the user shortcut type
if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_SHORTCUT_TYPE)) {
mUserShortcutType = savedInstanceState.getInt(EXTRA_SHORTCUT_TYPE,
UserShortcutType.SOFTWARE);
}
// Initial ShortcutPreference widget
@@ -344,11 +355,11 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
getShortcutPreferenceKey());
if (shortcutPreference != null) {
// TODO(b/142531156): Replace PreferredShortcutType.SOFTWARE value with dialog shortcut
// TODO(b/142531156): Replace UserShortcutType.SOFTWARE value with dialog shortcut
// preferred key.
shortcutPreference.setChecked(
AccessibilityUtil.hasValueInSettings(getContext(),
PreferredShortcutType.SOFTWARE,
UserShortcutType.SOFTWARE,
getComponentName()));
}