1 /*
2  * Copyright (C) 2018 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 package com.android.launcher3.touch;
17 
18 import static android.view.View.INVISIBLE;
19 import static android.view.View.VISIBLE;
20 
21 import static com.android.launcher3.LauncherState.ALL_APPS;
22 import static com.android.launcher3.LauncherState.NORMAL;
23 import static com.android.launcher3.LauncherState.OVERVIEW;
24 
25 import android.util.Log;
26 import android.view.View;
27 import android.view.View.OnLongClickListener;
28 
29 import com.android.launcher3.CellLayout;
30 import com.android.launcher3.DeviceProfile;
31 import com.android.launcher3.DropTarget;
32 import com.android.launcher3.ItemInfo;
33 import com.android.launcher3.Launcher;
34 import com.android.launcher3.dragndrop.DragController;
35 import com.android.launcher3.dragndrop.DragOptions;
36 import com.android.launcher3.folder.Folder;
37 import com.android.launcher3.testing.TestProtocol;
38 
39 import java.util.Arrays;
40 
41 /**
42  * Class to handle long-clicks on workspace items and start drag as a result.
43  */
44 public class ItemLongClickListener {
45 
46     public static OnLongClickListener INSTANCE_WORKSPACE =
47             ItemLongClickListener::onWorkspaceItemLongClick;
48 
49     public static OnLongClickListener INSTANCE_ALL_APPS =
50             ItemLongClickListener::onAllAppsItemLongClick;
51 
onWorkspaceItemLongClick(View v)52     private static boolean onWorkspaceItemLongClick(View v) {
53         Launcher launcher = Launcher.getLauncher(v.getContext());
54         if (!canStartDrag(launcher)) return false;
55         if (!launcher.isInState(NORMAL) && !launcher.isInState(OVERVIEW)) return false;
56         if (!(v.getTag() instanceof ItemInfo)) return false;
57 
58         launcher.setWaitingForResult(null);
59         beginDrag(v, launcher, (ItemInfo) v.getTag(), new DragOptions());
60         return true;
61     }
62 
beginDrag(View v, Launcher launcher, ItemInfo info, DragOptions dragOptions)63     public static void beginDrag(View v, Launcher launcher, ItemInfo info,
64             DragOptions dragOptions) {
65         if (info.container >= 0) {
66             Folder folder = Folder.getOpen(launcher);
67             if (folder != null) {
68                 if (!folder.getIconsInReadingOrder().contains(v)) {
69                     folder.close(true);
70                 } else {
71                     folder.startDrag(v, dragOptions);
72                     return;
73                 }
74             }
75         }
76 
77         CellLayout.CellInfo longClickCellInfo = new CellLayout.CellInfo(v, info);
78         launcher.getWorkspace().startDrag(longClickCellInfo, dragOptions);
79     }
80 
onAllAppsItemLongClick(View v)81     private static boolean onAllAppsItemLongClick(View v) {
82         if (TestProtocol.sDebugTracing) {
83             Log.d(TestProtocol.NO_CONTEXT_MENU, "onAllAppsItemLongClick1");
84         }
85         Launcher launcher = Launcher.getLauncher(v.getContext());
86         if (!canStartDrag(launcher)) return false;
87         if (TestProtocol.sDebugTracing) {
88             Log.d(TestProtocol.NO_CONTEXT_MENU, "onAllAppsItemLongClick2");
89         }
90         // When we have exited all apps or are in transition, disregard long clicks
91         if (!launcher.isInState(ALL_APPS) && !launcher.isInState(OVERVIEW)) return false;
92         if (TestProtocol.sDebugTracing) {
93             Log.d(TestProtocol.NO_CONTEXT_MENU, "onAllAppsItemLongClick3");
94         }
95         if (launcher.getWorkspace().isSwitchingState()) return false;
96 
97         // Start the drag
98         final DragController dragController = launcher.getDragController();
99         dragController.addDragListener(new DragController.DragListener() {
100             @Override
101             public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
102                 v.setVisibility(INVISIBLE);
103             }
104 
105             @Override
106             public void onDragEnd() {
107                 v.setVisibility(VISIBLE);
108                 dragController.removeDragListener(this);
109             }
110         });
111 
112         DeviceProfile grid = launcher.getDeviceProfile();
113         DragOptions options = new DragOptions();
114         options.intrinsicIconScaleFactor = (float) grid.allAppsIconSizePx / grid.iconSizePx;
115         launcher.getWorkspace().beginDragShared(v, launcher.getAppsView(), options);
116         return false;
117     }
118 
canStartDrag(Launcher launcher)119     public static boolean canStartDrag(Launcher launcher) {
120         if (launcher == null) {
121             return false;
122         }
123         // We prevent dragging when we are loading the workspace as it is possible to pick up a view
124         // that is subsequently removed from the workspace in startBinding().
125         if (launcher.isWorkspaceLocked()) return false;
126         // Return early if an item is already being dragged (e.g. when long-pressing two shortcuts)
127         if (launcher.getDragController().isDragging()) return false;
128 
129         return true;
130     }
131 }
132