blob: e54b083ecdf43fbd7e68367e01f9b7efa57bd189 [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
25import static com.android.systemui.tuner.TunablePadding.FLAG_START;
26import static com.android.systemui.tuner.TunablePadding.FLAG_END;
27
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;
69
70/**
71 * An overlay that draws screen decorations in software (e.g for rounded corners or display cutout)
72 * for antialiasing and emulation purposes.
73 */
74public class ScreenDecorations extends SystemUI implements Tunable {
75 public static final String SIZE = "sysui_rounded_size";
76 public static final String PADDING = "sysui_rounded_content_padding";
Adrian Roos56d1a2c2018-03-08 23:22:19 +010077 private static final boolean DEBUG_SCREENSHOT_ROUNDED_CORNERS =
78 SystemProperties.getBoolean("debug.screenshot_rounded_corners", false);
Adrian Roos5b518852018-01-23 17:23:38 +010079
80 private int mRoundedDefault;
81 private View mOverlay;
82 private View mBottomOverlay;
83 private float mDensity;
84 private WindowManager mWindowManager;
85 private boolean mLandscape;
86
87 @Override
88 public void start() {
89 mWindowManager = mContext.getSystemService(WindowManager.class);
90 mRoundedDefault = mContext.getResources().getDimensionPixelSize(
91 R.dimen.rounded_corner_radius);
92 if (mRoundedDefault != 0 || shouldDrawCutout()) {
93 setupDecorations();
94 }
95 int padding = mContext.getResources().getDimensionPixelSize(
96 R.dimen.rounded_corner_content_padding);
97 if (padding != 0) {
98 setupPadding(padding);
99 }
100 }
101
102 private void setupDecorations() {
103 mOverlay = LayoutInflater.from(mContext)
104 .inflate(R.layout.rounded_corners, null);
105 ((ViewGroup)mOverlay).addView(new DisplayCutoutView(mContext, true,
106 this::updateWindowVisibilities));
107 mBottomOverlay = LayoutInflater.from(mContext)
108 .inflate(R.layout.rounded_corners, null);
109 ((ViewGroup)mBottomOverlay).addView(new DisplayCutoutView(mContext, false,
110 this::updateWindowVisibilities));
111
112 mOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
113 mOverlay.setAlpha(0);
114
115 mBottomOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
116 mBottomOverlay.setAlpha(0);
117
118 updateViews();
119
120 mWindowManager.addView(mOverlay, getWindowLayoutParams());
121 mWindowManager.addView(mBottomOverlay, getBottomLayoutParams());
122
123 DisplayMetrics metrics = new DisplayMetrics();
124 mWindowManager.getDefaultDisplay().getMetrics(metrics);
125 mDensity = metrics.density;
126
127 Dependency.get(TunerService.class).addTunable(this, SIZE);
128
129 // Watch color inversion and invert the overlay as needed.
130 SecureSetting setting = new SecureSetting(mContext, Dependency.get(Dependency.MAIN_HANDLER),
131 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
132 @Override
133 protected void handleValueChanged(int value, boolean observedChange) {
134 int tint = value != 0 ? Color.WHITE : Color.BLACK;
135 ColorStateList tintList = ColorStateList.valueOf(tint);
136 ((ImageView) mOverlay.findViewById(R.id.left)).setImageTintList(tintList);
137 ((ImageView) mOverlay.findViewById(R.id.right)).setImageTintList(tintList);
138 ((ImageView) mBottomOverlay.findViewById(R.id.left)).setImageTintList(tintList);
139 ((ImageView) mBottomOverlay.findViewById(R.id.right)).setImageTintList(tintList);
140 }
141 };
142 setting.setListening(true);
143 setting.onChange(false);
144
145 mOverlay.addOnLayoutChangeListener(new OnLayoutChangeListener() {
146 @Override
147 public void onLayoutChange(View v, int left, int top, int right, int bottom,
148 int oldLeft,
149 int oldTop, int oldRight, int oldBottom) {
150 mOverlay.removeOnLayoutChangeListener(this);
151 mOverlay.animate()
152 .alpha(1)
153 .setDuration(1000)
154 .start();
155 mBottomOverlay.animate()
156 .alpha(1)
157 .setDuration(1000)
158 .start();
159 }
160 });
161 }
162
163 @Override
164 protected void onConfigurationChanged(Configuration newConfig) {
165 boolean newLanscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE;
166 if (newLanscape != mLandscape) {
167 mLandscape = newLanscape;
168
169 if (mOverlay != null) {
170 updateLayoutParams();
171 updateViews();
172 }
173 }
174 if (shouldDrawCutout() && mOverlay == null) {
175 setupDecorations();
176 }
177 }
178
179 private void updateViews() {
180 View topLeft = mOverlay.findViewById(R.id.left);
181 View topRight = mOverlay.findViewById(R.id.right);
182 View bottomLeft = mBottomOverlay.findViewById(R.id.left);
183 View bottomRight = mBottomOverlay.findViewById(R.id.right);
184 if (mLandscape) {
185 // Flip corners
186 View tmp = topRight;
187 topRight = bottomLeft;
188 bottomLeft = tmp;
189 }
190 updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
191 updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
192 updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
193 updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
194
195 updateWindowVisibilities();
196 }
197
198 private void updateView(View v, int gravity, int rotation) {
199 ((FrameLayout.LayoutParams)v.getLayoutParams()).gravity = gravity;
200 v.setRotation(rotation);
201 }
202
203 private void updateWindowVisibilities() {
204 updateWindowVisibility(mOverlay);
205 updateWindowVisibility(mBottomOverlay);
206 }
207
208 private void updateWindowVisibility(View overlay) {
209 boolean visibleForCutout = shouldDrawCutout()
210 && overlay.findViewById(R.id.display_cutout).getVisibility() == View.VISIBLE;
211 boolean visibleForRoundedCorners = mRoundedDefault > 0;
212 overlay.setVisibility(visibleForCutout || visibleForRoundedCorners
213 ? View.VISIBLE : View.GONE);
214 }
215
216 private boolean shouldDrawCutout() {
217 return mContext.getResources().getBoolean(
218 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout);
219 }
220
221 private void setupPadding(int padding) {
222 // Add some padding to all the content near the edge of the screen.
223 StatusBar sb = getComponent(StatusBar.class);
224 View statusBar = (sb != null ? sb.getStatusBarWindow() : null);
225 if (statusBar != null) {
226 TunablePadding.addTunablePadding(statusBar.findViewById(R.id.keyguard_header), PADDING,
227 padding, FLAG_END);
228
229 FragmentHostManager fragmentHostManager = FragmentHostManager.get(statusBar);
230 fragmentHostManager.addTagListener(CollapsedStatusBarFragment.TAG,
231 new TunablePaddingTagListener(padding, R.id.status_bar));
232 fragmentHostManager.addTagListener(QS.TAG,
233 new TunablePaddingTagListener(padding, R.id.header));
234 }
235 }
236
237 @VisibleForTesting
238 WindowManager.LayoutParams getWindowLayoutParams() {
239 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
240 ViewGroup.LayoutParams.MATCH_PARENT,
241 LayoutParams.WRAP_CONTENT,
242 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
Evan Lairdb0506ca2018-03-15 10:34:31 -0400243 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
Adrian Roos5b518852018-01-23 17:23:38 +0100244 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
245 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
246 | WindowManager.LayoutParams.FLAG_SLIPPERY
247 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
248 PixelFormat.TRANSLUCENT);
Robert Carr772e8bc2018-03-14 11:51:23 -0700249 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS
250 | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
251
Adrian Roos56d1a2c2018-03-08 23:22:19 +0100252 if (!DEBUG_SCREENSHOT_ROUNDED_CORNERS) {
253 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY;
254 }
Robert Carr772e8bc2018-03-14 11:51:23 -0700255
Adrian Roos5b518852018-01-23 17:23:38 +0100256 lp.setTitle("ScreenDecorOverlay");
257 lp.gravity = Gravity.TOP | Gravity.LEFT;
258 lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
259 if (mLandscape) {
260 lp.width = WRAP_CONTENT;
261 lp.height = MATCH_PARENT;
262 }
263 return lp;
264 }
265
266 private WindowManager.LayoutParams getBottomLayoutParams() {
267 WindowManager.LayoutParams lp = getWindowLayoutParams();
268 lp.setTitle("ScreenDecorOverlayBottom");
269 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
270 return lp;
271 }
272
273 private void updateLayoutParams() {
274 mWindowManager.updateViewLayout(mOverlay, getWindowLayoutParams());
275 mWindowManager.updateViewLayout(mBottomOverlay, getBottomLayoutParams());
276 }
277
278 @Override
279 public void onTuningChanged(String key, String newValue) {
280 if (mOverlay == null) return;
281 if (SIZE.equals(key)) {
282 int size = mRoundedDefault;
283 try {
284 size = (int) (Integer.parseInt(newValue) * mDensity);
285 } catch (Exception e) {
286 }
287 setSize(mOverlay.findViewById(R.id.left), size);
288 setSize(mOverlay.findViewById(R.id.right), size);
289 setSize(mBottomOverlay.findViewById(R.id.left), size);
290 setSize(mBottomOverlay.findViewById(R.id.right), size);
291 }
292 }
293
294 private void setSize(View view, int pixelSize) {
295 LayoutParams params = view.getLayoutParams();
296 params.width = pixelSize;
297 params.height = pixelSize;
298 view.setLayoutParams(params);
299 }
300
301 @VisibleForTesting
302 static class TunablePaddingTagListener implements FragmentListener {
303
304 private final int mPadding;
305 private final int mId;
306 private TunablePadding mTunablePadding;
307
308 public TunablePaddingTagListener(int padding, int id) {
309 mPadding = padding;
310 mId = id;
311 }
312
313 @Override
314 public void onFragmentViewCreated(String tag, Fragment fragment) {
315 if (mTunablePadding != null) {
316 mTunablePadding.destroy();
317 }
318 View view = fragment.getView();
319 if (mId != 0) {
320 view = view.findViewById(mId);
321 }
322 mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding,
323 FLAG_START | FLAG_END);
324 }
325 }
326
Evan Lairdb0506ca2018-03-15 10:34:31 -0400327 public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener,
328 RegionInterceptableView {
Adrian Roos5b518852018-01-23 17:23:38 +0100329
330 private final DisplayInfo mInfo = new DisplayInfo();
331 private final Paint mPaint = new Paint();
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100332 private final Region mBounds = new Region();
Adrian Roos5b518852018-01-23 17:23:38 +0100333 private final Rect mBoundingRect = new Rect();
334 private final Path mBoundingPath = new Path();
335 private final int[] mLocation = new int[2];
336 private final boolean mStart;
337 private final Runnable mVisibilityChangedListener;
338
339 public DisplayCutoutView(Context context, boolean start,
340 Runnable visibilityChangedListener) {
341 super(context);
342 mStart = start;
343 mVisibilityChangedListener = visibilityChangedListener;
344 setId(R.id.display_cutout);
345 }
346
347 @Override
348 protected void onAttachedToWindow() {
349 super.onAttachedToWindow();
350 mContext.getSystemService(DisplayManager.class).registerDisplayListener(this,
351 getHandler());
352 update();
353 }
354
355 @Override
356 protected void onDetachedFromWindow() {
357 super.onDetachedFromWindow();
358 mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this);
359 }
360
361 @Override
362 protected void onDraw(Canvas canvas) {
363 super.onDraw(canvas);
364 getLocationOnScreen(mLocation);
365 canvas.translate(-mLocation[0], -mLocation[1]);
366 if (!mBoundingPath.isEmpty()) {
367 mPaint.setColor(Color.BLACK);
368 mPaint.setStyle(Paint.Style.FILL);
Adrian Roos51072a82018-04-10 15:17:08 -0700369 mPaint.setAntiAlias(true);
Adrian Roos5b518852018-01-23 17:23:38 +0100370 canvas.drawPath(mBoundingPath, mPaint);
371 }
372 }
373
374 @Override
375 public void onDisplayAdded(int displayId) {
376 }
377
378 @Override
379 public void onDisplayRemoved(int displayId) {
380 }
381
382 @Override
383 public void onDisplayChanged(int displayId) {
384 if (displayId == getDisplay().getDisplayId()) {
385 update();
386 }
387 }
388
389 private void update() {
390 requestLayout();
391 getDisplay().getDisplayInfo(mInfo);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100392 mBounds.setEmpty();
Adrian Roos5b518852018-01-23 17:23:38 +0100393 mBoundingRect.setEmpty();
394 mBoundingPath.reset();
395 int newVisible;
396 if (hasCutout()) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100397 mBounds.set(mInfo.displayCutout.getBounds());
398 localBounds(mBoundingRect);
Adrian Roos51072a82018-04-10 15:17:08 -0700399 updateBoundingPath();
Adrian Roos1b028282018-03-14 14:43:03 +0100400 invalidate();
Adrian Roos5b518852018-01-23 17:23:38 +0100401 newVisible = VISIBLE;
402 } else {
403 newVisible = GONE;
404 }
405 if (newVisible != getVisibility()) {
406 setVisibility(newVisible);
407 mVisibilityChangedListener.run();
408 }
409 }
410
Adrian Roos51072a82018-04-10 15:17:08 -0700411 private void updateBoundingPath() {
412 int lw = mInfo.logicalWidth;
413 int lh = mInfo.logicalHeight;
414
415 boolean flipped = mInfo.rotation == ROTATION_90 || mInfo.rotation == ROTATION_270;
416
417 int dw = flipped ? lh : lw;
418 int dh = flipped ? lw : lh;
419
420 mBoundingPath.set(DisplayCutout.pathFromResources(getResources(), lw, lh));
421 Matrix m = new Matrix();
422 transformPhysicalToLogicalCoordinates(mInfo.rotation, dw, dh, m);
423 mBoundingPath.transform(m);
424 }
425
426 private static void transformPhysicalToLogicalCoordinates(@Surface.Rotation int rotation,
427 @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) {
428 switch (rotation) {
429 case ROTATION_0:
430 out.reset();
431 break;
432 case ROTATION_90:
433 out.setRotate(270);
434 out.postTranslate(0, physicalWidth);
435 break;
436 case ROTATION_180:
437 out.setRotate(180);
438 out.postTranslate(physicalWidth, physicalHeight);
439 break;
440 case ROTATION_270:
441 out.setRotate(90);
442 out.postTranslate(physicalHeight, 0);
443 break;
444 default:
445 throw new IllegalArgumentException("Unknown rotation: " + rotation);
446 }
447 }
448
Adrian Roos5b518852018-01-23 17:23:38 +0100449 private boolean hasCutout() {
Adrian Roos24264212018-02-19 16:26:15 +0100450 final DisplayCutout displayCutout = mInfo.displayCutout;
451 if (displayCutout == null) {
Adrian Roos5b518852018-01-23 17:23:38 +0100452 return false;
453 }
Adrian Roos5b518852018-01-23 17:23:38 +0100454 if (mStart) {
455 return displayCutout.getSafeInsetLeft() > 0
456 || displayCutout.getSafeInsetTop() > 0;
457 } else {
458 return displayCutout.getSafeInsetRight() > 0
459 || displayCutout.getSafeInsetBottom() > 0;
460 }
461 }
462
463 @Override
464 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100465 if (mBounds.isEmpty()) {
Adrian Roos5b518852018-01-23 17:23:38 +0100466 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
467 return;
468 }
469 setMeasuredDimension(
470 resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0),
471 resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0));
472 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100473
474 public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, Rect out) {
475 Region bounds = displayCutout.getBounds();
476 switch (gravity) {
477 case Gravity.TOP:
478 bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(),
479 Region.Op.INTERSECT);
480 out.set(bounds.getBounds());
481 break;
482 case Gravity.LEFT:
483 bounds.op(0, 0, displayCutout.getSafeInsetLeft(), Integer.MAX_VALUE,
484 Region.Op.INTERSECT);
485 out.set(bounds.getBounds());
486 break;
487 case Gravity.BOTTOM:
488 bounds.op(0, displayCutout.getSafeInsetTop() + 1, Integer.MAX_VALUE,
489 Integer.MAX_VALUE, Region.Op.INTERSECT);
490 out.set(bounds.getBounds());
491 break;
492 case Gravity.RIGHT:
493 bounds.op(displayCutout.getSafeInsetLeft() + 1, 0, Integer.MAX_VALUE,
494 Integer.MAX_VALUE, Region.Op.INTERSECT);
495 out.set(bounds.getBounds());
496 break;
497 }
498 bounds.recycle();
499 }
500
501 private void localBounds(Rect out) {
502 final DisplayCutout displayCutout = mInfo.displayCutout;
503
504 if (mStart) {
505 if (displayCutout.getSafeInsetLeft() > 0) {
506 boundsFromDirection(displayCutout, Gravity.LEFT, out);
507 } else if (displayCutout.getSafeInsetTop() > 0) {
508 boundsFromDirection(displayCutout, Gravity.TOP, out);
509 }
510 } else {
511 if (displayCutout.getSafeInsetRight() > 0) {
512 boundsFromDirection(displayCutout, Gravity.RIGHT, out);
513 } else if (displayCutout.getSafeInsetBottom() > 0) {
514 boundsFromDirection(displayCutout, Gravity.BOTTOM, out);
515 }
516 }
517 }
Evan Lairdb0506ca2018-03-15 10:34:31 -0400518
519 @Override
520 public boolean shouldInterceptTouch() {
521 return mInfo.displayCutout != null && getVisibility() == VISIBLE;
522 }
523
524 @Override
525 public Region getInterceptRegion() {
526 if (mInfo.displayCutout == null) {
527 return null;
528 }
529
530 return mInfo.displayCutout.getBounds();
531 }
Adrian Roos5b518852018-01-23 17:23:38 +0100532 }
533}