2 * This is a modified version of a class from the Android
3 * Open Source Project. The original copyright and license information follows.
5 * Copyright (C) 2008 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package com.blahti.drag;
22 import android.content.Context;
23 import android.graphics.Bitmap;
24 import android.graphics.Rect;
25 import android.os.IBinder;
26 import android.util.DisplayMetrics;
27 import android.util.Log;
28 import android.view.View;
29 import android.view.KeyEvent;
30 import android.view.MotionEvent;
31 import android.view.WindowManager;
32 import android.view.inputmethod.InputMethodManager;
34 import java.util.ArrayList;
37 * This class is used to initiate a drag within a view or across multiple views.
38 * When a drag starts it creates a special view (a DragView) that moves around the screen
39 * until the user ends the drag. As feedback to the user, this object causes the device to
40 * vibrate as the drag begins.
44 public class DragController {
45 public enum DragBehavior {
46 MOVE, // indicates the drag is move
47 COPY // indicates the drag is copy
49 public static final String TAG = "DragController";
51 private Context mContext;
52 private Rect mRectTemp = new Rect();
53 private final int[] mCoordinatesTemp = new int[2];
54 private boolean mDragging;
55 private float mMotionDownX;
56 private float mMotionDownY;
57 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
59 /** Original view that is being dragged. */
60 private View mOriginator;
62 /** X offset from the upper-left corner of the cell to where we touched. */
63 private float mTouchOffsetX;
65 /** Y offset from the upper-left corner of the cell to where we touched. */
66 private float mTouchOffsetY;
68 /** Where the drag originated */
69 private DragSource mDragSource;
71 /** The data associated with the object being dragged */
72 private Object mDragInfo;
74 /** The view that moves around while you drag. */
75 private DragView mDragView;
77 /** Who can receive drop events */
78 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
80 private DragListener mListener;
82 /** The window token used as the parent for the DragView. */
83 private IBinder mWindowToken;
85 private View mMoveTarget;
87 private DropTarget mLastDropTarget;
89 private InputMethodManager mInputMethodManager;
93 * Used to create a new DragLayer from XML.
95 * @param context The application's context.
97 public DragController(Context context) {
103 * It creates a bitmap of the view being dragged. That bitmap is what you see moving.
104 * The actual view can be repositioned if that is what the onDrop handle chooses to do.
106 * @param v The view that is being dragged
107 * @param source An object representing where the drag originated
108 * @param dragInfo The data associated with the object that is being dragged
109 * @param dragAction The drag behavior: move or copy
111 public void startDrag(View v, DragSource source, Object dragInfo, DragBehavior dragBehavior) {
112 if (source.allowDrag()) {
114 final Bitmap b = getViewBitmap(v);
116 final int[] loc = mCoordinatesTemp;
117 v.getLocationOnScreen(loc);
118 final int screenX = loc[0];
119 final int screenY = loc[1];
120 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(), source, dragInfo, dragBehavior);
122 if (dragBehavior == DragBehavior.MOVE) {
123 v.setVisibility(View.GONE);
132 * @param b The bitmap to display as the drag image. It will be re-scaled to the
134 * @param screenX The x position on screen of the left-top of the bitmap.
135 * @param screenY The y position on screen of the left-top of the bitmap.
136 * @param textureLeft The left edge of the region inside b to use.
137 * @param textureTop The top edge of the region inside b to use.
138 * @param textureWidth The width of the region inside b to use.
139 * @param textureHeight The height of the region inside b to use.
140 * @param source An object representing where the drag originated
141 * @param dragInfo The data associated with the object that is being dragged
142 * @param dragBehavior The drag action: move or copy
144 private void startDrag(Bitmap b, int screenX, int screenY, int textureLeft, int textureTop, int textureWidth, int textureHeight, DragSource source, Object dragInfo, DragBehavior dragBehavior) {
145 if (mInputMethodManager == null) {
146 mInputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
148 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
149 if (mListener != null) {
150 mListener.onDragStart(source, dragInfo, dragBehavior);
152 final int registrationX = ((int)mMotionDownX) - screenX;
153 final int registrationY = ((int)mMotionDownY) - screenY;
154 mTouchOffsetX = mMotionDownX - screenX;
155 mTouchOffsetY = mMotionDownY - screenY;
157 mDragSource = source;
158 mDragInfo = dragInfo;
159 mDragView = new DragView(mContext, b, registrationX, registrationY, textureLeft, textureTop, textureWidth, textureHeight);
160 mDragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
164 * Draw the view into a bitmap.
166 private Bitmap getViewBitmap(View v) {
170 boolean willNotCache = v.willNotCacheDrawing();
171 v.setWillNotCacheDrawing(false);
173 // Reset the drawing cache background color to fully transparent
174 // for the duration of this operation
175 int color = v.getDrawingCacheBackgroundColor();
176 v.setDrawingCacheBackgroundColor(0);
179 v.destroyDrawingCache();
181 v.buildDrawingCache();
182 Bitmap cacheBitmap = v.getDrawingCache();
183 if (cacheBitmap == null) {
184 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
188 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
191 v.destroyDrawingCache();
192 v.setWillNotCacheDrawing(willNotCache);
193 v.setDrawingCacheBackgroundColor(color);
198 public boolean dispatchKeyEvent(KeyEvent event) {
202 public void cancelDrag() {
206 private void endDrag() {
209 if (mOriginator != null) {
210 mOriginator.setVisibility(View.VISIBLE);
212 if (mListener != null) {
213 mListener.onDragEnd();
215 if (mDragView != null) {
222 public boolean onInterceptTouchEvent(MotionEvent ev) {
223 final int action = ev.getAction();
224 if (action == MotionEvent.ACTION_DOWN) {
227 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
228 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
230 case MotionEvent.ACTION_MOVE:
232 case MotionEvent.ACTION_DOWN:
233 mMotionDownX = screenX;
234 mMotionDownY = screenY;
235 mLastDropTarget = null;
237 case MotionEvent.ACTION_CANCEL:
238 case MotionEvent.ACTION_UP:
240 drop(screenX, screenY);
249 * Sets the view that should handle move events.
251 void setMoveTarget(View view) {
255 public boolean dispatchUnhandledMove(View focused, int direction) {
256 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
259 public boolean onTouchEvent(MotionEvent ev) {
264 final int action = ev.getAction();
265 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
266 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
269 case MotionEvent.ACTION_DOWN:
270 mMotionDownX = screenX;
271 mMotionDownY = screenY;
273 case MotionEvent.ACTION_MOVE:
274 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
275 final int[] coordinates = mCoordinatesTemp;
276 DropTarget dropTarget = findDropTarget(screenX, screenY, coordinates);
277 if (dropTarget != null) {
278 if (mLastDropTarget == dropTarget) {
279 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1], (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
282 if (mLastDropTarget != null) {
283 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
285 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1], (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
289 if (mLastDropTarget != null) {
290 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
293 mLastDropTarget = dropTarget;
295 case MotionEvent.ACTION_UP:
297 drop(screenX, screenY);
301 case MotionEvent.ACTION_CANCEL:
308 private boolean drop(float x, float y) {
309 final int[] coordinates = mCoordinatesTemp;
310 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
311 if (dropTarget != null) {
312 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
313 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
314 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1], (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
315 mDragSource.onDropCompleted((View)dropTarget, true);
318 mDragSource.onDropCompleted((View)dropTarget, false);
324 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
325 final Rect r = mRectTemp;
326 final ArrayList<DropTarget> dropTargets = mDropTargets;
327 final int count = dropTargets.size();
328 for (int i = count - 1; i >= 0; i--) {
329 final DropTarget target = dropTargets.get(i);
330 target.getHitRect(r);
331 target.getLocationOnScreen(dropCoordinates);
332 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
333 if (r.contains(x, y)) {
334 dropCoordinates[0] = x - dropCoordinates[0];
335 dropCoordinates[1] = y - dropCoordinates[1];
342 private void recordScreenSize() {
343 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(mDisplayMetrics);
345 private static int clamp(int val, int min, int max) {
349 else if (val >= max) {
357 public void setDragListener(DragListener listener) {
358 mListener = listener;
360 public void addDropTarget(DropTarget target) {
361 mDropTargets.add(target);
363 public void removeDropTarget(DropTarget target) {
364 mDropTargets.remove(target);