Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Handler; |
| 21 | import android.os.Message; |
| 22 | import android.os.SystemClock; |
| 23 | import android.util.Log; |
| 24 | import android.util.DisplayMetrics; |
| 25 | import android.view.MotionEvent; |
| 26 | import android.view.VelocityTracker; |
| 27 | import android.view.ViewConfiguration; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | |
| 31 | public class SwipeController { |
| 32 | private static final String TAG = "Launcher.SwipeController"; |
| 33 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 34 | 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 Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 38 | 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 Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 43 | private int mSlopX; |
| 44 | private int mSlopY; |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 45 | private float mSwipeDistance; |
| 46 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 47 | private AllAppsView mAllAppsView; |
| 48 | |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 49 | // state |
| 50 | private VelocityTracker mVelocityTracker; |
| 51 | private boolean mCanceled; |
| 52 | private boolean mTracking; |
| 53 | private int mDownX; |
| 54 | private int mDownY; |
| 55 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 56 | private int mMode; |
| 57 | private int mMinDest; |
| 58 | private int mMaxDest; |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 59 | private float mAmount; |
| 60 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 61 | public SwipeController(Context context) { |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 62 | ViewConfiguration config = ViewConfiguration.get(context); |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 63 | mSlopX = config.getScaledTouchSlop(); |
Joe Onorato | 08b5dfe | 2009-10-01 21:47:56 -0700 | [diff] [blame^] | 64 | mSlopY = 4 * mSlopX / 3; // make it 33% more biased towards horizontal swiping. |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 65 | |
| 66 | DisplayMetrics display = context.getResources().getDisplayMetrics(); |
| 67 | mSwipeDistance = display.heightPixels / 2; // one half of the screen |
| 68 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 69 | setMode(MODE_WORKSPACE, false); |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 72 | public void setAllAppsView(AllAppsView allAppsView) { |
| 73 | mAllAppsView = allAppsView; |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 76 | 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 Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 85 | mCanceled = true; |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 86 | 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 Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 106 | onTouchEvent(ev); |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 107 | return mTracking; |
| 108 | } |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 109 | |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 110 | 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 Onorato | 360d035 | 2009-09-28 14:37:53 -0400 | [diff] [blame] | 130 | mAllAppsView.setZoomSwipeInProgress(true, true); |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 131 | break; |
| 132 | |
| 133 | case MotionEvent.ACTION_MOVE: |
| 134 | if (!mCanceled && !mTracking) { |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 135 | if (Math.abs(deltaX) > mSlopX) { |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 136 | mCanceled = true; |
| 137 | mTracking = false; |
Joe Onorato | 360d035 | 2009-09-28 14:37:53 -0400 | [diff] [blame] | 138 | mAllAppsView.setZoomSwipeInProgress(false, true); |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 139 | } else if (Math.abs(deltaY) > mSlopY) { |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 140 | 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 Onorato | 360d035 | 2009-09-28 14:37:53 -0400 | [diff] [blame] | 152 | mAllAppsView.setZoomSwipeInProgress(false, false); |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 153 | } |
| 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 Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 172 | private float dist(int screenY) { |
| 173 | return clamp(mMode - ((screenY - mDownY) / mSwipeDistance)); |
| 174 | } |
| 175 | |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 176 | private void track(int screenY) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 177 | mAmount = dist(screenY); |
| 178 | |
| 179 | //Log.d(TAG, "mAmount=" + mAmount); |
| 180 | mAllAppsView.setZoom(mAmount); |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private void fling(int screenY) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 184 | mAmount = dist(screenY); |
| 185 | |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 186 | mVelocityTracker.computeCurrentVelocity(1); |
| 187 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 188 | float velocity = mVelocityTracker.getYVelocity() / mSwipeDistance; |
| 189 | int direction = velocity >= 0.0f ? 1 : -1; |
| 190 | mAmount = dist(screenY); |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 191 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 192 | int dest = mMode; |
| 193 | if (mMode < mAmount) { |
| 194 | if (velocity < 0) { // up |
| 195 | dest = mMode + 1; |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 196 | } |
| 197 | } else { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 198 | if (velocity > 0) { // down |
| 199 | dest = mMode - 1; |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 202 | // 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 Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 208 | } |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 209 | } |
| 210 | |