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 | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 21 | import android.annotation.IntRange; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 24 | import android.annotation.Px; |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 26 | import android.annotation.UiThread; |
| 27 | import android.content.Context; |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 28 | import android.content.res.Resources; |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 29 | import android.content.res.TypedArray; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 31 | import android.graphics.Canvas; |
Mihai Popa | d870b88 | 2018-02-27 14:25:52 +0000 | [diff] [blame] | 32 | import android.graphics.Color; |
Adrian Roos | 60f5929 | 2018-08-24 16:29:06 +0200 | [diff] [blame] | 33 | import android.graphics.Insets; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 34 | import android.graphics.Outline; |
| 35 | import android.graphics.Paint; |
| 36 | import android.graphics.PixelFormat; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 37 | import android.graphics.Point; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 38 | import android.graphics.PointF; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 39 | import android.graphics.RecordingCanvas; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 40 | import android.graphics.Rect; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 41 | import android.graphics.RenderNode; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 42 | import android.graphics.drawable.ColorDrawable; |
| 43 | import android.graphics.drawable.Drawable; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 44 | import android.os.Handler; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 45 | import android.os.HandlerThread; |
| 46 | import android.os.Message; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 47 | import android.util.Log; |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 48 | import android.view.ContextThemeWrapper; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 49 | import android.view.Display; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 50 | import android.view.PixelCopy; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 51 | import android.view.Surface; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 52 | import android.view.SurfaceControl; |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 53 | import android.view.SurfaceHolder; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 54 | import android.view.SurfaceSession; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 55 | import android.view.SurfaceView; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 56 | import android.view.ThreadedRenderer; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 57 | import android.view.View; |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 58 | import android.view.ViewRootImpl; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 59 | |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 60 | import com.android.internal.R; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 61 | import com.android.internal.util.Preconditions; |
| 62 | |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 63 | import java.lang.annotation.Retention; |
| 64 | import java.lang.annotation.RetentionPolicy; |
Daulet Zhanguzin | cb0d19b | 2019-12-18 15:08:09 +0000 | [diff] [blame] | 65 | import java.util.Objects; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 66 | |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 67 | /** |
Andrei Stingaceanu | d6644dc | 2017-11-21 14:53:38 +0000 | [diff] [blame] | 68 | * 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] | 69 | */ |
Andrei Stingaceanu | d6644dc | 2017-11-21 14:53:38 +0000 | [diff] [blame] | 70 | @UiThread |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 71 | public final class Magnifier { |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 72 | private static final String TAG = "Magnifier"; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 73 | // Use this to specify that a previous configuration value does not exist. |
| 74 | private static final int NONEXISTENT_PREVIOUS_CONFIG_VALUE = -1; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 75 | // The callbacks of the pixel copy requests will be invoked on |
| 76 | // the Handler of this Thread when the copy is finished. |
| 77 | private static final HandlerThread sPixelCopyHandlerThread = |
| 78 | new HandlerThread("magnifier pixel copy result handler"); |
| 79 | |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 80 | // The view to which this magnifier is attached. |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 81 | private final View mView; |
Mihai Popa | 1d1ed0c | 2018-01-12 12:38:12 +0000 | [diff] [blame] | 82 | // The coordinates of the view in the surface. |
| 83 | private final int[] mViewCoordinatesInSurface; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 84 | // The window containing the magnifier. |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 85 | private InternalPopupWindow mWindow; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 86 | // The width of the window containing the magnifier. |
| 87 | private final int mWindowWidth; |
| 88 | // The height of the window containing the magnifier. |
| 89 | private final int mWindowHeight; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 90 | // The zoom applied to the view region copied to the magnifier view. |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 91 | private float mZoom; |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 92 | // The width of the content that will be copied to the magnifier. |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 93 | private int mSourceWidth; |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 94 | // The height of the content that will be copied to the magnifier. |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 95 | private int mSourceHeight; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 96 | // Whether the zoom of the magnifier or the view position have changed since last content copy. |
| 97 | private boolean mDirtyState; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 98 | // The elevation of the window containing the magnifier. |
| 99 | private final float mWindowElevation; |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 100 | // The corner radius of the window containing the magnifier. |
| 101 | private final float mWindowCornerRadius; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 102 | // The overlay to be drawn on the top of the magnifier content. |
| 103 | private final Drawable mOverlay; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 104 | // The horizontal offset between the source and window coords when #show(float, float) is used. |
| 105 | private final int mDefaultHorizontalSourceToMagnifierOffset; |
| 106 | // The vertical offset between the source and window coords when #show(float, float) is used. |
| 107 | private final int mDefaultVerticalSourceToMagnifierOffset; |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 108 | // Whether the area where the magnifier can be positioned will be clipped to the main window |
| 109 | // and within system insets. |
| 110 | private final boolean mClippingEnabled; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 111 | // The behavior of the left bound of the rectangle where the content can be copied from. |
| 112 | private @SourceBound int mLeftContentBound; |
| 113 | // The behavior of the top bound of the rectangle where the content can be copied from. |
| 114 | private @SourceBound int mTopContentBound; |
| 115 | // The behavior of the right bound of the rectangle where the content can be copied from. |
| 116 | private @SourceBound int mRightContentBound; |
| 117 | // The behavior of the bottom bound of the rectangle where the content can be copied from. |
| 118 | private @SourceBound int mBottomContentBound; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 119 | // The parent surface for the magnifier surface. |
| 120 | private SurfaceInfo mParentSurface; |
| 121 | // The surface where the content will be copied from. |
| 122 | private SurfaceInfo mContentCopySurface; |
| 123 | // The center coordinates of the window containing the magnifier. |
| 124 | private final Point mWindowCoords = new Point(); |
| 125 | // The center coordinates of the content to be magnified, |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 126 | // clamped inside the visible region of the magnified view. |
| 127 | private final Point mClampedCenterZoomCoords = new Point(); |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 128 | // Variables holding previous states, used for detecting redundant calls and invalidation. |
| 129 | private final Point mPrevStartCoordsInSurface = new Point( |
| 130 | NONEXISTENT_PREVIOUS_CONFIG_VALUE, NONEXISTENT_PREVIOUS_CONFIG_VALUE); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 131 | private final PointF mPrevShowSourceCoords = new PointF( |
| 132 | NONEXISTENT_PREVIOUS_CONFIG_VALUE, NONEXISTENT_PREVIOUS_CONFIG_VALUE); |
| 133 | private final PointF mPrevShowWindowCoords = new PointF( |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 134 | NONEXISTENT_PREVIOUS_CONFIG_VALUE, NONEXISTENT_PREVIOUS_CONFIG_VALUE); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 135 | // Rectangle defining the view surface area we pixel copy content from. |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 136 | private final Rect mPixelCopyRequestRect = new Rect(); |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 137 | // Lock to synchronize between the UI thread and the thread that handles pixel copy results. |
| 138 | // Only sync mWindow writes from UI thread with mWindow reads from sPixelCopyHandlerThread. |
| 139 | private final Object mLock = new Object(); |
Bin Chen | d263141 | 2019-05-10 16:05:05 +0800 | [diff] [blame] | 140 | // The lock used to synchronize the UI and render threads when a #dismiss is performed. |
| 141 | private final Object mDestroyLock = new Object(); |
Andrei Stingaceanu | 15af561 | 2017-10-13 12:53:23 +0100 | [diff] [blame] | 142 | |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 143 | /** |
| 144 | * Initializes a magnifier. |
| 145 | * |
| 146 | * @param view the view for which this magnifier is attached |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 147 | * |
Mihai Popa | b6ca909 | 2018-09-24 21:14:50 +0100 | [diff] [blame] | 148 | * @deprecated Please use {@link Builder} instead |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 149 | */ |
Mihai Popa | b6ca909 | 2018-09-24 21:14:50 +0100 | [diff] [blame] | 150 | @Deprecated |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 151 | public Magnifier(@NonNull View view) { |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 152 | this(createBuilderWithOldMagnifierDefaults(view)); |
| 153 | } |
| 154 | |
| 155 | static Builder createBuilderWithOldMagnifierDefaults(final View view) { |
| 156 | final Builder params = new Builder(view); |
| 157 | final Context context = view.getContext(); |
| 158 | final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Magnifier, |
| 159 | R.attr.magnifierStyle, 0); |
| 160 | params.mWidth = a.getDimensionPixelSize(R.styleable.Magnifier_magnifierWidth, 0); |
| 161 | params.mHeight = a.getDimensionPixelSize(R.styleable.Magnifier_magnifierHeight, 0); |
| 162 | params.mElevation = a.getDimension(R.styleable.Magnifier_magnifierElevation, 0); |
| 163 | params.mCornerRadius = getDeviceDefaultDialogCornerRadius(context); |
| 164 | params.mZoom = a.getFloat(R.styleable.Magnifier_magnifierZoom, 0); |
| 165 | params.mHorizontalDefaultSourceToMagnifierOffset = |
| 166 | a.getDimensionPixelSize(R.styleable.Magnifier_magnifierHorizontalOffset, 0); |
| 167 | params.mVerticalDefaultSourceToMagnifierOffset = |
| 168 | a.getDimensionPixelSize(R.styleable.Magnifier_magnifierVerticalOffset, 0); |
| 169 | params.mOverlay = new ColorDrawable(a.getColor( |
| 170 | R.styleable.Magnifier_magnifierColorOverlay, Color.TRANSPARENT)); |
| 171 | a.recycle(); |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 172 | params.mClippingEnabled = true; |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 173 | params.mLeftContentBound = SOURCE_BOUND_MAX_VISIBLE; |
| 174 | params.mTopContentBound = SOURCE_BOUND_MAX_IN_SURFACE; |
| 175 | params.mRightContentBound = SOURCE_BOUND_MAX_VISIBLE; |
| 176 | params.mBottomContentBound = SOURCE_BOUND_MAX_IN_SURFACE; |
| 177 | return params; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns the device default theme dialog corner radius attribute. |
| 182 | * We retrieve this from the device default theme to avoid |
| 183 | * using the values set in the custom application themes. |
| 184 | */ |
| 185 | private static float getDeviceDefaultDialogCornerRadius(final Context context) { |
| 186 | final Context deviceDefaultContext = |
| 187 | new ContextThemeWrapper(context, R.style.Theme_DeviceDefault); |
| 188 | final TypedArray ta = deviceDefaultContext.obtainStyledAttributes( |
| 189 | new int[]{android.R.attr.dialogCornerRadius}); |
| 190 | final float dialogCornerRadius = ta.getDimension(0, 0); |
| 191 | ta.recycle(); |
| 192 | return dialogCornerRadius; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | private Magnifier(@NonNull Builder params) { |
| 196 | // Copy params from builder. |
| 197 | mView = params.mView; |
| 198 | mWindowWidth = params.mWidth; |
| 199 | mWindowHeight = params.mHeight; |
| 200 | mZoom = params.mZoom; |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 201 | mSourceWidth = Math.round(mWindowWidth / mZoom); |
| 202 | mSourceHeight = Math.round(mWindowHeight / mZoom); |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 203 | mWindowElevation = params.mElevation; |
| 204 | mWindowCornerRadius = params.mCornerRadius; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 205 | mOverlay = params.mOverlay; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 206 | mDefaultHorizontalSourceToMagnifierOffset = |
| 207 | params.mHorizontalDefaultSourceToMagnifierOffset; |
| 208 | mDefaultVerticalSourceToMagnifierOffset = |
| 209 | params.mVerticalDefaultSourceToMagnifierOffset; |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 210 | mClippingEnabled = params.mClippingEnabled; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 211 | mLeftContentBound = params.mLeftContentBound; |
| 212 | mTopContentBound = params.mTopContentBound; |
| 213 | mRightContentBound = params.mRightContentBound; |
| 214 | mBottomContentBound = params.mBottomContentBound; |
Mihai Popa | 1d1ed0c | 2018-01-12 12:38:12 +0000 | [diff] [blame] | 215 | // The view's surface coordinates will not be updated until the magnifier is first shown. |
| 216 | mViewCoordinatesInSurface = new int[2]; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 217 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 218 | |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 219 | static { |
| 220 | sPixelCopyHandlerThread.start(); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 224 | * Shows the magnifier on the screen. The method takes the coordinates of the center |
| 225 | * of the content source going to be magnified and copied to the magnifier. The coordinates |
| 226 | * are relative to the top left corner of the magnified view. The magnifier will be |
| 227 | * positioned such that its center will be at the default offset from the center of the source. |
| 228 | * The default offset can be specified using the method |
| 229 | * {@link Builder#setDefaultSourceToMagnifierOffset(int, int)}. If the offset should |
| 230 | * be different across calls to this method, you should consider to use method |
| 231 | * {@link #show(float, float, float, float)} instead. |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 232 | * |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 233 | * @param sourceCenterX horizontal coordinate of the source center, relative to the view |
| 234 | * @param sourceCenterY vertical coordinate of the source center, relative to the view |
| 235 | * |
| 236 | * @see Builder#setDefaultSourceToMagnifierOffset(int, int) |
| 237 | * @see Builder#getDefaultHorizontalSourceToMagnifierOffset() |
| 238 | * @see Builder#getDefaultVerticalSourceToMagnifierOffset() |
| 239 | * @see #show(float, float, float, float) |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 240 | */ |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 241 | public void show(@FloatRange(from = 0) float sourceCenterX, |
| 242 | @FloatRange(from = 0) float sourceCenterY) { |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 243 | show(sourceCenterX, sourceCenterY, |
| 244 | sourceCenterX + mDefaultHorizontalSourceToMagnifierOffset, |
| 245 | sourceCenterY + mDefaultVerticalSourceToMagnifierOffset); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 249 | * Shows the magnifier on the screen at a position that is independent from its content |
| 250 | * position. The first two arguments represent the coordinates of the center of the |
| 251 | * content source going to be magnified and copied to the magnifier. The last two arguments |
| 252 | * represent the coordinates of the center of the magnifier itself. All four coordinates |
| 253 | * are relative to the top left corner of the magnified view. If you consider using this |
| 254 | * method such that the offset between the source center and the magnifier center coordinates |
| 255 | * remains constant, you should consider using method {@link #show(float, float)} instead. |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 256 | * |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 257 | * @param sourceCenterX horizontal coordinate of the source center relative to the view |
| 258 | * @param sourceCenterY vertical coordinate of the source center, relative to the view |
| 259 | * @param magnifierCenterX horizontal coordinate of the magnifier center, relative to the view |
| 260 | * @param magnifierCenterY vertical coordinate of the magnifier center, relative to the view |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 261 | */ |
| 262 | public void show(@FloatRange(from = 0) float sourceCenterX, |
| 263 | @FloatRange(from = 0) float sourceCenterY, |
| 264 | float magnifierCenterX, float magnifierCenterY) { |
Andrei Stingaceanu | 451f947 | 2017-10-13 16:41:28 +0100 | [diff] [blame] | 265 | |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 266 | obtainSurfaces(); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 267 | obtainContentCoordinates(sourceCenterX, sourceCenterY); |
| 268 | obtainWindowCoordinates(magnifierCenterX, magnifierCenterY); |
Andrei Stingaceanu | ca189fe | 2017-10-19 17:02:22 +0100 | [diff] [blame] | 269 | |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 270 | final int startX = mClampedCenterZoomCoords.x - mSourceWidth / 2; |
| 271 | final int startY = mClampedCenterZoomCoords.y - mSourceHeight / 2; |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 272 | if (sourceCenterX != mPrevShowSourceCoords.x || sourceCenterY != mPrevShowSourceCoords.y |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 273 | || mDirtyState) { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 274 | if (mWindow == null) { |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 275 | synchronized (mLock) { |
| 276 | mWindow = new InternalPopupWindow(mView.getContext(), mView.getDisplay(), |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 277 | mParentSurface.mSurfaceControl, mWindowWidth, mWindowHeight, |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 278 | mWindowElevation, mWindowCornerRadius, |
| 279 | mOverlay != null ? mOverlay : new ColorDrawable(Color.TRANSPARENT), |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 280 | Handler.getMain() /* draw the magnifier on the UI thread */, mLock, |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 281 | mCallback); |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 282 | } |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 283 | } |
| 284 | performPixelCopy(startX, startY, true /* update window position */); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 285 | } else if (magnifierCenterX != mPrevShowWindowCoords.x |
| 286 | || magnifierCenterY != mPrevShowWindowCoords.y) { |
| 287 | final Point windowCoords = getCurrentClampedWindowCoordinates(); |
| 288 | final InternalPopupWindow currentWindowInstance = mWindow; |
| 289 | sPixelCopyHandlerThread.getThreadHandler().post(() -> { |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 290 | synchronized (mLock) { |
Mihai Popa | ddcd5481 | 2018-09-03 17:25:54 +0100 | [diff] [blame] | 291 | if (mWindow != currentWindowInstance) { |
| 292 | // The magnifier was dismissed (and maybe shown again) in the meantime. |
| 293 | return; |
| 294 | } |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 295 | mWindow.setContentPositionForNextDraw(windowCoords.x, windowCoords.y); |
| 296 | } |
| 297 | }); |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 298 | } |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 299 | mPrevShowSourceCoords.x = sourceCenterX; |
| 300 | mPrevShowSourceCoords.y = sourceCenterY; |
| 301 | mPrevShowWindowCoords.x = magnifierCenterX; |
| 302 | mPrevShowWindowCoords.y = magnifierCenterY; |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
Andrei Stingaceanu | d6644dc | 2017-11-21 14:53:38 +0000 | [diff] [blame] | 306 | * 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] | 307 | */ |
| 308 | public void dismiss() { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 309 | if (mWindow != null) { |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 310 | synchronized (mLock) { |
| 311 | mWindow.destroy(); |
| 312 | mWindow = null; |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 313 | } |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 314 | mPrevShowSourceCoords.x = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
| 315 | mPrevShowSourceCoords.y = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
| 316 | mPrevShowWindowCoords.x = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
| 317 | mPrevShowWindowCoords.y = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
Mihai Popa | 953b134 | 2018-03-21 18:05:13 +0000 | [diff] [blame] | 318 | mPrevStartCoordsInSurface.x = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
| 319 | mPrevStartCoordsInSurface.y = NONEXISTENT_PREVIOUS_CONFIG_VALUE; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 320 | } |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 321 | } |
Andrei Stingaceanu | 15af561 | 2017-10-13 12:53:23 +0100 | [diff] [blame] | 322 | |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 323 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 324 | * Asks the magnifier to update its content. It uses the previous coordinates passed to |
| 325 | * {@link #show(float, float)} or {@link #show(float, float, float, float)}. The |
| 326 | * method only has effect if the magnifier is currently showing. |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 327 | */ |
| 328 | public void update() { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 329 | if (mWindow != null) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 330 | obtainSurfaces(); |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 331 | if (!mDirtyState) { |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 332 | // Update the content shown in the magnifier. |
| 333 | performPixelCopy(mPrevStartCoordsInSurface.x, mPrevStartCoordsInSurface.y, |
| 334 | false /* update window position */); |
| 335 | } else { |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 336 | // If for example the zoom has changed, we cannot use the same top left |
| 337 | // coordinates as before, so just #show again to have them recomputed. |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 338 | show(mPrevShowSourceCoords.x, mPrevShowSourceCoords.y, |
| 339 | mPrevShowWindowCoords.x, mPrevShowWindowCoords.y); |
| 340 | } |
Andrei Stingaceanu | 15af561 | 2017-10-13 12:53:23 +0100 | [diff] [blame] | 341 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 344 | /** |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 345 | * @return the width of the magnifier window, in pixels |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 346 | * @see Magnifier.Builder#setSize(int, int) |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 347 | */ |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 348 | @Px |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 349 | public int getWidth() { |
| 350 | return mWindowWidth; |
| 351 | } |
| 352 | |
| 353 | /** |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 354 | * @return the height of the magnifier window, in pixels |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 355 | * @see Magnifier.Builder#setSize(int, int) |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 356 | */ |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 357 | @Px |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 358 | public int getHeight() { |
| 359 | return mWindowHeight; |
| 360 | } |
| 361 | |
| 362 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 363 | * @return the initial width of the content magnified and copied to the magnifier, in pixels |
| 364 | * @see Magnifier.Builder#setSize(int, int) |
Mihai Popa | 27cf08f | 2019-01-10 19:59:29 +0000 | [diff] [blame] | 365 | * @see Magnifier.Builder#setInitialZoom(float) |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 366 | */ |
| 367 | @Px |
| 368 | public int getSourceWidth() { |
| 369 | return mSourceWidth; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * @return the initial height of the content magnified and copied to the magnifier, in pixels |
| 374 | * @see Magnifier.Builder#setSize(int, int) |
Mihai Popa | 27cf08f | 2019-01-10 19:59:29 +0000 | [diff] [blame] | 375 | * @see Magnifier.Builder#setInitialZoom(float) |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 376 | */ |
| 377 | @Px |
| 378 | public int getSourceHeight() { |
| 379 | return mSourceHeight; |
| 380 | } |
| 381 | |
| 382 | /** |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 383 | * Sets the zoom to be applied to the chosen content before being copied to the magnifier popup. |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 384 | * The change will become effective at the next #show or #update call. |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 385 | * @param zoom the zoom to be set |
| 386 | */ |
| 387 | public void setZoom(@FloatRange(from = 0f) float zoom) { |
| 388 | Preconditions.checkArgumentPositive(zoom, "Zoom should be positive"); |
| 389 | mZoom = zoom; |
| 390 | mSourceWidth = Math.round(mWindowWidth / mZoom); |
| 391 | mSourceHeight = Math.round(mWindowHeight / mZoom); |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 392 | mDirtyState = true; |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /** |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 396 | * 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] | 397 | * 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] | 398 | * of the content being magnified will be (width / x, height / x). |
| 399 | * @return the zoom applied to the content |
Mihai Popa | 27cf08f | 2019-01-10 19:59:29 +0000 | [diff] [blame] | 400 | * @see Magnifier.Builder#setInitialZoom(float) |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 401 | */ |
| 402 | public float getZoom() { |
| 403 | return mZoom; |
| 404 | } |
| 405 | |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 406 | /** |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 407 | * @return the elevation set for the magnifier window, in pixels |
| 408 | * @see Magnifier.Builder#setElevation(float) |
| 409 | */ |
| 410 | @Px |
| 411 | public float getElevation() { |
| 412 | return mWindowElevation; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * @return the corner radius of the magnifier window, in pixels |
| 417 | * @see Magnifier.Builder#setCornerRadius(float) |
| 418 | */ |
| 419 | @Px |
| 420 | public float getCornerRadius() { |
| 421 | return mWindowCornerRadius; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Returns the horizontal offset, in pixels, to be applied to the source center position |
| 426 | * to obtain the magnifier center position when {@link #show(float, float)} is called. |
| 427 | * 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] | 428 | * |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 429 | * @return the default horizontal offset between the source center and the magnifier |
| 430 | * @see Magnifier.Builder#setDefaultSourceToMagnifierOffset(int, int) |
| 431 | * @see Magnifier#show(float, float) |
| 432 | */ |
| 433 | @Px |
| 434 | public int getDefaultHorizontalSourceToMagnifierOffset() { |
| 435 | return mDefaultHorizontalSourceToMagnifierOffset; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Returns the vertical offset, in pixels, to be applied to the source center position |
| 440 | * to obtain the magnifier center position when {@link #show(float, float)} is called. |
| 441 | * The value is ignored when {@link #show(float, float, float, float)} is used instead. |
| 442 | * |
| 443 | * @return the default vertical offset between the source center and the magnifier |
| 444 | * @see Magnifier.Builder#setDefaultSourceToMagnifierOffset(int, int) |
| 445 | * @see Magnifier#show(float, float) |
| 446 | */ |
| 447 | @Px |
| 448 | public int getDefaultVerticalSourceToMagnifierOffset() { |
| 449 | return mDefaultVerticalSourceToMagnifierOffset; |
| 450 | } |
| 451 | |
| 452 | /** |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 453 | * Returns the overlay to be drawn on the top of the magnifier, or |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 454 | * {@code null} if no overlay should be drawn. |
| 455 | * @return the overlay |
| 456 | * @see Magnifier.Builder#setOverlay(Drawable) |
| 457 | */ |
| 458 | @Nullable |
| 459 | public Drawable getOverlay() { |
| 460 | return mOverlay; |
| 461 | } |
| 462 | |
| 463 | /** |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 464 | * Returns whether the magnifier position will be adjusted such that the magnifier will be |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 465 | * fully within the bounds of the main application window, by also avoiding any overlap |
| 466 | * with system insets (such as the one corresponding to the status bar) i.e. whether the |
| 467 | * area where the magnifier can be positioned will be clipped to the main application window |
| 468 | * and the system insets. |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 469 | * @return whether the magnifier position will be adjusted |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 470 | * @see Magnifier.Builder#setClippingEnabled(boolean) |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 471 | */ |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 472 | public boolean isClippingEnabled() { |
| 473 | return mClippingEnabled; |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /** |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 477 | * Returns the top left coordinates of the magnifier, relative to the main application |
| 478 | * window. They will be determined by the coordinates of the last {@link #show(float, float)} |
| 479 | * or {@link #show(float, float, float, float)} call, adjusted to take into account any |
| 480 | * potential clamping behavior. The method can be used immediately after a #show |
| 481 | * call to find out where the magnifier will be positioned. However, the position of the |
| 482 | * magnifier will not be updated visually in the same frame, due to the async nature of |
| 483 | * the content copying and of the magnifier rendering. |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 484 | * The method will return {@code null} if #show has not yet been called, or if the last |
| 485 | * operation performed was a #dismiss. |
| 486 | * |
| 487 | * @return the top left coordinates of the magnifier |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 488 | */ |
| 489 | @Nullable |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 490 | public Point getPosition() { |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 491 | if (mWindow == null) { |
| 492 | return null; |
| 493 | } |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 494 | final Point position = getCurrentClampedWindowCoordinates(); |
| 495 | position.offset(-mParentSurface.mInsets.left, -mParentSurface.mInsets.top); |
| 496 | return new Point(position); |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Returns the top left coordinates of the magnifier source (i.e. the view region going to |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 501 | * be magnified and copied to the magnifier), relative to the window or surface the content |
| 502 | * is copied from. The content will be copied: |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 503 | * - if the magnified view is a {@link SurfaceView}, from the surface backing it |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 504 | * - otherwise, from the surface backing the main application window, and the coordinates |
| 505 | * returned will be relative to the main application window |
Mihai Popa | c2e0bee | 2018-07-19 12:18:30 +0100 | [diff] [blame] | 506 | * The method will return {@code null} if #show has not yet been called, or if the last |
| 507 | * operation performed was a #dismiss. |
| 508 | * |
| 509 | * @return the top left coordinates of the magnifier source |
| 510 | */ |
| 511 | @Nullable |
| 512 | public Point getSourcePosition() { |
| 513 | if (mWindow == null) { |
| 514 | return null; |
| 515 | } |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 516 | final Point position = new Point(mPixelCopyRequestRect.left, mPixelCopyRequestRect.top); |
| 517 | position.offset(-mContentCopySurface.mInsets.left, -mContentCopySurface.mInsets.top); |
| 518 | return new Point(position); |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 521 | /** |
| 522 | * Retrieves the surfaces used by the magnifier: |
| 523 | * - a parent surface for the magnifier surface. This will usually be the main app window. |
| 524 | * - a surface where the magnified content will be copied from. This will be the main app |
| 525 | * window unless the magnified view is a SurfaceView, in which case its backing surface |
| 526 | * will be used. |
| 527 | */ |
| 528 | private void obtainSurfaces() { |
| 529 | // Get the main window surface. |
| 530 | SurfaceInfo validMainWindowSurface = SurfaceInfo.NULL; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 531 | if (mView.getViewRootImpl() != null) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 532 | final ViewRootImpl viewRootImpl = mView.getViewRootImpl(); |
| 533 | final Surface mainWindowSurface = viewRootImpl.mSurface; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 534 | if (mainWindowSurface != null && mainWindowSurface.isValid()) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 535 | final Rect surfaceInsets = viewRootImpl.mWindowAttributes.surfaceInsets; |
| 536 | final int surfaceWidth = |
| 537 | viewRootImpl.getWidth() + surfaceInsets.left + surfaceInsets.right; |
| 538 | final int surfaceHeight = |
| 539 | viewRootImpl.getHeight() + surfaceInsets.top + surfaceInsets.bottom; |
| 540 | validMainWindowSurface = |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 541 | new SurfaceInfo(viewRootImpl.getSurfaceControl(), mainWindowSurface, |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 542 | surfaceWidth, surfaceHeight, surfaceInsets, true); |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 543 | } |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 544 | } |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 545 | // Get the surface backing the magnified view, if it is a SurfaceView. |
| 546 | SurfaceInfo validSurfaceViewSurface = SurfaceInfo.NULL; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 547 | if (mView instanceof SurfaceView) { |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 548 | final SurfaceControl sc = ((SurfaceView) mView).getSurfaceControl(); |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 549 | final SurfaceHolder surfaceHolder = ((SurfaceView) mView).getHolder(); |
| 550 | final Surface surfaceViewSurface = surfaceHolder.getSurface(); |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 551 | |
| 552 | if (sc != null && sc.isValid()) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 553 | final Rect surfaceFrame = surfaceHolder.getSurfaceFrame(); |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 554 | validSurfaceViewSurface = new SurfaceInfo(sc, surfaceViewSurface, |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 555 | surfaceFrame.right, surfaceFrame.bottom, new Rect(), false); |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 556 | } |
| 557 | } |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 558 | |
| 559 | // Choose the parent surface for the magnifier and the source surface for the content. |
| 560 | mParentSurface = validMainWindowSurface != SurfaceInfo.NULL |
| 561 | ? validMainWindowSurface : validSurfaceViewSurface; |
| 562 | mContentCopySurface = mView instanceof SurfaceView |
| 563 | ? validSurfaceViewSurface : validMainWindowSurface; |
Mihai Popa | 17ea305 | 2018-03-06 14:24:07 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 566 | /** |
| 567 | * Computes the coordinates of the center of the content going to be displayed in the |
| 568 | * magnifier. These are relative to the surface the content is copied from. |
| 569 | */ |
| 570 | private void obtainContentCoordinates(final float xPosInView, final float yPosInView) { |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 571 | final int prevViewXInSurface = mViewCoordinatesInSurface[0]; |
| 572 | final int prevViewYInSurface = mViewCoordinatesInSurface[1]; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 573 | mView.getLocationInSurface(mViewCoordinatesInSurface); |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 574 | if (mViewCoordinatesInSurface[0] != prevViewXInSurface |
| 575 | || mViewCoordinatesInSurface[1] != prevViewYInSurface) { |
| 576 | mDirtyState = true; |
| 577 | } |
| 578 | |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 579 | final int zoomCenterX; |
| 580 | final int zoomCenterY; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 581 | if (mView instanceof SurfaceView) { |
| 582 | // 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] | 583 | zoomCenterX = Math.round(xPosInView); |
| 584 | zoomCenterY = Math.round(yPosInView); |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 585 | } else { |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 586 | zoomCenterX = Math.round(xPosInView + mViewCoordinatesInSurface[0]); |
| 587 | zoomCenterY = Math.round(yPosInView + mViewCoordinatesInSurface[1]); |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Mihai Popa | 520e4475 | 2019-01-29 21:26:26 +0000 | [diff] [blame] | 590 | final Rect[] bounds = new Rect[2]; // [MAX_IN_SURFACE, MAX_VISIBLE] |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 591 | // Obtain the surface bounds rectangle. |
| 592 | final Rect surfaceBounds = new Rect(0, 0, |
| 593 | mContentCopySurface.mWidth, mContentCopySurface.mHeight); |
| 594 | bounds[0] = surfaceBounds; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 595 | // Obtain the visible view region rectangle. |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 596 | final Rect viewVisibleRegion = new Rect(); |
| 597 | mView.getGlobalVisibleRect(viewVisibleRegion); |
| 598 | if (mView.getViewRootImpl() != null) { |
| 599 | // Clamping coordinates relative to the surface, not to the window. |
| 600 | final Rect surfaceInsets = mView.getViewRootImpl().mWindowAttributes.surfaceInsets; |
| 601 | viewVisibleRegion.offset(surfaceInsets.left, surfaceInsets.top); |
| 602 | } |
| 603 | if (mView instanceof SurfaceView) { |
| 604 | // If we copy content from a SurfaceView, clamp coordinates relative to it. |
| 605 | viewVisibleRegion.offset(-mViewCoordinatesInSurface[0], -mViewCoordinatesInSurface[1]); |
| 606 | } |
Mihai Popa | 520e4475 | 2019-01-29 21:26:26 +0000 | [diff] [blame] | 607 | bounds[1] = viewVisibleRegion; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 608 | |
| 609 | // Aggregate the above to obtain the bounds where the content copy will be restricted. |
| 610 | int resolvedLeft = Integer.MIN_VALUE; |
| 611 | for (int i = mLeftContentBound; i >= 0; --i) { |
| 612 | resolvedLeft = Math.max(resolvedLeft, bounds[i].left); |
| 613 | } |
| 614 | int resolvedTop = Integer.MIN_VALUE; |
| 615 | for (int i = mTopContentBound; i >= 0; --i) { |
| 616 | resolvedTop = Math.max(resolvedTop, bounds[i].top); |
| 617 | } |
| 618 | int resolvedRight = Integer.MAX_VALUE; |
| 619 | for (int i = mRightContentBound; i >= 0; --i) { |
| 620 | resolvedRight = Math.min(resolvedRight, bounds[i].right); |
| 621 | } |
| 622 | int resolvedBottom = Integer.MAX_VALUE; |
| 623 | for (int i = mBottomContentBound; i >= 0; --i) { |
| 624 | resolvedBottom = Math.min(resolvedBottom, bounds[i].bottom); |
| 625 | } |
| 626 | // Adjust <left-right> and <top-bottom> pairs of bounds to make sense. |
| 627 | resolvedLeft = Math.min(resolvedLeft, mContentCopySurface.mWidth - mSourceWidth); |
| 628 | resolvedTop = Math.min(resolvedTop, mContentCopySurface.mHeight - mSourceHeight); |
| 629 | if (resolvedLeft < 0 || resolvedTop < 0) { |
| 630 | Log.e(TAG, "Magnifier's content is copied from a surface smaller than" |
Mihai Popa | 3aa46b2 | 2019-05-07 22:29:52 +0100 | [diff] [blame] | 631 | + "the content requested size. The magnifier will be dismissed."); |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 632 | } |
| 633 | resolvedRight = Math.max(resolvedRight, resolvedLeft + mSourceWidth); |
| 634 | resolvedBottom = Math.max(resolvedBottom, resolvedTop + mSourceHeight); |
| 635 | |
| 636 | // Finally compute the coordinates of the source center. |
| 637 | mClampedCenterZoomCoords.x = Math.max(resolvedLeft + mSourceWidth / 2, Math.min( |
| 638 | zoomCenterX, resolvedRight - mSourceWidth / 2)); |
| 639 | mClampedCenterZoomCoords.y = Math.max(resolvedTop + mSourceHeight / 2, Math.min( |
| 640 | zoomCenterY, resolvedBottom - mSourceHeight / 2)); |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 643 | /** |
| 644 | * Computes the coordinates of the top left corner of the magnifier window. |
| 645 | * These are relative to the surface the magnifier window is attached to. |
| 646 | */ |
| 647 | private void obtainWindowCoordinates(final float xWindowPos, final float yWindowPos) { |
| 648 | final int windowCenterX; |
| 649 | final int windowCenterY; |
| 650 | if (mView instanceof SurfaceView) { |
| 651 | // No offset required if the backing Surface matches the size of the SurfaceView. |
| 652 | windowCenterX = Math.round(xWindowPos); |
| 653 | windowCenterY = Math.round(yWindowPos); |
| 654 | } else { |
| 655 | windowCenterX = Math.round(xWindowPos + mViewCoordinatesInSurface[0]); |
| 656 | windowCenterY = Math.round(yWindowPos + mViewCoordinatesInSurface[1]); |
| 657 | } |
| 658 | |
| 659 | mWindowCoords.x = windowCenterX - mWindowWidth / 2; |
| 660 | mWindowCoords.y = windowCenterY - mWindowHeight / 2; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 661 | if (mParentSurface != mContentCopySurface) { |
| 662 | mWindowCoords.x += mViewCoordinatesInSurface[0]; |
| 663 | mWindowCoords.y += mViewCoordinatesInSurface[1]; |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 664 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 665 | } |
| 666 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 667 | private void performPixelCopy(final int startXInSurface, final int startYInSurface, |
| 668 | final boolean updateWindowPosition) { |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 669 | if (mContentCopySurface.mSurface == null || !mContentCopySurface.mSurface.isValid()) { |
Mihai Popa | 3aa46b2 | 2019-05-07 22:29:52 +0100 | [diff] [blame] | 670 | onPixelCopyFailed(); |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 671 | return; |
| 672 | } |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 673 | |
Mihai Popa | 953b134 | 2018-03-21 18:05:13 +0000 | [diff] [blame] | 674 | // Clamp window coordinates inside the parent surface, to avoid displaying |
| 675 | // the magnifier out of screen or overlapping with system insets. |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 676 | final Point windowCoords = getCurrentClampedWindowCoordinates(); |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 677 | |
| 678 | // Perform the pixel copy. |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 679 | mPixelCopyRequestRect.set(startXInSurface, |
| 680 | startYInSurface, |
| 681 | startXInSurface + mSourceWidth, |
| 682 | startYInSurface + mSourceHeight); |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 683 | final InternalPopupWindow currentWindowInstance = mWindow; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 684 | final Bitmap bitmap = |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 685 | Bitmap.createBitmap(mSourceWidth, mSourceHeight, Bitmap.Config.ARGB_8888); |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 686 | PixelCopy.request(mContentCopySurface.mSurface, mPixelCopyRequestRect, bitmap, |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 687 | result -> { |
Mihai Popa | 3aa46b2 | 2019-05-07 22:29:52 +0100 | [diff] [blame] | 688 | if (result != PixelCopy.SUCCESS) { |
| 689 | onPixelCopyFailed(); |
| 690 | return; |
| 691 | } |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 692 | synchronized (mLock) { |
| 693 | if (mWindow != currentWindowInstance) { |
| 694 | // The magnifier was dismissed (and maybe shown again) in the meantime. |
| 695 | return; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 696 | } |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 697 | if (updateWindowPosition) { |
| 698 | // TODO: pull the position update outside #performPixelCopy |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 699 | mWindow.setContentPositionForNextDraw(windowCoords.x, windowCoords.y); |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 700 | } |
| 701 | mWindow.updateContent(bitmap); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 702 | } |
Mihai Popa | 3589c2c | 2018-01-25 19:26:30 +0000 | [diff] [blame] | 703 | }, |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 704 | sPixelCopyHandlerThread.getThreadHandler()); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 705 | mPrevStartCoordsInSurface.x = startXInSurface; |
| 706 | mPrevStartCoordsInSurface.y = startYInSurface; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 707 | mDirtyState = false; |
Andrei Stingaceanu | 41589fa | 2017-11-02 13:54:10 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Mihai Popa | 3aa46b2 | 2019-05-07 22:29:52 +0100 | [diff] [blame] | 710 | private void onPixelCopyFailed() { |
| 711 | Log.e(TAG, "Magnifier failed to copy content from the view Surface. It will be dismissed."); |
| 712 | // Post to make sure #dismiss is done on the main thread. |
| 713 | Handler.getMain().postAtFrontOfQueue(() -> { |
| 714 | dismiss(); |
| 715 | if (mCallback != null) { |
| 716 | mCallback.onOperationComplete(); |
| 717 | } |
| 718 | }); |
| 719 | } |
| 720 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 721 | /** |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 722 | * Clamp window coordinates inside the surface the magnifier is attached to, to avoid |
| 723 | * displaying the magnifier out of screen or overlapping with system insets. |
| 724 | * @return the current window coordinates, after they are clamped inside the parent surface |
| 725 | */ |
| 726 | private Point getCurrentClampedWindowCoordinates() { |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 727 | if (!mClippingEnabled) { |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 728 | // No position adjustment should be done, so return the raw coordinates. |
| 729 | return new Point(mWindowCoords); |
| 730 | } |
| 731 | |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 732 | final Rect windowBounds; |
| 733 | if (mParentSurface.mIsMainWindowSurface) { |
Adrian Roos | 60f5929 | 2018-08-24 16:29:06 +0200 | [diff] [blame] | 734 | final Insets systemInsets = mView.getRootWindowInsets().getSystemWindowInsets(); |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 735 | windowBounds = new Rect( |
| 736 | systemInsets.left + mParentSurface.mInsets.left, |
| 737 | systemInsets.top + mParentSurface.mInsets.top, |
| 738 | mParentSurface.mWidth - systemInsets.right - mParentSurface.mInsets.right, |
| 739 | mParentSurface.mHeight - systemInsets.bottom |
| 740 | - mParentSurface.mInsets.bottom |
| 741 | ); |
Mihai Popa | 7433a07 | 2018-07-18 12:18:34 +0100 | [diff] [blame] | 742 | } else { |
| 743 | windowBounds = new Rect(0, 0, mParentSurface.mWidth, mParentSurface.mHeight); |
| 744 | } |
| 745 | final int windowCoordsX = Math.max(windowBounds.left, |
| 746 | Math.min(windowBounds.right - mWindowWidth, mWindowCoords.x)); |
| 747 | final int windowCoordsY = Math.max(windowBounds.top, |
| 748 | Math.min(windowBounds.bottom - mWindowHeight, mWindowCoords.y)); |
| 749 | return new Point(windowCoordsX, windowCoordsY); |
| 750 | } |
| 751 | |
| 752 | /** |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 753 | * Contains a surface and metadata corresponding to it. |
| 754 | */ |
| 755 | private static class SurfaceInfo { |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 756 | public static final SurfaceInfo NULL = new SurfaceInfo(null, null, 0, 0, null, false); |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 757 | |
| 758 | private Surface mSurface; |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 759 | private SurfaceControl mSurfaceControl; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 760 | private int mWidth; |
| 761 | private int mHeight; |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 762 | private Rect mInsets; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 763 | private boolean mIsMainWindowSurface; |
| 764 | |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 765 | SurfaceInfo(final SurfaceControl surfaceControl, final Surface surface, |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 766 | final int width, final int height, final Rect insets, |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 767 | final boolean isMainWindowSurface) { |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 768 | mSurfaceControl = surfaceControl; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 769 | mSurface = surface; |
| 770 | mWidth = width; |
| 771 | mHeight = height; |
Mihai Popa | ef45697 | 2019-02-18 15:17:09 +0000 | [diff] [blame] | 772 | mInsets = insets; |
Mihai Popa | f298068 | 2018-04-30 19:08:57 +0100 | [diff] [blame] | 773 | mIsMainWindowSurface = isMainWindowSurface; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | /** |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 778 | * Magnifier's own implementation of PopupWindow-similar floating window. |
| 779 | * This exists to ensure frame-synchronization between window position updates and window |
| 780 | * content updates. By using a PopupWindow, these events would happen in different frames, |
| 781 | * producing a shakiness effect for the magnifier content. |
| 782 | */ |
| 783 | private static class InternalPopupWindow { |
Mihai Popa | 819e90d | 2018-04-16 14:27:05 +0100 | [diff] [blame] | 784 | // The z of the magnifier surface, defining its z order in the list of |
| 785 | // siblings having the same parent surface (usually the main app surface). |
| 786 | private static final int SURFACE_Z = 5; |
Mihai Popa | d870b88 | 2018-02-27 14:25:52 +0000 | [diff] [blame] | 787 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 788 | // Display associated to the view the magnifier is attached to. |
| 789 | private final Display mDisplay; |
| 790 | // The size of the content of the magnifier. |
| 791 | private final int mContentWidth; |
| 792 | private final int mContentHeight; |
| 793 | // The size of the allocated surface. |
| 794 | private final int mSurfaceWidth; |
| 795 | private final int mSurfaceHeight; |
| 796 | // The insets of the content inside the allocated surface. |
| 797 | private final int mOffsetX; |
| 798 | private final int mOffsetY; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 799 | // The overlay to be drawn on the top of the content. |
| 800 | private final Drawable mOverlay; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 801 | // The surface we allocate for the magnifier content + shadow. |
| 802 | private final SurfaceSession mSurfaceSession; |
| 803 | private final SurfaceControl mSurfaceControl; |
Robert Carr | 64660be | 2019-08-26 16:20:55 -0700 | [diff] [blame] | 804 | private final SurfaceControl.Transaction mTransaction = new SurfaceControl.Transaction(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 805 | private final Surface mSurface; |
| 806 | // The renderer used for the allocated surface. |
| 807 | private final ThreadedRenderer.SimpleRenderer mRenderer; |
| 808 | // The RenderNode used to draw the magnifier content in the surface. |
| 809 | private final RenderNode mBitmapRenderNode; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 810 | // The RenderNode used to draw the overlay over the magnifier content. |
| 811 | private final RenderNode mOverlayRenderNode; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 812 | // The job that will be post'd to apply the pending magnifier updates to the surface. |
| 813 | private final Runnable mMagnifierUpdater; |
| 814 | // The handler where the magnifier updater jobs will be post'd. |
| 815 | private final Handler mHandler; |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 816 | // The callback to be run after the next draw. |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 817 | private Callback mCallback; |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 818 | // The position of the magnifier content when the last draw was requested. |
| 819 | private int mLastDrawContentPositionX; |
| 820 | private int mLastDrawContentPositionY; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 821 | |
| 822 | // Members below describe the state of the magnifier. Reads/writes to them |
| 823 | // have to be synchronized between the UI thread and the thread that handles |
| 824 | // the pixel copy results. This is the purpose of mLock. |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 825 | private final Object mLock; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 826 | // Whether a magnifier frame draw is currently pending in the UI thread queue. |
| 827 | private boolean mFrameDrawScheduled; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 828 | // The content bitmap, as returned by pixel copy. |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 829 | private Bitmap mBitmap; |
| 830 | // Whether the next draw will be the first one for the current instance. |
| 831 | private boolean mFirstDraw = true; |
| 832 | // The window position in the parent surface. Might be applied during the next draw, |
| 833 | // when mPendingWindowPositionUpdate is true. |
| 834 | private int mWindowPositionX; |
| 835 | private int mWindowPositionY; |
| 836 | private boolean mPendingWindowPositionUpdate; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 837 | |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 838 | // The current content of the magnifier. It is mBitmap + mOverlay, only used for testing. |
| 839 | private Bitmap mCurrentContent; |
| 840 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 841 | InternalPopupWindow(final Context context, final Display display, |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 842 | final SurfaceControl parentSurfaceControl, final int width, final int height, |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 843 | final float elevation, final float cornerRadius, final Drawable overlay, |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 844 | final Handler handler, final Object lock, final Callback callback) { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 845 | mDisplay = display; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 846 | mOverlay = overlay; |
Mihai Popa | 39a7133 | 2018-02-22 19:30:24 +0000 | [diff] [blame] | 847 | mLock = lock; |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 848 | mCallback = callback; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 849 | |
| 850 | mContentWidth = width; |
| 851 | mContentHeight = height; |
Mihai Popa | bd391f9 | 2019-01-08 20:07:01 +0000 | [diff] [blame] | 852 | mOffsetX = (int) (1.05f * elevation); |
| 853 | mOffsetY = (int) (1.05f * elevation); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 854 | // Setup the surface we will use for drawing the content and shadow. |
| 855 | mSurfaceWidth = mContentWidth + 2 * mOffsetX; |
| 856 | mSurfaceHeight = mContentHeight + 2 * mOffsetY; |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 857 | mSurfaceSession = new SurfaceSession(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 858 | mSurfaceControl = new SurfaceControl.Builder(mSurfaceSession) |
| 859 | .setFormat(PixelFormat.TRANSLUCENT) |
Vishnu Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 860 | .setBufferSize(mSurfaceWidth, mSurfaceHeight) |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 861 | .setName("magnifier surface") |
| 862 | .setFlags(SurfaceControl.HIDDEN) |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 863 | .setParent(parentSurfaceControl) |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 864 | .build(); |
| 865 | mSurface = new Surface(); |
| 866 | mSurface.copyFrom(mSurfaceControl); |
| 867 | |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 868 | // Setup the RenderNode tree. The root has two children, one containing the bitmap |
| 869 | // and one containing the overlay. We use a separate render node for the overlay |
| 870 | // to avoid drawing this as the same rate we do for content. |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 871 | mRenderer = new ThreadedRenderer.SimpleRenderer( |
| 872 | context, |
| 873 | "magnifier renderer", |
| 874 | mSurface |
| 875 | ); |
| 876 | mBitmapRenderNode = createRenderNodeForBitmap( |
| 877 | "magnifier content", |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 878 | elevation, |
| 879 | cornerRadius |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 880 | ); |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 881 | mOverlayRenderNode = createRenderNodeForOverlay( |
| 882 | "magnifier overlay", |
| 883 | cornerRadius |
| 884 | ); |
| 885 | setupOverlay(); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 886 | |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 887 | final RecordingCanvas canvas = mRenderer.getRootNode().beginRecording(width, height); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 888 | try { |
| 889 | canvas.insertReorderBarrier(); |
| 890 | canvas.drawRenderNode(mBitmapRenderNode); |
| 891 | canvas.insertInorderBarrier(); |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 892 | canvas.drawRenderNode(mOverlayRenderNode); |
| 893 | canvas.insertInorderBarrier(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 894 | } finally { |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 895 | mRenderer.getRootNode().endRecording(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 896 | } |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 897 | if (mCallback != null) { |
| 898 | mCurrentContent = |
| 899 | Bitmap.createBitmap(mContentWidth, mContentHeight, Bitmap.Config.ARGB_8888); |
| 900 | updateCurrentContentForTesting(); |
| 901 | } |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 902 | |
| 903 | // Initialize the update job and the handler where this will be post'd. |
| 904 | mHandler = handler; |
| 905 | mMagnifierUpdater = this::doDraw; |
| 906 | mFrameDrawScheduled = false; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 909 | private RenderNode createRenderNodeForBitmap(final String name, |
| 910 | final float elevation, final float cornerRadius) { |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 911 | final RenderNode bitmapRenderNode = RenderNode.create(name, null); |
| 912 | |
| 913 | // Define the position of the bitmap in the parent render node. The surface regions |
| 914 | // outside the bitmap are used to draw elevation. |
| 915 | bitmapRenderNode.setLeftTopRightBottom(mOffsetX, mOffsetY, |
| 916 | mOffsetX + mContentWidth, mOffsetY + mContentHeight); |
| 917 | bitmapRenderNode.setElevation(elevation); |
| 918 | |
| 919 | final Outline outline = new Outline(); |
Mihai Popa | fb4b6b8 | 2018-03-01 16:08:14 +0000 | [diff] [blame] | 920 | outline.setRoundRect(0, 0, mContentWidth, mContentHeight, cornerRadius); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 921 | outline.setAlpha(1.0f); |
| 922 | bitmapRenderNode.setOutline(outline); |
| 923 | bitmapRenderNode.setClipToOutline(true); |
| 924 | |
| 925 | // Create a dummy draw, which will be replaced later with real drawing. |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 926 | final RecordingCanvas canvas = bitmapRenderNode.beginRecording( |
| 927 | mContentWidth, mContentHeight); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 928 | try { |
| 929 | canvas.drawColor(0xFF00FF00); |
| 930 | } finally { |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 931 | bitmapRenderNode.endRecording(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | return bitmapRenderNode; |
| 935 | } |
| 936 | |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 937 | private RenderNode createRenderNodeForOverlay(final String name, final float cornerRadius) { |
| 938 | final RenderNode overlayRenderNode = RenderNode.create(name, null); |
| 939 | |
| 940 | // Define the position of the overlay in the parent render node. |
| 941 | // This coincides with the position of the content. |
| 942 | overlayRenderNode.setLeftTopRightBottom(mOffsetX, mOffsetY, |
| 943 | mOffsetX + mContentWidth, mOffsetY + mContentHeight); |
| 944 | |
| 945 | final Outline outline = new Outline(); |
| 946 | outline.setRoundRect(0, 0, mContentWidth, mContentHeight, cornerRadius); |
| 947 | outline.setAlpha(1.0f); |
| 948 | overlayRenderNode.setOutline(outline); |
| 949 | overlayRenderNode.setClipToOutline(true); |
| 950 | |
| 951 | return overlayRenderNode; |
| 952 | } |
| 953 | |
| 954 | private void setupOverlay() { |
| 955 | drawOverlay(); |
| 956 | |
| 957 | mOverlay.setCallback(new Drawable.Callback() { |
| 958 | @Override |
| 959 | public void invalidateDrawable(Drawable who) { |
| 960 | // When the overlay drawable is invalidated, redraw it to the render node. |
| 961 | drawOverlay(); |
| 962 | if (mCallback != null) { |
| 963 | updateCurrentContentForTesting(); |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | @Override |
| 968 | public void scheduleDrawable(Drawable who, Runnable what, long when) { |
| 969 | Handler.getMain().postAtTime(what, who, when); |
| 970 | } |
| 971 | |
| 972 | @Override |
| 973 | public void unscheduleDrawable(Drawable who, Runnable what) { |
| 974 | Handler.getMain().removeCallbacks(what, who); |
| 975 | } |
| 976 | }); |
| 977 | } |
| 978 | |
| 979 | private void drawOverlay() { |
| 980 | // Draw the drawable to the render node. This happens once during |
| 981 | // initialization and whenever the overlay drawable is invalidated. |
| 982 | final RecordingCanvas canvas = |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 983 | mOverlayRenderNode.beginRecording(mContentWidth, mContentHeight); |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 984 | try { |
| 985 | mOverlay.setBounds(0, 0, mContentWidth, mContentHeight); |
| 986 | mOverlay.draw(canvas); |
| 987 | } finally { |
| 988 | mOverlayRenderNode.endRecording(); |
| 989 | } |
| 990 | } |
| 991 | |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 992 | /** |
| 993 | * Sets the position of the magnifier content relative to the parent surface. |
| 994 | * 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] | 995 | * 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] | 996 | * |
| 997 | * @param contentX the x coordinate of the content |
| 998 | * @param contentY the y coordinate of the content |
| 999 | */ |
| 1000 | public void setContentPositionForNextDraw(final int contentX, final int contentY) { |
| 1001 | mWindowPositionX = contentX - mOffsetX; |
| 1002 | mWindowPositionY = contentY - mOffsetY; |
| 1003 | mPendingWindowPositionUpdate = true; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1004 | requestUpdate(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Sets the content that should be displayed in the magnifier. |
| 1009 | * The update happens immediately, and possibly triggers a pending window movement set |
| 1010 | * by {@link #setContentPositionForNextDraw(int, int)}. |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1011 | * 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] | 1012 | * |
| 1013 | * @param bitmap the content bitmap |
| 1014 | */ |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1015 | public void updateContent(final @NonNull Bitmap bitmap) { |
| 1016 | if (mBitmap != null) { |
| 1017 | mBitmap.recycle(); |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1018 | } |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1019 | mBitmap = bitmap; |
| 1020 | requestUpdate(); |
| 1021 | } |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1022 | |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1023 | private void requestUpdate() { |
| 1024 | if (mFrameDrawScheduled) { |
| 1025 | return; |
| 1026 | } |
| 1027 | final Message request = Message.obtain(mHandler, mMagnifierUpdater); |
| 1028 | request.setAsynchronous(true); |
| 1029 | request.sendToTarget(); |
| 1030 | mFrameDrawScheduled = true; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /** |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 1034 | * Destroys this instance. The method has to be called in a context holding {@link #mLock}. |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1035 | */ |
| 1036 | public void destroy() { |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 1037 | // Destroy the renderer. This will not proceed until pending frame callbacks complete. |
| 1038 | mRenderer.destroy(); |
| 1039 | mSurface.destroy(); |
chaviw | 9f6171e | 2019-06-07 16:33:50 -0700 | [diff] [blame] | 1040 | new SurfaceControl.Transaction().remove(mSurfaceControl).apply(); |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 1041 | mSurfaceSession.kill(); |
| 1042 | mHandler.removeCallbacks(mMagnifierUpdater); |
| 1043 | if (mBitmap != null) { |
| 1044 | mBitmap.recycle(); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1045 | } |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | private void doDraw() { |
| 1049 | final ThreadedRenderer.FrameDrawingCallback callback; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1050 | |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1051 | // Draw the current bitmap to the surface, and prepare the callback which updates the |
| 1052 | // surface position. These have to be in the same synchronized block, in order to |
| 1053 | // guarantee the consistency between the bitmap content and the surface position. |
| 1054 | synchronized (mLock) { |
| 1055 | if (!mSurface.isValid()) { |
| 1056 | // Probably #destroy() was called for the current instance, so we skip the draw. |
| 1057 | return; |
| 1058 | } |
| 1059 | |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 1060 | final RecordingCanvas canvas = |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 1061 | mBitmapRenderNode.beginRecording(mContentWidth, mContentHeight); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1062 | try { |
| 1063 | final Rect srcRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); |
| 1064 | final Rect dstRect = new Rect(0, 0, mContentWidth, mContentHeight); |
| 1065 | final Paint paint = new Paint(); |
| 1066 | paint.setFilterBitmap(true); |
| 1067 | canvas.drawBitmap(mBitmap, srcRect, dstRect, paint); |
| 1068 | } finally { |
John Reck | e57475e | 2019-02-20 17:39:52 -0800 | [diff] [blame] | 1069 | mBitmapRenderNode.endRecording(); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | if (mPendingWindowPositionUpdate || mFirstDraw) { |
| 1073 | // If the window has to be shown or moved, defer this until the next draw. |
| 1074 | final boolean firstDraw = mFirstDraw; |
| 1075 | mFirstDraw = false; |
| 1076 | final boolean updateWindowPosition = mPendingWindowPositionUpdate; |
| 1077 | mPendingWindowPositionUpdate = false; |
| 1078 | final int pendingX = mWindowPositionX; |
| 1079 | final int pendingY = mWindowPositionY; |
| 1080 | |
| 1081 | callback = frame -> { |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 1082 | if (!mSurface.isValid()) { |
| 1083 | return; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1084 | } |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 1085 | // Show or move the window at the content draw frame. |
Robert Carr | 64660be | 2019-08-26 16:20:55 -0700 | [diff] [blame] | 1086 | mTransaction.deferTransactionUntilSurface(mSurfaceControl, mSurface, frame); |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 1087 | if (updateWindowPosition) { |
Robert Carr | 64660be | 2019-08-26 16:20:55 -0700 | [diff] [blame] | 1088 | mTransaction.setPosition(mSurfaceControl, pendingX, pendingY); |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 1089 | } |
| 1090 | if (firstDraw) { |
Robert Carr | 64660be | 2019-08-26 16:20:55 -0700 | [diff] [blame] | 1091 | mTransaction.setLayer(mSurfaceControl, SURFACE_Z) |
| 1092 | .show(mSurfaceControl); |
| 1093 | |
Mihai Popa | afee43b | 2019-06-17 12:34:50 +0100 | [diff] [blame] | 1094 | } |
Robert Carr | 64660be | 2019-08-26 16:20:55 -0700 | [diff] [blame] | 1095 | mTransaction.apply(); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1096 | }; |
Mihai Popa | 6bf0b2f6 | 2019-02-26 18:01:37 +0000 | [diff] [blame] | 1097 | mRenderer.setLightCenter(mDisplay, pendingX, pendingY); |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1098 | } else { |
| 1099 | callback = null; |
| 1100 | } |
| 1101 | |
Mihai Popa | 63ee7f1 | 2018-04-05 12:01:53 +0100 | [diff] [blame] | 1102 | mLastDrawContentPositionX = mWindowPositionX + mOffsetX; |
| 1103 | mLastDrawContentPositionY = mWindowPositionY + mOffsetY; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1104 | mFrameDrawScheduled = false; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | mRenderer.draw(callback); |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 1108 | if (mCallback != null) { |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1109 | // The current content bitmap is only used in testing, so, for performance, |
| 1110 | // we only want to update it when running tests. For this, we check that |
| 1111 | // mCallback is not null, as it can only be set from a @TestApi. |
| 1112 | updateCurrentContentForTesting(); |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 1113 | mCallback.onOperationComplete(); |
| 1114 | } |
| 1115 | } |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1116 | |
| 1117 | /** |
| 1118 | * Updates mCurrentContent, which reproduces what is currently supposed to be |
| 1119 | * drawn in the magnifier. mCurrentContent is only used for testing, so this method |
| 1120 | * should only be called otherwise. |
| 1121 | */ |
| 1122 | private void updateCurrentContentForTesting() { |
| 1123 | final Canvas canvas = new Canvas(mCurrentContent); |
| 1124 | final Rect bounds = new Rect(0, 0, mContentWidth, mContentHeight); |
| 1125 | if (mBitmap != null && !mBitmap.isRecycled()) { |
| 1126 | final Rect originalBounds = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); |
| 1127 | canvas.drawBitmap(mBitmap, originalBounds, bounds, null); |
| 1128 | } |
| 1129 | mOverlay.setBounds(bounds); |
| 1130 | mOverlay.draw(canvas); |
| 1131 | } |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1134 | /** |
| 1135 | * Builder class for {@link Magnifier} objects. |
| 1136 | */ |
Mihai Popa | 53fd696 | 2019-03-05 20:37:35 +0000 | [diff] [blame] | 1137 | public static final class Builder { |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1138 | private @NonNull View mView; |
| 1139 | private @Px @IntRange(from = 0) int mWidth; |
| 1140 | private @Px @IntRange(from = 0) int mHeight; |
| 1141 | private float mZoom; |
| 1142 | private @FloatRange(from = 0f) float mElevation; |
| 1143 | private @FloatRange(from = 0f) float mCornerRadius; |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1144 | private @Nullable Drawable mOverlay; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1145 | private int mHorizontalDefaultSourceToMagnifierOffset; |
| 1146 | private int mVerticalDefaultSourceToMagnifierOffset; |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 1147 | private boolean mClippingEnabled; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1148 | private @SourceBound int mLeftContentBound; |
| 1149 | private @SourceBound int mTopContentBound; |
| 1150 | private @SourceBound int mRightContentBound; |
| 1151 | private @SourceBound int mBottomContentBound; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1152 | |
| 1153 | /** |
| 1154 | * Construct a new builder for {@link Magnifier} objects. |
| 1155 | * @param view the view this magnifier is attached to |
| 1156 | */ |
| 1157 | public Builder(@NonNull View view) { |
Daulet Zhanguzin | cb0d19b | 2019-12-18 15:08:09 +0000 | [diff] [blame] | 1158 | mView = Objects.requireNonNull(view); |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1159 | applyDefaults(); |
| 1160 | } |
| 1161 | |
| 1162 | private void applyDefaults() { |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1163 | final Resources resources = mView.getContext().getResources(); |
| 1164 | mWidth = resources.getDimensionPixelSize(R.dimen.default_magnifier_width); |
| 1165 | mHeight = resources.getDimensionPixelSize(R.dimen.default_magnifier_height); |
| 1166 | mElevation = resources.getDimension(R.dimen.default_magnifier_elevation); |
| 1167 | mCornerRadius = resources.getDimension(R.dimen.default_magnifier_corner_radius); |
| 1168 | mZoom = resources.getFloat(R.dimen.default_magnifier_zoom); |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1169 | mHorizontalDefaultSourceToMagnifierOffset = |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1170 | resources.getDimensionPixelSize(R.dimen.default_magnifier_horizontal_offset); |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1171 | mVerticalDefaultSourceToMagnifierOffset = |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1172 | resources.getDimensionPixelSize(R.dimen.default_magnifier_vertical_offset); |
| 1173 | mOverlay = new ColorDrawable(resources.getColor( |
| 1174 | R.color.default_magnifier_color_overlay, null)); |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 1175 | mClippingEnabled = true; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1176 | mLeftContentBound = SOURCE_BOUND_MAX_VISIBLE; |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1177 | mTopContentBound = SOURCE_BOUND_MAX_VISIBLE; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1178 | mRightContentBound = SOURCE_BOUND_MAX_VISIBLE; |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1179 | mBottomContentBound = SOURCE_BOUND_MAX_VISIBLE; |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * Sets the size of the magnifier window, in pixels. Defaults to (100dp, 48dp). |
| 1184 | * Note that the size of the content being magnified and copied to the magnifier |
| 1185 | * will be computed as (window width / zoom, window height / zoom). |
| 1186 | * @param width the window width to be set |
| 1187 | * @param height the window height to be set |
| 1188 | */ |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1189 | @NonNull |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1190 | public Builder setSize(@Px @IntRange(from = 0) int width, |
| 1191 | @Px @IntRange(from = 0) int height) { |
| 1192 | Preconditions.checkArgumentPositive(width, "Width should be positive"); |
| 1193 | Preconditions.checkArgumentPositive(height, "Height should be positive"); |
| 1194 | mWidth = width; |
| 1195 | mHeight = height; |
| 1196 | return this; |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * Sets the zoom to be applied to the chosen content before being copied to the magnifier. |
| 1201 | * A content of size (content_width, content_height) will be magnified to |
| 1202 | * (content_width * zoom, content_height * zoom), which will coincide with the size |
| 1203 | * of the magnifier. A zoom of 1 will translate to no magnification (the content will |
| 1204 | * be just copied to the magnifier with no scaling). The zoom defaults to 1.25. |
Mihai Popa | 27cf08f | 2019-01-10 19:59:29 +0000 | [diff] [blame] | 1205 | * Note that the zoom can also be changed after the instance is built, using the |
| 1206 | * {@link Magnifier#setZoom(float)} method. |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1207 | * @param zoom the zoom to be set |
| 1208 | */ |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1209 | @NonNull |
Mihai Popa | 27cf08f | 2019-01-10 19:59:29 +0000 | [diff] [blame] | 1210 | public Builder setInitialZoom(@FloatRange(from = 0f) float zoom) { |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1211 | Preconditions.checkArgumentPositive(zoom, "Zoom should be positive"); |
| 1212 | mZoom = zoom; |
| 1213 | return this; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * Sets the elevation of the magnifier window, in pixels. Defaults to 4dp. |
| 1218 | * @param elevation the elevation to be set |
| 1219 | */ |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1220 | @NonNull |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1221 | public Builder setElevation(@Px @FloatRange(from = 0) float elevation) { |
| 1222 | Preconditions.checkArgumentNonNegative(elevation, "Elevation should be non-negative"); |
| 1223 | mElevation = elevation; |
| 1224 | return this; |
| 1225 | } |
| 1226 | |
| 1227 | /** |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1228 | * Sets the corner radius of the magnifier window, in pixels. Defaults to 2dp. |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1229 | * @param cornerRadius the corner radius to be set |
| 1230 | */ |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1231 | @NonNull |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1232 | public Builder setCornerRadius(@Px @FloatRange(from = 0) float cornerRadius) { |
| 1233 | Preconditions.checkArgumentNonNegative(cornerRadius, |
| 1234 | "Corner radius should be non-negative"); |
| 1235 | mCornerRadius = cornerRadius; |
| 1236 | return this; |
| 1237 | } |
| 1238 | |
| 1239 | /** |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 1240 | * Sets an overlay that will be drawn on the top of the magnifier. |
| 1241 | * In general, the overlay should not be opaque, in order to let the magnified |
| 1242 | * content be partially visible in the magnifier. The default overlay is {@code null} |
| 1243 | * (no overlay). As an example, TextView applies a white {@link ColorDrawable} |
| 1244 | * overlay with 5% alpha, aiming to make the magnifier distinguishable when shown in dark |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1245 | * application regions. To disable the overlay, the parameter should be set |
| 1246 | * to {@code null}. If not null, the overlay will be automatically redrawn |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1247 | * when the drawable is invalidated. To achieve this, the magnifier will set a new |
| 1248 | * {@link android.graphics.drawable.Drawable.Callback} for the overlay drawable, |
| 1249 | * so keep in mind that any existing one set by the application will be lost. |
| 1250 | * @param overlay the overlay to be drawn on top |
| 1251 | */ |
| 1252 | @NonNull |
| 1253 | public Builder setOverlay(@Nullable Drawable overlay) { |
| 1254 | mOverlay = overlay; |
| 1255 | return this; |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * Sets an offset that should be added to the content source center to obtain |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1260 | * the position of the magnifier window, when the {@link #show(float, float)} |
| 1261 | * method is called. The offset is ignored when {@link #show(float, float, float, float)} |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1262 | * is used. The offset can be negative. It defaults to (0dp, 0dp). |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1263 | * @param horizontalOffset the horizontal component of the offset |
| 1264 | * @param verticalOffset the vertical component of the offset |
| 1265 | */ |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1266 | @NonNull |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1267 | public Builder setDefaultSourceToMagnifierOffset(@Px int horizontalOffset, |
| 1268 | @Px int verticalOffset) { |
| 1269 | mHorizontalDefaultSourceToMagnifierOffset = horizontalOffset; |
| 1270 | mVerticalDefaultSourceToMagnifierOffset = verticalOffset; |
| 1271 | return this; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 1275 | * Defines the behavior of the magnifier when it is requested to position outside the |
| 1276 | * surface of the main application window. The default value is {@code true}, which means |
| 1277 | * that the position will be adjusted such that the magnifier will be fully within the |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 1278 | * bounds of the main application window, while also avoiding any overlap with system insets |
| 1279 | * (such as the one corresponding to the status bar). If this flag is set to {@code false}, |
| 1280 | * the area where the magnifier can be positioned will no longer be clipped, so the |
| 1281 | * magnifier will be able to extend outside the main application window boundaries (and also |
| 1282 | * overlap the system insets). This can be useful if you require a custom behavior, but it |
| 1283 | * should be handled with care, when passing coordinates to {@link #show(float, float)}; |
| 1284 | * note that: |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 1285 | * <ul> |
| 1286 | * <li>in a multiwindow context, if the magnifier crosses the boundary between the two |
| 1287 | * windows, it will not be able to show over the window of the other application</li> |
| 1288 | * <li>if the magnifier overlaps the status bar, there is no guarantee about which one |
| 1289 | * will be displayed on top. This should be handled with care.</li> |
| 1290 | * </ul> |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 1291 | * @param clip whether the magnifier position will be adjusted |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 1292 | */ |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1293 | @NonNull |
Mihai Popa | becda34 | 2018-12-13 17:12:37 +0000 | [diff] [blame] | 1294 | public Builder setClippingEnabled(boolean clip) { |
| 1295 | mClippingEnabled = clip; |
Mihai Popa | 1903cab | 2018-08-01 14:33:12 +0100 | [diff] [blame] | 1296 | return this; |
| 1297 | } |
| 1298 | |
| 1299 | /** |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1300 | * Defines the bounds of the rectangle where the magnifier will be able to copy its content |
| 1301 | * from. The content will always be copied from the {@link Surface} of the main application |
| 1302 | * window unless the magnified view is a {@link SurfaceView}, in which case its backing |
| 1303 | * surface will be used. Each bound can have a different behavior, with the options being: |
| 1304 | * <ul> |
| 1305 | * <li>{@link #SOURCE_BOUND_MAX_VISIBLE}, which extends the bound as much as possible |
| 1306 | * while remaining in the visible region of the magnified view, as given by |
| 1307 | * {@link android.view.View#getGlobalVisibleRect(Rect)}. For example, this will take into |
| 1308 | * account the case when the view is contained in a scrollable container, and the |
| 1309 | * magnifier will refuse to copy content outside of the visible view region</li> |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1310 | * <li>{@link #SOURCE_BOUND_MAX_IN_SURFACE}, which extends the bound as much |
| 1311 | * as possible while remaining inside the surface the content is copied from.</li> |
| 1312 | * </ul> |
| 1313 | * Note that if either of the first three options is used, the bound will be compared to |
| 1314 | * the bound of the surface (i.e. as if {@link #SOURCE_BOUND_MAX_IN_SURFACE} was used), |
| 1315 | * and the more restrictive one will be chosen. In other words, no attempt to copy content |
| 1316 | * from outside the surface will be permitted. If two opposite bounds are not well-behaved |
| 1317 | * (i.e. left + sourceWidth > right or top + sourceHeight > bottom), the left and top |
| 1318 | * bounds will have priority and the others will be extended accordingly. If the pairs |
| 1319 | * obtained this way still remain out of bounds, the smallest possible offset will be added |
| 1320 | * to the pairs to bring them inside the surface bounds. If this is impossible |
| 1321 | * (i.e. the surface is too small for the size of the content we try to copy on either |
| 1322 | * dimension), an error will be logged and the magnifier content will look distorted. |
| 1323 | * The default values assumed by the builder for the source bounds are |
| 1324 | * left: {@link #SOURCE_BOUND_MAX_VISIBLE}, top: {@link #SOURCE_BOUND_MAX_IN_SURFACE}, |
| 1325 | * right: {@link #SOURCE_BOUND_MAX_VISIBLE}, bottom: {@link #SOURCE_BOUND_MAX_IN_SURFACE}. |
| 1326 | * @param left the left bound for content copy |
| 1327 | * @param top the top bound for content copy |
| 1328 | * @param right the right bound for content copy |
| 1329 | * @param bottom the bottom bound for content copy |
| 1330 | */ |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1331 | @NonNull |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1332 | public Builder setSourceBounds(@SourceBound int left, @SourceBound int top, |
| 1333 | @SourceBound int right, @SourceBound int bottom) { |
| 1334 | mLeftContentBound = left; |
| 1335 | mTopContentBound = top; |
| 1336 | mRightContentBound = right; |
| 1337 | mBottomContentBound = bottom; |
| 1338 | return this; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
Mihai Popa | 469aba8 | 2018-07-18 14:52:26 +0100 | [diff] [blame] | 1342 | * Builds a {@link Magnifier} instance based on the configuration of this {@link Builder}. |
| 1343 | */ |
| 1344 | public @NonNull Magnifier build() { |
| 1345 | return new Magnifier(this); |
| 1346 | } |
| 1347 | } |
| 1348 | |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1349 | /** |
| 1350 | * A source bound that will extend as much as possible, while remaining within the surface |
| 1351 | * the content is copied from. |
| 1352 | */ |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1353 | public static final int SOURCE_BOUND_MAX_IN_SURFACE = 0; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1354 | |
| 1355 | /** |
| 1356 | * A source bound that will extend as much as possible, while remaining within the |
| 1357 | * visible region of the magnified view, as determined by |
| 1358 | * {@link View#getGlobalVisibleRect(Rect)}. |
| 1359 | */ |
Mihai Popa | 520e4475 | 2019-01-29 21:26:26 +0000 | [diff] [blame] | 1360 | public static final int SOURCE_BOUND_MAX_VISIBLE = 1; |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1361 | |
| 1362 | |
| 1363 | /** |
| 1364 | * Used to describe the {@link Surface} rectangle where the magnifier's content is allowed |
| 1365 | * to be copied from. For more details, see method |
| 1366 | * {@link Magnifier.Builder#setSourceBounds(int, int, int, int)} |
| 1367 | * |
| 1368 | * @hide |
| 1369 | */ |
Mihai Popa | 520e4475 | 2019-01-29 21:26:26 +0000 | [diff] [blame] | 1370 | @IntDef({SOURCE_BOUND_MAX_IN_SURFACE, SOURCE_BOUND_MAX_VISIBLE}) |
Mihai Popa | 3e1aed1 | 2018-08-03 18:25:52 +0100 | [diff] [blame] | 1371 | @Retention(RetentionPolicy.SOURCE) |
| 1372 | public @interface SourceBound {} |
| 1373 | |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1374 | // The rest of the file consists of test APIs and methods relevant for tests. |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 1375 | |
| 1376 | /** |
| 1377 | * See {@link #setOnOperationCompleteCallback(Callback)}. |
| 1378 | */ |
| 1379 | @TestApi |
| 1380 | private Callback mCallback; |
| 1381 | |
| 1382 | /** |
| 1383 | * Sets a callback which will be invoked at the end of the next |
| 1384 | * {@link #show(float, float)} or {@link #update()} operation. |
| 1385 | * |
| 1386 | * @hide |
| 1387 | */ |
| 1388 | @TestApi |
| 1389 | public void setOnOperationCompleteCallback(final Callback callback) { |
| 1390 | mCallback = callback; |
| 1391 | if (mWindow != null) { |
| 1392 | mWindow.mCallback = callback; |
Mihai Popa | 4bcd4d40 | 2018-02-07 17:13:51 +0000 | [diff] [blame] | 1393 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 1394 | } |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1395 | |
| 1396 | /** |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1397 | * @return the drawing being currently displayed in the magnifier, as bitmap |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1398 | * |
| 1399 | * @hide |
| 1400 | */ |
| 1401 | @TestApi |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1402 | public @Nullable Bitmap getContent() { |
| 1403 | if (mWindow == null) { |
| 1404 | return null; |
| 1405 | } |
| 1406 | synchronized (mWindow.mLock) { |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1407 | return mWindow.mCurrentContent; |
Mihai Popa | 8b78910 | 2018-02-15 12:06:59 +0000 | [diff] [blame] | 1408 | } |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | /** |
Mihai Popa | 1ddabb2 | 2018-08-06 11:55:54 +0100 | [diff] [blame] | 1412 | * Returns a bitmap containing the content that was magnified and drew to the |
| 1413 | * magnifier, at its original size, without the overlay applied. |
| 1414 | * @return the content that is magnified, as bitmap |
Mihai Popa | beeaf55 | 2018-07-19 15:50:43 +0100 | [diff] [blame] | 1415 | * |
| 1416 | * @hide |
| 1417 | */ |
| 1418 | @TestApi |
| 1419 | public @Nullable Bitmap getOriginalContent() { |
| 1420 | if (mWindow == null) { |
| 1421 | return null; |
| 1422 | } |
| 1423 | synchronized (mWindow.mLock) { |
| 1424 | return Bitmap.createBitmap(mWindow.mBitmap); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | /** |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1429 | * @return the size of the magnifier window in dp |
| 1430 | * |
| 1431 | * @hide |
| 1432 | */ |
| 1433 | @TestApi |
| 1434 | public static PointF getMagnifierDefaultSize() { |
| 1435 | final Resources resources = Resources.getSystem(); |
| 1436 | final float density = resources.getDisplayMetrics().density; |
| 1437 | final PointF size = new PointF(); |
Mihai Popa | c695029 | 2018-11-15 21:32:42 +0000 | [diff] [blame] | 1438 | size.x = resources.getDimension(R.dimen.default_magnifier_width) / density; |
| 1439 | size.y = resources.getDimension(R.dimen.default_magnifier_height) / density; |
Mihai Popa | 137b584 | 2018-01-30 15:03:22 +0000 | [diff] [blame] | 1440 | return size; |
| 1441 | } |
Mihai Popa | 2ba5d8e | 2018-02-20 18:50:20 +0000 | [diff] [blame] | 1442 | |
| 1443 | /** |
| 1444 | * @hide |
| 1445 | */ |
| 1446 | @TestApi |
| 1447 | public interface Callback { |
| 1448 | /** |
| 1449 | * Callback called after the drawing for a magnifier update has happened. |
| 1450 | */ |
| 1451 | void onOperationComplete(); |
| 1452 | } |
Andrei Stingaceanu | d2eadfa | 2017-09-22 15:32:13 +0100 | [diff] [blame] | 1453 | } |