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