blob: 772d0e7e9a3b60f1008f07003022e89dbb876275 [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();
Daniel Sandlere7c5bbb2013-03-05 13:36:21 -050044
John Spurlock97642182013-07-29 17:58:39 -040045 private final void logf(String fmt, Object... args) {
John Spurlockcd686b52013-06-05 10:13:46 -040046 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args));
Daniel Sandler08d05e32012-08-08 16:39:54 -040047 }
48
Jorim Jaggi90129582014-06-02 14:44:49 +020049 protected PhoneStatusBar mStatusBar;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040050 private float mPeekHeight;
Jorim Jaggi90129582014-06-02 14:44:49 +020051 private float mHintDistance;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020052 private int mEdgeTapAreaWidth;
Selim Cinekb6d85eb2014-03-28 20:21:01 +010053 private float mInitialOffsetOnTouch;
Daniel Sandler08d05e32012-08-08 16:39:54 -040054 private float mExpandedFraction = 0;
Selim Cinek1408eb52014-06-02 14:45:38 +020055 protected float mExpandedHeight = 0;
Daniel Sandler0c1b75c2012-10-04 12:08:54 -040056 private boolean mJustPeeked;
Daniel Sandler50508132012-08-16 14:10:53 -040057 private boolean mClosing;
Jorim Jaggi8dd95e02014-06-03 16:19:33 +020058 protected boolean mTracking;
Jorim Jaggi90129582014-06-02 14:44:49 +020059 private boolean mTouchSlopExceeded;
John Spurlock48fa91a2013-08-15 09:29:31 -040060 private int mTrackingPointer;
Jorim Jaggid7daab72014-05-06 22:22:20 +020061 protected int mTouchSlop;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +020062 protected boolean mHintAnimationRunning;
Jorim Jaggi47c85a32014-06-05 17:25:40 +020063 private boolean mOverExpandedBeforeFling;
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 Jaggi787a0af2014-06-04 18:57:14 +0200165 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning);
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 Jaggi787a0af2014-06-04 18:57:14 +0200222 if ((mTracking && mTouchSlopExceeded)
223 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200224 float vel = getCurrentVelocity();
225 boolean expand = flingExpands(vel);
226 onTrackingStopped(expand);
227 fling(vel, expand);
228 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200229 boolean expands = onEmptySpaceClick(mInitialTouchX);
Jorim Jaggi90129582014-06-02 14:44:49 +0200230 onTrackingStopped(expands);
231 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100232 if (mVelocityTracker != null) {
233 mVelocityTracker.recycle();
234 mVelocityTracker = null;
235 }
236 break;
237 }
Selim Cinek4c6969a2014-05-26 19:22:17 +0200238 return !waitForTouchSlop || mTracking;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100239 }
240
Selim Cinek4c6969a2014-05-26 19:22:17 +0200241 protected abstract boolean hasConflictingGestures();
242
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200243 protected void onTrackingStopped(boolean expand) {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200244 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200245 mBar.onTrackingStopped(PanelView.this, expand);
Selim Cinek1685e632014-04-08 02:27:49 +0200246 }
247
248 protected void onTrackingStarted() {
Selim Cinek4c6969a2014-05-26 19:22:17 +0200249 mTracking = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200250 mBar.onTrackingStarted(PanelView.this);
251 onExpandingStarted();
252 }
253
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100254 private float getCurrentVelocity() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100255
256 // the velocitytracker might be null if we got a bad input stream
257 if (mVelocityTracker == null) {
258 return 0;
259 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100260 mVelocityTracker.computeCurrentVelocity(1000);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200261 return mVelocityTracker.getYVelocity();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100262 }
263
264 @Override
265 public boolean onInterceptTouchEvent(MotionEvent event) {
266
267 /*
268 * If the user drags anywhere inside the panel we intercept it if he moves his finger
269 * upwards. This allows closing the shade from anywhere inside the panel.
270 *
271 * We only do this if the current content is scrolled to the bottom,
272 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
273 * possible.
274 */
275 int pointerIndex = event.findPointerIndex(mTrackingPointer);
276 if (pointerIndex < 0) {
277 pointerIndex = 0;
278 mTrackingPointer = event.getPointerId(pointerIndex);
279 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200280 final float x = event.getX(pointerIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100281 final float y = event.getY(pointerIndex);
282 boolean scrolledToBottom = isScrolledToBottom();
283
284 switch (event.getActionMasked()) {
285 case MotionEvent.ACTION_DOWN:
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200286 if (mHeightAnimator != null && !mHintAnimationRunning) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200287 mHeightAnimator.cancel(); // end any outstanding animations
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200288 mTouchSlopExceeded = true;
Selim Cinek172e9142014-05-07 19:38:00 +0200289 return true;
290 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100291 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200292 mInitialTouchX = x;
Jorim Jaggi90129582014-06-02 14:44:49 +0200293 mTouchSlopExceeded = false;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100294 initVelocityTracker();
295 trackMovement(event);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100296 break;
297 case MotionEvent.ACTION_POINTER_UP:
298 final int upPointer = event.getPointerId(event.getActionIndex());
299 if (mTrackingPointer == upPointer) {
300 // gesture is ongoing, find a new pointer to track
301 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
302 mTrackingPointer = event.getPointerId(newIndex);
Jorim Jaggia6310292014-04-16 14:11:52 +0200303 mInitialTouchX = event.getX(newIndex);
304 mInitialTouchY = event.getY(newIndex);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100305 }
306 break;
307
308 case MotionEvent.ACTION_MOVE:
309 final float h = y - mInitialTouchY;
310 trackMovement(event);
311 if (scrolledToBottom) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200312 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200313 if (mHeightAnimator != null) {
314 mHeightAnimator.cancel();
315 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100316 mInitialOffsetOnTouch = mExpandedHeight;
317 mInitialTouchY = y;
Jorim Jaggia6310292014-04-16 14:11:52 +0200318 mInitialTouchX = x;
Selim Cinek91c39ef2014-04-07 21:18:09 +0200319 mTracking = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200320 mTouchSlopExceeded = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200321 onTrackingStarted();
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100322 return true;
323 }
324 }
325 break;
326 }
327 return false;
328 }
329
330 private void initVelocityTracker() {
331 if (mVelocityTracker != null) {
332 mVelocityTracker.recycle();
333 }
Jorim Jaggib7b61dd2014-05-21 15:45:07 +0200334 mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100335 }
336
337 protected boolean isScrolledToBottom() {
Selim Cinek172e9142014-05-07 19:38:00 +0200338 return true;
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100339 }
340
341 protected float getContentHeight() {
342 return mExpandedHeight;
Daniel Sandlerbf526d12012-09-04 22:56:44 -0400343 }
344
Daniel Sandler08d05e32012-08-08 16:39:54 -0400345 @Override
346 protected void onFinishInflate() {
347 super.onFinishInflate();
348 loadDimens();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400349 }
350
Jorim Jaggi069cd032014-05-15 03:09:01 +0200351 @Override
352 protected void onConfigurationChanged(Configuration newConfig) {
353 super.onConfigurationChanged(newConfig);
354 loadDimens();
355 mMaxPanelHeight = -1;
356 }
357
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200358 /**
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200359 * @param vel the current velocity of the motion
360 * @return whether a fling should expands the panel; contracts otherwise
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200361 */
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200362 private boolean flingExpands(float vel) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200363 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200364 return getExpandedFraction() > 0.5f;
Selim Cinek1685e632014-04-08 02:27:49 +0200365 } else {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200366 return vel > 0;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400367 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200368 }
369
370 protected void fling(float vel, boolean expand) {
371 cancelPeek();
372 float target = expand ? getMaxPanelHeight() : 0.0f;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200373 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200374 onExpandingFinished();
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200375 mBar.panelExpansionChanged(this, mExpandedFraction);
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200376 return;
377 }
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200378 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
Jorim Jaggi90129582014-06-02 14:44:49 +0200379 ValueAnimator animator = createHeightAnimator(target);
Jorim Jaggi1d480692014-05-20 19:41:58 +0200380 if (expand) {
381 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
382 } else {
383 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
384 getHeight());
385
386 // Make it shorter if we run a canned animation
387 if (vel == 0) {
388 animator.setDuration((long) (animator.getDuration() / 1.75f));
389 }
390 }
Jorim Jaggi1d480692014-05-20 19:41:58 +0200391 animator.addListener(new AnimatorListenerAdapter() {
392 @Override
393 public void onAnimationEnd(Animator animation) {
394 mHeightAnimator = null;
395 onExpandingFinished();
396 }
397 });
Jorim Jaggi1d480692014-05-20 19:41:58 +0200398 mHeightAnimator = animator;
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200399 animator.start();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400400 }
401
402 @Override
403 protected void onAttachedToWindow() {
404 super.onAttachedToWindow();
Daniel Sandler978f8532012-08-15 15:48:16 -0400405 mViewName = getResources().getResourceName(getId());
406 }
407
408 public String getName() {
409 return mViewName;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400410 }
411
Daniel Sandler08d05e32012-08-08 16:39:54 -0400412 @Override
413 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
414 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
415
John Spurlock97642182013-07-29 17:58:39 -0400416 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
Daniel Sandler08d05e32012-08-08 16:39:54 -0400417 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
Daniel Sandler198a0302012-08-17 16:04:31 -0400418
419 // Did one of our children change size?
420 int newHeight = getMeasuredHeight();
Selim Cinekb84a1072014-05-15 19:10:18 +0200421 if (newHeight > mMaxPanelHeight) {
422 // we only adapt the max height if it's bigger
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100423 mMaxPanelHeight = newHeight;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200424 // If the user isn't actively poking us, let's rubberband to the content
Jorim Jaggi1d480692014-05-20 19:41:58 +0200425 if (!mTracking && mHeightAnimator == null
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200426 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
427 && mMaxPanelHeight > 0) {
428 mExpandedHeight = mMaxPanelHeight;
429 }
Daniel Sandler50508132012-08-16 14:10:53 -0400430 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400431 }
432
Daniel Sandler08d05e32012-08-08 16:39:54 -0400433 public void setExpandedHeight(float height) {
John Spurlock97642182013-07-29 17:58:39 -0400434 if (DEBUG) logf("setExpandedHeight(%.1f)", height);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200435 setExpandedHeightInternal(height + getOverExpansionPixels());
Daniel Sandler0c1b75c2012-10-04 12:08:54 -0400436 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400437 }
438
Daniel Sandler50508132012-08-16 14:10:53 -0400439 @Override
440 protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100441 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
442 (int)mExpandedHeight, mMaxPanelHeight);
Daniel Sandler50508132012-08-16 14:10:53 -0400443 super.onLayout(changed, left, top, right, bottom);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100444 requestPanelHeightUpdate();
445 }
446
447 protected void requestPanelHeightUpdate() {
448 float currentMaxPanelHeight = getMaxPanelHeight();
449
450 // If the user isn't actively poking us, let's update the height
Jorim Jaggi1d480692014-05-20 19:41:58 +0200451 if (!mTracking && mHeightAnimator == null
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100452 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200453 setExpandedHeight(currentMaxPanelHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100454 }
Daniel Sandler50508132012-08-16 14:10:53 -0400455 }
456
Daniel Sandler08d05e32012-08-08 16:39:54 -0400457 public void setExpandedHeightInternal(float h) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200458 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
459 if (mHeightAnimator == null) {
460 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
461 if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
462 setOverExpansion(overExpansionPixels, true /* isPixels */);
463 }
464 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
465 } else {
466 mExpandedHeight = h;
467 if (mOverExpandedBeforeFling) {
468 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
469 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100470 }
Daniel Sandler198a0302012-08-17 16:04:31 -0400471
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100472 onHeightUpdated(mExpandedHeight);
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200473 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
474 ? 0
475 : mExpandedHeight / fhWithoutOverExpansion);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400476 }
477
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200478 protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
Selim Cinek24120a52014-05-26 10:05:42 +0200479
Jorim Jaggi90129582014-06-02 14:44:49 +0200480 protected abstract void onHeightUpdated(float expandedHeight);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100481
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200482 protected abstract float getOverExpansionAmount();
483
484 protected abstract float getOverExpansionPixels();
485
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100486 /**
487 * This returns the maximum height of the panel. Children should override this if their
488 * desired height is not the full height.
489 *
490 * @return the default implementation simply returns the maximum height.
491 */
492 protected int getMaxPanelHeight() {
Selim Cinekb84a1072014-05-15 19:10:18 +0200493 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100494 return mMaxPanelHeight;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400495 }
496
497 public void setExpandedFraction(float frac) {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100498 setExpandedHeight(getMaxPanelHeight() * frac);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400499 }
500
501 public float getExpandedHeight() {
502 return mExpandedHeight;
503 }
504
505 public float getExpandedFraction() {
506 return mExpandedFraction;
507 }
508
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700509 public boolean isFullyExpanded() {
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100510 return mExpandedHeight >= getMaxPanelHeight();
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700511 }
512
513 public boolean isFullyCollapsed() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400514 return mExpandedHeight <= 0;
515 }
516
517 public boolean isCollapsing() {
518 return mClosing;
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700519 }
520
John Spurlocka4b70af2013-08-17 14:05:49 -0400521 public boolean isTracking() {
522 return mTracking;
523 }
524
Daniel Sandler08d05e32012-08-08 16:39:54 -0400525 public void setBar(PanelBar panelBar) {
526 mBar = panelBar;
527 }
528
Daniel Sandler08d05e32012-08-08 16:39:54 -0400529 public void collapse() {
530 // TODO: abort animation or ongoing touch
John Spurlock97642182013-07-29 17:58:39 -0400531 if (DEBUG) logf("collapse: " + this);
Daniel Sandlerb4e56ed2012-09-12 23:07:44 -0700532 if (!isFullyCollapsed()) {
Jorim Jaggi1d480692014-05-20 19:41:58 +0200533 if (mHeightAnimator != null) {
534 mHeightAnimator.cancel();
535 }
Daniel Sandler750bb9b2012-10-03 16:24:00 -0400536 mClosing = true;
Selim Cinek1685e632014-04-08 02:27:49 +0200537 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200538 fling(0, false /* expand */);
Daniel Sandler08d05e32012-08-08 16:39:54 -0400539 }
540 }
541
542 public void expand() {
John Spurlock97642182013-07-29 17:58:39 -0400543 if (DEBUG) logf("expand: " + this);
Daniel Sandler198a0302012-08-17 16:04:31 -0400544 if (isFullyCollapsed()) {
545 mBar.startOpeningPanel(this);
Selim Cinek1685e632014-04-08 02:27:49 +0200546 onExpandingStarted();
Jorim Jaggi1d480692014-05-20 19:41:58 +0200547 fling(0, true /* expand */);
Daniel Sandler198a0302012-08-17 16:04:31 -0400548 } else if (DEBUG) {
John Spurlock97642182013-07-29 17:58:39 -0400549 if (DEBUG) logf("skipping expansion: is expanded");
550 }
551 }
552
553 public void cancelPeek() {
554 if (mPeekAnimator != null && mPeekAnimator.isStarted()) {
555 mPeekAnimator.cancel();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400556 }
557 }
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500558
Jorim Jaggi90129582014-06-02 14:44:49 +0200559 protected void startUnlockHintAnimation() {
560
561 // We don't need to hint the user if an animation is already running or the user is changing
562 // the expansion.
563 if (mHeightAnimator != null || mTracking) {
564 return;
565 }
566 cancelPeek();
567 onExpandingStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200568 startUnlockHintAnimationPhase1(new Runnable() {
569 @Override
570 public void run() {
571 onExpandingFinished();
572 mStatusBar.onHintFinished();
573 mHintAnimationRunning = false;
574 }
575 });
Jorim Jaggi90129582014-06-02 14:44:49 +0200576 mStatusBar.onUnlockHintStarted();
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200577 mHintAnimationRunning = true;
Jorim Jaggi90129582014-06-02 14:44:49 +0200578 }
579
580 /**
581 * Phase 1: Move everything upwards.
582 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200583 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200584 float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
585 ValueAnimator animator = createHeightAnimator(target);
586 animator.setDuration(250);
587 animator.setInterpolator(mLinearOutSlowInInterpolator);
588 animator.addListener(new AnimatorListenerAdapter() {
589 private boolean mCancelled;
590
591 @Override
592 public void onAnimationCancel(Animator animation) {
593 mCancelled = true;
594 }
595
596 @Override
597 public void onAnimationEnd(Animator animation) {
598 if (mCancelled) {
599 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200600 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200601 } else {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200602 startUnlockHintAnimationPhase2(onAnimationFinished);
Jorim Jaggi90129582014-06-02 14:44:49 +0200603 }
604 }
605 });
606 animator.start();
607 mHeightAnimator = animator;
608 }
609
610 /**
611 * Phase 2: Bounce down.
612 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200613 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
Jorim Jaggi90129582014-06-02 14:44:49 +0200614 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
615 animator.setDuration(450);
616 animator.setInterpolator(mBounceInterpolator);
617 animator.addListener(new AnimatorListenerAdapter() {
618 @Override
619 public void onAnimationEnd(Animator animation) {
620 mHeightAnimator = null;
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200621 onAnimationFinished.run();
Jorim Jaggi90129582014-06-02 14:44:49 +0200622 }
623 });
624 animator.start();
625 mHeightAnimator = animator;
626 }
627
628 private ValueAnimator createHeightAnimator(float targetHeight) {
629 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
630 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
631 @Override
632 public void onAnimationUpdate(ValueAnimator animation) {
Jorim Jaggi47c85a32014-06-05 17:25:40 +0200633 setExpandedHeightInternal((Float) animation.getAnimatedValue());
634 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction);
Jorim Jaggi90129582014-06-02 14:44:49 +0200635 }
636 });
637 return animator;
638 }
639
640 /**
641 * Gets called when the user performs a click anywhere in the empty area of the panel.
642 *
643 * @return whether the panel will be expanded after the action performed by this method
644 */
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200645 private boolean onEmptySpaceClick(float x) {
646 if (mHintAnimationRunning) {
647 return true;
648 }
Jorim Jaggi6539a832014-06-03 23:33:09 +0200649 if (x < mEdgeTapAreaWidth
650 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200651 onEdgeClicked(false /* right */);
652 return true;
Jorim Jaggi6539a832014-06-03 23:33:09 +0200653 } else if (x > getWidth() - mEdgeTapAreaWidth
654 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200655 onEdgeClicked(true /* right */);
656 return true;
657 } else {
658 return onMiddleClicked();
659 }
660 }
661
662 private boolean onMiddleClicked() {
Jorim Jaggi90129582014-06-02 14:44:49 +0200663 switch (mStatusBar.getBarState()) {
664 case StatusBarState.KEYGUARD:
665 startUnlockHintAnimation();
666 return true;
667 case StatusBarState.SHADE_LOCKED:
Jorim Jaggi6539a832014-06-03 23:33:09 +0200668 mStatusBar.goToKeyguard();
Jorim Jaggi90129582014-06-02 14:44:49 +0200669 return true;
670 case StatusBarState.SHADE:
671 collapse();
672 return false;
673 default:
674 return true;
675 }
676 }
677
Jorim Jaggib3f0a2f2014-06-02 19:29:39 +0200678 protected abstract void onEdgeClicked(boolean right);
679
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500680 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Selim Cineka3e5bcb2014-04-07 20:07:22 +0200681 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
John Spurlock50728832014-04-17 19:05:28 -0400682 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500683 + "]",
684 this.getClass().getSimpleName(),
685 getExpandedHeight(),
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100686 getMaxPanelHeight(),
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500687 mClosing?"T":"f",
688 mTracking?"T":"f",
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500689 mJustPeeked?"T":"f",
690 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
Jorim Jaggi1d480692014-05-20 19:41:58 +0200691 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
Daniel Sandler37a38aa2013-02-13 17:15:57 -0500692 ));
693 }
Selim Cinek3c4635c2014-05-29 02:12:47 +0200694
695 public abstract void resetViews();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400696}