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.DeviceProfile; 31 import com.android.launcher3.DropTarget; 32 import com.android.launcher3.Launcher; 33 import com.android.launcher3.dragndrop.DragController; 34 import com.android.launcher3.dragndrop.DragOptions; 35 import com.android.launcher3.folder.Folder; 36 import com.android.launcher3.logging.StatsLogManager.StatsLogger; 37 import com.android.launcher3.model.data.ItemInfo; 38 import com.android.launcher3.testing.TestLogging; 39 import com.android.launcher3.testing.TestProtocol; 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 final OnLongClickListener INSTANCE_WORKSPACE = 47 ItemLongClickListener::onWorkspaceItemLongClick; 48 49 public static final OnLongClickListener INSTANCE_ALL_APPS = 50 ItemLongClickListener::onAllAppsItemLongClick; 51 onWorkspaceItemLongClick(View v)52 private static boolean onWorkspaceItemLongClick(View v) { 53 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onWorkspaceItemLongClick"); 54 Launcher launcher = Launcher.getLauncher(v.getContext()); 55 if (!canStartDrag(launcher)) return false; 56 if (!launcher.isInState(NORMAL) && !launcher.isInState(OVERVIEW)) return false; 57 if (!(v.getTag() instanceof ItemInfo)) return false; 58 59 launcher.setWaitingForResult(null); 60 beginDrag(v, launcher, (ItemInfo) v.getTag(), launcher.getDefaultWorkspaceDragOptions()); 61 return true; 62 } 63 beginDrag(View v, Launcher launcher, ItemInfo info, DragOptions dragOptions)64 public static void beginDrag(View v, Launcher launcher, ItemInfo info, 65 DragOptions dragOptions) { 66 if (info.container >= 0) { 67 Folder folder = Folder.getOpen(launcher); 68 if (folder != null) { 69 if (!folder.getIconsInReadingOrder().contains(v)) { 70 folder.close(true); 71 } else { 72 folder.startDrag(v, dragOptions); 73 return; 74 } 75 } 76 } 77 78 CellLayout.CellInfo longClickCellInfo = new CellLayout.CellInfo(v, info); 79 launcher.getWorkspace().startDrag(longClickCellInfo, dragOptions); 80 } 81 onAllAppsItemLongClick(View v)82 private static boolean onAllAppsItemLongClick(View v) { 83 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onAllAppsItemLongClick"); 84 v.cancelLongPress(); 85 Launcher launcher = Launcher.getLauncher(v.getContext()); 86 if (!canStartDrag(launcher)) return false; 87 // When we have exited all apps or are in transition, disregard long clicks 88 if (!launcher.isInState(ALL_APPS) && !launcher.isInState(OVERVIEW)) return false; 89 if (launcher.getWorkspace().isSwitchingState()) return false; 90 91 StatsLogger logger = launcher.getStatsLogManager().logger(); 92 if (v.getTag() instanceof ItemInfo) { 93 logger.withItemInfo((ItemInfo) v.getTag()); 94 } 95 logger.log(LAUNCHER_ALLAPPS_ITEM_LONG_PRESSED); 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