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

@@ -26,6 +26,7 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.UserHandle; import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.view.View;
import android.view.accessibility.AccessibilityManager; import android.view.accessibility.AccessibilityManager;
import android.widget.Switch; import android.widget.Switch;
@@ -83,6 +84,12 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
}); });
} }
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mShortcutPreference.setVisible(false);
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();

View File

@@ -21,8 +21,6 @@ import static com.android.settings.accessibility.AccessibilityUtil.UserShortcutT
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@@ -35,22 +33,18 @@ public class LegacyAccessibilityServicePreferenceFragment extends
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
final PreferenceScreen preferenceScreen = getPreferenceScreen(); final CharSequence hardwareTitle = getPrefContext().getText(
final ShortcutPreference shortcutPreference = preferenceScreen.findPreference( R.string.accessibility_shortcut_edit_dialog_title_hardware);
getShortcutPreferenceKey()); mShortcutPreference.setSummary(hardwareTitle);
if (shortcutPreference != null) { mShortcutPreference.setSettingsVisibility(View.GONE);
final CharSequence hardwareTitle = getPrefContext().getText(
R.string.accessibility_shortcut_edit_dialog_title_hardware);
shortcutPreference.setSummary(hardwareTitle);
shortcutPreference.setSettingsVisibility(View.GONE);
}
// Only allowed hardware PreferredShortcutType in this fragment. // Only allowed hardware PreferredShortcutType in this fragment.
setAllowedPreferredShortcutType(UserShortcutType.HARDWARE); setAllowedPreferredShortcutType(UserShortcutType.HARDWARE);
} }
private void setAllowedPreferredShortcutType(int type) { private void setAllowedPreferredShortcutType(int type) {
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
getComponentName().flattenToString(), type); mComponentName.flattenToString(), type);
SharedPreferenceUtils.setUserShortcutType(getPrefContext(), SharedPreferenceUtils.setUserShortcutType(getPrefContext(),
ImmutableSet.of(shortcut.flattenToString())); ImmutableSet.of(shortcut.flattenToString()));

View File

