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 | |
Hyunyoung Song | 1ae4242 | 2020-01-26 23:25:14 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 20 | import android.graphics.Rect; |
| 21 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 22 | import com.android.launcher3.accessibility.DragViewStateAnnouncer; |
Hyunyoung Song | 1ae4242 | 2020-01-26 23:25:14 -0800 | [diff] [blame] | 23 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 24 | import com.android.launcher3.dragndrop.DragOptions; |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 25 | import com.android.launcher3.dragndrop.DragView; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 26 | import com.android.launcher3.dragndrop.DraggableView; |
Hyunyoung Song | 1ae4242 | 2020-01-26 23:25:14 -0800 | [diff] [blame] | 27 | import com.android.launcher3.folder.FolderNameProvider; |
thiruram | 59c5f39 | 2020-05-04 12:22:22 -0700 | [diff] [blame] | 28 | import com.android.launcher3.logging.InstanceId; |
| 29 | import com.android.launcher3.logging.InstanceIdSequence; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 30 | import com.android.launcher3.model.data.ItemInfo; |
Hyunyoung Song | d98d5eb | 2020-06-18 22:57:50 -0700 | [diff] [blame^] | 31 | import com.android.launcher3.util.Executors; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 32 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Interface defining an object that can receive a drag. |
| 35 | * |
| 36 | */ |
| 37 | public interface DropTarget { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 38 | |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 39 | class DragObject { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 40 | public int x = -1; |
| 41 | public int y = -1; |
| 42 | |
| 43 | /** X offset from the upper-left corner of the cell to where we touched. */ |
| 44 | public int xOffset = -1; |
| 45 | |
| 46 | /** Y offset from the upper-left corner of the cell to where we touched. */ |
| 47 | public int yOffset = -1; |
| 48 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 49 | /** This indicates whether a drag is in final stages, either drop or cancel. It |
| 50 | * differentiates onDragExit, since this is called when the drag is ending, above |
| 51 | * the current drag target, or when the drag moves off the current drag object. |
| 52 | */ |
| 53 | public boolean dragComplete = false; |
| 54 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 55 | /** The view that moves around while you drag. */ |
| 56 | public DragView dragView = null; |
| 57 | |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 58 | /** The data associated with the object, after item is dropped. */ |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 59 | public ItemInfo dragInfo = null; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 60 | |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 61 | /** The data associated with the object being dragged */ |
| 62 | public ItemInfo originalDragInfo = null; |
| 63 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 64 | /** Where the drag originated */ |
| 65 | public DragSource dragSource = null; |
| 66 | |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 67 | /** Indicates that the drag operation was cancelled */ |
| 68 | public boolean cancelled = false; |
| 69 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 70 | /** Defers removing the DragView from the DragLayer until after the drop animation. */ |
| 71 | public boolean deferDragViewCleanupPostAnimation = true; |
| 72 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 73 | public DragViewStateAnnouncer stateAnnouncer; |
| 74 | |
Hyunyoung Song | 1ae4242 | 2020-01-26 23:25:14 -0800 | [diff] [blame] | 75 | public FolderNameProvider folderNameProvider; |
| 76 | |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 77 | /** The source view (ie. icon, widget etc.) that is being dragged and which the |
| 78 | * DragView represents. May be an actual View class or a virtual stand-in */ |
| 79 | public DraggableView originalView = null; |
| 80 | |
thiruram | 59c5f39 | 2020-05-04 12:22:22 -0700 | [diff] [blame] | 81 | /** Used for matching DROP event with its corresponding DRAG event on the server side. */ |
thiruram | 5a01f0e | 2020-05-04 17:49:37 -0700 | [diff] [blame] | 82 | public final InstanceId logInstanceId = |
thiruram | 59c5f39 | 2020-05-04 12:22:22 -0700 | [diff] [blame] | 83 | new InstanceIdSequence(1 << 20 /*InstanceId.INSTANCE_ID_MAX*/) |
| 84 | .newInstanceId(); |
| 85 | |
Hyunyoung Song | 1ae4242 | 2020-01-26 23:25:14 -0800 | [diff] [blame] | 86 | public DragObject(Context context) { |
| 87 | if (FeatureFlags.FOLDER_NAME_SUGGEST.get()) { |
Hyunyoung Song | d98d5eb | 2020-06-18 22:57:50 -0700 | [diff] [blame^] | 88 | Executors.MODEL_EXECUTOR.post(() -> { |
| 89 | folderNameProvider = FolderNameProvider.newInstance(context); |
| 90 | }); |
Hyunyoung Song | 1ae4242 | 2020-01-26 23:25:14 -0800 | [diff] [blame] | 91 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 92 | } |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * This is used to compute the visual center of the dragView. This point is then |
| 96 | * used to visualize drop locations and determine where to drop an item. The idea is that |
| 97 | * the visual center represents the user's interpretation of where the item is, and hence |
| 98 | * is the appropriate point to use when determining drop location. |
| 99 | */ |
| 100 | public final float[] getVisualCenter(float[] recycle) { |
| 101 | final float res[] = (recycle == null) ? new float[2] : recycle; |
Jon Miranda | 23c2a02 | 2019-01-04 14:30:26 -0800 | [diff] [blame] | 102 | Rect dragRegion = dragView.getDragRegion(); |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 103 | |
| 104 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 105 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 106 | // top, as the dragRect is in that case taken to be the entire dragView. |
Jon Miranda | 23c2a02 | 2019-01-04 14:30:26 -0800 | [diff] [blame] | 107 | int left = x - xOffset - dragRegion.left; |
| 108 | int top = y - yOffset - dragRegion.top; |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 109 | |
| 110 | // In order to find the visual center, we shift by half the dragRect |
Jon Miranda | 23c2a02 | 2019-01-04 14:30:26 -0800 | [diff] [blame] | 111 | res[0] = left + dragRegion.width() / 2; |
| 112 | res[1] = top + dragRegion.height() / 2; |
Sunny Goyal | 1587d53 | 2015-01-29 09:57:16 -0800 | [diff] [blame] | 113 | |
| 114 | return res; |
| 115 | } |
Samuel Fufa | d12d6ab | 2020-05-11 19:48:59 -0700 | [diff] [blame] | 116 | |
| 117 | |
| 118 | /** |
| 119 | * This is used to determine if an object is dropped at a different location than it was |
| 120 | * dragged from |
| 121 | */ |
| 122 | public boolean isMoved() { |
| 123 | return dragInfo.cellX != originalDragInfo.cellX |
| 124 | || dragInfo.cellY != originalDragInfo.cellY |
| 125 | || dragInfo.screenId != originalDragInfo.screenId |
| 126 | || dragInfo.container != originalDragInfo.container; |
| 127 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 130 | /** |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 136 | |
| 137 | /** |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 138 | * Handle an object being dropped on the DropTarget. |
| 139 | * |
| 140 | * This will be called only if this target previously returned true for {@link #acceptDrop}. It |
| 141 | * is the responsibility of this target to exit out of the spring loaded mode (either |
| 142 | * immediately or after any pending animations). |
| 143 | * |
| 144 | * If the drop was cancelled for some reason, onDrop will never get called, the UI will |
| 145 | * automatically exit out of this mode. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 146 | */ |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 147 | void onDrop(DragObject dragObject, DragOptions options); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 148 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 149 | void onDragEnter(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 150 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 151 | void onDragOver(DragObject dragObject); |
| 152 | |
| 153 | void onDragExit(DragObject dragObject); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 154 | |
| 155 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 156 | * 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] | 157 | * This will be called just before onDrop. |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 158 | * @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] | 159 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 160 | boolean acceptDrop(DragObject dragObject); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 161 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 162 | void prepareAccessibilityDrop(); |
| 163 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 164 | // These methods are implemented in Views |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 165 | void getHitRectRelativeToDragLayer(Rect outRect); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 166 | } |