Support accessibility shortcut secondary action (11/n)

Refactor current shortcut design to base fragment and provide
protected function to customize the legacy design,
magnification or no need shortcut app.

Bug: 142530063
Bug: 142531156
Test: make RunSettingsRoboTests2

Change-Id: I51c951ef8f819a1940ddfe33b009b40ed5ec0530
This commit is contained in:
menghanli
2020-01-03 14:10:50 +08:00
parent d737601a66
commit b82931520f
7 changed files with 405 additions and 874 deletions

View File

@@ -24,7 +24,6 @@ import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
@@ -35,17 +34,10 @@ import android.os.UserHandle;
import android.os.storage.StorageManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
import android.widget.CheckBox;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
@@ -55,30 +47,14 @@ import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.ToggleSwitch;
import com.android.settingslib.accessibility.AccessibilityUtils;
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.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
/** Fragment for providing toggle bar and basic accessibility service setup. */
public class ToggleAccessibilityServicePreferenceFragment extends
ToggleFeaturePreferenceFragment implements ShortcutPreference.OnClickListener {
private static final String KEY_SHORTCUT_PREFERENCE = "shortcut_preference";
private static final String EXTRA_SHORTCUT_TYPE = "shortcut_type";
private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
private int mUserShortcutType = UserShortcutType.DEFAULT;
// Used to restore the edit dialog status.
private int mUserShortcutTypeCache = UserShortcutType.DEFAULT;
private CheckBox mSoftwareTypeCheckBox;
private CheckBox mHardwareTypeCheckBox;
ToggleFeaturePreferenceFragment {
public static final int ACTIVITY_REQUEST_CONFIRM_CREDENTIAL_FOR_WEAKER_ENCRYPTION = 1;
private CharSequence mDialogTitle;
private LockPatternUtils mLockPatternUtils;
private AtomicBoolean mIsDialogShown = new AtomicBoolean(/* initialValue= */ false);
@@ -92,14 +68,6 @@ public class ToggleAccessibilityServicePreferenceFragment extends
private Dialog mDialog;
@Retention(RetentionPolicy.SOURCE)
private @interface DialogType {
int ENABLE_WARNING_FROM_TOGGLE = 1;
int ENABLE_WARNING_FROM_SHORTCUT = 2;
int LAUNCH_ACCESSIBILITY_TUTORIAL = 3;
int EDIT_SHORTCUT = 4;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_SERVICE;
@@ -118,56 +86,13 @@ public class ToggleAccessibilityServicePreferenceFragment extends
mLockPatternUtils = new LockPatternUtils(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTouchExplorationStateChangeListener = isTouchExplorationEnabled -> {
removeDialog(DialogType.EDIT_SHORTCUT);
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
};
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Restore the user shortcut type.
if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_SHORTCUT_TYPE)) {
mUserShortcutTypeCache = savedInstanceState.getInt(EXTRA_SHORTCUT_TYPE,
UserShortcutType.DEFAULT);
}
initShortcutPreference();
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(EXTRA_SHORTCUT_TYPE, mUserShortcutTypeCache);
super.onSaveInstanceState(outState);
}
@Override
public void onResume() {
super.onResume();
mSettingsContentObserver.register(getContentResolver());
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.addTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
updateSwitchBarToggleSwitch();
updateShortcutPreferenceData();
updateShortcutPreference();
}
@Override
public void onPause() {
mSettingsContentObserver.unregister(getContentResolver());
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
super.onPause();
}
@Override
@@ -198,7 +123,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
@Override
public Dialog onCreateDialog(int dialogId) {
switch (dialogId) {
case DialogType.ENABLE_WARNING_FROM_TOGGLE: {
case DialogEnums.ENABLE_WARNING_FROM_TOGGLE: {
final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
if (info == null) {
return null;
@@ -208,7 +133,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
this::onDialogButtonFromToggleClicked);
break;
}
case DialogType.ENABLE_WARNING_FROM_SHORTCUT: {
case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT: {
final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
if (info == null) {
return null;
@@ -218,7 +143,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
this::onDialogButtonFromShortcutClicked);
break;
}
case DialogType.LAUNCH_ACCESSIBILITY_TUTORIAL: {
case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL: {
if (AccessibilityUtil.isGestureNavigateEnabled(getContext())) {
mDialog = AccessibilityGestureNavigationTutorial
.showGestureNavigationTutorialDialog(getActivity());
@@ -228,153 +153,25 @@ public class ToggleAccessibilityServicePreferenceFragment extends
}
break;
}
case DialogType.EDIT_SHORTCUT: {
final CharSequence dialogTitle = getActivity().getString(
R.string.accessibility_shortcut_edit_dialog_title, mDialogTitle);
mDialog = AccessibilityEditDialogUtils.showEditShortcutDialog(getActivity(),
dialogTitle, this::callOnAlertDialogCheckboxClicked);
initializeDialogCheckBox(mDialog);
break;
}
default: {
throw new IllegalArgumentException("Unsupported dialogId " + dialogId);
mDialog = super.onCreateDialog(dialogId);
}
}
return mDialog;
}
private void initializeDialogCheckBox(Dialog dialog) {
final View dialogSoftwareView = dialog.findViewById(R.id.software_shortcut);
mSoftwareTypeCheckBox = dialogSoftwareView.findViewById(R.id.checkbox);
final View dialogHardwareView = dialog.findViewById(R.id.hardware_shortcut);
mHardwareTypeCheckBox = dialogHardwareView.findViewById(R.id.checkbox);
updateAlertDialogCheckState();
updateAlertDialogEnableState();
}
private void updateAlertDialogCheckState() {
updateCheckStatus(mSoftwareTypeCheckBox, UserShortcutType.SOFTWARE);
updateCheckStatus(mHardwareTypeCheckBox, UserShortcutType.HARDWARE);
}
private void updateAlertDialogEnableState() {
if (!mSoftwareTypeCheckBox.isChecked()) {
mHardwareTypeCheckBox.setEnabled(false);
} else if (!mHardwareTypeCheckBox.isChecked()) {
mSoftwareTypeCheckBox.setEnabled(false);
} else {
mSoftwareTypeCheckBox.setEnabled(true);
mHardwareTypeCheckBox.setEnabled(true);
}
}
private void updateCheckStatus(CheckBox checkBox, @UserShortcutType int type) {
checkBox.setChecked((mUserShortcutTypeCache & type) == type);
checkBox.setOnClickListener(v -> {
updateUserShortcutType(/* saveChanges= */ false);
updateAlertDialogEnableState();
});
}
private void updateUserShortcutType(boolean saveChanges) {
mUserShortcutTypeCache = UserShortcutType.DEFAULT;
if (mSoftwareTypeCheckBox.isChecked()) {
mUserShortcutTypeCache |= UserShortcutType.SOFTWARE;
}
if (mHardwareTypeCheckBox.isChecked()) {
mUserShortcutTypeCache |= UserShortcutType.HARDWARE;
}
if (saveChanges) {
mUserShortcutType = mUserShortcutTypeCache;
setUserShortcutType(getPrefContext(), mUserShortcutType);
}
}
private void setUserShortcutType(Context context, int type) {
Set<String> info = SharedPreferenceUtils.getUserShortcutType(context);
final String componentName = getComponentName().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 AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
getComponentName().flattenToString(), type);
info.add(shortcut.flattenToString());
SharedPreferenceUtils.setUserShortcutType(context, info);
}
private String getShortcutTypeSummary(Context context) {
final int shortcutType = getUserShortcutType(context, UserShortcutType.SOFTWARE);
int resId = R.string.accessibility_shortcut_edit_dialog_title_software;
if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
resId = AccessibilityUtil.isTouchExploreEnabled(context)
? R.string.accessibility_shortcut_edit_dialog_title_software_gesture_talkback
: R.string.accessibility_shortcut_edit_dialog_title_software_gesture;
}
final CharSequence softwareTitle = context.getText(resId);
List<CharSequence> list = new ArrayList<>();
if ((shortcutType & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE) {
list.add(softwareTitle);
}
if ((shortcutType & UserShortcutType.HARDWARE) == UserShortcutType.HARDWARE) {
final CharSequence hardwareTitle = context.getText(
R.string.accessibility_shortcut_edit_dialog_title_hardware);
list.add(hardwareTitle);
}
// Show software shortcut if first time to use.
if (list.isEmpty()) {
list.add(softwareTitle);
}
final String joinStrings = TextUtils.join(/* delimiter= */", ", list);
return AccessibilityUtil.capitalize(joinStrings);
}
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;
}
final String str = (String) filtered.toArray()[0];
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str);
return shortcut.getUserShortcutType();
}
private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
updateUserShortcutType(/* saveChanges= */ true);
if (mShortcutPreference.getChecked()) {
AccessibilityUtil.optInAllValuesToSettings(getContext(), mUserShortcutType,
mComponentName);
AccessibilityUtil.optOutAllValuesFromSettings(getContext(), ~mUserShortcutType,
mComponentName);
}
mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext()));
}
@Override
public int getDialogMetricsCategory(int dialogId) {
switch (dialogId) {
case DialogType.ENABLE_WARNING_FROM_TOGGLE:
case DialogType.ENABLE_WARNING_FROM_SHORTCUT:
case DialogEnums.ENABLE_WARNING_FROM_TOGGLE:
case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT:
return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_ENABLE;
case DialogType.LAUNCH_ACCESSIBILITY_TUTORIAL:
return AccessibilityUtil.isGestureNavigateEnabled(getContext())
case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL:
return AccessibilityUtil.isGestureNavigateEnabled(getPrefContext())
? SettingsEnums.DIALOG_TOGGLE_SCREEN_GESTURE_NAVIGATION
: SettingsEnums.DIALOG_TOGGLE_SCREEN_ACCESSIBILITY_BUTTON;
case DialogType.EDIT_SHORTCUT:
return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_EDIT_SHORTCUT;
default:
return 0;
return super.getDialogMetricsCategory(dialogId);
}
}
@@ -387,55 +184,6 @@ public class ToggleAccessibilityServicePreferenceFragment extends
switchBar.setSwitchBarText(switchBarText, switchBarText);
}
private void updateShortcutPreferenceData() {
// Get the user shortcut type from settings provider.
mUserShortcutType = AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(),
getComponentName());
if (mUserShortcutType != UserShortcutType.DEFAULT) {
setUserShortcutType(getPrefContext(), mUserShortcutType);
} else {
// Get the user shortcut type from shared_prefs if cannot get from settings provider.
mUserShortcutType = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
}
}
@Override
protected void updateFooterTitle(PreferenceCategory category) {
final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
final String titleText = (info == null) ? "" :
getString(R.string.accessibility_footer_title,
info.getResolveInfo().loadLabel(getPackageManager()));
category.setTitle(titleText);
}
private void initShortcutPreference() {
final PreferenceScreen preferenceScreen = getPreferenceScreen();
mShortcutPreference = new ShortcutPreference(
preferenceScreen.getContext(), null);
mShortcutPreference.setPersistent(false);
mShortcutPreference.setKey(getShortcutPreferenceKey());
mShortcutPreference.setTitle(R.string.accessibility_shortcut_title);
mShortcutPreference.setOnClickListener(this);
}
private void updateShortcutPreference() {
final PreferenceScreen preferenceScreen = getPreferenceScreen();
ShortcutPreference shortcutPreference = preferenceScreen.findPreference(
getShortcutPreferenceKey());
if (shortcutPreference != null) {
final int shortcutTypes = getUserShortcutType(getContext(), UserShortcutType.SOFTWARE);
shortcutPreference.setChecked(
AccessibilityUtil.hasValuesInSettings(getContext(), shortcutTypes,
mComponentName));
shortcutPreference.setSummary(getShortcutTypeSummary(getContext()));
}
}
protected String getShortcutPreferenceKey() {
return KEY_SHORTCUT_PREFERENCE;
}
private void updateSwitchBarToggleSwitch() {
final boolean checked = AccessibilityUtils.getEnabledServicesFromSettings(getActivity())
.contains(mComponentName);
@@ -524,7 +272,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
if (preference.getChecked()) {
if (!getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED)) {
preference.setChecked(false);
showPopupDialog(DialogType.ENABLE_WARNING_FROM_SHORTCUT);
showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_SHORTCUT);
} else {
AccessibilityUtil.optInAllValuesToSettings(getContext(), shortcutTypes,
mComponentName);
@@ -538,8 +286,8 @@ public class ToggleAccessibilityServicePreferenceFragment extends
@Override
public void onSettingsClicked(ShortcutPreference preference) {
mUserShortcutTypeCache = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
showPopupDialog(DialogType.EDIT_SHORTCUT);
super.onSettingsClicked(preference);
showPopupDialog(DialogEnums.EDIT_SHORTCUT);
}
@Override
@@ -572,7 +320,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
mHtmlDescription = arguments.getCharSequence(AccessibilitySettings.EXTRA_HTML_DESCRIPTION);
// Get Accessibility service name.
mDialogTitle = getAccessibilityServiceInfo().getResolveInfo().loadLabel(
mPackageName = getAccessibilityServiceInfo().getResolveInfo().loadLabel(
getPackageManager());
}
@@ -597,7 +345,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
handleConfirmServiceEnabled(/* confirmed= */ true);
if (isServiceSupportAccessibilityButton()) {
mIsDialogShown.set(false);
showPopupDialog(DialogType.LAUNCH_ACCESSIBILITY_TUTORIAL);
showPopupDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
}
}
@@ -620,8 +368,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
}
private void onAllowButtonFromShortcutClicked() {
final ShortcutPreference shortcutPreference = findPreference(getShortcutPreferenceKey());
shortcutPreference.setChecked(true);
mShortcutPreference.setChecked(true);
final int shortcutTypes = getUserShortcutType(getContext(), UserShortcutType.SOFTWARE);
AccessibilityUtil.optInAllValuesToSettings(getContext(), shortcutTypes, mComponentName);
@@ -630,8 +377,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
}
private void onDenyButtonFromShortcutClicked() {
final ShortcutPreference shortcutPreference = findPreference(getShortcutPreferenceKey());
shortcutPreference.setChecked(false);
mShortcutPreference.setChecked(false);
mDialog.dismiss();
}
@@ -640,15 +386,12 @@ public class ToggleAccessibilityServicePreferenceFragment extends
if (checked) {
mSwitchBar.setCheckedInternal(false);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, false);
final ShortcutPreference shortcutPreference = findPreference(
getShortcutPreferenceKey());
if (!shortcutPreference.getChecked()) {
showPopupDialog(DialogType.ENABLE_WARNING_FROM_TOGGLE);
if (!mShortcutPreference.getChecked()) {
showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_TOGGLE);
} else {
handleConfirmServiceEnabled(/* confirmed= */ true);
if (isServiceSupportAccessibilityButton()) {
showPopupDialog(DialogType.LAUNCH_ACCESSIBILITY_TUTORIAL);
showPopupDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
}
}
} else {
@@ -665,8 +408,4 @@ public class ToggleAccessibilityServicePreferenceFragment extends
false));
}
}
ComponentName getComponentName() {
return mComponentName;
}
}