blob: f6d6af1594f52ff79c89acc8a06e141f4ec1ba6f [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;
Jorim Jaggid41083a2014-09-12 02:54:40 +020089 private boolean mTouchDisabled;
Daniel Sandler08d05e32012-08-08 16:39:54 -040090
Jorim Jaggi90129582014-06-02 14:44:49 +020091 private Interpolator mLinearOutSlowInInterpolator;
Jorim Jaggib472b3472014-06-30 19:56:24 +020092 private Interpolator mFastOutSlowInInterpolator;
Jorim Jaggi90129582014-06-02 14:44:49 +020093 private Interpolator mBounceInterpolator;
Selim Cinekf99d0002014-06-13 07:36:01 +020094 protected KeyguardBottomAreaView mKeyguardBottomArea;
Jorim Jaggi90129582014-06-02 14:44:49 +020095
Jorim Jaggib472b3472014-06-30 19:56:24 +020096 private boolean mPeekPending;
97 private boolean mCollapseAfterPeek;
98 private boolean mExpanding;
99 private boolean mGestureWaitForTouchSlop;
100 private Runnable mPeekRunnable = new Runnable() {
101 @Override
102 public void run() {
103 mPeekPending = false;
104 runPeekAnimation();
105 }
106 };
107
Selim Cinek1685e632014-04-08 02:27:49 +0200108 protected void onExpandingFinished() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200109 mClosing = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200110 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +0200111 }
112
113 protected void onExpandingStarted() {
114 }
115
Jorim Jaggib472b3472014-06-30 19:56:24 +0200116 private void notifyExpandingStarted() {
117 if (!mExpanding) {
118 mExpanding = true;
119 onExpandingStarted();
120 }
121 }
122
123 private void notifyExpandingFinished() {
124 if (mExpanding) {
125 mExpanding = false;
126 onExpandingFinished();
127 }
128 }
129
130 private void schedulePeek() {
131 mPeekPending = true;
132 long timeout = ViewConfiguration.getTapTimeout();
133 postOnAnimationDelayed(mPeekRunnable, timeout);
134 notifyBarPanelExpansionChanged();
135 }
136
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400137 private void runPeekAnimation() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200138 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
139 Log.i(TAG, "Starting peek animation");
140 }
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200141 mPeekHeight = getPeekHeight();
John Spurlock97642182013-07-29 17:58:39 -0400142 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200143 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400144 return;
145 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200146 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight)
147 .setDuration(250);
148 mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator);
149 mPeekAnimator.addListener(new AnimatorListenerAdapter() {
150 private boolean mCancelled;
151
152 @Override
153 public void onAnimationCancel(Animator animation) {
154 mCancelled = true;
155 }
156
157 @Override
158 public void onAnimationEnd(Animator animation) {
159 mPeekAnimator = null;
160 if (mCollapseAfterPeek && !mCancelled) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200161 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
162 Log.i(TAG, "Peek animation finished, posting collapse");
163 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200164 postOnAnimation(new Runnable() {
165 @Override
166 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200167 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
168 Log.i(TAG, "Peek animation finished, collapsing");
169 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200170 collapse(false /* delayed */);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200171 }
172 });
173 }
174 mCollapseAfterPeek = false;
175 }
176 });
177 notifyExpandingStarted();
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400178 mPeekAnimator.start();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200179 mJustPeeked = true;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400180 }
181
Daniel Sandler08d05e32012-08-08 16:39:54 -0400182 public PanelView(Context context, AttributeSet attrs) {
183 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200184 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200185 mFastOutSlowInInterpolator =
Jorim Jaggi90129582014-06-02 14:44:49 +0200186 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200187 mLinearOutSlowInInterpolator =
188 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
Jorim Jaggi90129582014-06-02 14:44:49 +0200189 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400190 }
191
Jorim Jaggi069cd032014-05-15 03:09:01 +0200192 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400193 final Resources res = getContext().getResources();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100194 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
195 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200196 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200197 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Selim Cinek19c8c702014-08-25 22:09:19 +0200198 mUnlockFalsingThreshold = res.getDimensionPixelSize(R.dimen.unlock_falsing_threshold);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400199 }
200
201 private void trackMovement(MotionEvent event) {
202 // Add movement to velocity tracker using raw screen X and Y coordinates instead
203 // of window coordinates because the window frame may be moving at the same time.
204 float deltaX = event.getRawX() - event.getX();
205 float deltaY = event.getRawY() - event.getY();
206 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400207 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400208 event.offsetLocation(-deltaX, -deltaY);
209 }
210
Jorim Jaggid41083a2014-09-12 02:54:40 +0200211 public void setTouchDisabled(boolean disabled) {
212 mTouchDisabled = disabled;
213 }
214
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400215 @Override
216 public boolean onTouchEvent(MotionEvent event) {
Jorim Jaggid41083a2014-09-12 02:54:40 +0200217 if (mInstantExpanding || mTouchDisabled) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200218 return false;
219 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100220
221 /*
222 * We capture touch events here and update the expand height here in case according to
223 * the users fingers. This also handles multi-touch.
224 *
225 * If the user just clicks shortly, we give him a quick peek of the shade.
226 *
227 * Flinging is also enabled in order to open or close the shade.
228 */
229
230 int pointerIndex = event.findPointerIndex(mTrackingPointer);
231 if (pointerIndex < 0) {
232 pointerIndex = 0;
233 mTrackingPointer = event.getPointerId(pointerIndex);
234 }
235 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200236 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100237
Jorim Jaggib472b3472014-06-30 19:56:24 +0200238 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
239 mGestureWaitForTouchSlop = mExpandedHeight == 0f;
240 }
241 boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200242
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100243 switch (event.getActionMasked()) {
244 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100245 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200246 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200247 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200248 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200249 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200250 mPanelClosedOnDown = mExpandedHeight == 0.0f;
251 mHasLayoutedSinceDown = false;
252 mUpdateFlingOnLayout = false;
Jorim Jaggib7a33032014-08-20 16:21:36 +0200253 mPeekTouching = mPanelClosedOnDown;
Selim Cinek19c8c702014-08-25 22:09:19 +0200254 mTouchAboveFalsingThreshold = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200255 if (mVelocityTracker == null) {
256 initVelocityTracker();
257 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100258 trackMovement(event);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200259 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) ||
260 mPeekPending || mPeekAnimator != null) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200261 if (mHeightAnimator != null) {
262 mHeightAnimator.cancel(); // end any outstanding animations
263 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200264 cancelPeek();
265 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning)
266 || mPeekPending || mPeekAnimator != null;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200267 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200268 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100269 if (mExpandedHeight == 0) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200270 schedulePeek();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100271 }
272 break;
273
274 case MotionEvent.ACTION_POINTER_UP:
275 final int upPointer = event.getPointerId(event.getActionIndex());
276 if (mTrackingPointer == upPointer) {
277 // gesture is ongoing, find a new pointer to track
278 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
279 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200280 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100281 mTrackingPointer = event.getPointerId(newIndex);
282 mInitialOffsetOnTouch = mExpandedHeight;
283 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200284 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100285 }
286 break;
287
288 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200289 float h = y - mInitialTouchY;
Jorim Jaggib7240132014-06-30 01:39:07 +0200290
291 // If the panel was collapsed when touching, we only need to check for the
292 // y-component of the gesture, as we have no conflicting horizontal gesture.
293 if (Math.abs(h) > mTouchSlop
294 && (Math.abs(h) > Math.abs(x - mInitialTouchX)
295 || mInitialOffsetOnTouch == 0f)) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200296 mTouchSlopExceeded = true;
297 if (waitForTouchSlop && !mTracking) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200298 if (!mJustPeeked) {
299 mInitialOffsetOnTouch = mExpandedHeight;
300 mInitialTouchX = x;
301 mInitialTouchY = y;
302 h = 0;
303 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200304 if (mHeightAnimator != null) {
305 mHeightAnimator.cancel(); // end any outstanding animations
306 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200307 removeCallbacks(mPeekRunnable);
308 mPeekPending = false;
Jorim Jaggi90129582014-06-02 14:44:49 +0200309 onTrackingStarted();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200310 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200311 }
Jorim Jaggi93439da2014-06-30 23:53:39 +0200312 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200313 if (newHeight > mPeekHeight) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200314 if (mPeekAnimator != null) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100315 mPeekAnimator.cancel();
316 }
317 mJustPeeked = false;
318 }
Selim Cinek19c8c702014-08-25 22:09:19 +0200319 if (-h >= mUnlockFalsingThreshold) {
320 mTouchAboveFalsingThreshold = true;
321 }
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200322 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) {
Jorim Jaggicc693242014-06-14 03:04:35 +0000323 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100324 }
325
326 trackMovement(event);
327 break;
328
329 case MotionEvent.ACTION_UP:
330 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100331 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100332 trackMovement(event);
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200333 if ((mTracking && mTouchSlopExceeded)
Jorim Jaggidc96d632014-07-01 18:48:52 +0200334 || Math.abs(x - mInitialTouchX) > mTouchSlop
335 || Math.abs(y - mInitialTouchY) > mTouchSlop
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200336 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
Jorim Jaggib7240132014-06-30 01:39:07 +0200337 float vel = 0f;
338 float vectorVel = 0f;
339 if (mVelocityTracker != null) {
340 mVelocityTracker.computeCurrentVelocity(1000);
341 vel = mVelocityTracker.getYVelocity();
342 vectorVel = (float) Math.hypot(
343 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
344 }
345 boolean expand = flingExpands(vel, vectorVel);
Jorim Jaggi90129582014-06-02 14:44:49 +0200346 onTrackingStopped(expand);
Jorim Jaggib01287f2014-09-12 02:12:26 +0200347 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
348 Log.i(TAG, "Flinging: expand=" + expand);
349 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200350 fling(vel, expand);
Selim Cinek31094df2014-08-14 19:28:15 +0200351 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown;
352 if (mUpdateFlingOnLayout) {
353 mUpdateFlingVelocity = vel;
354 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200355 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200356 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200357 onTrackingStopped(expands);
358 }
Jorim Jaggidc96d632014-07-01 18:48:52 +0200359
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100360 if (mVelocityTracker != null) {
361 mVelocityTracker.recycle();
362 mVelocityTracker = null;
363 }
Jorim Jaggib7a33032014-08-20 16:21:36 +0200364 mPeekTouching = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100365 break;
366 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200367 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100368 }
369
Selim Cinek4c6969a2014-05-26 19:22:17 +0200370 protected abstract boolean hasConflictingGestures();
371
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200372 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200373 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200374 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200375 }
376
377 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200378 mTracking = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200379 mCollapseAfterPeek = false;
Selim Cinek1685e632014-04-08 02:27:49 +0200380 mBar.onTrackingStarted(PanelView.this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200381 notifyExpandingStarted();
Selim Cinek1685e632014-04-08 02:27:49 +0200382 }
383
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100384 @Override
385 public boolean onInterceptTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200386 if (mInstantExpanding) {
387 return false;
388 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100389
390 /*
391 * If the user drags anywhere inside the panel we intercept it if he moves his finger
392 * upwards. This allows closing the shade from anywhere inside the panel.
393 *
394 * We only do this if the current content is scrolled to the bottom,
395 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
396 * possible.
397 */
398 int pointerIndex = event.findPointerIndex(mTrackingPointer);
399 if (pointerIndex < 0) {
400 pointerIndex = 0;
401 mTrackingPointer = event.getPointerId(pointerIndex);
402 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200403 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100404 final float y = event.getY(pointerIndex);
405 boolean scrolledToBottom = isScrolledToBottom();
406
407 switch (event.getActionMasked()) {
408 case MotionEvent.ACTION_DOWN:
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200409 mStatusBar.userActivity();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200410 if (mHeightAnimator != null && !mHintAnimationRunning ||
411 mPeekPending || mPeekAnimator != null) {
412 if (mHeightAnimator != null) {
413 mHeightAnimator.cancel(); // end any outstanding animations
414 }
415 cancelPeek();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200416 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200417 return true;
418 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100419 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200420 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200421 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200422 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200423 mPanelClosedOnDown = mExpandedHeight == 0.0f;
424 mHasLayoutedSinceDown = false;
425 mUpdateFlingOnLayout = false;
Selim Cinek19c8c702014-08-25 22:09:19 +0200426 mTouchAboveFalsingThreshold = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100427 initVelocityTracker();
428 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100429 break;
430 case MotionEvent.ACTION_POINTER_UP:
431 final int upPointer = event.getPointerId(event.getActionIndex());
432 if (mTrackingPointer == upPointer) {
433 // gesture is ongoing, find a new pointer to track
434 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
435 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200436 mInitialTouchX = event.getX(newIndex);
437 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100438 }
439 break;
440
441 case MotionEvent.ACTION_MOVE:
442 final float h = y - mInitialTouchY;
443 trackMovement(event);
444 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200445 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200446 if (mHeightAnimator != null) {
447 mHeightAnimator.cancel();
448 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100449 mInitialOffsetOnTouch = mExpandedHeight;
450 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200451 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200452 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200453 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200454 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100455 return true;
456 }
457 }
458 break;
Selim Cinek31094df2014-08-14 19:28:15 +0200459 case MotionEvent.ACTION_CANCEL:
460 case MotionEvent.ACTION_UP:
Selim Cinek31094df2014-08-14 19:28:15 +0200461 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100462 }
463 return false;
464 }
465
466 private void initVelocityTracker() {
467 if (mVelocityTracker != null) {
468 mVelocityTracker.recycle();
469 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200470 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100471 }
472
473 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200474 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100475 }
476
477 protected float getContentHeight() {
478 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400479 }
480
Daniel Sandler08d05e32012-08-08 16:39:54 -0400481 @Override
482 protected void onFinishInflate() {
483 super.onFinishInflate();
484 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400485 }
486
Jorim Jaggi069cd032014-05-15 03:09:01 +0200487 @Override
488 protected void onConfigurationChanged(Configuration newConfig) {
489 super.onConfigurationChanged(newConfig);
490 loadDimens();
Jorim Jaggi069cd032014-05-15 03:09:01 +0200491 }
492
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200493 /**
Jorim Jaggib7240132014-06-30 01:39:07 +0200494 * @param vel the current vertical velocity of the motion
495 * @param vectorVel the length of the vectorial velocity
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200496 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200497 */
Jorim Jaggidc96d632014-07-01 18:48:52 +0200498 protected boolean flingExpands(float vel, float vectorVel) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200499 if (isBelowFalsingThreshold()) {
Selim Cinek19c8c702014-08-25 22:09:19 +0200500 return true;
501 }
Jorim Jaggib7240132014-06-30 01:39:07 +0200502 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200503 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200504 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200505 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400506 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200507 }
508
Selim Cinek5386fb32014-09-03 16:37:36 +0200509 private boolean isBelowFalsingThreshold() {
510 return !mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded();
511 }
512
Jorim Jaggi1d480692014-05-20 19:41:58 +0200513 protected void fling(float vel, boolean expand) {
514 cancelPeek();
515 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200516
517 // Hack to make the expand transition look nice when clear all button is visible - we make
518 // the animation only to the last notification, and then jump to the maximum panel height so
519 // clear all just fades in and the decelerating motion is towards the last notification.
520 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible()
521 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight()
522 && !isClearAllVisible();
523 if (clearAllExpandHack) {
524 target = getMaxPanelHeight() - getClearAllHeight();
525 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200526 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200527 notifyExpandingFinished();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200528 return;
529 }
Jorim Jaggib01287f2014-09-12 02:12:26 +0200530 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
531 Log.i(TAG, "Executing fling: expand=" + expand + " vel=" + vel);
532 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200533 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200534 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200535 if (expand) {
Selim Cinek5386fb32014-09-03 16:37:36 +0200536 boolean belowFalsingThreshold = isBelowFalsingThreshold();
537 if (belowFalsingThreshold) {
538 vel = 0;
539 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200540 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
Selim Cinek5386fb32014-09-03 16:37:36 +0200541 if (belowFalsingThreshold) {
542 animator.setDuration(350);
543 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200544 } else {
545 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
546 getHeight());
547
548 // Make it shorter if we run a canned animation
549 if (vel == 0) {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200550 animator.setDuration((long)
551 (animator.getDuration() * getCannedFlingDurationFactor()));
Jorim Jaggi1d480692014-05-20 19:41:58 +0200552 }
553 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200554 animator.addListener(new AnimatorListenerAdapter() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200555 private boolean mCancelled;
556
557 @Override
558 public void onAnimationCancel(Animator animation) {
559 mCancelled = true;
560 }
561
Jorim Jaggi1d480692014-05-20 19:41:58 +0200562 @Override
563 public void onAnimationEnd(Animator animation) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200564 if (clearAllExpandHack && !mCancelled) {
Jorim Jaggi2ae259d2014-08-04 23:35:47 +0200565 setExpandedHeightInternal(getMaxPanelHeight());
566 }
567 mHeightAnimator = null;
568 if (!mCancelled) {
569 notifyExpandingFinished();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200570 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200571 }
572 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200573 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200574 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400575 }
576
577 @Override
578 protected void onAttachedToWindow() {
579 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400580 mViewName = getResources().getResourceName(getId());
581 }
582
583 public String getName() {
584 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400585 }
586
Daniel Sandler08d05e32012-08-08 16:39:54 -0400587 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400588 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200589 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler08d05e32012-08-08 16:39:54 -0400590 }
591
Daniel Sandler50508132012-08-16 14:10:53 -0400592 @Override
593 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Daniel Sandler50508132012-08-16 14:10:53 -0400594 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100595 requestPanelHeightUpdate();
Selim Cinek31094df2014-08-14 19:28:15 +0200596 mHasLayoutedSinceDown = true;
597 if (mUpdateFlingOnLayout) {
598 abortAnimations();
599 fling(mUpdateFlingVelocity, true);
600 mUpdateFlingOnLayout = false;
601 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100602 }
603
604 protected void requestPanelHeightUpdate() {
605 float currentMaxPanelHeight = getMaxPanelHeight();
606
607 // If the user isn't actively poking us, let's update the height
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200608 if ((!mTracking || isTrackingBlocked())
609 && mHeightAnimator == null
610 && mExpandedHeight > 0
611 && currentMaxPanelHeight != mExpandedHeight
612 && !mPeekPending
Selim Cinek31094df2014-08-14 19:28:15 +0200613 && mPeekAnimator == null
Jorim Jaggib7a33032014-08-20 16:21:36 +0200614 && !mPeekTouching) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200615 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100616 }
Daniel Sandler50508132012-08-16 14:10:53 -0400617 }
618
Daniel Sandler08d05e32012-08-08 16:39:54 -0400619 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200620 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
621 if (mHeightAnimator == null) {
622 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
623 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
624 setOverExpansion(overExpansionPixels, true /* isPixels */);
625 }
626 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
627 } else {
628 mExpandedHeight = h;
629 if (mOverExpandedBeforeFling) {
630 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
631 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100632 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400633
Jorim Jaggi93439da2014-06-30 23:53:39 +0200634 mExpandedHeight = Math.max(0, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100635 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200636 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
637 ? 0
638 : mExpandedHeight / fhWithoutOverExpansion);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200639 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400640 }
641
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200642 /**
643 * @return true if the panel tracking should be temporarily blocked; this is used when a
644 * conflicting gesture (opening QS) is happening
645 */
646 protected abstract boolean isTrackingBlocked();
647
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200648 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200649
Jorim Jaggi90129582014-06-02 14:44:49 +0200650 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100651
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200652 protected abstract float getOverExpansionAmount();
653
654 protected abstract float getOverExpansionPixels();
655
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100656 /**
657 * This returns the maximum height of the panel. Children should override this if their
658 * desired height is not the full height.
659 *
660 * @return the default implementation simply returns the maximum height.
661 */
Selim Cinek31094df2014-08-14 19:28:15 +0200662 protected abstract int getMaxPanelHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400663
664 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100665 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400666 }
667
668 public float getExpandedHeight() {
669 return mExpandedHeight;
670 }
671
672 public float getExpandedFraction() {
673 return mExpandedFraction;
674 }
675
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700676 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100677 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700678 }
679
680 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400681 return mExpandedHeight <= 0;
682 }
683
684 public boolean isCollapsing() {
685 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700686 }
687
John Spurlocka4b70af2013-08-17 14:05:49 -0400688 public boolean isTracking() {
689 return mTracking;
690 }
691
Daniel Sandler08d05e32012-08-08 16:39:54 -0400692 public void setBar(PanelBar panelBar) {
693 mBar = panelBar;
694 }
695
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200696 public void collapse(boolean delayed) {
John Spurlock97642182013-07-29 17:58:39 -0400697 if (DEBUG) logf("collapse: " + this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200698 if (mPeekPending || mPeekAnimator != null) {
699 mCollapseAfterPeek = true;
700 if (mPeekPending) {
701
702 // We know that the whole gesture is just a peek triggered by a simple click, so
703 // better start it now.
704 removeCallbacks(mPeekRunnable);
705 mPeekRunnable.run();
706 }
Jorim Jaggi98139552014-09-10 17:04:27 +0200707 } else if (!isFullyCollapsed() && !mTracking && !mClosing) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200708 if (mHeightAnimator != null) {
709 mHeightAnimator.cancel();
710 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400711 mClosing = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200712 notifyExpandingStarted();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200713 if (delayed) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200714 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
715 Log.i(TAG, "Posting collapse runnable, will be run in 120ms");
716 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200717 postDelayed(mFlingCollapseRunnable, 120);
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200718 } else {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200719 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
720 Log.i(TAG, "Animating collapsing now");
721 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200722 fling(0, false /* expand */);
723 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400724 }
725 }
726
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200727 private final Runnable mFlingCollapseRunnable = new Runnable() {
728 @Override
729 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200730 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
731 Log.i(TAG, "Executing collapse runnable, animating collapsing now");
732 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200733 fling(0, false /* expand */);
734 }
735 };
736
Daniel Sandler08d05e32012-08-08 16:39:54 -0400737 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400738 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400739 if (isFullyCollapsed()) {
740 mBar.startOpeningPanel(this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200741 notifyExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200742 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400743 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400744 if (DEBUG) logf("skipping expansion: is expanded");
745 }
746 }
747
748 public void cancelPeek() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200749 if (mPeekAnimator != null) {
John Spurlock97642182013-07-29 17:58:39 -0400750 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400751 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200752 removeCallbacks(mPeekRunnable);
753 mPeekPending = false;
754
755 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
756 // notify mBar that we might have closed ourselves.
757 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400758 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500759
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200760 public void instantExpand() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200761 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
762 Log.i(TAG, "Before instant expanding"
763 + " mTracking=" + mTracking
764 + " mExpanding=" + mExpanding);
765 }
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200766 mInstantExpanding = true;
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200767 mUpdateFlingOnLayout = false;
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200768 abortAnimations();
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200769 cancelPeek();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200770 if (mTracking) {
771 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
Jorim Jaggie62d5892014-09-02 16:31:19 +0200772 }
773 if (mExpanding) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200774 notifyExpandingFinished();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200775 }
776 setVisibility(VISIBLE);
777
778 // Wait for window manager to pickup the change, so we know the maximum height of the panel
779 // then.
780 getViewTreeObserver().addOnGlobalLayoutListener(
781 new ViewTreeObserver.OnGlobalLayoutListener() {
782 @Override
783 public void onGlobalLayout() {
784 if (mStatusBar.getStatusBarWindow().getHeight()
785 != mStatusBar.getStatusBarHeight()) {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200786 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
787 Log.i(TAG, "Now instant expanding after layout"
788 + " mTracking=" + mTracking
789 + " mExpanding=" + mExpanding);
790 }
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200791 getViewTreeObserver().removeOnGlobalLayoutListener(this);
792 setExpandedFraction(1f);
793 mInstantExpanding = false;
794 }
795 }
796 });
797
798 // Make sure a layout really happens.
799 requestLayout();
800 }
801
802 private void abortAnimations() {
803 cancelPeek();
804 if (mHeightAnimator != null) {
805 mHeightAnimator.cancel();
806 }
Jorim Jaggi5cbfe542014-09-10 22:23:08 +0200807 removeCallbacks(mPostCollapseRunnable);
808 removeCallbacks(mFlingCollapseRunnable);
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200809 }
810
Jorim Jaggi90129582014-06-02 14:44:49 +0200811 protected void startUnlockHintAnimation() {
812
813 // We don't need to hint the user if an animation is already running or the user is changing
814 // the expansion.
815 if (mHeightAnimator != null || mTracking) {
816 return;
817 }
818 cancelPeek();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200819 notifyExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200820 startUnlockHintAnimationPhase1(new Runnable() {
821 @Override
822 public void run() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200823 notifyExpandingFinished();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200824 mStatusBar.onHintFinished();
825 mHintAnimationRunning = false;
826 }
827 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200828 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200829 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200830 }
831
832 /**
833 * Phase 1: Move everything upwards.
834 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200835 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200836 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
837 ValueAnimator animator = createHeightAnimator(target);
838 animator.setDuration(250);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200839 animator.setInterpolator(mFastOutSlowInInterpolator);
Jorim Jaggi90129582014-06-02 14:44:49 +0200840 animator.addListener(new AnimatorListenerAdapter() {
841 private boolean mCancelled;
842
843 @Override
844 public void onAnimationCancel(Animator animation) {
845 mCancelled = true;
846 }
847
848 @Override
849 public void onAnimationEnd(Animator animation) {
850 if (mCancelled) {
851 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200852 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200853 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200854 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200855 }
856 }
857 });
858 animator.start();
859 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200860 mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY();
861 mKeyguardBottomArea.getIndicationView().animate()
862 .y(mOriginalIndicationY - mHintDistance)
863 .setDuration(250)
Jorim Jaggib472b3472014-06-30 19:56:24 +0200864 .setInterpolator(mFastOutSlowInInterpolator)
Selim Cinekf99d0002014-06-13 07:36:01 +0200865 .withEndAction(new Runnable() {
866 @Override
867 public void run() {
868 mKeyguardBottomArea.getIndicationView().animate()
869 .y(mOriginalIndicationY)
870 .setDuration(450)
871 .setInterpolator(mBounceInterpolator)
872 .start();
873 }
874 })
875 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200876 }
877
878 /**
879 * Phase 2: Bounce down.
880 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200881 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200882 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
883 animator.setDuration(450);
884 animator.setInterpolator(mBounceInterpolator);
885 animator.addListener(new AnimatorListenerAdapter() {
886 @Override
887 public void onAnimationEnd(Animator animation) {
888 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200889 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200890 }
891 });
892 animator.start();
893 mHeightAnimator = animator;
894 }
895
896 private ValueAnimator createHeightAnimator(float targetHeight) {
897 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
898 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
899 @Override
900 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200901 setExpandedHeightInternal((Float) animation.getAnimatedValue());
Jorim Jaggi90129582014-06-02 14:44:49 +0200902 }
903 });
904 return animator;
905 }
906
Jorim Jaggib472b3472014-06-30 19:56:24 +0200907 private void notifyBarPanelExpansionChanged() {
908 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
909 || mPeekAnimator != null);
910 }
911
Jorim Jaggi90129582014-06-02 14:44:49 +0200912 /**
913 * Gets called when the user performs a click anywhere in the empty area of the panel.
914 *
915 * @return whether the panel will be expanded after the action performed by this method
916 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200917 private boolean onEmptySpaceClick(float x) {
918 if (mHintAnimationRunning) {
919 return true;
920 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200921 if (x < mEdgeTapAreaWidth
922 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200923 onEdgeClicked(false /* right */);
924 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200925 } else if (x > getWidth() - mEdgeTapAreaWidth
926 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200927 onEdgeClicked(true /* right */);
928 return true;
929 } else {
930 return onMiddleClicked();
931 }
932 }
933
Jorim Jaggi488b7922014-08-05 21:12:02 +0200934 private final Runnable mPostCollapseRunnable = new Runnable() {
935 @Override
936 public void run() {
Jorim Jaggib01287f2014-09-12 02:12:26 +0200937 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
938 Log.i(TAG, "Collapsing after middle clicked");
939 }
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200940 collapse(false /* delayed */);
Jorim Jaggi488b7922014-08-05 21:12:02 +0200941 }
942 };
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200943 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200944 switch (mStatusBar.getBarState()) {
945 case StatusBarState.KEYGUARD:
946 startUnlockHintAnimation();
947 return true;
948 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200949 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200950 return true;
951 case StatusBarState.SHADE:
Jorim Jaggib01287f2014-09-12 02:12:26 +0200952 if (PhoneStatusBar.DEBUG_EMPTY_KEYGUARD) {
953 Log.i(TAG, "Middle clicked in shade state, posting collapsing runnable");
954 }
Jorim Jaggi488b7922014-08-05 21:12:02 +0200955
956 // This gets called in the middle of the touch handling, where the state is still
957 // that we are tracking the panel. Collapse the panel after this is done.
958 post(mPostCollapseRunnable);
Jorim Jaggi90129582014-06-02 14:44:49 +0200959 return false;
960 default:
961 return true;
962 }
963 }
964
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200965 protected abstract void onEdgeClicked(boolean right);
966
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500967 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200968 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400969 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500970 + "]",
971 this.getClass().getSimpleName(),
972 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100973 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500974 mClosing?"T":"f",
975 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500976 mJustPeeked?"T":"f",
977 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200978 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500979 ));
980 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200981
982 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200983
984 protected abstract float getPeekHeight();
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200985
986 protected abstract float getCannedFlingDurationFactor();
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200987
988 /**
989 * @return whether "Clear all" button will be visible when the panel is fully expanded
990 */
991 protected abstract boolean fullyExpandedClearAllVisible();
992
993 protected abstract boolean isClearAllVisible();
994
995 /**
996 * @return the height of the clear all button, in pixels
997 */
998 protected abstract int getClearAllHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400999}