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