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.content.Context; 20 import android.graphics.Rect; 21 import android.view.View; 22 23 import com.android.launcher3.accessibility.DragViewStateAnnouncer; 24 import com.android.launcher3.dragndrop.DragOptions; 25 import com.android.launcher3.dragndrop.DragView; 26 import com.android.launcher3.dragndrop.DraggableView; 27 import com.android.launcher3.folder.FolderNameProvider; 28 import com.android.launcher3.logging.InstanceId; 29 import com.android.launcher3.logging.InstanceIdSequence; 30 import com.android.launcher3.model.data.ItemInfo; 31 import com.android.launcher3.util.Executors; 32 33 /** 34 * Interface defining an object that can receive a drag. 35 * 36 */ 37 public interface DropTarget { 38 39 class DragObject { 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 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 55 /** The view that moves around while you drag. */ 56 public DragView dragView = null; 57 58 /** The data associated with the object, after item is dropped. */ 59 public ItemInfo dragInfo = null; 60 61 /** The data associated with the object being dragged */ 62 public ItemInfo originalDragInfo = null; 63 64 /** Where the drag originated */ 65 public DragSource dragSource = null; 66 67 /** Indicates that the drag operation was cancelled */ 68 public boolean cancelled = false; 69 70 /** Defers removing the DragView from the DragLayer until after the drop animation. */ 71 public boolean deferDragViewCleanupPostAnimation = true; 72 73 public DragViewStateAnnouncer stateAnnouncer; 74 75 public FolderNameProvider folderNameProvider; 76 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 81 /** Used for matching DROP event with its corresponding DRAG event on the server side. */ 82 public final InstanceId logInstanceId = new InstanceIdSequence().newInstanceId(); 83 DragObject(Context context)84 public DragObject(Context context) { 85 Executors.MODEL_EXECUTOR.post(() -> 86 folderNameProvider = FolderNameProvider.newInstance(context)); 87 } 88 89 /** 90 * This is used to compute the visual center of the dragView. This point is then 91 * used to visualize drop locations and determine where to drop an item. The idea is that 92 * the visual center represents the user's interpretation of where the item is, and hence 93 * is the appropriate point to use when determining drop location. 94 */ getVisualCenter(float[] recycle)95 public final float[] getVisualCenter(float[] recycle) { 96 final float res[] = (recycle == null) ? new float[2] : recycle; 97 Rect dragRegion = dragView.getDragRegion(); 98 99 // These represent the visual top and left of drag view if a dragRect was provided. 100 // If a dragRect was not provided, then they correspond to the actual view left and 101 // top, as the dragRect is in that case taken to be the entire dragView. 102 int left = x - xOffset - dragRegion.left; 103 int top = y - yOffset - dragRegion.top; 104 105 // In order to find the visual center, we shift by half the dragRect 106 res[0] = left + dragRegion.width() / 2; 107 res[1] = top + dragRegion.height() / 2; 108 109 return res; 110 } 111 } 112 113 /** 114 * Used to temporarily disable certain drop targets 115 * 116 * @return boolean specifying whether this drop target is currently enabled 117 */ isDropEnabled()118 boolean isDropEnabled(); 119 120 /** 121 * Handle an object being dropped on the DropTarget. 122 * 123 * This will be called only if this target previously returned true for {@link #acceptDrop}. It 124 * is the responsibility of this target to exit out of the spring loaded mode (either 125 * immediately or after any pending animations). 126 * 127 * If the drop was cancelled for some reason, onDrop will never get called, the UI will 128 * automatically exit out of this mode. 129 */ onDrop(DragObject dragObject, DragOptions options)130 void onDrop(DragObject dragObject, DragOptions options); 131 onDragEnter(DragObject dragObject)132 void onDragEnter(DragObject dragObject); 133 onDragOver(DragObject dragObject)134 void onDragOver(DragObject dragObject); 135 onDragExit(DragObject dragObject)136 void onDragExit(DragObject dragObject); 137 138 /** 139 * Check if a drop action can occur at, or near, the requested location. 140 * This will be called just before onDrop. 141 * @return True if the drop will be accepted, false otherwise. 142 */ acceptDrop(DragObject dragObject)143 boolean acceptDrop(DragObject dragObject); 144 prepareAccessibilityDrop()145 void prepareAccessibilityDrop(); 146 147 // These methods are implemented in Views getHitRectRelativeToDragLayer(Rect outRect)148 void getHitRectRelativeToDragLayer(Rect outRect); 149 150 /** Returns the drop target view. By default, the implementor class is cast to the view. */ getDropView()151 default View getDropView() { 152 return (View) this; 153 } 154 } 155