diff options
4 files changed, 108 insertions, 23 deletions
diff --git a/services/accessibility/java/com/android/server/accessibility/magnification/FullScreenMagnificationController.java b/services/accessibility/java/com/android/server/accessibility/magnification/FullScreenMagnificationController.java index b1cdc5053c27..ba3d4340a157 100644 --- a/services/accessibility/java/com/android/server/accessibility/magnification/FullScreenMagnificationController.java +++ b/services/accessibility/java/com/android/server/accessibility/magnification/FullScreenMagnificationController.java @@ -319,6 +319,10 @@ public class FullScreenMagnificationController implements FullScreenMagnificationController::onUserContextChanged, FullScreenMagnificationController.this, mDisplayId); mControllerCtx.getHandler().sendMessage(m); + + synchronized (mLock) { + refreshThumbnail(); + } } @Override @@ -344,7 +348,7 @@ public class FullScreenMagnificationController implements mMagnificationRegion.set(magnified); mMagnificationRegion.getBounds(mMagnificationBounds); - refreshThumbnail(getScale(), getCenterX(), getCenterY()); + refreshThumbnail(); // It's possible that our magnification spec is invalid with the new bounds. // Adjust the current spec's offsets if necessary. @@ -602,13 +606,13 @@ public class FullScreenMagnificationController implements } @GuardedBy("mLock") - void refreshThumbnail(float scale, float centerX, float centerY) { + void refreshThumbnail() { if (mMagnificationThumbnail != null) { mMagnificationThumbnail.setThumbnailBounds( mMagnificationBounds, - scale, - centerX, - centerY + getScale(), + getCenterX(), + getCenterY() ); } } @@ -627,7 +631,7 @@ public class FullScreenMagnificationController implements // We call refreshThumbnail when the thumbnail is just created to set current // magnification bounds to thumbnail. It to prevent the thumbnail size has not yet // updated properly and thus shows with huge size. (b/276314641) - refreshThumbnail(getScale(), getCenterX(), getCenterY()); + refreshThumbnail(); } } diff --git a/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationThumbnail.java b/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationThumbnail.java index 03fa93d8a3bc..a7bdd5a09ac2 100644 --- a/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationThumbnail.java +++ b/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationThumbnail.java @@ -99,15 +99,17 @@ public class MagnificationThumbnail { Log.d(LOG_TAG, "setThumbnailBounds " + currentBounds); } mHandler.post(() -> { - mWindowBounds = currentBounds; - setBackgroundBounds(); + refreshBackgroundBounds(currentBounds); if (mVisible) { updateThumbnailMainThread(scale, centerX, centerY); } }); } - private void setBackgroundBounds() { + @MainThread + private void refreshBackgroundBounds(Rect currentBounds) { + mWindowBounds = currentBounds; + Point magnificationBoundary = getMagnificationThumbnailPadding(mContext); mThumbnailWidth = (int) (mWindowBounds.width() / BG_ASPECT_RATIO); mThumbnailHeight = (int) (mWindowBounds.height() / BG_ASPECT_RATIO); @@ -117,6 +119,10 @@ public class MagnificationThumbnail { mBackgroundParams.height = mThumbnailHeight; mBackgroundParams.x = initX; mBackgroundParams.y = initY; + + if (mVisible) { + mWindowManager.updateViewLayout(mThumbnailLayout, mBackgroundParams); + } } @MainThread @@ -264,21 +270,16 @@ public class MagnificationThumbnail { mThumbnailView.setScaleX(scaleDown); mThumbnailView.setScaleY(scaleDown); } - float thumbnailWidth; - float thumbnailHeight; - if (mThumbnailView.getWidth() == 0 || mThumbnailView.getHeight() == 0) { - // if the thumbnail view size is not updated correctly, we just use the cached values. - thumbnailWidth = mThumbnailWidth; - thumbnailHeight = mThumbnailHeight; - } else { - thumbnailWidth = mThumbnailView.getWidth(); - thumbnailHeight = mThumbnailView.getHeight(); - } - if (!Float.isNaN(centerX)) { + + if (!Float.isNaN(centerX) + && !Float.isNaN(centerY) + && mThumbnailWidth > 0 + && mThumbnailHeight > 0 + ) { var padding = mThumbnailView.getPaddingTop(); var ratio = 1f / BG_ASPECT_RATIO; - var centerXScaled = centerX * ratio - (thumbnailWidth / 2f + padding); - var centerYScaled = centerY * ratio - (thumbnailHeight / 2f + padding); + var centerXScaled = centerX * ratio - (mThumbnailWidth / 2f + padding); + var centerYScaled = centerY * ratio - (mThumbnailHeight / 2f + padding); if (DEBUG) { Log.d( diff --git a/services/tests/servicestests/src/com/android/server/accessibility/magnification/FullScreenMagnificationControllerTest.java b/services/tests/servicestests/src/com/android/server/accessibility/magnification/FullScreenMagnificationControllerTest.java index e6ef044d4791..5b5c8d415f84 100644 --- a/services/tests/servicestests/src/com/android/server/accessibility/magnification/FullScreenMagnificationControllerTest.java +++ b/services/tests/servicestests/src/com/android/server/accessibility/magnification/FullScreenMagnificationControllerTest.java @@ -29,6 +29,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyFloat; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -202,6 +203,7 @@ public class FullScreenMagnificationControllerTest { assertFalse(mFullScreenMagnificationController.isRegistered(DISPLAY_0)); assertFalse(mFullScreenMagnificationController.isRegistered(DISPLAY_1)); + // Once for each display on unregister verify(mMockThumbnail, times(2)).hideThumbnail(); } @@ -543,7 +545,11 @@ public class FullScreenMagnificationControllerTest { // The first time is triggered when the thumbnail is just created. // The second time is triggered when the magnification region changed. verify(mMockThumbnail, times(2)).setThumbnailBounds( - any(), anyFloat(), anyFloat(), anyFloat()); + /* currentBounds= */ any(), + /* scale= */ anyFloat(), + /* centerX= */ anyFloat(), + /* centerY= */ anyFloat() + ); } @Test @@ -681,6 +687,9 @@ public class FullScreenMagnificationControllerTest { checkActivatedAndMagnifying(/* activated= */ true, /* magnifying= */ true, displayId); assertTrue(mFullScreenMagnificationController.resetIfNeeded(displayId, SERVICE_ID_2)); checkActivatedAndMagnifying(/* activated= */ false, /* magnifying= */ false, displayId); + + // Once on init before it's activated and once for reset + verify(mMockThumbnail, times(2)).hideThumbnail(); } @Test @@ -783,6 +792,9 @@ public class FullScreenMagnificationControllerTest { mMessageCapturingHandler.sendAllMessages(); checkActivatedAndMagnifying(/* activated= */ false, /* magnifying= */ false, DISPLAY_0); checkActivatedAndMagnifying(/* activated= */ false, /* magnifying= */ false, DISPLAY_1); + + // Twice for each display: once on init before it's activated and once for screen off + verify(mMockThumbnail, times(4)).hideThumbnail(); } @Test @@ -847,6 +859,15 @@ public class FullScreenMagnificationControllerTest { mMessageCapturingHandler.sendAllMessages(); checkActivatedAndMagnifying( /* activated= */ expectedActivated, /* magnifying= */ false, displayId); + + if (expectedActivated) { + verify(mMockThumbnail, times(2)).setThumbnailBounds( + /* currentBounds= */ any(), + /* scale= */ anyFloat(), + /* centerX= */ anyFloat(), + /* centerY= */ anyFloat() + ); + } } @Test @@ -950,6 +971,13 @@ public class FullScreenMagnificationControllerTest { INITIAL_BOUNDS_LOWER_RIGHT_2X_CENTER, scale); assertThat(endSpec, closeTo(getMagnificationSpec(scale, expectedOffsets))); verify(mMockWindowManager).setMagnificationSpec(eq(displayId), argThat(closeTo(endSpec))); + + verify(mMockThumbnail, atLeastOnce()).setThumbnailBounds( + /* currentBounds= */ any(), + eq(scale), + /* centerX= */ anyFloat(), + /* centerY= */ anyFloat() + ); } @Test @@ -984,6 +1012,13 @@ public class FullScreenMagnificationControllerTest { INITIAL_BOUNDS_LOWER_RIGHT_2X_CENTER, scale); assertThat(endSpec, closeTo(getMagnificationSpec(scale, expectedOffsets))); verify(mMockWindowManager).setMagnificationSpec(eq(displayId), argThat(closeTo(endSpec))); + + verify(mMockThumbnail, atLeastOnce()).setThumbnailBounds( + /* currentBounds= */ any(), + eq(scale), + /* centerX= */ anyFloat(), + /* centerY= */ anyFloat() + ); } @Test @@ -1246,6 +1281,13 @@ public class FullScreenMagnificationControllerTest { callbacks.onImeWindowVisibilityChanged(true); mMessageCapturingHandler.sendAllMessages(); verify(mRequestObserver).onImeWindowVisibilityChanged(eq(DISPLAY_0), eq(true)); + + verify(mMockThumbnail, atLeastOnce()).setThumbnailBounds( + /* currentBounds= */ any(), + /* scale= */ anyFloat(), + /* centerX= */ anyFloat(), + /* centerY= */ anyFloat() + ); } @Test @@ -1270,6 +1312,15 @@ public class FullScreenMagnificationControllerTest { mFullScreenMagnificationController.onUserContextChanged(DISPLAY_0); verify(mRequestObserver).onFullScreenMagnificationActivationState(eq(DISPLAY_0), eq(false)); + verify(mMockThumbnail).setThumbnailBounds( + /* currentBounds= */ any(), + /* scale= */ anyFloat(), + /* centerX= */ anyFloat(), + /* centerY= */ anyFloat() + ); + + // Once on init before it's activated and once for reset + verify(mMockThumbnail, times(2)).hideThumbnail(); } @Test @@ -1281,6 +1332,12 @@ public class FullScreenMagnificationControllerTest { assertEquals(1.0f, mFullScreenMagnificationController.getScale(DISPLAY_0), 0); assertTrue(mFullScreenMagnificationController.isActivated(DISPLAY_0)); + verify(mMockThumbnail).setThumbnailBounds( + /* currentBounds= */ any(), + /* scale= */ anyFloat(), + /* centerX= */ anyFloat(), + /* centerY= */ anyFloat() + ); } private void setScaleToMagnifying() { diff --git a/services/tests/servicestests/src/com/android/server/accessibility/magnification/MagnificationThumbnailTest.java b/services/tests/servicestests/src/com/android/server/accessibility/magnification/MagnificationThumbnailTest.java index 3baa102b882b..8faddf8ff541 100644 --- a/services/tests/servicestests/src/com/android/server/accessibility/magnification/MagnificationThumbnailTest.java +++ b/services/tests/servicestests/src/com/android/server/accessibility/magnification/MagnificationThumbnailTest.java @@ -187,6 +187,29 @@ public class MagnificationThumbnailTest { .addView(eq(mMagnificationThumbnail.mThumbnailLayout), any()); verify(mMockWindowManager, never()) .removeView(eq(mMagnificationThumbnail.mThumbnailLayout)); + verify(mMockWindowManager, never()) + .updateViewLayout(eq(mMagnificationThumbnail.mThumbnailLayout), any()); + } + + @Test + public void whenVisible_setBoundsUpdatesLayout() throws InterruptedException { + runOnMainSync(() -> mMagnificationThumbnail.updateThumbnail( + /* scale= */ 2f, + /* centerX= */ 5, + /* centerY= */ 10 + )); + runOnMainSync(() -> mMagnificationThumbnail.setThumbnailBounds( + new Rect(), + /* scale= */ 2f, + /* centerX= */ 5, + /* centerY= */ 10 + )); + idle(); + + verify(mMockWindowManager).updateViewLayout( + eq(mMagnificationThumbnail.mThumbnailLayout), + /* params= */ any() + ); } private static void idle() { |