blob: 1e45ce370708d8be858823385dfd4d080a68ec78 [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;
Selim Cinekf99d0002014-06-13 07:36:01 +020065 private float mOriginalIndicationY;
Daniel Sandler08d05e32012-08-08 16:39:54 -040066
Jorim Jaggi1d480692014-05-20 19:41:58 +020067 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040068 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020069 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020070 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040071
Jorim Jaggi0a27be82014-06-11 03:22:39 +020072 /**
73 * Whether an instant expand request is currently pending and we are just waiting for layout.
74 */
75 private boolean mInstantExpanding;
76
Daniel Sandler08d05e32012-08-08 16:39:54 -040077 PanelBar mBar;
78
Selim Cinekb84a1072014-05-15 19:10:18 +020079 protected int mMaxPanelHeight = -1;
Daniel Sandler50508132012-08-16 14:10:53 -040080 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020081 private float mInitialTouchY;
82 private float mInitialTouchX;
Daniel Sandler08d05e32012-08-08 16:39:54 -040083
Jorim Jaggi90129582014-06-02 14:44:49 +020084 private Interpolator mLinearOutSlowInInterpolator;
85 private Interpolator mBounceInterpolator;
Selim Cinekf99d0002014-06-13 07:36:01 +020086 protected KeyguardBottomAreaView mKeyguardBottomArea;
Jorim Jaggi90129582014-06-02 14:44:49 +020087
Selim Cinek1685e632014-04-08 02:27:49 +020088 protected void onExpandingFinished() {
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +020089 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +020090 }
91
92 protected void onExpandingStarted() {
93 }
94
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040095 private void runPeekAnimation() {
Jorim Jaggi2580a9762014-06-25 03:08:25 +020096 mPeekHeight = getPeekHeight();
John Spurlock97642182013-07-29 17:58:39 -040097 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +020098 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040099 return;
100 }
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200101 mPeekAnimator = ObjectAnimator.ofFloat(this,
102 "expandedHeight", mPeekHeight)
103 .setDuration(250);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400104 mPeekAnimator.start();
105 }
106
Daniel Sandler08d05e32012-08-08 16:39:54 -0400107 public PanelView(Context context, AttributeSet attrs) {
108 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200109 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggi90129582014-06-02 14:44:49 +0200110 mLinearOutSlowInInterpolator =
111 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
112 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400113 }
114
Jorim Jaggi069cd032014-05-15 03:09:01 +0200115 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400116 final Resources res = getContext().getResources();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100117 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
118 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200119 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200120 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400121 }
122
123 private void trackMovement(MotionEvent event) {
124 // Add movement to velocity tracker using raw screen X and Y coordinates instead
125 // of window coordinates because the window frame may be moving at the same time.
126 float deltaX = event.getRawX() - event.getX();
127 float deltaY = event.getRawY() - event.getY();
128 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400129 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400130 event.offsetLocation(-deltaX, -deltaY);
131 }
132
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400133 @Override
134 public boolean onTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200135 if (mInstantExpanding) {
136 return false;
137 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100138
139 /*
140 * We capture touch events here and update the expand height here in case according to
141 * the users fingers. This also handles multi-touch.
142 *
143 * If the user just clicks shortly, we give him a quick peek of the shade.
144 *
145 * Flinging is also enabled in order to open or close the shade.
146 */
147
148 int pointerIndex = event.findPointerIndex(mTrackingPointer);
149 if (pointerIndex < 0) {
150 pointerIndex = 0;
151 mTrackingPointer = event.getPointerId(pointerIndex);
152 }
153 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200154 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100155
Selim Cinek4c6969a2014-05-26 19:22:17 +0200156 boolean waitForTouchSlop = hasConflictingGestures();
157
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100158 switch (event.getActionMasked()) {
159 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100160 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200161 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200162 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200163 mTouchSlopExceeded = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200164 if (mVelocityTracker == null) {
165 initVelocityTracker();
166 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100167 trackMovement(event);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200168 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning)) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200169 if (mHeightAnimator != null) {
170 mHeightAnimator.cancel(); // end any outstanding animations
171 }
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200172 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200173 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200174 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100175 if (mExpandedHeight == 0) {
176 mJustPeeked = true;
177 runPeekAnimation();
178 }
179 break;
180
181 case MotionEvent.ACTION_POINTER_UP:
182 final int upPointer = event.getPointerId(event.getActionIndex());
183 if (mTrackingPointer == upPointer) {
184 // gesture is ongoing, find a new pointer to track
185 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
186 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200187 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100188 mTrackingPointer = event.getPointerId(newIndex);
189 mInitialOffsetOnTouch = mExpandedHeight;
190 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200191 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100192 }
193 break;
194
195 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200196 float h = y - mInitialTouchY;
Jorim Jaggi90129582014-06-02 14:44:49 +0200197 if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)) {
198 mTouchSlopExceeded = true;
199 if (waitForTouchSlop && !mTracking) {
200 mInitialOffsetOnTouch = mExpandedHeight;
201 mInitialTouchX = x;
202 mInitialTouchY = y;
203 if (mHeightAnimator != null) {
204 mHeightAnimator.cancel(); // end any outstanding animations
205 }
206 onTrackingStarted();
207 h = 0;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200208 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200209 }
210 final float newHeight = h + mInitialOffsetOnTouch;
211 if (newHeight > mPeekHeight) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100212 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
213 mPeekAnimator.cancel();
214 }
215 mJustPeeked = false;
216 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200217 if (!mJustPeeked && (!waitForTouchSlop || mTracking)) {
Jorim Jaggicc693242014-06-14 03:04:35 +0000218 setExpandedHeightInternal(newHeight);
219 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100220 }
221
222 trackMovement(event);
223 break;
224
225 case MotionEvent.ACTION_UP:
226 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100227 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100228 trackMovement(event);
Jorim Jaggi787a0af2014-06-04 18:57:14 +0200229 if ((mTracking && mTouchSlopExceeded)
230 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200231 float vel = getCurrentVelocity();
232 boolean expand = flingExpands(vel);
233 onTrackingStopped(expand);
234 fling(vel, expand);
235 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200236 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200237 onTrackingStopped(expands);
238 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100239 if (mVelocityTracker != null) {
240 mVelocityTracker.recycle();
241 mVelocityTracker = null;
242 }
243 break;
244 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200245 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100246 }
247
Selim Cinek4c6969a2014-05-26 19:22:17 +0200248 protected abstract boolean hasConflictingGestures();
249
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200250 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200251 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200252 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200253 }
254
255 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200256 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200257 mBar.onTrackingStarted(PanelView.this);
258 onExpandingStarted();
259 }
260
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100261 private float getCurrentVelocity() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100262
263 // the velocitytracker might be null if we got a bad input stream
264 if (mVelocityTracker == null) {
265 return 0;
266 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100267 mVelocityTracker.computeCurrentVelocity(1000);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200268 return mVelocityTracker.getYVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100269 }
270
271 @Override
272 public boolean onInterceptTouchEvent(MotionEvent event) {
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200273 if (mInstantExpanding) {
274 return false;
275 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100276
277 /*
278 * If the user drags anywhere inside the panel we intercept it if he moves his finger
279 * upwards. This allows closing the shade from anywhere inside the panel.
280 *
281 * We only do this if the current content is scrolled to the bottom,
282 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
283 * possible.
284 */
285 int pointerIndex = event.findPointerIndex(mTrackingPointer);
286 if (pointerIndex < 0) {
287 pointerIndex = 0;
288 mTrackingPointer = event.getPointerId(pointerIndex);
289 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200290 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100291 final float y = event.getY(pointerIndex);
292 boolean scrolledToBottom = isScrolledToBottom();
293
294 switch (event.getActionMasked()) {
295 case MotionEvent.ACTION_DOWN:
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200296 if (mHeightAnimator != null && !mHintAnimationRunning) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200297 mHeightAnimator.cancel(); // end any outstanding animations
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200298 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200299 return true;
300 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100301 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200302 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200303 mTouchSlopExceeded = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100304 initVelocityTracker();
305 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100306 break;
307 case MotionEvent.ACTION_POINTER_UP:
308 final int upPointer = event.getPointerId(event.getActionIndex());
309 if (mTrackingPointer == upPointer) {
310 // gesture is ongoing, find a new pointer to track
311 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
312 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200313 mInitialTouchX = event.getX(newIndex);
314 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100315 }
316 break;
317
318 case MotionEvent.ACTION_MOVE:
319 final float h = y - mInitialTouchY;
320 trackMovement(event);
321 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200322 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200323 if (mHeightAnimator != null) {
324 mHeightAnimator.cancel();
325 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100326 mInitialOffsetOnTouch = mExpandedHeight;
327 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200328 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200329 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200330 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200331 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100332 return true;
333 }
334 }
335 break;
336 }
337 return false;
338 }
339
340 private void initVelocityTracker() {
341 if (mVelocityTracker != null) {
342 mVelocityTracker.recycle();
343 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200344 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100345 }
346
347 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200348 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100349 }
350
351 protected float getContentHeight() {
352 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400353 }
354
Daniel Sandler08d05e32012-08-08 16:39:54 -0400355 @Override
356 protected void onFinishInflate() {
357 super.onFinishInflate();
358 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400359 }
360
Jorim Jaggi069cd032014-05-15 03:09:01 +0200361 @Override
362 protected void onConfigurationChanged(Configuration newConfig) {
363 super.onConfigurationChanged(newConfig);
364 loadDimens();
365 mMaxPanelHeight = -1;
366 }
367
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200368 /**
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200369 * @param vel the current velocity of the motion
370 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200371 */
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200372 private boolean flingExpands(float vel) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200373 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200374 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200375 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200376 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400377 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200378 }
379
380 protected void fling(float vel, boolean expand) {
381 cancelPeek();
382 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200383 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200384 onExpandingFinished();
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200385 mBar.panelExpansionChanged(this, mExpandedFraction);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200386 return;
387 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200388 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200389 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200390 if (expand) {
391 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
392 } else {
393 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
394 getHeight());
395
396 // Make it shorter if we run a canned animation
397 if (vel == 0) {
398 animator.setDuration((long) (animator.getDuration() / 1.75f));
399 }
400 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200401 animator.addListener(new AnimatorListenerAdapter() {
402 @Override
403 public void onAnimationEnd(Animator animation) {
404 mHeightAnimator = null;
405 onExpandingFinished();
406 }
407 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200408 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200409 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400410 }
411
412 @Override
413 protected void onAttachedToWindow() {
414 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400415 mViewName = getResources().getResourceName(getId());
416 }
417
418 public String getName() {
419 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400420 }
421
Daniel Sandler08d05e32012-08-08 16:39:54 -0400422 @Override
423 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
424 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
425
John Spurlock97642182013-07-29 17:58:39 -0400426 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
Daniel Sandler08d05e32012-08-08 16:39:54 -0400427 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
Daniel Sandler198a0302012-08-17 16:04:31 -0400428
429 // Did one of our children change size?
430 int newHeight = getMeasuredHeight();
Selim Cinekb84a1072014-05-15 19:10:18 +0200431 if (newHeight > mMaxPanelHeight) {
432 // we only adapt the max height if it's bigger
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100433 mMaxPanelHeight = newHeight;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200434 // If the user isn't actively poking us, let's rubberband to the content
Jorim Jaggi1d480692014-05-20 19:41:58 +0200435 if (!mTracking && mHeightAnimator == null
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200436 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
437 && mMaxPanelHeight > 0) {
438 mExpandedHeight = mMaxPanelHeight;
439 }
Daniel Sandler50508132012-08-16 14:10:53 -0400440 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400441 }
442
Daniel Sandler08d05e32012-08-08 16:39:54 -0400443 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400444 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200445 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400446 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400447 }
448
Daniel Sandler50508132012-08-16 14:10:53 -0400449 @Override
450 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100451 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
452 (int)mExpandedHeight, mMaxPanelHeight);
Daniel Sandler50508132012-08-16 14:10:53 -0400453 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100454 requestPanelHeightUpdate();
455 }
456
457 protected void requestPanelHeightUpdate() {
458 float currentMaxPanelHeight = getMaxPanelHeight();
459
460 // If the user isn't actively poking us, let's update the height
Jorim Jaggi1d480692014-05-20 19:41:58 +0200461 if (!mTracking && mHeightAnimator == null
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100462 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200463 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100464 }
Daniel Sandler50508132012-08-16 14:10:53 -0400465 }
466
Daniel Sandler08d05e32012-08-08 16:39:54 -0400467 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200468 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
469 if (mHeightAnimator == null) {
470 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
471 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
472 setOverExpansion(overExpansionPixels, true /* isPixels */);
473 }
474 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
475 } else {
476 mExpandedHeight = h;
477 if (mOverExpandedBeforeFling) {
478 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
479 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100480 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400481
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100482 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200483 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
484 ? 0
485 : mExpandedHeight / fhWithoutOverExpansion);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400486 }
487
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200488 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200489
Jorim Jaggi90129582014-06-02 14:44:49 +0200490 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100491
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200492 protected abstract float getOverExpansionAmount();
493
494 protected abstract float getOverExpansionPixels();
495
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100496 /**
497 * This returns the maximum height of the panel. Children should override this if their
498 * desired height is not the full height.
499 *
500 * @return the default implementation simply returns the maximum height.
501 */
502 protected int getMaxPanelHeight() {
Selim Cinekb84a1072014-05-15 19:10:18 +0200503 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100504 return mMaxPanelHeight;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400505 }
506
507 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100508 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400509 }
510
511 public float getExpandedHeight() {
512 return mExpandedHeight;
513 }
514
515 public float getExpandedFraction() {
516 return mExpandedFraction;
517 }
518
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700519 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100520 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700521 }
522
523 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400524 return mExpandedHeight <= 0;
525 }
526
527 public boolean isCollapsing() {
528 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700529 }
530
John Spurlocka4b70af2013-08-17 14:05:49 -0400531 public boolean isTracking() {
532 return mTracking;
533 }
534
Daniel Sandler08d05e32012-08-08 16:39:54 -0400535 public void setBar(PanelBar panelBar) {
536 mBar = panelBar;
537 }
538
Daniel Sandler08d05e32012-08-08 16:39:54 -0400539 public void collapse() {
540 // TODO: abort animation or ongoing touch
John Spurlock97642182013-07-29 17:58:39 -0400541 if (DEBUG) logf("collapse: " + this);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700542 if (!isFullyCollapsed()) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200543 if (mHeightAnimator != null) {
544 mHeightAnimator.cancel();
545 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400546 mClosing = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200547 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200548 fling(0, false /* expand */);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400549 }
550 }
551
552 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400553 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400554 if (isFullyCollapsed()) {
555 mBar.startOpeningPanel(this);
Selim Cinek1685e632014-04-08 02:27:49 +0200556 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200557 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400558 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400559 if (DEBUG) logf("skipping expansion: is expanded");
560 }
561 }
562
563 public void cancelPeek() {
564 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
565 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400566 }
567 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500568
Jorim Jaggi0a27be82014-06-11 03:22:39 +0200569 public void instantExpand() {
570 mInstantExpanding = true;
571 abortAnimations();
572 if (mTracking) {
573 onTrackingStopped(true /* expands */); // The panel is expanded after this call.
574 onExpandingFinished();
575 }
576 setVisibility(VISIBLE);
577
578 // Wait for window manager to pickup the change, so we know the maximum height of the panel
579 // then.
580 getViewTreeObserver().addOnGlobalLayoutListener(
581 new ViewTreeObserver.OnGlobalLayoutListener() {
582 @Override
583 public void onGlobalLayout() {
584 if (mStatusBar.getStatusBarWindow().getHeight()
585 != mStatusBar.getStatusBarHeight()) {
586 getViewTreeObserver().removeOnGlobalLayoutListener(this);
587 setExpandedFraction(1f);
588 mInstantExpanding = false;
589 }
590 }
591 });
592
593 // Make sure a layout really happens.
594 requestLayout();
595 }
596
597 private void abortAnimations() {
598 cancelPeek();
599 if (mHeightAnimator != null) {
600 mHeightAnimator.cancel();
601 }
602 }
603
Jorim Jaggi90129582014-06-02 14:44:49 +0200604 protected void startUnlockHintAnimation() {
605
606 // We don't need to hint the user if an animation is already running or the user is changing
607 // the expansion.
608 if (mHeightAnimator != null || mTracking) {
609 return;
610 }
611 cancelPeek();
612 onExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200613 startUnlockHintAnimationPhase1(new Runnable() {
614 @Override
615 public void run() {
616 onExpandingFinished();
617 mStatusBar.onHintFinished();
618 mHintAnimationRunning = false;
619 }
620 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200621 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200622 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200623 }
624
625 /**
626 * Phase 1: Move everything upwards.
627 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200628 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200629 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
630 ValueAnimator animator = createHeightAnimator(target);
631 animator.setDuration(250);
632 animator.setInterpolator(mLinearOutSlowInInterpolator);
633 animator.addListener(new AnimatorListenerAdapter() {
634 private boolean mCancelled;
635
636 @Override
637 public void onAnimationCancel(Animator animation) {
638 mCancelled = true;
639 }
640
641 @Override
642 public void onAnimationEnd(Animator animation) {
643 if (mCancelled) {
644 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200645 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200646 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200647 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200648 }
649 }
650 });
651 animator.start();
652 mHeightAnimator = animator;
Selim Cinekf99d0002014-06-13 07:36:01 +0200653 mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY();
654 mKeyguardBottomArea.getIndicationView().animate()
655 .y(mOriginalIndicationY - mHintDistance)
656 .setDuration(250)
657 .setInterpolator(mLinearOutSlowInInterpolator)
658 .withEndAction(new Runnable() {
659 @Override
660 public void run() {
661 mKeyguardBottomArea.getIndicationView().animate()
662 .y(mOriginalIndicationY)
663 .setDuration(450)
664 .setInterpolator(mBounceInterpolator)
665 .start();
666 }
667 })
668 .start();
Jorim Jaggi90129582014-06-02 14:44:49 +0200669 }
670
671 /**
672 * Phase 2: Bounce down.
673 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200674 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200675 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
676 animator.setDuration(450);
677 animator.setInterpolator(mBounceInterpolator);
678 animator.addListener(new AnimatorListenerAdapter() {
679 @Override
680 public void onAnimationEnd(Animator animation) {
681 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200682 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200683 }
684 });
685 animator.start();
686 mHeightAnimator = animator;
687 }
688
689 private ValueAnimator createHeightAnimator(float targetHeight) {
690 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
691 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
692 @Override
693 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200694 setExpandedHeightInternal((Float) animation.getAnimatedValue());
695 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Jorim Jaggi90129582014-06-02 14:44:49 +0200696 }
697 });
698 return animator;
699 }
700
701 /**
702 * Gets called when the user performs a click anywhere in the empty area of the panel.
703 *
704 * @return whether the panel will be expanded after the action performed by this method
705 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200706 private boolean onEmptySpaceClick(float x) {
707 if (mHintAnimationRunning) {
708 return true;
709 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200710 if (x < mEdgeTapAreaWidth
711 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200712 onEdgeClicked(false /* right */);
713 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200714 } else if (x > getWidth() - mEdgeTapAreaWidth
715 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200716 onEdgeClicked(true /* right */);
717 return true;
718 } else {
719 return onMiddleClicked();
720 }
721 }
722
723 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200724 switch (mStatusBar.getBarState()) {
725 case StatusBarState.KEYGUARD:
726 startUnlockHintAnimation();
727 return true;
728 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200729 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200730 return true;
731 case StatusBarState.SHADE:
732 collapse();
733 return false;
734 default:
735 return true;
736 }
737 }
738
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200739 protected abstract void onEdgeClicked(boolean right);
740
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500741 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200742 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400743 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500744 + "]",
745 this.getClass().getSimpleName(),
746 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100747 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500748 mClosing?"T":"f",
749 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500750 mJustPeeked?"T":"f",
751 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200752 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500753 ));
754 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200755
756 public abstract void resetViews();
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200757
758 protected abstract float getPeekHeight();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400759}