blob: da2df5c97b54e01d5476831315a6a82a2a8a3a4a [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) {
276 final int action = ev.getAction();
277
278 final float screenX = ev.getRawX();
279 final float screenY = ev.getRawY();
280
281 switch (action) {
282 case MotionEvent.ACTION_MOVE:
283 break;
284
285 case MotionEvent.ACTION_DOWN:
286 // Remember location of down touch
287 mMotionDownX = screenX;
288 mMotionDownY = screenY;
289 mLastDropTarget = null;
290 break;
291
292 case MotionEvent.ACTION_CANCEL:
293 case MotionEvent.ACTION_UP:
294 if (mDragging) {
295 drop(screenX, screenY);
296 }
297 endDrag();
298 break;
299 }
300
301 return mDragging;
302 }
303
304 /**
305 * Call this from a drag source view.
306 */
307 public boolean onTouchEvent(MotionEvent ev) {
308 View scrollView = mScrollView;
309
310 if (!mDragging) {
311 return false;
312 }
313
314 final int action = ev.getAction();
315 final float x = ev.getRawX();
316 final float y = ev.getRawY();
317
318 switch (action) {
319 case MotionEvent.ACTION_DOWN:
320
321 // Remember where the motion event started
322 mMotionDownX = x;
323 mMotionDownY = y;
324
325 if ((x < SCROLL_ZONE) || (x > scrollView.getWidth() - SCROLL_ZONE)) {
326 mScrollState = SCROLL_WAITING_IN_ZONE;
327 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
328 } else {
329 mScrollState = SCROLL_OUTSIDE_ZONE;
330 }
331
332 break;
333 case MotionEvent.ACTION_MOVE:
334 // Update the drag view.
335 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
336
337 // Drop on someone?
338 final int[] coordinates = mCoordinatesTemp;
339 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
340 if (dropTarget != null) {
341 if (mLastDropTarget == dropTarget) {
342 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
343 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
344 } else {
345 if (mLastDropTarget != null) {
346 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
347 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
348 }
349 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
350 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
351 }
352 } else {
353 if (mLastDropTarget != null) {
354 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
355 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
356 }
357 }
358 mLastDropTarget = dropTarget;
359
360 // Scroll, maybe, but not if we're in the delete region.
361 boolean inDeleteRegion = false;
362 if (mDeleteRegion != null) {
363 inDeleteRegion = mDeleteRegion.contains(ev.getRawX(), ev.getRawY());
364 }
365 if (!inDeleteRegion && x < SCROLL_ZONE) {
366 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
367 mScrollState = SCROLL_WAITING_IN_ZONE;
368 mScrollRunnable.setDirection(SCROLL_LEFT);
369 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
370 }
371 } else if (!inDeleteRegion && x > scrollView.getWidth() - SCROLL_ZONE) {
372 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
373 mScrollState = SCROLL_WAITING_IN_ZONE;
374 mScrollRunnable.setDirection(SCROLL_RIGHT);
375 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
376 }
377 } else {
378 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
379 mScrollState = SCROLL_OUTSIDE_ZONE;
380 mScrollRunnable.setDirection(SCROLL_RIGHT);
381 mHandler.removeCallbacks(mScrollRunnable);
382 }
383 }
384
385 break;
386 case MotionEvent.ACTION_UP:
387 mHandler.removeCallbacks(mScrollRunnable);
388 if (mDragging) {
389 drop(x, y);
390 }
391 endDrag();
392
393 break;
394 case MotionEvent.ACTION_CANCEL:
395 endDrag();
396 }
397
398 return true;
399 }
400
401 private boolean drop(float x, float y) {
402 final int[] coordinates = mCoordinatesTemp;
403 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
404
405 if (dropTarget != null) {
406 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
407 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
408 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
409 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
410 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
411 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
412 mDragSource.onDropCompleted((View) dropTarget, true);
413 return true;
414 } else {
415 mDragSource.onDropCompleted((View) dropTarget, false);
416 return true;
417 }
418 }
419 return false;
420 }
421
422 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
423 final Rect r = mRectTemp;
424
425 final ArrayList<DropTarget> dropTargets = mDropTargets;
426 final int count = dropTargets.size();
427 for (int i=count-1; i>=0; i--) {
428 final DropTarget target = dropTargets.get(i);
429 target.getHitRect(r);
430 target.getLocationOnScreen(dropCoordinates);
431 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
432 if (r.contains(x, y)) {
433 dropCoordinates[0] = x - dropCoordinates[0];
434 dropCoordinates[1] = y - dropCoordinates[1];
435 return target;
436 }
437 }
438 return null;
439 }
440
441 public void setDragScoller(DragScroller scroller) {
442 mDragScroller = scroller;
443 }
444
445 public void setWindowToken(IBinder token) {
446 mWindowToken = token;
447 }
448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 /**
450 * Sets the drag listner which will be notified when a drag starts or ends.
451 */
Joe Onorato00acb122009-08-04 16:04:30 -0400452 public void setDragListener(DragListener l) {
453 mListener = l;
454 }
455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 /**
457 * Remove a previously installed drag listener.
458 */
Joe Onorato00acb122009-08-04 16:04:30 -0400459 public void removeDragListener(DragListener l) {
460 mListener = null;
461 }
462
463 /**
464 * Add a DropTarget to the list of potential places to receive drop events.
465 */
466 public void addDropTarget(DropTarget target) {
467 mDropTargets.add(target);
468 }
469
470 /**
471 * Don't send drop events to <em>target</em> any more.
472 */
473 public void removeDropTarget(DropTarget target) {
474 mDropTargets.remove(target);
475 }
476
477 /**
478 * Set which view scrolls for touch events near the edge of the screen.
479 */
480 public void setScrollView(View v) {
481 mScrollView = v;
482 }
483
484 /**
485 * Specifies the delete region. We won't scroll on touch events over the delete region.
486 *
487 * @param region The rectangle in screen coordinates of the delete region.
488 */
489 void setDeleteRegion(RectF region) {
490 mDeleteRegion = region;
491 }
492
493 private class ScrollRunnable implements Runnable {
494 private int mDirection;
495
496 ScrollRunnable() {
497 }
498
499 public void run() {
500 if (mDragScroller != null) {
501 if (mDirection == SCROLL_LEFT) {
502 mDragScroller.scrollLeft();
503 } else {
504 mDragScroller.scrollRight();
505 }
506 mScrollState = SCROLL_OUTSIDE_ZONE;
507 }
508 }
509
510 void setDirection(int direction) {
511 mDirection = direction;
512 }
513 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514}