summaryrefslogtreecommitdiff
path: root/src/com/android/dialer/list/PhoneFavoriteDragAndDropListeners.java
blob: a976ead8ccf08625ecb8b0e15de408de73d51f12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.dialer.list;

import android.graphics.Rect;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;

import com.android.dialer.list.PhoneFavoritesTileAdapter.ContactTileRow;

/**
 * Implements the OnLongClickListener and OnDragListener for phone's favorite tiles and rows.
 */
public class PhoneFavoriteDragAndDropListeners {

    private static final String TAG = PhoneFavoriteDragAndDropListeners.class.getSimpleName();
    private static final boolean DEBUG = false;

    /**
     * Implements the OnDragListener to handle drag events.
     */
    public static class PhoneFavoriteDragListener implements OnDragListener {
        /** Location of the drag event. */
        private float mX = 0;
        private float mY = 0;
        private final ContactTileRow mContactTileRow;
        private final PhoneFavoritesTileAdapter mTileAdapter;

        public PhoneFavoriteDragListener(ContactTileRow contactTileRow,
                PhoneFavoritesTileAdapter tileAdapter) {
            super();
            mContactTileRow = contactTileRow;
            mTileAdapter = tileAdapter;
        }

        @Override
        public boolean onDrag(View v, DragEvent event) {
            if (DEBUG) {
                Log.v(TAG, event.toString());
            }
            // Handles drag events.
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    if (mTileAdapter != null && mContactTileRow != null
                            && !mTileAdapter.getInDragging()) {
                        mX = event.getX();
                        mY = event.getY();
                        if (DEBUG) {
                            Log.v(TAG, String.valueOf(mX) + "; " + String.valueOf(mY));
                        }

                        final int[] rowLocation = new int[2];
                        mContactTileRow.getLocationOnScreen(rowLocation);

                        final Rect locationRect = new Rect(rowLocation[0], rowLocation[1],
                                rowLocation[0] + mContactTileRow.getWidth(),
                                rowLocation[1] + mContactTileRow.getHeight());

                        if (locationRect.contains((int) mX, (int) mY)) {
                            // Finds out which item is being dragged.
                            // Computes relative coordinates as we get absolute coordinates.
                            final int dragIndex = mContactTileRow.getItemIndex(
                                    mX - rowLocation[0], mY - rowLocation[1]);
                            if (DEBUG) {
                                Log.v(TAG, "Start dragging " + String.valueOf(dragIndex));
                            }
                            // Indicates a drag has started.
                            mTileAdapter.setInDragging(true);

                            // Temporarily pops out the Contact entry.
                            mTileAdapter.popContactEntry(dragIndex);
                        }
                    }
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    break;
                case DragEvent.ACTION_DROP:
                    mX = event.getX();
                    mY = event.getY();
                    if (DEBUG) {
                        Log.v(TAG, String.valueOf(mX) + "; " + String.valueOf(mY));
                    }

                    // Indicates a drag has finished.
                    if (mTileAdapter != null && mContactTileRow != null) {
                        mTileAdapter.setInDragging(false);

                        // Finds out at which position of the list the Contact is being dropped.
                        final int dropIndex = mContactTileRow.getItemIndex(mX, mY);
                        if (DEBUG) {
                            Log.v(TAG, "Stop dragging " + String.valueOf(dropIndex));
                        }

                        // Adds the dragged contact to the drop position.
                        mTileAdapter.dropContactEntry(dropIndex);
                    }
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    if (mTileAdapter.getInDragging()) {
                        // If the drag and drop ends when the drop happens outside of any rows,
                        // we will end the drag here and put the item back to where it was dragged
                        // from before.
                        mTileAdapter.setInDragging(false);
                        mTileAdapter.dropToUnsupportedView();
                    }
                    break;
                case DragEvent.ACTION_DRAG_LOCATION:
                    break;
                default:
                    break;
            }
            return true;
        }
    }
}