summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/dragndrop/PinItemDragListener.java
blob: 1a99cc886c425b50b041b7d32a0e9fbfd4128eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher3.dragndrop;

import android.content.ClipDescription;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;

import com.android.launcher3.DeleteDropTarget;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.DragSource;
import com.android.launcher3.DropTarget;
import com.android.launcher3.ItemInfo;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.PendingAddItemInfo;
import com.android.launcher3.compat.PinItemRequestCompat;
import com.android.launcher3.folder.Folder;
import com.android.launcher3.graphics.LauncherIcons;
import com.android.launcher3.shortcuts.ShortcutInfoCompat;
import com.android.launcher3.userevent.nano.LauncherLogProto;
import com.android.launcher3.widget.PendingAddShortcutInfo;
import com.android.launcher3.widget.PendingAddWidgetInfo;
import com.android.launcher3.widget.PendingItemPreviewProvider;

import java.util.UUID;

/**
 * {@link DragSource} for handling drop from from a different window. This object is initialized
 * in the source window and is passed on to the Launcher activity as an Intent extra.
 */
public class PinItemDragListener implements Parcelable, View.OnDragListener, DragSource {

    private static final String TAG = "PinItemDragListener";

    private static final String MIME_TYPE_PREFIX = "com.android.launcher3.drag_and_drop/";
    public static final String EXTRA_PIN_ITEM_DRAG_LISTENER = "pin_item_drag_listener";

    private final PinItemRequestCompat mRequest;

    // Position of preview relative to the touch location
    private final Rect mPreviewRect;

    // Randomly generated id used to verify the drag event.
    private final String mId;

    private Launcher mLauncher;
    private DragController mDragController;
    private long mDragStartTime;

    public PinItemDragListener(PinItemRequestCompat request, Rect previewRect) {
        mRequest = request;
        mPreviewRect = previewRect;
        mId = UUID.randomUUID().toString();
    }

    private PinItemDragListener(Parcel parcel) {
        mRequest = PinItemRequestCompat.CREATOR.createFromParcel(parcel);
        mPreviewRect = Rect.CREATOR.createFromParcel(parcel);
        mId = parcel.readString();
    }

    public String getMimeType() {
        return MIME_TYPE_PREFIX + mId;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        mRequest.writeToParcel(parcel, i);
        mPreviewRect.writeToParcel(parcel, i);
        parcel.writeString(mId);
    }

    public void setLauncher(Launcher launcher) {
        mLauncher = launcher;
        mDragController = launcher.getDragController();
    }

    @Override
    public boolean onDrag(View view, DragEvent event) {
        if (mLauncher == null || mDragController == null) {
            postCleanup();
            return false;
        }
        if (event.getAction() == DragEvent.ACTION_DRAG_STARTED) {
            if (onDragStart(event)) {
                return true;
            } else {
                postCleanup();
                return false;
            }
        }
        return mDragController.onDragEvent(mDragStartTime, event);
    }

    private boolean onDragStart(DragEvent event) {
        if (!mRequest.isValid()) {
            return false;
        }
        ClipDescription desc =  event.getClipDescription();
        if (desc == null || !desc.hasMimeType(getMimeType())) {
            Log.e(TAG, "Someone started a dragAndDrop before us.");
            return false;
        }

        if (mLauncher.isWorkspaceLocked()) {
            // TODO: implement wait
            return false;
        }

        final PendingAddItemInfo item;
        final Bitmap preview;

        Point dragShift = new Point(mPreviewRect.left, mPreviewRect.top);
        if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT) {
            item = new PendingAddShortcutInfo(
                    new PinShortcutRequestActivityInfo(mRequest, mLauncher));

            ShortcutInfoCompat compat = new ShortcutInfoCompat(mRequest.getShortcutInfo());
            Bitmap icon = LauncherIcons.createShortcutIcon(compat, mLauncher, false /* badged */);

            // Create a preview same as the workspace cell size and draw the icon at the
            // appropriate position.
            int[] size = mLauncher.getWorkspace().estimateItemSize(item, true, false);
            preview = Bitmap.createBitmap(size[0], size[1], Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(preview);
            DeviceProfile dp = mLauncher.getDeviceProfile();
            c.drawBitmap(icon, (size[0] - icon.getWidth()) / 2,
                    (size[1] - icon.getHeight() - dp.iconTextSizePx - dp.iconDrawablePaddingPx) / 2,
                    new Paint(Paint.FILTER_BITMAP_FLAG));
        } else {
            PendingAddWidgetInfo info = new PendingAddWidgetInfo(
                    LauncherAppWidgetProviderInfo.fromProviderInfo(
                            mLauncher, mRequest.getAppWidgetProviderInfo(mLauncher)));
            int[] size = mLauncher.getWorkspace().estimateItemSize(info, true, false);

            float minScale = 1.25f;
            int maxWidth = Math.min((int) (mPreviewRect.width() * minScale), size[0]);
            int[] previewSizeBeforeScale = new int[1];
            preview = LauncherAppState.getInstance(mLauncher).getWidgetCache()
                    .generateWidgetPreview(mLauncher, info.info, maxWidth, null,
                            previewSizeBeforeScale);

            dragShift.offset(
                    (mPreviewRect.width() - preview.getWidth()) / 2,
                    (mPreviewRect.height() - preview.getHeight()) / 2);
            item = info;
        }

        PendingItemPreviewProvider previewProvider =
                new PendingItemPreviewProvider(new View(mLauncher), item, preview);

        // Since we are not going through the workspace for starting the drag, set drag related
        // information on the workspace before starting the drag.
        mLauncher.getWorkspace().prepareDragWithProvider(previewProvider);

        Point downPos = new Point((int) event.getX(), (int) event.getY());
        DragOptions options = new DragOptions();
        options.systemDndStartPoint = downPos;

        int x = downPos.x + dragShift.x;
        int y = downPos.y + dragShift.y;
        mDragController.startDrag(
                preview, x, y, this, item, null, null, 1f, options);
        mDragStartTime = SystemClock.uptimeMillis();
        return true;
    }

    @Override
    public boolean supportsAppInfoDropTarget() {
        return false;
    }

    @Override
    public boolean supportsDeleteDropTarget() {
        return false;
    }

    @Override
    public float getIntrinsicIconScaleFactor() {
        return 1f;
    }

    @Override
    public void onDropCompleted(View target, DropTarget.DragObject d, boolean isFlingToDelete,
            boolean success) {
        if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
                !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
            // Exit spring loaded mode if we have not successfully dropped or have not handled the
            // drop in Workspace
            mLauncher.exitSpringLoadedDragModeDelayed(true,
                    Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
        }
        postCleanup();
    }

    @Override
    public void fillInLogContainerData(View v, ItemInfo info, LauncherLogProto.Target target,
            LauncherLogProto.Target targetParent) {
        // TODO: We should probably log something
    }

    private void postCleanup() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                removeListener();
            }
        });
    }

    public void removeListener() {
        if (mLauncher != null) {
            mLauncher.getDragLayer().setOnDragListener(null);
        }
    }

    public static final Parcelable.Creator<PinItemDragListener> CREATOR =
            new Parcelable.Creator<PinItemDragListener>() {
                public PinItemDragListener createFromParcel(Parcel source) {
                    return new PinItemDragListener(source);
                }

                public PinItemDragListener[] newArray(int size) {
                    return new PinItemDragListener[size];
                }
            };
}