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