blob: 6994c3244302941276a7c2ebe590e10ce8325cea [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;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070030import android.util.DisplayMetrics;
Joe Onorato00acb122009-08-04 16:04:30 -040031import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.view.View;
Joe Onorato00acb122009-08-04 16:04:30 -040033import android.view.ViewGroup;
34import android.view.KeyEvent;
35import android.view.MotionEvent;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070036import android.view.WindowManager;
Joe Onorato00acb122009-08-04 16:04:30 -040037import android.view.inputmethod.InputMethodManager;
38import android.widget.FrameLayout;
39import android.widget.ImageView;
40
41import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042
43/**
Joe Onorato00acb122009-08-04 16:04:30 -040044 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 */
Joe Onorato00acb122009-08-04 16:04:30 -040046public class DragController {
47 /** Indicates the drag is a move. */
48 public static int DRAG_ACTION_MOVE = 0;
49
50 /** Indicates the drag is a copy. */
51 public static int DRAG_ACTION_COPY = 1;
52
53 private static final int SCROLL_DELAY = 600;
54 private static final int SCROLL_ZONE = 20;
55 private static final int VIBRATE_DURATION = 35;
56
57 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
58
59 private static final int SCROLL_OUTSIDE_ZONE = 0;
60 private static final int SCROLL_WAITING_IN_ZONE = 1;
61
62 private static final int SCROLL_LEFT = 0;
63 private static final int SCROLL_RIGHT = 1;
64
65 private Context mContext;
66 private Handler mHandler;
67 private final Vibrator mVibrator = new Vibrator();
68
69 // temporaries to avoid gc thrash
70 private Rect mRectTemp = new Rect();
71 private final int[] mCoordinatesTemp = new int[2];
72
73 /** Whether or not we're dragging. */
74 private boolean mDragging;
75
76 /** X coordinate of the down event. */
77 private float mMotionDownX;
78
79 /** Y coordinate of the down event. */
80 private float mMotionDownY;
81
Joe Onoratoe048e8a2009-09-25 10:39:17 -070082 /** Info about the screen for clamping. */
83 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
84
Joe Onorato00acb122009-08-04 16:04:30 -040085 /** Original view that is being dragged. */
86 private View mOriginator;
87
Joe Onorato00acb122009-08-04 16:04:30 -040088 /** X offset from the upper-left corner of the cell to where we touched. */
89 private float mTouchOffsetX;
90
91 /** Y offset from the upper-left corner of the cell to where we touched. */
92 private float mTouchOffsetY;
93
94 /** Where the drag originated */
95 private DragSource mDragSource;
96
97 /** The data associated with the object being dragged */
98 private Object mDragInfo;
99
100 /** The view that moves around while you drag. */
101 private DragView mDragView;
102
103 /** Who can receive drop events */
104 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
105
106 private DragListener mListener;
107
108 /** The window token used as the parent for the DragView. */
109 private IBinder mWindowToken;
110
111 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
112 private View mScrollView;
113
114 private DragScroller mDragScroller;
115 private int mScrollState = SCROLL_OUTSIDE_ZONE;
116 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
117
118 private RectF mDeleteRegion;
119 private DropTarget mLastDropTarget;
120
121 private InputMethodManager mInputMethodManager;
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 /**
124 * Interface to receive notifications when a drag starts or stops
125 */
126 interface DragListener {
127
128 /**
129 * A drag has begun
130 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 * @param source An object representing where the drag originated
132 * @param info The data associated with the object that is being dragged
133 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
134 * or {@link DragController#DRAG_ACTION_COPY}
135 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700136 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 /**
139 * The drag has eneded
140 */
141 void onDragEnd();
142 }
143
144 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400145 * Used to create a new DragLayer from XML.
146 *
147 * @param context The application's context.
148 * @param attrs The attribtues set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 */
Joe Onorato00acb122009-08-04 16:04:30 -0400150 public DragController(Context context) {
151 mContext = context;
152 mHandler = new Handler();
153 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
155 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700156 * Starts a drag.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 *
158 * @param v The view that is being dragged
159 * @param source An object representing where the drag originated
160 * @param info The data associated with the object that is being dragged
161 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
162 * {@link #DRAG_ACTION_COPY}
163 */
Joe Onorato00acb122009-08-04 16:04:30 -0400164 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700165 mOriginator = v;
166
167 Bitmap b = getViewBitmap(v);
168
169 int[] loc = mCoordinatesTemp;
170 v.getLocationOnScreen(loc);
171 int screenX = loc[0];
172 int screenY = loc[1];
173
174 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
175 source, dragInfo, dragAction);
176
177 b.recycle();
178
179 if (dragAction == DRAG_ACTION_MOVE) {
180 v.setVisibility(View.GONE);
181 }
182 }
183
184 /**
185 * Starts a drag.
186 *
187 * @param b The bitmap to display as the drag image. It will be re-scaled to the
188 * enlarged size.
189 * @param screenX The x position on screen of the left-top of the bitmap.
190 * @param screenY The y position on screen of the left-top of the bitmap.
191 * @param textureLeft The left edge of the region inside b to use.
192 * @param textureTop The top edge of the region inside b to use.
193 * @param textureWidth The width of the region inside b to use.
194 * @param textureHeight The height of the region inside b to use.
195 * @param source An object representing where the drag originated
196 * @param info The data associated with the object that is being dragged
197 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
198 * {@link #DRAG_ACTION_COPY}
199 */
200 public void startDrag(Bitmap b, int screenX, int screenY,
201 int textureLeft, int textureTop, int textureWidth, int textureHeight,
202 DragSource source, Object dragInfo, int dragAction) {
Joe Onorato00acb122009-08-04 16:04:30 -0400203 if (PROFILE_DRAWING_DURING_DRAG) {
204 android.os.Debug.startMethodTracing("Launcher");
205 }
206
207 // Hide soft keyboard, if visible
208 if (mInputMethodManager == null) {
209 mInputMethodManager = (InputMethodManager)
210 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
211 }
212 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
213
214 if (mListener != null) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700215 mListener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400216 }
217
Joe Onorato00acb122009-08-04 16:04:30 -0400218 int registrationX = ((int)mMotionDownX) - screenX;
219 int registrationY = ((int)mMotionDownY) - screenY;
220
221 mTouchOffsetX = mMotionDownX - screenX;
222 mTouchOffsetY = mMotionDownY - screenY;
223
224 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400225 mDragSource = source;
226 mDragInfo = dragInfo;
227
228 mVibrator.vibrate(VIBRATE_DURATION);
229
Joe Onorato5162ea92009-09-03 09:39:42 -0700230 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
231 textureLeft, textureTop, textureWidth, textureHeight);
Joe Onorato00acb122009-08-04 16:04:30 -0400232 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400233 }
234
235 /**
236 * Draw the view into a bitmap.
237 */
238 private Bitmap getViewBitmap(View v) {
239 v.clearFocus();
240 v.setPressed(false);
241
242 boolean willNotCache = v.willNotCacheDrawing();
243 v.setWillNotCacheDrawing(false);
244
245 // Reset the drawing cache background color to fully transparent
246 // for the duration of this operation
247 int color = v.getDrawingCacheBackgroundColor();
248 v.setDrawingCacheBackgroundColor(0);
249
250 if (color != 0) {
251 v.destroyDrawingCache();
252 }
253 v.buildDrawingCache();
254 Bitmap cacheBitmap = v.getDrawingCache();
255
256 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
257
258 // Restore the view
259 v.destroyDrawingCache();
260 v.setWillNotCacheDrawing(willNotCache);
261 v.setDrawingCacheBackgroundColor(color);
262
263 return bitmap;
264 }
265
266 /**
267 * Call this from a drag source view like this:
268 *
269 * <pre>
270 * @Override
271 * public boolean dispatchKeyEvent(KeyEvent event) {
272 * return mDragController.dispatchKeyEvent(this, event)
273 * || super.dispatchKeyEvent(event);
274 * </pre>
275 */
276 public boolean dispatchKeyEvent(KeyEvent event) {
277 return mDragging;
278 }
279
280 private void endDrag() {
281 if (mDragging) {
282 mDragging = false;
283 if (mOriginator != null) {
284 mOriginator.setVisibility(View.VISIBLE);
285 }
286 if (mListener != null) {
287 mListener.onDragEnd();
288 }
289 if (mDragView != null) {
290 mDragView.remove();
291 mDragView = null;
292 }
Joe Onorato00acb122009-08-04 16:04:30 -0400293 }
294 }
295
296 /**
297 * Call this from a drag source view.
298 */
299 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400300 if (false) {
301 Log.d(Launcher.LOG_TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
302 + mDragging);
303 }
Joe Onorato00acb122009-08-04 16:04:30 -0400304 final int action = ev.getAction();
305
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700306 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
307 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400308
309 switch (action) {
310 case MotionEvent.ACTION_MOVE:
311 break;
312
313 case MotionEvent.ACTION_DOWN:
314 // Remember location of down touch
315 mMotionDownX = screenX;
316 mMotionDownY = screenY;
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700317 recordScreenSize();
Joe Onorato00acb122009-08-04 16:04:30 -0400318 mLastDropTarget = null;
319 break;
320
321 case MotionEvent.ACTION_CANCEL:
322 case MotionEvent.ACTION_UP:
323 if (mDragging) {
324 drop(screenX, screenY);
325 }
326 endDrag();
327 break;
328 }
329
330 return mDragging;
331 }
332
333 /**
334 * Call this from a drag source view.
335 */
336 public boolean onTouchEvent(MotionEvent ev) {
337 View scrollView = mScrollView;
338
339 if (!mDragging) {
340 return false;
341 }
342
343 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700344 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
345 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400346
347 switch (action) {
348 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400349 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700350 mMotionDownX = screenX;
351 mMotionDownY = screenY;
352 recordScreenSize();
Joe Onorato00acb122009-08-04 16:04:30 -0400353
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700354 if ((screenX < SCROLL_ZONE) || (screenX > scrollView.getWidth() - SCROLL_ZONE)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400355 mScrollState = SCROLL_WAITING_IN_ZONE;
356 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
357 } else {
358 mScrollState = SCROLL_OUTSIDE_ZONE;
359 }
360
361 break;
362 case MotionEvent.ACTION_MOVE:
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700363 // Update the drag view. Don't use the clamped pos here so the dragging looks
364 // like it goes off screen a little, intead of bumping up against the edge.
Joe Onorato00acb122009-08-04 16:04:30 -0400365 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
366
367 // Drop on someone?
368 final int[] coordinates = mCoordinatesTemp;
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700369 DropTarget dropTarget = findDropTarget((int) screenX, (int) screenY, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400370 if (dropTarget != null) {
371 if (mLastDropTarget == dropTarget) {
372 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
373 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
374 } else {
375 if (mLastDropTarget != null) {
376 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
377 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
378 }
379 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
380 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
381 }
382 } else {
383 if (mLastDropTarget != null) {
384 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
385 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
386 }
387 }
388 mLastDropTarget = dropTarget;
389
390 // Scroll, maybe, but not if we're in the delete region.
391 boolean inDeleteRegion = false;
392 if (mDeleteRegion != null) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700393 inDeleteRegion = mDeleteRegion.contains(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400394 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700395 if (!inDeleteRegion && screenX < SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400396 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
397 mScrollState = SCROLL_WAITING_IN_ZONE;
398 mScrollRunnable.setDirection(SCROLL_LEFT);
399 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
400 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700401 } else if (!inDeleteRegion && screenX > scrollView.getWidth() - SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400402 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
403 mScrollState = SCROLL_WAITING_IN_ZONE;
404 mScrollRunnable.setDirection(SCROLL_RIGHT);
405 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
406 }
407 } else {
408 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
409 mScrollState = SCROLL_OUTSIDE_ZONE;
410 mScrollRunnable.setDirection(SCROLL_RIGHT);
411 mHandler.removeCallbacks(mScrollRunnable);
412 }
413 }
414
415 break;
416 case MotionEvent.ACTION_UP:
417 mHandler.removeCallbacks(mScrollRunnable);
418 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700419 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400420 }
421 endDrag();
422
423 break;
424 case MotionEvent.ACTION_CANCEL:
425 endDrag();
426 }
427
428 return true;
429 }
430
431 private boolean drop(float x, float y) {
432 final int[] coordinates = mCoordinatesTemp;
433 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
434
435 if (dropTarget != null) {
436 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
437 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
438 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
439 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
440 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
441 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
442 mDragSource.onDropCompleted((View) dropTarget, true);
443 return true;
444 } else {
445 mDragSource.onDropCompleted((View) dropTarget, false);
446 return true;
447 }
448 }
449 return false;
450 }
451
452 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
453 final Rect r = mRectTemp;
454
455 final ArrayList<DropTarget> dropTargets = mDropTargets;
456 final int count = dropTargets.size();
457 for (int i=count-1; i>=0; i--) {
458 final DropTarget target = dropTargets.get(i);
459 target.getHitRect(r);
460 target.getLocationOnScreen(dropCoordinates);
461 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
462 if (r.contains(x, y)) {
463 dropCoordinates[0] = x - dropCoordinates[0];
464 dropCoordinates[1] = y - dropCoordinates[1];
465 return target;
466 }
467 }
468 return null;
469 }
470
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700471 /**
472 * Get the screen size so we can clamp events to the screen size so even if
473 * you drag off the edge of the screen, we find something.
474 */
475 private void recordScreenSize() {
476 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
477 .getDefaultDisplay().getMetrics(mDisplayMetrics);
478 }
479
480 /**
481 * Clamp val to be &gt;= min and &lt; max.
482 */
483 private static int clamp(int val, int min, int max) {
484 if (val < min) {
485 return min;
486 } else if (val >= max) {
487 return max - 1;
488 } else {
489 return val;
490 }
491 }
492
Joe Onorato00acb122009-08-04 16:04:30 -0400493 public void setDragScoller(DragScroller scroller) {
494 mDragScroller = scroller;
495 }
496
497 public void setWindowToken(IBinder token) {
498 mWindowToken = token;
499 }
500
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 /**
502 * Sets the drag listner which will be notified when a drag starts or ends.
503 */
Joe Onorato00acb122009-08-04 16:04:30 -0400504 public void setDragListener(DragListener l) {
505 mListener = l;
506 }
507
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 /**
509 * Remove a previously installed drag listener.
510 */
Joe Onorato00acb122009-08-04 16:04:30 -0400511 public void removeDragListener(DragListener l) {
512 mListener = null;
513 }
514
515 /**
516 * Add a DropTarget to the list of potential places to receive drop events.
517 */
518 public void addDropTarget(DropTarget target) {
519 mDropTargets.add(target);
520 }
521
522 /**
523 * Don't send drop events to <em>target</em> any more.
524 */
525 public void removeDropTarget(DropTarget target) {
526 mDropTargets.remove(target);
527 }
528
529 /**
530 * Set which view scrolls for touch events near the edge of the screen.
531 */
532 public void setScrollView(View v) {
533 mScrollView = v;
534 }
535
536 /**
537 * Specifies the delete region. We won't scroll on touch events over the delete region.
538 *
539 * @param region The rectangle in screen coordinates of the delete region.
540 */
541 void setDeleteRegion(RectF region) {
542 mDeleteRegion = region;
543 }
544
545 private class ScrollRunnable implements Runnable {
546 private int mDirection;
547
548 ScrollRunnable() {
549 }
550
551 public void run() {
552 if (mDragScroller != null) {
553 if (mDirection == SCROLL_LEFT) {
554 mDragScroller.scrollLeft();
555 } else {
556 mDragScroller.scrollRight();
557 }
558 mScrollState = SCROLL_OUTSIDE_ZONE;
559 }
560 }
561
562 void setDirection(int direction) {
563 mDirection = direction;
564 }
565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566}