blob: 64dcb34caad3b91beeee244ffe04faf2f5a23c1c [file] [log] [blame]
Michael Jurka0142d492010-08-25 17:46:15 -07001/*
2 * Copyright (C) 2008 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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Michael Jurka0142d492010-08-25 17:46:15 -070018
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.animation.Interpolator;
22import android.widget.Scroller;
23
Michael Jurka0142d492010-08-25 17:46:15 -070024public abstract class SmoothPagedView extends PagedView {
25 private static final float SMOOTHING_SPEED = 0.75f;
26 private static final float SMOOTHING_CONSTANT = (float) (0.016 / Math.log(SMOOTHING_SPEED));
27
Adam Cohenf34bab52010-09-30 14:11:56 -070028 private float mBaseLineFlingVelocity;
29 private float mFlingVelocityInfluence;
Michael Jurka0142d492010-08-25 17:46:15 -070030
Adam Cohene0f66b52010-11-23 15:06:07 -080031 static final int DEFAULT_MODE = 0;
32 static final int X_LARGE_MODE = 1;
Michael Jurka0142d492010-08-25 17:46:15 -070033
Adam Cohenf34bab52010-09-30 14:11:56 -070034 int mScrollMode;
35
36 private Interpolator mScrollInterpolator;
Michael Jurka0142d492010-08-25 17:46:15 -070037
Winson Chungf0c6ae02012-03-21 16:10:31 -070038 public static class OvershootInterpolator implements Interpolator {
Michael Jurka0142d492010-08-25 17:46:15 -070039 private static final float DEFAULT_TENSION = 1.3f;
40 private float mTension;
41
Winson Chungf0c6ae02012-03-21 16:10:31 -070042 public OvershootInterpolator() {
Michael Jurka0142d492010-08-25 17:46:15 -070043 mTension = DEFAULT_TENSION;
44 }
45
46 public void setDistance(int distance) {
47 mTension = distance > 0 ? DEFAULT_TENSION / distance : DEFAULT_TENSION;
48 }
49
50 public void disableSettle() {
51 mTension = 0.f;
52 }
53
54 public float getInterpolation(float t) {
Michael Jurka0142d492010-08-25 17:46:15 -070055 t -= 1.0f;
56 return t * t * ((mTension + 1) * t + mTension) + 1.0f;
57 }
58 }
59
60 /**
61 * Used to inflate the Workspace from XML.
62 *
63 * @param context The application's context.
64 * @param attrs The attributes set containing the Workspace's customization values.
65 */
66 public SmoothPagedView(Context context, AttributeSet attrs) {
67 this(context, attrs, 0);
68 }
69
70 /**
71 * Used to inflate the Workspace from XML.
72 *
73 * @param context The application's context.
74 * @param attrs The attributes set containing the Workspace's customization values.
75 * @param defStyle Unused.
76 */
77 public SmoothPagedView(Context context, AttributeSet attrs, int defStyle) {
78 super(context, attrs, defStyle);
79
80 mUsePagingTouchSlop = false;
81
82 // This means that we'll take care of updating the scroll parameter ourselves (we do it
Adam Cohene0f66b52010-11-23 15:06:07 -080083 // in computeScroll), we only do this in the OVERSHOOT_MODE, ie. on phones
84 mDeferScrollUpdate = mScrollMode != X_LARGE_MODE;
Michael Jurka0142d492010-08-25 17:46:15 -070085 }
86
Adam Cohenf34bab52010-09-30 14:11:56 -070087 protected int getScrollMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -070088 return X_LARGE_MODE;
Adam Cohenf34bab52010-09-30 14:11:56 -070089 }
90
Michael Jurka0142d492010-08-25 17:46:15 -070091 /**
92 * Initializes various states for this workspace.
93 */
94 @Override
95 protected void init() {
96 super.init();
Adam Cohenf34bab52010-09-30 14:11:56 -070097
98 mScrollMode = getScrollMode();
Adam Cohene0f66b52010-11-23 15:06:07 -080099 if (mScrollMode == DEFAULT_MODE) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700100 mBaseLineFlingVelocity = 2500.0f;
101 mFlingVelocityInfluence = 0.4f;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700102 mScrollInterpolator = new OvershootInterpolator();
Adam Cohene0f66b52010-11-23 15:06:07 -0800103 mScroller = new Scroller(getContext(), mScrollInterpolator);
Adam Cohenf34bab52010-09-30 14:11:56 -0700104 }
Michael Jurka0142d492010-08-25 17:46:15 -0700105 }
106
107 @Override
108 protected void snapToDestination() {
Adam Cohene0f66b52010-11-23 15:06:07 -0800109 if (mScrollMode == X_LARGE_MODE) {
110 super.snapToDestination();
111 } else {
112 snapToPageWithVelocity(getPageNearestToCenterOfScreen(), 0);
113 }
Michael Jurka0142d492010-08-25 17:46:15 -0700114 }
115
116 @Override
117 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800118 if (mScrollMode == X_LARGE_MODE) {
119 super.snapToPageWithVelocity(whichPage, velocity);
120 } else {
121 snapToPageWithVelocity(whichPage, 0, true);
122 }
Michael Jurka0142d492010-08-25 17:46:15 -0700123 }
124
Patrick Dubroy54fa3b92010-11-17 12:18:45 -0800125 private void snapToPageWithVelocity(int whichPage, int velocity, boolean settle) {
Michael Jurka0142d492010-08-25 17:46:15 -0700126 // if (!mScroller.isFinished()) return;
127
128 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
129
130 final int screenDelta = Math.max(1, Math.abs(whichPage - mCurrentPage));
Adam Cohenedb40762013-07-18 16:45:45 -0700131 final int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -0800132 final int delta = newX - mUnboundedScrollX;
Adam Cohene0f66b52010-11-23 15:06:07 -0800133 int duration = (screenDelta + 1) * 100;
Michael Jurka0142d492010-08-25 17:46:15 -0700134
135 if (!mScroller.isFinished()) {
136 mScroller.abortAnimation();
137 }
138
Adam Cohene0f66b52010-11-23 15:06:07 -0800139 if (settle) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700140 ((OvershootInterpolator) mScrollInterpolator).setDistance(screenDelta);
Adam Cohene0f66b52010-11-23 15:06:07 -0800141 } else {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700142 ((OvershootInterpolator) mScrollInterpolator).disableSettle();
Michael Jurka0142d492010-08-25 17:46:15 -0700143 }
144
145 velocity = Math.abs(velocity);
146 if (velocity > 0) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700147 duration += (duration / (velocity / mBaseLineFlingVelocity)) * mFlingVelocityInfluence;
Michael Jurka0142d492010-08-25 17:46:15 -0700148 } else {
149 duration += 100;
150 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700151
Michael Jurka0142d492010-08-25 17:46:15 -0700152 snapToPage(whichPage, delta, duration);
153 }
154
155 @Override
156 protected void snapToPage(int whichPage) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800157 if (mScrollMode == X_LARGE_MODE) {
158 super.snapToPage(whichPage);
159 } else {
160 snapToPageWithVelocity(whichPage, 0, false);
161 }
Michael Jurka0142d492010-08-25 17:46:15 -0700162 }
163
164 @Override
165 public void computeScroll() {
Adam Cohene0f66b52010-11-23 15:06:07 -0800166 if (mScrollMode == X_LARGE_MODE) {
167 super.computeScroll();
168 } else {
169 boolean scrollComputed = computeScrollHelper();
Michael Jurka0142d492010-08-25 17:46:15 -0700170
Adam Cohene0f66b52010-11-23 15:06:07 -0800171 if (!scrollComputed && mTouchState == TOUCH_STATE_SCROLLING) {
172 final float now = System.nanoTime() / NANOTIME_DIV;
173 final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT);
Adam Cohen68d73932010-11-15 10:50:58 -0800174
Adam Cohene0f66b52010-11-23 15:06:07 -0800175 final float dx = mTouchX - mUnboundedScrollX;
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 scrollTo(Math.round(mUnboundedScrollX + dx * e), getScrollY());
Adam Cohene0f66b52010-11-23 15:06:07 -0800177 mSmoothingTime = now;
Michael Jurka0142d492010-08-25 17:46:15 -0700178
Adam Cohene0f66b52010-11-23 15:06:07 -0800179 // Keep generating points as long as we're more than 1px away from the target
180 if (dx > 1.f || dx < -1.f) {
181 invalidate();
182 }
Michael Jurka0142d492010-08-25 17:46:15 -0700183 }
184 }
Michael Jurka0142d492010-08-25 17:46:15 -0700185 }
186}