2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3.dragndrop;
19 import android.appwidget.AppWidgetManager;
20 import android.content.ClipDescription;
21 import android.graphics.Point;
22 import android.graphics.Rect;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.os.SystemClock;
29 import android.util.Log;
30 import android.view.DragEvent;
31 import android.view.View;
32 import android.widget.RemoteViews;
34 import com.android.launcher3.DeleteDropTarget;
35 import com.android.launcher3.DragSource;
36 import com.android.launcher3.DropTarget;
37 import com.android.launcher3.ItemInfo;
38 import com.android.launcher3.Launcher;
39 import com.android.launcher3.LauncherAppWidgetProviderInfo;
40 import com.android.launcher3.PendingAddItemInfo;
41 import com.android.launcher3.R;
42 import com.android.launcher3.compat.PinItemRequestCompat;
43 import com.android.launcher3.folder.Folder;
44 import com.android.launcher3.userevent.nano.LauncherLogProto;
45 import com.android.launcher3.widget.PendingAddShortcutInfo;
46 import com.android.launcher3.widget.PendingAddWidgetInfo;
47 import com.android.launcher3.widget.PendingItemDragHelper;
48 import com.android.launcher3.widget.WidgetAddFlowHandler;
50 import java.util.UUID;
53 * {@link DragSource} for handling drop from a different window. This object is initialized
54 * in the source window and is passed on to the Launcher activity as an Intent extra.
56 public class PinItemDragListener
57 implements Parcelable, View.OnDragListener, DragSource, DragOptions.PreDragCondition {
59 private static final String TAG = "PinItemDragListener";
61 private static final String MIME_TYPE_PREFIX = "com.android.launcher3.drag_and_drop/";
62 public static final String EXTRA_PIN_ITEM_DRAG_LISTENER = "pin_item_drag_listener";
64 private final PinItemRequestCompat mRequest;
66 // Position of preview relative to the touch location
67 private final Rect mPreviewRect;
69 private final int mPreviewBitmapWidth;
70 private final int mPreviewViewWidth;
72 // Randomly generated id used to verify the drag event.
73 private final String mId;
75 private Launcher mLauncher;
76 private DragController mDragController;
77 private long mDragStartTime;
79 public PinItemDragListener(PinItemRequestCompat request, Rect previewRect,
80 int previewBitmapWidth, int previewViewWidth) {
82 mPreviewRect = previewRect;
83 mPreviewBitmapWidth = previewBitmapWidth;
84 mPreviewViewWidth = previewViewWidth;
85 mId = UUID.randomUUID().toString();
88 private PinItemDragListener(Parcel parcel) {
89 mRequest = PinItemRequestCompat.CREATOR.createFromParcel(parcel);
90 mPreviewRect = Rect.CREATOR.createFromParcel(parcel);
91 mPreviewBitmapWidth = parcel.readInt();
92 mPreviewViewWidth = parcel.readInt();
93 mId = parcel.readString();
96 public String getMimeType() {
97 return MIME_TYPE_PREFIX + mId;
101 public int describeContents() {
106 public void writeToParcel(Parcel parcel, int i) {
107 mRequest.writeToParcel(parcel, i);
108 mPreviewRect.writeToParcel(parcel, i);
109 parcel.writeInt(mPreviewBitmapWidth);
110 parcel.writeInt(mPreviewViewWidth);
111 parcel.writeString(mId);
114 public void setLauncher(Launcher launcher) {
115 mLauncher = launcher;
116 mDragController = launcher.getDragController();
120 public boolean onDrag(View view, DragEvent event) {
121 if (mLauncher == null || mDragController == null) {
125 if (event.getAction() == DragEvent.ACTION_DRAG_STARTED) {
126 if (onDragStart(event)) {
133 return mDragController.onDragEvent(mDragStartTime, event);
136 private boolean onDragStart(DragEvent event) {
137 if (!mRequest.isValid()) {
140 ClipDescription desc = event.getClipDescription();
141 if (desc == null || !desc.hasMimeType(getMimeType())) {
142 Log.e(TAG, "Someone started a dragAndDrop before us.");
146 final PendingAddItemInfo item;
147 if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT) {
148 item = new PendingAddShortcutInfo(
149 new PinShortcutRequestActivityInfo(mRequest, mLauncher));
151 // mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_APPWIDGET
152 LauncherAppWidgetProviderInfo providerInfo =
153 LauncherAppWidgetProviderInfo.fromProviderInfo(
154 mLauncher, mRequest.getAppWidgetProviderInfo(mLauncher));
155 final PinWidgetFlowHandler flowHandler =
156 new PinWidgetFlowHandler(providerInfo, mRequest);
157 item = new PendingAddWidgetInfo(providerInfo) {
159 public WidgetAddFlowHandler getHandler() {
164 View view = new View(mLauncher);
167 Point downPos = new Point((int) event.getX(), (int) event.getY());
168 DragOptions options = new DragOptions();
169 options.systemDndStartPoint = downPos;
170 options.preDragCondition = this;
172 // We use drag event position as the screenPos for the preview image. Since mPreviewRect
173 // already includes the view position relative to the drag event on the source window,
174 // and the absolute position (position relative to the screen) of drag event is same
175 // across windows, using drag position here give a good estimate for relative position
177 PendingItemDragHelper dragHelper = new PendingItemDragHelper(view);
178 if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_APPWIDGET) {
179 dragHelper.setPreview(getPreview(mRequest));
182 dragHelper.startDrag(new Rect(mPreviewRect),
183 mPreviewBitmapWidth, mPreviewViewWidth, downPos, this, options);
184 mDragStartTime = SystemClock.uptimeMillis();
189 public boolean shouldStartDrag(double distanceDragged) {
190 // Stay in pre-drag mode, if workspace is locked.
191 return !mLauncher.isWorkspaceLocked();
195 public void onPreDragStart(DropTarget.DragObject dragObject) {
196 // The predrag starts when the workspace is not yet loaded. In some cases we set
197 // the dragLayer alpha to 0 to have a nice fade-in animation. But that will prevent the
198 // dragView from being visible. Instead just skip the fade-in animation here.
199 mLauncher.getDragLayer().setAlpha(1);
201 dragObject.dragView.setColor(
202 mLauncher.getResources().getColor(R.color.delete_target_hover_tint));
206 public void onPreDragEnd(DropTarget.DragObject dragObject, boolean dragStarted) {
208 dragObject.dragView.setColor(0);
213 public boolean supportsAppInfoDropTarget() {
218 public boolean supportsDeleteDropTarget() {
223 public float getIntrinsicIconScaleFactor() {
228 public void onDropCompleted(View target, DropTarget.DragObject d, boolean isFlingToDelete,
230 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
231 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
232 // Exit spring loaded mode if we have not successfully dropped or have not handled the
234 mLauncher.exitSpringLoadedDragModeDelayed(true,
235 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
241 public void fillInLogContainerData(View v, ItemInfo info, LauncherLogProto.Target target,
242 LauncherLogProto.Target targetParent) {
243 // TODO: We should probably log something
246 private void postCleanup() {
247 new Handler(Looper.getMainLooper()).post(new Runnable() {
255 public void removeListener() {
256 if (mLauncher != null) {
257 mLauncher.getDragLayer().setOnDragListener(null);
261 public static RemoteViews getPreview(PinItemRequestCompat request) {
262 Bundle extras = request.getExtras();
263 if (extras != null &&
264 extras.get(AppWidgetManager.EXTRA_APPWIDGET_PREVIEW) instanceof RemoteViews) {
265 return (RemoteViews) extras.get(AppWidgetManager.EXTRA_APPWIDGET_PREVIEW);
270 public static final Parcelable.Creator<PinItemDragListener> CREATOR =
271 new Parcelable.Creator<PinItemDragListener>() {
272 public PinItemDragListener createFromParcel(Parcel source) {
273 return new PinItemDragListener(source);
276 public PinItemDragListener[] newArray(int size) {
277 return new PinItemDragListener[size];