blob: f43f3489b00a01c30ced5413a948cd00391d0fde [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;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020053 private int mEdgeTapAreaWidth;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010054 private float mInitialOffsetOnTouch;
Daniel Sandler08d05e32012-08-08 16:39:54 -040055 private float mExpandedFraction = 0;
56 private float mExpandedHeight = 0;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040057 private boolean mJustPeeked;
Daniel Sandler50508132012-08-16 14:10:53 -040058 private boolean mClosing;
Jorim Jaggi8dd95e02014-06-03 16:19:33 +020059 protected boolean mTracking;
Jorim Jaggi90129582014-06-02 14:44:49 +020060 private boolean mTouchSlopExceeded;
John Spurlock48fa91a2013-08-15 09:29:31 -040061 private int mTrackingPointer;
Jorim Jaggid7daab72014-05-06 22:22:20 +020062 protected int mTouchSlop;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020063 protected boolean mHintAnimationRunning;
Daniel Sandler08d05e32012-08-08 16:39:54 -040064
Jorim Jaggi1d480692014-05-20 19:41:58 +020065 private ValueAnimator mHeightAnimator;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040066 private ObjectAnimator mPeekAnimator;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +020067 private VelocityTrackerInterface mVelocityTracker;
Jorim Jaggi1d480692014-05-20 19:41:58 +020068 private FlingAnimationUtils mFlingAnimationUtils;
Daniel Sandler08d05e32012-08-08 16:39:54 -040069
Daniel Sandler08d05e32012-08-08 16:39:54 -040070 PanelBar mBar;
71
Selim Cinekb84a1072014-05-15 19:10:18 +020072 protected int mMaxPanelHeight = -1;
Daniel Sandler50508132012-08-16 14:10:53 -040073 private String mViewName;
Jorim Jaggid7daab72014-05-06 22:22:20 +020074 private float mInitialTouchY;
75 private float mInitialTouchX;
Daniel Sandler08d05e32012-08-08 16:39:54 -040076
Jorim Jaggi90129582014-06-02 14:44:49 +020077 private Interpolator mLinearOutSlowInInterpolator;
78 private Interpolator mBounceInterpolator;
79
Selim Cinek1685e632014-04-08 02:27:49 +020080 protected void onExpandingFinished() {
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +020081 mBar.onExpandingFinished();
Selim Cinek1685e632014-04-08 02:27:49 +020082 }
83
84 protected void onExpandingStarted() {
85 }
86
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040087 private void runPeekAnimation() {
John Spurlock97642182013-07-29 17:58:39 -040088 if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
Jorim Jaggi1d480692014-05-20 19:41:58 +020089 if (mHeightAnimator != null) {
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040090 return;
91 }
92 if (mPeekAnimator == null) {
John Spurlock209bede2013-07-17 12:23:27 -040093 mPeekAnimator = ObjectAnimator.ofFloat(this,
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040094 "expandedHeight", mPeekHeight)
Daniel Sandler3679bf52012-10-16 21:30:28 -040095 .setDuration(250);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040096 }
97 mPeekAnimator.start();
98 }
99
Daniel Sandler08d05e32012-08-08 16:39:54 -0400100 public PanelView(Context context, AttributeSet attrs) {
101 super(context, attrs);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200102 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
Jorim Jaggi90129582014-06-02 14:44:49 +0200103 mLinearOutSlowInInterpolator =
104 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
105 mBounceInterpolator = new BounceInterpolator();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400106 }
107
Jorim Jaggi069cd032014-05-15 03:09:01 +0200108 protected void loadDimens() {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400109 final Resources res = getContext().getResources();
John Spurlock209bede2013-07-17 12:23:27 -0400110 mPeekHeight = res.getDimension(R.dimen.peek_height)
John Spurlock50728832014-04-17 19:05:28 -0400111 + getPaddingBottom(); // our window might have a dropshadow
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100112
113 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
114 mTouchSlop = configuration.getScaledTouchSlop();
Jorim Jaggi90129582014-06-02 14:44:49 +0200115 mHintDistance = res.getDimension(R.dimen.hint_move_distance);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200116 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400117 }
118
119 private void trackMovement(MotionEvent event) {
120 // Add movement to velocity tracker using raw screen X and Y coordinates instead
121 // of window coordinates because the window frame may be moving at the same time.
122 float deltaX = event.getRawX() - event.getX();
123 float deltaY = event.getRawY() - event.getY();
124 event.offsetLocation(deltaX, deltaY);
Daniel Sandlerb17a7262012-10-05 14:32:50 -0400125 if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400126 event.offsetLocation(-deltaX, -deltaY);
127 }
128
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400129 @Override
130 public boolean onTouchEvent(MotionEvent event) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100131
132 /*
133 * We capture touch events here and update the expand height here in case according to
134 * the users fingers. This also handles multi-touch.
135 *
136 * If the user just clicks shortly, we give him a quick peek of the shade.
137 *
138 * Flinging is also enabled in order to open or close the shade.
139 */
140
141 int pointerIndex = event.findPointerIndex(mTrackingPointer);
142 if (pointerIndex < 0) {
143 pointerIndex = 0;
144 mTrackingPointer = event.getPointerId(pointerIndex);
145 }
146 final float y = event.getY(pointerIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200147 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100148
Selim Cinek4c6969a2014-05-26 19:22:17 +0200149 boolean waitForTouchSlop = hasConflictingGestures();
150
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100151 switch (event.getActionMasked()) {
152 case MotionEvent.ACTION_DOWN:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100153 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200154 mInitialTouchX = x;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200155 mInitialOffsetOnTouch = mExpandedHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +0200156 mTouchSlopExceeded = false;
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200157 if (mVelocityTracker == null) {
158 initVelocityTracker();
159 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100160 trackMovement(event);
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200161 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning)) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200162 if (mHeightAnimator != null) {
163 mHeightAnimator.cancel(); // end any outstanding animations
164 }
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200165 mTouchSlopExceeded = true;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200166 onTrackingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200167 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100168 if (mExpandedHeight == 0) {
169 mJustPeeked = true;
170 runPeekAnimation();
171 }
172 break;
173
174 case MotionEvent.ACTION_POINTER_UP:
175 final int upPointer = event.getPointerId(event.getActionIndex());
176 if (mTrackingPointer == upPointer) {
177 // gesture is ongoing, find a new pointer to track
178 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
179 final float newY = event.getY(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200180 final float newX = event.getX(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100181 mTrackingPointer = event.getPointerId(newIndex);
182 mInitialOffsetOnTouch = mExpandedHeight;
183 mInitialTouchY = newY;
Jorim Jaggia6310292014-04-16 14:11:52 +0200184 mInitialTouchX = newX;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100185 }
186 break;
187
188 case MotionEvent.ACTION_MOVE:
Selim Cinek4c6969a2014-05-26 19:22:17 +0200189 float h = y - mInitialTouchY;
Jorim Jaggi90129582014-06-02 14:44:49 +0200190 if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)) {
191 mTouchSlopExceeded = true;
192 if (waitForTouchSlop && !mTracking) {
193 mInitialOffsetOnTouch = mExpandedHeight;
194 mInitialTouchX = x;
195 mInitialTouchY = y;
196 if (mHeightAnimator != null) {
197 mHeightAnimator.cancel(); // end any outstanding animations
198 }
199 onTrackingStarted();
200 h = 0;
Selim Cinek4c6969a2014-05-26 19:22:17 +0200201 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200202 }
203 final float newHeight = h + mInitialOffsetOnTouch;
204 if (newHeight > mPeekHeight) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100205 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
206 mPeekAnimator.cancel();
207 }
208 mJustPeeked = false;
209 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200210 if (!mJustPeeked && (!waitForTouchSlop || mTracking)) {
211 setExpandedHeightInternal(newHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100212 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
213 }
214
215 trackMovement(event);
216 break;
217
218 case MotionEvent.ACTION_UP:
219 case MotionEvent.ACTION_CANCEL:
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100220 mTrackingPointer = -1;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100221 trackMovement(event);
Jorim Jaggi90129582014-06-02 14:44:49 +0200222 if (mTracking && mTouchSlopExceeded) {
223 float vel = getCurrentVelocity();
224 boolean expand = flingExpands(vel);
225 onTrackingStopped(expand);
226 fling(vel, expand);
227 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200228 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200229 onTrackingStopped(expands);
230 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100231 if (mVelocityTracker != null) {
232 mVelocityTracker.recycle();
233 mVelocityTracker = null;
234 }
235 break;
236 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200237 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100238 }
239
Selim Cinek4c6969a2014-05-26 19:22:17 +0200240 protected abstract boolean hasConflictingGestures();
241
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200242 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200243 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200244 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200245 }
246
247 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200248 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200249 mBar.onTrackingStarted(PanelView.this);
250 onExpandingStarted();
251 }
252
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100253 private float getCurrentVelocity() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100254
255 // the velocitytracker might be null if we got a bad input stream
256 if (mVelocityTracker == null) {
257 return 0;
258 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100259 mVelocityTracker.computeCurrentVelocity(1000);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200260 return mVelocityTracker.getYVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100261 }
262
263 @Override
264 public boolean onInterceptTouchEvent(MotionEvent event) {
265
266 /*
267 * If the user drags anywhere inside the panel we intercept it if he moves his finger
268 * upwards. This allows closing the shade from anywhere inside the panel.
269 *
270 * We only do this if the current content is scrolled to the bottom,
271 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
272 * possible.
273 */
274 int pointerIndex = event.findPointerIndex(mTrackingPointer);
275 if (pointerIndex < 0) {
276 pointerIndex = 0;
277 mTrackingPointer = event.getPointerId(pointerIndex);
278 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200279 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100280 final float y = event.getY(pointerIndex);
281 boolean scrolledToBottom = isScrolledToBottom();
282
283 switch (event.getActionMasked()) {
284 case MotionEvent.ACTION_DOWN:
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200285 if (mHeightAnimator != null && !mHintAnimationRunning) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200286 mHeightAnimator.cancel(); // end any outstanding animations
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200287 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200288 return true;
289 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100290 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200291 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200292 mTouchSlopExceeded = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100293 initVelocityTracker();
294 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100295 break;
296 case MotionEvent.ACTION_POINTER_UP:
297 final int upPointer = event.getPointerId(event.getActionIndex());
298 if (mTrackingPointer == upPointer) {
299 // gesture is ongoing, find a new pointer to track
300 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
301 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200302 mInitialTouchX = event.getX(newIndex);
303 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100304 }
305 break;
306
307 case MotionEvent.ACTION_MOVE:
308 final float h = y - mInitialTouchY;
309 trackMovement(event);
310 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200311 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200312 if (mHeightAnimator != null) {
313 mHeightAnimator.cancel();
314 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100315 mInitialOffsetOnTouch = mExpandedHeight;
316 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200317 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200318 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200319 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200320 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100321 return true;
322 }
323 }
324 break;
325 }
326 return false;
327 }
328
329 private void initVelocityTracker() {
330 if (mVelocityTracker != null) {
331 mVelocityTracker.recycle();
332 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200333 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100334 }
335
336 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200337 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100338 }
339
340 protected float getContentHeight() {
341 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400342 }
343
Daniel Sandler08d05e32012-08-08 16:39:54 -0400344 @Override
345 protected void onFinishInflate() {
346 super.onFinishInflate();
347 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400348 }
349
Jorim Jaggi069cd032014-05-15 03:09:01 +0200350 @Override
351 protected void onConfigurationChanged(Configuration newConfig) {
352 super.onConfigurationChanged(newConfig);
353 loadDimens();
354 mMaxPanelHeight = -1;
355 }
356
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200357 /**
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200358 * @param vel the current velocity of the motion
359 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200360 */
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200361 private boolean flingExpands(float vel) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200362 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200363 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200364 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200365 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400366 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200367 }
368
369 protected void fling(float vel, boolean expand) {
370 cancelPeek();
371 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200372 if (target == mExpandedHeight) {
373 onExpandingFinished();
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200374 mBar.panelExpansionChanged(this, mExpandedFraction);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200375 return;
376 }
Jorim Jaggi90129582014-06-02 14:44:49 +0200377 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200378 if (expand) {
379 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
380 } else {
381 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
382 getHeight());
383
384 // Make it shorter if we run a canned animation
385 if (vel == 0) {
386 animator.setDuration((long) (animator.getDuration() / 1.75f));
387 }
388 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200389 animator.addListener(new AnimatorListenerAdapter() {
390 @Override
391 public void onAnimationEnd(Animator animation) {
392 mHeightAnimator = null;
393 onExpandingFinished();
394 }
395 });
396 animator.start();
397 mHeightAnimator = animator;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400398 }
399
400 @Override
401 protected void onAttachedToWindow() {
402 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400403 mViewName = getResources().getResourceName(getId());
404 }
405
406 public String getName() {
407 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400408 }
409
Daniel Sandler08d05e32012-08-08 16:39:54 -0400410 @Override
411 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
412 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
413
John Spurlock97642182013-07-29 17:58:39 -0400414 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
Daniel Sandler08d05e32012-08-08 16:39:54 -0400415 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
Daniel Sandler198a0302012-08-17 16:04:31 -0400416
417 // Did one of our children change size?
418 int newHeight = getMeasuredHeight();
Selim Cinekb84a1072014-05-15 19:10:18 +0200419 if (newHeight > mMaxPanelHeight) {
420 // we only adapt the max height if it's bigger
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100421 mMaxPanelHeight = newHeight;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200422 // If the user isn't actively poking us, let's rubberband to the content
Jorim Jaggi1d480692014-05-20 19:41:58 +0200423 if (!mTracking && mHeightAnimator == null
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200424 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
425 && mMaxPanelHeight > 0) {
426 mExpandedHeight = mMaxPanelHeight;
427 }
Daniel Sandler50508132012-08-16 14:10:53 -0400428 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400429 }
430
Daniel Sandler08d05e32012-08-08 16:39:54 -0400431 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400432 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400433 setExpandedHeightInternal(height);
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400434 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400435 }
436
Daniel Sandler50508132012-08-16 14:10:53 -0400437 @Override
438 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100439 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
440 (int)mExpandedHeight, mMaxPanelHeight);
Daniel Sandler50508132012-08-16 14:10:53 -0400441 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100442 requestPanelHeightUpdate();
443 }
444
445 protected void requestPanelHeightUpdate() {
446 float currentMaxPanelHeight = getMaxPanelHeight();
447
448 // If the user isn't actively poking us, let's update the height
Jorim Jaggi1d480692014-05-20 19:41:58 +0200449 if (!mTracking && mHeightAnimator == null
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100450 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) {
451 setExpandedHeightInternal(currentMaxPanelHeight);
452 }
Daniel Sandler50508132012-08-16 14:10:53 -0400453 }
454
Daniel Sandler08d05e32012-08-08 16:39:54 -0400455 public void setExpandedHeightInternal(float h) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100456 float fh = getMaxPanelHeight();
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200457 mExpandedHeight = Math.max(0, Math.min(fh, h));
Selim Cinek24120a52014-05-26 10:05:42 +0200458 float overExpansion = h - fh;
459 overExpansion = Math.max(0, overExpansion);
460 if (overExpansion != mOverExpansion) {
461 onOverExpansionChanged(overExpansion);
462 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400463
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100464 if (DEBUG) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200465 logf("setExpansion: height=%.1f fh=%.1f tracking=%s", h, fh, mTracking ? "T" : "f");
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100466 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400467
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100468 onHeightUpdated(mExpandedHeight);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200469 mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : mExpandedHeight / fh);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400470 }
471
Selim Cinek24120a52014-05-26 10:05:42 +0200472 protected void onOverExpansionChanged(float overExpansion) {
473 mOverExpansion = overExpansion;
474 }
475
Jorim Jaggi90129582014-06-02 14:44:49 +0200476 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100477
478 /**
479 * This returns the maximum height of the panel. Children should override this if their
480 * desired height is not the full height.
481 *
482 * @return the default implementation simply returns the maximum height.
483 */
484 protected int getMaxPanelHeight() {
Selim Cinekb84a1072014-05-15 19:10:18 +0200485 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100486 return mMaxPanelHeight;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400487 }
488
489 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100490 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400491 }
492
493 public float getExpandedHeight() {
494 return mExpandedHeight;
495 }
496
497 public float getExpandedFraction() {
498 return mExpandedFraction;
499 }
500
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700501 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100502 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700503 }
504
505 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400506 return mExpandedHeight <= 0;
507 }
508
509 public boolean isCollapsing() {
510 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700511 }
512
John Spurlocka4b70af2013-08-17 14:05:49 -0400513 public boolean isTracking() {
514 return mTracking;
515 }
516
Daniel Sandler08d05e32012-08-08 16:39:54 -0400517 public void setBar(PanelBar panelBar) {
518 mBar = panelBar;
519 }
520
Daniel Sandler08d05e32012-08-08 16:39:54 -0400521 public void collapse() {
522 // TODO: abort animation or ongoing touch
John Spurlock97642182013-07-29 17:58:39 -0400523 if (DEBUG) logf("collapse: " + this);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700524 if (!isFullyCollapsed()) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200525 if (mHeightAnimator != null) {
526 mHeightAnimator.cancel();
527 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400528 mClosing = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200529 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200530 fling(0, false /* expand */);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400531 }
532 }
533
534 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400535 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400536 if (isFullyCollapsed()) {
537 mBar.startOpeningPanel(this);
Selim Cinek1685e632014-04-08 02:27:49 +0200538 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200539 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400540 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400541 if (DEBUG) logf("skipping expansion: is expanded");
542 }
543 }
544
545 public void cancelPeek() {
546 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
547 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400548 }
549 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500550
Jorim Jaggi90129582014-06-02 14:44:49 +0200551 protected void startUnlockHintAnimation() {
552
553 // We don't need to hint the user if an animation is already running or the user is changing
554 // the expansion.
555 if (mHeightAnimator != null || mTracking) {
556 return;
557 }
558 cancelPeek();
559 onExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200560 startUnlockHintAnimationPhase1(new Runnable() {
561 @Override
562 public void run() {
563 onExpandingFinished();
564 mStatusBar.onHintFinished();
565 mHintAnimationRunning = false;
566 }
567 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200568 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200569 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200570 }
571
572 /**
573 * Phase 1: Move everything upwards.
574 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200575 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200576 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
577 ValueAnimator animator = createHeightAnimator(target);
578 animator.setDuration(250);
579 animator.setInterpolator(mLinearOutSlowInInterpolator);
580 animator.addListener(new AnimatorListenerAdapter() {
581 private boolean mCancelled;
582
583 @Override
584 public void onAnimationCancel(Animator animation) {
585 mCancelled = true;
586 }
587
588 @Override
589 public void onAnimationEnd(Animator animation) {
590 if (mCancelled) {
591 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200592 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200593 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200594 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200595 }
596 }
597 });
598 animator.start();
599 mHeightAnimator = animator;
600 }
601
602 /**
603 * Phase 2: Bounce down.
604 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200605 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200606 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
607 animator.setDuration(450);
608 animator.setInterpolator(mBounceInterpolator);
609 animator.addListener(new AnimatorListenerAdapter() {
610 @Override
611 public void onAnimationEnd(Animator animation) {
612 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200613 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200614 }
615 });
616 animator.start();
617 mHeightAnimator = animator;
618 }
619
620 private ValueAnimator createHeightAnimator(float targetHeight) {
621 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
622 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
623 @Override
624 public void onAnimationUpdate(ValueAnimator animation) {
625 setExpandedHeight((Float) animation.getAnimatedValue());
626 }
627 });
628 return animator;
629 }
630
631 /**
632 * Gets called when the user performs a click anywhere in the empty area of the panel.
633 *
634 * @return whether the panel will be expanded after the action performed by this method
635 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200636 private boolean onEmptySpaceClick(float x) {
637 if (mHintAnimationRunning) {
638 return true;
639 }
640 if (x < mEdgeTapAreaWidth) {
641 onEdgeClicked(false /* right */);
642 return true;
643 } else if (x > getWidth() - mEdgeTapAreaWidth) {
644 onEdgeClicked(true /* right */);
645 return true;
646 } else {
647 return onMiddleClicked();
648 }
649 }
650
651 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200652 switch (mStatusBar.getBarState()) {
653 case StatusBarState.KEYGUARD:
654 startUnlockHintAnimation();
655 return true;
656 case StatusBarState.SHADE_LOCKED:
657 // TODO: Go to Keyguard again.
658 return true;
659 case StatusBarState.SHADE:
660 collapse();
661 return false;
662 default:
663 return true;
664 }
665 }
666
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200667 protected abstract void onEdgeClicked(boolean right);
668
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500669 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200670 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400671 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500672 + "]",
673 this.getClass().getSimpleName(),
674 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100675 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500676 mClosing?"T":"f",
677 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500678 mJustPeeked?"T":"f",
679 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200680 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500681 ));
682 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200683
684 public abstract void resetViews();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400685}