blob: 75693c63d0fc35a3e058b383a9946ca36b30603d [file] [log] [blame]
Sunny Goyal027fba32017-06-19 15:30:19 -07001/*
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
17package com.android.launcher3.dragndrop;
18
Sunny Goyald3864fa2017-11-14 15:06:36 -080019import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyal623eddd2018-03-02 12:24:41 -080020import static com.android.launcher3.states.RotationHelper.REQUEST_LOCK;
21import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyald3864fa2017-11-14 15:06:36 -080022
Sunny Goyal027fba32017-06-19 15:30:19 -070023import android.content.ClipDescription;
24import android.content.Intent;
25import android.graphics.Point;
26import android.graphics.Rect;
27import android.os.Handler;
28import android.os.Looper;
Sunny Goyal027fba32017-06-19 15:30:19 -070029import android.os.SystemClock;
30import android.util.Log;
31import android.view.DragEvent;
32import android.view.View;
33
Sunny Goyald3864fa2017-11-14 15:06:36 -080034import com.android.launcher3.AbstractFloatingView;
Sunny Goyal027fba32017-06-19 15:30:19 -070035import com.android.launcher3.DragSource;
Sunny Goyal1797af42017-10-06 13:29:57 -070036import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal027fba32017-06-19 15:30:19 -070037import com.android.launcher3.Launcher;
38import com.android.launcher3.R;
Sunny Goyale84c5b82019-09-26 17:05:31 -070039import com.android.launcher3.util.ActivityTracker.SchedulerCallback;
Sunny Goyal027fba32017-06-19 15:30:19 -070040import com.android.launcher3.widget.PendingItemDragHelper;
41
42import java.util.UUID;
43
44/**
45 * {@link DragSource} for handling drop from a different window.
46 */
Sunny Goyale84c5b82019-09-26 17:05:31 -070047public abstract class BaseItemDragListener implements View.OnDragListener, DragSource,
48 DragOptions.PreDragCondition, SchedulerCallback<Launcher> {
Sunny Goyal027fba32017-06-19 15:30:19 -070049
50 private static final String TAG = "BaseItemDragListener";
51
52 private static final String MIME_TYPE_PREFIX = "com.android.launcher3.drag_and_drop/";
53 public static final String EXTRA_PIN_ITEM_DRAG_LISTENER = "pin_item_drag_listener";
54
55 // Position of preview relative to the touch location
56 private final Rect mPreviewRect;
57
58 private final int mPreviewBitmapWidth;
59 private final int mPreviewViewWidth;
60
61 // Randomly generated id used to verify the drag event.
62 private final String mId;
63
64 protected Launcher mLauncher;
65 private DragController mDragController;
66 private long mDragStartTime;
67
68 public BaseItemDragListener(Rect previewRect, int previewBitmapWidth, int previewViewWidth) {
69 mPreviewRect = previewRect;
70 mPreviewBitmapWidth = previewBitmapWidth;
71 mPreviewViewWidth = previewViewWidth;
72 mId = UUID.randomUUID().toString();
73 }
74
Sunny Goyal027fba32017-06-19 15:30:19 -070075 public String getMimeType() {
76 return MIME_TYPE_PREFIX + mId;
77 }
78
Sunny Goyald3864fa2017-11-14 15:06:36 -080079 @Override
Winson Chung1a341002018-01-17 10:00:23 -080080 public boolean init(Launcher launcher, boolean alreadyOnHome) {
Sunny Goyald3864fa2017-11-14 15:06:36 -080081 AbstractFloatingView.closeAllOpenViews(launcher, alreadyOnHome);
82 launcher.getStateManager().goToState(NORMAL, alreadyOnHome /* animated */);
83 launcher.getDragLayer().setOnDragListener(this);
Sunny Goyal623eddd2018-03-02 12:24:41 -080084 launcher.getRotationHelper().setStateHandlerRequest(REQUEST_LOCK);
Sunny Goyald3864fa2017-11-14 15:06:36 -080085
Sunny Goyal027fba32017-06-19 15:30:19 -070086 mLauncher = launcher;
87 mDragController = launcher.getDragController();
Winson Chung1a341002018-01-17 10:00:23 -080088 return false;
Sunny Goyal027fba32017-06-19 15:30:19 -070089 }
90
91 @Override
92 public boolean onDrag(View view, DragEvent event) {
93 if (mLauncher == null || mDragController == null) {
94 postCleanup();
95 return false;
96 }
97 if (event.getAction() == DragEvent.ACTION_DRAG_STARTED) {
98 if (onDragStart(event)) {
99 return true;
100 } else {
101 postCleanup();
102 return false;
103 }
104 }
105 return mDragController.onDragEvent(mDragStartTime, event);
106 }
107
108 protected boolean onDragStart(DragEvent event) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700109 return onDragStart(event, this);
110 }
111
112 protected boolean onDragStart(DragEvent event, DragOptions.PreDragCondition preDragCondition) {
Sunny Goyal027fba32017-06-19 15:30:19 -0700113 ClipDescription desc = event.getClipDescription();
114 if (desc == null || !desc.hasMimeType(getMimeType())) {
115 Log.e(TAG, "Someone started a dragAndDrop before us.");
116 return false;
117 }
118
119 Point downPos = new Point((int) event.getX(), (int) event.getY());
120 DragOptions options = new DragOptions();
121 options.systemDndStartPoint = downPos;
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700122 options.preDragCondition = preDragCondition;
Sunny Goyal027fba32017-06-19 15:30:19 -0700123
124 // We use drag event position as the screenPos for the preview image. Since mPreviewRect
125 // already includes the view position relative to the drag event on the source window,
126 // and the absolute position (position relative to the screen) of drag event is same
127 // across windows, using drag position here give a good estimate for relative position
128 // to source window.
129 createDragHelper().startDrag(new Rect(mPreviewRect),
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700130 mPreviewBitmapWidth, mPreviewViewWidth, downPos, this, options);
Sunny Goyal027fba32017-06-19 15:30:19 -0700131 mDragStartTime = SystemClock.uptimeMillis();
132 return true;
133 }
134
135 protected abstract PendingItemDragHelper createDragHelper();
136
137 @Override
138 public boolean shouldStartDrag(double distanceDragged) {
139 // Stay in pre-drag mode, if workspace is locked.
140 return !mLauncher.isWorkspaceLocked();
141 }
142
143 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700144 public void onPreDragStart(DragObject dragObject) {
Sunny Goyal027fba32017-06-19 15:30:19 -0700145 // The predrag starts when the workspace is not yet loaded. In some cases we set
146 // the dragLayer alpha to 0 to have a nice fade-in animation. But that will prevent the
147 // dragView from being visible. Instead just skip the fade-in animation here.
148 mLauncher.getDragLayer().setAlpha(1);
149
150 dragObject.dragView.setColor(
151 mLauncher.getResources().getColor(R.color.delete_target_hover_tint));
152 }
153
154 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700155 public void onPreDragEnd(DragObject dragObject, boolean dragStarted) {
Sunny Goyal027fba32017-06-19 15:30:19 -0700156 if (dragStarted) {
157 dragObject.dragView.setColor(0);
158 }
159 }
160
161 @Override
Sunny Goyal1797af42017-10-06 13:29:57 -0700162 public void onDropCompleted(View target, DragObject d, boolean success) {
Sunny Goyal027fba32017-06-19 15:30:19 -0700163 postCleanup();
164 }
165
Sunny Goyal4dcda062018-05-25 10:32:40 -0700166 protected void postCleanup() {
Sunny Goyale84c5b82019-09-26 17:05:31 -0700167 Launcher.ACTIVITY_TRACKER.clearReference(this);
Sunny Goyal027fba32017-06-19 15:30:19 -0700168 if (mLauncher != null) {
169 // Remove any drag params from the launcher intent since the drag operation is complete.
170 Intent newIntent = new Intent(mLauncher.getIntent());
171 newIntent.removeExtra(EXTRA_PIN_ITEM_DRAG_LISTENER);
172 mLauncher.setIntent(newIntent);
173 }
174
Sunny Goyal623eddd2018-03-02 12:24:41 -0800175 new Handler(Looper.getMainLooper()).post(this::removeListener);
Sunny Goyal027fba32017-06-19 15:30:19 -0700176 }
177
178 public void removeListener() {
179 if (mLauncher != null) {
Sunny Goyal623eddd2018-03-02 12:24:41 -0800180 mLauncher.getRotationHelper().setStateHandlerRequest(REQUEST_NONE);
Sunny Goyal027fba32017-06-19 15:30:19 -0700181 mLauncher.getDragLayer().setOnDragListener(null);
182 }
183 }
Sunny Goyal027fba32017-06-19 15:30:19 -0700184}