@@ -24,7 +24,6 @@ import android.app.settings.SettingsEnums;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
@@ -35,17 +34,10 @@ import android.os.UserHandle;
import android.os.storage.StorageManager; import android.os.storage.StorageManager;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager; 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.internal.widget.LockPatternUtils;
import com.android.settings.R; import com.android.settings.R;
@@ -55,30 +47,14 @@ import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.ToggleSwitch; import com.android.settings.widget.ToggleSwitch;
import com.android.settingslib.accessibility.AccessibilityUtils; 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.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
/** Fragment for providing toggle bar and basic accessibility service setup. */ /** Fragment for providing toggle bar and basic accessibility service setup. */
public class ToggleAccessibilityServicePreferenceFragment extends public class ToggleAccessibilityServicePreferenceFragment extends
ToggleFeaturePreferenceFragment implements ShortcutPreference.OnClickListener { ToggleFeaturePreferenceFragment {
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;
public static final int ACTIVITY_REQUEST_CONFIRM_CREDENTIAL_FOR_WEAKER_ENCRYPTION = 1; public static final int ACTIVITY_REQUEST_CONFIRM_CREDENTIAL_FOR_WEAKER_ENCRYPTION = 1;
private CharSequence mDialogTitle;
private LockPatternUtils mLockPatternUtils; private LockPatternUtils mLockPatternUtils;
private AtomicBoolean mIsDialogShown = new AtomicBoolean(/* initialValue= */ false); private AtomicBoolean mIsDialogShown = new AtomicBoolean(/* initialValue= */ false);
@@ -92,14 +68,6 @@ public class ToggleAccessibilityServicePreferenceFragment extends
private Dialog mDialog; 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 @Override
public int getMetricsCategory() { public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_SERVICE; return SettingsEnums.ACCESSIBILITY_SERVICE;
@@ -118,56 +86,13 @@ public class ToggleAccessibilityServicePreferenceFragment extends
mLockPatternUtils = new LockPatternUtils(getActivity()); 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 @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mSettingsContentObserver.register(getContentResolver()); mSettingsContentObserver.register(getContentResolver());
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.addTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
updateSwitchBarToggleSwitch(); updateSwitchBarToggleSwitch();
updateShortcutPreferenceData();
updateShortcutPreference();
}
@Override
public void onPause() {
mSettingsContentObserver.unregister(getContentResolver());
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
super.onPause();
} }
@Override @Override
@@ -198,7 +123,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
@Override @Override
public Dialog onCreateDialog(int dialogId) { public Dialog onCreateDialog(int dialogId) {
switch (dialogId) { switch (dialogId) {
case DialogType.ENABLE_WARNING_FROM_TOGGLE: { case DialogEnums.ENABLE_WARNING_FROM_TOGGLE: {
final AccessibilityServiceInfo info = getAccessibilityServiceInfo(); final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
if (info == null) { if (info == null) {
return null; return null;
@@ -208,7 +133,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
this::onDialogButtonFromToggleClicked); this::onDialogButtonFromToggleClicked);
break; break;
} }
case DialogType.ENABLE_WARNING_FROM_SHORTCUT: { case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT: {
final AccessibilityServiceInfo info = getAccessibilityServiceInfo(); final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
if (info == null) { if (info == null) {
return null; return null;
@@ -218,7 +143,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
this::onDialogButtonFromShortcutClicked); this::onDialogButtonFromShortcutClicked);
break; break;
} }
case DialogType.LAUNCH_ACCESSIBILITY_TUTORIAL: { case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL: {
if (AccessibilityUtil.isGestureNavigateEnabled(getContext())) { if (AccessibilityUtil.isGestureNavigateEnabled(getContext())) {
mDialog = AccessibilityGestureNavigationTutorial mDialog = AccessibilityGestureNavigationTutorial
.showGestureNavigationTutorialDialog(getActivity()); .showGestureNavigationTutorialDialog(getActivity());
@@ -228,153 +153,25 @@ public class ToggleAccessibilityServicePreferenceFragment extends
} }
break; 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: { default: {
throw new IllegalArgumentException("Unsupported dialogId " + dialogId); mDialog = super.onCreateDialog(dialogId);
} }
} }
return mDialog; 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 @Override
public int getDialogMetricsCategory(int dialogId) { public int getDialogMetricsCategory(int dialogId) {
switch (dialogId) { switch (dialogId) {
case DialogType.ENABLE_WARNING_FROM_TOGGLE: case DialogEnums.ENABLE_WARNING_FROM_TOGGLE:
case DialogType.ENABLE_WARNING_FROM_SHORTCUT: case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT:
return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_ENABLE; return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_ENABLE;
case DialogType.LAUNCH_ACCESSIBILITY_TUTORIAL: case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL:
return AccessibilityUtil.isGestureNavigateEnabled(getContext()) return AccessibilityUtil.isGestureNavigateEnabled(getPrefContext())
? SettingsEnums.DIALOG_TOGGLE_SCREEN_GESTURE_NAVIGATION ? SettingsEnums.DIALOG_TOGGLE_SCREEN_GESTURE_NAVIGATION
: SettingsEnums.DIALOG_TOGGLE_SCREEN_ACCESSIBILITY_BUTTON; : SettingsEnums.DIALOG_TOGGLE_SCREEN_ACCESSIBILITY_BUTTON;
case DialogType.EDIT_SHORTCUT:
return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_EDIT_SHORTCUT;
default: default:
return 0; return super.getDialogMetricsCategory(dialogId);
} }
} }
@@ -387,55 +184,6 @@ public class ToggleAccessibilityServicePreferenceFragment extends
switchBar.setSwitchBarText(switchBarText, switchBarText); 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() { private void updateSwitchBarToggleSwitch() {
final boolean checked = AccessibilityUtils.getEnabledServicesFromSettings(getActivity()) final boolean checked = AccessibilityUtils.getEnabledServicesFromSettings(getActivity())
.contains(mComponentName); .contains(mComponentName);
@@ -524,7 +272,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
if (preference.getChecked()) { if (preference.getChecked()) {
if (!getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED)) { if (!getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED)) {
preference.setChecked(false); preference.setChecked(false);
showPopupDialog(DialogType.ENABLE_WARNING_FROM_SHORTCUT); showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_SHORTCUT);
} else { } else {
AccessibilityUtil.optInAllValuesToSettings(getContext(), shortcutTypes, AccessibilityUtil.optInAllValuesToSettings(getContext(), shortcutTypes,
mComponentName); mComponentName);
@@ -538,8 +286,8 @@ public class ToggleAccessibilityServicePreferenceFragment extends
@Override @Override
public void onSettingsClicked(ShortcutPreference preference) { public void onSettingsClicked(ShortcutPreference preference) {
mUserShortcutTypeCache = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE); super.onSettingsClicked(preference);
showPopupDialog(DialogType.EDIT_SHORTCUT); showPopupDialog(DialogEnums.EDIT_SHORTCUT);
} }
@Override @Override
@@ -572,7 +320,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
mHtmlDescription = arguments.getCharSequence(AccessibilitySettings.EXTRA_HTML_DESCRIPTION); mHtmlDescription = arguments.getCharSequence(AccessibilitySettings.EXTRA_HTML_DESCRIPTION);
// Get Accessibility service name. // Get Accessibility service name.
mDialogTitle = getAccessibilityServiceInfo().getResolveInfo().loadLabel( mPackageName = getAccessibilityServiceInfo().getResolveInfo().loadLabel(
getPackageManager()); getPackageManager());
} }
@@ -597,7 +345,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
handleConfirmServiceEnabled(/* confirmed= */ true); handleConfirmServiceEnabled(/* confirmed= */ true);
if (isServiceSupportAccessibilityButton()) { if (isServiceSupportAccessibilityButton()) {
mIsDialogShown.set(false); mIsDialogShown.set(false);
showPopupDialog(DialogType.LAUNCH_ACCESSIBILITY_TUTORIAL); showPopupDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
} }
} }
@@ -620,8 +368,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
} }
private void onAllowButtonFromShortcutClicked() { private void onAllowButtonFromShortcutClicked() {
final ShortcutPreference shortcutPreference = findPreference(getShortcutPreferenceKey()); mShortcutPreference.setChecked(true);
shortcutPreference.setChecked(true);
final int shortcutTypes = getUserShortcutType(getContext(), UserShortcutType.SOFTWARE); final int shortcutTypes = getUserShortcutType(getContext(), UserShortcutType.SOFTWARE);
AccessibilityUtil.optInAllValuesToSettings(getContext(), shortcutTypes, mComponentName); AccessibilityUtil.optInAllValuesToSettings(getContext(), shortcutTypes, mComponentName);
@@ -630,8 +377,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
} }
private void onDenyButtonFromShortcutClicked() { private void onDenyButtonFromShortcutClicked() {
final ShortcutPreference shortcutPreference = findPreference(getShortcutPreferenceKey()); mShortcutPreference.setChecked(false);
shortcutPreference.setChecked(false);
mDialog.dismiss(); mDialog.dismiss();
} }
@@ -640,15 +386,12 @@ public class ToggleAccessibilityServicePreferenceFragment extends
if (checked) { if (checked) {
mSwitchBar.setCheckedInternal(false); mSwitchBar.setCheckedInternal(false);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, false); getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, false);
if (!mShortcutPreference.getChecked()) {
final ShortcutPreference shortcutPreference = findPreference( showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_TOGGLE);
getShortcutPreferenceKey());
if (!shortcutPreference.getChecked()) {
showPopupDialog(DialogType.ENABLE_WARNING_FROM_TOGGLE);
} else { } else {
handleConfirmServiceEnabled(/* confirmed= */ true); handleConfirmServiceEnabled(/* confirmed= */ true);
if (isServiceSupportAccessibilityButton()) { if (isServiceSupportAccessibilityButton()) {
showPopupDialog(DialogType.LAUNCH_ACCESSIBILITY_TUTORIAL); showPopupDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
} }
} }
} else { } else {
@@ -665,8 +408,4 @@ public class ToggleAccessibilityServicePreferenceFragment extends
false)); false));
} }
} }
ComponentName getComponentName() {
return mComponentName;
}
} }

View File

