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 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 19 | import android.graphics.PointF; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 20 | import android.graphics.Rect; |
| 21 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | /** |
| 23 | * Interface defining an object that can receive a drag. |
| 24 | * |
| 25 | */ |
| 26 | public interface DropTarget { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 27 | |
Adam Cohen | 570f514 | 2012-04-24 14:36:11 -0700 | [diff] [blame] | 28 | public static final String TAG = "DropTarget"; |
| 29 | |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 30 | public static class DragObject { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 31 | public int x = -1; |
| 32 | public int y = -1; |
| 33 | |
| 34 | /** X offset from the upper-left corner of the cell to where we touched. */ |
| 35 | public int xOffset = -1; |
| 36 | |
| 37 | /** Y offset from the upper-left corner of the cell to where we touched. */ |
| 38 | public int yOffset = -1; |
| 39 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 40 | /** This indicates whether a drag is in final stages, either drop or cancel. It |
| 41 | * differentiates onDragExit, since this is called when the drag is ending, above |
| 42 | * the current drag target, or when the drag moves off the current drag object. |
| 43 | */ |
| 44 | public boolean dragComplete = false; |
| 45 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 46 | /** The view that moves around while you drag. */ |
| 47 | public DragView dragView = null; |
| 48 | |
| 49 | /** The data associated with the object being dragged */ |
| 50 | public Object dragInfo = null; |
| 51 | |
| 52 | /** Where the drag originated */ |
| 53 | public DragSource dragSource = null; |
| 54 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 55 | /** The object is part of an accessible drag operation */ |
| 56 | public boolean accessibleDrag; |
| 57 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 58 | /** Post drag animation runnable */ |
| 59 | public Runnable postAnimationRunnable = null; |
| 60 | |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 61 | /** Indicates that the drag operation was cancelled */ |
| 62 | public boolean cancelled = false; |
| 63 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 64 | /** Defers removing the DragView from the DragLayer until after the drop animation. */ |
| 65 | public boolean deferDragViewCleanupPostAnimation = true; |
| 66 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 67 | public DragObject() { |
| 68 | } |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * This is used to compute the visual center of the dragView. This point is then |
| 72 | * used to visualize drop locations and determine where to drop an item. The idea is that |
| 73 | * the visual center represents the user's interpretation of where the item is, and hence |
| 74 | * is the appropriate point to use when determining drop location. |
| 75 | */ |
| 76 | public final float[] getVisualCenter(float[] recycle) { |
| 77 | final float res[] = (recycle == null) ? new float[2] : recycle; |
| 78 | |
| 79 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 80 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 81 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 82 | // R.dimen.dragViewOffsetY. |
| 83 | int left = x - xOffset; |
| 84 | int top = y - yOffset; |
| 85 | |
| 86 | // In order to find the visual center, we shift by half the dragRect |
| 87 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 88 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 89 | |
| 90 | return res; |
| 91 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Used to temporarily disable certain drop targets |
| 96 | * |
| 97 | * @return boolean specifying whether this drop target is currently enabled |
| 98 | */ |
| 99 | boolean isDropEnabled(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Handle an object being dropped on the DropTarget |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 103 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | * @param source DragSource where the drag started |
| 105 | * @param x X coordinate of the drop location |
| 106 | * @param y Y coordinate of the drop location |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 107 | * @param xOffset Horizontal offset with the object being dragged where the original |
| 108 | * touch happened |
| 109 | * @param yOffset Vertical offset with the object being dragged where the original |
| 110 | * touch happened |
| 111 | * @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] | 112 | * @param dragInfo Data associated with the object being dragged |
Mindy DelliCarpini | 53b8d07 | 2013-07-03 08:23:13 -0700 | [diff] [blame] | 113 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 114 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 115 | void onDrop(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 117 | void onDragEnter(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 118 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 119 | void onDragOver(DragObject dragObject); |
| 120 | |
| 121 | void onDragExit(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | |
| 123 | /** |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 124 | * Handle an object being dropped as a result of flinging to delete and will be called in place |
| 125 | * of onDrop(). (This is only called on objects that are set as the DragController's |
| 126 | * fling-to-delete target. |
| 127 | */ |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 128 | void onFlingToDelete(DragObject dragObject, PointF vec); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 129 | |
| 130 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 131 | * 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] | 132 | * This will be called just before onDrop. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 133 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 134 | * @param source DragSource where the drag started |
| 135 | * @param x X coordinate of the drop location |
| 136 | * @param y Y coordinate of the drop location |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 137 | * @param xOffset Horizontal offset with the object being dragged where the |
| 138 | * original touch happened |
| 139 | * @param yOffset Vertical offset with the object being dragged where the |
| 140 | * original touch happened |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 141 | * @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] | 142 | * @param dragInfo Data associated with the object being dragged |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 143 | * @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] | 144 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 145 | boolean acceptDrop(DragObject dragObject); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 146 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 147 | void prepareAccessibilityDrop(); |
| 148 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 149 | // These methods are implemented in Views |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 150 | void getHitRectRelativeToDragLayer(Rect outRect); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 151 | void getLocationInDragLayer(int[] loc); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 152 | int getLeft(); |
| 153 | int getTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 154 | } |