blob: 2d67ff9ced09ea177a46331fba7c668da6566614 [file] [log] [blame]
Garfield, Tan804133e2016-04-20 15:13:56 -07001/*
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
17package com.android.documentsui;
18
19import android.content.ClipData;
Garfield, Tan57facaf72016-05-27 15:02:35 -070020import android.graphics.drawable.Drawable;
Garfield, Tan804133e2016-04-20 15:13:56 -070021import android.util.Log;
22import android.view.DragEvent;
23import android.view.View;
24import android.view.View.OnDragListener;
Garfield, Tan804133e2016-04-20 15:13:56 -070025
26import com.android.documentsui.ItemDragListener.DragHost;
27import com.android.internal.annotations.VisibleForTesting;
28
29import java.util.Timer;
30import java.util.TimerTask;
31
Ben Lin5a305b42016-09-08 11:33:07 -070032import javax.annotation.Nullable;
33
Garfield, Tan804133e2016-04-20 15:13:56 -070034/**
35 * An {@link OnDragListener} that adds support for "spring loading views". Use this when you want
36 * items to pop-open when user hovers on them during a drag n drop.
37 */
38public class ItemDragListener<H extends DragHost> implements OnDragListener {
39
40 private static final String TAG = "ItemDragListener";
41
42 @VisibleForTesting
Garfield, Tana5588b62016-07-13 09:23:04 -070043 static final int SPRING_TIMEOUT = 1000;
Garfield, Tan804133e2016-04-20 15:13:56 -070044
45 protected final H mDragHost;
46 private final Timer mHoverTimer;
47
48 public ItemDragListener(H dragHost) {
49 this(dragHost, new Timer());
50 }
51
52 @VisibleForTesting
53 protected ItemDragListener(H dragHost, Timer timer) {
54 mDragHost = dragHost;
55 mHoverTimer = timer;
56 }
57
58 @Override
59 public boolean onDrag(final View v, DragEvent event) {
60 switch (event.getAction()) {
61 case DragEvent.ACTION_DRAG_STARTED:
62 return true;
63 case DragEvent.ACTION_DRAG_ENTERED:
Ben Lin5a305b42016-09-08 11:33:07 -070064 handleEnteredEvent(v, event);
Garfield, Tan804133e2016-04-20 15:13:56 -070065 return true;
66 case DragEvent.ACTION_DRAG_LOCATION:
Garfield, Tan57facaf72016-05-27 15:02:35 -070067 handleLocationEvent(v, event.getX(), event.getY());
Garfield, Tan804133e2016-04-20 15:13:56 -070068 return true;
69 case DragEvent.ACTION_DRAG_EXITED:
70 case DragEvent.ACTION_DRAG_ENDED:
71 handleExitedEndedEvent(v);
72 return true;
73 case DragEvent.ACTION_DROP:
74 return handleDropEvent(v, event);
75 }
76
77 return false;
78 }
79
Ben Lin5a305b42016-09-08 11:33:07 -070080 private void handleEnteredEvent(View v, DragEvent event) {
81 @Nullable TimerTask task = createOpenTask(v, event);
82 if (task == null) {
83 return;
84 }
Garfield, Tan804133e2016-04-20 15:13:56 -070085 mDragHost.setDropTargetHighlight(v, true);
Garfield, Tan804133e2016-04-20 15:13:56 -070086 v.setTag(R.id.drag_hovering_tag, task);
Garfield, Tana5588b62016-07-13 09:23:04 -070087 mHoverTimer.schedule(task, SPRING_TIMEOUT);
Garfield, Tan804133e2016-04-20 15:13:56 -070088 }
89
Garfield, Tan57facaf72016-05-27 15:02:35 -070090 private void handleLocationEvent(View v, float x, float y) {
91 Drawable background = v.getBackground();
92 if (background != null) {
93 background.setHotspot(x, y);
94 }
95 }
96
Garfield, Tan804133e2016-04-20 15:13:56 -070097 private void handleExitedEndedEvent(View v) {
98 mDragHost.setDropTargetHighlight(v, false);
99
100 TimerTask task = (TimerTask) v.getTag(R.id.drag_hovering_tag);
101 if (task != null) {
102 task.cancel();
103 }
104 }
105
106 private boolean handleDropEvent(View v, DragEvent event) {
107 ClipData clipData = event.getClipData();
108 if (clipData == null) {
109 Log.w(TAG, "Received invalid drop event with null clipdata. Ignoring.");
110 return false;
111 }
112
113 return handleDropEventChecked(v, event);
114 }
115
Ben Lin5a305b42016-09-08 11:33:07 -0700116 /**
117 * Sub-classes such as {@link DirectoryDragListener} can override this method and return null.
118 */
119 public @Nullable TimerTask createOpenTask(final View v, DragEvent event) {
Garfield, Tan804133e2016-04-20 15:13:56 -0700120 TimerTask task = new TimerTask() {
121 @Override
122 public void run() {
123 mDragHost.runOnUiThread(() -> {
124 mDragHost.onViewHovered(v);
125 });
126 }
127 };
128 return task;
129 }
130
131 /**
132 * Handles a drop event. Override it if you want to do something on drop event. It's called when
133 * {@link DragEvent#ACTION_DROP} happens. ClipData in DragEvent is guaranteed not null.
134 *
135 * @param v The view where user drops.
136 * @param event the drag event.
137 * @return true if this event is consumed; false otherwise
138 */
139 public boolean handleDropEventChecked(View v, DragEvent event) {
140 return false; // we didn't handle the drop
141 }
142
143 /**
144 * An interface {@link ItemDragListener} uses to make some callbacks.
145 */
146 public interface DragHost {
147
148 /**
149 * Runs this runnable in main thread.
150 */
151 void runOnUiThread(Runnable runnable);
152
153 /**
154 * Highlights/unhighlights the view to visually indicate this view is being hovered.
155 * @param v the view being hovered
156 * @param highlight true if highlight the view; false if unhighlight it
157 */
158 void setDropTargetHighlight(View v, boolean highlight);
159
160 /**
161 * Notifies hovering timeout has elapsed
162 * @param v the view being hovered
163 */
164 void onViewHovered(View v);
165 }
166}