blob: d627a4c2ee0466e45e905cc6aea034dfba73b729 [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
Adam Cohenc6cc61d2012-04-04 12:47:08 -070019import android.content.Context;
Winson Chung043f2af2012-03-01 16:09:54 -080020import android.graphics.PointF;
Jeff Sharkey70864282009-04-07 21:08:40 -070021import android.graphics.Rect;
Adam Cohen570f5142012-04-24 14:36:11 -070022import android.util.Log;
Jeff Sharkey70864282009-04-07 21:08:40 -070023
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024/**
25 * Interface defining an object that can receive a drag.
26 *
27 */
28public interface DropTarget {
Adam Cohencb3382b2011-05-24 14:07:08 -070029
Adam Cohen570f5142012-04-24 14:36:11 -070030 public static final String TAG = "DropTarget";
31
Adam Cohencb3382b2011-05-24 14:07:08 -070032 class DragObject {
33 public int x = -1;
34 public int y = -1;
35
36 /** X offset from the upper-left corner of the cell to where we touched. */
37 public int xOffset = -1;
38
39 /** Y offset from the upper-left corner of the cell to where we touched. */
40 public int yOffset = -1;
41
Adam Cohenbfbfd262011-06-13 16:55:12 -070042 /** This indicates whether a drag is in final stages, either drop or cancel. It
43 * differentiates onDragExit, since this is called when the drag is ending, above
44 * the current drag target, or when the drag moves off the current drag object.
45 */
46 public boolean dragComplete = false;
47
Adam Cohencb3382b2011-05-24 14:07:08 -070048 /** The view that moves around while you drag. */
49 public DragView dragView = null;
50
51 /** The data associated with the object being dragged */
52 public Object dragInfo = null;
53
54 /** Where the drag originated */
55 public DragSource dragSource = null;
56
Winson Chung557d6ed2011-07-08 15:34:52 -070057 /** Post drag animation runnable */
58 public Runnable postAnimationRunnable = null;
59
Adam Cohen36cc09b2011-09-29 17:33:15 -070060 /** Indicates that the drag operation was cancelled */
61 public boolean cancelled = false;
62
Winson Chung7bd1bbb2012-02-13 18:29:29 -080063 /** Defers removing the DragView from the DragLayer until after the drop animation. */
64 public boolean deferDragViewCleanupPostAnimation = true;
65
Adam Cohencb3382b2011-05-24 14:07:08 -070066 public DragObject() {
67 }
68 }
69
Adam Cohenc6cc61d2012-04-04 12:47:08 -070070 public static class DragEnforcer implements DragController.DragListener {
71 int dragParity = 0;
72
73 public DragEnforcer(Context context) {
74 Launcher launcher = (Launcher) context;
75 launcher.getDragController().addDragListener(this);
76 }
77
78 void onDragEnter() {
79 dragParity++;
80 if (dragParity != 1) {
Adam Cohen570f5142012-04-24 14:36:11 -070081 Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity);
Adam Cohenc6cc61d2012-04-04 12:47:08 -070082 }
83 }
84
85 void onDragExit() {
86 dragParity--;
87 if (dragParity != 0) {
Adam Cohen570f5142012-04-24 14:36:11 -070088 Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity);
Adam Cohenc6cc61d2012-04-04 12:47:08 -070089 }
90 }
91
92 @Override
93 public void onDragStart(DragSource source, Object info, int dragAction) {
94 if (dragParity != 0) {
Adam Cohen570f5142012-04-24 14:36:11 -070095 Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity);
Adam Cohenc6cc61d2012-04-04 12:47:08 -070096 }
97 }
98
99 @Override
100 public void onDragEnd() {
101 if (dragParity != 0) {
Adam Cohen570f5142012-04-24 14:36:11 -0700102 Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity);
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700103 }
104 }
105 }
106
Michael Jurka0280c3b2010-09-17 15:00:07 -0700107 /**
108 * Used to temporarily disable certain drop targets
109 *
110 * @return boolean specifying whether this drop target is currently enabled
111 */
112 boolean isDropEnabled();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 /**
115 * Handle an object being dropped on the DropTarget
116 *
117 * @param source DragSource where the drag started
118 * @param x X coordinate of the drop location
119 * @param y Y coordinate of the drop location
Joe Onorato00acb122009-08-04 16:04:30 -0400120 * @param xOffset Horizontal offset with the object being dragged where the original
121 * touch happened
122 * @param yOffset Vertical offset with the object being dragged where the original
123 * touch happened
124 * @param dragView The DragView that's being dragged around on screen.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 * @param dragInfo Data associated with the object being dragged
126 *
127 */
Adam Cohencb3382b2011-05-24 14:07:08 -0700128 void onDrop(DragObject dragObject);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Adam Cohencb3382b2011-05-24 14:07:08 -0700130 void onDragEnter(DragObject dragObject);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Adam Cohencb3382b2011-05-24 14:07:08 -0700132 void onDragOver(DragObject dragObject);
133
134 void onDragExit(DragObject dragObject);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 /**
Winson Chung043f2af2012-03-01 16:09:54 -0800137 * Handle an object being dropped as a result of flinging to delete and will be called in place
138 * of onDrop(). (This is only called on objects that are set as the DragController's
139 * fling-to-delete target.
140 */
141 void onFlingToDelete(DragObject dragObject, int x, int y, PointF vec);
142
143 /**
Patrick Dubroy440c3602010-07-13 17:50:32 -0700144 * Allows a DropTarget to delegate drag and drop events to another object.
145 *
146 * Most subclasses will should just return null from this method.
147 *
148 * @param source DragSource where the drag started
149 * @param x X coordinate of the drop location
150 * @param y Y coordinate of the drop location
151 * @param xOffset Horizontal offset with the object being dragged where the original
152 * touch happened
153 * @param yOffset Vertical offset with the object being dragged where the original
154 * touch happened
155 * @param dragView The DragView that's being dragged around on screen.
156 * @param dragInfo Data associated with the object being dragged
157 *
158 * @return The DropTarget to delegate to, or null to not delegate to another object.
159 */
Adam Cohencb3382b2011-05-24 14:07:08 -0700160 DropTarget getDropTargetDelegate(DragObject dragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700161
162 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700163 * Check if a drop action can occur at, or near, the requested location.
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700164 * This will be called just before onDrop.
Jeff Sharkey70864282009-04-07 21:08:40 -0700165 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 * @param source DragSource where the drag started
167 * @param x X coordinate of the drop location
168 * @param y Y coordinate of the drop location
Jeff Sharkey70864282009-04-07 21:08:40 -0700169 * @param xOffset Horizontal offset with the object being dragged where the
170 * original touch happened
171 * @param yOffset Vertical offset with the object being dragged where the
172 * original touch happened
Joe Onorato00acb122009-08-04 16:04:30 -0400173 * @param dragView The DragView that's being dragged around on screen.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 * @param dragInfo Data associated with the object being dragged
Jeff Sharkey70864282009-04-07 21:08:40 -0700175 * @return True if the drop will be accepted, false otherwise.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 */
Adam Cohencb3382b2011-05-24 14:07:08 -0700177 boolean acceptDrop(DragObject dragObject);
Jeff Sharkey70864282009-04-07 21:08:40 -0700178
Joe Onorato00acb122009-08-04 16:04:30 -0400179 // These methods are implemented in Views
180 void getHitRect(Rect outRect);
Adam Cohen8dfcba42011-07-07 16:38:18 -0700181 void getLocationInDragLayer(int[] loc);
Joe Onorato00acb122009-08-04 16:04:30 -0400182 int getLeft();
183 int getTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184}