blob: 82fbb1648b869b95525baa028179b2d1ac9e7f2e [file] [log] [blame]
Daniel Sandler08d05e32012-08-08 16:39:54 -04001/*
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
17package com.android.systemui.statusbar.phone;
18
19import android.content.Context;
20import android.util.AttributeSet;
Daniel Sandler040c2e42012-10-17 00:56:33 -040021import android.view.MotionEvent;
Daniel Sandler13522a22012-09-27 14:46:58 -040022import android.view.View;
Casey Burkhardt23b0a4e2013-04-29 12:18:32 -070023import android.view.accessibility.AccessibilityEvent;
Daniel Sandler13522a22012-09-27 14:46:58 -040024
Chet Haase4d179dc2012-08-22 07:14:42 -070025import com.android.systemui.R;
Jorim Jaggibe565df2014-04-28 17:51:23 +020026import com.android.systemui.statusbar.ExpandableView;
Daniel Sandler151f00d2012-10-02 22:33:08 -040027import com.android.systemui.statusbar.GestureRecorder;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010028import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
Daniel Sandler08d05e32012-08-08 16:39:54 -040029
Jorim Jaggibe565df2014-04-28 17:51:23 +020030public class NotificationPanelView extends PanelView implements
31 ExpandableView.OnHeightChangedListener {
Chris Wren64161cc2012-12-17 16:49:30 -050032 public static final boolean DEBUG_GESTURES = true;
Chet Haase4d179dc2012-08-22 07:14:42 -070033
Daniel Sandler040c2e42012-10-17 00:56:33 -040034 PhoneStatusBar mStatusBar;
John Spurlock73203eb2014-04-15 16:14:46 -040035 private View mHeader;
36 private View mKeyguardStatusView;
37
Selim Cinekb6d85eb2014-03-28 20:21:01 +010038 private NotificationStackScrollLayout mNotificationStackScroller;
39 private int[] mTempLocation = new int[2];
40 private int[] mTempChildLocation = new int[2];
41 private View mNotificationParent;
John Spurlock73203eb2014-04-15 16:14:46 -040042 private boolean mTrackingSettings;
John Spurlockf0b06c72014-04-18 15:04:11 -040043 private float mExpandedHeight = -1;
Chet Haase4d179dc2012-08-22 07:14:42 -070044
Daniel Sandler08d05e32012-08-08 16:39:54 -040045 public NotificationPanelView(Context context, AttributeSet attrs) {
46 super(context, attrs);
Daniel Sandler13522a22012-09-27 14:46:58 -040047 }
Chet Haase4d179dc2012-08-22 07:14:42 -070048
Daniel Sandler040c2e42012-10-17 00:56:33 -040049 public void setStatusBar(PhoneStatusBar bar) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +010050 if (mStatusBar != null) {
51 mStatusBar.setOnFlipRunnable(null);
52 }
Daniel Sandler040c2e42012-10-17 00:56:33 -040053 mStatusBar = bar;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010054 if (bar != null) {
55 mStatusBar.setOnFlipRunnable(new Runnable() {
56 @Override
57 public void run() {
58 requestPanelHeightUpdate();
59 }
60 });
61 }
Daniel Sandler040c2e42012-10-17 00:56:33 -040062 }
63
Daniel Sandler13522a22012-09-27 14:46:58 -040064 @Override
65 protected void onFinishInflate() {
66 super.onFinishInflate();
67
John Spurlock73203eb2014-04-15 16:14:46 -040068 mHeader = findViewById(R.id.header);
69 mKeyguardStatusView = findViewById(R.id.keyguard_status_view);
Selim Cinekb6d85eb2014-03-28 20:21:01 +010070 mNotificationStackScroller = (NotificationStackScrollLayout)
71 findViewById(R.id.notification_stack_scroller);
Jorim Jaggibe565df2014-04-28 17:51:23 +020072 mNotificationStackScroller.setOnHeightChangedListener(this);
Selim Cinekb6d85eb2014-03-28 20:21:01 +010073 mNotificationParent = findViewById(R.id.notification_container_parent);
Daniel Sandler08d05e32012-08-08 16:39:54 -040074 }
75
Daniel Sandler08d05e32012-08-08 16:39:54 -040076 @Override
77 public void fling(float vel, boolean always) {
Daniel Sandler151f00d2012-10-02 22:33:08 -040078 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 Sandler08d05e32012-08-08 16:39:54 -040084 super.fling(vel, always);
85 }
Chet Haase4d179dc2012-08-22 07:14:42 -070086
Casey Burkhardt23b0a4e2013-04-29 12:18:32 -070087 @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 Cinekb6d85eb2014-03-28 20:21:01 +010098 /**
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 Haase4d179dc2012-08-22 07:14:42 -0700108 }
Daniel Sandler040c2e42012-10-17 00:56:33 -0400109
110 @Override
John Spurlock73203eb2014-04-15 16:14:46 -0400111 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 Sandler040c2e42012-10-17 00:56:33 -0400129 public boolean onTouchEvent(MotionEvent event) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100130 // TODO: Handle doublefinger swipe to notifications again. Look at history for a reference
131 // implementation.
John Spurlock73203eb2014-04-15 16:14:46 -0400132 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 Cinekb6d85eb2014-03-28 20:21:01 +0100143 return super.onTouchEvent(event);
144 }
145
146 @Override
147 protected boolean isScrolledToBottom() {
148 if (!isInSettings()) {
149 return mNotificationStackScroller.isScrolledToBottom();
Chris Wren64161cc2012-12-17 16:49:30 -0500150 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100151 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 Sandler040c2e42012-10-17 00:56:33 -0400160 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100161 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 Spurlockf0b06c72014-04-18 15:04:11 -0400180 if (mExpandedHeight == expandedHeight) return;
181 mExpandedHeight = expandedHeight;
Selim Cinek1685e632014-04-08 02:27:49 +0200182 mNotificationStackScroller.setIsExpanded(expandedHeight > 0.0f);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100183 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 Sandler040c2e42012-10-17 00:56:33 -0400211 }
Selim Cinek1685e632014-04-08 02:27:49 +0200212
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 Jaggibe565df2014-04-28 17:51:23 +0200224
225 @Override
226 public void onHeightChanged(ExpandableView view) {
227 requestPanelHeightUpdate();
228 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400229}