The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 20 | import android.graphics.PointF; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 21 | import android.graphics.Rect; |
Adam Cohen | 570f514 | 2012-04-24 14:36:11 -0700 | [diff] [blame] | 22 | import android.util.Log; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 23 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Interface defining an object that can receive a drag. |
| 26 | * |
| 27 | */ |
| 28 | public interface DropTarget { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 29 | |
Adam Cohen | 570f514 | 2012-04-24 14:36:11 -0700 | [diff] [blame] | 30 | public static final String TAG = "DropTarget"; |
| 31 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 32 | 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 Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 42 | /** 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 Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 48 | /** 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 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 57 | /** The object is part of an accessible drag operation */ |
| 58 | public boolean accessibleDrag; |
| 59 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 60 | /** Post drag animation runnable */ |
| 61 | public Runnable postAnimationRunnable = null; |
| 62 | |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 63 | /** Indicates that the drag operation was cancelled */ |
| 64 | public boolean cancelled = false; |
| 65 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 66 | /** Defers removing the DragView from the DragLayer until after the drop animation. */ |
| 67 | public boolean deferDragViewCleanupPostAnimation = true; |
| 68 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 69 | public DragObject() { |
| 70 | } |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * This is used to compute the visual center of the dragView. This point is then |
| 74 | * used to visualize drop locations and determine where to drop an item. The idea is that |
| 75 | * the visual center represents the user's interpretation of where the item is, and hence |
| 76 | * is the appropriate point to use when determining drop location. |
| 77 | */ |
| 78 | public final float[] getVisualCenter(float[] recycle) { |
| 79 | final float res[] = (recycle == null) ? new float[2] : recycle; |
| 80 | |
| 81 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 82 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 83 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 84 | // R.dimen.dragViewOffsetY. |
| 85 | int left = x - xOffset; |
| 86 | int top = y - yOffset; |
| 87 | |
| 88 | // In order to find the visual center, we shift by half the dragRect |
| 89 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 90 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 91 | |
| 92 | return res; |
| 93 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 96 | public static class DragEnforcer implements DragController.DragListener { |
| 97 | int dragParity = 0; |
| 98 | |
| 99 | public DragEnforcer(Context context) { |
| 100 | Launcher launcher = (Launcher) context; |
| 101 | launcher.getDragController().addDragListener(this); |
| 102 | } |
| 103 | |
| 104 | void onDragEnter() { |
| 105 | dragParity++; |
| 106 | if (dragParity != 1) { |
Adam Cohen | 570f514 | 2012-04-24 14:36:11 -0700 | [diff] [blame] | 107 | Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | void onDragExit() { |
| 112 | dragParity--; |
| 113 | if (dragParity != 0) { |
Adam Cohen | 570f514 | 2012-04-24 14:36:11 -0700 | [diff] [blame] | 114 | Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void onDragStart(DragSource source, Object info, int dragAction) { |
| 120 | if (dragParity != 0) { |
Adam Cohen | 570f514 | 2012-04-24 14:36:11 -0700 | [diff] [blame] | 121 | Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onDragEnd() { |
| 127 | if (dragParity != 0) { |
Adam Cohen | 570f514 | 2012-04-24 14:36:11 -0700 | [diff] [blame] | 128 | Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Used to temporarily disable certain drop targets |
| 135 | * |
| 136 | * @return boolean specifying whether this drop target is currently enabled |
| 137 | */ |
| 138 | boolean isDropEnabled(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Handle an object being dropped on the DropTarget |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 142 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 143 | * @param source DragSource where the drag started |
| 144 | * @param x X coordinate of the drop location |
| 145 | * @param y Y coordinate of the drop location |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 146 | * @param xOffset Horizontal offset with the object being dragged where the original |
| 147 | * touch happened |
| 148 | * @param yOffset Vertical offset with the object being dragged where the original |
| 149 | * touch happened |
| 150 | * @param dragView The DragView that's being dragged around on screen. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 151 | * @param dragInfo Data associated with the object being dragged |
Mindy DelliCarpini | 53b8d07 | 2013-07-03 08:23:13 -0700 | [diff] [blame] | 152 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 153 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 154 | void onDrop(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 155 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 156 | void onDragEnter(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 157 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 158 | void onDragOver(DragObject dragObject); |
| 159 | |
| 160 | void onDragExit(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 161 | |
| 162 | /** |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 163 | * Handle an object being dropped as a result of flinging to delete and will be called in place |
| 164 | * of onDrop(). (This is only called on objects that are set as the DragController's |
| 165 | * fling-to-delete target. |
| 166 | */ |
| 167 | void onFlingToDelete(DragObject dragObject, int x, int y, PointF vec); |
| 168 | |
| 169 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 170 | * Check if a drop action can occur at, or near, the requested location. |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 171 | * This will be called just before onDrop. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 172 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 173 | * @param source DragSource where the drag started |
| 174 | * @param x X coordinate of the drop location |
| 175 | * @param y Y coordinate of the drop location |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 176 | * @param xOffset Horizontal offset with the object being dragged where the |
| 177 | * original touch happened |
| 178 | * @param yOffset Vertical offset with the object being dragged where the |
| 179 | * original touch happened |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 180 | * @param dragView The DragView that's being dragged around on screen. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 181 | * @param dragInfo Data associated with the object being dragged |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 182 | * @return True if the drop will be accepted, false otherwise. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 183 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 184 | boolean acceptDrop(DragObject dragObject); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 185 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame^] | 186 | void prepareAccessibilityDrop(); |
| 187 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 188 | // These methods are implemented in Views |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 189 | void getHitRectRelativeToDragLayer(Rect outRect); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 190 | void getLocationInDragLayer(int[] loc); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 191 | int getLeft(); |
| 192 | int getTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 193 | } |