Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.launcher3.dragndrop; |
| 18 | |
Sunny Goyal | d3864fa | 2017-11-14 15:06:36 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.LauncherState.NORMAL; |
Sunny Goyal | 623eddd | 2018-03-02 12:24:41 -0800 | [diff] [blame] | 20 | import static com.android.launcher3.states.RotationHelper.REQUEST_LOCK; |
| 21 | import static com.android.launcher3.states.RotationHelper.REQUEST_NONE; |
Sunny Goyal | d3864fa | 2017-11-14 15:06:36 -0800 | [diff] [blame] | 22 | |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 23 | import android.content.ClipDescription; |
| 24 | import android.content.Intent; |
| 25 | import android.graphics.Point; |
| 26 | import android.graphics.Rect; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Looper; |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | import android.view.DragEvent; |
| 31 | import android.view.View; |
| 32 | |
Sunny Goyal | d3864fa | 2017-11-14 15:06:36 -0800 | [diff] [blame] | 33 | import com.android.launcher3.AbstractFloatingView; |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 34 | import com.android.launcher3.DragSource; |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 35 | import com.android.launcher3.DropTarget.DragObject; |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 36 | import com.android.launcher3.Launcher; |
| 37 | import com.android.launcher3.R; |
Sunny Goyal | e84c5b8 | 2019-09-26 17:05:31 -0700 | [diff] [blame] | 38 | import com.android.launcher3.util.ActivityTracker.SchedulerCallback; |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 39 | import com.android.launcher3.widget.PendingItemDragHelper; |
| 40 | |
| 41 | import java.util.UUID; |
| 42 | |
| 43 | /** |
| 44 | * {@link DragSource} for handling drop from a different window. |
| 45 | */ |
Sunny Goyal | e84c5b8 | 2019-09-26 17:05:31 -0700 | [diff] [blame] | 46 | public abstract class BaseItemDragListener implements View.OnDragListener, DragSource, |
| 47 | DragOptions.PreDragCondition, SchedulerCallback<Launcher> { |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 48 | |
| 49 | private static final String TAG = "BaseItemDragListener"; |
| 50 | |
| 51 | private static final String MIME_TYPE_PREFIX = "com.android.launcher3.drag_and_drop/"; |
| 52 | public static final String EXTRA_PIN_ITEM_DRAG_LISTENER = "pin_item_drag_listener"; |
| 53 | |
| 54 | // Position of preview relative to the touch location |
| 55 | private final Rect mPreviewRect; |
| 56 | |
| 57 | private final int mPreviewBitmapWidth; |
| 58 | private final int mPreviewViewWidth; |
| 59 | |
| 60 | // Randomly generated id used to verify the drag event. |
| 61 | private final String mId; |
| 62 | |
| 63 | protected Launcher mLauncher; |
| 64 | private DragController mDragController; |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 65 | |
| 66 | public BaseItemDragListener(Rect previewRect, int previewBitmapWidth, int previewViewWidth) { |
| 67 | mPreviewRect = previewRect; |
| 68 | mPreviewBitmapWidth = previewBitmapWidth; |
| 69 | mPreviewViewWidth = previewViewWidth; |
| 70 | mId = UUID.randomUUID().toString(); |
| 71 | } |
| 72 | |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 73 | public String getMimeType() { |
| 74 | return MIME_TYPE_PREFIX + mId; |
| 75 | } |
| 76 | |
Sunny Goyal | d3864fa | 2017-11-14 15:06:36 -0800 | [diff] [blame] | 77 | @Override |
Winson Chung | 1a34100 | 2018-01-17 10:00:23 -0800 | [diff] [blame] | 78 | public boolean init(Launcher launcher, boolean alreadyOnHome) { |
Sunny Goyal | d3864fa | 2017-11-14 15:06:36 -0800 | [diff] [blame] | 79 | AbstractFloatingView.closeAllOpenViews(launcher, alreadyOnHome); |
| 80 | launcher.getStateManager().goToState(NORMAL, alreadyOnHome /* animated */); |
| 81 | launcher.getDragLayer().setOnDragListener(this); |
Sunny Goyal | 623eddd | 2018-03-02 12:24:41 -0800 | [diff] [blame] | 82 | launcher.getRotationHelper().setStateHandlerRequest(REQUEST_LOCK); |
Sunny Goyal | d3864fa | 2017-11-14 15:06:36 -0800 | [diff] [blame] | 83 | |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 84 | mLauncher = launcher; |
| 85 | mDragController = launcher.getDragController(); |
Winson Chung | 1a34100 | 2018-01-17 10:00:23 -0800 | [diff] [blame] | 86 | return false; |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean onDrag(View view, DragEvent event) { |
| 91 | if (mLauncher == null || mDragController == null) { |
| 92 | postCleanup(); |
| 93 | return false; |
| 94 | } |
| 95 | if (event.getAction() == DragEvent.ACTION_DRAG_STARTED) { |
| 96 | if (onDragStart(event)) { |
| 97 | return true; |
| 98 | } else { |
| 99 | postCleanup(); |
| 100 | return false; |
| 101 | } |
| 102 | } |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame^] | 103 | return mDragController.onDragEvent(event); |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | protected boolean onDragStart(DragEvent event) { |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 107 | return onDragStart(event, this); |
| 108 | } |
| 109 | |
| 110 | protected boolean onDragStart(DragEvent event, DragOptions.PreDragCondition preDragCondition) { |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 111 | ClipDescription desc = event.getClipDescription(); |
| 112 | if (desc == null || !desc.hasMimeType(getMimeType())) { |
| 113 | Log.e(TAG, "Someone started a dragAndDrop before us."); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | Point downPos = new Point((int) event.getX(), (int) event.getY()); |
| 118 | DragOptions options = new DragOptions(); |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame^] | 119 | options.simulatedDndStartPoint = downPos; |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 120 | options.preDragCondition = preDragCondition; |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 121 | |
| 122 | // We use drag event position as the screenPos for the preview image. Since mPreviewRect |
| 123 | // already includes the view position relative to the drag event on the source window, |
| 124 | // and the absolute position (position relative to the screen) of drag event is same |
| 125 | // across windows, using drag position here give a good estimate for relative position |
| 126 | // to source window. |
| 127 | createDragHelper().startDrag(new Rect(mPreviewRect), |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 128 | mPreviewBitmapWidth, mPreviewViewWidth, downPos, this, options); |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
| 132 | protected abstract PendingItemDragHelper createDragHelper(); |
| 133 | |
| 134 | @Override |
| 135 | public boolean shouldStartDrag(double distanceDragged) { |
| 136 | // Stay in pre-drag mode, if workspace is locked. |
| 137 | return !mLauncher.isWorkspaceLocked(); |
| 138 | } |
| 139 | |
| 140 | @Override |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 141 | public void onPreDragStart(DragObject dragObject) { |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 142 | // The predrag starts when the workspace is not yet loaded. In some cases we set |
| 143 | // the dragLayer alpha to 0 to have a nice fade-in animation. But that will prevent the |
| 144 | // dragView from being visible. Instead just skip the fade-in animation here. |
| 145 | mLauncher.getDragLayer().setAlpha(1); |
| 146 | |
| 147 | dragObject.dragView.setColor( |
| 148 | mLauncher.getResources().getColor(R.color.delete_target_hover_tint)); |
| 149 | } |
| 150 | |
| 151 | @Override |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 152 | public void onPreDragEnd(DragObject dragObject, boolean dragStarted) { |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 153 | if (dragStarted) { |
| 154 | dragObject.dragView.setColor(0); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | @Override |
Sunny Goyal | 1797af4 | 2017-10-06 13:29:57 -0700 | [diff] [blame] | 159 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 160 | postCleanup(); |
| 161 | } |
| 162 | |
Sunny Goyal | 4dcda06 | 2018-05-25 10:32:40 -0700 | [diff] [blame] | 163 | protected void postCleanup() { |
Sunny Goyal | e84c5b8 | 2019-09-26 17:05:31 -0700 | [diff] [blame] | 164 | Launcher.ACTIVITY_TRACKER.clearReference(this); |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 165 | if (mLauncher != null) { |
| 166 | // Remove any drag params from the launcher intent since the drag operation is complete. |
| 167 | Intent newIntent = new Intent(mLauncher.getIntent()); |
| 168 | newIntent.removeExtra(EXTRA_PIN_ITEM_DRAG_LISTENER); |
| 169 | mLauncher.setIntent(newIntent); |
| 170 | } |
| 171 | |
Sunny Goyal | 623eddd | 2018-03-02 12:24:41 -0800 | [diff] [blame] | 172 | new Handler(Looper.getMainLooper()).post(this::removeListener); |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void removeListener() { |
| 176 | if (mLauncher != null) { |
Sunny Goyal | 623eddd | 2018-03-02 12:24:41 -0800 | [diff] [blame] | 177 | mLauncher.getRotationHelper().setStateHandlerRequest(REQUEST_NONE); |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 178 | mLauncher.getDragLayer().setOnDragListener(null); |
| 179 | } |
| 180 | } |
Sunny Goyal | 027fba3 | 2017-06-19 15:30:19 -0700 | [diff] [blame] | 181 | } |