blob: 439e3145e89145ab3bcdebf944015bf84fb8afed [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;
Sunny Goyal66b24572016-09-21 15:57:55 -070060 public 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 Goyal66b24572016-09-21 15:57:55 -0700103 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700104 }
105
Sunny Goyal66b24572016-09-21 15:57:55 -0700106 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800107 if (!(host.getTag() instanceof ItemInfo)) return;
108 ItemInfo item = (ItemInfo) host.getTag();
109
Sunny Goyal66b24572016-09-21 15:57:55 -0700110 // If the request came from keyboard, do not add custom shortcuts as that is already
111 // exposed as a direct shortcut
112 if (!fromKeyboard && host instanceof BubbleTextView
113 && ((BubbleTextView) host).hasDeepShortcuts()) {
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700114 info.addAction(mActions.get(DEEP_SHORTCUTS));
115 }
116
Tony Wickham9aae47f2015-10-01 13:04:22 -0700117 if (DeleteDropTarget.supportsAccessibleDrop(item)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700118 info.addAction(mActions.get(REMOVE));
119 }
120 if (UninstallDropTarget.supportsDrop(host.getContext(), item)) {
121 info.addAction(mActions.get(UNINSTALL));
122 }
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800123 if (InfoDropTarget.supportsDrop(item)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700124 info.addAction(mActions.get(INFO));
125 }
126
Sunny Goyal66b24572016-09-21 15:57:55 -0700127 // Do not add move actions for keyboard request as this uses virtual nodes.
128 if (!fromKeyboard && ((item instanceof ShortcutInfo)
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800129 || (item instanceof LauncherAppWidgetInfo)
Sunny Goyal66b24572016-09-21 15:57:55 -0700130 || (item instanceof FolderInfo))) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800131 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700132
133 if (item.container >= 0) {
134 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700135 } else if (item instanceof LauncherAppWidgetInfo) {
136 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
137 info.addAction(mActions.get(RESIZE));
138 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700139 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700140 }
141
142 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800143 info.addAction(mActions.get(ADD_TO_WORKSPACE));
144 }
145 }
146
147 @Override
148 public boolean performAccessibilityAction(View host, int action, Bundle args) {
149 if ((host.getTag() instanceof ItemInfo)
150 && performAction(host, (ItemInfo) host.getTag(), action)) {
151 return true;
152 }
153 return super.performAccessibilityAction(host, action, args);
154 }
155
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700156 public boolean performAction(final View host, final ItemInfo item, int action) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800157 if (action == REMOVE) {
Sunny Goyale78e3d72015-09-24 11:23:31 -0700158 DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host);
159 return true;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800160 } else if (action == INFO) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800161 InfoDropTarget.startDetailsActivityForInfo(item, mLauncher, null);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800162 return true;
163 } else if (action == UNINSTALL) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700164 return UninstallDropTarget.startUninstallActivity(mLauncher, item);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800165 } else if (action == MOVE) {
166 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800167 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700168 final int[] coordinates = new int[2];
169 final long screenId = findSpaceOnWorkspace(item, coordinates);
170 mLauncher.showWorkspace(true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800171
172 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700173 public void run() {
174 if (item instanceof AppInfo) {
175 ShortcutInfo info = ((AppInfo) item).makeShortcut();
176 LauncherModel.addItemToDatabase(mLauncher, info,
177 LauncherSettings.Favorites.CONTAINER_DESKTOP,
178 screenId, coordinates[0], coordinates[1]);
179
180 ArrayList<ItemInfo> itemList = new ArrayList<>();
181 itemList.add(info);
182 mLauncher.bindItems(itemList, 0, itemList.size(), true);
183 } else if (item instanceof PendingAddItemInfo) {
184 PendingAddItemInfo info = (PendingAddItemInfo) item;
185 Workspace workspace = mLauncher.getWorkspace();
186 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
187 mLauncher.addPendingItem(info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
188 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800189 }
Sunny Goyala9116722015-04-29 13:55:58 -0700190 announceConfirmation(R.string.item_added_to_workspace);
191 }
192 });
193 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700194 } else if (action == MOVE_TO_WORKSPACE) {
195 Folder folder = mLauncher.getWorkspace().getOpenFolder();
Sunny Goyal935fca12015-10-13 10:19:01 -0700196 mLauncher.closeFolder(folder, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700197 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700198 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700199
200 final int[] coordinates = new int[2];
201 final long screenId = findSpaceOnWorkspace(item, coordinates);
202 LauncherModel.moveItemInDatabase(mLauncher, info,
203 LauncherSettings.Favorites.CONTAINER_DESKTOP,
204 screenId, coordinates[0], coordinates[1]);
205
206 // Bind the item in next frame so that if a new workspace page was created,
207 // it will get laid out.
208 new Handler().post(new Runnable() {
209
210 @Override
211 public void run() {
212 ArrayList<ItemInfo> itemList = new ArrayList<>();
213 itemList.add(item);
214 mLauncher.bindItems(itemList, 0, itemList.size(), true);
215 announceConfirmation(R.string.item_moved);
216 }
217 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700218 } else if (action == RESIZE) {
219 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
220 final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
221 CharSequence[] labels = new CharSequence[actions.size()];
222 for (int i = 0; i < actions.size(); i++) {
223 labels[i] = mLauncher.getText(actions.get(i));
224 }
225
226 new AlertDialog.Builder(mLauncher)
227 .setTitle(R.string.action_resize)
228 .setItems(labels, new DialogInterface.OnClickListener() {
229
230 @Override
231 public void onClick(DialogInterface dialog, int which) {
232 performResizeAction(actions.get(which), host, info);
233 dialog.dismiss();
234 }
235 })
236 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700237 return true;
238 } else if (action == DEEP_SHORTCUTS) {
239 return DeepShortcutsContainer.showForIcon((BubbleTextView) host) != null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800240 }
241 return false;
242 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800243
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700244 private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700245 ArrayList<Integer> actions = new ArrayList<>();
246
Sunny Goyald3d8c952015-06-01 10:09:06 -0700247 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
248 if (providerInfo == null) {
249 return actions;
250 }
251
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700252 CellLayout layout = (CellLayout) host.getParent().getParent();
253 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
254 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
255 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
256 actions.add(R.string.action_increase_width);
257 }
258
259 if (info.spanX > info.minSpanX && info.spanX > 1) {
260 actions.add(R.string.action_decrease_width);
261 }
262 }
263
264 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
265 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
266 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
267 actions.add(R.string.action_increase_height);
268 }
269
270 if (info.spanY > info.minSpanY && info.spanY > 1) {
271 actions.add(R.string.action_decrease_height);
272 }
273 }
274 return actions;
275 }
276
Sunny Goyal316490e2015-06-02 09:38:28 -0700277 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700278 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
279 CellLayout layout = (CellLayout) host.getParent().getParent();
280 layout.markCellsAsUnoccupiedForView(host);
281
282 if (action == R.string.action_increase_width) {
283 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
284 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
285 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
286 lp.cellX --;
287 info.cellX --;
288 }
289 lp.cellHSpan ++;
290 info.spanX ++;
291 } else if (action == R.string.action_decrease_width) {
292 lp.cellHSpan --;
293 info.spanX --;
294 } else if (action == R.string.action_increase_height) {
295 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
296 lp.cellY --;
297 info.cellY --;
298 }
299 lp.cellVSpan ++;
300 info.spanY ++;
301 } else if (action == R.string.action_decrease_height) {
302 lp.cellVSpan --;
303 info.spanY --;
304 }
305
306 layout.markCellsAsOccupiedForView(host);
307 Rect sizeRange = new Rect();
308 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
309 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
310 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
311 host.requestLayout();
312 LauncherModel.updateItemInDatabase(mLauncher, info);
313 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
314 }
315
Adam Cohen091440a2015-03-18 14:16:05 -0700316 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800317 announceConfirmation(mLauncher.getResources().getString(resId));
318 }
319
Adam Cohen091440a2015-03-18 14:16:05 -0700320 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800321 mLauncher.getDragLayer().announceForAccessibility(confirmation);
322
323 }
324
325 public boolean isInAccessibleDrag() {
326 return mDragInfo != null;
327 }
328
329 public DragInfo getDragInfo() {
330 return mDragInfo;
331 }
332
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700333 /**
334 * @param clickedTarget the actual view that was clicked
335 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
336 * as the actual drop location otherwise the views center is used.
337 */
338 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800339 String confirmation) {
340 if (!isInAccessibleDrag()) return;
341
342 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700343 if (dropLocation == null) {
344 loc[0] = clickedTarget.getWidth() / 2;
345 loc[1] = clickedTarget.getHeight() / 2;
346 } else {
347 loc[0] = dropLocation.centerX();
348 loc[1] = dropLocation.centerY();
349 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800350
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700351 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800352 mLauncher.getDragController().completeAccessibleDrag(loc);
353
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700354 if (!TextUtils.isEmpty(confirmation)) {
355 announceConfirmation(confirmation);
356 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800357 }
358
359 public void beginAccessibleDrag(View item, ItemInfo info) {
360 mDragInfo = new DragInfo();
361 mDragInfo.info = info;
362 mDragInfo.item = item;
363 mDragInfo.dragType = DragType.ICON;
364 if (info instanceof FolderInfo) {
365 mDragInfo.dragType = DragType.FOLDER;
366 } else if (info instanceof LauncherAppWidgetInfo) {
367 mDragInfo.dragType = DragType.WIDGET;
368 }
369
370 CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info);
371
372 Rect pos = new Rect();
373 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800374 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyale9b651e2015-04-24 11:44:51 -0700375
376 Workspace workspace = mLauncher.getWorkspace();
377
378 Folder folder = workspace.getOpenFolder();
379 if (folder != null) {
Sunny Goyal94b510c2016-08-16 15:36:48 -0700380 if (!folder.getItemsInReadingOrder().contains(item)) {
Sunny Goyale9b651e2015-04-24 11:44:51 -0700381 mLauncher.closeFolder();
Sunny Goyal94b510c2016-08-16 15:36:48 -0700382 folder = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700383 }
384 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800385
Sunny Goyal94b510c2016-08-16 15:36:48 -0700386 mLauncher.getDragController().addDragListener(this);
387
388 DragOptions options = new DragOptions();
389 options.isAccessibleDrag = true;
390 if (folder != null) {
391 folder.startDrag(cellInfo.cell, options);
392 } else {
393 workspace.startDrag(cellInfo, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800394 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800395 }
396
Sunny Goyal45478022015-06-08 16:52:41 -0700397 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700398 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700399 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800400 }
401
Sunny Goyal45478022015-06-08 16:52:41 -0700402 @Override
403 public void onDragEnd() {
404 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800405 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800406 }
Sunny Goyala9116722015-04-29 13:55:58 -0700407
408 /**
409 * Find empty space on the workspace and returns the screenId.
410 */
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700411 protected long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700412 Workspace workspace = mLauncher.getWorkspace();
413 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
414 long screenId;
415
416 // First check if there is space on the current screen.
417 int screenIndex = workspace.getCurrentPage();
418 screenId = workspaceScreens.get(screenIndex);
419 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
420
421 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
422 screenIndex = workspace.hasCustomContent() ? 1 : 0;
423 while (!found && screenIndex < workspaceScreens.size()) {
424 screenId = workspaceScreens.get(screenIndex);
425 layout = (CellLayout) workspace.getPageAt(screenIndex);
426 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
427 screenIndex++;
428 }
429
430 if (found) {
431 return screenId;
432 }
433
434 workspace.addExtraEmptyScreen();
435 screenId = workspace.commitExtraEmptyScreen();
436 layout = workspace.getScreenWithId(screenId);
437 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
438
439 if (!found) {
440 Log.wtf(TAG, "Not enough space on an empty screen");
441 }
442 return screenId;
443 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444}