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