blob: 2f21781edb222714c81cb8b1eeab7a8f9282d75a [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
35import com.android.systemui.R;
Jorim Jaggi1d480692014-05-20 19:41:58 +020036import com.android.systemui.statusbar.FlingAnimationUtils;
Jorim Jaggi90129582014-06-02 14:44:49 +020037import com.android.systemui.statusbar.StatusBarState;
Daniel Sandler08d05e32012-08-08 16:39:54 -040038
John Spurlockde84f0e2013-06-12 12:41:00 -040039import java.io.FileDescriptor;
40import java.io.PrintWriter;
John Spurlockde84f0e2013-06-12 12:41:00 -040041
Selim Cinek4c6969a2014-05-26 19:22:17 +020042public abstract class PanelView extends FrameLayout {
Daniel Sandler198a0302012-08-17 16:04:31 -040043 public static final boolean DEBUG = PanelBar.DEBUG;
Daniel Sandler08d05e32012-08-08 16:39:54 -040044 public static final String TAG = PanelView.class.getSimpleName();
Daniel Sandlere7c5bbb2013-03-05 13:36:21 -050045
John Spurlock97642182013-07-29 17:58:39 -040046 private final void logf(String fmt, Object... args) {
John Spurlockcd686b52013-06-05 10:13:46 -040047 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args));
Daniel Sandler08d05e32012-08-08 16:39:54 -040048 }
49
Jorim Jaggi90129582014-06-02 14:44:49 +020050 protected PhoneStatusBar mStatusBar;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040051 private float mPeekHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +020052 private float mHintDistance;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020053 private int mEdgeTapAreaWidth;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010054 private float mInitialOffsetOnTouch;
Daniel Sandler08d05e32012-08-08 16:39:54 -040055 private float mExpandedFraction = 0;
Selim Cinek1408eb52014-06-02 14:45:38 +020056 protected float mExpandedHeight = 0;
Selim Cinek31094df2014-08-14 19:28:15 +020057 private boolean mPanelClosedOnDown;
58 private boolean mHasLayoutedSinceDown;
59 private float mUpdateFlingVelocity;
60 private boolean mUpdateFlingOnLayout;
Jorim Jaggib7a33032014-08-20 16:21:36 +020061 private boolean mPeekTouching;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040062 private boolean mJustPeeked;
Daniel Sandler50508132012-08-16 14:10:53 -040063 private boolean mClosing;
Jorim Jaggi8dd95e02014-06-03 16:19:33 +020064 protected boolean mTracking;
Jorim Jaggi90129582014-06-02 14:44:49 +020065 private boolean mTouchSlopExceeded;
John Spurlock48fa91a2013-08-15 09:29:31 -040066 private int mTrackingPointer;
Jorim Jaggid7daab72014-05-06 22:22:20 +020067 protected int mTouchSlop;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020068 protected boolean mHintAnimationRunning;
Jorim Jaggi47c85a32014-06-05 17:25:40 +020069 private boolean mOverExpandedBeforeFling;
Selim Cinekf99d0002014-06-13 07:36:01 +020070 private float mOriginalIndicationY;
Selim Cinek19c8c702014-08-25 22:09:19 +020071 private boolean mTouchAboveFalsingThreshold;
72 private int mUnlockFalsingThreshold;
Daniel Sandler08d05e32012-08-08 16:39:54 -040073
Jorim Jaggi1d480692014-05-20 19:41:58 +020074 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040075 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020076 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020077 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040078
Jorim Jaggi0a27be82014-06-11 03:22:39 +020079 /**
80 * Whether an instant expand request is currently pending and we are just waiting for layout.
81 */
82 private boolean mInstantExpanding;
83
Daniel Sandler08d05e32012-08-08 16:39:54 -040084 PanelBar mBar;
85
Daniel Sandler50508132012-08-16 14:10:53 -040086 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020087 private float mInitialTouchY;
88 private float mInitialTouchX;
Daniel Sandler08d05e32012-08-08 16:39:54 -040089
Jorim Jaggi90129582014-06-02 14:44:49 +020090 private Interpolator mLinearOutSlowInInterpolator;
Jorim Jaggib472b3472014-06-30 19:56:24 +020091 private Interpolator mFastOutSlowInInterpolator;
Jorim Jaggi90129582014-06-02 14:44:49 +020092 private Interpolator mBounceInterpolator;
Selim Cinekf99d0002014-06-13 07:36:01 +020093 protected KeyguardBottomAreaView mKeyguardBottomArea;
Jorim Jaggi90129582014-06-02 14:44:49 +020094
Jorim Jaggib472b3472014-06-30 19:56:24 +020095 private boolean mPeekPending;
96 private boolean mCollapseAfterPeek;
97 private boolean mExpanding;
98 private boolean mGestureWaitForTouchSlop;
99 private Runnable mPeekRunnable = new Runnable() {
100 @Override
101 public void run() {
102 mPeekPending = false;
103 runPeekAnimation();
104 }
105 };
106
Selim Cinek1685e632014-04-08 02:27:49 +0200107 protected void onExpandingFinished() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200108 mClosing = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200109 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +0200110 }
111
112 protected void onExpandingStarted() {
113 }
114
Jorim Jaggib472b3472014-06-30 19:56:24 +0200115 private void notifyExpandingStarted() {
116 if (!mExpanding) {
117 mExpanding = true;
118 onExpandingStarted();
119 }
120 }
121
122 private void notifyExpandingFinished() {
123 if (mExpanding) {
124 mExpanding = false;
125 onExpandingFinished();
126 }
127 }
128
129 private void schedulePeek() {
130 mPeekPending = true;
131 long timeout = ViewConfiguration.getTapTimeout();
132 postOnAnimationDelayed(mPeekRunnable, timeout);
133 notifyBarPanelExpansionChanged();
134 }
135
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400136 private void runPeekAnimation() {
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200137 mPeekHeight = getPeekHeight();
John Spurlock97642182013-07-29 17:58:39 -0400138 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200139 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400140 return;
141 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200142 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight)
143 .setDuration(250);
144 mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator);
145 mPeekAnimator.addListener(new AnimatorListenerAdapter() {
146 private boolean mCancelled;
147
148 @Override
149 public void onAnimationCancel(Animator animation) {
150 mCancelled = true;
151 }
152
153 @Override
154 public void onAnimationEnd(Animator animation) {
155 mPeekAnimator = null;
156 if (mCollapseAfterPeek && !mCancelled) {
157 postOnAnimation(new Runnable() {
158 @Override
159 public void run() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200160 collapse(false /* delayed */);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200161 }
162 });
163 }
164 mCollapseAfterPeek = false;
165 }
166 });
167 notifyExpandingStarted();
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400168 mPeekAnimator.start();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200169 mJustPeeked = true;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400170 }
171
Daniel Sandler08d05e32012-08-08 16:39:54 -0400172 public PanelView(Context context, AttributeSet attrs) {
173 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200174 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200175 mFastOutSlowInInterpolator =
Jorim Jaggi90129582014-06-02 14:44:49 +0200176 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200177 mLinearOutSlowInInterpolator =
178 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
Jorim Jaggi90129582014-06-02 14:44:49 +0200179 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400180 }
181
Jorim Jaggi069cd032014-05-15 03:09:01 +0200182 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400183 final Resources res = getContext().getResources();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100184 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
185 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200186 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200187 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Selim Cinek19c8c702014-08-25 22:09:19 +0200188 mUnlockFalsingThreshold = res.getDimensionPixelSize(R.dimen.unlock_falsing_threshold);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400189 }
190
191 private void trackMovement(MotionEvent event) {
192 // Add movement to velocity tracker using raw screen X and Y coordinates instead
193 // of window coordinates because the window frame may be moving at the same time.
194 float deltaX = event.getRawX() - event.getX();
195 float deltaY = event.getRawY() - event.getY();
196 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400197 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400198 event.offsetLocation(-deltaX, -deltaY);
199 }
200
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400201 @Override
202 public boolean onTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200203 if (mInstantExpanding) {
204 return false;
205 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100206
207 /*
208 * We capture touch events here and update the expand height here in case according to
209 * the users fingers. This also handles multi-touch.
210 *
211 * If the user just clicks shortly, we give him a quick peek of the shade.
212 *
213 * Flinging is also enabled in order to open or close the shade.
214 */
215
216 int pointerIndex = event.findPointerIndex(mTrackingPointer);
217 if (pointerIndex < 0) {
218 pointerIndex = 0;
219 mTrackingPointer = event.getPointerId(pointerIndex);
220 }
221 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200222 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100223
Jorim Jaggib472b3472014-06-30 19:56:24 +0200224 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
225 mGestureWaitForTouchSlop = mExpandedHeight == 0f;
226 }
227 boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200228
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100229 switch (event.getActionMasked()) {
230 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100231 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200232 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200233 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200234 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200235 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200236 mPanelClosedOnDown = mExpandedHeight == 0.0f;
237 mHasLayoutedSinceDown = false;
238 mUpdateFlingOnLayout = false;
Jorim Jaggib7a33032014-08-20 16:21:36 +0200239 mPeekTouching = mPanelClosedOnDown;
Selim Cinek19c8c702014-08-25 22:09:19 +0200240 mTouchAboveFalsingThreshold = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200241 if (mVelocityTracker == null) {
242 initVelocityTracker();
243 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100244 trackMovement(event);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200245 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) ||
246 mPeekPending || mPeekAnimator != null) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200247 if (mHeightAnimator != null) {
248 mHeightAnimator.cancel(); // end any outstanding animations
249 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200250 cancelPeek();
251 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning)
252 || mPeekPending || mPeekAnimator != null;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200253 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200254 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100255 if (mExpandedHeight == 0) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200256 schedulePeek();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100257 }
258 break;
259
260 case MotionEvent.ACTION_POINTER_UP:
261 final int upPointer = event.getPointerId(event.getActionIndex());
262 if (mTrackingPointer == upPointer) {
263 // gesture is ongoing, find a new pointer to track
264 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
265 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200266 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100267 mTrackingPointer = event.getPointerId(newIndex);
268 mInitialOffsetOnTouch = mExpandedHeight;
269 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200270 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100271 }
272 break;
273
274 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200275 float h = y - mInitialTouchY;
Jorim Jaggib7240132014-06-30 01:39:07 +0200276
277 // If the panel was collapsed when touching, we only need to check for the
278 // y-component of the gesture, as we have no conflicting horizontal gesture.
279 if (Math.abs(h) > mTouchSlop
280 && (Math.abs(h) > Math.abs(x - mInitialTouchX)
281 || mInitialOffsetOnTouch == 0f)) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200282 mTouchSlopExceeded = true;
283 if (waitForTouchSlop && !mTracking) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200284 if (!mJustPeeked) {
285 mInitialOffsetOnTouch = mExpandedHeight;
286 mInitialTouchX = x;
287 mInitialTouchY = y;
288 h = 0;
289 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200290 if (mHeightAnimator != null) {
291 mHeightAnimator.cancel(); // end any outstanding animations
292 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200293 removeCallbacks(mPeekRunnable);
294 mPeekPending = false;
Jorim Jaggi90129582014-06-02 14:44:49 +0200295 onTrackingStarted();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200296 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200297 }
Jorim Jaggi93439da2014-06-30 23:53:39 +0200298 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200299 if (newHeight > mPeekHeight) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200300 if (mPeekAnimator != null) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100301 mPeekAnimator.cancel();
302 }
303 mJustPeeked = false;
304 }
Selim Cinek19c8c702014-08-25 22:09:19 +0200305 if (-h >= mUnlockFalsingThreshold) {
306 mTouchAboveFalsingThreshold = true;
307 }
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200308 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) {
Jorim Jaggicc693242014-06-14 03:04:35 +0000309 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100310 }
311
312 trackMovement(event);
313 break;
314
315 case MotionEvent.ACTION_UP:
316 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100317 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100318 trackMovement(event);
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200319 if ((mTracking && mTouchSlopExceeded)
Jorim Jaggidc96d632014-07-01 18:48:52 +0200320 || Math.abs(x - mInitialTouchX) > mTouchSlop
321 || Math.abs(y - mInitialTouchY) > mTouchSlop
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200322 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
Jorim Jaggib7240132014-06-30 01:39:07 +0200323 float vel = 0f;
324 float vectorVel = 0f;
325 if (mVelocityTracker != null) {
326 mVelocityTracker.computeCurrentVelocity(1000);
327 vel = mVelocityTracker.getYVelocity();
328 vectorVel = (float) Math.hypot(
329 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
330 }
331 boolean expand = flingExpands(vel, vectorVel);
Jorim Jaggi90129582014-06-02 14:44:49 +0200332 onTrackingStopped(expand);
333 fling(vel, expand);
Selim Cinek31094df2014-08-14 19:28:15 +0200334 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown;
335 if (mUpdateFlingOnLayout) {
336 mUpdateFlingVelocity = vel;
337 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200338 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200339 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200340 onTrackingStopped(expands);
341 }
Jorim Jaggidc96d632014-07-01 18:48:52 +0200342
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100343 if (mVelocityTracker != null) {
344 mVelocityTracker.recycle();
345 mVelocityTracker = null;
346 }
Jorim Jaggib7a33032014-08-20 16:21:36 +0200347 mPeekTouching = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100348 break;
349 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200350 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100351 }
352
Selim Cinek4c6969a2014-05-26 19:22:17 +0200353 protected abstract boolean hasConflictingGestures();
354
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200355 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200356 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200357 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200358 }
359
360 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200361 mTracking = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200362 mCollapseAfterPeek = false;
Selim Cinek1685e632014-04-08 02:27:49 +0200363 mBar.onTrackingStarted(PanelView.this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200364 notifyExpandingStarted();
Selim Cinek1685e632014-04-08 02:27:49 +0200365 }
366
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100367 @Override
368 public boolean onInterceptTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200369 if (mInstantExpanding) {
370 return false;
371 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100372
373 /*
374 * If the user drags anywhere inside the panel we intercept it if he moves his finger
375 * upwards. This allows closing the shade from anywhere inside the panel.
376 *
377 * We only do this if the current content is scrolled to the bottom,
378 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
379 * possible.
380 */
381 int pointerIndex = event.findPointerIndex(mTrackingPointer);
382 if (pointerIndex < 0) {
383 pointerIndex = 0;
384 mTrackingPointer = event.getPointerId(pointerIndex);
385 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200386 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100387 final float y = event.getY(pointerIndex);
388 boolean scrolledToBottom = isScrolledToBottom();
389
390 switch (event.getActionMasked()) {
391 case MotionEvent.ACTION_DOWN:
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200392 mStatusBar.userActivity();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200393 if (mHeightAnimator != null && !mHintAnimationRunning ||
394 mPeekPending || mPeekAnimator != null) {
395 if (mHeightAnimator != null) {
396 mHeightAnimator.cancel(); // end any outstanding animations
397 }
398 cancelPeek();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200399 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200400 return true;
401 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100402 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200403 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200404 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200405 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200406 mPanelClosedOnDown = mExpandedHeight == 0.0f;
407 mHasLayoutedSinceDown = false;
408 mUpdateFlingOnLayout = false;
Selim Cinek19c8c702014-08-25 22:09:19 +0200409 mTouchAboveFalsingThreshold = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100410 initVelocityTracker();
411 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100412 break;
413 case MotionEvent.ACTION_POINTER_UP:
414 final int upPointer = event.getPointerId(event.getActionIndex());
415 if (mTrackingPointer == upPointer) {
416 // gesture is ongoing, find a new pointer to track
417 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
418 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200419 mInitialTouchX = event.getX(newIndex);
420 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100421 }
422 break;
423
424 case MotionEvent.ACTION_MOVE:
425 final float h = y - mInitialTouchY;
426 trackMovement(event);
427 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200428 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200429 if (mHeightAnimator != null) {
430 mHeightAnimator.cancel();
431 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100432 mInitialOffsetOnTouch = mExpandedHeight;
433 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200434 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200435 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200436 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200437 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100438 return true;
439 }
440 }
441 break;
Selim Cinek31094df2014-08-14 19:28:15 +0200442 case MotionEvent.ACTION_CANCEL:
443 case MotionEvent.ACTION_UP:
Selim Cinek31094df2014-08-14 19:28:15 +0200444 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100445 }
446 return false;
447 }
448
449 private void initVelocityTracker() {
450 if (mVelocityTracker != null) {
451 mVelocityTracker.recycle();
452 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200453 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100454 }
455
456 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200457 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100458 }
459
460 protected float getContentHeight() {
461 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400462 }
463
Daniel Sandler08d05e32012-08-08 16:39:54 -0400464 @Override
465 protected void onFinishInflate() {
466 super.onFinishInflate();
467 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400468 }
469
Jorim Jaggi069cd032014-05-15 03:09:01 +0200470 @Override
471 protected void onConfigurationChanged(Configuration newConfig) {
472 super.onConfigurationChanged(newConfig);
473 loadDimens();
Jorim Jaggi069cd032014-05-15 03:09:01 +0200474 }
475
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200476 /**
Jorim Jaggib7240132014-06-30 01:39:07 +0200477 * @param vel the current vertical velocity of the motion
478 * @param vectorVel the length of the vectorial velocity
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200479 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200480 */
Jorim Jaggidc96d632014-07-01 18:48:52 +0200481 protected boolean flingExpands(float vel, float vectorVel) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200482 if (isBelowFalsingThreshold()) {
Selim Cinek19c8c702014-08-25 22:09:19 +0200483 return true;
484 }
Jorim Jaggib7240132014-06-30 01:39:07 +0200485 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200486 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200487 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200488 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400489 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200490 }
491
Selim Cinek5386fb32014-09-03 16:37:36 +0200492 private boolean isBelowFalsingThreshold() {
493 return !mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded();
494 }
495
Jorim Jaggi1d480692014-05-20 19:41:58 +0200496 protected void fling(float vel, boolean expand) {
497 cancelPeek();
498 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200499
500 // Hack to make the expand transition look nice when clear all button is visible - we make
501 // the animation only to the last notification, and then jump to the maximum panel height so
502 // clear all just fades in and the decelerating motion is towards the last notification.
503 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible()
504 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight()
505 && !isClearAllVisible();
506 if (clearAllExpandHack) {
507 target = getMaxPanelHeight() - getClearAllHeight();
508 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200509 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200510 notifyExpandingFinished();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200511 return;
512 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200513 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200514 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200515 if (expand) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200516 boolean belowFalsingThreshold = isBelowFalsingThreshold();
517 if (belowFalsingThreshold) {
518 vel = 0;
519 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200520 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
Selim Cinek5386fb32014-09-03 16:37:36 +0200521 if (belowFalsingThreshold) {
522 animator.setDuration(350);
523 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200524 } else {
525 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
526 getHeight());
527
528 // Make it shorter if we run a canned animation
529 if (vel == 0) {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200530 animator.setDuration((long)
531 (animator.getDuration() * getCannedFlingDurationFactor()));
Jorim Jaggi1d480692014-05-20 19:41:58 +0200532 }
533 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200534 animator.addListener(new AnimatorListenerAdapter() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200535 private boolean mCancelled;
536
537 @Override
538 public void onAnimationCancel(Animator animation) {
539 mCancelled = true;
540 }
541
Jorim Jaggi1d480692014-05-20 19:41:58 +0200542 @Override
543 public void onAnimationEnd(Animator animation) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200544 if (clearAllExpandHack && !mCancelled) {
Jorim Jaggi2ae259d2014-08-04 23:35:47 +0200545 setExpandedHeightInternal(getMaxPanelHeight());
546 }
547 mHeightAnimator = null;
548 if (!mCancelled) {
549 notifyExpandingFinished();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200550 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200551 }
552 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200553 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200554 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400555 }
556
557 @Override
558 protected void onAttachedToWindow() {
559 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400560 mViewName = getResources().getResourceName(getId());
561 }
562
563 public String getName() {
564 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400565 }
566
Daniel Sandler08d05e32012-08-08 16:39:54 -0400567 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400568 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200569 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler08d05e32012-08-08 16:39:54 -0400570 }
571
Daniel Sandler50508132012-08-16 14:10:53 -0400572 @Override
573 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Daniel Sandler50508132012-08-16 14:10:53 -0400574 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100575 requestPanelHeightUpdate();
Selim Cinek31094df2014-08-14 19:28:15 +0200576 mHasLayoutedSinceDown = true;
577 if (mUpdateFlingOnLayout) {
578 abortAnimations();
579 fling(mUpdateFlingVelocity, true);
580 mUpdateFlingOnLayout = false;
581 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100582 }
583
584 protected void requestPanelHeightUpdate() {
585 float currentMaxPanelHeight = getMaxPanelHeight();
586
587 // If the user isn't actively poking us, let's update the height
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200588 if ((!mTracking || isTrackingBlocked())
589 && mHeightAnimator == null
590 && mExpandedHeight > 0
591 && currentMaxPanelHeight != mExpandedHeight
592 && !mPeekPending
Selim Cinek31094df2014-08-14 19:28:15 +0200593 && mPeekAnimator == null
Jorim Jaggib7a33032014-08-20 16:21:36 +0200594 && !mPeekTouching) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200595 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100596 }
Daniel Sandler50508132012-08-16 14:10:53 -0400597 }
598
Daniel Sandler08d05e32012-08-08 16:39:54 -0400599 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200600 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
601 if (mHeightAnimator == null) {
602 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
603 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
604 setOverExpansion(overExpansionPixels, true /* isPixels */);
605 }
606 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
607 } else {
608 mExpandedHeight = h;
609 if (mOverExpandedBeforeFling) {
610 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
611 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100612 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400613
Jorim Jaggi93439da2014-06-30 23:53:39 +0200614 mExpandedHeight = Math.max(0, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100615 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200616 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
617 ? 0
618 : mExpandedHeight / fhWithoutOverExpansion);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200619 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400620 }
621
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200622 /**
623 * @return true if the panel tracking should be temporarily blocked; this is used when a
624 * conflicting gesture (opening QS) is happening
625 */
626 protected abstract boolean isTrackingBlocked();
627
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200628 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200629
Jorim Jaggi90129582014-06-02 14:44:49 +0200630 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100631
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200632 protected abstract float getOverExpansionAmount();
633
634 protected abstract float getOverExpansionPixels();
635
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100636 /**
637 * This returns the maximum height of the panel. Children should override this if their
638 * desired height is not the full height.
639 *
640 * @return the default implementation simply returns the maximum height.
641 */
Selim Cinek31094df2014-08-14 19:28:15 +0200642 protected abstract int getMaxPanelHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400643
644 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100645 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400646 }
647
648 public float getExpandedHeight() {
649 return mExpandedHeight;
650 }
651
652 public float getExpandedFraction() {
653 return mExpandedFraction;
654 }
655
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700656 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100657 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700658 }
659
660 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400661 return mExpandedHeight <= 0;
662 }
663
664 public boolean isCollapsing() {
665 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700666 }
667
John Spurlocka4b70af2013-08-17 14:05:49 -0400668 public boolean isTracking() {
669 return mTracking;
670 }
671
Daniel Sandler08d05e32012-08-08 16:39:54 -0400672 public void setBar(PanelBar panelBar) {
673 mBar = panelBar;
674 }
675
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200676 public void collapse(boolean delayed) {
John Spurlock97642182013-07-29 17:58:39 -0400677 if (DEBUG) logf("collapse: " + this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200678 if (mPeekPending || mPeekAnimator != null) {
679 mCollapseAfterPeek = true;
680 if (mPeekPending) {
681
682 // We know that the whole gesture is just a peek triggered by a simple click, so
683 // better start it now.
684 removeCallbacks(mPeekRunnable);
685 mPeekRunnable.run();
686 }
Jorim Jaggi98139552014-09-10 17:04:27 +0200687 } else if (!isFullyCollapsed() && !mTracking && !mClosing) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200688 if (mHeightAnimator != null) {
689 mHeightAnimator.cancel();
690 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400691 mClosing = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200692 notifyExpandingStarted();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200693 if (delayed) {
694 postDelayed(new Runnable() {
695 @Override
696 public void run() {
697 fling(0, false /* expand */);
698 }
699 }, 120);
700 } else {
701 fling(0, false /* expand */);
702 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400703 }
704 }
705
706 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400707 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400708 if (isFullyCollapsed()) {
709 mBar.startOpeningPanel(this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200710 notifyExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200711 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400712 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400713 if (DEBUG) logf("skipping expansion: is expanded");
714 }
715 }
716
717 public void cancelPeek() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200718 if (mPeekAnimator != null) {
John Spurlock97642182013-07-29 17:58:39 -0400719 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400720 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200721 removeCallbacks(mPeekRunnable);
722 mPeekPending = false;
723
724 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
725 // notify mBar that we might have closed ourselves.
726 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400727 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500728
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200729 public void instantExpand() {
730 mInstantExpanding = true;
731 abortAnimations();
732 if (mTracking) {
733 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
Jorim Jaggie62d5892014-09-02 16:31:19 +0200734 }
735 if (mExpanding) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200736 notifyExpandingFinished();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200737 }
738 setVisibility(VISIBLE);
739
740 // Wait for window manager to pickup the change, so we know the maximum height of the panel
741 // then.
742 getViewTreeObserver().addOnGlobalLayoutListener(
743 new ViewTreeObserver.OnGlobalLayoutListener() {
744 @Override
745 public void onGlobalLayout() {
746 if (mStatusBar.getStatusBarWindow().getHeight()
747 != mStatusBar.getStatusBarHeight()) {
748 getViewTreeObserver().removeOnGlobalLayoutListener(this);
749 setExpandedFraction(1f);
750 mInstantExpanding = false;
751 }
752 }
753 });
754
755 // Make sure a layout really happens.
756 requestLayout();
757 }
758
759 private void abortAnimations() {
760 cancelPeek();
761 if (mHeightAnimator != null) {
762 mHeightAnimator.cancel();
763 }
764 }
765
Jorim Jaggi90129582014-06-02 14:44:49 +0200766 protected void startUnlockHintAnimation() {
767
768 // We don't need to hint the user if an animation is already running or the user is changing
769 // the expansion.
770 if (mHeightAnimator != null || mTracking) {
771 return;
772 }
773 cancelPeek();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200774 notifyExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200775 startUnlockHintAnimationPhase1(new Runnable() {
776 @Override
777 public void run() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200778 notifyExpandingFinished();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200779 mStatusBar.onHintFinished();
780 mHintAnimationRunning = false;
781 }
782 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200783 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200784 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200785 }
786
787 /**
788 * Phase 1: Move everything upwards.
789 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200790 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200791 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
792 ValueAnimator animator = createHeightAnimator(target);
793 animator.setDuration(250);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200794 animator.setInterpolator(mFastOutSlowInInterpolator);
Jorim Jaggi90129582014-06-02 14:44:49 +0200795 animator.addListener(new AnimatorListenerAdapter() {
796 private boolean mCancelled;
797
798 @Override
799 public void onAnimationCancel(Animator animation) {
800 mCancelled = true;
801 }
802
803 @Override
804 public void onAnimationEnd(Animator animation) {
805 if (mCancelled) {
806 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200807 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200808 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200809 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200810 }
811 }
812 });
813 animator.start();
814 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200815 mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY();
816 mKeyguardBottomArea.getIndicationView().animate()
817 .y(mOriginalIndicationY - mHintDistance)
818 .setDuration(250)
Jorim Jaggib472b3472014-06-30 19:56:24 +0200819 .setInterpolator(mFastOutSlowInInterpolator)
Selim Cinekf99d0002014-06-13 07:36:01 +0200820 .withEndAction(new Runnable() {
821 @Override
822 public void run() {
823 mKeyguardBottomArea.getIndicationView().animate()
824 .y(mOriginalIndicationY)
825 .setDuration(450)
826 .setInterpolator(mBounceInterpolator)
827 .start();
828 }
829 })
830 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200831 }
832
833 /**
834 * Phase 2: Bounce down.
835 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200836 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200837 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
838 animator.setDuration(450);
839 animator.setInterpolator(mBounceInterpolator);
840 animator.addListener(new AnimatorListenerAdapter() {
841 @Override
842 public void onAnimationEnd(Animator animation) {
843 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200844 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200845 }
846 });
847 animator.start();
848 mHeightAnimator = animator;
849 }
850
851 private ValueAnimator createHeightAnimator(float targetHeight) {
852 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
853 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
854 @Override
855 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200856 setExpandedHeightInternal((Float) animation.getAnimatedValue());
Jorim Jaggi90129582014-06-02 14:44:49 +0200857 }
858 });
859 return animator;
860 }
861
Jorim Jaggib472b3472014-06-30 19:56:24 +0200862 private void notifyBarPanelExpansionChanged() {
863 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
864 || mPeekAnimator != null);
865 }
866
Jorim Jaggi90129582014-06-02 14:44:49 +0200867 /**
868 * Gets called when the user performs a click anywhere in the empty area of the panel.
869 *
870 * @return whether the panel will be expanded after the action performed by this method
871 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200872 private boolean onEmptySpaceClick(float x) {
873 if (mHintAnimationRunning) {
874 return true;
875 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200876 if (x < mEdgeTapAreaWidth
877 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200878 onEdgeClicked(false /* right */);
879 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200880 } else if (x > getWidth() - mEdgeTapAreaWidth
881 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200882 onEdgeClicked(true /* right */);
883 return true;
884 } else {
885 return onMiddleClicked();
886 }
887 }
888
Jorim Jaggi488b7922014-08-05 21:12:02 +0200889 private final Runnable mPostCollapseRunnable = new Runnable() {
890 @Override
891 public void run() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200892 collapse(false /* delayed */);
Jorim Jaggi488b7922014-08-05 21:12:02 +0200893 }
894 };
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200895 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200896 switch (mStatusBar.getBarState()) {
897 case StatusBarState.KEYGUARD:
898 startUnlockHintAnimation();
899 return true;
900 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200901 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200902 return true;
903 case StatusBarState.SHADE:
Jorim Jaggi488b7922014-08-05 21:12:02 +0200904
905 // This gets called in the middle of the touch handling, where the state is still
906 // that we are tracking the panel. Collapse the panel after this is done.
907 post(mPostCollapseRunnable);
Jorim Jaggi90129582014-06-02 14:44:49 +0200908 return false;
909 default:
910 return true;
911 }
912 }
913
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200914 protected abstract void onEdgeClicked(boolean right);
915
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500916 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200917 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400918 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500919 + "]",
920 this.getClass().getSimpleName(),
921 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100922 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500923 mClosing?"T":"f",
924 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500925 mJustPeeked?"T":"f",
926 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200927 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500928 ));
929 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200930
931 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200932
933 protected abstract float getPeekHeight();
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200934
935 protected abstract float getCannedFlingDurationFactor();
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200936
937 /**
938 * @return whether "Clear all" button will be visible when the panel is fully expanded
939 */
940 protected abstract boolean fullyExpandedClearAllVisible();
941
942 protected abstract boolean isClearAllVisible();
943
944 /**
945 * @return the height of the clear all button, in pixels
946 */
947 protected abstract int getClearAllHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400948}