blob: e1cb4b8dc3c449441670989a022a45092666532d [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
Mehdi Alizadeh8588fd02018-04-27 14:08:04 -070097 public void addAccessibilityAction(int action, int actionLabel) {
98 mActions.put(action, new AccessibilityAction(action, mLauncher.getText(actionLabel)));
99 }
100
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800101 @Override
102 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
103 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -0700104 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700105 }
106
Sunny Goyal66b24572016-09-21 15:57:55 -0700107 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800108 if (!(host.getTag() instanceof ItemInfo)) return;
109 ItemInfo item = (ItemInfo) host.getTag();
110
Sunny Goyal66b24572016-09-21 15:57:55 -0700111 // If the request came from keyboard, do not add custom shortcuts as that is already
112 // exposed as a direct shortcut
Tony18c4aa42017-05-10 21:23:57 -0500113 if (!fromKeyboard && DeepShortcutManager.supportsShortcuts(item)) {
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700114 info.addAction(mActions.get(DEEP_SHORTCUTS));
115 }
116
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700117 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000118 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700119 info.addAction(mActions.get(target.getAccessibilityAction()));
120 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700121 }
122
Sunny Goyal66b24572016-09-21 15:57:55 -0700123 // Do not add move actions for keyboard request as this uses virtual nodes.
124 if (!fromKeyboard && ((item instanceof ShortcutInfo)
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800125 || (item instanceof LauncherAppWidgetInfo)
Sunny Goyal66b24572016-09-21 15:57:55 -0700126 || (item instanceof FolderInfo))) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800127 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700128
129 if (item.container >= 0) {
130 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700131 } else if (item instanceof LauncherAppWidgetInfo) {
132 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
133 info.addAction(mActions.get(RESIZE));
134 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700135 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700136 }
137
138 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800139 info.addAction(mActions.get(ADD_TO_WORKSPACE));
140 }
141 }
142
143 @Override
144 public boolean performAccessibilityAction(View host, int action, Bundle args) {
145 if ((host.getTag() instanceof ItemInfo)
146 && performAction(host, (ItemInfo) host.getTag(), action)) {
147 return true;
148 }
149 return super.performAccessibilityAction(host, action, args);
150 }
151
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700152 public boolean performAction(final View host, final ItemInfo item, int action) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700153 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800154 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700156 final int[] coordinates = new int[2];
157 final long screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700158 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800159
160 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700161 public void run() {
162 if (item instanceof AppInfo) {
163 ShortcutInfo info = ((AppInfo) item).makeShortcut();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800164 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700165 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700166 screenId, coordinates[0], coordinates[1]);
167
168 ArrayList<ItemInfo> itemList = new ArrayList<>();
169 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700170 mLauncher.bindItems(itemList, true);
Sunny Goyala9116722015-04-29 13:55:58 -0700171 } else if (item instanceof PendingAddItemInfo) {
172 PendingAddItemInfo info = (PendingAddItemInfo) item;
173 Workspace workspace = mLauncher.getWorkspace();
174 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700175 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700176 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800177 }
Sunny Goyala9116722015-04-29 13:55:58 -0700178 announceConfirmation(R.string.item_added_to_workspace);
179 }
180 });
181 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700182 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700183 Folder folder = Folder.getOpen(mLauncher);
184 folder.close(true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700185 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700186 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700187
188 final int[] coordinates = new int[2];
189 final long screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800190 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700191 LauncherSettings.Favorites.CONTAINER_DESKTOP,
192 screenId, coordinates[0], coordinates[1]);
193
194 // Bind the item in next frame so that if a new workspace page was created,
195 // it will get laid out.
196 new Handler().post(new Runnable() {
197
198 @Override
199 public void run() {
200 ArrayList<ItemInfo> itemList = new ArrayList<>();
201 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700202 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700203 announceConfirmation(R.string.item_moved);
204 }
205 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700206 } else if (action == RESIZE) {
207 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
208 final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
209 CharSequence[] labels = new CharSequence[actions.size()];
210 for (int i = 0; i < actions.size(); i++) {
211 labels[i] = mLauncher.getText(actions.get(i));
212 }
213
214 new AlertDialog.Builder(mLauncher)
215 .setTitle(R.string.action_resize)
216 .setItems(labels, new DialogInterface.OnClickListener() {
217
218 @Override
219 public void onClick(DialogInterface dialog, int which) {
220 performResizeAction(actions.get(which), host, info);
221 dialog.dismiss();
222 }
223 })
224 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700225 return true;
226 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800227 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700228 } else {
229 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000230 if (dropTarget.supportsAccessibilityDrop(item, host) &&
231 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700232 dropTarget.onAccessibilityDrop(host, item);
233 return true;
234 }
235 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800236 }
237 return false;
238 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800239
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700240 private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700241 ArrayList<Integer> actions = new ArrayList<>();
242
Sunny Goyald3d8c952015-06-01 10:09:06 -0700243 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
244 if (providerInfo == null) {
245 return actions;
246 }
247
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700248 CellLayout layout = (CellLayout) host.getParent().getParent();
249 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
250 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
251 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
252 actions.add(R.string.action_increase_width);
253 }
254
255 if (info.spanX > info.minSpanX && info.spanX > 1) {
256 actions.add(R.string.action_decrease_width);
257 }
258 }
259
260 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
261 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
262 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
263 actions.add(R.string.action_increase_height);
264 }
265
266 if (info.spanY > info.minSpanY && info.spanY > 1) {
267 actions.add(R.string.action_decrease_height);
268 }
269 }
270 return actions;
271 }
272
Sunny Goyal316490e2015-06-02 09:38:28 -0700273 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700274 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
275 CellLayout layout = (CellLayout) host.getParent().getParent();
276 layout.markCellsAsUnoccupiedForView(host);
277
278 if (action == R.string.action_increase_width) {
279 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
280 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
281 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
282 lp.cellX --;
283 info.cellX --;
284 }
285 lp.cellHSpan ++;
286 info.spanX ++;
287 } else if (action == R.string.action_decrease_width) {
288 lp.cellHSpan --;
289 info.spanX --;
290 } else if (action == R.string.action_increase_height) {
291 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
292 lp.cellY --;
293 info.cellY --;
294 }
295 lp.cellVSpan ++;
296 info.spanY ++;
297 } else if (action == R.string.action_decrease_height) {
298 lp.cellVSpan --;
299 info.spanY --;
300 }
301
302 layout.markCellsAsOccupiedForView(host);
303 Rect sizeRange = new Rect();
304 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
305 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
306 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
307 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800308 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700309 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
310 }
311
Adam Cohen091440a2015-03-18 14:16:05 -0700312 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800313 announceConfirmation(mLauncher.getResources().getString(resId));
314 }
315
Adam Cohen091440a2015-03-18 14:16:05 -0700316 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800317 mLauncher.getDragLayer().announceForAccessibility(confirmation);
318
319 }
320
321 public boolean isInAccessibleDrag() {
322 return mDragInfo != null;
323 }
324
325 public DragInfo getDragInfo() {
326 return mDragInfo;
327 }
328
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700329 /**
330 * @param clickedTarget the actual view that was clicked
331 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
332 * as the actual drop location otherwise the views center is used.
333 */
334 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800335 String confirmation) {
336 if (!isInAccessibleDrag()) return;
337
338 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700339 if (dropLocation == null) {
340 loc[0] = clickedTarget.getWidth() / 2;
341 loc[1] = clickedTarget.getHeight() / 2;
342 } else {
343 loc[0] = dropLocation.centerX();
344 loc[1] = dropLocation.centerY();
345 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800346
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700347 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800348 mLauncher.getDragController().completeAccessibleDrag(loc);
349
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700350 if (!TextUtils.isEmpty(confirmation)) {
351 announceConfirmation(confirmation);
352 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800353 }
354
355 public void beginAccessibleDrag(View item, ItemInfo info) {
356 mDragInfo = new DragInfo();
357 mDragInfo.info = info;
358 mDragInfo.item = item;
359 mDragInfo.dragType = DragType.ICON;
360 if (info instanceof FolderInfo) {
361 mDragInfo.dragType = DragType.FOLDER;
362 } else if (info instanceof LauncherAppWidgetInfo) {
363 mDragInfo.dragType = DragType.WIDGET;
364 }
365
Adam Cohenc9735cf2015-01-23 16:11:55 -0800366 Rect pos = new Rect();
367 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800368 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyal94b510c2016-08-16 15:36:48 -0700369 mLauncher.getDragController().addDragListener(this);
370
371 DragOptions options = new DragOptions();
372 options.isAccessibleDrag = true;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800373 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800374 }
375
Sunny Goyal45478022015-06-08 16:52:41 -0700376 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700377 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700378 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800379 }
380
Sunny Goyal45478022015-06-08 16:52:41 -0700381 @Override
382 public void onDragEnd() {
383 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800384 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800385 }
Sunny Goyala9116722015-04-29 13:55:58 -0700386
387 /**
388 * Find empty space on the workspace and returns the screenId.
389 */
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700390 protected long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700391 Workspace workspace = mLauncher.getWorkspace();
392 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
393 long screenId;
394
395 // First check if there is space on the current screen.
396 int screenIndex = workspace.getCurrentPage();
397 screenId = workspaceScreens.get(screenIndex);
398 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
399
400 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700401 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700402 while (!found && screenIndex < workspaceScreens.size()) {
403 screenId = workspaceScreens.get(screenIndex);
404 layout = (CellLayout) workspace.getPageAt(screenIndex);
405 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
406 screenIndex++;
407 }
408
409 if (found) {
410 return screenId;
411 }
412
413 workspace.addExtraEmptyScreen();
414 screenId = workspace.commitExtraEmptyScreen();
415 layout = workspace.getScreenWithId(screenId);
416 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
417
418 if (!found) {
419 Log.wtf(TAG, "Not enough space on an empty screen");
420 }
421 return screenId;
422 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800423}