blob: ed869bb09a9fdf03e6a7755319cca9fdff409fd3 [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) {
Adam Cohen732b1d42020-01-30 10:46:52 -0800169 if (action == ACTION_LONG_CLICK) {
170 if (ShortcutUtil.isDeepShortcut(item)) {
171 CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host);
172 if (popup.canShow()) {
173 popup.show();
174 return true;
175 }
176 } else if (host instanceof BubbleTextView) {
177 // Long press should be consumed for workspace items, and it should invoke the
178 // Shortcuts / Notifications / Actions pop-up menu, and not start a drag as the
179 // standard long press path does.
180 PopupContainerWithArrow.showForIcon((BubbleTextView) host);
Pinyao Ting8a739f92019-09-12 11:56:18 -0700181 return true;
182 }
183 }
Adam Cohen732b1d42020-01-30 10:46:52 -0800184
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700185 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800186 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800187 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700188 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700189 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700190 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800191
192 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700193 public void run() {
194 if (item instanceof AppInfo) {
Sunny Goyal95899162019-03-27 16:03:06 -0700195 WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800196 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700197 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700198 screenId, coordinates[0], coordinates[1]);
199
200 ArrayList<ItemInfo> itemList = new ArrayList<>();
201 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700202 mLauncher.bindItems(itemList, true);
Pinyao Tingc9074272019-11-11 11:51:44 -0800203 announceConfirmation(R.string.item_added_to_workspace);
Sunny Goyala9116722015-04-29 13:55:58 -0700204 } else if (item instanceof PendingAddItemInfo) {
205 PendingAddItemInfo info = (PendingAddItemInfo) item;
206 Workspace workspace = mLauncher.getWorkspace();
207 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700208 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700209 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800210 }
Sunny Goyala9116722015-04-29 13:55:58 -0700211 }
212 });
213 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700214 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700215 Folder folder = Folder.getOpen(mLauncher);
216 folder.close(true);
Sunny Goyal95899162019-03-27 16:03:06 -0700217 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700218 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700219
220 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700221 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800222 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700223 LauncherSettings.Favorites.CONTAINER_DESKTOP,
224 screenId, coordinates[0], coordinates[1]);
225
226 // Bind the item in next frame so that if a new workspace page was created,
227 // it will get laid out.
228 new Handler().post(new Runnable() {
229
230 @Override
231 public void run() {
232 ArrayList<ItemInfo> itemList = new ArrayList<>();
233 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700234 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700235 announceConfirmation(R.string.item_moved);
236 }
237 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700238 } else if (action == RESIZE) {
239 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700240 final IntArray actions = getSupportedResizeActions(host, info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700241 CharSequence[] labels = new CharSequence[actions.size()];
242 for (int i = 0; i < actions.size(); i++) {
243 labels[i] = mLauncher.getText(actions.get(i));
244 }
245
246 new AlertDialog.Builder(mLauncher)
247 .setTitle(R.string.action_resize)
248 .setItems(labels, new DialogInterface.OnClickListener() {
249
250 @Override
251 public void onClick(DialogInterface dialog, int which) {
252 performResizeAction(actions.get(which), host, info);
253 dialog.dismiss();
254 }
255 })
256 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700257 return true;
258 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800259 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700260 } else {
261 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000262 if (dropTarget.supportsAccessibilityDrop(item, host) &&
263 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700264 dropTarget.onAccessibilityDrop(host, item);
265 return true;
266 }
267 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800268 }
269 return false;
270 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800271
Sunny Goyalefb7e842018-10-04 15:11:00 -0700272 private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
273 IntArray actions = new IntArray();
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700274
Sunny Goyald3d8c952015-06-01 10:09:06 -0700275 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
276 if (providerInfo == null) {
277 return actions;
278 }
279
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700280 CellLayout layout = (CellLayout) host.getParent().getParent();
281 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
282 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
283 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
284 actions.add(R.string.action_increase_width);
285 }
286
287 if (info.spanX > info.minSpanX && info.spanX > 1) {
288 actions.add(R.string.action_decrease_width);
289 }
290 }
291
292 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
293 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
294 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
295 actions.add(R.string.action_increase_height);
296 }
297
298 if (info.spanY > info.minSpanY && info.spanY > 1) {
299 actions.add(R.string.action_decrease_height);
300 }
301 }
302 return actions;
303 }
304
Sunny Goyal316490e2015-06-02 09:38:28 -0700305 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700306 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
307 CellLayout layout = (CellLayout) host.getParent().getParent();
308 layout.markCellsAsUnoccupiedForView(host);
309
310 if (action == R.string.action_increase_width) {
311 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
312 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
313 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
314 lp.cellX --;
315 info.cellX --;
316 }
317 lp.cellHSpan ++;
318 info.spanX ++;
319 } else if (action == R.string.action_decrease_width) {
320 lp.cellHSpan --;
321 info.spanX --;
322 } else if (action == R.string.action_increase_height) {
323 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
324 lp.cellY --;
325 info.cellY --;
326 }
327 lp.cellVSpan ++;
328 info.spanY ++;
329 } else if (action == R.string.action_decrease_height) {
330 lp.cellVSpan --;
331 info.spanY --;
332 }
333
334 layout.markCellsAsOccupiedForView(host);
335 Rect sizeRange = new Rect();
336 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
337 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
338 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
339 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800340 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700341 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
342 }
343
Adam Cohen091440a2015-03-18 14:16:05 -0700344 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800345 announceConfirmation(mLauncher.getResources().getString(resId));
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800349 mLauncher.getDragLayer().announceForAccessibility(confirmation);
350
351 }
352
353 public boolean isInAccessibleDrag() {
354 return mDragInfo != null;
355 }
356
357 public DragInfo getDragInfo() {
358 return mDragInfo;
359 }
360
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700361 /**
362 * @param clickedTarget the actual view that was clicked
363 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
364 * as the actual drop location otherwise the views center is used.
365 */
366 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800367 String confirmation) {
368 if (!isInAccessibleDrag()) return;
369
370 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700371 if (dropLocation == null) {
372 loc[0] = clickedTarget.getWidth() / 2;
373 loc[1] = clickedTarget.getHeight() / 2;
374 } else {
375 loc[0] = dropLocation.centerX();
376 loc[1] = dropLocation.centerY();
377 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800378
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700379 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800380 mLauncher.getDragController().completeAccessibleDrag(loc);
381
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700382 if (!TextUtils.isEmpty(confirmation)) {
383 announceConfirmation(confirmation);
384 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800385 }
386
387 public void beginAccessibleDrag(View item, ItemInfo info) {
388 mDragInfo = new DragInfo();
389 mDragInfo.info = info;
390 mDragInfo.item = item;
391 mDragInfo.dragType = DragType.ICON;
392 if (info instanceof FolderInfo) {
393 mDragInfo.dragType = DragType.FOLDER;
394 } else if (info instanceof LauncherAppWidgetInfo) {
395 mDragInfo.dragType = DragType.WIDGET;
396 }
397
Adam Cohenc9735cf2015-01-23 16:11:55 -0800398 Rect pos = new Rect();
399 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800400 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyal94b510c2016-08-16 15:36:48 -0700401 mLauncher.getDragController().addDragListener(this);
402
403 DragOptions options = new DragOptions();
404 options.isAccessibleDrag = true;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800405 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800406 }
407
Sunny Goyal45478022015-06-08 16:52:41 -0700408 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700409 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700410 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800411 }
412
Sunny Goyal45478022015-06-08 16:52:41 -0700413 @Override
414 public void onDragEnd() {
415 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800416 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800417 }
Sunny Goyala9116722015-04-29 13:55:58 -0700418
419 /**
420 * Find empty space on the workspace and returns the screenId.
421 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700422 protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700423 Workspace workspace = mLauncher.getWorkspace();
Sunny Goyalefb7e842018-10-04 15:11:00 -0700424 IntArray workspaceScreens = workspace.getScreenOrder();
425 int screenId;
Sunny Goyala9116722015-04-29 13:55:58 -0700426
427 // First check if there is space on the current screen.
428 int screenIndex = workspace.getCurrentPage();
429 screenId = workspaceScreens.get(screenIndex);
430 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
431
432 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700433 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700434 while (!found && screenIndex < workspaceScreens.size()) {
435 screenId = workspaceScreens.get(screenIndex);
436 layout = (CellLayout) workspace.getPageAt(screenIndex);
437 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
438 screenIndex++;
439 }
440
441 if (found) {
442 return screenId;
443 }
444
445 workspace.addExtraEmptyScreen();
446 screenId = workspace.commitExtraEmptyScreen();
447 layout = workspace.getScreenWithId(screenId);
448 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
449
450 if (!found) {
451 Log.wtf(TAG, "Not enough space on an empty screen");
452 }
453 return screenId;
454 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800455}