a11y: Clean up magnification mode controller and fix lint errors
This cleans up the magnification mode controller and related code. It also fixes most of the lint errors in the files. NO_IFTTT=linter not working Bug: b/388335935 Flag: EXEMPT refactor Test: com.android.settings.accessibility.ToggleScreenMagnificationPreferenceFragmentTest & com.android.settings.accessibility.MagnificationModePreferenceControllerTest Change-Id: I368128c3c71285a9511a7831e1d01232e7536d2f
This commit is contained in:
@@ -37,11 +37,13 @@ import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.util.Preconditions;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.DialogCreatable;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.accessibility.AccessibilityDialogUtils.DialogEnums;
|
||||
import com.android.settings.accessibility.MagnificationCapabilities.MagnificationMode;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.utils.AnnotationSpan;
|
||||
@@ -57,33 +59,37 @@ public class MagnificationModePreferenceController extends BasePreferenceControl
|
||||
DialogCreatable, LifecycleObserver, OnCreate, OnSaveInstanceState {
|
||||
|
||||
static final String PREF_KEY = "screen_magnification_mode";
|
||||
private static final int DIALOG_ID_BASE = 10;
|
||||
@VisibleForTesting
|
||||
static final int DIALOG_MAGNIFICATION_MODE = DIALOG_ID_BASE + 1;
|
||||
@VisibleForTesting
|
||||
static final int DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING = DIALOG_ID_BASE + 2;
|
||||
@VisibleForTesting
|
||||
static final String EXTRA_MODE = "mode";
|
||||
|
||||
private static final String TAG = "MagnificationModePreferenceController";
|
||||
private static final String TAG = MagnificationModePreferenceController.class.getSimpleName();
|
||||
|
||||
@Nullable
|
||||
private DialogHelper mDialogHelper;
|
||||
// The magnification mode in the dialog.
|
||||
@MagnificationMode
|
||||
private int mModeCache = MagnificationMode.NONE;
|
||||
@Nullable
|
||||
private Preference mModePreference;
|
||||
@Nullable
|
||||
private ShortcutPreference mLinkPreference;
|
||||
|
||||
@VisibleForTesting
|
||||
@Nullable
|
||||
ListView mMagnificationModesListView;
|
||||
|
||||
private final List<MagnificationModeInfo> mModeInfos = new ArrayList<>();
|
||||
|
||||
public MagnificationModePreferenceController(Context context, String preferenceKey) {
|
||||
public MagnificationModePreferenceController(@NonNull Context context,
|
||||
@NonNull String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
initModeInfos();
|
||||
}
|
||||
|
||||
|
||||
public void setDialogHelper(@NonNull DialogHelper dialogHelper) {
|
||||
mDialogHelper = dialogHelper;
|
||||
}
|
||||
|
||||
private void initModeInfos() {
|
||||
mModeInfos.add(new MagnificationModeInfo(mContext.getText(
|
||||
R.string.accessibility_magnification_mode_dialog_option_full_screen), null,
|
||||
@@ -103,6 +109,7 @@ public class MagnificationModePreferenceController extends BasePreferenceControl
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
final int capabilities = MagnificationCapabilities.getCapabilities(mContext);
|
||||
@@ -110,99 +117,98 @@ public class MagnificationModePreferenceController extends BasePreferenceControl
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
if (savedInstanceState != null) {
|
||||
mModeCache = savedInstanceState.getInt(EXTRA_MODE, MagnificationMode.NONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
public void displayPreference(@NonNull PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mModePreference = screen.findPreference(getPreferenceKey());
|
||||
mLinkPreference = screen.findPreference(
|
||||
ToggleFeaturePreferenceFragment.KEY_SHORTCUT_PREFERENCE);
|
||||
mModePreference.setOnPreferenceClickListener(preference -> {
|
||||
Preconditions.checkNotNull(mModePreference).setOnPreferenceClickListener(preference -> {
|
||||
mModeCache = MagnificationCapabilities.getCapabilities(mContext);
|
||||
mDialogHelper.showDialog(DIALOG_MAGNIFICATION_MODE);
|
||||
Preconditions.checkNotNull(mDialogHelper).showDialog(
|
||||
DialogEnums.DIALOG_MAGNIFICATION_MODE);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
outState.putInt(EXTRA_MODE, mModeCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets {@link DialogHelper} used to show the dialog.
|
||||
*/
|
||||
public void setDialogHelper(DialogHelper dialogHelper) {
|
||||
mDialogHelper = dialogHelper;
|
||||
mDialogHelper.setDialogDelegate(this);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(int dialogId) {
|
||||
switch (dialogId) {
|
||||
case DIALOG_MAGNIFICATION_MODE:
|
||||
return createMagnificationModeDialog();
|
||||
|
||||
case DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING:
|
||||
return createMagnificationTripleTapWarningDialog();
|
||||
}
|
||||
return null;
|
||||
return switch (dialogId) {
|
||||
case DialogEnums.DIALOG_MAGNIFICATION_MODE -> createMagnificationModeDialog();
|
||||
case DialogEnums.DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING ->
|
||||
createMagnificationTripleTapWarningDialog();
|
||||
default -> throw new IllegalArgumentException(
|
||||
"This only handles magnification mode and triple tap warning dialog");
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDialogMetricsCategory(int dialogId) {
|
||||
switch (dialogId) {
|
||||
case DIALOG_MAGNIFICATION_MODE:
|
||||
return SettingsEnums.DIALOG_MAGNIFICATION_CAPABILITY;
|
||||
case DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING:
|
||||
return SettingsEnums.DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return switch (dialogId) {
|
||||
case DialogEnums.DIALOG_MAGNIFICATION_MODE ->
|
||||
SettingsEnums.DIALOG_MAGNIFICATION_CAPABILITY;
|
||||
case DialogEnums.DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING ->
|
||||
SettingsEnums.DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING;
|
||||
default -> 0;
|
||||
};
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private ListView getMagnificationModesListView() {
|
||||
return Preconditions.checkNotNull(mMagnificationModesListView);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Dialog createMagnificationModeDialog() {
|
||||
mMagnificationModesListView = AccessibilityDialogUtils.createSingleChoiceListView(
|
||||
mContext, mModeInfos, this::onMagnificationModeSelected);
|
||||
|
||||
final View headerView = LayoutInflater.from(mContext).inflate(
|
||||
R.layout.accessibility_magnification_mode_header, mMagnificationModesListView,
|
||||
false);
|
||||
mMagnificationModesListView.addHeaderView(headerView, /* data= */ null, /* isSelectable= */
|
||||
false);
|
||||
R.layout.accessibility_magnification_mode_header,
|
||||
getMagnificationModesListView(), /* attachToRoot= */false);
|
||||
getMagnificationModesListView().addHeaderView(headerView, /* data= */null,
|
||||
/* isSelectable= */false);
|
||||
|
||||
mMagnificationModesListView.setItemChecked(computeSelectionIndex(), true);
|
||||
getMagnificationModesListView().setItemChecked(computeSelectionIndex(), /* value= */true);
|
||||
final CharSequence title = mContext.getString(
|
||||
R.string.accessibility_magnification_mode_dialog_title);
|
||||
final CharSequence positiveBtnText = mContext.getString(R.string.save);
|
||||
final CharSequence negativeBtnText = mContext.getString(R.string.cancel);
|
||||
|
||||
return AccessibilityDialogUtils.createCustomDialog(mContext, title,
|
||||
mMagnificationModesListView,
|
||||
positiveBtnText, this::onMagnificationModeDialogPositiveButtonClicked,
|
||||
negativeBtnText, /* negativeListener= */ null);
|
||||
getMagnificationModesListView(), positiveBtnText,
|
||||
this::onMagnificationModeDialogPositiveButtonClicked,
|
||||
negativeBtnText, /* negativeListener= */null);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void onMagnificationModeDialogPositiveButtonClicked(DialogInterface dialogInterface,
|
||||
void onMagnificationModeDialogPositiveButtonClicked(@NonNull DialogInterface dialogInterface,
|
||||
int which) {
|
||||
final int selectedIndex = mMagnificationModesListView.getCheckedItemPosition();
|
||||
final int selectedIndex = getMagnificationModesListView().getCheckedItemPosition();
|
||||
if (selectedIndex == AdapterView.INVALID_POSITION) {
|
||||
Log.w(TAG, "invalid index");
|
||||
Log.w(TAG, "Selected positive button with INVALID_POSITION index");
|
||||
return;
|
||||
}
|
||||
|
||||
mModeCache = ((MagnificationModeInfo) mMagnificationModesListView.getItemAtPosition(
|
||||
selectedIndex)).mMagnificationMode;
|
||||
mModeCache = ((MagnificationModeInfo) getMagnificationModesListView().getItemAtPosition(
|
||||
selectedIndex)).mMagnificationMode;
|
||||
|
||||
// Do not save mode until user clicks positive button in triple tap warning dialog.
|
||||
if (isTripleTapEnabled(mContext) && mModeCache != MagnificationMode.FULLSCREEN) {
|
||||
mDialogHelper.showDialog(DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING);
|
||||
Preconditions.checkNotNull(mDialogHelper).showDialog(
|
||||
DialogEnums.DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING);
|
||||
} else { // Save mode (capabilities) value, don't need to show dialog to confirm.
|
||||
updateCapabilitiesAndSummary(mModeCache);
|
||||
}
|
||||
@@ -211,15 +217,14 @@ public class MagnificationModePreferenceController extends BasePreferenceControl
|
||||
private void updateCapabilitiesAndSummary(@MagnificationMode int mode) {
|
||||
mModeCache = mode;
|
||||
MagnificationCapabilities.setCapabilities(mContext, mModeCache);
|
||||
mModePreference.setSummary(
|
||||
Preconditions.checkNotNull(mModePreference).setSummary(
|
||||
MagnificationCapabilities.getSummary(mContext, mModeCache));
|
||||
}
|
||||
|
||||
private void onMagnificationModeSelected(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
private void onMagnificationModeSelected(@NonNull AdapterView<?> parent, @NonNull View view,
|
||||
int position, long id) {
|
||||
final MagnificationModeInfo modeInfo =
|
||||
(MagnificationModeInfo) mMagnificationModesListView.getItemAtPosition(
|
||||
position);
|
||||
(MagnificationModeInfo) getMagnificationModesListView().getItemAtPosition(position);
|
||||
if (modeInfo.mMagnificationMode == mModeCache) {
|
||||
return;
|
||||
}
|
||||
@@ -230,20 +235,22 @@ public class MagnificationModePreferenceController extends BasePreferenceControl
|
||||
final int modesSize = mModeInfos.size();
|
||||
for (int i = 0; i < modesSize; i++) {
|
||||
if (mModeInfos.get(i).mMagnificationMode == mModeCache) {
|
||||
return i + mMagnificationModesListView.getHeaderViewsCount();
|
||||
return i + getMagnificationModesListView().getHeaderViewsCount();
|
||||
}
|
||||
}
|
||||
Log.w(TAG, "computeSelectionIndex failed");
|
||||
Log.w(TAG, "Can not find matching mode in mModeInfos");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static boolean isTripleTapEnabled(Context context) {
|
||||
static boolean isTripleTapEnabled(@NonNull Context context) {
|
||||
return Settings.Secure.getInt(context.getContentResolver(),
|
||||
Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, OFF) == ON;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Dialog createMagnificationTripleTapWarningDialog() {
|
||||
@SuppressWarnings({"InflateParams"})
|
||||
final View contentView = LayoutInflater.from(mContext).inflate(
|
||||
R.layout.magnification_triple_tap_warning_dialog, /* root= */ null);
|
||||
final CharSequence title = mContext.getString(
|
||||
@@ -263,12 +270,13 @@ public class MagnificationModePreferenceController extends BasePreferenceControl
|
||||
return dialog;
|
||||
}
|
||||
|
||||
private void updateLinkInTripleTapWarningDialog(Dialog dialog, View contentView) {
|
||||
private void updateLinkInTripleTapWarningDialog(@NonNull Dialog dialog,
|
||||
@NonNull View contentView) {
|
||||
final TextView messageView = contentView.findViewById(R.id.message);
|
||||
// TODO(b/225682559): Need to remove performClick() after refactoring accessibility dialog.
|
||||
final View.OnClickListener linkListener = view -> {
|
||||
updateCapabilitiesAndSummary(mModeCache);
|
||||
mLinkPreference.performClick();
|
||||
Preconditions.checkNotNull(mLinkPreference).performClick();
|
||||
dialog.dismiss();
|
||||
};
|
||||
final AnnotationSpan.LinkInfo linkInfo = new AnnotationSpan.LinkInfo(
|
||||
@@ -285,25 +293,18 @@ public class MagnificationModePreferenceController extends BasePreferenceControl
|
||||
|
||||
@VisibleForTesting
|
||||
void onMagnificationTripleTapWarningDialogNegativeButtonClicked(
|
||||
DialogInterface dialogInterface, int which) {
|
||||
@NonNull DialogInterface dialogInterface, int which) {
|
||||
mModeCache = MagnificationCapabilities.getCapabilities(mContext);
|
||||
mDialogHelper.showDialog(DIALOG_MAGNIFICATION_MODE);
|
||||
Preconditions.checkNotNull(mDialogHelper).showDialog(
|
||||
DialogEnums.DIALOG_MAGNIFICATION_MODE);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void onMagnificationTripleTapWarningDialogPositiveButtonClicked(
|
||||
DialogInterface dialogInterface, int which) {
|
||||
@NonNull DialogInterface dialogInterface, int which) {
|
||||
updateCapabilitiesAndSummary(mModeCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface to help the delegate to show the dialog. It will be injected to the delegate.
|
||||
*/
|
||||
interface DialogHelper extends DialogCreatable {
|
||||
void showDialog(int dialogId);
|
||||
void setDialogDelegate(DialogCreatable delegate);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static class MagnificationModeInfo extends ItemInfoArrayAdapter.ItemInfo {
|
||||
@MagnificationMode
|
||||
|
||||
Reference in New Issue
Block a user