blob: 1f3098d94d41de9c8eeb3becb74b52814a87864b [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;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040057 private boolean mJustPeeked;
Daniel Sandler50508132012-08-16 14:10:53 -040058 private boolean mClosing;
Jorim Jaggi8dd95e02014-06-03 16:19:33 +020059 protected boolean mTracking;
Jorim Jaggi90129582014-06-02 14:44:49 +020060 private boolean mTouchSlopExceeded;
John Spurlock48fa91a2013-08-15 09:29:31 -040061 private int mTrackingPointer;
Jorim Jaggid7daab72014-05-06 22:22:20 +020062 protected int mTouchSlop;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020063 protected boolean mHintAnimationRunning;
Jorim Jaggi47c85a32014-06-05 17:25:40 +020064 private boolean mOverExpandedBeforeFling;
Daniel Sandler08d05e32012-08-08 16:39:54 -040065
Jorim Jaggi1d480692014-05-20 19:41:58 +020066 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040067 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020068 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020069 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040070
Jorim Jaggi0a27be82014-06-11 03:22:39 +020071 /**
72 * Whether an instant expand request is currently pending and we are just waiting for layout.
73 */
74 private boolean mInstantExpanding;
75
Daniel Sandler08d05e32012-08-08 16:39:54 -040076 PanelBar mBar;
77
Selim Cinekb84a1072014-05-15 19:10:18 +020078 protected int mMaxPanelHeight = -1;
Daniel Sandler50508132012-08-16 14:10:53 -040079 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020080 private float mInitialTouchY;
81 private float mInitialTouchX;
Daniel Sandler08d05e32012-08-08 16:39:54 -040082
Jorim Jaggi90129582014-06-02 14:44:49 +020083 private Interpolator mLinearOutSlowInInterpolator;
84 private Interpolator mBounceInterpolator;
85
Selim Cinek1685e632014-04-08 02:27:49 +020086 protected void onExpandingFinished() {
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +020087 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +020088 }
89
90 protected void onExpandingStarted() {
91 }
92
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040093 private void runPeekAnimation() {
John Spurlock97642182013-07-29 17:58:39 -040094 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +020095 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040096 return;
97 }
98 if (mPeekAnimator == null) {
John Spurlock209bede2013-07-17 12:23:27 -040099 mPeekAnimator = ObjectAnimator.ofFloat(this,
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400100 "expandedHeight", mPeekHeight)
Daniel Sandler3679bf52012-10-16 21:30:28 -0400101 .setDuration(250);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400102 }
103 mPeekAnimator.start();
104 }
105
Daniel Sandler08d05e32012-08-08 16:39:54 -0400106 public PanelView(Context context, AttributeSet attrs) {
107 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200108 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggi90129582014-06-02 14:44:49 +0200109 mLinearOutSlowInInterpolator =
110 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
111 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400112 }
113
Jorim Jaggi069cd032014-05-15 03:09:01 +0200114 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400115 final Resources res = getContext().getResources();
John Spurlock209bede2013-07-17 12:23:27 -0400116 mPeekHeight = res.getDimension(R.dimen.peek_height)
John Spurlock50728832014-04-17 19:05:28 -0400117 + getPaddingBottom(); // our window might have a dropshadow
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100118
119 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
120 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200121 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200122 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400123 }
124
125 private void trackMovement(MotionEvent event) {
126 // Add movement to velocity tracker using raw screen X and Y coordinates instead
127 // of window coordinates because the window frame may be moving at the same time.
128 float deltaX = event.getRawX() - event.getX();
129 float deltaY = event.getRawY() - event.getY();
130 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400131 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400132 event.offsetLocation(-deltaX, -deltaY);
133 }
134
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400135 @Override
136 public boolean onTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200137 if (mInstantExpanding) {
138 return false;
139 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100140
141 /*
142 * We capture touch events here and update the expand height here in case according to
143 * the users fingers. This also handles multi-touch.
144 *
145 * If the user just clicks shortly, we give him a quick peek of the shade.
146 *
147 * Flinging is also enabled in order to open or close the shade.
148 */
149
150 int pointerIndex = event.findPointerIndex(mTrackingPointer);
151 if (pointerIndex < 0) {
152 pointerIndex = 0;
153 mTrackingPointer = event.getPointerId(pointerIndex);
154 }
155 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200156 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100157
Selim Cinek4c6969a2014-05-26 19:22:17 +0200158 boolean waitForTouchSlop = hasConflictingGestures();
159
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100160 switch (event.getActionMasked()) {
161 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100162 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200163 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200164 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200165 mTouchSlopExceeded = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200166 if (mVelocityTracker == null) {
167 initVelocityTracker();
168 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100169 trackMovement(event);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200170 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning)) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200171 if (mHeightAnimator != null) {
172 mHeightAnimator.cancel(); // end any outstanding animations
173 }
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200174 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200175 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200176 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100177 if (mExpandedHeight == 0) {
178 mJustPeeked = true;
179 runPeekAnimation();
180 }
181 break;
182
183 case MotionEvent.ACTION_POINTER_UP:
184 final int upPointer = event.getPointerId(event.getActionIndex());
185 if (mTrackingPointer == upPointer) {
186 // gesture is ongoing, find a new pointer to track
187 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
188 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200189 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100190 mTrackingPointer = event.getPointerId(newIndex);
191 mInitialOffsetOnTouch = mExpandedHeight;
192 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200193 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100194 }
195 break;
196
197 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200198 float h = y - mInitialTouchY;
Jorim Jaggi90129582014-06-02 14:44:49 +0200199 if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)) {
200 mTouchSlopExceeded = true;
201 if (waitForTouchSlop && !mTracking) {
202 mInitialOffsetOnTouch = mExpandedHeight;
203 mInitialTouchX = x;
204 mInitialTouchY = y;
205 if (mHeightAnimator != null) {
206 mHeightAnimator.cancel(); // end any outstanding animations
207 }
208 onTrackingStarted();
209 h = 0;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200210 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200211 }
212 final float newHeight = h + mInitialOffsetOnTouch;
213 if (newHeight > mPeekHeight) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100214 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
215 mPeekAnimator.cancel();
216 }
217 mJustPeeked = false;
218 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200219 if (!mJustPeeked && (!waitForTouchSlop || mTracking)) {
220 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100221 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
222 }
223
224 trackMovement(event);
225 break;
226
227 case MotionEvent.ACTION_UP:
228 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100229 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100230 trackMovement(event);
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200231 if ((mTracking && mTouchSlopExceeded)
232 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200233 float vel = getCurrentVelocity();
234 boolean expand = flingExpands(vel);
235 onTrackingStopped(expand);
236 fling(vel, expand);
237 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200238 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200239 onTrackingStopped(expands);
240 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100241 if (mVelocityTracker != null) {
242 mVelocityTracker.recycle();
243 mVelocityTracker = null;
244 }
245 break;
246 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200247 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100248 }
249
Selim Cinek4c6969a2014-05-26 19:22:17 +0200250 protected abstract boolean hasConflictingGestures();
251
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200252 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200253 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200254 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200255 }
256
257 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200258 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200259 mBar.onTrackingStarted(PanelView.this);
260 onExpandingStarted();
261 }
262
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100263 private float getCurrentVelocity() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100264
265 // the velocitytracker might be null if we got a bad input stream
266 if (mVelocityTracker == null) {
267 return 0;
268 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100269 mVelocityTracker.computeCurrentVelocity(1000);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200270 return mVelocityTracker.getYVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100271 }
272
273 @Override
274 public boolean onInterceptTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200275 if (mInstantExpanding) {
276 return false;
277 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100278
279 /*
280 * If the user drags anywhere inside the panel we intercept it if he moves his finger
281 * upwards. This allows closing the shade from anywhere inside the panel.
282 *
283 * We only do this if the current content is scrolled to the bottom,
284 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
285 * possible.
286 */
287 int pointerIndex = event.findPointerIndex(mTrackingPointer);
288 if (pointerIndex < 0) {
289 pointerIndex = 0;
290 mTrackingPointer = event.getPointerId(pointerIndex);
291 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200292 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100293 final float y = event.getY(pointerIndex);
294 boolean scrolledToBottom = isScrolledToBottom();
295
296 switch (event.getActionMasked()) {
297 case MotionEvent.ACTION_DOWN:
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200298 if (mHeightAnimator != null && !mHintAnimationRunning) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200299 mHeightAnimator.cancel(); // end any outstanding animations
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200300 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200301 return true;
302 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100303 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200304 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200305 mTouchSlopExceeded = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100306 initVelocityTracker();
307 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100308 break;
309 case MotionEvent.ACTION_POINTER_UP:
310 final int upPointer = event.getPointerId(event.getActionIndex());
311 if (mTrackingPointer == upPointer) {
312 // gesture is ongoing, find a new pointer to track
313 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
314 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200315 mInitialTouchX = event.getX(newIndex);
316 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100317 }
318 break;
319
320 case MotionEvent.ACTION_MOVE:
321 final float h = y - mInitialTouchY;
322 trackMovement(event);
323 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200324 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200325 if (mHeightAnimator != null) {
326 mHeightAnimator.cancel();
327 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100328 mInitialOffsetOnTouch = mExpandedHeight;
329 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200330 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200331 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200332 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200333 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100334 return true;
335 }
336 }
337 break;
338 }
339 return false;
340 }
341
342 private void initVelocityTracker() {
343 if (mVelocityTracker != null) {
344 mVelocityTracker.recycle();
345 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200346 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100347 }
348
349 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200350 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100351 }
352
353 protected float getContentHeight() {
354 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400355 }
356
Daniel Sandler08d05e32012-08-08 16:39:54 -0400357 @Override
358 protected void onFinishInflate() {
359 super.onFinishInflate();
360 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400361 }
362
Jorim Jaggi069cd032014-05-15 03:09:01 +0200363 @Override
364 protected void onConfigurationChanged(Configuration newConfig) {
365 super.onConfigurationChanged(newConfig);
366 loadDimens();
367 mMaxPanelHeight = -1;
368 }
369
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200370 /**
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200371 * @param vel the current velocity of the motion
372 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200373 */
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200374 private boolean flingExpands(float vel) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200375 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200376 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200377 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200378 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400379 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200380 }
381
382 protected void fling(float vel, boolean expand) {
383 cancelPeek();
384 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200385 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200386 onExpandingFinished();
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200387 mBar.panelExpansionChanged(this, mExpandedFraction);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200388 return;
389 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200390 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200391 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200392 if (expand) {
393 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
394 } else {
395 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
396 getHeight());
397
398 // Make it shorter if we run a canned animation
399 if (vel == 0) {
400 animator.setDuration((long) (animator.getDuration() / 1.75f));
401 }
402 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200403 animator.addListener(new AnimatorListenerAdapter() {
404 @Override
405 public void onAnimationEnd(Animator animation) {
406 mHeightAnimator = null;
407 onExpandingFinished();
408 }
409 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200410 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200411 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400412 }
413
414 @Override
415 protected void onAttachedToWindow() {
416 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400417 mViewName = getResources().getResourceName(getId());
418 }
419
420 public String getName() {
421 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400422 }
423
Daniel Sandler08d05e32012-08-08 16:39:54 -0400424 @Override
425 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
426 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
427
John Spurlock97642182013-07-29 17:58:39 -0400428 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
Daniel Sandler08d05e32012-08-08 16:39:54 -0400429 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
Daniel Sandler198a0302012-08-17 16:04:31 -0400430
431 // Did one of our children change size?
432 int newHeight = getMeasuredHeight();
Selim Cinekb84a1072014-05-15 19:10:18 +0200433 if (newHeight > mMaxPanelHeight) {
434 // we only adapt the max height if it's bigger
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100435 mMaxPanelHeight = newHeight;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200436 // If the user isn't actively poking us, let's rubberband to the content
Jorim Jaggi1d480692014-05-20 19:41:58 +0200437 if (!mTracking && mHeightAnimator == null
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200438 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
439 && mMaxPanelHeight > 0) {
440 mExpandedHeight = mMaxPanelHeight;
441 }
Daniel Sandler50508132012-08-16 14:10:53 -0400442 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400443 }
444
Daniel Sandler08d05e32012-08-08 16:39:54 -0400445 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400446 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200447 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400448 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400449 }
450
Daniel Sandler50508132012-08-16 14:10:53 -0400451 @Override
452 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100453 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
454 (int)mExpandedHeight, mMaxPanelHeight);
Daniel Sandler50508132012-08-16 14:10:53 -0400455 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100456 requestPanelHeightUpdate();
457 }
458
459 protected void requestPanelHeightUpdate() {
460 float currentMaxPanelHeight = getMaxPanelHeight();
461
462 // If the user isn't actively poking us, let's update the height
Jorim Jaggi1d480692014-05-20 19:41:58 +0200463 if (!mTracking && mHeightAnimator == null
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100464 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200465 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100466 }
Daniel Sandler50508132012-08-16 14:10:53 -0400467 }
468
Daniel Sandler08d05e32012-08-08 16:39:54 -0400469 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200470 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
471 if (mHeightAnimator == null) {
472 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
473 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
474 setOverExpansion(overExpansionPixels, true /* isPixels */);
475 }
476 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
477 } else {
478 mExpandedHeight = h;
479 if (mOverExpandedBeforeFling) {
480 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
481 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100482 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400483
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100484 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200485 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
486 ? 0
487 : mExpandedHeight / fhWithoutOverExpansion);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400488 }
489
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200490 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200491
Jorim Jaggi90129582014-06-02 14:44:49 +0200492 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100493
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200494 protected abstract float getOverExpansionAmount();
495
496 protected abstract float getOverExpansionPixels();
497
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100498 /**
499 * This returns the maximum height of the panel. Children should override this if their
500 * desired height is not the full height.
501 *
502 * @return the default implementation simply returns the maximum height.
503 */
504 protected int getMaxPanelHeight() {
Selim Cinekb84a1072014-05-15 19:10:18 +0200505 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100506 return mMaxPanelHeight;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400507 }
508
509 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100510 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400511 }
512
513 public float getExpandedHeight() {
514 return mExpandedHeight;
515 }
516
517 public float getExpandedFraction() {
518 return mExpandedFraction;
519 }
520
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700521 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100522 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700523 }
524
525 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400526 return mExpandedHeight <= 0;
527 }
528
529 public boolean isCollapsing() {
530 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700531 }
532
John Spurlocka4b70af2013-08-17 14:05:49 -0400533 public boolean isTracking() {
534 return mTracking;
535 }
536
Daniel Sandler08d05e32012-08-08 16:39:54 -0400537 public void setBar(PanelBar panelBar) {
538 mBar = panelBar;
539 }
540
Daniel Sandler08d05e32012-08-08 16:39:54 -0400541 public void collapse() {
542 // TODO: abort animation or ongoing touch
John Spurlock97642182013-07-29 17:58:39 -0400543 if (DEBUG) logf("collapse: " + this);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700544 if (!isFullyCollapsed()) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200545 if (mHeightAnimator != null) {
546 mHeightAnimator.cancel();
547 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400548 mClosing = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200549 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200550 fling(0, false /* expand */);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400551 }
552 }
553
554 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400555 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400556 if (isFullyCollapsed()) {
557 mBar.startOpeningPanel(this);
Selim Cinek1685e632014-04-08 02:27:49 +0200558 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200559 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400560 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400561 if (DEBUG) logf("skipping expansion: is expanded");
562 }
563 }
564
565 public void cancelPeek() {
566 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
567 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400568 }
569 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500570
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200571 public void instantExpand() {
572 mInstantExpanding = true;
573 abortAnimations();
574 if (mTracking) {
575 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
576 onExpandingFinished();
577 }
578 setVisibility(VISIBLE);
579
580 // Wait for window manager to pickup the change, so we know the maximum height of the panel
581 // then.
582 getViewTreeObserver().addOnGlobalLayoutListener(
583 new ViewTreeObserver.OnGlobalLayoutListener() {
584 @Override
585 public void onGlobalLayout() {
586 if (mStatusBar.getStatusBarWindow().getHeight()
587 != mStatusBar.getStatusBarHeight()) {
588 getViewTreeObserver().removeOnGlobalLayoutListener(this);
589 setExpandedFraction(1f);
590 mInstantExpanding = false;
591 }
592 }
593 });
594
595 // Make sure a layout really happens.
596 requestLayout();
597 }
598
599 private void abortAnimations() {
600 cancelPeek();
601 if (mHeightAnimator != null) {
602 mHeightAnimator.cancel();
603 }
604 }
605
Jorim Jaggi90129582014-06-02 14:44:49 +0200606 protected void startUnlockHintAnimation() {
607
608 // We don't need to hint the user if an animation is already running or the user is changing
609 // the expansion.
610 if (mHeightAnimator != null || mTracking) {
611 return;
612 }
613 cancelPeek();
614 onExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200615 startUnlockHintAnimationPhase1(new Runnable() {
616 @Override
617 public void run() {
618 onExpandingFinished();
619 mStatusBar.onHintFinished();
620 mHintAnimationRunning = false;
621 }
622 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200623 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200624 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200625 }
626
627 /**
628 * Phase 1: Move everything upwards.
629 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200630 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200631 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
632 ValueAnimator animator = createHeightAnimator(target);
633 animator.setDuration(250);
634 animator.setInterpolator(mLinearOutSlowInInterpolator);
635 animator.addListener(new AnimatorListenerAdapter() {
636 private boolean mCancelled;
637
638 @Override
639 public void onAnimationCancel(Animator animation) {
640 mCancelled = true;
641 }
642
643 @Override
644 public void onAnimationEnd(Animator animation) {
645 if (mCancelled) {
646 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200647 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200648 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200649 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200650 }
651 }
652 });
653 animator.start();
654 mHeightAnimator = animator;
655 }
656
657 /**
658 * Phase 2: Bounce down.
659 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200660 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200661 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
662 animator.setDuration(450);
663 animator.setInterpolator(mBounceInterpolator);
664 animator.addListener(new AnimatorListenerAdapter() {
665 @Override
666 public void onAnimationEnd(Animator animation) {
667 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200668 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200669 }
670 });
671 animator.start();
672 mHeightAnimator = animator;
673 }
674
675 private ValueAnimator createHeightAnimator(float targetHeight) {
676 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
677 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
678 @Override
679 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200680 setExpandedHeightInternal((Float) animation.getAnimatedValue());
681 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Jorim Jaggi90129582014-06-02 14:44:49 +0200682 }
683 });
684 return animator;
685 }
686
687 /**
688 * Gets called when the user performs a click anywhere in the empty area of the panel.
689 *
690 * @return whether the panel will be expanded after the action performed by this method
691 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200692 private boolean onEmptySpaceClick(float x) {
693 if (mHintAnimationRunning) {
694 return true;
695 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200696 if (x < mEdgeTapAreaWidth
697 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200698 onEdgeClicked(false /* right */);
699 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200700 } else if (x > getWidth() - mEdgeTapAreaWidth
701 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200702 onEdgeClicked(true /* right */);
703 return true;
704 } else {
705 return onMiddleClicked();
706 }
707 }
708
709 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200710 switch (mStatusBar.getBarState()) {
711 case StatusBarState.KEYGUARD:
712 startUnlockHintAnimation();
713 return true;
714 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200715 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200716 return true;
717 case StatusBarState.SHADE:
718 collapse();
719 return false;
720 default:
721 return true;
722 }
723 }
724
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200725 protected abstract void onEdgeClicked(boolean right);
726
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500727 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200728 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400729 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500730 + "]",
731 this.getClass().getSimpleName(),
732 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100733 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500734 mClosing?"T":"f",
735 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500736 mJustPeeked?"T":"f",
737 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200738 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500739 ));
740 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200741
742 public abstract void resetViews();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400743}