blob: f2fad9a083a35576565ccc2c545256b87c0c353b [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;
Joe Onorato00acb122009-08-04 16:04:30 -040021import android.graphics.Rect;
22import android.graphics.RectF;
23import android.os.IBinder;
24import android.os.Handler;
25import android.os.Vibrator;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070026import android.util.DisplayMetrics;
Joe Onorato00acb122009-08-04 16:04:30 -040027import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.view.View;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.view.KeyEvent;
30import android.view.MotionEvent;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070031import android.view.WindowManager;
Joe Onorato00acb122009-08-04 16:04:30 -040032import android.view.inputmethod.InputMethodManager;
Joe Onorato00acb122009-08-04 16:04:30 -040033
34import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035
36/**
Joe Onorato00acb122009-08-04 16:04:30 -040037 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038 */
Joe Onorato00acb122009-08-04 16:04:30 -040039public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080040 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070041 private static final String TAG = "Launcher.DragController";
42
Joe Onorato00acb122009-08-04 16:04:30 -040043 /** Indicates the drag is a move. */
44 public static int DRAG_ACTION_MOVE = 0;
45
46 /** Indicates the drag is a copy. */
47 public static int DRAG_ACTION_COPY = 1;
48
49 private static final int SCROLL_DELAY = 600;
50 private static final int SCROLL_ZONE = 20;
51 private static final int VIBRATE_DURATION = 35;
52
53 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
54
55 private static final int SCROLL_OUTSIDE_ZONE = 0;
56 private static final int SCROLL_WAITING_IN_ZONE = 1;
57
58 private static final int SCROLL_LEFT = 0;
59 private static final int SCROLL_RIGHT = 1;
60
61 private Context mContext;
62 private Handler mHandler;
63 private final Vibrator mVibrator = new Vibrator();
64
65 // temporaries to avoid gc thrash
66 private Rect mRectTemp = new Rect();
67 private final int[] mCoordinatesTemp = new int[2];
68
69 /** Whether or not we're dragging. */
70 private boolean mDragging;
71
72 /** X coordinate of the down event. */
73 private float mMotionDownX;
74
75 /** Y coordinate of the down event. */
76 private float mMotionDownY;
77
Joe Onoratoe048e8a2009-09-25 10:39:17 -070078 /** Info about the screen for clamping. */
79 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
80
Joe Onorato00acb122009-08-04 16:04:30 -040081 /** Original view that is being dragged. */
82 private View mOriginator;
83
Joe Onorato00acb122009-08-04 16:04:30 -040084 /** X offset from the upper-left corner of the cell to where we touched. */
85 private float mTouchOffsetX;
86
87 /** Y offset from the upper-left corner of the cell to where we touched. */
88 private float mTouchOffsetY;
89
90 /** Where the drag originated */
91 private DragSource mDragSource;
92
93 /** The data associated with the object being dragged */
94 private Object mDragInfo;
95
96 /** The view that moves around while you drag. */
97 private DragView mDragView;
98
99 /** Who can receive drop events */
100 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
101
102 private DragListener mListener;
103
104 /** The window token used as the parent for the DragView. */
105 private IBinder mWindowToken;
106
107 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
108 private View mScrollView;
109
Romain Guyea3763c2010-01-11 18:02:04 -0800110 private View mMoveTarget;
111
Joe Onorato00acb122009-08-04 16:04:30 -0400112 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 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 * @param source An object representing where the drag originated
130 * @param info The data associated with the object that is being dragged
131 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
132 * or {@link DragController#DRAG_ACTION_COPY}
133 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700134 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 /**
137 * The drag has eneded
138 */
139 void onDragEnd();
140 }
141
142 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400143 * Used to create a new DragLayer from XML.
144 *
145 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 */
Joe Onorato00acb122009-08-04 16:04:30 -0400147 public DragController(Context context) {
148 mContext = context;
149 mHandler = new Handler();
150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
152 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700153 * Starts a drag.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 *
155 * @param v The view that is being dragged
156 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800157 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
159 * {@link #DRAG_ACTION_COPY}
160 */
Joe Onorato00acb122009-08-04 16:04:30 -0400161 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700162 mOriginator = v;
163
164 Bitmap b = getViewBitmap(v);
165
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400166 if (b == null) {
167 // out of memory?
168 return;
169 }
170
Joe Onorato5162ea92009-09-03 09:39:42 -0700171 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
Romain Guyea3763c2010-01-11 18:02:04 -0800198 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700199 * @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();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400257 if (cacheBitmap == null) {
258 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
259 return null;
260 }
Joe Onorato00acb122009-08-04 16:04:30 -0400261
262 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
263
264 // Restore the view
265 v.destroyDrawingCache();
266 v.setWillNotCacheDrawing(willNotCache);
267 v.setDrawingCacheBackgroundColor(color);
268
269 return bitmap;
270 }
271
272 /**
273 * Call this from a drag source view like this:
274 *
275 * <pre>
276 * @Override
277 * public boolean dispatchKeyEvent(KeyEvent event) {
278 * return mDragController.dispatchKeyEvent(this, event)
279 * || super.dispatchKeyEvent(event);
280 * </pre>
281 */
Romain Guyea3763c2010-01-11 18:02:04 -0800282 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400283 public boolean dispatchKeyEvent(KeyEvent event) {
284 return mDragging;
285 }
286
Joe Onorato24b6fd82009-11-12 13:47:09 -0800287 /**
288 * Stop dragging without dropping.
289 */
290 public void cancelDrag() {
291 endDrag();
292 }
293
Joe Onorato00acb122009-08-04 16:04:30 -0400294 private void endDrag() {
295 if (mDragging) {
296 mDragging = false;
297 if (mOriginator != null) {
298 mOriginator.setVisibility(View.VISIBLE);
299 }
300 if (mListener != null) {
301 mListener.onDragEnd();
302 }
303 if (mDragView != null) {
304 mDragView.remove();
305 mDragView = null;
306 }
Joe Onorato00acb122009-08-04 16:04:30 -0400307 }
308 }
309
310 /**
311 * Call this from a drag source view.
312 */
313 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400314 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800315 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316 + mDragging);
317 }
Joe Onorato00acb122009-08-04 16:04:30 -0400318 final int action = ev.getAction();
319
Joe Onorato87467d32009-11-08 14:36:43 -0500320 if (action == MotionEvent.ACTION_DOWN) {
321 recordScreenSize();
322 }
323
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700324 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
325 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400326
327 switch (action) {
328 case MotionEvent.ACTION_MOVE:
329 break;
330
331 case MotionEvent.ACTION_DOWN:
332 // Remember location of down touch
333 mMotionDownX = screenX;
334 mMotionDownY = screenY;
335 mLastDropTarget = null;
336 break;
337
338 case MotionEvent.ACTION_CANCEL:
339 case MotionEvent.ACTION_UP:
340 if (mDragging) {
341 drop(screenX, screenY);
342 }
343 endDrag();
344 break;
345 }
346
347 return mDragging;
348 }
349
350 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800351 * Sets the view that should handle move events.
352 */
353 void setMoveTarget(View view) {
354 mMoveTarget = view;
355 }
356
357 public boolean dispatchUnhandledMove(View focused, int direction) {
358 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
359 }
360
361 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400362 * Call this from a drag source view.
363 */
364 public boolean onTouchEvent(MotionEvent ev) {
365 View scrollView = mScrollView;
366
367 if (!mDragging) {
368 return false;
369 }
370
371 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700372 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
373 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400374
375 switch (action) {
376 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400377 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700378 mMotionDownX = screenX;
379 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400380
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700381 if ((screenX < SCROLL_ZONE) || (screenX > scrollView.getWidth() - SCROLL_ZONE)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400382 mScrollState = SCROLL_WAITING_IN_ZONE;
383 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
384 } else {
385 mScrollState = SCROLL_OUTSIDE_ZONE;
386 }
387
388 break;
389 case MotionEvent.ACTION_MOVE:
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700390 // Update the drag view. Don't use the clamped pos here so the dragging looks
391 // like it goes off screen a little, intead of bumping up against the edge.
Joe Onorato00acb122009-08-04 16:04:30 -0400392 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
393
394 // Drop on someone?
395 final int[] coordinates = mCoordinatesTemp;
Romain Guyea3763c2010-01-11 18:02:04 -0800396 DropTarget dropTarget = findDropTarget(screenX, screenY, coordinates);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700397 DropTarget delegate = dropTarget.getDropTargetDelegate(
398 mDragSource, coordinates[0], coordinates[1],
399 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
400 if (delegate != null) {
401 dropTarget = delegate;
402 }
Joe Onorato00acb122009-08-04 16:04:30 -0400403 if (dropTarget != null) {
404 if (mLastDropTarget == dropTarget) {
405 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
406 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
407 } else {
408 if (mLastDropTarget != null) {
409 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
410 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
411 }
412 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
413 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
414 }
415 } else {
416 if (mLastDropTarget != null) {
417 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
418 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
419 }
420 }
421 mLastDropTarget = dropTarget;
422
423 // Scroll, maybe, but not if we're in the delete region.
424 boolean inDeleteRegion = false;
425 if (mDeleteRegion != null) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700426 inDeleteRegion = mDeleteRegion.contains(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400427 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700428 if (!inDeleteRegion && screenX < SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400429 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
430 mScrollState = SCROLL_WAITING_IN_ZONE;
431 mScrollRunnable.setDirection(SCROLL_LEFT);
432 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
433 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700434 } else if (!inDeleteRegion && screenX > scrollView.getWidth() - SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400435 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
436 mScrollState = SCROLL_WAITING_IN_ZONE;
437 mScrollRunnable.setDirection(SCROLL_RIGHT);
438 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
439 }
440 } else {
441 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
442 mScrollState = SCROLL_OUTSIDE_ZONE;
443 mScrollRunnable.setDirection(SCROLL_RIGHT);
444 mHandler.removeCallbacks(mScrollRunnable);
445 }
446 }
447
448 break;
449 case MotionEvent.ACTION_UP:
450 mHandler.removeCallbacks(mScrollRunnable);
451 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700452 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400453 }
454 endDrag();
455
456 break;
457 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800458 cancelDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400459 }
460
461 return true;
462 }
463
464 private boolean drop(float x, float y) {
465 final int[] coordinates = mCoordinatesTemp;
466 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
467
468 if (dropTarget != null) {
469 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
470 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
471 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
472 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
473 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
474 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
475 mDragSource.onDropCompleted((View) dropTarget, true);
476 return true;
477 } else {
478 mDragSource.onDropCompleted((View) dropTarget, false);
479 return true;
480 }
481 }
482 return false;
483 }
484
485 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
486 final Rect r = mRectTemp;
487
488 final ArrayList<DropTarget> dropTargets = mDropTargets;
489 final int count = dropTargets.size();
490 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700491 DropTarget target = dropTargets.get(i);
Joe Onorato00acb122009-08-04 16:04:30 -0400492 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700493
494 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400495 target.getLocationOnScreen(dropCoordinates);
496 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700497
Joe Onorato00acb122009-08-04 16:04:30 -0400498 if (r.contains(x, y)) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700499 DropTarget delegate = target.getDropTargetDelegate(mDragSource,
500 x, y, (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
501 if (delegate != null) {
502 target = delegate;
503 target.getLocationOnScreen(dropCoordinates);
504 }
505
506 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400507 dropCoordinates[0] = x - dropCoordinates[0];
508 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700509
Joe Onorato00acb122009-08-04 16:04:30 -0400510 return target;
511 }
512 }
513 return null;
514 }
515
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700516 /**
517 * Get the screen size so we can clamp events to the screen size so even if
518 * you drag off the edge of the screen, we find something.
519 */
520 private void recordScreenSize() {
521 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
522 .getDefaultDisplay().getMetrics(mDisplayMetrics);
523 }
524
525 /**
526 * Clamp val to be &gt;= min and &lt; max.
527 */
528 private static int clamp(int val, int min, int max) {
529 if (val < min) {
530 return min;
531 } else if (val >= max) {
532 return max - 1;
533 } else {
534 return val;
535 }
536 }
537
Joe Onorato00acb122009-08-04 16:04:30 -0400538 public void setDragScoller(DragScroller scroller) {
539 mDragScroller = scroller;
540 }
541
542 public void setWindowToken(IBinder token) {
543 mWindowToken = token;
544 }
545
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 /**
547 * Sets the drag listner which will be notified when a drag starts or ends.
548 */
Joe Onorato00acb122009-08-04 16:04:30 -0400549 public void setDragListener(DragListener l) {
550 mListener = l;
551 }
552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 /**
554 * Remove a previously installed drag listener.
555 */
Joe Onorato00acb122009-08-04 16:04:30 -0400556 public void removeDragListener(DragListener l) {
557 mListener = null;
558 }
559
560 /**
561 * Add a DropTarget to the list of potential places to receive drop events.
562 */
563 public void addDropTarget(DropTarget target) {
564 mDropTargets.add(target);
565 }
566
567 /**
568 * Don't send drop events to <em>target</em> any more.
569 */
570 public void removeDropTarget(DropTarget target) {
571 mDropTargets.remove(target);
572 }
573
574 /**
575 * Set which view scrolls for touch events near the edge of the screen.
576 */
577 public void setScrollView(View v) {
578 mScrollView = v;
579 }
580
581 /**
582 * Specifies the delete region. We won't scroll on touch events over the delete region.
583 *
584 * @param region The rectangle in screen coordinates of the delete region.
585 */
586 void setDeleteRegion(RectF region) {
587 mDeleteRegion = region;
588 }
589
590 private class ScrollRunnable implements Runnable {
591 private int mDirection;
592
593 ScrollRunnable() {
594 }
595
596 public void run() {
597 if (mDragScroller != null) {
598 if (mDirection == SCROLL_LEFT) {
599 mDragScroller.scrollLeft();
600 } else {
601 mDragScroller.scrollRight();
602 }
603 mScrollState = SCROLL_OUTSIDE_ZONE;
604 }
605 }
606
607 void setDirection(int direction) {
608 mDirection = direction;
609 }
610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611}