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 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 19 | import android.graphics.Rect; |
| 20 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 21 | import com.android.launcher3.accessibility.DragViewStateAnnouncer; |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 22 | import com.android.launcher3.dragndrop.DragView; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -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 | |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 30 | 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 | |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 49 | /** The data associated with the object, after item is dropped. */ |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 50 | public ItemInfo dragInfo = null; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 51 | |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 52 | /** The data associated with the object being dragged */ |
| 53 | public ItemInfo originalDragInfo = null; |
| 54 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 55 | /** Where the drag originated */ |
| 56 | public DragSource dragSource = null; |
| 57 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 58 | /** The object is part of an accessible drag operation */ |
| 59 | public boolean accessibleDrag; |
| 60 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 61 | /** Post drag animation runnable */ |
| 62 | public Runnable postAnimationRunnable = null; |
| 63 | |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 64 | /** Indicates that the drag operation was cancelled */ |
| 65 | public boolean cancelled = false; |
| 66 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 67 | /** Defers removing the DragView from the DragLayer until after the drop animation. */ |
| 68 | public boolean deferDragViewCleanupPostAnimation = true; |
| 69 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 70 | public DragViewStateAnnouncer stateAnnouncer; |
| 71 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 72 | public DragObject() { |
| 73 | } |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * This is used to compute the visual center of the dragView. This point is then |
| 77 | * used to visualize drop locations and determine where to drop an item. The idea is that |
| 78 | * the visual center represents the user's interpretation of where the item is, and hence |
| 79 | * is the appropriate point to use when determining drop location. |
| 80 | */ |
| 81 | public final float[] getVisualCenter(float[] recycle) { |
| 82 | final float res[] = (recycle == null) ? new float[2] : recycle; |
| 83 | |
| 84 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 85 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 86 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 87 | // R.dimen.dragViewOffsetY. |
| 88 | int left = x - xOffset; |
| 89 | int top = y - yOffset; |
| 90 | |
| 91 | // In order to find the visual center, we shift by half the dragRect |
| 92 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 93 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 94 | |
| 95 | return res; |
| 96 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Used to temporarily disable certain drop targets |
| 101 | * |
| 102 | * @return boolean specifying whether this drop target is currently enabled |
| 103 | */ |
| 104 | boolean isDropEnabled(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Handle an object being dropped on the DropTarget |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 109 | void onDrop(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 110 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 111 | void onDragEnter(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 112 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 113 | void onDragOver(DragObject dragObject); |
| 114 | |
| 115 | void onDragExit(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | |
| 117 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 118 | * 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] | 119 | * This will be called just before onDrop. |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 120 | * @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] | 121 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 122 | boolean acceptDrop(DragObject dragObject); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 123 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 124 | void prepareAccessibilityDrop(); |
| 125 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 126 | // These methods are implemented in Views |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 127 | void getHitRectRelativeToDragLayer(Rect outRect); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 128 | } |