blob: 2cd8b1d729357c1feb03be27e9826e2e51b52d18 [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 Goyal29947f02017-12-18 13:49:44 -080028import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal83a8f042015-05-19 12:52:12 -070029import com.android.launcher3.LauncherAppWidgetInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070030import com.android.launcher3.LauncherSettings;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070031import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal83a8f042015-05-19 12:52:12 -070032import com.android.launcher3.PendingAddItemInfo;
33import com.android.launcher3.R;
34import com.android.launcher3.ShortcutInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070035import com.android.launcher3.Workspace;
Vadim Tryshevfedca432015-08-19 17:55:02 -070036import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyala52ecb02016-12-16 15:04:51 -080037import com.android.launcher3.dragndrop.DragOptions;
38import com.android.launcher3.folder.Folder;
Mario Bertschlerc06af332017-03-28 12:23:22 -070039import com.android.launcher3.popup.PopupContainerWithArrow;
Tony18c4aa42017-05-10 21:23:57 -050040import com.android.launcher3.shortcuts.DeepShortcutManager;
Adam Cohen091440a2015-03-18 14:16:05 -070041import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080042
43import java.util.ArrayList;
44
Sunny Goyal45478022015-06-08 16:52:41 -070045public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080046
Sunny Goyala9116722015-04-29 13:55:58 -070047 private static final String TAG = "LauncherAccessibilityDelegate";
48
Sunny Goyal0236d0b2017-10-24 14:54:30 -070049 public static final int REMOVE = R.id.action_remove;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070050 public static final int UNINSTALL = R.id.action_uninstall;
Winson Chung1054d4e2018-03-05 19:39:21 +000051 public static final int RECONFIGURE = R.id.action_reconfigure;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070052 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
53 protected static final int MOVE = R.id.action_move;
54 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
55 protected static final int RESIZE = R.id.action_resize;
Sunny Goyal66b24572016-09-21 15:57:55 -070056 public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Adam Cohenc9735cf2015-01-23 16:11:55 -080057
Sunny Goyale9b651e2015-04-24 11:44:51 -070058 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080059 ICON,
60 FOLDER,
61 WIDGET
62 }
63
64 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070065 public DragType dragType;
66 public ItemInfo info;
67 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080068 }
69
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070070 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070071 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080072
Sunny Goyale9b651e2015-04-24 11:44:51 -070073 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070074
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075 public LauncherAccessibilityDelegate(Launcher launcher) {
76 mLauncher = launcher;
77
78 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070079 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070081 launcher.getText(R.string.uninstall_drop_target_label)));
Winson Chung1054d4e2018-03-05 19:39:21 +000082 mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE,
83 launcher.getText(R.string.gadget_setup_text)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080084 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
85 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080086 mActions.put(MOVE, new AccessibilityAction(MOVE,
87 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070088 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
89 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -070090 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
91 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070092 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
93 launcher.getText(R.string.action_deep_shortcut)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080094 }
95
96 @Override
97 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
98 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -070099 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700100 }
101
Sunny Goyal66b24572016-09-21 15:57:55 -0700102 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800103 if (!(host.getTag() instanceof ItemInfo)) return;
104 ItemInfo item = (ItemInfo) host.getTag();
105
Sunny Goyal66b24572016-09-21 15:57:55 -0700106 // If the request came from keyboard, do not add custom shortcuts as that is already
107 // exposed as a direct shortcut
Tony18c4aa42017-05-10 21:23:57 -0500108 if (!fromKeyboard && DeepShortcutManager.supportsShortcuts(item)) {
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700109 info.addAction(mActions.get(DEEP_SHORTCUTS));
110 }
111
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700112 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000113 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700114 info.addAction(mActions.get(target.getAccessibilityAction()));
115 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700116 }
117
Sunny Goyal66b24572016-09-21 15:57:55 -0700118 // Do not add move actions for keyboard request as this uses virtual nodes.
119 if (!fromKeyboard && ((item instanceof ShortcutInfo)
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800120 || (item instanceof LauncherAppWidgetInfo)
Sunny Goyal66b24572016-09-21 15:57:55 -0700121 || (item instanceof FolderInfo))) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800122 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700123
124 if (item.container >= 0) {
125 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700126 } else if (item instanceof LauncherAppWidgetInfo) {
127 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
128 info.addAction(mActions.get(RESIZE));
129 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700130 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700131 }
132
133 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800134 info.addAction(mActions.get(ADD_TO_WORKSPACE));
135 }
136 }
137
138 @Override
139 public boolean performAccessibilityAction(View host, int action, Bundle args) {
140 if ((host.getTag() instanceof ItemInfo)
141 && performAction(host, (ItemInfo) host.getTag(), action)) {
142 return true;
143 }
144 return super.performAccessibilityAction(host, action, args);
145 }
146
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700147 public boolean performAction(final View host, final ItemInfo item, int action) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700148 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800149 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800150 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700151 final int[] coordinates = new int[2];
152 final long screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700153 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800154
155 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700156 public void run() {
157 if (item instanceof AppInfo) {
158 ShortcutInfo info = ((AppInfo) item).makeShortcut();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800159 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700160 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700161 screenId, coordinates[0], coordinates[1]);
162
163 ArrayList<ItemInfo> itemList = new ArrayList<>();
164 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700165 mLauncher.bindItems(itemList, true);
Sunny Goyala9116722015-04-29 13:55:58 -0700166 } else if (item instanceof PendingAddItemInfo) {
167 PendingAddItemInfo info = (PendingAddItemInfo) item;
168 Workspace workspace = mLauncher.getWorkspace();
169 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700170 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700171 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800172 }
Sunny Goyala9116722015-04-29 13:55:58 -0700173 announceConfirmation(R.string.item_added_to_workspace);
174 }
175 });
176 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700177 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700178 Folder folder = Folder.getOpen(mLauncher);
179 folder.close(true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700180 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700181 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700182
183 final int[] coordinates = new int[2];
184 final long screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800185 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700186 LauncherSettings.Favorites.CONTAINER_DESKTOP,
187 screenId, coordinates[0], coordinates[1]);
188
189 // Bind the item in next frame so that if a new workspace page was created,
190 // it will get laid out.
191 new Handler().post(new Runnable() {
192
193 @Override
194 public void run() {
195 ArrayList<ItemInfo> itemList = new ArrayList<>();
196 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700197 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700198 announceConfirmation(R.string.item_moved);
199 }
200 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700201 } else if (action == RESIZE) {
202 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
203 final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
204 CharSequence[] labels = new CharSequence[actions.size()];
205 for (int i = 0; i < actions.size(); i++) {
206 labels[i] = mLauncher.getText(actions.get(i));
207 }
208
209 new AlertDialog.Builder(mLauncher)
210 .setTitle(R.string.action_resize)
211 .setItems(labels, new DialogInterface.OnClickListener() {
212
213 @Override
214 public void onClick(DialogInterface dialog, int which) {
215 performResizeAction(actions.get(which), host, info);
216 dialog.dismiss();
217 }
218 })
219 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700220 return true;
221 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800222 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700223 } else {
224 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000225 if (dropTarget.supportsAccessibilityDrop(item, host) &&
226 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700227 dropTarget.onAccessibilityDrop(host, item);
228 return true;
229 }
230 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800231 }
232 return false;
233 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800234
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700235 private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700236 ArrayList<Integer> actions = new ArrayList<>();
237
Sunny Goyald3d8c952015-06-01 10:09:06 -0700238 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
239 if (providerInfo == null) {
240 return actions;
241 }
242
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700243 CellLayout layout = (CellLayout) host.getParent().getParent();
244 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
245 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
246 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
247 actions.add(R.string.action_increase_width);
248 }
249
250 if (info.spanX > info.minSpanX && info.spanX > 1) {
251 actions.add(R.string.action_decrease_width);
252 }
253 }
254
255 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
256 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
257 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
258 actions.add(R.string.action_increase_height);
259 }
260
261 if (info.spanY > info.minSpanY && info.spanY > 1) {
262 actions.add(R.string.action_decrease_height);
263 }
264 }
265 return actions;
266 }
267
Sunny Goyal316490e2015-06-02 09:38:28 -0700268 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700269 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
270 CellLayout layout = (CellLayout) host.getParent().getParent();
271 layout.markCellsAsUnoccupiedForView(host);
272
273 if (action == R.string.action_increase_width) {
274 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
275 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
276 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
277 lp.cellX --;
278 info.cellX --;
279 }
280 lp.cellHSpan ++;
281 info.spanX ++;
282 } else if (action == R.string.action_decrease_width) {
283 lp.cellHSpan --;
284 info.spanX --;
285 } else if (action == R.string.action_increase_height) {
286 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
287 lp.cellY --;
288 info.cellY --;
289 }
290 lp.cellVSpan ++;
291 info.spanY ++;
292 } else if (action == R.string.action_decrease_height) {
293 lp.cellVSpan --;
294 info.spanY --;
295 }
296
297 layout.markCellsAsOccupiedForView(host);
298 Rect sizeRange = new Rect();
299 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
300 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
301 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
302 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800303 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700304 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
305 }
306
Adam Cohen091440a2015-03-18 14:16:05 -0700307 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800308 announceConfirmation(mLauncher.getResources().getString(resId));
309 }
310
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800312 mLauncher.getDragLayer().announceForAccessibility(confirmation);
313
314 }
315
316 public boolean isInAccessibleDrag() {
317 return mDragInfo != null;
318 }
319
320 public DragInfo getDragInfo() {
321 return mDragInfo;
322 }
323
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700324 /**
325 * @param clickedTarget the actual view that was clicked
326 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
327 * as the actual drop location otherwise the views center is used.
328 */
329 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800330 String confirmation) {
331 if (!isInAccessibleDrag()) return;
332
333 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700334 if (dropLocation == null) {
335 loc[0] = clickedTarget.getWidth() / 2;
336 loc[1] = clickedTarget.getHeight() / 2;
337 } else {
338 loc[0] = dropLocation.centerX();
339 loc[1] = dropLocation.centerY();
340 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800341
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700342 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800343 mLauncher.getDragController().completeAccessibleDrag(loc);
344
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700345 if (!TextUtils.isEmpty(confirmation)) {
346 announceConfirmation(confirmation);
347 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800348 }
349
350 public void beginAccessibleDrag(View item, ItemInfo info) {
351 mDragInfo = new DragInfo();
352 mDragInfo.info = info;
353 mDragInfo.item = item;
354 mDragInfo.dragType = DragType.ICON;
355 if (info instanceof FolderInfo) {
356 mDragInfo.dragType = DragType.FOLDER;
357 } else if (info instanceof LauncherAppWidgetInfo) {
358 mDragInfo.dragType = DragType.WIDGET;
359 }
360
361 CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info);
362
363 Rect pos = new Rect();
364 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800365 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyale9b651e2015-04-24 11:44:51 -0700366
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700367 Folder folder = Folder.getOpen(mLauncher);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700368 if (folder != null) {
Jonathan Miranda8701cd52017-07-13 19:26:46 +0000369 if (!folder.getItemsInReadingOrder().contains(item)) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700370 folder.close(true);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700371 folder = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700372 }
373 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800374
Sunny Goyal94b510c2016-08-16 15:36:48 -0700375 mLauncher.getDragController().addDragListener(this);
376
377 DragOptions options = new DragOptions();
378 options.isAccessibleDrag = true;
379 if (folder != null) {
380 folder.startDrag(cellInfo.cell, options);
381 } else {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700382 mLauncher.getWorkspace().startDrag(cellInfo, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800383 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800384 }
385
Sunny Goyal45478022015-06-08 16:52:41 -0700386 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700387 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700388 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800389 }
390
Sunny Goyal45478022015-06-08 16:52:41 -0700391 @Override
392 public void onDragEnd() {
393 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800394 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800395 }
Sunny Goyala9116722015-04-29 13:55:58 -0700396
397 /**
398 * Find empty space on the workspace and returns the screenId.
399 */
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700400 protected long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700401 Workspace workspace = mLauncher.getWorkspace();
402 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
403 long screenId;
404
405 // First check if there is space on the current screen.
406 int screenIndex = workspace.getCurrentPage();
407 screenId = workspaceScreens.get(screenIndex);
408 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
409
410 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700411 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700412 while (!found && screenIndex < workspaceScreens.size()) {
413 screenId = workspaceScreens.get(screenIndex);
414 layout = (CellLayout) workspace.getPageAt(screenIndex);
415 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
416 screenIndex++;
417 }
418
419 if (found) {
420 return screenId;
421 }
422
423 workspace.addExtraEmptyScreen();
424 screenId = workspace.commitExtraEmptyScreen();
425 layout = workspace.getScreenWithId(screenId);
426 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
427
428 if (!found) {
429 Log.wtf(TAG, "Not enough space on an empty screen");
430 }
431 return screenId;
432 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800433}