blob: ddce9d51dec0f3bdb1e907e7bd9302838ad9b857 [file] [log] [blame]
Daniel Sandler50a53132012-10-24 15:02:27 -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
Daniel Sandler08d05e32012-08-08 16:39:54 -040017package com.android.systemui.statusbar.phone;
18
Jorim Jaggi1d480692014-05-20 19:41:58 +020019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040021import android.animation.ObjectAnimator;
Jorim Jaggi1d480692014-05-20 19:41:58 +020022import android.animation.ValueAnimator;
Daniel Sandler08d05e32012-08-08 16:39:54 -040023import android.content.Context;
Selim Cinekb84a1072014-05-15 19:10:18 +020024import android.content.res.Configuration;
Daniel Sandler08d05e32012-08-08 16:39:54 -040025import android.content.res.Resources;
26import android.util.AttributeSet;
John Spurlockcd686b52013-06-05 10:13:46 -040027import android.util.Log;
Daniel Sandler08d05e32012-08-08 16:39:54 -040028import android.view.MotionEvent;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010029import android.view.ViewConfiguration;
Jorim Jaggi0a27be82014-06-11 03:22:39 +020030import android.view.ViewTreeObserver;
Jorim Jaggi90129582014-06-02 14:44:49 +020031import android.view.animation.AnimationUtils;
32import android.view.animation.Interpolator;
Daniel Sandler08d05e32012-08-08 16:39:54 -040033import android.widget.FrameLayout;
34
Christoph Studerb0183992014-12-22 21:02:26 +010035import com.android.systemui.EventLogConstants;
36import com.android.systemui.EventLogTags;
Daniel Sandler08d05e32012-08-08 16:39:54 -040037import com.android.systemui.R;
John Spurlock813552c2014-09-19 08:30:21 -040038import com.android.systemui.doze.DozeLog;
Jorim Jaggi1d480692014-05-20 19:41:58 +020039import com.android.systemui.statusbar.FlingAnimationUtils;
Jorim Jaggi90129582014-06-02 14:44:49 +020040import com.android.systemui.statusbar.StatusBarState;
Selim Cinekb8f09cf2015-03-16 17:09:28 -070041import com.android.systemui.statusbar.policy.HeadsUpManager;
Daniel Sandler08d05e32012-08-08 16:39:54 -040042
John Spurlockde84f0e2013-06-12 12:41:00 -040043import java.io.FileDescriptor;
44import java.io.PrintWriter;
John Spurlockde84f0e2013-06-12 12:41:00 -040045
Selim Cinek4c6969a2014-05-26 19:22:17 +020046public abstract class PanelView extends FrameLayout {
Daniel Sandler198a0302012-08-17 16:04:31 -040047 public static final boolean DEBUG = PanelBar.DEBUG;
Daniel Sandler08d05e32012-08-08 16:39:54 -040048 public static final String TAG = PanelView.class.getSimpleName();
Selim Cinekb8f09cf2015-03-16 17:09:28 -070049 protected HeadsUpManager mHeadsUpManager;
Daniel Sandlere7c5bbb2013-03-05 13:36:21 -050050
John Spurlock97642182013-07-29 17:58:39 -040051 private final void logf(String fmt, Object... args) {
John Spurlockcd686b52013-06-05 10:13:46 -040052 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args));
Daniel Sandler08d05e32012-08-08 16:39:54 -040053 }
54
Jorim Jaggi90129582014-06-02 14:44:49 +020055 protected PhoneStatusBar mStatusBar;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040056 private float mPeekHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +020057 private float mHintDistance;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020058 private int mEdgeTapAreaWidth;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010059 private float mInitialOffsetOnTouch;
Daniel Sandler08d05e32012-08-08 16:39:54 -040060 private float mExpandedFraction = 0;
Selim Cinek1408eb52014-06-02 14:45:38 +020061 protected float mExpandedHeight = 0;
Selim Cinek31094df2014-08-14 19:28:15 +020062 private boolean mPanelClosedOnDown;
63 private boolean mHasLayoutedSinceDown;
64 private float mUpdateFlingVelocity;
65 private boolean mUpdateFlingOnLayout;
Jorim Jaggib7a33032014-08-20 16:21:36 +020066 private boolean mPeekTouching;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040067 private boolean mJustPeeked;
Daniel Sandler50508132012-08-16 14:10:53 -040068 private boolean mClosing;
Jorim Jaggi8dd95e02014-06-03 16:19:33 +020069 protected boolean mTracking;
Jorim Jaggi90129582014-06-02 14:44:49 +020070 private boolean mTouchSlopExceeded;
John Spurlock48fa91a2013-08-15 09:29:31 -040071 private int mTrackingPointer;
Jorim Jaggid7daab72014-05-06 22:22:20 +020072 protected int mTouchSlop;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020073 protected boolean mHintAnimationRunning;
Jorim Jaggi47c85a32014-06-05 17:25:40 +020074 private boolean mOverExpandedBeforeFling;
Selim Cinek19c8c702014-08-25 22:09:19 +020075 private boolean mTouchAboveFalsingThreshold;
76 private int mUnlockFalsingThreshold;
Selim Cinekab1dc952014-10-30 20:20:29 +010077 private boolean mTouchStartedInEmptyArea;
Selim Cinek547a06b2014-11-27 14:06:04 +010078 private boolean mMotionAborted;
Daniel Sandler08d05e32012-08-08 16:39:54 -040079
Jorim Jaggi1d480692014-05-20 19:41:58 +020080 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040081 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020082 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020083 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040084
Jorim Jaggi0a27be82014-06-11 03:22:39 +020085 /**
86 * Whether an instant expand request is currently pending and we are just waiting for layout.
87 */
88 private boolean mInstantExpanding;
89
Daniel Sandler08d05e32012-08-08 16:39:54 -040090 PanelBar mBar;
91
Daniel Sandler50508132012-08-16 14:10:53 -040092 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020093 private float mInitialTouchY;
94 private float mInitialTouchX;
Jorim Jaggid41083a2014-09-12 02:54:40 +020095 private boolean mTouchDisabled;
Daniel Sandler08d05e32012-08-08 16:39:54 -040096
Jorim Jaggi90129582014-06-02 14:44:49 +020097 private Interpolator mLinearOutSlowInInterpolator;
Jorim Jaggib472b3472014-06-30 19:56:24 +020098 private Interpolator mFastOutSlowInInterpolator;
Jorim Jaggi90129582014-06-02 14:44:49 +020099 private Interpolator mBounceInterpolator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200100 protected KeyguardBottomAreaView mKeyguardBottomArea;
Jorim Jaggi90129582014-06-02 14:44:49 +0200101
Jorim Jaggib472b3472014-06-30 19:56:24 +0200102 private boolean mPeekPending;
103 private boolean mCollapseAfterPeek;
104 private boolean mExpanding;
105 private boolean mGestureWaitForTouchSlop;
106 private Runnable mPeekRunnable = new Runnable() {
107 @Override
108 public void run() {
109 mPeekPending = false;
110 runPeekAnimation();
111 }
112 };
113
Selim Cinek1685e632014-04-08 02:27:49 +0200114 protected void onExpandingFinished() {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200115 endClosing();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200116 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +0200117 }
118
119 protected void onExpandingStarted() {
120 }
121
Jorim Jaggib472b3472014-06-30 19:56:24 +0200122 private void notifyExpandingStarted() {
123 if (!mExpanding) {
124 mExpanding = true;
125 onExpandingStarted();
126 }
127 }
128
129 private void notifyExpandingFinished() {
130 if (mExpanding) {
131 mExpanding = false;
132 onExpandingFinished();
133 }
134 }
135
136 private void schedulePeek() {
137 mPeekPending = true;
138 long timeout = ViewConfiguration.getTapTimeout();
139 postOnAnimationDelayed(mPeekRunnable, timeout);
140 notifyBarPanelExpansionChanged();
141 }
142
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400143 private void runPeekAnimation() {
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200144 mPeekHeight = getPeekHeight();
John Spurlock97642182013-07-29 17:58:39 -0400145 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200146 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400147 return;
148 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200149 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight)
150 .setDuration(250);
151 mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator);
152 mPeekAnimator.addListener(new AnimatorListenerAdapter() {
153 private boolean mCancelled;
154
155 @Override
156 public void onAnimationCancel(Animator animation) {
157 mCancelled = true;
158 }
159
160 @Override
161 public void onAnimationEnd(Animator animation) {
162 mPeekAnimator = null;
163 if (mCollapseAfterPeek && !mCancelled) {
164 postOnAnimation(new Runnable() {
165 @Override
166 public void run() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200167 collapse(false /* delayed */);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200168 }
169 });
170 }
171 mCollapseAfterPeek = false;
172 }
173 });
174 notifyExpandingStarted();
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400175 mPeekAnimator.start();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200176 mJustPeeked = true;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400177 }
178
Daniel Sandler08d05e32012-08-08 16:39:54 -0400179 public PanelView(Context context, AttributeSet attrs) {
180 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200181 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200182 mFastOutSlowInInterpolator =
Jorim Jaggi90129582014-06-02 14:44:49 +0200183 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200184 mLinearOutSlowInInterpolator =
185 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
Jorim Jaggi90129582014-06-02 14:44:49 +0200186 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400187 }
188
Jorim Jaggi069cd032014-05-15 03:09:01 +0200189 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400190 final Resources res = getContext().getResources();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100191 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
192 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200193 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200194 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Selim Cinek19c8c702014-08-25 22:09:19 +0200195 mUnlockFalsingThreshold = res.getDimensionPixelSize(R.dimen.unlock_falsing_threshold);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400196 }
197
198 private void trackMovement(MotionEvent event) {
199 // Add movement to velocity tracker using raw screen X and Y coordinates instead
200 // of window coordinates because the window frame may be moving at the same time.
201 float deltaX = event.getRawX() - event.getX();
202 float deltaY = event.getRawY() - event.getY();
203 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400204 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400205 event.offsetLocation(-deltaX, -deltaY);
206 }
207
Jorim Jaggid41083a2014-09-12 02:54:40 +0200208 public void setTouchDisabled(boolean disabled) {
209 mTouchDisabled = disabled;
210 }
211
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400212 @Override
213 public boolean onTouchEvent(MotionEvent event) {
Selim Cinek547a06b2014-11-27 14:06:04 +0100214 if (mInstantExpanding || mTouchDisabled
215 || (mMotionAborted && event.getActionMasked() != MotionEvent.ACTION_DOWN)) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200216 return false;
217 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100218
219 /*
220 * We capture touch events here and update the expand height here in case according to
221 * the users fingers. This also handles multi-touch.
222 *
223 * If the user just clicks shortly, we give him a quick peek of the shade.
224 *
225 * Flinging is also enabled in order to open or close the shade.
226 */
227
228 int pointerIndex = event.findPointerIndex(mTrackingPointer);
229 if (pointerIndex < 0) {
230 pointerIndex = 0;
231 mTrackingPointer = event.getPointerId(pointerIndex);
232 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200233 final float x = event.getX(pointerIndex);
Selim Cinek547a06b2014-11-27 14:06:04 +0100234 final float y = event.getY(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100235
Jorim Jaggib472b3472014-06-30 19:56:24 +0200236 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
Selim Cinekd8535862015-04-14 20:31:54 -0700237 mGestureWaitForTouchSlop = isShadeCollapsed();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200238 }
239 boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200240
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100241 switch (event.getActionMasked()) {
242 case MotionEvent.ACTION_DOWN:
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700243 startExpandMotion(x, y, false /* startTracking */, mExpandedHeight);
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200244 mJustPeeked = false;
Selim Cinekd8535862015-04-14 20:31:54 -0700245 mPanelClosedOnDown = isShadeCollapsed();
Selim Cinek31094df2014-08-14 19:28:15 +0200246 mHasLayoutedSinceDown = false;
247 mUpdateFlingOnLayout = false;
Selim Cinek547a06b2014-11-27 14:06:04 +0100248 mMotionAborted = false;
Jorim Jaggib7a33032014-08-20 16:21:36 +0200249 mPeekTouching = mPanelClosedOnDown;
Selim Cinek19c8c702014-08-25 22:09:19 +0200250 mTouchAboveFalsingThreshold = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200251 if (mVelocityTracker == null) {
252 initVelocityTracker();
253 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100254 trackMovement(event);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200255 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) ||
256 mPeekPending || mPeekAnimator != null) {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200257 cancelHeightAnimator();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200258 cancelPeek();
259 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning)
260 || mPeekPending || mPeekAnimator != null;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200261 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200262 }
Selim Cinekd8535862015-04-14 20:31:54 -0700263 if (isShadeCollapsed()) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200264 schedulePeek();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100265 }
266 break;
267
268 case MotionEvent.ACTION_POINTER_UP:
269 final int upPointer = event.getPointerId(event.getActionIndex());
270 if (mTrackingPointer == upPointer) {
271 // gesture is ongoing, find a new pointer to track
272 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
273 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200274 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100275 mTrackingPointer = event.getPointerId(newIndex);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700276 startExpandMotion(newX, newY, true /* startTracking */, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100277 }
278 break;
Selim Cinek547a06b2014-11-27 14:06:04 +0100279 case MotionEvent.ACTION_POINTER_DOWN:
280 if (mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
281 mMotionAborted = true;
282 endMotionEvent(event, x, y, true /* forceCancel */);
283 return false;
284 }
285 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100286 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200287 float h = y - mInitialTouchY;
Jorim Jaggib7240132014-06-30 01:39:07 +0200288
289 // If the panel was collapsed when touching, we only need to check for the
290 // y-component of the gesture, as we have no conflicting horizontal gesture.
291 if (Math.abs(h) > mTouchSlop
292 && (Math.abs(h) > Math.abs(x - mInitialTouchX)
293 || mInitialOffsetOnTouch == 0f)) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200294 mTouchSlopExceeded = true;
295 if (waitForTouchSlop && !mTracking) {
Jorim Jaggi0b1528a2014-10-28 22:47:46 +0100296 if (!mJustPeeked && mInitialOffsetOnTouch != 0f) {
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700297 startExpandMotion(x, y, false /* startTracking */, mExpandedHeight);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200298 h = 0;
299 }
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200300 cancelHeightAnimator();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200301 removeCallbacks(mPeekRunnable);
302 mPeekPending = false;
Jorim Jaggi90129582014-06-02 14:44:49 +0200303 onTrackingStarted();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200304 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200305 }
Jorim Jaggi93439da2014-06-30 23:53:39 +0200306 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200307 if (newHeight > mPeekHeight) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200308 if (mPeekAnimator != null) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100309 mPeekAnimator.cancel();
310 }
311 mJustPeeked = false;
312 }
Selim Cinek29ed3c92014-09-23 20:44:35 +0200313 if (-h >= getFalsingThreshold()) {
Selim Cinek19c8c702014-08-25 22:09:19 +0200314 mTouchAboveFalsingThreshold = true;
315 }
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200316 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) {
Jorim Jaggicc693242014-06-14 03:04:35 +0000317 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100318 }
319
320 trackMovement(event);
321 break;
322
323 case MotionEvent.ACTION_UP:
324 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100325 trackMovement(event);
Selim Cinek547a06b2014-11-27 14:06:04 +0100326 endMotionEvent(event, x, y, false /* forceCancel */);
327 break;
328 }
329 return !waitForTouchSlop || mTracking;
330 }
331
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700332 protected void startExpandMotion(float newX, float newY, boolean startTracking,
333 float expandedHeight) {
334 mInitialOffsetOnTouch = expandedHeight;
335 mInitialTouchY = newY;
336 mInitialTouchX = newX;
337 if (startTracking) {
338 mTouchSlopExceeded = true;
339 onTrackingStarted();
340 }
341 }
342
Selim Cinek547a06b2014-11-27 14:06:04 +0100343 private void endMotionEvent(MotionEvent event, float x, float y, boolean forceCancel) {
344 mTrackingPointer = -1;
345 if ((mTracking && mTouchSlopExceeded)
346 || Math.abs(x - mInitialTouchX) > mTouchSlop
347 || Math.abs(y - mInitialTouchY) > mTouchSlop
348 || event.getActionMasked() == MotionEvent.ACTION_CANCEL
349 || forceCancel) {
350 float vel = 0f;
351 float vectorVel = 0f;
352 if (mVelocityTracker != null) {
353 mVelocityTracker.computeCurrentVelocity(1000);
354 vel = mVelocityTracker.getYVelocity();
355 vectorVel = (float) Math.hypot(
356 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
357 }
358 boolean expand = flingExpands(vel, vectorVel)
359 || event.getActionMasked() == MotionEvent.ACTION_CANCEL
360 || forceCancel;
361 onTrackingStopped(expand);
362 DozeLog.traceFling(expand, mTouchAboveFalsingThreshold,
363 mStatusBar.isFalsingThresholdNeeded(),
364 mStatusBar.isScreenOnComingFromTouch());
Christoph Studerb0183992014-12-22 21:02:26 +0100365 // Log collapse gesture if on lock screen.
366 if (!expand && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
367 float displayDensity = mStatusBar.getDisplayDensity();
368 int heightDp = (int) Math.abs((y - mInitialTouchY) / displayDensity);
369 int velocityDp = (int) Math.abs(vel / displayDensity);
370 EventLogTags.writeSysuiLockscreenGesture(
371 EventLogConstants.SYSUI_LOCKSCREEN_GESTURE_SWIPE_UP_UNLOCK,
372 heightDp, velocityDp);
373 }
Selim Cinek547a06b2014-11-27 14:06:04 +0100374 fling(vel, expand);
375 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown;
376 if (mUpdateFlingOnLayout) {
377 mUpdateFlingVelocity = vel;
378 }
379 } else {
380 boolean expands = onEmptySpaceClick(mInitialTouchX);
381 onTrackingStopped(expands);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100382 }
Selim Cinek547a06b2014-11-27 14:06:04 +0100383
384 if (mVelocityTracker != null) {
385 mVelocityTracker.recycle();
386 mVelocityTracker = null;
387 }
388 mPeekTouching = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100389 }
390
Selim Cinek29ed3c92014-09-23 20:44:35 +0200391 private int getFalsingThreshold() {
392 float factor = mStatusBar.isScreenOnComingFromTouch() ? 1.5f : 1.0f;
393 return (int) (mUnlockFalsingThreshold * factor);
394 }
395
Selim Cinek4c6969a2014-05-26 19:22:17 +0200396 protected abstract boolean hasConflictingGestures();
397
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200398 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200399 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200400 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200401 }
402
403 protected void onTrackingStarted() {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200404 endClosing();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200405 mTracking = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200406 mCollapseAfterPeek = false;
Selim Cinek1685e632014-04-08 02:27:49 +0200407 mBar.onTrackingStarted(PanelView.this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200408 notifyExpandingStarted();
Selim Cinek1685e632014-04-08 02:27:49 +0200409 }
410
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100411 @Override
412 public boolean onInterceptTouchEvent(MotionEvent event) {
Selim Cinek547a06b2014-11-27 14:06:04 +0100413 if (mInstantExpanding
414 || (mMotionAborted && event.getActionMasked() != MotionEvent.ACTION_DOWN)) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200415 return false;
416 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100417
418 /*
419 * If the user drags anywhere inside the panel we intercept it if he moves his finger
420 * upwards. This allows closing the shade from anywhere inside the panel.
421 *
422 * We only do this if the current content is scrolled to the bottom,
423 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
424 * possible.
425 */
426 int pointerIndex = event.findPointerIndex(mTrackingPointer);
427 if (pointerIndex < 0) {
428 pointerIndex = 0;
429 mTrackingPointer = event.getPointerId(pointerIndex);
430 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200431 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100432 final float y = event.getY(pointerIndex);
433 boolean scrolledToBottom = isScrolledToBottom();
434
435 switch (event.getActionMasked()) {
436 case MotionEvent.ACTION_DOWN:
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200437 mStatusBar.userActivity();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200438 if (mHeightAnimator != null && !mHintAnimationRunning ||
439 mPeekPending || mPeekAnimator != null) {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200440 cancelHeightAnimator();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200441 cancelPeek();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200442 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200443 return true;
444 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100445 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200446 mInitialTouchX = x;
Selim Cinekab1dc952014-10-30 20:20:29 +0100447 mTouchStartedInEmptyArea = !isInContentBounds(x, y);
Jorim Jaggi90129582014-06-02 14:44:49 +0200448 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200449 mJustPeeked = false;
Selim Cinek547a06b2014-11-27 14:06:04 +0100450 mMotionAborted = false;
Selim Cinekd8535862015-04-14 20:31:54 -0700451 mPanelClosedOnDown = isShadeCollapsed();
Selim Cinek31094df2014-08-14 19:28:15 +0200452 mHasLayoutedSinceDown = false;
453 mUpdateFlingOnLayout = false;
Selim Cinek19c8c702014-08-25 22:09:19 +0200454 mTouchAboveFalsingThreshold = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100455 initVelocityTracker();
456 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100457 break;
458 case MotionEvent.ACTION_POINTER_UP:
459 final int upPointer = event.getPointerId(event.getActionIndex());
460 if (mTrackingPointer == upPointer) {
461 // gesture is ongoing, find a new pointer to track
462 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
463 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200464 mInitialTouchX = event.getX(newIndex);
465 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100466 }
467 break;
Selim Cinek547a06b2014-11-27 14:06:04 +0100468 case MotionEvent.ACTION_POINTER_DOWN:
469 if (mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
470 mMotionAborted = true;
471 if (mVelocityTracker != null) {
472 mVelocityTracker.recycle();
473 mVelocityTracker = null;
474 }
475 }
476 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100477 case MotionEvent.ACTION_MOVE:
478 final float h = y - mInitialTouchY;
479 trackMovement(event);
Selim Cinekab1dc952014-10-30 20:20:29 +0100480 if (scrolledToBottom || mTouchStartedInEmptyArea) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200481 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200482 cancelHeightAnimator();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700483 startExpandMotion(x, y, true /* startTracking */, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100484 return true;
485 }
486 }
487 break;
Selim Cinek31094df2014-08-14 19:28:15 +0200488 case MotionEvent.ACTION_CANCEL:
489 case MotionEvent.ACTION_UP:
Selim Cinek547a06b2014-11-27 14:06:04 +0100490 if (mVelocityTracker != null) {
491 mVelocityTracker.recycle();
492 mVelocityTracker = null;
493 }
Selim Cinek31094df2014-08-14 19:28:15 +0200494 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100495 }
496 return false;
497 }
498
Selim Cinekab1dc952014-10-30 20:20:29 +0100499 /**
500 * @return Whether a pair of coordinates are inside the visible view content bounds.
501 */
502 protected abstract boolean isInContentBounds(float x, float y);
503
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200504 private void cancelHeightAnimator() {
505 if (mHeightAnimator != null) {
506 mHeightAnimator.cancel();
507 }
508 endClosing();
509 }
510
511 private void endClosing() {
512 if (mClosing) {
513 mClosing = false;
514 onClosingFinished();
515 }
516 }
517
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100518 private void initVelocityTracker() {
519 if (mVelocityTracker != null) {
520 mVelocityTracker.recycle();
521 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200522 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100523 }
524
525 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200526 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100527 }
528
529 protected float getContentHeight() {
530 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400531 }
532
Daniel Sandler08d05e32012-08-08 16:39:54 -0400533 @Override
534 protected void onFinishInflate() {
535 super.onFinishInflate();
536 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400537 }
538
Jorim Jaggi069cd032014-05-15 03:09:01 +0200539 @Override
540 protected void onConfigurationChanged(Configuration newConfig) {
541 super.onConfigurationChanged(newConfig);
542 loadDimens();
Jorim Jaggi069cd032014-05-15 03:09:01 +0200543 }
544
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200545 /**
Jorim Jaggib7240132014-06-30 01:39:07 +0200546 * @param vel the current vertical velocity of the motion
547 * @param vectorVel the length of the vectorial velocity
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200548 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200549 */
Jorim Jaggidc96d632014-07-01 18:48:52 +0200550 protected boolean flingExpands(float vel, float vectorVel) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200551 if (isBelowFalsingThreshold()) {
Selim Cinek19c8c702014-08-25 22:09:19 +0200552 return true;
553 }
Jorim Jaggib7240132014-06-30 01:39:07 +0200554 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200555 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200556 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200557 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400558 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200559 }
560
Selim Cinek5386fb32014-09-03 16:37:36 +0200561 private boolean isBelowFalsingThreshold() {
562 return !mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded();
563 }
564
Jorim Jaggi1d480692014-05-20 19:41:58 +0200565 protected void fling(float vel, boolean expand) {
566 cancelPeek();
567 float target = expand ? getMaxPanelHeight() : 0.0f;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700568 flingToHeight(vel, expand, target);
569 }
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200570
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700571 protected void flingToHeight(float vel, boolean expand, float target) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200572 // Hack to make the expand transition look nice when clear all button is visible - we make
573 // the animation only to the last notification, and then jump to the maximum panel height so
574 // clear all just fades in and the decelerating motion is towards the last notification.
575 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible()
576 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight()
577 && !isClearAllVisible();
578 if (clearAllExpandHack) {
579 target = getMaxPanelHeight() - getClearAllHeight();
580 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200581 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200582 notifyExpandingFinished();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200583 return;
584 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200585 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200586 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200587 if (expand) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200588 boolean belowFalsingThreshold = isBelowFalsingThreshold();
589 if (belowFalsingThreshold) {
590 vel = 0;
591 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200592 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
Selim Cinek5386fb32014-09-03 16:37:36 +0200593 if (belowFalsingThreshold) {
594 animator.setDuration(350);
595 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200596 } else {
597 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
598 getHeight());
599
600 // Make it shorter if we run a canned animation
601 if (vel == 0) {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200602 animator.setDuration((long)
603 (animator.getDuration() * getCannedFlingDurationFactor()));
Jorim Jaggi1d480692014-05-20 19:41:58 +0200604 }
605 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200606 animator.addListener(new AnimatorListenerAdapter() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200607 private boolean mCancelled;
608
609 @Override
610 public void onAnimationCancel(Animator animation) {
611 mCancelled = true;
612 }
613
Jorim Jaggi1d480692014-05-20 19:41:58 +0200614 @Override
615 public void onAnimationEnd(Animator animation) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200616 if (clearAllExpandHack && !mCancelled) {
Jorim Jaggi2ae259d2014-08-04 23:35:47 +0200617 setExpandedHeightInternal(getMaxPanelHeight());
618 }
619 mHeightAnimator = null;
620 if (!mCancelled) {
621 notifyExpandingFinished();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200622 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200623 }
624 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200625 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200626 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400627 }
628
629 @Override
630 protected void onAttachedToWindow() {
631 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400632 mViewName = getResources().getResourceName(getId());
633 }
634
635 public String getName() {
636 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400637 }
638
Daniel Sandler08d05e32012-08-08 16:39:54 -0400639 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400640 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200641 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler08d05e32012-08-08 16:39:54 -0400642 }
643
Daniel Sandler50508132012-08-16 14:10:53 -0400644 @Override
645 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Daniel Sandler50508132012-08-16 14:10:53 -0400646 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100647 requestPanelHeightUpdate();
Selim Cinek31094df2014-08-14 19:28:15 +0200648 mHasLayoutedSinceDown = true;
649 if (mUpdateFlingOnLayout) {
650 abortAnimations();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700651 fling(mUpdateFlingVelocity, true /* expands */);
Selim Cinek31094df2014-08-14 19:28:15 +0200652 mUpdateFlingOnLayout = false;
653 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100654 }
655
656 protected void requestPanelHeightUpdate() {
657 float currentMaxPanelHeight = getMaxPanelHeight();
658
659 // If the user isn't actively poking us, let's update the height
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200660 if ((!mTracking || isTrackingBlocked())
661 && mHeightAnimator == null
Selim Cineka59ecc32015-04-07 10:51:49 -0700662 && !isShadeCollapsed()
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200663 && currentMaxPanelHeight != mExpandedHeight
664 && !mPeekPending
Selim Cinek31094df2014-08-14 19:28:15 +0200665 && mPeekAnimator == null
Jorim Jaggib7a33032014-08-20 16:21:36 +0200666 && !mPeekTouching) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200667 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100668 }
Daniel Sandler50508132012-08-16 14:10:53 -0400669 }
670
Daniel Sandler08d05e32012-08-08 16:39:54 -0400671 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200672 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
673 if (mHeightAnimator == null) {
674 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
675 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
676 setOverExpansion(overExpansionPixels, true /* isPixels */);
677 }
678 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
679 } else {
680 mExpandedHeight = h;
681 if (mOverExpandedBeforeFling) {
682 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
683 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100684 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400685
Jorim Jaggi93439da2014-06-30 23:53:39 +0200686 mExpandedHeight = Math.max(0, mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200687 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
688 ? 0
689 : mExpandedHeight / fhWithoutOverExpansion);
Jorim Jaggi06a0c3a2014-10-29 17:17:21 +0100690 onHeightUpdated(mExpandedHeight);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200691 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400692 }
693
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200694 /**
695 * @return true if the panel tracking should be temporarily blocked; this is used when a
696 * conflicting gesture (opening QS) is happening
697 */
698 protected abstract boolean isTrackingBlocked();
699
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200700 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200701
Jorim Jaggi90129582014-06-02 14:44:49 +0200702 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100703
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200704 protected abstract float getOverExpansionAmount();
705
706 protected abstract float getOverExpansionPixels();
707
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100708 /**
709 * This returns the maximum height of the panel. Children should override this if their
710 * desired height is not the full height.
711 *
712 * @return the default implementation simply returns the maximum height.
713 */
Selim Cinek31094df2014-08-14 19:28:15 +0200714 protected abstract int getMaxPanelHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400715
716 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100717 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400718 }
719
720 public float getExpandedHeight() {
721 return mExpandedHeight;
722 }
723
724 public float getExpandedFraction() {
725 return mExpandedFraction;
726 }
727
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700728 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100729 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700730 }
731
732 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400733 return mExpandedHeight <= 0;
734 }
735
736 public boolean isCollapsing() {
737 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700738 }
739
John Spurlocka4b70af2013-08-17 14:05:49 -0400740 public boolean isTracking() {
741 return mTracking;
742 }
743
Daniel Sandler08d05e32012-08-08 16:39:54 -0400744 public void setBar(PanelBar panelBar) {
745 mBar = panelBar;
746 }
747
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200748 public void collapse(boolean delayed) {
John Spurlock97642182013-07-29 17:58:39 -0400749 if (DEBUG) logf("collapse: " + this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200750 if (mPeekPending || mPeekAnimator != null) {
751 mCollapseAfterPeek = true;
752 if (mPeekPending) {
753
754 // We know that the whole gesture is just a peek triggered by a simple click, so
755 // better start it now.
756 removeCallbacks(mPeekRunnable);
757 mPeekRunnable.run();
758 }
Jorim Jaggi98139552014-09-10 17:04:27 +0200759 } else if (!isFullyCollapsed() && !mTracking && !mClosing) {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200760 cancelHeightAnimator();
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400761 mClosing = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200762 notifyExpandingStarted();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200763 if (delayed) {
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200764 postDelayed(mFlingCollapseRunnable, 120);
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200765 } else {
766 fling(0, false /* expand */);
767 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400768 }
769 }
770
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200771 private final Runnable mFlingCollapseRunnable = new Runnable() {
772 @Override
773 public void run() {
774 fling(0, false /* expand */);
775 }
776 };
777
Daniel Sandler08d05e32012-08-08 16:39:54 -0400778 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400779 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400780 if (isFullyCollapsed()) {
781 mBar.startOpeningPanel(this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200782 notifyExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200783 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400784 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400785 if (DEBUG) logf("skipping expansion: is expanded");
786 }
787 }
788
789 public void cancelPeek() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200790 if (mPeekAnimator != null) {
John Spurlock97642182013-07-29 17:58:39 -0400791 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400792 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200793 removeCallbacks(mPeekRunnable);
794 mPeekPending = false;
795
796 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
797 // notify mBar that we might have closed ourselves.
798 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400799 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500800
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200801 public void instantExpand() {
802 mInstantExpanding = true;
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200803 mUpdateFlingOnLayout = false;
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200804 abortAnimations();
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200805 cancelPeek();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200806 if (mTracking) {
807 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
Jorim Jaggie62d5892014-09-02 16:31:19 +0200808 }
809 if (mExpanding) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200810 notifyExpandingFinished();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200811 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700812 notifyBarPanelExpansionChanged();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200813
814 // Wait for window manager to pickup the change, so we know the maximum height of the panel
815 // then.
816 getViewTreeObserver().addOnGlobalLayoutListener(
817 new ViewTreeObserver.OnGlobalLayoutListener() {
818 @Override
819 public void onGlobalLayout() {
820 if (mStatusBar.getStatusBarWindow().getHeight()
821 != mStatusBar.getStatusBarHeight()) {
822 getViewTreeObserver().removeOnGlobalLayoutListener(this);
823 setExpandedFraction(1f);
824 mInstantExpanding = false;
825 }
826 }
827 });
828
829 // Make sure a layout really happens.
830 requestLayout();
831 }
832
Selim Cinek6bb4a9b2014-10-09 17:48:05 -0700833 public void instantCollapse() {
834 abortAnimations();
835 setExpandedFraction(0f);
836 if (mExpanding) {
837 notifyExpandingFinished();
838 }
839 }
840
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200841 private void abortAnimations() {
842 cancelPeek();
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200843 cancelHeightAnimator();
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200844 removeCallbacks(mPostCollapseRunnable);
845 removeCallbacks(mFlingCollapseRunnable);
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200846 }
847
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200848 protected void onClosingFinished() {
849 mBar.onClosingFinished();
850 }
851
852
Jorim Jaggi90129582014-06-02 14:44:49 +0200853 protected void startUnlockHintAnimation() {
854
855 // We don't need to hint the user if an animation is already running or the user is changing
856 // the expansion.
857 if (mHeightAnimator != null || mTracking) {
858 return;
859 }
860 cancelPeek();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200861 notifyExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200862 startUnlockHintAnimationPhase1(new Runnable() {
863 @Override
864 public void run() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200865 notifyExpandingFinished();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200866 mStatusBar.onHintFinished();
867 mHintAnimationRunning = false;
868 }
869 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200870 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200871 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200872 }
873
874 /**
875 * Phase 1: Move everything upwards.
876 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200877 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200878 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
879 ValueAnimator animator = createHeightAnimator(target);
880 animator.setDuration(250);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200881 animator.setInterpolator(mFastOutSlowInInterpolator);
Jorim Jaggi90129582014-06-02 14:44:49 +0200882 animator.addListener(new AnimatorListenerAdapter() {
883 private boolean mCancelled;
884
885 @Override
886 public void onAnimationCancel(Animator animation) {
887 mCancelled = true;
888 }
889
890 @Override
891 public void onAnimationEnd(Animator animation) {
892 if (mCancelled) {
893 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200894 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200895 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200896 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200897 }
898 }
899 });
900 animator.start();
901 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200902 mKeyguardBottomArea.getIndicationView().animate()
Selim Cinek5256d932014-10-22 20:05:03 +0200903 .translationY(-mHintDistance)
Selim Cinekf99d0002014-06-13 07:36:01 +0200904 .setDuration(250)
Jorim Jaggib472b3472014-06-30 19:56:24 +0200905 .setInterpolator(mFastOutSlowInInterpolator)
Selim Cinekf99d0002014-06-13 07:36:01 +0200906 .withEndAction(new Runnable() {
907 @Override
908 public void run() {
909 mKeyguardBottomArea.getIndicationView().animate()
Selim Cinek5256d932014-10-22 20:05:03 +0200910 .translationY(0)
Selim Cinekf99d0002014-06-13 07:36:01 +0200911 .setDuration(450)
912 .setInterpolator(mBounceInterpolator)
913 .start();
914 }
915 })
916 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200917 }
918
919 /**
920 * Phase 2: Bounce down.
921 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200922 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200923 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
924 animator.setDuration(450);
925 animator.setInterpolator(mBounceInterpolator);
926 animator.addListener(new AnimatorListenerAdapter() {
927 @Override
928 public void onAnimationEnd(Animator animation) {
929 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200930 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200931 }
932 });
933 animator.start();
934 mHeightAnimator = animator;
935 }
936
937 private ValueAnimator createHeightAnimator(float targetHeight) {
938 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
939 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
940 @Override
941 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200942 setExpandedHeightInternal((Float) animation.getAnimatedValue());
Jorim Jaggi90129582014-06-02 14:44:49 +0200943 }
944 });
945 return animator;
946 }
947
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700948 protected void notifyBarPanelExpansionChanged() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200949 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700950 || mPeekAnimator != null || mInstantExpanding || mHeadsUpManager.hasPinnedHeadsUp());
Jorim Jaggib472b3472014-06-30 19:56:24 +0200951 }
952
Jorim Jaggi90129582014-06-02 14:44:49 +0200953 /**
954 * Gets called when the user performs a click anywhere in the empty area of the panel.
955 *
956 * @return whether the panel will be expanded after the action performed by this method
957 */
Selim Cinek3a9c10a2014-10-28 14:21:10 +0100958 protected boolean onEmptySpaceClick(float x) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200959 if (mHintAnimationRunning) {
960 return true;
961 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200962 if (x < mEdgeTapAreaWidth
963 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200964 onEdgeClicked(false /* right */);
965 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200966 } else if (x > getWidth() - mEdgeTapAreaWidth
967 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200968 onEdgeClicked(true /* right */);
969 return true;
970 } else {
971 return onMiddleClicked();
972 }
973 }
974
Jorim Jaggi1cf6e102015-01-20 16:45:05 +0100975 protected final Runnable mPostCollapseRunnable = new Runnable() {
Jorim Jaggi488b7922014-08-05 21:12:02 +0200976 @Override
977 public void run() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200978 collapse(false /* delayed */);
Jorim Jaggi488b7922014-08-05 21:12:02 +0200979 }
980 };
Jorim Jaggi488b7922014-08-05 21:12:02 +0200981
Jorim Jaggi1cf6e102015-01-20 16:45:05 +0100982 protected abstract boolean onMiddleClicked();
Jorim Jaggi90129582014-06-02 14:44:49 +0200983
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200984 protected abstract void onEdgeClicked(boolean right);
985
Jorim Jaggid7912d22014-09-30 17:38:19 +0200986 protected abstract boolean isDozing();
987
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500988 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200989 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
Jorim Jaggi50d87a72014-09-12 21:43:35 +0200990 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s touchDisabled=%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500991 + "]",
992 this.getClass().getSimpleName(),
993 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100994 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500995 mClosing?"T":"f",
996 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500997 mJustPeeked?"T":"f",
998 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi50d87a72014-09-12 21:43:35 +0200999 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":""),
1000 mTouchDisabled?"T":"f"
Daniel Sandler37a38aa2013-02-13 17:15:57 -05001001 ));
1002 }
Selim Cinek3c4635c2014-05-29 02:12:47 +02001003
1004 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +02001005
1006 protected abstract float getPeekHeight();
Jorim Jaggi30c305c2014-07-01 23:34:41 +02001007
1008 protected abstract float getCannedFlingDurationFactor();
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +02001009
1010 /**
1011 * @return whether "Clear all" button will be visible when the panel is fully expanded
1012 */
1013 protected abstract boolean fullyExpandedClearAllVisible();
1014
1015 protected abstract boolean isClearAllVisible();
1016
1017 /**
1018 * @return the height of the clear all button, in pixels
1019 */
1020 protected abstract int getClearAllHeight();
Selim Cinekb8f09cf2015-03-16 17:09:28 -07001021
Selim Cineka59ecc32015-04-07 10:51:49 -07001022 protected abstract boolean isShadeCollapsed();
1023
Selim Cinekb8f09cf2015-03-16 17:09:28 -07001024 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
1025 mHeadsUpManager = headsUpManager;
1026 }
Daniel Sandler08d05e32012-08-08 16:39:54 -04001027}