Fix software shortcut in dialog did not update when content change.

Root Cause: Dialog only get updated when clicked the trigger button to show dialog.

Solution: Update the software shortcut content in dialog if needed in onResume() stage.

Bug: 183342594
Test: atest AccessibilityDialogUtilsTest
Change-Id: Ibcd66d6c57f1790f96f234e49f456cbc2c990f64
This commit is contained in:
jasonwshsu
2021-10-08 01:58:38 +08:00
parent 65022abe43
commit eba8e8da19
4 changed files with 111 additions and 17 deletions

View File

@@ -180,6 +180,23 @@ public class AccessibilityDialogUtils {
return alertDialog; return alertDialog;
} }
/**
* Updates the software shortcut in edit shortcut dialog.
*
* @param context A valid context
* @param editShortcutDialog Need to be a type of edit shortcut dialog
* @return True if the update is successful
*/
public static boolean updateSoftwareShortcutInDialog(Context context,
Dialog editShortcutDialog) {
final View container = editShortcutDialog.findViewById(R.id.container_layout);
if (container != null) {
initSoftwareShortcut(context, container);
return true;
}
return false;
}
private static AlertDialog createDialog(Context context, int dialogType, private static AlertDialog createDialog(Context context, int dialogType,
CharSequence dialogTitle, DialogInterface.OnClickListener listener) { CharSequence dialogTitle, DialogInterface.OnClickListener listener) {

View File

@@ -79,6 +79,7 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
protected Preference mSettingsPreference; protected Preference mSettingsPreference;
protected AccessibilityFooterPreferenceController mFooterPreferenceController; protected AccessibilityFooterPreferenceController mFooterPreferenceController;
protected String mPreferenceKey; protected String mPreferenceKey;
protected Dialog mDialog;
protected CharSequence mSettingsTitle; protected CharSequence mSettingsTitle;
protected Intent mSettingsIntent; protected Intent mSettingsIntent;
@@ -106,6 +107,7 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
public static final int NOT_SET = -1; public static final int NOT_SET = -1;
// Save user's shortcutType value when savedInstance has value (e.g. device rotated). // Save user's shortcutType value when savedInstance has value (e.g. device rotated).
protected int mSavedCheckBoxValue = NOT_SET; protected int mSavedCheckBoxValue = NOT_SET;
private boolean mSavedAccessibilityFloatingMenuEnabled;
// For html description of accessibility service, must follow the rule, such as // For html description of accessibility service, must follow the rule, such as
// <img src="R.drawable.fileName"/>, a11y settings will get the resources successfully. // <img src="R.drawable.fileName"/>, a11y settings will get the resources successfully.
@@ -127,7 +129,6 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Restore the user shortcut type. // Restore the user shortcut type.
if (savedInstanceState != null && savedInstanceState.containsKey( if (savedInstanceState != null && savedInstanceState.containsKey(
KEY_SAVED_USER_SHORTCUT_TYPE)) { KEY_SAVED_USER_SHORTCUT_TYPE)) {
@@ -200,6 +201,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
mSettingsContentObserver.register(getContentResolver()); mSettingsContentObserver.register(getContentResolver());
updateShortcutPreferenceData(); updateShortcutPreferenceData();
updateShortcutPreference(); updateShortcutPreference();
updateEditShortcutDialogIfNeeded();
} }
@Override @Override
@@ -208,6 +211,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
AccessibilityManager.class); AccessibilityManager.class);
am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener); am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
mSettingsContentObserver.unregister(getContentResolver()); mSettingsContentObserver.unregister(getContentResolver());
mSavedAccessibilityFloatingMenuEnabled = AccessibilityUtil.isFloatingMenuEnabled(
getContext());
super.onPause(); super.onPause();
} }
@@ -222,24 +227,23 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
@Override @Override
public Dialog onCreateDialog(int dialogId) { public Dialog onCreateDialog(int dialogId) {
Dialog dialog;
switch (dialogId) { switch (dialogId) {
case DialogEnums.EDIT_SHORTCUT: case DialogEnums.EDIT_SHORTCUT:
final CharSequence dialogTitle = getPrefContext().getString( final CharSequence dialogTitle = getPrefContext().getString(
R.string.accessibility_shortcut_title, mPackageName); R.string.accessibility_shortcut_title, mPackageName);
final int dialogType = WizardManagerHelper.isAnySetupWizard(getIntent()) final int dialogType = WizardManagerHelper.isAnySetupWizard(getIntent())
? DialogType.EDIT_SHORTCUT_GENERIC_SUW : DialogType.EDIT_SHORTCUT_GENERIC; ? DialogType.EDIT_SHORTCUT_GENERIC_SUW : DialogType.EDIT_SHORTCUT_GENERIC;
dialog = AccessibilityDialogUtils.showEditShortcutDialog( mDialog = AccessibilityDialogUtils.showEditShortcutDialog(
getPrefContext(), dialogType, dialogTitle, getPrefContext(), dialogType, dialogTitle,
this::callOnAlertDialogCheckboxClicked); this::callOnAlertDialogCheckboxClicked);
setupEditShortcutDialog(dialog); setupEditShortcutDialog(mDialog);
return dialog; return mDialog;
case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL: case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL:
dialog = AccessibilityGestureNavigationTutorial mDialog = AccessibilityGestureNavigationTutorial
.createAccessibilityTutorialDialog(getPrefContext(), .createAccessibilityTutorialDialog(getPrefContext(),
getUserShortcutTypes()); getUserShortcutTypes());
dialog.setCanceledOnTouchOutside(false); mDialog.setCanceledOnTouchOutside(false);
return dialog; return mDialog;
default: default:
throw new IllegalArgumentException("Unsupported dialogId " + dialogId); throw new IllegalArgumentException("Unsupported dialogId " + dialogId);
} }
@@ -733,6 +737,20 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
} }
} }
private void updateEditShortcutDialogIfNeeded() {
if (mDialog == null || !mDialog.isShowing()) {
return;
}
// Content in software shortcut need to be adjusted depend on the accessibility button
// mode status which can be changed in background.
final boolean valueChanged = mSavedAccessibilityFloatingMenuEnabled
!= AccessibilityUtil.isFloatingMenuEnabled(getContext());
if (valueChanged) {
AccessibilityDialogUtils.updateSoftwareShortcutInDialog(getContext(), mDialog);
}
}
@VisibleForTesting @VisibleForTesting
void saveNonEmptyUserShortcutType(int type) { void saveNonEmptyUserShortcutType(int type) {
if (type == UserShortcutType.EMPTY) { if (type == UserShortcutType.EMPTY) {

View File

@@ -38,7 +38,6 @@ import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener; import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
import android.widget.CheckBox; import android.widget.CheckBox;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
@@ -132,12 +131,11 @@ public class ToggleScreenMagnificationPreferenceFragment extends
@Override @Override
public Dialog onCreateDialog(int dialogId) { public Dialog onCreateDialog(int dialogId) {
if (mDialogDelegate != null) { if (mDialogDelegate != null) {
final Dialog dialog = mDialogDelegate.onCreateDialog(dialogId); mDialog = mDialogDelegate.onCreateDialog(dialogId);
if (dialog != null) { if (mDialog != null) {
return dialog; return mDialog;
} }
} }
final AlertDialog dialog;
switch (dialogId) { switch (dialogId) {
case DialogEnums.GESTURE_NAVIGATION_TUTORIAL: case DialogEnums.GESTURE_NAVIGATION_TUTORIAL:
return AccessibilityGestureNavigationTutorial return AccessibilityGestureNavigationTutorial
@@ -148,10 +146,10 @@ public class ToggleScreenMagnificationPreferenceFragment extends
final int dialogType = WizardManagerHelper.isAnySetupWizard(getIntent()) final int dialogType = WizardManagerHelper.isAnySetupWizard(getIntent())
? DialogType.EDIT_SHORTCUT_MAGNIFICATION_SUW ? DialogType.EDIT_SHORTCUT_MAGNIFICATION_SUW
: DialogType.EDIT_SHORTCUT_MAGNIFICATION; : DialogType.EDIT_SHORTCUT_MAGNIFICATION;
dialog = AccessibilityDialogUtils.showEditShortcutDialog(getPrefContext(), mDialog = AccessibilityDialogUtils.showEditShortcutDialog(getPrefContext(),
dialogType, dialogTitle, this::callOnAlertDialogCheckboxClicked); dialogType, dialogTitle, this::callOnAlertDialogCheckboxClicked);
setupMagnificationEditShortcutDialog(dialog); setupMagnificationEditShortcutDialog(mDialog);
return dialog; return mDialog;
default: default:
return super.onCreateDialog(dialogId); return super.onCreateDialog(dialogId);
} }
@@ -209,7 +207,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
} }
@VisibleForTesting @VisibleForTesting
void setupMagnificationEditShortcutDialog(AlertDialog dialog) { void setupMagnificationEditShortcutDialog(Dialog dialog) {
final View dialogSoftwareView = dialog.findViewById(R.id.software_shortcut); final View dialogSoftwareView = dialog.findViewById(R.id.software_shortcut);
mSoftwareTypeCheckBox = dialogSoftwareView.findViewById(R.id.checkbox); mSoftwareTypeCheckBox = dialogSoftwareView.findViewById(R.id.checkbox);
setDialogTextAreaClickListener(dialogSoftwareView, mSoftwareTypeCheckBox); setDialogTextAreaClickListener(dialogSoftwareView, mSoftwareTypeCheckBox);

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.appcompat.app.AlertDialog;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link AccessibilityDialogUtils} */
@RunWith(RobolectricTestRunner.class)
public class AccessibilityDialogUtilsTest {
private final Context mContext = ApplicationProvider.getApplicationContext();
@Before
public void setUp() {
mContext.setTheme(R.style.Theme_AppCompat);
}
@Test
public void updateSoftwareShortcutInDialog_correctDialogType_success() {
final AlertDialog dialog = AccessibilityDialogUtils.showEditShortcutDialog(
mContext, AccessibilityDialogUtils.DialogType.EDIT_SHORTCUT_GENERIC, "Title",
null);
assertThat(
AccessibilityDialogUtils.updateSoftwareShortcutInDialog(mContext, dialog)).isTrue();
}
@Test
public void updateSoftwareShortcutInDialog_useNotSupportedDialog_fail() {
final AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("Title").show();
assertThat(AccessibilityDialogUtils.updateSoftwareShortcutInDialog(mContext,
dialog)).isFalse();
}
}