blob: 7617fe056db78970c1ace264259a9bfa4eecd199 [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 Onorato85a02a82009-09-08 12:34:22 -070043 private int mSlop;
44 private float mSwipeDistance;
45
Joe Onorato7bb17492009-09-24 17:51:01 -070046 private AllAppsView mAllAppsView;
47
Joe Onorato85a02a82009-09-08 12:34:22 -070048 // state
49 private VelocityTracker mVelocityTracker;
50 private boolean mCanceled;
51 private boolean mTracking;
52 private int mDownX;
53 private int mDownY;
54
Joe Onorato7bb17492009-09-24 17:51:01 -070055 private int mMode;
56 private int mMinDest;
57 private int mMaxDest;
Joe Onorato85a02a82009-09-08 12:34:22 -070058 private float mAmount;
59
Joe Onorato7bb17492009-09-24 17:51:01 -070060 public SwipeController(Context context) {
Joe Onorato85a02a82009-09-08 12:34:22 -070061 ViewConfiguration config = ViewConfiguration.get(context);
62 mSlop = config.getScaledTouchSlop();
63
64 DisplayMetrics display = context.getResources().getDisplayMetrics();
65 mSwipeDistance = display.heightPixels / 2; // one half of the screen
66
Joe Onorato7bb17492009-09-24 17:51:01 -070067 setMode(MODE_WORKSPACE, false);
Joe Onorato85a02a82009-09-08 12:34:22 -070068 }
69
Joe Onorato7bb17492009-09-24 17:51:01 -070070 public void setAllAppsView(AllAppsView allAppsView) {
71 mAllAppsView = allAppsView;
Joe Onorato85a02a82009-09-08 12:34:22 -070072 }
73
Joe Onorato7bb17492009-09-24 17:51:01 -070074 public void setMode(int mode, boolean animate) {
75 mMinDest = mode - 1;
76 if (mMinDest < MODE_WORKSPACE) {
77 mMinDest = MODE_WORKSPACE;
78 }
79 mMaxDest = mode + 1;
80 if (mMaxDest > MODE_ALL_APPS) { // TODO: support _ZOOMED
81 mMaxDest = MODE_ALL_APPS;
82 }
Joe Onorato85a02a82009-09-08 12:34:22 -070083 mCanceled = true;
Joe Onorato7bb17492009-09-24 17:51:01 -070084 if (mAllAppsView != null) {
85 // TODO: do something with the wallpaper
86 if (animate) {
87 mAllAppsView.setZoomTarget(mode);
88 } else {
89 mAllAppsView.setZoom(mode);
90 }
91 }
92 mMode = mode;
93 }
94
95 /**
96 * Cancels the current swipe, if there is one, and animates back to wherever we were before.
97 */
98 public void cancelSwipe() {
99 mCanceled = true;
100 mAllAppsView.setZoomTarget(mMode);
Joe Onorato85a02a82009-09-08 12:34:22 -0700101 }
102
103 public boolean onInterceptTouchEvent(MotionEvent ev) {
104 onTouchEvent(ev);
Joe Onorato85a02a82009-09-08 12:34:22 -0700105 return mTracking;
106 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700107
Joe Onorato85a02a82009-09-08 12:34:22 -0700108 public boolean onTouchEvent(MotionEvent ev) {
109 if (mVelocityTracker == null) {
110 mVelocityTracker = VelocityTracker.obtain();
111 }
112 mVelocityTracker.addMovement(ev);
113
114 final int screenX = (int)ev.getRawX();
115 final int screenY = (int)ev.getRawY();
116
117 final int deltaX = screenX - mDownX;
118 final int deltaY = screenY - mDownY;
119
120 final int action = ev.getAction();
121 switch (action) {
122 case MotionEvent.ACTION_DOWN:
123 // Remember location of down touch
124 mCanceled = false;
125 mTracking = false;
126 mDownX = screenX;
127 mDownY = screenY;
128 break;
129
130 case MotionEvent.ACTION_MOVE:
131 if (!mCanceled && !mTracking) {
132 if (Math.abs(deltaX) > mSlop) {
133 mCanceled = true;
134 mTracking = false;
135 }
136 if (Math.abs(deltaY) > mSlop) {
137 mTracking = true;
138 }
139 }
140 if (mTracking && !mCanceled) {
141 track(screenY);
142 }
143 break;
144
145 case MotionEvent.ACTION_CANCEL:
146 case MotionEvent.ACTION_UP:
147 if (mTracking && !mCanceled) {
148 fling(screenY);
149 }
150 mVelocityTracker.recycle();
151 mVelocityTracker = null;
152 break;
153 }
154
155 return mTracking || mCanceled;
156 }
157
158 private float clamp(float v) {
159 if (v < mMinDest) {
160 return mMinDest;
161 } else if (v > mMaxDest) {
162 return mMaxDest;
163 } else {
164 return v;
165 }
166 }
167
Joe Onorato7bb17492009-09-24 17:51:01 -0700168 private float dist(int screenY) {
169 return clamp(mMode - ((screenY - mDownY) / mSwipeDistance));
170 }
171
Joe Onorato85a02a82009-09-08 12:34:22 -0700172 private void track(int screenY) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700173 mAmount = dist(screenY);
174
175 //Log.d(TAG, "mAmount=" + mAmount);
176 mAllAppsView.setZoom(mAmount);
Joe Onorato85a02a82009-09-08 12:34:22 -0700177 }
178
179 private void fling(int screenY) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700180 mAmount = dist(screenY);
181
Joe Onorato85a02a82009-09-08 12:34:22 -0700182 mVelocityTracker.computeCurrentVelocity(1);
183
Joe Onorato7bb17492009-09-24 17:51:01 -0700184 float velocity = mVelocityTracker.getYVelocity() / mSwipeDistance;
185 int direction = velocity >= 0.0f ? 1 : -1;
186 mAmount = dist(screenY);
Joe Onorato85a02a82009-09-08 12:34:22 -0700187
Joe Onorato7bb17492009-09-24 17:51:01 -0700188 int dest = mMode;
189 if (mMode < mAmount) {
190 if (velocity < 0) { // up
191 dest = mMode + 1;
Joe Onorato85a02a82009-09-08 12:34:22 -0700192 }
193 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700194 if (velocity > 0) { // down
195 dest = mMode - 1;
Joe Onorato85a02a82009-09-08 12:34:22 -0700196 }
197 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700198 // else dest == mMode, so go back to where we started
199
200 //Log.d(TAG, "velocity=" + velocity + " mAmount=" + mAmount + " dest=" + dest);
201 mAllAppsView.setZoomTarget(dest);
202 mMode = dest;
203 mCanceled = true;
Joe Onorato85a02a82009-09-08 12:34:22 -0700204 }
Joe Onorato85a02a82009-09-08 12:34:22 -0700205}
206