blob: 1655cb8cd3d0a9cea427bdc1ab379a9d40eeb6e0 [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 {
Joe Onorato2e5c4322009-10-06 12:34:42 -070047 private static final String TAG = "Launcher.DragController";
48
Joe Onorato00acb122009-08-04 16:04:30 -040049 /** Indicates the drag is a move. */
50 public static int DRAG_ACTION_MOVE = 0;
51
52 /** Indicates the drag is a copy. */
53 public static int DRAG_ACTION_COPY = 1;
54
55 private static final int SCROLL_DELAY = 600;
56 private static final int SCROLL_ZONE = 20;
57 private static final int VIBRATE_DURATION = 35;
58
59 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
60
61 private static final int SCROLL_OUTSIDE_ZONE = 0;
62 private static final int SCROLL_WAITING_IN_ZONE = 1;
63
64 private static final int SCROLL_LEFT = 0;
65 private static final int SCROLL_RIGHT = 1;
66
67 private Context mContext;
68 private Handler mHandler;
69 private final Vibrator mVibrator = new Vibrator();
70
71 // temporaries to avoid gc thrash
72 private Rect mRectTemp = new Rect();
73 private final int[] mCoordinatesTemp = new int[2];
74
75 /** Whether or not we're dragging. */
76 private boolean mDragging;
77
78 /** X coordinate of the down event. */
79 private float mMotionDownX;
80
81 /** Y coordinate of the down event. */
82 private float mMotionDownY;
83
Joe Onoratoe048e8a2009-09-25 10:39:17 -070084 /** Info about the screen for clamping. */
85 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
86
Joe Onorato00acb122009-08-04 16:04:30 -040087 /** Original view that is being dragged. */
88 private View mOriginator;
89
Joe Onorato00acb122009-08-04 16:04:30 -040090 /** X offset from the upper-left corner of the cell to where we touched. */
91 private float mTouchOffsetX;
92
93 /** Y offset from the upper-left corner of the cell to where we touched. */
94 private float mTouchOffsetY;
95
96 /** Where the drag originated */
97 private DragSource mDragSource;
98
99 /** The data associated with the object being dragged */
100 private Object mDragInfo;
101
102 /** The view that moves around while you drag. */
103 private DragView mDragView;
104
105 /** Who can receive drop events */
106 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
107
108 private DragListener mListener;
109
110 /** The window token used as the parent for the DragView. */
111 private IBinder mWindowToken;
112
113 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
114 private View mScrollView;
115
116 private DragScroller mDragScroller;
117 private int mScrollState = SCROLL_OUTSIDE_ZONE;
118 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
119
120 private RectF mDeleteRegion;
121 private DropTarget mLastDropTarget;
122
123 private InputMethodManager mInputMethodManager;
124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 /**
126 * Interface to receive notifications when a drag starts or stops
127 */
128 interface DragListener {
129
130 /**
131 * A drag has begun
132 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 * @param source An object representing where the drag originated
134 * @param info The data associated with the object that is being dragged
135 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
136 * or {@link DragController#DRAG_ACTION_COPY}
137 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700138 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 /**
141 * The drag has eneded
142 */
143 void onDragEnd();
144 }
145
146 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400147 * Used to create a new DragLayer from XML.
148 *
149 * @param context The application's context.
150 * @param attrs The attribtues set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 */
Joe Onorato00acb122009-08-04 16:04:30 -0400152 public DragController(Context context) {
153 mContext = context;
154 mHandler = new Handler();
155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
157 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700158 * Starts a drag.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 *
160 * @param v The view that is being dragged
161 * @param source An object representing where the drag originated
162 * @param info The data associated with the object that is being dragged
163 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
164 * {@link #DRAG_ACTION_COPY}
165 */
Joe Onorato00acb122009-08-04 16:04:30 -0400166 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700167 mOriginator = v;
168
169 Bitmap b = getViewBitmap(v);
170
171 int[] loc = mCoordinatesTemp;
172 v.getLocationOnScreen(loc);
173 int screenX = loc[0];
174 int screenY = loc[1];
175
176 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
177 source, dragInfo, dragAction);
178
179 b.recycle();
180
181 if (dragAction == DRAG_ACTION_MOVE) {
182 v.setVisibility(View.GONE);
183 }
184 }
185
186 /**
187 * Starts a drag.
188 *
189 * @param b The bitmap to display as the drag image. It will be re-scaled to the
190 * enlarged size.
191 * @param screenX The x position on screen of the left-top of the bitmap.
192 * @param screenY The y position on screen of the left-top of the bitmap.
193 * @param textureLeft The left edge of the region inside b to use.
194 * @param textureTop The top edge of the region inside b to use.
195 * @param textureWidth The width of the region inside b to use.
196 * @param textureHeight The height of the region inside b to use.
197 * @param source An object representing where the drag originated
198 * @param info The data associated with the object that is being dragged
199 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
200 * {@link #DRAG_ACTION_COPY}
201 */
202 public void startDrag(Bitmap b, int screenX, int screenY,
203 int textureLeft, int textureTop, int textureWidth, int textureHeight,
204 DragSource source, Object dragInfo, int dragAction) {
Joe Onorato00acb122009-08-04 16:04:30 -0400205 if (PROFILE_DRAWING_DURING_DRAG) {
206 android.os.Debug.startMethodTracing("Launcher");
207 }
208
209 // Hide soft keyboard, if visible
210 if (mInputMethodManager == null) {
211 mInputMethodManager = (InputMethodManager)
212 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
213 }
214 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
215
216 if (mListener != null) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700217 mListener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400218 }
219
Joe Onorato00acb122009-08-04 16:04:30 -0400220 int registrationX = ((int)mMotionDownX) - screenX;
221 int registrationY = ((int)mMotionDownY) - screenY;
222
223 mTouchOffsetX = mMotionDownX - screenX;
224 mTouchOffsetY = mMotionDownY - screenY;
225
226 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400227 mDragSource = source;
228 mDragInfo = dragInfo;
229
230 mVibrator.vibrate(VIBRATE_DURATION);
231
Joe Onorato5162ea92009-09-03 09:39:42 -0700232 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
233 textureLeft, textureTop, textureWidth, textureHeight);
Joe Onorato00acb122009-08-04 16:04:30 -0400234 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400235 }
236
237 /**
238 * Draw the view into a bitmap.
239 */
240 private Bitmap getViewBitmap(View v) {
241 v.clearFocus();
242 v.setPressed(false);
243
244 boolean willNotCache = v.willNotCacheDrawing();
245 v.setWillNotCacheDrawing(false);
246
247 // Reset the drawing cache background color to fully transparent
248 // for the duration of this operation
249 int color = v.getDrawingCacheBackgroundColor();
250 v.setDrawingCacheBackgroundColor(0);
251
252 if (color != 0) {
253 v.destroyDrawingCache();
254 }
255 v.buildDrawingCache();
256 Bitmap cacheBitmap = v.getDrawingCache();
257
258 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
259
260 // Restore the view
261 v.destroyDrawingCache();
262 v.setWillNotCacheDrawing(willNotCache);
263 v.setDrawingCacheBackgroundColor(color);
264
265 return bitmap;
266 }
267
268 /**
269 * Call this from a drag source view like this:
270 *
271 * <pre>
272 * @Override
273 * public boolean dispatchKeyEvent(KeyEvent event) {
274 * return mDragController.dispatchKeyEvent(this, event)
275 * || super.dispatchKeyEvent(event);
276 * </pre>
277 */
278 public boolean dispatchKeyEvent(KeyEvent event) {
279 return mDragging;
280 }
281
282 private void endDrag() {
283 if (mDragging) {
284 mDragging = false;
285 if (mOriginator != null) {
286 mOriginator.setVisibility(View.VISIBLE);
287 }
288 if (mListener != null) {
289 mListener.onDragEnd();
290 }
291 if (mDragView != null) {
292 mDragView.remove();
293 mDragView = null;
294 }
Joe Onorato00acb122009-08-04 16:04:30 -0400295 }
296 }
297
298 /**
299 * Call this from a drag source view.
300 */
301 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800303 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304 + mDragging);
305 }
Joe Onorato00acb122009-08-04 16:04:30 -0400306 final int action = ev.getAction();
307
Joe Onorato87467d32009-11-08 14:36:43 -0500308 if (action == MotionEvent.ACTION_DOWN) {
309 recordScreenSize();
310 }
311
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700312 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
313 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400314
315 switch (action) {
316 case MotionEvent.ACTION_MOVE:
317 break;
318
319 case MotionEvent.ACTION_DOWN:
320 // Remember location of down touch
321 mMotionDownX = screenX;
322 mMotionDownY = screenY;
323 mLastDropTarget = null;
324 break;
325
326 case MotionEvent.ACTION_CANCEL:
327 case MotionEvent.ACTION_UP:
328 if (mDragging) {
329 drop(screenX, screenY);
330 }
331 endDrag();
332 break;
333 }
334
335 return mDragging;
336 }
337
338 /**
339 * Call this from a drag source view.
340 */
341 public boolean onTouchEvent(MotionEvent ev) {
342 View scrollView = mScrollView;
343
344 if (!mDragging) {
345 return false;
346 }
347
348 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700349 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
350 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400351
352 switch (action) {
353 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400354 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700355 mMotionDownX = screenX;
356 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400357
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700358 if ((screenX < SCROLL_ZONE) || (screenX > scrollView.getWidth() - SCROLL_ZONE)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400359 mScrollState = SCROLL_WAITING_IN_ZONE;
360 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
361 } else {
362 mScrollState = SCROLL_OUTSIDE_ZONE;
363 }
364
365 break;
366 case MotionEvent.ACTION_MOVE:
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700367 // Update the drag view. Don't use the clamped pos here so the dragging looks
368 // like it goes off screen a little, intead of bumping up against the edge.
Joe Onorato00acb122009-08-04 16:04:30 -0400369 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
370
371 // Drop on someone?
372 final int[] coordinates = mCoordinatesTemp;
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700373 DropTarget dropTarget = findDropTarget((int) screenX, (int) screenY, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400374 if (dropTarget != null) {
375 if (mLastDropTarget == dropTarget) {
376 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
377 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
378 } else {
379 if (mLastDropTarget != null) {
380 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
381 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
382 }
383 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
384 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
385 }
386 } else {
387 if (mLastDropTarget != null) {
388 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
389 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
390 }
391 }
392 mLastDropTarget = dropTarget;
393
394 // Scroll, maybe, but not if we're in the delete region.
395 boolean inDeleteRegion = false;
396 if (mDeleteRegion != null) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700397 inDeleteRegion = mDeleteRegion.contains(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400398 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700399 if (!inDeleteRegion && screenX < SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400400 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
401 mScrollState = SCROLL_WAITING_IN_ZONE;
402 mScrollRunnable.setDirection(SCROLL_LEFT);
403 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
404 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700405 } else if (!inDeleteRegion && screenX > scrollView.getWidth() - SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400406 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
407 mScrollState = SCROLL_WAITING_IN_ZONE;
408 mScrollRunnable.setDirection(SCROLL_RIGHT);
409 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
410 }
411 } else {
412 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
413 mScrollState = SCROLL_OUTSIDE_ZONE;
414 mScrollRunnable.setDirection(SCROLL_RIGHT);
415 mHandler.removeCallbacks(mScrollRunnable);
416 }
417 }
418
419 break;
420 case MotionEvent.ACTION_UP:
421 mHandler.removeCallbacks(mScrollRunnable);
422 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700423 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400424 }
425 endDrag();
426
427 break;
428 case MotionEvent.ACTION_CANCEL:
429 endDrag();
430 }
431
432 return true;
433 }
434
435 private boolean drop(float x, float y) {
436 final int[] coordinates = mCoordinatesTemp;
437 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
438
439 if (dropTarget != null) {
440 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
441 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
442 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
443 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
444 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
445 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
446 mDragSource.onDropCompleted((View) dropTarget, true);
447 return true;
448 } else {
449 mDragSource.onDropCompleted((View) dropTarget, false);
450 return true;
451 }
452 }
453 return false;
454 }
455
456 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
457 final Rect r = mRectTemp;
458
459 final ArrayList<DropTarget> dropTargets = mDropTargets;
460 final int count = dropTargets.size();
461 for (int i=count-1; i>=0; i--) {
462 final DropTarget target = dropTargets.get(i);
463 target.getHitRect(r);
464 target.getLocationOnScreen(dropCoordinates);
465 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
466 if (r.contains(x, y)) {
467 dropCoordinates[0] = x - dropCoordinates[0];
468 dropCoordinates[1] = y - dropCoordinates[1];
469 return target;
470 }
471 }
472 return null;
473 }
474
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700475 /**
476 * Get the screen size so we can clamp events to the screen size so even if
477 * you drag off the edge of the screen, we find something.
478 */
479 private void recordScreenSize() {
480 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
481 .getDefaultDisplay().getMetrics(mDisplayMetrics);
482 }
483
484 /**
485 * Clamp val to be &gt;= min and &lt; max.
486 */
487 private static int clamp(int val, int min, int max) {
488 if (val < min) {
489 return min;
490 } else if (val >= max) {
491 return max - 1;
492 } else {
493 return val;
494 }
495 }
496
Joe Onorato00acb122009-08-04 16:04:30 -0400497 public void setDragScoller(DragScroller scroller) {
498 mDragScroller = scroller;
499 }
500
501 public void setWindowToken(IBinder token) {
502 mWindowToken = token;
503 }
504
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 /**
506 * Sets the drag listner which will be notified when a drag starts or ends.
507 */
Joe Onorato00acb122009-08-04 16:04:30 -0400508 public void setDragListener(DragListener l) {
509 mListener = l;
510 }
511
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 /**
513 * Remove a previously installed drag listener.
514 */
Joe Onorato00acb122009-08-04 16:04:30 -0400515 public void removeDragListener(DragListener l) {
516 mListener = null;
517 }
518
519 /**
520 * Add a DropTarget to the list of potential places to receive drop events.
521 */
522 public void addDropTarget(DropTarget target) {
523 mDropTargets.add(target);
524 }
525
526 /**
527 * Don't send drop events to <em>target</em> any more.
528 */
529 public void removeDropTarget(DropTarget target) {
530 mDropTargets.remove(target);
531 }
532
533 /**
534 * Set which view scrolls for touch events near the edge of the screen.
535 */
536 public void setScrollView(View v) {
537 mScrollView = v;
538 }
539
540 /**
541 * Specifies the delete region. We won't scroll on touch events over the delete region.
542 *
543 * @param region The rectangle in screen coordinates of the delete region.
544 */
545 void setDeleteRegion(RectF region) {
546 mDeleteRegion = region;
547 }
548
549 private class ScrollRunnable implements Runnable {
550 private int mDirection;
551
552 ScrollRunnable() {
553 }
554
555 public void run() {
556 if (mDragScroller != null) {
557 if (mDirection == SCROLL_LEFT) {
558 mDragScroller.scrollLeft();
559 } else {
560 mDragScroller.scrollRight();
561 }
562 mScrollState = SCROLL_OUTSIDE_ZONE;
563 }
564 }
565
566 void setDirection(int direction) {
567 mDirection = direction;
568 }
569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570}