Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
felkachang | 3d00f35 | 2018-05-22 12:53:50 +0800 | [diff] [blame] | 19 | import android.graphics.Point; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 20 | import android.graphics.Rect; |
felkachang | 7749c9a | 2018-06-11 15:56:15 +0800 | [diff] [blame] | 21 | import android.view.DisplayCutout; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 22 | import android.view.View; |
felkachang | 3d00f35 | 2018-05-22 12:53:50 +0800 | [diff] [blame] | 23 | import android.view.WindowInsets; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 24 | |
| 25 | import com.android.internal.annotations.VisibleForTesting; |
| 26 | import com.android.systemui.Dependency; |
| 27 | import com.android.systemui.R; |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 28 | import com.android.systemui.statusbar.CrossFadeHelper; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 29 | import com.android.systemui.statusbar.ExpandableNotificationRow; |
| 30 | import com.android.systemui.statusbar.HeadsUpStatusBarView; |
| 31 | import com.android.systemui.statusbar.NotificationData; |
| 32 | import com.android.systemui.statusbar.policy.DarkIconDispatcher; |
| 33 | import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; |
| 34 | import com.android.systemui.statusbar.stack.NotificationStackScrollLayout; |
| 35 | |
Selim Cinek | 60ffea6 | 2018-03-22 13:16:44 -0700 | [diff] [blame] | 36 | import java.util.function.BiConsumer; |
| 37 | import java.util.function.Consumer; |
| 38 | |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Controls the appearance of heads up notifications in the icon area and the header itself. |
| 41 | */ |
Selim Cinek | f0c79e1 | 2018-05-14 17:17:31 -0700 | [diff] [blame] | 42 | public class HeadsUpAppearanceController implements OnHeadsUpChangedListener, |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 43 | DarkIconDispatcher.DarkReceiver { |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 44 | public static final int CONTENT_FADE_DURATION = 110; |
| 45 | public static final int CONTENT_FADE_DELAY = 100; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 46 | private final NotificationIconAreaController mNotificationIconAreaController; |
| 47 | private final HeadsUpManagerPhone mHeadsUpManager; |
| 48 | private final NotificationStackScrollLayout mStackScroller; |
| 49 | private final HeadsUpStatusBarView mHeadsUpStatusBarView; |
| 50 | private final View mClockView; |
| 51 | private final DarkIconDispatcher mDarkIconDispatcher; |
Selim Cinek | 60ffea6 | 2018-03-22 13:16:44 -0700 | [diff] [blame] | 52 | private final NotificationPanelView mPanelView; |
| 53 | private final Consumer<ExpandableNotificationRow> |
| 54 | mSetTrackingHeadsUp = this::setTrackingHeadsUp; |
| 55 | private final Runnable mUpdatePanelTranslation = this::updatePanelTranslation; |
| 56 | private final BiConsumer<Float, Float> mSetExpandedHeight = this::setExpandedHeight; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 57 | private float mExpandedHeight; |
| 58 | private boolean mIsExpanded; |
| 59 | private float mExpandFraction; |
| 60 | private ExpandableNotificationRow mTrackedChild; |
| 61 | private boolean mShown; |
Selim Cinek | 60ffea6 | 2018-03-22 13:16:44 -0700 | [diff] [blame] | 62 | private final View.OnLayoutChangeListener mStackScrollLayoutChangeListener = |
| 63 | (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) |
| 64 | -> updatePanelTranslation(); |
felkachang | 3d00f35 | 2018-05-22 12:53:50 +0800 | [diff] [blame] | 65 | Point mPoint; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 66 | |
| 67 | public HeadsUpAppearanceController( |
| 68 | NotificationIconAreaController notificationIconAreaController, |
| 69 | HeadsUpManagerPhone headsUpManager, |
| 70 | View statusbarView) { |
| 71 | this(notificationIconAreaController, headsUpManager, |
| 72 | statusbarView.findViewById(R.id.heads_up_status_bar_view), |
| 73 | statusbarView.findViewById(R.id.notification_stack_scroller), |
| 74 | statusbarView.findViewById(R.id.notification_panel), |
| 75 | statusbarView.findViewById(R.id.clock)); |
| 76 | } |
| 77 | |
| 78 | @VisibleForTesting |
| 79 | public HeadsUpAppearanceController( |
| 80 | NotificationIconAreaController notificationIconAreaController, |
| 81 | HeadsUpManagerPhone headsUpManager, |
| 82 | HeadsUpStatusBarView headsUpStatusBarView, |
| 83 | NotificationStackScrollLayout stackScroller, |
| 84 | NotificationPanelView panelView, |
| 85 | View clockView) { |
| 86 | mNotificationIconAreaController = notificationIconAreaController; |
| 87 | mHeadsUpManager = headsUpManager; |
| 88 | mHeadsUpManager.addListener(this); |
| 89 | mHeadsUpStatusBarView = headsUpStatusBarView; |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 90 | headsUpStatusBarView.setOnDrawingRectChangedListener( |
| 91 | () -> updateIsolatedIconLocation(true /* requireUpdate */)); |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 92 | mStackScroller = stackScroller; |
Selim Cinek | 60ffea6 | 2018-03-22 13:16:44 -0700 | [diff] [blame] | 93 | mPanelView = panelView; |
| 94 | panelView.addTrackingHeadsUpListener(mSetTrackingHeadsUp); |
| 95 | panelView.addVerticalTranslationListener(mUpdatePanelTranslation); |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 96 | panelView.setHeadsUpAppearanceController(this); |
Selim Cinek | 60ffea6 | 2018-03-22 13:16:44 -0700 | [diff] [blame] | 97 | mStackScroller.addOnExpandedHeightListener(mSetExpandedHeight); |
| 98 | mStackScroller.addOnLayoutChangeListener(mStackScrollLayoutChangeListener); |
Selim Cinek | f0c79e1 | 2018-05-14 17:17:31 -0700 | [diff] [blame] | 99 | mStackScroller.setHeadsUpAppearanceController(this); |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 100 | mClockView = clockView; |
| 101 | mDarkIconDispatcher = Dependency.get(DarkIconDispatcher.class); |
| 102 | mDarkIconDispatcher.addDarkReceiver(this); |
| 103 | } |
| 104 | |
Selim Cinek | 60ffea6 | 2018-03-22 13:16:44 -0700 | [diff] [blame] | 105 | |
| 106 | public void destroy() { |
| 107 | mHeadsUpManager.removeListener(this); |
| 108 | mHeadsUpStatusBarView.setOnDrawingRectChangedListener(null); |
| 109 | mPanelView.removeTrackingHeadsUpListener(mSetTrackingHeadsUp); |
| 110 | mPanelView.removeVerticalTranslationListener(mUpdatePanelTranslation); |
| 111 | mPanelView.setHeadsUpAppearanceController(null); |
| 112 | mStackScroller.removeOnExpandedHeightListener(mSetExpandedHeight); |
| 113 | mStackScroller.removeOnLayoutChangeListener(mStackScrollLayoutChangeListener); |
| 114 | mDarkIconDispatcher.removeDarkReceiver(this); |
| 115 | } |
| 116 | |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 117 | private void updateIsolatedIconLocation(boolean requireStateUpdate) { |
| 118 | mNotificationIconAreaController.setIsolatedIconLocation( |
| 119 | mHeadsUpStatusBarView.getIconDrawingRect(), requireStateUpdate); |
| 120 | } |
| 121 | |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 122 | @Override |
| 123 | public void onHeadsUpPinned(ExpandableNotificationRow headsUp) { |
| 124 | updateTopEntry(); |
| 125 | updateHeader(headsUp.getEntry()); |
| 126 | } |
| 127 | |
felkachang | 3d00f35 | 2018-05-22 12:53:50 +0800 | [diff] [blame] | 128 | /** To count the distance from the window right boundary to scroller right boundary. The |
| 129 | * distance formula is the following: |
| 130 | * Y = screenSize - (SystemWindow's width + Scroller.getRight()) |
| 131 | * There are four modes MUST to be considered in Cut Out of RTL. |
| 132 | * No Cut Out: |
| 133 | * Scroller + NB |
| 134 | * NB + Scroller |
| 135 | * => SystemWindow = NavigationBar's width |
| 136 | * => Y = screenSize - (SystemWindow's width + Scroller.getRight()) |
| 137 | * Corner Cut Out or Tall Cut Out: |
| 138 | * cut out + Scroller + NB |
| 139 | * NB + Scroller + cut out |
| 140 | * => SystemWindow = NavigationBar's width |
| 141 | * => Y = screenSize - (SystemWindow's width + Scroller.getRight()) |
| 142 | * Double Cut Out: |
| 143 | * cut out left + Scroller + (NB + cut out right) |
| 144 | * SystemWindow = NavigationBar's width + cut out right width |
| 145 | * => Y = screenSize - (SystemWindow's width + Scroller.getRight()) |
| 146 | * (cut out left + NB) + Scroller + cut out right |
| 147 | * SystemWindow = NavigationBar's width + cut out left width |
| 148 | * => Y = screenSize - (SystemWindow's width + Scroller.getRight()) |
| 149 | * @return the translation X value for RTL. In theory, it should be negative. i.e. -Y |
| 150 | */ |
| 151 | private int getRtlTranslation() { |
felkachang | 3d00f35 | 2018-05-22 12:53:50 +0800 | [diff] [blame] | 152 | if (mPoint == null) { |
| 153 | mPoint = new Point(); |
| 154 | } |
| 155 | |
| 156 | int realDisplaySize = 0; |
| 157 | if (mStackScroller.getDisplay() != null) { |
| 158 | mStackScroller.getDisplay().getRealSize(mPoint); |
| 159 | realDisplaySize = mPoint.x; |
| 160 | } |
| 161 | |
| 162 | WindowInsets windowInset = mStackScroller.getRootWindowInsets(); |
felkachang | 7749c9a | 2018-06-11 15:56:15 +0800 | [diff] [blame] | 163 | DisplayCutout cutout = (windowInset != null) ? windowInset.getDisplayCutout() : null; |
| 164 | int sysWinLeft = (windowInset != null) ? windowInset.getStableInsetLeft() : 0; |
| 165 | int sysWinRight = (windowInset != null) ? windowInset.getStableInsetRight() : 0; |
| 166 | int cutoutLeft = (cutout != null) ? cutout.getSafeInsetLeft() : 0; |
| 167 | int cutoutRight = (cutout != null) ? cutout.getSafeInsetRight() : 0; |
| 168 | int leftInset = Math.max(sysWinLeft, cutoutLeft); |
| 169 | int rightInset = Math.max(sysWinRight, cutoutRight); |
| 170 | |
| 171 | return leftInset + mStackScroller.getRight() + rightInset - realDisplaySize; |
felkachang | 3d00f35 | 2018-05-22 12:53:50 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 174 | public void updatePanelTranslation() { |
felkachang | 3d00f35 | 2018-05-22 12:53:50 +0800 | [diff] [blame] | 175 | float newTranslation; |
| 176 | if (mStackScroller.isLayoutRtl()) { |
| 177 | newTranslation = getRtlTranslation(); |
| 178 | } else { |
| 179 | newTranslation = mStackScroller.getLeft(); |
| 180 | } |
| 181 | newTranslation += mStackScroller.getTranslationX(); |
felkachang | e8a3536 | 2018-05-18 20:11:38 +0800 | [diff] [blame] | 182 | mHeadsUpStatusBarView.setPanelTranslation(newTranslation); |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 185 | private void updateTopEntry() { |
| 186 | NotificationData.Entry newEntry = null; |
| 187 | if (!mIsExpanded && mHeadsUpManager.hasPinnedHeadsUp()) { |
| 188 | newEntry = mHeadsUpManager.getTopEntry(); |
| 189 | } |
| 190 | NotificationData.Entry previousEntry = mHeadsUpStatusBarView.getShowingEntry(); |
| 191 | mHeadsUpStatusBarView.setEntry(newEntry); |
| 192 | if (newEntry != previousEntry) { |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 193 | boolean animateIsolation = false; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 194 | if (newEntry == null) { |
| 195 | // no heads up anymore, lets start the disappear animation |
| 196 | |
| 197 | setShown(false); |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 198 | animateIsolation = !mIsExpanded; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 199 | } else if (previousEntry == null) { |
| 200 | // We now have a headsUp and didn't have one before. Let's start the disappear |
| 201 | // animation |
| 202 | setShown(true); |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 203 | animateIsolation = !mIsExpanded; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 204 | } |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 205 | updateIsolatedIconLocation(false /* requireUpdate */); |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 206 | mNotificationIconAreaController.showIconIsolated(newEntry == null ? null |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 207 | : newEntry.icon, animateIsolation); |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | private void setShown(boolean isShown) { |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 212 | if (mShown != isShown) { |
| 213 | mShown = isShown; |
| 214 | if (isShown) { |
| 215 | mHeadsUpStatusBarView.setVisibility(View.VISIBLE); |
| 216 | CrossFadeHelper.fadeIn(mHeadsUpStatusBarView, CONTENT_FADE_DURATION /* duration */, |
| 217 | CONTENT_FADE_DELAY /* delay */); |
| 218 | CrossFadeHelper.fadeOut(mClockView, CONTENT_FADE_DURATION/* duration */, |
| 219 | 0 /* delay */, () -> mClockView.setVisibility(View.INVISIBLE)); |
| 220 | } else { |
| 221 | CrossFadeHelper.fadeIn(mClockView, CONTENT_FADE_DURATION /* duration */, |
| 222 | CONTENT_FADE_DELAY /* delay */); |
| 223 | CrossFadeHelper.fadeOut(mHeadsUpStatusBarView, CONTENT_FADE_DURATION/* duration */, |
| 224 | 0 /* delay */, () -> mHeadsUpStatusBarView.setVisibility(View.GONE)); |
| 225 | |
| 226 | } |
| 227 | } |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @VisibleForTesting |
| 231 | public boolean isShown() { |
| 232 | return mShown; |
| 233 | } |
| 234 | |
Selim Cinek | 332c23f | 2018-03-16 17:37:50 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Should the headsup status bar view be visible right now? This may be different from isShown, |
| 237 | * since the headsUp manager might not have notified us yet of the state change. |
| 238 | * |
| 239 | * @return if the heads up status bar view should be shown |
| 240 | */ |
| 241 | public boolean shouldBeVisible() { |
| 242 | return !mIsExpanded && mHeadsUpManager.hasPinnedHeadsUp(); |
| 243 | } |
| 244 | |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 245 | @Override |
| 246 | public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) { |
| 247 | updateTopEntry(); |
| 248 | updateHeader(headsUp.getEntry()); |
| 249 | } |
| 250 | |
| 251 | public void setExpandedHeight(float expandedHeight, float appearFraction) { |
| 252 | boolean changedHeight = expandedHeight != mExpandedHeight; |
| 253 | mExpandedHeight = expandedHeight; |
| 254 | mExpandFraction = appearFraction; |
| 255 | boolean isExpanded = expandedHeight > 0; |
| 256 | if (changedHeight) { |
| 257 | updateHeadsUpHeaders(); |
| 258 | } |
| 259 | if (isExpanded != mIsExpanded) { |
| 260 | mIsExpanded = isExpanded; |
| 261 | updateTopEntry(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Set a headsUp to be tracked, meaning that it is currently being pulled down after being |
| 267 | * in a pinned state on the top. The expand animation is different in that case and we need |
| 268 | * to update the header constantly afterwards. |
| 269 | * |
| 270 | * @param trackedChild the tracked headsUp or null if it's not tracking anymore. |
| 271 | */ |
| 272 | public void setTrackingHeadsUp(ExpandableNotificationRow trackedChild) { |
| 273 | ExpandableNotificationRow previousTracked = mTrackedChild; |
| 274 | mTrackedChild = trackedChild; |
| 275 | if (previousTracked != null) { |
| 276 | updateHeader(previousTracked.getEntry()); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | private void updateHeadsUpHeaders() { |
| 281 | mHeadsUpManager.getAllEntries().forEach(entry -> { |
| 282 | updateHeader(entry); |
| 283 | }); |
| 284 | } |
| 285 | |
Selim Cinek | f0c79e1 | 2018-05-14 17:17:31 -0700 | [diff] [blame] | 286 | public void updateHeader(NotificationData.Entry entry) { |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 287 | ExpandableNotificationRow row = entry.row; |
| 288 | float headerVisibleAmount = 1.0f; |
Selim Cinek | f0c79e1 | 2018-05-14 17:17:31 -0700 | [diff] [blame] | 289 | if (row.isPinned() || row.isHeadsUpAnimatingAway() || row == mTrackedChild) { |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 290 | headerVisibleAmount = mExpandFraction; |
| 291 | } |
| 292 | row.setHeaderVisibleAmount(headerVisibleAmount); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
| 297 | mHeadsUpStatusBarView.onDarkChanged(area, darkIntensity, tint); |
| 298 | } |
| 299 | |
| 300 | public void setPublicMode(boolean publicMode) { |
| 301 | mHeadsUpStatusBarView.setPublicMode(publicMode); |
| 302 | updateTopEntry(); |
| 303 | } |
| 304 | } |