@@ -18,61 +18,38 @@ package com.android.settings.accessibility;
import static com.android.internal.accessibility.AccessibilityShortcutController.COLOR_INVERSION_COMPONENT_NAME; import static com.android.internal.accessibility.AccessibilityShortcutController.COLOR_INVERSION_COMPONENT_NAME;
import android.app.Dialog;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
import android.widget.CheckBox;
import android.widget.Switch; import android.widget.Switch;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.State; import com.android.settings.accessibility.AccessibilityUtil.State;
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SwitchBar; import com.android.settings.widget.SwitchBar;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/** Settings page for color inversion. */ /** Settings page for color inversion. */
@SearchIndexable @SearchIndexable
public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePreferenceFragment public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePreferenceFragment
implements SwitchBar.OnSwitchChangeListener, ShortcutPreference.OnClickListener { implements SwitchBar.OnSwitchChangeListener {
private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED; private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED;
private static final String PREVIEW_PREFERENCE_KEY = "color_inversion_preview";
private static final String CATEGORY_FOOTER_KEY = "color_inversion_footer_category"; private static final String CATEGORY_FOOTER_KEY = "color_inversion_footer_category";
private static final String KEY_SHORTCUT_PREFERENCE = "shortcut_preference";
private static final int DIALOG_ID_EDIT_SHORTCUT = 1; private static final int DIALOG_ID_EDIT_SHORTCUT = 1;
private static final String EXTRA_SHORTCUT_TYPE = "shortcut_type";
private final Handler mHandler = new Handler(); private final Handler mHandler = new Handler();
private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
private SettingsContentObserver mSettingsContentObserver; private SettingsContentObserver mSettingsContentObserver;
private int mUserShortcutType = UserShortcutType.DEFAULT;
// Used to restore the edit dialog status.
private int mUserShortcutTypeCache = UserShortcutType.DEFAULT;
private CheckBox mSoftwareTypeCheckBox;
private CheckBox mHardwareTypeCheckBox;
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
@@ -101,13 +78,6 @@ public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePrefere
R.string.accessibility_display_inversion_switch_title); R.string.accessibility_display_inversion_switch_title);
} }
@Override
protected void updateFooterTitle(PreferenceCategory category) {
final String titleText = getString(R.string.accessibility_footer_title,
getString(R.string.accessibility_display_inversion_preference_title));
category.setTitle(titleText);
}
@Override @Override
public void onSwitchChanged(Switch switchView, boolean isChecked) { public void onSwitchChanged(Switch switchView, boolean isChecked) {
Settings.Secure.putInt(getContentResolver(), ENABLED, isChecked ? State.ON : State.OFF); Settings.Secure.putInt(getContentResolver(), ENABLED, isChecked ? State.ON : State.OFF);
@@ -124,6 +94,8 @@ public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePrefere
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
mComponentName = COLOR_INVERSION_COMPONENT_NAME;
mPackageName = getString(R.string.accessibility_display_inversion_preference_title);
final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1); final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1);
enableServiceFeatureKeys.add(ENABLED); enableServiceFeatureKeys.add(ENABLED);
mSettingsContentObserver = new SettingsContentObserver(mHandler, enableServiceFeatureKeys) { mSettingsContentObserver = new SettingsContentObserver(mHandler, enableServiceFeatureKeys) {
@@ -134,262 +106,35 @@ public class ToggleColorInversionPreferenceFragment extends ToggleFeaturePrefere
== State.ON); == State.ON);
} }
}; };
mTouchExplorationStateChangeListener = isTouchExplorationEnabled -> {
removeDialog(DIALOG_ID_EDIT_SHORTCUT);
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
};
return super.onCreateView(inflater, container, savedInstanceState); return super.onCreateView(inflater, container, savedInstanceState);
} }
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { 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); super.onViewCreated(view, savedInstanceState);
final PreferenceScreen preferenceScreen = getPreferenceScreen(); final PreferenceScreen preferenceScreen = getPreferenceScreen();
preferenceScreen.setOrderingAsAdded(false); preferenceScreen.setOrderingAsAdded(false);
final PreferenceCategory footerCategory = preferenceScreen.findPreference( final PreferenceCategory footerCategory = preferenceScreen.findPreference(
CATEGORY_FOOTER_KEY); CATEGORY_FOOTER_KEY);
updateFooterTitle(footerCategory);
footerCategory.setOrder(Integer.MAX_VALUE); footerCategory.setOrder(Integer.MAX_VALUE);
} }
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(EXTRA_SHORTCUT_TYPE, mUserShortcutTypeCache);
super.onSaveInstanceState(outState);
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mSettingsContentObserver.register(getContentResolver()); mSettingsContentObserver.register(getContentResolver());
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.addTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
updateShortcutPreferenceData();
updateShortcutPreference();
} }
@Override @Override
public void onPause() { public void onPause() {
mSettingsContentObserver.unregister(getContentResolver()); mSettingsContentObserver.unregister(getContentResolver());
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
super.onPause(); super.onPause();
} }
@Override
public Dialog onCreateDialog(int dialogId) {
if (dialogId == DIALOG_ID_EDIT_SHORTCUT) {
final CharSequence dialogTitle = getActivity().getText(
R.string.accessibility_shortcut_edit_dialog_title_daltonizer);
final AlertDialog dialog = AccessibilityEditDialogUtils.showEditShortcutDialog(
getActivity(),
dialogTitle, this::callOnAlertDialogCheckboxClicked);
initializeDialogCheckBox(dialog);
return dialog;
}
throw new IllegalArgumentException("Unsupported dialogId " + dialogId);
}
private void initializeDialogCheckBox(AlertDialog 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);
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 = 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,
getComponentName());
AccessibilityUtil.optOutAllValuesFromSettings(getContext(), ~mUserShortcutType,
getComponentName());
}
mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext()));
}
@Override
public int getDialogMetricsCategory(int dialogId) {
if (dialogId == DIALOG_ID_EDIT_SHORTCUT) {
return SettingsEnums.DIALOG_COLOR_INVERSION_EDIT_SHORTCUT;
}
return 0;
}
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);
}
}
private void initShortcutPreference() {
final PreferenceScreen preferenceScreen = getPreferenceScreen();
mShortcutPreference = new ShortcutPreference(
preferenceScreen.getContext(), null);
final Preference previewPreference = findPreference(PREVIEW_PREFERENCE_KEY);
mShortcutPreference.setPersistent(false);
mShortcutPreference.setKey(getShortcutPreferenceKey());
mShortcutPreference.setTitle(R.string.accessibility_shortcut_title);
mShortcutPreference.setOnClickListener(this);
}
private void updateShortcutPreference() {
final PreferenceScreen preferenceScreen = getPreferenceScreen();
final ShortcutPreference shortcutPreference = preferenceScreen.findPreference(
getShortcutPreferenceKey());
if (shortcutPreference != null) {
final int shortcutTypes = getUserShortcutType(getContext(), UserShortcutType.SOFTWARE);
shortcutPreference.setChecked(
AccessibilityUtil.hasValuesInSettings(getContext(), shortcutTypes,
getComponentName()));
shortcutPreference.setSummary(getShortcutTypeSummary(getContext()));
}
}
private String getShortcutPreferenceKey() {
return KEY_SHORTCUT_PREFERENCE;
}
private ComponentName getComponentName() {
return COLOR_INVERSION_COMPONENT_NAME;
}
@Override
public void onCheckboxClicked(ShortcutPreference preference) {
final int shortcutTypes = getUserShortcutType(getContext(), UserShortcutType.SOFTWARE);
if (preference.getChecked()) {
AccessibilityUtil.optInAllValuesToSettings(getContext(), shortcutTypes,
getComponentName());
} else {
AccessibilityUtil.optOutAllValuesFromSettings(getContext(), shortcutTypes,
getComponentName());
}
}
@Override @Override
public void onSettingsClicked(ShortcutPreference preference) { public void onSettingsClicked(ShortcutPreference preference) {
mUserShortcutTypeCache = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE); super.onSettingsClicked(preference);
showDialog(DIALOG_ID_EDIT_SHORTCUT); showDialog(DIALOG_ID_EDIT_SHORTCUT);
} }

