blob: 4398f6e6b631d56cae61e9dec16ffa96bc295c06 [file] [log] [blame]
Sunny Goyal83a8f042015-05-19 12:52:12 -07001package com.android.launcher3.accessibility;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07003import static com.android.launcher3.LauncherState.NORMAL;
4
Sunny Goyal9ca9c132015-04-29 14:57:22 -07005import android.app.AlertDialog;
6import android.appwidget.AppWidgetProviderInfo;
7import android.content.DialogInterface;
Adam Cohenc9735cf2015-01-23 16:11:55 -08008import android.graphics.Rect;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08009import android.os.Bundle;
Sunny Goyal9ae77772015-04-29 16:30:23 -070010import android.os.Handler;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070011import android.text.TextUtils;
Sunny Goyala9116722015-04-29 13:55:58 -070012import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080013import android.util.SparseArray;
14import android.view.View;
15import android.view.View.AccessibilityDelegate;
16import android.view.accessibility.AccessibilityNodeInfo;
17import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
18
Sunny Goyal83a8f042015-05-19 12:52:12 -070019import com.android.launcher3.AppInfo;
20import com.android.launcher3.AppWidgetResizeFrame;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070021import com.android.launcher3.BubbleTextView;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070022import com.android.launcher3.ButtonDropTarget;
Sunny Goyal83a8f042015-05-19 12:52:12 -070023import com.android.launcher3.CellLayout;
Sunny Goyal94b510c2016-08-16 15:36:48 -070024import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal83a8f042015-05-19 12:52:12 -070025import com.android.launcher3.FolderInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070026import com.android.launcher3.ItemInfo;
27import com.android.launcher3.Launcher;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -080028import com.android.launcher3.touch.ItemLongClickListener;
Sunny Goyal29947f02017-12-18 13:49:44 -080029import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal83a8f042015-05-19 12:52:12 -070030import com.android.launcher3.LauncherAppWidgetInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070031import com.android.launcher3.LauncherSettings;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070032import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal83a8f042015-05-19 12:52:12 -070033import com.android.launcher3.PendingAddItemInfo;
34import com.android.launcher3.R;
35import com.android.launcher3.ShortcutInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070036import com.android.launcher3.Workspace;
Vadim Tryshevfedca432015-08-19 17:55:02 -070037import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyala52ecb02016-12-16 15:04:51 -080038import com.android.launcher3.dragndrop.DragOptions;
39import com.android.launcher3.folder.Folder;
Mario Bertschlerc06af332017-03-28 12:23:22 -070040import com.android.launcher3.popup.PopupContainerWithArrow;
Tony18c4aa42017-05-10 21:23:57 -050041import com.android.launcher3.shortcuts.DeepShortcutManager;
Adam Cohen091440a2015-03-18 14:16:05 -070042import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080043
44import java.util.ArrayList;
45
Sunny Goyal45478022015-06-08 16:52:41 -070046public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080047
Sunny Goyala9116722015-04-29 13:55:58 -070048 private static final String TAG = "LauncherAccessibilityDelegate";
49
Sunny Goyal0236d0b2017-10-24 14:54:30 -070050 public static final int REMOVE = R.id.action_remove;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070051 public static final int UNINSTALL = R.id.action_uninstall;
Winson Chung1054d4e2018-03-05 19:39:21 +000052 public static final int RECONFIGURE = R.id.action_reconfigure;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070053 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
54 protected static final int MOVE = R.id.action_move;
55 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
56 protected static final int RESIZE = R.id.action_resize;
Sunny Goyal66b24572016-09-21 15:57:55 -070057 public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Adam Cohenc9735cf2015-01-23 16:11:55 -080058
Sunny Goyale9b651e2015-04-24 11:44:51 -070059 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080060 ICON,
61 FOLDER,
62 WIDGET
63 }
64
65 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070066 public DragType dragType;
67 public ItemInfo info;
68 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080069 }
70
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070071 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070072 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073
Sunny Goyale9b651e2015-04-24 11:44:51 -070074 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070075
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080076 public LauncherAccessibilityDelegate(Launcher launcher) {
77 mLauncher = launcher;
78
79 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070080 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080081 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070082 launcher.getText(R.string.uninstall_drop_target_label)));
Winson Chung1054d4e2018-03-05 19:39:21 +000083 mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE,
84 launcher.getText(R.string.gadget_setup_text)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080085 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
86 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080087 mActions.put(MOVE, new AccessibilityAction(MOVE,
88 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070089 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
90 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -070091 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
92 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070093 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
94 launcher.getText(R.string.action_deep_shortcut)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080095 }
96
97 @Override
98 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
99 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -0700100 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700101 }
102
Sunny Goyal66b24572016-09-21 15:57:55 -0700103 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800104 if (!(host.getTag() instanceof ItemInfo)) return;
105 ItemInfo item = (ItemInfo) host.getTag();
106
Sunny Goyal66b24572016-09-21 15:57:55 -0700107 // If the request came from keyboard, do not add custom shortcuts as that is already
108 // exposed as a direct shortcut
Tony18c4aa42017-05-10 21:23:57 -0500109 if (!fromKeyboard && DeepShortcutManager.supportsShortcuts(item)) {
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700110 info.addAction(mActions.get(DEEP_SHORTCUTS));
111 }
112
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700113 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000114 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700115 info.addAction(mActions.get(target.getAccessibilityAction()));
116 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700117 }
118
Sunny Goyal66b24572016-09-21 15:57:55 -0700119 // Do not add move actions for keyboard request as this uses virtual nodes.
120 if (!fromKeyboard && ((item instanceof ShortcutInfo)
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800121 || (item instanceof LauncherAppWidgetInfo)
Sunny Goyal66b24572016-09-21 15:57:55 -0700122 || (item instanceof FolderInfo))) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800123 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700124
125 if (item.container >= 0) {
126 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700127 } else if (item instanceof LauncherAppWidgetInfo) {
128 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
129 info.addAction(mActions.get(RESIZE));
130 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700131 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700132 }
133
134 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800135 info.addAction(mActions.get(ADD_TO_WORKSPACE));
136 }
137 }
138
139 @Override
140 public boolean performAccessibilityAction(View host, int action, Bundle args) {
141 if ((host.getTag() instanceof ItemInfo)
142 && performAction(host, (ItemInfo) host.getTag(), action)) {
143 return true;
144 }
145 return super.performAccessibilityAction(host, action, args);
146 }
147
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700148 public boolean performAction(final View host, final ItemInfo item, int action) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700149 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800150 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800151 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700152 final int[] coordinates = new int[2];
153 final long screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700154 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155
156 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700157 public void run() {
158 if (item instanceof AppInfo) {
159 ShortcutInfo info = ((AppInfo) item).makeShortcut();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800160 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700161 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700162 screenId, coordinates[0], coordinates[1]);
163
164 ArrayList<ItemInfo> itemList = new ArrayList<>();
165 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700166 mLauncher.bindItems(itemList, true);
Sunny Goyala9116722015-04-29 13:55:58 -0700167 } else if (item instanceof PendingAddItemInfo) {
168 PendingAddItemInfo info = (PendingAddItemInfo) item;
169 Workspace workspace = mLauncher.getWorkspace();
170 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700171 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700172 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800173 }
Sunny Goyala9116722015-04-29 13:55:58 -0700174 announceConfirmation(R.string.item_added_to_workspace);
175 }
176 });
177 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700178 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700179 Folder folder = Folder.getOpen(mLauncher);
180 folder.close(true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700181 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700182 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700183
184 final int[] coordinates = new int[2];
185 final long screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800186 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700187 LauncherSettings.Favorites.CONTAINER_DESKTOP,
188 screenId, coordinates[0], coordinates[1]);
189
190 // Bind the item in next frame so that if a new workspace page was created,
191 // it will get laid out.
192 new Handler().post(new Runnable() {
193
194 @Override
195 public void run() {
196 ArrayList<ItemInfo> itemList = new ArrayList<>();
197 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700198 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700199 announceConfirmation(R.string.item_moved);
200 }
201 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700202 } else if (action == RESIZE) {
203 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
204 final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
205 CharSequence[] labels = new CharSequence[actions.size()];
206 for (int i = 0; i < actions.size(); i++) {
207 labels[i] = mLauncher.getText(actions.get(i));
208 }
209
210 new AlertDialog.Builder(mLauncher)
211 .setTitle(R.string.action_resize)
212 .setItems(labels, new DialogInterface.OnClickListener() {
213
214 @Override
215 public void onClick(DialogInterface dialog, int which) {
216 performResizeAction(actions.get(which), host, info);
217 dialog.dismiss();
218 }
219 })
220 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700221 return true;
222 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800223 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700224 } else {
225 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000226 if (dropTarget.supportsAccessibilityDrop(item, host) &&
227 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700228 dropTarget.onAccessibilityDrop(host, item);
229 return true;
230 }
231 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800232 }
233 return false;
234 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800235
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700236 private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700237 ArrayList<Integer> actions = new ArrayList<>();
238
Sunny Goyald3d8c952015-06-01 10:09:06 -0700239 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
240 if (providerInfo == null) {
241 return actions;
242 }
243
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700244 CellLayout layout = (CellLayout) host.getParent().getParent();
245 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
246 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
247 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
248 actions.add(R.string.action_increase_width);
249 }
250
251 if (info.spanX > info.minSpanX && info.spanX > 1) {
252 actions.add(R.string.action_decrease_width);
253 }
254 }
255
256 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
257 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
258 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
259 actions.add(R.string.action_increase_height);
260 }
261
262 if (info.spanY > info.minSpanY && info.spanY > 1) {
263 actions.add(R.string.action_decrease_height);
264 }
265 }
266 return actions;
267 }
268
Sunny Goyal316490e2015-06-02 09:38:28 -0700269 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700270 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
271 CellLayout layout = (CellLayout) host.getParent().getParent();
272 layout.markCellsAsUnoccupiedForView(host);
273
274 if (action == R.string.action_increase_width) {
275 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
276 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
277 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
278 lp.cellX --;
279 info.cellX --;
280 }
281 lp.cellHSpan ++;
282 info.spanX ++;
283 } else if (action == R.string.action_decrease_width) {
284 lp.cellHSpan --;
285 info.spanX --;
286 } else if (action == R.string.action_increase_height) {
287 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
288 lp.cellY --;
289 info.cellY --;
290 }
291 lp.cellVSpan ++;
292 info.spanY ++;
293 } else if (action == R.string.action_decrease_height) {
294 lp.cellVSpan --;
295 info.spanY --;
296 }
297
298 layout.markCellsAsOccupiedForView(host);
299 Rect sizeRange = new Rect();
300 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
301 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
302 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
303 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800304 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700305 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
306 }
307
Adam Cohen091440a2015-03-18 14:16:05 -0700308 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800309 announceConfirmation(mLauncher.getResources().getString(resId));
310 }
311
Adam Cohen091440a2015-03-18 14:16:05 -0700312 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800313 mLauncher.getDragLayer().announceForAccessibility(confirmation);
314
315 }
316
317 public boolean isInAccessibleDrag() {
318 return mDragInfo != null;
319 }
320
321 public DragInfo getDragInfo() {
322 return mDragInfo;
323 }
324
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700325 /**
326 * @param clickedTarget the actual view that was clicked
327 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
328 * as the actual drop location otherwise the views center is used.
329 */
330 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800331 String confirmation) {
332 if (!isInAccessibleDrag()) return;
333
334 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700335 if (dropLocation == null) {
336 loc[0] = clickedTarget.getWidth() / 2;
337 loc[1] = clickedTarget.getHeight() / 2;
338 } else {
339 loc[0] = dropLocation.centerX();
340 loc[1] = dropLocation.centerY();
341 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800342
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700343 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800344 mLauncher.getDragController().completeAccessibleDrag(loc);
345
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700346 if (!TextUtils.isEmpty(confirmation)) {
347 announceConfirmation(confirmation);
348 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800349 }
350
351 public void beginAccessibleDrag(View item, ItemInfo info) {
352 mDragInfo = new DragInfo();
353 mDragInfo.info = info;
354 mDragInfo.item = item;
355 mDragInfo.dragType = DragType.ICON;
356 if (info instanceof FolderInfo) {
357 mDragInfo.dragType = DragType.FOLDER;
358 } else if (info instanceof LauncherAppWidgetInfo) {
359 mDragInfo.dragType = DragType.WIDGET;
360 }
361
Adam Cohenc9735cf2015-01-23 16:11:55 -0800362 Rect pos = new Rect();
363 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800364 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyal94b510c2016-08-16 15:36:48 -0700365 mLauncher.getDragController().addDragListener(this);
366
367 DragOptions options = new DragOptions();
368 options.isAccessibleDrag = true;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800369 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800370 }
371
Sunny Goyal45478022015-06-08 16:52:41 -0700372 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700373 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700374 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800375 }
376
Sunny Goyal45478022015-06-08 16:52:41 -0700377 @Override
378 public void onDragEnd() {
379 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800380 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800381 }
Sunny Goyala9116722015-04-29 13:55:58 -0700382
383 /**
384 * Find empty space on the workspace and returns the screenId.
385 */
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700386 protected long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700387 Workspace workspace = mLauncher.getWorkspace();
388 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
389 long screenId;
390
391 // First check if there is space on the current screen.
392 int screenIndex = workspace.getCurrentPage();
393 screenId = workspaceScreens.get(screenIndex);
394 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
395
396 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700397 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700398 while (!found && screenIndex < workspaceScreens.size()) {
399 screenId = workspaceScreens.get(screenIndex);
400 layout = (CellLayout) workspace.getPageAt(screenIndex);
401 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
402 screenIndex++;
403 }
404
405 if (found) {
406 return screenId;
407 }
408
409 workspace.addExtraEmptyScreen();
410 screenId = workspace.commitExtraEmptyScreen();
411 layout = workspace.getScreenWithId(screenId);
412 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
413
414 if (!found) {
415 Log.wtf(TAG, "Not enough space on an empty screen");
416 }
417 return screenId;
418 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419}