2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.recents.views;
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.ObjectAnimator;
22 import android.animation.ValueAnimator;
23 import android.content.Context;
24 import android.widget.OverScroller;
25 import com.android.systemui.recents.RecentsConfiguration;
26 import com.android.systemui.recents.misc.Utilities;
28 /* The scrolling logic for a TaskStackView */
29 public class TaskStackViewScroller {
30 public interface TaskStackViewScrollerCallbacks {
31 public void onScrollChanged(float p);
34 RecentsConfiguration mConfig;
35 TaskStackViewLayoutAlgorithm mLayoutAlgorithm;
36 TaskStackViewScrollerCallbacks mCb;
40 OverScroller mScroller;
41 ObjectAnimator mScrollAnimator;
42 float mFinalAnimatedScroll;
44 public TaskStackViewScroller(Context context, RecentsConfiguration config, TaskStackViewLayoutAlgorithm layoutAlgorithm) {
46 mScroller = new OverScroller(context);
47 mLayoutAlgorithm = layoutAlgorithm;
48 setStackScroll(getStackScroll());
51 /** Resets the task scroller. */
56 /** Sets the callbacks */
57 void setCallbacks(TaskStackViewScrollerCallbacks cb) {
61 /** Gets the current stack scroll */
62 public float getStackScroll() {
66 /** Sets the current stack scroll */
67 public void setStackScroll(float s) {
70 mCb.onScrollChanged(mStackScrollP);
74 /** Sets the current stack scroll without calling the callback. */
75 void setStackScrollRaw(float s) {
80 * Sets the current stack scroll to the initial state when you first enter recents.
81 * @return whether the stack progress changed.
83 public boolean setStackScrollToInitialState() {
84 float prevStackScrollP = mStackScrollP;
85 setStackScroll(getBoundedStackScroll(mLayoutAlgorithm.mInitialScrollP));
86 return Float.compare(prevStackScrollP, mStackScrollP) != 0;
89 /** Bounds the current scroll if necessary */
90 public boolean boundScroll() {
91 float curScroll = getStackScroll();
92 float newScroll = getBoundedStackScroll(curScroll);
93 if (Float.compare(newScroll, curScroll) != 0) {
94 setStackScroll(newScroll);
100 /** Returns the bounded stack scroll */
101 float getBoundedStackScroll(float scroll) {
102 return Math.max(mLayoutAlgorithm.mMinScrollP, Math.min(mLayoutAlgorithm.mMaxScrollP, scroll));
105 /** Returns the amount that the absolute value of how much the scroll is out of bounds. */
106 float getScrollAmountOutOfBounds(float scroll) {
107 if (scroll < mLayoutAlgorithm.mMinScrollP) {
108 return Math.abs(scroll - mLayoutAlgorithm.mMinScrollP);
109 } else if (scroll > mLayoutAlgorithm.mMaxScrollP) {
110 return Math.abs(scroll - mLayoutAlgorithm.mMaxScrollP);
115 /** Returns whether the specified scroll is out of bounds */
116 boolean isScrollOutOfBounds() {
117 return Float.compare(getScrollAmountOutOfBounds(mStackScrollP), 0f) != 0;
120 /** Animates the stack scroll into bounds */
121 ObjectAnimator animateBoundScroll() {
122 float curScroll = getStackScroll();
123 float newScroll = getBoundedStackScroll(curScroll);
124 if (Float.compare(newScroll, curScroll) != 0) {
125 // Start a new scroll animation
126 animateScroll(curScroll, newScroll, null);
128 return mScrollAnimator;
131 /** Animates the stack scroll */
132 void animateScroll(float curScroll, float newScroll, final Runnable postRunnable) {
133 // Finish any current scrolling animations
134 if (mScrollAnimator != null && mScrollAnimator.isRunning()) {
135 setStackScroll(mFinalAnimatedScroll);
136 mScroller.startScroll(0, progressToScrollRange(mFinalAnimatedScroll), 0, 0, 0);
139 stopBoundScrollAnimation();
141 mFinalAnimatedScroll = newScroll;
142 mScrollAnimator = ObjectAnimator.ofFloat(this, "stackScroll", curScroll, newScroll);
143 mScrollAnimator.setDuration(mConfig.taskStackScrollDuration);
144 mScrollAnimator.setInterpolator(mConfig.linearOutSlowInInterpolator);
145 mScrollAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
147 public void onAnimationUpdate(ValueAnimator animation) {
148 setStackScroll((Float) animation.getAnimatedValue());
151 mScrollAnimator.addListener(new AnimatorListenerAdapter() {
153 public void onAnimationEnd(Animator animation) {
154 if (postRunnable != null) {
157 mScrollAnimator.removeAllListeners();
160 mScrollAnimator.start();
163 /** Aborts any current stack scrolls */
164 void stopBoundScrollAnimation() {
165 Utilities.cancelAnimationWithoutCallbacks(mScrollAnimator);
168 /**** OverScroller ****/
170 int progressToScrollRange(float p) {
171 return (int) (p * mLayoutAlgorithm.mStackVisibleRect.height());
174 float scrollRangeToProgress(int s) {
175 return (float) s / mLayoutAlgorithm.mStackVisibleRect.height();
178 /** Called from the view draw, computes the next scroll. */
179 boolean computeScroll() {
180 if (mScroller.computeScrollOffset()) {
181 float scroll = scrollRangeToProgress(mScroller.getCurrY());
182 setStackScrollRaw(scroll);
184 mCb.onScrollChanged(scroll);
191 /** Returns whether the overscroller is scrolling. */
192 boolean isScrolling() {
193 return !mScroller.isFinished();
196 /** Stops the scroller and any current fling. */
197 void stopScroller() {
198 if (!mScroller.isFinished()) {
199 mScroller.abortAnimation();