View File

@@ -18,33 +18,24 @@ package com.android.settings.accessibility;
import static com.android.internal.accessibility.AccessibilityShortcutController.DALTONIZER_COMPONENT_NAME; import static com.android.internal.accessibility.AccessibilityShortcutController.DALTONIZER_COMPONENT_NAME;
import android.app.Dialog;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
import android.widget.CheckBox;
import android.widget.Switch; import android.widget.Switch;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.State; import com.android.settings.accessibility.AccessibilityUtil.State;
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SwitchBar; import com.android.settings.widget.SwitchBar;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
@@ -52,30 +43,18 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@SearchIndexable @SearchIndexable
public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceFragment public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceFragment
implements DaltonizerRadioButtonPreferenceController.OnChangeListener, implements DaltonizerRadioButtonPreferenceController.OnChangeListener,
SwitchBar.OnSwitchChangeListener, ShortcutPreference.OnClickListener { SwitchBar.OnSwitchChangeListener{
private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED; private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED;
private static final String CATEGORY_FOOTER_KEY = "daltonizer_footer_category"; private static final String CATEGORY_FOOTER_KEY = "daltonizer_footer_category";
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 static final List<AbstractPreferenceController> sControllers = new ArrayList<>();
private final Handler mHandler = new Handler(); private final Handler mHandler = new Handler();
private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
private SettingsContentObserver mSettingsContentObserver; private SettingsContentObserver mSettingsContentObserver;
private int mUserShortcutType = UserShortcutType.DEFAULT;
// Used to restore the edit dialog status.
private int mUserShortcutTypeCache = UserShortcutType.DEFAULT;
private CheckBox mSoftwareTypeCheckBox;
private CheckBox mHardwareTypeCheckBox;
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context, private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle) { Lifecycle lifecycle) {
@@ -102,6 +81,8 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
mComponentName = DALTONIZER_COMPONENT_NAME;
mPackageName = getString(R.string.accessibility_display_daltonizer_preference_title);
final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1); final List<String> enableServiceFeatureKeys = new ArrayList<>(/* initialCapacity= */ 1);
enableServiceFeatureKeys.add(ENABLED); enableServiceFeatureKeys.add(ENABLED);
mSettingsContentObserver = new SettingsContentObserver(mHandler, enableServiceFeatureKeys) { mSettingsContentObserver = new SettingsContentObserver(mHandler, enableServiceFeatureKeys) {
@@ -112,218 +93,46 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
== State.ON); == State.ON);
} }
}; };
mTouchExplorationStateChangeListener = isTouchExplorationEnabled -> {
removeDialog(DIALOG_ID_EDIT_SHORTCUT);
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
};
return super.onCreateView(inflater, container, savedInstanceState); return super.onCreateView(inflater, container, savedInstanceState);
} }
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { 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); super.onViewCreated(view, savedInstanceState);
final PreferenceScreen preferenceScreen = getPreferenceScreen(); final PreferenceScreen preferenceScreen = getPreferenceScreen();
preferenceScreen.setOrderingAsAdded(false); preferenceScreen.setOrderingAsAdded(false);
final PreferenceCategory footerCategory = preferenceScreen.findPreference( final PreferenceCategory footerCategory = preferenceScreen.findPreference(
CATEGORY_FOOTER_KEY); CATEGORY_FOOTER_KEY);
updateFooterTitle(footerCategory);
footerCategory.setOrder(Integer.MAX_VALUE); footerCategory.setOrder(Integer.MAX_VALUE);
} }
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(EXTRA_SHORTCUT_TYPE, mUserShortcutTypeCache);
super.onSaveInstanceState(outState);
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mSettingsContentObserver.register(getContentResolver()); mSettingsContentObserver.register(getContentResolver());
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.addTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
for (AbstractPreferenceController controller : for (AbstractPreferenceController controller :
buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) { buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) {
((DaltonizerRadioButtonPreferenceController) controller).setOnChangeListener(this); ((DaltonizerRadioButtonPreferenceController) controller).setOnChangeListener(this);
((DaltonizerRadioButtonPreferenceController) controller).displayPreference( ((DaltonizerRadioButtonPreferenceController) controller).displayPreference(
getPreferenceScreen()); getPreferenceScreen());
} }
updateShortcutPreferenceData();
updateShortcutPreference();
} }
@Override @Override
public void onPause() { public void onPause() {
mSettingsContentObserver.unregister(getContentResolver()); mSettingsContentObserver.unregister(getContentResolver());
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
for (AbstractPreferenceController controller : for (AbstractPreferenceController controller :
buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) { buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) {
((DaltonizerRadioButtonPreferenceController) controller).setOnChangeListener(null); ((DaltonizerRadioButtonPreferenceController) controller).setOnChangeListener(null);
} }
super.onPause(); super.onPause();
} }
@Override
public Dialog onCreateDialog(int dialogId) {
if (dialogId == DIALOG_ID_EDIT_SHORTCUT) {
final CharSequence dialogTitle = getActivity().getText(
R.string.accessibility_shortcut_edit_dialog_title_daltonizer);
final AlertDialog dialog = AccessibilityEditDialogUtils.showEditShortcutDialog(
getActivity(),
dialogTitle, this::callOnAlertDialogCheckboxClicked);
initializeDialogCheckBox(dialog);
return dialog;
}
throw new IllegalArgumentException("Unsupported dialogId " + dialogId);
}
private void initializeDialogCheckBox(AlertDialog 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);
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 = 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,
getComponentName());
AccessibilityUtil.optOutAllValuesFromSettings(getContext(), ~mUserShortcutType,
getComponentName());
}
mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext()));
}
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_TOGGLE_DALTONIZER; return SettingsEnums.ACCESSIBILITY_TOGGLE_DALTONIZER;
} }
@Override
public int getDialogMetricsCategory(int dialogId) {
if (dialogId == DIALOG_ID_EDIT_SHORTCUT) {
return SettingsEnums.DIALOG_DALTONIZER_EDIT_SHORTCUT;
}
return 0;
}
@Override @Override
public int getHelpResource() { public int getHelpResource() {
return R.string.help_url_color_correction; return R.string.help_url_color_correction;
@@ -351,13 +160,6 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
R.string.accessibility_daltonizer_master_switch_title); R.string.accessibility_daltonizer_master_switch_title);
} }
@Override
protected void updateFooterTitle(PreferenceCategory category) {
final String titleText = getString(R.string.accessibility_footer_title,
getString(R.string.accessibility_display_daltonizer_preference_title));
category.setTitle(titleText);
}
@Override @Override
public void onSwitchChanged(Switch switchView, boolean isChecked) { public void onSwitchChanged(Switch switchView, boolean isChecked) {
Settings.Secure.putInt(getContentResolver(), ENABLED, isChecked ? State.ON : State.OFF); Settings.Secure.putInt(getContentResolver(), ENABLED, isChecked ? State.ON : State.OFF);
@@ -371,68 +173,10 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
mSwitchBar.addOnSwitchChangeListener(this); mSwitchBar.addOnSwitchChangeListener(this);
} }
@Override
public void onCheckboxClicked(ShortcutPreference preference) {
final int shortcutTypes = getUserShortcutType(getContext(), UserShortcutType.SOFTWARE);
if (preference.getChecked()) {
AccessibilityUtil.optInAllValuesToSettings(getContext(), shortcutTypes,
getComponentName());
} else {
AccessibilityUtil.optOutAllValuesFromSettings(getContext(), shortcutTypes,
getComponentName());
}
}
@Override @Override
public void onSettingsClicked(ShortcutPreference preference) { public void onSettingsClicked(ShortcutPreference preference) {
mUserShortcutTypeCache = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE); super.onSettingsClicked(preference);
showDialog(DIALOG_ID_EDIT_SHORTCUT); showDialog(DialogEnums.EDIT_SHORTCUT);
}
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.setSummary(getShortcutTypeSummary(getPrefContext()));
mShortcutPreference.setOnClickListener(this);
}
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);
}
}
private void updateShortcutPreference() {
final PreferenceScreen preferenceScreen = getPreferenceScreen();
final ShortcutPreference shortcutPreference = preferenceScreen.findPreference(
getShortcutPreferenceKey());
if (shortcutPreference != null) {
final int shortcutTypes = getUserShortcutType(getContext(), UserShortcutType.SOFTWARE);
shortcutPreference.setChecked(
AccessibilityUtil.hasValuesInSettings(getContext(), shortcutTypes,
getComponentName()));
shortcutPreference.setSummary(getShortcutTypeSummary(getContext()));
}
}
private String getShortcutPreferenceKey() {
return KEY_SHORTCUT_PREFERENCE;
}
private ComponentName getComponentName() {
return DALTONIZER_COMPONENT_NAME;
} }
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =

