blob: 3a30429a5a4fa6f509393f6c3058ec5aecf70fbb [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;
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700104
105 /**
106 * Speed-up factor to be used when {@link #mFlingCollapseRunnable} runs the next time.
107 */
108 private float mNextCollapseSpeedUpFactor = 1.0f;
109
Jorim Jaggib472b3472014-06-30 19:56:24 +0200110 private boolean mExpanding;
111 private boolean mGestureWaitForTouchSlop;
112 private Runnable mPeekRunnable = new Runnable() {
113 @Override
114 public void run() {
115 mPeekPending = false;
116 runPeekAnimation();
117 }
118 };
119
Selim Cinek1685e632014-04-08 02:27:49 +0200120 protected void onExpandingFinished() {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200121 endClosing();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200122 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +0200123 }
124
125 protected void onExpandingStarted() {
126 }
127
Jorim Jaggib472b3472014-06-30 19:56:24 +0200128 private void notifyExpandingStarted() {
129 if (!mExpanding) {
130 mExpanding = true;
131 onExpandingStarted();
132 }
133 }
134
135 private void notifyExpandingFinished() {
136 if (mExpanding) {
137 mExpanding = false;
138 onExpandingFinished();
139 }
140 }
141
142 private void schedulePeek() {
143 mPeekPending = true;
144 long timeout = ViewConfiguration.getTapTimeout();
145 postOnAnimationDelayed(mPeekRunnable, timeout);
146 notifyBarPanelExpansionChanged();
147 }
148
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400149 private void runPeekAnimation() {
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200150 mPeekHeight = getPeekHeight();
John Spurlock97642182013-07-29 17:58:39 -0400151 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200152 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400153 return;
154 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200155 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight)
156 .setDuration(250);
157 mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator);
158 mPeekAnimator.addListener(new AnimatorListenerAdapter() {
159 private boolean mCancelled;
160
161 @Override
162 public void onAnimationCancel(Animator animation) {
163 mCancelled = true;
164 }
165
166 @Override
167 public void onAnimationEnd(Animator animation) {
168 mPeekAnimator = null;
169 if (mCollapseAfterPeek && !mCancelled) {
170 postOnAnimation(new Runnable() {
171 @Override
172 public void run() {
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700173 collapse(false /* delayed */, 1.0f /* speedUpFactor */);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200174 }
175 });
176 }
177 mCollapseAfterPeek = false;
178 }
179 });
180 notifyExpandingStarted();
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400181 mPeekAnimator.start();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200182 mJustPeeked = true;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400183 }
184
Daniel Sandler08d05e32012-08-08 16:39:54 -0400185 public PanelView(Context context, AttributeSet attrs) {
186 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200187 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200188 mFastOutSlowInInterpolator =
Jorim Jaggi90129582014-06-02 14:44:49 +0200189 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200190 mLinearOutSlowInInterpolator =
191 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
Jorim Jaggi90129582014-06-02 14:44:49 +0200192 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400193 }
194
Jorim Jaggi069cd032014-05-15 03:09:01 +0200195 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400196 final Resources res = getContext().getResources();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100197 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
198 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200199 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200200 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Selim Cinek19c8c702014-08-25 22:09:19 +0200201 mUnlockFalsingThreshold = res.getDimensionPixelSize(R.dimen.unlock_falsing_threshold);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400202 }
203
204 private void trackMovement(MotionEvent event) {
205 // Add movement to velocity tracker using raw screen X and Y coordinates instead
206 // of window coordinates because the window frame may be moving at the same time.
207 float deltaX = event.getRawX() - event.getX();
208 float deltaY = event.getRawY() - event.getY();
209 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400210 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400211 event.offsetLocation(-deltaX, -deltaY);
212 }
213
Jorim Jaggid41083a2014-09-12 02:54:40 +0200214 public void setTouchDisabled(boolean disabled) {
215 mTouchDisabled = disabled;
216 }
217
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400218 @Override
219 public boolean onTouchEvent(MotionEvent event) {
Selim Cinek547a06b2014-11-27 14:06:04 +0100220 if (mInstantExpanding || mTouchDisabled
221 || (mMotionAborted && event.getActionMasked() != MotionEvent.ACTION_DOWN)) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200222 return false;
223 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100224
225 /*
226 * We capture touch events here and update the expand height here in case according to
227 * the users fingers. This also handles multi-touch.
228 *
229 * If the user just clicks shortly, we give him a quick peek of the shade.
230 *
231 * Flinging is also enabled in order to open or close the shade.
232 */
233
234 int pointerIndex = event.findPointerIndex(mTrackingPointer);
235 if (pointerIndex < 0) {
236 pointerIndex = 0;
237 mTrackingPointer = event.getPointerId(pointerIndex);
238 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200239 final float x = event.getX(pointerIndex);
Selim Cinek547a06b2014-11-27 14:06:04 +0100240 final float y = event.getY(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100241
Jorim Jaggib472b3472014-06-30 19:56:24 +0200242 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
Selim Cinekd8535862015-04-14 20:31:54 -0700243 mGestureWaitForTouchSlop = isShadeCollapsed();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200244 }
245 boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200246
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100247 switch (event.getActionMasked()) {
248 case MotionEvent.ACTION_DOWN:
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700249 startExpandMotion(x, y, false /* startTracking */, mExpandedHeight);
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200250 mJustPeeked = false;
Selim Cinekd8535862015-04-14 20:31:54 -0700251 mPanelClosedOnDown = isShadeCollapsed();
Selim Cinek31094df2014-08-14 19:28:15 +0200252 mHasLayoutedSinceDown = false;
253 mUpdateFlingOnLayout = false;
Selim Cinek547a06b2014-11-27 14:06:04 +0100254 mMotionAborted = false;
Jorim Jaggib7a33032014-08-20 16:21:36 +0200255 mPeekTouching = mPanelClosedOnDown;
Selim Cinek19c8c702014-08-25 22:09:19 +0200256 mTouchAboveFalsingThreshold = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200257 if (mVelocityTracker == null) {
258 initVelocityTracker();
259 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100260 trackMovement(event);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200261 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) ||
262 mPeekPending || mPeekAnimator != null) {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200263 cancelHeightAnimator();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200264 cancelPeek();
265 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning)
266 || mPeekPending || mPeekAnimator != null;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200267 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200268 }
Selim Cinekd8535862015-04-14 20:31:54 -0700269 if (isShadeCollapsed()) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200270 schedulePeek();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100271 }
272 break;
273
274 case MotionEvent.ACTION_POINTER_UP:
275 final int upPointer = event.getPointerId(event.getActionIndex());
276 if (mTrackingPointer == upPointer) {
277 // gesture is ongoing, find a new pointer to track
278 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
279 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200280 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100281 mTrackingPointer = event.getPointerId(newIndex);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700282 startExpandMotion(newX, newY, true /* startTracking */, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100283 }
284 break;
Selim Cinek547a06b2014-11-27 14:06:04 +0100285 case MotionEvent.ACTION_POINTER_DOWN:
286 if (mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
287 mMotionAborted = true;
288 endMotionEvent(event, x, y, true /* forceCancel */);
289 return false;
290 }
291 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100292 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200293 float h = y - mInitialTouchY;
Jorim Jaggib7240132014-06-30 01:39:07 +0200294
295 // If the panel was collapsed when touching, we only need to check for the
296 // y-component of the gesture, as we have no conflicting horizontal gesture.
297 if (Math.abs(h) > mTouchSlop
298 && (Math.abs(h) > Math.abs(x - mInitialTouchX)
299 || mInitialOffsetOnTouch == 0f)) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200300 mTouchSlopExceeded = true;
301 if (waitForTouchSlop && !mTracking) {
Jorim Jaggi0b1528a2014-10-28 22:47:46 +0100302 if (!mJustPeeked && mInitialOffsetOnTouch != 0f) {
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700303 startExpandMotion(x, y, false /* startTracking */, mExpandedHeight);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200304 h = 0;
305 }
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200306 cancelHeightAnimator();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200307 removeCallbacks(mPeekRunnable);
308 mPeekPending = false;
Jorim Jaggi90129582014-06-02 14:44:49 +0200309 onTrackingStarted();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200310 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200311 }
Jorim Jaggi93439da2014-06-30 23:53:39 +0200312 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200313 if (newHeight > mPeekHeight) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200314 if (mPeekAnimator != null) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100315 mPeekAnimator.cancel();
316 }
317 mJustPeeked = false;
318 }
Selim Cinek29ed3c92014-09-23 20:44:35 +0200319 if (-h >= getFalsingThreshold()) {
Selim Cinek19c8c702014-08-25 22:09:19 +0200320 mTouchAboveFalsingThreshold = true;
321 }
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200322 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) {
Jorim Jaggicc693242014-06-14 03:04:35 +0000323 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100324 }
325
326 trackMovement(event);
327 break;
328
329 case MotionEvent.ACTION_UP:
330 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100331 trackMovement(event);
Selim Cinek547a06b2014-11-27 14:06:04 +0100332 endMotionEvent(event, x, y, false /* forceCancel */);
333 break;
334 }
335 return !waitForTouchSlop || mTracking;
336 }
337
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700338 protected void startExpandMotion(float newX, float newY, boolean startTracking,
339 float expandedHeight) {
340 mInitialOffsetOnTouch = expandedHeight;
341 mInitialTouchY = newY;
342 mInitialTouchX = newX;
343 if (startTracking) {
344 mTouchSlopExceeded = true;
345 onTrackingStarted();
346 }
347 }
348
Selim Cinek547a06b2014-11-27 14:06:04 +0100349 private void endMotionEvent(MotionEvent event, float x, float y, boolean forceCancel) {
350 mTrackingPointer = -1;
351 if ((mTracking && mTouchSlopExceeded)
352 || Math.abs(x - mInitialTouchX) > mTouchSlop
353 || Math.abs(y - mInitialTouchY) > mTouchSlop
354 || event.getActionMasked() == MotionEvent.ACTION_CANCEL
355 || forceCancel) {
356 float vel = 0f;
357 float vectorVel = 0f;
358 if (mVelocityTracker != null) {
359 mVelocityTracker.computeCurrentVelocity(1000);
360 vel = mVelocityTracker.getYVelocity();
361 vectorVel = (float) Math.hypot(
362 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
363 }
364 boolean expand = flingExpands(vel, vectorVel)
365 || event.getActionMasked() == MotionEvent.ACTION_CANCEL
366 || forceCancel;
367 onTrackingStopped(expand);
368 DozeLog.traceFling(expand, mTouchAboveFalsingThreshold,
369 mStatusBar.isFalsingThresholdNeeded(),
370 mStatusBar.isScreenOnComingFromTouch());
Christoph Studerb0183992014-12-22 21:02:26 +0100371 // Log collapse gesture if on lock screen.
372 if (!expand && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
373 float displayDensity = mStatusBar.getDisplayDensity();
374 int heightDp = (int) Math.abs((y - mInitialTouchY) / displayDensity);
375 int velocityDp = (int) Math.abs(vel / displayDensity);
376 EventLogTags.writeSysuiLockscreenGesture(
377 EventLogConstants.SYSUI_LOCKSCREEN_GESTURE_SWIPE_UP_UNLOCK,
378 heightDp, velocityDp);
379 }
Selim Cinek547a06b2014-11-27 14:06:04 +0100380 fling(vel, expand);
381 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown;
382 if (mUpdateFlingOnLayout) {
383 mUpdateFlingVelocity = vel;
384 }
385 } else {
386 boolean expands = onEmptySpaceClick(mInitialTouchX);
387 onTrackingStopped(expands);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100388 }
Selim Cinek547a06b2014-11-27 14:06:04 +0100389
390 if (mVelocityTracker != null) {
391 mVelocityTracker.recycle();
392 mVelocityTracker = null;
393 }
394 mPeekTouching = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100395 }
396
Selim Cinek29ed3c92014-09-23 20:44:35 +0200397 private int getFalsingThreshold() {
398 float factor = mStatusBar.isScreenOnComingFromTouch() ? 1.5f : 1.0f;
399 return (int) (mUnlockFalsingThreshold * factor);
400 }
401
Selim Cinek4c6969a2014-05-26 19:22:17 +0200402 protected abstract boolean hasConflictingGestures();
403
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200404 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200405 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200406 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200407 }
408
409 protected void onTrackingStarted() {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200410 endClosing();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200411 mTracking = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200412 mCollapseAfterPeek = false;
Selim Cinek1685e632014-04-08 02:27:49 +0200413 mBar.onTrackingStarted(PanelView.this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200414 notifyExpandingStarted();
Selim Cinek1685e632014-04-08 02:27:49 +0200415 }
416
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100417 @Override
418 public boolean onInterceptTouchEvent(MotionEvent event) {
Selim Cinek547a06b2014-11-27 14:06:04 +0100419 if (mInstantExpanding
420 || (mMotionAborted && event.getActionMasked() != MotionEvent.ACTION_DOWN)) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200421 return false;
422 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100423
424 /*
425 * If the user drags anywhere inside the panel we intercept it if he moves his finger
426 * upwards. This allows closing the shade from anywhere inside the panel.
427 *
428 * We only do this if the current content is scrolled to the bottom,
429 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
430 * possible.
431 */
432 int pointerIndex = event.findPointerIndex(mTrackingPointer);
433 if (pointerIndex < 0) {
434 pointerIndex = 0;
435 mTrackingPointer = event.getPointerId(pointerIndex);
436 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200437 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100438 final float y = event.getY(pointerIndex);
439 boolean scrolledToBottom = isScrolledToBottom();
440
441 switch (event.getActionMasked()) {
442 case MotionEvent.ACTION_DOWN:
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200443 mStatusBar.userActivity();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200444 if (mHeightAnimator != null && !mHintAnimationRunning ||
445 mPeekPending || mPeekAnimator != null) {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200446 cancelHeightAnimator();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200447 cancelPeek();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200448 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200449 return true;
450 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100451 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200452 mInitialTouchX = x;
Selim Cinekab1dc952014-10-30 20:20:29 +0100453 mTouchStartedInEmptyArea = !isInContentBounds(x, y);
Jorim Jaggi90129582014-06-02 14:44:49 +0200454 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200455 mJustPeeked = false;
Selim Cinek547a06b2014-11-27 14:06:04 +0100456 mMotionAborted = false;
Selim Cinekd8535862015-04-14 20:31:54 -0700457 mPanelClosedOnDown = isShadeCollapsed();
Selim Cinek31094df2014-08-14 19:28:15 +0200458 mHasLayoutedSinceDown = false;
459 mUpdateFlingOnLayout = false;
Selim Cinek19c8c702014-08-25 22:09:19 +0200460 mTouchAboveFalsingThreshold = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100461 initVelocityTracker();
462 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100463 break;
464 case MotionEvent.ACTION_POINTER_UP:
465 final int upPointer = event.getPointerId(event.getActionIndex());
466 if (mTrackingPointer == upPointer) {
467 // gesture is ongoing, find a new pointer to track
468 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
469 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200470 mInitialTouchX = event.getX(newIndex);
471 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100472 }
473 break;
Selim Cinek547a06b2014-11-27 14:06:04 +0100474 case MotionEvent.ACTION_POINTER_DOWN:
475 if (mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
476 mMotionAborted = true;
477 if (mVelocityTracker != null) {
478 mVelocityTracker.recycle();
479 mVelocityTracker = null;
480 }
481 }
482 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100483 case MotionEvent.ACTION_MOVE:
484 final float h = y - mInitialTouchY;
485 trackMovement(event);
Selim Cinekab1dc952014-10-30 20:20:29 +0100486 if (scrolledToBottom || mTouchStartedInEmptyArea) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200487 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200488 cancelHeightAnimator();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700489 startExpandMotion(x, y, true /* startTracking */, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100490 return true;
491 }
492 }
493 break;
Selim Cinek31094df2014-08-14 19:28:15 +0200494 case MotionEvent.ACTION_CANCEL:
495 case MotionEvent.ACTION_UP:
Selim Cinek547a06b2014-11-27 14:06:04 +0100496 if (mVelocityTracker != null) {
497 mVelocityTracker.recycle();
498 mVelocityTracker = null;
499 }
Selim Cinek31094df2014-08-14 19:28:15 +0200500 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100501 }
502 return false;
503 }
504
Selim Cinekab1dc952014-10-30 20:20:29 +0100505 /**
506 * @return Whether a pair of coordinates are inside the visible view content bounds.
507 */
508 protected abstract boolean isInContentBounds(float x, float y);
509
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200510 private void cancelHeightAnimator() {
511 if (mHeightAnimator != null) {
512 mHeightAnimator.cancel();
513 }
514 endClosing();
515 }
516
517 private void endClosing() {
518 if (mClosing) {
519 mClosing = false;
520 onClosingFinished();
521 }
522 }
523
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100524 private void initVelocityTracker() {
525 if (mVelocityTracker != null) {
526 mVelocityTracker.recycle();
527 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200528 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100529 }
530
531 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200532 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100533 }
534
535 protected float getContentHeight() {
536 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400537 }
538
Daniel Sandler08d05e32012-08-08 16:39:54 -0400539 @Override
540 protected void onFinishInflate() {
541 super.onFinishInflate();
542 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400543 }
544
Jorim Jaggi069cd032014-05-15 03:09:01 +0200545 @Override
546 protected void onConfigurationChanged(Configuration newConfig) {
547 super.onConfigurationChanged(newConfig);
548 loadDimens();
Jorim Jaggi069cd032014-05-15 03:09:01 +0200549 }
550
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200551 /**
Jorim Jaggib7240132014-06-30 01:39:07 +0200552 * @param vel the current vertical velocity of the motion
553 * @param vectorVel the length of the vectorial velocity
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200554 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200555 */
Jorim Jaggidc96d632014-07-01 18:48:52 +0200556 protected boolean flingExpands(float vel, float vectorVel) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200557 if (isBelowFalsingThreshold()) {
Selim Cinek19c8c702014-08-25 22:09:19 +0200558 return true;
559 }
Jorim Jaggib7240132014-06-30 01:39:07 +0200560 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200561 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200562 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200563 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400564 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200565 }
566
Selim Cinek5386fb32014-09-03 16:37:36 +0200567 private boolean isBelowFalsingThreshold() {
568 return !mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded();
569 }
570
Jorim Jaggi1d480692014-05-20 19:41:58 +0200571 protected void fling(float vel, boolean expand) {
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700572 fling(vel, expand, 1.0f /* collapseSpeedUpFactor */);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700573 }
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200574
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700575 protected void fling(float vel, boolean expand, float collapseSpeedUpFactor) {
576 cancelPeek();
577 float target = expand ? getMaxPanelHeight() : 0.0f;
578 flingToHeight(vel, expand, target, collapseSpeedUpFactor);
579 }
580
581 protected void flingToHeight(float vel, boolean expand, float target,
582 float collapseSpeedUpFactor) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200583 // Hack to make the expand transition look nice when clear all button is visible - we make
584 // the animation only to the last notification, and then jump to the maximum panel height so
585 // clear all just fades in and the decelerating motion is towards the last notification.
586 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible()
587 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight()
588 && !isClearAllVisible();
589 if (clearAllExpandHack) {
590 target = getMaxPanelHeight() - getClearAllHeight();
591 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200592 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200593 notifyExpandingFinished();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200594 return;
595 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200596 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200597 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200598 if (expand) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200599 boolean belowFalsingThreshold = isBelowFalsingThreshold();
600 if (belowFalsingThreshold) {
601 vel = 0;
602 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200603 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
Selim Cinek5386fb32014-09-03 16:37:36 +0200604 if (belowFalsingThreshold) {
605 animator.setDuration(350);
606 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200607 } else {
608 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
609 getHeight());
610
611 // Make it shorter if we run a canned animation
612 if (vel == 0) {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200613 animator.setDuration((long)
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700614 (animator.getDuration() * getCannedFlingDurationFactor()
615 / collapseSpeedUpFactor));
Jorim Jaggi1d480692014-05-20 19:41:58 +0200616 }
617 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200618 animator.addListener(new AnimatorListenerAdapter() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200619 private boolean mCancelled;
620
621 @Override
622 public void onAnimationCancel(Animator animation) {
623 mCancelled = true;
624 }
625
Jorim Jaggi1d480692014-05-20 19:41:58 +0200626 @Override
627 public void onAnimationEnd(Animator animation) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200628 if (clearAllExpandHack && !mCancelled) {
Jorim Jaggi2ae259d2014-08-04 23:35:47 +0200629 setExpandedHeightInternal(getMaxPanelHeight());
630 }
631 mHeightAnimator = null;
632 if (!mCancelled) {
633 notifyExpandingFinished();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200634 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200635 }
636 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200637 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200638 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400639 }
640
641 @Override
642 protected void onAttachedToWindow() {
643 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400644 mViewName = getResources().getResourceName(getId());
645 }
646
647 public String getName() {
648 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400649 }
650
Daniel Sandler08d05e32012-08-08 16:39:54 -0400651 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400652 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200653 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler08d05e32012-08-08 16:39:54 -0400654 }
655
Daniel Sandler50508132012-08-16 14:10:53 -0400656 @Override
657 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Daniel Sandler50508132012-08-16 14:10:53 -0400658 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100659 requestPanelHeightUpdate();
Selim Cinek31094df2014-08-14 19:28:15 +0200660 mHasLayoutedSinceDown = true;
661 if (mUpdateFlingOnLayout) {
662 abortAnimations();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700663 fling(mUpdateFlingVelocity, true /* expands */);
Selim Cinek31094df2014-08-14 19:28:15 +0200664 mUpdateFlingOnLayout = false;
665 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100666 }
667
668 protected void requestPanelHeightUpdate() {
669 float currentMaxPanelHeight = getMaxPanelHeight();
670
671 // If the user isn't actively poking us, let's update the height
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200672 if ((!mTracking || isTrackingBlocked())
673 && mHeightAnimator == null
Selim Cineka59ecc32015-04-07 10:51:49 -0700674 && !isShadeCollapsed()
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200675 && currentMaxPanelHeight != mExpandedHeight
676 && !mPeekPending
Selim Cinek31094df2014-08-14 19:28:15 +0200677 && mPeekAnimator == null
Jorim Jaggib7a33032014-08-20 16:21:36 +0200678 && !mPeekTouching) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200679 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100680 }
Daniel Sandler50508132012-08-16 14:10:53 -0400681 }
682
Daniel Sandler08d05e32012-08-08 16:39:54 -0400683 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200684 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
685 if (mHeightAnimator == null) {
686 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
687 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
688 setOverExpansion(overExpansionPixels, true /* isPixels */);
689 }
690 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
691 } else {
692 mExpandedHeight = h;
693 if (mOverExpandedBeforeFling) {
694 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
695 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100696 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400697
Jorim Jaggi93439da2014-06-30 23:53:39 +0200698 mExpandedHeight = Math.max(0, mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200699 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
700 ? 0
701 : mExpandedHeight / fhWithoutOverExpansion);
Jorim Jaggi06a0c3a2014-10-29 17:17:21 +0100702 onHeightUpdated(mExpandedHeight);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200703 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400704 }
705
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200706 /**
707 * @return true if the panel tracking should be temporarily blocked; this is used when a
708 * conflicting gesture (opening QS) is happening
709 */
710 protected abstract boolean isTrackingBlocked();
711
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200712 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200713
Jorim Jaggi90129582014-06-02 14:44:49 +0200714 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100715
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200716 protected abstract float getOverExpansionAmount();
717
718 protected abstract float getOverExpansionPixels();
719
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100720 /**
721 * This returns the maximum height of the panel. Children should override this if their
722 * desired height is not the full height.
723 *
724 * @return the default implementation simply returns the maximum height.
725 */
Selim Cinek31094df2014-08-14 19:28:15 +0200726 protected abstract int getMaxPanelHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400727
728 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100729 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400730 }
731
732 public float getExpandedHeight() {
733 return mExpandedHeight;
734 }
735
736 public float getExpandedFraction() {
737 return mExpandedFraction;
738 }
739
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700740 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100741 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700742 }
743
744 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400745 return mExpandedHeight <= 0;
746 }
747
748 public boolean isCollapsing() {
749 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700750 }
751
John Spurlocka4b70af2013-08-17 14:05:49 -0400752 public boolean isTracking() {
753 return mTracking;
754 }
755
Daniel Sandler08d05e32012-08-08 16:39:54 -0400756 public void setBar(PanelBar panelBar) {
757 mBar = panelBar;
758 }
759
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700760 public void collapse(boolean delayed, float speedUpFactor) {
John Spurlock97642182013-07-29 17:58:39 -0400761 if (DEBUG) logf("collapse: " + this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200762 if (mPeekPending || mPeekAnimator != null) {
763 mCollapseAfterPeek = true;
764 if (mPeekPending) {
765
766 // We know that the whole gesture is just a peek triggered by a simple click, so
767 // better start it now.
768 removeCallbacks(mPeekRunnable);
769 mPeekRunnable.run();
770 }
Jorim Jaggi98139552014-09-10 17:04:27 +0200771 } else if (!isFullyCollapsed() && !mTracking && !mClosing) {
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200772 cancelHeightAnimator();
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400773 mClosing = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200774 notifyExpandingStarted();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200775 if (delayed) {
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700776 mNextCollapseSpeedUpFactor = speedUpFactor;
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200777 postDelayed(mFlingCollapseRunnable, 120);
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200778 } else {
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700779 fling(0, false /* expand */, speedUpFactor);
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200780 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400781 }
782 }
783
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200784 private final Runnable mFlingCollapseRunnable = new Runnable() {
785 @Override
786 public void run() {
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700787 fling(0, false /* expand */, mNextCollapseSpeedUpFactor);
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200788 }
789 };
790
Daniel Sandler08d05e32012-08-08 16:39:54 -0400791 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400792 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400793 if (isFullyCollapsed()) {
794 mBar.startOpeningPanel(this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200795 notifyExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200796 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400797 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400798 if (DEBUG) logf("skipping expansion: is expanded");
799 }
800 }
801
802 public void cancelPeek() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200803 if (mPeekAnimator != null) {
John Spurlock97642182013-07-29 17:58:39 -0400804 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400805 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200806 removeCallbacks(mPeekRunnable);
807 mPeekPending = false;
808
809 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
810 // notify mBar that we might have closed ourselves.
811 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400812 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500813
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200814 public void instantExpand() {
815 mInstantExpanding = true;
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200816 mUpdateFlingOnLayout = false;
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200817 abortAnimations();
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200818 cancelPeek();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200819 if (mTracking) {
820 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
Jorim Jaggie62d5892014-09-02 16:31:19 +0200821 }
822 if (mExpanding) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200823 notifyExpandingFinished();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200824 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700825 notifyBarPanelExpansionChanged();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200826
827 // Wait for window manager to pickup the change, so we know the maximum height of the panel
828 // then.
829 getViewTreeObserver().addOnGlobalLayoutListener(
830 new ViewTreeObserver.OnGlobalLayoutListener() {
831 @Override
832 public void onGlobalLayout() {
833 if (mStatusBar.getStatusBarWindow().getHeight()
834 != mStatusBar.getStatusBarHeight()) {
835 getViewTreeObserver().removeOnGlobalLayoutListener(this);
836 setExpandedFraction(1f);
837 mInstantExpanding = false;
838 }
839 }
840 });
841
842 // Make sure a layout really happens.
843 requestLayout();
844 }
845
Selim Cinek6bb4a9b2014-10-09 17:48:05 -0700846 public void instantCollapse() {
847 abortAnimations();
848 setExpandedFraction(0f);
849 if (mExpanding) {
850 notifyExpandingFinished();
851 }
852 }
853
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200854 private void abortAnimations() {
855 cancelPeek();
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200856 cancelHeightAnimator();
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200857 removeCallbacks(mPostCollapseRunnable);
858 removeCallbacks(mFlingCollapseRunnable);
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200859 }
860
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200861 protected void onClosingFinished() {
862 mBar.onClosingFinished();
863 }
864
865
Jorim Jaggi90129582014-06-02 14:44:49 +0200866 protected void startUnlockHintAnimation() {
867
868 // We don't need to hint the user if an animation is already running or the user is changing
869 // the expansion.
870 if (mHeightAnimator != null || mTracking) {
871 return;
872 }
873 cancelPeek();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200874 notifyExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200875 startUnlockHintAnimationPhase1(new Runnable() {
876 @Override
877 public void run() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200878 notifyExpandingFinished();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200879 mStatusBar.onHintFinished();
880 mHintAnimationRunning = false;
881 }
882 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200883 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200884 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200885 }
886
887 /**
888 * Phase 1: Move everything upwards.
889 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200890 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200891 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
892 ValueAnimator animator = createHeightAnimator(target);
893 animator.setDuration(250);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200894 animator.setInterpolator(mFastOutSlowInInterpolator);
Jorim Jaggi90129582014-06-02 14:44:49 +0200895 animator.addListener(new AnimatorListenerAdapter() {
896 private boolean mCancelled;
897
898 @Override
899 public void onAnimationCancel(Animator animation) {
900 mCancelled = true;
901 }
902
903 @Override
904 public void onAnimationEnd(Animator animation) {
905 if (mCancelled) {
906 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200907 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200908 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200909 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200910 }
911 }
912 });
913 animator.start();
914 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200915 mKeyguardBottomArea.getIndicationView().animate()
Selim Cinek5256d932014-10-22 20:05:03 +0200916 .translationY(-mHintDistance)
Selim Cinekf99d0002014-06-13 07:36:01 +0200917 .setDuration(250)
Jorim Jaggib472b3472014-06-30 19:56:24 +0200918 .setInterpolator(mFastOutSlowInInterpolator)
Selim Cinekf99d0002014-06-13 07:36:01 +0200919 .withEndAction(new Runnable() {
920 @Override
921 public void run() {
922 mKeyguardBottomArea.getIndicationView().animate()
Selim Cinek5256d932014-10-22 20:05:03 +0200923 .translationY(0)
Selim Cinekf99d0002014-06-13 07:36:01 +0200924 .setDuration(450)
925 .setInterpolator(mBounceInterpolator)
926 .start();
927 }
928 })
929 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200930 }
931
932 /**
933 * Phase 2: Bounce down.
934 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200935 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200936 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
937 animator.setDuration(450);
938 animator.setInterpolator(mBounceInterpolator);
939 animator.addListener(new AnimatorListenerAdapter() {
940 @Override
941 public void onAnimationEnd(Animator animation) {
942 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200943 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200944 }
945 });
946 animator.start();
947 mHeightAnimator = animator;
948 }
949
950 private ValueAnimator createHeightAnimator(float targetHeight) {
951 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
952 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
953 @Override
954 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200955 setExpandedHeightInternal((Float) animation.getAnimatedValue());
Jorim Jaggi90129582014-06-02 14:44:49 +0200956 }
957 });
958 return animator;
959 }
960
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700961 protected void notifyBarPanelExpansionChanged() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200962 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700963 || mPeekAnimator != null || mInstantExpanding || mHeadsUpManager.hasPinnedHeadsUp());
Jorim Jaggib472b3472014-06-30 19:56:24 +0200964 }
965
Jorim Jaggi90129582014-06-02 14:44:49 +0200966 /**
967 * Gets called when the user performs a click anywhere in the empty area of the panel.
968 *
969 * @return whether the panel will be expanded after the action performed by this method
970 */
Selim Cinek3a9c10a2014-10-28 14:21:10 +0100971 protected boolean onEmptySpaceClick(float x) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200972 if (mHintAnimationRunning) {
973 return true;
974 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200975 if (x < mEdgeTapAreaWidth
976 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200977 onEdgeClicked(false /* right */);
978 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200979 } else if (x > getWidth() - mEdgeTapAreaWidth
980 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200981 onEdgeClicked(true /* right */);
982 return true;
983 } else {
984 return onMiddleClicked();
985 }
986 }
987
Jorim Jaggi1cf6e102015-01-20 16:45:05 +0100988 protected final Runnable mPostCollapseRunnable = new Runnable() {
Jorim Jaggi488b7922014-08-05 21:12:02 +0200989 @Override
990 public void run() {
Jorim Jaggif3b3bee2015-04-16 14:57:34 -0700991 collapse(false /* delayed */, 1.0f /* speedUpFactor */);
Jorim Jaggi488b7922014-08-05 21:12:02 +0200992 }
993 };
Jorim Jaggi488b7922014-08-05 21:12:02 +0200994
Jorim Jaggi1cf6e102015-01-20 16:45:05 +0100995 protected abstract boolean onMiddleClicked();
Jorim Jaggi90129582014-06-02 14:44:49 +0200996
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200997 protected abstract void onEdgeClicked(boolean right);
998
Jorim Jaggid7912d22014-09-30 17:38:19 +0200999 protected abstract boolean isDozing();
1000
Daniel Sandler37a38aa2013-02-13 17:15:57 -05001001 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +02001002 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
Jorim Jaggi50d87a72014-09-12 21:43:35 +02001003 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s touchDisabled=%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -05001004 + "]",
1005 this.getClass().getSimpleName(),
1006 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +01001007 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -05001008 mClosing?"T":"f",
1009 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -05001010 mJustPeeked?"T":"f",
1011 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi50d87a72014-09-12 21:43:35 +02001012 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":""),
1013 mTouchDisabled?"T":"f"
Daniel Sandler37a38aa2013-02-13 17:15:57 -05001014 ));
1015 }
Selim Cinek3c4635c2014-05-29 02:12:47 +02001016
1017 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +02001018
1019 protected abstract float getPeekHeight();
Jorim Jaggi30c305c2014-07-01 23:34:41 +02001020
1021 protected abstract float getCannedFlingDurationFactor();
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +02001022
1023 /**
1024 * @return whether "Clear all" button will be visible when the panel is fully expanded
1025 */
1026 protected abstract boolean fullyExpandedClearAllVisible();
1027
1028 protected abstract boolean isClearAllVisible();
1029
1030 /**
1031 * @return the height of the clear all button, in pixels
1032 */
1033 protected abstract int getClearAllHeight();
Selim Cinekb8f09cf2015-03-16 17:09:28 -07001034
Selim Cineka59ecc32015-04-07 10:51:49 -07001035 protected abstract boolean isShadeCollapsed();
1036
Selim Cinekb8f09cf2015-03-16 17:09:28 -07001037 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
1038 mHeadsUpManager = headsUpManager;
1039 }
Daniel Sandler08d05e32012-08-08 16:39:54 -04001040}