summaryrefslogtreecommitdiffstats
path: root/core/java/android/widget/StackView.java
blob: 4cd44d99a777b6440414ba4d362e1cba16aa169f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.widget;

import java.util.WeakHashMap;

import android.animation.PropertyAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.RemoteViews.RemoteView;

@RemoteView
/**
 * A view that displays its children in a stack and allows users to discretely swipe
 * through the children.
 */
public class StackView extends AdapterViewAnimator {
    private final String TAG = "StackView";

    /**
     * Default animation parameters
     */
    private final int DEFAULT_ANIMATION_DURATION = 400;
    private final int MINIMUM_ANIMATION_DURATION = 50;

    /**
     * These specify the different gesture states
     */
    private final int GESTURE_NONE = 0;
    private final int GESTURE_SLIDE_UP = 1;
    private final int GESTURE_SLIDE_DOWN = 2;

    /**
     * Specifies how far you need to swipe (up or down) before it
     * will be consider a completed gesture when you lift your finger
     */
    private final float SWIPE_THRESHOLD_RATIO = 0.35f;
    private final float SLIDE_UP_RATIO = 0.7f;

    private final WeakHashMap<View, Float> mRotations = new WeakHashMap<View, Float>();
    private final WeakHashMap<View, Integer>
            mChildrenToApplyTransformsTo = new WeakHashMap<View, Integer>();

    /**
     * Sentinel value for no current active pointer.
     * Used by {@link #mActivePointerId}.
     */
    private static final int INVALID_POINTER = -1;

    /**
     * These variables are all related to the current state of touch interaction
     * with the stack
     */
    private boolean mGestureComplete = false;
    private float mInitialY;
    private float mInitialX;
    private int mActivePointerId;
    private int mYOffset = 0;
    private int mYVelocity = 0;
    private int mSwipeGestureType = GESTURE_NONE;
    private int mViewHeight;
    private int mSwipeThreshold;
    private int mTouchSlop;
    private int mMaximumVelocity;
    private VelocityTracker mVelocityTracker;

    private boolean mFirstLayoutHappened = false;

    // TODO: temp hack to get this thing started
    int mIndex = 5;

    public StackView(Context context) {
        super(context);
        initStackView();
    }

