blob: 7500c105e9f9aadbb0a2a86770b69b952f6f7b08 [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
Daniel Sandler08d05e32012-08-08 16:39:54 -040038public 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
133 switch (event.getActionMasked()) {
134 case MotionEvent.ACTION_DOWN:
135 mTracking = true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100136
137 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200138 mInitialTouchX = x;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200139 if (mVelocityTracker == null) {
140 initVelocityTracker();
141 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100142 trackMovement(event);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200143 if (mHeightAnimator != null) {
144 mHeightAnimator.cancel(); // end any outstanding animations
145 }
Selim Cinek1685e632014-04-08 02:27:49 +0200146 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100147 mInitialOffsetOnTouch = mExpandedHeight;
148 if (mExpandedHeight == 0) {
149 mJustPeeked = true;
150 runPeekAnimation();
151 }
152 break;
153
154 case MotionEvent.ACTION_POINTER_UP:
155 final int upPointer = event.getPointerId(event.getActionIndex());
156 if (mTrackingPointer == upPointer) {
157 // gesture is ongoing, find a new pointer to track
158 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
159 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200160 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100161 mTrackingPointer = event.getPointerId(newIndex);
162 mInitialOffsetOnTouch = mExpandedHeight;
163 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200164 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100165 }
166 break;
167
168 case MotionEvent.ACTION_MOVE:
169 final float h = y - mInitialTouchY + mInitialOffsetOnTouch;
170 if (h > mPeekHeight) {
171 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
172 mPeekAnimator.cancel();
173 }
174 mJustPeeked = false;
175 }
176 if (!mJustPeeked) {
177 setExpandedHeightInternal(h);
178 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
179 }
180
181 trackMovement(event);
182 break;
183
184 case MotionEvent.ACTION_UP:
185 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100186 mTracking = false;
187 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100188 trackMovement(event);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200189 boolean expand = flingWithCurrentVelocity();
190 onTrackingStopped(expand);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100191 if (mVelocityTracker != null) {
192 mVelocityTracker.recycle();
193 mVelocityTracker = null;
194 }
195 break;
196 }
197 return true;
198 }
199
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200200 protected void onTrackingStopped(boolean expand) {
201 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200202 }
203
204 protected void onTrackingStarted() {
205 mBar.onTrackingStarted(PanelView.this);
206 onExpandingStarted();
207 }
208
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100209 private float getCurrentVelocity() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100210
211 // the velocitytracker might be null if we got a bad input stream
212 if (mVelocityTracker == null) {
213 return 0;
214 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100215 mVelocityTracker.computeCurrentVelocity(1000);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200216 return mVelocityTracker.getYVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100217 }
218
219 @Override
220 public boolean onInterceptTouchEvent(MotionEvent event) {
221
222 /*
223 * If the user drags anywhere inside the panel we intercept it if he moves his finger
224 * upwards. This allows closing the shade from anywhere inside the panel.
225 *
226 * We only do this if the current content is scrolled to the bottom,
227 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
228 * possible.
229 */
230 int pointerIndex = event.findPointerIndex(mTrackingPointer);
231 if (pointerIndex < 0) {
232 pointerIndex = 0;
233 mTrackingPointer = event.getPointerId(pointerIndex);
234 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200235 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100236 final float y = event.getY(pointerIndex);
237 boolean scrolledToBottom = isScrolledToBottom();
238
239 switch (event.getActionMasked()) {
240 case MotionEvent.ACTION_DOWN:
Jorim Jaggi1d480692014-05-20 19:41:58 +0200241 if (mHeightAnimator != null) {
242 mHeightAnimator.cancel(); // end any outstanding animations
Selim Cinek172e9142014-05-07 19:38:00 +0200243 return true;
244 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100245 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200246 mInitialTouchX = x;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100247 initVelocityTracker();
248 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100249 break;
250 case MotionEvent.ACTION_POINTER_UP:
251 final int upPointer = event.getPointerId(event.getActionIndex());
252 if (mTrackingPointer == upPointer) {
253 // gesture is ongoing, find a new pointer to track
254 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
255 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200256 mInitialTouchX = event.getX(newIndex);
257 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100258 }
259 break;
260
261 case MotionEvent.ACTION_MOVE:
262 final float h = y - mInitialTouchY;
263 trackMovement(event);
264 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200265 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100266 mInitialOffsetOnTouch = mExpandedHeight;
267 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200268 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200269 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200270 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100271 return true;
272 }
273 }
274 break;
275 }
276 return false;
277 }
278
279 private void initVelocityTracker() {
280 if (mVelocityTracker != null) {
281 mVelocityTracker.recycle();
282 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200283 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100284 }
285
286 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200287 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100288 }
289
290 protected float getContentHeight() {
291 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400292 }
293
Daniel Sandler08d05e32012-08-08 16:39:54 -0400294 @Override
295 protected void onFinishInflate() {
296 super.onFinishInflate();
297 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400298 }
299
Jorim Jaggi069cd032014-05-15 03:09:01 +0200300 @Override
301 protected void onConfigurationChanged(Configuration newConfig) {
302 super.onConfigurationChanged(newConfig);
303 loadDimens();
304 mMaxPanelHeight = -1;
305 }
306
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200307 /**
308 * @return whether the panel will be expanded after the animation
309 */
310 private boolean flingWithCurrentVelocity() {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200311 float vel = getCurrentVelocity();
312 boolean expand;
313 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
314 expand = getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200315 } else {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200316 expand = vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400317 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200318 fling(vel, expand);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200319 return expand;
Jorim Jaggi1d480692014-05-20 19:41:58 +0200320 }
321
322 protected void fling(float vel, boolean expand) {
323 cancelPeek();
324 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200325 if (target == mExpandedHeight) {
326 onExpandingFinished();
327 return;
328 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200329 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, target);
330 if (expand) {
331 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
332 } else {
333 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
334 getHeight());
335
336 // Make it shorter if we run a canned animation
337 if (vel == 0) {
338 animator.setDuration((long) (animator.getDuration() / 1.75f));
339 }
340 }
341 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
342 @Override
343 public void onAnimationUpdate(ValueAnimator animation) {
344 setExpandedHeight((Float) animation.getAnimatedValue());
345 }
346 });
347 animator.addListener(new AnimatorListenerAdapter() {
348 @Override
349 public void onAnimationEnd(Animator animation) {
350 mHeightAnimator = null;
351 onExpandingFinished();
352 }
353 });
354 animator.start();
355 mHeightAnimator = animator;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400356 }
357
358 @Override
359 protected void onAttachedToWindow() {
360 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400361 mViewName = getResources().getResourceName(getId());
362 }
363
364 public String getName() {
365 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400366 }
367
Daniel Sandler08d05e32012-08-08 16:39:54 -0400368 @Override
369 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
370 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
371
John Spurlock97642182013-07-29 17:58:39 -0400372 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
Daniel Sandler08d05e32012-08-08 16:39:54 -0400373 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
Daniel Sandler198a0302012-08-17 16:04:31 -0400374
375 // Did one of our children change size?
376 int newHeight = getMeasuredHeight();
Selim Cinekb84a1072014-05-15 19:10:18 +0200377 if (newHeight > mMaxPanelHeight) {
378 // we only adapt the max height if it's bigger
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100379 mMaxPanelHeight = newHeight;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200380 // If the user isn't actively poking us, let's rubberband to the content
Jorim Jaggi1d480692014-05-20 19:41:58 +0200381 if (!mTracking && mHeightAnimator == null
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200382 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
383 && mMaxPanelHeight > 0) {
384 mExpandedHeight = mMaxPanelHeight;
385 }
Daniel Sandler50508132012-08-16 14:10:53 -0400386 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400387 }
388
Daniel Sandler08d05e32012-08-08 16:39:54 -0400389 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400390 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400391 setExpandedHeightInternal(height);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400392 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400393 }
394
Daniel Sandler50508132012-08-16 14:10:53 -0400395 @Override
396 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100397 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
398 (int)mExpandedHeight, mMaxPanelHeight);
Daniel Sandler50508132012-08-16 14:10:53 -0400399 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100400 requestPanelHeightUpdate();
401 }
402
403 protected void requestPanelHeightUpdate() {
404 float currentMaxPanelHeight = getMaxPanelHeight();
405
406 // If the user isn't actively poking us, let's update the height
Jorim Jaggi1d480692014-05-20 19:41:58 +0200407 if (!mTracking && mHeightAnimator == null
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100408 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) {
409 setExpandedHeightInternal(currentMaxPanelHeight);
410 }
Daniel Sandler50508132012-08-16 14:10:53 -0400411 }
412
Daniel Sandler08d05e32012-08-08 16:39:54 -0400413 public void setExpandedHeightInternal(float h) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100414 float fh = getMaxPanelHeight();
Jorim Jaggi7d16bb12014-05-22 04:24:50 +0200415 mExpandedHeight = Math.min(fh, h);
Selim Cinek24120a52014-05-26 10:05:42 +0200416 float overExpansion = h - fh;
417 overExpansion = Math.max(0, overExpansion);
418 if (overExpansion != mOverExpansion) {
419 onOverExpansionChanged(overExpansion);
420 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400421
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100422 if (DEBUG) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200423 logf("setExpansion: height=%.1f fh=%.1f tracking=%s", h, fh, mTracking ? "T" : "f");
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100424 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400425
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100426 onHeightUpdated(mExpandedHeight);
Daniel Sandler198a0302012-08-17 16:04:31 -0400427 mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : h / fh);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400428 }
429
Selim Cinek24120a52014-05-26 10:05:42 +0200430 protected void onOverExpansionChanged(float overExpansion) {
431 mOverExpansion = overExpansion;
432 }
433
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100434 protected void onHeightUpdated(float expandedHeight) {
435 requestLayout();
436 }
437
438 /**
439 * This returns the maximum height of the panel. Children should override this if their
440 * desired height is not the full height.
441 *
442 * @return the default implementation simply returns the maximum height.
443 */
444 protected int getMaxPanelHeight() {
Selim Cinekb84a1072014-05-15 19:10:18 +0200445 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100446 return mMaxPanelHeight;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400447 }
448
449 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100450 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400451 }
452
453 public float getExpandedHeight() {
454 return mExpandedHeight;
455 }
456
457 public float getExpandedFraction() {
458 return mExpandedFraction;
459 }
460
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700461 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100462 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700463 }
464
465 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400466 return mExpandedHeight <= 0;
467 }
468
469 public boolean isCollapsing() {
470 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700471 }
472
John Spurlocka4b70af2013-08-17 14:05:49 -0400473 public boolean isTracking() {
474 return mTracking;
475 }
476
Daniel Sandler08d05e32012-08-08 16:39:54 -0400477 public void setBar(PanelBar panelBar) {
478 mBar = panelBar;
479 }
480
Daniel Sandler08d05e32012-08-08 16:39:54 -0400481 public void collapse() {
482 // TODO: abort animation or ongoing touch
John Spurlock97642182013-07-29 17:58:39 -0400483 if (DEBUG) logf("collapse: " + this);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700484 if (!isFullyCollapsed()) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200485 if (mHeightAnimator != null) {
486 mHeightAnimator.cancel();
487 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400488 mClosing = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200489 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200490 fling(0, false /* expand */);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400491 }
492 }
493
494 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400495 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400496 if (isFullyCollapsed()) {
497 mBar.startOpeningPanel(this);
Selim Cinek1685e632014-04-08 02:27:49 +0200498 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200499 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400500 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400501 if (DEBUG) logf("skipping expansion: is expanded");
502 }
503 }
504
505 public void cancelPeek() {
506 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
507 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400508 }
509 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500510
511 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200512 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400513 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500514 + "]",
515 this.getClass().getSimpleName(),
516 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100517 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500518 mClosing?"T":"f",
519 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500520 mJustPeeked?"T":"f",
521 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200522 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500523 ));
524 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400525}