Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 21 | import android.view.MotionEvent; |
Daniel Sandler | 13522a2 | 2012-09-27 14:46:58 -0400 | [diff] [blame] | 22 | import android.view.View; |
Casey Burkhardt | 23b0a4e | 2013-04-29 12:18:32 -0700 | [diff] [blame] | 23 | import android.view.accessibility.AccessibilityEvent; |
Daniel Sandler | 13522a2 | 2012-09-27 14:46:58 -0400 | [diff] [blame] | 24 | |
Chet Haase | 4d179dc | 2012-08-22 07:14:42 -0700 | [diff] [blame] | 25 | import com.android.systemui.R; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 26 | import com.android.systemui.statusbar.ExpandableView; |
Daniel Sandler | 151f00d | 2012-10-02 22:33:08 -0400 | [diff] [blame] | 27 | import com.android.systemui.statusbar.GestureRecorder; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 28 | import com.android.systemui.statusbar.stack.NotificationStackScrollLayout; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 29 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 30 | public class NotificationPanelView extends PanelView implements |
| 31 | ExpandableView.OnHeightChangedListener { |
Chris Wren | 64161cc | 2012-12-17 16:49:30 -0500 | [diff] [blame] | 32 | public static final boolean DEBUG_GESTURES = true; |
Chet Haase | 4d179dc | 2012-08-22 07:14:42 -0700 | [diff] [blame] | 33 | |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 34 | PhoneStatusBar mStatusBar; |
John Spurlock | 73203eb | 2014-04-15 16:14:46 -0400 | [diff] [blame] | 35 | private View mHeader; |
| 36 | private View mKeyguardStatusView; |
| 37 | |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 38 | private NotificationStackScrollLayout mNotificationStackScroller; |
| 39 | private int[] mTempLocation = new int[2]; |
| 40 | private int[] mTempChildLocation = new int[2]; |
| 41 | private View mNotificationParent; |
John Spurlock | 73203eb | 2014-04-15 16:14:46 -0400 | [diff] [blame] | 42 | private boolean mTrackingSettings; |
John Spurlock | f0b06c7 | 2014-04-18 15:04:11 -0400 | [diff] [blame] | 43 | private float mExpandedHeight = -1; |
Chet Haase | 4d179dc | 2012-08-22 07:14:42 -0700 | [diff] [blame] | 44 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 45 | public NotificationPanelView(Context context, AttributeSet attrs) { |
| 46 | super(context, attrs); |
Daniel Sandler | 13522a2 | 2012-09-27 14:46:58 -0400 | [diff] [blame] | 47 | } |
Chet Haase | 4d179dc | 2012-08-22 07:14:42 -0700 | [diff] [blame] | 48 | |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 49 | public void setStatusBar(PhoneStatusBar bar) { |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 50 | if (mStatusBar != null) { |
| 51 | mStatusBar.setOnFlipRunnable(null); |
| 52 | } |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 53 | mStatusBar = bar; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 54 | if (bar != null) { |
| 55 | mStatusBar.setOnFlipRunnable(new Runnable() { |
| 56 | @Override |
| 57 | public void run() { |
| 58 | requestPanelHeightUpdate(); |
| 59 | } |
| 60 | }); |
| 61 | } |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Daniel Sandler | 13522a2 | 2012-09-27 14:46:58 -0400 | [diff] [blame] | 64 | @Override |
| 65 | protected void onFinishInflate() { |
| 66 | super.onFinishInflate(); |
| 67 | |
John Spurlock | 73203eb | 2014-04-15 16:14:46 -0400 | [diff] [blame] | 68 | mHeader = findViewById(R.id.header); |
| 69 | mKeyguardStatusView = findViewById(R.id.keyguard_status_view); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 70 | mNotificationStackScroller = (NotificationStackScrollLayout) |
| 71 | findViewById(R.id.notification_stack_scroller); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 72 | mNotificationStackScroller.setOnHeightChangedListener(this); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 73 | mNotificationParent = findViewById(R.id.notification_container_parent); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 76 | @Override |
| 77 | public void fling(float vel, boolean always) { |
Daniel Sandler | 151f00d | 2012-10-02 22:33:08 -0400 | [diff] [blame] | 78 | GestureRecorder gr = ((PhoneStatusBarView) mBar).mBar.getGestureRecorder(); |
| 79 | if (gr != null) { |
| 80 | gr.tag( |
| 81 | "fling " + ((vel > 0) ? "open" : "closed"), |
| 82 | "notifications,v=" + vel); |
| 83 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 84 | super.fling(vel, always); |
| 85 | } |
Chet Haase | 4d179dc | 2012-08-22 07:14:42 -0700 | [diff] [blame] | 86 | |
Casey Burkhardt | 23b0a4e | 2013-04-29 12:18:32 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 89 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 90 | event.getText() |
| 91 | .add(getContext().getString(R.string.accessibility_desc_notification_shade)); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | return super.dispatchPopulateAccessibilityEvent(event); |
| 96 | } |
| 97 | |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 98 | /** |
| 99 | * Gets the relative position of a view on the screen in regard to this view. |
| 100 | * |
| 101 | * @param requestedView the view we want to find the relative position for |
| 102 | * @return |
| 103 | */ |
| 104 | private int getRelativeTop(View requestedView) { |
| 105 | getLocationOnScreen(mTempLocation); |
| 106 | requestedView.getLocationOnScreen(mTempChildLocation); |
| 107 | return mTempChildLocation[1] - mTempLocation[1]; |
Chet Haase | 4d179dc | 2012-08-22 07:14:42 -0700 | [diff] [blame] | 108 | } |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 109 | |
| 110 | @Override |
John Spurlock | 73203eb | 2014-04-15 16:14:46 -0400 | [diff] [blame] | 111 | public boolean onInterceptTouchEvent(MotionEvent event) { |
| 112 | // intercept for quick settings |
| 113 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
| 114 | final View target = mStatusBar.isOnKeyguard() ? mKeyguardStatusView : mHeader; |
| 115 | final boolean inTarget = PhoneStatusBar.inBounds(target, event, true); |
| 116 | if (inTarget && !isInSettings()) { |
| 117 | mTrackingSettings = true; |
| 118 | return true; |
| 119 | } |
| 120 | if (!inTarget && isInSettings()) { |
| 121 | mTrackingSettings = true; |
| 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | return super.onInterceptTouchEvent(event); |
| 126 | } |
| 127 | |
| 128 | @Override |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 129 | public boolean onTouchEvent(MotionEvent event) { |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 130 | // TODO: Handle doublefinger swipe to notifications again. Look at history for a reference |
| 131 | // implementation. |
John Spurlock | 73203eb | 2014-04-15 16:14:46 -0400 | [diff] [blame] | 132 | if (mTrackingSettings) { |
| 133 | mStatusBar.onSettingsEvent(event); |
| 134 | if (event.getAction() == MotionEvent.ACTION_UP |
| 135 | || event.getAction() == MotionEvent.ACTION_CANCEL) { |
| 136 | mTrackingSettings = false; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | if (isInSettings()) { |
| 141 | return true; |
| 142 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 143 | return super.onTouchEvent(event); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected boolean isScrolledToBottom() { |
| 148 | if (!isInSettings()) { |
| 149 | return mNotificationStackScroller.isScrolledToBottom(); |
Chris Wren | 64161cc | 2012-12-17 16:49:30 -0500 | [diff] [blame] | 150 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 151 | return super.isScrolledToBottom(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected int getMaxPanelHeight() { |
| 156 | if (!isInSettings()) { |
| 157 | int maxPanelHeight = super.getMaxPanelHeight(); |
| 158 | int emptyBottomMargin = mNotificationStackScroller.getEmptyBottomMargin(); |
| 159 | return maxPanelHeight - emptyBottomMargin; |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 160 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 161 | return super.getMaxPanelHeight(); |
| 162 | } |
| 163 | |
| 164 | private boolean isInSettings() { |
| 165 | return mStatusBar != null && mStatusBar.isFlippedToSettings(); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected void onHeightUpdated(float expandedHeight) { |
| 170 | updateNotificationStackHeight(expandedHeight); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Update the height of the {@link #mNotificationStackScroller} to the new expanded height. |
| 175 | * This is much more efficient than doing it over the layout pass. |
| 176 | * |
| 177 | * @param expandedHeight the new expanded height |
| 178 | */ |
| 179 | private void updateNotificationStackHeight(float expandedHeight) { |
John Spurlock | f0b06c7 | 2014-04-18 15:04:11 -0400 | [diff] [blame] | 180 | if (mExpandedHeight == expandedHeight) return; |
| 181 | mExpandedHeight = expandedHeight; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 182 | mNotificationStackScroller.setIsExpanded(expandedHeight > 0.0f); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 183 | float childOffset = getRelativeTop(mNotificationStackScroller) |
| 184 | - mNotificationParent.getTranslationY(); |
| 185 | int newStackHeight = (int) (expandedHeight - childOffset); |
| 186 | int itemHeight = mNotificationStackScroller.getItemHeight(); |
| 187 | int bottomStackPeekSize = mNotificationStackScroller.getBottomStackPeekSize(); |
| 188 | int minStackHeight = itemHeight + bottomStackPeekSize; |
| 189 | if (newStackHeight >= minStackHeight) { |
| 190 | mNotificationParent.setTranslationY(0); |
| 191 | mNotificationStackScroller.setCurrentStackHeight(newStackHeight); |
| 192 | } else { |
| 193 | |
| 194 | // We did not reach the position yet where we actually start growing, |
| 195 | // so we translate the stack upwards. |
| 196 | int translationY = (newStackHeight - minStackHeight); |
| 197 | // A slight parallax effect is introduced in order for the stack to catch up with |
| 198 | // the top card. |
| 199 | float partiallyThere = (float) newStackHeight / minStackHeight; |
| 200 | partiallyThere = Math.max(0, partiallyThere); |
| 201 | translationY += (1 - partiallyThere) * bottomStackPeekSize; |
| 202 | mNotificationParent.setTranslationY(translationY); |
| 203 | mNotificationStackScroller.setCurrentStackHeight( |
| 204 | (int) (expandedHeight - (childOffset + translationY))); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | protected int getDesiredMeasureHeight() { |
| 210 | return mMaxPanelHeight; |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 211 | } |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 212 | |
| 213 | @Override |
| 214 | protected void onExpandingStarted() { |
| 215 | super.onExpandingStarted(); |
| 216 | mNotificationStackScroller.onExpansionStarted(); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | protected void onExpandingFinished() { |
| 221 | super.onExpandingFinished(); |
| 222 | mNotificationStackScroller.onExpansionStopped(); |
| 223 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 224 | |
| 225 | @Override |
| 226 | public void onHeightChanged(ExpandableView view) { |
| 227 | requestPanelHeightUpdate(); |
| 228 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 229 | } |