Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
Andrei Stingaceanu | d6644dc | 2017-11-21 14:53:38 +0000 | [diff] [blame] | 14 | * limitations under the License. |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
Andrei Stingaceanu | d6644dc | 2017-11-21 14:53:38 +0000 | [diff] [blame] | 17 | package android.widget; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 18 | |
| 19 | import android.annotation.FloatRange; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 20 | import android.annotation.IntRange; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 23 | import android.annotation.Px; |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 24 | import android.annotation.TestApi; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 25 | import android.annotation.UiThread; |
| 26 | import android.content.Context; |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 27 | import android.content.res.Resources; |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
Mihai Popa | d870b88 | 2018-02-27 14:25:52 +0000 | [diff] [blame] | 30 | import android.graphics.Color; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 31 | import android.graphics.Outline; |
| 32 | import android.graphics.Paint; |
| 33 | import android.graphics.PixelFormat; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 34 | import android.graphics.Point; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 35 | import android.graphics.PointF; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 36 | import android.graphics.Rect; |
| 37 | import android.os.Handler; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 38 | import android.os.HandlerThread; |
| 39 | import android.os.Message; |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 40 | import android.view.ContextThemeWrapper; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 41 | import android.view.Display; |
| 42 | import android.view.DisplayListCanvas; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 43 | import android.view.PixelCopy; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 44 | import android.view.RenderNode; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 45 | import android.view.Surface; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 46 | import android.view.SurfaceControl; |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 47 | import android.view.SurfaceHolder; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 48 | import android.view.SurfaceSession; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 49 | import android.view.SurfaceView; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 50 | import android.view.ThreadedRenderer; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 51 | import android.view.View; |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 52 | import android.view.ViewRootImpl; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 53 | |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 54 | import com.android.internal.R; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 55 | import com.android.internal.util.Preconditions; |
| 56 | |
| 57 | /** |
Andrei Stingaceanu | d6644dc | 2017-11-21 14:53:38 +0000 | [diff] [blame] | 58 | * Android magnifier widget. Can be used by any view which is attached to a window. |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 59 | */ |
Andrei Stingaceanu | d6644dc | 2017-11-21 14:53:38 +0000 | [diff] [blame] | 60 | @UiThread |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 61 | public final class Magnifier { |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 62 | // Use this to specify that a previous configuration value does not exist. |
| 63 | private static final int NONEXISTENT_PREVIOUS_CONFIG_VALUE = -1; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 64 | // The callbacks of the pixel copy requests will be invoked on |
| 65 | // the Handler of this Thread when the copy is finished. |
| 66 | private static final HandlerThread sPixelCopyHandlerThread = |
| 67 | new HandlerThread("magnifier pixel copy result handler"); |
| 68 | |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 69 | // The view to which this magnifier is attached. |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 70 | private final View mView; |
Mihai Popa | 1d1ed0c | 2018-01-12 12:38:12 +0000 | [diff] [blame] | 71 | // The coordinates of the view in the surface. |
| 72 | private final int[] mViewCoordinatesInSurface; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 73 | // The window containing the magnifier. |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 74 | private InternalPopupWindow mWindow; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 75 | // The width of the window containing the magnifier. |
| 76 | private final int mWindowWidth; |
| 77 | // The height of the window containing the magnifier. |
| 78 | private final int mWindowHeight; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 79 | // The zoom applied to the view region copied to the magnifier view. |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 80 | private float mZoom; |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 81 | // The width of the content that will be copied to the magnifier. |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 82 | private int mSourceWidth; |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 83 | // The height of the content that will be copied to the magnifier. |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 84 | private int mSourceHeight; |
| 85 | // Whether the zoom of the magnifier has changed since last content copy. |
| 86 | private boolean mDirtyZoom; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 87 | // The elevation of the window containing the magnifier. |
| 88 | private final float mWindowElevation; |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 89 | // The corner radius of the window containing the magnifier. |
| 90 | private final float mWindowCornerRadius; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 91 | // The horizontal offset between the source and window coords when #show(float, float) is used. |
| 92 | private final int mDefaultHorizontalSourceToMagnifierOffset; |
| 93 | // The vertical offset between the source and window coords when #show(float, float) is used. |
| 94 | private final int mDefaultVerticalSourceToMagnifierOffset; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 95 | // The parent surface for the magnifier surface. |
| 96 | private SurfaceInfo mParentSurface; |
| 97 | // The surface where the content will be copied from. |
| 98 | private SurfaceInfo mContentCopySurface; |
| 99 | // The center coordinates of the window containing the magnifier. |
| 100 | private final Point mWindowCoords = new Point(); |
| 101 | // The center coordinates of the content to be magnified, |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 102 | // clamped inside the visible region of the magnified view. |
| 103 | private final Point mClampedCenterZoomCoords = new Point(); |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 104 | // Variables holding previous states, used for detecting redundant calls and invalidation. |
| 105 | private final Point mPrevStartCoordsInSurface = new Point( |
| 106 | NONEXISTENT_PREVIOUS_CONFIG_VALUE, NONEXISTENT_PREVIOUS_CONFIG_VALUE); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 107 | private final PointF mPrevShowSourceCoords = new PointF( |
| 108 | NONEXISTENT_PREVIOUS_CONFIG_VALUE, NONEXISTENT_PREVIOUS_CONFIG_VALUE); |
| 109 | private final PointF mPrevShowWindowCoords = new PointF( |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 110 | NONEXISTENT_PREVIOUS_CONFIG_VALUE, NONEXISTENT_PREVIOUS_CONFIG_VALUE); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 111 | // Rectangle defining the view surface area we pixel copy content from. |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 112 | private final Rect mPixelCopyRequestRect = new Rect(); |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 113 | // Lock to synchronize between the UI thread and the thread that handles pixel copy results. |
| 114 | // Only sync mWindow writes from UI thread with mWindow reads from sPixelCopyHandlerThread. |
| 115 | private final Object mLock = new Object(); |
Andrei Stingaceanu | 15af561 | 2017-10-13 12:53:23 +0100 | [diff] [blame] | 116 | |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 117 | /** |
| 118 | * Initializes a magnifier. |
| 119 | * |
| 120 | * @param view the view for which this magnifier is attached |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 121 | * |
Mihai Popa | b6ca909 | 2018-09-24 21:14:50 +0100 | [diff] [blame] | 122 | * @deprecated Please use {@link Builder} instead |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 123 | */ |
Mihai Popa | b6ca909 | 2018-09-24 21:14:50 +0100 | [diff] [blame] | 124 | @Deprecated |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 125 | public Magnifier(@NonNull View view) { |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 126 | this(new Builder(view)); |
| 127 | } |
| 128 | |
| 129 | private Magnifier(@NonNull Builder params) { |
| 130 | // Copy params from builder. |
| 131 | mView = params.mView; |
| 132 | mWindowWidth = params.mWidth; |
| 133 | mWindowHeight = params.mHeight; |
| 134 | mZoom = params.mZoom; |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 135 | mSourceWidth = Math.round(mWindowWidth / mZoom); |
| 136 | mSourceHeight = Math.round(mWindowHeight / mZoom); |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 137 | mWindowElevation = params.mElevation; |
| 138 | mWindowCornerRadius = params.mCornerRadius; |
| 139 | mDefaultHorizontalSourceToMagnifierOffset = |
| 140 | params.mHorizontalDefaultSourceToMagnifierOffset; |
| 141 | mDefaultVerticalSourceToMagnifierOffset = |
| 142 | params.mVerticalDefaultSourceToMagnifierOffset; |
Mihai Popa | 1d1ed0c | 2018-01-12 12:38:12 +0000 | [diff] [blame] | 143 | // The view's surface coordinates will not be updated until the magnifier is first shown. |
| 144 | mViewCoordinatesInSurface = new int[2]; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 145 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 146 | |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 147 | static { |
| 148 | sPixelCopyHandlerThread.start(); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 152 | * Shows the magnifier on the screen. The method takes the coordinates of the center |
| 153 | * of the content source going to be magnified and copied to the magnifier. The coordinates |
| 154 | * are relative to the top left corner of the magnified view. The magnifier will be |
| 155 | * positioned such that its center will be at the default offset from the center of the source. |
| 156 | * The default offset can be specified using the method |
| 157 | * {@link Builder#setDefaultSourceToMagnifierOffset(int, int)}. If the offset should |
| 158 | * be different across calls to this method, you should consider to use method |
| 159 | * {@link #show(float, float, float, float)} instead. |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 160 | * |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 161 | * @param sourceCenterX horizontal coordinate of the source center, relative to the view |
| 162 | * @param sourceCenterY vertical coordinate of the source center, relative to the view |
| 163 | * |
| 164 | * @see Builder#setDefaultSourceToMagnifierOffset(int, int) |
| 165 | * @see Builder#getDefaultHorizontalSourceToMagnifierOffset() |
| 166 | * @see Builder#getDefaultVerticalSourceToMagnifierOffset() |
| 167 | * @see #show(float, float, float, float) |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 168 | */ |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 169 | public void show(@FloatRange(from = 0) float sourceCenterX, |
| 170 | @FloatRange(from = 0) float sourceCenterY) { |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 171 | show(sourceCenterX, sourceCenterY, |
| 172 | sourceCenterX + mDefaultHorizontalSourceToMagnifierOffset, |
| 173 | sourceCenterY + mDefaultVerticalSourceToMagnifierOffset); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 177 | * Shows the magnifier on the screen at a position that is independent from its content |
| 178 | * position. The first two arguments represent the coordinates of the center of the |
| 179 | * content source going to be magnified and copied to the magnifier. The last two arguments |
| 180 | * represent the coordinates of the center of the magnifier itself. All four coordinates |
| 181 | * are relative to the top left corner of the magnified view. If you consider using this |
| 182 | * method such that the offset between the source center and the magnifier center coordinates |
| 183 | * remains constant, you should consider using method {@link #show(float, float)} instead. |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 184 | * |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 185 | * @param sourceCenterX horizontal coordinate of the source center relative to the view |
| 186 | * @param sourceCenterY vertical coordinate of the source center, relative to the view |
| 187 | * @param magnifierCenterX horizontal coordinate of the magnifier center, relative to the view |
| 188 | * @param magnifierCenterY vertical coordinate of the magnifier center, relative to the view |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 189 | */ |
| 190 | public void show(@FloatRange(from = 0) float sourceCenterX, |
| 191 | @FloatRange(from = 0) float sourceCenterY, |
| 192 | float magnifierCenterX, float magnifierCenterY) { |
| 193 | sourceCenterX = Math.max(0, Math.min(sourceCenterX, mView.getWidth())); |
| 194 | sourceCenterY = Math.max(0, Math.min(sourceCenterY, mView.getHeight())); |
Andrei Stingaceanu | 451f947 | 2017-10-13 16:41:28 +0100 | [diff] [blame] | 195 | |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 196 | obtainSurfaces(); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 197 | obtainContentCoordinates(sourceCenterX, sourceCenterY); |
| 198 | obtainWindowCoordinates(magnifierCenterX, magnifierCenterY); |
Andrei Stingaceanu | ca189fe | 2017-10-19 17:02:22 +0100 | [diff] [blame] | 199 | |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 200 | final int startX = mClampedCenterZoomCoords.x - mSourceWidth / 2; |
| 201 | final int startY = mClampedCenterZoomCoords.y - mSourceHeight / 2; |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 202 | if (sourceCenterX != mPrevShowSourceCoords.x || sourceCenterY != mPrevShowSourceCoords.y |
| 203 | || mDirtyZoom) { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 204 | if (mWindow == null) { |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 205 | synchronized (mLock) { |
| 206 | mWindow = new InternalPopupWindow(mView.getContext(), mView.getDisplay(), |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 207 | mParentSurface.mSurface, |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 208 | mWindowWidth, mWindowHeight, mWindowElevation, mWindowCornerRadius, |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 209 | Handler.getMain() /* draw the magnifier on the UI thread */, mLock, |
| 210 | mCallback); |
| 211 | } |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 212 | } |
| 213 | performPixelCopy(startX, startY, true /* update window position */); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 214 | } else if (magnifierCenterX != mPrevShowWindowCoords.x |
| 215 | || magnifierCenterY != mPrevShowWindowCoords.y) { |
| 216 | final Point windowCoords = getCurrentClampedWindowCoordinates(); |
| 217 | final InternalPopupWindow currentWindowInstance = mWindow; |
| 218 | sPixelCopyHandlerThread.getThreadHandler().post(() -> { |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 219 | synchronized (mLock) { |
Mihai Popa | ddcd5481 | 2018-09-03 17:25:54 +0100 | [diff] [blame] | 220 | if (mWindow != currentWindowInstance) { |
| 221 | // The magnifier was dismissed (and maybe shown again) in the meantime. |
| 222 | return; |
| 223 | } |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 224 | mWindow.setContentPositionForNextDraw(windowCoords.x, windowCoords.y); |
| 225 | } |
| 226 | }); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 227 | } |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 228 | mPrevShowSourceCoords.x = sourceCenterX; |
| 229 | mPrevShowSourceCoords.y = sourceCenterY; |
| 230 | mPrevShowWindowCoords.x = magnifierCenterX; |
| 231 | mPrevShowWindowCoords.y = magnifierCenterY; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
Andrei Stingaceanu | d6644dc | 2017-11-21 14:53:38 +0000 | [diff] [blame] | 235 | * Dismisses the magnifier from the screen. Calling this on a dismissed magnifier is a no-op. |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 236 | */ |
| 237 | public void dismiss() { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 238 | if (mWindow != null) { |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 239 | synchronized (mLock) { |
| 240 | mWindow.destroy(); |
| 241 | mWindow = null; |
| 242 | } |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 243 | mPrevShowSourceCoords.x = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
| 244 | mPrevShowSourceCoords.y = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
| 245 | mPrevShowWindowCoords.x = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
| 246 | mPrevShowWindowCoords.y = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
Mihai Popa | 953b134 | 2018-03-21 18:05:13 +0000 | [diff] [blame] | 247 | mPrevStartCoordsInSurface.x = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
| 248 | mPrevStartCoordsInSurface.y = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 249 | } |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 250 | } |
Andrei Stingaceanu | 15af561 | 2017-10-13 12:53:23 +0100 | [diff] [blame] | 251 | |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 252 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 253 | * Asks the magnifier to update its content. It uses the previous coordinates passed to |
| 254 | * {@link #show(float, float)} or {@link #show(float, float, float, float)}. The |
| 255 | * method only has effect if the magnifier is currently showing. |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 256 | */ |
| 257 | public void update() { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 258 | if (mWindow != null) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 259 | obtainSurfaces(); |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 260 | if (!mDirtyZoom) { |
| 261 | // Update the content shown in the magnifier. |
| 262 | performPixelCopy(mPrevStartCoordsInSurface.x, mPrevStartCoordsInSurface.y, |
| 263 | false /* update window position */); |
| 264 | } else { |
| 265 | // If the zoom has changed, we cannot use the same top left coordinates |
| 266 | // as before, so just #show again to have them recomputed. |
| 267 | show(mPrevShowSourceCoords.x, mPrevShowSourceCoords.y, |
| 268 | mPrevShowWindowCoords.x, mPrevShowWindowCoords.y); |
| 269 | } |
Andrei Stingaceanu | 15af561 | 2017-10-13 12:53:23 +0100 | [diff] [blame] | 270 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 273 | /** |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 274 | * @return the width of the magnifier window, in pixels |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 275 | * @see Magnifier.Builder#setSize(int, int) |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 276 | */ |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 277 | @Px |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 278 | public int getWidth() { |
| 279 | return mWindowWidth; |
| 280 | } |
| 281 | |
| 282 | /** |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 283 | * @return the height of the magnifier window, in pixels |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 284 | * @see Magnifier.Builder#setSize(int, int) |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 285 | */ |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 286 | @Px |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 287 | public int getHeight() { |
| 288 | return mWindowHeight; |
| 289 | } |
| 290 | |
| 291 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 292 | * @return the initial width of the content magnified and copied to the magnifier, in pixels |
| 293 | * @see Magnifier.Builder#setSize(int, int) |
| 294 | * @see Magnifier.Builder#setZoom(float) |
| 295 | */ |
| 296 | @Px |
| 297 | public int getSourceWidth() { |
| 298 | return mSourceWidth; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @return the initial height of the content magnified and copied to the magnifier, in pixels |
| 303 | * @see Magnifier.Builder#setSize(int, int) |
| 304 | * @see Magnifier.Builder#setZoom(float) |
| 305 | */ |
| 306 | @Px |
| 307 | public int getSourceHeight() { |
| 308 | return mSourceHeight; |
| 309 | } |
| 310 | |
| 311 | /** |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 312 | * Sets the zoom to be applied to the chosen content before being copied to the magnifier popup. |
| 313 | * @param zoom the zoom to be set |
| 314 | */ |
| 315 | public void setZoom(@FloatRange(from = 0f) float zoom) { |
| 316 | Preconditions.checkArgumentPositive(zoom, "Zoom should be positive"); |
| 317 | mZoom = zoom; |
| 318 | mSourceWidth = Math.round(mWindowWidth / mZoom); |
| 319 | mSourceHeight = Math.round(mWindowHeight / mZoom); |
| 320 | mDirtyZoom = true; |
| 321 | } |
| 322 | |
| 323 | /** |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 324 | * Returns the zoom to be applied to the magnified view region copied to the magnifier. |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 325 | * If the zoom is x and the magnifier window size is (width, height), the original size |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 326 | * of the content being magnified will be (width / x, height / x). |
| 327 | * @return the zoom applied to the content |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 328 | * @see Magnifier.Builder#setZoom(float) |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 329 | */ |
| 330 | public float getZoom() { |
| 331 | return mZoom; |
| 332 | } |
| 333 | |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 334 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 335 | * @return the elevation set for the magnifier window, in pixels |
| 336 | * @see Magnifier.Builder#setElevation(float) |
| 337 | */ |
| 338 | @Px |
| 339 | public float getElevation() { |
| 340 | return mWindowElevation; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * @return the corner radius of the magnifier window, in pixels |
| 345 | * @see Magnifier.Builder#setCornerRadius(float) |
| 346 | */ |
| 347 | @Px |
| 348 | public float getCornerRadius() { |
| 349 | return mWindowCornerRadius; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Returns the horizontal offset, in pixels, to be applied to the source center position |
| 354 | * to obtain the magnifier center position when {@link #show(float, float)} is called. |
| 355 | * The value is ignored when {@link #show(float, float, float, float)} is used instead. |
Mihai Popa | 0450a16 | 2018-04-27 13:09:12 +0100 | [diff] [blame] | 356 | * |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 357 | * @return the default horizontal offset between the source center and the magnifier |
| 358 | * @see Magnifier.Builder#setDefaultSourceToMagnifierOffset(int, int) |
| 359 | * @see Magnifier#show(float, float) |
| 360 | */ |
| 361 | @Px |
| 362 | public int getDefaultHorizontalSourceToMagnifierOffset() { |
| 363 | return mDefaultHorizontalSourceToMagnifierOffset; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Returns the vertical offset, in pixels, to be applied to the source center position |
| 368 | * to obtain the magnifier center position when {@link #show(float, float)} is called. |
| 369 | * The value is ignored when {@link #show(float, float, float, float)} is used instead. |
| 370 | * |
| 371 | * @return the default vertical offset between the source center and the magnifier |
| 372 | * @see Magnifier.Builder#setDefaultSourceToMagnifierOffset(int, int) |
| 373 | * @see Magnifier#show(float, float) |
| 374 | */ |
| 375 | @Px |
| 376 | public int getDefaultVerticalSourceToMagnifierOffset() { |
| 377 | return mDefaultVerticalSourceToMagnifierOffset; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Returns the top left coordinates of the magnifier, relative to the surface of the |
| 382 | * main application window. They will be determined by the coordinates of the last |
| 383 | * {@link #show(float, float)} or {@link #show(float, float, float, float)} call, adjusted |
| 384 | * to take into account any potential clamping behavior. The method can be used immediately |
| 385 | * after a #show call to find out where the magnifier will be positioned. However, the |
| 386 | * position of the magnifier will not be updated in the same frame due to the async |
| 387 | * copying of the content copying and of the magnifier rendering. |
| 388 | * The method will return {@code null} if #show has not yet been called, or if the last |
| 389 | * operation performed was a #dismiss. |
| 390 | * |
| 391 | * @return the top left coordinates of the magnifier |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 392 | */ |
| 393 | @Nullable |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 394 | public Point getPosition() { |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 395 | if (mWindow == null) { |
| 396 | return null; |
| 397 | } |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 398 | return new Point(getCurrentClampedWindowCoordinates()); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Returns the top left coordinates of the magnifier source (i.e. the view region going to |
| 403 | * be magnified and copied to the magnifier), relative to the surface the content is copied |
| 404 | * from. The content will be copied: |
| 405 | * - if the magnified view is a {@link SurfaceView}, from the surface backing it |
| 406 | * - otherwise, from the surface of the main application window |
| 407 | * The method will return {@code null} if #show has not yet been called, or if the last |
| 408 | * operation performed was a #dismiss. |
| 409 | * |
| 410 | * @return the top left coordinates of the magnifier source |
| 411 | */ |
| 412 | @Nullable |
| 413 | public Point getSourcePosition() { |
| 414 | if (mWindow == null) { |
| 415 | return null; |
| 416 | } |
| 417 | return new Point(mPixelCopyRequestRect.left, mPixelCopyRequestRect.top); |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 420 | /** |
| 421 | * Retrieves the surfaces used by the magnifier: |
| 422 | * - a parent surface for the magnifier surface. This will usually be the main app window. |
| 423 | * - a surface where the magnified content will be copied from. This will be the main app |
| 424 | * window unless the magnified view is a SurfaceView, in which case its backing surface |
| 425 | * will be used. |
| 426 | */ |
| 427 | private void obtainSurfaces() { |
| 428 | // Get the main window surface. |
| 429 | SurfaceInfo validMainWindowSurface = SurfaceInfo.NULL; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 430 | if (mView.getViewRootImpl() != null) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 431 | final ViewRootImpl viewRootImpl = mView.getViewRootImpl(); |
| 432 | final Surface mainWindowSurface = viewRootImpl.mSurface; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 433 | if (mainWindowSurface != null && mainWindowSurface.isValid()) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 434 | final Rect surfaceInsets = viewRootImpl.mWindowAttributes.surfaceInsets; |
| 435 | final int surfaceWidth = |
| 436 | viewRootImpl.getWidth() + surfaceInsets.left + surfaceInsets.right; |
| 437 | final int surfaceHeight = |
| 438 | viewRootImpl.getHeight() + surfaceInsets.top + surfaceInsets.bottom; |
| 439 | validMainWindowSurface = |
| 440 | new SurfaceInfo(mainWindowSurface, surfaceWidth, surfaceHeight, true); |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 441 | } |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 442 | } |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 443 | // Get the surface backing the magnified view, if it is a SurfaceView. |
| 444 | SurfaceInfo validSurfaceViewSurface = SurfaceInfo.NULL; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 445 | if (mView instanceof SurfaceView) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 446 | final SurfaceHolder surfaceHolder = ((SurfaceView) mView).getHolder(); |
| 447 | final Surface surfaceViewSurface = surfaceHolder.getSurface(); |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 448 | if (surfaceViewSurface != null && surfaceViewSurface.isValid()) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 449 | final Rect surfaceFrame = surfaceHolder.getSurfaceFrame(); |
| 450 | validSurfaceViewSurface = new SurfaceInfo(surfaceViewSurface, |
| 451 | surfaceFrame.right, surfaceFrame.bottom, false); |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 452 | } |
| 453 | } |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 454 | |
| 455 | // Choose the parent surface for the magnifier and the source surface for the content. |
| 456 | mParentSurface = validMainWindowSurface != SurfaceInfo.NULL |
| 457 | ? validMainWindowSurface : validSurfaceViewSurface; |
| 458 | mContentCopySurface = mView instanceof SurfaceView |
| 459 | ? validSurfaceViewSurface : validMainWindowSurface; |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 462 | /** |
| 463 | * Computes the coordinates of the center of the content going to be displayed in the |
| 464 | * magnifier. These are relative to the surface the content is copied from. |
| 465 | */ |
| 466 | private void obtainContentCoordinates(final float xPosInView, final float yPosInView) { |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 467 | mView.getLocationInSurface(mViewCoordinatesInSurface); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 468 | final int zoomCenterX; |
| 469 | final int zoomCenterY; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 470 | if (mView instanceof SurfaceView) { |
| 471 | // No offset required if the backing Surface matches the size of the SurfaceView. |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 472 | zoomCenterX = Math.round(xPosInView); |
| 473 | zoomCenterY = Math.round(yPosInView); |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 474 | } else { |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 475 | zoomCenterX = Math.round(xPosInView + mViewCoordinatesInSurface[0]); |
| 476 | zoomCenterY = Math.round(yPosInView + mViewCoordinatesInSurface[1]); |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 479 | // Clamp the x location to avoid magnifying content which does not belong |
| 480 | // to the magnified view. This will not take into account overlapping views. |
| 481 | final Rect viewVisibleRegion = new Rect(); |
| 482 | mView.getGlobalVisibleRect(viewVisibleRegion); |
| 483 | if (mView.getViewRootImpl() != null) { |
| 484 | // Clamping coordinates relative to the surface, not to the window. |
| 485 | final Rect surfaceInsets = mView.getViewRootImpl().mWindowAttributes.surfaceInsets; |
| 486 | viewVisibleRegion.offset(surfaceInsets.left, surfaceInsets.top); |
| 487 | } |
| 488 | if (mView instanceof SurfaceView) { |
| 489 | // If we copy content from a SurfaceView, clamp coordinates relative to it. |
| 490 | viewVisibleRegion.offset(-mViewCoordinatesInSurface[0], -mViewCoordinatesInSurface[1]); |
| 491 | } |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 492 | mClampedCenterZoomCoords.x = Math.max(viewVisibleRegion.left + mSourceWidth / 2, Math.min( |
| 493 | zoomCenterX, viewVisibleRegion.right - mSourceWidth / 2)); |
| 494 | mClampedCenterZoomCoords.y = zoomCenterY; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 497 | /** |
| 498 | * Computes the coordinates of the top left corner of the magnifier window. |
| 499 | * These are relative to the surface the magnifier window is attached to. |
| 500 | */ |
| 501 | private void obtainWindowCoordinates(final float xWindowPos, final float yWindowPos) { |
| 502 | final int windowCenterX; |
| 503 | final int windowCenterY; |
| 504 | if (mView instanceof SurfaceView) { |
| 505 | // No offset required if the backing Surface matches the size of the SurfaceView. |
| 506 | windowCenterX = Math.round(xWindowPos); |
| 507 | windowCenterY = Math.round(yWindowPos); |
| 508 | } else { |
| 509 | windowCenterX = Math.round(xWindowPos + mViewCoordinatesInSurface[0]); |
| 510 | windowCenterY = Math.round(yWindowPos + mViewCoordinatesInSurface[1]); |
| 511 | } |
| 512 | |
| 513 | mWindowCoords.x = windowCenterX - mWindowWidth / 2; |
| 514 | mWindowCoords.y = windowCenterY - mWindowHeight / 2; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 515 | if (mParentSurface != mContentCopySurface) { |
| 516 | mWindowCoords.x += mViewCoordinatesInSurface[0]; |
| 517 | mWindowCoords.y += mViewCoordinatesInSurface[1]; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 518 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 521 | private void performPixelCopy(final int startXInSurface, final int startYInSurface, |
| 522 | final boolean updateWindowPosition) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 523 | if (mContentCopySurface.mSurface == null || !mContentCopySurface.mSurface.isValid()) { |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 526 | // Clamp copy coordinates inside the surface to avoid displaying distorted content. |
| 527 | final int clampedStartXInSurface = Math.max(0, |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 528 | Math.min(startXInSurface, mContentCopySurface.mWidth - mSourceWidth)); |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 529 | final int clampedStartYInSurface = Math.max(0, |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 530 | Math.min(startYInSurface, mContentCopySurface.mHeight - mSourceHeight)); |
Mihai Popa | 953b134 | 2018-03-21 18:05:13 +0000 | [diff] [blame] | 531 | // Clamp window coordinates inside the parent surface, to avoid displaying |
| 532 | // the magnifier out of screen or overlapping with system insets. |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 533 | final Point windowCoords = getCurrentClampedWindowCoordinates(); |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 534 | |
| 535 | // Perform the pixel copy. |
| 536 | mPixelCopyRequestRect.set(clampedStartXInSurface, |
| 537 | clampedStartYInSurface, |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 538 | clampedStartXInSurface + mSourceWidth, |
| 539 | clampedStartYInSurface + mSourceHeight); |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 540 | final InternalPopupWindow currentWindowInstance = mWindow; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 541 | final Bitmap bitmap = |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 542 | Bitmap.createBitmap(mSourceWidth, mSourceHeight, Bitmap.Config.ARGB_8888); |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 543 | PixelCopy.request(mContentCopySurface.mSurface, mPixelCopyRequestRect, bitmap, |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 544 | result -> { |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 545 | synchronized (mLock) { |
| 546 | if (mWindow != currentWindowInstance) { |
| 547 | // The magnifier was dismissed (and maybe shown again) in the meantime. |
| 548 | return; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 549 | } |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 550 | if (updateWindowPosition) { |
| 551 | // TODO: pull the position update outside #performPixelCopy |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 552 | mWindow.setContentPositionForNextDraw(windowCoords.x, windowCoords.y); |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 553 | } |
| 554 | mWindow.updateContent(bitmap); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 555 | } |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 556 | }, |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 557 | sPixelCopyHandlerThread.getThreadHandler()); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 558 | mPrevStartCoordsInSurface.x = startXInSurface; |
| 559 | mPrevStartCoordsInSurface.y = startYInSurface; |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 560 | mDirtyZoom = false; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 563 | /** |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 564 | * Clamp window coordinates inside the surface the magnifier is attached to, to avoid |
| 565 | * displaying the magnifier out of screen or overlapping with system insets. |
| 566 | * @return the current window coordinates, after they are clamped inside the parent surface |
| 567 | */ |
| 568 | private Point getCurrentClampedWindowCoordinates() { |
| 569 | final Rect windowBounds; |
| 570 | if (mParentSurface.mIsMainWindowSurface) { |
| 571 | final Rect systemInsets = mView.getRootWindowInsets().getSystemWindowInsets(); |
| 572 | windowBounds = new Rect(systemInsets.left, systemInsets.top, |
| 573 | mParentSurface.mWidth - systemInsets.right, |
| 574 | mParentSurface.mHeight - systemInsets.bottom); |
| 575 | } else { |
| 576 | windowBounds = new Rect(0, 0, mParentSurface.mWidth, mParentSurface.mHeight); |
| 577 | } |
| 578 | final int windowCoordsX = Math.max(windowBounds.left, |
| 579 | Math.min(windowBounds.right - mWindowWidth, mWindowCoords.x)); |
| 580 | final int windowCoordsY = Math.max(windowBounds.top, |
| 581 | Math.min(windowBounds.bottom - mWindowHeight, mWindowCoords.y)); |
| 582 | return new Point(windowCoordsX, windowCoordsY); |
| 583 | } |
| 584 | |
| 585 | /** |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 586 | * Contains a surface and metadata corresponding to it. |
| 587 | */ |
| 588 | private static class SurfaceInfo { |
| 589 | public static final SurfaceInfo NULL = new SurfaceInfo(null, 0, 0, false); |
| 590 | |
| 591 | private Surface mSurface; |
| 592 | private int mWidth; |
| 593 | private int mHeight; |
| 594 | private boolean mIsMainWindowSurface; |
| 595 | |
| 596 | SurfaceInfo(final Surface surface, final int width, final int height, |
| 597 | final boolean isMainWindowSurface) { |
| 598 | mSurface = surface; |
| 599 | mWidth = width; |
| 600 | mHeight = height; |
| 601 | mIsMainWindowSurface = isMainWindowSurface; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /** |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 606 | * Magnifier's own implementation of PopupWindow-similar floating window. |
| 607 | * This exists to ensure frame-synchronization between window position updates and window |
| 608 | * content updates. By using a PopupWindow, these events would happen in different frames, |
| 609 | * producing a shakiness effect for the magnifier content. |
| 610 | */ |
| 611 | private static class InternalPopupWindow { |
Mihai Popa | d870b88 | 2018-02-27 14:25:52 +0000 | [diff] [blame] | 612 | // The alpha set on the magnifier's content, which defines how |
| 613 | // prominent the white background is. |
| 614 | private static final int CONTENT_BITMAP_ALPHA = 242; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 615 | // The z of the magnifier surface, defining its z order in the list of |
| 616 | // siblings having the same parent surface (usually the main app surface). |
| 617 | private static final int SURFACE_Z = 5; |
Mihai Popa | d870b88 | 2018-02-27 14:25:52 +0000 | [diff] [blame] | 618 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 619 | // Display associated to the view the magnifier is attached to. |
| 620 | private final Display mDisplay; |
| 621 | // The size of the content of the magnifier. |
| 622 | private final int mContentWidth; |
| 623 | private final int mContentHeight; |
| 624 | // The size of the allocated surface. |
| 625 | private final int mSurfaceWidth; |
| 626 | private final int mSurfaceHeight; |
| 627 | // The insets of the content inside the allocated surface. |
| 628 | private final int mOffsetX; |
| 629 | private final int mOffsetY; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 630 | // The surface we allocate for the magnifier content + shadow. |
| 631 | private final SurfaceSession mSurfaceSession; |
| 632 | private final SurfaceControl mSurfaceControl; |
| 633 | private final Surface mSurface; |
| 634 | // The renderer used for the allocated surface. |
| 635 | private final ThreadedRenderer.SimpleRenderer mRenderer; |
| 636 | // The RenderNode used to draw the magnifier content in the surface. |
| 637 | private final RenderNode mBitmapRenderNode; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 638 | // The job that will be post'd to apply the pending magnifier updates to the surface. |
| 639 | private final Runnable mMagnifierUpdater; |
| 640 | // The handler where the magnifier updater jobs will be post'd. |
| 641 | private final Handler mHandler; |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 642 | // The callback to be run after the next draw. |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 643 | private Callback mCallback; |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 644 | // The position of the magnifier content when the last draw was requested. |
| 645 | private int mLastDrawContentPositionX; |
| 646 | private int mLastDrawContentPositionY; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 647 | |
| 648 | // Members below describe the state of the magnifier. Reads/writes to them |
| 649 | // have to be synchronized between the UI thread and the thread that handles |
| 650 | // the pixel copy results. This is the purpose of mLock. |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 651 | private final Object mLock; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 652 | // Whether a magnifier frame draw is currently pending in the UI thread queue. |
| 653 | private boolean mFrameDrawScheduled; |
| 654 | // The content bitmap. |
| 655 | private Bitmap mBitmap; |
| 656 | // Whether the next draw will be the first one for the current instance. |
| 657 | private boolean mFirstDraw = true; |
| 658 | // The window position in the parent surface. Might be applied during the next draw, |
| 659 | // when mPendingWindowPositionUpdate is true. |
| 660 | private int mWindowPositionX; |
| 661 | private int mWindowPositionY; |
| 662 | private boolean mPendingWindowPositionUpdate; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 663 | |
Mihai Popa | 5d983d2 | 2018-03-29 15:56:17 +0100 | [diff] [blame] | 664 | // The lock used to synchronize the UI and render threads when a #destroy |
| 665 | // is performed on the UI thread and a frame callback on the render thread. |
| 666 | // When both mLock and mDestroyLock need to be held at the same time, |
| 667 | // mDestroyLock should be acquired before mLock in order to avoid deadlocks. |
| 668 | private final Object mDestroyLock = new Object(); |
| 669 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 670 | InternalPopupWindow(final Context context, final Display display, |
| 671 | final Surface parentSurface, |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 672 | final int width, final int height, final float elevation, final float cornerRadius, |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 673 | final Handler handler, final Object lock, final Callback callback) { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 674 | mDisplay = display; |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 675 | mLock = lock; |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 676 | mCallback = callback; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 677 | |
| 678 | mContentWidth = width; |
| 679 | mContentHeight = height; |
| 680 | mOffsetX = (int) (0.1f * width); |
| 681 | mOffsetY = (int) (0.1f * height); |
| 682 | // Setup the surface we will use for drawing the content and shadow. |
| 683 | mSurfaceWidth = mContentWidth + 2 * mOffsetX; |
| 684 | mSurfaceHeight = mContentHeight + 2 * mOffsetY; |
| 685 | mSurfaceSession = new SurfaceSession(parentSurface); |
| 686 | mSurfaceControl = new SurfaceControl.Builder(mSurfaceSession) |
| 687 | .setFormat(PixelFormat.TRANSLUCENT) |
| 688 | .setSize(mSurfaceWidth, mSurfaceHeight) |
| 689 | .setName("magnifier surface") |
| 690 | .setFlags(SurfaceControl.HIDDEN) |
| 691 | .build(); |
| 692 | mSurface = new Surface(); |
| 693 | mSurface.copyFrom(mSurfaceControl); |
| 694 | |
| 695 | // Setup the RenderNode tree. The root has only one child, which contains the bitmap. |
| 696 | mRenderer = new ThreadedRenderer.SimpleRenderer( |
| 697 | context, |
| 698 | "magnifier renderer", |
| 699 | mSurface |
| 700 | ); |
| 701 | mBitmapRenderNode = createRenderNodeForBitmap( |
| 702 | "magnifier content", |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 703 | elevation, |
| 704 | cornerRadius |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 705 | ); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 706 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 707 | final DisplayListCanvas canvas = mRenderer.getRootNode().start(width, height); |
| 708 | try { |
| 709 | canvas.insertReorderBarrier(); |
| 710 | canvas.drawRenderNode(mBitmapRenderNode); |
| 711 | canvas.insertInorderBarrier(); |
| 712 | } finally { |
| 713 | mRenderer.getRootNode().end(canvas); |
| 714 | } |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 715 | |
| 716 | // Initialize the update job and the handler where this will be post'd. |
| 717 | mHandler = handler; |
| 718 | mMagnifierUpdater = this::doDraw; |
| 719 | mFrameDrawScheduled = false; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 722 | private RenderNode createRenderNodeForBitmap(final String name, |
| 723 | final float elevation, final float cornerRadius) { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 724 | final RenderNode bitmapRenderNode = RenderNode.create(name, null); |
| 725 | |
| 726 | // Define the position of the bitmap in the parent render node. The surface regions |
| 727 | // outside the bitmap are used to draw elevation. |
| 728 | bitmapRenderNode.setLeftTopRightBottom(mOffsetX, mOffsetY, |
| 729 | mOffsetX + mContentWidth, mOffsetY + mContentHeight); |
| 730 | bitmapRenderNode.setElevation(elevation); |
| 731 | |
| 732 | final Outline outline = new Outline(); |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 733 | outline.setRoundRect(0, 0, mContentWidth, mContentHeight, cornerRadius); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 734 | outline.setAlpha(1.0f); |
| 735 | bitmapRenderNode.setOutline(outline); |
| 736 | bitmapRenderNode.setClipToOutline(true); |
| 737 | |
| 738 | // Create a dummy draw, which will be replaced later with real drawing. |
| 739 | final DisplayListCanvas canvas = bitmapRenderNode.start(mContentWidth, mContentHeight); |
| 740 | try { |
| 741 | canvas.drawColor(0xFF00FF00); |
| 742 | } finally { |
| 743 | bitmapRenderNode.end(canvas); |
| 744 | } |
| 745 | |
| 746 | return bitmapRenderNode; |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Sets the position of the magnifier content relative to the parent surface. |
| 751 | * The position update will happen in the same frame with the next draw. |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 752 | * The method has to be called in a context that holds {@link #mLock}. |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 753 | * |
| 754 | * @param contentX the x coordinate of the content |
| 755 | * @param contentY the y coordinate of the content |
| 756 | */ |
| 757 | public void setContentPositionForNextDraw(final int contentX, final int contentY) { |
| 758 | mWindowPositionX = contentX - mOffsetX; |
| 759 | mWindowPositionY = contentY - mOffsetY; |
| 760 | mPendingWindowPositionUpdate = true; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 761 | requestUpdate(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Sets the content that should be displayed in the magnifier. |
| 766 | * The update happens immediately, and possibly triggers a pending window movement set |
| 767 | * by {@link #setContentPositionForNextDraw(int, int)}. |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 768 | * The method has to be called in a context that holds {@link #mLock}. |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 769 | * |
| 770 | * @param bitmap the content bitmap |
| 771 | */ |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 772 | public void updateContent(final @NonNull Bitmap bitmap) { |
| 773 | if (mBitmap != null) { |
| 774 | mBitmap.recycle(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 775 | } |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 776 | mBitmap = bitmap; |
| 777 | requestUpdate(); |
| 778 | } |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 779 | |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 780 | private void requestUpdate() { |
| 781 | if (mFrameDrawScheduled) { |
| 782 | return; |
| 783 | } |
| 784 | final Message request = Message.obtain(mHandler, mMagnifierUpdater); |
| 785 | request.setAsynchronous(true); |
| 786 | request.sendToTarget(); |
| 787 | mFrameDrawScheduled = true; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Destroys this instance. |
| 792 | */ |
| 793 | public void destroy() { |
Mihai Popa | 5d983d2 | 2018-03-29 15:56:17 +0100 | [diff] [blame] | 794 | synchronized (mDestroyLock) { |
| 795 | mSurface.destroy(); |
| 796 | } |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 797 | synchronized (mLock) { |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 798 | mRenderer.destroy(); |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 799 | mSurfaceControl.destroy(); |
| 800 | mSurfaceSession.kill(); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 801 | mHandler.removeCallbacks(mMagnifierUpdater); |
| 802 | if (mBitmap != null) { |
| 803 | mBitmap.recycle(); |
| 804 | } |
| 805 | } |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | private void doDraw() { |
| 809 | final ThreadedRenderer.FrameDrawingCallback callback; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 810 | |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 811 | // Draw the current bitmap to the surface, and prepare the callback which updates the |
| 812 | // surface position. These have to be in the same synchronized block, in order to |
| 813 | // guarantee the consistency between the bitmap content and the surface position. |
| 814 | synchronized (mLock) { |
| 815 | if (!mSurface.isValid()) { |
| 816 | // Probably #destroy() was called for the current instance, so we skip the draw. |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | final DisplayListCanvas canvas = |
| 821 | mBitmapRenderNode.start(mContentWidth, mContentHeight); |
| 822 | try { |
Mihai Popa | d870b88 | 2018-02-27 14:25:52 +0000 | [diff] [blame] | 823 | canvas.drawColor(Color.WHITE); |
| 824 | |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 825 | final Rect srcRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); |
| 826 | final Rect dstRect = new Rect(0, 0, mContentWidth, mContentHeight); |
| 827 | final Paint paint = new Paint(); |
| 828 | paint.setFilterBitmap(true); |
Mihai Popa | d870b88 | 2018-02-27 14:25:52 +0000 | [diff] [blame] | 829 | paint.setAlpha(CONTENT_BITMAP_ALPHA); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 830 | canvas.drawBitmap(mBitmap, srcRect, dstRect, paint); |
| 831 | } finally { |
| 832 | mBitmapRenderNode.end(canvas); |
| 833 | } |
| 834 | |
| 835 | if (mPendingWindowPositionUpdate || mFirstDraw) { |
| 836 | // If the window has to be shown or moved, defer this until the next draw. |
| 837 | final boolean firstDraw = mFirstDraw; |
| 838 | mFirstDraw = false; |
| 839 | final boolean updateWindowPosition = mPendingWindowPositionUpdate; |
| 840 | mPendingWindowPositionUpdate = false; |
| 841 | final int pendingX = mWindowPositionX; |
| 842 | final int pendingY = mWindowPositionY; |
| 843 | |
| 844 | callback = frame -> { |
Mihai Popa | 5d983d2 | 2018-03-29 15:56:17 +0100 | [diff] [blame] | 845 | synchronized (mDestroyLock) { |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 846 | if (!mSurface.isValid()) { |
| 847 | return; |
| 848 | } |
Mihai Popa | 5d983d2 | 2018-03-29 15:56:17 +0100 | [diff] [blame] | 849 | synchronized (mLock) { |
| 850 | mRenderer.setLightCenter(mDisplay, pendingX, pendingY); |
| 851 | // Show or move the window at the content draw frame. |
| 852 | SurfaceControl.openTransaction(); |
| 853 | mSurfaceControl.deferTransactionUntil(mSurface, frame); |
| 854 | if (updateWindowPosition) { |
| 855 | mSurfaceControl.setPosition(pendingX, pendingY); |
| 856 | } |
| 857 | if (firstDraw) { |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 858 | mSurfaceControl.setLayer(SURFACE_Z); |
Mihai Popa | 5d983d2 | 2018-03-29 15:56:17 +0100 | [diff] [blame] | 859 | mSurfaceControl.show(); |
| 860 | } |
| 861 | SurfaceControl.closeTransaction(); |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 862 | } |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 863 | } |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 864 | }; |
| 865 | } else { |
| 866 | callback = null; |
| 867 | } |
| 868 | |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 869 | mLastDrawContentPositionX = mWindowPositionX + mOffsetX; |
| 870 | mLastDrawContentPositionY = mWindowPositionY + mOffsetY; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 871 | mFrameDrawScheduled = false; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | mRenderer.draw(callback); |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 875 | if (mCallback != null) { |
| 876 | mCallback.onOperationComplete(); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 881 | /** |
| 882 | * Builder class for {@link Magnifier} objects. |
| 883 | */ |
| 884 | public static class Builder { |
| 885 | private @NonNull View mView; |
| 886 | private @Px @IntRange(from = 0) int mWidth; |
| 887 | private @Px @IntRange(from = 0) int mHeight; |
| 888 | private float mZoom; |
| 889 | private @FloatRange(from = 0f) float mElevation; |
| 890 | private @FloatRange(from = 0f) float mCornerRadius; |
| 891 | private int mHorizontalDefaultSourceToMagnifierOffset; |
| 892 | private int mVerticalDefaultSourceToMagnifierOffset; |
| 893 | |
| 894 | /** |
| 895 | * Construct a new builder for {@link Magnifier} objects. |
| 896 | * @param view the view this magnifier is attached to |
| 897 | */ |
| 898 | public Builder(@NonNull View view) { |
| 899 | mView = Preconditions.checkNotNull(view); |
| 900 | applyDefaults(); |
| 901 | } |
| 902 | |
| 903 | private void applyDefaults() { |
| 904 | final Context context = mView.getContext(); |
| 905 | final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Magnifier, |
| 906 | R.attr.magnifierStyle, 0); |
| 907 | mWidth = a.getDimensionPixelSize(R.styleable.Magnifier_magnifierWidth, 0); |
| 908 | mHeight = a.getDimensionPixelSize(R.styleable.Magnifier_magnifierHeight, 0); |
| 909 | mElevation = a.getDimension(R.styleable.Magnifier_magnifierElevation, 0); |
| 910 | mCornerRadius = getDeviceDefaultDialogCornerRadius(); |
| 911 | mZoom = a.getFloat(R.styleable.Magnifier_magnifierZoom, 0); |
| 912 | mHorizontalDefaultSourceToMagnifierOffset = |
| 913 | a.getDimensionPixelSize(R.styleable.Magnifier_magnifierHorizontalOffset, 0); |
| 914 | mVerticalDefaultSourceToMagnifierOffset = |
| 915 | a.getDimensionPixelSize(R.styleable.Magnifier_magnifierVerticalOffset, 0); |
| 916 | a.recycle(); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * Returns the device default theme dialog corner radius attribute. |
| 921 | * We retrieve this from the device default theme to avoid |
| 922 | * using the values set in the custom application themes. |
| 923 | */ |
| 924 | private float getDeviceDefaultDialogCornerRadius() { |
| 925 | final Context deviceDefaultContext = |
| 926 | new ContextThemeWrapper(mView.getContext(), R.style.Theme_DeviceDefault); |
| 927 | final TypedArray ta = deviceDefaultContext.obtainStyledAttributes( |
| 928 | new int[]{android.R.attr.dialogCornerRadius}); |
| 929 | final float dialogCornerRadius = ta.getDimension(0, 0); |
| 930 | ta.recycle(); |
| 931 | return dialogCornerRadius; |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Sets the size of the magnifier window, in pixels. Defaults to (100dp, 48dp). |
| 936 | * Note that the size of the content being magnified and copied to the magnifier |
| 937 | * will be computed as (window width / zoom, window height / zoom). |
| 938 | * @param width the window width to be set |
| 939 | * @param height the window height to be set |
| 940 | */ |
| 941 | public Builder setSize(@Px @IntRange(from = 0) int width, |
| 942 | @Px @IntRange(from = 0) int height) { |
| 943 | Preconditions.checkArgumentPositive(width, "Width should be positive"); |
| 944 | Preconditions.checkArgumentPositive(height, "Height should be positive"); |
| 945 | mWidth = width; |
| 946 | mHeight = height; |
| 947 | return this; |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Sets the zoom to be applied to the chosen content before being copied to the magnifier. |
| 952 | * A content of size (content_width, content_height) will be magnified to |
| 953 | * (content_width * zoom, content_height * zoom), which will coincide with the size |
| 954 | * of the magnifier. A zoom of 1 will translate to no magnification (the content will |
| 955 | * be just copied to the magnifier with no scaling). The zoom defaults to 1.25. |
| 956 | * @param zoom the zoom to be set |
| 957 | */ |
| 958 | public Builder setZoom(@FloatRange(from = 0f) float zoom) { |
| 959 | Preconditions.checkArgumentPositive(zoom, "Zoom should be positive"); |
| 960 | mZoom = zoom; |
| 961 | return this; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Sets the elevation of the magnifier window, in pixels. Defaults to 4dp. |
| 966 | * @param elevation the elevation to be set |
| 967 | */ |
| 968 | public Builder setElevation(@Px @FloatRange(from = 0) float elevation) { |
| 969 | Preconditions.checkArgumentNonNegative(elevation, "Elevation should be non-negative"); |
| 970 | mElevation = elevation; |
| 971 | return this; |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * Sets the corner radius of the magnifier window, in pixels. |
| 976 | * Defaults to the corner radius defined in the device default theme. |
| 977 | * @param cornerRadius the corner radius to be set |
| 978 | */ |
| 979 | public Builder setCornerRadius(@Px @FloatRange(from = 0) float cornerRadius) { |
| 980 | Preconditions.checkArgumentNonNegative(cornerRadius, |
| 981 | "Corner radius should be non-negative"); |
| 982 | mCornerRadius = cornerRadius; |
| 983 | return this; |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Sets an offset, in pixels, that should be added to the content source center to obtain |
| 988 | * the position of the magnifier window, when the {@link #show(float, float)} |
| 989 | * method is called. The offset is ignored when {@link #show(float, float, float, float)} |
| 990 | * is used. The offset can be negative, and it defaults to (0dp, -42dp). |
| 991 | * @param horizontalOffset the horizontal component of the offset |
| 992 | * @param verticalOffset the vertical component of the offset |
| 993 | */ |
| 994 | public Builder setDefaultSourceToMagnifierOffset(@Px int horizontalOffset, |
| 995 | @Px int verticalOffset) { |
| 996 | mHorizontalDefaultSourceToMagnifierOffset = horizontalOffset; |
| 997 | mVerticalDefaultSourceToMagnifierOffset = verticalOffset; |
| 998 | return this; |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Builds a {@link Magnifier} instance based on the configuration of this {@link Builder}. |
| 1003 | */ |
| 1004 | public @NonNull Magnifier build() { |
| 1005 | return new Magnifier(this); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 1009 | // The rest of the file consists of test APIs. |
| 1010 | |
| 1011 | /** |
| 1012 | * See {@link #setOnOperationCompleteCallback(Callback)}. |
| 1013 | */ |
| 1014 | @TestApi |
| 1015 | private Callback mCallback; |
| 1016 | |
| 1017 | /** |
| 1018 | * Sets a callback which will be invoked at the end of the next |
| 1019 | * {@link #show(float, float)} or {@link #update()} operation. |
| 1020 | * |
| 1021 | * @hide |
| 1022 | */ |
| 1023 | @TestApi |
| 1024 | public void setOnOperationCompleteCallback(final Callback callback) { |
| 1025 | mCallback = callback; |
| 1026 | if (mWindow != null) { |
| 1027 | mWindow.mCallback = callback; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1028 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 1029 | } |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1030 | |
| 1031 | /** |
| 1032 | * @return the content being currently displayed in the magnifier, as bitmap |
| 1033 | * |
| 1034 | * @hide |
| 1035 | */ |
| 1036 | @TestApi |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1037 | public @Nullable Bitmap getContent() { |
| 1038 | if (mWindow == null) { |
| 1039 | return null; |
| 1040 | } |
| 1041 | synchronized (mWindow.mLock) { |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 1042 | return Bitmap.createScaledBitmap(mWindow.mBitmap, mWindowWidth, mWindowHeight, true); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1043 | } |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | /** |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 1047 | * @return the content to be magnified, as bitmap |
| 1048 | * |
| 1049 | * @hide |
| 1050 | */ |
| 1051 | @TestApi |
| 1052 | public @Nullable Bitmap getOriginalContent() { |
| 1053 | if (mWindow == null) { |
| 1054 | return null; |
| 1055 | } |
| 1056 | synchronized (mWindow.mLock) { |
| 1057 | return Bitmap.createBitmap(mWindow.mBitmap); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /** |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1062 | * @return the size of the magnifier window in dp |
| 1063 | * |
| 1064 | * @hide |
| 1065 | */ |
| 1066 | @TestApi |
| 1067 | public static PointF getMagnifierDefaultSize() { |
| 1068 | final Resources resources = Resources.getSystem(); |
| 1069 | final float density = resources.getDisplayMetrics().density; |
| 1070 | final PointF size = new PointF(); |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 1071 | size.x = resources.getDimension(R.dimen.magnifier_width) / density; |
| 1072 | size.y = resources.getDimension(R.dimen.magnifier_height) / density; |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1073 | return size; |
| 1074 | } |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 1075 | |
| 1076 | /** |
| 1077 | * @hide |
| 1078 | */ |
| 1079 | @TestApi |
| 1080 | public interface Callback { |
| 1081 | /** |
| 1082 | * Callback called after the drawing for a magnifier update has happened. |
| 1083 | */ |
| 1084 | void onOperationComplete(); |
| 1085 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 1086 | } |