blob: e347a144dc9c674d981ab89686d9a0bde4e3ab4d [file] [log] [blame]
Adrian Roos5b518852018-01-23 17:23:38 +01001/*
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
15package com.android.systemui;
16
Adrian Roos51072a82018-04-10 15:17:08 -070017import static android.view.Surface.ROTATION_0;
18import static android.view.Surface.ROTATION_180;
19import static android.view.Surface.ROTATION_270;
20import static android.view.Surface.ROTATION_90;
Adrian Roos5b518852018-01-23 17:23:38 +010021import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
22import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
23import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
24
Adrian Roos5b518852018-01-23 17:23:38 +010025import static com.android.systemui.tuner.TunablePadding.FLAG_END;
Beverlye91f0d02018-05-15 14:40:47 -040026import static com.android.systemui.tuner.TunablePadding.FLAG_START;
Adrian Roos5b518852018-01-23 17:23:38 +010027
Adrian Roos51072a82018-04-10 15:17:08 -070028import android.annotation.Dimension;
Adrian Roos5b518852018-01-23 17:23:38 +010029import android.app.Fragment;
30import android.content.Context;
31import android.content.res.ColorStateList;
32import android.content.res.Configuration;
33import android.graphics.Canvas;
34import android.graphics.Color;
Adrian Roos51072a82018-04-10 15:17:08 -070035import android.graphics.Matrix;
Adrian Roos5b518852018-01-23 17:23:38 +010036import android.graphics.Paint;
37import android.graphics.Path;
38import android.graphics.PixelFormat;
39import android.graphics.Rect;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010040import android.graphics.Region;
Adrian Roos5b518852018-01-23 17:23:38 +010041import android.hardware.display.DisplayManager;
Adrian Roos56d1a2c2018-03-08 23:22:19 +010042import android.os.SystemProperties;
Adrian Roos5b518852018-01-23 17:23:38 +010043import android.provider.Settings.Secure;
44import android.support.annotation.VisibleForTesting;
45import android.util.DisplayMetrics;
46import android.view.DisplayCutout;
47import android.view.DisplayInfo;
48import android.view.Gravity;
49import android.view.LayoutInflater;
Adrian Roos51072a82018-04-10 15:17:08 -070050import android.view.Surface;
Adrian Roos5b518852018-01-23 17:23:38 +010051import android.view.View;
52import android.view.View.OnLayoutChangeListener;
53import android.view.ViewGroup;
54import android.view.ViewGroup.LayoutParams;
55import android.view.WindowManager;
56import android.widget.FrameLayout;
57import android.widget.ImageView;
58
Evan Lairdb0506ca2018-03-15 10:34:31 -040059import com.android.systemui.RegionInterceptingFrameLayout.RegionInterceptableView;
Adrian Roos5b518852018-01-23 17:23:38 +010060import com.android.systemui.fragments.FragmentHostManager;
61import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
62import com.android.systemui.plugins.qs.QS;
63import com.android.systemui.qs.SecureSetting;
64import com.android.systemui.statusbar.phone.CollapsedStatusBarFragment;
65import com.android.systemui.statusbar.phone.StatusBar;
66import com.android.systemui.tuner.TunablePadding;
67import com.android.systemui.tuner.TunerService;
68import com.android.systemui.tuner.TunerService.Tunable;
Beverlye91f0d02018-05-15 14:40:47 -040069import com.android.systemui.util.leak.RotationUtils;
Adrian Roos5b518852018-01-23 17:23:38 +010070
71/**
72 * An overlay that draws screen decorations in software (e.g for rounded corners or display cutout)
73 * for antialiasing and emulation purposes.
74 */
75public class ScreenDecorations extends SystemUI implements Tunable {
76 public static final String SIZE = "sysui_rounded_size";
77 public static final String PADDING = "sysui_rounded_content_padding";
Adrian Roos56d1a2c2018-03-08 23:22:19 +010078 private static final boolean DEBUG_SCREENSHOT_ROUNDED_CORNERS =
79 SystemProperties.getBoolean("debug.screenshot_rounded_corners", false);
Adrian Roos5b518852018-01-23 17:23:38 +010080
Beverlye91f0d02018-05-15 14:40:47 -040081 private DisplayManager mDisplayManager;
82 private DisplayManager.DisplayListener mDisplayListener;
83
Adrian Roos5b518852018-01-23 17:23:38 +010084 private int mRoundedDefault;
Beverlya5f7a302018-04-25 09:19:05 -040085 private int mRoundedDefaultTop;
86 private int mRoundedDefaultBottom;
Adrian Roos5b518852018-01-23 17:23:38 +010087 private View mOverlay;
88 private View mBottomOverlay;
89 private float mDensity;
90 private WindowManager mWindowManager;
Beverlye91f0d02018-05-15 14:40:47 -040091 private int mRotation;
Adrian Roos5b518852018-01-23 17:23:38 +010092
93 @Override
94 public void start() {
95 mWindowManager = mContext.getSystemService(WindowManager.class);
96 mRoundedDefault = mContext.getResources().getDimensionPixelSize(
97 R.dimen.rounded_corner_radius);
Beverlya5f7a302018-04-25 09:19:05 -040098 mRoundedDefaultTop = mContext.getResources().getDimensionPixelSize(
99 R.dimen.rounded_corner_radius_top);
100 mRoundedDefaultBottom = mContext.getResources().getDimensionPixelSize(
101 R.dimen.rounded_corner_radius_bottom);
102 if (hasRoundedCorners() || shouldDrawCutout()) {
Adrian Roos5b518852018-01-23 17:23:38 +0100103 setupDecorations();
104 }
Beverlya5f7a302018-04-25 09:19:05 -0400105
Adrian Roos5b518852018-01-23 17:23:38 +0100106 int padding = mContext.getResources().getDimensionPixelSize(
107 R.dimen.rounded_corner_content_padding);
108 if (padding != 0) {
109 setupPadding(padding);
110 }
Beverlye91f0d02018-05-15 14:40:47 -0400111
112 mDisplayListener = new DisplayManager.DisplayListener() {
113 @Override
114 public void onDisplayAdded(int displayId) {
115 // do nothing
116 }
117
118 @Override
119 public void onDisplayRemoved(int displayId) {
120 // do nothing
121 }
122
123 @Override
124 public void onDisplayChanged(int displayId) {
125 updateOrientation();
126 }
127 };
128
129 mRotation = -1;
130 mDisplayManager = (DisplayManager) mContext.getSystemService(
131 Context.DISPLAY_SERVICE);
132 mDisplayManager.registerDisplayListener(mDisplayListener, null);
Adrian Roos5b518852018-01-23 17:23:38 +0100133 }
134
135 private void setupDecorations() {
136 mOverlay = LayoutInflater.from(mContext)
137 .inflate(R.layout.rounded_corners, null);
138 ((ViewGroup)mOverlay).addView(new DisplayCutoutView(mContext, true,
139 this::updateWindowVisibilities));
140 mBottomOverlay = LayoutInflater.from(mContext)
141 .inflate(R.layout.rounded_corners, null);
142 ((ViewGroup)mBottomOverlay).addView(new DisplayCutoutView(mContext, false,
143 this::updateWindowVisibilities));
144
145 mOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
146 mOverlay.setAlpha(0);
147
148 mBottomOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
149 mBottomOverlay.setAlpha(0);
150
151 updateViews();
152
153 mWindowManager.addView(mOverlay, getWindowLayoutParams());
154 mWindowManager.addView(mBottomOverlay, getBottomLayoutParams());
155
156 DisplayMetrics metrics = new DisplayMetrics();
157 mWindowManager.getDefaultDisplay().getMetrics(metrics);
158 mDensity = metrics.density;
159
160 Dependency.get(TunerService.class).addTunable(this, SIZE);
161
162 // Watch color inversion and invert the overlay as needed.
163 SecureSetting setting = new SecureSetting(mContext, Dependency.get(Dependency.MAIN_HANDLER),
164 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
165 @Override
166 protected void handleValueChanged(int value, boolean observedChange) {
167 int tint = value != 0 ? Color.WHITE : Color.BLACK;
168 ColorStateList tintList = ColorStateList.valueOf(tint);
169 ((ImageView) mOverlay.findViewById(R.id.left)).setImageTintList(tintList);
170 ((ImageView) mOverlay.findViewById(R.id.right)).setImageTintList(tintList);
171 ((ImageView) mBottomOverlay.findViewById(R.id.left)).setImageTintList(tintList);
172 ((ImageView) mBottomOverlay.findViewById(R.id.right)).setImageTintList(tintList);
173 }
174 };
175 setting.setListening(true);
176 setting.onChange(false);
177
178 mOverlay.addOnLayoutChangeListener(new OnLayoutChangeListener() {
179 @Override
180 public void onLayoutChange(View v, int left, int top, int right, int bottom,
181 int oldLeft,
182 int oldTop, int oldRight, int oldBottom) {
183 mOverlay.removeOnLayoutChangeListener(this);
184 mOverlay.animate()
185 .alpha(1)
186 .setDuration(1000)
187 .start();
188 mBottomOverlay.animate()
189 .alpha(1)
190 .setDuration(1000)
191 .start();
192 }
193 });
194 }
195
196 @Override
197 protected void onConfigurationChanged(Configuration newConfig) {
Beverlye91f0d02018-05-15 14:40:47 -0400198 updateOrientation();
Beverly5c2b48a2018-05-24 09:25:45 -0400199 if (shouldDrawCutout() && mOverlay == null) {
200 setupDecorations();
201 }
Beverlye91f0d02018-05-15 14:40:47 -0400202 }
203
204 protected void updateOrientation() {
205 int newRotation = RotationUtils.getExactRotation(mContext);
206 if (newRotation != mRotation) {
207 mRotation = newRotation;
Adrian Roos5b518852018-01-23 17:23:38 +0100208
209 if (mOverlay != null) {
210 updateLayoutParams();
211 updateViews();
212 }
Adrian Roos5b518852018-01-23 17:23:38 +0100213 }
214 }
215
216 private void updateViews() {
217 View topLeft = mOverlay.findViewById(R.id.left);
218 View topRight = mOverlay.findViewById(R.id.right);
219 View bottomLeft = mBottomOverlay.findViewById(R.id.left);
220 View bottomRight = mBottomOverlay.findViewById(R.id.right);
Beverlye91f0d02018-05-15 14:40:47 -0400221
222 if (mRotation == RotationUtils.ROTATION_NONE) {
223 updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
224 updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
225 updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
226 updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
227 } else if (mRotation == RotationUtils.ROTATION_LANDSCAPE) {
228 updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
229 updateView(topRight, Gravity.BOTTOM | Gravity.LEFT, 270);
230 updateView(bottomLeft, Gravity.TOP | Gravity.RIGHT, 90);;
231 updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
232 } else if (mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
233 updateView(topLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
234 updateView(topRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
235 updateView(bottomLeft, Gravity.TOP | Gravity.LEFT, 0);
236 updateView(bottomRight, Gravity.TOP | Gravity.RIGHT, 90);
237 } else if (mRotation == RotationUtils.ROTATION_SEASCAPE) {
238 updateView(topLeft, Gravity.BOTTOM | Gravity.RIGHT, 180);
239 updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
240 updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
241 updateView(bottomRight, Gravity.TOP | Gravity.LEFT, 0);
Adrian Roos5b518852018-01-23 17:23:38 +0100242 }
Adrian Roos5b518852018-01-23 17:23:38 +0100243
244 updateWindowVisibilities();
245 }
246
247 private void updateView(View v, int gravity, int rotation) {
248 ((FrameLayout.LayoutParams)v.getLayoutParams()).gravity = gravity;
249 v.setRotation(rotation);
250 }
251
252 private void updateWindowVisibilities() {
253 updateWindowVisibility(mOverlay);
254 updateWindowVisibility(mBottomOverlay);
255 }
256
257 private void updateWindowVisibility(View overlay) {
258 boolean visibleForCutout = shouldDrawCutout()
259 && overlay.findViewById(R.id.display_cutout).getVisibility() == View.VISIBLE;
Beverlya5f7a302018-04-25 09:19:05 -0400260 boolean visibleForRoundedCorners = hasRoundedCorners();
Adrian Roos5b518852018-01-23 17:23:38 +0100261 overlay.setVisibility(visibleForCutout || visibleForRoundedCorners
262 ? View.VISIBLE : View.GONE);
263 }
264
Beverlya5f7a302018-04-25 09:19:05 -0400265 private boolean hasRoundedCorners() {
266 return mRoundedDefault > 0 || mRoundedDefaultBottom > 0 || mRoundedDefaultTop > 0;
267 }
268
Adrian Roos5b518852018-01-23 17:23:38 +0100269 private boolean shouldDrawCutout() {
Adrian Roosc41b32b2018-04-12 10:13:48 +0200270 return shouldDrawCutout(mContext);
271 }
272
273 static boolean shouldDrawCutout(Context context) {
274 return context.getResources().getBoolean(
Adrian Roos5b518852018-01-23 17:23:38 +0100275 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout);
276 }
277
278 private void setupPadding(int padding) {
279 // Add some padding to all the content near the edge of the screen.
280 StatusBar sb = getComponent(StatusBar.class);
281 View statusBar = (sb != null ? sb.getStatusBarWindow() : null);
282 if (statusBar != null) {
283 TunablePadding.addTunablePadding(statusBar.findViewById(R.id.keyguard_header), PADDING,
284 padding, FLAG_END);
285
286 FragmentHostManager fragmentHostManager = FragmentHostManager.get(statusBar);
287 fragmentHostManager.addTagListener(CollapsedStatusBarFragment.TAG,
288 new TunablePaddingTagListener(padding, R.id.status_bar));
289 fragmentHostManager.addTagListener(QS.TAG,
290 new TunablePaddingTagListener(padding, R.id.header));
291 }
292 }
293
294 @VisibleForTesting
295 WindowManager.LayoutParams getWindowLayoutParams() {
296 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
297 ViewGroup.LayoutParams.MATCH_PARENT,
298 LayoutParams.WRAP_CONTENT,
299 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
Evan Lairdb0506ca2018-03-15 10:34:31 -0400300 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
Adrian Roos5b518852018-01-23 17:23:38 +0100301 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
302 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
303 | WindowManager.LayoutParams.FLAG_SLIPPERY
304 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
305 PixelFormat.TRANSLUCENT);
Robert Carr772e8bc2018-03-14 11:51:23 -0700306 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS
307 | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
308
Adrian Roos56d1a2c2018-03-08 23:22:19 +0100309 if (!DEBUG_SCREENSHOT_ROUNDED_CORNERS) {
310 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY;
311 }
Robert Carr772e8bc2018-03-14 11:51:23 -0700312
Adrian Roos5b518852018-01-23 17:23:38 +0100313 lp.setTitle("ScreenDecorOverlay");
Beverlye91f0d02018-05-15 14:40:47 -0400314 if (mRotation == RotationUtils.ROTATION_SEASCAPE
315 || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
316 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
317 } else {
318 lp.gravity = Gravity.TOP | Gravity.LEFT;
319 }
Adrian Roos5b518852018-01-23 17:23:38 +0100320 lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Beverlye91f0d02018-05-15 14:40:47 -0400321 if (isLandscape(mRotation)) {
Adrian Roos5b518852018-01-23 17:23:38 +0100322 lp.width = WRAP_CONTENT;
323 lp.height = MATCH_PARENT;
324 }
325 return lp;
326 }
327
328 private WindowManager.LayoutParams getBottomLayoutParams() {
329 WindowManager.LayoutParams lp = getWindowLayoutParams();
330 lp.setTitle("ScreenDecorOverlayBottom");
Beverlye91f0d02018-05-15 14:40:47 -0400331 if (mRotation == RotationUtils.ROTATION_SEASCAPE
332 || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
333 lp.gravity = Gravity.TOP | Gravity.LEFT;
334 } else {
335 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
336 }
Adrian Roos5b518852018-01-23 17:23:38 +0100337 return lp;
338 }
339
340 private void updateLayoutParams() {
341 mWindowManager.updateViewLayout(mOverlay, getWindowLayoutParams());
342 mWindowManager.updateViewLayout(mBottomOverlay, getBottomLayoutParams());
343 }
344
345 @Override
346 public void onTuningChanged(String key, String newValue) {
347 if (mOverlay == null) return;
348 if (SIZE.equals(key)) {
349 int size = mRoundedDefault;
Beverlya5f7a302018-04-25 09:19:05 -0400350 int sizeTop = mRoundedDefaultTop;
351 int sizeBottom = mRoundedDefaultBottom;
352 if (newValue != null) {
353 try {
354 size = (int) (Integer.parseInt(newValue) * mDensity);
355 } catch (Exception e) {
356 }
Adrian Roos5b518852018-01-23 17:23:38 +0100357 }
Beverlya5f7a302018-04-25 09:19:05 -0400358
359 if (sizeTop == 0) {
360 sizeTop = size;
361 }
362 if (sizeBottom == 0) {
363 sizeBottom = size;
364 }
365
366 setSize(mOverlay.findViewById(R.id.left), sizeTop);
367 setSize(mOverlay.findViewById(R.id.right), sizeTop);
368 setSize(mBottomOverlay.findViewById(R.id.left), sizeBottom);
369 setSize(mBottomOverlay.findViewById(R.id.right), sizeBottom);
Adrian Roos5b518852018-01-23 17:23:38 +0100370 }
371 }
372
373 private void setSize(View view, int pixelSize) {
374 LayoutParams params = view.getLayoutParams();
375 params.width = pixelSize;
376 params.height = pixelSize;
377 view.setLayoutParams(params);
378 }
379
380 @VisibleForTesting
381 static class TunablePaddingTagListener implements FragmentListener {
382
383 private final int mPadding;
384 private final int mId;
385 private TunablePadding mTunablePadding;
386
387 public TunablePaddingTagListener(int padding, int id) {
388 mPadding = padding;
389 mId = id;
390 }
391
392 @Override
393 public void onFragmentViewCreated(String tag, Fragment fragment) {
394 if (mTunablePadding != null) {
395 mTunablePadding.destroy();
396 }
397 View view = fragment.getView();
398 if (mId != 0) {
399 view = view.findViewById(mId);
400 }
401 mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding,
402 FLAG_START | FLAG_END);
403 }
404 }
405
Evan Lairdb0506ca2018-03-15 10:34:31 -0400406 public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener,
407 RegionInterceptableView {
Adrian Roos5b518852018-01-23 17:23:38 +0100408
409 private final DisplayInfo mInfo = new DisplayInfo();
410 private final Paint mPaint = new Paint();
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100411 private final Region mBounds = new Region();
Adrian Roos5b518852018-01-23 17:23:38 +0100412 private final Rect mBoundingRect = new Rect();
413 private final Path mBoundingPath = new Path();
414 private final int[] mLocation = new int[2];
415 private final boolean mStart;
416 private final Runnable mVisibilityChangedListener;
417
418 public DisplayCutoutView(Context context, boolean start,
419 Runnable visibilityChangedListener) {
420 super(context);
421 mStart = start;
422 mVisibilityChangedListener = visibilityChangedListener;
423 setId(R.id.display_cutout);
424 }
425
426 @Override
427 protected void onAttachedToWindow() {
428 super.onAttachedToWindow();
429 mContext.getSystemService(DisplayManager.class).registerDisplayListener(this,
430 getHandler());
431 update();
432 }
433
434 @Override
435 protected void onDetachedFromWindow() {
436 super.onDetachedFromWindow();
437 mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this);
438 }
439
440 @Override
441 protected void onDraw(Canvas canvas) {
442 super.onDraw(canvas);
443 getLocationOnScreen(mLocation);
444 canvas.translate(-mLocation[0], -mLocation[1]);
445 if (!mBoundingPath.isEmpty()) {
446 mPaint.setColor(Color.BLACK);
447 mPaint.setStyle(Paint.Style.FILL);
Adrian Roos51072a82018-04-10 15:17:08 -0700448 mPaint.setAntiAlias(true);
Adrian Roos5b518852018-01-23 17:23:38 +0100449 canvas.drawPath(mBoundingPath, mPaint);
450 }
451 }
452
453 @Override
454 public void onDisplayAdded(int displayId) {
455 }
456
457 @Override
458 public void onDisplayRemoved(int displayId) {
459 }
460
461 @Override
462 public void onDisplayChanged(int displayId) {
463 if (displayId == getDisplay().getDisplayId()) {
464 update();
465 }
466 }
467
468 private void update() {
469 requestLayout();
470 getDisplay().getDisplayInfo(mInfo);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100471 mBounds.setEmpty();
Adrian Roos5b518852018-01-23 17:23:38 +0100472 mBoundingRect.setEmpty();
473 mBoundingPath.reset();
474 int newVisible;
Adrian Roosc41b32b2018-04-12 10:13:48 +0200475 if (shouldDrawCutout(getContext()) && hasCutout()) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100476 mBounds.set(mInfo.displayCutout.getBounds());
477 localBounds(mBoundingRect);
Adrian Roos51072a82018-04-10 15:17:08 -0700478 updateBoundingPath();
Adrian Roos1b028282018-03-14 14:43:03 +0100479 invalidate();
Adrian Roos5b518852018-01-23 17:23:38 +0100480 newVisible = VISIBLE;
481 } else {
482 newVisible = GONE;
483 }
484 if (newVisible != getVisibility()) {
485 setVisibility(newVisible);
486 mVisibilityChangedListener.run();
487 }
488 }
489
Adrian Roos51072a82018-04-10 15:17:08 -0700490 private void updateBoundingPath() {
491 int lw = mInfo.logicalWidth;
492 int lh = mInfo.logicalHeight;
493
494 boolean flipped = mInfo.rotation == ROTATION_90 || mInfo.rotation == ROTATION_270;
495
496 int dw = flipped ? lh : lw;
497 int dh = flipped ? lw : lh;
498
Adrian Roosa99f5d62018-04-16 16:03:04 +0200499 mBoundingPath.set(DisplayCutout.pathFromResources(getResources(), dw, dh));
Adrian Roos51072a82018-04-10 15:17:08 -0700500 Matrix m = new Matrix();
501 transformPhysicalToLogicalCoordinates(mInfo.rotation, dw, dh, m);
502 mBoundingPath.transform(m);
503 }
504
505 private static void transformPhysicalToLogicalCoordinates(@Surface.Rotation int rotation,
506 @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) {
507 switch (rotation) {
508 case ROTATION_0:
509 out.reset();
510 break;
511 case ROTATION_90:
512 out.setRotate(270);
513 out.postTranslate(0, physicalWidth);
514 break;
515 case ROTATION_180:
516 out.setRotate(180);
517 out.postTranslate(physicalWidth, physicalHeight);
518 break;
519 case ROTATION_270:
520 out.setRotate(90);
521 out.postTranslate(physicalHeight, 0);
522 break;
523 default:
524 throw new IllegalArgumentException("Unknown rotation: " + rotation);
525 }
526 }
527
Adrian Roos5b518852018-01-23 17:23:38 +0100528 private boolean hasCutout() {
Adrian Roos24264212018-02-19 16:26:15 +0100529 final DisplayCutout displayCutout = mInfo.displayCutout;
530 if (displayCutout == null) {
Adrian Roos5b518852018-01-23 17:23:38 +0100531 return false;
532 }
Adrian Roos5b518852018-01-23 17:23:38 +0100533 if (mStart) {
534 return displayCutout.getSafeInsetLeft() > 0
535 || displayCutout.getSafeInsetTop() > 0;
536 } else {
537 return displayCutout.getSafeInsetRight() > 0
538 || displayCutout.getSafeInsetBottom() > 0;
539 }
540 }
541
542 @Override
543 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100544 if (mBounds.isEmpty()) {
Adrian Roos5b518852018-01-23 17:23:38 +0100545 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
546 return;
547 }
548 setMeasuredDimension(
549 resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0),
550 resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0));
551 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100552
553 public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, Rect out) {
554 Region bounds = displayCutout.getBounds();
555 switch (gravity) {
556 case Gravity.TOP:
557 bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(),
558 Region.Op.INTERSECT);
559 out.set(bounds.getBounds());
560 break;
561 case Gravity.LEFT:
562 bounds.op(0, 0, displayCutout.getSafeInsetLeft(), Integer.MAX_VALUE,
563 Region.Op.INTERSECT);
564 out.set(bounds.getBounds());
565 break;
566 case Gravity.BOTTOM:
567 bounds.op(0, displayCutout.getSafeInsetTop() + 1, Integer.MAX_VALUE,
568 Integer.MAX_VALUE, Region.Op.INTERSECT);
569 out.set(bounds.getBounds());
570 break;
571 case Gravity.RIGHT:
572 bounds.op(displayCutout.getSafeInsetLeft() + 1, 0, Integer.MAX_VALUE,
573 Integer.MAX_VALUE, Region.Op.INTERSECT);
574 out.set(bounds.getBounds());
575 break;
576 }
577 bounds.recycle();
578 }
579
580 private void localBounds(Rect out) {
581 final DisplayCutout displayCutout = mInfo.displayCutout;
582
583 if (mStart) {
584 if (displayCutout.getSafeInsetLeft() > 0) {
585 boundsFromDirection(displayCutout, Gravity.LEFT, out);
586 } else if (displayCutout.getSafeInsetTop() > 0) {
587 boundsFromDirection(displayCutout, Gravity.TOP, out);
588 }
589 } else {
590 if (displayCutout.getSafeInsetRight() > 0) {
591 boundsFromDirection(displayCutout, Gravity.RIGHT, out);
592 } else if (displayCutout.getSafeInsetBottom() > 0) {
593 boundsFromDirection(displayCutout, Gravity.BOTTOM, out);
594 }
595 }
596 }
Evan Lairdb0506ca2018-03-15 10:34:31 -0400597
598 @Override
599 public boolean shouldInterceptTouch() {
600 return mInfo.displayCutout != null && getVisibility() == VISIBLE;
601 }
602
603 @Override
604 public Region getInterceptRegion() {
605 if (mInfo.displayCutout == null) {
606 return null;
607 }
608
Adrian Roos134e1cb2018-05-16 17:04:29 +0200609 View rootView = getRootView();
610 Region cutoutBounds = mInfo.displayCutout.getBounds();
611
612 // Transform to window's coordinate space
613 rootView.getLocationOnScreen(mLocation);
614 cutoutBounds.translate(-mLocation[0], -mLocation[1]);
615
616 // Intersect with window's frame
617 cutoutBounds.op(rootView.getLeft(), rootView.getTop(), rootView.getRight(),
618 rootView.getBottom(), Region.Op.INTERSECT);
619
620 return cutoutBounds;
Evan Lairdb0506ca2018-03-15 10:34:31 -0400621 }
Adrian Roos5b518852018-01-23 17:23:38 +0100622 }
Beverlye91f0d02018-05-15 14:40:47 -0400623
624 private boolean isLandscape(int rotation) {
625 return rotation == RotationUtils.ROTATION_LANDSCAPE || rotation ==
626 RotationUtils.ROTATION_SEASCAPE;
627 }
Adrian Roos5b518852018-01-23 17:23:38 +0100628}