blob: 4e331aa2c877083d23e0eb6678e8ad7f28be46ad [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;
Michael Jurka0142d492010-08-25 17:46:15 -070022
Michael Jurka0142d492010-08-25 17:46:15 -070023public abstract class SmoothPagedView extends PagedView {
24 private static final float SMOOTHING_SPEED = 0.75f;
25 private static final float SMOOTHING_CONSTANT = (float) (0.016 / Math.log(SMOOTHING_SPEED));
26
Adam Cohenf34bab52010-09-30 14:11:56 -070027 private float mBaseLineFlingVelocity;
28 private float mFlingVelocityInfluence;
Michael Jurka0142d492010-08-25 17:46:15 -070029
Adam Cohene0f66b52010-11-23 15:06:07 -080030 static final int DEFAULT_MODE = 0;
31 static final int X_LARGE_MODE = 1;
Michael Jurka0142d492010-08-25 17:46:15 -070032
Adam Cohenf34bab52010-09-30 14:11:56 -070033 int mScrollMode;
34
35 private Interpolator mScrollInterpolator;
Michael Jurka0142d492010-08-25 17:46:15 -070036
Winson Chungf0c6ae02012-03-21 16:10:31 -070037 public static class OvershootInterpolator implements Interpolator {
Michael Jurka0142d492010-08-25 17:46:15 -070038 private static final float DEFAULT_TENSION = 1.3f;
39 private float mTension;
40
Winson Chungf0c6ae02012-03-21 16:10:31 -070041 public OvershootInterpolator() {
Michael Jurka0142d492010-08-25 17:46:15 -070042 mTension = DEFAULT_TENSION;
43 }
44
45 public void setDistance(int distance) {
46 mTension = distance > 0 ? DEFAULT_TENSION / distance : DEFAULT_TENSION;
47 }
48
49 public void disableSettle() {
50 mTension = 0.f;
51 }
52
53 public float getInterpolation(float t) {
Michael Jurka0142d492010-08-25 17:46:15 -070054 t -= 1.0f;
55 return t * t * ((mTension + 1) * t + mTension) + 1.0f;
56 }
57 }
58
59 /**
60 * Used to inflate the Workspace from XML.
61 *
62 * @param context The application's context.
63 * @param attrs The attributes set containing the Workspace's customization values.
64 */
65 public SmoothPagedView(Context context, AttributeSet attrs) {
66 this(context, attrs, 0);
67 }
68
69 /**
70 * Used to inflate the Workspace from XML.
71 *
72 * @param context The application's context.
73 * @param attrs The attributes set containing the Workspace's customization values.
74 * @param defStyle Unused.
75 */
76 public SmoothPagedView(Context context, AttributeSet attrs, int defStyle) {
77 super(context, attrs, defStyle);
78
79 mUsePagingTouchSlop = false;
80
81 // This means that we'll take care of updating the scroll parameter ourselves (we do it
Adam Cohene0f66b52010-11-23 15:06:07 -080082 // in computeScroll), we only do this in the OVERSHOOT_MODE, ie. on phones
83 mDeferScrollUpdate = mScrollMode != X_LARGE_MODE;
Michael Jurka0142d492010-08-25 17:46:15 -070084 }
85
Adam Cohenf34bab52010-09-30 14:11:56 -070086 protected int getScrollMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -070087 return X_LARGE_MODE;
Adam Cohenf34bab52010-09-30 14:11:56 -070088 }
89
Michael Jurka0142d492010-08-25 17:46:15 -070090 /**
91 * Initializes various states for this workspace.
92 */
93 @Override
94 protected void init() {
95 super.init();
Adam Cohenf34bab52010-09-30 14:11:56 -070096
97 mScrollMode = getScrollMode();
Adam Cohene0f66b52010-11-23 15:06:07 -080098 if (mScrollMode == DEFAULT_MODE) {
Adam Cohenf34bab52010-09-30 14:11:56 -070099 mBaseLineFlingVelocity = 2500.0f;
100 mFlingVelocityInfluence = 0.4f;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700101 mScrollInterpolator = new OvershootInterpolator();
Adam Cohenf9618852013-11-08 06:45:03 -0800102 setDefaultInterpolator(mScrollInterpolator);
Adam Cohenf34bab52010-09-30 14:11:56 -0700103 }
Michael Jurka0142d492010-08-25 17:46:15 -0700104 }
105
106 @Override
107 protected void snapToDestination() {
Adam Cohene0f66b52010-11-23 15:06:07 -0800108 if (mScrollMode == X_LARGE_MODE) {
109 super.snapToDestination();
110 } else {
111 snapToPageWithVelocity(getPageNearestToCenterOfScreen(), 0);
112 }
Michael Jurka0142d492010-08-25 17:46:15 -0700113 }
114
115 @Override
116 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800117 if (mScrollMode == X_LARGE_MODE) {
118 super.snapToPageWithVelocity(whichPage, velocity);
119 } else {
120 snapToPageWithVelocity(whichPage, 0, true);
121 }
Michael Jurka0142d492010-08-25 17:46:15 -0700122 }
123
Patrick Dubroy54fa3b92010-11-17 12:18:45 -0800124 private void snapToPageWithVelocity(int whichPage, int velocity, boolean settle) {
Michael Jurka0142d492010-08-25 17:46:15 -0700125 // if (!mScroller.isFinished()) return;
126
127 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
128
129 final int screenDelta = Math.max(1, Math.abs(whichPage - mCurrentPage));
Adam Cohenedb40762013-07-18 16:45:45 -0700130 final int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -0800131 final int delta = newX - mUnboundedScrollX;
Adam Cohene0f66b52010-11-23 15:06:07 -0800132 int duration = (screenDelta + 1) * 100;
Michael Jurka0142d492010-08-25 17:46:15 -0700133
134 if (!mScroller.isFinished()) {
135 mScroller.abortAnimation();
136 }
137
Adam Cohene0f66b52010-11-23 15:06:07 -0800138 if (settle) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700139 ((OvershootInterpolator) mScrollInterpolator).setDistance(screenDelta);
Adam Cohene0f66b52010-11-23 15:06:07 -0800140 } else {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700141 ((OvershootInterpolator) mScrollInterpolator).disableSettle();
Michael Jurka0142d492010-08-25 17:46:15 -0700142 }
143
144 velocity = Math.abs(velocity);
145 if (velocity > 0) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700146 duration += (duration / (velocity / mBaseLineFlingVelocity)) * mFlingVelocityInfluence;
Michael Jurka0142d492010-08-25 17:46:15 -0700147 } else {
148 duration += 100;
149 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700150
Michael Jurka0142d492010-08-25 17:46:15 -0700151 snapToPage(whichPage, delta, duration);
152 }
153
154 @Override
155 protected void snapToPage(int whichPage) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800156 if (mScrollMode == X_LARGE_MODE) {
157 super.snapToPage(whichPage);
158 } else {
159 snapToPageWithVelocity(whichPage, 0, false);
160 }
Michael Jurka0142d492010-08-25 17:46:15 -0700161 }
162
163 @Override
164 public void computeScroll() {
Adam Cohene0f66b52010-11-23 15:06:07 -0800165 if (mScrollMode == X_LARGE_MODE) {
166 super.computeScroll();
167 } else {
168 boolean scrollComputed = computeScrollHelper();
Michael Jurka0142d492010-08-25 17:46:15 -0700169
Adam Cohene0f66b52010-11-23 15:06:07 -0800170 if (!scrollComputed && mTouchState == TOUCH_STATE_SCROLLING) {
171 final float now = System.nanoTime() / NANOTIME_DIV;
172 final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT);
Adam Cohen68d73932010-11-15 10:50:58 -0800173
Adam Cohene0f66b52010-11-23 15:06:07 -0800174 final float dx = mTouchX - mUnboundedScrollX;
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 scrollTo(Math.round(mUnboundedScrollX + dx * e), getScrollY());
Adam Cohene0f66b52010-11-23 15:06:07 -0800176 mSmoothingTime = now;
Michael Jurka0142d492010-08-25 17:46:15 -0700177
Adam Cohene0f66b52010-11-23 15:06:07 -0800178 // Keep generating points as long as we're more than 1px away from the target
179 if (dx > 1.f || dx < -1.f) {
180 invalidate();
181 }
Michael Jurka0142d492010-08-25 17:46:15 -0700182 }
183 }
Michael Jurka0142d492010-08-25 17:46:15 -0700184 }
185}