blob: 590ca6927050eeedb901ef346096a2f3f800201f [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
Joe Onorato24b6fd82009-11-12 13:47:09 -0800282 /**
283 * Stop dragging without dropping.
284 */
285 public void cancelDrag() {
286 endDrag();
287 }
288
Joe Onorato00acb122009-08-04 16:04:30 -0400289 private void endDrag() {
290 if (mDragging) {
291 mDragging = false;
292 if (mOriginator != null) {
293 mOriginator.setVisibility(View.VISIBLE);
294 }
295 if (mListener != null) {
296 mListener.onDragEnd();
297 }
298 if (mDragView != null) {
299 mDragView.remove();
300 mDragView = null;
301 }
Joe Onorato00acb122009-08-04 16:04:30 -0400302 }
303 }
304
305 /**
306 * Call this from a drag source view.
307 */
308 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800310 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311 + mDragging);
312 }
Joe Onorato00acb122009-08-04 16:04:30 -0400313 final int action = ev.getAction();
314
Joe Onorato87467d32009-11-08 14:36:43 -0500315 if (action == MotionEvent.ACTION_DOWN) {
316 recordScreenSize();
317 }
318
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700319 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
320 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400321
322 switch (action) {
323 case MotionEvent.ACTION_MOVE:
324 break;
325
326 case MotionEvent.ACTION_DOWN:
327 // Remember location of down touch
328 mMotionDownX = screenX;
329 mMotionDownY = screenY;
330 mLastDropTarget = null;
331 break;
332
333 case MotionEvent.ACTION_CANCEL:
334 case MotionEvent.ACTION_UP:
335 if (mDragging) {
336 drop(screenX, screenY);
337 }
338 endDrag();
339 break;
340 }
341
342 return mDragging;
343 }
344
345 /**
346 * Call this from a drag source view.
347 */
348 public boolean onTouchEvent(MotionEvent ev) {
349 View scrollView = mScrollView;
350
351 if (!mDragging) {
352 return false;
353 }
354
355 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700356 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
357 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400358
359 switch (action) {
360 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400361 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700362 mMotionDownX = screenX;
363 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400364
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700365 if ((screenX < SCROLL_ZONE) || (screenX > scrollView.getWidth() - SCROLL_ZONE)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400366 mScrollState = SCROLL_WAITING_IN_ZONE;
367 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
368 } else {
369 mScrollState = SCROLL_OUTSIDE_ZONE;
370 }
371
372 break;
373 case MotionEvent.ACTION_MOVE:
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700374 // Update the drag view. Don't use the clamped pos here so the dragging looks
375 // like it goes off screen a little, intead of bumping up against the edge.
Joe Onorato00acb122009-08-04 16:04:30 -0400376 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
377
378 // Drop on someone?
379 final int[] coordinates = mCoordinatesTemp;
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700380 DropTarget dropTarget = findDropTarget((int) screenX, (int) screenY, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400381 if (dropTarget != null) {
382 if (mLastDropTarget == dropTarget) {
383 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
384 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
385 } else {
386 if (mLastDropTarget != null) {
387 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
388 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
389 }
390 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
391 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
392 }
393 } else {
394 if (mLastDropTarget != null) {
395 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
396 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
397 }
398 }
399 mLastDropTarget = dropTarget;
400
401 // Scroll, maybe, but not if we're in the delete region.
402 boolean inDeleteRegion = false;
403 if (mDeleteRegion != null) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700404 inDeleteRegion = mDeleteRegion.contains(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400405 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700406 if (!inDeleteRegion && screenX < SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400407 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
408 mScrollState = SCROLL_WAITING_IN_ZONE;
409 mScrollRunnable.setDirection(SCROLL_LEFT);
410 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
411 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700412 } else if (!inDeleteRegion && screenX > scrollView.getWidth() - SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400413 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
414 mScrollState = SCROLL_WAITING_IN_ZONE;
415 mScrollRunnable.setDirection(SCROLL_RIGHT);
416 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
417 }
418 } else {
419 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
420 mScrollState = SCROLL_OUTSIDE_ZONE;
421 mScrollRunnable.setDirection(SCROLL_RIGHT);
422 mHandler.removeCallbacks(mScrollRunnable);
423 }
424 }
425
426 break;
427 case MotionEvent.ACTION_UP:
428 mHandler.removeCallbacks(mScrollRunnable);
429 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700430 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400431 }
432 endDrag();
433
434 break;
435 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800436 cancelDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400437 }
438
439 return true;
440 }
441
442 private boolean drop(float x, float y) {
443 final int[] coordinates = mCoordinatesTemp;
444 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
445
446 if (dropTarget != null) {
447 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
448 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
449 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
450 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
451 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
452 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
453 mDragSource.onDropCompleted((View) dropTarget, true);
454 return true;
455 } else {
456 mDragSource.onDropCompleted((View) dropTarget, false);
457 return true;
458 }
459 }
460 return false;
461 }
462
463 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
464 final Rect r = mRectTemp;
465
466 final ArrayList<DropTarget> dropTargets = mDropTargets;
467 final int count = dropTargets.size();
468 for (int i=count-1; i>=0; i--) {
469 final DropTarget target = dropTargets.get(i);
470 target.getHitRect(r);
471 target.getLocationOnScreen(dropCoordinates);
472 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
473 if (r.contains(x, y)) {
474 dropCoordinates[0] = x - dropCoordinates[0];
475 dropCoordinates[1] = y - dropCoordinates[1];
476 return target;
477 }
478 }
479 return null;
480 }
481
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700482 /**
483 * Get the screen size so we can clamp events to the screen size so even if
484 * you drag off the edge of the screen, we find something.
485 */
486 private void recordScreenSize() {
487 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
488 .getDefaultDisplay().getMetrics(mDisplayMetrics);
489 }
490
491 /**
492 * Clamp val to be &gt;= min and &lt; max.
493 */
494 private static int clamp(int val, int min, int max) {
495 if (val < min) {
496 return min;
497 } else if (val >= max) {
498 return max - 1;
499 } else {
500 return val;
501 }
502 }
503
Joe Onorato00acb122009-08-04 16:04:30 -0400504 public void setDragScoller(DragScroller scroller) {
505 mDragScroller = scroller;
506 }
507
508 public void setWindowToken(IBinder token) {
509 mWindowToken = token;
510 }
511
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 /**
513 * Sets the drag listner which will be notified when a drag starts or ends.
514 */
Joe Onorato00acb122009-08-04 16:04:30 -0400515 public void setDragListener(DragListener l) {
516 mListener = l;
517 }
518
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 /**
520 * Remove a previously installed drag listener.
521 */
Joe Onorato00acb122009-08-04 16:04:30 -0400522 public void removeDragListener(DragListener l) {
523 mListener = null;
524 }
525
526 /**
527 * Add a DropTarget to the list of potential places to receive drop events.
528 */
529 public void addDropTarget(DropTarget target) {
530 mDropTargets.add(target);
531 }
532
533 /**
534 * Don't send drop events to <em>target</em> any more.
535 */
536 public void removeDropTarget(DropTarget target) {
537 mDropTargets.remove(target);
538 }
539
540 /**
541 * Set which view scrolls for touch events near the edge of the screen.
542 */
543 public void setScrollView(View v) {
544 mScrollView = v;
545 }
546
547 /**
548 * Specifies the delete region. We won't scroll on touch events over the delete region.
549 *
550 * @param region The rectangle in screen coordinates of the delete region.
551 */
552 void setDeleteRegion(RectF region) {
553 mDeleteRegion = region;
554 }
555
556 private class ScrollRunnable implements Runnable {
557 private int mDirection;
558
559 ScrollRunnable() {
560 }
561
562 public void run() {
563 if (mDragScroller != null) {
564 if (mDirection == SCROLL_LEFT) {
565 mDragScroller.scrollLeft();
566 } else {
567 mDragScroller.scrollRight();
568 }
569 mScrollState = SCROLL_OUTSIDE_ZONE;
570 }
571 }
572
573 void setDirection(int direction) {
574 mDirection = direction;
575 }
576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577}