blob: 642e78d27e43374b87c1cb8c94f7dee9aa4fd97f [file] [log] [blame]
Joe Onorato85a02a82009-09-08 12:34:22 -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
17package com.android.launcher2;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.Message;
22import android.os.SystemClock;
23import android.util.Log;
24import android.util.DisplayMetrics;
25import android.view.MotionEvent;
26import android.view.VelocityTracker;
27import android.view.ViewConfiguration;
28
29import java.util.ArrayList;
30
31public class SwipeController {
32 private static final String TAG = "Launcher.SwipeController";
33
Joe Onorato7bb17492009-09-24 17:51:01 -070034 public static final int MODE_WORKSPACE = 0;
35 public static final int MODE_ALL_APPS = 1;
36 public static final int MODE_ALL_APPS_ZOOMED = 2;
37
Joe Onorato85a02a82009-09-08 12:34:22 -070038 private static final int FRAME_DELAY = 1000 / 30;
39 private static final float DECAY_CONSTANT = 0.65f;
40 private static final float SPRING_CONSTANT = 0.0009f;
41
42 // configuration
Joe Onoratof7b0e012009-10-01 14:09:15 -070043 private int mSlopX;
44 private int mSlopY;
Joe Onorato85a02a82009-09-08 12:34:22 -070045 private float mSwipeDistance;
46
Joe Onorato7bb17492009-09-24 17:51:01 -070047 private AllAppsView mAllAppsView;
48
Joe Onorato85a02a82009-09-08 12:34:22 -070049 // state
50 private VelocityTracker mVelocityTracker;
51 private boolean mCanceled;
52 private boolean mTracking;
53 private int mDownX;
54 private int mDownY;
55
Joe Onorato7bb17492009-09-24 17:51:01 -070056 private int mMode;
57 private int mMinDest;
58 private int mMaxDest;
Joe Onorato85a02a82009-09-08 12:34:22 -070059 private float mAmount;
60
Joe Onorato7bb17492009-09-24 17:51:01 -070061 public SwipeController(Context context) {
Joe Onorato85a02a82009-09-08 12:34:22 -070062 ViewConfiguration config = ViewConfiguration.get(context);
Joe Onoratof7b0e012009-10-01 14:09:15 -070063 mSlopX = config.getScaledTouchSlop();
Joe Onorato08b5dfe2009-10-01 21:47:56 -070064 mSlopY = 4 * mSlopX / 3; // make it 33% more biased towards horizontal swiping.
Joe Onorato85a02a82009-09-08 12:34:22 -070065
66 DisplayMetrics display = context.getResources().getDisplayMetrics();
67 mSwipeDistance = display.heightPixels / 2; // one half of the screen
68
Joe Onorato7bb17492009-09-24 17:51:01 -070069 setMode(MODE_WORKSPACE, false);
Joe Onorato85a02a82009-09-08 12:34:22 -070070 }
71
Joe Onorato7bb17492009-09-24 17:51:01 -070072 public void setAllAppsView(AllAppsView allAppsView) {
73 mAllAppsView = allAppsView;
Joe Onorato85a02a82009-09-08 12:34:22 -070074 }
75
Joe Onorato7bb17492009-09-24 17:51:01 -070076 public void setMode(int mode, boolean animate) {
77 mMinDest = mode - 1;
78 if (mMinDest < MODE_WORKSPACE) {
79 mMinDest = MODE_WORKSPACE;
80 }
81 mMaxDest = mode + 1;
82 if (mMaxDest > MODE_ALL_APPS) { // TODO: support _ZOOMED
83 mMaxDest = MODE_ALL_APPS;
84 }
Joe Onorato85a02a82009-09-08 12:34:22 -070085 mCanceled = true;
Joe Onorato7bb17492009-09-24 17:51:01 -070086 if (mAllAppsView != null) {
87 // TODO: do something with the wallpaper
88 if (animate) {
89 mAllAppsView.setZoomTarget(mode);
90 } else {
91 mAllAppsView.setZoom(mode);
92 }
93 }
94 mMode = mode;
95 }
96
97 /**
98 * Cancels the current swipe, if there is one, and animates back to wherever we were before.
99 */
100 public void cancelSwipe() {
101 mCanceled = true;
102 mAllAppsView.setZoomTarget(mMode);
Joe Onorato85a02a82009-09-08 12:34:22 -0700103 }
104
105 public boolean onInterceptTouchEvent(MotionEvent ev) {
106 onTouchEvent(ev);
Joe Onorato85a02a82009-09-08 12:34:22 -0700107 return mTracking;
108 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700109
Joe Onorato85a02a82009-09-08 12:34:22 -0700110 public boolean onTouchEvent(MotionEvent ev) {
111 if (mVelocityTracker == null) {
112 mVelocityTracker = VelocityTracker.obtain();
113 }
114 mVelocityTracker.addMovement(ev);
115
116 final int screenX = (int)ev.getRawX();
117 final int screenY = (int)ev.getRawY();
118
119 final int deltaX = screenX - mDownX;
120 final int deltaY = screenY - mDownY;
121
122 final int action = ev.getAction();
123 switch (action) {
124 case MotionEvent.ACTION_DOWN:
125 // Remember location of down touch
126 mCanceled = false;
127 mTracking = false;
128 mDownX = screenX;
129 mDownY = screenY;
Joe Onorato360d0352009-09-28 14:37:53 -0400130 mAllAppsView.setZoomSwipeInProgress(true, true);
Joe Onorato85a02a82009-09-08 12:34:22 -0700131 break;
132
133 case MotionEvent.ACTION_MOVE:
134 if (!mCanceled && !mTracking) {
Joe Onoratof7b0e012009-10-01 14:09:15 -0700135 if (Math.abs(deltaX) > mSlopX) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700136 mCanceled = true;
137 mTracking = false;
Joe Onorato360d0352009-09-28 14:37:53 -0400138 mAllAppsView.setZoomSwipeInProgress(false, true);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700139 } else if (Math.abs(deltaY) > mSlopY) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700140 mTracking = true;
141 }
142 }
143 if (mTracking && !mCanceled) {
144 track(screenY);
145 }
146 break;
147
148 case MotionEvent.ACTION_CANCEL:
149 case MotionEvent.ACTION_UP:
150 if (mTracking && !mCanceled) {
151 fling(screenY);
Joe Onorato360d0352009-09-28 14:37:53 -0400152 mAllAppsView.setZoomSwipeInProgress(false, false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700153 }
154 mVelocityTracker.recycle();
155 mVelocityTracker = null;
156 break;
157 }
158
159 return mTracking || mCanceled;
160 }
161
162 private float clamp(float v) {
163 if (v < mMinDest) {
164 return mMinDest;
165 } else if (v > mMaxDest) {
166 return mMaxDest;
167 } else {
168 return v;
169 }
170 }
171
Joe Onorato7bb17492009-09-24 17:51:01 -0700172 private float dist(int screenY) {
173 return clamp(mMode - ((screenY - mDownY) / mSwipeDistance));
174 }
175
Joe Onorato85a02a82009-09-08 12:34:22 -0700176 private void track(int screenY) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700177 mAmount = dist(screenY);
178
179 //Log.d(TAG, "mAmount=" + mAmount);
180 mAllAppsView.setZoom(mAmount);
Joe Onorato85a02a82009-09-08 12:34:22 -0700181 }
182
183 private void fling(int screenY) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700184 mAmount = dist(screenY);
185
Joe Onorato85a02a82009-09-08 12:34:22 -0700186 mVelocityTracker.computeCurrentVelocity(1);
187
Joe Onorato7bb17492009-09-24 17:51:01 -0700188 float velocity = mVelocityTracker.getYVelocity() / mSwipeDistance;
189 int direction = velocity >= 0.0f ? 1 : -1;
190 mAmount = dist(screenY);
Joe Onorato85a02a82009-09-08 12:34:22 -0700191
Joe Onorato7bb17492009-09-24 17:51:01 -0700192 int dest = mMode;
193 if (mMode < mAmount) {
194 if (velocity < 0) { // up
195 dest = mMode + 1;
Joe Onorato85a02a82009-09-08 12:34:22 -0700196 }
197 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700198 if (velocity > 0) { // down
199 dest = mMode - 1;
Joe Onorato85a02a82009-09-08 12:34:22 -0700200 }
201 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700202 // else dest == mMode, so go back to where we started
203
204 //Log.d(TAG, "velocity=" + velocity + " mAmount=" + mAmount + " dest=" + dest);
205 mAllAppsView.setZoomTarget(dest);
206 mMode = dest;
207 mCanceled = true;
Joe Onorato85a02a82009-09-08 12:34:22 -0700208 }
Joe Onorato85a02a82009-09-08 12:34:22 -0700209}
210