blob: c3b263dbff6c41b8feee96273b14df701b6ccd00 [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 Jaggib01287f2014-09-12 02:12:26 +0200137 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
138 Log.i(TAG, "Starting peek animation");
139 }
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200140 mPeekHeight = getPeekHeight();
John Spurlock97642182013-07-29 17:58:39 -0400141 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200142 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400143 return;
144 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200145 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight)
146 .setDuration(250);
147 mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator);
148 mPeekAnimator.addListener(new AnimatorListenerAdapter() {
149 private boolean mCancelled;
150
151 @Override
152 public void onAnimationCancel(Animator animation) {
153 mCancelled = true;
154 }
155
156 @Override
157 public void onAnimationEnd(Animator animation) {
158 mPeekAnimator = null;
159 if (mCollapseAfterPeek && !mCancelled) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200160 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
161 Log.i(TAG, "Peek animation finished, posting collapse");
162 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200163 postOnAnimation(new Runnable() {
164 @Override
165 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200166 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
167 Log.i(TAG, "Peek animation finished, collapsing");
168 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200169 collapse(false /* delayed */);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200170 }
171 });
172 }
173 mCollapseAfterPeek = false;
174 }
175 });
176 notifyExpandingStarted();
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400177 mPeekAnimator.start();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200178 mJustPeeked = true;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400179 }
180
Daniel Sandler08d05e32012-08-08 16:39:54 -0400181 public PanelView(Context context, AttributeSet attrs) {
182 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200183 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200184 mFastOutSlowInInterpolator =
Jorim Jaggi90129582014-06-02 14:44:49 +0200185 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200186 mLinearOutSlowInInterpolator =
187 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
Jorim Jaggi90129582014-06-02 14:44:49 +0200188 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400189 }
190
Jorim Jaggi069cd032014-05-15 03:09:01 +0200191 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400192 final Resources res = getContext().getResources();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100193 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
194 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200195 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200196 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Selim Cinek19c8c702014-08-25 22:09:19 +0200197 mUnlockFalsingThreshold = res.getDimensionPixelSize(R.dimen.unlock_falsing_threshold);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400198 }
199
200 private void trackMovement(MotionEvent event) {
201 // Add movement to velocity tracker using raw screen X and Y coordinates instead
202 // of window coordinates because the window frame may be moving at the same time.
203 float deltaX = event.getRawX() - event.getX();
204 float deltaY = event.getRawY() - event.getY();
205 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400206 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400207 event.offsetLocation(-deltaX, -deltaY);
208 }
209
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400210 @Override
211 public boolean onTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200212 if (mInstantExpanding) {
213 return false;
214 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100215
216 /*
217 * We capture touch events here and update the expand height here in case according to
218 * the users fingers. This also handles multi-touch.
219 *
220 * If the user just clicks shortly, we give him a quick peek of the shade.
221 *
222 * Flinging is also enabled in order to open or close the shade.
223 */
224
225 int pointerIndex = event.findPointerIndex(mTrackingPointer);
226 if (pointerIndex < 0) {
227 pointerIndex = 0;
228 mTrackingPointer = event.getPointerId(pointerIndex);
229 }
230 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200231 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100232
Jorim Jaggib472b3472014-06-30 19:56:24 +0200233 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
234 mGestureWaitForTouchSlop = mExpandedHeight == 0f;
235 }
236 boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200237
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100238 switch (event.getActionMasked()) {
239 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100240 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200241 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200242 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200243 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200244 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200245 mPanelClosedOnDown = mExpandedHeight == 0.0f;
246 mHasLayoutedSinceDown = false;
247 mUpdateFlingOnLayout = false;
Jorim Jaggib7a33032014-08-20 16:21:36 +0200248 mPeekTouching = mPanelClosedOnDown;
Selim Cinek19c8c702014-08-25 22:09:19 +0200249 mTouchAboveFalsingThreshold = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200250 if (mVelocityTracker == null) {
251 initVelocityTracker();
252 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100253 trackMovement(event);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200254 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) ||
255 mPeekPending || mPeekAnimator != null) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200256 if (mHeightAnimator != null) {
257 mHeightAnimator.cancel(); // end any outstanding animations
258 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200259 cancelPeek();
260 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning)
261 || mPeekPending || mPeekAnimator != null;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200262 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200263 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100264 if (mExpandedHeight == 0) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200265 schedulePeek();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100266 }
267 break;
268
269 case MotionEvent.ACTION_POINTER_UP:
270 final int upPointer = event.getPointerId(event.getActionIndex());
271 if (mTrackingPointer == upPointer) {
272 // gesture is ongoing, find a new pointer to track
273 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
274 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200275 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100276 mTrackingPointer = event.getPointerId(newIndex);
277 mInitialOffsetOnTouch = mExpandedHeight;
278 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200279 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100280 }
281 break;
282
283 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200284 float h = y - mInitialTouchY;
Jorim Jaggib7240132014-06-30 01:39:07 +0200285
286 // If the panel was collapsed when touching, we only need to check for the
287 // y-component of the gesture, as we have no conflicting horizontal gesture.
288 if (Math.abs(h) > mTouchSlop
289 && (Math.abs(h) > Math.abs(x - mInitialTouchX)
290 || mInitialOffsetOnTouch == 0f)) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200291 mTouchSlopExceeded = true;
292 if (waitForTouchSlop && !mTracking) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200293 if (!mJustPeeked) {
294 mInitialOffsetOnTouch = mExpandedHeight;
295 mInitialTouchX = x;
296 mInitialTouchY = y;
297 h = 0;
298 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200299 if (mHeightAnimator != null) {
300 mHeightAnimator.cancel(); // end any outstanding animations
301 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200302 removeCallbacks(mPeekRunnable);
303 mPeekPending = false;
Jorim Jaggi90129582014-06-02 14:44:49 +0200304 onTrackingStarted();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200305 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200306 }
Jorim Jaggi93439da2014-06-30 23:53:39 +0200307 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200308 if (newHeight > mPeekHeight) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200309 if (mPeekAnimator != null) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100310 mPeekAnimator.cancel();
311 }
312 mJustPeeked = false;
313 }
Selim Cinek19c8c702014-08-25 22:09:19 +0200314 if (-h >= mUnlockFalsingThreshold) {
315 mTouchAboveFalsingThreshold = true;
316 }
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200317 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) {
Jorim Jaggicc693242014-06-14 03:04:35 +0000318 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100319 }
320
321 trackMovement(event);
322 break;
323
324 case MotionEvent.ACTION_UP:
325 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100326 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100327 trackMovement(event);
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200328 if ((mTracking && mTouchSlopExceeded)
Jorim Jaggidc96d632014-07-01 18:48:52 +0200329 || Math.abs(x - mInitialTouchX) > mTouchSlop
330 || Math.abs(y - mInitialTouchY) > mTouchSlop
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200331 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
Jorim Jaggib7240132014-06-30 01:39:07 +0200332 float vel = 0f;
333 float vectorVel = 0f;
334 if (mVelocityTracker != null) {
335 mVelocityTracker.computeCurrentVelocity(1000);
336 vel = mVelocityTracker.getYVelocity();
337 vectorVel = (float) Math.hypot(
338 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
339 }
340 boolean expand = flingExpands(vel, vectorVel);
Jorim Jaggi90129582014-06-02 14:44:49 +0200341 onTrackingStopped(expand);
Jorim Jaggib01287f2014-09-12 02:12:26 +0200342 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
343 Log.i(TAG, "Flinging: expand=" + expand);
344 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200345 fling(vel, expand);
Selim Cinek31094df2014-08-14 19:28:15 +0200346 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown;
347 if (mUpdateFlingOnLayout) {
348 mUpdateFlingVelocity = vel;
349 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200350 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200351 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200352 onTrackingStopped(expands);
353 }
Jorim Jaggidc96d632014-07-01 18:48:52 +0200354
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100355 if (mVelocityTracker != null) {
356 mVelocityTracker.recycle();
357 mVelocityTracker = null;
358 }
Jorim Jaggib7a33032014-08-20 16:21:36 +0200359 mPeekTouching = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100360 break;
361 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200362 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100363 }
364
Selim Cinek4c6969a2014-05-26 19:22:17 +0200365 protected abstract boolean hasConflictingGestures();
366
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200367 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200368 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200369 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200370 }
371
372 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200373 mTracking = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200374 mCollapseAfterPeek = false;
Selim Cinek1685e632014-04-08 02:27:49 +0200375 mBar.onTrackingStarted(PanelView.this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200376 notifyExpandingStarted();
Selim Cinek1685e632014-04-08 02:27:49 +0200377 }
378
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100379 @Override
380 public boolean onInterceptTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200381 if (mInstantExpanding) {
382 return false;
383 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100384
385 /*
386 * If the user drags anywhere inside the panel we intercept it if he moves his finger
387 * upwards. This allows closing the shade from anywhere inside the panel.
388 *
389 * We only do this if the current content is scrolled to the bottom,
390 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
391 * possible.
392 */
393 int pointerIndex = event.findPointerIndex(mTrackingPointer);
394 if (pointerIndex < 0) {
395 pointerIndex = 0;
396 mTrackingPointer = event.getPointerId(pointerIndex);
397 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200398 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100399 final float y = event.getY(pointerIndex);
400 boolean scrolledToBottom = isScrolledToBottom();
401
402 switch (event.getActionMasked()) {
403 case MotionEvent.ACTION_DOWN:
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200404 mStatusBar.userActivity();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200405 if (mHeightAnimator != null && !mHintAnimationRunning ||
406 mPeekPending || mPeekAnimator != null) {
407 if (mHeightAnimator != null) {
408 mHeightAnimator.cancel(); // end any outstanding animations
409 }
410 cancelPeek();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200411 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200412 return true;
413 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100414 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200415 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200416 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200417 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200418 mPanelClosedOnDown = mExpandedHeight == 0.0f;
419 mHasLayoutedSinceDown = false;
420 mUpdateFlingOnLayout = false;
Selim Cinek19c8c702014-08-25 22:09:19 +0200421 mTouchAboveFalsingThreshold = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100422 initVelocityTracker();
423 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100424 break;
425 case MotionEvent.ACTION_POINTER_UP:
426 final int upPointer = event.getPointerId(event.getActionIndex());
427 if (mTrackingPointer == upPointer) {
428 // gesture is ongoing, find a new pointer to track
429 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
430 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200431 mInitialTouchX = event.getX(newIndex);
432 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100433 }
434 break;
435
436 case MotionEvent.ACTION_MOVE:
437 final float h = y - mInitialTouchY;
438 trackMovement(event);
439 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200440 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200441 if (mHeightAnimator != null) {
442 mHeightAnimator.cancel();
443 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100444 mInitialOffsetOnTouch = mExpandedHeight;
445 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200446 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200447 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200448 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200449 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100450 return true;
451 }
452 }
453 break;
Selim Cinek31094df2014-08-14 19:28:15 +0200454 case MotionEvent.ACTION_CANCEL:
455 case MotionEvent.ACTION_UP:
Selim Cinek31094df2014-08-14 19:28:15 +0200456 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100457 }
458 return false;
459 }
460
461 private void initVelocityTracker() {
462 if (mVelocityTracker != null) {
463 mVelocityTracker.recycle();
464 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200465 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100466 }
467
468 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200469 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100470 }
471
472 protected float getContentHeight() {
473 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400474 }
475
Daniel Sandler08d05e32012-08-08 16:39:54 -0400476 @Override
477 protected void onFinishInflate() {
478 super.onFinishInflate();
479 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400480 }
481
Jorim Jaggi069cd032014-05-15 03:09:01 +0200482 @Override
483 protected void onConfigurationChanged(Configuration newConfig) {
484 super.onConfigurationChanged(newConfig);
485 loadDimens();
Jorim Jaggi069cd032014-05-15 03:09:01 +0200486 }
487
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200488 /**
Jorim Jaggib7240132014-06-30 01:39:07 +0200489 * @param vel the current vertical velocity of the motion
490 * @param vectorVel the length of the vectorial velocity
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200491 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200492 */
Jorim Jaggidc96d632014-07-01 18:48:52 +0200493 protected boolean flingExpands(float vel, float vectorVel) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200494 if (isBelowFalsingThreshold()) {
Selim Cinek19c8c702014-08-25 22:09:19 +0200495 return true;
496 }
Jorim Jaggib7240132014-06-30 01:39:07 +0200497 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200498 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200499 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200500 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400501 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200502 }
503
Selim Cinek5386fb32014-09-03 16:37:36 +0200504 private boolean isBelowFalsingThreshold() {
505 return !mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded();
506 }
507
Jorim Jaggi1d480692014-05-20 19:41:58 +0200508 protected void fling(float vel, boolean expand) {
509 cancelPeek();
510 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200511
512 // Hack to make the expand transition look nice when clear all button is visible - we make
513 // the animation only to the last notification, and then jump to the maximum panel height so
514 // clear all just fades in and the decelerating motion is towards the last notification.
515 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible()
516 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight()
517 && !isClearAllVisible();
518 if (clearAllExpandHack) {
519 target = getMaxPanelHeight() - getClearAllHeight();
520 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200521 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200522 notifyExpandingFinished();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200523 return;
524 }
Jorim Jaggib01287f2014-09-12 02:12:26 +0200525 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
526 Log.i(TAG, "Executing fling: expand=" + expand + " vel=" + vel);
527 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200528 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200529 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200530 if (expand) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200531 boolean belowFalsingThreshold = isBelowFalsingThreshold();
532 if (belowFalsingThreshold) {
533 vel = 0;
534 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200535 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
Selim Cinek5386fb32014-09-03 16:37:36 +0200536 if (belowFalsingThreshold) {
537 animator.setDuration(350);
538 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200539 } else {
540 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
541 getHeight());
542
543 // Make it shorter if we run a canned animation
544 if (vel == 0) {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200545 animator.setDuration((long)
546 (animator.getDuration() * getCannedFlingDurationFactor()));
Jorim Jaggi1d480692014-05-20 19:41:58 +0200547 }
548 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200549 animator.addListener(new AnimatorListenerAdapter() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200550 private boolean mCancelled;
551
552 @Override
553 public void onAnimationCancel(Animator animation) {
554 mCancelled = true;
555 }
556
Jorim Jaggi1d480692014-05-20 19:41:58 +0200557 @Override
558 public void onAnimationEnd(Animator animation) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200559 if (clearAllExpandHack && !mCancelled) {
Jorim Jaggi2ae259d2014-08-04 23:35:47 +0200560 setExpandedHeightInternal(getMaxPanelHeight());
561 }
562 mHeightAnimator = null;
563 if (!mCancelled) {
564 notifyExpandingFinished();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200565 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200566 }
567 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200568 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200569 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400570 }
571
572 @Override
573 protected void onAttachedToWindow() {
574 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400575 mViewName = getResources().getResourceName(getId());
576 }
577
578 public String getName() {
579 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400580 }
581
Daniel Sandler08d05e32012-08-08 16:39:54 -0400582 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400583 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200584 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler08d05e32012-08-08 16:39:54 -0400585 }
586
Daniel Sandler50508132012-08-16 14:10:53 -0400587 @Override
588 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Daniel Sandler50508132012-08-16 14:10:53 -0400589 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100590 requestPanelHeightUpdate();
Selim Cinek31094df2014-08-14 19:28:15 +0200591 mHasLayoutedSinceDown = true;
592 if (mUpdateFlingOnLayout) {
593 abortAnimations();
594 fling(mUpdateFlingVelocity, true);
595 mUpdateFlingOnLayout = false;
596 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100597 }
598
599 protected void requestPanelHeightUpdate() {
600 float currentMaxPanelHeight = getMaxPanelHeight();
601
602 // If the user isn't actively poking us, let's update the height
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200603 if ((!mTracking || isTrackingBlocked())
604 && mHeightAnimator == null
605 && mExpandedHeight > 0
606 && currentMaxPanelHeight != mExpandedHeight
607 && !mPeekPending
Selim Cinek31094df2014-08-14 19:28:15 +0200608 && mPeekAnimator == null
Jorim Jaggib7a33032014-08-20 16:21:36 +0200609 && !mPeekTouching) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200610 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100611 }
Daniel Sandler50508132012-08-16 14:10:53 -0400612 }
613
Daniel Sandler08d05e32012-08-08 16:39:54 -0400614 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200615 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
616 if (mHeightAnimator == null) {
617 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
618 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
619 setOverExpansion(overExpansionPixels, true /* isPixels */);
620 }
621 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
622 } else {
623 mExpandedHeight = h;
624 if (mOverExpandedBeforeFling) {
625 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
626 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100627 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400628
Jorim Jaggi93439da2014-06-30 23:53:39 +0200629 mExpandedHeight = Math.max(0, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100630 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200631 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
632 ? 0
633 : mExpandedHeight / fhWithoutOverExpansion);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200634 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400635 }
636
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200637 /**
638 * @return true if the panel tracking should be temporarily blocked; this is used when a
639 * conflicting gesture (opening QS) is happening
640 */
641 protected abstract boolean isTrackingBlocked();
642
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200643 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200644
Jorim Jaggi90129582014-06-02 14:44:49 +0200645 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100646
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200647 protected abstract float getOverExpansionAmount();
648
649 protected abstract float getOverExpansionPixels();
650
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100651 /**
652 * This returns the maximum height of the panel. Children should override this if their
653 * desired height is not the full height.
654 *
655 * @return the default implementation simply returns the maximum height.
656 */
Selim Cinek31094df2014-08-14 19:28:15 +0200657 protected abstract int getMaxPanelHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400658
659 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100660 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400661 }
662
663 public float getExpandedHeight() {
664 return mExpandedHeight;
665 }
666
667 public float getExpandedFraction() {
668 return mExpandedFraction;
669 }
670
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700671 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100672 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700673 }
674
675 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400676 return mExpandedHeight <= 0;
677 }
678
679 public boolean isCollapsing() {
680 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700681 }
682
John Spurlocka4b70af2013-08-17 14:05:49 -0400683 public boolean isTracking() {
684 return mTracking;
685 }
686
Daniel Sandler08d05e32012-08-08 16:39:54 -0400687 public void setBar(PanelBar panelBar) {
688 mBar = panelBar;
689 }
690
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200691 public void collapse(boolean delayed) {
John Spurlock97642182013-07-29 17:58:39 -0400692 if (DEBUG) logf("collapse: " + this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200693 if (mPeekPending || mPeekAnimator != null) {
694 mCollapseAfterPeek = true;
695 if (mPeekPending) {
696
697 // We know that the whole gesture is just a peek triggered by a simple click, so
698 // better start it now.
699 removeCallbacks(mPeekRunnable);
700 mPeekRunnable.run();
701 }
Jorim Jaggi98139552014-09-10 17:04:27 +0200702 } else if (!isFullyCollapsed() && !mTracking && !mClosing) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200703 if (mHeightAnimator != null) {
704 mHeightAnimator.cancel();
705 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400706 mClosing = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200707 notifyExpandingStarted();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200708 if (delayed) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200709 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
710 Log.i(TAG, "Posting collapse runnable, will be run in 120ms");
711 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200712 postDelayed(mFlingCollapseRunnable, 120);
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200713 } else {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200714 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
715 Log.i(TAG, "Animating collapsing now");
716 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200717 fling(0, false /* expand */);
718 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400719 }
720 }
721
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200722 private final Runnable mFlingCollapseRunnable = new Runnable() {
723 @Override
724 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200725 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
726 Log.i(TAG, "Executing collapse runnable, animating collapsing now");
727 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200728 fling(0, false /* expand */);
729 }
730 };
731
Daniel Sandler08d05e32012-08-08 16:39:54 -0400732 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400733 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400734 if (isFullyCollapsed()) {
735 mBar.startOpeningPanel(this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200736 notifyExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200737 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400738 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400739 if (DEBUG) logf("skipping expansion: is expanded");
740 }
741 }
742
743 public void cancelPeek() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200744 if (mPeekAnimator != null) {
John Spurlock97642182013-07-29 17:58:39 -0400745 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400746 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200747 removeCallbacks(mPeekRunnable);
748 mPeekPending = false;
749
750 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
751 // notify mBar that we might have closed ourselves.
752 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400753 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500754
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200755 public void instantExpand() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200756 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
757 Log.i(TAG, "Before instant expanding"
758 + " mTracking=" + mTracking
759 + " mExpanding=" + mExpanding);
760 }
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200761 mInstantExpanding = true;
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200762 mUpdateFlingOnLayout = false;
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200763 abortAnimations();
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200764 cancelPeek();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200765 if (mTracking) {
766 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
Jorim Jaggie62d5892014-09-02 16:31:19 +0200767 }
768 if (mExpanding) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200769 notifyExpandingFinished();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200770 }
771 setVisibility(VISIBLE);
772
773 // Wait for window manager to pickup the change, so we know the maximum height of the panel
774 // then.
775 getViewTreeObserver().addOnGlobalLayoutListener(
776 new ViewTreeObserver.OnGlobalLayoutListener() {
777 @Override
778 public void onGlobalLayout() {
779 if (mStatusBar.getStatusBarWindow().getHeight()
780 != mStatusBar.getStatusBarHeight()) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200781 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
782 Log.i(TAG, "Now instant expanding after layout"
783 + " mTracking=" + mTracking
784 + " mExpanding=" + mExpanding);
785 }
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200786 getViewTreeObserver().removeOnGlobalLayoutListener(this);
787 setExpandedFraction(1f);
788 mInstantExpanding = false;
789 }
790 }
791 });
792
793 // Make sure a layout really happens.
794 requestLayout();
795 }
796
797 private void abortAnimations() {
798 cancelPeek();
799 if (mHeightAnimator != null) {
800 mHeightAnimator.cancel();
801 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200802 removeCallbacks(mPostCollapseRunnable);
803 removeCallbacks(mFlingCollapseRunnable);
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200804 }
805
Jorim Jaggi90129582014-06-02 14:44:49 +0200806 protected void startUnlockHintAnimation() {
807
808 // We don't need to hint the user if an animation is already running or the user is changing
809 // the expansion.
810 if (mHeightAnimator != null || mTracking) {
811 return;
812 }
813 cancelPeek();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200814 notifyExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200815 startUnlockHintAnimationPhase1(new Runnable() {
816 @Override
817 public void run() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200818 notifyExpandingFinished();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200819 mStatusBar.onHintFinished();
820 mHintAnimationRunning = false;
821 }
822 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200823 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200824 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200825 }
826
827 /**
828 * Phase 1: Move everything upwards.
829 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200830 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200831 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
832 ValueAnimator animator = createHeightAnimator(target);
833 animator.setDuration(250);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200834 animator.setInterpolator(mFastOutSlowInInterpolator);
Jorim Jaggi90129582014-06-02 14:44:49 +0200835 animator.addListener(new AnimatorListenerAdapter() {
836 private boolean mCancelled;
837
838 @Override
839 public void onAnimationCancel(Animator animation) {
840 mCancelled = true;
841 }
842
843 @Override
844 public void onAnimationEnd(Animator animation) {
845 if (mCancelled) {
846 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200847 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200848 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200849 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200850 }
851 }
852 });
853 animator.start();
854 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200855 mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY();
856 mKeyguardBottomArea.getIndicationView().animate()
857 .y(mOriginalIndicationY - mHintDistance)
858 .setDuration(250)
Jorim Jaggib472b3472014-06-30 19:56:24 +0200859 .setInterpolator(mFastOutSlowInInterpolator)
Selim Cinekf99d0002014-06-13 07:36:01 +0200860 .withEndAction(new Runnable() {
861 @Override
862 public void run() {
863 mKeyguardBottomArea.getIndicationView().animate()
864 .y(mOriginalIndicationY)
865 .setDuration(450)
866 .setInterpolator(mBounceInterpolator)
867 .start();
868 }
869 })
870 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200871 }
872
873 /**
874 * Phase 2: Bounce down.
875 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200876 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200877 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
878 animator.setDuration(450);
879 animator.setInterpolator(mBounceInterpolator);
880 animator.addListener(new AnimatorListenerAdapter() {
881 @Override
882 public void onAnimationEnd(Animator animation) {
883 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200884 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200885 }
886 });
887 animator.start();
888 mHeightAnimator = animator;
889 }
890
891 private ValueAnimator createHeightAnimator(float targetHeight) {
892 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
893 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
894 @Override
895 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200896 setExpandedHeightInternal((Float) animation.getAnimatedValue());
Jorim Jaggi90129582014-06-02 14:44:49 +0200897 }
898 });
899 return animator;
900 }
901
Jorim Jaggib472b3472014-06-30 19:56:24 +0200902 private void notifyBarPanelExpansionChanged() {
903 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
904 || mPeekAnimator != null);
905 }
906
Jorim Jaggi90129582014-06-02 14:44:49 +0200907 /**
908 * Gets called when the user performs a click anywhere in the empty area of the panel.
909 *
910 * @return whether the panel will be expanded after the action performed by this method
911 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200912 private boolean onEmptySpaceClick(float x) {
913 if (mHintAnimationRunning) {
914 return true;
915 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200916 if (x < mEdgeTapAreaWidth
917 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200918 onEdgeClicked(false /* right */);
919 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200920 } else if (x > getWidth() - mEdgeTapAreaWidth
921 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200922 onEdgeClicked(true /* right */);
923 return true;
924 } else {
925 return onMiddleClicked();
926 }
927 }
928
Jorim Jaggi488b7922014-08-05 21:12:02 +0200929 private final Runnable mPostCollapseRunnable = new Runnable() {
930 @Override
931 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200932 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
933 Log.i(TAG, "Collapsing after middle clicked");
934 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200935 collapse(false /* delayed */);
Jorim Jaggi488b7922014-08-05 21:12:02 +0200936 }
937 };
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200938 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200939 switch (mStatusBar.getBarState()) {
940 case StatusBarState.KEYGUARD:
941 startUnlockHintAnimation();
942 return true;
943 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200944 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200945 return true;
946 case StatusBarState.SHADE:
Jorim Jaggib01287f2014-09-12 02:12:26 +0200947 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
948 Log.i(TAG, "Middle clicked in shade state, posting collapsing runnable");
949 }
Jorim Jaggi488b7922014-08-05 21:12:02 +0200950
951 // This gets called in the middle of the touch handling, where the state is still
952 // that we are tracking the panel. Collapse the panel after this is done.
953 post(mPostCollapseRunnable);
Jorim Jaggi90129582014-06-02 14:44:49 +0200954 return false;
955 default:
956 return true;
957 }
958 }
959
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200960 protected abstract void onEdgeClicked(boolean right);
961
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500962 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200963 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400964 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500965 + "]",
966 this.getClass().getSimpleName(),
967 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100968 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500969 mClosing?"T":"f",
970 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500971 mJustPeeked?"T":"f",
972 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200973 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500974 ));
975 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200976
977 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200978
979 protected abstract float getPeekHeight();
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200980
981 protected abstract float getCannedFlingDurationFactor();
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200982
983 /**
984 * @return whether "Clear all" button will be visible when the panel is fully expanded
985 */
986 protected abstract boolean fullyExpandedClearAllVisible();
987
988 protected abstract boolean isClearAllVisible();
989
990 /**
991 * @return the height of the clear all button, in pixels
992 */
993 protected abstract int getClearAllHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400994}