blob: 0b0983cbee20ec11e08ec5392202404aab96f619 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Hyunyoung Song1ae42422020-01-26 23:25:14 -080019import android.content.Context;
Jeff Sharkey70864282009-04-07 21:08:40 -070020import android.graphics.Rect;
21
Sunny Goyale78e3d72015-09-24 11:23:31 -070022import com.android.launcher3.accessibility.DragViewStateAnnouncer;
Hyunyoung Song1ae42422020-01-26 23:25:14 -080023import com.android.launcher3.config.FeatureFlags;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070024import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyal0f76b562016-12-13 19:37:10 -080025import com.android.launcher3.dragndrop.DragView;
Adam Cohen65086992020-02-19 08:40:49 -080026import com.android.launcher3.dragndrop.DraggableView;
Hyunyoung Song1ae42422020-01-26 23:25:14 -080027import com.android.launcher3.folder.FolderNameProvider;
thiruram59c5f392020-05-04 12:22:22 -070028import com.android.launcher3.logging.InstanceId;
29import com.android.launcher3.logging.InstanceIdSequence;
Sunny Goyale396abf2020-04-06 15:11:17 -070030import com.android.launcher3.model.data.ItemInfo;
Sunny Goyale78e3d72015-09-24 11:23:31 -070031
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032/**
33 * Interface defining an object that can receive a drag.
34 *
35 */
36public interface DropTarget {
Adam Cohencb3382b2011-05-24 14:07:08 -070037
Sunny Goyal9eba1fd2015-10-16 08:58:57 -070038 class DragObject {
Adam Cohencb3382b2011-05-24 14:07:08 -070039 public int x = -1;
40 public int y = -1;
41
42 /** X offset from the upper-left corner of the cell to where we touched. */
43 public int xOffset = -1;
44
45 /** Y offset from the upper-left corner of the cell to where we touched. */
46 public int yOffset = -1;
47
Adam Cohenbfbfd262011-06-13 16:55:12 -070048 /** This indicates whether a drag is in final stages, either drop or cancel. It
49 * differentiates onDragExit, since this is called when the drag is ending, above
50 * the current drag target, or when the drag moves off the current drag object.
51 */
52 public boolean dragComplete = false;
53
Adam Cohencb3382b2011-05-24 14:07:08 -070054 /** The view that moves around while you drag. */
55 public DragView dragView = null;
56
Hyunyoung Song59a23802016-09-01 12:47:12 -070057 /** The data associated with the object, after item is dropped. */
Sunny Goyalaa8ef112015-06-12 20:04:41 -070058 public ItemInfo dragInfo = null;
Adam Cohencb3382b2011-05-24 14:07:08 -070059
Hyunyoung Song59a23802016-09-01 12:47:12 -070060 /** The data associated with the object being dragged */
61 public ItemInfo originalDragInfo = null;
62
Adam Cohencb3382b2011-05-24 14:07:08 -070063 /** Where the drag originated */
64 public DragSource dragSource = null;
65
Adam Cohen36cc09b2011-09-29 17:33:15 -070066 /** Indicates that the drag operation was cancelled */
67 public boolean cancelled = false;
68
Winson Chung7bd1bbb2012-02-13 18:29:29 -080069 /** Defers removing the DragView from the DragLayer until after the drop animation. */
70 public boolean deferDragViewCleanupPostAnimation = true;
71
Sunny Goyale78e3d72015-09-24 11:23:31 -070072 public DragViewStateAnnouncer stateAnnouncer;
73
Hyunyoung Song1ae42422020-01-26 23:25:14 -080074 public FolderNameProvider folderNameProvider;
75
Adam Cohen65086992020-02-19 08:40:49 -080076 /** The source view (ie. icon, widget etc.) that is being dragged and which the
77 * DragView represents. May be an actual View class or a virtual stand-in */
78 public DraggableView originalView = null;
79
thiruram59c5f392020-05-04 12:22:22 -070080 /** Used for matching DROP event with its corresponding DRAG event on the server side. */
thiruram5a01f0e2020-05-04 17:49:37 -070081 public final InstanceId logInstanceId =
thiruram59c5f392020-05-04 12:22:22 -070082 new InstanceIdSequence(1 << 20 /*InstanceId.INSTANCE_ID_MAX*/)
83 .newInstanceId();
84
Hyunyoung Song1ae42422020-01-26 23:25:14 -080085 public DragObject(Context context) {
86 if (FeatureFlags.FOLDER_NAME_SUGGEST.get()) {
87 folderNameProvider = FolderNameProvider.newInstance(context);
88 }
Adam Cohencb3382b2011-05-24 14:07:08 -070089 }
Sunny Goyal1587d532015-01-29 09:57:16 -080090
91 /**
92 * This is used to compute the visual center of the dragView. This point is then
93 * used to visualize drop locations and determine where to drop an item. The idea is that
94 * the visual center represents the user's interpretation of where the item is, and hence
95 * is the appropriate point to use when determining drop location.
96 */
97 public final float[] getVisualCenter(float[] recycle) {
98 final float res[] = (recycle == null) ? new float[2] : recycle;
Jon Miranda23c2a022019-01-04 14:30:26 -080099 Rect dragRegion = dragView.getDragRegion();
Sunny Goyal1587d532015-01-29 09:57:16 -0800100
101 // These represent the visual top and left of drag view if a dragRect was provided.
102 // If a dragRect was not provided, then they correspond to the actual view left and
103 // top, as the dragRect is in that case taken to be the entire dragView.
Jon Miranda23c2a022019-01-04 14:30:26 -0800104 int left = x - xOffset - dragRegion.left;
105 int top = y - yOffset - dragRegion.top;
Sunny Goyal1587d532015-01-29 09:57:16 -0800106
107 // In order to find the visual center, we shift by half the dragRect
Jon Miranda23c2a022019-01-04 14:30:26 -0800108 res[0] = left + dragRegion.width() / 2;
109 res[1] = top + dragRegion.height() / 2;
Sunny Goyal1587d532015-01-29 09:57:16 -0800110
111 return res;
112 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700113 }
114
Michael Jurka0280c3b2010-09-17 15:00:07 -0700115 /**
116 * Used to temporarily disable certain drop targets
117 *
118 * @return boolean specifying whether this drop target is currently enabled
119 */
120 boolean isDropEnabled();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 /**
Sunny Goyal1797af42017-10-06 13:29:57 -0700123 * Handle an object being dropped on the DropTarget.
124 *
125 * This will be called only if this target previously returned true for {@link #acceptDrop}. It
126 * is the responsibility of this target to exit out of the spring loaded mode (either
127 * immediately or after any pending animations).
128 *
129 * If the drop was cancelled for some reason, onDrop will never get called, the UI will
130 * automatically exit out of this mode.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 */
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700132 void onDrop(DragObject dragObject, DragOptions options);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Adam Cohencb3382b2011-05-24 14:07:08 -0700134 void onDragEnter(DragObject dragObject);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Adam Cohencb3382b2011-05-24 14:07:08 -0700136 void onDragOver(DragObject dragObject);
137
138 void onDragExit(DragObject dragObject);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 /**
Jeff Sharkey70864282009-04-07 21:08:40 -0700141 * Check if a drop action can occur at, or near, the requested location.
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700142 * This will be called just before onDrop.
Jeff Sharkey70864282009-04-07 21:08:40 -0700143 * @return True if the drop will be accepted, false otherwise.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 */
Adam Cohencb3382b2011-05-24 14:07:08 -0700145 boolean acceptDrop(DragObject dragObject);
Jeff Sharkey70864282009-04-07 21:08:40 -0700146
Sunny Goyale9b651e2015-04-24 11:44:51 -0700147 void prepareAccessibilityDrop();
148
Joe Onorato00acb122009-08-04 16:04:30 -0400149 // These methods are implemented in Views
Adam Cohen7d30a372013-07-01 17:03:59 -0700150 void getHitRectRelativeToDragLayer(Rect outRect);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151}