blob: f6db8a82b0bf4d77ace9059be6da1bd7c7618dd0 [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;
Daniel Sandler08d05e32012-08-08 16:39:54 -040030import android.widget.FrameLayout;
31
32import com.android.systemui.R;
Jorim Jaggi1d480692014-05-20 19:41:58 +020033import com.android.systemui.statusbar.FlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040034
John Spurlockde84f0e2013-06-12 12:41:00 -040035import java.io.FileDescriptor;
36import java.io.PrintWriter;
John Spurlockde84f0e2013-06-12 12:41:00 -040037
Selim Cinek4c6969a2014-05-26 19:22:17 +020038public abstract class PanelView extends FrameLayout {
Daniel Sandler198a0302012-08-17 16:04:31 -040039 public static final boolean DEBUG = PanelBar.DEBUG;
Daniel Sandler08d05e32012-08-08 16:39:54 -040040 public static final String TAG = PanelView.class.getSimpleName();
Selim Cinek24120a52014-05-26 10:05:42 +020041 protected float mOverExpansion;
Daniel Sandlere7c5bbb2013-03-05 13:36:21 -050042
John Spurlock97642182013-07-29 17:58:39 -040043 private final void logf(String fmt, Object... args) {
John Spurlockcd686b52013-06-05 10:13:46 -040044 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args));
Daniel Sandler08d05e32012-08-08 16:39:54 -040045 }
46
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040047 private float mPeekHeight;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010048 private float mInitialOffsetOnTouch;
Daniel Sandler08d05e32012-08-08 16:39:54 -040049 private float mExpandedFraction = 0;
50 private float mExpandedHeight = 0;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040051 private boolean mJustPeeked;
Daniel Sandler50508132012-08-16 14:10:53 -040052 private boolean mClosing;
Daniel Sandler50508132012-08-16 14:10:53 -040053 private boolean mTracking;
John Spurlock48fa91a2013-08-15 09:29:31 -040054 private int mTrackingPointer;
Jorim Jaggid7daab72014-05-06 22:22:20 +020055 protected int mTouchSlop;
Daniel Sandler08d05e32012-08-08 16:39:54 -040056
Jorim Jaggi1d480692014-05-20 19:41:58 +020057 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040058 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020059 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020060 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040061
Daniel Sandler08d05e32012-08-08 16:39:54 -040062 PanelBar mBar;
63
Selim Cinekb84a1072014-05-15 19:10:18 +020064 protected int mMaxPanelHeight = -1;
Daniel Sandler50508132012-08-16 14:10:53 -040065 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020066 private float mInitialTouchY;
67 private float mInitialTouchX;
Daniel Sandler08d05e32012-08-08 16:39:54 -040068
Selim Cinek1685e632014-04-08 02:27:49 +020069 protected void onExpandingFinished() {
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +020070 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +020071 }
72
73 protected void onExpandingStarted() {
74 }
75
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040076 private void runPeekAnimation() {
John Spurlock97642182013-07-29 17:58:39 -040077 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +020078 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040079 return;
80 }
81 if (mPeekAnimator == null) {
John Spurlock209bede2013-07-17 12:23:27 -040082 mPeekAnimator = ObjectAnimator.ofFloat(this,
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040083 "expandedHeight", mPeekHeight)
Daniel Sandler3679bf52012-10-16 21:30:28 -040084 .setDuration(250);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040085 }
86 mPeekAnimator.start();
87 }
88
Daniel Sandler08d05e32012-08-08 16:39:54 -040089 public PanelView(Context context, AttributeSet attrs) {
90 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +020091 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Daniel Sandler08d05e32012-08-08 16:39:54 -040092 }
93
Jorim Jaggi069cd032014-05-15 03:09:01 +020094 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -040095 final Resources res = getContext().getResources();
John Spurlock209bede2013-07-17 12:23:27 -040096 mPeekHeight = res.getDimension(R.dimen.peek_height)
John Spurlock50728832014-04-17 19:05:28 -040097 + getPaddingBottom(); // our window might have a dropshadow
Selim Cinekb6d85eb2014-03-28 20:21:01 +010098
99 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
100 mTouchSlop = configuration.getScaledTouchSlop();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400101 }
102
103 private void trackMovement(MotionEvent event) {
104 // Add movement to velocity tracker using raw screen X and Y coordinates instead
105 // of window coordinates because the window frame may be moving at the same time.
106 float deltaX = event.getRawX() - event.getX();
107 float deltaY = event.getRawY() - event.getY();
108 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400109 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400110 event.offsetLocation(-deltaX, -deltaY);
111 }
112
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400113 @Override
114 public boolean onTouchEvent(MotionEvent event) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100115
116 /*
117 * We capture touch events here and update the expand height here in case according to
118 * the users fingers. This also handles multi-touch.
119 *
120 * If the user just clicks shortly, we give him a quick peek of the shade.
121 *
122 * Flinging is also enabled in order to open or close the shade.
123 */
124
125 int pointerIndex = event.findPointerIndex(mTrackingPointer);
126 if (pointerIndex < 0) {
127 pointerIndex = 0;
128 mTrackingPointer = event.getPointerId(pointerIndex);
129 }
130 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200131 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100132
Selim Cinek4c6969a2014-05-26 19:22:17 +0200133 boolean waitForTouchSlop = hasConflictingGestures();
134
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100135 switch (event.getActionMasked()) {
136 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100137
138 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200139 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200140 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200141 if (mVelocityTracker == null) {
142 initVelocityTracker();
143 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100144 trackMovement(event);
Selim Cinek4c6969a2014-05-26 19:22:17 +0200145 if (!waitForTouchSlop || mHeightAnimator != null) {
146 if (mHeightAnimator != null) {
147 mHeightAnimator.cancel(); // end any outstanding animations
148 }
149 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200150 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100151 if (mExpandedHeight == 0) {
152 mJustPeeked = true;
153 runPeekAnimation();
154 }
155 break;
156
157 case MotionEvent.ACTION_POINTER_UP:
158 final int upPointer = event.getPointerId(event.getActionIndex());
159 if (mTrackingPointer == upPointer) {
160 // gesture is ongoing, find a new pointer to track
161 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
162 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200163 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100164 mTrackingPointer = event.getPointerId(newIndex);
165 mInitialOffsetOnTouch = mExpandedHeight;
166 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200167 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100168 }
169 break;
170
171 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200172 float h = y - mInitialTouchY;
173 if (waitForTouchSlop && !mTracking && Math.abs(h) > mTouchSlop
174 && Math.abs(h) > Math.abs(x - mInitialTouchX)) {
175 mInitialOffsetOnTouch = mExpandedHeight;
176 mInitialTouchX = x;
177 mInitialTouchY = y;
178 if (mHeightAnimator != null) {
179 mHeightAnimator.cancel(); // end any outstanding animations
180 }
181 onTrackingStarted();
182 h = 0;
183 }
184 final float newHeight = h + mInitialOffsetOnTouch;
185 if (newHeight > mPeekHeight) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100186 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
187 mPeekAnimator.cancel();
188 }
189 mJustPeeked = false;
190 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200191 if (!mJustPeeked && (!waitForTouchSlop || mTracking)) {
192 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100193 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
194 }
195
196 trackMovement(event);
197 break;
198
199 case MotionEvent.ACTION_UP:
200 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100201 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100202 trackMovement(event);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200203 boolean expand = flingWithCurrentVelocity();
204 onTrackingStopped(expand);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100205 if (mVelocityTracker != null) {
206 mVelocityTracker.recycle();
207 mVelocityTracker = null;
208 }
209 break;
210 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200211 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100212 }
213
Selim Cinek4c6969a2014-05-26 19:22:17 +0200214 protected abstract boolean hasConflictingGestures();
215
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200216 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200217 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200218 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200219 }
220
221 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200222 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200223 mBar.onTrackingStarted(PanelView.this);
224 onExpandingStarted();
225 }
226
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100227 private float getCurrentVelocity() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100228
229 // the velocitytracker might be null if we got a bad input stream
230 if (mVelocityTracker == null) {
231 return 0;
232 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100233 mVelocityTracker.computeCurrentVelocity(1000);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200234 return mVelocityTracker.getYVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100235 }
236
237 @Override
238 public boolean onInterceptTouchEvent(MotionEvent event) {
239
240 /*
241 * If the user drags anywhere inside the panel we intercept it if he moves his finger
242 * upwards. This allows closing the shade from anywhere inside the panel.
243 *
244 * We only do this if the current content is scrolled to the bottom,
245 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
246 * possible.
247 */
248 int pointerIndex = event.findPointerIndex(mTrackingPointer);
249 if (pointerIndex < 0) {
250 pointerIndex = 0;
251 mTrackingPointer = event.getPointerId(pointerIndex);
252 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200253 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100254 final float y = event.getY(pointerIndex);
255 boolean scrolledToBottom = isScrolledToBottom();
256
257 switch (event.getActionMasked()) {
258 case MotionEvent.ACTION_DOWN:
Jorim Jaggi1d480692014-05-20 19:41:58 +0200259 if (mHeightAnimator != null) {
260 mHeightAnimator.cancel(); // end any outstanding animations
Selim Cinek172e9142014-05-07 19:38:00 +0200261 return true;
262 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100263 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200264 mInitialTouchX = x;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100265 initVelocityTracker();
266 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100267 break;
268 case MotionEvent.ACTION_POINTER_UP:
269 final int upPointer = event.getPointerId(event.getActionIndex());
270 if (mTrackingPointer == upPointer) {
271 // gesture is ongoing, find a new pointer to track
272 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
273 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200274 mInitialTouchX = event.getX(newIndex);
275 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100276 }
277 break;
278
279 case MotionEvent.ACTION_MOVE:
280 final float h = y - mInitialTouchY;
281 trackMovement(event);
282 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200283 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100284 mInitialOffsetOnTouch = mExpandedHeight;
285 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200286 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200287 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200288 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100289 return true;
290 }
291 }
292 break;
293 }
294 return false;
295 }
296
297 private void initVelocityTracker() {
298 if (mVelocityTracker != null) {
299 mVelocityTracker.recycle();
300 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200301 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100302 }
303
304 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200305 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100306 }
307
308 protected float getContentHeight() {
309 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400310 }
311
Daniel Sandler08d05e32012-08-08 16:39:54 -0400312 @Override
313 protected void onFinishInflate() {
314 super.onFinishInflate();
315 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400316 }
317
Jorim Jaggi069cd032014-05-15 03:09:01 +0200318 @Override
319 protected void onConfigurationChanged(Configuration newConfig) {
320 super.onConfigurationChanged(newConfig);
321 loadDimens();
322 mMaxPanelHeight = -1;
323 }
324
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200325 /**
326 * @return whether the panel will be expanded after the animation
327 */
328 private boolean flingWithCurrentVelocity() {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200329 float vel = getCurrentVelocity();
330 boolean expand;
331 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
332 expand = getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200333 } else {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200334 expand = vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400335 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200336 fling(vel, expand);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200337 return expand;
Jorim Jaggi1d480692014-05-20 19:41:58 +0200338 }
339
340 protected void fling(float vel, boolean expand) {
341 cancelPeek();
342 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200343 if (target == mExpandedHeight) {
344 onExpandingFinished();
345 return;
346 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200347 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, target);
348 if (expand) {
349 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
350 } else {
351 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
352 getHeight());
353
354 // Make it shorter if we run a canned animation
355 if (vel == 0) {
356 animator.setDuration((long) (animator.getDuration() / 1.75f));
357 }
358 }
359 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
360 @Override
361 public void onAnimationUpdate(ValueAnimator animation) {
362 setExpandedHeight((Float) animation.getAnimatedValue());
363 }
364 });
365 animator.addListener(new AnimatorListenerAdapter() {
366 @Override
367 public void onAnimationEnd(Animator animation) {
368 mHeightAnimator = null;
369 onExpandingFinished();
370 }
371 });
372 animator.start();
373 mHeightAnimator = animator;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400374 }
375
376 @Override
377 protected void onAttachedToWindow() {
378 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400379 mViewName = getResources().getResourceName(getId());
380 }
381
382 public String getName() {
383 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400384 }
385
Daniel Sandler08d05e32012-08-08 16:39:54 -0400386 @Override
387 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
388 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
389
John Spurlock97642182013-07-29 17:58:39 -0400390 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
Daniel Sandler08d05e32012-08-08 16:39:54 -0400391 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
Daniel Sandler198a0302012-08-17 16:04:31 -0400392
393 // Did one of our children change size?
394 int newHeight = getMeasuredHeight();
Selim Cinekb84a1072014-05-15 19:10:18 +0200395 if (newHeight > mMaxPanelHeight) {
396 // we only adapt the max height if it's bigger
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100397 mMaxPanelHeight = newHeight;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200398 // If the user isn't actively poking us, let's rubberband to the content
Jorim Jaggi1d480692014-05-20 19:41:58 +0200399 if (!mTracking && mHeightAnimator == null
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200400 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
401 && mMaxPanelHeight > 0) {
402 mExpandedHeight = mMaxPanelHeight;
403 }
Daniel Sandler50508132012-08-16 14:10:53 -0400404 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400405 }
406
Daniel Sandler08d05e32012-08-08 16:39:54 -0400407 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400408 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400409 setExpandedHeightInternal(height);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400410 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400411 }
412
Daniel Sandler50508132012-08-16 14:10:53 -0400413 @Override
414 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100415 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
416 (int)mExpandedHeight, mMaxPanelHeight);
Daniel Sandler50508132012-08-16 14:10:53 -0400417 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100418 requestPanelHeightUpdate();
419 }
420
421 protected void requestPanelHeightUpdate() {
422 float currentMaxPanelHeight = getMaxPanelHeight();
423
424 // If the user isn't actively poking us, let's update the height
Jorim Jaggi1d480692014-05-20 19:41:58 +0200425 if (!mTracking && mHeightAnimator == null
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100426 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) {
427 setExpandedHeightInternal(currentMaxPanelHeight);
428 }
Daniel Sandler50508132012-08-16 14:10:53 -0400429 }
430
Daniel Sandler08d05e32012-08-08 16:39:54 -0400431 public void setExpandedHeightInternal(float h) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100432 float fh = getMaxPanelHeight();
Jorim Jaggi7d16bb12014-05-22 04:24:50 +0200433 mExpandedHeight = Math.min(fh, h);
Selim Cinek24120a52014-05-26 10:05:42 +0200434 float overExpansion = h - fh;
435 overExpansion = Math.max(0, overExpansion);
436 if (overExpansion != mOverExpansion) {
437 onOverExpansionChanged(overExpansion);
438 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400439
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100440 if (DEBUG) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200441 logf("setExpansion: height=%.1f fh=%.1f tracking=%s", h, fh, mTracking ? "T" : "f");
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100442 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400443
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100444 onHeightUpdated(mExpandedHeight);
Daniel Sandler198a0302012-08-17 16:04:31 -0400445 mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : h / fh);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400446 }
447
Selim Cinek24120a52014-05-26 10:05:42 +0200448 protected void onOverExpansionChanged(float overExpansion) {
449 mOverExpansion = overExpansion;
450 }
451
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100452 protected void onHeightUpdated(float expandedHeight) {
453 requestLayout();
454 }
455
456 /**
457 * This returns the maximum height of the panel. Children should override this if their
458 * desired height is not the full height.
459 *
460 * @return the default implementation simply returns the maximum height.
461 */
462 protected int getMaxPanelHeight() {
Selim Cinekb84a1072014-05-15 19:10:18 +0200463 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100464 return mMaxPanelHeight;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400465 }
466
467 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100468 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400469 }
470
471 public float getExpandedHeight() {
472 return mExpandedHeight;
473 }
474
475 public float getExpandedFraction() {
476 return mExpandedFraction;
477 }
478
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700479 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100480 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700481 }
482
483 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400484 return mExpandedHeight <= 0;
485 }
486
487 public boolean isCollapsing() {
488 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700489 }
490
John Spurlocka4b70af2013-08-17 14:05:49 -0400491 public boolean isTracking() {
492 return mTracking;
493 }
494
Daniel Sandler08d05e32012-08-08 16:39:54 -0400495 public void setBar(PanelBar panelBar) {
496 mBar = panelBar;
497 }
498
Daniel Sandler08d05e32012-08-08 16:39:54 -0400499 public void collapse() {
500 // TODO: abort animation or ongoing touch
John Spurlock97642182013-07-29 17:58:39 -0400501 if (DEBUG) logf("collapse: " + this);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700502 if (!isFullyCollapsed()) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200503 if (mHeightAnimator != null) {
504 mHeightAnimator.cancel();
505 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400506 mClosing = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200507 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200508 fling(0, false /* expand */);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400509 }
510 }
511
512 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400513 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400514 if (isFullyCollapsed()) {
515 mBar.startOpeningPanel(this);
Selim Cinek1685e632014-04-08 02:27:49 +0200516 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200517 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400518 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400519 if (DEBUG) logf("skipping expansion: is expanded");
520 }
521 }
522
523 public void cancelPeek() {
524 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
525 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400526 }
527 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500528
529 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200530 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400531 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500532 + "]",
533 this.getClass().getSimpleName(),
534 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100535 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500536 mClosing?"T":"f",
537 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500538 mJustPeeked?"T":"f",
539 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200540 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500541 ));
542 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400543}