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 | } |
| 474 | return lp; |
| 475 | } |
| 476 | |
| 477 | private WindowManager.LayoutParams getBottomLayoutParams() { |
| 478 | WindowManager.LayoutParams lp = getWindowLayoutParams(); |
| 479 | lp.setTitle("ScreenDecorOverlayBottom"); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 480 | if (mRotation == RotationUtils.ROTATION_SEASCAPE |
| 481 | || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) { |
| 482 | lp.gravity = Gravity.TOP | Gravity.LEFT; |
| 483 | } else { |
| 484 | lp.gravity = Gravity.BOTTOM | Gravity.RIGHT; |
| 485 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 486 | return lp; |
| 487 | } |
| 488 | |
| 489 | private void updateLayoutParams() { |
| 490 | mWindowManager.updateViewLayout(mOverlay, getWindowLayoutParams()); |
| 491 | mWindowManager.updateViewLayout(mBottomOverlay, getBottomLayoutParams()); |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public void onTuningChanged(String key, String newValue) { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 496 | mHandler.post(() -> { |
| 497 | if (mOverlay == null) return; |
| 498 | if (SIZE.equals(key)) { |
| 499 | int size = mRoundedDefault; |
| 500 | int sizeTop = mRoundedDefaultTop; |
| 501 | int sizeBottom = mRoundedDefaultBottom; |
| 502 | if (newValue != null) { |
| 503 | try { |
| 504 | size = (int) (Integer.parseInt(newValue) * mDensity); |
| 505 | } catch (Exception e) { |
| 506 | } |
Beverly | a5f7a30 | 2018-04-25 09:19:05 -0400 | [diff] [blame] | 507 | } |
Adrian Roos | 7294064 | 2018-08-09 15:23:56 +0000 | [diff] [blame] | 508 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 509 | if (sizeTop == 0) { |
| 510 | sizeTop = size; |
| 511 | } |
| 512 | if (sizeBottom == 0) { |
| 513 | sizeBottom = size; |
| 514 | } |
Adrian Roos | 7294064 | 2018-08-09 15:23:56 +0000 | [diff] [blame] | 515 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 516 | setSize(mOverlay.findViewById(R.id.left), sizeTop); |
| 517 | setSize(mOverlay.findViewById(R.id.right), sizeTop); |
| 518 | setSize(mBottomOverlay.findViewById(R.id.left), sizeBottom); |
| 519 | setSize(mBottomOverlay.findViewById(R.id.right), sizeBottom); |
| 520 | } |
| 521 | }); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | private void setSize(View view, int pixelSize) { |
| 525 | LayoutParams params = view.getLayoutParams(); |
| 526 | params.width = pixelSize; |
| 527 | params.height = pixelSize; |
| 528 | view.setLayoutParams(params); |
| 529 | } |
| 530 | |
| 531 | @VisibleForTesting |
| 532 | static class TunablePaddingTagListener implements FragmentListener { |
| 533 | |
| 534 | private final int mPadding; |
| 535 | private final int mId; |
| 536 | private TunablePadding mTunablePadding; |
| 537 | |
| 538 | public TunablePaddingTagListener(int padding, int id) { |
| 539 | mPadding = padding; |
| 540 | mId = id; |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void onFragmentViewCreated(String tag, Fragment fragment) { |
| 545 | if (mTunablePadding != null) { |
| 546 | mTunablePadding.destroy(); |
| 547 | } |
| 548 | View view = fragment.getView(); |
| 549 | if (mId != 0) { |
| 550 | view = view.findViewById(mId); |
| 551 | } |
| 552 | mTunablePadding = TunablePadding.addTunablePadding(view, PADDING, mPadding, |
| 553 | FLAG_START | FLAG_END); |
| 554 | } |
| 555 | } |
| 556 | |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 557 | public static class DisplayCutoutView extends View implements DisplayManager.DisplayListener, |
| 558 | RegionInterceptableView { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 559 | |
| 560 | private final DisplayInfo mInfo = new DisplayInfo(); |
| 561 | private final Paint mPaint = new Paint(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 562 | private final List<Rect> mBounds = new ArrayList(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 563 | private final Rect mBoundingRect = new Rect(); |
| 564 | private final Path mBoundingPath = new Path(); |
| 565 | private final int[] mLocation = new int[2]; |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 566 | private final boolean mInitialStart; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 567 | private final Runnable mVisibilityChangedListener; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 568 | private final ScreenDecorations mDecorations; |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 569 | private int mColor = Color.BLACK; |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 570 | private boolean mStart; |
| 571 | private int mRotation; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 572 | |
| 573 | public DisplayCutoutView(Context context, boolean start, |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 574 | Runnable visibilityChangedListener, ScreenDecorations decorations) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 575 | super(context); |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 576 | mInitialStart = start; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 577 | mVisibilityChangedListener = visibilityChangedListener; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 578 | mDecorations = decorations; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 579 | setId(R.id.display_cutout); |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 580 | if (DEBUG) { |
| 581 | getViewTreeObserver().addOnDrawListener(() -> Log.i(TAG, |
| 582 | (mInitialStart ? "OverlayTop" : "OverlayBottom") |
| 583 | + " drawn in rot " + mRotation)); |
| 584 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 587 | public void setColor(int color) { |
| 588 | mColor = color; |
| 589 | invalidate(); |
| 590 | } |
| 591 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 592 | @Override |
| 593 | protected void onAttachedToWindow() { |
| 594 | super.onAttachedToWindow(); |
| 595 | mContext.getSystemService(DisplayManager.class).registerDisplayListener(this, |
| 596 | getHandler()); |
| 597 | update(); |
| 598 | } |
| 599 | |
| 600 | @Override |
| 601 | protected void onDetachedFromWindow() { |
| 602 | super.onDetachedFromWindow(); |
| 603 | mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this); |
| 604 | } |
| 605 | |
| 606 | @Override |
| 607 | protected void onDraw(Canvas canvas) { |
| 608 | super.onDraw(canvas); |
| 609 | getLocationOnScreen(mLocation); |
| 610 | canvas.translate(-mLocation[0], -mLocation[1]); |
| 611 | if (!mBoundingPath.isEmpty()) { |
Adrian Roos | 8b29a84 | 2018-05-31 14:14:13 +0200 | [diff] [blame] | 612 | mPaint.setColor(mColor); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 613 | mPaint.setStyle(Paint.Style.FILL); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 614 | mPaint.setAntiAlias(true); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 615 | canvas.drawPath(mBoundingPath, mPaint); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public void onDisplayAdded(int displayId) { |
| 621 | } |
| 622 | |
| 623 | @Override |
| 624 | public void onDisplayRemoved(int displayId) { |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void onDisplayChanged(int displayId) { |
| 629 | if (displayId == getDisplay().getDisplayId()) { |
| 630 | update(); |
| 631 | } |
| 632 | } |
| 633 | |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 634 | public void setRotation(int rotation) { |
| 635 | mRotation = rotation; |
| 636 | update(); |
| 637 | } |
| 638 | |
| 639 | private boolean isStart() { |
| 640 | final boolean flipped = (mRotation == RotationUtils.ROTATION_SEASCAPE |
| 641 | || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN); |
| 642 | return flipped ? !mInitialStart : mInitialStart; |
| 643 | } |
| 644 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 645 | private void update() { |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 646 | if (!isAttachedToWindow() || mDecorations.mPendingRotationChange) { |
Adrian Roos | 9acf813 | 2018-05-31 19:54:53 +0200 | [diff] [blame] | 647 | return; |
| 648 | } |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 649 | mStart = isStart(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 650 | requestLayout(); |
| 651 | getDisplay().getDisplayInfo(mInfo); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 652 | mBounds.clear(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 653 | mBoundingRect.setEmpty(); |
| 654 | mBoundingPath.reset(); |
| 655 | int newVisible; |
Adrian Roos | c41b32b | 2018-04-12 10:13:48 +0200 | [diff] [blame] | 656 | if (shouldDrawCutout(getContext()) && hasCutout()) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 657 | mBounds.addAll(mInfo.displayCutout.getBoundingRects()); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 658 | localBounds(mBoundingRect); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 659 | updateBoundingPath(); |
Adrian Roos | 1b02828 | 2018-03-14 14:43:03 +0100 | [diff] [blame] | 660 | invalidate(); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 661 | newVisible = VISIBLE; |
| 662 | } else { |
| 663 | newVisible = GONE; |
| 664 | } |
| 665 | if (newVisible != getVisibility()) { |
| 666 | setVisibility(newVisible); |
| 667 | mVisibilityChangedListener.run(); |
| 668 | } |
| 669 | } |
| 670 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 671 | private void updateBoundingPath() { |
| 672 | int lw = mInfo.logicalWidth; |
| 673 | int lh = mInfo.logicalHeight; |
| 674 | |
| 675 | boolean flipped = mInfo.rotation == ROTATION_90 || mInfo.rotation == ROTATION_270; |
| 676 | |
| 677 | int dw = flipped ? lh : lw; |
| 678 | int dh = flipped ? lw : lh; |
| 679 | |
Adrian Roos | a99f5d6 | 2018-04-16 16:03:04 +0200 | [diff] [blame] | 680 | mBoundingPath.set(DisplayCutout.pathFromResources(getResources(), dw, dh)); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 681 | Matrix m = new Matrix(); |
| 682 | transformPhysicalToLogicalCoordinates(mInfo.rotation, dw, dh, m); |
| 683 | mBoundingPath.transform(m); |
| 684 | } |
| 685 | |
| 686 | private static void transformPhysicalToLogicalCoordinates(@Surface.Rotation int rotation, |
| 687 | @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) { |
| 688 | switch (rotation) { |
| 689 | case ROTATION_0: |
| 690 | out.reset(); |
| 691 | break; |
| 692 | case ROTATION_90: |
| 693 | out.setRotate(270); |
| 694 | out.postTranslate(0, physicalWidth); |
| 695 | break; |
| 696 | case ROTATION_180: |
| 697 | out.setRotate(180); |
| 698 | out.postTranslate(physicalWidth, physicalHeight); |
| 699 | break; |
| 700 | case ROTATION_270: |
| 701 | out.setRotate(90); |
| 702 | out.postTranslate(physicalHeight, 0); |
| 703 | break; |
| 704 | default: |
| 705 | throw new IllegalArgumentException("Unknown rotation: " + rotation); |
| 706 | } |
| 707 | } |
| 708 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 709 | private boolean hasCutout() { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 710 | final DisplayCutout displayCutout = mInfo.displayCutout; |
| 711 | if (displayCutout == null) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 712 | return false; |
| 713 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 714 | if (mStart) { |
| 715 | return displayCutout.getSafeInsetLeft() > 0 |
| 716 | || displayCutout.getSafeInsetTop() > 0; |
| 717 | } else { |
| 718 | return displayCutout.getSafeInsetRight() > 0 |
| 719 | || displayCutout.getSafeInsetBottom() > 0; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | @Override |
| 724 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 725 | if (mBounds.isEmpty()) { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 726 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 727 | return; |
| 728 | } |
| 729 | setMeasuredDimension( |
| 730 | resolveSizeAndState(mBoundingRect.width(), widthMeasureSpec, 0), |
| 731 | resolveSizeAndState(mBoundingRect.height(), heightMeasureSpec, 0)); |
| 732 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 733 | |
Jorim Jaggi | 71aaa40 | 2018-06-06 17:22:56 +0200 | [diff] [blame] | 734 | public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, |
| 735 | Rect out) { |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 736 | switch (gravity) { |
| 737 | case Gravity.TOP: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 738 | out.set(displayCutout.getBoundingRectTop()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 739 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 740 | case Gravity.LEFT: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 741 | out.set(displayCutout.getBoundingRectLeft()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 742 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 743 | case Gravity.BOTTOM: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 744 | out.set(displayCutout.getBoundingRectBottom()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 745 | break; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 746 | case Gravity.RIGHT: |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 747 | out.set(displayCutout.getBoundingRectRight()); |
Evan Laird | 6703f42 | 2018-10-11 13:24:05 -0400 | [diff] [blame] | 748 | break; |
| 749 | default: |
| 750 | out.setEmpty(); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 751 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | private void localBounds(Rect out) { |
| 755 | final DisplayCutout displayCutout = mInfo.displayCutout; |
| 756 | |
| 757 | if (mStart) { |
| 758 | if (displayCutout.getSafeInsetLeft() > 0) { |
| 759 | boundsFromDirection(displayCutout, Gravity.LEFT, out); |
| 760 | } else if (displayCutout.getSafeInsetTop() > 0) { |
| 761 | boundsFromDirection(displayCutout, Gravity.TOP, out); |
| 762 | } |
| 763 | } else { |
| 764 | if (displayCutout.getSafeInsetRight() > 0) { |
| 765 | boundsFromDirection(displayCutout, Gravity.RIGHT, out); |
| 766 | } else if (displayCutout.getSafeInsetBottom() > 0) { |
| 767 | boundsFromDirection(displayCutout, Gravity.BOTTOM, out); |
| 768 | } |
| 769 | } |
| 770 | } |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 771 | |
| 772 | @Override |
| 773 | public boolean shouldInterceptTouch() { |
| 774 | return mInfo.displayCutout != null && getVisibility() == VISIBLE; |
| 775 | } |
| 776 | |
| 777 | @Override |
| 778 | public Region getInterceptRegion() { |
| 779 | if (mInfo.displayCutout == null) { |
| 780 | return null; |
| 781 | } |
| 782 | |
Adrian Roos | 134e1cb | 2018-05-16 17:04:29 +0200 | [diff] [blame] | 783 | View rootView = getRootView(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 784 | Region cutoutBounds = rectsToRegion( |
| 785 | mInfo.displayCutout.getBoundingRects()); |
Adrian Roos | 134e1cb | 2018-05-16 17:04:29 +0200 | [diff] [blame] | 786 | |
| 787 | // Transform to window's coordinate space |
| 788 | rootView.getLocationOnScreen(mLocation); |
| 789 | cutoutBounds.translate(-mLocation[0], -mLocation[1]); |
| 790 | |
| 791 | // Intersect with window's frame |
| 792 | cutoutBounds.op(rootView.getLeft(), rootView.getTop(), rootView.getRight(), |
| 793 | rootView.getBottom(), Region.Op.INTERSECT); |
| 794 | |
| 795 | return cutoutBounds; |
Evan Laird | b0506ca | 2018-03-15 10:34:31 -0400 | [diff] [blame] | 796 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 797 | } |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 798 | |
| 799 | private boolean isLandscape(int rotation) { |
| 800 | return rotation == RotationUtils.ROTATION_LANDSCAPE || rotation == |
| 801 | RotationUtils.ROTATION_SEASCAPE; |
| 802 | } |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 803 | |
| 804 | /** |
| 805 | * A pre-draw listener, that cancels the draw and restarts the traversal with the updated |
| 806 | * window attributes. |
| 807 | */ |
| 808 | private class RestartingPreDrawListener implements ViewTreeObserver.OnPreDrawListener { |
| 809 | |
| 810 | private final View mView; |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 811 | private final int mTargetRotation; |
Vishnu Nair | 83537a7 | 2018-07-19 21:27:48 -0700 | [diff] [blame] | 812 | |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 813 | private RestartingPreDrawListener(View view, int targetRotation) { |
| 814 | mView = view; |
| 815 | mTargetRotation = targetRotation; |
| 816 | } |
| 817 | |
| 818 | @Override |
| 819 | public boolean onPreDraw() { |
| 820 | mView.getViewTreeObserver().removeOnPreDrawListener(this); |
| 821 | |
| 822 | if (mTargetRotation == mRotation) { |
| 823 | if (DEBUG) { |
| 824 | Log.i(TAG, (mView == mOverlay ? "OverlayTop" : "OverlayBottom") |
| 825 | + " already in target rot " |
| 826 | + mTargetRotation + ", allow draw without restarting it"); |
| 827 | } |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | mPendingRotationChange = false; |
| 832 | // This changes the window attributes - we need to restart the traversal for them to |
| 833 | // take effect. |
| 834 | updateOrientation(); |
| 835 | if (DEBUG) { |
| 836 | Log.i(TAG, (mView == mOverlay ? "OverlayTop" : "OverlayBottom") |
| 837 | + " restarting listener fired, restarting draw for rot " + mRotation); |
| 838 | } |
| 839 | mView.invalidate(); |
| 840 | return false; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * A pre-draw listener, that validates that the rotation we draw in matches the displays |
| 846 | * rotation before continuing the draw. |
| 847 | * |
| 848 | * This is to prevent a race condition, where we have not received the display changed event |
| 849 | * yet, and would thus draw in an old orientation. |
| 850 | */ |
| 851 | private class ValidatingPreDrawListener implements ViewTreeObserver.OnPreDrawListener { |
| 852 | |
| 853 | private final View mView; |
| 854 | |
| 855 | public ValidatingPreDrawListener(View view) { |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 856 | mView = view; |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | @Override |
| 860 | public boolean onPreDraw() { |
Adrian Roos | faa102f | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 861 | final int displayRotation = RotationUtils.getExactRotation(mContext); |
| 862 | if (displayRotation != mRotation && !mPendingRotationChange) { |
| 863 | if (DEBUG) { |
| 864 | Log.i(TAG, "Drawing rot " + mRotation + ", but display is at rot " |
| 865 | + displayRotation + ". Restarting draw"); |
| 866 | } |
| 867 | mView.invalidate(); |
| 868 | return false; |
| 869 | } |
| 870 | return true; |
Adrian Roos | 61f557a | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 871 | } |
| 872 | } |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 873 | } |