blob: 173aad044ed26648d5324f23a7991c97da514276 [file] [log] [blame]
Sunny Goyal83a8f042015-05-19 12:52:12 -07001package com.android.launcher3.accessibility;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002
3import android.annotation.TargetApi;
Sunny Goyal9ca9c132015-04-29 14:57:22 -07004import android.app.AlertDialog;
5import android.appwidget.AppWidgetProviderInfo;
6import android.content.DialogInterface;
Adam Cohenc9735cf2015-01-23 16:11:55 -08007import android.graphics.Rect;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08008import android.os.Build;
9import 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 Goyal83a8f042015-05-19 12:52:12 -070022import com.android.launcher3.CellLayout;
23import com.android.launcher3.DeleteDropTarget;
Sunny Goyal45478022015-06-08 16:52:41 -070024import com.android.launcher3.DragSource;
Sunny Goyal94b510c2016-08-16 15:36:48 -070025import com.android.launcher3.DropTarget.DragObject;
26import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyal26119432016-02-18 22:09:23 +000027import com.android.launcher3.folder.Folder;
Sunny Goyal83a8f042015-05-19 12:52:12 -070028import com.android.launcher3.FolderInfo;
29import com.android.launcher3.InfoDropTarget;
30import com.android.launcher3.ItemInfo;
31import com.android.launcher3.Launcher;
32import com.android.launcher3.LauncherAppWidgetHostView;
33import com.android.launcher3.LauncherAppWidgetInfo;
34import com.android.launcher3.LauncherModel;
35import com.android.launcher3.LauncherSettings;
36import com.android.launcher3.PendingAddItemInfo;
37import com.android.launcher3.R;
38import com.android.launcher3.ShortcutInfo;
39import com.android.launcher3.UninstallDropTarget;
40import com.android.launcher3.Workspace;
Vadim Tryshevfedca432015-08-19 17:55:02 -070041import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070042import com.android.launcher3.shortcuts.DeepShortcutTextView;
43import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Adam Cohen091440a2015-03-18 14:16:05 -070044import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080045
46import java.util.ArrayList;
47
48@TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal45478022015-06-08 16:52:41 -070049public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080050
Sunny Goyala9116722015-04-29 13:55:58 -070051 private static final String TAG = "LauncherAccessibilityDelegate";
52
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070053 protected static final int REMOVE = R.id.action_remove;
54 protected static final int INFO = R.id.action_info;
55 protected static final int UNINSTALL = R.id.action_uninstall;
56 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
57 protected static final int MOVE = R.id.action_move;
58 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
59 protected static final int RESIZE = R.id.action_resize;
60 protected static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Adam Cohenc9735cf2015-01-23 16:11:55 -080061
Sunny Goyale9b651e2015-04-24 11:44:51 -070062 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080063 ICON,
64 FOLDER,
65 WIDGET
66 }
67
68 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070069 public DragType dragType;
70 public ItemInfo info;
71 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080072 }
73
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070074 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070075 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080076
Sunny Goyale9b651e2015-04-24 11:44:51 -070077 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070078
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079 public LauncherAccessibilityDelegate(Launcher launcher) {
80 mLauncher = launcher;
81
82 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070083 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080084 mActions.put(INFO, new AccessibilityAction(INFO,
Tony Wickham9aae47f2015-10-01 13:04:22 -070085 launcher.getText(R.string.app_info_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080086 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070087 launcher.getText(R.string.uninstall_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080088 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
89 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080090 mActions.put(MOVE, new AccessibilityAction(MOVE,
91 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070092 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
93 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -070094 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
95 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070096 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
97 launcher.getText(R.string.action_deep_shortcut)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080098 }
99
100 @Override
101 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
102 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700103 addActions(host, info);
104 }
105
106 protected void addActions(View host, AccessibilityNodeInfo info) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800107 if (!(host.getTag() instanceof ItemInfo)) return;
108 ItemInfo item = (ItemInfo) host.getTag();
109
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700110 if (host instanceof BubbleTextView && ((BubbleTextView) host).hasDeepShortcuts()) {
111 info.addAction(mActions.get(DEEP_SHORTCUTS));
112 }
113
Tony Wickham9aae47f2015-10-01 13:04:22 -0700114 if (DeleteDropTarget.supportsAccessibleDrop(item)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700115 info.addAction(mActions.get(REMOVE));
116 }
117 if (UninstallDropTarget.supportsDrop(host.getContext(), item)) {
118 info.addAction(mActions.get(UNINSTALL));
119 }
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800120 if (InfoDropTarget.supportsDrop(item)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700121 info.addAction(mActions.get(INFO));
122 }
123
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800124 if ((item instanceof ShortcutInfo)
125 || (item instanceof LauncherAppWidgetInfo)
126 || (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 Goyal71b5c0b2015-01-08 16:59:04 -0800153 if (action == REMOVE) {
Sunny Goyale78e3d72015-09-24 11:23:31 -0700154 DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host);
155 return true;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800156 } else if (action == INFO) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800157 InfoDropTarget.startDetailsActivityForInfo(item, mLauncher, null);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800158 return true;
159 } else if (action == UNINSTALL) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700160 return UninstallDropTarget.startUninstallActivity(mLauncher, item);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800161 } else if (action == MOVE) {
162 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800163 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700164 final int[] coordinates = new int[2];
165 final long screenId = findSpaceOnWorkspace(item, coordinates);
166 mLauncher.showWorkspace(true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800167
168 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700169 public void run() {
170 if (item instanceof AppInfo) {
171 ShortcutInfo info = ((AppInfo) item).makeShortcut();
172 LauncherModel.addItemToDatabase(mLauncher, info,
173 LauncherSettings.Favorites.CONTAINER_DESKTOP,
174 screenId, coordinates[0], coordinates[1]);
175
176 ArrayList<ItemInfo> itemList = new ArrayList<>();
177 itemList.add(info);
178 mLauncher.bindItems(itemList, 0, itemList.size(), true);
179 } else if (item instanceof PendingAddItemInfo) {
180 PendingAddItemInfo info = (PendingAddItemInfo) item;
181 Workspace workspace = mLauncher.getWorkspace();
182 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
183 mLauncher.addPendingItem(info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
184 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800185 }
Sunny Goyala9116722015-04-29 13:55:58 -0700186 announceConfirmation(R.string.item_added_to_workspace);
187 }
188 });
189 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700190 } else if (action == MOVE_TO_WORKSPACE) {
191 Folder folder = mLauncher.getWorkspace().getOpenFolder();
Sunny Goyal935fca12015-10-13 10:19:01 -0700192 mLauncher.closeFolder(folder, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700193 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700194 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700195
196 final int[] coordinates = new int[2];
197 final long screenId = findSpaceOnWorkspace(item, coordinates);
198 LauncherModel.moveItemInDatabase(mLauncher, info,
199 LauncherSettings.Favorites.CONTAINER_DESKTOP,
200 screenId, coordinates[0], coordinates[1]);
201
202 // Bind the item in next frame so that if a new workspace page was created,
203 // it will get laid out.
204 new Handler().post(new Runnable() {
205
206 @Override
207 public void run() {
208 ArrayList<ItemInfo> itemList = new ArrayList<>();
209 itemList.add(item);
210 mLauncher.bindItems(itemList, 0, itemList.size(), true);
211 announceConfirmation(R.string.item_moved);
212 }
213 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700214 } else if (action == RESIZE) {
215 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
216 final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
217 CharSequence[] labels = new CharSequence[actions.size()];
218 for (int i = 0; i < actions.size(); i++) {
219 labels[i] = mLauncher.getText(actions.get(i));
220 }
221
222 new AlertDialog.Builder(mLauncher)
223 .setTitle(R.string.action_resize)
224 .setItems(labels, new DialogInterface.OnClickListener() {
225
226 @Override
227 public void onClick(DialogInterface dialog, int which) {
228 performResizeAction(actions.get(which), host, info);
229 dialog.dismiss();
230 }
231 })
232 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700233 return true;
234 } else if (action == DEEP_SHORTCUTS) {
235 return DeepShortcutsContainer.showForIcon((BubbleTextView) host) != null;
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();
308 LauncherModel.updateItemInDatabase(mLauncher, info);
309 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
366 CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info);
367
368 Rect pos = new Rect();
369 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800370 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyale9b651e2015-04-24 11:44:51 -0700371
372 Workspace workspace = mLauncher.getWorkspace();
373
374 Folder folder = workspace.getOpenFolder();
375 if (folder != null) {
Sunny Goyal94b510c2016-08-16 15:36:48 -0700376 if (!folder.getItemsInReadingOrder().contains(item)) {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700377 mLauncher.closeFolder();
Sunny Goyal94b510c2016-08-16 15:36:48 -0700378 folder = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700379 }
380 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800381
Sunny Goyal94b510c2016-08-16 15:36:48 -0700382 mLauncher.getDragController().addDragListener(this);
383
384 DragOptions options = new DragOptions();
385 options.isAccessibleDrag = true;
386 if (folder != null) {
387 folder.startDrag(cellInfo.cell, options);
388 } else {
389 workspace.startDrag(cellInfo, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800390 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800391 }
392
Sunny Goyal45478022015-06-08 16:52:41 -0700393 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700394 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700395 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800396 }
397
Sunny Goyal45478022015-06-08 16:52:41 -0700398 @Override
399 public void onDragEnd() {
400 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800401 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800402 }
Sunny Goyala9116722015-04-29 13:55:58 -0700403
404 /**
405 * Find empty space on the workspace and returns the screenId.
406 */
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700407 protected long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700408 Workspace workspace = mLauncher.getWorkspace();
409 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
410 long screenId;
411
412 // First check if there is space on the current screen.
413 int screenIndex = workspace.getCurrentPage();
414 screenId = workspaceScreens.get(screenIndex);
415 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
416
417 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
418 screenIndex = workspace.hasCustomContent() ? 1 : 0;
419 while (!found && screenIndex < workspaceScreens.size()) {
420 screenId = workspaceScreens.get(screenIndex);
421 layout = (CellLayout) workspace.getPageAt(screenIndex);
422 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
423 screenIndex++;
424 }
425
426 if (found) {
427 return screenId;
428 }
429
430 workspace.addExtraEmptyScreen();
431 screenId = workspace.commitExtraEmptyScreen();
432 layout = workspace.getScreenWithId(screenId);
433 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
434
435 if (!found) {
436 Log.wtf(TAG, "Not enough space on an empty screen");
437 }
438 return screenId;
439 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800440}