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