blob: 38bc468e6063ca910a38b96b91b1c4ae6853fd19 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato00acb122009-08-04 16:04:30 -040019import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Canvas;
22import android.graphics.Matrix;
23import android.graphics.Rect;
24import android.graphics.RectF;
25import android.os.IBinder;
26import android.os.Handler;
27import android.os.Vibrator;
28import android.os.SystemClock;
29import android.util.AttributeSet;
30import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.view.View;
Joe Onorato00acb122009-08-04 16:04:30 -040032import android.view.ViewGroup;
33import android.view.KeyEvent;
34import android.view.MotionEvent;
35import android.view.inputmethod.InputMethodManager;
36import android.widget.FrameLayout;
37import android.widget.ImageView;
38
39import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
41/**
Joe Onorato00acb122009-08-04 16:04:30 -040042 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 */
Joe Onorato00acb122009-08-04 16:04:30 -040044public class DragController {
45 /** Indicates the drag is a move. */
46 public static int DRAG_ACTION_MOVE = 0;
47
48 /** Indicates the drag is a copy. */
49 public static int DRAG_ACTION_COPY = 1;
50
51 private static final int SCROLL_DELAY = 600;
52 private static final int SCROLL_ZONE = 20;
53 private static final int VIBRATE_DURATION = 35;
54
55 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
56
57 private static final int SCROLL_OUTSIDE_ZONE = 0;
58 private static final int SCROLL_WAITING_IN_ZONE = 1;
59
60 private static final int SCROLL_LEFT = 0;
61 private static final int SCROLL_RIGHT = 1;
62
63 private Context mContext;
64 private Handler mHandler;
65 private final Vibrator mVibrator = new Vibrator();
66
67 // temporaries to avoid gc thrash
68 private Rect mRectTemp = new Rect();
69 private final int[] mCoordinatesTemp = new int[2];
70
71 /** Whether or not we're dragging. */
72 private boolean mDragging;
73
74 /** X coordinate of the down event. */
75 private float mMotionDownX;
76
77 /** Y coordinate of the down event. */
78 private float mMotionDownY;
79
80 /** Original view that is being dragged. */
81 private View mOriginator;
82
83 /** The contents of mOriginator with no scaling. */
84 private Bitmap mDragBitmap;
85
86 /** X offset from the upper-left corner of the cell to where we touched. */
87 private float mTouchOffsetX;
88
89 /** Y offset from the upper-left corner of the cell to where we touched. */
90 private float mTouchOffsetY;
91
92 /** Where the drag originated */
93 private DragSource mDragSource;
94
95 /** The data associated with the object being dragged */
96 private Object mDragInfo;
97
98 /** The view that moves around while you drag. */
99 private DragView mDragView;
100
101 /** Who can receive drop events */
102 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
103
104 private DragListener mListener;
105
106 /** The window token used as the parent for the DragView. */
107 private IBinder mWindowToken;
108
109 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
110 private View mScrollView;
111
112 private DragScroller mDragScroller;
113 private int mScrollState = SCROLL_OUTSIDE_ZONE;
114 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
115
116 private RectF mDeleteRegion;
117 private DropTarget mLastDropTarget;
118
119 private InputMethodManager mInputMethodManager;
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 /**
122 * Interface to receive notifications when a drag starts or stops
123 */
124 interface DragListener {
125
126 /**
127 * A drag has begun
128 *
129 * @param v The view that is being dragged
130 * @param source An object representing where the drag originated
131 * @param info The data associated with the object that is being dragged
132 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
133 * or {@link DragController#DRAG_ACTION_COPY}
134 */
135 void onDragStart(View v, DragSource source, Object info, int dragAction);
136
137 /**
138 * The drag has eneded
139 */
140 void onDragEnd();
141 }
142
143 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400144 * Used to create a new DragLayer from XML.
145 *
146 * @param context The application's context.
147 * @param attrs The attribtues set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 */
Joe Onorato00acb122009-08-04 16:04:30 -0400149 public DragController(Context context) {
150 mContext = context;
151 mHandler = new Handler();
152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
154 /**
155 * Starts a drag
156 *
157 * @param v The view that is being dragged
158 * @param source An object representing where the drag originated
159 * @param info The data associated with the object that is being dragged
160 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
161 * {@link #DRAG_ACTION_COPY}
162 */
Joe Onorato00acb122009-08-04 16:04:30 -0400163 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
164 if (PROFILE_DRAWING_DURING_DRAG) {
165 android.os.Debug.startMethodTracing("Launcher");
166 }
167
168 // Hide soft keyboard, if visible
169 if (mInputMethodManager == null) {
170 mInputMethodManager = (InputMethodManager)
171 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
172 }
173 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
174
175 if (mListener != null) {
176 mListener.onDragStart(v, source, dragInfo, dragAction);
177 }
178
179 int[] loc = mCoordinatesTemp;
180 v.getLocationOnScreen(loc);
181 int screenX = loc[0];
182 int screenY = loc[1];
183
184 int registrationX = ((int)mMotionDownX) - screenX;
185 int registrationY = ((int)mMotionDownY) - screenY;
186
187 mTouchOffsetX = mMotionDownX - screenX;
188 mTouchOffsetY = mMotionDownY - screenY;
189
190 mDragging = true;
191 mOriginator = v;
192 mDragSource = source;
193 mDragInfo = dragInfo;
194
195 mVibrator.vibrate(VIBRATE_DURATION);
196
197 mDragBitmap = getViewBitmap(v);
198 DragView dragView = mDragView = new DragView(mContext, mDragBitmap,
199 registrationX, registrationY);
200 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
201
202 if (dragAction == DRAG_ACTION_MOVE) {
203 v.setVisibility(View.GONE);
204 }
205 }
206
207 /**
208 * Draw the view into a bitmap.
209 */
210 private Bitmap getViewBitmap(View v) {
211 v.clearFocus();
212 v.setPressed(false);
213
214 boolean willNotCache = v.willNotCacheDrawing();
215 v.setWillNotCacheDrawing(false);
216
217 // Reset the drawing cache background color to fully transparent
218 // for the duration of this operation
219 int color = v.getDrawingCacheBackgroundColor();
220 v.setDrawingCacheBackgroundColor(0);
221
222 if (color != 0) {
223 v.destroyDrawingCache();
224 }
225 v.buildDrawingCache();
226 Bitmap cacheBitmap = v.getDrawingCache();
227
228 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
229
230 // Restore the view
231 v.destroyDrawingCache();
232 v.setWillNotCacheDrawing(willNotCache);
233 v.setDrawingCacheBackgroundColor(color);
234
235 return bitmap;
236 }
237
238 /**
239 * Call this from a drag source view like this:
240 *
241 * <pre>
242 * @Override
243 * public boolean dispatchKeyEvent(KeyEvent event) {
244 * return mDragController.dispatchKeyEvent(this, event)
245 * || super.dispatchKeyEvent(event);
246 * </pre>
247 */
248 public boolean dispatchKeyEvent(KeyEvent event) {
249 return mDragging;
250 }
251
252 private void endDrag() {
253 if (mDragging) {
254 mDragging = false;
255 if (mOriginator != null) {
256 mOriginator.setVisibility(View.VISIBLE);
257 }
258 if (mListener != null) {
259 mListener.onDragEnd();
260 }
261 if (mDragView != null) {
262 mDragView.remove();
263 mDragView = null;
264 }
265 if (mDragBitmap != null) {
266 mDragBitmap.recycle();
267 mDragBitmap = null;
268 }
269 }
270 }
271
272 /**
273 * Call this from a drag source view.
274 */
275 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 if (false) {
277 Log.d(Launcher.LOG_TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
278 + mDragging);
279 }
Joe Onorato00acb122009-08-04 16:04:30 -0400280 final int action = ev.getAction();
281
282 final float screenX = ev.getRawX();
283 final float screenY = ev.getRawY();
284
285 switch (action) {
286 case MotionEvent.ACTION_MOVE:
287 break;
288
289 case MotionEvent.ACTION_DOWN:
290 // Remember location of down touch
291 mMotionDownX = screenX;
292 mMotionDownY = screenY;
293 mLastDropTarget = null;
294 break;
295
296 case MotionEvent.ACTION_CANCEL:
297 case MotionEvent.ACTION_UP:
298 if (mDragging) {
299 drop(screenX, screenY);
300 }
301 endDrag();
302 break;
303 }
304
305 return mDragging;
306 }
307
308 /**
309 * Call this from a drag source view.
310 */
311 public boolean onTouchEvent(MotionEvent ev) {
312 View scrollView = mScrollView;
313
314 if (!mDragging) {
315 return false;
316 }
317
318 final int action = ev.getAction();
319 final float x = ev.getRawX();
320 final float y = ev.getRawY();
321
322 switch (action) {
323 case MotionEvent.ACTION_DOWN:
324
325 // Remember where the motion event started
326 mMotionDownX = x;
327 mMotionDownY = y;
328
329 if ((x < SCROLL_ZONE) || (x > scrollView.getWidth() - SCROLL_ZONE)) {
330 mScrollState = SCROLL_WAITING_IN_ZONE;
331 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
332 } else {
333 mScrollState = SCROLL_OUTSIDE_ZONE;
334 }
335
336 break;
337 case MotionEvent.ACTION_MOVE:
338 // Update the drag view.
339 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
340
341 // Drop on someone?
342 final int[] coordinates = mCoordinatesTemp;
343 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
344 if (dropTarget != null) {
345 if (mLastDropTarget == dropTarget) {
346 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
347 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
348 } else {
349 if (mLastDropTarget != null) {
350 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
351 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
352 }
353 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
354 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
355 }
356 } else {
357 if (mLastDropTarget != null) {
358 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
359 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
360 }
361 }
362 mLastDropTarget = dropTarget;
363
364 // Scroll, maybe, but not if we're in the delete region.
365 boolean inDeleteRegion = false;
366 if (mDeleteRegion != null) {
367 inDeleteRegion = mDeleteRegion.contains(ev.getRawX(), ev.getRawY());
368 }
369 if (!inDeleteRegion && x < SCROLL_ZONE) {
370 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
371 mScrollState = SCROLL_WAITING_IN_ZONE;
372 mScrollRunnable.setDirection(SCROLL_LEFT);
373 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
374 }
375 } else if (!inDeleteRegion && x > scrollView.getWidth() - SCROLL_ZONE) {
376 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
377 mScrollState = SCROLL_WAITING_IN_ZONE;
378 mScrollRunnable.setDirection(SCROLL_RIGHT);
379 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
380 }
381 } else {
382 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
383 mScrollState = SCROLL_OUTSIDE_ZONE;
384 mScrollRunnable.setDirection(SCROLL_RIGHT);
385 mHandler.removeCallbacks(mScrollRunnable);
386 }
387 }
388
389 break;
390 case MotionEvent.ACTION_UP:
391 mHandler.removeCallbacks(mScrollRunnable);
392 if (mDragging) {
393 drop(x, y);
394 }
395 endDrag();
396
397 break;
398 case MotionEvent.ACTION_CANCEL:
399 endDrag();
400 }
401
402 return true;
403 }
404
405 private boolean drop(float x, float y) {
406 final int[] coordinates = mCoordinatesTemp;
407 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
408
409 if (dropTarget != null) {
410 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
411 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
412 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
413 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
414 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
415 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
416 mDragSource.onDropCompleted((View) dropTarget, true);
417 return true;
418 } else {
419 mDragSource.onDropCompleted((View) dropTarget, false);
420 return true;
421 }
422 }
423 return false;
424 }
425
426 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
427 final Rect r = mRectTemp;
428
429 final ArrayList<DropTarget> dropTargets = mDropTargets;
430 final int count = dropTargets.size();
431 for (int i=count-1; i>=0; i--) {
432 final DropTarget target = dropTargets.get(i);
433 target.getHitRect(r);
434 target.getLocationOnScreen(dropCoordinates);
435 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
436 if (r.contains(x, y)) {
437 dropCoordinates[0] = x - dropCoordinates[0];
438 dropCoordinates[1] = y - dropCoordinates[1];
439 return target;
440 }
441 }
442 return null;
443 }
444
445 public void setDragScoller(DragScroller scroller) {
446 mDragScroller = scroller;
447 }
448
449 public void setWindowToken(IBinder token) {
450 mWindowToken = token;
451 }
452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 /**
454 * Sets the drag listner which will be notified when a drag starts or ends.
455 */
Joe Onorato00acb122009-08-04 16:04:30 -0400456 public void setDragListener(DragListener l) {
457 mListener = l;
458 }
459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 /**
461 * Remove a previously installed drag listener.
462 */
Joe Onorato00acb122009-08-04 16:04:30 -0400463 public void removeDragListener(DragListener l) {
464 mListener = null;
465 }
466
467 /**
468 * Add a DropTarget to the list of potential places to receive drop events.
469 */
470 public void addDropTarget(DropTarget target) {
471 mDropTargets.add(target);
472 }
473
474 /**
475 * Don't send drop events to <em>target</em> any more.
476 */
477 public void removeDropTarget(DropTarget target) {
478 mDropTargets.remove(target);
479 }
480
481 /**
482 * Set which view scrolls for touch events near the edge of the screen.
483 */
484 public void setScrollView(View v) {
485 mScrollView = v;
486 }
487
488 /**
489 * Specifies the delete region. We won't scroll on touch events over the delete region.
490 *
491 * @param region The rectangle in screen coordinates of the delete region.
492 */
493 void setDeleteRegion(RectF region) {
494 mDeleteRegion = region;
495 }
496
497 private class ScrollRunnable implements Runnable {
498 private int mDirection;
499
500 ScrollRunnable() {
501 }
502
503 public void run() {
504 if (mDragScroller != null) {
505 if (mDirection == SCROLL_LEFT) {
506 mDragScroller.scrollLeft();
507 } else {
508 mDragScroller.scrollRight();
509 }
510 mScrollState = SCROLL_OUTSIDE_ZONE;
511 }
512 }
513
514 void setDirection(int direction) {
515 mDirection = direction;
516 }
517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518}