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 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 17 | import static android.view.Surface.ROTATION_0; |
| 18 | import static android.view.Surface.ROTATION_180; |
| 19 | import static android.view.Surface.ROTATION_270; |
| 20 | import static android.view.Surface.ROTATION_90; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 21 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 22 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 23 | import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
| 24 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 25 | import static com.android.systemui.tuner.TunablePadding.FLAG_END; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 26 | import static com.android.systemui.tuner.TunablePadding.FLAG_START; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 27 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 28 | import android.annotation.Dimension; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 29 | import android.app.Fragment; |
| 30 | import android.content.Context; |
| 31 | import android.content.res.ColorStateList; |
| 32 | import android.content.res.Configuration; |
| 33 | import android.graphics.Canvas; |
| 34 | import android.graphics.Color; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 35 | import android.graphics.Matrix; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 36 | import android.graphics.Paint; |
| 37 | import android.graphics.Path; |
| 38 | import android.graphics.PixelFormat; |
| 39 | import android.graphics.Rect; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 40 | import android.graphics.Region; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 41 | import android.hardware.display.DisplayManager; |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 42 | import android.os.Handler; |
| 43 | import android.os.HandlerThread; |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 44 | import android.os.SystemProperties; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 45 | import android.provider.Settings.Secure; |
Aurimas Liutikas | a14377a | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 46 | import androidx.annotation.VisibleForTesting; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 47 | import android.util.DisplayMetrics; |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 48 | import android.util.Log; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 49 | import android.view.DisplayCutout; |
| 50 | import android.view.DisplayInfo; |
| 51 | import android.view.Gravity; |
| 52 | import android.view.LayoutInflater; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 53 | import android.view.Surface; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 54 | import android.view.View; |
| 55 | import android.view.View.OnLayoutChangeListener; |
| 56 | import android.view.ViewGroup; |
| 57 | import android.view.ViewGroup.LayoutParams; |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 58 | import android.view.ViewTreeObserver; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 59 | import android.view.WindowManager; |
| 60 | import android.widget.FrameLayout; |
| 61 | import android.widget.ImageView; |
| 62 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 63 | import com.android.internal.util.Preconditions; |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 64 | import com.android.systemui.RegionInterceptingFrameLayout.RegionInterceptableView; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 65 | import com.android.systemui.fragments.FragmentHostManager; |
| 66 | import com.android.systemui.fragments.FragmentHostManager.FragmentListener; |
| 67 | import com.android.systemui.plugins.qs.QS; |
| 68 | import com.android.systemui.qs.SecureSetting; |
| 69 | import com.android.systemui.statusbar.phone.CollapsedStatusBarFragment; |
| 70 | import com.android.systemui.statusbar.phone.StatusBar; |
| 71 | import com.android.systemui.tuner.TunablePadding; |
| 72 | import com.android.systemui.tuner.TunerService; |
| 73 | import com.android.systemui.tuner.TunerService.Tunable; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 74 | import com.android.systemui.util.leak.RotationUtils; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * An overlay that draws screen decorations in software (e.g for rounded corners or display cutout) |
| 78 | * for antialiasing and emulation purposes. |
| 79 | */ |
| 80 | public class ScreenDecorations extends SystemUI implements Tunable { |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 81 | private static final boolean DEBUG = false; |
| 82 | private static final String TAG = "ScreenDecorations"; |
| 83 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 84 | public static final String SIZE = "sysui_rounded_size"; |
| 85 | public static final String PADDING = "sysui_rounded_content_padding"; |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 86 | private static final boolean DEBUG_SCREENSHOT_ROUNDED_CORNERS = |
| 87 | SystemProperties.getBoolean("debug.screenshot_rounded_corners", false); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 88 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 89 | private DisplayManager mDisplayManager; |
| 90 | private DisplayManager.DisplayListener mDisplayListener; |
| 91 | |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 92 | @VisibleForTesting protected int mRoundedDefault; |
| 93 | @VisibleForTesting protected int mRoundedDefaultTop; |
| 94 | @VisibleForTesting protected int mRoundedDefaultBottom; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 95 | private View mOverlay; |
| 96 | private View mBottomOverlay; |
| 97 | private float mDensity; |
| 98 | private WindowManager mWindowManager; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 99 | private int mRotation; |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 100 | private DisplayCutoutView mCutoutTop; |
| 101 | private DisplayCutoutView mCutoutBottom; |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 102 | private boolean mPendingRotationChange; |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 103 | private Handler mHandler; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public void start() { |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 107 | mHandler = startHandlerThread(); |
| 108 | mHandler.post(this::startOnScreenDecorationsThread); |
| 109 | setupStatusBarPaddingIfNeeded(); |
| 110 | } |
| 111 | |
| 112 | @VisibleForTesting |
| 113 | Handler startHandlerThread() { |
| 114 | HandlerThread thread = new HandlerThread("ScreenDecorations"); |
| 115 | thread.start(); |
| 116 | return thread.getThreadHandler(); |
| 117 | } |
| 118 | |
| 119 | private void startOnScreenDecorationsThread() { |
| 120 | mRotation = RotationUtils.getExactRotation(mContext); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 121 | mWindowManager = mContext.getSystemService(WindowManager.class); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 122 | updateRoundedCornerRadii(); |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 123 | if (hasRoundedCorners() || shouldDrawCutout()) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 124 | setupDecorations(); |
| 125 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 126 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 127 | mDisplayListener = new DisplayManager.DisplayListener() { |
| 128 | @Override |
| 129 | public void onDisplayAdded(int displayId) { |
| 130 | // do nothing |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onDisplayRemoved(int displayId) { |
| 135 | // do nothing |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void onDisplayChanged(int displayId) { |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 140 | final int newRotation = RotationUtils.getExactRotation(mContext); |
| 141 | if (mOverlay != null && mBottomOverlay != null && mRotation != newRotation) { |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 142 | // We cannot immediately update the orientation. Otherwise |
| 143 | // WindowManager is still deferring layout until it has finished dispatching |
| 144 | // the config changes, which may cause divergence between what we draw |
| 145 | // (new orientation), and where we are placed on the screen (old orientation). |
| 146 | // Instead we wait until either: |
| 147 | // - we are trying to redraw. This because WM resized our window and told us to. |
| 148 | // - the config change has been dispatched, so WM is no longer deferring layout. |
| 149 | mPendingRotationChange = true; |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 150 | if (DEBUG) { |
| 151 | Log.i(TAG, "Rotation changed, deferring " + newRotation + ", staying at " |
| 152 | + mRotation); |
| 153 | } |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 154 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 155 | mOverlay.getViewTreeObserver().addOnPreDrawListener( |
| 156 | new RestartingPreDrawListener(mOverlay, newRotation)); |
| 157 | mBottomOverlay.getViewTreeObserver().addOnPreDrawListener( |
| 158 | new RestartingPreDrawListener(mBottomOverlay, newRotation)); |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 159 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 160 | updateOrientation(); |
| 161 | } |
| 162 | }; |
| 163 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 164 | mDisplayManager = (DisplayManager) mContext.getSystemService( |
| 165 | Context.DISPLAY_SERVICE); |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 166 | mDisplayManager.registerDisplayListener(mDisplayListener, mHandler); |
| 167 | updateOrientation(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private void setupDecorations() { |
| 171 | mOverlay = LayoutInflater.from(mContext) |
| 172 | .inflate(R.layout.rounded_corners, null); |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 173 | mCutoutTop = new DisplayCutoutView(mContext, true, |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 174 | this::updateWindowVisibilities, this); |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 175 | ((ViewGroup)mOverlay).addView(mCutoutTop); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 176 | mBottomOverlay = LayoutInflater.from(mContext) |
| 177 | .inflate(R.layout.rounded_corners, null); |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 178 | mCutoutBottom = new DisplayCutoutView(mContext, false, |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 179 | this::updateWindowVisibilities, this); |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 180 | ((ViewGroup)mBottomOverlay).addView(mCutoutBottom); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 181 | |
| 182 | mOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 183 | mOverlay.setAlpha(0); |
| 184 | |
| 185 | mBottomOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 186 | mBottomOverlay.setAlpha(0); |
| 187 | |
| 188 | updateViews(); |
| 189 | |
| 190 | mWindowManager.addView(mOverlay, getWindowLayoutParams()); |
| 191 | mWindowManager.addView(mBottomOverlay, getBottomLayoutParams()); |
| 192 | |
| 193 | DisplayMetrics metrics = new DisplayMetrics(); |
| 194 | mWindowManager.getDefaultDisplay().getMetrics(metrics); |
| 195 | mDensity = metrics.density; |
| 196 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 197 | Dependency.get(Dependency.MAIN_HANDLER).post( |
| 198 | () -> Dependency.get(TunerService.class).addTunable(this, SIZE)); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 199 | |
| 200 | // Watch color inversion and invert the overlay as needed. |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 201 | SecureSetting setting = new SecureSetting(mContext, mHandler, |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 202 | Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) { |
| 203 | @Override |
| 204 | protected void handleValueChanged(int value, boolean observedChange) { |
| 205 | int tint = value != 0 ? Color.WHITE : Color.BLACK; |
| 206 | ColorStateList tintList = ColorStateList.valueOf(tint); |
| 207 | ((ImageView) mOverlay.findViewById(R.id.left)).setImageTintList(tintList); |
| 208 | ((ImageView) mOverlay.findViewById(R.id.right)).setImageTintList(tintList); |
| 209 | ((ImageView) mBottomOverlay.findViewById(R.id.left)).setImageTintList(tintList); |
| 210 | ((ImageView) mBottomOverlay.findViewById(R.id.right)).setImageTintList(tintList); |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 211 | mCutoutTop.setColor(tint); |
| 212 | mCutoutBottom.setColor(tint); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 213 | } |
| 214 | }; |
| 215 | setting.setListening(true); |
| 216 | setting.onChange(false); |
| 217 | |
| 218 | mOverlay.addOnLayoutChangeListener(new OnLayoutChangeListener() { |
| 219 | @Override |
| 220 | public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 221 | int oldLeft, |
| 222 | int oldTop, int oldRight, int oldBottom) { |
| 223 | mOverlay.removeOnLayoutChangeListener(this); |
| 224 | mOverlay.animate() |
| 225 | .alpha(1) |
| 226 | .setDuration(1000) |
| 227 | .start(); |
| 228 | mBottomOverlay.animate() |
| 229 | .alpha(1) |
| 230 | .setDuration(1000) |
| 231 | .start(); |
| 232 | } |
| 233 | }); |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 234 | |
| 235 | mOverlay.getViewTreeObserver().addOnPreDrawListener( |
| 236 | new ValidatingPreDrawListener(mOverlay)); |
| 237 | mBottomOverlay.getViewTreeObserver().addOnPreDrawListener( |
| 238 | new ValidatingPreDrawListener(mBottomOverlay)); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | protected void onConfigurationChanged(Configuration newConfig) { |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 243 | mHandler.post(() -> { |
| 244 | int oldRotation = mRotation; |
| 245 | mPendingRotationChange = false; |
| 246 | updateOrientation(); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 247 | updateRoundedCornerRadii(); |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 248 | if (DEBUG) Log.i(TAG, "onConfigChanged from rot " + oldRotation + " to " + mRotation); |
| 249 | if (shouldDrawCutout() && mOverlay == null) { |
| 250 | setupDecorations(); |
| 251 | } |
| 252 | if (mOverlay != null) { |
| 253 | // Updating the layout params ensures that ViewRootImpl will call relayoutWindow(), |
| 254 | // which ensures that the forced seamless rotation will end, even if we updated |
| 255 | // the rotation before window manager was ready (and was still waiting for sending |
| 256 | // the updated rotation). |
| 257 | updateLayoutParams(); |
| 258 | } |
| 259 | }); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 260 | } |
| 261 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 262 | private void updateOrientation() { |
| 263 | Preconditions.checkState(mHandler.getLooper().getThread() == Thread.currentThread(), |
| 264 | "must call on " + mHandler.getLooper().getThread() |
| 265 | + ", but was " + Thread.currentThread()); |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 266 | if (mPendingRotationChange) { |
| 267 | return; |
| 268 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 269 | int newRotation = RotationUtils.getExactRotation(mContext); |
| 270 | if (newRotation != mRotation) { |
| 271 | mRotation = newRotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 272 | |
| 273 | if (mOverlay != null) { |
| 274 | updateLayoutParams(); |
| 275 | updateViews(); |
| 276 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 280 | private void updateRoundedCornerRadii() { |
| 281 | final int newRoundedDefault = mContext.getResources().getDimensionPixelSize( |
| 282 | R.dimen.rounded_corner_radius); |
| 283 | final int newRoundedDefaultTop = mContext.getResources().getDimensionPixelSize( |
| 284 | R.dimen.rounded_corner_radius_top); |
| 285 | final int newRoundedDefaultBottom = mContext.getResources().getDimensionPixelSize( |
| 286 | R.dimen.rounded_corner_radius_bottom); |
| 287 | |
| 288 | final boolean roundedCornersChanged = mRoundedDefault != newRoundedDefault |
| 289 | || mRoundedDefaultBottom != newRoundedDefaultBottom |
| 290 | || mRoundedDefaultTop != newRoundedDefaultTop; |
| 291 | |
| 292 | if (roundedCornersChanged) { |
| 293 | mRoundedDefault = newRoundedDefault; |
| 294 | mRoundedDefaultTop = newRoundedDefaultTop; |
| 295 | mRoundedDefaultBottom = newRoundedDefaultBottom; |
| 296 | onTuningChanged(SIZE, null); |
| 297 | } |
| 298 | } |
| 299 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 300 | private void updateViews() { |
| 301 | View topLeft = mOverlay.findViewById(R.id.left); |
| 302 | View topRight = mOverlay.findViewById(R.id.right); |
| 303 | View bottomLeft = mBottomOverlay.findViewById(R.id.left); |
| 304 | View bottomRight = mBottomOverlay.findViewById(R.id.right); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 305 | |
| 306 | if (mRotation == RotationUtils.ROTATION_NONE) { |
| 307 | updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0); |
| 308 | updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90); |
| 309 | updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270); |
| 310 | updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180); |
| 311 | } else if (mRotation == RotationUtils.ROTATION_LANDSCAPE) { |
| 312 | updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0); |
| 313 | updateView(topRight, Gravity.BOTTOM | Gravity.LEFT, 270); |
| 314 | updateView(bottomLeft, Gravity.TOP | Gravity.RIGHT, 90);; |
| 315 | updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180); |
| 316 | } else if (mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) { |
| 317 | updateView(topLeft, Gravity.BOTTOM | Gravity.LEFT, 270); |
| 318 | updateView(topRight, Gravity.BOTTOM | Gravity.RIGHT, 180); |
| 319 | updateView(bottomLeft, Gravity.TOP | Gravity.LEFT, 0); |
| 320 | updateView(bottomRight, Gravity.TOP | Gravity.RIGHT, 90); |
| 321 | } else if (mRotation == RotationUtils.ROTATION_SEASCAPE) { |
| 322 | updateView(topLeft, Gravity.BOTTOM | Gravity.RIGHT, 180); |
| 323 | updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90); |
| 324 | updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270); |
| 325 | updateView(bottomRight, Gravity.TOP | Gravity.LEFT, 0); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 326 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 327 | |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 328 | mCutoutTop.setRotation(mRotation); |
| 329 | mCutoutBottom.setRotation(mRotation); |
| 330 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 331 | updateWindowVisibilities(); |
| 332 | } |
| 333 | |
| 334 | private void updateView(View v, int gravity, int rotation) { |
| 335 | ((FrameLayout.LayoutParams)v.getLayoutParams()).gravity = gravity; |
| 336 | v.setRotation(rotation); |
| 337 | } |
| 338 | |
| 339 | private void updateWindowVisibilities() { |
| 340 | updateWindowVisibility(mOverlay); |
| 341 | updateWindowVisibility(mBottomOverlay); |
| 342 | } |
| 343 | |
| 344 | private void updateWindowVisibility(View overlay) { |
| 345 | boolean visibleForCutout = shouldDrawCutout() |
| 346 | && overlay.findViewById(R.id.display_cutout).getVisibility() == View.VISIBLE; |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 347 | boolean visibleForRoundedCorners = hasRoundedCorners(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 348 | overlay.setVisibility(visibleForCutout || visibleForRoundedCorners |
| 349 | ? View.VISIBLE : View.GONE); |
| 350 | } |
| 351 | |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 352 | private boolean hasRoundedCorners() { |
| 353 | return mRoundedDefault > 0 || mRoundedDefaultBottom > 0 || mRoundedDefaultTop > 0; |
| 354 | } |
| 355 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 356 | private boolean shouldDrawCutout() { |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 357 | return shouldDrawCutout(mContext); |
| 358 | } |
| 359 | |
| 360 | static boolean shouldDrawCutout(Context context) { |
| 361 | return context.getResources().getBoolean( |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 362 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout); |
| 363 | } |
| 364 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 365 | |
| 366 | private void setupStatusBarPaddingIfNeeded() { |
| 367 | // TODO: This should be moved to a more appropriate place, as it is not related to the |
| 368 | // screen decorations overlay. |
| 369 | int padding = mContext.getResources().getDimensionPixelSize( |
| 370 | R.dimen.rounded_corner_content_padding); |
| 371 | if (padding != 0) { |
| 372 | setupStatusBarPadding(padding); |
| 373 | } |
| 374 | |
| 375 | } |
| 376 | |
| 377 | private void setupStatusBarPadding(int padding) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 378 | // Add some padding to all the content near the edge of the screen. |
| 379 | StatusBar sb = getComponent(StatusBar.class); |
| 380 | View statusBar = (sb != null ? sb.getStatusBarWindow() : null); |
| 381 | if (statusBar != null) { |
| 382 | TunablePadding.addTunablePadding(statusBar.findViewById(R.id.keyguard_header), PADDING, |
| 383 | padding, FLAG_END); |
| 384 | |
| 385 | FragmentHostManager fragmentHostManager = FragmentHostManager.get(statusBar); |
| 386 | fragmentHostManager.addTagListener(CollapsedStatusBarFragment.TAG, |
| 387 | new TunablePaddingTagListener(padding, R.id.status_bar)); |
| 388 | fragmentHostManager.addTagListener(QS.TAG, |
| 389 | new TunablePaddingTagListener(padding, R.id.header)); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | @VisibleForTesting |
| 394 | WindowManager.LayoutParams getWindowLayoutParams() { |
| 395 | final WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 396 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 397 | LayoutParams.WRAP_CONTENT, |
| 398 | WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL, |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 399 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 400 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 401 | | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
| 402 | | WindowManager.LayoutParams.FLAG_SLIPPERY |
| 403 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, |
| 404 | PixelFormat.TRANSLUCENT); |
Robert Carr | 772e8bc | 2018-03-14 11:51:23 -0700 | [diff] [blame] | 405 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS |
| 406 | | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION; |
| 407 | |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 408 | if (!DEBUG_SCREENSHOT_ROUNDED_CORNERS) { |
| 409 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY; |
| 410 | } |
Robert Carr | 772e8bc | 2018-03-14 11:51:23 -0700 | [diff] [blame] | 411 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 412 | lp.setTitle("ScreenDecorOverlay"); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 413 | if (mRotation == RotationUtils.ROTATION_SEASCAPE |
| 414 | || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) { |
| 415 | lp.gravity = Gravity.BOTTOM | Gravity.RIGHT; |
| 416 | } else { |
| 417 | lp.gravity = Gravity.TOP | Gravity.LEFT; |
| 418 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 419 | lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 420 | if (isLandscape(mRotation)) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 421 | lp.width = WRAP_CONTENT; |
| 422 | lp.height = MATCH_PARENT; |
| 423 | } |
| 424 | return lp; |
| 425 | } |
| 426 | |
| 427 | private WindowManager.LayoutParams getBottomLayoutParams() { |
| 428 | WindowManager.LayoutParams lp = getWindowLayoutParams(); |
| 429 | lp.setTitle("ScreenDecorOverlayBottom"); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 430 | if (mRotation == RotationUtils.ROTATION_SEASCAPE |
| 431 | || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) { |
| 432 | lp.gravity = Gravity.TOP | Gravity.LEFT; |
| 433 | } else { |
| 434 | lp.gravity = Gravity.BOTTOM | Gravity.RIGHT; |
| 435 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 436 | return lp; |
| 437 | } |
| 438 | |
| 439 | private void updateLayoutParams() { |
| 440 | mWindowManager.updateViewLayout(mOverlay, getWindowLayoutParams()); |
| 441 | mWindowManager.updateViewLayout(mBottomOverlay, getBottomLayoutParams()); |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public void onTuningChanged(String key, String newValue) { |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 446 | mHandler.post(() -> { |
| 447 | if (mOverlay == null) return; |
| 448 | if (SIZE.equals(key)) { |
| 449 | int size = mRoundedDefault; |
| 450 | int sizeTop = mRoundedDefaultTop; |
| 451 | int sizeBottom = mRoundedDefaultBottom; |
| 452 | if (newValue != null) { |
| 453 | try { |
| 454 | size = (int) (Integer.parseInt(newValue) * mDensity); |
| 455 | } catch (Exception e) { |
| 456 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 457 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 458 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 459 | if (sizeTop == 0) { |
| 460 | sizeTop = size; |
| 461 | } |
| 462 | if (sizeBottom == 0) { |
| 463 | sizeBottom = size; |
| 464 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 465 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 466 | setSize(mOverlay.findViewById(R.id.left), sizeTop); |
| 467 | setSize(mOverlay.findViewById(R.id.right), sizeTop); |
| 468 | setSize(mBottomOverlay.findViewById(R.id.left), sizeBottom); |
| 469 | setSize(mBottomOverlay.findViewById(R.id.right), sizeBottom); |
| 470 | } |
| 471 | }); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | private void setSize(View view, int pixelSize) { |
| 475 | LayoutParams params = view.getLayoutParams(); |
| 476 | params.width = pixelSize; |
| 477 | params.height = pixelSize; |
| 478 | view.setLayoutParams(params); |
| 479 | } |
| 480 | |
| 481 | @VisibleForTesting |
| 482 | static class TunablePaddingTagListener implements FragmentListener { |
| 483 | |
| 484 | private final int mPadding; |
| 485 | private final int mId; |
| 486 | private TunablePadding mTunablePadding; |
| 487 | |
| 488 | public TunablePaddingTagListener(int padding, int id) { |
| 489 | mPadding = padding; |
| 490 | mId = id; |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public void onFragmentViewCreated(String tag, Fragment fragment) { |
| 495 | if (mTunablePadding != null) { |
| 496 | mTunablePadding.destroy(); |
| 497 | } |
| 498 | View view = fragment.getView(); |
| 499 | if (mId != 0) { |
| 500 | view = view.findViewById(mId); |
| 501 | } |
| 502 | mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding, |
| 503 | FLAG_START | FLAG_END); |
| 504 | } |
| 505 | } |
| 506 | |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 507 | public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener, |
| 508 | RegionInterceptableView { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 509 | |
| 510 | private final DisplayInfo mInfo = new DisplayInfo(); |
| 511 | private final Paint mPaint = new Paint(); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 512 | private final Region mBounds = new Region(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 513 | private final Rect mBoundingRect = new Rect(); |
| 514 | private final Path mBoundingPath = new Path(); |
| 515 | private final int[] mLocation = new int[2]; |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 516 | private final boolean mInitialStart; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 517 | private final Runnable mVisibilityChangedListener; |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 518 | private final ScreenDecorations mDecorations; |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 519 | private int mColor = Color.BLACK; |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 520 | private boolean mStart; |
| 521 | private int mRotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 522 | |
| 523 | public DisplayCutoutView(Context context, boolean start, |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 524 | Runnable visibilityChangedListener, ScreenDecorations decorations) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 525 | super(context); |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 526 | mInitialStart = start; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 527 | mVisibilityChangedListener = visibilityChangedListener; |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 528 | mDecorations = decorations; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 529 | setId(R.id.display_cutout); |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 530 | if (DEBUG) { |
| 531 | getViewTreeObserver().addOnDrawListener(() -> Log.i(TAG, |
| 532 | (mInitialStart ? "OverlayTop" : "OverlayBottom") |
| 533 | + " drawn in rot " + mRotation)); |
| 534 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 535 | } |
| 536 | |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 537 | public void setColor(int color) { |
| 538 | mColor = color; |
| 539 | invalidate(); |
| 540 | } |
| 541 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 542 | @Override |
| 543 | protected void onAttachedToWindow() { |
| 544 | super.onAttachedToWindow(); |
| 545 | mContext.getSystemService(DisplayManager.class).registerDisplayListener(this, |
| 546 | getHandler()); |
| 547 | update(); |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | protected void onDetachedFromWindow() { |
| 552 | super.onDetachedFromWindow(); |
| 553 | mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this); |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | protected void onDraw(Canvas canvas) { |
| 558 | super.onDraw(canvas); |
| 559 | getLocationOnScreen(mLocation); |
| 560 | canvas.translate(-mLocation[0], -mLocation[1]); |
| 561 | if (!mBoundingPath.isEmpty()) { |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 562 | mPaint.setColor(mColor); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 563 | mPaint.setStyle(Paint.Style.FILL); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 564 | mPaint.setAntiAlias(true); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 565 | canvas.drawPath(mBoundingPath, mPaint); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public void onDisplayAdded(int displayId) { |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public void onDisplayRemoved(int displayId) { |
| 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public void onDisplayChanged(int displayId) { |
| 579 | if (displayId == getDisplay().getDisplayId()) { |
| 580 | update(); |
| 581 | } |
| 582 | } |
| 583 | |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 584 | public void setRotation(int rotation) { |
| 585 | mRotation = rotation; |
| 586 | update(); |
| 587 | } |
| 588 | |
| 589 | private boolean isStart() { |
| 590 | final boolean flipped = (mRotation == RotationUtils.ROTATION_SEASCAPE |
| 591 | || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN); |
| 592 | return flipped ? !mInitialStart : mInitialStart; |
| 593 | } |
| 594 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 595 | private void update() { |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 596 | if (!isAttachedToWindow() || mDecorations.mPendingRotationChange) { |
Adrian Roos | beeb7f4 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 597 | return; |
| 598 | } |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 599 | mStart = isStart(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 600 | requestLayout(); |
| 601 | getDisplay().getDisplayInfo(mInfo); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 602 | mBounds.setEmpty(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 603 | mBoundingRect.setEmpty(); |
| 604 | mBoundingPath.reset(); |
| 605 | int newVisible; |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 606 | if (shouldDrawCutout(getContext()) && hasCutout()) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 607 | mBounds.set(mInfo.displayCutout.getBounds()); |
| 608 | localBounds(mBoundingRect); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 609 | updateBoundingPath(); |
Adrian Roos | 1b02828 | 2018-03-14 14:43:03 +0100 | [diff] [blame] | 610 | invalidate(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 611 | newVisible = VISIBLE; |
| 612 | } else { |
| 613 | newVisible = GONE; |
| 614 | } |
| 615 | if (newVisible != getVisibility()) { |
| 616 | setVisibility(newVisible); |
| 617 | mVisibilityChangedListener.run(); |
| 618 | } |
| 619 | } |
| 620 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 621 | private void updateBoundingPath() { |
| 622 | int lw = mInfo.logicalWidth; |
| 623 | int lh = mInfo.logicalHeight; |
| 624 | |
| 625 | boolean flipped = mInfo.rotation == ROTATION_90 || mInfo.rotation == ROTATION_270; |
| 626 | |
| 627 | int dw = flipped ? lh : lw; |
| 628 | int dh = flipped ? lw : lh; |
| 629 | |
Adrian Roos | a99f5d6 | 2018-04-16 16:03:04 +0200 | [diff] [blame] | 630 | mBoundingPath.set(DisplayCutout.pathFromResources(getResources(), dw, dh)); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 631 | Matrix m = new Matrix(); |
| 632 | transformPhysicalToLogicalCoordinates(mInfo.rotation, dw, dh, m); |
| 633 | mBoundingPath.transform(m); |
| 634 | } |
| 635 | |
| 636 | private static void transformPhysicalToLogicalCoordinates(@Surface.Rotation int rotation, |
| 637 | @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) { |
| 638 | switch (rotation) { |
| 639 | case ROTATION_0: |
| 640 | out.reset(); |
| 641 | break; |
| 642 | case ROTATION_90: |
| 643 | out.setRotate(270); |
| 644 | out.postTranslate(0, physicalWidth); |
| 645 | break; |
| 646 | case ROTATION_180: |
| 647 | out.setRotate(180); |
| 648 | out.postTranslate(physicalWidth, physicalHeight); |
| 649 | break; |
| 650 | case ROTATION_270: |
| 651 | out.setRotate(90); |
| 652 | out.postTranslate(physicalHeight, 0); |
| 653 | break; |
| 654 | default: |
| 655 | throw new IllegalArgumentException("Unknown rotation: " + rotation); |
| 656 | } |
| 657 | } |
| 658 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 659 | private boolean hasCutout() { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 660 | final DisplayCutout displayCutout = mInfo.displayCutout; |
| 661 | if (displayCutout == null) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 662 | return false; |
| 663 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 664 | if (mStart) { |
| 665 | return displayCutout.getSafeInsetLeft() > 0 |
| 666 | || displayCutout.getSafeInsetTop() > 0; |
| 667 | } else { |
| 668 | return displayCutout.getSafeInsetRight() > 0 |
| 669 | || displayCutout.getSafeInsetBottom() > 0; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | @Override |
| 674 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 675 | if (mBounds.isEmpty()) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 676 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 677 | return; |
| 678 | } |
| 679 | setMeasuredDimension( |
| 680 | resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0), |
| 681 | resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0)); |
| 682 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 683 | |
Jorim Jaggi | 0d4a995 | 2018-06-06 17:22:56 +0200 | [diff] [blame] | 684 | public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, |
| 685 | Rect out) { |
| 686 | Region bounds = boundsFromDirection(displayCutout, gravity); |
| 687 | out.set(bounds.getBounds()); |
| 688 | bounds.recycle(); |
| 689 | } |
| 690 | |
| 691 | public static Region boundsFromDirection(DisplayCutout displayCutout, int gravity) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 692 | Region bounds = displayCutout.getBounds(); |
| 693 | switch (gravity) { |
| 694 | case Gravity.TOP: |
| 695 | bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(), |
| 696 | Region.Op.INTERSECT); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 697 | break; |
| 698 | case Gravity.LEFT: |
| 699 | bounds.op(0, 0, displayCutout.getSafeInsetLeft(), Integer.MAX_VALUE, |
| 700 | Region.Op.INTERSECT); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 701 | break; |
| 702 | case Gravity.BOTTOM: |
| 703 | bounds.op(0, displayCutout.getSafeInsetTop() + 1, Integer.MAX_VALUE, |
| 704 | Integer.MAX_VALUE, Region.Op.INTERSECT); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 705 | break; |
| 706 | case Gravity.RIGHT: |
| 707 | bounds.op(displayCutout.getSafeInsetLeft() + 1, 0, Integer.MAX_VALUE, |
| 708 | Integer.MAX_VALUE, Region.Op.INTERSECT); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 709 | break; |
| 710 | } |
Jorim Jaggi | 0d4a995 | 2018-06-06 17:22:56 +0200 | [diff] [blame] | 711 | return bounds; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | private void localBounds(Rect out) { |
| 715 | final DisplayCutout displayCutout = mInfo.displayCutout; |
| 716 | |
| 717 | if (mStart) { |
| 718 | if (displayCutout.getSafeInsetLeft() > 0) { |
| 719 | boundsFromDirection(displayCutout, Gravity.LEFT, out); |
| 720 | } else if (displayCutout.getSafeInsetTop() > 0) { |
| 721 | boundsFromDirection(displayCutout, Gravity.TOP, out); |
| 722 | } |
| 723 | } else { |
| 724 | if (displayCutout.getSafeInsetRight() > 0) { |
| 725 | boundsFromDirection(displayCutout, Gravity.RIGHT, out); |
| 726 | } else if (displayCutout.getSafeInsetBottom() > 0) { |
| 727 | boundsFromDirection(displayCutout, Gravity.BOTTOM, out); |
| 728 | } |
| 729 | } |
| 730 | } |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 731 | |
| 732 | @Override |
| 733 | public boolean shouldInterceptTouch() { |
| 734 | return mInfo.displayCutout != null && getVisibility() == VISIBLE; |
| 735 | } |
| 736 | |
| 737 | @Override |
| 738 | public Region getInterceptRegion() { |
| 739 | if (mInfo.displayCutout == null) { |
| 740 | return null; |
| 741 | } |
| 742 | |
Adrian Roos | 134e1cb | 2018-05-16 17:04:29 +0200 | [diff] [blame] | 743 | View rootView = getRootView(); |
| 744 | Region cutoutBounds = mInfo.displayCutout.getBounds(); |
| 745 | |
| 746 | // Transform to window's coordinate space |
| 747 | rootView.getLocationOnScreen(mLocation); |
| 748 | cutoutBounds.translate(-mLocation[0], -mLocation[1]); |
| 749 | |
| 750 | // Intersect with window's frame |
| 751 | cutoutBounds.op(rootView.getLeft(), rootView.getTop(), rootView.getRight(), |
| 752 | rootView.getBottom(), Region.Op.INTERSECT); |
| 753 | |
| 754 | return cutoutBounds; |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 755 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 756 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 757 | |
| 758 | private boolean isLandscape(int rotation) { |
| 759 | return rotation == RotationUtils.ROTATION_LANDSCAPE || rotation == |
| 760 | RotationUtils.ROTATION_SEASCAPE; |
| 761 | } |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 762 | |
| 763 | /** |
| 764 | * A pre-draw listener, that cancels the draw and restarts the traversal with the updated |
| 765 | * window attributes. |
| 766 | */ |
| 767 | private class RestartingPreDrawListener implements ViewTreeObserver.OnPreDrawListener { |
| 768 | |
| 769 | private final View mView; |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 770 | private final int mTargetRotation; |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 771 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 772 | private RestartingPreDrawListener(View view, int targetRotation) { |
| 773 | mView = view; |
| 774 | mTargetRotation = targetRotation; |
| 775 | } |
| 776 | |
| 777 | @Override |
| 778 | public boolean onPreDraw() { |
| 779 | mView.getViewTreeObserver().removeOnPreDrawListener(this); |
| 780 | |
| 781 | if (mTargetRotation == mRotation) { |
| 782 | if (DEBUG) { |
| 783 | Log.i(TAG, (mView == mOverlay ? "OverlayTop" : "OverlayBottom") |
| 784 | + " already in target rot " |
| 785 | + mTargetRotation + ", allow draw without restarting it"); |
| 786 | } |
| 787 | return true; |
| 788 | } |
| 789 | |
| 790 | mPendingRotationChange = false; |
| 791 | // This changes the window attributes - we need to restart the traversal for them to |
| 792 | // take effect. |
| 793 | updateOrientation(); |
| 794 | if (DEBUG) { |
| 795 | Log.i(TAG, (mView == mOverlay ? "OverlayTop" : "OverlayBottom") |
| 796 | + " restarting listener fired, restarting draw for rot " + mRotation); |
| 797 | } |
| 798 | mView.invalidate(); |
| 799 | return false; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * A pre-draw listener, that validates that the rotation we draw in matches the displays |
| 805 | * rotation before continuing the draw. |
| 806 | * |
| 807 | * This is to prevent a race condition, where we have not received the display changed event |
| 808 | * yet, and would thus draw in an old orientation. |
| 809 | */ |
| 810 | private class ValidatingPreDrawListener implements ViewTreeObserver.OnPreDrawListener { |
| 811 | |
| 812 | private final View mView; |
| 813 | |
| 814 | public ValidatingPreDrawListener(View view) { |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 815 | mView = view; |
| 816 | } |
| 817 | |
| 818 | @Override |
| 819 | public boolean onPreDraw() { |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 820 | final int displayRotation = RotationUtils.getExactRotation(mContext); |
| 821 | if (displayRotation != mRotation && !mPendingRotationChange) { |
| 822 | if (DEBUG) { |
| 823 | Log.i(TAG, "Drawing rot " + mRotation + ", but display is at rot " |
| 824 | + displayRotation + ". Restarting draw"); |
| 825 | } |
| 826 | mView.invalidate(); |
| 827 | return false; |
| 828 | } |
| 829 | return true; |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 830 | } |
| 831 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 832 | } |