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 | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 42 | import android.os.SystemProperties; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 43 | import android.provider.Settings.Secure; |
| 44 | import android.support.annotation.VisibleForTesting; |
| 45 | import android.util.DisplayMetrics; |
| 46 | import android.view.DisplayCutout; |
| 47 | import android.view.DisplayInfo; |
| 48 | import android.view.Gravity; |
| 49 | import android.view.LayoutInflater; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 50 | import android.view.Surface; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 51 | import android.view.View; |
| 52 | import android.view.View.OnLayoutChangeListener; |
| 53 | import android.view.ViewGroup; |
| 54 | import android.view.ViewGroup.LayoutParams; |
| 55 | import android.view.WindowManager; |
| 56 | import android.widget.FrameLayout; |
| 57 | import android.widget.ImageView; |
| 58 | |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 59 | import com.android.systemui.RegionInterceptingFrameLayout.RegionInterceptableView; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 60 | import com.android.systemui.fragments.FragmentHostManager; |
| 61 | import com.android.systemui.fragments.FragmentHostManager.FragmentListener; |
| 62 | import com.android.systemui.plugins.qs.QS; |
| 63 | import com.android.systemui.qs.SecureSetting; |
| 64 | import com.android.systemui.statusbar.phone.CollapsedStatusBarFragment; |
| 65 | import com.android.systemui.statusbar.phone.StatusBar; |
| 66 | import com.android.systemui.tuner.TunablePadding; |
| 67 | import com.android.systemui.tuner.TunerService; |
| 68 | import com.android.systemui.tuner.TunerService.Tunable; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 69 | import com.android.systemui.util.leak.RotationUtils; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 70 | |
| 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 | */ |
| 75 | public 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 Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 78 | private static final boolean DEBUG_SCREENSHOT_ROUNDED_CORNERS = |
| 79 | SystemProperties.getBoolean("debug.screenshot_rounded_corners", false); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 80 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 81 | private DisplayManager mDisplayManager; |
| 82 | private DisplayManager.DisplayListener mDisplayListener; |
| 83 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 84 | private int mRoundedDefault; |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 85 | private int mRoundedDefaultTop; |
| 86 | private int mRoundedDefaultBottom; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 87 | private View mOverlay; |
| 88 | private View mBottomOverlay; |
| 89 | private float mDensity; |
| 90 | private WindowManager mWindowManager; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 91 | private int mRotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 92 | |
| 93 | @Override |
| 94 | public void start() { |
| 95 | mWindowManager = mContext.getSystemService(WindowManager.class); |
| 96 | mRoundedDefault = mContext.getResources().getDimensionPixelSize( |
| 97 | R.dimen.rounded_corner_radius); |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 98 | 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 Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 103 | setupDecorations(); |
| 104 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 105 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 106 | int padding = mContext.getResources().getDimensionPixelSize( |
| 107 | R.dimen.rounded_corner_content_padding); |
| 108 | if (padding != 0) { |
| 109 | setupPadding(padding); |
| 110 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 111 | |
| 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 Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 133 | } |
| 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) { |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 198 | updateOrientation(); |
| 199 | } |
| 200 | |
| 201 | protected void updateOrientation() { |
| 202 | int newRotation = RotationUtils.getExactRotation(mContext); |
| 203 | if (newRotation != mRotation) { |
| 204 | mRotation = newRotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 205 | |
| 206 | if (mOverlay != null) { |
| 207 | updateLayoutParams(); |
| 208 | updateViews(); |
| 209 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 210 | |
| 211 | if (shouldDrawCutout() && mOverlay == null) { |
| 212 | setupDecorations(); |
| 213 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 214 | } |
| 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); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 222 | |
| 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 Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 243 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 244 | |
| 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; |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 261 | boolean visibleForRoundedCorners = hasRoundedCorners(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 262 | overlay.setVisibility(visibleForCutout || visibleForRoundedCorners |
| 263 | ? View.VISIBLE : View.GONE); |
| 264 | } |
| 265 | |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 266 | private boolean hasRoundedCorners() { |
| 267 | return mRoundedDefault > 0 || mRoundedDefaultBottom > 0 || mRoundedDefaultTop > 0; |
| 268 | } |
| 269 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 270 | private boolean shouldDrawCutout() { |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 271 | return shouldDrawCutout(mContext); |
| 272 | } |
| 273 | |
| 274 | static boolean shouldDrawCutout(Context context) { |
| 275 | return context.getResources().getBoolean( |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 276 | 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 Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 301 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 302 | | 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 Carr | 772e8bc | 2018-03-14 11:51:23 -0700 | [diff] [blame] | 307 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS |
| 308 | | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION; |
| 309 | |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 310 | if (!DEBUG_SCREENSHOT_ROUNDED_CORNERS) { |
| 311 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY; |
| 312 | } |
Robert Carr | 772e8bc | 2018-03-14 11:51:23 -0700 | [diff] [blame] | 313 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 314 | lp.setTitle("ScreenDecorOverlay"); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 315 | 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 Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 321 | lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 322 | if (isLandscape(mRotation)) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 323 | 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"); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 332 | 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 Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 338 | 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; |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 351 | 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 Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 358 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 359 | |
| 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 Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 371 | } |
| 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 Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 407 | public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener, |
| 408 | RegionInterceptableView { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 409 | |
| 410 | private final DisplayInfo mInfo = new DisplayInfo(); |
| 411 | private final Paint mPaint = new Paint(); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 412 | private final Region mBounds = new Region(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 413 | 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 Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 449 | mPaint.setAntiAlias(true); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 450 | 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 Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 472 | mBounds.setEmpty(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 473 | mBoundingRect.setEmpty(); |
| 474 | mBoundingPath.reset(); |
| 475 | int newVisible; |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 476 | if (shouldDrawCutout(getContext()) && hasCutout()) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 477 | mBounds.set(mInfo.displayCutout.getBounds()); |
| 478 | localBounds(mBoundingRect); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 479 | updateBoundingPath(); |
Adrian Roos | 1b02828 | 2018-03-14 14:43:03 +0100 | [diff] [blame] | 480 | invalidate(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 481 | newVisible = VISIBLE; |
| 482 | } else { |
| 483 | newVisible = GONE; |
| 484 | } |
| 485 | if (newVisible != getVisibility()) { |
| 486 | setVisibility(newVisible); |
| 487 | mVisibilityChangedListener.run(); |
| 488 | } |
| 489 | } |
| 490 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 491 | 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 Roos | a99f5d6 | 2018-04-16 16:03:04 +0200 | [diff] [blame] | 500 | mBoundingPath.set(DisplayCutout.pathFromResources(getResources(), dw, dh)); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 501 | 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 Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 529 | private boolean hasCutout() { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 530 | final DisplayCutout displayCutout = mInfo.displayCutout; |
| 531 | if (displayCutout == null) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 532 | return false; |
| 533 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 534 | 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 Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 545 | if (mBounds.isEmpty()) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 546 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 547 | return; |
| 548 | } |
| 549 | setMeasuredDimension( |
| 550 | resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0), |
| 551 | resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0)); |
| 552 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 553 | |
| 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 Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 598 | |
| 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 Roos | 134e1cb | 2018-05-16 17:04:29 +0200 | [diff] [blame] | 610 | 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 Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 622 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 623 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame^] | 624 | |
| 625 | private boolean isLandscape(int rotation) { |
| 626 | return rotation == RotationUtils.ROTATION_LANDSCAPE || rotation == |
| 627 | RotationUtils.ROTATION_SEASCAPE; |
| 628 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 629 | } |