blob: 67c77236ba7879ea0d2668291adb9d61ffa3e58c [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;
John Spurlock813552c2014-09-19 08:30:21 -040036import com.android.systemui.doze.DozeLog;
Jorim Jaggi1d480692014-05-20 19:41:58 +020037import com.android.systemui.statusbar.FlingAnimationUtils;
Jorim Jaggi90129582014-06-02 14:44:49 +020038import com.android.systemui.statusbar.StatusBarState;
Daniel Sandler08d05e32012-08-08 16:39:54 -040039
John Spurlockde84f0e2013-06-12 12:41:00 -040040import java.io.FileDescriptor;
41import java.io.PrintWriter;
John Spurlockde84f0e2013-06-12 12:41:00 -040042
Selim Cinek4c6969a2014-05-26 19:22:17 +020043public abstract class PanelView extends FrameLayout {
Daniel Sandler198a0302012-08-17 16:04:31 -040044 public static final boolean DEBUG = PanelBar.DEBUG;
Daniel Sandler08d05e32012-08-08 16:39:54 -040045 public static final String TAG = PanelView.class.getSimpleName();
Daniel Sandlere7c5bbb2013-03-05 13:36:21 -050046
John Spurlock97642182013-07-29 17:58:39 -040047 private final void logf(String fmt, Object... args) {
John Spurlockcd686b52013-06-05 10:13:46 -040048 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args));
Daniel Sandler08d05e32012-08-08 16:39:54 -040049 }
50
Jorim Jaggi90129582014-06-02 14:44:49 +020051 protected PhoneStatusBar mStatusBar;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040052 private float mPeekHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +020053 private float mHintDistance;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020054 private int mEdgeTapAreaWidth;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010055 private float mInitialOffsetOnTouch;
Daniel Sandler08d05e32012-08-08 16:39:54 -040056 private float mExpandedFraction = 0;
Selim Cinek1408eb52014-06-02 14:45:38 +020057 protected float mExpandedHeight = 0;
Selim Cinek31094df2014-08-14 19:28:15 +020058 private boolean mPanelClosedOnDown;
59 private boolean mHasLayoutedSinceDown;
60 private float mUpdateFlingVelocity;
61 private boolean mUpdateFlingOnLayout;
Jorim Jaggib7a33032014-08-20 16:21:36 +020062 private boolean mPeekTouching;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040063 private boolean mJustPeeked;
Daniel Sandler50508132012-08-16 14:10:53 -040064 private boolean mClosing;
Jorim Jaggi8dd95e02014-06-03 16:19:33 +020065 protected boolean mTracking;
Jorim Jaggi90129582014-06-02 14:44:49 +020066 private boolean mTouchSlopExceeded;
John Spurlock48fa91a2013-08-15 09:29:31 -040067 private int mTrackingPointer;
Jorim Jaggid7daab72014-05-06 22:22:20 +020068 protected int mTouchSlop;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020069 protected boolean mHintAnimationRunning;
Jorim Jaggi47c85a32014-06-05 17:25:40 +020070 private boolean mOverExpandedBeforeFling;
Selim Cinekf99d0002014-06-13 07:36:01 +020071 private float mOriginalIndicationY;
Selim Cinek19c8c702014-08-25 22:09:19 +020072 private boolean mTouchAboveFalsingThreshold;
73 private int mUnlockFalsingThreshold;
Daniel Sandler08d05e32012-08-08 16:39:54 -040074
Jorim Jaggi1d480692014-05-20 19:41:58 +020075 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040076 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020077 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020078 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040079
Jorim Jaggi0a27be82014-06-11 03:22:39 +020080 /**
81 * Whether an instant expand request is currently pending and we are just waiting for layout.
82 */
83 private boolean mInstantExpanding;
84
Daniel Sandler08d05e32012-08-08 16:39:54 -040085 PanelBar mBar;
86
Daniel Sandler50508132012-08-16 14:10:53 -040087 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020088 private float mInitialTouchY;
89 private float mInitialTouchX;
Jorim Jaggid41083a2014-09-12 02:54:40 +020090 private boolean mTouchDisabled;
Daniel Sandler08d05e32012-08-08 16:39:54 -040091
Jorim Jaggi90129582014-06-02 14:44:49 +020092 private Interpolator mLinearOutSlowInInterpolator;
Jorim Jaggib472b3472014-06-30 19:56:24 +020093 private Interpolator mFastOutSlowInInterpolator;
Jorim Jaggi90129582014-06-02 14:44:49 +020094 private Interpolator mBounceInterpolator;
Selim Cinekf99d0002014-06-13 07:36:01 +020095 protected KeyguardBottomAreaView mKeyguardBottomArea;
Jorim Jaggi90129582014-06-02 14:44:49 +020096
Jorim Jaggib472b3472014-06-30 19:56:24 +020097 private boolean mPeekPending;
98 private boolean mCollapseAfterPeek;
99 private boolean mExpanding;
100 private boolean mGestureWaitForTouchSlop;
101 private Runnable mPeekRunnable = new Runnable() {
102 @Override
103 public void run() {
104 mPeekPending = false;
105 runPeekAnimation();
106 }
107 };
108
Selim Cinek1685e632014-04-08 02:27:49 +0200109 protected void onExpandingFinished() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200110 mClosing = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200111 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +0200112 }
113
114 protected void onExpandingStarted() {
115 }
116
Jorim Jaggib472b3472014-06-30 19:56:24 +0200117 private void notifyExpandingStarted() {
118 if (!mExpanding) {
119 mExpanding = true;
120 onExpandingStarted();
121 }
122 }
123
124 private void notifyExpandingFinished() {
125 if (mExpanding) {
126 mExpanding = false;
127 onExpandingFinished();
128 }
129 }
130
131 private void schedulePeek() {
132 mPeekPending = true;
133 long timeout = ViewConfiguration.getTapTimeout();
134 postOnAnimationDelayed(mPeekRunnable, timeout);
135 notifyBarPanelExpansionChanged();
136 }
137
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400138 private void runPeekAnimation() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200139 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
140 Log.i(TAG, "Starting peek animation");
141 }
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200142 mPeekHeight = getPeekHeight();
John Spurlock97642182013-07-29 17:58:39 -0400143 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200144 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400145 return;
146 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200147 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight)
148 .setDuration(250);
149 mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator);
150 mPeekAnimator.addListener(new AnimatorListenerAdapter() {
151 private boolean mCancelled;
152
153 @Override
154 public void onAnimationCancel(Animator animation) {
155 mCancelled = true;
156 }
157
158 @Override
159 public void onAnimationEnd(Animator animation) {
160 mPeekAnimator = null;
161 if (mCollapseAfterPeek && !mCancelled) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200162 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
163 Log.i(TAG, "Peek animation finished, posting collapse");
164 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200165 postOnAnimation(new Runnable() {
166 @Override
167 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200168 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
169 Log.i(TAG, "Peek animation finished, collapsing");
170 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200171 collapse(false /* delayed */);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200172 }
173 });
174 }
175 mCollapseAfterPeek = false;
176 }
177 });
178 notifyExpandingStarted();
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400179 mPeekAnimator.start();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200180 mJustPeeked = true;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400181 }
182
Daniel Sandler08d05e32012-08-08 16:39:54 -0400183 public PanelView(Context context, AttributeSet attrs) {
184 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200185 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200186 mFastOutSlowInInterpolator =
Jorim Jaggi90129582014-06-02 14:44:49 +0200187 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200188 mLinearOutSlowInInterpolator =
189 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
Jorim Jaggi90129582014-06-02 14:44:49 +0200190 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400191 }
192
Jorim Jaggi069cd032014-05-15 03:09:01 +0200193 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400194 final Resources res = getContext().getResources();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100195 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
196 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200197 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200198 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Selim Cinek19c8c702014-08-25 22:09:19 +0200199 mUnlockFalsingThreshold = res.getDimensionPixelSize(R.dimen.unlock_falsing_threshold);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400200 }
201
202 private void trackMovement(MotionEvent event) {
203 // Add movement to velocity tracker using raw screen X and Y coordinates instead
204 // of window coordinates because the window frame may be moving at the same time.
205 float deltaX = event.getRawX() - event.getX();
206 float deltaY = event.getRawY() - event.getY();
207 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400208 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400209 event.offsetLocation(-deltaX, -deltaY);
210 }
211
Jorim Jaggid41083a2014-09-12 02:54:40 +0200212 public void setTouchDisabled(boolean disabled) {
213 mTouchDisabled = disabled;
214 }
215
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400216 @Override
217 public boolean onTouchEvent(MotionEvent event) {
Jorim Jaggid41083a2014-09-12 02:54:40 +0200218 if (mInstantExpanding || mTouchDisabled) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200219 return false;
220 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100221
222 /*
223 * We capture touch events here and update the expand height here in case according to
224 * the users fingers. This also handles multi-touch.
225 *
226 * If the user just clicks shortly, we give him a quick peek of the shade.
227 *
228 * Flinging is also enabled in order to open or close the shade.
229 */
230
231 int pointerIndex = event.findPointerIndex(mTrackingPointer);
232 if (pointerIndex < 0) {
233 pointerIndex = 0;
234 mTrackingPointer = event.getPointerId(pointerIndex);
235 }
236 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200237 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100238
Jorim Jaggib472b3472014-06-30 19:56:24 +0200239 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
240 mGestureWaitForTouchSlop = mExpandedHeight == 0f;
241 }
242 boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200243
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100244 switch (event.getActionMasked()) {
245 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100246 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200247 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200248 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200249 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200250 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200251 mPanelClosedOnDown = mExpandedHeight == 0.0f;
252 mHasLayoutedSinceDown = false;
253 mUpdateFlingOnLayout = false;
Jorim Jaggib7a33032014-08-20 16:21:36 +0200254 mPeekTouching = mPanelClosedOnDown;
Selim Cinek19c8c702014-08-25 22:09:19 +0200255 mTouchAboveFalsingThreshold = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200256 if (mVelocityTracker == null) {
257 initVelocityTracker();
258 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100259 trackMovement(event);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200260 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) ||
261 mPeekPending || mPeekAnimator != null) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200262 if (mHeightAnimator != null) {
263 mHeightAnimator.cancel(); // end any outstanding animations
264 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200265 cancelPeek();
266 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning)
267 || mPeekPending || mPeekAnimator != null;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200268 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200269 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100270 if (mExpandedHeight == 0) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200271 schedulePeek();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100272 }
273 break;
274
275 case MotionEvent.ACTION_POINTER_UP:
276 final int upPointer = event.getPointerId(event.getActionIndex());
277 if (mTrackingPointer == upPointer) {
278 // gesture is ongoing, find a new pointer to track
279 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
280 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200281 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100282 mTrackingPointer = event.getPointerId(newIndex);
283 mInitialOffsetOnTouch = mExpandedHeight;
284 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200285 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100286 }
287 break;
288
289 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200290 float h = y - mInitialTouchY;
Jorim Jaggib7240132014-06-30 01:39:07 +0200291
292 // If the panel was collapsed when touching, we only need to check for the
293 // y-component of the gesture, as we have no conflicting horizontal gesture.
294 if (Math.abs(h) > mTouchSlop
295 && (Math.abs(h) > Math.abs(x - mInitialTouchX)
296 || mInitialOffsetOnTouch == 0f)) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200297 mTouchSlopExceeded = true;
298 if (waitForTouchSlop && !mTracking) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200299 if (!mJustPeeked) {
300 mInitialOffsetOnTouch = mExpandedHeight;
301 mInitialTouchX = x;
302 mInitialTouchY = y;
303 h = 0;
304 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200305 if (mHeightAnimator != null) {
306 mHeightAnimator.cancel(); // end any outstanding animations
307 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200308 removeCallbacks(mPeekRunnable);
309 mPeekPending = false;
Jorim Jaggi90129582014-06-02 14:44:49 +0200310 onTrackingStarted();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200311 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200312 }
Jorim Jaggi93439da2014-06-30 23:53:39 +0200313 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200314 if (newHeight > mPeekHeight) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200315 if (mPeekAnimator != null) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100316 mPeekAnimator.cancel();
317 }
318 mJustPeeked = false;
319 }
Selim Cinek19c8c702014-08-25 22:09:19 +0200320 if (-h >= mUnlockFalsingThreshold) {
321 mTouchAboveFalsingThreshold = true;
322 }
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200323 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) {
Jorim Jaggicc693242014-06-14 03:04:35 +0000324 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100325 }
326
327 trackMovement(event);
328 break;
329
330 case MotionEvent.ACTION_UP:
331 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100332 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100333 trackMovement(event);
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200334 if ((mTracking && mTouchSlopExceeded)
Jorim Jaggidc96d632014-07-01 18:48:52 +0200335 || Math.abs(x - mInitialTouchX) > mTouchSlop
336 || Math.abs(y - mInitialTouchY) > mTouchSlop
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200337 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
Jorim Jaggib7240132014-06-30 01:39:07 +0200338 float vel = 0f;
339 float vectorVel = 0f;
340 if (mVelocityTracker != null) {
341 mVelocityTracker.computeCurrentVelocity(1000);
342 vel = mVelocityTracker.getYVelocity();
343 vectorVel = (float) Math.hypot(
344 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
345 }
346 boolean expand = flingExpands(vel, vectorVel);
Jorim Jaggi90129582014-06-02 14:44:49 +0200347 onTrackingStopped(expand);
Jorim Jaggib01287f2014-09-12 02:12:26 +0200348 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
349 Log.i(TAG, "Flinging: expand=" + expand);
350 }
John Spurlock813552c2014-09-19 08:30:21 -0400351 DozeLog.traceFling(expand, mTouchAboveFalsingThreshold,
352 mStatusBar.isFalsingThresholdNeeded());
Jorim Jaggi90129582014-06-02 14:44:49 +0200353 fling(vel, expand);
Selim Cinek31094df2014-08-14 19:28:15 +0200354 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown;
355 if (mUpdateFlingOnLayout) {
356 mUpdateFlingVelocity = vel;
357 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200358 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200359 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200360 onTrackingStopped(expands);
361 }
Jorim Jaggidc96d632014-07-01 18:48:52 +0200362
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100363 if (mVelocityTracker != null) {
364 mVelocityTracker.recycle();
365 mVelocityTracker = null;
366 }
Jorim Jaggib7a33032014-08-20 16:21:36 +0200367 mPeekTouching = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100368 break;
369 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200370 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100371 }
372
Selim Cinek4c6969a2014-05-26 19:22:17 +0200373 protected abstract boolean hasConflictingGestures();
374
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200375 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200376 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200377 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200378 }
379
380 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200381 mTracking = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200382 mCollapseAfterPeek = false;
Selim Cinek1685e632014-04-08 02:27:49 +0200383 mBar.onTrackingStarted(PanelView.this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200384 notifyExpandingStarted();
Selim Cinek1685e632014-04-08 02:27:49 +0200385 }
386
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100387 @Override
388 public boolean onInterceptTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200389 if (mInstantExpanding) {
390 return false;
391 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100392
393 /*
394 * If the user drags anywhere inside the panel we intercept it if he moves his finger
395 * upwards. This allows closing the shade from anywhere inside the panel.
396 *
397 * We only do this if the current content is scrolled to the bottom,
398 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
399 * possible.
400 */
401 int pointerIndex = event.findPointerIndex(mTrackingPointer);
402 if (pointerIndex < 0) {
403 pointerIndex = 0;
404 mTrackingPointer = event.getPointerId(pointerIndex);
405 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200406 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100407 final float y = event.getY(pointerIndex);
408 boolean scrolledToBottom = isScrolledToBottom();
409
410 switch (event.getActionMasked()) {
411 case MotionEvent.ACTION_DOWN:
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200412 mStatusBar.userActivity();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200413 if (mHeightAnimator != null && !mHintAnimationRunning ||
414 mPeekPending || mPeekAnimator != null) {
415 if (mHeightAnimator != null) {
416 mHeightAnimator.cancel(); // end any outstanding animations
417 }
418 cancelPeek();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200419 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200420 return true;
421 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100422 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200423 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200424 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200425 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200426 mPanelClosedOnDown = mExpandedHeight == 0.0f;
427 mHasLayoutedSinceDown = false;
428 mUpdateFlingOnLayout = false;
Selim Cinek19c8c702014-08-25 22:09:19 +0200429 mTouchAboveFalsingThreshold = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100430 initVelocityTracker();
431 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100432 break;
433 case MotionEvent.ACTION_POINTER_UP:
434 final int upPointer = event.getPointerId(event.getActionIndex());
435 if (mTrackingPointer == upPointer) {
436 // gesture is ongoing, find a new pointer to track
437 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
438 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200439 mInitialTouchX = event.getX(newIndex);
440 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100441 }
442 break;
443
444 case MotionEvent.ACTION_MOVE:
445 final float h = y - mInitialTouchY;
446 trackMovement(event);
447 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200448 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200449 if (mHeightAnimator != null) {
450 mHeightAnimator.cancel();
451 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100452 mInitialOffsetOnTouch = mExpandedHeight;
453 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200454 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200455 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200456 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200457 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100458 return true;
459 }
460 }
461 break;
Selim Cinek31094df2014-08-14 19:28:15 +0200462 case MotionEvent.ACTION_CANCEL:
463 case MotionEvent.ACTION_UP:
Selim Cinek31094df2014-08-14 19:28:15 +0200464 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100465 }
466 return false;
467 }
468
469 private void initVelocityTracker() {
470 if (mVelocityTracker != null) {
471 mVelocityTracker.recycle();
472 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200473 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100474 }
475
476 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200477 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100478 }
479
480 protected float getContentHeight() {
481 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400482 }
483
Daniel Sandler08d05e32012-08-08 16:39:54 -0400484 @Override
485 protected void onFinishInflate() {
486 super.onFinishInflate();
487 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400488 }
489
Jorim Jaggi069cd032014-05-15 03:09:01 +0200490 @Override
491 protected void onConfigurationChanged(Configuration newConfig) {
492 super.onConfigurationChanged(newConfig);
493 loadDimens();
Jorim Jaggi069cd032014-05-15 03:09:01 +0200494 }
495
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200496 /**
Jorim Jaggib7240132014-06-30 01:39:07 +0200497 * @param vel the current vertical velocity of the motion
498 * @param vectorVel the length of the vectorial velocity
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200499 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200500 */
Jorim Jaggidc96d632014-07-01 18:48:52 +0200501 protected boolean flingExpands(float vel, float vectorVel) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200502 if (isBelowFalsingThreshold()) {
Selim Cinek19c8c702014-08-25 22:09:19 +0200503 return true;
504 }
Jorim Jaggib7240132014-06-30 01:39:07 +0200505 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200506 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200507 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200508 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400509 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200510 }
511
Selim Cinek5386fb32014-09-03 16:37:36 +0200512 private boolean isBelowFalsingThreshold() {
513 return !mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded();
514 }
515
Jorim Jaggi1d480692014-05-20 19:41:58 +0200516 protected void fling(float vel, boolean expand) {
517 cancelPeek();
518 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200519
520 // Hack to make the expand transition look nice when clear all button is visible - we make
521 // the animation only to the last notification, and then jump to the maximum panel height so
522 // clear all just fades in and the decelerating motion is towards the last notification.
523 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible()
524 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight()
525 && !isClearAllVisible();
526 if (clearAllExpandHack) {
527 target = getMaxPanelHeight() - getClearAllHeight();
528 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200529 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200530 notifyExpandingFinished();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200531 return;
532 }
Jorim Jaggib01287f2014-09-12 02:12:26 +0200533 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
534 Log.i(TAG, "Executing fling: expand=" + expand + " vel=" + vel);
535 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200536 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200537 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200538 if (expand) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200539 boolean belowFalsingThreshold = isBelowFalsingThreshold();
540 if (belowFalsingThreshold) {
541 vel = 0;
542 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200543 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
Selim Cinek5386fb32014-09-03 16:37:36 +0200544 if (belowFalsingThreshold) {
545 animator.setDuration(350);
546 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200547 } else {
548 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
549 getHeight());
550
551 // Make it shorter if we run a canned animation
552 if (vel == 0) {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200553 animator.setDuration((long)
554 (animator.getDuration() * getCannedFlingDurationFactor()));
Jorim Jaggi1d480692014-05-20 19:41:58 +0200555 }
556 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200557 animator.addListener(new AnimatorListenerAdapter() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200558 private boolean mCancelled;
559
560 @Override
561 public void onAnimationCancel(Animator animation) {
562 mCancelled = true;
563 }
564
Jorim Jaggi1d480692014-05-20 19:41:58 +0200565 @Override
566 public void onAnimationEnd(Animator animation) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200567 if (clearAllExpandHack && !mCancelled) {
Jorim Jaggi2ae259d2014-08-04 23:35:47 +0200568 setExpandedHeightInternal(getMaxPanelHeight());
569 }
570 mHeightAnimator = null;
571 if (!mCancelled) {
572 notifyExpandingFinished();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200573 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200574 }
575 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200576 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200577 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400578 }
579
580 @Override
581 protected void onAttachedToWindow() {
582 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400583 mViewName = getResources().getResourceName(getId());
584 }
585
586 public String getName() {
587 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400588 }
589
Daniel Sandler08d05e32012-08-08 16:39:54 -0400590 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400591 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200592 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler08d05e32012-08-08 16:39:54 -0400593 }
594
Daniel Sandler50508132012-08-16 14:10:53 -0400595 @Override
596 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Daniel Sandler50508132012-08-16 14:10:53 -0400597 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100598 requestPanelHeightUpdate();
Selim Cinek31094df2014-08-14 19:28:15 +0200599 mHasLayoutedSinceDown = true;
600 if (mUpdateFlingOnLayout) {
601 abortAnimations();
602 fling(mUpdateFlingVelocity, true);
603 mUpdateFlingOnLayout = false;
604 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100605 }
606
607 protected void requestPanelHeightUpdate() {
608 float currentMaxPanelHeight = getMaxPanelHeight();
609
610 // If the user isn't actively poking us, let's update the height
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200611 if ((!mTracking || isTrackingBlocked())
612 && mHeightAnimator == null
613 && mExpandedHeight > 0
614 && currentMaxPanelHeight != mExpandedHeight
615 && !mPeekPending
Selim Cinek31094df2014-08-14 19:28:15 +0200616 && mPeekAnimator == null
Jorim Jaggib7a33032014-08-20 16:21:36 +0200617 && !mPeekTouching) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200618 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100619 }
Daniel Sandler50508132012-08-16 14:10:53 -0400620 }
621
Daniel Sandler08d05e32012-08-08 16:39:54 -0400622 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200623 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
624 if (mHeightAnimator == null) {
625 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
626 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
627 setOverExpansion(overExpansionPixels, true /* isPixels */);
628 }
629 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
630 } else {
631 mExpandedHeight = h;
632 if (mOverExpandedBeforeFling) {
633 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
634 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100635 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400636
Jorim Jaggi93439da2014-06-30 23:53:39 +0200637 mExpandedHeight = Math.max(0, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100638 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200639 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
640 ? 0
641 : mExpandedHeight / fhWithoutOverExpansion);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200642 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400643 }
644
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200645 /**
646 * @return true if the panel tracking should be temporarily blocked; this is used when a
647 * conflicting gesture (opening QS) is happening
648 */
649 protected abstract boolean isTrackingBlocked();
650
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200651 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200652
Jorim Jaggi90129582014-06-02 14:44:49 +0200653 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100654
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200655 protected abstract float getOverExpansionAmount();
656
657 protected abstract float getOverExpansionPixels();
658
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100659 /**
660 * This returns the maximum height of the panel. Children should override this if their
661 * desired height is not the full height.
662 *
663 * @return the default implementation simply returns the maximum height.
664 */
Selim Cinek31094df2014-08-14 19:28:15 +0200665 protected abstract int getMaxPanelHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400666
667 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100668 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400669 }
670
671 public float getExpandedHeight() {
672 return mExpandedHeight;
673 }
674
675 public float getExpandedFraction() {
676 return mExpandedFraction;
677 }
678
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700679 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100680 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700681 }
682
683 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400684 return mExpandedHeight <= 0;
685 }
686
687 public boolean isCollapsing() {
688 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700689 }
690
John Spurlocka4b70af2013-08-17 14:05:49 -0400691 public boolean isTracking() {
692 return mTracking;
693 }
694
Daniel Sandler08d05e32012-08-08 16:39:54 -0400695 public void setBar(PanelBar panelBar) {
696 mBar = panelBar;
697 }
698
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200699 public void collapse(boolean delayed) {
John Spurlock97642182013-07-29 17:58:39 -0400700 if (DEBUG) logf("collapse: " + this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200701 if (mPeekPending || mPeekAnimator != null) {
702 mCollapseAfterPeek = true;
703 if (mPeekPending) {
704
705 // We know that the whole gesture is just a peek triggered by a simple click, so
706 // better start it now.
707 removeCallbacks(mPeekRunnable);
708 mPeekRunnable.run();
709 }
Jorim Jaggi98139552014-09-10 17:04:27 +0200710 } else if (!isFullyCollapsed() && !mTracking && !mClosing) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200711 if (mHeightAnimator != null) {
712 mHeightAnimator.cancel();
713 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400714 mClosing = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200715 notifyExpandingStarted();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200716 if (delayed) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200717 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
718 Log.i(TAG, "Posting collapse runnable, will be run in 120ms");
719 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200720 postDelayed(mFlingCollapseRunnable, 120);
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200721 } else {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200722 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
723 Log.i(TAG, "Animating collapsing now");
724 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200725 fling(0, false /* expand */);
726 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400727 }
728 }
729
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200730 private final Runnable mFlingCollapseRunnable = new Runnable() {
731 @Override
732 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200733 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
734 Log.i(TAG, "Executing collapse runnable, animating collapsing now");
735 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200736 fling(0, false /* expand */);
737 }
738 };
739
Daniel Sandler08d05e32012-08-08 16:39:54 -0400740 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400741 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400742 if (isFullyCollapsed()) {
743 mBar.startOpeningPanel(this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200744 notifyExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200745 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400746 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400747 if (DEBUG) logf("skipping expansion: is expanded");
748 }
749 }
750
751 public void cancelPeek() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200752 if (mPeekAnimator != null) {
John Spurlock97642182013-07-29 17:58:39 -0400753 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400754 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200755 removeCallbacks(mPeekRunnable);
756 mPeekPending = false;
757
758 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
759 // notify mBar that we might have closed ourselves.
760 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400761 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500762
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200763 public void instantExpand() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200764 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
765 Log.i(TAG, "Before instant expanding"
766 + " mTracking=" + mTracking
767 + " mExpanding=" + mExpanding);
768 }
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200769 mInstantExpanding = true;
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200770 mUpdateFlingOnLayout = false;
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200771 abortAnimations();
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200772 cancelPeek();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200773 if (mTracking) {
774 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
Jorim Jaggie62d5892014-09-02 16:31:19 +0200775 }
776 if (mExpanding) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200777 notifyExpandingFinished();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200778 }
779 setVisibility(VISIBLE);
780
781 // Wait for window manager to pickup the change, so we know the maximum height of the panel
782 // then.
783 getViewTreeObserver().addOnGlobalLayoutListener(
784 new ViewTreeObserver.OnGlobalLayoutListener() {
785 @Override
786 public void onGlobalLayout() {
787 if (mStatusBar.getStatusBarWindow().getHeight()
788 != mStatusBar.getStatusBarHeight()) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200789 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
790 Log.i(TAG, "Now instant expanding after layout"
791 + " mTracking=" + mTracking
792 + " mExpanding=" + mExpanding);
793 }
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200794 getViewTreeObserver().removeOnGlobalLayoutListener(this);
795 setExpandedFraction(1f);
796 mInstantExpanding = false;
797 }
798 }
799 });
800
801 // Make sure a layout really happens.
802 requestLayout();
803 }
804
805 private void abortAnimations() {
806 cancelPeek();
807 if (mHeightAnimator != null) {
808 mHeightAnimator.cancel();
809 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200810 removeCallbacks(mPostCollapseRunnable);
811 removeCallbacks(mFlingCollapseRunnable);
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200812 }
813
Jorim Jaggi90129582014-06-02 14:44:49 +0200814 protected void startUnlockHintAnimation() {
815
816 // We don't need to hint the user if an animation is already running or the user is changing
817 // the expansion.
818 if (mHeightAnimator != null || mTracking) {
819 return;
820 }
821 cancelPeek();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200822 notifyExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200823 startUnlockHintAnimationPhase1(new Runnable() {
824 @Override
825 public void run() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200826 notifyExpandingFinished();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200827 mStatusBar.onHintFinished();
828 mHintAnimationRunning = false;
829 }
830 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200831 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200832 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200833 }
834
835 /**
836 * Phase 1: Move everything upwards.
837 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200838 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200839 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
840 ValueAnimator animator = createHeightAnimator(target);
841 animator.setDuration(250);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200842 animator.setInterpolator(mFastOutSlowInInterpolator);
Jorim Jaggi90129582014-06-02 14:44:49 +0200843 animator.addListener(new AnimatorListenerAdapter() {
844 private boolean mCancelled;
845
846 @Override
847 public void onAnimationCancel(Animator animation) {
848 mCancelled = true;
849 }
850
851 @Override
852 public void onAnimationEnd(Animator animation) {
853 if (mCancelled) {
854 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200855 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200856 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200857 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200858 }
859 }
860 });
861 animator.start();
862 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200863 mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY();
864 mKeyguardBottomArea.getIndicationView().animate()
865 .y(mOriginalIndicationY - mHintDistance)
866 .setDuration(250)
Jorim Jaggib472b3472014-06-30 19:56:24 +0200867 .setInterpolator(mFastOutSlowInInterpolator)
Selim Cinekf99d0002014-06-13 07:36:01 +0200868 .withEndAction(new Runnable() {
869 @Override
870 public void run() {
871 mKeyguardBottomArea.getIndicationView().animate()
872 .y(mOriginalIndicationY)
873 .setDuration(450)
874 .setInterpolator(mBounceInterpolator)
875 .start();
876 }
877 })
878 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200879 }
880
881 /**
882 * Phase 2: Bounce down.
883 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200884 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200885 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
886 animator.setDuration(450);
887 animator.setInterpolator(mBounceInterpolator);
888 animator.addListener(new AnimatorListenerAdapter() {
889 @Override
890 public void onAnimationEnd(Animator animation) {
891 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200892 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200893 }
894 });
895 animator.start();
896 mHeightAnimator = animator;
897 }
898
899 private ValueAnimator createHeightAnimator(float targetHeight) {
900 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
901 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
902 @Override
903 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200904 setExpandedHeightInternal((Float) animation.getAnimatedValue());
Jorim Jaggi90129582014-06-02 14:44:49 +0200905 }
906 });
907 return animator;
908 }
909
Jorim Jaggib472b3472014-06-30 19:56:24 +0200910 private void notifyBarPanelExpansionChanged() {
911 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
912 || mPeekAnimator != null);
913 }
914
Jorim Jaggi90129582014-06-02 14:44:49 +0200915 /**
916 * Gets called when the user performs a click anywhere in the empty area of the panel.
917 *
918 * @return whether the panel will be expanded after the action performed by this method
919 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200920 private boolean onEmptySpaceClick(float x) {
921 if (mHintAnimationRunning) {
922 return true;
923 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200924 if (x < mEdgeTapAreaWidth
925 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200926 onEdgeClicked(false /* right */);
927 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200928 } else if (x > getWidth() - mEdgeTapAreaWidth
929 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200930 onEdgeClicked(true /* right */);
931 return true;
932 } else {
933 return onMiddleClicked();
934 }
935 }
936
Jorim Jaggi488b7922014-08-05 21:12:02 +0200937 private final Runnable mPostCollapseRunnable = new Runnable() {
938 @Override
939 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200940 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
941 Log.i(TAG, "Collapsing after middle clicked");
942 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200943 collapse(false /* delayed */);
Jorim Jaggi488b7922014-08-05 21:12:02 +0200944 }
945 };
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200946 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200947 switch (mStatusBar.getBarState()) {
948 case StatusBarState.KEYGUARD:
949 startUnlockHintAnimation();
950 return true;
951 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200952 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200953 return true;
954 case StatusBarState.SHADE:
Jorim Jaggib01287f2014-09-12 02:12:26 +0200955 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
956 Log.i(TAG, "Middle clicked in shade state, posting collapsing runnable");
957 }
Jorim Jaggi488b7922014-08-05 21:12:02 +0200958
959 // This gets called in the middle of the touch handling, where the state is still
960 // that we are tracking the panel. Collapse the panel after this is done.
961 post(mPostCollapseRunnable);
Jorim Jaggi90129582014-06-02 14:44:49 +0200962 return false;
963 default:
964 return true;
965 }
966 }
967
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200968 protected abstract void onEdgeClicked(boolean right);
969
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500970 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200971 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
Jorim Jaggi50d87a72014-09-12 21:43:35 +0200972 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s touchDisabled=%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500973 + "]",
974 this.getClass().getSimpleName(),
975 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100976 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500977 mClosing?"T":"f",
978 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500979 mJustPeeked?"T":"f",
980 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi50d87a72014-09-12 21:43:35 +0200981 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":""),
982 mTouchDisabled?"T":"f"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500983 ));
984 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200985
986 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200987
988 protected abstract float getPeekHeight();
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200989
990 protected abstract float getCannedFlingDurationFactor();
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200991
992 /**
993 * @return whether "Clear all" button will be visible when the panel is fully expanded
994 */
995 protected abstract boolean fullyExpandedClearAllVisible();
996
997 protected abstract boolean isClearAllVisible();
998
999 /**
1000 * @return the height of the clear all button, in pixels
1001 */
1002 protected abstract int getClearAllHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -04001003}