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; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 29 | import android.app.ActivityManager; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 30 | import android.app.Fragment; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 31 | import android.content.BroadcastReceiver; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 32 | import android.content.Context; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 35 | import android.content.res.ColorStateList; |
| 36 | import android.content.res.Configuration; |
| 37 | import android.graphics.Canvas; |
| 38 | import android.graphics.Color; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 39 | import android.graphics.Matrix; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 40 | import android.graphics.Paint; |
| 41 | import android.graphics.Path; |
| 42 | import android.graphics.PixelFormat; |
| 43 | import android.graphics.Rect; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 44 | import android.graphics.Region; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 45 | import android.hardware.display.DisplayManager; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 46 | import android.os.Handler; |
| 47 | import android.os.HandlerThread; |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 48 | import android.os.SystemProperties; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 49 | import android.provider.Settings.Secure; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 50 | import android.util.DisplayMetrics; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 51 | import android.util.Log; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 52 | import android.view.DisplayCutout; |
| 53 | import android.view.DisplayInfo; |
| 54 | import android.view.Gravity; |
| 55 | import android.view.LayoutInflater; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 56 | import android.view.Surface; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 57 | import android.view.View; |
| 58 | import android.view.View.OnLayoutChangeListener; |
| 59 | import android.view.ViewGroup; |
| 60 | import android.view.ViewGroup.LayoutParams; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 61 | import android.view.ViewTreeObserver; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 62 | import android.view.WindowManager; |
| 63 | import android.widget.FrameLayout; |
| 64 | import android.widget.ImageView; |
| 65 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 66 | import androidx.annotation.VisibleForTesting; |
| 67 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 68 | import com.android.internal.util.Preconditions; |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 69 | import com.android.systemui.RegionInterceptingFrameLayout.RegionInterceptableView; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 70 | import com.android.systemui.fragments.FragmentHostManager; |
| 71 | import com.android.systemui.fragments.FragmentHostManager.FragmentListener; |
| 72 | import com.android.systemui.plugins.qs.QS; |
| 73 | import com.android.systemui.qs.SecureSetting; |
| 74 | import com.android.systemui.statusbar.phone.CollapsedStatusBarFragment; |
| 75 | import com.android.systemui.statusbar.phone.StatusBar; |
| 76 | import com.android.systemui.tuner.TunablePadding; |
| 77 | import com.android.systemui.tuner.TunerService; |
| 78 | import com.android.systemui.tuner.TunerService.Tunable; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 79 | import com.android.systemui.util.leak.RotationUtils; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 80 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 81 | import java.util.ArrayList; |
| 82 | import java.util.List; |
| 83 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 84 | /** |
| 85 | * An overlay that draws screen decorations in software (e.g for rounded corners or display cutout) |
| 86 | * for antialiasing and emulation purposes. |
| 87 | */ |
| 88 | public class ScreenDecorations extends SystemUI implements Tunable { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 89 | private static final boolean DEBUG = false; |
| 90 | private static final String TAG = "ScreenDecorations"; |
| 91 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 92 | public static final String SIZE = "sysui_rounded_size"; |
| 93 | public static final String PADDING = "sysui_rounded_content_padding"; |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 94 | private static final boolean DEBUG_SCREENSHOT_ROUNDED_CORNERS = |
| 95 | SystemProperties.getBoolean("debug.screenshot_rounded_corners", false); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 96 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 97 | private DisplayManager mDisplayManager; |
| 98 | private DisplayManager.DisplayListener mDisplayListener; |
| 99 | |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 100 | @VisibleForTesting protected int mRoundedDefault; |
| 101 | @VisibleForTesting protected int mRoundedDefaultTop; |
| 102 | @VisibleForTesting protected int mRoundedDefaultBottom; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 103 | private View mOverlay; |
| 104 | private View mBottomOverlay; |
| 105 | private float mDensity; |
| 106 | private WindowManager mWindowManager; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 107 | private int mRotation; |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 108 | private DisplayCutoutView mCutoutTop; |
| 109 | private DisplayCutoutView mCutoutBottom; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 110 | private SecureSetting mColorInversionSetting; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 111 | private boolean mPendingRotationChange; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 112 | private Handler mHandler; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 113 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 114 | /** |
| 115 | * Converts a set of {@link Rect}s into a {@link Region} |
| 116 | * |
| 117 | * @hide |
| 118 | */ |
| 119 | public static Region rectsToRegion(List<Rect> rects) { |
| 120 | Region result = Region.obtain(); |
| 121 | if (rects != null) { |
| 122 | for (Rect r : rects) { |
| 123 | if (r != null && !r.isEmpty()) { |
| 124 | result.op(r, Region.Op.UNION); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return result; |
| 129 | } |
| 130 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 131 | @Override |
| 132 | public void start() { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 133 | mHandler = startHandlerThread(); |
| 134 | mHandler.post(this::startOnScreenDecorationsThread); |
| 135 | setupStatusBarPaddingIfNeeded(); |
| 136 | } |
| 137 | |
| 138 | @VisibleForTesting |
| 139 | Handler startHandlerThread() { |
| 140 | HandlerThread thread = new HandlerThread("ScreenDecorations"); |
| 141 | thread.start(); |
| 142 | return thread.getThreadHandler(); |
| 143 | } |
| 144 | |
| 145 | private void startOnScreenDecorationsThread() { |
| 146 | mRotation = RotationUtils.getExactRotation(mContext); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 147 | mWindowManager = mContext.getSystemService(WindowManager.class); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 148 | updateRoundedCornerRadii(); |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 149 | if (hasRoundedCorners() || shouldDrawCutout()) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 150 | setupDecorations(); |
| 151 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 152 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 153 | mDisplayListener = new DisplayManager.DisplayListener() { |
| 154 | @Override |
| 155 | public void onDisplayAdded(int displayId) { |
| 156 | // do nothing |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onDisplayRemoved(int displayId) { |
| 161 | // do nothing |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void onDisplayChanged(int displayId) { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 166 | final int newRotation = RotationUtils.getExactRotation(mContext); |
| 167 | if (mOverlay != null && mBottomOverlay != null && mRotation != newRotation) { |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 168 | // We cannot immediately update the orientation. Otherwise |
| 169 | // WindowManager is still deferring layout until it has finished dispatching |
| 170 | // the config changes, which may cause divergence between what we draw |
| 171 | // (new orientation), and where we are placed on the screen (old orientation). |
| 172 | // Instead we wait until either: |
| 173 | // - we are trying to redraw. This because WM resized our window and told us to. |
| 174 | // - the config change has been dispatched, so WM is no longer deferring layout. |
| 175 | mPendingRotationChange = true; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 176 | if (DEBUG) { |
| 177 | Log.i(TAG, "Rotation changed, deferring " + newRotation + ", staying at " |
| 178 | + mRotation); |
| 179 | } |
| 180 | |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 181 | mOverlay.getViewTreeObserver().addOnPreDrawListener( |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 182 | new RestartingPreDrawListener(mOverlay, newRotation)); |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 183 | mBottomOverlay.getViewTreeObserver().addOnPreDrawListener( |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 184 | new RestartingPreDrawListener(mBottomOverlay, newRotation)); |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 185 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 186 | updateOrientation(); |
| 187 | } |
| 188 | }; |
| 189 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 190 | mDisplayManager = (DisplayManager) mContext.getSystemService( |
| 191 | Context.DISPLAY_SERVICE); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 192 | mDisplayManager.registerDisplayListener(mDisplayListener, mHandler); |
| 193 | updateOrientation(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | private void setupDecorations() { |
| 197 | mOverlay = LayoutInflater.from(mContext) |
| 198 | .inflate(R.layout.rounded_corners, null); |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 199 | mCutoutTop = new DisplayCutoutView(mContext, true, |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 200 | this::updateWindowVisibilities, this); |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 201 | ((ViewGroup)mOverlay).addView(mCutoutTop); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 202 | mBottomOverlay = LayoutInflater.from(mContext) |
| 203 | .inflate(R.layout.rounded_corners, null); |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 204 | mCutoutBottom = new DisplayCutoutView(mContext, false, |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 205 | this::updateWindowVisibilities, this); |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 206 | ((ViewGroup)mBottomOverlay).addView(mCutoutBottom); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 207 | |
| 208 | mOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 209 | mOverlay.setAlpha(0); |
John Reck | a2d20b4 | 2018-10-01 12:21:55 -0700 | [diff] [blame] | 210 | mOverlay.setForceDarkAllowed(false); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 211 | |
| 212 | mBottomOverlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 213 | mBottomOverlay.setAlpha(0); |
John Reck | a2d20b4 | 2018-10-01 12:21:55 -0700 | [diff] [blame] | 214 | mBottomOverlay.setForceDarkAllowed(false); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 215 | |
| 216 | updateViews(); |
| 217 | |
| 218 | mWindowManager.addView(mOverlay, getWindowLayoutParams()); |
| 219 | mWindowManager.addView(mBottomOverlay, getBottomLayoutParams()); |
| 220 | |
| 221 | DisplayMetrics metrics = new DisplayMetrics(); |
| 222 | mWindowManager.getDefaultDisplay().getMetrics(metrics); |
| 223 | mDensity = metrics.density; |
| 224 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 225 | Dependency.get(Dependency.MAIN_HANDLER).post( |
| 226 | () -> Dependency.get(TunerService.class).addTunable(this, SIZE)); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 227 | |
| 228 | // Watch color inversion and invert the overlay as needed. |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 229 | mColorInversionSetting = new SecureSetting(mContext, mHandler, |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 230 | Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED) { |
| 231 | @Override |
| 232 | protected void handleValueChanged(int value, boolean observedChange) { |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 233 | updateColorInversion(value); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 234 | } |
| 235 | }; |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 236 | mColorInversionSetting.setListening(true); |
| 237 | mColorInversionSetting.onChange(false); |
| 238 | |
| 239 | IntentFilter filter = new IntentFilter(); |
| 240 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 241 | mContext.registerReceiver(mIntentReceiver, filter, null /* permission */, mHandler); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 242 | |
| 243 | mOverlay.addOnLayoutChangeListener(new OnLayoutChangeListener() { |
| 244 | @Override |
| 245 | public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 246 | int oldLeft, |
| 247 | int oldTop, int oldRight, int oldBottom) { |
| 248 | mOverlay.removeOnLayoutChangeListener(this); |
| 249 | mOverlay.animate() |
| 250 | .alpha(1) |
| 251 | .setDuration(1000) |
| 252 | .start(); |
| 253 | mBottomOverlay.animate() |
| 254 | .alpha(1) |
| 255 | .setDuration(1000) |
| 256 | .start(); |
| 257 | } |
| 258 | }); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 259 | |
| 260 | mOverlay.getViewTreeObserver().addOnPreDrawListener( |
| 261 | new ValidatingPreDrawListener(mOverlay)); |
| 262 | mBottomOverlay.getViewTreeObserver().addOnPreDrawListener( |
| 263 | new ValidatingPreDrawListener(mBottomOverlay)); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Beverly | 374acde | 2018-06-13 10:49:26 -0400 | [diff] [blame] | 266 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 267 | @Override |
| 268 | public void onReceive(Context context, Intent intent) { |
| 269 | String action = intent.getAction(); |
| 270 | if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
| 271 | int newUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 272 | ActivityManager.getCurrentUser()); |
| 273 | // update color inversion setting to the new user |
| 274 | mColorInversionSetting.setUserId(newUserId); |
| 275 | updateColorInversion(mColorInversionSetting.getValue()); |
| 276 | } |
| 277 | } |
| 278 | }; |
| 279 | |
| 280 | private void updateColorInversion(int colorsInvertedValue) { |
| 281 | int tint = colorsInvertedValue != 0 ? Color.WHITE : Color.BLACK; |
| 282 | ColorStateList tintList = ColorStateList.valueOf(tint); |
| 283 | ((ImageView) mOverlay.findViewById(R.id.left)).setImageTintList(tintList); |
| 284 | ((ImageView) mOverlay.findViewById(R.id.right)).setImageTintList(tintList); |
| 285 | ((ImageView) mBottomOverlay.findViewById(R.id.left)).setImageTintList(tintList); |
| 286 | ((ImageView) mBottomOverlay.findViewById(R.id.right)).setImageTintList(tintList); |
| 287 | mCutoutTop.setColor(tint); |
| 288 | mCutoutBottom.setColor(tint); |
| 289 | } |
| 290 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 291 | @Override |
| 292 | protected void onConfigurationChanged(Configuration newConfig) { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 293 | mHandler.post(() -> { |
| 294 | int oldRotation = mRotation; |
| 295 | mPendingRotationChange = false; |
| 296 | updateOrientation(); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 297 | updateRoundedCornerRadii(); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 298 | if (DEBUG) Log.i(TAG, "onConfigChanged from rot " + oldRotation + " to " + mRotation); |
| 299 | if (shouldDrawCutout() && mOverlay == null) { |
| 300 | setupDecorations(); |
| 301 | } |
| 302 | if (mOverlay != null) { |
| 303 | // Updating the layout params ensures that ViewRootImpl will call relayoutWindow(), |
| 304 | // which ensures that the forced seamless rotation will end, even if we updated |
| 305 | // the rotation before window manager was ready (and was still waiting for sending |
| 306 | // the updated rotation). |
| 307 | updateLayoutParams(); |
| 308 | } |
| 309 | }); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 310 | } |
| 311 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 312 | private void updateOrientation() { |
| 313 | Preconditions.checkState(mHandler.getLooper().getThread() == Thread.currentThread(), |
| 314 | "must call on " + mHandler.getLooper().getThread() |
| 315 | + ", but was " + Thread.currentThread()); |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 316 | if (mPendingRotationChange) { |
| 317 | return; |
| 318 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 319 | int newRotation = RotationUtils.getExactRotation(mContext); |
| 320 | if (newRotation != mRotation) { |
| 321 | mRotation = newRotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 322 | |
| 323 | if (mOverlay != null) { |
| 324 | updateLayoutParams(); |
| 325 | updateViews(); |
| 326 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 330 | private void updateRoundedCornerRadii() { |
| 331 | final int newRoundedDefault = mContext.getResources().getDimensionPixelSize( |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 332 | com.android.internal.R.dimen.rounded_corner_radius); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 333 | final int newRoundedDefaultTop = mContext.getResources().getDimensionPixelSize( |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 334 | com.android.internal.R.dimen.rounded_corner_radius_top); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 335 | final int newRoundedDefaultBottom = mContext.getResources().getDimensionPixelSize( |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 336 | com.android.internal.R.dimen.rounded_corner_radius_bottom); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 337 | |
| 338 | final boolean roundedCornersChanged = mRoundedDefault != newRoundedDefault |
| 339 | || mRoundedDefaultBottom != newRoundedDefaultBottom |
| 340 | || mRoundedDefaultTop != newRoundedDefaultTop; |
| 341 | |
| 342 | if (roundedCornersChanged) { |
| 343 | mRoundedDefault = newRoundedDefault; |
| 344 | mRoundedDefaultTop = newRoundedDefaultTop; |
| 345 | mRoundedDefaultBottom = newRoundedDefaultBottom; |
| 346 | onTuningChanged(SIZE, null); |
| 347 | } |
| 348 | } |
| 349 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 350 | private void updateViews() { |
| 351 | View topLeft = mOverlay.findViewById(R.id.left); |
| 352 | View topRight = mOverlay.findViewById(R.id.right); |
| 353 | View bottomLeft = mBottomOverlay.findViewById(R.id.left); |
| 354 | View bottomRight = mBottomOverlay.findViewById(R.id.right); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 355 | |
| 356 | if (mRotation == RotationUtils.ROTATION_NONE) { |
| 357 | updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0); |
| 358 | updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90); |
| 359 | updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270); |
| 360 | updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180); |
| 361 | } else if (mRotation == RotationUtils.ROTATION_LANDSCAPE) { |
| 362 | updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0); |
| 363 | updateView(topRight, Gravity.BOTTOM | Gravity.LEFT, 270); |
| 364 | updateView(bottomLeft, Gravity.TOP | Gravity.RIGHT, 90);; |
| 365 | updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180); |
| 366 | } else if (mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) { |
| 367 | updateView(topLeft, Gravity.BOTTOM | Gravity.LEFT, 270); |
| 368 | updateView(topRight, Gravity.BOTTOM | Gravity.RIGHT, 180); |
| 369 | updateView(bottomLeft, Gravity.TOP | Gravity.LEFT, 0); |
| 370 | updateView(bottomRight, Gravity.TOP | Gravity.RIGHT, 90); |
| 371 | } else if (mRotation == RotationUtils.ROTATION_SEASCAPE) { |
| 372 | updateView(topLeft, Gravity.BOTTOM | Gravity.RIGHT, 180); |
| 373 | updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90); |
| 374 | updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270); |
| 375 | updateView(bottomRight, Gravity.TOP | Gravity.LEFT, 0); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 376 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 377 | |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 378 | mCutoutTop.setRotation(mRotation); |
| 379 | mCutoutBottom.setRotation(mRotation); |
| 380 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 381 | updateWindowVisibilities(); |
| 382 | } |
| 383 | |
| 384 | private void updateView(View v, int gravity, int rotation) { |
| 385 | ((FrameLayout.LayoutParams)v.getLayoutParams()).gravity = gravity; |
| 386 | v.setRotation(rotation); |
| 387 | } |
| 388 | |
| 389 | private void updateWindowVisibilities() { |
| 390 | updateWindowVisibility(mOverlay); |
| 391 | updateWindowVisibility(mBottomOverlay); |
| 392 | } |
| 393 | |
| 394 | private void updateWindowVisibility(View overlay) { |
| 395 | boolean visibleForCutout = shouldDrawCutout() |
| 396 | && overlay.findViewById(R.id.display_cutout).getVisibility() == View.VISIBLE; |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 397 | boolean visibleForRoundedCorners = hasRoundedCorners(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 398 | overlay.setVisibility(visibleForCutout || visibleForRoundedCorners |
| 399 | ? View.VISIBLE : View.GONE); |
| 400 | } |
| 401 | |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 402 | private boolean hasRoundedCorners() { |
| 403 | return mRoundedDefault > 0 || mRoundedDefaultBottom > 0 || mRoundedDefaultTop > 0; |
| 404 | } |
| 405 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 406 | private boolean shouldDrawCutout() { |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 407 | return shouldDrawCutout(mContext); |
| 408 | } |
| 409 | |
| 410 | static boolean shouldDrawCutout(Context context) { |
| 411 | return context.getResources().getBoolean( |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 412 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout); |
| 413 | } |
| 414 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 415 | |
| 416 | private void setupStatusBarPaddingIfNeeded() { |
| 417 | // TODO: This should be moved to a more appropriate place, as it is not related to the |
| 418 | // screen decorations overlay. |
| 419 | int padding = mContext.getResources().getDimensionPixelSize( |
| 420 | R.dimen.rounded_corner_content_padding); |
| 421 | if (padding != 0) { |
| 422 | setupStatusBarPadding(padding); |
| 423 | } |
| 424 | |
| 425 | } |
| 426 | |
| 427 | private void setupStatusBarPadding(int padding) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 428 | // Add some padding to all the content near the edge of the screen. |
| 429 | StatusBar sb = getComponent(StatusBar.class); |
| 430 | View statusBar = (sb != null ? sb.getStatusBarWindow() : null); |
| 431 | if (statusBar != null) { |
| 432 | TunablePadding.addTunablePadding(statusBar.findViewById(R.id.keyguard_header), PADDING, |
| 433 | padding, FLAG_END); |
| 434 | |
| 435 | FragmentHostManager fragmentHostManager = FragmentHostManager.get(statusBar); |
| 436 | fragmentHostManager.addTagListener(CollapsedStatusBarFragment.TAG, |
| 437 | new TunablePaddingTagListener(padding, R.id.status_bar)); |
| 438 | fragmentHostManager.addTagListener(QS.TAG, |
| 439 | new TunablePaddingTagListener(padding, R.id.header)); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | @VisibleForTesting |
| 444 | WindowManager.LayoutParams getWindowLayoutParams() { |
| 445 | final WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 446 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 447 | LayoutParams.WRAP_CONTENT, |
| 448 | WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL, |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 449 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 450 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 451 | | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
| 452 | | WindowManager.LayoutParams.FLAG_SLIPPERY |
| 453 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, |
| 454 | PixelFormat.TRANSLUCENT); |
Robert Carr | 772e8bc | 2018-03-14 11:51:23 -0700 | [diff] [blame] | 455 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS |
| 456 | | WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION; |
| 457 | |
Adrian Roos | 56d1a2c | 2018-03-08 23:22:19 +0100 | [diff] [blame] | 458 | if (!DEBUG_SCREENSHOT_ROUNDED_CORNERS) { |
| 459 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY; |
| 460 | } |
Robert Carr | 772e8bc | 2018-03-14 11:51:23 -0700 | [diff] [blame] | 461 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 462 | lp.setTitle("ScreenDecorOverlay"); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 463 | if (mRotation == RotationUtils.ROTATION_SEASCAPE |
| 464 | || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) { |
| 465 | lp.gravity = Gravity.BOTTOM | Gravity.RIGHT; |
| 466 | } else { |
| 467 | lp.gravity = Gravity.TOP | Gravity.LEFT; |
| 468 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 469 | lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 470 | if (isLandscape(mRotation)) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 471 | lp.width = WRAP_CONTENT; |
| 472 | lp.height = MATCH_PARENT; |
| 473 | } |
Peiyong Lin | 7504538 | 2019-03-04 19:22:33 -0800 | [diff] [blame^] | 474 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COLOR_SPACE_AGNOSTIC; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 475 | return lp; |
| 476 | } |
| 477 | |
| 478 | private WindowManager.LayoutParams getBottomLayoutParams() { |
| 479 | WindowManager.LayoutParams lp = getWindowLayoutParams(); |
| 480 | lp.setTitle("ScreenDecorOverlayBottom"); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 481 | if (mRotation == RotationUtils.ROTATION_SEASCAPE |
| 482 | || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) { |
| 483 | lp.gravity = Gravity.TOP | Gravity.LEFT; |
| 484 | } else { |
| 485 | lp.gravity = Gravity.BOTTOM | Gravity.RIGHT; |
| 486 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 487 | return lp; |
| 488 | } |
| 489 | |
| 490 | private void updateLayoutParams() { |
| 491 | mWindowManager.updateViewLayout(mOverlay, getWindowLayoutParams()); |
| 492 | mWindowManager.updateViewLayout(mBottomOverlay, getBottomLayoutParams()); |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public void onTuningChanged(String key, String newValue) { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 497 | mHandler.post(() -> { |
| 498 | if (mOverlay == null) return; |
| 499 | if (SIZE.equals(key)) { |
| 500 | int size = mRoundedDefault; |
| 501 | int sizeTop = mRoundedDefaultTop; |
| 502 | int sizeBottom = mRoundedDefaultBottom; |
| 503 | if (newValue != null) { |
| 504 | try { |
| 505 | size = (int) (Integer.parseInt(newValue) * mDensity); |
| 506 | } catch (Exception e) { |
| 507 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 508 | } |
Adrian Roos | 7294064 | 2018-08-09 15:23:56 +0000 | [diff] [blame] | 509 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 510 | if (sizeTop == 0) { |
| 511 | sizeTop = size; |
| 512 | } |
| 513 | if (sizeBottom == 0) { |
| 514 | sizeBottom = size; |
| 515 | } |
Adrian Roos | 7294064 | 2018-08-09 15:23:56 +0000 | [diff] [blame] | 516 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 517 | setSize(mOverlay.findViewById(R.id.left), sizeTop); |
| 518 | setSize(mOverlay.findViewById(R.id.right), sizeTop); |
| 519 | setSize(mBottomOverlay.findViewById(R.id.left), sizeBottom); |
| 520 | setSize(mBottomOverlay.findViewById(R.id.right), sizeBottom); |
| 521 | } |
| 522 | }); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | private void setSize(View view, int pixelSize) { |
| 526 | LayoutParams params = view.getLayoutParams(); |
| 527 | params.width = pixelSize; |
| 528 | params.height = pixelSize; |
| 529 | view.setLayoutParams(params); |
| 530 | } |
| 531 | |
| 532 | @VisibleForTesting |
| 533 | static class TunablePaddingTagListener implements FragmentListener { |
| 534 | |
| 535 | private final int mPadding; |
| 536 | private final int mId; |
| 537 | private TunablePadding mTunablePadding; |
| 538 | |
| 539 | public TunablePaddingTagListener(int padding, int id) { |
| 540 | mPadding = padding; |
| 541 | mId = id; |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public void onFragmentViewCreated(String tag, Fragment fragment) { |
| 546 | if (mTunablePadding != null) { |
| 547 | mTunablePadding.destroy(); |
| 548 | } |
| 549 | View view = fragment.getView(); |
| 550 | if (mId != 0) { |
| 551 | view = view.findViewById(mId); |
| 552 | } |
| 553 | mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding, |
| 554 | FLAG_START | FLAG_END); |
| 555 | } |
| 556 | } |
| 557 | |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 558 | public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener, |
| 559 | RegionInterceptableView { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 560 | |
| 561 | private final DisplayInfo mInfo = new DisplayInfo(); |
| 562 | private final Paint mPaint = new Paint(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 563 | private final List<Rect> mBounds = new ArrayList(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 564 | private final Rect mBoundingRect = new Rect(); |
| 565 | private final Path mBoundingPath = new Path(); |
| 566 | private final int[] mLocation = new int[2]; |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 567 | private final boolean mInitialStart; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 568 | private final Runnable mVisibilityChangedListener; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 569 | private final ScreenDecorations mDecorations; |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 570 | private int mColor = Color.BLACK; |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 571 | private boolean mStart; |
| 572 | private int mRotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 573 | |
| 574 | public DisplayCutoutView(Context context, boolean start, |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 575 | Runnable visibilityChangedListener, ScreenDecorations decorations) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 576 | super(context); |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 577 | mInitialStart = start; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 578 | mVisibilityChangedListener = visibilityChangedListener; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 579 | mDecorations = decorations; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 580 | setId(R.id.display_cutout); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 581 | if (DEBUG) { |
| 582 | getViewTreeObserver().addOnDrawListener(() -> Log.i(TAG, |
| 583 | (mInitialStart ? "OverlayTop" : "OverlayBottom") |
| 584 | + " drawn in rot " + mRotation)); |
| 585 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 588 | public void setColor(int color) { |
| 589 | mColor = color; |
| 590 | invalidate(); |
| 591 | } |
| 592 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 593 | @Override |
| 594 | protected void onAttachedToWindow() { |
| 595 | super.onAttachedToWindow(); |
| 596 | mContext.getSystemService(DisplayManager.class).registerDisplayListener(this, |
| 597 | getHandler()); |
| 598 | update(); |
| 599 | } |
| 600 | |
| 601 | @Override |
| 602 | protected void onDetachedFromWindow() { |
| 603 | super.onDetachedFromWindow(); |
| 604 | mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this); |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | protected void onDraw(Canvas canvas) { |
| 609 | super.onDraw(canvas); |
| 610 | getLocationOnScreen(mLocation); |
| 611 | canvas.translate(-mLocation[0], -mLocation[1]); |
| 612 | if (!mBoundingPath.isEmpty()) { |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 613 | mPaint.setColor(mColor); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 614 | mPaint.setStyle(Paint.Style.FILL); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 615 | mPaint.setAntiAlias(true); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 616 | canvas.drawPath(mBoundingPath, mPaint); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | @Override |
| 621 | public void onDisplayAdded(int displayId) { |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public void onDisplayRemoved(int displayId) { |
| 626 | } |
| 627 | |
| 628 | @Override |
| 629 | public void onDisplayChanged(int displayId) { |
| 630 | if (displayId == getDisplay().getDisplayId()) { |
| 631 | update(); |
| 632 | } |
| 633 | } |
| 634 | |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 635 | public void setRotation(int rotation) { |
| 636 | mRotation = rotation; |
| 637 | update(); |
| 638 | } |
| 639 | |
| 640 | private boolean isStart() { |
| 641 | final boolean flipped = (mRotation == RotationUtils.ROTATION_SEASCAPE |
| 642 | || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN); |
| 643 | return flipped ? !mInitialStart : mInitialStart; |
| 644 | } |
| 645 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 646 | private void update() { |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 647 | if (!isAttachedToWindow() || mDecorations.mPendingRotationChange) { |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 648 | return; |
| 649 | } |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 650 | mStart = isStart(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 651 | requestLayout(); |
| 652 | getDisplay().getDisplayInfo(mInfo); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 653 | mBounds.clear(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 654 | mBoundingRect.setEmpty(); |
| 655 | mBoundingPath.reset(); |
| 656 | int newVisible; |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 657 | if (shouldDrawCutout(getContext()) && hasCutout()) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 658 | mBounds.addAll(mInfo.displayCutout.getBoundingRects()); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 659 | localBounds(mBoundingRect); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 660 | updateBoundingPath(); |
Adrian Roos | 1b02828 | 2018-03-14 14:43:03 +0100 | [diff] [blame] | 661 | invalidate(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 662 | newVisible = VISIBLE; |
| 663 | } else { |
| 664 | newVisible = GONE; |
| 665 | } |
| 666 | if (newVisible != getVisibility()) { |
| 667 | setVisibility(newVisible); |
| 668 | mVisibilityChangedListener.run(); |
| 669 | } |
| 670 | } |
| 671 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 672 | private void updateBoundingPath() { |
| 673 | int lw = mInfo.logicalWidth; |
| 674 | int lh = mInfo.logicalHeight; |
| 675 | |
| 676 | boolean flipped = mInfo.rotation == ROTATION_90 || mInfo.rotation == ROTATION_270; |
| 677 | |
| 678 | int dw = flipped ? lh : lw; |
| 679 | int dh = flipped ? lw : lh; |
| 680 | |
Adrian Roos | a99f5d6 | 2018-04-16 16:03:04 +0200 | [diff] [blame] | 681 | mBoundingPath.set(DisplayCutout.pathFromResources(getResources(), dw, dh)); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 682 | Matrix m = new Matrix(); |
| 683 | transformPhysicalToLogicalCoordinates(mInfo.rotation, dw, dh, m); |
| 684 | mBoundingPath.transform(m); |
| 685 | } |
| 686 | |
| 687 | private static void transformPhysicalToLogicalCoordinates(@Surface.Rotation int rotation, |
| 688 | @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) { |
| 689 | switch (rotation) { |
| 690 | case ROTATION_0: |
| 691 | out.reset(); |
| 692 | break; |
| 693 | case ROTATION_90: |
| 694 | out.setRotate(270); |
| 695 | out.postTranslate(0, physicalWidth); |
| 696 | break; |
| 697 | case ROTATION_180: |
| 698 | out.setRotate(180); |
| 699 | out.postTranslate(physicalWidth, physicalHeight); |
| 700 | break; |
| 701 | case ROTATION_270: |
| 702 | out.setRotate(90); |
| 703 | out.postTranslate(physicalHeight, 0); |
| 704 | break; |
| 705 | default: |
| 706 | throw new IllegalArgumentException("Unknown rotation: " + rotation); |
| 707 | } |
| 708 | } |
| 709 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 710 | private boolean hasCutout() { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 711 | final DisplayCutout displayCutout = mInfo.displayCutout; |
| 712 | if (displayCutout == null) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 713 | return false; |
| 714 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 715 | if (mStart) { |
| 716 | return displayCutout.getSafeInsetLeft() > 0 |
| 717 | || displayCutout.getSafeInsetTop() > 0; |
| 718 | } else { |
| 719 | return displayCutout.getSafeInsetRight() > 0 |
| 720 | || displayCutout.getSafeInsetBottom() > 0; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | @Override |
| 725 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 726 | if (mBounds.isEmpty()) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 727 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 728 | return; |
| 729 | } |
| 730 | setMeasuredDimension( |
| 731 | resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0), |
| 732 | resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0)); |
| 733 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 734 | |
Jorim Jaggi | 71aaa40 | 2018-06-06 17:22:56 +0200 | [diff] [blame] | 735 | public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, |
| 736 | Rect out) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 737 | switch (gravity) { |
| 738 | case Gravity.TOP: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 739 | out.set(displayCutout.getBoundingRectTop()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 740 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 741 | case Gravity.LEFT: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 742 | out.set(displayCutout.getBoundingRectLeft()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 743 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 744 | case Gravity.BOTTOM: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 745 | out.set(displayCutout.getBoundingRectBottom()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 746 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 747 | case Gravity.RIGHT: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 748 | out.set(displayCutout.getBoundingRectRight()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 749 | break; |
| 750 | default: |
| 751 | out.setEmpty(); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 752 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | private void localBounds(Rect out) { |
| 756 | final DisplayCutout displayCutout = mInfo.displayCutout; |
| 757 | |
| 758 | if (mStart) { |
| 759 | if (displayCutout.getSafeInsetLeft() > 0) { |
| 760 | boundsFromDirection(displayCutout, Gravity.LEFT, out); |
| 761 | } else if (displayCutout.getSafeInsetTop() > 0) { |
| 762 | boundsFromDirection(displayCutout, Gravity.TOP, out); |
| 763 | } |
| 764 | } else { |
| 765 | if (displayCutout.getSafeInsetRight() > 0) { |
| 766 | boundsFromDirection(displayCutout, Gravity.RIGHT, out); |
| 767 | } else if (displayCutout.getSafeInsetBottom() > 0) { |
| 768 | boundsFromDirection(displayCutout, Gravity.BOTTOM, out); |
| 769 | } |
| 770 | } |
| 771 | } |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 772 | |
| 773 | @Override |
| 774 | public boolean shouldInterceptTouch() { |
| 775 | return mInfo.displayCutout != null && getVisibility() == VISIBLE; |
| 776 | } |
| 777 | |
| 778 | @Override |
| 779 | public Region getInterceptRegion() { |
| 780 | if (mInfo.displayCutout == null) { |
| 781 | return null; |
| 782 | } |
| 783 | |
Adrian Roos | 134e1cb | 2018-05-16 17:04:29 +0200 | [diff] [blame] | 784 | View rootView = getRootView(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 785 | Region cutoutBounds = rectsToRegion( |
| 786 | mInfo.displayCutout.getBoundingRects()); |
Adrian Roos | 134e1cb | 2018-05-16 17:04:29 +0200 | [diff] [blame] | 787 | |
| 788 | // Transform to window's coordinate space |
| 789 | rootView.getLocationOnScreen(mLocation); |
| 790 | cutoutBounds.translate(-mLocation[0], -mLocation[1]); |
| 791 | |
| 792 | // Intersect with window's frame |
| 793 | cutoutBounds.op(rootView.getLeft(), rootView.getTop(), rootView.getRight(), |
| 794 | rootView.getBottom(), Region.Op.INTERSECT); |
| 795 | |
| 796 | return cutoutBounds; |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 797 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 798 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 799 | |
| 800 | private boolean isLandscape(int rotation) { |
| 801 | return rotation == RotationUtils.ROTATION_LANDSCAPE || rotation == |
| 802 | RotationUtils.ROTATION_SEASCAPE; |
| 803 | } |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 804 | |
| 805 | /** |
| 806 | * A pre-draw listener, that cancels the draw and restarts the traversal with the updated |
| 807 | * window attributes. |
| 808 | */ |
| 809 | private class RestartingPreDrawListener implements ViewTreeObserver.OnPreDrawListener { |
| 810 | |
| 811 | private final View mView; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 812 | private final int mTargetRotation; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 813 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 814 | private RestartingPreDrawListener(View view, int targetRotation) { |
| 815 | mView = view; |
| 816 | mTargetRotation = targetRotation; |
| 817 | } |
| 818 | |
| 819 | @Override |
| 820 | public boolean onPreDraw() { |
| 821 | mView.getViewTreeObserver().removeOnPreDrawListener(this); |
| 822 | |
| 823 | if (mTargetRotation == mRotation) { |
| 824 | if (DEBUG) { |
| 825 | Log.i(TAG, (mView == mOverlay ? "OverlayTop" : "OverlayBottom") |
| 826 | + " already in target rot " |
| 827 | + mTargetRotation + ", allow draw without restarting it"); |
| 828 | } |
| 829 | return true; |
| 830 | } |
| 831 | |
| 832 | mPendingRotationChange = false; |
| 833 | // This changes the window attributes - we need to restart the traversal for them to |
| 834 | // take effect. |
| 835 | updateOrientation(); |
| 836 | if (DEBUG) { |
| 837 | Log.i(TAG, (mView == mOverlay ? "OverlayTop" : "OverlayBottom") |
| 838 | + " restarting listener fired, restarting draw for rot " + mRotation); |
| 839 | } |
| 840 | mView.invalidate(); |
| 841 | return false; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * A pre-draw listener, that validates that the rotation we draw in matches the displays |
| 847 | * rotation before continuing the draw. |
| 848 | * |
| 849 | * This is to prevent a race condition, where we have not received the display changed event |
| 850 | * yet, and would thus draw in an old orientation. |
| 851 | */ |
| 852 | private class ValidatingPreDrawListener implements ViewTreeObserver.OnPreDrawListener { |
| 853 | |
| 854 | private final View mView; |
| 855 | |
| 856 | public ValidatingPreDrawListener(View view) { |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 857 | mView = view; |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | @Override |
| 861 | public boolean onPreDraw() { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 862 | final int displayRotation = RotationUtils.getExactRotation(mContext); |
| 863 | if (displayRotation != mRotation && !mPendingRotationChange) { |
| 864 | if (DEBUG) { |
| 865 | Log.i(TAG, "Drawing rot " + mRotation + ", but display is at rot " |
| 866 | + displayRotation + ". Restarting draw"); |
| 867 | } |
| 868 | mView.invalidate(); |
| 869 | return false; |
| 870 | } |
| 871 | return true; |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 872 | } |
| 873 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 874 | } |