blob: 7ede427519b9ebca36e58cc7f57aa2c15addf7ee [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
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 }
Sunny Goyal1587d532015-01-29 09:57:16 -080068
69 /**
70 * This is used to compute the visual center of the dragView. This point is then
71 * used to visualize drop locations and determine where to drop an item. The idea is that
72 * the visual center represents the user's interpretation of where the item is, and hence
73 * is the appropriate point to use when determining drop location.
74 */
75 public final float[] getVisualCenter(float[] recycle) {
76 final float res[] = (recycle == null) ? new float[2] : recycle;
77
78 // These represent the visual top and left of drag view if a dragRect was provided.
79 // If a dragRect was not provided, then they correspond to the actual view left and
80 // top, as the dragRect is in that case taken to be the entire dragView.
81 // R.dimen.dragViewOffsetY.
82 int left = x - xOffset;
83 int top = y - yOffset;
84
85 // In order to find the visual center, we shift by half the dragRect
86 res[0] = left + dragView.getDragRegion().width() / 2;
87 res[1] = top + dragView.getDragRegion().height() / 2;
88
89 return res;
90 }
Adam Cohencb3382b2011-05-24 14:07:08 -070091 }
92
Adam Cohenc6cc61d2012-04-04 12:47:08 -070093 public static class DragEnforcer implements DragController.DragListener {
94 int dragParity = 0;
95
96 public DragEnforcer(Context context) {
97 Launcher launcher = (Launcher) context;
98 launcher.getDragController().addDragListener(this);
99 }
100
101 void onDragEnter() {
102 dragParity++;
103 if (dragParity != 1) {
Adam Cohen570f5142012-04-24 14:36:11 -0700104 Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity);
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700105 }
106 }
107
108 void onDragExit() {
109 dragParity--;
110 if (dragParity != 0) {
Adam Cohen570f5142012-04-24 14:36:11 -0700111 Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity);
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700112 }
113 }
114
115 @Override
116 public void onDragStart(DragSource source, Object info, int dragAction) {
117 if (dragParity != 0) {
Adam Cohen570f5142012-04-24 14:36:11 -0700118 Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity);
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700119 }
120 }
121
122 @Override
123 public void onDragEnd() {
124 if (dragParity != 0) {
Adam Cohen570f5142012-04-24 14:36:11 -0700125 Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity);
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700126 }
127 }
128 }
129
Michael Jurka0280c3b2010-09-17 15:00:07 -0700130 /**
131 * Used to temporarily disable certain drop targets
132 *
133 * @return boolean specifying whether this drop target is currently enabled
134 */
135 boolean isDropEnabled();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 /**
138 * Handle an object being dropped on the DropTarget
139 *
140 * @param source DragSource where the drag started
141 * @param x X coordinate of the drop location
142 * @param y Y coordinate of the drop location
Joe Onorato00acb122009-08-04 16:04:30 -0400143 * @param xOffset Horizontal offset with the object being dragged where the original
144 * touch happened
145 * @param yOffset Vertical offset with the object being dragged where the original
146 * touch happened
147 * @param dragView The DragView that's being dragged around on screen.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 * @param dragInfo Data associated with the object being dragged
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700149 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 */
Adam Cohencb3382b2011-05-24 14:07:08 -0700151 void onDrop(DragObject dragObject);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Adam Cohencb3382b2011-05-24 14:07:08 -0700153 void onDragEnter(DragObject dragObject);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Adam Cohencb3382b2011-05-24 14:07:08 -0700155 void onDragOver(DragObject dragObject);
156
157 void onDragExit(DragObject dragObject);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
159 /**
Winson Chung043f2af2012-03-01 16:09:54 -0800160 * Handle an object being dropped as a result of flinging to delete and will be called in place
161 * of onDrop(). (This is only called on objects that are set as the DragController's
162 * fling-to-delete target.
163 */
164 void onFlingToDelete(DragObject dragObject, int x, int y, PointF vec);
165
166 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700167 * Check if a drop action can occur at, or near, the requested location.
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700168 * This will be called just before onDrop.
Jeff Sharkey70864282009-04-07 21:08:40 -0700169 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 * @param source DragSource where the drag started
171 * @param x X coordinate of the drop location
172 * @param y Y coordinate of the drop location
Jeff Sharkey70864282009-04-07 21:08:40 -0700173 * @param xOffset Horizontal offset with the object being dragged where the
174 * original touch happened
175 * @param yOffset Vertical offset with the object being dragged where the
176 * original touch happened
Joe Onorato00acb122009-08-04 16:04:30 -0400177 * @param dragView The DragView that's being dragged around on screen.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 * @param dragInfo Data associated with the object being dragged
Jeff Sharkey70864282009-04-07 21:08:40 -0700179 * @return True if the drop will be accepted, false otherwise.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 */
Adam Cohencb3382b2011-05-24 14:07:08 -0700181 boolean acceptDrop(DragObject dragObject);
Jeff Sharkey70864282009-04-07 21:08:40 -0700182
Joe Onorato00acb122009-08-04 16:04:30 -0400183 // These methods are implemented in Views
Adam Cohen7d30a372013-07-01 17:03:59 -0700184 void getHitRectRelativeToDragLayer(Rect outRect);
Adam Cohen8dfcba42011-07-07 16:38:18 -0700185 void getLocationInDragLayer(int[] loc);
Joe Onorato00acb122009-08-04 16:04:30 -0400186 int getLeft();
187 int getTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188}