Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +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"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui; |
| 16 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 17 | import static android.view.Display.DEFAULT_DISPLAY; |
| 18 | import static android.view.DisplayCutout.BOUNDS_POSITION_BOTTOM; |
| 19 | import static android.view.DisplayCutout.BOUNDS_POSITION_LEFT; |
| 20 | import static android.view.DisplayCutout.BOUNDS_POSITION_LENGTH; |
| 21 | import static android.view.DisplayCutout.BOUNDS_POSITION_RIGHT; |
| 22 | import static android.view.DisplayCutout.BOUNDS_POSITION_TOP; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 23 | import static android.view.Surface.ROTATION_0; |
| 24 | import static android.view.Surface.ROTATION_180; |
| 25 | import static android.view.Surface.ROTATION_270; |
| 26 | import static android.view.Surface.ROTATION_90; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 27 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 28 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 29 | import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
| 30 | |
Matt Casey | 9fedd81 | 2019-06-05 17:14:09 -0400 | [diff] [blame] | 31 | import android.animation.Animator; |
Lucas Dupin | 2b05ccc | 2020-03-04 14:15:52 -0800 | [diff] [blame] | 32 | import android.animation.AnimatorListenerAdapter; |
Lucas Dupin | 2b05ccc | 2020-03-04 14:15:52 -0800 | [diff] [blame] | 33 | import android.animation.ValueAnimator; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 34 | import android.annotation.Dimension; |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 35 | import android.annotation.NonNull; |
Bill Lin | 2f67cf7 | 2020-03-24 01:16:34 +0800 | [diff] [blame] | 36 | import android.annotation.Nullable; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 37 | import android.app.ActivityManager; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 38 | import android.content.BroadcastReceiver; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 39 | import android.content.Context; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 40 | import android.content.Intent; |
| 41 | import android.content.IntentFilter; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 42 | import android.content.res.ColorStateList; |
| 43 | import android.content.res.Configuration; |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 44 | import android.content.res.Resources; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 45 | import android.graphics.Canvas; |
| 46 | import android.graphics.Color; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 47 | import android.graphics.Matrix; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 48 | import android.graphics.Paint; |
| 49 | import android.graphics.Path; |
| 50 | import android.graphics.PixelFormat; |
| 51 | import android.graphics.Rect; |
Lucas Dupin | 78904621 | 2020-03-10 14:13:02 -0700 | [diff] [blame] | 52 | import android.graphics.RectF; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 53 | import android.graphics.Region; |
Bill Lin | d5e7915 | 2020-02-14 15:08:13 +0800 | [diff] [blame] | 54 | import android.graphics.drawable.VectorDrawable; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 55 | import android.hardware.display.DisplayManager; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 56 | import android.os.Handler; |
Beverly | c5b1b56 | 2020-04-14 13:18:41 -0400 | [diff] [blame] | 57 | import android.os.HandlerExecutor; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 58 | import android.os.HandlerThread; |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 59 | import android.os.SystemProperties; |
Beverly | c5b1b56 | 2020-04-14 13:18:41 -0400 | [diff] [blame] | 60 | import android.os.UserHandle; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 61 | import android.provider.Settings.Secure; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 62 | import android.util.DisplayMetrics; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 63 | import android.util.Log; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 64 | import android.view.DisplayCutout; |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 65 | import android.view.DisplayCutout.BoundsPosition; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 66 | import android.view.DisplayInfo; |
| 67 | import android.view.Gravity; |
| 68 | import android.view.LayoutInflater; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 69 | import android.view.Surface; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 70 | import android.view.View; |
| 71 | import android.view.View.OnLayoutChangeListener; |
| 72 | import android.view.ViewGroup; |
| 73 | import android.view.ViewGroup.LayoutParams; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 74 | import android.view.ViewTreeObserver; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 75 | import android.view.WindowManager; |
| 76 | import android.widget.FrameLayout; |
| 77 | import android.widget.ImageView; |
| 78 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 79 | import androidx.annotation.VisibleForTesting; |
| 80 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 81 | import com.android.internal.util.Preconditions; |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 82 | import com.android.systemui.RegionInterceptingFrameLayout.RegionInterceptableView; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 83 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 84 | import com.android.systemui.dagger.qualifiers.Main; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 85 | import com.android.systemui.qs.SecureSetting; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 86 | import com.android.systemui.tuner.TunerService; |
| 87 | import com.android.systemui.tuner.TunerService.Tunable; |
| 88 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 89 | import java.util.ArrayList; |
| 90 | import java.util.List; |
| 91 | |
Dave Mankoff | 4ddc25b | 2019-10-23 15:46:08 -0400 | [diff] [blame] | 92 | import javax.inject.Inject; |
| 93 | import javax.inject.Singleton; |
| 94 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 95 | /** |
| 96 | * An overlay that draws screen decorations in software (e.g for rounded corners or display cutout) |
| 97 | * for antialiasing and emulation purposes. |
| 98 | */ |
Dave Mankoff | 4ddc25b | 2019-10-23 15:46:08 -0400 | [diff] [blame] | 99 | @Singleton |
shawnlin | 87af538 | 2019-09-13 14:13:13 +0800 | [diff] [blame] | 100 | public class ScreenDecorations extends SystemUI implements Tunable { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 101 | private static final boolean DEBUG = false; |
| 102 | private static final String TAG = "ScreenDecorations"; |
| 103 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 104 | public static final String SIZE = "sysui_rounded_size"; |
| 105 | public static final String PADDING = "sysui_rounded_content_padding"; |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 106 | private static final boolean DEBUG_SCREENSHOT_ROUNDED_CORNERS = |
| 107 | SystemProperties.getBoolean("debug.screenshot_rounded_corners", false); |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 108 | private static final boolean VERBOSE = false; |
Bill Lin | 3bf9ca2 | 2020-02-18 15:00:39 +0800 | [diff] [blame] | 109 | private static final boolean DEBUG_COLOR = DEBUG_SCREENSHOT_ROUNDED_CORNERS; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 110 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 111 | private DisplayManager mDisplayManager; |
shawnlin | 014c687 | 2020-02-26 17:51:51 +0800 | [diff] [blame] | 112 | @VisibleForTesting |
| 113 | protected boolean mIsRegistered; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 114 | private final BroadcastDispatcher mBroadcastDispatcher; |
| 115 | private final Handler mMainHandler; |
| 116 | private final TunerService mTunerService; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 117 | private DisplayManager.DisplayListener mDisplayListener; |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 118 | private CameraAvailabilityListener mCameraListener; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 119 | |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 120 | @VisibleForTesting |
| 121 | protected int mRoundedDefault; |
| 122 | @VisibleForTesting |
| 123 | protected int mRoundedDefaultTop; |
| 124 | @VisibleForTesting |
| 125 | protected int mRoundedDefaultBottom; |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 126 | @VisibleForTesting |
| 127 | protected View[] mOverlays; |
Bill Lin | 2f67cf7 | 2020-03-24 01:16:34 +0800 | [diff] [blame] | 128 | @Nullable |
| 129 | private DisplayCutoutView[] mCutoutViews; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 130 | private float mDensity; |
| 131 | private WindowManager mWindowManager; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 132 | private int mRotation; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 133 | private SecureSetting mColorInversionSetting; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 134 | private Handler mHandler; |
Bill Lin | d5e7915 | 2020-02-14 15:08:13 +0800 | [diff] [blame] | 135 | private boolean mPendingRotationChange; |
| 136 | private boolean mIsRoundedCornerMultipleRadius; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 137 | |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 138 | private CameraAvailabilityListener.CameraTransitionCallback mCameraTransitionCallback = |
| 139 | new CameraAvailabilityListener.CameraTransitionCallback() { |
| 140 | @Override |
| 141 | public void onApplyCameraProtection(@NonNull Path protectionPath, @NonNull Rect bounds) { |
Bill Lin | 2f67cf7 | 2020-03-24 01:16:34 +0800 | [diff] [blame] | 142 | if (mCutoutViews == null) { |
| 143 | Log.w(TAG, "DisplayCutoutView do not initialized"); |
| 144 | return; |
| 145 | } |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 146 | // Show the extra protection around the front facing camera if necessary |
| 147 | for (DisplayCutoutView dcv : mCutoutViews) { |
Bill Lin | 2f67cf7 | 2020-03-24 01:16:34 +0800 | [diff] [blame] | 148 | // Check Null since not all mCutoutViews[pos] be inflated at the meanwhile |
| 149 | if (dcv != null) { |
| 150 | dcv.setProtection(protectionPath, bounds); |
| 151 | dcv.setShowProtection(true); |
| 152 | } |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void onHideCameraProtection() { |
Bill Lin | 2f67cf7 | 2020-03-24 01:16:34 +0800 | [diff] [blame] | 158 | if (mCutoutViews == null) { |
| 159 | Log.w(TAG, "DisplayCutoutView do not initialized"); |
| 160 | return; |
| 161 | } |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 162 | // Go back to the regular anti-aliasing |
| 163 | for (DisplayCutoutView dcv : mCutoutViews) { |
Bill Lin | 2f67cf7 | 2020-03-24 01:16:34 +0800 | [diff] [blame] | 164 | // Check Null since not all mCutoutViews[pos] be inflated at the meanwhile |
| 165 | if (dcv != null) { |
| 166 | dcv.setShowProtection(false); |
| 167 | } |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | }; |
| 171 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 172 | /** |
| 173 | * Converts a set of {@link Rect}s into a {@link Region} |
| 174 | * |
| 175 | * @hide |
| 176 | */ |
| 177 | public static Region rectsToRegion(List<Rect> rects) { |
| 178 | Region result = Region.obtain(); |
| 179 | if (rects != null) { |
| 180 | for (Rect r : rects) { |
| 181 | if (r != null && !r.isEmpty()) { |
| 182 | result.op(r, Region.Op.UNION); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | return result; |
| 187 | } |
| 188 | |
Dave Mankoff | 4ddc25b | 2019-10-23 15:46:08 -0400 | [diff] [blame] | 189 | @Inject |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 190 | public ScreenDecorations(Context context, |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 191 | @Main Handler handler, |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 192 | BroadcastDispatcher broadcastDispatcher, |
| 193 | TunerService tunerService) { |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 194 | super(context); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 195 | mMainHandler = handler; |
| 196 | mBroadcastDispatcher = broadcastDispatcher; |
| 197 | mTunerService = tunerService; |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 200 | @Override |
| 201 | public void start() { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 202 | mHandler = startHandlerThread(); |
| 203 | mHandler.post(this::startOnScreenDecorationsThread); |
James O'Leary | 4335c70 | 2019-05-29 12:38:51 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 206 | @VisibleForTesting |
| 207 | Handler startHandlerThread() { |
| 208 | HandlerThread thread = new HandlerThread("ScreenDecorations"); |
| 209 | thread.start(); |
| 210 | return thread.getThreadHandler(); |
| 211 | } |
| 212 | |
| 213 | private void startOnScreenDecorationsThread() { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 214 | mRotation = mContext.getDisplay().getRotation(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 215 | mWindowManager = mContext.getSystemService(WindowManager.class); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 216 | mDisplayManager = mContext.getSystemService(DisplayManager.class); |
Bill Lin | d5e7915 | 2020-02-14 15:08:13 +0800 | [diff] [blame] | 217 | mIsRoundedCornerMultipleRadius = mContext.getResources().getBoolean( |
| 218 | R.bool.config_roundedCornerMultipleRadius); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 219 | updateRoundedCornerRadii(); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 220 | setupDecorations(); |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 221 | setupCameraListener(); |
| 222 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 223 | mDisplayListener = new DisplayManager.DisplayListener() { |
| 224 | @Override |
| 225 | public void onDisplayAdded(int displayId) { |
| 226 | // do nothing |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onDisplayRemoved(int displayId) { |
| 231 | // do nothing |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void onDisplayChanged(int displayId) { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 236 | final int newRotation = mContext.getDisplay().getRotation(); |
| 237 | if (mOverlays != null && mRotation != newRotation) { |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 238 | // We cannot immediately update the orientation. Otherwise |
| 239 | // WindowManager is still deferring layout until it has finished dispatching |
| 240 | // the config changes, which may cause divergence between what we draw |
| 241 | // (new orientation), and where we are placed on the screen (old orientation). |
| 242 | // Instead we wait until either: |
| 243 | // - we are trying to redraw. This because WM resized our window and told us to. |
| 244 | // - the config change has been dispatched, so WM is no longer deferring layout. |
| 245 | mPendingRotationChange = true; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 246 | if (DEBUG) { |
| 247 | Log.i(TAG, "Rotation changed, deferring " + newRotation + ", staying at " |
| 248 | + mRotation); |
| 249 | } |
| 250 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 251 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; i++) { |
| 252 | if (mOverlays[i] != null) { |
| 253 | mOverlays[i].getViewTreeObserver().addOnPreDrawListener( |
| 254 | new RestartingPreDrawListener(mOverlays[i], i, newRotation)); |
| 255 | } |
| 256 | } |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 257 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 258 | updateOrientation(); |
| 259 | } |
| 260 | }; |
| 261 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 262 | mDisplayManager.registerDisplayListener(mDisplayListener, mHandler); |
| 263 | updateOrientation(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | private void setupDecorations() { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 267 | if (hasRoundedCorners() || shouldDrawCutout()) { |
| 268 | final DisplayCutout cutout = getCutout(); |
| 269 | final Rect[] bounds = cutout == null ? null : cutout.getBoundingRectsAll(); |
| 270 | int rotatedPos; |
| 271 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; i++) { |
| 272 | rotatedPos = getBoundPositionFromRotation(i, mRotation); |
| 273 | if ((bounds != null && !bounds[rotatedPos].isEmpty()) |
| 274 | || shouldShowRoundedCorner(i)) { |
| 275 | createOverlay(i); |
| 276 | } else { |
| 277 | removeOverlay(i); |
| 278 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 279 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 280 | } else { |
| 281 | removeAllOverlays(); |
| 282 | } |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 283 | |
shawnlin | 014c687 | 2020-02-26 17:51:51 +0800 | [diff] [blame] | 284 | if (hasOverlays()) { |
| 285 | if (mIsRegistered) { |
| 286 | return; |
| 287 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 288 | DisplayMetrics metrics = new DisplayMetrics(); |
| 289 | mDisplayManager.getDisplay(DEFAULT_DISPLAY).getMetrics(metrics); |
| 290 | mDensity = metrics.density; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 291 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 292 | mMainHandler.post(() -> mTunerService.addTunable(this, SIZE)); |
| 293 | |
| 294 | // Watch color inversion and invert the overlay as needed. |
| 295 | if (mColorInversionSetting == null) { |
| 296 | mColorInversionSetting = new SecureSetting(mContext, mHandler, |
| 297 | Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) { |
| 298 | @Override |
| 299 | protected void handleValueChanged(int value, boolean observedChange) { |
| 300 | updateColorInversion(value); |
| 301 | } |
| 302 | }; |
Beverly | c5b1b56 | 2020-04-14 13:18:41 -0400 | [diff] [blame] | 303 | |
| 304 | mColorInversionSetting.setListening(true); |
| 305 | mColorInversionSetting.onChange(false); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 306 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 307 | |
| 308 | IntentFilter filter = new IntentFilter(); |
| 309 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Beverly | c5b1b56 | 2020-04-14 13:18:41 -0400 | [diff] [blame] | 310 | mBroadcastDispatcher.registerReceiver(mUserSwitchIntentReceiver, filter, |
| 311 | new HandlerExecutor(mHandler), UserHandle.ALL); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 312 | mIsRegistered = true; |
| 313 | } else { |
| 314 | mMainHandler.post(() -> mTunerService.removeTunable(this)); |
| 315 | |
| 316 | if (mColorInversionSetting != null) { |
| 317 | mColorInversionSetting.setListening(false); |
| 318 | } |
| 319 | |
Beverly | c5b1b56 | 2020-04-14 13:18:41 -0400 | [diff] [blame] | 320 | mBroadcastDispatcher.unregisterReceiver(mUserSwitchIntentReceiver); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 321 | mIsRegistered = false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | @VisibleForTesting |
| 326 | DisplayCutout getCutout() { |
| 327 | return mContext.getDisplay().getCutout(); |
| 328 | } |
| 329 | |
shawnlin | 014c687 | 2020-02-26 17:51:51 +0800 | [diff] [blame] | 330 | @VisibleForTesting |
| 331 | boolean hasOverlays() { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 332 | if (mOverlays == null) { |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; i++) { |
| 337 | if (mOverlays[i] != null) { |
| 338 | return true; |
| 339 | } |
| 340 | } |
| 341 | mOverlays = null; |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | private void removeAllOverlays() { |
| 346 | if (mOverlays == null) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; i++) { |
| 351 | if (mOverlays[i] != null) { |
| 352 | removeOverlay(i); |
| 353 | } |
| 354 | } |
| 355 | mOverlays = null; |
| 356 | } |
| 357 | |
| 358 | private void removeOverlay(@BoundsPosition int pos) { |
| 359 | if (mOverlays == null || mOverlays[pos] == null) { |
| 360 | return; |
| 361 | } |
| 362 | mWindowManager.removeViewImmediate(mOverlays[pos]); |
| 363 | mOverlays[pos] = null; |
| 364 | } |
| 365 | |
| 366 | private void createOverlay(@BoundsPosition int pos) { |
| 367 | if (mOverlays == null) { |
| 368 | mOverlays = new View[BOUNDS_POSITION_LENGTH]; |
| 369 | } |
| 370 | |
| 371 | if (mCutoutViews == null) { |
| 372 | mCutoutViews = new DisplayCutoutView[BOUNDS_POSITION_LENGTH]; |
| 373 | } |
| 374 | |
| 375 | if (mOverlays[pos] != null) { |
| 376 | return; |
| 377 | } |
| 378 | mOverlays[pos] = LayoutInflater.from(mContext) |
| 379 | .inflate(R.layout.rounded_corners, null); |
| 380 | |
| 381 | mCutoutViews[pos] = new DisplayCutoutView(mContext, pos, this); |
| 382 | ((ViewGroup) mOverlays[pos]).addView(mCutoutViews[pos]); |
| 383 | |
| 384 | mOverlays[pos].setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 385 | mOverlays[pos].setAlpha(0); |
| 386 | mOverlays[pos].setForceDarkAllowed(false); |
| 387 | |
| 388 | updateView(pos); |
| 389 | |
| 390 | mWindowManager.addView(mOverlays[pos], getWindowLayoutParams(pos)); |
| 391 | |
| 392 | mOverlays[pos].addOnLayoutChangeListener(new OnLayoutChangeListener() { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 393 | @Override |
| 394 | public void onLayoutChange(View v, int left, int top, int right, int bottom, |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 395 | int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 396 | mOverlays[pos].removeOnLayoutChangeListener(this); |
| 397 | mOverlays[pos].animate() |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 398 | .alpha(1) |
| 399 | .setDuration(1000) |
| 400 | .start(); |
| 401 | } |
| 402 | }); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 403 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 404 | mOverlays[pos].getViewTreeObserver().addOnPreDrawListener( |
| 405 | new ValidatingPreDrawListener(mOverlays[pos])); |
| 406 | } |
| 407 | |
| 408 | private void updateView(@BoundsPosition int pos) { |
| 409 | if (mOverlays == null || mOverlays[pos] == null) { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | // update rounded corner view rotation |
| 414 | updateRoundedCornerView(pos, R.id.left); |
| 415 | updateRoundedCornerView(pos, R.id.right); |
shawnlin | 0264a6c | 2020-03-24 16:45:47 +0800 | [diff] [blame] | 416 | updateRoundedCornerSize(mRoundedDefault, mRoundedDefaultTop, mRoundedDefaultBottom); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 417 | |
| 418 | // update cutout view rotation |
| 419 | if (mCutoutViews != null && mCutoutViews[pos] != null) { |
| 420 | mCutoutViews[pos].setRotation(mRotation); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | @VisibleForTesting |
| 425 | WindowManager.LayoutParams getWindowLayoutParams(@BoundsPosition int pos) { |
| 426 | final WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 427 | getWidthLayoutParamByPos(pos), |
| 428 | getHeightLayoutParamByPos(pos), |
| 429 | WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL, |
| 430 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 431 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 432 | | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
| 433 | | WindowManager.LayoutParams.FLAG_SLIPPERY |
| 434 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, |
| 435 | PixelFormat.TRANSLUCENT); |
| 436 | lp.privateFlags |= WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS |
| 437 | | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION; |
| 438 | |
| 439 | if (!DEBUG_SCREENSHOT_ROUNDED_CORNERS) { |
| 440 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY; |
| 441 | } |
| 442 | |
| 443 | lp.setTitle(getWindowTitleByPos(pos)); |
| 444 | lp.gravity = getOverlayWindowGravity(pos); |
| 445 | lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
| 446 | lp.setFitInsetsTypes(0 /* types */); |
| 447 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COLOR_SPACE_AGNOSTIC; |
| 448 | return lp; |
| 449 | } |
| 450 | |
| 451 | private int getWidthLayoutParamByPos(@BoundsPosition int pos) { |
| 452 | final int rotatedPos = getBoundPositionFromRotation(pos, mRotation); |
| 453 | return rotatedPos == BOUNDS_POSITION_TOP || rotatedPos == BOUNDS_POSITION_BOTTOM |
| 454 | ? MATCH_PARENT : WRAP_CONTENT; |
| 455 | } |
| 456 | |
| 457 | private int getHeightLayoutParamByPos(@BoundsPosition int pos) { |
| 458 | final int rotatedPos = getBoundPositionFromRotation(pos, mRotation); |
| 459 | return rotatedPos == BOUNDS_POSITION_TOP || rotatedPos == BOUNDS_POSITION_BOTTOM |
| 460 | ? WRAP_CONTENT : MATCH_PARENT; |
| 461 | } |
| 462 | |
| 463 | private static String getWindowTitleByPos(@BoundsPosition int pos) { |
| 464 | switch (pos) { |
| 465 | case BOUNDS_POSITION_LEFT: |
| 466 | return "ScreenDecorOverlayLeft"; |
| 467 | case BOUNDS_POSITION_TOP: |
| 468 | return "ScreenDecorOverlay"; |
| 469 | case BOUNDS_POSITION_RIGHT: |
| 470 | return "ScreenDecorOverlayRight"; |
| 471 | case BOUNDS_POSITION_BOTTOM: |
| 472 | return "ScreenDecorOverlayBottom"; |
| 473 | default: |
| 474 | throw new IllegalArgumentException("unknown bound position: " + pos); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | private int getOverlayWindowGravity(@BoundsPosition int pos) { |
| 479 | final int rotated = getBoundPositionFromRotation(pos, mRotation); |
| 480 | switch (rotated) { |
| 481 | case BOUNDS_POSITION_TOP: |
| 482 | return Gravity.TOP; |
| 483 | case BOUNDS_POSITION_BOTTOM: |
| 484 | return Gravity.BOTTOM; |
| 485 | case BOUNDS_POSITION_LEFT: |
| 486 | return Gravity.LEFT; |
| 487 | case BOUNDS_POSITION_RIGHT: |
| 488 | return Gravity.RIGHT; |
| 489 | default: |
| 490 | throw new IllegalArgumentException("unknown bound position: " + pos); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | private static int getBoundPositionFromRotation(@BoundsPosition int pos, int rotation) { |
| 495 | return (pos - rotation) < 0 |
| 496 | ? pos - rotation + DisplayCutout.BOUNDS_POSITION_LENGTH |
| 497 | : pos - rotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 500 | private void setupCameraListener() { |
| 501 | Resources res = mContext.getResources(); |
| 502 | boolean enabled = res.getBoolean(R.bool.config_enableDisplayCutoutProtection); |
| 503 | if (enabled) { |
| 504 | mCameraListener = CameraAvailabilityListener.Factory.build(mContext, mHandler::post); |
| 505 | mCameraListener.addTransitionCallback(mCameraTransitionCallback); |
| 506 | mCameraListener.startListening(); |
| 507 | } |
| 508 | } |
| 509 | |
Beverly | c5b1b56 | 2020-04-14 13:18:41 -0400 | [diff] [blame] | 510 | private final BroadcastReceiver mUserSwitchIntentReceiver = new BroadcastReceiver() { |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 511 | @Override |
| 512 | public void onReceive(Context context, Intent intent) { |
Beverly | c5b1b56 | 2020-04-14 13:18:41 -0400 | [diff] [blame] | 513 | int newUserId = ActivityManager.getCurrentUser(); |
| 514 | if (DEBUG) { |
| 515 | Log.d(TAG, "UserSwitched newUserId=" + newUserId); |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 516 | } |
Beverly | c5b1b56 | 2020-04-14 13:18:41 -0400 | [diff] [blame] | 517 | // update color inversion setting to the new user |
| 518 | mColorInversionSetting.setUserId(newUserId); |
| 519 | updateColorInversion(mColorInversionSetting.getValue()); |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 520 | } |
| 521 | }; |
| 522 | |
| 523 | private void updateColorInversion(int colorsInvertedValue) { |
| 524 | int tint = colorsInvertedValue != 0 ? Color.WHITE : Color.BLACK; |
Bill Lin | 3bf9ca2 | 2020-02-18 15:00:39 +0800 | [diff] [blame] | 525 | if (DEBUG_COLOR) { |
| 526 | tint = Color.RED; |
| 527 | } |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 528 | ColorStateList tintList = ColorStateList.valueOf(tint); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 529 | |
| 530 | if (mOverlays == null) { |
| 531 | return; |
| 532 | } |
| 533 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; i++) { |
| 534 | if (mOverlays[i] == null) { |
| 535 | continue; |
| 536 | } |
| 537 | final int size = ((ViewGroup) mOverlays[i]).getChildCount(); |
| 538 | View child; |
| 539 | for (int j = 0; j < size; j++) { |
| 540 | child = ((ViewGroup) mOverlays[i]).getChildAt(j); |
| 541 | if (child instanceof ImageView) { |
| 542 | ((ImageView) child).setImageTintList(tintList); |
| 543 | } else if (child instanceof DisplayCutoutView) { |
| 544 | ((DisplayCutoutView) child).setColor(tint); |
| 545 | } |
| 546 | } |
| 547 | } |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 550 | @Override |
| 551 | protected void onConfigurationChanged(Configuration newConfig) { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 552 | mHandler.post(() -> { |
| 553 | int oldRotation = mRotation; |
| 554 | mPendingRotationChange = false; |
| 555 | updateOrientation(); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 556 | updateRoundedCornerRadii(); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 557 | if (DEBUG) Log.i(TAG, "onConfigChanged from rot " + oldRotation + " to " + mRotation); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 558 | setupDecorations(); |
| 559 | if (mOverlays != null) { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 560 | // Updating the layout params ensures that ViewRootImpl will call relayoutWindow(), |
| 561 | // which ensures that the forced seamless rotation will end, even if we updated |
| 562 | // the rotation before window manager was ready (and was still waiting for sending |
| 563 | // the updated rotation). |
| 564 | updateLayoutParams(); |
| 565 | } |
| 566 | }); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 567 | } |
| 568 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 569 | private void updateOrientation() { |
| 570 | Preconditions.checkState(mHandler.getLooper().getThread() == Thread.currentThread(), |
| 571 | "must call on " + mHandler.getLooper().getThread() |
| 572 | + ", but was " + Thread.currentThread()); |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 573 | if (mPendingRotationChange) { |
| 574 | return; |
| 575 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 576 | int newRotation = mContext.getDisplay().getRotation(); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 577 | if (newRotation != mRotation) { |
| 578 | mRotation = newRotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 579 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 580 | if (mOverlays != null) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 581 | updateLayoutParams(); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 582 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; i++) { |
| 583 | if (mOverlays[i] == null) { |
| 584 | continue; |
| 585 | } |
| 586 | updateView(i); |
| 587 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 588 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 592 | private void updateRoundedCornerRadii() { |
| 593 | final int newRoundedDefault = mContext.getResources().getDimensionPixelSize( |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 594 | com.android.internal.R.dimen.rounded_corner_radius); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 595 | final int newRoundedDefaultTop = mContext.getResources().getDimensionPixelSize( |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 596 | com.android.internal.R.dimen.rounded_corner_radius_top); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 597 | final int newRoundedDefaultBottom = mContext.getResources().getDimensionPixelSize( |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 598 | com.android.internal.R.dimen.rounded_corner_radius_bottom); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 599 | final boolean roundedCornersChanged = mRoundedDefault != newRoundedDefault |
| 600 | || mRoundedDefaultBottom != newRoundedDefaultBottom |
| 601 | || mRoundedDefaultTop != newRoundedDefaultTop; |
| 602 | |
| 603 | if (roundedCornersChanged) { |
Bill Lin | d5e7915 | 2020-02-14 15:08:13 +0800 | [diff] [blame] | 604 | // If config_roundedCornerMultipleRadius set as true, ScreenDecorations respect the |
| 605 | // max(width, height) size of drawable/rounded.xml instead of rounded_corner_radius |
| 606 | if (mIsRoundedCornerMultipleRadius) { |
| 607 | final VectorDrawable d = (VectorDrawable) mContext.getDrawable(R.drawable.rounded); |
| 608 | mRoundedDefault = Math.max(d.getIntrinsicWidth(), d.getIntrinsicHeight()); |
| 609 | mRoundedDefaultTop = mRoundedDefaultBottom = mRoundedDefault; |
| 610 | } else { |
| 611 | mRoundedDefault = newRoundedDefault; |
| 612 | mRoundedDefaultTop = newRoundedDefaultTop; |
| 613 | mRoundedDefaultBottom = newRoundedDefaultBottom; |
| 614 | } |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 615 | onTuningChanged(SIZE, null); |
| 616 | } |
| 617 | } |
| 618 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 619 | private void updateRoundedCornerView(@BoundsPosition int pos, int id) { |
| 620 | final View rounded = mOverlays[pos].findViewById(id); |
| 621 | if (rounded == null) { |
| 622 | return; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 623 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 624 | rounded.setVisibility(View.GONE); |
| 625 | if (shouldShowRoundedCorner(pos)) { |
| 626 | final int gravity = getRoundedCornerGravity(pos, id == R.id.left); |
| 627 | ((FrameLayout.LayoutParams) rounded.getLayoutParams()).gravity = gravity; |
| 628 | rounded.setRotation(getRoundedCornerRotation(gravity)); |
| 629 | rounded.setVisibility(View.VISIBLE); |
| 630 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 631 | } |
| 632 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 633 | private int getRoundedCornerGravity(@BoundsPosition int pos, boolean isStart) { |
| 634 | final int rotatedPos = getBoundPositionFromRotation(pos, mRotation); |
| 635 | switch (rotatedPos) { |
| 636 | case BOUNDS_POSITION_LEFT: |
| 637 | return isStart ? Gravity.TOP | Gravity.LEFT : Gravity.BOTTOM | Gravity.LEFT; |
| 638 | case BOUNDS_POSITION_TOP: |
| 639 | return isStart ? Gravity.TOP | Gravity.LEFT : Gravity.TOP | Gravity.RIGHT; |
| 640 | case BOUNDS_POSITION_RIGHT: |
| 641 | return isStart ? Gravity.TOP | Gravity.RIGHT : Gravity.BOTTOM | Gravity.RIGHT; |
| 642 | case BOUNDS_POSITION_BOTTOM: |
| 643 | return isStart ? Gravity.BOTTOM | Gravity.LEFT : Gravity.BOTTOM | Gravity.RIGHT; |
| 644 | default: |
| 645 | throw new IllegalArgumentException("Incorrect position: " + rotatedPos); |
| 646 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 647 | } |
| 648 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 649 | private int getRoundedCornerRotation(int gravity) { |
| 650 | switch (gravity) { |
| 651 | case Gravity.TOP | Gravity.LEFT: |
| 652 | return 0; |
| 653 | case Gravity.TOP | Gravity.RIGHT: |
| 654 | return 90; |
| 655 | case Gravity.BOTTOM | Gravity.LEFT: |
| 656 | return 270; |
| 657 | case Gravity.BOTTOM | Gravity.RIGHT: |
| 658 | return 180; |
| 659 | default: |
| 660 | throw new IllegalArgumentException("Unsupported gravity: " + gravity); |
| 661 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 662 | } |
| 663 | |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 664 | private boolean hasRoundedCorners() { |
Bill Lin | d5e7915 | 2020-02-14 15:08:13 +0800 | [diff] [blame] | 665 | return mRoundedDefault > 0 || mRoundedDefaultBottom > 0 || mRoundedDefaultTop > 0 |
| 666 | || mIsRoundedCornerMultipleRadius; |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 667 | } |
| 668 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 669 | private boolean shouldShowRoundedCorner(@BoundsPosition int pos) { |
| 670 | if (!hasRoundedCorners()) { |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | DisplayCutout cutout = getCutout(); |
| 675 | // for cutout is null or cutout with only waterfall. |
| 676 | final boolean emptyBoundsOrWaterfall = cutout == null || cutout.isBoundsEmpty(); |
| 677 | // Shows rounded corner on left and right overlays only when there is no top or bottom |
| 678 | // cutout. |
| 679 | final int rotatedTop = getBoundPositionFromRotation(BOUNDS_POSITION_TOP, mRotation); |
| 680 | final int rotatedBottom = getBoundPositionFromRotation(BOUNDS_POSITION_BOTTOM, mRotation); |
| 681 | if (emptyBoundsOrWaterfall || !cutout.getBoundingRectsAll()[rotatedTop].isEmpty() |
| 682 | || !cutout.getBoundingRectsAll()[rotatedBottom].isEmpty()) { |
| 683 | return pos == BOUNDS_POSITION_TOP || pos == BOUNDS_POSITION_BOTTOM; |
| 684 | } else { |
| 685 | return pos == BOUNDS_POSITION_LEFT || pos == BOUNDS_POSITION_RIGHT; |
| 686 | } |
| 687 | } |
| 688 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 689 | private boolean shouldDrawCutout() { |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 690 | return shouldDrawCutout(mContext); |
| 691 | } |
| 692 | |
| 693 | static boolean shouldDrawCutout(Context context) { |
| 694 | return context.getResources().getBoolean( |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 695 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout); |
| 696 | } |
| 697 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 698 | private void updateLayoutParams() { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 699 | if (mOverlays == null) { |
| 700 | return; |
| 701 | } |
| 702 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; i++) { |
| 703 | if (mOverlays[i] == null) { |
| 704 | continue; |
| 705 | } |
| 706 | mWindowManager.updateViewLayout(mOverlays[i], getWindowLayoutParams(i)); |
| 707 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | @Override |
| 711 | public void onTuningChanged(String key, String newValue) { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 712 | mHandler.post(() -> { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 713 | if (mOverlays == null) return; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 714 | if (SIZE.equals(key)) { |
| 715 | int size = mRoundedDefault; |
| 716 | int sizeTop = mRoundedDefaultTop; |
| 717 | int sizeBottom = mRoundedDefaultBottom; |
| 718 | if (newValue != null) { |
| 719 | try { |
| 720 | size = (int) (Integer.parseInt(newValue) * mDensity); |
| 721 | } catch (Exception e) { |
| 722 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 723 | } |
shawnlin | 0264a6c | 2020-03-24 16:45:47 +0800 | [diff] [blame] | 724 | updateRoundedCornerSize(size, sizeTop, sizeBottom); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 725 | } |
| 726 | }); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 727 | } |
| 728 | |
shawnlin | 0264a6c | 2020-03-24 16:45:47 +0800 | [diff] [blame] | 729 | private void updateRoundedCornerSize(int sizeDefault, int sizeTop, int sizeBottom) { |
| 730 | if (mOverlays == null) { |
| 731 | return; |
| 732 | } |
| 733 | if (sizeTop == 0) { |
| 734 | sizeTop = sizeDefault; |
| 735 | } |
| 736 | if (sizeBottom == 0) { |
| 737 | sizeBottom = sizeDefault; |
| 738 | } |
| 739 | |
| 740 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; i++) { |
| 741 | if (mOverlays[i] == null) { |
| 742 | continue; |
| 743 | } |
| 744 | if (i == BOUNDS_POSITION_LEFT || i == BOUNDS_POSITION_RIGHT) { |
| 745 | if (mRotation == ROTATION_270) { |
| 746 | setSize(mOverlays[i].findViewById(R.id.left), sizeBottom); |
| 747 | setSize(mOverlays[i].findViewById(R.id.right), sizeTop); |
| 748 | } else { |
| 749 | setSize(mOverlays[i].findViewById(R.id.left), sizeTop); |
| 750 | setSize(mOverlays[i].findViewById(R.id.right), sizeBottom); |
| 751 | } |
| 752 | } else if (i == BOUNDS_POSITION_TOP) { |
| 753 | setSize(mOverlays[i].findViewById(R.id.left), sizeTop); |
| 754 | setSize(mOverlays[i].findViewById(R.id.right), sizeTop); |
| 755 | } else if (i == BOUNDS_POSITION_BOTTOM) { |
| 756 | setSize(mOverlays[i].findViewById(R.id.left), sizeBottom); |
| 757 | setSize(mOverlays[i].findViewById(R.id.right), sizeBottom); |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | @VisibleForTesting |
| 763 | protected void setSize(View view, int pixelSize) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 764 | LayoutParams params = view.getLayoutParams(); |
| 765 | params.width = pixelSize; |
| 766 | params.height = pixelSize; |
| 767 | view.setLayoutParams(params); |
| 768 | } |
| 769 | |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 770 | public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener, |
| 771 | RegionInterceptableView { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 772 | |
Lucas Dupin | 2b05ccc | 2020-03-04 14:15:52 -0800 | [diff] [blame] | 773 | private static final float HIDDEN_CAMERA_PROTECTION_SCALE = 0.5f; |
| 774 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 775 | private final DisplayInfo mInfo = new DisplayInfo(); |
| 776 | private final Paint mPaint = new Paint(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 777 | private final List<Rect> mBounds = new ArrayList(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 778 | private final Rect mBoundingRect = new Rect(); |
| 779 | private final Path mBoundingPath = new Path(); |
Evan Laird | 377838b | 2020-03-06 16:55:46 -0500 | [diff] [blame] | 780 | // Don't initialize these yet because they may never exist |
Lucas Dupin | 78904621 | 2020-03-10 14:13:02 -0700 | [diff] [blame] | 781 | private RectF mProtectionRect; |
| 782 | private RectF mProtectionRectOrig; |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 783 | private Path mProtectionPath; |
Evan Laird | 377838b | 2020-03-06 16:55:46 -0500 | [diff] [blame] | 784 | private Path mProtectionPathOrig; |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 785 | private Rect mTotalBounds = new Rect(); |
| 786 | // Whether or not to show the cutout protection path |
| 787 | private boolean mShowProtection = false; |
| 788 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 789 | private final int[] mLocation = new int[2]; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 790 | private final ScreenDecorations mDecorations; |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 791 | private int mColor = Color.BLACK; |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 792 | private int mRotation; |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 793 | private int mInitialPosition; |
| 794 | private int mPosition; |
Lucas Dupin | 2b05ccc | 2020-03-04 14:15:52 -0800 | [diff] [blame] | 795 | private float mCameraProtectionProgress = HIDDEN_CAMERA_PROTECTION_SCALE; |
| 796 | private ValueAnimator mCameraProtectionAnimator; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 797 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 798 | public DisplayCutoutView(Context context, @BoundsPosition int pos, |
| 799 | ScreenDecorations decorations) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 800 | super(context); |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 801 | mInitialPosition = pos; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 802 | mDecorations = decorations; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 803 | setId(R.id.display_cutout); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 804 | if (DEBUG) { |
| 805 | getViewTreeObserver().addOnDrawListener(() -> Log.i(TAG, |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 806 | getWindowTitleByPos(pos) + " drawn in rot " + mRotation)); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 807 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 810 | public void setColor(int color) { |
| 811 | mColor = color; |
| 812 | invalidate(); |
| 813 | } |
| 814 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 815 | @Override |
| 816 | protected void onAttachedToWindow() { |
| 817 | super.onAttachedToWindow(); |
| 818 | mContext.getSystemService(DisplayManager.class).registerDisplayListener(this, |
| 819 | getHandler()); |
| 820 | update(); |
| 821 | } |
| 822 | |
| 823 | @Override |
| 824 | protected void onDetachedFromWindow() { |
| 825 | super.onDetachedFromWindow(); |
| 826 | mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this); |
| 827 | } |
| 828 | |
| 829 | @Override |
| 830 | protected void onDraw(Canvas canvas) { |
| 831 | super.onDraw(canvas); |
| 832 | getLocationOnScreen(mLocation); |
| 833 | canvas.translate(-mLocation[0], -mLocation[1]); |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 834 | |
Lucas Dupin | 2b05ccc | 2020-03-04 14:15:52 -0800 | [diff] [blame] | 835 | if (!mBoundingPath.isEmpty()) { |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 836 | mPaint.setColor(mColor); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 837 | mPaint.setStyle(Paint.Style.FILL); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 838 | mPaint.setAntiAlias(true); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 839 | canvas.drawPath(mBoundingPath, mPaint); |
| 840 | } |
Lucas Dupin | 2b05ccc | 2020-03-04 14:15:52 -0800 | [diff] [blame] | 841 | if (mCameraProtectionProgress > HIDDEN_CAMERA_PROTECTION_SCALE |
| 842 | && !mProtectionRect.isEmpty()) { |
| 843 | canvas.scale(mCameraProtectionProgress, mCameraProtectionProgress, |
| 844 | mProtectionRect.centerX(), mProtectionRect.centerY()); |
| 845 | canvas.drawPath(mProtectionPath, mPaint); |
| 846 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | @Override |
| 850 | public void onDisplayAdded(int displayId) { |
| 851 | } |
| 852 | |
| 853 | @Override |
| 854 | public void onDisplayRemoved(int displayId) { |
| 855 | } |
| 856 | |
| 857 | @Override |
| 858 | public void onDisplayChanged(int displayId) { |
| 859 | if (displayId == getDisplay().getDisplayId()) { |
| 860 | update(); |
| 861 | } |
| 862 | } |
| 863 | |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 864 | public void setRotation(int rotation) { |
| 865 | mRotation = rotation; |
| 866 | update(); |
| 867 | } |
| 868 | |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 869 | void setProtection(Path protectionPath, Rect pathBounds) { |
Evan Laird | 377838b | 2020-03-06 16:55:46 -0500 | [diff] [blame] | 870 | if (mProtectionPathOrig == null) { |
| 871 | mProtectionPathOrig = new Path(); |
| 872 | mProtectionPath = new Path(); |
| 873 | } |
| 874 | mProtectionPathOrig.set(protectionPath); |
Lucas Dupin | 78904621 | 2020-03-10 14:13:02 -0700 | [diff] [blame] | 875 | if (mProtectionRectOrig == null) { |
| 876 | mProtectionRectOrig = new RectF(); |
| 877 | mProtectionRect = new RectF(); |
| 878 | } |
| 879 | mProtectionRectOrig.set(pathBounds); |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void setShowProtection(boolean shouldShow) { |
| 883 | if (mShowProtection == shouldShow) { |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | mShowProtection = shouldShow; |
| 888 | updateBoundingPath(); |
Lucas Dupin | 2b05ccc | 2020-03-04 14:15:52 -0800 | [diff] [blame] | 889 | // Delay the relayout until the end of the animation when hiding the cutout, |
| 890 | // otherwise we'd clip it. |
| 891 | if (mShowProtection) { |
| 892 | requestLayout(); |
| 893 | } |
| 894 | if (mCameraProtectionAnimator != null) { |
| 895 | mCameraProtectionAnimator.cancel(); |
| 896 | } |
| 897 | mCameraProtectionAnimator = ValueAnimator.ofFloat(mCameraProtectionProgress, |
| 898 | mShowProtection ? 1.0f : HIDDEN_CAMERA_PROTECTION_SCALE).setDuration(750); |
| 899 | mCameraProtectionAnimator.setInterpolator(Interpolators.DECELERATE_QUINT); |
| 900 | mCameraProtectionAnimator.addUpdateListener(animation -> { |
| 901 | mCameraProtectionProgress = (float) animation.getAnimatedValue(); |
| 902 | invalidate(); |
| 903 | }); |
| 904 | mCameraProtectionAnimator.addListener(new AnimatorListenerAdapter() { |
| 905 | @Override |
| 906 | public void onAnimationEnd(Animator animation) { |
| 907 | mCameraProtectionAnimator = null; |
| 908 | if (!mShowProtection) { |
| 909 | requestLayout(); |
| 910 | } |
| 911 | } |
| 912 | }); |
| 913 | mCameraProtectionAnimator.start(); |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 914 | } |
| 915 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 916 | private void update() { |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 917 | if (!isAttachedToWindow() || mDecorations.mPendingRotationChange) { |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 918 | return; |
| 919 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 920 | mPosition = getBoundPositionFromRotation(mInitialPosition, mRotation); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 921 | requestLayout(); |
| 922 | getDisplay().getDisplayInfo(mInfo); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 923 | mBounds.clear(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 924 | mBoundingRect.setEmpty(); |
| 925 | mBoundingPath.reset(); |
| 926 | int newVisible; |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 927 | if (shouldDrawCutout(getContext()) && hasCutout()) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 928 | mBounds.addAll(mInfo.displayCutout.getBoundingRects()); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 929 | localBounds(mBoundingRect); |
Adrian Roos | 0098aee | 2019-06-03 16:47:02 +0200 | [diff] [blame] | 930 | updateGravity(); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 931 | updateBoundingPath(); |
Adrian Roos | 1b02828 | 2018-03-14 14:43:03 +0100 | [diff] [blame] | 932 | invalidate(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 933 | newVisible = VISIBLE; |
| 934 | } else { |
| 935 | newVisible = GONE; |
| 936 | } |
| 937 | if (newVisible != getVisibility()) { |
| 938 | setVisibility(newVisible); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 942 | private void updateBoundingPath() { |
| 943 | int lw = mInfo.logicalWidth; |
| 944 | int lh = mInfo.logicalHeight; |
| 945 | |
| 946 | boolean flipped = mInfo.rotation == ROTATION_90 || mInfo.rotation == ROTATION_270; |
| 947 | |
| 948 | int dw = flipped ? lh : lw; |
| 949 | int dh = flipped ? lw : lh; |
| 950 | |
Lucas Dupin | 46b55fe | 2020-04-15 12:43:28 -0700 | [diff] [blame] | 951 | Path path = DisplayCutout.pathFromResources(getResources(), dw, dh); |
| 952 | if (path != null) { |
| 953 | mBoundingPath.set(path); |
| 954 | } else { |
| 955 | mBoundingPath.reset(); |
| 956 | } |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 957 | Matrix m = new Matrix(); |
| 958 | transformPhysicalToLogicalCoordinates(mInfo.rotation, dw, dh, m); |
| 959 | mBoundingPath.transform(m); |
Evan Laird | 377838b | 2020-03-06 16:55:46 -0500 | [diff] [blame] | 960 | if (mProtectionPathOrig != null) { |
| 961 | // Reset the protection path so we don't aggregate rotations |
| 962 | mProtectionPath.set(mProtectionPathOrig); |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 963 | mProtectionPath.transform(m); |
Lucas Dupin | 78904621 | 2020-03-10 14:13:02 -0700 | [diff] [blame] | 964 | m.mapRect(mProtectionRect, mProtectionRectOrig); |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 965 | } |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | private static void transformPhysicalToLogicalCoordinates(@Surface.Rotation int rotation, |
| 969 | @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) { |
| 970 | switch (rotation) { |
| 971 | case ROTATION_0: |
| 972 | out.reset(); |
| 973 | break; |
| 974 | case ROTATION_90: |
| 975 | out.setRotate(270); |
| 976 | out.postTranslate(0, physicalWidth); |
| 977 | break; |
| 978 | case ROTATION_180: |
| 979 | out.setRotate(180); |
| 980 | out.postTranslate(physicalWidth, physicalHeight); |
| 981 | break; |
| 982 | case ROTATION_270: |
| 983 | out.setRotate(90); |
| 984 | out.postTranslate(physicalHeight, 0); |
| 985 | break; |
| 986 | default: |
| 987 | throw new IllegalArgumentException("Unknown rotation: " + rotation); |
| 988 | } |
| 989 | } |
| 990 | |
Adrian Roos | 0098aee | 2019-06-03 16:47:02 +0200 | [diff] [blame] | 991 | private void updateGravity() { |
| 992 | LayoutParams lp = getLayoutParams(); |
| 993 | if (lp instanceof FrameLayout.LayoutParams) { |
| 994 | FrameLayout.LayoutParams flp = (FrameLayout.LayoutParams) lp; |
| 995 | int newGravity = getGravity(mInfo.displayCutout); |
| 996 | if (flp.gravity != newGravity) { |
| 997 | flp.gravity = newGravity; |
| 998 | setLayoutParams(flp); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 1003 | private boolean hasCutout() { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 1004 | final DisplayCutout displayCutout = mInfo.displayCutout; |
| 1005 | if (displayCutout == null) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 1006 | return false; |
| 1007 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1008 | |
| 1009 | if (mPosition == BOUNDS_POSITION_LEFT) { |
| 1010 | return !displayCutout.getBoundingRectLeft().isEmpty(); |
| 1011 | } else if (mPosition == BOUNDS_POSITION_TOP) { |
| 1012 | return !displayCutout.getBoundingRectTop().isEmpty(); |
| 1013 | } else if (mPosition == BOUNDS_POSITION_BOTTOM) { |
| 1014 | return !displayCutout.getBoundingRectBottom().isEmpty(); |
| 1015 | } else if (mPosition == BOUNDS_POSITION_RIGHT) { |
| 1016 | return !displayCutout.getBoundingRectRight().isEmpty(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 1017 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1018 | return false; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | @Override |
| 1022 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1023 | if (mBounds.isEmpty()) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 1024 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 1025 | return; |
| 1026 | } |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 1027 | |
| 1028 | if (mShowProtection) { |
| 1029 | // Make sure that our measured height encompases the protection |
| 1030 | mTotalBounds.union(mBoundingRect); |
Lucas Dupin | 78904621 | 2020-03-10 14:13:02 -0700 | [diff] [blame] | 1031 | mTotalBounds.union((int) mProtectionRect.left, (int) mProtectionRect.top, |
| 1032 | (int) mProtectionRect.right, (int) mProtectionRect.bottom); |
Evan Laird | 1698145 | 2020-02-21 14:33:42 -0500 | [diff] [blame] | 1033 | setMeasuredDimension( |
| 1034 | resolveSizeAndState(mTotalBounds.width(), widthMeasureSpec, 0), |
| 1035 | resolveSizeAndState(mTotalBounds.height(), heightMeasureSpec, 0)); |
| 1036 | } else { |
| 1037 | setMeasuredDimension( |
| 1038 | resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0), |
| 1039 | resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0)); |
| 1040 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 1041 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1042 | |
Jorim Jaggi | 71aaa40 | 2018-06-06 17:22:56 +0200 | [diff] [blame] | 1043 | public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, |
| 1044 | Rect out) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1045 | switch (gravity) { |
| 1046 | case Gravity.TOP: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 1047 | out.set(displayCutout.getBoundingRectTop()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 1048 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1049 | case Gravity.LEFT: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 1050 | out.set(displayCutout.getBoundingRectLeft()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 1051 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1052 | case Gravity.BOTTOM: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 1053 | out.set(displayCutout.getBoundingRectBottom()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 1054 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1055 | case Gravity.RIGHT: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 1056 | out.set(displayCutout.getBoundingRectRight()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 1057 | break; |
| 1058 | default: |
| 1059 | out.setEmpty(); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1060 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | private void localBounds(Rect out) { |
Adrian Roos | 0098aee | 2019-06-03 16:47:02 +0200 | [diff] [blame] | 1064 | DisplayCutout displayCutout = mInfo.displayCutout; |
| 1065 | boundsFromDirection(displayCutout, getGravity(displayCutout), out); |
| 1066 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1067 | |
Adrian Roos | 0098aee | 2019-06-03 16:47:02 +0200 | [diff] [blame] | 1068 | private int getGravity(DisplayCutout displayCutout) { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1069 | if (mPosition == BOUNDS_POSITION_LEFT) { |
shawnlin | bd6d0ad | 2020-01-29 18:38:49 +0800 | [diff] [blame] | 1070 | if (!displayCutout.getBoundingRectLeft().isEmpty()) { |
Adrian Roos | 0098aee | 2019-06-03 16:47:02 +0200 | [diff] [blame] | 1071 | return Gravity.LEFT; |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1072 | } |
| 1073 | } else if (mPosition == BOUNDS_POSITION_TOP) { |
| 1074 | if (!displayCutout.getBoundingRectTop().isEmpty()) { |
Adrian Roos | 0098aee | 2019-06-03 16:47:02 +0200 | [diff] [blame] | 1075 | return Gravity.TOP; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1076 | } |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1077 | } else if (mPosition == BOUNDS_POSITION_BOTTOM) { |
| 1078 | if (!displayCutout.getBoundingRectBottom().isEmpty()) { |
| 1079 | return Gravity.BOTTOM; |
| 1080 | } |
| 1081 | } else if (mPosition == BOUNDS_POSITION_RIGHT) { |
shawnlin | bd6d0ad | 2020-01-29 18:38:49 +0800 | [diff] [blame] | 1082 | if (!displayCutout.getBoundingRectRight().isEmpty()) { |
Adrian Roos | 0098aee | 2019-06-03 16:47:02 +0200 | [diff] [blame] | 1083 | return Gravity.RIGHT; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1084 | } |
| 1085 | } |
Adrian Roos | 0098aee | 2019-06-03 16:47:02 +0200 | [diff] [blame] | 1086 | return Gravity.NO_GRAVITY; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 1087 | } |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 1088 | |
| 1089 | @Override |
| 1090 | public boolean shouldInterceptTouch() { |
| 1091 | return mInfo.displayCutout != null && getVisibility() == VISIBLE; |
| 1092 | } |
| 1093 | |
| 1094 | @Override |
| 1095 | public Region getInterceptRegion() { |
| 1096 | if (mInfo.displayCutout == null) { |
| 1097 | return null; |
| 1098 | } |
| 1099 | |
Adrian Roos | 134e1cb | 2018-05-16 17:04:29 +0200 | [diff] [blame] | 1100 | View rootView = getRootView(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 1101 | Region cutoutBounds = rectsToRegion( |
| 1102 | mInfo.displayCutout.getBoundingRects()); |
Adrian Roos | 134e1cb | 2018-05-16 17:04:29 +0200 | [diff] [blame] | 1103 | |
| 1104 | // Transform to window's coordinate space |
| 1105 | rootView.getLocationOnScreen(mLocation); |
| 1106 | cutoutBounds.translate(-mLocation[0], -mLocation[1]); |
| 1107 | |
| 1108 | // Intersect with window's frame |
| 1109 | cutoutBounds.op(rootView.getLeft(), rootView.getTop(), rootView.getRight(), |
| 1110 | rootView.getBottom(), Region.Op.INTERSECT); |
| 1111 | |
| 1112 | return cutoutBounds; |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 1113 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 1114 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 1115 | |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 1116 | /** |
| 1117 | * A pre-draw listener, that cancels the draw and restarts the traversal with the updated |
| 1118 | * window attributes. |
| 1119 | */ |
| 1120 | private class RestartingPreDrawListener implements ViewTreeObserver.OnPreDrawListener { |
| 1121 | |
| 1122 | private final View mView; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1123 | private final int mTargetRotation; |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1124 | private final int mPosition; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 1125 | |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1126 | private RestartingPreDrawListener(View view, @BoundsPosition int position, |
| 1127 | int targetRotation) { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1128 | mView = view; |
| 1129 | mTargetRotation = targetRotation; |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1130 | mPosition = position; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | @Override |
| 1134 | public boolean onPreDraw() { |
| 1135 | mView.getViewTreeObserver().removeOnPreDrawListener(this); |
| 1136 | |
| 1137 | if (mTargetRotation == mRotation) { |
| 1138 | if (DEBUG) { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1139 | Log.i(TAG, getWindowTitleByPos(mPosition) + " already in target rot " |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1140 | + mTargetRotation + ", allow draw without restarting it"); |
| 1141 | } |
| 1142 | return true; |
| 1143 | } |
| 1144 | |
| 1145 | mPendingRotationChange = false; |
| 1146 | // This changes the window attributes - we need to restart the traversal for them to |
| 1147 | // take effect. |
| 1148 | updateOrientation(); |
| 1149 | if (DEBUG) { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1150 | Log.i(TAG, getWindowTitleByPos(mPosition) |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1151 | + " restarting listener fired, restarting draw for rot " + mRotation); |
| 1152 | } |
| 1153 | mView.invalidate(); |
| 1154 | return false; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /** |
| 1159 | * A pre-draw listener, that validates that the rotation we draw in matches the displays |
| 1160 | * rotation before continuing the draw. |
| 1161 | * |
| 1162 | * This is to prevent a race condition, where we have not received the display changed event |
| 1163 | * yet, and would thus draw in an old orientation. |
| 1164 | */ |
| 1165 | private class ValidatingPreDrawListener implements ViewTreeObserver.OnPreDrawListener { |
| 1166 | |
| 1167 | private final View mView; |
| 1168 | |
| 1169 | public ValidatingPreDrawListener(View view) { |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1170 | mView = view; |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | @Override |
| 1174 | public boolean onPreDraw() { |
shawnlin | 63dfae2 | 2020-02-16 17:15:07 +0800 | [diff] [blame] | 1175 | final int displayRotation = mContext.getDisplay().getRotation(); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1176 | if (displayRotation != mRotation && !mPendingRotationChange) { |
| 1177 | if (DEBUG) { |
| 1178 | Log.i(TAG, "Drawing rot " + mRotation + ", but display is at rot " |
| 1179 | + displayRotation + ". Restarting draw"); |
| 1180 | } |
| 1181 | mView.invalidate(); |
| 1182 | return false; |
| 1183 | } |
| 1184 | return true; |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 1185 | } |
| 1186 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 1187 | } |