    public StackView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initStackView();
    }

    private void initStackView() {
        configureViewAnimator(4, 2);
        setStaticTransformationsEnabled(true);
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();// + 5;
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
        mActivePointerId = INVALID_POINTER;
    }

    /**
     * Animate the views between different relative indexes within the {@link AdapterViewAnimator}
     */
    void animateViewForTransition(int fromIndex, int toIndex, View view) {
        if (fromIndex == -1 && toIndex == 0) {
            // Fade item in
            if (view.getAlpha() == 1) {
                view.setAlpha(0);
            }
            PropertyAnimator fadeIn = new PropertyAnimator(DEFAULT_ANIMATION_DURATION,
                    view, "alpha", view.getAlpha(), 1.0f);
            fadeIn.start();
        } else if (fromIndex == mNumActiveViews - 1 && toIndex == mNumActiveViews - 2) {
            // Slide item in
            view.setVisibility(VISIBLE);
            LayoutParams lp = (LayoutParams) view.getLayoutParams();

            int largestDuration = (int) Math.round(
                    (lp.verticalOffset*1.0f/-mViewHeight)*DEFAULT_ANIMATION_DURATION);
            int duration = largestDuration;
            if (mYVelocity != 0) {
                duration = 1000*(0 - lp.verticalOffset)/Math.abs(mYVelocity);
            }

            duration = Math.min(duration, largestDuration);
            duration = Math.max(duration, MINIMUM_ANIMATION_DURATION);

            PropertyAnimator slideDown = new PropertyAnimator(duration, lp,
                    "verticalOffset", lp.verticalOffset, 0);
            slideDown.start();

            PropertyAnimator fadeIn = new PropertyAnimator(duration, view,
                    "alpha", view.getAlpha(), 1.0f);
            fadeIn.start();
        } else if (fromIndex == mNumActiveViews - 2 && toIndex == mNumActiveViews - 1) {
            // Slide item out
            LayoutParams lp = (LayoutParams) view.getLayoutParams();

            int largestDuration = (int) Math.round(
                    (1 - (lp.verticalOffset*1.0f/-mViewHeight))*DEFAULT_ANIMATION_DURATION);
            int duration = largestDuration;
            if (mYVelocity != 0) {
                duration = 1000*(lp.verticalOffset + mViewHeight)/Math.abs(mYVelocity);
            }

            duration = Math.min(duration, largestDuration);
            duration = Math.max(duration, MINIMUM_ANIMATION_DURATION);

            PropertyAnimator slideUp = new PropertyAnimator(duration, lp,
                    "verticalOffset", lp.verticalOffset, -mViewHeight);
            slideUp.start();

            PropertyAnimator fadeOut = new PropertyAnimator(duration, view,
                    "alpha", view.getAlpha(), 0.0f);
            fadeOut.start();
        } else if (fromIndex == -1 && toIndex == mNumActiveViews - 1) {
            // Make sure this view that is "waiting in the wings" is invisible
            view.setAlpha(0.0f);
        } else if (toIndex == -1) {
            // Fade item out
            PropertyAnimator fadeOut = new PropertyAnimator(DEFAULT_ANIMATION_DURATION,
                    view, "alpha", view.getAlpha(), 0);
            fadeOut.start();
        }
    }

    /**
     * Apply any necessary tranforms for the child that is being added.
     */
    void applyTransformForChildAtIndex(View child, int relativeIndex) {
        float rotation;

        if (!mRotations.containsKey(child)) {
            rotation = (float) (Math.random()*26 - 13);
            mRotations.put(child, rotation);
        } else {
            rotation = mRotations.get(child);
        }

        // Child has been removed
        if (relativeIndex == -1) {
            if (mRotations.containsKey(child)) {
                mRotations.remove(child);
            }
            if (mChildrenToApplyTransformsTo.containsKey(child)) {
                mChildrenToApplyTransformsTo.remove(child);
            }
        }

        // if this view is already in the layout, we need to
        // wait until layout has finished in order to set the
        // pivot point of the rotation (requiring getMeasuredWidth/Height())
        mChildrenToApplyTransformsTo.put(child, relativeIndex);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (!mChildrenToApplyTransformsTo.isEmpty()) {
            for (View child: mChildrenToApplyTransformsTo.keySet()) {
                if (mRotations.containsKey(child)) {
                    child.setPivotX(child.getMeasuredWidth()/2);
                    child.setPivotY(child.getMeasuredHeight()/2);
                    child.setRotation(mRotations.get(child));
                }
            }
            mChildrenToApplyTransformsTo.clear();
        }

        if (!mFirstLayoutHappened) {
            mViewHeight = (int) Math.round(SLIDE_UP_RATIO*getMeasuredHeight());
            mSwipeThreshold = (int) Math.round(SWIPE_THRESHOLD_RATIO*mViewHeight);

            // TODO: Right now this walks all the way up the view hierarchy and disables
            // ClipChildren and ClipToPadding. We're probably going  to want to reset
            // these flags as well.
            setClipChildren(false);
            ViewGroup view = this;
            while (view.getParent() != null && view.getParent() instanceof ViewGroup) {
                view = (ViewGroup) view.getParent();
                view.setClipChildren(false);
                view.setClipToPadding(false);
            }

            mFirstLayoutHappened = true;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch(action & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN: {
                if (mActivePointerId == INVALID_POINTER) {
                    mInitialX = ev.getX();
                    mInitialY = ev.getY();
                    mActivePointerId = ev.getPointerId(0);
                }
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                int pointerIndex = ev.findPointerIndex(mActivePointerId);
                if (pointerIndex == INVALID_POINTER) {
                    // no data for our primary pointer, this shouldn't happen, log it
                    Log.d(TAG, "Error: No data for our primary pointer.");
                    return false;
                }

                float newY = ev.getY(pointerIndex);
                float deltaY = newY - mInitialY;

                if ((int) Math.abs(deltaY) > mTouchSlop && mSwipeGestureType == GESTURE_NONE) {
                    mSwipeGestureType = deltaY < 0 ? GESTURE_SLIDE_UP : GESTURE_SLIDE_DOWN;
                    mGestureComplete = false;
                    cancelLongPress();
                    requestDisallowInterceptTouchEvent(true);
                }
                break;
            }
            case MotionEvent.ACTION_POINTER_UP: {
                onSecondaryPointerUp(ev);
                break;
            }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL: {
                mActivePointerId = INVALID_POINTER;
                mSwipeGestureType = GESTURE_NONE;
                mGestureComplete = true;
            }
        }

        return mSwipeGestureType != GESTURE_NONE;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        int pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex == INVALID_POINTER) {
            // no data for our primary pointer, this shouldn't happen, log it
            Log.d(TAG, "Error: No data for our primary pointer.");
            return false;
        }

        float newY = ev.getY(pointerIndex);
        float deltaY = newY - mInitialY;

        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(ev);

        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_MOVE: {
                if ((int) Math.abs(deltaY) > mTouchSlop && mSwipeGestureType == GESTURE_NONE) {
                    mSwipeGestureType = deltaY < 0 ? GESTURE_SLIDE_UP : GESTURE_SLIDE_DOWN;
                    mGestureComplete = false;
                    cancelLongPress();
                    requestDisallowInterceptTouchEvent(true);
                }

                if (!mGestureComplete) {
                    if (mSwipeGestureType == GESTURE_SLIDE_DOWN) {
                        View v = getViewAtRelativeIndex(mNumActiveViews - 1);
                        if (v != null) {
                            // This view is present but hidden, make sure it's visible
                            // if they pull down
                            v.setVisibility(VISIBLE);

                            float r = (deltaY-mTouchSlop)*1.0f / (mSwipeThreshold);
                            mYOffset = Math.min(-mViewHeight + (int)  Math.round(
                                    r*mSwipeThreshold) - mTouchSlop, 0);
                            LayoutParams lp = (LayoutParams) v.getLayoutParams();
                            lp.setVerticalOffset(mYOffset);

                            float alpha = Math.max(0.0f, 1.0f - (1.0f*mYOffset/-mViewHeight));
                            alpha = Math.min(1.0f, alpha);
                            v.setAlpha(alpha);
                        }
                        return true;
                    } else if (mSwipeGestureType == GESTURE_SLIDE_UP) {
                        View v = getViewAtRelativeIndex(mNumActiveViews - 2);

                        if (v != null) {
                            float r = -(deltaY*1.0f + mTouchSlop) / (mSwipeThreshold);
                            mYOffset = Math.min((int) Math.round(r*-mSwipeThreshold), 0);
                            LayoutParams lp = (LayoutParams) v.getLayoutParams();
                            lp.setVerticalOffset(mYOffset);

                            float alpha = Math.max(0.0f, 1.0f - (1.0f*mYOffset/-mViewHeight));
                            alpha = Math.min(1.0f, alpha);
                            v.setAlpha(alpha);
                        }
                        return true;
                    }
                }
                break;
            }
            case MotionEvent.ACTION_UP: {
                handlePointerUp(ev);
                break;
            }
            case MotionEvent.ACTION_POINTER_UP: {
                onSecondaryPointerUp(ev);
                break;
            }
            case MotionEvent.ACTION_CANCEL: {
                mActivePointerId = INVALID_POINTER;
                mGestureComplete = true;
                mSwipeGestureType = GESTURE_NONE;
                mYOffset = 0;
                break;
            }
        }
        return true;
    }

    private final Rect touchRect = new Rect();
    private void onSecondaryPointerUp(MotionEvent ev) {
        final int activePointerIndex = ev.getActionIndex();
        final int pointerId = ev.getPointerId(activePointerIndex);
        if (pointerId == mActivePointerId) {

            int activeViewIndex = (mSwipeGestureType == GESTURE_SLIDE_DOWN) ? mNumActiveViews - 1
                    : mNumActiveViews - 2;

            View v = getViewAtRelativeIndex(activeViewIndex);
            if (v == null) return;

            // Our primary pointer has gone up -- let's see if we can find
            // another pointer on the view. If so, then we should replace
            // our primary pointer with this new pointer and adjust things
            // so that the view doesn't jump
            for (int index = 0; index < ev.getPointerCount(); index++) {
                if (index != activePointerIndex) {

                    float x = ev.getX(index);
                    float y = ev.getY(index);

                    touchRect.set(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
                    if (touchRect.contains((int) Math.round(x), (int) Math.round(y))) {
                        float oldX = ev.getX(activePointerIndex);
                        float oldY = ev.getY(activePointerIndex);

                        // adjust our frame of reference to avoid a jump
                        mInitialY += (y - oldY);
                        mInitialX += (x - oldX);

                        mActivePointerId = ev.getPointerId(index);
                        if (mVelocityTracker != null) {
                            mVelocityTracker.clear();
                        }
                        // ok, we're good, we found a new pointer which is touching the active view
                        return;
                    }
                }
            }
            // if we made it this far, it means we didn't find a satisfactory new pointer :(,
            // so end the
            handlePointerUp(ev);
        }
    }

    private void handlePointerUp(MotionEvent ev) {
        int pointerIndex = ev.findPointerIndex(mActivePointerId);
        float newY = ev.getY(pointerIndex);
        int deltaY = (int) (newY - mInitialY);

        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        mYVelocity = (int) mVelocityTracker.getYVelocity(mActivePointerId);

        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }

        if (deltaY > mSwipeThreshold && mSwipeGestureType == GESTURE_SLIDE_DOWN &&
                !mGestureComplete) {
            // Swipe threshold exceeded, swipe down
            showNext();
        } else if (deltaY < -mSwipeThreshold && mSwipeGestureType == GESTURE_SLIDE_UP &&
                !mGestureComplete) {
            // Swipe threshold exceeded, swipe up
            showPrevious();
        } else if (mSwipeGestureType == GESTURE_SLIDE_UP && !mGestureComplete) {
            // Didn't swipe up far enough, snap back down
            View v = getViewAtRelativeIndex(mNumActiveViews - 2);
            if (v != null) {
                // Compute the animation duration based on how far they pulled it up
                LayoutParams lp = (LayoutParams) v.getLayoutParams();
                int duration = (int) Math.round(
                        lp.verticalOffset*1.0f/-mViewHeight*DEFAULT_ANIMATION_DURATION);
                duration = Math.max(MINIMUM_ANIMATION_DURATION, duration);

                // Animate back down
                PropertyAnimator slideDown = new PropertyAnimator(duration, lp,
                        "verticalOffset", lp.verticalOffset, 0);
                slideDown.start();
                PropertyAnimator fadeIn = new PropertyAnimator(duration, v,
                        "alpha",v.getAlpha(), 1.0f);
                fadeIn.start();
            }
        } else if (mSwipeGestureType == GESTURE_SLIDE_DOWN && !mGestureComplete) {
            // Didn't swipe down far enough, snap back up
            View v = getViewAtRelativeIndex(mNumActiveViews - 1);
            if (v != null) {
                // Compute the animation duration based on how far they pulled it down
                LayoutParams lp = (LayoutParams) v.getLayoutParams();
                int duration = (int) Math.round(
                        (1 - lp.verticalOffset*1.0f/-mViewHeight)*DEFAULT_ANIMATION_DURATION);
                duration = Math.max(MINIMUM_ANIMATION_DURATION, duration);

                // Animate back up
                PropertyAnimator slideUp = new PropertyAnimator(duration, lp,
                        "verticalOffset", lp.verticalOffset, -mViewHeight);
                slideUp.start();
                PropertyAnimator fadeOut = new PropertyAnimator(duration, v,
                        "alpha",v.getAlpha(), 0.0f);
                fadeOut.start();
            }
        }

        mActivePointerId = INVALID_POINTER;
        mGestureComplete = true;
        mSwipeGestureType = GESTURE_NONE;
        mYOffset = 0;
    }

    @Override
    public void onRemoteAdapterConnected() {
        super.onRemoteAdapterConnected();
        setDisplayedChild(mIndex);
    }
}