blob: b175fd5cdd1724acc39bce24d7211bc7c5b665d3 [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;
Daniel Sandler08d05e32012-08-08 16:39:54 -040071
Jorim Jaggi1d480692014-05-20 19:41:58 +020072 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040073 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020074 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020075 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040076
Jorim Jaggi0a27be82014-06-11 03:22:39 +020077 /**
78 * Whether an instant expand request is currently pending and we are just waiting for layout.
79 */
80 private boolean mInstantExpanding;
81
Daniel Sandler08d05e32012-08-08 16:39:54 -040082 PanelBar mBar;
83
Daniel Sandler50508132012-08-16 14:10:53 -040084 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020085 private float mInitialTouchY;
86 private float mInitialTouchX;
Daniel Sandler08d05e32012-08-08 16:39:54 -040087
Jorim Jaggi90129582014-06-02 14:44:49 +020088 private Interpolator mLinearOutSlowInInterpolator;
Jorim Jaggib472b3472014-06-30 19:56:24 +020089 private Interpolator mFastOutSlowInInterpolator;
Jorim Jaggi90129582014-06-02 14:44:49 +020090 private Interpolator mBounceInterpolator;
Selim Cinekf99d0002014-06-13 07:36:01 +020091 protected KeyguardBottomAreaView mKeyguardBottomArea;
Jorim Jaggi90129582014-06-02 14:44:49 +020092
Jorim Jaggib472b3472014-06-30 19:56:24 +020093 private boolean mPeekPending;
94 private boolean mCollapseAfterPeek;
95 private boolean mExpanding;
96 private boolean mGestureWaitForTouchSlop;
97 private Runnable mPeekRunnable = new Runnable() {
98 @Override
99 public void run() {
100 mPeekPending = false;
101 runPeekAnimation();
102 }
103 };
104
Selim Cinek1685e632014-04-08 02:27:49 +0200105 protected void onExpandingFinished() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200106 mClosing = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200107 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +0200108 }
109
110 protected void onExpandingStarted() {
111 }
112
Jorim Jaggib472b3472014-06-30 19:56:24 +0200113 private void notifyExpandingStarted() {
114 if (!mExpanding) {
115 mExpanding = true;
116 onExpandingStarted();
117 }
118 }
119
120 private void notifyExpandingFinished() {
121 if (mExpanding) {
122 mExpanding = false;
123 onExpandingFinished();
124 }
125 }
126
127 private void schedulePeek() {
128 mPeekPending = true;
129 long timeout = ViewConfiguration.getTapTimeout();
130 postOnAnimationDelayed(mPeekRunnable, timeout);
131 notifyBarPanelExpansionChanged();
132 }
133
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400134 private void runPeekAnimation() {
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200135 mPeekHeight = getPeekHeight();
John Spurlock97642182013-07-29 17:58:39 -0400136 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200137 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400138 return;
139 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200140 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight)
141 .setDuration(250);
142 mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator);
143 mPeekAnimator.addListener(new AnimatorListenerAdapter() {
144 private boolean mCancelled;
145
146 @Override
147 public void onAnimationCancel(Animator animation) {
148 mCancelled = true;
149 }
150
151 @Override
152 public void onAnimationEnd(Animator animation) {
153 mPeekAnimator = null;
154 if (mCollapseAfterPeek && !mCancelled) {
155 postOnAnimation(new Runnable() {
156 @Override
157 public void run() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200158 collapse(false /* delayed */);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200159 }
160 });
161 }
162 mCollapseAfterPeek = false;
163 }
164 });
165 notifyExpandingStarted();
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400166 mPeekAnimator.start();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200167 mJustPeeked = true;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400168 }
169
Daniel Sandler08d05e32012-08-08 16:39:54 -0400170 public PanelView(Context context, AttributeSet attrs) {
171 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200172 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200173 mFastOutSlowInInterpolator =
Jorim Jaggi90129582014-06-02 14:44:49 +0200174 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200175 mLinearOutSlowInInterpolator =
176 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
Jorim Jaggi90129582014-06-02 14:44:49 +0200177 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400178 }
179
Jorim Jaggi069cd032014-05-15 03:09:01 +0200180 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400181 final Resources res = getContext().getResources();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100182 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
183 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200184 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200185 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400186 }
187
188 private void trackMovement(MotionEvent event) {
189 // Add movement to velocity tracker using raw screen X and Y coordinates instead
190 // of window coordinates because the window frame may be moving at the same time.
191 float deltaX = event.getRawX() - event.getX();
192 float deltaY = event.getRawY() - event.getY();
193 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400194 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400195 event.offsetLocation(-deltaX, -deltaY);
196 }
197
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400198 @Override
199 public boolean onTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200200 if (mInstantExpanding) {
201 return false;
202 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100203
204 /*
205 * We capture touch events here and update the expand height here in case according to
206 * the users fingers. This also handles multi-touch.
207 *
208 * If the user just clicks shortly, we give him a quick peek of the shade.
209 *
210 * Flinging is also enabled in order to open or close the shade.
211 */
212
213 int pointerIndex = event.findPointerIndex(mTrackingPointer);
214 if (pointerIndex < 0) {
215 pointerIndex = 0;
216 mTrackingPointer = event.getPointerId(pointerIndex);
217 }
218 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200219 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100220
Jorim Jaggib472b3472014-06-30 19:56:24 +0200221 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
222 mGestureWaitForTouchSlop = mExpandedHeight == 0f;
223 }
224 boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200225
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100226 switch (event.getActionMasked()) {
227 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100228 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200229 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200230 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200231 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200232 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200233 mPanelClosedOnDown = mExpandedHeight == 0.0f;
234 mHasLayoutedSinceDown = false;
235 mUpdateFlingOnLayout = false;
Jorim Jaggib7a33032014-08-20 16:21:36 +0200236 mPeekTouching = mPanelClosedOnDown;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200237 if (mVelocityTracker == null) {
238 initVelocityTracker();
239 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100240 trackMovement(event);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200241 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) ||
242 mPeekPending || mPeekAnimator != null) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200243 if (mHeightAnimator != null) {
244 mHeightAnimator.cancel(); // end any outstanding animations
245 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200246 cancelPeek();
247 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning)
248 || mPeekPending || mPeekAnimator != null;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200249 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200250 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100251 if (mExpandedHeight == 0) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200252 schedulePeek();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100253 }
254 break;
255
256 case MotionEvent.ACTION_POINTER_UP:
257 final int upPointer = event.getPointerId(event.getActionIndex());
258 if (mTrackingPointer == upPointer) {
259 // gesture is ongoing, find a new pointer to track
260 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
261 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200262 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100263 mTrackingPointer = event.getPointerId(newIndex);
264 mInitialOffsetOnTouch = mExpandedHeight;
265 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200266 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100267 }
268 break;
269
270 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200271 float h = y - mInitialTouchY;
Jorim Jaggib7240132014-06-30 01:39:07 +0200272
273 // If the panel was collapsed when touching, we only need to check for the
274 // y-component of the gesture, as we have no conflicting horizontal gesture.
275 if (Math.abs(h) > mTouchSlop
276 && (Math.abs(h) > Math.abs(x - mInitialTouchX)
277 || mInitialOffsetOnTouch == 0f)) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200278 mTouchSlopExceeded = true;
279 if (waitForTouchSlop && !mTracking) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200280 if (!mJustPeeked) {
281 mInitialOffsetOnTouch = mExpandedHeight;
282 mInitialTouchX = x;
283 mInitialTouchY = y;
284 h = 0;
285 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200286 if (mHeightAnimator != null) {
287 mHeightAnimator.cancel(); // end any outstanding animations
288 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200289 removeCallbacks(mPeekRunnable);
290 mPeekPending = false;
Jorim Jaggi90129582014-06-02 14:44:49 +0200291 onTrackingStarted();
Selim Cinek4c6969a2014-05-26 19:22:17 +0200292 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200293 }
Jorim Jaggi93439da2014-06-30 23:53:39 +0200294 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200295 if (newHeight > mPeekHeight) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200296 if (mPeekAnimator != null) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100297 mPeekAnimator.cancel();
298 }
299 mJustPeeked = false;
300 }
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200301 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) {
Jorim Jaggicc693242014-06-14 03:04:35 +0000302 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100303 }
304
305 trackMovement(event);
306 break;
307
308 case MotionEvent.ACTION_UP:
309 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100310 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100311 trackMovement(event);
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200312 if ((mTracking && mTouchSlopExceeded)
Jorim Jaggidc96d632014-07-01 18:48:52 +0200313 || Math.abs(x - mInitialTouchX) > mTouchSlop
314 || Math.abs(y - mInitialTouchY) > mTouchSlop
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200315 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
Jorim Jaggib7240132014-06-30 01:39:07 +0200316 float vel = 0f;
317 float vectorVel = 0f;
318 if (mVelocityTracker != null) {
319 mVelocityTracker.computeCurrentVelocity(1000);
320 vel = mVelocityTracker.getYVelocity();
321 vectorVel = (float) Math.hypot(
322 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
323 }
324 boolean expand = flingExpands(vel, vectorVel);
Jorim Jaggi90129582014-06-02 14:44:49 +0200325 onTrackingStopped(expand);
326 fling(vel, expand);
Selim Cinek31094df2014-08-14 19:28:15 +0200327 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown;
328 if (mUpdateFlingOnLayout) {
329 mUpdateFlingVelocity = vel;
330 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200331 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200332 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200333 onTrackingStopped(expands);
334 }
Jorim Jaggidc96d632014-07-01 18:48:52 +0200335
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100336 if (mVelocityTracker != null) {
337 mVelocityTracker.recycle();
338 mVelocityTracker = null;
339 }
Jorim Jaggib7a33032014-08-20 16:21:36 +0200340 mPeekTouching = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100341 break;
342 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200343 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100344 }
345
Selim Cinek4c6969a2014-05-26 19:22:17 +0200346 protected abstract boolean hasConflictingGestures();
347
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200348 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200349 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200350 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200351 }
352
353 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200354 mTracking = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200355 mCollapseAfterPeek = false;
Selim Cinek1685e632014-04-08 02:27:49 +0200356 mBar.onTrackingStarted(PanelView.this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200357 notifyExpandingStarted();
Selim Cinek1685e632014-04-08 02:27:49 +0200358 }
359
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100360 @Override
361 public boolean onInterceptTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200362 if (mInstantExpanding) {
363 return false;
364 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100365
366 /*
367 * If the user drags anywhere inside the panel we intercept it if he moves his finger
368 * upwards. This allows closing the shade from anywhere inside the panel.
369 *
370 * We only do this if the current content is scrolled to the bottom,
371 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
372 * possible.
373 */
374 int pointerIndex = event.findPointerIndex(mTrackingPointer);
375 if (pointerIndex < 0) {
376 pointerIndex = 0;
377 mTrackingPointer = event.getPointerId(pointerIndex);
378 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200379 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100380 final float y = event.getY(pointerIndex);
381 boolean scrolledToBottom = isScrolledToBottom();
382
383 switch (event.getActionMasked()) {
384 case MotionEvent.ACTION_DOWN:
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200385 mStatusBar.userActivity();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200386 if (mHeightAnimator != null && !mHintAnimationRunning ||
387 mPeekPending || mPeekAnimator != null) {
388 if (mHeightAnimator != null) {
389 mHeightAnimator.cancel(); // end any outstanding animations
390 }
391 cancelPeek();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200392 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200393 return true;
394 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100395 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200396 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200397 mTouchSlopExceeded = false;
Jorim Jaggi3857ac42014-06-27 18:01:12 +0200398 mJustPeeked = false;
Selim Cinek31094df2014-08-14 19:28:15 +0200399 mPanelClosedOnDown = mExpandedHeight == 0.0f;
400 mHasLayoutedSinceDown = false;
401 mUpdateFlingOnLayout = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100402 initVelocityTracker();
403 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100404 break;
405 case MotionEvent.ACTION_POINTER_UP:
406 final int upPointer = event.getPointerId(event.getActionIndex());
407 if (mTrackingPointer == upPointer) {
408 // gesture is ongoing, find a new pointer to track
409 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
410 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200411 mInitialTouchX = event.getX(newIndex);
412 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100413 }
414 break;
415
416 case MotionEvent.ACTION_MOVE:
417 final float h = y - mInitialTouchY;
418 trackMovement(event);
419 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200420 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200421 if (mHeightAnimator != null) {
422 mHeightAnimator.cancel();
423 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100424 mInitialOffsetOnTouch = mExpandedHeight;
425 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200426 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200427 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200428 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200429 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100430 return true;
431 }
432 }
433 break;
Selim Cinek31094df2014-08-14 19:28:15 +0200434 case MotionEvent.ACTION_CANCEL:
435 case MotionEvent.ACTION_UP:
Selim Cinek31094df2014-08-14 19:28:15 +0200436 break;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100437 }
438 return false;
439 }
440
441 private void initVelocityTracker() {
442 if (mVelocityTracker != null) {
443 mVelocityTracker.recycle();
444 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200445 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100446 }
447
448 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200449 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100450 }
451
452 protected float getContentHeight() {
453 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400454 }
455
Daniel Sandler08d05e32012-08-08 16:39:54 -0400456 @Override
457 protected void onFinishInflate() {
458 super.onFinishInflate();
459 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400460 }
461
Jorim Jaggi069cd032014-05-15 03:09:01 +0200462 @Override
463 protected void onConfigurationChanged(Configuration newConfig) {
464 super.onConfigurationChanged(newConfig);
465 loadDimens();
Jorim Jaggi069cd032014-05-15 03:09:01 +0200466 }
467
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200468 /**
Jorim Jaggib7240132014-06-30 01:39:07 +0200469 * @param vel the current vertical velocity of the motion
470 * @param vectorVel the length of the vectorial velocity
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200471 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200472 */
Jorim Jaggidc96d632014-07-01 18:48:52 +0200473 protected boolean flingExpands(float vel, float vectorVel) {
Jorim Jaggib7240132014-06-30 01:39:07 +0200474 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200475 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200476 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200477 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400478 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200479 }
480
481 protected void fling(float vel, boolean expand) {
482 cancelPeek();
483 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200484
485 // Hack to make the expand transition look nice when clear all button is visible - we make
486 // the animation only to the last notification, and then jump to the maximum panel height so
487 // clear all just fades in and the decelerating motion is towards the last notification.
488 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible()
489 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight()
490 && !isClearAllVisible();
491 if (clearAllExpandHack) {
492 target = getMaxPanelHeight() - getClearAllHeight();
493 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200494 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200495 notifyExpandingFinished();
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200496 return;
497 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200498 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200499 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200500 if (expand) {
501 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
502 } else {
503 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
504 getHeight());
505
506 // Make it shorter if we run a canned animation
507 if (vel == 0) {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200508 animator.setDuration((long)
509 (animator.getDuration() * getCannedFlingDurationFactor()));
Jorim Jaggi1d480692014-05-20 19:41:58 +0200510 }
511 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200512 animator.addListener(new AnimatorListenerAdapter() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200513 private boolean mCancelled;
514
515 @Override
516 public void onAnimationCancel(Animator animation) {
517 mCancelled = true;
518 }
519
Jorim Jaggi1d480692014-05-20 19:41:58 +0200520 @Override
521 public void onAnimationEnd(Animator animation) {
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200522 if (clearAllExpandHack && !mCancelled) {
Jorim Jaggi2ae259d2014-08-04 23:35:47 +0200523 setExpandedHeightInternal(getMaxPanelHeight());
524 }
525 mHeightAnimator = null;
526 if (!mCancelled) {
527 notifyExpandingFinished();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200528 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200529 }
530 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200531 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200532 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400533 }
534
535 @Override
536 protected void onAttachedToWindow() {
537 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400538 mViewName = getResources().getResourceName(getId());
539 }
540
541 public String getName() {
542 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400543 }
544
Daniel Sandler08d05e32012-08-08 16:39:54 -0400545 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400546 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200547 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler08d05e32012-08-08 16:39:54 -0400548 }
549
Daniel Sandler50508132012-08-16 14:10:53 -0400550 @Override
551 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Daniel Sandler50508132012-08-16 14:10:53 -0400552 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100553 requestPanelHeightUpdate();
Selim Cinek31094df2014-08-14 19:28:15 +0200554 mHasLayoutedSinceDown = true;
555 if (mUpdateFlingOnLayout) {
556 abortAnimations();
557 fling(mUpdateFlingVelocity, true);
558 mUpdateFlingOnLayout = false;
559 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100560 }
561
562 protected void requestPanelHeightUpdate() {
563 float currentMaxPanelHeight = getMaxPanelHeight();
564
565 // If the user isn't actively poking us, let's update the height
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200566 if ((!mTracking || isTrackingBlocked())
567 && mHeightAnimator == null
568 && mExpandedHeight > 0
569 && currentMaxPanelHeight != mExpandedHeight
570 && !mPeekPending
Selim Cinek31094df2014-08-14 19:28:15 +0200571 && mPeekAnimator == null
Jorim Jaggib7a33032014-08-20 16:21:36 +0200572 && !mPeekTouching) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200573 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100574 }
Daniel Sandler50508132012-08-16 14:10:53 -0400575 }
576
Daniel Sandler08d05e32012-08-08 16:39:54 -0400577 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200578 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
579 if (mHeightAnimator == null) {
580 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
581 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
582 setOverExpansion(overExpansionPixels, true /* isPixels */);
583 }
584 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
585 } else {
586 mExpandedHeight = h;
587 if (mOverExpandedBeforeFling) {
588 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
589 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100590 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400591
Jorim Jaggi93439da2014-06-30 23:53:39 +0200592 mExpandedHeight = Math.max(0, mExpandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100593 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200594 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
595 ? 0
596 : mExpandedHeight / fhWithoutOverExpansion);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200597 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400598 }
599
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200600 /**
601 * @return true if the panel tracking should be temporarily blocked; this is used when a
602 * conflicting gesture (opening QS) is happening
603 */
604 protected abstract boolean isTrackingBlocked();
605
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200606 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200607
Jorim Jaggi90129582014-06-02 14:44:49 +0200608 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100609
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200610 protected abstract float getOverExpansionAmount();
611
612 protected abstract float getOverExpansionPixels();
613
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100614 /**
615 * This returns the maximum height of the panel. Children should override this if their
616 * desired height is not the full height.
617 *
618 * @return the default implementation simply returns the maximum height.
619 */
Selim Cinek31094df2014-08-14 19:28:15 +0200620 protected abstract int getMaxPanelHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400621
622 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100623 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400624 }
625
626 public float getExpandedHeight() {
627 return mExpandedHeight;
628 }
629
630 public float getExpandedFraction() {
631 return mExpandedFraction;
632 }
633
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700634 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100635 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700636 }
637
638 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400639 return mExpandedHeight <= 0;
640 }
641
642 public boolean isCollapsing() {
643 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700644 }
645
John Spurlocka4b70af2013-08-17 14:05:49 -0400646 public boolean isTracking() {
647 return mTracking;
648 }
649
Daniel Sandler08d05e32012-08-08 16:39:54 -0400650 public void setBar(PanelBar panelBar) {
651 mBar = panelBar;
652 }
653
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200654 public void collapse(boolean delayed) {
John Spurlock97642182013-07-29 17:58:39 -0400655 if (DEBUG) logf("collapse: " + this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200656 if (mPeekPending || mPeekAnimator != null) {
657 mCollapseAfterPeek = true;
658 if (mPeekPending) {
659
660 // We know that the whole gesture is just a peek triggered by a simple click, so
661 // better start it now.
662 removeCallbacks(mPeekRunnable);
663 mPeekRunnable.run();
664 }
Jorim Jaggifb98a502014-07-11 02:12:03 +0200665 } else if (!isFullyCollapsed() && !mTracking) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200666 if (mHeightAnimator != null) {
667 mHeightAnimator.cancel();
668 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400669 mClosing = true;
Jorim Jaggib472b3472014-06-30 19:56:24 +0200670 notifyExpandingStarted();
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200671 if (delayed) {
672 postDelayed(new Runnable() {
673 @Override
674 public void run() {
675 fling(0, false /* expand */);
676 }
677 }, 120);
678 } else {
679 fling(0, false /* expand */);
680 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400681 }
682 }
683
684 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400685 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400686 if (isFullyCollapsed()) {
687 mBar.startOpeningPanel(this);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200688 notifyExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200689 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400690 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400691 if (DEBUG) logf("skipping expansion: is expanded");
692 }
693 }
694
695 public void cancelPeek() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200696 if (mPeekAnimator != null) {
John Spurlock97642182013-07-29 17:58:39 -0400697 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400698 }
Jorim Jaggib472b3472014-06-30 19:56:24 +0200699 removeCallbacks(mPeekRunnable);
700 mPeekPending = false;
701
702 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
703 // notify mBar that we might have closed ourselves.
704 notifyBarPanelExpansionChanged();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400705 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500706
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200707 public void instantExpand() {
708 mInstantExpanding = true;
709 abortAnimations();
710 if (mTracking) {
711 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
Jorim Jaggib472b3472014-06-30 19:56:24 +0200712 notifyExpandingFinished();
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200713 }
714 setVisibility(VISIBLE);
715
716 // Wait for window manager to pickup the change, so we know the maximum height of the panel
717 // then.
718 getViewTreeObserver().addOnGlobalLayoutListener(
719 new ViewTreeObserver.OnGlobalLayoutListener() {
720 @Override
721 public void onGlobalLayout() {
722 if (mStatusBar.getStatusBarWindow().getHeight()
723 != mStatusBar.getStatusBarHeight()) {
724 getViewTreeObserver().removeOnGlobalLayoutListener(this);
725 setExpandedFraction(1f);
726 mInstantExpanding = false;
727 }
728 }
729 });
730
731 // Make sure a layout really happens.
732 requestLayout();
733 }
734
735 private void abortAnimations() {
736 cancelPeek();
737 if (mHeightAnimator != null) {
738 mHeightAnimator.cancel();
739 }
740 }
741
Jorim Jaggi90129582014-06-02 14:44:49 +0200742 protected void startUnlockHintAnimation() {
743
744 // We don't need to hint the user if an animation is already running or the user is changing
745 // the expansion.
746 if (mHeightAnimator != null || mTracking) {
747 return;
748 }
749 cancelPeek();
Jorim Jaggib472b3472014-06-30 19:56:24 +0200750 notifyExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200751 startUnlockHintAnimationPhase1(new Runnable() {
752 @Override
753 public void run() {
Jorim Jaggib472b3472014-06-30 19:56:24 +0200754 notifyExpandingFinished();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200755 mStatusBar.onHintFinished();
756 mHintAnimationRunning = false;
757 }
758 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200759 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200760 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200761 }
762
763 /**
764 * Phase 1: Move everything upwards.
765 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200766 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200767 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
768 ValueAnimator animator = createHeightAnimator(target);
769 animator.setDuration(250);
Jorim Jaggib472b3472014-06-30 19:56:24 +0200770 animator.setInterpolator(mFastOutSlowInInterpolator);
Jorim Jaggi90129582014-06-02 14:44:49 +0200771 animator.addListener(new AnimatorListenerAdapter() {
772 private boolean mCancelled;
773
774 @Override
775 public void onAnimationCancel(Animator animation) {
776 mCancelled = true;
777 }
778
779 @Override
780 public void onAnimationEnd(Animator animation) {
781 if (mCancelled) {
782 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200783 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200784 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200785 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200786 }
787 }
788 });
789 animator.start();
790 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200791 mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY();
792 mKeyguardBottomArea.getIndicationView().animate()
793 .y(mOriginalIndicationY - mHintDistance)
794 .setDuration(250)
Jorim Jaggib472b3472014-06-30 19:56:24 +0200795 .setInterpolator(mFastOutSlowInInterpolator)
Selim Cinekf99d0002014-06-13 07:36:01 +0200796 .withEndAction(new Runnable() {
797 @Override
798 public void run() {
799 mKeyguardBottomArea.getIndicationView().animate()
800 .y(mOriginalIndicationY)
801 .setDuration(450)
802 .setInterpolator(mBounceInterpolator)
803 .start();
804 }
805 })
806 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200807 }
808
809 /**
810 * Phase 2: Bounce down.
811 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200812 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200813 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
814 animator.setDuration(450);
815 animator.setInterpolator(mBounceInterpolator);
816 animator.addListener(new AnimatorListenerAdapter() {
817 @Override
818 public void onAnimationEnd(Animator animation) {
819 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200820 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200821 }
822 });
823 animator.start();
824 mHeightAnimator = animator;
825 }
826
827 private ValueAnimator createHeightAnimator(float targetHeight) {
828 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
829 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
830 @Override
831 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200832 setExpandedHeightInternal((Float) animation.getAnimatedValue());
Jorim Jaggi90129582014-06-02 14:44:49 +0200833 }
834 });
835 return animator;
836 }
837
Jorim Jaggib472b3472014-06-30 19:56:24 +0200838 private void notifyBarPanelExpansionChanged() {
839 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
840 || mPeekAnimator != null);
841 }
842
Jorim Jaggi90129582014-06-02 14:44:49 +0200843 /**
844 * Gets called when the user performs a click anywhere in the empty area of the panel.
845 *
846 * @return whether the panel will be expanded after the action performed by this method
847 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200848 private boolean onEmptySpaceClick(float x) {
849 if (mHintAnimationRunning) {
850 return true;
851 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200852 if (x < mEdgeTapAreaWidth
853 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200854 onEdgeClicked(false /* right */);
855 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200856 } else if (x > getWidth() - mEdgeTapAreaWidth
857 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200858 onEdgeClicked(true /* right */);
859 return true;
860 } else {
861 return onMiddleClicked();
862 }
863 }
864
Jorim Jaggi488b7922014-08-05 21:12:02 +0200865 private final Runnable mPostCollapseRunnable = new Runnable() {
866 @Override
867 public void run() {
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200868 collapse(false /* delayed */);
Jorim Jaggi488b7922014-08-05 21:12:02 +0200869 }
870 };
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200871 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200872 switch (mStatusBar.getBarState()) {
873 case StatusBarState.KEYGUARD:
874 startUnlockHintAnimation();
875 return true;
876 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200877 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200878 return true;
879 case StatusBarState.SHADE:
Jorim Jaggi488b7922014-08-05 21:12:02 +0200880
881 // This gets called in the middle of the touch handling, where the state is still
882 // that we are tracking the panel. Collapse the panel after this is done.
883 post(mPostCollapseRunnable);
Jorim Jaggi90129582014-06-02 14:44:49 +0200884 return false;
885 default:
886 return true;
887 }
888 }
889
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200890 protected abstract void onEdgeClicked(boolean right);
891
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500892 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200893 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400894 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500895 + "]",
896 this.getClass().getSimpleName(),
897 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100898 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500899 mClosing?"T":"f",
900 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500901 mJustPeeked?"T":"f",
902 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200903 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500904 ));
905 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200906
907 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200908
909 protected abstract float getPeekHeight();
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200910
911 protected abstract float getCannedFlingDurationFactor();
Jorim Jaggi4b04a3a2014-07-28 17:43:56 +0200912
913 /**
914 * @return whether "Clear all" button will be visible when the panel is fully expanded
915 */
916 protected abstract boolean fullyExpandedClearAllVisible();
917
918 protected abstract boolean isClearAllVisible();
919
920 /**
921 * @return the height of the clear all button, in pixels
922 */
923 protected abstract int getClearAllHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400924}