From 7712805752c10330c3748d31b193b83f589f2278 Mon Sep 17 00:00:00 2001 From: mincheli Date: Mon, 29 Nov 2021 14:14:09 +0800 Subject: [PATCH] The legacy magnification controller APIs controls only full-screen magnification See go/b200769372 - the legacy public methods The legacy magnification controller methods, setScale(), setCenter(), getScale(), getCenterX(), getCenterY(), getMagnificationRegion(), reset(), will keep the behavior before that control only full-screen magnification. To make the service able to control the actiavted magnifier on the display, the service should use the new public APIs, 1. setMagnificationConfig(), 2. getMagnificationConfig(), 3. getCurrentMagnificationRegion() instead of the legacy APIs. This change 1. Updates the doc of the legcacy APIs. 2. The legacy APIs controls only fullscreen magnification TODO: Add new public methods, getCurrentMagnificationRegion(), resetCurrentMagnification() for the current controlling magnifier Bug: 210069654 Test: atest MagnificationProcessorTest, atest AbstractAccessibilityServiceConnectionTest, atest AccessibilityMagnificationTest, Change-Id: Ia157971d2c9716df7aa14024525ee0adfdc0d46f --- .../AccessibilityService.java | 29 +++++ ...bstractAccessibilityServiceConnection.java | 6 +- .../magnification/MagnificationProcessor.java | 121 +++++++----------- ...actAccessibilityServiceConnectionTest.java | 13 +- .../MagnificationProcessorTest.java | 33 ++--- 5 files changed, 99 insertions(+), 103 deletions(-) diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java index ad628728fe1ee..dcb7ffa4abe87 100644 --- a/core/java/android/accessibilityservice/AccessibilityService.java +++ b/core/java/android/accessibilityservice/AccessibilityService.java @@ -1392,6 +1392,12 @@ public abstract class AccessibilityService extends Service { * {@link AccessibilityService#onServiceConnected()} has not yet been * called) or the service has been disconnected, this method will * return a default value of {@code 1.0f}. + *

+ *

+ * Note: This legacy API gets the scale of full-screen + * magnification. To get the scale of the current controlling magnifier, + * use {@link #getMagnificationConfig} instead. + *

* * @return the current magnification scale */ @@ -1420,6 +1426,12 @@ public abstract class AccessibilityService extends Service { * {@link AccessibilityService#onServiceConnected()} has not yet been * called) or the service has been disconnected, this method will * return a default value of {@code 0.0f}. + *

+ *

+ * Note: This legacy API gets the center position of full-screen + * magnification. To get the magnification center of the current controlling magnifier, + * use {@link #getMagnificationConfig} instead. + *

* * @return the unscaled screen-relative X coordinate of the center of * the magnified region @@ -1449,6 +1461,12 @@ public abstract class AccessibilityService extends Service { * {@link AccessibilityService#onServiceConnected()} has not yet been * called) or the service has been disconnected, this method will * return a default value of {@code 0.0f}. + *

+ *

+ * Note: This legacy API gets the center position of full-screen + * magnification. To get the magnification center of the current controlling magnifier, + * use {@link #getMagnificationConfig} instead. + *

* * @return the unscaled screen-relative Y coordinate of the center of * the magnified region @@ -1569,6 +1587,11 @@ public abstract class AccessibilityService extends Service { * {@link AccessibilityService#onServiceConnected()} has not yet been * called) or the service has been disconnected, this method will have * no effect and return {@code false}. + *

+ * Note: This legacy API sets the scale of full-screen + * magnification. To set the scale of the specified magnifier, + * use {@link #setMagnificationConfig} instead. + *

* * @param scale the magnification scale to set, must be >= 1 and <= 8 * @param animate {@code true} to animate from the current scale or @@ -1600,6 +1623,12 @@ public abstract class AccessibilityService extends Service { * {@link AccessibilityService#onServiceConnected()} has not yet been * called) or the service has been disconnected, this method will have * no effect and return {@code false}. + *

+ *

+ * Note: This legacy API sets the center of full-screen + * magnification. To set the center of the specified magnifier, + * use {@link #setMagnificationConfig} instead. + *

* * @param centerX the unscaled screen-relative X coordinate on which to * center the viewport diff --git a/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java b/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java index 881c910b399b6..f050b6622a5d4 100644 --- a/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java +++ b/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java @@ -1027,8 +1027,8 @@ abstract class AbstractAccessibilityServiceConnection extends IAccessibilityServ mSystemSupport.getMagnificationProcessor(); final long identity = Binder.clearCallingIdentity(); try { - magnificationProcessor.getMagnificationRegion(displayId, region, - mSecurityPolicy.canControlMagnification(this)); + magnificationProcessor.getFullscreenMagnificationRegion(displayId, + region, mSecurityPolicy.canControlMagnification(this)); return region; } finally { Binder.restoreCallingIdentity(identity); @@ -1095,7 +1095,7 @@ abstract class AbstractAccessibilityServiceConnection extends IAccessibilityServ try { MagnificationProcessor magnificationProcessor = mSystemSupport.getMagnificationProcessor(); - return (magnificationProcessor.reset(displayId, animate) + return (magnificationProcessor.resetFullscreenMagnification(displayId, animate) || !magnificationProcessor.isMagnifying(displayId)); } finally { Binder.restoreCallingIdentity(identity); diff --git a/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationProcessor.java b/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationProcessor.java index d0b989582ebe5..7a525ee23d276 100644 --- a/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationProcessor.java +++ b/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationProcessor.java @@ -119,6 +119,18 @@ public class MagnificationProcessor { return false; } + private boolean setScaleAndCenterForFullScreenMagnification(int displayId, float scale, + float centerX, float centerY, + boolean animate, int id) { + if (!isRegistered(displayId)) { + register(displayId); + } + return mController.getFullScreenMagnificationController().setScaleAndCenter( + displayId, + scale, + centerX, centerY, animate, id); + } + /** * Returns {@code true} if transition magnification mode needed. And it is no need to transition * mode when the controlling mode is unchanged or the controlling magnifier is not activated. @@ -135,24 +147,18 @@ public class MagnificationProcessor { } /** - * Returns the magnification scale. If an animation is in progress, - * this reflects the end state of the animation. + * Returns the magnification scale of full-screen magnification on the display. + * If an animation is in progress, this reflects the end state of the animation. * * @param displayId The logical display id. * @return the scale */ public float getScale(int displayId) { - int mode = getControllingMode(displayId); - if (mode == MAGNIFICATION_MODE_FULLSCREEN) { - return mController.getFullScreenMagnificationController().getScale(displayId); - } else if (mode == MAGNIFICATION_MODE_WINDOW) { - return mController.getWindowMagnificationMgr().getScale(displayId); - } - return 0; + return mController.getFullScreenMagnificationController().getScale(displayId); } /** - * Returns the magnification center in X coordinate of the controlling magnification mode. + * Returns the magnification center in X coordinate of full-screen magnification. * If the service can control magnification but fullscreen magnifier is not registered, it will * register the magnifier for this call then unregister the magnifier finally to make the * magnification center correct. @@ -162,25 +168,19 @@ public class MagnificationProcessor { * @return the X coordinate */ public float getCenterX(int displayId, boolean canControlMagnification) { - int mode = getControllingMode(displayId); - if (mode == MAGNIFICATION_MODE_FULLSCREEN) { - boolean registeredJustForThisCall = registerDisplayMagnificationIfNeeded(displayId, - canControlMagnification); - try { - return mController.getFullScreenMagnificationController().getCenterX(displayId); - } finally { - if (registeredJustForThisCall) { - unregister(displayId); - } + boolean registeredJustForThisCall = registerDisplayMagnificationIfNeeded(displayId, + canControlMagnification); + try { + return mController.getFullScreenMagnificationController().getCenterX(displayId); + } finally { + if (registeredJustForThisCall) { + unregister(displayId); } - } else if (mode == MAGNIFICATION_MODE_WINDOW) { - return mController.getWindowMagnificationMgr().getCenterX(displayId); } - return 0; } /** - * Returns the magnification center in Y coordinate of the controlling magnification mode. + * Returns the magnification center in Y coordinate of full-screen magnification. * If the service can control magnification but fullscreen magnifier is not registered, it will * register the magnifier for this call then unregister the magnifier finally to make the * magnification center correct. @@ -190,49 +190,25 @@ public class MagnificationProcessor { * @return the Y coordinate */ public float getCenterY(int displayId, boolean canControlMagnification) { - int mode = getControllingMode(displayId); - if (mode == MAGNIFICATION_MODE_FULLSCREEN) { - boolean registeredJustForThisCall = registerDisplayMagnificationIfNeeded(displayId, - canControlMagnification); - try { - return mController.getFullScreenMagnificationController().getCenterY(displayId); - } finally { - if (registeredJustForThisCall) { - unregister(displayId); - } + boolean registeredJustForThisCall = registerDisplayMagnificationIfNeeded(displayId, + canControlMagnification); + try { + return mController.getFullScreenMagnificationController().getCenterY(displayId); + } finally { + if (registeredJustForThisCall) { + unregister(displayId); } - } else if (mode == MAGNIFICATION_MODE_WINDOW) { - return mController.getWindowMagnificationMgr().getCenterY(displayId); } - return 0; } /** - * Return the magnification bounds of the current controlling magnification on the given - * display. If the magnifier is not enabled, it returns an empty region. - * If the service can control magnification but fullscreen magnifier is not registered, it will - * register the magnifier for this call then unregister the magnifier finally to make - * the magnification region correct. + * Returns the magnification bounds of full-screen magnification on the given display. * * @param displayId The logical display id * @param outRegion the region to populate * @param canControlMagnification Whether the service can control magnification - * @return outRegion the magnification bounds of full-screen magnifier or the magnification - * source bounds of window magnifier */ - public Region getMagnificationRegion(int displayId, @NonNull Region outRegion, - boolean canControlMagnification) { - int mode = getControllingMode(displayId); - if (mode == MAGNIFICATION_MODE_FULLSCREEN) { - getFullscreenMagnificationRegion(displayId, outRegion, canControlMagnification); - } else if (mode == MAGNIFICATION_MODE_WINDOW) { - mController.getWindowMagnificationMgr().getMagnificationSourceBounds(displayId, - outRegion); - } - return outRegion; - } - - private void getFullscreenMagnificationRegion(int displayId, @NonNull Region outRegion, + public void getFullscreenMagnificationRegion(int displayId, @NonNull Region outRegion, boolean canControlMagnification) { boolean registeredJustForThisCall = registerDisplayMagnificationIfNeeded(displayId, canControlMagnification); @@ -246,21 +222,9 @@ public class MagnificationProcessor { } } - private boolean setScaleAndCenterForFullScreenMagnification(int displayId, float scale, - float centerX, float centerY, - boolean animate, int id) { - if (!isRegistered(displayId)) { - register(displayId); - } - return mController.getFullScreenMagnificationController().setScaleAndCenter( - displayId, - scale, - centerX, centerY, animate, id); - } - /** - * Resets the magnification on the given display. The reset mode could be full-screen or - * window if it is activated. + * Resets the current magnification on the given display. The reset mode could be + * full-screen or window if it is activated. * * @param displayId The logical display id. * @param animate {@code true} to animate the transition, {@code false} @@ -268,7 +232,7 @@ public class MagnificationProcessor { * @return {@code true} if the magnification spec changed, {@code false} if * the spec did not change */ - public boolean reset(int displayId, boolean animate) { + public boolean resetCurrentMagnification(int displayId, boolean animate) { int mode = getControllingMode(displayId); if (mode == MAGNIFICATION_MODE_FULLSCREEN) { return mController.getFullScreenMagnificationController().reset(displayId, animate); @@ -278,6 +242,19 @@ public class MagnificationProcessor { return false; } + /** + * Resets the full-screen magnification on the given display. + * + * @param displayId The logical display id. + * @param animate {@code true} to animate the transition, {@code false} + * to transition immediately + * @return {@code true} if the magnification spec changed, {@code false} if + * the spec did not change + */ + public boolean resetFullscreenMagnification(int displayId, boolean animate) { + return mController.getFullScreenMagnificationController().reset(displayId, animate); + } + /** * {@link FullScreenMagnificationController#resetIfNeeded(int, boolean)} */ diff --git a/services/tests/servicestests/src/com/android/server/accessibility/AbstractAccessibilityServiceConnectionTest.java b/services/tests/servicestests/src/com/android/server/accessibility/AbstractAccessibilityServiceConnectionTest.java index f92b872e1d26e..28c1c815508fe 100644 --- a/services/tests/servicestests/src/com/android/server/accessibility/AbstractAccessibilityServiceConnectionTest.java +++ b/services/tests/servicestests/src/com/android/server/accessibility/AbstractAccessibilityServiceConnectionTest.java @@ -585,8 +585,8 @@ public class AbstractAccessibilityServiceConnectionTest { doAnswer((invocation) -> { ((Region) invocation.getArguments()[1]).set(region); return null; - }).when(mMockMagnificationProcessor).getMagnificationRegion(eq(displayId), - any(), anyBoolean()); + }).when(mMockMagnificationProcessor).getFullscreenMagnificationRegion(eq(displayId), any(), + anyBoolean()); when(mMockSystemSupport.getCurrentUserIdLocked()).thenReturn(USER_ID2); final Region result = mServiceConnection.getMagnificationRegion(displayId); @@ -620,7 +620,8 @@ public class AbstractAccessibilityServiceConnectionTest { @Test public void resetMagnification() { final int displayId = 1; - when(mMockMagnificationProcessor.reset(displayId, true)).thenReturn(true); + when(mMockMagnificationProcessor.resetFullscreenMagnification(displayId, true)).thenReturn( + true); final boolean result = mServiceConnection.resetMagnification(displayId, true); assertThat(result, is(true)); @@ -629,7 +630,8 @@ public class AbstractAccessibilityServiceConnectionTest { @Test public void resetMagnification_cantControlMagnification_returnFalse() { final int displayId = 1; - when(mMockMagnificationProcessor.reset(displayId, true)).thenReturn(true); + when(mMockMagnificationProcessor.resetFullscreenMagnification(displayId, true)).thenReturn( + true); when(mMockSecurityPolicy.canControlMagnification(mServiceConnection)).thenReturn(false); final boolean result = mServiceConnection.resetMagnification(displayId, true); @@ -639,7 +641,8 @@ public class AbstractAccessibilityServiceConnectionTest { @Test public void resetMagnification_serviceNotBelongCurrentUser_returnFalse() { final int displayId = 1; - when(mMockMagnificationProcessor.reset(displayId, true)).thenReturn(true); + when(mMockMagnificationProcessor.resetFullscreenMagnification(displayId, true)).thenReturn( + true); when(mMockSystemSupport.getCurrentUserIdLocked()).thenReturn(USER_ID2); final boolean result = mServiceConnection.resetMagnification(displayId, true); diff --git a/services/tests/servicestests/src/com/android/server/accessibility/MagnificationProcessorTest.java b/services/tests/servicestests/src/com/android/server/accessibility/MagnificationProcessorTest.java index 9ebec981fa21d..621507e2bfc8d 100644 --- a/services/tests/servicestests/src/com/android/server/accessibility/MagnificationProcessorTest.java +++ b/services/tests/servicestests/src/com/android/server/accessibility/MagnificationProcessorTest.java @@ -98,7 +98,7 @@ public class MagnificationProcessorTest { .setScale(TEST_SCALE).build(); setMagnificationActivated(TEST_DISPLAY, config); - float scale = mMagnificationProcessor.getScale(TEST_DISPLAY); + float scale = mMagnificationProcessor.getMagnificationConfig(TEST_DISPLAY).getScale(); assertEquals(scale, TEST_SCALE, 0); } @@ -111,20 +111,19 @@ public class MagnificationProcessorTest { setMagnificationActivated(TEST_DISPLAY, config); float centerX = mMagnificationProcessor.getCenterX( - TEST_DISPLAY, /* canControlMagnification= */true); + TEST_DISPLAY, /* canControlMagnification= */true); assertEquals(centerX, TEST_CENTER_X, 0); } @Test - public void getCenterX_canControlWindowMagnification_returnCenterX() { + public void getCenterX_controlWindowMagnification_returnCenterX() { final MagnificationConfig config = new MagnificationConfig.Builder() .setMode(MAGNIFICATION_MODE_WINDOW) .setCenterX(TEST_CENTER_X).build(); setMagnificationActivated(TEST_DISPLAY, config); - float centerX = mMagnificationProcessor.getCenterX( - TEST_DISPLAY, /* canControlMagnification= */true); + float centerX = mMagnificationProcessor.getMagnificationConfig(TEST_DISPLAY).getCenterX(); assertEquals(centerX, TEST_CENTER_X, 0); } @@ -143,14 +142,13 @@ public class MagnificationProcessorTest { } @Test - public void getCenterY_canControlWindowMagnification_returnCenterY() { + public void getCenterY_controlWindowMagnification_returnCenterY() { final MagnificationConfig config = new MagnificationConfig.Builder() .setMode(MAGNIFICATION_MODE_WINDOW) .setCenterY(TEST_CENTER_Y).build(); setMagnificationActivated(TEST_DISPLAY, config); - float centerY = mMagnificationProcessor.getCenterY( - TEST_DISPLAY, /* canControlMagnification= */false); + float centerY = mMagnificationProcessor.getMagnificationConfig(TEST_DISPLAY).getCenterY(); assertEquals(centerY, TEST_CENTER_Y, 0); } @@ -159,24 +157,13 @@ public class MagnificationProcessorTest { public void getMagnificationRegion_canControlFullscreenMagnification_returnRegion() { final Region region = new Region(10, 20, 100, 200); setMagnificationActivated(TEST_DISPLAY, MAGNIFICATION_MODE_FULLSCREEN); - mMagnificationProcessor.getMagnificationRegion(TEST_DISPLAY, + mMagnificationProcessor.getFullscreenMagnificationRegion(TEST_DISPLAY, region, /* canControlMagnification= */true); verify(mMockFullScreenMagnificationController).getMagnificationRegion(eq(TEST_DISPLAY), eq(region)); } - @Test - public void getMagnificationRegion_canControlWindowMagnification_returnRegion() { - final Region region = new Region(10, 20, 100, 200); - setMagnificationActivated(TEST_DISPLAY, MAGNIFICATION_MODE_WINDOW); - mMagnificationProcessor.getMagnificationRegion(TEST_DISPLAY, - region, /* canControlMagnification= */true); - - verify(mMockWindowMagnificationManager).getMagnificationSourceBounds(eq(TEST_DISPLAY), - eq(region)); - } - @Test public void getMagnificationRegion_fullscreenModeNotRegistered_shouldRegisterThenUnregister() { final Region region = new Region(10, 20, 100, 200); @@ -188,7 +175,7 @@ public class MagnificationProcessorTest { any()); final Region result = new Region(); - mMagnificationProcessor.getMagnificationRegion(TEST_DISPLAY, + mMagnificationProcessor.getFullscreenMagnificationRegion(TEST_DISPLAY, result, /* canControlMagnification= */true); assertEquals(region, result); verify(mMockFullScreenMagnificationController).register(TEST_DISPLAY); @@ -237,7 +224,7 @@ public class MagnificationProcessorTest { public void reset_fullscreenMagnificationActivated() { setMagnificationActivated(TEST_DISPLAY, MAGNIFICATION_MODE_FULLSCREEN); - mMagnificationProcessor.reset(TEST_DISPLAY, /* animate= */false); + mMagnificationProcessor.resetFullscreenMagnification(TEST_DISPLAY, /* animate= */false); verify(mMockFullScreenMagnificationController).reset(TEST_DISPLAY, false); } @@ -246,7 +233,7 @@ public class MagnificationProcessorTest { public void reset_windowMagnificationActivated() { setMagnificationActivated(TEST_DISPLAY, MAGNIFICATION_MODE_WINDOW); - mMagnificationProcessor.reset(TEST_DISPLAY, /* animate= */false); + mMagnificationProcessor.resetCurrentMagnification(TEST_DISPLAY, /* animate= */false); verify(mMockWindowMagnificationManager).reset(TEST_DISPLAY); }