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

@@ -23,15 +23,20 @@ import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Switch;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.ShortcutType;
import com.android.settings.accessibility.AccessibilityUtil.State;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -48,9 +53,16 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
SwitchBar.OnSwitchChangeListener, ShortcutPreference.OnClickListener {
private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED;
private static final String RADIOPREFERENCE_KEY = "daltonizer_mode_deuteranomaly";
private static final String PREFERENCE_KEY = "daltonizer_mode_deuteranomaly";
private static final String EXTRA_SHORTCUT_TYPE = "shortcutType";
// TODO(b/142530063): Check the new setting key to decide which summary should be shown.
private static final String KEY_SHORTCUT_TYPE = Settings.System.MASTER_MONO;
private static final int DIALOG_ID_EDIT_SHORTCUT = 1;
private static final List<AbstractPreferenceController> sControllers = new ArrayList<>();
private ShortcutPreference mShortcutPreference;
private int mShortcutType = ShortcutType.DEFAULT;
private CheckBox mSoftwareTypeCheckBox;
private CheckBox mHardwareTypeCheckBox;
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle) {
@@ -67,15 +79,6 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
return sControllers;
}
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;
@Override
public void onCheckedChanged(Preference preference) {
for (AbstractPreferenceController controller : sControllers) {
@@ -86,10 +89,16 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
@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();
@@ -115,11 +124,111 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
if (dialogId == DIALOG_ID_EDIT_SHORTCUT) {
final CharSequence dialogTitle = getActivity().getString(
R.string.accessibility_shortcut_edit_dialog_title_daltonizer);
mDialog = AccessibilityEditDialogUtils.showEditShortcutDialog(getActivity(),
dialogTitle, mDialogListener);
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, ShortcutType.SOFTWARE);
updateCheckStatus(mHardwareTypeCheckBox, ShortcutType.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, @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 (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;
// 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
@@ -147,7 +256,7 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
@Override
protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? 0 : 1);
Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? State.OFF : State.ON);
}
@Override
@@ -165,14 +274,14 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
Settings.Secure.putInt(getContentResolver(), ENABLED, isChecked ? 1 : 0);
Settings.Secure.putInt(getContentResolver(), ENABLED, isChecked ? State.ON : State.OFF);
}
@Override
protected void onInstallSwitchBarToggleSwitch() {
super.onInstallSwitchBarToggleSwitch();
mSwitchBar.setCheckedInternal(
Settings.Secure.getInt(getContentResolver(), ENABLED, 0) == 1);
Settings.Secure.getInt(getContentResolver(), ENABLED, State.OFF) == State.ON);
mSwitchBar.addOnSwitchChangeListener(this);
}
@@ -187,22 +296,31 @@ public final class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePrefe
@Override
public void onSettingsClicked(ShortcutPreference preference) {
mShortcutType = getShortcutType(getPrefContext());
showDialog(DIALOG_ID_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);
final RadioButtonPreference radioButtonPreference = findPreference(RADIOPREFERENCE_KEY);
mShortcutPreference.setTitle(R.string.accessibility_shortcut_title);
mShortcutPreference.setOnClickListener(this);
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
// Put the shortcutPreference before radioButtonPreference.
shortcutPreference.setOrder(radioButtonPreference.getOrder() - 1);
shortcutPreference.setTitle(R.string.accessibility_shortcut_title);
// 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.
final RadioButtonPreference radioButtonPreference = findPreference(PREFERENCE_KEY);
mShortcutPreference.setOrder(radioButtonPreference.getOrder() - 1);
preferenceScreen.addPreference(mShortcutPreference);
// TODO(b/142530063): Check the new key to decide whether checkbox should be checked.
shortcutPreference.setOnClickListener(this);
preferenceScreen.addPreference(shortcutPreference);
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =