Home | History | Annotate | Download | only in list
      1 package com.android.dialer.list;
      2 
      3 import android.util.Log;
      4 import android.view.View;
      5 
      6 import com.android.contacts.common.compat.CompatUtils;
      7 
      8 import java.util.ArrayList;
      9 import java.util.List;
     10 
     11 /**
     12  * Class that handles and combines drag events generated from multiple views, and then fires
     13  * off events to any OnDragDropListeners that have registered for callbacks.
     14  */
     15 public class DragDropController {
     16 
     17     private final List<OnDragDropListener> mOnDragDropListeners =
     18             new ArrayList<OnDragDropListener>();
     19     private final DragItemContainer mDragItemContainer;
     20     private final int[] mLocationOnScreen = new int[2];
     21 
     22     /**
     23      * Callback interface used to retrieve views based on the current touch coordinates of the
     24      * drag event. The {@link DragItemContainer} houses the draggable views that this
     25      * {@link DragDropController} controls.
     26      */
     27     public interface DragItemContainer {
     28         public PhoneFavoriteSquareTileView getViewForLocation(int x, int y);
     29     }
     30 
     31     public DragDropController(DragItemContainer dragItemContainer) {
     32         mDragItemContainer = dragItemContainer;
     33     }
     34 
     35     /**
     36      * @return True if the drag is started, false if the drag is cancelled for some reason.
     37      */
     38     boolean handleDragStarted(View v, int x, int y) {
     39         int screenX = x;
     40         int screenY = y;
     41         // The coordinates in dragEvent of DragEvent.ACTION_DRAG_STARTED before NYC is window-related.
     42         // This is fixed in NYC.
     43         if (CompatUtils.isNCompatible()) {
     44             v.getLocationOnScreen(mLocationOnScreen);
     45             screenX = x + mLocationOnScreen[0];
     46             screenY = y + mLocationOnScreen[1];
     47         }
     48         final PhoneFavoriteSquareTileView tileView = mDragItemContainer.getViewForLocation(
     49                 screenX, screenY);
     50         if (tileView == null) {
     51             return false;
     52         }
     53         for (int i = 0; i < mOnDragDropListeners.size(); i++) {
     54             mOnDragDropListeners.get(i).onDragStarted(screenX, screenY, tileView);
     55         }
     56 
     57         return true;
     58     }
     59 
     60     public void handleDragHovered(View v, int x, int y) {
     61         v.getLocationOnScreen(mLocationOnScreen);
     62         final int screenX = x + mLocationOnScreen[0];
     63         final int screenY = y + mLocationOnScreen[1];
     64         final PhoneFavoriteSquareTileView view = mDragItemContainer.getViewForLocation(
     65                 screenX, screenY);
     66         for (int i = 0; i < mOnDragDropListeners.size(); i++) {
     67             mOnDragDropListeners.get(i).onDragHovered(screenX, screenY, view);
     68         }
     69     }
     70 
     71     public void handleDragFinished(int x, int y, boolean isRemoveView) {
     72         if (isRemoveView) {
     73             for (int i = 0; i < mOnDragDropListeners.size(); i++) {
     74                 mOnDragDropListeners.get(i).onDroppedOnRemove();
     75             }
     76         }
     77 
     78         for (int i = 0; i < mOnDragDropListeners.size(); i++) {
     79             mOnDragDropListeners.get(i).onDragFinished(x, y);
     80         }
     81     }
     82 
     83     public void addOnDragDropListener(OnDragDropListener listener) {
     84         if (!mOnDragDropListeners.contains(listener)) {
     85             mOnDragDropListeners.add(listener);
     86         }
     87     }
     88 
     89     public void removeOnDragDropListener(OnDragDropListener listener) {
     90         if (mOnDragDropListeners.contains(listener)) {
     91             mOnDragDropListeners.remove(listener);
     92         }
     93     }
     94 
     95 }
     96