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