blob: 2746a71e331683ab1f753425f8a3b43bf97b5434 [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();
199 }
200
201 protected void updateOrientation() {
202 int newRotation = RotationUtils.getExactRotation(mContext);
203 if (newRotation != mRotation) {
204 mRotation = newRotation;
Adrian Roos5b518852018-01-23 17:23:38 +0100205
206 if (mOverlay != null) {
207 updateLayoutParams();
208 updateViews();
209 }
Beverlye91f0d02018-05-15 14:40:47 -0400210
211 if (shouldDrawCutout() && mOverlay == null) {
212 setupDecorations();
213 }
Adrian Roos5b518852018-01-23 17:23:38 +0100214 }
215 }
216
217 private void updateViews() {
218 View topLeft = mOverlay.findViewById(R.id.left);
219 View topRight = mOverlay.findViewById(R.id.right);
220 View bottomLeft = mBottomOverlay.findViewById(R.id.left);
221 View bottomRight = mBottomOverlay.findViewById(R.id.right);
Beverlye91f0d02018-05-15 14:40:47 -0400222
223 if (mRotation == RotationUtils.ROTATION_NONE) {
224 updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
225 updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
226 updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
227 updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
228 } else if (mRotation == RotationUtils.ROTATION_LANDSCAPE) {
229 updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
230 updateView(topRight, Gravity.BOTTOM | Gravity.LEFT, 270);
231 updateView(bottomLeft, Gravity.TOP | Gravity.RIGHT, 90);;
232 updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
233 } else if (mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
234 updateView(topLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
235 updateView(topRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
236 updateView(bottomLeft, Gravity.TOP | Gravity.LEFT, 0);
237 updateView(bottomRight, Gravity.TOP | Gravity.RIGHT, 90);
238 } else if (mRotation == RotationUtils.ROTATION_SEASCAPE) {
239 updateView(topLeft, Gravity.BOTTOM | Gravity.RIGHT, 180);
240 updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
241 updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
242 updateView(bottomRight, Gravity.TOP | Gravity.LEFT, 0);
Adrian Roos5b518852018-01-23 17:23:38 +0100243 }
Adrian Roos5b518852018-01-23 17:23:38 +0100244
245 updateWindowVisibilities();
246 }
247
248 private void updateView(View v, int gravity, int rotation) {
249 ((FrameLayout.LayoutParams)v.getLayoutParams()).gravity = gravity;
250 v.setRotation(rotation);
251 }
252
253 private void updateWindowVisibilities() {
254 updateWindowVisibility(mOverlay);
255 updateWindowVisibility(mBottomOverlay);
256 }
257
258 private void updateWindowVisibility(View overlay) {
259 boolean visibleForCutout = shouldDrawCutout()
260 && overlay.findViewById(R.id.display_cutout).getVisibility() == View.VISIBLE;
Beverlya5f7a302018-04-25 09:19:05 -0400261 boolean visibleForRoundedCorners = hasRoundedCorners();
Adrian Roos5b518852018-01-23 17:23:38 +0100262 overlay.setVisibility(visibleForCutout || visibleForRoundedCorners
263 ? View.VISIBLE : View.GONE);
264 }
265
Beverlya5f7a302018-04-25 09:19:05 -0400266 private boolean hasRoundedCorners() {
267 return mRoundedDefault > 0 || mRoundedDefaultBottom > 0 || mRoundedDefaultTop > 0;
268 }
269
Adrian Roos5b518852018-01-23 17:23:38 +0100270 private boolean shouldDrawCutout() {
Adrian Roosc41b32b2018-04-12 10:13:48 +0200271 return shouldDrawCutout(mContext);
272 }
273
274 static boolean shouldDrawCutout(Context context) {
275 return context.getResources().getBoolean(
Adrian Roos5b518852018-01-23 17:23:38 +0100276 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout);
277 }
278
279 private void setupPadding(int padding) {
280 // Add some padding to all the content near the edge of the screen.
281 StatusBar sb = getComponent(StatusBar.class);
282 View statusBar = (sb != null ? sb.getStatusBarWindow() : null);
283 if (statusBar != null) {
284 TunablePadding.addTunablePadding(statusBar.findViewById(R.id.keyguard_header), PADDING,
285 padding, FLAG_END);
286
287 FragmentHostManager fragmentHostManager = FragmentHostManager.get(statusBar);
288 fragmentHostManager.addTagListener(CollapsedStatusBarFragment.TAG,
289 new TunablePaddingTagListener(padding, R.id.status_bar));
290 fragmentHostManager.addTagListener(QS.TAG,
291 new TunablePaddingTagListener(padding, R.id.header));
292 }
293 }
294
295 @VisibleForTesting
296 WindowManager.LayoutParams getWindowLayoutParams() {
297 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
298 ViewGroup.LayoutParams.MATCH_PARENT,
299 LayoutParams.WRAP_CONTENT,
300 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
Evan Lairdb0506ca2018-03-15 10:34:31 -0400301 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
Adrian Roos5b518852018-01-23 17:23:38 +0100302 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
303 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
304 | WindowManager.LayoutParams.FLAG_SLIPPERY
305 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
306 PixelFormat.TRANSLUCENT);
Robert Carr772e8bc2018-03-14 11:51:23 -0700307 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS
308 | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
309
Adrian Roos56d1a2c2018-03-08 23:22:19 +0100310 if (!DEBUG_SCREENSHOT_ROUNDED_CORNERS) {
311 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY;
312 }
Robert Carr772e8bc2018-03-14 11:51:23 -0700313
Adrian Roos5b518852018-01-23 17:23:38 +0100314 lp.setTitle("ScreenDecorOverlay");
Beverlye91f0d02018-05-15 14:40:47 -0400315 if (mRotation == RotationUtils.ROTATION_SEASCAPE
316 || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
317 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
318 } else {
319 lp.gravity = Gravity.TOP | Gravity.LEFT;
320 }
Adrian Roos5b518852018-01-23 17:23:38 +0100321 lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Beverlye91f0d02018-05-15 14:40:47 -0400322 if (isLandscape(mRotation)) {
Adrian Roos5b518852018-01-23 17:23:38 +0100323 lp.width = WRAP_CONTENT;
324 lp.height = MATCH_PARENT;
325 }
326 return lp;
327 }
328
329 private WindowManager.LayoutParams getBottomLayoutParams() {
330 WindowManager.LayoutParams lp = getWindowLayoutParams();
331 lp.setTitle("ScreenDecorOverlayBottom");
Beverlye91f0d02018-05-15 14:40:47 -0400332 if (mRotation == RotationUtils.ROTATION_SEASCAPE
333 || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
334 lp.gravity = Gravity.TOP | Gravity.LEFT;
335 } else {
336 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
337 }
Adrian Roos5b518852018-01-23 17:23:38 +0100338 return lp;
339 }
340
341 private void updateLayoutParams() {
342 mWindowManager.updateViewLayout(mOverlay, getWindowLayoutParams());
343 mWindowManager.updateViewLayout(mBottomOverlay, getBottomLayoutParams());
344 }
345
346 @Override
347 public void onTuningChanged(String key, String newValue) {
348 if (mOverlay == null) return;
349 if (SIZE.equals(key)) {
350 int size = mRoundedDefault;
Beverlya5f7a302018-04-25 09:19:05 -0400351 int sizeTop = mRoundedDefaultTop;
352 int sizeBottom = mRoundedDefaultBottom;
353 if (newValue != null) {
354 try {
355 size = (int) (Integer.parseInt(newValue) * mDensity);
356 } catch (Exception e) {
357 }
Adrian Roos5b518852018-01-23 17:23:38 +0100358 }
Beverlya5f7a302018-04-25 09:19:05 -0400359
360 if (sizeTop == 0) {
361 sizeTop = size;
362 }
363 if (sizeBottom == 0) {
364 sizeBottom = size;
365 }
366
367 setSize(mOverlay.findViewById(R.id.left), sizeTop);
368 setSize(mOverlay.findViewById(R.id.right), sizeTop);
369 setSize(mBottomOverlay.findViewById(R.id.left), sizeBottom);
370 setSize(mBottomOverlay.findViewById(R.id.right), sizeBottom);
Adrian Roos5b518852018-01-23 17:23:38 +0100371 }
372 }
373
374 private void setSize(View view, int pixelSize) {
375 LayoutParams params = view.getLayoutParams();
376 params.width = pixelSize;
377 params.height = pixelSize;
378 view.setLayoutParams(params);
379 }
380
381 @VisibleForTesting
382 static class TunablePaddingTagListener implements FragmentListener {
383
384 private final int mPadding;
385 private final int mId;
386 private TunablePadding mTunablePadding;
387
388 public TunablePaddingTagListener(int padding, int id) {
389 mPadding = padding;
390 mId = id;
391 }
392
393 @Override
394 public void onFragmentViewCreated(String tag, Fragment fragment) {
395 if (mTunablePadding != null) {
396 mTunablePadding.destroy();
397 }
398 View view = fragment.getView();
399 if (mId != 0) {
400 view = view.findViewById(mId);
401 }
402 mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding,
403 FLAG_START | FLAG_END);
404 }
405 }
406
Evan Lairdb0506ca2018-03-15 10:34:31 -0400407 public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener,
408 RegionInterceptableView {
Adrian Roos5b518852018-01-23 17:23:38 +0100409
410 private final DisplayInfo mInfo = new DisplayInfo();
411 private final Paint mPaint = new Paint();
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100412 private final Region mBounds = new Region();
Adrian Roos5b518852018-01-23 17:23:38 +0100413 private final Rect mBoundingRect = new Rect();
414 private final Path mBoundingPath = new Path();
415 private final int[] mLocation = new int[2];
416 private final boolean mStart;
417 private final Runnable mVisibilityChangedListener;
418
419 public DisplayCutoutView(Context context, boolean start,
420 Runnable visibilityChangedListener) {
421 super(context);
422 mStart = start;
423 mVisibilityChangedListener = visibilityChangedListener;
424 setId(R.id.display_cutout);
425 }
426
427 @Override
428 protected void onAttachedToWindow() {
429 super.onAttachedToWindow();
430 mContext.getSystemService(DisplayManager.class).registerDisplayListener(this,
431 getHandler());
432 update();
433 }
434
435 @Override
436 protected void onDetachedFromWindow() {
437 super.onDetachedFromWindow();
438 mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this);
439 }
440
441 @Override
442 protected void onDraw(Canvas canvas) {
443 super.onDraw(canvas);
444 getLocationOnScreen(mLocation);
445 canvas.translate(-mLocation[0], -mLocation[1]);
446 if (!mBoundingPath.isEmpty()) {
447 mPaint.setColor(Color.BLACK);
448 mPaint.setStyle(Paint.Style.FILL);
Adrian Roos51072a82018-04-10 15:17:08 -0700449 mPaint.setAntiAlias(true);
Adrian Roos5b518852018-01-23 17:23:38 +0100450 canvas.drawPath(mBoundingPath, mPaint);
451 }
452 }
453
454 @Override
455 public void onDisplayAdded(int displayId) {
456 }
457
458 @Override
459 public void onDisplayRemoved(int displayId) {
460 }
461
462 @Override
463 public void onDisplayChanged(int displayId) {
464 if (displayId == getDisplay().getDisplayId()) {
465 update();
466 }
467 }
468
469 private void update() {
470 requestLayout();
471 getDisplay().getDisplayInfo(mInfo);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100472 mBounds.setEmpty();
Adrian Roos5b518852018-01-23 17:23:38 +0100473 mBoundingRect.setEmpty();
474 mBoundingPath.reset();
475 int newVisible;
Adrian Roosc41b32b2018-04-12 10:13:48 +0200476 if (shouldDrawCutout(getContext()) && hasCutout()) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100477 mBounds.set(mInfo.displayCutout.getBounds());
478 localBounds(mBoundingRect);
Adrian Roos51072a82018-04-10 15:17:08 -0700479 updateBoundingPath();
Adrian Roos1b028282018-03-14 14:43:03 +0100480 invalidate();
Adrian Roos5b518852018-01-23 17:23:38 +0100481 newVisible = VISIBLE;
482 } else {
483 newVisible = GONE;
484 }
485 if (newVisible != getVisibility()) {
486 setVisibility(newVisible);
487 mVisibilityChangedListener.run();
488 }
489 }
490
Adrian Roos51072a82018-04-10 15:17:08 -0700491 private void updateBoundingPath() {
492 int lw = mInfo.logicalWidth;
493 int lh = mInfo.logicalHeight;
494
495 boolean flipped = mInfo.rotation == ROTATION_90 || mInfo.rotation == ROTATION_270;
496
497 int dw = flipped ? lh : lw;
498 int dh = flipped ? lw : lh;
499
Adrian Roosa99f5d62018-04-16 16:03:04 +0200500 mBoundingPath.set(DisplayCutout.pathFromResources(getResources(), dw, dh));
Adrian Roos51072a82018-04-10 15:17:08 -0700501 Matrix m = new Matrix();
502 transformPhysicalToLogicalCoordinates(mInfo.rotation, dw, dh, m);
503 mBoundingPath.transform(m);
504 }
505
506 private static void transformPhysicalToLogicalCoordinates(@Surface.Rotation int rotation,
507 @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) {
508 switch (rotation) {
509 case ROTATION_0:
510 out.reset();
511 break;
512 case ROTATION_90:
513 out.setRotate(270);
514 out.postTranslate(0, physicalWidth);
515 break;
516 case ROTATION_180:
517 out.setRotate(180);
518 out.postTranslate(physicalWidth, physicalHeight);
519 break;
520 case ROTATION_270:
521 out.setRotate(90);
522 out.postTranslate(physicalHeight, 0);
523 break;
524 default:
525 throw new IllegalArgumentException("Unknown rotation: " + rotation);
526 }
527 }
528
Adrian Roos5b518852018-01-23 17:23:38 +0100529 private boolean hasCutout() {
Adrian Roos24264212018-02-19 16:26:15 +0100530 final DisplayCutout displayCutout = mInfo.displayCutout;
531 if (displayCutout == null) {
Adrian Roos5b518852018-01-23 17:23:38 +0100532 return false;
533 }
Adrian Roos5b518852018-01-23 17:23:38 +0100534 if (mStart) {
535 return displayCutout.getSafeInsetLeft() > 0
536 || displayCutout.getSafeInsetTop() > 0;
537 } else {
538 return displayCutout.getSafeInsetRight() > 0
539 || displayCutout.getSafeInsetBottom() > 0;
540 }
541 }
542
543 @Override
544 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100545 if (mBounds.isEmpty()) {
Adrian Roos5b518852018-01-23 17:23:38 +0100546 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
547 return;
548 }
549 setMeasuredDimension(
550 resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0),
551 resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0));
552 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100553
554 public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, Rect out) {
555 Region bounds = displayCutout.getBounds();
556 switch (gravity) {
557 case Gravity.TOP:
558 bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(),
559 Region.Op.INTERSECT);
560 out.set(bounds.getBounds());
561 break;
562 case Gravity.LEFT:
563 bounds.op(0, 0, displayCutout.getSafeInsetLeft(), Integer.MAX_VALUE,
564 Region.Op.INTERSECT);
565 out.set(bounds.getBounds());
566 break;
567 case Gravity.BOTTOM:
568 bounds.op(0, displayCutout.getSafeInsetTop() + 1, Integer.MAX_VALUE,
569 Integer.MAX_VALUE, Region.Op.INTERSECT);
570 out.set(bounds.getBounds());
571 break;
572 case Gravity.RIGHT:
573 bounds.op(displayCutout.getSafeInsetLeft() + 1, 0, Integer.MAX_VALUE,
574 Integer.MAX_VALUE, Region.Op.INTERSECT);
575 out.set(bounds.getBounds());
576 break;
577 }
578 bounds.recycle();
579 }
580
581 private void localBounds(Rect out) {
582 final DisplayCutout displayCutout = mInfo.displayCutout;
583
584 if (mStart) {
585 if (displayCutout.getSafeInsetLeft() > 0) {
586 boundsFromDirection(displayCutout, Gravity.LEFT, out);
587 } else if (displayCutout.getSafeInsetTop() > 0) {
588 boundsFromDirection(displayCutout, Gravity.TOP, out);
589 }
590 } else {
591 if (displayCutout.getSafeInsetRight() > 0) {
592 boundsFromDirection(displayCutout, Gravity.RIGHT, out);
593 } else if (displayCutout.getSafeInsetBottom() > 0) {
594 boundsFromDirection(displayCutout, Gravity.BOTTOM, out);
595 }
596 }
597 }
Evan Lairdb0506ca2018-03-15 10:34:31 -0400598
599 @Override
600 public boolean shouldInterceptTouch() {
601 return mInfo.displayCutout != null && getVisibility() == VISIBLE;
602 }
603
604 @Override
605 public Region getInterceptRegion() {
606 if (mInfo.displayCutout == null) {
607 return null;
608 }
609
Adrian Roos134e1cb2018-05-16 17:04:29 +0200610 View rootView = getRootView();
611 Region cutoutBounds = mInfo.displayCutout.getBounds();
612
613 // Transform to window's coordinate space
614 rootView.getLocationOnScreen(mLocation);
615 cutoutBounds.translate(-mLocation[0], -mLocation[1]);
616
617 // Intersect with window's frame
618 cutoutBounds.op(rootView.getLeft(), rootView.getTop(), rootView.getRight(),
619 rootView.getBottom(), Region.Op.INTERSECT);
620
621 return cutoutBounds;
Evan Lairdb0506ca2018-03-15 10:34:31 -0400622 }
Adrian Roos5b518852018-01-23 17:23:38 +0100623 }
Beverlye91f0d02018-05-15 14:40:47 -0400624
625 private boolean isLandscape(int rotation) {
626 return rotation == RotationUtils.ROTATION_LANDSCAPE || rotation ==
627 RotationUtils.ROTATION_SEASCAPE;
628 }
Adrian Roos5b518852018-01-23 17:23:38 +0100629}