Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 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.documentsui; |
| 18 | |
| 19 | import android.content.ClipData; |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 20 | import android.graphics.drawable.Drawable; |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 21 | import android.util.Log; |
| 22 | import android.view.DragEvent; |
| 23 | import android.view.View; |
| 24 | import android.view.View.OnDragListener; |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 25 | |
| 26 | import com.android.documentsui.ItemDragListener.DragHost; |
| 27 | import com.android.internal.annotations.VisibleForTesting; |
| 28 | |
| 29 | import java.util.Timer; |
| 30 | import java.util.TimerTask; |
| 31 | |
Ben Lin | 5a305b4 | 2016-09-08 11:33:07 -0700 | [diff] [blame] | 32 | import javax.annotation.Nullable; |
| 33 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 34 | /** |
| 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 | */ |
| 38 | public class ItemDragListener<H extends DragHost> implements OnDragListener { |
| 39 | |
| 40 | private static final String TAG = "ItemDragListener"; |
| 41 | |
| 42 | @VisibleForTesting |
Garfield, Tan | a5588b6 | 2016-07-13 09:23:04 -0700 | [diff] [blame] | 43 | static final int SPRING_TIMEOUT = 1000; |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 44 | |
| 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 Lin | 5a305b4 | 2016-09-08 11:33:07 -0700 | [diff] [blame] | 64 | handleEnteredEvent(v, event); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 65 | return true; |
| 66 | case DragEvent.ACTION_DRAG_LOCATION: |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 67 | handleLocationEvent(v, event.getX(), event.getY()); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 68 | 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 Lin | 5a305b4 | 2016-09-08 11:33:07 -0700 | [diff] [blame] | 80 | private void handleEnteredEvent(View v, DragEvent event) { |
| 81 | @Nullable TimerTask task = createOpenTask(v, event); |
| 82 | if (task == null) { |
| 83 | return; |
| 84 | } |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 85 | mDragHost.setDropTargetHighlight(v, true); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 86 | v.setTag(R.id.drag_hovering_tag, task); |
Garfield, Tan | a5588b6 | 2016-07-13 09:23:04 -0700 | [diff] [blame] | 87 | mHoverTimer.schedule(task, SPRING_TIMEOUT); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 90 | 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, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 97 | 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 Lin | 5a305b4 | 2016-09-08 11:33:07 -0700 | [diff] [blame] | 116 | /** |
| 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, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 120 | 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 | } |