Home | History | Annotate | Download | only in launcher3
      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.PointF;
     21 import android.graphics.Rect;
     22 import android.util.Log;
     23 
     24 /**
     25  * Interface defining an object that can receive a drag.
     26  *
     27  */
     28 public interface DropTarget {
     29 
     30     public static final String TAG = "DropTarget";
     31 
     32     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         /** Post drag animation runnable */
     58         public Runnable postAnimationRunnable = null;
     59 
     60         /** Indicates that the drag operation was cancelled */
     61         public boolean cancelled = false;
     62 
     63         /** Defers removing the DragView from the DragLayer until after the drop animation. */
     64         public boolean deferDragViewCleanupPostAnimation = true;
     65 
     66         public DragObject() {
     67         }
     68     }
     69 
     70     public static class DragEnforcer implements DragController.DragListener {
     71         int dragParity = 0;
     72 
     73         public DragEnforcer(Context context) {
     74             Launcher launcher = (Launcher) context;
     75             launcher.getDragController().addDragListener(this);
     76         }
     77 
     78         void onDragEnter() {
     79             dragParity++;
     80             if (dragParity != 1) {
     81                 Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity);
     82             }
     83         }
     84 
     85         void onDragExit() {
     86             dragParity--;
     87             if (dragParity != 0) {
     88                 Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity);
     89             }
     90         }
     91 
     92         @Override
     93         public void onDragStart(DragSource source, Object info, int dragAction) {
     94             if (dragParity != 0) {
     95                 Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity);
     96             }
     97         }
     98 
     99         @Override
    100         public void onDragEnd() {
    101             if (dragParity != 0) {
    102                 Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity);
    103             }
    104         }
    105     }
    106 
    107     /**
    108      * Used to temporarily disable certain drop targets
    109      *
    110      * @return boolean specifying whether this drop target is currently enabled
    111      */
    112     boolean isDropEnabled();
    113 
    114     /**
    115      * Handle an object being dropped on the DropTarget
    116      *
    117      * @param source DragSource where the drag started
    118      * @param x X coordinate of the drop location
    119      * @param y Y coordinate of the drop location
    120      * @param xOffset Horizontal offset with the object being dragged where the original
    121      *          touch happened
    122      * @param yOffset Vertical offset with the object being dragged where the original
    123      *          touch happened
    124      * @param dragView The DragView that's being dragged around on screen.
    125      * @param dragInfo Data associated with the object being dragged
    126      *
    127      */
    128     void onDrop(DragObject dragObject);
    129 
    130     void onDragEnter(DragObject dragObject);
    131 
    132     void onDragOver(DragObject dragObject);
    133 
    134     void onDragExit(DragObject dragObject);
    135 
    136     /**
    137      * Handle an object being dropped as a result of flinging to delete and will be called in place
    138      * of onDrop().  (This is only called on objects that are set as the DragController's
    139      * fling-to-delete target.
    140      */
    141     void onFlingToDelete(DragObject dragObject, int x, int y, PointF vec);
    142 
    143     /**
    144      * Check if a drop action can occur at, or near, the requested location.
    145      * This will be called just before onDrop.
    146      *
    147      * @param source DragSource where the drag started
    148      * @param x X coordinate of the drop location
    149      * @param y Y coordinate of the drop location
    150      * @param xOffset Horizontal offset with the object being dragged where the
    151      *            original touch happened
    152      * @param yOffset Vertical offset with the object being dragged where the
    153      *            original touch happened
    154      * @param dragView The DragView that's being dragged around on screen.
    155      * @param dragInfo Data associated with the object being dragged
    156      * @return True if the drop will be accepted, false otherwise.
    157      */
    158     boolean acceptDrop(DragObject dragObject);
    159 
    160     // These methods are implemented in Views
    161     void getHitRectRelativeToDragLayer(Rect outRect);
    162     void getLocationInDragLayer(int[] loc);
    163     int getLeft();
    164     int getTop();
    165 }
    166