View File

@@ -16,8 +16,12 @@
package com.android.settings.accessibility; package com.android.settings.accessibility;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@@ -25,7 +29,12 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.Html; import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
import android.widget.CheckBox;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.preference.Preference; import androidx.preference.Preference;
@@ -35,14 +44,25 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
import com.android.settings.widget.SwitchBar; import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.ToggleSwitch; import com.android.settings.widget.ToggleSwitch;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.stream.Collectors;
public abstract class ToggleFeaturePreferenceFragment extends SettingsPreferenceFragment { /**
* Base class for accessibility fragments with toggle, shortcut, some helper functions
* and dialog management.
*/
public abstract class ToggleFeaturePreferenceFragment extends SettingsPreferenceFragment
implements ShortcutPreference.OnClickListener {
protected SwitchBar mSwitchBar; protected SwitchBar mSwitchBar;
protected ToggleSwitch mToggleSwitch; protected ToggleSwitch mToggleSwitch;
@@ -52,13 +72,24 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
protected CharSequence mSettingsTitle; protected CharSequence mSettingsTitle;
protected Intent mSettingsIntent; protected Intent mSettingsIntent;
// The mComponentName maybe null, such as Magnify
protected ComponentName mComponentName; protected ComponentName mComponentName;
protected CharSequence mPackageName;
protected Uri mImageUri; protected Uri mImageUri;
protected CharSequence mStaticDescription; protected CharSequence mStaticDescription;
protected CharSequence mHtmlDescription; protected CharSequence mHtmlDescription;
private static final String ANCHOR_TAG = "a"; private static final String ANCHOR_TAG = "a";
private static final String DRAWABLE_FOLDER = "drawable"; private static final String DRAWABLE_FOLDER = "drawable";
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;
// For html description of accessibility service, third party developer must follow the rule, // For html description of accessibility service, third party developer must follow the rule,
// such as <img src="R.drawable.fileName"/>, a11y settings will get third party resources // such as <img src="R.drawable.fileName"/>, a11y settings will get third party resources
// by this. // by this.
@@ -88,6 +119,16 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
} }
} }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTouchExplorationStateChangeListener = isTouchExplorationEnabled -> {
removeDialog(DialogEnums.EDIT_SHORTCUT);
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
};
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
@@ -108,6 +149,13 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
preferenceScreen.addPreference(animatedImagePreference); preferenceScreen.addPreference(animatedImagePreference);
} }
final PreferenceCategory category = new PreferenceCategory(getPrefContext());
category.setTitle(R.string.accessibility_screen_option);
preferenceScreen.addPreference(category);
initShortcutPreference(savedInstanceState);
category.addPreference(mShortcutPreference);
// Show the "Settings" menu as if it were a preference screen. // Show the "Settings" menu as if it were a preference screen.
if (mSettingsTitle != null && mSettingsIntent != null) { if (mSettingsTitle != null && mSettingsIntent != null) {
mSettingsPreference = new Preference(preferenceScreen.getContext()); mSettingsPreference = new Preference(preferenceScreen.getContext());
@@ -116,23 +164,15 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
mSettingsPreference.setIntent(mSettingsIntent); mSettingsPreference.setIntent(mSettingsIntent);
} }
if (mSettingsPreference != null || mShortcutPreference != null) { // The downloaded app may not show Settings. The framework app has Settings.
final PreferenceCategory category = new PreferenceCategory(getPrefContext()); if (mSettingsPreference != null) {
category.setTitle(R.string.accessibility_screen_option); category.addPreference(mSettingsPreference);
preferenceScreen.addPreference(category);
if (mShortcutPreference != null) {
category.addPreference(mShortcutPreference);
}
if (mSettingsPreference != null) {
category.addPreference(mSettingsPreference);
}
} }
if (mStaticDescription != null || mHtmlDescription != null) { if (mStaticDescription != null || mHtmlDescription != null) {
final PreferenceCategory footerCategory = new PreferenceCategory(getPrefContext()); final PreferenceCategory footerCategory = new PreferenceCategory(getPrefContext());
updateFooterTitle(footerCategory); final CharSequence title = getString(R.string.accessibility_footer_title, mPackageName);
footerCategory.setTitle(title);
preferenceScreen.addPreference(footerCategory); preferenceScreen.addPreference(footerCategory);
if (mStaticDescription != null) { if (mStaticDescription != null) {
@@ -166,6 +206,96 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
installActionBarToggleSwitch(); installActionBarToggleSwitch();
} }
@Override
public void onResume() {
super.onResume();
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.addTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
updateShortcutPreferenceData();
updateShortcutPreference();
}
@Override
public void onPause() {
final AccessibilityManager am = getPrefContext().getSystemService(
AccessibilityManager.class);
am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
super.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(EXTRA_SHORTCUT_TYPE, mUserShortcutTypeCache);
super.onSaveInstanceState(outState);
}
@Override
public Dialog onCreateDialog(int dialogId) {
switch (dialogId) {
case DialogEnums.EDIT_SHORTCUT:
final CharSequence dialogTitle = getActivity().getString(
R.string.accessibility_shortcut_edit_dialog_title, mPackageName);
Dialog dialog = AccessibilityEditDialogUtils.showEditShortcutDialog(getActivity(),
dialogTitle, this::callOnAlertDialogCheckboxClicked);
initializeDialogCheckBox(dialog);
return dialog;
default:
throw new IllegalArgumentException("Unsupported dialogId " + dialogId);
}
}
@Override
public int getDialogMetricsCategory(int dialogId) {
switch (dialogId) {
case DialogEnums.EDIT_SHORTCUT:
return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_EDIT_SHORTCUT;
default:
return SettingsEnums.ACTION_UNKNOWN;
}
}
/** Denotes the dialog emuns for show dialog */
@Retention(RetentionPolicy.SOURCE)
protected @interface DialogEnums {
int UNKNOWN = 0;
/** OPEN: Settings > Accessibility > Any toggle service > Shortcut > Settings. */
int EDIT_SHORTCUT = 1;
/** OPEN: Settings > Accessibility > Magnification > Shortcut > Settings. */
int MAGNIFICATION_EDIT_SHORTCUT = 2;
/**
* OPEN: Settings > Accessibility > Magnification > Toggle user service in gesture
* navigation.
*/
int GESTURE_NAVIGATION_TUTORIAL = 1001;
/**
* OPEN: Settings > Accessibility > Magnification > Toggle user service in button
* navigation.
*/
int ACCESSIBILITY_BUTTON_TUTORIAL = 1002;
/** OPEN: Settings > Accessibility > Downloaded toggle service > Toggle user service. */
int ENABLE_WARNING_FROM_TOGGLE = 1003;
/** OPEN: Settings > Accessibility > Downloaded toggle service > Shortcut checkbox. */
int ENABLE_WARNING_FROM_SHORTCUT = 1004;
/**
* OPEN: Settings > Accessibility > Downloaded toggle service > Toggle user service > Show
* launch tutorial.
*/
int LAUNCH_ACCESSIBILITY_TUTORIAL = 1005;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_SERVICE;
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
@@ -178,14 +308,6 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
R.string.accessibility_service_master_switch_title); R.string.accessibility_service_master_switch_title);
} }
protected void updateFooterTitle(PreferenceCategory category) {
// Implement this to provide meaningful text in the footer.
if (category != null) {
category.setTitle(getString(R.string.accessibility_footer_title,
mComponentName.getPackageName()));
}
}
protected abstract void onPreferenceToggled(String preferenceKey, boolean enabled); protected abstract void onPreferenceToggled(String preferenceKey, boolean enabled);
protected void onInstallSwitchBarToggleSwitch() { protected void onInstallSwitchBarToggleSwitch() {
@@ -303,4 +425,203 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
return joiner.toString(); return joiner.toString();
} }
} }
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) {
if (mComponentName == null) {
return;
}
Set<String> info = SharedPreferenceUtils.getUserShortcutType(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 AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
componentName, 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);
}
protected int getUserShortcutType(Context context, @UserShortcutType int defaultValue) {
if (mComponentName == null) {
return defaultValue;
}
final Set<String> info = SharedPreferenceUtils.getUserShortcutType(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 AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str);
return shortcut.getUserShortcutType();
}
private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
if (mComponentName == null) {
return;
}
updateUserShortcutType(/* saveChanges= */ true);
if (mShortcutPreference.getChecked()) {
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), mUserShortcutType,
mComponentName);
AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), ~mUserShortcutType,
mComponentName);
}
mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext()));
}
private void updateShortcutPreferenceData() {
if (mComponentName == null) {
return;
}
// Get the user shortcut type from settings provider.
mUserShortcutType = AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(),
mComponentName);
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);
}
}
private void initShortcutPreference(Bundle savedInstanceState) {
// Restore the user shortcut type.
if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_SHORTCUT_TYPE)) {
mUserShortcutTypeCache = savedInstanceState.getInt(EXTRA_SHORTCUT_TYPE,
UserShortcutType.DEFAULT);
}
// Initial the shortcut preference.
mShortcutPreference = new ShortcutPreference(getPrefContext(), null);
mShortcutPreference.setPersistent(false);
mShortcutPreference.setKey(getShortcutPreferenceKey());
mShortcutPreference.setTitle(R.string.accessibility_shortcut_title);
mShortcutPreference.setOnClickListener(this);
}
private void updateShortcutPreference() {
if (mComponentName == null) {
return;
}
final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
mShortcutPreference.setChecked(
AccessibilityUtil.hasValuesInSettings(getPrefContext(), shortcutTypes,
mComponentName));
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
}
private String getShortcutPreferenceKey() {
return KEY_SHORTCUT_PREFERENCE;
}
@Override
public void onCheckboxClicked(ShortcutPreference preference) {
if (mComponentName == null) {
return;
}
final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
if (preference.getChecked()) {
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes,
mComponentName);
} else {
AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), shortcutTypes,
mComponentName);
}
}
@Override
public void onSettingsClicked(ShortcutPreference preference) {
mUserShortcutTypeCache = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
}
} }

