blob: 0562cf54b97d551b3c25bea072dd6345624099f1 [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 Goyal26119432016-02-18 22:09:23 +000025import com.android.launcher3.folder.Folder;
Sunny Goyal83a8f042015-05-19 12:52:12 -070026import com.android.launcher3.FolderInfo;
27import com.android.launcher3.InfoDropTarget;
28import com.android.launcher3.ItemInfo;
29import com.android.launcher3.Launcher;
30import com.android.launcher3.LauncherAppWidgetHostView;
31import com.android.launcher3.LauncherAppWidgetInfo;
32import com.android.launcher3.LauncherModel;
33import com.android.launcher3.LauncherSettings;
34import com.android.launcher3.PendingAddItemInfo;
35import com.android.launcher3.R;
36import com.android.launcher3.ShortcutInfo;
37import com.android.launcher3.UninstallDropTarget;
38import com.android.launcher3.Workspace;
Vadim Tryshevfedca432015-08-19 17:55:02 -070039import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070040import com.android.launcher3.shortcuts.DeepShortcutTextView;
41import com.android.launcher3.shortcuts.DeepShortcutsContainer;
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
46@TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal45478022015-06-08 16:52:41 -070047public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048
Sunny Goyala9116722015-04-29 13:55:58 -070049 private static final String TAG = "LauncherAccessibilityDelegate";
50
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070051 protected static final int REMOVE = R.id.action_remove;
52 protected static final int INFO = R.id.action_info;
53 protected static final int UNINSTALL = R.id.action_uninstall;
54 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
55 protected static final int MOVE = R.id.action_move;
56 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
57 protected static final int RESIZE = R.id.action_resize;
58 protected static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Adam Cohenc9735cf2015-01-23 16:11:55 -080059
Sunny Goyale9b651e2015-04-24 11:44:51 -070060 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080061 ICON,
62 FOLDER,
63 WIDGET
64 }
65
66 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070067 public DragType dragType;
68 public ItemInfo info;
69 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080070 }
71
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070072 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070073 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074
Sunny Goyale9b651e2015-04-24 11:44:51 -070075 private DragInfo mDragInfo = null;
76 private AccessibilityDragSource mDragSource = null;
77
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078 public LauncherAccessibilityDelegate(Launcher launcher) {
79 mLauncher = launcher;
80
81 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070082 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080083 mActions.put(INFO, new AccessibilityAction(INFO,
Tony Wickham9aae47f2015-10-01 13:04:22 -070084 launcher.getText(R.string.app_info_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080085 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070086 launcher.getText(R.string.uninstall_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080087 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
88 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080089 mActions.put(MOVE, new AccessibilityAction(MOVE,
90 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070091 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
92 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -070093 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
94 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070095 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
96 launcher.getText(R.string.action_deep_shortcut)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080097 }
98
99 @Override
100 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
101 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700102 addActions(host, info);
103 }
104
105 protected void addActions(View host, AccessibilityNodeInfo info) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800106 if (!(host.getTag() instanceof ItemInfo)) return;
107 ItemInfo item = (ItemInfo) host.getTag();
108
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700109 if (host instanceof BubbleTextView && ((BubbleTextView) host).hasDeepShortcuts()) {
110 info.addAction(mActions.get(DEEP_SHORTCUTS));
111 }
112
Tony Wickham9aae47f2015-10-01 13:04:22 -0700113 if (DeleteDropTarget.supportsAccessibleDrop(item)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700114 info.addAction(mActions.get(REMOVE));
115 }
116 if (UninstallDropTarget.supportsDrop(host.getContext(), item)) {
117 info.addAction(mActions.get(UNINSTALL));
118 }
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800119 if (InfoDropTarget.supportsDrop(item)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700120 info.addAction(mActions.get(INFO));
121 }
122
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800123 if ((item instanceof ShortcutInfo)
124 || (item instanceof LauncherAppWidgetInfo)
125 || (item instanceof FolderInfo)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800126 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700127
128 if (item.container >= 0) {
129 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700130 } else if (item instanceof LauncherAppWidgetInfo) {
131 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
132 info.addAction(mActions.get(RESIZE));
133 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700134 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700135 }
136
137 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800138 info.addAction(mActions.get(ADD_TO_WORKSPACE));
139 }
140 }
141
142 @Override
143 public boolean performAccessibilityAction(View host, int action, Bundle args) {
144 if ((host.getTag() instanceof ItemInfo)
145 && performAction(host, (ItemInfo) host.getTag(), action)) {
146 return true;
147 }
148 return super.performAccessibilityAction(host, action, args);
149 }
150
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700151 public boolean performAction(final View host, final ItemInfo item, int action) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800152 if (action == REMOVE) {
Sunny Goyale78e3d72015-09-24 11:23:31 -0700153 DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host);
154 return true;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155 } else if (action == INFO) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800156 InfoDropTarget.startDetailsActivityForInfo(item, mLauncher, null);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800157 return true;
158 } else if (action == UNINSTALL) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700159 return UninstallDropTarget.startUninstallActivity(mLauncher, item);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800160 } else if (action == MOVE) {
161 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800162 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700163 final int[] coordinates = new int[2];
164 final long screenId = findSpaceOnWorkspace(item, coordinates);
165 mLauncher.showWorkspace(true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800166
167 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700168 public void run() {
169 if (item instanceof AppInfo) {
170 ShortcutInfo info = ((AppInfo) item).makeShortcut();
171 LauncherModel.addItemToDatabase(mLauncher, info,
172 LauncherSettings.Favorites.CONTAINER_DESKTOP,
173 screenId, coordinates[0], coordinates[1]);
174
175 ArrayList<ItemInfo> itemList = new ArrayList<>();
176 itemList.add(info);
177 mLauncher.bindItems(itemList, 0, itemList.size(), true);
178 } else if (item instanceof PendingAddItemInfo) {
179 PendingAddItemInfo info = (PendingAddItemInfo) item;
180 Workspace workspace = mLauncher.getWorkspace();
181 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
182 mLauncher.addPendingItem(info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
183 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800184 }
Sunny Goyala9116722015-04-29 13:55:58 -0700185 announceConfirmation(R.string.item_added_to_workspace);
186 }
187 });
188 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700189 } else if (action == MOVE_TO_WORKSPACE) {
190 Folder folder = mLauncher.getWorkspace().getOpenFolder();
Sunny Goyal935fca12015-10-13 10:19:01 -0700191 mLauncher.closeFolder(folder, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700192 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700193 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700194
195 final int[] coordinates = new int[2];
196 final long screenId = findSpaceOnWorkspace(item, coordinates);
197 LauncherModel.moveItemInDatabase(mLauncher, info,
198 LauncherSettings.Favorites.CONTAINER_DESKTOP,
199 screenId, coordinates[0], coordinates[1]);
200
201 // Bind the item in next frame so that if a new workspace page was created,
202 // it will get laid out.
203 new Handler().post(new Runnable() {
204
205 @Override
206 public void run() {
207 ArrayList<ItemInfo> itemList = new ArrayList<>();
208 itemList.add(item);
209 mLauncher.bindItems(itemList, 0, itemList.size(), true);
210 announceConfirmation(R.string.item_moved);
211 }
212 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700213 } else if (action == RESIZE) {
214 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
215 final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
216 CharSequence[] labels = new CharSequence[actions.size()];
217 for (int i = 0; i < actions.size(); i++) {
218 labels[i] = mLauncher.getText(actions.get(i));
219 }
220
221 new AlertDialog.Builder(mLauncher)
222 .setTitle(R.string.action_resize)
223 .setItems(labels, new DialogInterface.OnClickListener() {
224
225 @Override
226 public void onClick(DialogInterface dialog, int which) {
227 performResizeAction(actions.get(which), host, info);
228 dialog.dismiss();
229 }
230 })
231 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700232 return true;
233 } else if (action == DEEP_SHORTCUTS) {
234 return DeepShortcutsContainer.showForIcon((BubbleTextView) host) != null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800235 }
236 return false;
237 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800238
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700239 private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700240 ArrayList<Integer> actions = new ArrayList<>();
241
Sunny Goyald3d8c952015-06-01 10:09:06 -0700242 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
243 if (providerInfo == null) {
244 return actions;
245 }
246
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700247 CellLayout layout = (CellLayout) host.getParent().getParent();
248 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
249 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
250 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
251 actions.add(R.string.action_increase_width);
252 }
253
254 if (info.spanX > info.minSpanX && info.spanX > 1) {
255 actions.add(R.string.action_decrease_width);
256 }
257 }
258
259 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
260 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
261 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
262 actions.add(R.string.action_increase_height);
263 }
264
265 if (info.spanY > info.minSpanY && info.spanY > 1) {
266 actions.add(R.string.action_decrease_height);
267 }
268 }
269 return actions;
270 }
271
Sunny Goyal316490e2015-06-02 09:38:28 -0700272 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700273 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
274 CellLayout layout = (CellLayout) host.getParent().getParent();
275 layout.markCellsAsUnoccupiedForView(host);
276
277 if (action == R.string.action_increase_width) {
278 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
279 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
280 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
281 lp.cellX --;
282 info.cellX --;
283 }
284 lp.cellHSpan ++;
285 info.spanX ++;
286 } else if (action == R.string.action_decrease_width) {
287 lp.cellHSpan --;
288 info.spanX --;
289 } else if (action == R.string.action_increase_height) {
290 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
291 lp.cellY --;
292 info.cellY --;
293 }
294 lp.cellVSpan ++;
295 info.spanY ++;
296 } else if (action == R.string.action_decrease_height) {
297 lp.cellVSpan --;
298 info.spanY --;
299 }
300
301 layout.markCellsAsOccupiedForView(host);
302 Rect sizeRange = new Rect();
303 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
304 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
305 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
306 host.requestLayout();
307 LauncherModel.updateItemInDatabase(mLauncher, info);
308 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
309 }
310
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800312 announceConfirmation(mLauncher.getResources().getString(resId));
313 }
314
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800316 mLauncher.getDragLayer().announceForAccessibility(confirmation);
317
318 }
319
320 public boolean isInAccessibleDrag() {
321 return mDragInfo != null;
322 }
323
324 public DragInfo getDragInfo() {
325 return mDragInfo;
326 }
327
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700328 /**
329 * @param clickedTarget the actual view that was clicked
330 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
331 * as the actual drop location otherwise the views center is used.
332 */
333 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800334 String confirmation) {
335 if (!isInAccessibleDrag()) return;
336
337 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700338 if (dropLocation == null) {
339 loc[0] = clickedTarget.getWidth() / 2;
340 loc[1] = clickedTarget.getHeight() / 2;
341 } else {
342 loc[0] = dropLocation.centerX();
343 loc[1] = dropLocation.centerY();
344 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800345
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700346 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800347 mLauncher.getDragController().completeAccessibleDrag(loc);
348
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700349 if (!TextUtils.isEmpty(confirmation)) {
350 announceConfirmation(confirmation);
351 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800352 }
353
354 public void beginAccessibleDrag(View item, ItemInfo info) {
355 mDragInfo = new DragInfo();
356 mDragInfo.info = info;
357 mDragInfo.item = item;
358 mDragInfo.dragType = DragType.ICON;
359 if (info instanceof FolderInfo) {
360 mDragInfo.dragType = DragType.FOLDER;
361 } else if (info instanceof LauncherAppWidgetInfo) {
362 mDragInfo.dragType = DragType.WIDGET;
363 }
364
365 CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info);
366
367 Rect pos = new Rect();
368 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800369 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyale9b651e2015-04-24 11:44:51 -0700370
371 Workspace workspace = mLauncher.getWorkspace();
372
373 Folder folder = workspace.getOpenFolder();
374 if (folder != null) {
375 if (folder.getItemsInReadingOrder().contains(item)) {
376 mDragSource = folder;
377 } else {
378 mLauncher.closeFolder();
379 }
380 }
381 if (mDragSource == null) {
382 mDragSource = workspace;
383 }
384 mDragSource.enableAccessibleDrag(true);
385 mDragSource.startDrag(cellInfo, true);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800386
Sunny Goyal45478022015-06-08 16:52:41 -0700387 if (mLauncher.getDragController().isDragging()) {
388 mLauncher.getDragController().addDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800389 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800390 }
391
Sunny Goyal45478022015-06-08 16:52:41 -0700392
393 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700394 public void onDragStart(DragSource source, ItemInfo info, int dragAction) {
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;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700402 if (mDragSource != null) {
403 mDragSource.enableAccessibleDrag(false);
404 mDragSource = null;
405 }
406 }
407
408 public static interface AccessibilityDragSource {
409 void startDrag(CellLayout.CellInfo cellInfo, boolean accessible);
410
411 void enableAccessibleDrag(boolean enable);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800412 }
Sunny Goyala9116722015-04-29 13:55:58 -0700413
414 /**
415 * Find empty space on the workspace and returns the screenId.
416 */
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700417 protected long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700418 Workspace workspace = mLauncher.getWorkspace();
419 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
420 long screenId;
421
422 // First check if there is space on the current screen.
423 int screenIndex = workspace.getCurrentPage();
424 screenId = workspaceScreens.get(screenIndex);
425 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
426
427 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
428 screenIndex = workspace.hasCustomContent() ? 1 : 0;
429 while (!found && screenIndex < workspaceScreens.size()) {
430 screenId = workspaceScreens.get(screenIndex);
431 layout = (CellLayout) workspace.getPageAt(screenIndex);
432 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
433 screenIndex++;
434 }
435
436 if (found) {
437 return screenId;
438 }
439
440 workspace.addExtraEmptyScreen();
441 screenId = workspace.commitExtraEmptyScreen();
442 layout = workspace.getScreenWithId(screenId);
443 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
444
445 if (!found) {
446 Log.wtf(TAG, "Not enough space on an empty screen");
447 }
448 return screenId;
449 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450}