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