blob: e818d2350ddda59690362bf0a6a1170f2a2453e7 [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 Cinek19c8c702014-08-25 22:09:19 +0200482 if (!mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded()) {
483 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
492 protected void fling(float vel, boolean expand) {
493 cancelPeek();
494 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200495
496 // Hack to make the expand transition look nice when clear all button is visible - we make
497 // the animation only to the last notification, and then jump to the maximum panel height so
498 // clear all just fades in and the decelerating motion is towards the last notification.
499 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible()
500 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight()
501 && !isClearAllVisible();
502 if (clearAllExpandHack) {
503 target = getMaxPanelHeight() - getClearAllHeight();
504 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200505 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200506 notifyExpandingFinished();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200507 return;
508 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200509 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200510 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200511 if (expand) {
512 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
513 } else {
514 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
515 getHeight());
516
517 // Make it shorter if we run a canned animation
518 if (vel == 0) {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200519 animator.setDuration((long)
520 (animator.getDuration() * getCannedFlingDurationFactor()));
Jorim Jaggi1d480692014-05-20 19:41:58 +0200521 }
522 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200523 animator.addListener(new AnimatorListenerAdapter() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200524 private boolean mCancelled;
525
526 @Override
527 public void onAnimationCancel(Animator animation) {
528 mCancelled = true;
529 }
530
Jorim Jaggi1d480692014-05-20 19:41:58 +0200531 @Override
532 public void onAnimationEnd(Animator animation) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200533 if (clearAllExpandHack && !mCancelled) {
Jorim Jaggi2ae259d2014-08-04 23:35:47 +0200534 setExpandedHeightInternal(getMaxPanelHeight());
535 }
536 mHeightAnimator = null;
537 if (!mCancelled) {
538 notifyExpandingFinished();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200539 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200540 }
541 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200542 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200543 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400544 }
545
546 @Override
547 protected void onAttachedToWindow() {
548 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400549 mViewName = getResources().getResourceName(getId());
550 }
551
552 public String getName() {
553 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400554 }
555
Daniel Sandler08d05e32012-08-08 16:39:54 -0400556 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400557 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200558 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler08d05e32012-08-08 16:39:54 -0400559 }
560
Daniel Sandler50508132012-08-16 14:10:53 -0400561 @Override
562 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Daniel Sandler50508132012-08-16 14:10:53 -0400563 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100564 requestPanelHeightUpdate();
Selim Cinek31094df2014-08-14 19:28:15 +0200565 mHasLayoutedSinceDown = true;
566 if (mUpdateFlingOnLayout) {
567 abortAnimations();
568 fling(mUpdateFlingVelocity, true);
569 mUpdateFlingOnLayout = false;
570 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100571 }
572
573 protected void requestPanelHeightUpdate() {
574 float currentMaxPanelHeight = getMaxPanelHeight();
575
576 // If the user isn't actively poking us, let's update the height
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200577 if ((!mTracking || isTrackingBlocked())
578 && mHeightAnimator == null
579 && mExpandedHeight > 0
580 && currentMaxPanelHeight != mExpandedHeight
581 && !mPeekPending
Selim Cinek31094df2014-08-14 19:28:15 +0200582 && mPeekAnimator == null
Jorim Jaggib7a33032014-08-20 16:21:36 +0200583 && !mPeekTouching) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200584 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100585 }
Daniel Sandler50508132012-08-16 14:10:53 -0400586 }
587
Daniel Sandler08d05e32012-08-08 16:39:54 -0400588 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200589 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
590 if (mHeightAnimator == null) {
591 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
592 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
593 setOverExpansion(overExpansionPixels, true /* isPixels */);
594 }
595 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
596 } else {
597 mExpandedHeight = h;
598 if (mOverExpandedBeforeFling) {
599 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
600 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100601 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400602
Jorim Jaggi93439da2014-06-30 23:53:39 +0200603 mExpandedHeight = Math.max(0, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100604 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200605 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
606 ? 0
607 : mExpandedHeight / fhWithoutOverExpansion);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200608 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400609 }
610
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200611 /**
612 * @return true if the panel tracking should be temporarily blocked; this is used when a
613 * conflicting gesture (opening QS) is happening
614 */
615 protected abstract boolean isTrackingBlocked();
616
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200617 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200618
Jorim Jaggi90129582014-06-02 14:44:49 +0200619 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100620
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200621 protected abstract float getOverExpansionAmount();
622
623 protected abstract float getOverExpansionPixels();
624
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100625 /**
626 * This returns the maximum height of the panel. Children should override this if their
627 * desired height is not the full height.
628 *
629 * @return the default implementation simply returns the maximum height.
630 */
Selim Cinek31094df2014-08-14 19:28:15 +0200631 protected abstract int getMaxPanelHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400632
633 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100634 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400635 }
636
637 public float getExpandedHeight() {
638 return mExpandedHeight;
639 }
640
641 public float getExpandedFraction() {
642 return mExpandedFraction;
643 }
644
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700645 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100646 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700647 }
648
649 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400650 return mExpandedHeight <= 0;
651 }
652
653 public boolean isCollapsing() {
654 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700655 }
656
John Spurlocka4b70af2013-08-17 14:05:49 -0400657 public boolean isTracking() {
658 return mTracking;
659 }
660
Daniel Sandler08d05e32012-08-08 16:39:54 -0400661 public void setBar(PanelBar panelBar) {
662 mBar = panelBar;
663 }
664
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200665 public void collapse(boolean delayed) {
John Spurlock97642182013-07-29 17:58:39 -0400666 if (DEBUG) logf("collapse: " + this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200667 if (mPeekPending || mPeekAnimator != null) {
668 mCollapseAfterPeek = true;
669 if (mPeekPending) {
670
671 // We know that the whole gesture is just a peek triggered by a simple click, so
672 // better start it now.
673 removeCallbacks(mPeekRunnable);
674 mPeekRunnable.run();
675 }
Jorim Jaggifb98a502014-07-11 02:12:03 +0200676 } else if (!isFullyCollapsed() && !mTracking) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200677 if (mHeightAnimator != null) {
678 mHeightAnimator.cancel();
679 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400680 mClosing = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200681 notifyExpandingStarted();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200682 if (delayed) {
683 postDelayed(new Runnable() {
684 @Override
685 public void run() {
686 fling(0, false /* expand */);
687 }
688 }, 120);
689 } else {
690 fling(0, false /* expand */);
691 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400692 }
693 }
694
695 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400696 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400697 if (isFullyCollapsed()) {
698 mBar.startOpeningPanel(this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200699 notifyExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200700 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400701 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400702 if (DEBUG) logf("skipping expansion: is expanded");
703 }
704 }
705
706 public void cancelPeek() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200707 if (mPeekAnimator != null) {
John Spurlock97642182013-07-29 17:58:39 -0400708 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400709 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200710 removeCallbacks(mPeekRunnable);
711 mPeekPending = false;
712
713 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
714 // notify mBar that we might have closed ourselves.
715 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400716 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500717
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200718 public void instantExpand() {
719 mInstantExpanding = true;
720 abortAnimations();
721 if (mTracking) {
722 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
Jorim Jaggib472b3472014-06-30 19:56:24 +0200723 notifyExpandingFinished();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200724 }
725 setVisibility(VISIBLE);
726
727 // Wait for window manager to pickup the change, so we know the maximum height of the panel
728 // then.
729 getViewTreeObserver().addOnGlobalLayoutListener(
730 new ViewTreeObserver.OnGlobalLayoutListener() {
731 @Override
732 public void onGlobalLayout() {
733 if (mStatusBar.getStatusBarWindow().getHeight()
734 != mStatusBar.getStatusBarHeight()) {
735 getViewTreeObserver().removeOnGlobalLayoutListener(this);
736 setExpandedFraction(1f);
737 mInstantExpanding = false;
738 }
739 }
740 });
741
742 // Make sure a layout really happens.
743 requestLayout();
744 }
745
746 private void abortAnimations() {
747 cancelPeek();
748 if (mHeightAnimator != null) {
749 mHeightAnimator.cancel();
750 }
751 }
752
Jorim Jaggi90129582014-06-02 14:44:49 +0200753 protected void startUnlockHintAnimation() {
754
755 // We don't need to hint the user if an animation is already running or the user is changing
756 // the expansion.
757 if (mHeightAnimator != null || mTracking) {
758 return;
759 }
760 cancelPeek();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200761 notifyExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200762 startUnlockHintAnimationPhase1(new Runnable() {
763 @Override
764 public void run() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200765 notifyExpandingFinished();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200766 mStatusBar.onHintFinished();
767 mHintAnimationRunning = false;
768 }
769 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200770 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200771 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200772 }
773
774 /**
775 * Phase 1: Move everything upwards.
776 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200777 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200778 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
779 ValueAnimator animator = createHeightAnimator(target);
780 animator.setDuration(250);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200781 animator.setInterpolator(mFastOutSlowInInterpolator);
Jorim Jaggi90129582014-06-02 14:44:49 +0200782 animator.addListener(new AnimatorListenerAdapter() {
783 private boolean mCancelled;
784
785 @Override
786 public void onAnimationCancel(Animator animation) {
787 mCancelled = true;
788 }
789
790 @Override
791 public void onAnimationEnd(Animator animation) {
792 if (mCancelled) {
793 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200794 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200795 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200796 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200797 }
798 }
799 });
800 animator.start();
801 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200802 mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY();
803 mKeyguardBottomArea.getIndicationView().animate()
804 .y(mOriginalIndicationY - mHintDistance)
805 .setDuration(250)
Jorim Jaggib472b3472014-06-30 19:56:24 +0200806 .setInterpolator(mFastOutSlowInInterpolator)
Selim Cinekf99d0002014-06-13 07:36:01 +0200807 .withEndAction(new Runnable() {
808 @Override
809 public void run() {
810 mKeyguardBottomArea.getIndicationView().animate()
811 .y(mOriginalIndicationY)
812 .setDuration(450)
813 .setInterpolator(mBounceInterpolator)
814 .start();
815 }
816 })
817 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200818 }
819
820 /**
821 * Phase 2: Bounce down.
822 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200823 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200824 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
825 animator.setDuration(450);
826 animator.setInterpolator(mBounceInterpolator);
827 animator.addListener(new AnimatorListenerAdapter() {
828 @Override
829 public void onAnimationEnd(Animator animation) {
830 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200831 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200832 }
833 });
834 animator.start();
835 mHeightAnimator = animator;
836 }
837
838 private ValueAnimator createHeightAnimator(float targetHeight) {
839 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
840 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
841 @Override
842 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200843 setExpandedHeightInternal((Float) animation.getAnimatedValue());
Jorim Jaggi90129582014-06-02 14:44:49 +0200844 }
845 });
846 return animator;
847 }
848
Jorim Jaggib472b3472014-06-30 19:56:24 +0200849 private void notifyBarPanelExpansionChanged() {
850 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
851 || mPeekAnimator != null);
852 }
853
Jorim Jaggi90129582014-06-02 14:44:49 +0200854 /**
855 * Gets called when the user performs a click anywhere in the empty area of the panel.
856 *
857 * @return whether the panel will be expanded after the action performed by this method
858 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200859 private boolean onEmptySpaceClick(float x) {
860 if (mHintAnimationRunning) {
861 return true;
862 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200863 if (x < mEdgeTapAreaWidth
864 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200865 onEdgeClicked(false /* right */);
866 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200867 } else if (x > getWidth() - mEdgeTapAreaWidth
868 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200869 onEdgeClicked(true /* right */);
870 return true;
871 } else {
872 return onMiddleClicked();
873 }
874 }
875
Jorim Jaggi488b7922014-08-05 21:12:02 +0200876 private final Runnable mPostCollapseRunnable = new Runnable() {
877 @Override
878 public void run() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200879 collapse(false /* delayed */);
Jorim Jaggi488b7922014-08-05 21:12:02 +0200880 }
881 };
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200882 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200883 switch (mStatusBar.getBarState()) {
884 case StatusBarState.KEYGUARD:
885 startUnlockHintAnimation();
886 return true;
887 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200888 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200889 return true;
890 case StatusBarState.SHADE:
Jorim Jaggi488b7922014-08-05 21:12:02 +0200891
892 // This gets called in the middle of the touch handling, where the state is still
893 // that we are tracking the panel. Collapse the panel after this is done.
894 post(mPostCollapseRunnable);
Jorim Jaggi90129582014-06-02 14:44:49 +0200895 return false;
896 default:
897 return true;
898 }
899 }
900
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200901 protected abstract void onEdgeClicked(boolean right);
902
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500903 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200904 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400905 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500906 + "]",
907 this.getClass().getSimpleName(),
908 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100909 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500910 mClosing?"T":"f",
911 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500912 mJustPeeked?"T":"f",
913 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200914 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500915 ));
916 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200917
918 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200919
920 protected abstract float getPeekHeight();
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200921
922 protected abstract float getCannedFlingDurationFactor();
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200923
924 /**
925 * @return whether "Clear all" button will be visible when the panel is fully expanded
926 */
927 protected abstract boolean fullyExpandedClearAllVisible();
928
929 protected abstract boolean isClearAllVisible();
930
931 /**
932 * @return the height of the clear all button, in pixels
933 */
934 protected abstract int getClearAllHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400935}