blob: fe79c56d84dbf519dd1738014ff51d8776176a2b [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
Joe Onorato00acb122009-08-04 16:04:30 -040083 /** X offset from the upper-left corner of the cell to where we touched. */
84 private float mTouchOffsetX;
85
86 /** Y offset from the upper-left corner of the cell to where we touched. */
87 private float mTouchOffsetY;
88
89 /** Where the drag originated */
90 private DragSource mDragSource;
91
92 /** The data associated with the object being dragged */
93 private Object mDragInfo;
94
95 /** The view that moves around while you drag. */
96 private DragView mDragView;
97
98 /** Who can receive drop events */
99 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
100
101 private DragListener mListener;
102
103 /** The window token used as the parent for the DragView. */
104 private IBinder mWindowToken;
105
106 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
107 private View mScrollView;
108
109 private DragScroller mDragScroller;
110 private int mScrollState = SCROLL_OUTSIDE_ZONE;
111 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
112
113 private RectF mDeleteRegion;
114 private DropTarget mLastDropTarget;
115
116 private InputMethodManager mInputMethodManager;
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 /**
119 * Interface to receive notifications when a drag starts or stops
120 */
121 interface DragListener {
122
123 /**
124 * A drag has begun
125 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 * @param source An object representing where the drag originated
127 * @param info The data associated with the object that is being dragged
128 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
129 * or {@link DragController#DRAG_ACTION_COPY}
130 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700131 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 /**
134 * The drag has eneded
135 */
136 void onDragEnd();
137 }
138
139 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400140 * Used to create a new DragLayer from XML.
141 *
142 * @param context The application's context.
143 * @param attrs The attribtues set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 */
Joe Onorato00acb122009-08-04 16:04:30 -0400145 public DragController(Context context) {
146 mContext = context;
147 mHandler = new Handler();
148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
150 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700151 * Starts a drag.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 *
153 * @param v The view that is being dragged
154 * @param source An object representing where the drag originated
155 * @param info The data associated with the object that is being dragged
156 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
157 * {@link #DRAG_ACTION_COPY}
158 */
Joe Onorato00acb122009-08-04 16:04:30 -0400159 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700160 mOriginator = v;
161
162 Bitmap b = getViewBitmap(v);
163
164 int[] loc = mCoordinatesTemp;
165 v.getLocationOnScreen(loc);
166 int screenX = loc[0];
167 int screenY = loc[1];
168
169 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
170 source, dragInfo, dragAction);
171
172 b.recycle();
173
174 if (dragAction == DRAG_ACTION_MOVE) {
175 v.setVisibility(View.GONE);
176 }
177 }
178
179 /**
180 * Starts a drag.
181 *
182 * @param b The bitmap to display as the drag image. It will be re-scaled to the
183 * enlarged size.
184 * @param screenX The x position on screen of the left-top of the bitmap.
185 * @param screenY The y position on screen of the left-top of the bitmap.
186 * @param textureLeft The left edge of the region inside b to use.
187 * @param textureTop The top edge of the region inside b to use.
188 * @param textureWidth The width of the region inside b to use.
189 * @param textureHeight The height of the region inside b to use.
190 * @param source An object representing where the drag originated
191 * @param info The data associated with the object that is being dragged
192 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
193 * {@link #DRAG_ACTION_COPY}
194 */
195 public void startDrag(Bitmap b, int screenX, int screenY,
196 int textureLeft, int textureTop, int textureWidth, int textureHeight,
197 DragSource source, Object dragInfo, int dragAction) {
Joe Onorato00acb122009-08-04 16:04:30 -0400198 if (PROFILE_DRAWING_DURING_DRAG) {
199 android.os.Debug.startMethodTracing("Launcher");
200 }
201
202 // Hide soft keyboard, if visible
203 if (mInputMethodManager == null) {
204 mInputMethodManager = (InputMethodManager)
205 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
206 }
207 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
208
209 if (mListener != null) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700210 mListener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400211 }
212
Joe Onorato00acb122009-08-04 16:04:30 -0400213 int registrationX = ((int)mMotionDownX) - screenX;
214 int registrationY = ((int)mMotionDownY) - screenY;
215
216 mTouchOffsetX = mMotionDownX - screenX;
217 mTouchOffsetY = mMotionDownY - screenY;
218
219 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400220 mDragSource = source;
221 mDragInfo = dragInfo;
222
223 mVibrator.vibrate(VIBRATE_DURATION);
224
Joe Onorato5162ea92009-09-03 09:39:42 -0700225 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
226 textureLeft, textureTop, textureWidth, textureHeight);
Joe Onorato00acb122009-08-04 16:04:30 -0400227 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400228 }
229
230 /**
231 * Draw the view into a bitmap.
232 */
233 private Bitmap getViewBitmap(View v) {
234 v.clearFocus();
235 v.setPressed(false);
236
237 boolean willNotCache = v.willNotCacheDrawing();
238 v.setWillNotCacheDrawing(false);
239
240 // Reset the drawing cache background color to fully transparent
241 // for the duration of this operation
242 int color = v.getDrawingCacheBackgroundColor();
243 v.setDrawingCacheBackgroundColor(0);
244
245 if (color != 0) {
246 v.destroyDrawingCache();
247 }
248 v.buildDrawingCache();
249 Bitmap cacheBitmap = v.getDrawingCache();
250
251 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
252
253 // Restore the view
254 v.destroyDrawingCache();
255 v.setWillNotCacheDrawing(willNotCache);
256 v.setDrawingCacheBackgroundColor(color);
257
258 return bitmap;
259 }
260
261 /**
262 * Call this from a drag source view like this:
263 *
264 * <pre>
265 * @Override
266 * public boolean dispatchKeyEvent(KeyEvent event) {
267 * return mDragController.dispatchKeyEvent(this, event)
268 * || super.dispatchKeyEvent(event);
269 * </pre>
270 */
271 public boolean dispatchKeyEvent(KeyEvent event) {
272 return mDragging;
273 }
274
275 private void endDrag() {
276 if (mDragging) {
277 mDragging = false;
278 if (mOriginator != null) {
279 mOriginator.setVisibility(View.VISIBLE);
280 }
281 if (mListener != null) {
282 mListener.onDragEnd();
283 }
284 if (mDragView != null) {
285 mDragView.remove();
286 mDragView = null;
287 }
Joe Onorato00acb122009-08-04 16:04:30 -0400288 }
289 }
290
291 /**
292 * Call this from a drag source view.
293 */
294 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 if (false) {
296 Log.d(Launcher.LOG_TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
297 + mDragging);
298 }
Joe Onorato00acb122009-08-04 16:04:30 -0400299 final int action = ev.getAction();
300
301 final float screenX = ev.getRawX();
302 final float screenY = ev.getRawY();
303
304 switch (action) {
305 case MotionEvent.ACTION_MOVE:
306 break;
307
308 case MotionEvent.ACTION_DOWN:
309 // Remember location of down touch
310 mMotionDownX = screenX;
311 mMotionDownY = screenY;
312 mLastDropTarget = null;
313 break;
314
315 case MotionEvent.ACTION_CANCEL:
316 case MotionEvent.ACTION_UP:
317 if (mDragging) {
318 drop(screenX, screenY);
319 }
320 endDrag();
321 break;
322 }
323
324 return mDragging;
325 }
326
327 /**
328 * Call this from a drag source view.
329 */
330 public boolean onTouchEvent(MotionEvent ev) {
331 View scrollView = mScrollView;
332
333 if (!mDragging) {
334 return false;
335 }
336
337 final int action = ev.getAction();
338 final float x = ev.getRawX();
339 final float y = ev.getRawY();
340
341 switch (action) {
342 case MotionEvent.ACTION_DOWN:
343
344 // Remember where the motion event started
345 mMotionDownX = x;
346 mMotionDownY = y;
347
348 if ((x < SCROLL_ZONE) || (x > scrollView.getWidth() - SCROLL_ZONE)) {
349 mScrollState = SCROLL_WAITING_IN_ZONE;
350 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
351 } else {
352 mScrollState = SCROLL_OUTSIDE_ZONE;
353 }
354
355 break;
356 case MotionEvent.ACTION_MOVE:
357 // Update the drag view.
358 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
359
360 // Drop on someone?
361 final int[] coordinates = mCoordinatesTemp;
362 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
363 if (dropTarget != null) {
364 if (mLastDropTarget == dropTarget) {
365 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
366 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
367 } else {
368 if (mLastDropTarget != null) {
369 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
370 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
371 }
372 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
373 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
374 }
375 } else {
376 if (mLastDropTarget != null) {
377 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
378 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
379 }
380 }
381 mLastDropTarget = dropTarget;
382
383 // Scroll, maybe, but not if we're in the delete region.
384 boolean inDeleteRegion = false;
385 if (mDeleteRegion != null) {
386 inDeleteRegion = mDeleteRegion.contains(ev.getRawX(), ev.getRawY());
387 }
388 if (!inDeleteRegion && x < SCROLL_ZONE) {
389 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
390 mScrollState = SCROLL_WAITING_IN_ZONE;
391 mScrollRunnable.setDirection(SCROLL_LEFT);
392 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
393 }
394 } else if (!inDeleteRegion && x > scrollView.getWidth() - SCROLL_ZONE) {
395 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
396 mScrollState = SCROLL_WAITING_IN_ZONE;
397 mScrollRunnable.setDirection(SCROLL_RIGHT);
398 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
399 }
400 } else {
401 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
402 mScrollState = SCROLL_OUTSIDE_ZONE;
403 mScrollRunnable.setDirection(SCROLL_RIGHT);
404 mHandler.removeCallbacks(mScrollRunnable);
405 }
406 }
407
408 break;
409 case MotionEvent.ACTION_UP:
410 mHandler.removeCallbacks(mScrollRunnable);
411 if (mDragging) {
412 drop(x, y);
413 }
414 endDrag();
415
416 break;
417 case MotionEvent.ACTION_CANCEL:
418 endDrag();
419 }
420
421 return true;
422 }
423
424 private boolean drop(float x, float y) {
425 final int[] coordinates = mCoordinatesTemp;
426 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
427
428 if (dropTarget != null) {
429 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
430 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
431 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
432 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
433 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
434 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
435 mDragSource.onDropCompleted((View) dropTarget, true);
436 return true;
437 } else {
438 mDragSource.onDropCompleted((View) dropTarget, false);
439 return true;
440 }
441 }
442 return false;
443 }
444
445 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
446 final Rect r = mRectTemp;
447
448 final ArrayList<DropTarget> dropTargets = mDropTargets;
449 final int count = dropTargets.size();
450 for (int i=count-1; i>=0; i--) {
451 final DropTarget target = dropTargets.get(i);
452 target.getHitRect(r);
453 target.getLocationOnScreen(dropCoordinates);
454 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
455 if (r.contains(x, y)) {
456 dropCoordinates[0] = x - dropCoordinates[0];
457 dropCoordinates[1] = y - dropCoordinates[1];
458 return target;
459 }
460 }
461 return null;
462 }
463
464 public void setDragScoller(DragScroller scroller) {
465 mDragScroller = scroller;
466 }
467
468 public void setWindowToken(IBinder token) {
469 mWindowToken = token;
470 }
471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 /**
473 * Sets the drag listner which will be notified when a drag starts or ends.
474 */
Joe Onorato00acb122009-08-04 16:04:30 -0400475 public void setDragListener(DragListener l) {
476 mListener = l;
477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 /**
480 * Remove a previously installed drag listener.
481 */
Joe Onorato00acb122009-08-04 16:04:30 -0400482 public void removeDragListener(DragListener l) {
483 mListener = null;
484 }
485
486 /**
487 * Add a DropTarget to the list of potential places to receive drop events.
488 */
489 public void addDropTarget(DropTarget target) {
490 mDropTargets.add(target);
491 }
492
493 /**
494 * Don't send drop events to <em>target</em> any more.
495 */
496 public void removeDropTarget(DropTarget target) {
497 mDropTargets.remove(target);
498 }
499
500 /**
501 * Set which view scrolls for touch events near the edge of the screen.
502 */
503 public void setScrollView(View v) {
504 mScrollView = v;
505 }
506
507 /**
508 * Specifies the delete region. We won't scroll on touch events over the delete region.
509 *
510 * @param region The rectangle in screen coordinates of the delete region.
511 */
512 void setDeleteRegion(RectF region) {
513 mDeleteRegion = region;
514 }
515
516 private class ScrollRunnable implements Runnable {
517 private int mDirection;
518
519 ScrollRunnable() {
520 }
521
522 public void run() {
523 if (mDragScroller != null) {
524 if (mDirection == SCROLL_LEFT) {
525 mDragScroller.scrollLeft();
526 } else {
527 mDragScroller.scrollRight();
528 }
529 mScrollState = SCROLL_OUTSIDE_ZONE;
530 }
531 }
532
533 void setDirection(int direction) {
534 mDirection = direction;
535 }
536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537}