blob: 6fac79aa7eceb90fb1e483803ca3df8ef57b0ecd [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;
Sunny Goyal9b180102020-03-11 10:02:29 -070010import android.graphics.Point;
Adam Cohenc9735cf2015-01-23 16:11:55 -080011import android.graphics.Rect;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080012import android.os.Bundle;
Sunny Goyal9ae77772015-04-29 16:30:23 -070013import android.os.Handler;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070014import android.text.TextUtils;
Sunny Goyala9116722015-04-29 13:55:58 -070015import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080016import android.util.SparseArray;
17import android.view.View;
18import android.view.View.AccessibilityDelegate;
19import android.view.accessibility.AccessibilityNodeInfo;
20import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
21
Sunny Goyal83a8f042015-05-19 12:52:12 -070022import 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.Launcher;
Sunny Goyal83a8f042015-05-19 12:52:12 -070028import com.android.launcher3.LauncherSettings;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070029import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal83a8f042015-05-19 12:52:12 -070030import com.android.launcher3.PendingAddItemInfo;
31import com.android.launcher3.R;
Sunny Goyal83a8f042015-05-19 12:52:12 -070032import com.android.launcher3.Workspace;
Vadim Tryshevfedca432015-08-19 17:55:02 -070033import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyala52ecb02016-12-16 15:04:51 -080034import com.android.launcher3.dragndrop.DragOptions;
35import com.android.launcher3.folder.Folder;
Pinyao Ting8a739f92019-09-12 11:56:18 -070036import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyala4647b62021-02-02 13:45:34 -080037import com.android.launcher3.keyboard.KeyboardDragAndDropView;
Sunny Goyale396abf2020-04-06 15:11:17 -070038import com.android.launcher3.model.data.AppInfo;
39import com.android.launcher3.model.data.FolderInfo;
40import com.android.launcher3.model.data.ItemInfo;
41import com.android.launcher3.model.data.LauncherAppWidgetInfo;
42import com.android.launcher3.model.data.WorkspaceItemInfo;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070043import com.android.launcher3.notification.NotificationListener;
Mario Bertschlerc06af332017-03-28 12:23:22 -070044import com.android.launcher3.popup.PopupContainerWithArrow;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070045import com.android.launcher3.touch.ItemLongClickListener;
Sunny Goyalefb7e842018-10-04 15:11:00 -070046import com.android.launcher3.util.IntArray;
Pinyao Ting49a3e692019-07-26 12:28:38 -070047import com.android.launcher3.util.ShortcutUtil;
Adam Cohen091440a2015-03-18 14:16:05 -070048import com.android.launcher3.util.Thunk;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070049import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080050
51import java.util.ArrayList;
52
Sunny Goyal45478022015-06-08 16:52:41 -070053public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080054
Sunny Goyala9116722015-04-29 13:55:58 -070055 private static final String TAG = "LauncherAccessibilityDelegate";
56
Sunny Goyal0236d0b2017-10-24 14:54:30 -070057 public static final int REMOVE = R.id.action_remove;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070058 public static final int UNINSTALL = R.id.action_uninstall;
Samuel Fufa5cf3e862020-02-03 20:22:54 -080059 public static final int DISMISS_PREDICTION = R.id.action_dismiss_prediction;
Samuel Fufa6eaf9892020-04-01 11:40:40 -070060 public static final int PIN_PREDICTION = R.id.action_pin_prediction;
Winson Chung1054d4e2018-03-05 19:39:21 +000061 public static final int RECONFIGURE = R.id.action_reconfigure;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070062 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
63 protected static final int MOVE = R.id.action_move;
64 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
65 protected static final int RESIZE = R.id.action_resize;
Sunny Goyal66b24572016-09-21 15:57:55 -070066 public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070067 public static final int SHORTCUTS_AND_NOTIFICATIONS = R.id.action_shortcuts_and_notifications;
Adam Cohenc9735cf2015-01-23 16:11:55 -080068
Sunny Goyale9b651e2015-04-24 11:44:51 -070069 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080070 ICON,
71 FOLDER,
72 WIDGET
73 }
74
75 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070076 public DragType dragType;
77 public ItemInfo info;
78 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080079 }
80
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070081 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070082 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080083
Sunny Goyale9b651e2015-04-24 11:44:51 -070084 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070085
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080086 public LauncherAccessibilityDelegate(Launcher launcher) {
87 mLauncher = launcher;
88
89 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070090 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080091 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070092 launcher.getText(R.string.uninstall_drop_target_label)));
Samuel Fufa5cf3e862020-02-03 20:22:54 -080093 mActions.put(DISMISS_PREDICTION, new AccessibilityAction(DISMISS_PREDICTION,
94 launcher.getText(R.string.dismiss_prediction_label)));
Winson Chung1054d4e2018-03-05 19:39:21 +000095 mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE,
96 launcher.getText(R.string.gadget_setup_text)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080097 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
98 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080099 mActions.put(MOVE, new AccessibilityAction(MOVE,
100 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700101 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
102 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700103 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
104 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700105 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
106 launcher.getText(R.string.action_deep_shortcut)));
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700107 mActions.put(SHORTCUTS_AND_NOTIFICATIONS, new AccessibilityAction(DEEP_SHORTCUTS,
108 launcher.getText(R.string.shortcuts_menu_with_notifications_description)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800109 }
110
111 @Override
112 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
113 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -0700114 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700115 }
116
Sunny Goyal66b24572016-09-21 15:57:55 -0700117 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800118 if (!(host.getTag() instanceof ItemInfo)) return;
119 ItemInfo item = (ItemInfo) host.getTag();
120
Samuel Fufa6eaf9892020-04-01 11:40:40 -0700121 if (host instanceof AccessibilityActionHandler) {
122 ((AccessibilityActionHandler) host).addSupportedAccessibilityActions(info);
123 }
124
Sunny Goyal66b24572016-09-21 15:57:55 -0700125 // If the request came from keyboard, do not add custom shortcuts as that is already
126 // exposed as a direct shortcut
Pinyao Ting49a3e692019-07-26 12:28:38 -0700127 if (!fromKeyboard && ShortcutUtil.supportsShortcuts(item)) {
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700128 info.addAction(mActions.get(NotificationListener.getInstanceIfConnected() != null
129 ? SHORTCUTS_AND_NOTIFICATIONS : DEEP_SHORTCUTS));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700130 }
131
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700132 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000133 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700134 info.addAction(mActions.get(target.getAccessibilityAction()));
135 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700136 }
137
Sunny Goyal66b24572016-09-21 15:57:55 -0700138 // Do not add move actions for keyboard request as this uses virtual nodes.
Sunny Goyala4647b62021-02-02 13:45:34 -0800139 if (itemSupportsAccessibleDrag(item)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800140 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700141
142 if (item.container >= 0) {
143 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700144 } else if (item instanceof LauncherAppWidgetInfo) {
145 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
146 info.addAction(mActions.get(RESIZE));
147 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700148 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700149 }
150
Jon Mirandacafe9182020-03-25 11:46:11 -0700151 if (!fromKeyboard && !itemSupportsLongClick(host, item)) {
152 info.setLongClickable(false);
153 info.removeAction(AccessibilityAction.ACTION_LONG_CLICK);
154 }
155
Adam Cohen6e92f052016-06-07 14:30:10 -0700156 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800157 info.addAction(mActions.get(ADD_TO_WORKSPACE));
158 }
159 }
160
Jon Mirandacafe9182020-03-25 11:46:11 -0700161 private boolean itemSupportsLongClick(View host, ItemInfo info) {
Jon Mirandaff656742020-03-31 19:25:48 -0700162 return PopupContainerWithArrow.canShow(host, info)
163 || new CustomActionsPopup(mLauncher, host).canShow();
Jon Mirandacafe9182020-03-25 11:46:11 -0700164 }
165
vadimt5bc87ec2019-02-07 19:44:08 -0800166 private boolean itemSupportsAccessibleDrag(ItemInfo item) {
Sunny Goyal95899162019-03-27 16:03:06 -0700167 if (item instanceof WorkspaceItemInfo) {
vadimt5bc87ec2019-02-07 19:44:08 -0800168 // Support the action unless the item is in a context menu.
Samuel Fufa6eaf9892020-04-01 11:40:40 -0700169 return item.screenId >= 0 && item.container != Favorites.CONTAINER_HOTSEAT_PREDICTION;
vadimt5bc87ec2019-02-07 19:44:08 -0800170 }
171 return (item instanceof LauncherAppWidgetInfo)
172 || (item instanceof FolderInfo);
173 }
174
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800175 @Override
176 public boolean performAccessibilityAction(View host, int action, Bundle args) {
177 if ((host.getTag() instanceof ItemInfo)
Sunny Goyala4647b62021-02-02 13:45:34 -0800178 && performAction(host, (ItemInfo) host.getTag(), action, false)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800179 return true;
180 }
181 return super.performAccessibilityAction(host, action, args);
182 }
183
Sunny Goyala4647b62021-02-02 13:45:34 -0800184 /**
185 * Performs the provided action on the host
186 */
187 public boolean performAction(final View host, final ItemInfo item, int action,
188 boolean fromKeyboard) {
Adam Cohen732b1d42020-01-30 10:46:52 -0800189 if (action == ACTION_LONG_CLICK) {
Jon Mirandaff656742020-03-31 19:25:48 -0700190 if (PopupContainerWithArrow.canShow(host, item)) {
Adam Cohen732b1d42020-01-30 10:46:52 -0800191 // Long press should be consumed for workspace items, and it should invoke the
192 // Shortcuts / Notifications / Actions pop-up menu, and not start a drag as the
193 // standard long press path does.
194 PopupContainerWithArrow.showForIcon((BubbleTextView) host);
Pinyao Ting8a739f92019-09-12 11:56:18 -0700195 return true;
Jon Mirandacafe9182020-03-25 11:46:11 -0700196 } else {
197 CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host);
198 if (popup.canShow()) {
199 popup.show();
200 return true;
201 }
Pinyao Ting8a739f92019-09-12 11:56:18 -0700202 }
203 }
Samuel Fufa6eaf9892020-04-01 11:40:40 -0700204 if (host instanceof AccessibilityActionHandler
205 && ((AccessibilityActionHandler) host).performAccessibilityAction(action, item)) {
206 return true;
207 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700208 if (action == MOVE) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800209 return beginAccessibleDrag(host, item, fromKeyboard);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800210 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700211 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700212 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700213 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800214
215 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700216 public void run() {
217 if (item instanceof AppInfo) {
Sunny Goyal95899162019-03-27 16:03:06 -0700218 WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800219 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700220 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700221 screenId, coordinates[0], coordinates[1]);
222
223 ArrayList<ItemInfo> itemList = new ArrayList<>();
224 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700225 mLauncher.bindItems(itemList, true);
Pinyao Tingc9074272019-11-11 11:51:44 -0800226 announceConfirmation(R.string.item_added_to_workspace);
Sunny Goyala9116722015-04-29 13:55:58 -0700227 } else if (item instanceof PendingAddItemInfo) {
228 PendingAddItemInfo info = (PendingAddItemInfo) item;
229 Workspace workspace = mLauncher.getWorkspace();
230 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700231 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700232 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800233 }
Sunny Goyala9116722015-04-29 13:55:58 -0700234 }
235 });
236 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700237 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700238 Folder folder = Folder.getOpen(mLauncher);
239 folder.close(true);
Sunny Goyal95899162019-03-27 16:03:06 -0700240 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700241 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700242
243 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700244 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800245 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700246 LauncherSettings.Favorites.CONTAINER_DESKTOP,
247 screenId, coordinates[0], coordinates[1]);
248
249 // Bind the item in next frame so that if a new workspace page was created,
250 // it will get laid out.
251 new Handler().post(new Runnable() {
252
253 @Override
254 public void run() {
255 ArrayList<ItemInfo> itemList = new ArrayList<>();
256 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700257 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700258 announceConfirmation(R.string.item_moved);
259 }
260 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700261 } else if (action == RESIZE) {
262 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700263 final IntArray actions = getSupportedResizeActions(host, info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700264 CharSequence[] labels = new CharSequence[actions.size()];
265 for (int i = 0; i < actions.size(); i++) {
266 labels[i] = mLauncher.getText(actions.get(i));
267 }
268
269 new AlertDialog.Builder(mLauncher)
270 .setTitle(R.string.action_resize)
271 .setItems(labels, new DialogInterface.OnClickListener() {
272
273 @Override
274 public void onClick(DialogInterface dialog, int which) {
275 performResizeAction(actions.get(which), host, info);
276 dialog.dismiss();
277 }
278 })
279 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700280 return true;
281 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800282 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700283 } else {
284 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000285 if (dropTarget.supportsAccessibilityDrop(item, host) &&
286 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700287 dropTarget.onAccessibilityDrop(host, item);
288 return true;
289 }
290 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800291 }
292 return false;
293 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800294
Sunny Goyalefb7e842018-10-04 15:11:00 -0700295 private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
296 IntArray actions = new IntArray();
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700297
Sunny Goyald3d8c952015-06-01 10:09:06 -0700298 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
299 if (providerInfo == null) {
300 return actions;
301 }
302
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700303 CellLayout layout = (CellLayout) host.getParent().getParent();
304 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
305 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
306 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
307 actions.add(R.string.action_increase_width);
308 }
309
310 if (info.spanX > info.minSpanX && info.spanX > 1) {
311 actions.add(R.string.action_decrease_width);
312 }
313 }
314
315 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
316 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
317 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
318 actions.add(R.string.action_increase_height);
319 }
320
321 if (info.spanY > info.minSpanY && info.spanY > 1) {
322 actions.add(R.string.action_decrease_height);
323 }
324 }
325 return actions;
326 }
327
Sunny Goyal316490e2015-06-02 09:38:28 -0700328 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700329 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
330 CellLayout layout = (CellLayout) host.getParent().getParent();
331 layout.markCellsAsUnoccupiedForView(host);
332
333 if (action == R.string.action_increase_width) {
334 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
335 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
336 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
337 lp.cellX --;
338 info.cellX --;
339 }
340 lp.cellHSpan ++;
341 info.spanX ++;
342 } else if (action == R.string.action_decrease_width) {
343 lp.cellHSpan --;
344 info.spanX --;
345 } else if (action == R.string.action_increase_height) {
346 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
347 lp.cellY --;
348 info.cellY --;
349 }
350 lp.cellVSpan ++;
351 info.spanY ++;
352 } else if (action == R.string.action_decrease_height) {
353 lp.cellVSpan --;
354 info.spanY --;
355 }
356
357 layout.markCellsAsOccupiedForView(host);
358 Rect sizeRange = new Rect();
359 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
360 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
361 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
362 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800363 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700364 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
365 }
366
Adam Cohen091440a2015-03-18 14:16:05 -0700367 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800368 announceConfirmation(mLauncher.getResources().getString(resId));
369 }
370
Adam Cohen091440a2015-03-18 14:16:05 -0700371 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800372 mLauncher.getDragLayer().announceForAccessibility(confirmation);
373
374 }
375
376 public boolean isInAccessibleDrag() {
377 return mDragInfo != null;
378 }
379
380 public DragInfo getDragInfo() {
381 return mDragInfo;
382 }
383
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700384 /**
385 * @param clickedTarget the actual view that was clicked
386 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
387 * as the actual drop location otherwise the views center is used.
388 */
389 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800390 String confirmation) {
391 if (!isInAccessibleDrag()) return;
392
393 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700394 if (dropLocation == null) {
395 loc[0] = clickedTarget.getWidth() / 2;
396 loc[1] = clickedTarget.getHeight() / 2;
397 } else {
398 loc[0] = dropLocation.centerX();
399 loc[1] = dropLocation.centerY();
400 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800401
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700402 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800403 mLauncher.getDragController().completeAccessibleDrag(loc);
404
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700405 if (!TextUtils.isEmpty(confirmation)) {
406 announceConfirmation(confirmation);
407 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800408 }
409
Sunny Goyala4647b62021-02-02 13:45:34 -0800410 private boolean beginAccessibleDrag(View item, ItemInfo info, boolean fromKeyboard) {
411 if (!itemSupportsAccessibleDrag(info)) {
412 return false;
413 }
414
Adam Cohenc9735cf2015-01-23 16:11:55 -0800415 mDragInfo = new DragInfo();
416 mDragInfo.info = info;
417 mDragInfo.item = item;
418 mDragInfo.dragType = DragType.ICON;
419 if (info instanceof FolderInfo) {
420 mDragInfo.dragType = DragType.FOLDER;
421 } else if (info instanceof LauncherAppWidgetInfo) {
422 mDragInfo.dragType = DragType.WIDGET;
423 }
424
Adam Cohenc9735cf2015-01-23 16:11:55 -0800425 Rect pos = new Rect();
426 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700427 mLauncher.getDragController().addDragListener(this);
428
429 DragOptions options = new DragOptions();
430 options.isAccessibleDrag = true;
Sunny Goyala4647b62021-02-02 13:45:34 -0800431 options.isKeyboardDrag = fromKeyboard;
Sunny Goyal9b180102020-03-11 10:02:29 -0700432 options.simulatedDndStartPoint = new Point(pos.centerX(), pos.centerY());
Sunny Goyala4647b62021-02-02 13:45:34 -0800433
434 if (fromKeyboard) {
435 KeyboardDragAndDropView popup = (KeyboardDragAndDropView) mLauncher.getLayoutInflater()
436 .inflate(R.layout.keyboard_drag_and_drop, mLauncher.getDragLayer(), false);
437 popup.showForIcon(item, info, options);
438 } else {
439 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
440 }
441 return true;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800442 }
443
Sunny Goyal45478022015-06-08 16:52:41 -0700444 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700445 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700446 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800447 }
448
Sunny Goyal45478022015-06-08 16:52:41 -0700449 @Override
450 public void onDragEnd() {
451 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800452 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800453 }
Sunny Goyala9116722015-04-29 13:55:58 -0700454
455 /**
456 * Find empty space on the workspace and returns the screenId.
457 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700458 protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700459 Workspace workspace = mLauncher.getWorkspace();
Sunny Goyalefb7e842018-10-04 15:11:00 -0700460 IntArray workspaceScreens = workspace.getScreenOrder();
461 int screenId;
Sunny Goyala9116722015-04-29 13:55:58 -0700462
463 // First check if there is space on the current screen.
464 int screenIndex = workspace.getCurrentPage();
465 screenId = workspaceScreens.get(screenIndex);
466 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
467
468 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700469 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700470 while (!found && screenIndex < workspaceScreens.size()) {
471 screenId = workspaceScreens.get(screenIndex);
472 layout = (CellLayout) workspace.getPageAt(screenIndex);
473 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
474 screenIndex++;
475 }
476
477 if (found) {
478 return screenId;
479 }
480
481 workspace.addExtraEmptyScreen();
482 screenId = workspace.commitExtraEmptyScreen();
483 layout = workspace.getScreenWithId(screenId);
484 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
485
486 if (!found) {
487 Log.wtf(TAG, "Not enough space on an empty screen");
488 }
489 return screenId;
490 }
Samuel Fufa6eaf9892020-04-01 11:40:40 -0700491
492 /**
493 * An interface allowing views to handle their own action.
494 */
495 public interface AccessibilityActionHandler {
496
497 /**
498 * performs accessibility action and returns true on success
499 */
500 boolean performAccessibilityAction(int action, ItemInfo itemInfo);
501
502 /**
503 * adds all the accessibility actions that can be handled.
504 */
505 void addSupportedAccessibilityActions(AccessibilityNodeInfo accessibilityNodeInfo);
506 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800507}