blob: 81a0e1d5dfb9ea4efae5c3cf832353b30980fda0 [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 Goyal83a8f042015-05-19 12:52:12 -070028import com.android.launcher3.LauncherAppWidgetInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070029import com.android.launcher3.LauncherSettings;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070030import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal83a8f042015-05-19 12:52:12 -070031import com.android.launcher3.PendingAddItemInfo;
32import com.android.launcher3.R;
33import com.android.launcher3.ShortcutInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070034import com.android.launcher3.Workspace;
Vadim Tryshevfedca432015-08-19 17:55:02 -070035import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyala52ecb02016-12-16 15:04:51 -080036import com.android.launcher3.dragndrop.DragOptions;
37import com.android.launcher3.folder.Folder;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070038import com.android.launcher3.notification.NotificationListener;
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;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070041import com.android.launcher3.touch.ItemLongClickListener;
Adam Cohen091440a2015-03-18 14:16:05 -070042import com.android.launcher3.util.Thunk;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070043import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044
45import java.util.ArrayList;
46
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 Goyal0236d0b2017-10-24 14:54:30 -070051 public static final int REMOVE = R.id.action_remove;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070052 public static final int UNINSTALL = R.id.action_uninstall;
Winson Chung1054d4e2018-03-05 19:39:21 +000053 public static final int RECONFIGURE = R.id.action_reconfigure;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070054 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;
Sunny Goyal66b24572016-09-21 15:57:55 -070058 public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070059 public static final int SHORTCUTS_AND_NOTIFICATIONS = R.id.action_shortcuts_and_notifications;
Adam Cohenc9735cf2015-01-23 16:11:55 -080060
Sunny Goyale9b651e2015-04-24 11:44:51 -070061 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080062 ICON,
63 FOLDER,
64 WIDGET
65 }
66
67 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070068 public DragType dragType;
69 public ItemInfo info;
70 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080071 }
72
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070073 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070074 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075
Sunny Goyale9b651e2015-04-24 11:44:51 -070076 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070077
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(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070084 launcher.getText(R.string.uninstall_drop_target_label)));
Winson Chung1054d4e2018-03-05 19:39:21 +000085 mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE,
86 launcher.getText(R.string.gadget_setup_text)));
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)));
Vadim Trysheva50d99f2018-05-04 17:58:29 -070097 mActions.put(SHORTCUTS_AND_NOTIFICATIONS, new AccessibilityAction(DEEP_SHORTCUTS,
98 launcher.getText(R.string.shortcuts_menu_with_notifications_description)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080099 }
100
Mehdi Alizadeh8588fd02018-04-27 14:08:04 -0700101 public void addAccessibilityAction(int action, int actionLabel) {
102 mActions.put(action, new AccessibilityAction(action, mLauncher.getText(actionLabel)));
103 }
104
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800105 @Override
106 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
107 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -0700108 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700109 }
110
Sunny Goyal66b24572016-09-21 15:57:55 -0700111 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800112 if (!(host.getTag() instanceof ItemInfo)) return;
113 ItemInfo item = (ItemInfo) host.getTag();
114
Sunny Goyal66b24572016-09-21 15:57:55 -0700115 // If the request came from keyboard, do not add custom shortcuts as that is already
116 // exposed as a direct shortcut
Tony18c4aa42017-05-10 21:23:57 -0500117 if (!fromKeyboard && DeepShortcutManager.supportsShortcuts(item)) {
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700118 info.addAction(mActions.get(NotificationListener.getInstanceIfConnected() != null
119 ? SHORTCUTS_AND_NOTIFICATIONS : DEEP_SHORTCUTS));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700120 }
121
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700122 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000123 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700124 info.addAction(mActions.get(target.getAccessibilityAction()));
125 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700126 }
127
Sunny Goyal66b24572016-09-21 15:57:55 -0700128 // Do not add move actions for keyboard request as this uses virtual nodes.
129 if (!fromKeyboard && ((item instanceof ShortcutInfo)
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800130 || (item instanceof LauncherAppWidgetInfo)
Sunny Goyal66b24572016-09-21 15:57:55 -0700131 || (item instanceof FolderInfo))) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800132 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700133
134 if (item.container >= 0) {
135 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700136 } else if (item instanceof LauncherAppWidgetInfo) {
137 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
138 info.addAction(mActions.get(RESIZE));
139 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700140 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700141 }
142
143 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800144 info.addAction(mActions.get(ADD_TO_WORKSPACE));
145 }
146 }
147
148 @Override
149 public boolean performAccessibilityAction(View host, int action, Bundle args) {
150 if ((host.getTag() instanceof ItemInfo)
151 && performAction(host, (ItemInfo) host.getTag(), action)) {
152 return true;
153 }
154 return super.performAccessibilityAction(host, action, args);
155 }
156
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700157 public boolean performAction(final View host, final ItemInfo item, int action) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700158 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800159 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800160 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700161 final int[] coordinates = new int[2];
162 final long screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700163 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800164
165 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700166 public void run() {
167 if (item instanceof AppInfo) {
168 ShortcutInfo info = ((AppInfo) item).makeShortcut();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800169 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700170 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700171 screenId, coordinates[0], coordinates[1]);
172
173 ArrayList<ItemInfo> itemList = new ArrayList<>();
174 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700175 mLauncher.bindItems(itemList, true);
Sunny Goyala9116722015-04-29 13:55:58 -0700176 } else if (item instanceof PendingAddItemInfo) {
177 PendingAddItemInfo info = (PendingAddItemInfo) item;
178 Workspace workspace = mLauncher.getWorkspace();
179 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700180 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700181 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800182 }
Sunny Goyala9116722015-04-29 13:55:58 -0700183 announceConfirmation(R.string.item_added_to_workspace);
184 }
185 });
186 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700187 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700188 Folder folder = Folder.getOpen(mLauncher);
189 folder.close(true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700190 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700191 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700192
193 final int[] coordinates = new int[2];
194 final long screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800195 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700196 LauncherSettings.Favorites.CONTAINER_DESKTOP,
197 screenId, coordinates[0], coordinates[1]);
198
199 // Bind the item in next frame so that if a new workspace page was created,
200 // it will get laid out.
201 new Handler().post(new Runnable() {
202
203 @Override
204 public void run() {
205 ArrayList<ItemInfo> itemList = new ArrayList<>();
206 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700207 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700208 announceConfirmation(R.string.item_moved);
209 }
210 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700211 } else if (action == RESIZE) {
212 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
213 final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
214 CharSequence[] labels = new CharSequence[actions.size()];
215 for (int i = 0; i < actions.size(); i++) {
216 labels[i] = mLauncher.getText(actions.get(i));
217 }
218
219 new AlertDialog.Builder(mLauncher)
220 .setTitle(R.string.action_resize)
221 .setItems(labels, new DialogInterface.OnClickListener() {
222
223 @Override
224 public void onClick(DialogInterface dialog, int which) {
225 performResizeAction(actions.get(which), host, info);
226 dialog.dismiss();
227 }
228 })
229 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700230 return true;
231 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800232 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700233 } else {
234 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000235 if (dropTarget.supportsAccessibilityDrop(item, host) &&
236 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700237 dropTarget.onAccessibilityDrop(host, item);
238 return true;
239 }
240 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800241 }
242 return false;
243 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800244
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700245 private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700246 ArrayList<Integer> actions = new ArrayList<>();
247
Sunny Goyald3d8c952015-06-01 10:09:06 -0700248 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
249 if (providerInfo == null) {
250 return actions;
251 }
252
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700253 CellLayout layout = (CellLayout) host.getParent().getParent();
254 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
255 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
256 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
257 actions.add(R.string.action_increase_width);
258 }
259
260 if (info.spanX > info.minSpanX && info.spanX > 1) {
261 actions.add(R.string.action_decrease_width);
262 }
263 }
264
265 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
266 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
267 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
268 actions.add(R.string.action_increase_height);
269 }
270
271 if (info.spanY > info.minSpanY && info.spanY > 1) {
272 actions.add(R.string.action_decrease_height);
273 }
274 }
275 return actions;
276 }
277
Sunny Goyal316490e2015-06-02 09:38:28 -0700278 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700279 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
280 CellLayout layout = (CellLayout) host.getParent().getParent();
281 layout.markCellsAsUnoccupiedForView(host);
282
283 if (action == R.string.action_increase_width) {
284 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
285 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
286 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
287 lp.cellX --;
288 info.cellX --;
289 }
290 lp.cellHSpan ++;
291 info.spanX ++;
292 } else if (action == R.string.action_decrease_width) {
293 lp.cellHSpan --;
294 info.spanX --;
295 } else if (action == R.string.action_increase_height) {
296 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
297 lp.cellY --;
298 info.cellY --;
299 }
300 lp.cellVSpan ++;
301 info.spanY ++;
302 } else if (action == R.string.action_decrease_height) {
303 lp.cellVSpan --;
304 info.spanY --;
305 }
306
307 layout.markCellsAsOccupiedForView(host);
308 Rect sizeRange = new Rect();
309 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
310 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
311 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
312 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800313 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700314 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
315 }
316
Adam Cohen091440a2015-03-18 14:16:05 -0700317 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800318 announceConfirmation(mLauncher.getResources().getString(resId));
319 }
320
Adam Cohen091440a2015-03-18 14:16:05 -0700321 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800322 mLauncher.getDragLayer().announceForAccessibility(confirmation);
323
324 }
325
326 public boolean isInAccessibleDrag() {
327 return mDragInfo != null;
328 }
329
330 public DragInfo getDragInfo() {
331 return mDragInfo;
332 }
333
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700334 /**
335 * @param clickedTarget the actual view that was clicked
336 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
337 * as the actual drop location otherwise the views center is used.
338 */
339 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800340 String confirmation) {
341 if (!isInAccessibleDrag()) return;
342
343 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700344 if (dropLocation == null) {
345 loc[0] = clickedTarget.getWidth() / 2;
346 loc[1] = clickedTarget.getHeight() / 2;
347 } else {
348 loc[0] = dropLocation.centerX();
349 loc[1] = dropLocation.centerY();
350 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800351
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700352 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800353 mLauncher.getDragController().completeAccessibleDrag(loc);
354
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700355 if (!TextUtils.isEmpty(confirmation)) {
356 announceConfirmation(confirmation);
357 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800358 }
359
360 public void beginAccessibleDrag(View item, ItemInfo info) {
361 mDragInfo = new DragInfo();
362 mDragInfo.info = info;
363 mDragInfo.item = item;
364 mDragInfo.dragType = DragType.ICON;
365 if (info instanceof FolderInfo) {
366 mDragInfo.dragType = DragType.FOLDER;
367 } else if (info instanceof LauncherAppWidgetInfo) {
368 mDragInfo.dragType = DragType.WIDGET;
369 }
370
Adam Cohenc9735cf2015-01-23 16:11:55 -0800371 Rect pos = new Rect();
372 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800373 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyal94b510c2016-08-16 15:36:48 -0700374 mLauncher.getDragController().addDragListener(this);
375
376 DragOptions options = new DragOptions();
377 options.isAccessibleDrag = true;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800378 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800379 }
380
Sunny Goyal45478022015-06-08 16:52:41 -0700381 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700382 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700383 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800384 }
385
Sunny Goyal45478022015-06-08 16:52:41 -0700386 @Override
387 public void onDragEnd() {
388 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800389 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800390 }
Sunny Goyala9116722015-04-29 13:55:58 -0700391
392 /**
393 * Find empty space on the workspace and returns the screenId.
394 */
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700395 protected long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700396 Workspace workspace = mLauncher.getWorkspace();
397 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
398 long screenId;
399
400 // First check if there is space on the current screen.
401 int screenIndex = workspace.getCurrentPage();
402 screenId = workspaceScreens.get(screenIndex);
403 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
404
405 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700406 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700407 while (!found && screenIndex < workspaceScreens.size()) {
408 screenId = workspaceScreens.get(screenIndex);
409 layout = (CellLayout) workspace.getPageAt(screenIndex);
410 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
411 screenIndex++;
412 }
413
414 if (found) {
415 return screenId;
416 }
417
418 workspace.addExtraEmptyScreen();
419 screenId = workspace.commitExtraEmptyScreen();
420 layout = workspace.getScreenWithId(screenId);
421 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
422
423 if (!found) {
424 Log.wtf(TAG, "Not enough space on an empty screen");
425 }
426 return screenId;
427 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428}