blob: e79006137a5e29c6943fe07229cb1ae081ed02f5 [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
17import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
18import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
19import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
20
21import static com.android.systemui.tuner.TunablePadding.FLAG_START;
22import static com.android.systemui.tuner.TunablePadding.FLAG_END;
23
24import android.app.Fragment;
25import android.content.Context;
26import android.content.res.ColorStateList;
27import android.content.res.Configuration;
28import android.graphics.Canvas;
29import android.graphics.Color;
30import android.graphics.Paint;
31import android.graphics.Path;
32import android.graphics.PixelFormat;
33import android.graphics.Rect;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010034import android.graphics.Region;
Adrian Roos5b518852018-01-23 17:23:38 +010035import android.hardware.display.DisplayManager;
Adrian Roos56d1a2c2018-03-08 23:22:19 +010036import android.os.SystemProperties;
Adrian Roos5b518852018-01-23 17:23:38 +010037import android.provider.Settings.Secure;
38import android.support.annotation.VisibleForTesting;
39import android.util.DisplayMetrics;
40import android.view.DisplayCutout;
41import android.view.DisplayInfo;
42import android.view.Gravity;
43import android.view.LayoutInflater;
44import android.view.View;
45import android.view.View.OnLayoutChangeListener;
46import android.view.ViewGroup;
47import android.view.ViewGroup.LayoutParams;
48import android.view.WindowManager;
49import android.widget.FrameLayout;
50import android.widget.ImageView;
51
52import com.android.systemui.fragments.FragmentHostManager;
53import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
54import com.android.systemui.plugins.qs.QS;
55import com.android.systemui.qs.SecureSetting;
56import com.android.systemui.statusbar.phone.CollapsedStatusBarFragment;
57import com.android.systemui.statusbar.phone.StatusBar;
58import com.android.systemui.tuner.TunablePadding;
59import com.android.systemui.tuner.TunerService;
60import com.android.systemui.tuner.TunerService.Tunable;
61
62/**
63 * An overlay that draws screen decorations in software (e.g for rounded corners or display cutout)
64 * for antialiasing and emulation purposes.
65 */
66public class ScreenDecorations extends SystemUI implements Tunable {
67 public static final String SIZE = "sysui_rounded_size";
68 public static final String PADDING = "sysui_rounded_content_padding";
Adrian Roos56d1a2c2018-03-08 23:22:19 +010069 private static final boolean DEBUG_SCREENSHOT_ROUNDED_CORNERS =
70 SystemProperties.getBoolean("debug.screenshot_rounded_corners", false);
Adrian Roos5b518852018-01-23 17:23:38 +010071
72 private int mRoundedDefault;
73 private View mOverlay;
74 private View mBottomOverlay;
75 private float mDensity;
76 private WindowManager mWindowManager;
77 private boolean mLandscape;
78
79 @Override
80 public void start() {
81 mWindowManager = mContext.getSystemService(WindowManager.class);
82 mRoundedDefault = mContext.getResources().getDimensionPixelSize(
83 R.dimen.rounded_corner_radius);
84 if (mRoundedDefault != 0 || shouldDrawCutout()) {
85 setupDecorations();
86 }
87 int padding = mContext.getResources().getDimensionPixelSize(
88 R.dimen.rounded_corner_content_padding);
89 if (padding != 0) {
90 setupPadding(padding);
91 }
92 }
93
94 private void setupDecorations() {
95 mOverlay = LayoutInflater.from(mContext)
96 .inflate(R.layout.rounded_corners, null);
97 ((ViewGroup)mOverlay).addView(new DisplayCutoutView(mContext, true,
98 this::updateWindowVisibilities));
99 mBottomOverlay = LayoutInflater.from(mContext)
100 .inflate(R.layout.rounded_corners, null);
101 ((ViewGroup)mBottomOverlay).addView(new DisplayCutoutView(mContext, false,
102 this::updateWindowVisibilities));
103
104 mOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
105 mOverlay.setAlpha(0);
106
107 mBottomOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
108 mBottomOverlay.setAlpha(0);
109
110 updateViews();
111
112 mWindowManager.addView(mOverlay, getWindowLayoutParams());
113 mWindowManager.addView(mBottomOverlay, getBottomLayoutParams());
114
115 DisplayMetrics metrics = new DisplayMetrics();
116 mWindowManager.getDefaultDisplay().getMetrics(metrics);
117 mDensity = metrics.density;
118
119 Dependency.get(TunerService.class).addTunable(this, SIZE);
120
121 // Watch color inversion and invert the overlay as needed.
122 SecureSetting setting = new SecureSetting(mContext, Dependency.get(Dependency.MAIN_HANDLER),
123 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) {
124 @Override
125 protected void handleValueChanged(int value, boolean observedChange) {
126 int tint = value != 0 ? Color.WHITE : Color.BLACK;
127 ColorStateList tintList = ColorStateList.valueOf(tint);
128 ((ImageView) mOverlay.findViewById(R.id.left)).setImageTintList(tintList);
129 ((ImageView) mOverlay.findViewById(R.id.right)).setImageTintList(tintList);
130 ((ImageView) mBottomOverlay.findViewById(R.id.left)).setImageTintList(tintList);
131 ((ImageView) mBottomOverlay.findViewById(R.id.right)).setImageTintList(tintList);
132 }
133 };
134 setting.setListening(true);
135 setting.onChange(false);
136
137 mOverlay.addOnLayoutChangeListener(new OnLayoutChangeListener() {
138 @Override
139 public void onLayoutChange(View v, int left, int top, int right, int bottom,
140 int oldLeft,
141 int oldTop, int oldRight, int oldBottom) {
142 mOverlay.removeOnLayoutChangeListener(this);
143 mOverlay.animate()
144 .alpha(1)
145 .setDuration(1000)
146 .start();
147 mBottomOverlay.animate()
148 .alpha(1)
149 .setDuration(1000)
150 .start();
151 }
152 });
153 }
154
155 @Override
156 protected void onConfigurationChanged(Configuration newConfig) {
157 boolean newLanscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE;
158 if (newLanscape != mLandscape) {
159 mLandscape = newLanscape;
160
161 if (mOverlay != null) {
162 updateLayoutParams();
163 updateViews();
164 }
165 }
166 if (shouldDrawCutout() && mOverlay == null) {
167 setupDecorations();
168 }
169 }
170
171 private void updateViews() {
172 View topLeft = mOverlay.findViewById(R.id.left);
173 View topRight = mOverlay.findViewById(R.id.right);
174 View bottomLeft = mBottomOverlay.findViewById(R.id.left);
175 View bottomRight = mBottomOverlay.findViewById(R.id.right);
176 if (mLandscape) {
177 // Flip corners
178 View tmp = topRight;
179 topRight = bottomLeft;
180 bottomLeft = tmp;
181 }
182 updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
183 updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
184 updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
185 updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
186
187 updateWindowVisibilities();
188 }
189
190 private void updateView(View v, int gravity, int rotation) {
191 ((FrameLayout.LayoutParams)v.getLayoutParams()).gravity = gravity;
192 v.setRotation(rotation);
193 }
194
195 private void updateWindowVisibilities() {
196 updateWindowVisibility(mOverlay);
197 updateWindowVisibility(mBottomOverlay);
198 }
199
200 private void updateWindowVisibility(View overlay) {
201 boolean visibleForCutout = shouldDrawCutout()
202 && overlay.findViewById(R.id.display_cutout).getVisibility() == View.VISIBLE;
203 boolean visibleForRoundedCorners = mRoundedDefault > 0;
204 overlay.setVisibility(visibleForCutout || visibleForRoundedCorners
205 ? View.VISIBLE : View.GONE);
206 }
207
208 private boolean shouldDrawCutout() {
209 return mContext.getResources().getBoolean(
210 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout);
211 }
212
213 private void setupPadding(int padding) {
214 // Add some padding to all the content near the edge of the screen.
215 StatusBar sb = getComponent(StatusBar.class);
216 View statusBar = (sb != null ? sb.getStatusBarWindow() : null);
217 if (statusBar != null) {
218 TunablePadding.addTunablePadding(statusBar.findViewById(R.id.keyguard_header), PADDING,
219 padding, FLAG_END);
220
221 FragmentHostManager fragmentHostManager = FragmentHostManager.get(statusBar);
222 fragmentHostManager.addTagListener(CollapsedStatusBarFragment.TAG,
223 new TunablePaddingTagListener(padding, R.id.status_bar));
224 fragmentHostManager.addTagListener(QS.TAG,
225 new TunablePaddingTagListener(padding, R.id.header));
226 }
227 }
228
229 @VisibleForTesting
230 WindowManager.LayoutParams getWindowLayoutParams() {
231 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
232 ViewGroup.LayoutParams.MATCH_PARENT,
233 LayoutParams.WRAP_CONTENT,
234 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
235 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
236 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
237 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
238 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
239 | WindowManager.LayoutParams.FLAG_SLIPPERY
240 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
241 PixelFormat.TRANSLUCENT);
Robert Carr772e8bc2018-03-14 11:51:23 -0700242 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS
243 | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
244
Adrian Roos56d1a2c2018-03-08 23:22:19 +0100245 if (!DEBUG_SCREENSHOT_ROUNDED_CORNERS) {
246 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY;
247 }
Robert Carr772e8bc2018-03-14 11:51:23 -0700248
Adrian Roos5b518852018-01-23 17:23:38 +0100249 lp.setTitle("ScreenDecorOverlay");
250 lp.gravity = Gravity.TOP | Gravity.LEFT;
251 lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
252 if (mLandscape) {
253 lp.width = WRAP_CONTENT;
254 lp.height = MATCH_PARENT;
255 }
256 return lp;
257 }
258
259 private WindowManager.LayoutParams getBottomLayoutParams() {
260 WindowManager.LayoutParams lp = getWindowLayoutParams();
261 lp.setTitle("ScreenDecorOverlayBottom");
262 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
263 return lp;
264 }
265
266 private void updateLayoutParams() {
267 mWindowManager.updateViewLayout(mOverlay, getWindowLayoutParams());
268 mWindowManager.updateViewLayout(mBottomOverlay, getBottomLayoutParams());
269 }
270
271 @Override
272 public void onTuningChanged(String key, String newValue) {
273 if (mOverlay == null) return;
274 if (SIZE.equals(key)) {
275 int size = mRoundedDefault;
276 try {
277 size = (int) (Integer.parseInt(newValue) * mDensity);
278 } catch (Exception e) {
279 }
280 setSize(mOverlay.findViewById(R.id.left), size);
281 setSize(mOverlay.findViewById(R.id.right), size);
282 setSize(mBottomOverlay.findViewById(R.id.left), size);
283 setSize(mBottomOverlay.findViewById(R.id.right), size);
284 }
285 }
286
287 private void setSize(View view, int pixelSize) {
288 LayoutParams params = view.getLayoutParams();
289 params.width = pixelSize;
290 params.height = pixelSize;
291 view.setLayoutParams(params);
292 }
293
294 @VisibleForTesting
295 static class TunablePaddingTagListener implements FragmentListener {
296
297 private final int mPadding;
298 private final int mId;
299 private TunablePadding mTunablePadding;
300
301 public TunablePaddingTagListener(int padding, int id) {
302 mPadding = padding;
303 mId = id;
304 }
305
306 @Override
307 public void onFragmentViewCreated(String tag, Fragment fragment) {
308 if (mTunablePadding != null) {
309 mTunablePadding.destroy();
310 }
311 View view = fragment.getView();
312 if (mId != 0) {
313 view = view.findViewById(mId);
314 }
315 mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding,
316 FLAG_START | FLAG_END);
317 }
318 }
319
320 public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener {
321
322 private final DisplayInfo mInfo = new DisplayInfo();
323 private final Paint mPaint = new Paint();
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100324 private final Region mBounds = new Region();
Adrian Roos5b518852018-01-23 17:23:38 +0100325 private final Rect mBoundingRect = new Rect();
326 private final Path mBoundingPath = new Path();
327 private final int[] mLocation = new int[2];
328 private final boolean mStart;
329 private final Runnable mVisibilityChangedListener;
330
331 public DisplayCutoutView(Context context, boolean start,
332 Runnable visibilityChangedListener) {
333 super(context);
334 mStart = start;
335 mVisibilityChangedListener = visibilityChangedListener;
336 setId(R.id.display_cutout);
337 }
338
339 @Override
340 protected void onAttachedToWindow() {
341 super.onAttachedToWindow();
342 mContext.getSystemService(DisplayManager.class).registerDisplayListener(this,
343 getHandler());
344 update();
345 }
346
347 @Override
348 protected void onDetachedFromWindow() {
349 super.onDetachedFromWindow();
350 mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this);
351 }
352
353 @Override
354 protected void onDraw(Canvas canvas) {
355 super.onDraw(canvas);
356 getLocationOnScreen(mLocation);
357 canvas.translate(-mLocation[0], -mLocation[1]);
358 if (!mBoundingPath.isEmpty()) {
359 mPaint.setColor(Color.BLACK);
360 mPaint.setStyle(Paint.Style.FILL);
361 canvas.drawPath(mBoundingPath, mPaint);
362 }
363 }
364
365 @Override
366 public void onDisplayAdded(int displayId) {
367 }
368
369 @Override
370 public void onDisplayRemoved(int displayId) {
371 }
372
373 @Override
374 public void onDisplayChanged(int displayId) {
375 if (displayId == getDisplay().getDisplayId()) {
376 update();
377 }
378 }
379
380 private void update() {
381 requestLayout();
382 getDisplay().getDisplayInfo(mInfo);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100383 mBounds.setEmpty();
Adrian Roos5b518852018-01-23 17:23:38 +0100384 mBoundingRect.setEmpty();
385 mBoundingPath.reset();
386 int newVisible;
387 if (hasCutout()) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100388 mBounds.set(mInfo.displayCutout.getBounds());
389 localBounds(mBoundingRect);
Adrian Roos5b518852018-01-23 17:23:38 +0100390 mInfo.displayCutout.getBounds().getBoundaryPath(mBoundingPath);
391 newVisible = VISIBLE;
392 } else {
393 newVisible = GONE;
394 }
395 if (newVisible != getVisibility()) {
396 setVisibility(newVisible);
397 mVisibilityChangedListener.run();
398 }
399 }
400
401 private boolean hasCutout() {
Adrian Roos24264212018-02-19 16:26:15 +0100402 final DisplayCutout displayCutout = mInfo.displayCutout;
403 if (displayCutout == null) {
Adrian Roos5b518852018-01-23 17:23:38 +0100404 return false;
405 }
Adrian Roos5b518852018-01-23 17:23:38 +0100406 if (mStart) {
407 return displayCutout.getSafeInsetLeft() > 0
408 || displayCutout.getSafeInsetTop() > 0;
409 } else {
410 return displayCutout.getSafeInsetRight() > 0
411 || displayCutout.getSafeInsetBottom() > 0;
412 }
413 }
414
415 @Override
416 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100417 if (mBounds.isEmpty()) {
Adrian Roos5b518852018-01-23 17:23:38 +0100418 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
419 return;
420 }
421 setMeasuredDimension(
422 resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0),
423 resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0));
424 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100425
426 public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, Rect out) {
427 Region bounds = displayCutout.getBounds();
428 switch (gravity) {
429 case Gravity.TOP:
430 bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(),
431 Region.Op.INTERSECT);
432 out.set(bounds.getBounds());
433 break;
434 case Gravity.LEFT:
435 bounds.op(0, 0, displayCutout.getSafeInsetLeft(), Integer.MAX_VALUE,
436 Region.Op.INTERSECT);
437 out.set(bounds.getBounds());
438 break;
439 case Gravity.BOTTOM:
440 bounds.op(0, displayCutout.getSafeInsetTop() + 1, Integer.MAX_VALUE,
441 Integer.MAX_VALUE, Region.Op.INTERSECT);
442 out.set(bounds.getBounds());
443 break;
444 case Gravity.RIGHT:
445 bounds.op(displayCutout.getSafeInsetLeft() + 1, 0, Integer.MAX_VALUE,
446 Integer.MAX_VALUE, Region.Op.INTERSECT);
447 out.set(bounds.getBounds());
448 break;
449 }
450 bounds.recycle();
451 }
452
453 private void localBounds(Rect out) {
454 final DisplayCutout displayCutout = mInfo.displayCutout;
455
456 if (mStart) {
457 if (displayCutout.getSafeInsetLeft() > 0) {
458 boundsFromDirection(displayCutout, Gravity.LEFT, out);
459 } else if (displayCutout.getSafeInsetTop() > 0) {
460 boundsFromDirection(displayCutout, Gravity.TOP, out);
461 }
462 } else {
463 if (displayCutout.getSafeInsetRight() > 0) {
464 boundsFromDirection(displayCutout, Gravity.RIGHT, out);
465 } else if (displayCutout.getSafeInsetBottom() > 0) {
466 boundsFromDirection(displayCutout, Gravity.BOTTOM, out);
467 }
468 }
469 }
Adrian Roos5b518852018-01-23 17:23:38 +0100470 }
471}