blob: 4686933be6da3f1ea7d26be7634c6f0b8e449039 [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 Jaggi90129582014-06-02 14:44:49 +020030import android.view.animation.AnimationUtils;
31import android.view.animation.Interpolator;
Daniel Sandler08d05e32012-08-08 16:39:54 -040032import android.widget.FrameLayout;
33
34import com.android.systemui.R;
Jorim Jaggi1d480692014-05-20 19:41:58 +020035import com.android.systemui.statusbar.FlingAnimationUtils;
Jorim Jaggi90129582014-06-02 14:44:49 +020036import com.android.systemui.statusbar.StatusBarState;
Daniel Sandler08d05e32012-08-08 16:39:54 -040037
John Spurlockde84f0e2013-06-12 12:41:00 -040038import java.io.FileDescriptor;
39import java.io.PrintWriter;
John Spurlockde84f0e2013-06-12 12:41:00 -040040
Selim Cinek4c6969a2014-05-26 19:22:17 +020041public abstract class PanelView extends FrameLayout {
Daniel Sandler198a0302012-08-17 16:04:31 -040042 public static final boolean DEBUG = PanelBar.DEBUG;
Daniel Sandler08d05e32012-08-08 16:39:54 -040043 public static final String TAG = PanelView.class.getSimpleName();
Selim Cinek24120a52014-05-26 10:05:42 +020044 protected float mOverExpansion;
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;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010053 private float mInitialOffsetOnTouch;
Daniel Sandler08d05e32012-08-08 16:39:54 -040054 private float mExpandedFraction = 0;
55 private float mExpandedHeight = 0;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040056 private boolean mJustPeeked;
Daniel Sandler50508132012-08-16 14:10:53 -040057 private boolean mClosing;
Daniel Sandler50508132012-08-16 14:10:53 -040058 private boolean mTracking;
Jorim Jaggi90129582014-06-02 14:44:49 +020059 private boolean mTouchSlopExceeded;
John Spurlock48fa91a2013-08-15 09:29:31 -040060 private int mTrackingPointer;
Jorim Jaggid7daab72014-05-06 22:22:20 +020061 protected int mTouchSlop;
Daniel Sandler08d05e32012-08-08 16:39:54 -040062
Jorim Jaggi1d480692014-05-20 19:41:58 +020063 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040064 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020065 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020066 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040067
Daniel Sandler08d05e32012-08-08 16:39:54 -040068 PanelBar mBar;
69
Selim Cinekb84a1072014-05-15 19:10:18 +020070 protected int mMaxPanelHeight = -1;
Daniel Sandler50508132012-08-16 14:10:53 -040071 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020072 private float mInitialTouchY;
73 private float mInitialTouchX;
Daniel Sandler08d05e32012-08-08 16:39:54 -040074
Jorim Jaggi90129582014-06-02 14:44:49 +020075 private Interpolator mLinearOutSlowInInterpolator;
76 private Interpolator mBounceInterpolator;
77
Selim Cinek1685e632014-04-08 02:27:49 +020078 protected void onExpandingFinished() {
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +020079 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +020080 }
81
82 protected void onExpandingStarted() {
83 }
84
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040085 private void runPeekAnimation() {
John Spurlock97642182013-07-29 17:58:39 -040086 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +020087 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040088 return;
89 }
90 if (mPeekAnimator == null) {
John Spurlock209bede2013-07-17 12:23:27 -040091 mPeekAnimator = ObjectAnimator.ofFloat(this,
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040092 "expandedHeight", mPeekHeight)
Daniel Sandler3679bf52012-10-16 21:30:28 -040093 .setDuration(250);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040094 }
95 mPeekAnimator.start();
96 }
97
Daniel Sandler08d05e32012-08-08 16:39:54 -040098 public PanelView(Context context, AttributeSet attrs) {
99 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200100 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggi90129582014-06-02 14:44:49 +0200101 mLinearOutSlowInInterpolator =
102 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
103 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400104 }
105
Jorim Jaggi069cd032014-05-15 03:09:01 +0200106 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400107 final Resources res = getContext().getResources();
John Spurlock209bede2013-07-17 12:23:27 -0400108 mPeekHeight = res.getDimension(R.dimen.peek_height)
John Spurlock50728832014-04-17 19:05:28 -0400109 + getPaddingBottom(); // our window might have a dropshadow
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100110
111 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
112 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200113 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400114 }
115
116 private void trackMovement(MotionEvent event) {
117 // Add movement to velocity tracker using raw screen X and Y coordinates instead
118 // of window coordinates because the window frame may be moving at the same time.
119 float deltaX = event.getRawX() - event.getX();
120 float deltaY = event.getRawY() - event.getY();
121 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400122 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400123 event.offsetLocation(-deltaX, -deltaY);
124 }
125
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400126 @Override
127 public boolean onTouchEvent(MotionEvent event) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100128
129 /*
130 * We capture touch events here and update the expand height here in case according to
131 * the users fingers. This also handles multi-touch.
132 *
133 * If the user just clicks shortly, we give him a quick peek of the shade.
134 *
135 * Flinging is also enabled in order to open or close the shade.
136 */
137
138 int pointerIndex = event.findPointerIndex(mTrackingPointer);
139 if (pointerIndex < 0) {
140 pointerIndex = 0;
141 mTrackingPointer = event.getPointerId(pointerIndex);
142 }
143 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200144 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100145
Selim Cinek4c6969a2014-05-26 19:22:17 +0200146 boolean waitForTouchSlop = hasConflictingGestures();
147
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100148 switch (event.getActionMasked()) {
149 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100150
151 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200152 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200153 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200154 mTouchSlopExceeded = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200155 if (mVelocityTracker == null) {
156 initVelocityTracker();
157 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100158 trackMovement(event);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200159 if (!waitForTouchSlop || mHeightAnimator != null) {
160 if (mHeightAnimator != null) {
161 mHeightAnimator.cancel(); // end any outstanding animations
162 }
163 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200164 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100165 if (mExpandedHeight == 0) {
166 mJustPeeked = true;
167 runPeekAnimation();
168 }
169 break;
170
171 case MotionEvent.ACTION_POINTER_UP:
172 final int upPointer = event.getPointerId(event.getActionIndex());
173 if (mTrackingPointer == upPointer) {
174 // gesture is ongoing, find a new pointer to track
175 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
176 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200177 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100178 mTrackingPointer = event.getPointerId(newIndex);
179 mInitialOffsetOnTouch = mExpandedHeight;
180 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200181 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100182 }
183 break;
184
185 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200186 float h = y - mInitialTouchY;
Jorim Jaggi90129582014-06-02 14:44:49 +0200187 if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)) {
188 mTouchSlopExceeded = true;
189 if (waitForTouchSlop && !mTracking) {
190 mInitialOffsetOnTouch = mExpandedHeight;
191 mInitialTouchX = x;
192 mInitialTouchY = y;
193 if (mHeightAnimator != null) {
194 mHeightAnimator.cancel(); // end any outstanding animations
195 }
196 onTrackingStarted();
197 h = 0;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200198 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200199 }
200 final float newHeight = h + mInitialOffsetOnTouch;
201 if (newHeight > mPeekHeight) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100202 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
203 mPeekAnimator.cancel();
204 }
205 mJustPeeked = false;
206 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200207 if (!mJustPeeked && (!waitForTouchSlop || mTracking)) {
208 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100209 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
210 }
211
212 trackMovement(event);
213 break;
214
215 case MotionEvent.ACTION_UP:
216 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100217 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100218 trackMovement(event);
Jorim Jaggi90129582014-06-02 14:44:49 +0200219 if (mTracking && mTouchSlopExceeded) {
220 float vel = getCurrentVelocity();
221 boolean expand = flingExpands(vel);
222 onTrackingStopped(expand);
223 fling(vel, expand);
224 } else {
225 boolean expands = onEmptySpaceClick();
226 onTrackingStopped(expands);
227 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100228 if (mVelocityTracker != null) {
229 mVelocityTracker.recycle();
230 mVelocityTracker = null;
231 }
232 break;
233 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200234 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100235 }
236
Selim Cinek4c6969a2014-05-26 19:22:17 +0200237 protected abstract boolean hasConflictingGestures();
238
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200239 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200240 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200241 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200242 }
243
244 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200245 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200246 mBar.onTrackingStarted(PanelView.this);
247 onExpandingStarted();
248 }
249
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100250 private float getCurrentVelocity() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100251
252 // the velocitytracker might be null if we got a bad input stream
253 if (mVelocityTracker == null) {
254 return 0;
255 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100256 mVelocityTracker.computeCurrentVelocity(1000);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200257 return mVelocityTracker.getYVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100258 }
259
260 @Override
261 public boolean onInterceptTouchEvent(MotionEvent event) {
262
263 /*
264 * If the user drags anywhere inside the panel we intercept it if he moves his finger
265 * upwards. This allows closing the shade from anywhere inside the panel.
266 *
267 * We only do this if the current content is scrolled to the bottom,
268 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
269 * possible.
270 */
271 int pointerIndex = event.findPointerIndex(mTrackingPointer);
272 if (pointerIndex < 0) {
273 pointerIndex = 0;
274 mTrackingPointer = event.getPointerId(pointerIndex);
275 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200276 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100277 final float y = event.getY(pointerIndex);
278 boolean scrolledToBottom = isScrolledToBottom();
279
280 switch (event.getActionMasked()) {
281 case MotionEvent.ACTION_DOWN:
Jorim Jaggi1d480692014-05-20 19:41:58 +0200282 if (mHeightAnimator != null) {
283 mHeightAnimator.cancel(); // end any outstanding animations
Selim Cinek172e9142014-05-07 19:38:00 +0200284 return true;
285 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100286 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200287 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200288 mTouchSlopExceeded = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100289 initVelocityTracker();
290 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100291 break;
292 case MotionEvent.ACTION_POINTER_UP:
293 final int upPointer = event.getPointerId(event.getActionIndex());
294 if (mTrackingPointer == upPointer) {
295 // gesture is ongoing, find a new pointer to track
296 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
297 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200298 mInitialTouchX = event.getX(newIndex);
299 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100300 }
301 break;
302
303 case MotionEvent.ACTION_MOVE:
304 final float h = y - mInitialTouchY;
305 trackMovement(event);
306 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200307 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100308 mInitialOffsetOnTouch = mExpandedHeight;
309 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200310 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200311 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200312 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200313 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100314 return true;
315 }
316 }
317 break;
318 }
319 return false;
320 }
321
322 private void initVelocityTracker() {
323 if (mVelocityTracker != null) {
324 mVelocityTracker.recycle();
325 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200326 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100327 }
328
329 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200330 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100331 }
332
333 protected float getContentHeight() {
334 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400335 }
336
Daniel Sandler08d05e32012-08-08 16:39:54 -0400337 @Override
338 protected void onFinishInflate() {
339 super.onFinishInflate();
340 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400341 }
342
Jorim Jaggi069cd032014-05-15 03:09:01 +0200343 @Override
344 protected void onConfigurationChanged(Configuration newConfig) {
345 super.onConfigurationChanged(newConfig);
346 loadDimens();
347 mMaxPanelHeight = -1;
348 }
349
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200350 /**
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200351 * @param vel the current velocity of the motion
352 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200353 */
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200354 private boolean flingExpands(float vel) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200355 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200356 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200357 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200358 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400359 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200360 }
361
362 protected void fling(float vel, boolean expand) {
363 cancelPeek();
364 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200365 if (target == mExpandedHeight) {
366 onExpandingFinished();
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200367 mBar.panelExpansionChanged(this, mExpandedFraction);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200368 return;
369 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200370 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200371 if (expand) {
372 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
373 } else {
374 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
375 getHeight());
376
377 // Make it shorter if we run a canned animation
378 if (vel == 0) {
379 animator.setDuration((long) (animator.getDuration() / 1.75f));
380 }
381 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200382 animator.addListener(new AnimatorListenerAdapter() {
383 @Override
384 public void onAnimationEnd(Animator animation) {
385 mHeightAnimator = null;
386 onExpandingFinished();
387 }
388 });
389 animator.start();
390 mHeightAnimator = animator;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400391 }
392
393 @Override
394 protected void onAttachedToWindow() {
395 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400396 mViewName = getResources().getResourceName(getId());
397 }
398
399 public String getName() {
400 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400401 }
402
Daniel Sandler08d05e32012-08-08 16:39:54 -0400403 @Override
404 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
405 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
406
John Spurlock97642182013-07-29 17:58:39 -0400407 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
Daniel Sandler08d05e32012-08-08 16:39:54 -0400408 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
Daniel Sandler198a0302012-08-17 16:04:31 -0400409
410 // Did one of our children change size?
411 int newHeight = getMeasuredHeight();
Selim Cinekb84a1072014-05-15 19:10:18 +0200412 if (newHeight > mMaxPanelHeight) {
413 // we only adapt the max height if it's bigger
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100414 mMaxPanelHeight = newHeight;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200415 // If the user isn't actively poking us, let's rubberband to the content
Jorim Jaggi1d480692014-05-20 19:41:58 +0200416 if (!mTracking && mHeightAnimator == null
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200417 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
418 && mMaxPanelHeight > 0) {
419 mExpandedHeight = mMaxPanelHeight;
420 }
Daniel Sandler50508132012-08-16 14:10:53 -0400421 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400422 }
423
Daniel Sandler08d05e32012-08-08 16:39:54 -0400424 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400425 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400426 setExpandedHeightInternal(height);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400427 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400428 }
429
Daniel Sandler50508132012-08-16 14:10:53 -0400430 @Override
431 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100432 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
433 (int)mExpandedHeight, mMaxPanelHeight);
Daniel Sandler50508132012-08-16 14:10:53 -0400434 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100435 requestPanelHeightUpdate();
436 }
437
438 protected void requestPanelHeightUpdate() {
439 float currentMaxPanelHeight = getMaxPanelHeight();
440
441 // If the user isn't actively poking us, let's update the height
Jorim Jaggi1d480692014-05-20 19:41:58 +0200442 if (!mTracking && mHeightAnimator == null
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100443 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) {
444 setExpandedHeightInternal(currentMaxPanelHeight);
445 }
Daniel Sandler50508132012-08-16 14:10:53 -0400446 }
447
Daniel Sandler08d05e32012-08-08 16:39:54 -0400448 public void setExpandedHeightInternal(float h) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100449 float fh = getMaxPanelHeight();
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200450 mExpandedHeight = Math.max(0, Math.min(fh, h));
Selim Cinek24120a52014-05-26 10:05:42 +0200451 float overExpansion = h - fh;
452 overExpansion = Math.max(0, overExpansion);
453 if (overExpansion != mOverExpansion) {
454 onOverExpansionChanged(overExpansion);
455 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400456
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100457 if (DEBUG) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200458 logf("setExpansion: height=%.1f fh=%.1f tracking=%s", h, fh, mTracking ? "T" : "f");
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100459 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400460
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100461 onHeightUpdated(mExpandedHeight);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200462 mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : mExpandedHeight / fh);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400463 }
464
Selim Cinek24120a52014-05-26 10:05:42 +0200465 protected void onOverExpansionChanged(float overExpansion) {
466 mOverExpansion = overExpansion;
467 }
468
Jorim Jaggi90129582014-06-02 14:44:49 +0200469 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100470
471 /**
472 * This returns the maximum height of the panel. Children should override this if their
473 * desired height is not the full height.
474 *
475 * @return the default implementation simply returns the maximum height.
476 */
477 protected int getMaxPanelHeight() {
Selim Cinekb84a1072014-05-15 19:10:18 +0200478 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100479 return mMaxPanelHeight;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400480 }
481
482 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100483 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400484 }
485
486 public float getExpandedHeight() {
487 return mExpandedHeight;
488 }
489
490 public float getExpandedFraction() {
491 return mExpandedFraction;
492 }
493
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700494 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100495 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700496 }
497
498 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400499 return mExpandedHeight <= 0;
500 }
501
502 public boolean isCollapsing() {
503 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700504 }
505
John Spurlocka4b70af2013-08-17 14:05:49 -0400506 public boolean isTracking() {
507 return mTracking;
508 }
509
Daniel Sandler08d05e32012-08-08 16:39:54 -0400510 public void setBar(PanelBar panelBar) {
511 mBar = panelBar;
512 }
513
Daniel Sandler08d05e32012-08-08 16:39:54 -0400514 public void collapse() {
515 // TODO: abort animation or ongoing touch
John Spurlock97642182013-07-29 17:58:39 -0400516 if (DEBUG) logf("collapse: " + this);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700517 if (!isFullyCollapsed()) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200518 if (mHeightAnimator != null) {
519 mHeightAnimator.cancel();
520 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400521 mClosing = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200522 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200523 fling(0, false /* expand */);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400524 }
525 }
526
527 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400528 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400529 if (isFullyCollapsed()) {
530 mBar.startOpeningPanel(this);
Selim Cinek1685e632014-04-08 02:27:49 +0200531 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200532 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400533 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400534 if (DEBUG) logf("skipping expansion: is expanded");
535 }
536 }
537
538 public void cancelPeek() {
539 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
540 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400541 }
542 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500543
Jorim Jaggi90129582014-06-02 14:44:49 +0200544 protected void startUnlockHintAnimation() {
545
546 // We don't need to hint the user if an animation is already running or the user is changing
547 // the expansion.
548 if (mHeightAnimator != null || mTracking) {
549 return;
550 }
551 cancelPeek();
552 onExpandingStarted();
553 startUnlockHintAnimationPhase1();
554 mStatusBar.onUnlockHintStarted();
555 }
556
557 /**
558 * Phase 1: Move everything upwards.
559 */
560 private void startUnlockHintAnimationPhase1() {
561 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
562 ValueAnimator animator = createHeightAnimator(target);
563 animator.setDuration(250);
564 animator.setInterpolator(mLinearOutSlowInInterpolator);
565 animator.addListener(new AnimatorListenerAdapter() {
566 private boolean mCancelled;
567
568 @Override
569 public void onAnimationCancel(Animator animation) {
570 mCancelled = true;
571 }
572
573 @Override
574 public void onAnimationEnd(Animator animation) {
575 if (mCancelled) {
576 mHeightAnimator = null;
577 onExpandingFinished();
578 mStatusBar.onUnlockHintFinished();
579 } else {
580 startUnlockHintAnimationPhase2();
581 }
582 }
583 });
584 animator.start();
585 mHeightAnimator = animator;
586 }
587
588 /**
589 * Phase 2: Bounce down.
590 */
591 private void startUnlockHintAnimationPhase2() {
592 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
593 animator.setDuration(450);
594 animator.setInterpolator(mBounceInterpolator);
595 animator.addListener(new AnimatorListenerAdapter() {
596 @Override
597 public void onAnimationEnd(Animator animation) {
598 mHeightAnimator = null;
599 onExpandingFinished();
600 mStatusBar.onUnlockHintFinished();
601 }
602 });
603 animator.start();
604 mHeightAnimator = animator;
605 }
606
607 private ValueAnimator createHeightAnimator(float targetHeight) {
608 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
609 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
610 @Override
611 public void onAnimationUpdate(ValueAnimator animation) {
612 setExpandedHeight((Float) animation.getAnimatedValue());
613 }
614 });
615 return animator;
616 }
617
618 /**
619 * Gets called when the user performs a click anywhere in the empty area of the panel.
620 *
621 * @return whether the panel will be expanded after the action performed by this method
622 */
623 private boolean onEmptySpaceClick() {
624 switch (mStatusBar.getBarState()) {
625 case StatusBarState.KEYGUARD:
626 startUnlockHintAnimation();
627 return true;
628 case StatusBarState.SHADE_LOCKED:
629 // TODO: Go to Keyguard again.
630 return true;
631 case StatusBarState.SHADE:
632 collapse();
633 return false;
634 default:
635 return true;
636 }
637 }
638
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500639 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200640 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400641 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500642 + "]",
643 this.getClass().getSimpleName(),
644 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100645 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500646 mClosing?"T":"f",
647 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500648 mJustPeeked?"T":"f",
649 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200650 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500651 ));
652 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200653
654 public abstract void resetViews();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400655}