From eaab4f2517c48157bdeca328ee49af80c39c9025 Mon Sep 17 00:00:00 2001 From: Roy Chou Date: Mon, 17 Apr 2023 14:49:35 +0000 Subject: [PATCH] fix(#MagnificationThumbnail): thumbnail magnifying area position is not correct sometimes In MagnificationThumbnail, mThumbnailView size is 0 when the layout has not added to windowManager ever, and thus the magnifying area position x/y calculations would not be correct. Therefore, we use the magnification bounds to calculate the estimated size if the size is 0. If the mThumbnailView is added to window and its size is update properly, we can use the real size to calculate the magfying area x/y after. Bug: 278509054 Test: manually atest MagnificationThumbnailTest Change-Id: Ifdc84b178e264694061b3b15cbb496ab8f0374b3 --- .../magnification/MagnificationThumbnail.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) 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 5a783f47ccdc9..f3c62c63a7d57 100644 --- a/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationThumbnail.java +++ b/services/accessibility/java/com/android/server/accessibility/magnification/MagnificationThumbnail.java @@ -59,6 +59,8 @@ public class MagnificationThumbnail { public final FrameLayout mThumbnailLayout; private final View mThumbNailView; + private int mThumbnailWidth; + private int mThumbnailHeight; private final WindowManager.LayoutParams mBackgroundParams; private boolean mVisible = false; @@ -82,6 +84,8 @@ public class MagnificationThumbnail { mThumbNailView = mThumbnailLayout.findViewById(R.id.accessibility_magnification_thumbnail_view); mBackgroundParams = createLayoutParams(); + mThumbnailWidth = 0; + mThumbnailHeight = 0; } /** @@ -105,12 +109,12 @@ public class MagnificationThumbnail { private void setBackgroundBounds() { Point magnificationBoundary = getMagnificationThumbnailPadding(mContext); - final int thumbNailWidth = (int) (mWindowBounds.width() / BG_ASPECT_RATIO); - final int thumbNailHeight = (int) (mWindowBounds.height() / BG_ASPECT_RATIO); + mThumbnailWidth = (int) (mWindowBounds.width() / BG_ASPECT_RATIO); + mThumbnailHeight = (int) (mWindowBounds.height() / BG_ASPECT_RATIO); int initX = magnificationBoundary.x; int initY = magnificationBoundary.y; - mBackgroundParams.width = thumbNailWidth; - mBackgroundParams.height = thumbNailHeight; + mBackgroundParams.width = mThumbnailWidth; + mBackgroundParams.height = mThumbnailHeight; mBackgroundParams.x = initX; mBackgroundParams.y = initY; } @@ -260,11 +264,21 @@ 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)) { var padding = mThumbNailView.getPaddingTop(); var ratio = 1f / BG_ASPECT_RATIO; - var centerXScaled = centerX * ratio - (mThumbNailView.getWidth() / 2f + padding); - var centerYScaled = centerY * ratio - (mThumbNailView.getHeight() / 2f + padding); + var centerXScaled = centerX * ratio - (thumbnailWidth / 2f + padding); + var centerYScaled = centerY * ratio - (thumbnailHeight / 2f + padding); if (DEBUG) { Log.d(