Support accessibility shortcut secondary action (6/n)

Implements new edit shortcut dialog selection and save what user
prefered key back for color inversion, color correction, magnifiction,
downlaoded service - LEGACY, INVISIBLE, INTUITIVE case.

Bug: 142530063
Bug: 142531156
Test: make RunSettingsRoboTests ROBOTEST_FILTER=AccessibilityUtilTest
Test: make RunSettingsRoboTests2
Change-Id: I94607db918047ae4082457dd33f17c8675934e2c
This commit is contained in:
menghanli
2019-12-30 17:10:22 +08:00
parent 2951e4eddf
commit a5d8e12766
10 changed files with 718 additions and 213 deletions

View File

@@ -16,15 +16,12 @@
package com.android.settings.accessibility;
import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Point;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
@@ -32,41 +29,43 @@ import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Switch;
import android.widget.VideoView;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.ShortcutType;
import com.android.settings.widget.SwitchBar;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
public class ToggleScreenMagnificationPreferenceFragment extends
ToggleFeaturePreferenceFragment implements SwitchBar.OnSwitchChangeListener,
ShortcutPreference.OnClickListener {
private static final String SETTINGS_KEY = "screen_magnification_settings";
private static final int DIALOG_ID_GESTURE_NAVIGATION_TUTORIAL = 1;
private static final int DIALOG_ID_ACCESSIBILITY_BUTTON_TUTORIAL = 2;
private static final int DIALOG_ID_EDIT_SHORTCUT = 3;
private final DialogInterface.OnClickListener mDialogListener =
(DialogInterface dialog, int id) -> {
if (id == DialogInterface.BUTTON_POSITIVE) {
// TODO(b/142531156): Save the shortcut type preference.
}
};
private Dialog mDialog;
private static final String EXTRA_SHORTCUT_TYPE = "shortcutType";
// TODO(b/142530063): Check the new setting key to decide which summary should be shown.
private static final String KEY_SHORTCUT_TYPE = Settings.System.MASTER_MONO;
private ShortcutPreference mShortcutPreference;
private int mShortcutType = ShortcutType.DEFAULT;
private CheckBox mSoftwareTypeCheckBox;
private CheckBox mHardwareTypeCheckBox;
private CheckBox mTripleTapTypeCheckBox;
protected class VideoPreference extends Preference {
private ImageView mVideoBackgroundView;
@@ -147,9 +146,6 @@ public class ToggleScreenMagnificationPreferenceFragment extends
protected VideoPreference mVideoPreference;
protected Preference mConfigWarningPreference;
private boolean mLaunchFromSuw = false;
private boolean mInitialSetting = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -185,10 +181,16 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initShortcutPreference();
initShortcutPreference(savedInstanceState);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(EXTRA_SHORTCUT_TYPE, mShortcutType);
super.onSaveInstanceState(outState);
}
@Override
public void onResume() {
super.onResume();
@@ -204,25 +206,142 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override
public Dialog onCreateDialog(int dialogId) {
switch (dialogId) {
case DIALOG_ID_GESTURE_NAVIGATION_TUTORIAL:
mDialog = AccessibilityGestureNavigationTutorial
case DialogType.GESTURE_NAVIGATION_TUTORIAL:
return AccessibilityGestureNavigationTutorial
.showGestureNavigationTutorialDialog(getActivity());
break;
case DIALOG_ID_ACCESSIBILITY_BUTTON_TUTORIAL:
mDialog = AccessibilityGestureNavigationTutorial
case DialogType.ACCESSIBILITY_BUTTON_TUTORIAL:
return AccessibilityGestureNavigationTutorial
.showAccessibilityButtonTutorialDialog(getActivity());
break;
case DIALOG_ID_EDIT_SHORTCUT:
case DialogType.EDIT_SHORTCUT:
final CharSequence dialogTitle = getActivity().getString(
R.string.accessibility_shortcut_edit_dialog_title_magnification);
mDialog = AccessibilityEditDialogUtils.showMagnificationEditShortcutDialog(
getActivity(), dialogTitle, mDialogListener);
break;
default:
throw new IllegalArgumentException();
final AlertDialog dialog =
AccessibilityEditDialogUtils.showMagnificationEditShortcutDialog(
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);
final View dialogTripleTapView = dialog.findViewById(R.id.triple_tap_shortcut);
mTripleTapTypeCheckBox = dialogTripleTapView.findViewById(R.id.checkbox);
final View advancedView = dialog.findViewById(R.id.advanced_shortcut);
updateAlertDialogCheckState();
updateAlertDialogEnableState();
// Shows the triple tap checkbox directly if clicked.
if (mTripleTapTypeCheckBox.isChecked()) {
advancedView.setVisibility(View.GONE);
dialogTripleTapView.setVisibility(View.VISIBLE);
}
}
private void updateAlertDialogCheckState() {
updateCheckStatus(mSoftwareTypeCheckBox, ShortcutType.SOFTWARE);
updateCheckStatus(mHardwareTypeCheckBox, ShortcutType.HARDWARE);
updateCheckStatus(mTripleTapTypeCheckBox, ShortcutType.TRIPLETAP);
}
private void updateAlertDialogEnableState() {
if (!mSoftwareTypeCheckBox.isChecked() && !mTripleTapTypeCheckBox.isChecked()) {
mHardwareTypeCheckBox.setEnabled(false);
} else if (!mHardwareTypeCheckBox.isChecked() && !mTripleTapTypeCheckBox.isChecked()) {
mSoftwareTypeCheckBox.setEnabled(false);
} else if (!mSoftwareTypeCheckBox.isChecked() && !mHardwareTypeCheckBox.isChecked()) {
mTripleTapTypeCheckBox.setEnabled(false);
} else {
mSoftwareTypeCheckBox.setEnabled(true);
mHardwareTypeCheckBox.setEnabled(true);
mTripleTapTypeCheckBox.setEnabled(true);
}
}
private void updateCheckStatus(CheckBox checkBox, @ShortcutType int type) {
checkBox.setChecked((mShortcutType & type) == type);
checkBox.setOnClickListener(v -> {
updateShortcutType(false);
updateAlertDialogEnableState();
});
}
private void updateShortcutType(boolean saveToDB) {
mShortcutType = ShortcutType.DEFAULT;
if (mSoftwareTypeCheckBox.isChecked()) {
mShortcutType |= ShortcutType.SOFTWARE;
}
if (mHardwareTypeCheckBox.isChecked()) {
mShortcutType |= ShortcutType.HARDWARE;
}
if (mTripleTapTypeCheckBox.isChecked()) {
mShortcutType |= ShortcutType.TRIPLETAP;
}
if (saveToDB) {
setShortcutType(mShortcutType);
}
}
private void setSecureIntValue(String key, @ShortcutType int value) {
Settings.Secure.putIntForUser(getPrefContext().getContentResolver(),
key, value, getPrefContext().getContentResolver().getUserId());
}
private void setShortcutType(@ShortcutType int type) {
setSecureIntValue(KEY_SHORTCUT_TYPE, type);
}
private String getShortcutTypeSummary(Context context) {
final int shortcutType = getShortcutType(context);
final CharSequence softwareTitle =
context.getText(AccessibilityUtil.isGestureNavigateEnabled(context)
? R.string.accessibility_shortcut_edit_dialog_title_software_gesture
: R.string.accessibility_shortcut_edit_dialog_title_software);
List<CharSequence> list = new ArrayList<>();
if ((shortcutType & ShortcutType.SOFTWARE) == ShortcutType.SOFTWARE) {
list.add(softwareTitle);
}
if ((shortcutType & ShortcutType.HARDWARE) == ShortcutType.HARDWARE) {
final CharSequence hardwareTitle = context.getText(
R.string.accessibility_shortcut_edit_dialog_title_hardware);
list.add(hardwareTitle);
}
return mDialog;
if ((shortcutType & ShortcutType.TRIPLETAP) == ShortcutType.TRIPLETAP) {
final CharSequence tripleTapTitle = context.getText(
R.string.accessibility_shortcut_edit_dialog_title_triple_tap);
list.add(tripleTapTitle);
}
// 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);
}
@ShortcutType
private int getShortcutType(Context context) {
return getSecureIntValue(context, KEY_SHORTCUT_TYPE, ShortcutType.SOFTWARE);
}
@ShortcutType
private int getSecureIntValue(Context context, String key, @ShortcutType int defaultValue) {
return Settings.Secure.getIntForUser(
context.getContentResolver(),
key, defaultValue, context.getContentResolver().getUserId());
}
private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
updateShortcutType(true);
mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext()));
}
@Override
@@ -234,11 +353,11 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override
public int getDialogMetricsCategory(int dialogId) {
switch (dialogId) {
case DIALOG_ID_GESTURE_NAVIGATION_TUTORIAL:
case DialogType.GESTURE_NAVIGATION_TUTORIAL:
return SettingsEnums.DIALOG_TOGGLE_SCREEN_MAGNIFICATION_GESTURE_NAVIGATION;
case DIALOG_ID_ACCESSIBILITY_BUTTON_TUTORIAL:
case DialogType.ACCESSIBILITY_BUTTON_TUTORIAL:
return SettingsEnums.DIALOG_TOGGLE_SCREEN_MAGNIFICATION_ACCESSIBILITY_BUTTON;
case DIALOG_ID_EDIT_SHORTCUT:
case DialogType.EDIT_SHORTCUT:
return SettingsEnums.DIALOG_MAGNIFICATION_EDIT_SHORTCUT;
default:
return 0;
@@ -255,8 +374,9 @@ public class ToggleScreenMagnificationPreferenceFragment extends
if (enabled && TextUtils.equals(
Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
preferenceKey)) {
showDialog(isGestureNavigateEnabled() ? DIALOG_ID_GESTURE_NAVIGATION_TUTORIAL
: DIALOG_ID_ACCESSIBILITY_BUTTON_TUTORIAL);
showDialog(AccessibilityUtil.isGestureNavigateEnabled(getContext())
? DialogType.GESTURE_NAVIGATION_TUTORIAL
: DialogType.ACCESSIBILITY_BUTTON_TUTORIAL);
}
MagnificationPreferenceFragment.setChecked(getContentResolver(), preferenceKey, enabled);
updateConfigurationWarningIfNeeded();
@@ -293,20 +413,10 @@ public class ToggleScreenMagnificationPreferenceFragment extends
if (arguments.containsKey(AccessibilitySettings.EXTRA_VIDEO_RAW_RESOURCE_ID)) {
mVideoPreference.setVisible(true);
final int resId = arguments.getInt(
AccessibilitySettings.EXTRA_VIDEO_RAW_RESOURCE_ID);
} else {
mVideoPreference.setVisible(false);
}
if (arguments.containsKey(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW)) {
mLaunchFromSuw = arguments.getBoolean(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW);
}
if (arguments.containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
mInitialSetting = arguments.getBoolean(AccessibilitySettings.EXTRA_CHECKED);
}
if (arguments.containsKey(AccessibilitySettings.EXTRA_TITLE_RES)) {
final int titleRes = arguments.getInt(AccessibilitySettings.EXTRA_TITLE_RES);
if (titleRes > 0) {
@@ -326,27 +436,30 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override
public void onSettingsClicked(ShortcutPreference preference) {
showDialog(DIALOG_ID_EDIT_SHORTCUT);
mShortcutType = getShortcutType(getPrefContext());
showDialog(DialogType.EDIT_SHORTCUT);
}
private void initShortcutPreference() {
private void initShortcutPreference(Bundle savedInstanceState) {
// Restore the Shortcut type
if (savedInstanceState != null) {
mShortcutType = savedInstanceState.getInt(EXTRA_SHORTCUT_TYPE, ShortcutType.DEFAULT);
}
if (mShortcutType == ShortcutType.DEFAULT) {
mShortcutType = getShortcutType(getPrefContext());
}
// Initial ShortcutPreference widget
final PreferenceScreen preferenceScreen = getPreferenceScreen();
final ShortcutPreference shortcutPreference = new ShortcutPreference(
mShortcutPreference = new ShortcutPreference(
preferenceScreen.getContext(), null);
mShortcutPreference.setTitle(R.string.accessibility_magnification_shortcut_title);
mShortcutPreference.setOnClickListener(this);
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
// Put the shortcutPreference before videoPreference.
shortcutPreference.setOrder(mVideoPreference.getOrder() - 1);
shortcutPreference.setTitle(R.string.accessibility_magnification_shortcut_title);
shortcutPreference.setOnClickListener(this);
// TODO(b/142530063): Check the new setting key to decide which summary should be shown.
// TODO(b/142530063): Check if gesture mode is on to decide which summary should be shown.
mShortcutPreference.setOrder(mVideoPreference.getOrder() - 1);
preferenceScreen.addPreference(mShortcutPreference);
// TODO(b/142530063): Check the new key to decide whether checkbox should be checked.
preferenceScreen.addPreference(shortcutPreference);
}
private boolean isGestureNavigateEnabled() {
return getContext().getResources().getInteger(
com.android.internal.R.integer.config_navBarInteractionMode)
== NAV_BAR_MODE_GESTURAL;
}
private void updateConfigurationWarningIfNeeded() {
@@ -359,11 +472,11 @@ public class ToggleScreenMagnificationPreferenceFragment extends
mConfigWarningPreference.setVisible(warningMessage != null);
}
private static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size.x;
@Retention(RetentionPolicy.SOURCE)
private @interface DialogType {
int GESTURE_NAVIGATION_TUTORIAL = 1;
int ACCESSIBILITY_BUTTON_TUTORIAL = 2;
int EDIT_SHORTCUT = 3;
}
}