blob: 0c12c60a0892df4d2cfd799d63a539a26382dad8 [file] [log] [blame]
Sunny Goyal83a8f042015-05-19 12:52:12 -07001package com.android.launcher3.accessibility;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002
Pinyao Ting8a739f92019-09-12 11:56:18 -07003import static android.view.accessibility.AccessibilityNodeInfo.ACTION_LONG_CLICK;
4
Sunny Goyal3e3f44c2017-10-23 17:14:52 -07005import static com.android.launcher3.LauncherState.NORMAL;
6
Sunny Goyal9ca9c132015-04-29 14:57:22 -07007import android.app.AlertDialog;
8import android.appwidget.AppWidgetProviderInfo;
9import android.content.DialogInterface;
Adam Cohenc9735cf2015-01-23 16:11:55 -080010import android.graphics.Rect;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080011import android.os.Bundle;
Sunny Goyal9ae77772015-04-29 16:30:23 -070012import android.os.Handler;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070013import android.text.TextUtils;
Sunny Goyala9116722015-04-29 13:55:58 -070014import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080015import android.util.SparseArray;
16import android.view.View;
17import android.view.View.AccessibilityDelegate;
18import android.view.accessibility.AccessibilityNodeInfo;
19import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
20
Sunny Goyal83a8f042015-05-19 12:52:12 -070021import com.android.launcher3.AppInfo;
22import com.android.launcher3.AppWidgetResizeFrame;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070023import com.android.launcher3.BubbleTextView;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070024import com.android.launcher3.ButtonDropTarget;
Sunny Goyal83a8f042015-05-19 12:52:12 -070025import com.android.launcher3.CellLayout;
Sunny Goyal94b510c2016-08-16 15:36:48 -070026import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal83a8f042015-05-19 12:52:12 -070027import com.android.launcher3.FolderInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070028import com.android.launcher3.ItemInfo;
29import com.android.launcher3.Launcher;
Sunny Goyal83a8f042015-05-19 12:52:12 -070030import com.android.launcher3.LauncherAppWidgetInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070031import com.android.launcher3.LauncherSettings;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070032import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal83a8f042015-05-19 12:52:12 -070033import com.android.launcher3.PendingAddItemInfo;
34import com.android.launcher3.R;
Sunny Goyal83a8f042015-05-19 12:52:12 -070035import com.android.launcher3.Workspace;
Pinyao Ting8a739f92019-09-12 11:56:18 -070036import com.android.launcher3.WorkspaceItemInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070037import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyala52ecb02016-12-16 15:04:51 -080038import com.android.launcher3.dragndrop.DragOptions;
39import com.android.launcher3.folder.Folder;
Pinyao Ting8a739f92019-09-12 11:56:18 -070040import com.android.launcher3.keyboard.CustomActionsPopup;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070041import com.android.launcher3.notification.NotificationListener;
Mario Bertschlerc06af332017-03-28 12:23:22 -070042import com.android.launcher3.popup.PopupContainerWithArrow;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070043import com.android.launcher3.touch.ItemLongClickListener;
Sunny Goyalefb7e842018-10-04 15:11:00 -070044import com.android.launcher3.util.IntArray;
Pinyao Ting49a3e692019-07-26 12:28:38 -070045import com.android.launcher3.util.ShortcutUtil;
Adam Cohen091440a2015-03-18 14:16:05 -070046import com.android.launcher3.util.Thunk;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070047import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048
49import java.util.ArrayList;
50
Sunny Goyal45478022015-06-08 16:52:41 -070051public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080052
Sunny Goyala9116722015-04-29 13:55:58 -070053 private static final String TAG = "LauncherAccessibilityDelegate";
54
Sunny Goyal0236d0b2017-10-24 14:54:30 -070055 public static final int REMOVE = R.id.action_remove;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070056 public static final int UNINSTALL = R.id.action_uninstall;
Winson Chung1054d4e2018-03-05 19:39:21 +000057 public static final int RECONFIGURE = R.id.action_reconfigure;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070058 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
59 protected static final int MOVE = R.id.action_move;
60 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
61 protected static final int RESIZE = R.id.action_resize;
Sunny Goyal66b24572016-09-21 15:57:55 -070062 public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070063 public static final int SHORTCUTS_AND_NOTIFICATIONS = R.id.action_shortcuts_and_notifications;
Adam Cohenc9735cf2015-01-23 16:11:55 -080064
Sunny Goyale9b651e2015-04-24 11:44:51 -070065 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080066 ICON,
67 FOLDER,
68 WIDGET
69 }
70
71 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070072 public DragType dragType;
73 public ItemInfo info;
74 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080075 }
76
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070077 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070078 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079
Sunny Goyale9b651e2015-04-24 11:44:51 -070080 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070081
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080082 public LauncherAccessibilityDelegate(Launcher launcher) {
83 mLauncher = launcher;
84
85 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070086 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080087 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070088 launcher.getText(R.string.uninstall_drop_target_label)));
Winson Chung1054d4e2018-03-05 19:39:21 +000089 mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE,
90 launcher.getText(R.string.gadget_setup_text)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080091 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
92 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080093 mActions.put(MOVE, new AccessibilityAction(MOVE,
94 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070095 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
96 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -070097 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
98 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070099 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
100 launcher.getText(R.string.action_deep_shortcut)));
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700101 mActions.put(SHORTCUTS_AND_NOTIFICATIONS, new AccessibilityAction(DEEP_SHORTCUTS,
102 launcher.getText(R.string.shortcuts_menu_with_notifications_description)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800103 }
104
Mehdi Alizadeh8588fd02018-04-27 14:08:04 -0700105 public void addAccessibilityAction(int action, int actionLabel) {
106 mActions.put(action, new AccessibilityAction(action, mLauncher.getText(actionLabel)));
107 }
108
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800109 @Override
110 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
111 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -0700112 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700113 }
114
Sunny Goyal66b24572016-09-21 15:57:55 -0700115 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800116 if (!(host.getTag() instanceof ItemInfo)) return;
117 ItemInfo item = (ItemInfo) host.getTag();
118
Sunny Goyal66b24572016-09-21 15:57:55 -0700119 // If the request came from keyboard, do not add custom shortcuts as that is already
120 // exposed as a direct shortcut
Pinyao Ting49a3e692019-07-26 12:28:38 -0700121 if (!fromKeyboard && ShortcutUtil.supportsShortcuts(item)) {
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700122 info.addAction(mActions.get(NotificationListener.getInstanceIfConnected() != null
123 ? SHORTCUTS_AND_NOTIFICATIONS : DEEP_SHORTCUTS));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700124 }
125
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700126 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000127 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700128 info.addAction(mActions.get(target.getAccessibilityAction()));
129 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700130 }
131
Sunny Goyal66b24572016-09-21 15:57:55 -0700132 // Do not add move actions for keyboard request as this uses virtual nodes.
vadimt5bc87ec2019-02-07 19:44:08 -0800133 if (!fromKeyboard && itemSupportsAccessibleDrag(item)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800134 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700135
136 if (item.container >= 0) {
137 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700138 } else if (item instanceof LauncherAppWidgetInfo) {
139 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
140 info.addAction(mActions.get(RESIZE));
141 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700142 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700143 }
144
145 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800146 info.addAction(mActions.get(ADD_TO_WORKSPACE));
147 }
148 }
149
vadimt5bc87ec2019-02-07 19:44:08 -0800150 private boolean itemSupportsAccessibleDrag(ItemInfo item) {
Sunny Goyal95899162019-03-27 16:03:06 -0700151 if (item instanceof WorkspaceItemInfo) {
vadimt5bc87ec2019-02-07 19:44:08 -0800152 // Support the action unless the item is in a context menu.
153 return item.screenId >= 0;
154 }
155 return (item instanceof LauncherAppWidgetInfo)
156 || (item instanceof FolderInfo);
157 }
158
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800159 @Override
160 public boolean performAccessibilityAction(View host, int action, Bundle args) {
161 if ((host.getTag() instanceof ItemInfo)
162 && performAction(host, (ItemInfo) host.getTag(), action)) {
163 return true;
164 }
165 return super.performAccessibilityAction(host, action, args);
166 }
167
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700168 public boolean performAction(final View host, final ItemInfo item, int action) {
Pinyao Ting8a739f92019-09-12 11:56:18 -0700169 if (action == ACTION_LONG_CLICK && ShortcutUtil.isDeepShortcut(item)) {
170 CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host);
171 if (popup.canShow()) {
172 popup.show();
173 return true;
174 }
175 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700176 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800177 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800178 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700179 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700180 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700181 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800182
183 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700184 public void run() {
185 if (item instanceof AppInfo) {
Sunny Goyal95899162019-03-27 16:03:06 -0700186 WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800187 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700188 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700189 screenId, coordinates[0], coordinates[1]);
190
191 ArrayList<ItemInfo> itemList = new ArrayList<>();
192 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700193 mLauncher.bindItems(itemList, true);
Sunny Goyala9116722015-04-29 13:55:58 -0700194 } else if (item instanceof PendingAddItemInfo) {
195 PendingAddItemInfo info = (PendingAddItemInfo) item;
196 Workspace workspace = mLauncher.getWorkspace();
197 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700198 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700199 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800200 }
Sunny Goyala9116722015-04-29 13:55:58 -0700201 announceConfirmation(R.string.item_added_to_workspace);
202 }
203 });
204 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700205 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700206 Folder folder = Folder.getOpen(mLauncher);
207 folder.close(true);
Sunny Goyal95899162019-03-27 16:03:06 -0700208 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700209 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700210
211 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700212 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800213 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700214 LauncherSettings.Favorites.CONTAINER_DESKTOP,
215 screenId, coordinates[0], coordinates[1]);
216
217 // Bind the item in next frame so that if a new workspace page was created,
218 // it will get laid out.
219 new Handler().post(new Runnable() {
220
221 @Override
222 public void run() {
223 ArrayList<ItemInfo> itemList = new ArrayList<>();
224 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700225 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700226 announceConfirmation(R.string.item_moved);
227 }
228 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700229 } else if (action == RESIZE) {
230 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700231 final IntArray actions = getSupportedResizeActions(host, info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700232 CharSequence[] labels = new CharSequence[actions.size()];
233 for (int i = 0; i < actions.size(); i++) {
234 labels[i] = mLauncher.getText(actions.get(i));
235 }
236
237 new AlertDialog.Builder(mLauncher)
238 .setTitle(R.string.action_resize)
239 .setItems(labels, new DialogInterface.OnClickListener() {
240
241 @Override
242 public void onClick(DialogInterface dialog, int which) {
243 performResizeAction(actions.get(which), host, info);
244 dialog.dismiss();
245 }
246 })
247 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700248 return true;
249 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800250 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700251 } else {
252 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000253 if (dropTarget.supportsAccessibilityDrop(item, host) &&
254 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700255 dropTarget.onAccessibilityDrop(host, item);
256 return true;
257 }
258 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800259 }
260 return false;
261 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800262
Sunny Goyalefb7e842018-10-04 15:11:00 -0700263 private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
264 IntArray actions = new IntArray();
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700265
Sunny Goyald3d8c952015-06-01 10:09:06 -0700266 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
267 if (providerInfo == null) {
268 return actions;
269 }
270
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700271 CellLayout layout = (CellLayout) host.getParent().getParent();
272 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
273 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
274 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
275 actions.add(R.string.action_increase_width);
276 }
277
278 if (info.spanX > info.minSpanX && info.spanX > 1) {
279 actions.add(R.string.action_decrease_width);
280 }
281 }
282
283 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
284 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
285 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
286 actions.add(R.string.action_increase_height);
287 }
288
289 if (info.spanY > info.minSpanY && info.spanY > 1) {
290 actions.add(R.string.action_decrease_height);
291 }
292 }
293 return actions;
294 }
295
Sunny Goyal316490e2015-06-02 09:38:28 -0700296 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700297 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
298 CellLayout layout = (CellLayout) host.getParent().getParent();
299 layout.markCellsAsUnoccupiedForView(host);
300
301 if (action == R.string.action_increase_width) {
302 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
303 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
304 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
305 lp.cellX --;
306 info.cellX --;
307 }
308 lp.cellHSpan ++;
309 info.spanX ++;
310 } else if (action == R.string.action_decrease_width) {
311 lp.cellHSpan --;
312 info.spanX --;
313 } else if (action == R.string.action_increase_height) {
314 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
315 lp.cellY --;
316 info.cellY --;
317 }
318 lp.cellVSpan ++;
319 info.spanY ++;
320 } else if (action == R.string.action_decrease_height) {
321 lp.cellVSpan --;
322 info.spanY --;
323 }
324
325 layout.markCellsAsOccupiedForView(host);
326 Rect sizeRange = new Rect();
327 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
328 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
329 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
330 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800331 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700332 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
333 }
334
Adam Cohen091440a2015-03-18 14:16:05 -0700335 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800336 announceConfirmation(mLauncher.getResources().getString(resId));
337 }
338
Adam Cohen091440a2015-03-18 14:16:05 -0700339 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800340 mLauncher.getDragLayer().announceForAccessibility(confirmation);
341
342 }
343
344 public boolean isInAccessibleDrag() {
345 return mDragInfo != null;
346 }
347
348 public DragInfo getDragInfo() {
349 return mDragInfo;
350 }
351
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700352 /**
353 * @param clickedTarget the actual view that was clicked
354 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
355 * as the actual drop location otherwise the views center is used.
356 */
357 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800358 String confirmation) {
359 if (!isInAccessibleDrag()) return;
360
361 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700362 if (dropLocation == null) {
363 loc[0] = clickedTarget.getWidth() / 2;
364 loc[1] = clickedTarget.getHeight() / 2;
365 } else {
366 loc[0] = dropLocation.centerX();
367 loc[1] = dropLocation.centerY();
368 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800369
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700370 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800371 mLauncher.getDragController().completeAccessibleDrag(loc);
372
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700373 if (!TextUtils.isEmpty(confirmation)) {
374 announceConfirmation(confirmation);
375 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800376 }
377
378 public void beginAccessibleDrag(View item, ItemInfo info) {
379 mDragInfo = new DragInfo();
380 mDragInfo.info = info;
381 mDragInfo.item = item;
382 mDragInfo.dragType = DragType.ICON;
383 if (info instanceof FolderInfo) {
384 mDragInfo.dragType = DragType.FOLDER;
385 } else if (info instanceof LauncherAppWidgetInfo) {
386 mDragInfo.dragType = DragType.WIDGET;
387 }
388
Adam Cohenc9735cf2015-01-23 16:11:55 -0800389 Rect pos = new Rect();
390 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800391 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyal94b510c2016-08-16 15:36:48 -0700392 mLauncher.getDragController().addDragListener(this);
393
394 DragOptions options = new DragOptions();
395 options.isAccessibleDrag = true;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800396 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800397 }
398
Sunny Goyal45478022015-06-08 16:52:41 -0700399 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700400 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700401 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800402 }
403
Sunny Goyal45478022015-06-08 16:52:41 -0700404 @Override
405 public void onDragEnd() {
406 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800407 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800408 }
Sunny Goyala9116722015-04-29 13:55:58 -0700409
410 /**
411 * Find empty space on the workspace and returns the screenId.
412 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700413 protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700414 Workspace workspace = mLauncher.getWorkspace();
Sunny Goyalefb7e842018-10-04 15:11:00 -0700415 IntArray workspaceScreens = workspace.getScreenOrder();
416 int screenId;
Sunny Goyala9116722015-04-29 13:55:58 -0700417
418 // First check if there is space on the current screen.
419 int screenIndex = workspace.getCurrentPage();
420 screenId = workspaceScreens.get(screenIndex);
421 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
422
423 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700424 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700425 while (!found && screenIndex < workspaceScreens.size()) {
426 screenId = workspaceScreens.get(screenIndex);
427 layout = (CellLayout) workspace.getPageAt(screenIndex);
428 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
429 screenIndex++;
430 }
431
432 if (found) {
433 return screenId;
434 }
435
436 workspace.addExtraEmptyScreen();
437 screenId = workspace.commitExtraEmptyScreen();
438 layout = workspace.getScreenWithId(screenId);
439 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
440
441 if (!found) {
442 Log.wtf(TAG, "Not enough space on an empty screen");
443 }
444 return screenId;
445 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446}