blob: 136d43ea7334ad7c543aa976e8caf2c6fe715e7f [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 Goyale396abf2020-04-06 15:11:17 -070037import com.android.launcher3.model.data.AppInfo;
38import com.android.launcher3.model.data.FolderInfo;
39import com.android.launcher3.model.data.ItemInfo;
40import com.android.launcher3.model.data.LauncherAppWidgetInfo;
41import com.android.launcher3.model.data.WorkspaceItemInfo;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070042import com.android.launcher3.notification.NotificationListener;
Mario Bertschlerc06af332017-03-28 12:23:22 -070043import com.android.launcher3.popup.PopupContainerWithArrow;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070044import com.android.launcher3.touch.ItemLongClickListener;
Sunny Goyalefb7e842018-10-04 15:11:00 -070045import com.android.launcher3.util.IntArray;
Pinyao Ting49a3e692019-07-26 12:28:38 -070046import com.android.launcher3.util.ShortcutUtil;
Adam Cohen091440a2015-03-18 14:16:05 -070047import com.android.launcher3.util.Thunk;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070048import com.android.launcher3.widget.LauncherAppWidgetHostView;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049
50import java.util.ArrayList;
51
Sunny Goyal45478022015-06-08 16:52:41 -070052public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080053
Sunny Goyala9116722015-04-29 13:55:58 -070054 private static final String TAG = "LauncherAccessibilityDelegate";
55
Sunny Goyal0236d0b2017-10-24 14:54:30 -070056 public static final int REMOVE = R.id.action_remove;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070057 public static final int UNINSTALL = R.id.action_uninstall;
Samuel Fufa5cf3e862020-02-03 20:22:54 -080058 public static final int DISMISS_PREDICTION = R.id.action_dismiss_prediction;
Samuel Fufa6eaf9892020-04-01 11:40:40 -070059 public static final int PIN_PREDICTION = R.id.action_pin_prediction;
Winson Chung1054d4e2018-03-05 19:39:21 +000060 public static final int RECONFIGURE = R.id.action_reconfigure;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070061 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
62 protected static final int MOVE = R.id.action_move;
63 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
64 protected static final int RESIZE = R.id.action_resize;
Sunny Goyal66b24572016-09-21 15:57:55 -070065 public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070066 public static final int SHORTCUTS_AND_NOTIFICATIONS = R.id.action_shortcuts_and_notifications;
Adam Cohenc9735cf2015-01-23 16:11:55 -080067
Sunny Goyale9b651e2015-04-24 11:44:51 -070068 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080069 ICON,
70 FOLDER,
71 WIDGET
72 }
73
74 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070075 public DragType dragType;
76 public ItemInfo info;
77 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080078 }
79
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070080 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070081 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080082
Sunny Goyale9b651e2015-04-24 11:44:51 -070083 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070084
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080085 public LauncherAccessibilityDelegate(Launcher launcher) {
86 mLauncher = launcher;
87
88 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070089 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080090 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070091 launcher.getText(R.string.uninstall_drop_target_label)));
Samuel Fufa5cf3e862020-02-03 20:22:54 -080092 mActions.put(DISMISS_PREDICTION, new AccessibilityAction(DISMISS_PREDICTION,
93 launcher.getText(R.string.dismiss_prediction_label)));
Winson Chung1054d4e2018-03-05 19:39:21 +000094 mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE,
95 launcher.getText(R.string.gadget_setup_text)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080096 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
97 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080098 mActions.put(MOVE, new AccessibilityAction(MOVE,
99 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700100 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
101 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700102 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
103 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700104 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
105 launcher.getText(R.string.action_deep_shortcut)));
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700106 mActions.put(SHORTCUTS_AND_NOTIFICATIONS, new AccessibilityAction(DEEP_SHORTCUTS,
107 launcher.getText(R.string.shortcuts_menu_with_notifications_description)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800108 }
109
Mehdi Alizadeh8588fd02018-04-27 14:08:04 -0700110 public void addAccessibilityAction(int action, int actionLabel) {
111 mActions.put(action, new AccessibilityAction(action, mLauncher.getText(actionLabel)));
112 }
113
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800114 @Override
115 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
116 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -0700117 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700118 }
119
Sunny Goyal66b24572016-09-21 15:57:55 -0700120 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800121 if (!(host.getTag() instanceof ItemInfo)) return;
122 ItemInfo item = (ItemInfo) host.getTag();
123
Samuel Fufa6eaf9892020-04-01 11:40:40 -0700124 if (host instanceof AccessibilityActionHandler) {
125 ((AccessibilityActionHandler) host).addSupportedAccessibilityActions(info);
126 }
127
Sunny Goyal66b24572016-09-21 15:57:55 -0700128 // If the request came from keyboard, do not add custom shortcuts as that is already
129 // exposed as a direct shortcut
Pinyao Ting49a3e692019-07-26 12:28:38 -0700130 if (!fromKeyboard && ShortcutUtil.supportsShortcuts(item)) {
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700131 info.addAction(mActions.get(NotificationListener.getInstanceIfConnected() != null
132 ? SHORTCUTS_AND_NOTIFICATIONS : DEEP_SHORTCUTS));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700133 }
134
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700135 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000136 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700137 info.addAction(mActions.get(target.getAccessibilityAction()));
138 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700139 }
140
Sunny Goyal66b24572016-09-21 15:57:55 -0700141 // Do not add move actions for keyboard request as this uses virtual nodes.
vadimt5bc87ec2019-02-07 19:44:08 -0800142 if (!fromKeyboard && itemSupportsAccessibleDrag(item)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800143 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700144
145 if (item.container >= 0) {
146 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700147 } else if (item instanceof LauncherAppWidgetInfo) {
148 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
149 info.addAction(mActions.get(RESIZE));
150 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700151 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700152 }
153
Jon Mirandacafe9182020-03-25 11:46:11 -0700154 if (!fromKeyboard && !itemSupportsLongClick(host, item)) {
155 info.setLongClickable(false);
156 info.removeAction(AccessibilityAction.ACTION_LONG_CLICK);
157 }
158
Adam Cohen6e92f052016-06-07 14:30:10 -0700159 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800160 info.addAction(mActions.get(ADD_TO_WORKSPACE));
161 }
162 }
163
Jon Mirandacafe9182020-03-25 11:46:11 -0700164 private boolean itemSupportsLongClick(View host, ItemInfo info) {
Jon Mirandaff656742020-03-31 19:25:48 -0700165 return PopupContainerWithArrow.canShow(host, info)
166 || new CustomActionsPopup(mLauncher, host).canShow();
Jon Mirandacafe9182020-03-25 11:46:11 -0700167 }
168
vadimt5bc87ec2019-02-07 19:44:08 -0800169 private boolean itemSupportsAccessibleDrag(ItemInfo item) {
Sunny Goyal95899162019-03-27 16:03:06 -0700170 if (item instanceof WorkspaceItemInfo) {
vadimt5bc87ec2019-02-07 19:44:08 -0800171 // Support the action unless the item is in a context menu.
Samuel Fufa6eaf9892020-04-01 11:40:40 -0700172 return item.screenId >= 0 && item.container != Favorites.CONTAINER_HOTSEAT_PREDICTION;
vadimt5bc87ec2019-02-07 19:44:08 -0800173 }
174 return (item instanceof LauncherAppWidgetInfo)
175 || (item instanceof FolderInfo);
176 }
177
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800178 @Override
179 public boolean performAccessibilityAction(View host, int action, Bundle args) {
180 if ((host.getTag() instanceof ItemInfo)
181 && performAction(host, (ItemInfo) host.getTag(), action)) {
182 return true;
183 }
184 return super.performAccessibilityAction(host, action, args);
185 }
186
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700187 public boolean performAction(final View host, final ItemInfo item, int action) {
Adam Cohen732b1d42020-01-30 10:46:52 -0800188 if (action == ACTION_LONG_CLICK) {
Jon Mirandaff656742020-03-31 19:25:48 -0700189 if (PopupContainerWithArrow.canShow(host, item)) {
Adam Cohen732b1d42020-01-30 10:46:52 -0800190 // Long press should be consumed for workspace items, and it should invoke the
191 // Shortcuts / Notifications / Actions pop-up menu, and not start a drag as the
192 // standard long press path does.
193 PopupContainerWithArrow.showForIcon((BubbleTextView) host);
Pinyao Ting8a739f92019-09-12 11:56:18 -0700194 return true;
Jon Mirandacafe9182020-03-25 11:46:11 -0700195 } else {
196 CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host);
197 if (popup.canShow()) {
198 popup.show();
199 return true;
200 }
Pinyao Ting8a739f92019-09-12 11:56:18 -0700201 }
202 }
Samuel Fufa6eaf9892020-04-01 11:40:40 -0700203 if (host instanceof AccessibilityActionHandler
204 && ((AccessibilityActionHandler) host).performAccessibilityAction(action, item)) {
205 return true;
206 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700207 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800208 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800209 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700210 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700211 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700212 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800213
214 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700215 public void run() {
216 if (item instanceof AppInfo) {
Sunny Goyal95899162019-03-27 16:03:06 -0700217 WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800218 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700219 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700220 screenId, coordinates[0], coordinates[1]);
221
222 ArrayList<ItemInfo> itemList = new ArrayList<>();
223 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700224 mLauncher.bindItems(itemList, true);
Pinyao Tingc9074272019-11-11 11:51:44 -0800225 announceConfirmation(R.string.item_added_to_workspace);
Sunny Goyala9116722015-04-29 13:55:58 -0700226 } else if (item instanceof PendingAddItemInfo) {
227 PendingAddItemInfo info = (PendingAddItemInfo) item;
228 Workspace workspace = mLauncher.getWorkspace();
229 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700230 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700231 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800232 }
Sunny Goyala9116722015-04-29 13:55:58 -0700233 }
234 });
235 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700236 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700237 Folder folder = Folder.getOpen(mLauncher);
238 folder.close(true);
Sunny Goyal95899162019-03-27 16:03:06 -0700239 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700240 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700241
242 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700243 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800244 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700245 LauncherSettings.Favorites.CONTAINER_DESKTOP,
246 screenId, coordinates[0], coordinates[1]);
247
248 // Bind the item in next frame so that if a new workspace page was created,
249 // it will get laid out.
250 new Handler().post(new Runnable() {
251
252 @Override
253 public void run() {
254 ArrayList<ItemInfo> itemList = new ArrayList<>();
255 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700256 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700257 announceConfirmation(R.string.item_moved);
258 }
259 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700260 } else if (action == RESIZE) {
261 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700262 final IntArray actions = getSupportedResizeActions(host, info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700263 CharSequence[] labels = new CharSequence[actions.size()];
264 for (int i = 0; i < actions.size(); i++) {
265 labels[i] = mLauncher.getText(actions.get(i));
266 }
267
268 new AlertDialog.Builder(mLauncher)
269 .setTitle(R.string.action_resize)
270 .setItems(labels, new DialogInterface.OnClickListener() {
271
272 @Override
273 public void onClick(DialogInterface dialog, int which) {
274 performResizeAction(actions.get(which), host, info);
275 dialog.dismiss();
276 }
277 })
278 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700279 return true;
280 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800281 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700282 } else {
283 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000284 if (dropTarget.supportsAccessibilityDrop(item, host) &&
285 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700286 dropTarget.onAccessibilityDrop(host, item);
287 return true;
288 }
289 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800290 }
291 return false;
292 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800293
Sunny Goyalefb7e842018-10-04 15:11:00 -0700294 private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
295 IntArray actions = new IntArray();
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700296
Sunny Goyald3d8c952015-06-01 10:09:06 -0700297 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
298 if (providerInfo == null) {
299 return actions;
300 }
301
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700302 CellLayout layout = (CellLayout) host.getParent().getParent();
303 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
304 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
305 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
306 actions.add(R.string.action_increase_width);
307 }
308
309 if (info.spanX > info.minSpanX && info.spanX > 1) {
310 actions.add(R.string.action_decrease_width);
311 }
312 }
313
314 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
315 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
316 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
317 actions.add(R.string.action_increase_height);
318 }
319
320 if (info.spanY > info.minSpanY && info.spanY > 1) {
321 actions.add(R.string.action_decrease_height);
322 }
323 }
324 return actions;
325 }
326
Sunny Goyal316490e2015-06-02 09:38:28 -0700327 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700328 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
329 CellLayout layout = (CellLayout) host.getParent().getParent();
330 layout.markCellsAsUnoccupiedForView(host);
331
332 if (action == R.string.action_increase_width) {
333 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
334 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
335 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
336 lp.cellX --;
337 info.cellX --;
338 }
339 lp.cellHSpan ++;
340 info.spanX ++;
341 } else if (action == R.string.action_decrease_width) {
342 lp.cellHSpan --;
343 info.spanX --;
344 } else if (action == R.string.action_increase_height) {
345 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
346 lp.cellY --;
347 info.cellY --;
348 }
349 lp.cellVSpan ++;
350 info.spanY ++;
351 } else if (action == R.string.action_decrease_height) {
352 lp.cellVSpan --;
353 info.spanY --;
354 }
355
356 layout.markCellsAsOccupiedForView(host);
357 Rect sizeRange = new Rect();
358 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
359 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
360 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
361 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800362 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700363 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
364 }
365
Adam Cohen091440a2015-03-18 14:16:05 -0700366 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800367 announceConfirmation(mLauncher.getResources().getString(resId));
368 }
369
Adam Cohen091440a2015-03-18 14:16:05 -0700370 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800371 mLauncher.getDragLayer().announceForAccessibility(confirmation);
372
373 }
374
375 public boolean isInAccessibleDrag() {
376 return mDragInfo != null;
377 }
378
379 public DragInfo getDragInfo() {
380 return mDragInfo;
381 }
382
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700383 /**
384 * @param clickedTarget the actual view that was clicked
385 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
386 * as the actual drop location otherwise the views center is used.
387 */
388 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800389 String confirmation) {
390 if (!isInAccessibleDrag()) return;
391
392 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700393 if (dropLocation == null) {
394 loc[0] = clickedTarget.getWidth() / 2;
395 loc[1] = clickedTarget.getHeight() / 2;
396 } else {
397 loc[0] = dropLocation.centerX();
398 loc[1] = dropLocation.centerY();
399 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800400
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700401 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800402 mLauncher.getDragController().completeAccessibleDrag(loc);
403
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700404 if (!TextUtils.isEmpty(confirmation)) {
405 announceConfirmation(confirmation);
406 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800407 }
408
409 public void beginAccessibleDrag(View item, ItemInfo info) {
410 mDragInfo = new DragInfo();
411 mDragInfo.info = info;
412 mDragInfo.item = item;
413 mDragInfo.dragType = DragType.ICON;
414 if (info instanceof FolderInfo) {
415 mDragInfo.dragType = DragType.FOLDER;
416 } else if (info instanceof LauncherAppWidgetInfo) {
417 mDragInfo.dragType = DragType.WIDGET;
418 }
419
Adam Cohenc9735cf2015-01-23 16:11:55 -0800420 Rect pos = new Rect();
421 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700422 mLauncher.getDragController().addDragListener(this);
423
424 DragOptions options = new DragOptions();
425 options.isAccessibleDrag = true;
Sunny Goyal9b180102020-03-11 10:02:29 -0700426 options.simulatedDndStartPoint = new Point(pos.centerX(), pos.centerY());
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800427 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800428 }
429
Sunny Goyal45478022015-06-08 16:52:41 -0700430 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700431 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700432 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800433 }
434
Sunny Goyal45478022015-06-08 16:52:41 -0700435 @Override
436 public void onDragEnd() {
437 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800438 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800439 }
Sunny Goyala9116722015-04-29 13:55:58 -0700440
441 /**
442 * Find empty space on the workspace and returns the screenId.
443 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700444 protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700445 Workspace workspace = mLauncher.getWorkspace();
Sunny Goyalefb7e842018-10-04 15:11:00 -0700446 IntArray workspaceScreens = workspace.getScreenOrder();
447 int screenId;
Sunny Goyala9116722015-04-29 13:55:58 -0700448
449 // First check if there is space on the current screen.
450 int screenIndex = workspace.getCurrentPage();
451 screenId = workspaceScreens.get(screenIndex);
452 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
453
454 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700455 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700456 while (!found && screenIndex < workspaceScreens.size()) {
457 screenId = workspaceScreens.get(screenIndex);
458 layout = (CellLayout) workspace.getPageAt(screenIndex);
459 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
460 screenIndex++;
461 }
462
463 if (found) {
464 return screenId;
465 }
466
467 workspace.addExtraEmptyScreen();
468 screenId = workspace.commitExtraEmptyScreen();
469 layout = workspace.getScreenWithId(screenId);
470 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
471
472 if (!found) {
473 Log.wtf(TAG, "Not enough space on an empty screen");
474 }
475 return screenId;
476 }
Samuel Fufa6eaf9892020-04-01 11:40:40 -0700477
478 /**
479 * An interface allowing views to handle their own action.
480 */
481 public interface AccessibilityActionHandler {
482
483 /**
484 * performs accessibility action and returns true on success
485 */
486 boolean performAccessibilityAction(int action, ItemInfo itemInfo);
487
488 /**
489 * adds all the accessibility actions that can be handled.
490 */
491 void addSupportedAccessibilityActions(AccessibilityNodeInfo accessibilityNodeInfo);
492 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800493}