blob: 0b439ec36226fd2985565e0f673f9f60e47323f2 [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;
Samuel Fufa5cf3e862020-02-03 20:22:54 -080057 public static final int DISMISS_PREDICTION = R.id.action_dismiss_prediction;
Winson Chung1054d4e2018-03-05 19:39:21 +000058 public static final int RECONFIGURE = R.id.action_reconfigure;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070059 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
60 protected static final int MOVE = R.id.action_move;
61 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
62 protected static final int RESIZE = R.id.action_resize;
Sunny Goyal66b24572016-09-21 15:57:55 -070063 public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070064 public static final int SHORTCUTS_AND_NOTIFICATIONS = R.id.action_shortcuts_and_notifications;
Adam Cohenc9735cf2015-01-23 16:11:55 -080065
Sunny Goyale9b651e2015-04-24 11:44:51 -070066 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080067 ICON,
68 FOLDER,
69 WIDGET
70 }
71
72 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070073 public DragType dragType;
74 public ItemInfo info;
75 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080076 }
77
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070078 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070079 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080
Sunny Goyale9b651e2015-04-24 11:44:51 -070081 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070082
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080083 public LauncherAccessibilityDelegate(Launcher launcher) {
84 mLauncher = launcher;
85
86 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070087 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080088 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070089 launcher.getText(R.string.uninstall_drop_target_label)));
Samuel Fufa5cf3e862020-02-03 20:22:54 -080090 mActions.put(DISMISS_PREDICTION, new AccessibilityAction(DISMISS_PREDICTION,
91 launcher.getText(R.string.dismiss_prediction_label)));
Winson Chung1054d4e2018-03-05 19:39:21 +000092 mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE,
93 launcher.getText(R.string.gadget_setup_text)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080094 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
95 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080096 mActions.put(MOVE, new AccessibilityAction(MOVE,
97 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070098 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
99 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700100 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
101 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700102 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
103 launcher.getText(R.string.action_deep_shortcut)));
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700104 mActions.put(SHORTCUTS_AND_NOTIFICATIONS, new AccessibilityAction(DEEP_SHORTCUTS,
105 launcher.getText(R.string.shortcuts_menu_with_notifications_description)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800106 }
107
Mehdi Alizadeh8588fd02018-04-27 14:08:04 -0700108 public void addAccessibilityAction(int action, int actionLabel) {
109 mActions.put(action, new AccessibilityAction(action, mLauncher.getText(actionLabel)));
110 }
111
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800112 @Override
113 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
114 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -0700115 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700116 }
117
Sunny Goyal66b24572016-09-21 15:57:55 -0700118 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800119 if (!(host.getTag() instanceof ItemInfo)) return;
120 ItemInfo item = (ItemInfo) host.getTag();
121
Sunny Goyal66b24572016-09-21 15:57:55 -0700122 // If the request came from keyboard, do not add custom shortcuts as that is already
123 // exposed as a direct shortcut
Pinyao Ting49a3e692019-07-26 12:28:38 -0700124 if (!fromKeyboard && ShortcutUtil.supportsShortcuts(item)) {
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700125 info.addAction(mActions.get(NotificationListener.getInstanceIfConnected() != null
126 ? SHORTCUTS_AND_NOTIFICATIONS : DEEP_SHORTCUTS));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700127 }
128
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700129 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000130 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700131 info.addAction(mActions.get(target.getAccessibilityAction()));
132 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700133 }
134
Sunny Goyal66b24572016-09-21 15:57:55 -0700135 // Do not add move actions for keyboard request as this uses virtual nodes.
vadimt5bc87ec2019-02-07 19:44:08 -0800136 if (!fromKeyboard && itemSupportsAccessibleDrag(item)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800137 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700138
139 if (item.container >= 0) {
140 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700141 } else if (item instanceof LauncherAppWidgetInfo) {
142 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
143 info.addAction(mActions.get(RESIZE));
144 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700145 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700146 }
147
148 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800149 info.addAction(mActions.get(ADD_TO_WORKSPACE));
150 }
151 }
152
vadimt5bc87ec2019-02-07 19:44:08 -0800153 private boolean itemSupportsAccessibleDrag(ItemInfo item) {
Sunny Goyal95899162019-03-27 16:03:06 -0700154 if (item instanceof WorkspaceItemInfo) {
vadimt5bc87ec2019-02-07 19:44:08 -0800155 // Support the action unless the item is in a context menu.
156 return item.screenId >= 0;
157 }
158 return (item instanceof LauncherAppWidgetInfo)
159 || (item instanceof FolderInfo);
160 }
161
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800162 @Override
163 public boolean performAccessibilityAction(View host, int action, Bundle args) {
164 if ((host.getTag() instanceof ItemInfo)
165 && performAction(host, (ItemInfo) host.getTag(), action)) {
166 return true;
167 }
168 return super.performAccessibilityAction(host, action, args);
169 }
170
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700171 public boolean performAction(final View host, final ItemInfo item, int action) {
Adam Cohen732b1d42020-01-30 10:46:52 -0800172 if (action == ACTION_LONG_CLICK) {
173 if (ShortcutUtil.isDeepShortcut(item)) {
174 CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host);
175 if (popup.canShow()) {
176 popup.show();
177 return true;
178 }
179 } else if (host instanceof BubbleTextView) {
180 // Long press should be consumed for workspace items, and it should invoke the
181 // Shortcuts / Notifications / Actions pop-up menu, and not start a drag as the
182 // standard long press path does.
183 PopupContainerWithArrow.showForIcon((BubbleTextView) host);
Pinyao Ting8a739f92019-09-12 11:56:18 -0700184 return true;
185 }
186 }
Adam Cohen732b1d42020-01-30 10:46:52 -0800187
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700188 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800189 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800190 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700191 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700192 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700193 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800194
195 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700196 public void run() {
197 if (item instanceof AppInfo) {
Sunny Goyal95899162019-03-27 16:03:06 -0700198 WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800199 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700200 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700201 screenId, coordinates[0], coordinates[1]);
202
203 ArrayList<ItemInfo> itemList = new ArrayList<>();
204 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700205 mLauncher.bindItems(itemList, true);
Pinyao Tingc9074272019-11-11 11:51:44 -0800206 announceConfirmation(R.string.item_added_to_workspace);
Sunny Goyala9116722015-04-29 13:55:58 -0700207 } else if (item instanceof PendingAddItemInfo) {
208 PendingAddItemInfo info = (PendingAddItemInfo) item;
209 Workspace workspace = mLauncher.getWorkspace();
210 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700211 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700212 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800213 }
Sunny Goyala9116722015-04-29 13:55:58 -0700214 }
215 });
216 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700217 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700218 Folder folder = Folder.getOpen(mLauncher);
219 folder.close(true);
Sunny Goyal95899162019-03-27 16:03:06 -0700220 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700221 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700222
223 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700224 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800225 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700226 LauncherSettings.Favorites.CONTAINER_DESKTOP,
227 screenId, coordinates[0], coordinates[1]);
228
229 // Bind the item in next frame so that if a new workspace page was created,
230 // it will get laid out.
231 new Handler().post(new Runnable() {
232
233 @Override
234 public void run() {
235 ArrayList<ItemInfo> itemList = new ArrayList<>();
236 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700237 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700238 announceConfirmation(R.string.item_moved);
239 }
240 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700241 } else if (action == RESIZE) {
242 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700243 final IntArray actions = getSupportedResizeActions(host, info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700244 CharSequence[] labels = new CharSequence[actions.size()];
245 for (int i = 0; i < actions.size(); i++) {
246 labels[i] = mLauncher.getText(actions.get(i));
247 }
248
249 new AlertDialog.Builder(mLauncher)
250 .setTitle(R.string.action_resize)
251 .setItems(labels, new DialogInterface.OnClickListener() {
252
253 @Override
254 public void onClick(DialogInterface dialog, int which) {
255 performResizeAction(actions.get(which), host, info);
256 dialog.dismiss();
257 }
258 })
259 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700260 return true;
261 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800262 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700263 } else {
264 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000265 if (dropTarget.supportsAccessibilityDrop(item, host) &&
266 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700267 dropTarget.onAccessibilityDrop(host, item);
268 return true;
269 }
270 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800271 }
272 return false;
273 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800274
Sunny Goyalefb7e842018-10-04 15:11:00 -0700275 private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
276 IntArray actions = new IntArray();
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700277
Sunny Goyald3d8c952015-06-01 10:09:06 -0700278 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
279 if (providerInfo == null) {
280 return actions;
281 }
282
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700283 CellLayout layout = (CellLayout) host.getParent().getParent();
284 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
285 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
286 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
287 actions.add(R.string.action_increase_width);
288 }
289
290 if (info.spanX > info.minSpanX && info.spanX > 1) {
291 actions.add(R.string.action_decrease_width);
292 }
293 }
294
295 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
296 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
297 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
298 actions.add(R.string.action_increase_height);
299 }
300
301 if (info.spanY > info.minSpanY && info.spanY > 1) {
302 actions.add(R.string.action_decrease_height);
303 }
304 }
305 return actions;
306 }
307
Sunny Goyal316490e2015-06-02 09:38:28 -0700308 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700309 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
310 CellLayout layout = (CellLayout) host.getParent().getParent();
311 layout.markCellsAsUnoccupiedForView(host);
312
313 if (action == R.string.action_increase_width) {
314 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
315 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
316 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
317 lp.cellX --;
318 info.cellX --;
319 }
320 lp.cellHSpan ++;
321 info.spanX ++;
322 } else if (action == R.string.action_decrease_width) {
323 lp.cellHSpan --;
324 info.spanX --;
325 } else if (action == R.string.action_increase_height) {
326 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
327 lp.cellY --;
328 info.cellY --;
329 }
330 lp.cellVSpan ++;
331 info.spanY ++;
332 } else if (action == R.string.action_decrease_height) {
333 lp.cellVSpan --;
334 info.spanY --;
335 }
336
337 layout.markCellsAsOccupiedForView(host);
338 Rect sizeRange = new Rect();
339 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
340 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
341 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
342 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800343 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700344 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
345 }
346
Adam Cohen091440a2015-03-18 14:16:05 -0700347 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800348 announceConfirmation(mLauncher.getResources().getString(resId));
349 }
350
Adam Cohen091440a2015-03-18 14:16:05 -0700351 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800352 mLauncher.getDragLayer().announceForAccessibility(confirmation);
353
354 }
355
356 public boolean isInAccessibleDrag() {
357 return mDragInfo != null;
358 }
359
360 public DragInfo getDragInfo() {
361 return mDragInfo;
362 }
363
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700364 /**
365 * @param clickedTarget the actual view that was clicked
366 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
367 * as the actual drop location otherwise the views center is used.
368 */
369 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800370 String confirmation) {
371 if (!isInAccessibleDrag()) return;
372
373 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700374 if (dropLocation == null) {
375 loc[0] = clickedTarget.getWidth() / 2;
376 loc[1] = clickedTarget.getHeight() / 2;
377 } else {
378 loc[0] = dropLocation.centerX();
379 loc[1] = dropLocation.centerY();
380 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800381
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700382 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800383 mLauncher.getDragController().completeAccessibleDrag(loc);
384
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700385 if (!TextUtils.isEmpty(confirmation)) {
386 announceConfirmation(confirmation);
387 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800388 }
389
390 public void beginAccessibleDrag(View item, ItemInfo info) {
391 mDragInfo = new DragInfo();
392 mDragInfo.info = info;
393 mDragInfo.item = item;
394 mDragInfo.dragType = DragType.ICON;
395 if (info instanceof FolderInfo) {
396 mDragInfo.dragType = DragType.FOLDER;
397 } else if (info instanceof LauncherAppWidgetInfo) {
398 mDragInfo.dragType = DragType.WIDGET;
399 }
400
Adam Cohenc9735cf2015-01-23 16:11:55 -0800401 Rect pos = new Rect();
402 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800403 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyal94b510c2016-08-16 15:36:48 -0700404 mLauncher.getDragController().addDragListener(this);
405
406 DragOptions options = new DragOptions();
407 options.isAccessibleDrag = true;
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800408 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800409 }
410
Sunny Goyal45478022015-06-08 16:52:41 -0700411 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700412 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700413 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800414 }
415
Sunny Goyal45478022015-06-08 16:52:41 -0700416 @Override
417 public void onDragEnd() {
418 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800419 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800420 }
Sunny Goyala9116722015-04-29 13:55:58 -0700421
422 /**
423 * Find empty space on the workspace and returns the screenId.
424 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700425 protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700426 Workspace workspace = mLauncher.getWorkspace();
Sunny Goyalefb7e842018-10-04 15:11:00 -0700427 IntArray workspaceScreens = workspace.getScreenOrder();
428 int screenId;
Sunny Goyala9116722015-04-29 13:55:58 -0700429
430 // First check if there is space on the current screen.
431 int screenIndex = workspace.getCurrentPage();
432 screenId = workspaceScreens.get(screenIndex);
433 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
434
435 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700436 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700437 while (!found && screenIndex < workspaceScreens.size()) {
438 screenId = workspaceScreens.get(screenIndex);
439 layout = (CellLayout) workspace.getPageAt(screenIndex);
440 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
441 screenIndex++;
442 }
443
444 if (found) {
445 return screenId;
446 }
447
448 workspace.addExtraEmptyScreen();
449 screenId = workspace.commitExtraEmptyScreen();
450 layout = workspace.getScreenWithId(screenId);
451 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
452
453 if (!found) {
454 Log.wtf(TAG, "Not enough space on an empty screen");
455 }
456 return screenId;
457 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800458}