Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 17 | package com.android.systemui.statusbar.phone; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 19 | import static android.content.res.Configuration.ORIENTATION_PORTRAIT; |
| 20 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 21 | import static com.android.systemui.ScreenDecorations.DisplayCutoutView.boundsFromDirection; |
| 22 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 23 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 25 | import android.content.res.Configuration; |
Adrian Roos | 1383605 | 2018-03-15 21:06:37 +0100 | [diff] [blame] | 26 | import android.graphics.Point; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 27 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Chris Wren | 64161cc | 2012-12-17 16:49:30 -0500 | [diff] [blame] | 29 | import android.util.EventLog; |
Adrian Roos | 1383605 | 2018-03-15 21:06:37 +0100 | [diff] [blame] | 30 | import android.util.Pair; |
| 31 | import android.view.Display; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 32 | import android.view.DisplayCutout; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 33 | import android.view.Gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.view.MotionEvent; |
| 35 | import android.view.View; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 36 | import android.view.ViewGroup; |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 37 | import android.view.WindowInsets; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 38 | import android.view.accessibility.AccessibilityEvent; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 39 | import android.widget.FrameLayout; |
| 40 | import android.widget.LinearLayout; |
Adrian Roos | 1383605 | 2018-03-15 21:06:37 +0100 | [diff] [blame] | 41 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 42 | import com.android.systemui.Dependency; |
Chris Wren | 64161cc | 2012-12-17 16:49:30 -0500 | [diff] [blame] | 43 | import com.android.systemui.EventLogTags; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 44 | import com.android.systemui.R; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 45 | import com.android.systemui.statusbar.policy.DarkIconDispatcher; |
| 46 | import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver; |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 47 | |
| 48 | import java.util.Objects; |
Joe Onorato | 79de0c5 | 2010-05-26 17:03:26 -0400 | [diff] [blame] | 49 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 50 | public class PhoneStatusBarView extends PanelBar { |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 51 | private static final String TAG = "PhoneStatusBarView"; |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 52 | private static final boolean DEBUG = StatusBar.DEBUG; |
Christoph Studer | 934025e | 2014-11-25 12:53:55 +0100 | [diff] [blame] | 53 | private static final boolean DEBUG_GESTURES = false; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 54 | private static final int NO_VALUE = Integer.MIN_VALUE; |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 55 | |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 56 | StatusBar mBar; |
Daniel Sandler | 5a8aefa | 2012-09-25 01:21:12 -0400 | [diff] [blame] | 57 | |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 58 | boolean mIsFullyOpenedPanel = false; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 59 | private final PhoneStatusBarTransitions mBarTransitions; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 60 | private ScrimController mScrimController; |
Selim Cinek | 3d395c6 | 2015-06-16 19:37:37 -0700 | [diff] [blame] | 61 | private float mMinFraction; |
| 62 | private float mPanelFraction; |
Selim Cinek | 80c2abe | 2015-06-17 15:37:30 -0700 | [diff] [blame] | 63 | private Runnable mHideExpandedRunnable = new Runnable() { |
| 64 | @Override |
| 65 | public void run() { |
Selim Cinek | 529c532 | 2016-04-06 20:03:45 -0700 | [diff] [blame] | 66 | if (mPanelFraction == 0.0f) { |
| 67 | mBar.makeExpandedInvisible(); |
| 68 | } |
Selim Cinek | 80c2abe | 2015-06-17 15:37:30 -0700 | [diff] [blame] | 69 | } |
| 70 | }; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 71 | private DarkReceiver mBattery; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 72 | private int mLastOrientation; |
Evan Laird | cda685d | 2018-01-22 18:25:17 -0500 | [diff] [blame] | 73 | @Nullable |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 74 | private View mCutoutSpace; |
| 75 | @Nullable |
| 76 | private DisplayCutout mDisplayCutout; |
Joe Onorato | 119a401 | 2010-06-30 14:49:51 -0400 | [diff] [blame] | 77 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 78 | public PhoneStatusBarView(Context context, AttributeSet attrs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | super(context, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 81 | mBarTransitions = new PhoneStatusBarTransitions(this); |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | public BarTransitions getBarTransitions() { |
| 85 | return mBarTransitions; |
Daniel Sandler | 1c1edaa | 2012-08-14 11:14:45 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 88 | public void setBar(StatusBar bar) { |
Daniel Sandler | efb0faf | 2012-10-10 14:15:34 -0700 | [diff] [blame] | 89 | mBar = bar; |
| 90 | } |
| 91 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 92 | public void setScrimController(ScrimController scrimController) { |
| 93 | mScrimController = scrimController; |
| 94 | } |
| 95 | |
Daniel Sandler | efb0faf | 2012-10-10 14:15:34 -0700 | [diff] [blame] | 96 | @Override |
Jorim Jaggi | acea100 | 2014-05-10 01:30:10 +0200 | [diff] [blame] | 97 | public void onFinishInflate() { |
John Spurlock | e6f0a71 | 2013-09-03 16:23:49 -0400 | [diff] [blame] | 98 | mBarTransitions.init(); |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 99 | mBattery = findViewById(R.id.battery); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 100 | mCutoutSpace = findViewById(R.id.cutout_space_view); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected void onAttachedToWindow() { |
| 105 | super.onAttachedToWindow(); |
| 106 | // Always have Battery meters in the status bar observe the dark/light modes. |
| 107 | Dependency.get(DarkIconDispatcher.class).addDarkReceiver(mBattery); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 108 | if (updateOrientationAndCutout(getResources().getConfiguration().orientation)) { |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 109 | updateLayoutForCutout(); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 110 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | protected void onDetachedFromWindow() { |
| 115 | super.onDetachedFromWindow(); |
| 116 | Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(mBattery); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 117 | mDisplayCutout = null; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected void onConfigurationChanged(Configuration newConfig) { |
| 122 | super.onConfigurationChanged(newConfig); |
| 123 | |
| 124 | // May trigger cutout space layout-ing |
| 125 | if (updateOrientationAndCutout(newConfig.orientation)) { |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 126 | updateLayoutForCutout(); |
| 127 | requestLayout(); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 131 | @Override |
| 132 | public WindowInsets onApplyWindowInsets(WindowInsets insets) { |
| 133 | if (updateOrientationAndCutout(mLastOrientation)) { |
| 134 | updateLayoutForCutout(); |
| 135 | requestLayout(); |
| 136 | } |
| 137 | return super.onApplyWindowInsets(insets); |
| 138 | } |
| 139 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 140 | /** |
| 141 | * |
| 142 | * @param newOrientation may pass NO_VALUE for no change |
| 143 | * @return boolean indicating if we need to update the cutout location / margins |
| 144 | */ |
| 145 | private boolean updateOrientationAndCutout(int newOrientation) { |
| 146 | boolean changed = false; |
| 147 | if (newOrientation != NO_VALUE) { |
| 148 | if (mLastOrientation != newOrientation) { |
| 149 | changed = true; |
| 150 | mLastOrientation = newOrientation; |
| 151 | } |
| 152 | } |
| 153 | |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 154 | if (!Objects.equals(getRootWindowInsets().getDisplayCutout(), mDisplayCutout)) { |
| 155 | changed = true; |
| 156 | mDisplayCutout = getRootWindowInsets().getDisplayCutout(); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return changed; |
Daniel Sandler | efb0faf | 2012-10-10 14:15:34 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Daniel Sandler | 1c1edaa | 2012-08-14 11:14:45 -0400 | [diff] [blame] | 162 | @Override |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 163 | public boolean panelEnabled() { |
John Spurlock | 9764218 | 2013-07-29 17:58:39 -0400 | [diff] [blame] | 164 | return mBar.panelsEnabled(); |
Daniel Sandler | 1e8feef | 2012-08-16 11:37:41 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 168 | public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) { |
Alan Viverette | 5b04675 | 2015-01-08 11:13:06 -0800 | [diff] [blame] | 169 | if (super.onRequestSendAccessibilityEventInternal(child, event)) { |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 170 | // The status bar is very small so augment the view that the user is touching |
| 171 | // with the content of the status bar a whole. This way an accessibility service |
| 172 | // may announce the current item as well as the entire content if appropriate. |
| 173 | AccessibilityEvent record = AccessibilityEvent.obtain(); |
| 174 | onInitializeAccessibilityEvent(record); |
| 175 | dispatchPopulateAccessibilityEvent(record); |
| 176 | event.appendRecord(record); |
| 177 | return true; |
| 178 | } |
| 179 | return false; |
| 180 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 181 | |
| 182 | @Override |
| 183 | public void onPanelPeeked() { |
| 184 | super.onPanelPeeked(); |
Jorim Jaggi | fa505a7 | 2014-04-28 20:04:11 +0200 | [diff] [blame] | 185 | mBar.makeExpandedVisible(false); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 189 | public void onPanelCollapsed() { |
| 190 | super.onPanelCollapsed(); |
Jorim Jaggi | 2580a976 | 2014-06-25 03:08:25 +0200 | [diff] [blame] | 191 | // Close the status bar in the next frame so we can show the end of the animation. |
Jorim Jaggi | 45e2d8f | 2017-05-16 14:23:19 +0200 | [diff] [blame] | 192 | post(mHideExpandedRunnable); |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 193 | mIsFullyOpenedPanel = false; |
Daniel Sandler | 8e72c9e | 2012-08-15 00:09:26 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Selim Cinek | 80c2abe | 2015-06-17 15:37:30 -0700 | [diff] [blame] | 196 | public void removePendingHideExpandedRunnables() { |
Jorim Jaggi | 45e2d8f | 2017-05-16 14:23:19 +0200 | [diff] [blame] | 197 | removeCallbacks(mHideExpandedRunnable); |
Selim Cinek | 80c2abe | 2015-06-17 15:37:30 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Daniel Sandler | 8e72c9e | 2012-08-15 00:09:26 -0400 | [diff] [blame] | 200 | @Override |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 201 | public void onPanelFullyOpened() { |
| 202 | super.onPanelFullyOpened(); |
| 203 | if (!mIsFullyOpenedPanel) { |
| 204 | mPanel.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
Casey Burkhardt | bac221f | 2012-10-03 18:13:58 -0700 | [diff] [blame] | 205 | } |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 206 | mIsFullyOpenedPanel = true; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public boolean onTouchEvent(MotionEvent event) { |
Chris Wren | 64161cc | 2012-12-17 16:49:30 -0500 | [diff] [blame] | 211 | boolean barConsumedEvent = mBar.interceptTouchEvent(event); |
| 212 | |
| 213 | if (DEBUG_GESTURES) { |
| 214 | if (event.getActionMasked() != MotionEvent.ACTION_MOVE) { |
| 215 | EventLog.writeEvent(EventLogTags.SYSUI_PANELBAR_TOUCH, |
| 216 | event.getActionMasked(), (int) event.getX(), (int) event.getY(), |
| 217 | barConsumedEvent ? 1 : 0); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return barConsumedEvent || super.onTouchEvent(event); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 225 | public void onTrackingStarted() { |
| 226 | super.onTrackingStarted(); |
Jorim Jaggi | e70d31f | 2014-04-24 22:08:30 +0200 | [diff] [blame] | 227 | mBar.onTrackingStarted(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 228 | mScrimController.onTrackingStarted(); |
Selim Cinek | 529c532 | 2016-04-06 20:03:45 -0700 | [diff] [blame] | 229 | removePendingHideExpandedRunnables(); |
Jorim Jaggi | e70d31f | 2014-04-24 22:08:30 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
Selim Cinek | dbbcfbe | 2014-10-24 17:52:35 +0200 | [diff] [blame] | 233 | public void onClosingFinished() { |
| 234 | super.onClosingFinished(); |
| 235 | mBar.onClosingFinished(); |
| 236 | } |
| 237 | |
| 238 | @Override |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 239 | public void onTrackingStopped(boolean expand) { |
| 240 | super.onTrackingStopped(expand); |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 241 | mBar.onTrackingStopped(expand); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public void onExpandingFinished() { |
| 246 | super.onExpandingFinished(); |
| 247 | mScrimController.onExpandingFinished(); |
Jorim Jaggi | e70d31f | 2014-04-24 22:08:30 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 251 | public boolean onInterceptTouchEvent(MotionEvent event) { |
| 252 | return mBar.interceptTouchEvent(event) || super.onInterceptTouchEvent(event); |
| 253 | } |
| 254 | |
| 255 | @Override |
Selim Cinek | 3d395c6 | 2015-06-16 19:37:37 -0700 | [diff] [blame] | 256 | public void panelScrimMinFractionChanged(float minFraction) { |
| 257 | if (mMinFraction != minFraction) { |
| 258 | mMinFraction = minFraction; |
| 259 | updateScrimFraction(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | @Override |
Xiaohui Chen | 9f96711 | 2016-01-07 14:14:06 -0800 | [diff] [blame] | 264 | public void panelExpansionChanged(float frac, boolean expanded) { |
| 265 | super.panelExpansionChanged(frac, expanded); |
Selim Cinek | 3d395c6 | 2015-06-16 19:37:37 -0700 | [diff] [blame] | 266 | mPanelFraction = frac; |
| 267 | updateScrimFraction(); |
Matthew Ng | 78f88d1 | 2018-01-23 12:39:55 -0800 | [diff] [blame] | 268 | if ((frac == 0 || frac == 1) && mBar.getNavigationBarView() != null) { |
| 269 | mBar.getNavigationBarView().onPanelExpandedChange(expanded); |
| 270 | } |
Selim Cinek | 3d395c6 | 2015-06-16 19:37:37 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | private void updateScrimFraction() { |
Selim Cinek | ae76b4c | 2017-03-07 16:49:43 -0800 | [diff] [blame] | 274 | float scrimFraction = mPanelFraction; |
| 275 | if (mMinFraction < 1.0f) { |
| 276 | scrimFraction = Math.max((mPanelFraction - mMinFraction) / (1.0f - mMinFraction), |
| 277 | 0); |
| 278 | } |
Selim Cinek | 3d395c6 | 2015-06-16 19:37:37 -0700 | [diff] [blame] | 279 | mScrimController.setPanelExpansion(scrimFraction); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 280 | } |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 281 | |
Adrian Roos | 22af650 | 2018-02-22 16:57:08 +0100 | [diff] [blame] | 282 | public void updateResources() { |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 283 | ViewGroup.LayoutParams layoutParams = getLayoutParams(); |
| 284 | layoutParams.height = getResources().getDimensionPixelSize( |
| 285 | R.dimen.status_bar_height); |
| 286 | setLayoutParams(layoutParams); |
| 287 | } |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 288 | |
| 289 | private void updateLayoutForCutout() { |
Adrian Roos | 1383605 | 2018-03-15 21:06:37 +0100 | [diff] [blame] | 290 | Pair<Integer, Integer> cornerCutoutMargins = cornerCutoutMargins(mDisplayCutout, |
| 291 | getDisplay()); |
| 292 | updateCutoutLocation(cornerCutoutMargins); |
| 293 | updateSafeInsets(cornerCutoutMargins); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 294 | } |
| 295 | |
Adrian Roos | 1383605 | 2018-03-15 21:06:37 +0100 | [diff] [blame] | 296 | private void updateCutoutLocation(Pair<Integer, Integer> cornerCutoutMargins) { |
Evan Laird | cda685d | 2018-01-22 18:25:17 -0500 | [diff] [blame] | 297 | // Not all layouts have a cutout (e.g., Car) |
| 298 | if (mCutoutSpace == null) { |
| 299 | return; |
| 300 | } |
| 301 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 302 | if (mDisplayCutout == null || mDisplayCutout.isEmpty() |
Adrian Roos | 1383605 | 2018-03-15 21:06:37 +0100 | [diff] [blame] | 303 | || mLastOrientation != ORIENTATION_PORTRAIT || cornerCutoutMargins != null) { |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 304 | mCutoutSpace.setVisibility(View.GONE); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | mCutoutSpace.setVisibility(View.VISIBLE); |
| 309 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mCutoutSpace.getLayoutParams(); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 310 | |
| 311 | Rect bounds = new Rect(); |
| 312 | boundsFromDirection(mDisplayCutout, Gravity.TOP, bounds); |
| 313 | |
| 314 | lp.width = bounds.width(); |
| 315 | lp.height = bounds.height(); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 316 | } |
| 317 | |
Adrian Roos | 1383605 | 2018-03-15 21:06:37 +0100 | [diff] [blame] | 318 | private void updateSafeInsets(Pair<Integer, Integer> cornerCutoutMargins) { |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 319 | // Depending on our rotation, we may have to work around a cutout in the middle of the view, |
| 320 | // or letterboxing from the right or left sides. |
| 321 | |
| 322 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 323 | if (mDisplayCutout == null) { |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 324 | lp.leftMargin = 0; |
| 325 | lp.rightMargin = 0; |
| 326 | return; |
| 327 | } |
| 328 | |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 329 | lp.leftMargin = mDisplayCutout.getSafeInsetLeft(); |
| 330 | lp.rightMargin = mDisplayCutout.getSafeInsetRight(); |
Adrian Roos | 1383605 | 2018-03-15 21:06:37 +0100 | [diff] [blame] | 331 | |
| 332 | if (cornerCutoutMargins != null) { |
| 333 | lp.leftMargin = Math.max(lp.leftMargin, cornerCutoutMargins.first); |
| 334 | lp.rightMargin = Math.max(lp.rightMargin, cornerCutoutMargins.second); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | public static Pair<Integer, Integer> cornerCutoutMargins(DisplayCutout cutout, |
| 339 | Display display) { |
| 340 | if (cutout == null) { |
| 341 | return null; |
| 342 | } |
| 343 | Point size = new Point(); |
| 344 | display.getRealSize(size); |
| 345 | |
| 346 | Rect bounds = new Rect(); |
| 347 | boundsFromDirection(cutout, Gravity.TOP, bounds); |
| 348 | |
| 349 | if (bounds.left <= 0) { |
| 350 | return new Pair<>(bounds.right, 0); |
| 351 | } |
| 352 | if (bounds.right >= size.x) { |
| 353 | return new Pair<>(0, size.x - bounds.left); |
| 354 | } |
| 355 | return null; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 356 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 357 | } |