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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
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; |
| 22 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Interface defining an object that can receive a drag. |
| 25 | * |
| 26 | */ |
| 27 | public interface DropTarget { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 28 | |
| 29 | class DragObject { |
| 30 | public int x = -1; |
| 31 | public int y = -1; |
| 32 | |
| 33 | /** X offset from the upper-left corner of the cell to where we touched. */ |
| 34 | public int xOffset = -1; |
| 35 | |
| 36 | /** Y offset from the upper-left corner of the cell to where we touched. */ |
| 37 | public int yOffset = -1; |
| 38 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 39 | /** This indicates whether a drag is in final stages, either drop or cancel. It |
| 40 | * differentiates onDragExit, since this is called when the drag is ending, above |
| 41 | * the current drag target, or when the drag moves off the current drag object. |
| 42 | */ |
| 43 | public boolean dragComplete = false; |
| 44 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 45 | /** The view that moves around while you drag. */ |
| 46 | public DragView dragView = null; |
| 47 | |
| 48 | /** The data associated with the object being dragged */ |
| 49 | public Object dragInfo = null; |
| 50 | |
| 51 | /** Where the drag originated */ |
| 52 | public DragSource dragSource = null; |
| 53 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 54 | /** Post drag animation runnable */ |
| 55 | public Runnable postAnimationRunnable = null; |
| 56 | |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 57 | /** Indicates that the drag operation was cancelled */ |
| 58 | public boolean cancelled = false; |
| 59 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 60 | /** Defers removing the DragView from the DragLayer until after the drop animation. */ |
| 61 | public boolean deferDragViewCleanupPostAnimation = true; |
| 62 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 63 | public DragObject() { |
| 64 | } |
| 65 | } |
| 66 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame^] | 67 | public static class DragEnforcer implements DragController.DragListener { |
| 68 | int dragParity = 0; |
| 69 | |
| 70 | public DragEnforcer(Context context) { |
| 71 | Launcher launcher = (Launcher) context; |
| 72 | launcher.getDragController().addDragListener(this); |
| 73 | } |
| 74 | |
| 75 | void onDragEnter() { |
| 76 | dragParity++; |
| 77 | if (dragParity != 1) { |
| 78 | throw new RuntimeException("onDragEnter: Drag contract violated: " + dragParity); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void onDragExit() { |
| 83 | dragParity--; |
| 84 | if (dragParity != 0) { |
| 85 | throw new RuntimeException("onDragExit: Drag contract violated: " + dragParity); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onDragStart(DragSource source, Object info, int dragAction) { |
| 91 | if (dragParity != 0) { |
| 92 | throw new RuntimeException("onDragEnter: Drag contract violated: " + dragParity); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onDragEnd() { |
| 98 | if (dragParity != 0) { |
| 99 | throw new RuntimeException("onDragExit: Drag contract violated: " + dragParity); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Used to temporarily disable certain drop targets |
| 106 | * |
| 107 | * @return boolean specifying whether this drop target is currently enabled |
| 108 | */ |
| 109 | boolean isDropEnabled(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Handle an object being dropped on the DropTarget |
| 113 | * |
| 114 | * @param source DragSource where the drag started |
| 115 | * @param x X coordinate of the drop location |
| 116 | * @param y Y coordinate of the drop location |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 117 | * @param xOffset Horizontal offset with the object being dragged where the original |
| 118 | * touch happened |
| 119 | * @param yOffset Vertical offset with the object being dragged where the original |
| 120 | * touch happened |
| 121 | * @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] | 122 | * @param dragInfo Data associated with the object being dragged |
| 123 | * |
| 124 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 125 | void onDrop(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 126 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 127 | void onDragEnter(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 128 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 129 | void onDragOver(DragObject dragObject); |
| 130 | |
| 131 | void onDragExit(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 132 | |
| 133 | /** |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 134 | * Handle an object being dropped as a result of flinging to delete and will be called in place |
| 135 | * of onDrop(). (This is only called on objects that are set as the DragController's |
| 136 | * fling-to-delete target. |
| 137 | */ |
| 138 | void onFlingToDelete(DragObject dragObject, int x, int y, PointF vec); |
| 139 | |
| 140 | /** |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 141 | * Allows a DropTarget to delegate drag and drop events to another object. |
| 142 | * |
| 143 | * Most subclasses will should just return null from this method. |
| 144 | * |
| 145 | * @param source DragSource where the drag started |
| 146 | * @param x X coordinate of the drop location |
| 147 | * @param y Y coordinate of the drop location |
| 148 | * @param xOffset Horizontal offset with the object being dragged where the original |
| 149 | * touch happened |
| 150 | * @param yOffset Vertical offset with the object being dragged where the original |
| 151 | * touch happened |
| 152 | * @param dragView The DragView that's being dragged around on screen. |
| 153 | * @param dragInfo Data associated with the object being dragged |
| 154 | * |
| 155 | * @return The DropTarget to delegate to, or null to not delegate to another object. |
| 156 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 157 | DropTarget getDropTargetDelegate(DragObject dragObject); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 160 | * 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] | 161 | * This will be called just before onDrop. |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 162 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 163 | * @param source DragSource where the drag started |
| 164 | * @param x X coordinate of the drop location |
| 165 | * @param y Y coordinate of the drop location |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 166 | * @param xOffset Horizontal offset with the object being dragged where the |
| 167 | * original touch happened |
| 168 | * @param yOffset Vertical offset with the object being dragged where the |
| 169 | * original touch happened |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 170 | * @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] | 171 | * @param dragInfo Data associated with the object being dragged |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 172 | * @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] | 173 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 174 | boolean acceptDrop(DragObject dragObject); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 175 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 176 | // These methods are implemented in Views |
| 177 | void getHitRect(Rect outRect); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 178 | void getLocationInDragLayer(int[] loc); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 179 | int getLeft(); |
| 180 | int getTop(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 181 | } |