ShortcutPreference Toggle design.

- For target SDK < 30 app show as preference item to trigger shortcut on/off.
- For target SDK >= 30 app show two item: the left one can edit shortcut and the right one be a on/off trigger button.

The design is reference from WIFI setting which has similar comportment.

Bug: 148989269
Test: Test result in Buganizer.

Change-Id: I815206d9e885c01bf3e6445820176aed4a743323
This commit is contained in:
menghanli
2020-02-18 23:05:38 +08:00
parent 4d59bce21b
commit 919e555a51
9 changed files with 182 additions and 170 deletions

View File

@@ -32,7 +32,7 @@ import com.android.settingslib.accessibility.AccessibilityUtils;
* {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON} is set.
*/
public class InvisibleToggleAccessibilityServicePreferenceFragment extends
ToggleAccessibilityServicePreferenceFragment implements ShortcutPreference.OnClickListener{
ToggleAccessibilityServicePreferenceFragment implements ShortcutPreference.OnClickCallback {
@Override
protected void onInstallSwitchPreferenceToggleSwitch() {
@@ -47,10 +47,10 @@ public class InvisibleToggleAccessibilityServicePreferenceFragment extends
* accessibility service when shortcutPreference is unchecked.
*/
@Override
public void onCheckboxClicked(ShortcutPreference preference) {
super.onCheckboxClicked(preference);
public void onToggleClicked(ShortcutPreference preference) {
super.onToggleClicked(preference);
boolean enabled = getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED)
&& preference.getChecked();
&& preference.isChecked();
AccessibilityUtils.setAccessibilityServiceState(getContext(), mComponentName, enabled);
}
@@ -61,8 +61,8 @@ public class InvisibleToggleAccessibilityServicePreferenceFragment extends
* Enables accessibility service when user clicks permission allow button.
*/
@Override
void onDialogButtonFromShortcutClicked(View view) {
super.onDialogButtonFromShortcutClicked(view);
void onDialogButtonFromShortcutToggleClicked(View view) {
super.onDialogButtonFromShortcutToggleClicked(view);
if (view.getId() == R.id.permission_enable_allow_button) {
AccessibilityUtils.setAccessibilityServiceState(getContext(), mComponentName,
true);

View File

@@ -43,7 +43,7 @@ public class LegacyAccessibilityServicePreferenceFragment extends
final CharSequence hardwareTitle = getPrefContext().getText(
R.string.accessibility_shortcut_edit_dialog_title_hardware);
mShortcutPreference.setSummary(hardwareTitle);
mShortcutPreference.setSettingsVisibility(View.GONE);
mShortcutPreference.setSettingsEditable(false);
setAllowedPreferredShortcutType(UserShortcutType.HARDWARE);
}

View File

@@ -18,9 +18,11 @@ package com.android.settings.accessibility;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Switch;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
@@ -34,77 +36,84 @@ import com.android.settings.R;
public class ShortcutPreference extends Preference {
/**
* Interface definition for a callback to be invoked when the checkbox or settings has been
* Interface definition for a callback to be invoked when the toggle or settings has been
* clicked.
*/
public interface OnClickListener {
/**
* Called when the checkbox in ShortcutPreference has been clicked.
*
* @param preference The clicked preference
*/
void onCheckboxClicked(ShortcutPreference preference);
public interface OnClickCallback {
/**
* Called when the settings view has been clicked.
*
* @param preference The clicked preference
*/
void onSettingsClicked(ShortcutPreference preference);
}
private OnClickListener mListener = null;
private static final float DISABLED_ALPHA = 0.77f;
private static final float ENABLED_ALPHA = 1.0f;
private int mSettingsVisibility = View.VISIBLE;
private boolean mAutoEnabledSettings;
/**
* Called when the toggle in ShortcutPreference has been clicked.
*
* @param preference The clicked preference
*/
void onToggleClicked(ShortcutPreference preference);
}
private OnClickCallback mClickCallback = null;
private boolean mChecked = false;
private boolean mSettingsEditable = true;
ShortcutPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
setLayoutResource(R.layout.accessibility_shortcut_secondary_action);
setWidgetLayoutResource(R.layout.preference_widget_master_switch);
setIconSpaceReserved(true);
setSelectable(false);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
final TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
outValue, true);
final LinearLayout mainFrame = holder.itemView.findViewById(R.id.main_frame);
if (mainFrame != null) {
mainFrame.setOnClickListener(view -> callOnCheckboxClicked());
mainFrame.setOnClickListener(view -> callOnSettingsClicked());
mainFrame.setClickable(mSettingsEditable);
mainFrame.setFocusable(mSettingsEditable);
mainFrame.setBackgroundResource(
mSettingsEditable ? outValue.resourceId : /* Remove background */ 0);
}
final CheckBox checkBox = holder.itemView.findViewById(R.id.checkbox);
if (checkBox != null) {
checkBox.setChecked(mChecked);
}
final View settings = holder.itemView.findViewById(android.R.id.widget_frame);
if (settings != null) {
settings.setOnClickListener(view -> callOnSettingsClicked());
settings.setEnabled(mAutoEnabledSettings ? mChecked : /* enabled */ true);
float alpha;
if (mAutoEnabledSettings) {
alpha = mChecked ? ENABLED_ALPHA : DISABLED_ALPHA;
} else {
alpha = ENABLED_ALPHA;
}
settings.setAlpha(alpha);
settings.setVisibility(mSettingsVisibility);
Switch switchWidget = holder.itemView.findViewById(R.id.switchWidget);
if (switchWidget != null) {
// Consumes move events to ignore drag actions.
switchWidget.setOnTouchListener((v, event) -> {
return event.getActionMasked() == MotionEvent.ACTION_MOVE;
});
switchWidget.setContentDescription(
getContext().getText(R.string.accessibility_shortcut_settings));
switchWidget.setChecked(mChecked);
switchWidget.setOnClickListener(view -> callOnToggleClicked());
switchWidget.setClickable(mSettingsEditable);
switchWidget.setFocusable(mSettingsEditable);
switchWidget.setBackgroundResource(
mSettingsEditable ? outValue.resourceId : /* Remove background */ 0);
}
final View divider = holder.itemView.findViewById(R.id.divider);
if (divider != null) {
divider.setVisibility(mSettingsVisibility);
divider.setVisibility(mSettingsEditable ? View.VISIBLE : View.GONE);
}
holder.itemView.setOnClickListener(view -> callOnToggleClicked());
holder.itemView.setClickable(!mSettingsEditable);
holder.itemView.setFocusable(!mSettingsEditable);
}
/**
* Sets the shortcut checkbox according to checked value.
* Sets the shortcut toggle according to checked value.
*
* @param checked the state value of shortcut checkbox
* @param checked the state value of shortcut toggle
*/
public void setChecked(boolean checked) {
if (mChecked != checked) {
@@ -114,72 +123,50 @@ public class ShortcutPreference extends Preference {
}
/**
* Gets the checked value of shortcut checkbox.
* Gets the checked value of shortcut toggle.
*
* @return the checked value of shortcut checkbox
* @return the checked value of shortcut toggle
*/
public boolean getChecked() {
public boolean isChecked() {
return mChecked;
}
/**
* Automatically/Manually enable settings according to checkbox click status.
*
* Automatically enable settings means settings view enabled when checkbox is clicked, and
* disabled when checkbox is not clicked.
* Manually enable settings means settings view always enabled.
*
* @param autoEnabled True will automatically enable settings, false will let settings view
* always enabled.
* Sets the editable state of Settings view. If the view cannot edited, it makes the settings
* and toggle be not touchable. The main ui handles touch event directly by {@link #onClick}.
*/
public void setAutoEnabledSettings(boolean autoEnabled) {
if (mAutoEnabledSettings != autoEnabled) {
mAutoEnabledSettings = autoEnabled;
public void setSettingsEditable(boolean enabled) {
if (mSettingsEditable != enabled) {
mSettingsEditable = enabled;
// Disable whole component to let each child component can be addressed.
setSelectable(!mSettingsEditable);
notifyChanged();
}
}
/**
* Sets the visibility state of Settings view.
*
* @param visibility one of {@link View#VISIBLE}, {@link View#INVISIBLE}, or {@link View#GONE}.
*/
public void setSettingsVisibility(@View.Visibility int visibility) {
if (mSettingsVisibility != visibility) {
mSettingsVisibility = visibility;
notifyChanged();
}
public boolean isSettingsEditable() {
return mSettingsEditable;
}
/**
* Sets the callback to be invoked when this preference is clicked by the user.
*
* @param listener the callback to be invoked
* @param callback the callback to be invoked
*/
public void setOnClickListener(OnClickListener listener) {
mListener = listener;
}
private void init() {
setLayoutResource(R.layout.accessibility_shortcut_secondary_action);
setWidgetLayoutResource(R.layout.preference_widget_settings);
setIconSpaceReserved(false);
mAutoEnabledSettings = true;
// Disable whole component to let each child component can be addressed.
setSelectable(false);
public void setOnClickCallback(OnClickCallback callback) {
mClickCallback = callback;
}
private void callOnSettingsClicked() {
if (mListener != null) {
mListener.onSettingsClicked(this);
if (mClickCallback != null) {
mClickCallback.onSettingsClicked(this);
}
}
private void callOnCheckboxClicked() {
private void callOnToggleClicked() {
setChecked(!mChecked);
if (mListener != null) {
mListener.onCheckboxClicked(this);
if (mClickCallback != null) {
mClickCallback.onToggleClicked(this);
}
}
}

View File

@@ -134,6 +134,16 @@ public class ToggleAccessibilityServicePreferenceFragment extends
this::onDialogButtonFromEnableToggleClicked);
break;
}
case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT_TOGGLE: {
final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
if (info == null) {
return null;
}
mDialog = AccessibilityServiceWarning
.createCapabilitiesDialog(getPrefContext(), info,
this::onDialogButtonFromShortcutToggleClicked);
break;
}
case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT: {
final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
if (info == null) {
@@ -176,6 +186,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
switch (dialogId) {
case DialogEnums.ENABLE_WARNING_FROM_TOGGLE:
case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT:
case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT_TOGGLE:
return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_ENABLE;
case DialogEnums.DISABLE_WARNING_FROM_TOGGLE:
return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_DISABLE;
@@ -283,12 +294,12 @@ public class ToggleAccessibilityServicePreferenceFragment extends
}
@Override
public void onCheckboxClicked(ShortcutPreference preference) {
public void onToggleClicked(ShortcutPreference preference) {
final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
if (preference.getChecked()) {
if (!getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED)) {
if (preference.isChecked()) {
if (!mToggleServiceDividerSwitchPreference.isChecked()) {
preference.setChecked(false);
showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_SHORTCUT);
showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_SHORTCUT_TOGGLE);
} else {
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes,
mComponentName);
@@ -296,14 +307,17 @@ public class ToggleAccessibilityServicePreferenceFragment extends
} else {
AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), shortcutTypes,
mComponentName);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, false);
}
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
}
@Override
public void onSettingsClicked(ShortcutPreference preference) {
super.onSettingsClicked(preference);
showPopupDialog(DialogEnums.EDIT_SHORTCUT);
final boolean isServiceOnOrShortcutAdded = mShortcutPreference.isChecked()
|| mToggleServiceDividerSwitchPreference.isChecked();
showPopupDialog(isServiceOnOrShortcutAdded ? DialogEnums.EDIT_SHORTCUT
: DialogEnums.ENABLE_WARNING_FROM_SHORTCUT);
}
@Override
@@ -385,6 +399,32 @@ public class ToggleAccessibilityServicePreferenceFragment extends
mDialog.dismiss();
}
void onDialogButtonFromShortcutToggleClicked(View view) {
final int viewId = view.getId();
if (viewId == R.id.permission_enable_allow_button) {
onAllowButtonFromShortcutToggleClicked();
} else if (viewId == R.id.permission_enable_deny_button) {
onDenyButtonFromShortcutToggleClicked();
} else {
throw new IllegalArgumentException("Unexpected view id");
}
}
private void onAllowButtonFromShortcutToggleClicked() {
mShortcutPreference.setChecked(true);
final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes, mComponentName);
mDialog.dismiss();
}
private void onDenyButtonFromShortcutToggleClicked() {
mShortcutPreference.setChecked(false);
mDialog.dismiss();
}
void onDialogButtonFromShortcutClicked(View view) {
final int viewId = view.getId();
if (viewId == R.id.permission_enable_allow_button) {
@@ -397,17 +437,13 @@ public class ToggleAccessibilityServicePreferenceFragment extends
}
private void onAllowButtonFromShortcutClicked() {
mShortcutPreference.setChecked(true);
final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes, mComponentName);
mIsDialogShown.set(false);
showPopupDialog(DialogEnums.EDIT_SHORTCUT);
mDialog.dismiss();
}
private void onDenyButtonFromShortcutClicked() {
mShortcutPreference.setChecked(false);
mDialog.dismiss();
}
@@ -417,7 +453,7 @@ public class ToggleAccessibilityServicePreferenceFragment extends
mToggleServiceDividerSwitchPreference.setChecked(false);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED,
/* disableService */ false);
if (!mShortcutPreference.getChecked()) {
if (!mShortcutPreference.isChecked()) {
showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_TOGGLE);
} else {
handleConfirmServiceEnabled(/* confirmed= */ true);

View File

@@ -69,7 +69,7 @@ import java.util.stream.Collectors;
* and dialog management.
*/
public abstract class ToggleFeaturePreferenceFragment extends SettingsPreferenceFragment
implements ShortcutPreference.OnClickListener {
implements ShortcutPreference.OnClickCallback {
protected DividerSwitchPreference mToggleServiceDividerSwitchPreference;
protected ShortcutPreference mShortcutPreference;
@@ -277,33 +277,38 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
*/
int ENABLE_WARNING_FROM_TOGGLE = 1002;
/** OPEN: Settings > Accessibility > Downloaded toggle service > Shortcut checkbox. */
int ENABLE_WARNING_FROM_SHORTCUT = 1003;
/**
* OPEN: Settings > Accessibility > Downloaded toggle service > Shortcut checkbox
* toggle.
*/
int ENABLE_WARNING_FROM_SHORTCUT_TOGGLE = 1004;
/**
* OPEN: Settings > Accessibility > Downloaded toggle service > Toggle use service to
* disable service.
*/
int DISABLE_WARNING_FROM_TOGGLE = 1004;
int DISABLE_WARNING_FROM_TOGGLE = 1005;
/**
* OPEN: Settings > Accessibility > Magnification > Toggle user service in button
* navigation.
*/
int ACCESSIBILITY_BUTTON_TUTORIAL = 1005;
int ACCESSIBILITY_BUTTON_TUTORIAL = 1006;
/**
* OPEN: Settings > Accessibility > Magnification > Toggle user service in gesture
* navigation.
*/
int GESTURE_NAVIGATION_TUTORIAL = 1006;
int GESTURE_NAVIGATION_TUTORIAL = 1007;
/**
* OPEN: Settings > Accessibility > Downloaded toggle service > Toggle user service > Show
* launch tutorial.
*/
int LAUNCH_ACCESSIBILITY_TUTORIAL = 1007;
int LAUNCH_ACCESSIBILITY_TUTORIAL = 1008;
}
@Override
@@ -516,7 +521,15 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
SharedPreferenceUtils.setUserShortcutType(context, info);
}
private String getShortcutTypeSummary(Context context) {
protected CharSequence getShortcutTypeSummary(Context context) {
if (!mShortcutPreference.isSettingsEditable()) {
return context.getText(R.string.accessibility_shortcut_edit_dialog_title_hardware);
}
if (!mShortcutPreference.isChecked()) {
return context.getText(R.string.switch_off_text);
}
final int shortcutType = getUserShortcutType(context, UserShortcutType.SOFTWARE);
int resId = R.string.accessibility_shortcut_edit_summary_software;
if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
@@ -569,12 +582,13 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
}
updateUserShortcutType(/* saveChanges= */ true);
if (mShortcutPreference.getChecked()) {
if (mShortcutPreference.isChecked()) {
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), mUserShortcutType,
mComponentName);
AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), ~mUserShortcutType,
mComponentName);
}
mShortcutPreference.setChecked(true);
mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext()));
}
@@ -606,7 +620,7 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
mShortcutPreference = new ShortcutPreference(getPrefContext(), null);
mShortcutPreference.setPersistent(false);
mShortcutPreference.setKey(getShortcutPreferenceKey());
mShortcutPreference.setOnClickListener(this);
mShortcutPreference.setOnClickCallback(this);
final CharSequence title = getString(R.string.accessibility_shortcut_title, mPackageName);
mShortcutPreference.setTitle(title);
@@ -629,19 +643,20 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
}
@Override
public void onCheckboxClicked(ShortcutPreference preference) {
public void onToggleClicked(ShortcutPreference preference) {
if (mComponentName == null) {
return;
}
final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
if (preference.getChecked()) {
if (preference.isChecked()) {
AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes,
mComponentName);
} else {
AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), shortcutTypes,
mComponentName);
}
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
}
@Override

View File

@@ -330,7 +330,12 @@ public class ToggleScreenMagnificationPreferenceFragment extends
SharedPreferenceUtils.setUserShortcutType(context, info);
}
private String getShortcutTypeSummary(Context context) {
@Override
protected CharSequence getShortcutTypeSummary(Context context) {
if (!mShortcutPreference.isChecked()) {
return context.getText(R.string.switch_off_text);
}
final int shortcutType = getUserShortcutType(context, UserShortcutType.DEFAULT);
int resId = R.string.accessibility_shortcut_edit_summary_software;
if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
@@ -381,10 +386,11 @@ public class ToggleScreenMagnificationPreferenceFragment extends
private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
updateUserShortcutType(/* saveChanges= */ true);
if (mShortcutPreference.getChecked()) {
if (mShortcutPreference.isChecked()) {
optInAllMagnificationValuesToSettings(getPrefContext(), mUserShortcutType);
optOutAllMagnificationValuesFromSettings(getPrefContext(), ~mUserShortcutType);
}
mShortcutPreference.setChecked(true);
mShortcutPreference.setSummary(
getShortcutTypeSummary(getPrefContext()));
}
@@ -442,13 +448,14 @@ public class ToggleScreenMagnificationPreferenceFragment extends
}
@Override
public void onCheckboxClicked(ShortcutPreference preference) {
public void onToggleClicked(ShortcutPreference preference) {
final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
if (preference.getChecked()) {
if (preference.isChecked()) {
optInAllMagnificationValuesToSettings(getPrefContext(), shortcutTypes);
} else {
optOutAllMagnificationValuesFromSettings(getPrefContext(), shortcutTypes);
}
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
}
@Override
@@ -473,7 +480,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
mShortcutPreference.setPersistent(false);
mShortcutPreference.setKey(KEY_SHORTCUT_PREFERENCE);
mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
mShortcutPreference.setOnClickListener(this);
mShortcutPreference.setOnClickCallback(this);
final CharSequence title = getString(R.string.accessibility_shortcut_title, mPackageName);
mShortcutPreference.setTitle(title);