View File

@@ -46,7 +46,6 @@ import android.widget.VideoView;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
@@ -54,8 +53,6 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType; import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@@ -64,7 +61,7 @@ import java.util.StringJoiner;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ToggleScreenMagnificationPreferenceFragment extends public class ToggleScreenMagnificationPreferenceFragment extends
ToggleFeaturePreferenceFragment implements ShortcutPreference.OnClickListener { ToggleFeaturePreferenceFragment {
private static final String SETTINGS_KEY = "screen_magnification_settings"; private static final String SETTINGS_KEY = "screen_magnification_settings";
private static final String EXTRA_SHORTCUT_TYPE = "shortcut_type"; private static final String EXTRA_SHORTCUT_TYPE = "shortcut_type";
@@ -172,8 +169,9 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
mPackageName = getString(R.string.accessibility_screen_magnification_title);
mTouchExplorationStateChangeListener = isTouchExplorationEnabled -> { mTouchExplorationStateChangeListener = isTouchExplorationEnabled -> {
removeDialog(DialogType.EDIT_SHORTCUT); removeDialog(DialogEnums.EDIT_SHORTCUT);
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext())); mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
}; };
return super.onCreateView(inflater, container, savedInstanceState); return super.onCreateView(inflater, container, savedInstanceState);
@@ -181,7 +179,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
final PreferenceScreen preferenceScreen = getPreferenceManager().getPreferenceScreen(); final PreferenceScreen preferenceScreen = getPreferenceScreen();
mVideoPreference = new VideoPreference(getPrefContext()); mVideoPreference = new VideoPreference(getPrefContext());
mVideoPreference.setSelectable(false); mVideoPreference.setSelectable(false);
mVideoPreference.setPersistent(false); mVideoPreference.setPersistent(false);
@@ -205,13 +203,6 @@ public class ToggleScreenMagnificationPreferenceFragment extends
preferenceScreen.addPreference(mConfigWarningPreference); preferenceScreen.addPreference(mConfigWarningPreference);
} }
@Override
protected void updateFooterTitle(PreferenceCategory category) {
final String titleText = getString(R.string.accessibility_footer_title,
getString(R.string.accessibility_screen_magnification_title));
category.setTitle(titleText);
}
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
outState.putInt(EXTRA_SHORTCUT_TYPE, mUserShortcutTypeCache); outState.putInt(EXTRA_SHORTCUT_TYPE, mUserShortcutTypeCache);
@@ -248,13 +239,13 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override @Override
public Dialog onCreateDialog(int dialogId) { public Dialog onCreateDialog(int dialogId) {
switch (dialogId) { switch (dialogId) {
case DialogType.GESTURE_NAVIGATION_TUTORIAL: case DialogEnums.GESTURE_NAVIGATION_TUTORIAL:
return AccessibilityGestureNavigationTutorial return AccessibilityGestureNavigationTutorial
.showGestureNavigationTutorialDialog(getActivity()); .showGestureNavigationTutorialDialog(getActivity());
case DialogType.ACCESSIBILITY_BUTTON_TUTORIAL: case DialogEnums.ACCESSIBILITY_BUTTON_TUTORIAL:
return AccessibilityGestureNavigationTutorial return AccessibilityGestureNavigationTutorial
.showAccessibilityButtonTutorialDialog(getActivity()); .showAccessibilityButtonTutorialDialog(getActivity());
case DialogType.EDIT_SHORTCUT: case DialogEnums.MAGNIFICATION_EDIT_SHORTCUT:
final CharSequence dialogTitle = getActivity().getText( final CharSequence dialogTitle = getActivity().getText(
R.string.accessibility_shortcut_edit_dialog_title_magnification); R.string.accessibility_shortcut_edit_dialog_title_magnification);
final AlertDialog dialog = final AlertDialog dialog =
@@ -340,12 +331,12 @@ public class ToggleScreenMagnificationPreferenceFragment extends
info = new HashSet<>(); info = new HashSet<>();
} else { } else {
final Set<String> filtered = info.stream().filter( final Set<String> filtered = info.stream().filter(
str -> str.contains(getComponentName())).collect( str -> str.contains(MAGNIFICATION_CONTROLLER_NAME)).collect(
Collectors.toSet()); Collectors.toSet());
info.removeAll(filtered); info.removeAll(filtered);
} }
final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType( final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
getComponentName(), type); MAGNIFICATION_CONTROLLER_NAME, type);
info.add(shortcut.flattenToString()); info.add(shortcut.flattenToString());
SharedPreferenceUtils.setUserShortcutType(context, info); SharedPreferenceUtils.setUserShortcutType(context, info);
} }
@@ -384,10 +375,11 @@ public class ToggleScreenMagnificationPreferenceFragment extends
return AccessibilityUtil.capitalize(joinStrings); return AccessibilityUtil.capitalize(joinStrings);
} }
private int getUserShortcutType(Context context, @UserShortcutType int defaultValue) { @Override
protected int getUserShortcutType(Context context, @UserShortcutType int defaultValue) {
final Set<String> info = SharedPreferenceUtils.getUserShortcutType(context); final Set<String> info = SharedPreferenceUtils.getUserShortcutType(context);
final Set<String> filtered = info.stream().filter( final Set<String> filtered = info.stream().filter(
str -> str.contains(getComponentName())).collect( str -> str.contains(MAGNIFICATION_CONTROLLER_NAME)).collect(
Collectors.toSet()); Collectors.toSet());
if (filtered.isEmpty()) { if (filtered.isEmpty()) {
return defaultValue; return defaultValue;
@@ -401,17 +393,13 @@ public class ToggleScreenMagnificationPreferenceFragment extends
private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) { private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
updateUserShortcutType(/* saveChanges= */ true); updateUserShortcutType(/* saveChanges= */ true);
if (mShortcutPreference.getChecked()) { if (mShortcutPreference.getChecked()) {
optInAllMagnificationValuesToSettings(getContext(), mUserShortcutType); optInAllMagnificationValuesToSettings(getPrefContext(), mUserShortcutType);
optOutAllMagnificationValuesFromSettings(getContext(), ~mUserShortcutType); optOutAllMagnificationValuesFromSettings(getPrefContext(), ~mUserShortcutType);
} }
mShortcutPreference.setSummary( mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext())); getShortcutTypeSummary(getPrefContext()));
} }
private String getComponentName() {
return MAGNIFICATION_CONTROLLER_NAME;
}
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
// TODO: Distinguish between magnification modes // TODO: Distinguish between magnification modes
@@ -421,11 +409,11 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override @Override
public int getDialogMetricsCategory(int dialogId) { public int getDialogMetricsCategory(int dialogId) {
switch (dialogId) { switch (dialogId) {
case DialogType.GESTURE_NAVIGATION_TUTORIAL: case DialogEnums.GESTURE_NAVIGATION_TUTORIAL:
return SettingsEnums.DIALOG_TOGGLE_SCREEN_MAGNIFICATION_GESTURE_NAVIGATION; return SettingsEnums.DIALOG_TOGGLE_SCREEN_MAGNIFICATION_GESTURE_NAVIGATION;
case DialogType.ACCESSIBILITY_BUTTON_TUTORIAL: case DialogEnums.ACCESSIBILITY_BUTTON_TUTORIAL:
return SettingsEnums.DIALOG_TOGGLE_SCREEN_MAGNIFICATION_ACCESSIBILITY_BUTTON; return SettingsEnums.DIALOG_TOGGLE_SCREEN_MAGNIFICATION_ACCESSIBILITY_BUTTON;
case DialogType.EDIT_SHORTCUT: case DialogEnums.MAGNIFICATION_EDIT_SHORTCUT:
return SettingsEnums.DIALOG_MAGNIFICATION_EDIT_SHORTCUT; return SettingsEnums.DIALOG_MAGNIFICATION_EDIT_SHORTCUT;
default: default:
return 0; return 0;
@@ -438,8 +426,8 @@ public class ToggleScreenMagnificationPreferenceFragment extends
Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
preferenceKey)) { preferenceKey)) {
showDialog(AccessibilityUtil.isGestureNavigateEnabled(getContext()) showDialog(AccessibilityUtil.isGestureNavigateEnabled(getContext())
? DialogType.GESTURE_NAVIGATION_TUTORIAL ? DialogEnums.GESTURE_NAVIGATION_TUTORIAL
: DialogType.ACCESSIBILITY_BUTTON_TUTORIAL); : DialogEnums.ACCESSIBILITY_BUTTON_TUTORIAL);
} }
MagnificationPreferenceFragment.setChecked(getContentResolver(), preferenceKey, enabled); MagnificationPreferenceFragment.setChecked(getContentResolver(), preferenceKey, enabled);
updateConfigurationWarningIfNeeded(); updateConfigurationWarningIfNeeded();
@@ -480,7 +468,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override @Override
public void onSettingsClicked(ShortcutPreference preference) { public void onSettingsClicked(ShortcutPreference preference) {
mUserShortcutTypeCache = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE); mUserShortcutTypeCache = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
showDialog(DialogType.EDIT_SHORTCUT); showDialog(DialogEnums.MAGNIFICATION_EDIT_SHORTCUT);
} }
private void updateShortcutPreferenceData() { private void updateShortcutPreferenceData() {
@@ -532,13 +520,6 @@ public class ToggleScreenMagnificationPreferenceFragment extends
mConfigWarningPreference.setVisible(warningMessage != null); mConfigWarningPreference.setVisible(warningMessage != null);
} }
@Retention(RetentionPolicy.SOURCE)
private @interface DialogType {
int GESTURE_NAVIGATION_TUTORIAL = 1;
int ACCESSIBILITY_BUTTON_TUTORIAL = 2;
int EDIT_SHORTCUT = 3;
}
@VisibleForTesting @VisibleForTesting
static void optInAllMagnificationValuesToSettings(Context context, int shortcutTypes) { static void optInAllMagnificationValuesToSettings(Context context, int shortcutTypes) {
if ((shortcutTypes & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE) { if ((shortcutTypes & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE) {