blob: 7c1f2cf41db03dcaf43986561db1d76fef562391 [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() {
70 }
71
72 protected void onExpandingStarted() {
73 }
74
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040075 private void runPeekAnimation() {
John Spurlock97642182013-07-29 17:58:39 -040076 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +020077 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040078 return;
79 }
80 if (mPeekAnimator == null) {
John Spurlock209bede2013-07-17 12:23:27 -040081 mPeekAnimator = ObjectAnimator.ofFloat(this,
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040082 "expandedHeight", mPeekHeight)
Daniel Sandler3679bf52012-10-16 21:30:28 -040083 .setDuration(250);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040084 }
85 mPeekAnimator.start();
86 }
87
Daniel Sandler08d05e32012-08-08 16:39:54 -040088 public PanelView(Context context, AttributeSet attrs) {
89 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +020090 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Daniel Sandler08d05e32012-08-08 16:39:54 -040091 }
92
Jorim Jaggi069cd032014-05-15 03:09:01 +020093 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -040094 final Resources res = getContext().getResources();
John Spurlock209bede2013-07-17 12:23:27 -040095 mPeekHeight = res.getDimension(R.dimen.peek_height)
John Spurlock50728832014-04-17 19:05:28 -040096 + getPaddingBottom(); // our window might have a dropshadow
Selim Cinekb6d85eb2014-03-28 20:21:01 +010097
98 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
99 mTouchSlop = configuration.getScaledTouchSlop();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400100 }
101
102 private void trackMovement(MotionEvent event) {
103 // Add movement to velocity tracker using raw screen X and Y coordinates instead
104 // of window coordinates because the window frame may be moving at the same time.
105 float deltaX = event.getRawX() - event.getX();
106 float deltaY = event.getRawY() - event.getY();
107 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400108 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400109 event.offsetLocation(-deltaX, -deltaY);
110 }
111
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400112 @Override
113 public boolean onTouchEvent(MotionEvent event) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100114
115 /*
116 * We capture touch events here and update the expand height here in case according to
117 * the users fingers. This also handles multi-touch.
118 *
119 * If the user just clicks shortly, we give him a quick peek of the shade.
120 *
121 * Flinging is also enabled in order to open or close the shade.
122 */
123
124 int pointerIndex = event.findPointerIndex(mTrackingPointer);
125 if (pointerIndex < 0) {
126 pointerIndex = 0;
127 mTrackingPointer = event.getPointerId(pointerIndex);
128 }
129 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200130 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100131
132 switch (event.getActionMasked()) {
133 case MotionEvent.ACTION_DOWN:
134 mTracking = true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100135
136 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200137 mInitialTouchX = x;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200138 if (mVelocityTracker == null) {
139 initVelocityTracker();
140 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100141 trackMovement(event);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200142 if (mHeightAnimator != null) {
143 mHeightAnimator.cancel(); // end any outstanding animations
144 }
Selim Cinek1685e632014-04-08 02:27:49 +0200145 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100146 mInitialOffsetOnTouch = mExpandedHeight;
147 if (mExpandedHeight == 0) {
148 mJustPeeked = true;
149 runPeekAnimation();
150 }
151 break;
152
153 case MotionEvent.ACTION_POINTER_UP:
154 final int upPointer = event.getPointerId(event.getActionIndex());
155 if (mTrackingPointer == upPointer) {
156 // gesture is ongoing, find a new pointer to track
157 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
158 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200159 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100160 mTrackingPointer = event.getPointerId(newIndex);
161 mInitialOffsetOnTouch = mExpandedHeight;
162 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200163 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100164 }
165 break;
166
167 case MotionEvent.ACTION_MOVE:
168 final float h = y - mInitialTouchY + mInitialOffsetOnTouch;
169 if (h > mPeekHeight) {
170 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
171 mPeekAnimator.cancel();
172 }
173 mJustPeeked = false;
174 }
175 if (!mJustPeeked) {
176 setExpandedHeightInternal(h);
177 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
178 }
179
180 trackMovement(event);
181 break;
182
183 case MotionEvent.ACTION_UP:
184 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100185 mTracking = false;
186 mTrackingPointer = -1;
Selim Cinek1685e632014-04-08 02:27:49 +0200187 onTrackingStopped();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100188 trackMovement(event);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200189 flingWithCurrentVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100190 if (mVelocityTracker != null) {
191 mVelocityTracker.recycle();
192 mVelocityTracker = null;
193 }
194 break;
195 }
196 return true;
197 }
198
Selim Cinek1685e632014-04-08 02:27:49 +0200199 protected void onTrackingStopped() {
200 mBar.onTrackingStopped(PanelView.this);
201 }
202
203 protected void onTrackingStarted() {
204 mBar.onTrackingStarted(PanelView.this);
205 onExpandingStarted();
206 }
207
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100208 private float getCurrentVelocity() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100209
210 // the velocitytracker might be null if we got a bad input stream
211 if (mVelocityTracker == null) {
212 return 0;
213 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100214 mVelocityTracker.computeCurrentVelocity(1000);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200215 return mVelocityTracker.getYVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100216 }
217
218 @Override
219 public boolean onInterceptTouchEvent(MotionEvent event) {
220
221 /*
222 * If the user drags anywhere inside the panel we intercept it if he moves his finger
223 * upwards. This allows closing the shade from anywhere inside the panel.
224 *
225 * We only do this if the current content is scrolled to the bottom,
226 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
227 * possible.
228 */
229 int pointerIndex = event.findPointerIndex(mTrackingPointer);
230 if (pointerIndex < 0) {
231 pointerIndex = 0;
232 mTrackingPointer = event.getPointerId(pointerIndex);
233 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200234 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100235 final float y = event.getY(pointerIndex);
236 boolean scrolledToBottom = isScrolledToBottom();
237
238 switch (event.getActionMasked()) {
239 case MotionEvent.ACTION_DOWN:
Jorim Jaggi1d480692014-05-20 19:41:58 +0200240 if (mHeightAnimator != null) {
241 mHeightAnimator.cancel(); // end any outstanding animations
Selim Cinek172e9142014-05-07 19:38:00 +0200242 return true;
243 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100244 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200245 mInitialTouchX = x;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100246 initVelocityTracker();
247 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100248 break;
249 case MotionEvent.ACTION_POINTER_UP:
250 final int upPointer = event.getPointerId(event.getActionIndex());
251 if (mTrackingPointer == upPointer) {
252 // gesture is ongoing, find a new pointer to track
253 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
254 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200255 mInitialTouchX = event.getX(newIndex);
256 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100257 }
258 break;
259
260 case MotionEvent.ACTION_MOVE:
261 final float h = y - mInitialTouchY;
262 trackMovement(event);
263 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200264 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100265 mInitialOffsetOnTouch = mExpandedHeight;
266 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200267 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200268 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200269 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100270 return true;
271 }
272 }
273 break;
274 }
275 return false;
276 }
277
278 private void initVelocityTracker() {
279 if (mVelocityTracker != null) {
280 mVelocityTracker.recycle();
281 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200282 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100283 }
284
285 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200286 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100287 }
288
289 protected float getContentHeight() {
290 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400291 }
292
Daniel Sandler08d05e32012-08-08 16:39:54 -0400293 @Override
294 protected void onFinishInflate() {
295 super.onFinishInflate();
296 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400297 }
298
Jorim Jaggi069cd032014-05-15 03:09:01 +0200299 @Override
300 protected void onConfigurationChanged(Configuration newConfig) {
301 super.onConfigurationChanged(newConfig);
302 loadDimens();
303 mMaxPanelHeight = -1;
304 }
305
Jorim Jaggi1d480692014-05-20 19:41:58 +0200306 private void flingWithCurrentVelocity() {
307 float vel = getCurrentVelocity();
308 boolean expand;
309 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
310 expand = getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200311 } else {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200312 expand = vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400313 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200314 fling(vel, expand);
315 }
316
317 protected void fling(float vel, boolean expand) {
318 cancelPeek();
319 float target = expand ? getMaxPanelHeight() : 0.0f;
320 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, target);
321 if (expand) {
322 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
323 } else {
324 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
325 getHeight());
326
327 // Make it shorter if we run a canned animation
328 if (vel == 0) {
329 animator.setDuration((long) (animator.getDuration() / 1.75f));
330 }
331 }
332 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
333 @Override
334 public void onAnimationUpdate(ValueAnimator animation) {
335 setExpandedHeight((Float) animation.getAnimatedValue());
336 }
337 });
338 animator.addListener(new AnimatorListenerAdapter() {
339 @Override
340 public void onAnimationEnd(Animator animation) {
341 mHeightAnimator = null;
342 onExpandingFinished();
343 }
344 });
345 animator.start();
346 mHeightAnimator = animator;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400347 }
348
349 @Override
350 protected void onAttachedToWindow() {
351 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400352 mViewName = getResources().getResourceName(getId());
353 }
354
355 public String getName() {
356 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400357 }
358
Daniel Sandler08d05e32012-08-08 16:39:54 -0400359 @Override
360 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
361 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
362
John Spurlock97642182013-07-29 17:58:39 -0400363 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
Daniel Sandler08d05e32012-08-08 16:39:54 -0400364 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
Daniel Sandler198a0302012-08-17 16:04:31 -0400365
366 // Did one of our children change size?
367 int newHeight = getMeasuredHeight();
Selim Cinekb84a1072014-05-15 19:10:18 +0200368 if (newHeight > mMaxPanelHeight) {
369 // we only adapt the max height if it's bigger
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100370 mMaxPanelHeight = newHeight;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200371 // If the user isn't actively poking us, let's rubberband to the content
Jorim Jaggi1d480692014-05-20 19:41:58 +0200372 if (!mTracking && mHeightAnimator == null
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200373 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
374 && mMaxPanelHeight > 0) {
375 mExpandedHeight = mMaxPanelHeight;
376 }
Daniel Sandler50508132012-08-16 14:10:53 -0400377 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400378 }
379
Daniel Sandler08d05e32012-08-08 16:39:54 -0400380 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400381 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400382 setExpandedHeightInternal(height);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400383 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400384 }
385
Daniel Sandler50508132012-08-16 14:10:53 -0400386 @Override
387 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100388 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
389 (int)mExpandedHeight, mMaxPanelHeight);
Daniel Sandler50508132012-08-16 14:10:53 -0400390 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100391 requestPanelHeightUpdate();
392 }
393
394 protected void requestPanelHeightUpdate() {
395 float currentMaxPanelHeight = getMaxPanelHeight();
396
397 // If the user isn't actively poking us, let's update the height
Jorim Jaggi1d480692014-05-20 19:41:58 +0200398 if (!mTracking && mHeightAnimator == null
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100399 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) {
400 setExpandedHeightInternal(currentMaxPanelHeight);
401 }
Daniel Sandler50508132012-08-16 14:10:53 -0400402 }
403
Daniel Sandler08d05e32012-08-08 16:39:54 -0400404 public void setExpandedHeightInternal(float h) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100405 float fh = getMaxPanelHeight();
Jorim Jaggi7d16bb12014-05-22 04:24:50 +0200406 mExpandedHeight = Math.min(fh, h);
Selim Cinek24120a52014-05-26 10:05:42 +0200407 float overExpansion = h - fh;
408 overExpansion = Math.max(0, overExpansion);
409 if (overExpansion != mOverExpansion) {
410 onOverExpansionChanged(overExpansion);
411 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400412
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100413 if (DEBUG) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200414 logf("setExpansion: height=%.1f fh=%.1f tracking=%s", h, fh, mTracking ? "T" : "f");
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100415 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400416
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100417 onHeightUpdated(mExpandedHeight);
Daniel Sandler198a0302012-08-17 16:04:31 -0400418 mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : h / fh);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400419 }
420
Selim Cinek24120a52014-05-26 10:05:42 +0200421 protected void onOverExpansionChanged(float overExpansion) {
422 mOverExpansion = overExpansion;
423 }
424
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100425 protected void onHeightUpdated(float expandedHeight) {
426 requestLayout();
427 }
428
429 /**
430 * This returns the maximum height of the panel. Children should override this if their
431 * desired height is not the full height.
432 *
433 * @return the default implementation simply returns the maximum height.
434 */
435 protected int getMaxPanelHeight() {
Selim Cinekb84a1072014-05-15 19:10:18 +0200436 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100437 return mMaxPanelHeight;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400438 }
439
440 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100441 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400442 }
443
444 public float getExpandedHeight() {
445 return mExpandedHeight;
446 }
447
448 public float getExpandedFraction() {
449 return mExpandedFraction;
450 }
451
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700452 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100453 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700454 }
455
456 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400457 return mExpandedHeight <= 0;
458 }
459
460 public boolean isCollapsing() {
461 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700462 }
463
John Spurlocka4b70af2013-08-17 14:05:49 -0400464 public boolean isTracking() {
465 return mTracking;
466 }
467
Daniel Sandler08d05e32012-08-08 16:39:54 -0400468 public void setBar(PanelBar panelBar) {
469 mBar = panelBar;
470 }
471
Daniel Sandler08d05e32012-08-08 16:39:54 -0400472 public void collapse() {
473 // TODO: abort animation or ongoing touch
John Spurlock97642182013-07-29 17:58:39 -0400474 if (DEBUG) logf("collapse: " + this);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700475 if (!isFullyCollapsed()) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200476 if (mHeightAnimator != null) {
477 mHeightAnimator.cancel();
478 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400479 mClosing = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200480 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200481 fling(0, false /* expand */);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400482 }
483 }
484
485 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400486 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400487 if (isFullyCollapsed()) {
488 mBar.startOpeningPanel(this);
Selim Cinek1685e632014-04-08 02:27:49 +0200489 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200490 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400491 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400492 if (DEBUG) logf("skipping expansion: is expanded");
493 }
494 }
495
496 public void cancelPeek() {
497 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
498 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400499 }
500 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500501
502 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200503 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400504 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500505 + "]",
506 this.getClass().getSimpleName(),
507 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100508 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500509 mClosing?"T":"f",
510 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500511 mJustPeeked?"T":"f",
512 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200513 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500514 ));
515 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400516}