• 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 
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 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ALLAPPS_ITEM_LONG_PRESSED;
25 
26 import android.view.View;
27 import android.view.View.OnLongClickListener;
28 
29 import com.android.launcher3.CellLayout;
30 import com.android.launcher3.DropTarget;
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 import com.android.launcher3.logging.StatsLogManager.StatsLogger;
36 import com.android.launcher3.model.data.ItemInfo;
37 import com.android.launcher3.testing.TestLogging;
38 import com.android.launcher3.testing.shared.TestProtocol;
39 import com.android.launcher3.views.BubbleTextHolder;
40 import com.android.launcher3.widget.LauncherAppWidgetHostView;
41 
42 /**
43  * Class to handle long-clicks on workspace items and start drag as a result.
44  */
45 public class ItemLongClickListener {
46 
47     public static final OnLongClickListener INSTANCE_WORKSPACE =
48             ItemLongClickListener::onWorkspaceItemLongClick;
49 
50     public static final OnLongClickListener INSTANCE_ALL_APPS =
51             ItemLongClickListener::onAllAppsItemLongClick;
52 
onWorkspaceItemLongClick(View v)53     private static boolean onWorkspaceItemLongClick(View v) {
54         if (v instanceof LauncherAppWidgetHostView) {
55             TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "Widgets.onLongClick");
56         } else {
57             TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onWorkspaceItemLongClick");
58         }
59         Launcher launcher = Launcher.getLauncher(v.getContext());
60         if (!canStartDrag(launcher)) return false;
61         if (!launcher.isInState(NORMAL) && !launcher.isInState(OVERVIEW)) return false;
62         if (!(v.getTag() instanceof ItemInfo)) return false;
63 
64         launcher.setWaitingForResult(null);
65         beginDrag(v, launcher, (ItemInfo) v.getTag(), launcher.getDefaultWorkspaceDragOptions());
66         return true;
67     }
68 
beginDrag(View v, Launcher launcher, ItemInfo info, DragOptions dragOptions)69     public static void beginDrag(View v, Launcher launcher, ItemInfo info,
70             DragOptions dragOptions) {
71         if (info.container >= 0) {
72             Folder folder = Folder.getOpen(launcher);
73             if (folder != null) {
74                 if (!folder.getIconsInReadingOrder().contains(v)) {
75                     folder.close(true);
76                 } else {
77                     folder.startDrag(v, dragOptions);
78                     return;
79                 }
80             }
81         }
82 
83         CellLayout.CellInfo longClickCellInfo = new CellLayout.CellInfo(v, info,
84                 launcher.getCellPosMapper().mapModelToPresenter(info));
85         launcher.getWorkspace().startDrag(longClickCellInfo, dragOptions);
86     }
87 
onAllAppsItemLongClick(View view)88     private static boolean onAllAppsItemLongClick(View view) {
89         TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onAllAppsItemLongClick");
90         view.cancelLongPress();
91         View v = (view instanceof BubbleTextHolder)
92                 ? ((BubbleTextHolder) view).getBubbleText()
93                 : view;
94         Launcher launcher = Launcher.getLauncher(v.getContext());
95         if (!canStartDrag(launcher)) return false;
96         // When we have exited all apps or are in transition, disregard long clicks
97         if (!launcher.isInState(ALL_APPS) && !launcher.isInState(OVERVIEW)) return false;
98         if (launcher.getWorkspace().isSwitchingState()) return false;
99 
100         StatsLogger logger = launcher.getStatsLogManager().logger();
101         if (v.getTag() instanceof ItemInfo) {
102             logger.withItemInfo((ItemInfo) v.getTag());
103         }
104         logger.log(LAUNCHER_ALLAPPS_ITEM_LONG_PRESSED);
105 
106         // Start the drag
107         final DragController dragController = launcher.getDragController();
108         dragController.addDragListener(new DragController.DragListener() {
109             @Override
110             public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
111                 v.setVisibility(INVISIBLE);
112             }
113 
114             @Override
115             public void onDragEnd() {
116                 v.setVisibility(VISIBLE);
117                 dragController.removeDragListener(this);
118             }
119         });
120 
121         launcher.getWorkspace().beginDragShared(v, launcher.getAppsView(), new DragOptions());
122         return false;
123     }
124 
canStartDrag(Launcher launcher)125     public static boolean canStartDrag(Launcher launcher) {
126         if (launcher == null) {
127             return false;
128         }
129         // We prevent dragging when we are loading the workspace as it is possible to pick up a view
130         // that is subsequently removed from the workspace in startBinding().
131         if (launcher.isWorkspaceLocked()) return false;
132         // Return early if an item is already being dragged (e.g. when long-pressing two shortcuts)
133         if (launcher.getDragController().isDragging()) return false;
134 
135         return true;
136     }
137 }
138