Skip to content
Snippets Groups Projects
Commit 7f63e2e9 authored by Riley Jones's avatar Riley Jones Committed by Android (Google) Code Review
Browse files

Merge "Refactoring AccessibilityManagerService Read & Restore" into main

parents 428629d5 05b19d6d
No related branches found
No related tags found
No related merge requests found
......@@ -180,6 +180,26 @@ public final class ShortcutUtils {
}
}
/**
* Converts {@link Settings.Secure} key to {@link UserShortcutType}.
*
* @param key The shortcut key.
* @return Mapping type in Settings.
*/
public static int convertToType(String key) {
return switch (key) {
case Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS -> UserShortcutType.SOFTWARE;
case Settings.Secure.ACCESSIBILITY_QS_TARGETS -> UserShortcutType.QUICK_SETTINGS;
case Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE -> UserShortcutType.HARDWARE;
case Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED ->
UserShortcutType.TRIPLETAP;
case Settings.Secure.ACCESSIBILITY_MAGNIFICATION_TWO_FINGER_TRIPLE_TAP_ENABLED ->
UserShortcutType.TWOFINGER_DOUBLETAP;
default -> throw new IllegalArgumentException(
"Unsupported user shortcut key: " + key);
};
}
/**
* Updates an accessibility state if the accessibility service is a Always-On a11y service,
* a.k.a. AccessibilityServices that has FLAG_REQUEST_ACCESSIBILITY_BUTTON
......
......@@ -777,12 +777,20 @@ class AccessibilityUserState {
* @return The array set of the strings
*/
public ArraySet<String> getShortcutTargetsLocked(@UserShortcutType int shortcutType) {
ArraySet<String> set = getShortcutTargetsInternalLocked(shortcutType);
// if it's Quick Settings, make a defensive copy. Otherwise, return the raw set.
if (shortcutType == UserShortcutType.QUICK_SETTINGS) {
set = new ArraySet<>(set);
}
return set;
}
private ArraySet<String> getShortcutTargetsInternalLocked(@UserShortcutType int shortcutType) {
if (shortcutType == UserShortcutType.HARDWARE) {
return mAccessibilityShortcutKeyTargets;
} else if (shortcutType == UserShortcutType.SOFTWARE) {
return mAccessibilityButtonTargets;
} else if (shortcutType == UserShortcutType.QUICK_SETTINGS) {
return getA11yQsTargets();
return mAccessibilityQsTargets;
} else if ((shortcutType == UserShortcutType.TRIPLETAP
&& isMagnificationSingleFingerTripleTapEnabledLocked()) || (
shortcutType == UserShortcutType.TWOFINGER_DOUBLETAP
......@@ -794,6 +802,32 @@ class AccessibilityUserState {
return new ArraySet<>();
}
/**
* Updates the corresponding shortcut targets with the provided set.
* Tap shortcuts don't operate using sets of targets,
* so trying to update {@code TRIPLETAP} or {@code TWOFINGER_DOUBLETAP}
* will instead throw an {@code IllegalArgumentException}
* @param newTargets set of targets to replace the existing set.
* @param shortcutType type to be replaced.
* @return {@code true} if the set was changed, or {@code false} if the elements are the same.
* @throws IllegalArgumentException if {@code TRIPLETAP} or {@code TWOFINGER_DOUBLETAP} is used.
*/
boolean updateShortcutTargetsLocked(
Set<String> newTargets, @UserShortcutType int shortcutType) {
final int mask = UserShortcutType.TRIPLETAP | UserShortcutType.TWOFINGER_DOUBLETAP;
if ((shortcutType & mask) != 0) {
throw new IllegalArgumentException("Tap shortcuts cannot be updated with target sets.");
}
final Set<String> currentTargets = getShortcutTargetsInternalLocked(shortcutType);
if (newTargets.equals(currentTargets)) {
return false;
}
currentTargets.clear();
currentTargets.addAll(newTargets);
return true;
}
/**
* Whether or not the given shortcut target is installed in device.
*
......@@ -844,8 +878,8 @@ class AccessibilityUserState {
);
}
Set<String> targets = getShortcutTargetsLocked(shortcutType);
boolean result = targets.removeIf(name -> {
Set<String> targets = getShortcutTargetsInternalLocked(shortcutType);
return targets.removeIf(name -> {
ComponentName componentName;
if (name == null
|| (componentName = ComponentName.unflattenFromString(name)) == null) {
......@@ -853,11 +887,6 @@ class AccessibilityUserState {
}
return componentName.equals(target);
});
if (shortcutType == UserShortcutType.QUICK_SETTINGS) {
updateA11yQsTargetLocked(targets);
}
return result;
}
/**
......@@ -1114,11 +1143,6 @@ class AccessibilityUserState {
);
}
public void updateA11yQsTargetLocked(Set<String> targets) {
mAccessibilityQsTargets.clear();
mAccessibilityQsTargets.addAll(targets);
}
/**
* Returns a copy of the targets which has qs shortcut turned on
*/
......
......@@ -25,6 +25,7 @@ import static android.view.accessibility.Flags.FLAG_SKIP_ACCESSIBILITY_WARNING_D
import static com.android.internal.accessibility.AccessibilityShortcutController.ACCESSIBILITY_HEARING_AIDS_COMPONENT_NAME;
import static com.android.internal.accessibility.AccessibilityShortcutController.MAGNIFICATION_CONTROLLER_NAME;
import static com.android.internal.accessibility.common.ShortcutConstants.UserShortcutType.QUICK_SETTINGS;
import static com.android.server.accessibility.AccessibilityManagerService.ACTION_LAUNCH_HEARING_DEVICES_DIALOG;
import static com.android.window.flags.Flags.FLAG_ALWAYS_DRAW_MAGNIFICATION_FULLSCREEN_BORDER;
......@@ -1390,14 +1391,14 @@ public class AccessibilityManagerServiceTest {
mA11yms.enableShortcutsForTargets(
/* enable= */ true,
UserShortcutType.QUICK_SETTINGS,
QUICK_SETTINGS,
List.of(TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString()),
mA11yms.getCurrentUserIdLocked());
mTestableLooper.processAllMessages();
assertThat(
ShortcutUtils.isComponentIdExistingInSettings(
mTestableContext, UserShortcutType.QUICK_SETTINGS,
mTestableContext, QUICK_SETTINGS,
TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString())
).isTrue();
verify(mStatusBarManagerInternal)
......@@ -1417,14 +1418,14 @@ public class AccessibilityManagerServiceTest {
mA11yms.enableShortcutsForTargets(
/* enable= */ false,
UserShortcutType.QUICK_SETTINGS,
QUICK_SETTINGS,
List.of(TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString()),
mA11yms.getCurrentUserIdLocked());
mTestableLooper.processAllMessages();
assertThat(
ShortcutUtils.isComponentIdExistingInSettings(
mTestableContext, UserShortcutType.QUICK_SETTINGS,
mTestableContext, QUICK_SETTINGS,
TARGET_ALWAYS_ON_A11Y_SERVICE.flattenToString())
).isFalse();
verify(mStatusBarManagerInternal)
......@@ -1621,7 +1622,7 @@ public class AccessibilityManagerServiceTest {
AccessibilityShortcutController.COLOR_INVERSION_COMPONENT_NAME.flattenToString();
final AccessibilityUserState userState = new AccessibilityUserState(
UserHandle.USER_SYSTEM, mTestableContext, mA11yms);
userState.updateA11yQsTargetLocked(Set.of(daltonizerTile));
userState.updateShortcutTargetsLocked(Set.of(daltonizerTile), QUICK_SETTINGS);
mA11yms.mUserStates.put(UserHandle.USER_SYSTEM, userState);
broadcastSettingRestored(
......@@ -1642,7 +1643,7 @@ public class AccessibilityManagerServiceTest {
AccessibilityShortcutController.COLOR_INVERSION_COMPONENT_NAME.flattenToString();
final AccessibilityUserState userState = new AccessibilityUserState(
UserHandle.USER_SYSTEM, mTestableContext, mA11yms);
userState.updateA11yQsTargetLocked(Set.of(daltonizerTile));
userState.updateShortcutTargetsLocked(Set.of(daltonizerTile), QUICK_SETTINGS);
mA11yms.mUserStates.put(UserHandle.USER_SYSTEM, userState);
broadcastSettingRestored(
......
......@@ -28,6 +28,7 @@ import static android.view.accessibility.AccessibilityManager.STATE_FLAG_ACCESSI
import static android.view.accessibility.AccessibilityManager.STATE_FLAG_HIGH_TEXT_CONTRAST_ENABLED;
import static android.view.accessibility.AccessibilityManager.STATE_FLAG_TOUCH_EXPLORATION_ENABLED;
import static com.android.internal.accessibility.common.ShortcutConstants.UserShortcutType.QUICK_SETTINGS;
import static com.android.server.accessibility.AccessibilityUserState.doesShortcutTargetsStringContain;
import static com.google.common.truth.Truth.assertThat;
......@@ -429,20 +430,20 @@ public class AccessibilityUserStateTest {
}
@Test
public void updateA11yQsTargetLocked_valueUpdated() {
public void updateShortcutTargetsLocked_quickSettings_valueUpdated() {
Set<String> newTargets = Set.of(
AccessibilityShortcutController.DALTONIZER_COMPONENT_NAME.flattenToString(),
AccessibilityShortcutController.COLOR_INVERSION_COMPONENT_NAME.flattenToString()
);
mUserState.updateA11yQsTargetLocked(newTargets);
mUserState.updateShortcutTargetsLocked(newTargets, QUICK_SETTINGS);
assertThat(mUserState.getA11yQsTargets()).isEqualTo(newTargets);
}
@Test
public void getA11yQsTargets_returnsCopiedData() {
updateA11yQsTargetLocked_valueUpdated();
updateShortcutTargetsLocked_quickSettings_valueUpdated();
Set<String> targets = mUserState.getA11yQsTargets();
targets.clear();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment