blob: f45c0b19d24a863912827d05838da0929d9ec6f7 [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.AppInfo;
23import com.android.launcher3.AppWidgetResizeFrame;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070024import com.android.launcher3.BubbleTextView;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070025import com.android.launcher3.ButtonDropTarget;
Sunny Goyal83a8f042015-05-19 12:52:12 -070026import com.android.launcher3.CellLayout;
Sunny Goyal94b510c2016-08-16 15:36:48 -070027import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal83a8f042015-05-19 12:52:12 -070028import com.android.launcher3.FolderInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070029import com.android.launcher3.ItemInfo;
30import com.android.launcher3.Launcher;
Sunny Goyal83a8f042015-05-19 12:52:12 -070031import com.android.launcher3.LauncherAppWidgetInfo;
Sunny Goyal83a8f042015-05-19 12:52:12 -070032import com.android.launcher3.LauncherSettings;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070033import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal83a8f042015-05-19 12:52:12 -070034import com.android.launcher3.PendingAddItemInfo;
35import com.android.launcher3.R;
Sunny Goyal83a8f042015-05-19 12:52:12 -070036import com.android.launcher3.Workspace;
Pinyao Ting8a739f92019-09-12 11:56:18 -070037import com.android.launcher3.WorkspaceItemInfo;
Vadim Tryshevfedca432015-08-19 17:55:02 -070038import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyala52ecb02016-12-16 15:04:51 -080039import com.android.launcher3.dragndrop.DragOptions;
40import com.android.launcher3.folder.Folder;
Pinyao Ting8a739f92019-09-12 11:56:18 -070041import com.android.launcher3.keyboard.CustomActionsPopup;
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;
Winson Chung1054d4e2018-03-05 19:39:21 +000059 public static final int RECONFIGURE = R.id.action_reconfigure;
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070060 protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
61 protected static final int MOVE = R.id.action_move;
62 protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
63 protected static final int RESIZE = R.id.action_resize;
Sunny Goyal66b24572016-09-21 15:57:55 -070064 public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
Vadim Trysheva50d99f2018-05-04 17:58:29 -070065 public static final int SHORTCUTS_AND_NOTIFICATIONS = R.id.action_shortcuts_and_notifications;
Adam Cohenc9735cf2015-01-23 16:11:55 -080066
Sunny Goyale9b651e2015-04-24 11:44:51 -070067 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080068 ICON,
69 FOLDER,
70 WIDGET
71 }
72
73 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070074 public DragType dragType;
75 public ItemInfo info;
76 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080077 }
78
Sunny Goyal3ffa64d2016-07-25 13:54:32 -070079 protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070080 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080081
Sunny Goyale9b651e2015-04-24 11:44:51 -070082 private DragInfo mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -070083
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080084 public LauncherAccessibilityDelegate(Launcher launcher) {
85 mLauncher = launcher;
86
87 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
Tony Wickham9aae47f2015-10-01 13:04:22 -070088 launcher.getText(R.string.remove_drop_target_label)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080089 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
Tony Wickham9aae47f2015-10-01 13:04:22 -070090 launcher.getText(R.string.uninstall_drop_target_label)));
Samuel Fufa5cf3e862020-02-03 20:22:54 -080091 mActions.put(DISMISS_PREDICTION, new AccessibilityAction(DISMISS_PREDICTION,
92 launcher.getText(R.string.dismiss_prediction_label)));
Winson Chung1054d4e2018-03-05 19:39:21 +000093 mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE,
94 launcher.getText(R.string.gadget_setup_text)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080095 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
96 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080097 mActions.put(MOVE, new AccessibilityAction(MOVE,
98 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070099 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
100 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700101 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
102 launcher.getText(R.string.action_resize)));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700103 mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
104 launcher.getText(R.string.action_deep_shortcut)));
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700105 mActions.put(SHORTCUTS_AND_NOTIFICATIONS, new AccessibilityAction(DEEP_SHORTCUTS,
106 launcher.getText(R.string.shortcuts_menu_with_notifications_description)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800107 }
108
Mehdi Alizadeh8588fd02018-04-27 14:08:04 -0700109 public void addAccessibilityAction(int action, int actionLabel) {
110 mActions.put(action, new AccessibilityAction(action, mLauncher.getText(actionLabel)));
111 }
112
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800113 @Override
114 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
115 super.onInitializeAccessibilityNodeInfo(host, info);
Sunny Goyal66b24572016-09-21 15:57:55 -0700116 addSupportedActions(host, info, false);
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700117 }
118
Sunny Goyal66b24572016-09-21 15:57:55 -0700119 public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800120 if (!(host.getTag() instanceof ItemInfo)) return;
121 ItemInfo item = (ItemInfo) host.getTag();
122
Sunny Goyal66b24572016-09-21 15:57:55 -0700123 // If the request came from keyboard, do not add custom shortcuts as that is already
124 // exposed as a direct shortcut
Pinyao Ting49a3e692019-07-26 12:28:38 -0700125 if (!fromKeyboard && ShortcutUtil.supportsShortcuts(item)) {
Vadim Trysheva50d99f2018-05-04 17:58:29 -0700126 info.addAction(mActions.get(NotificationListener.getInstanceIfConnected() != null
127 ? SHORTCUTS_AND_NOTIFICATIONS : DEEP_SHORTCUTS));
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700128 }
129
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700130 for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000131 if (target.supportsAccessibilityDrop(item, host)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700132 info.addAction(mActions.get(target.getAccessibilityAction()));
133 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700134 }
135
Sunny Goyal66b24572016-09-21 15:57:55 -0700136 // Do not add move actions for keyboard request as this uses virtual nodes.
vadimt5bc87ec2019-02-07 19:44:08 -0800137 if (!fromKeyboard && itemSupportsAccessibleDrag(item)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800138 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700139
140 if (item.container >= 0) {
141 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700142 } else if (item instanceof LauncherAppWidgetInfo) {
143 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
144 info.addAction(mActions.get(RESIZE));
145 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700146 }
Adam Cohen6e92f052016-06-07 14:30:10 -0700147 }
148
Jon Mirandacafe9182020-03-25 11:46:11 -0700149 if (!fromKeyboard && !itemSupportsLongClick(host, item)) {
150 info.setLongClickable(false);
151 info.removeAction(AccessibilityAction.ACTION_LONG_CLICK);
152 }
153
Adam Cohen6e92f052016-06-07 14:30:10 -0700154 if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155 info.addAction(mActions.get(ADD_TO_WORKSPACE));
156 }
157 }
158
Jon Mirandacafe9182020-03-25 11:46:11 -0700159 private boolean itemSupportsLongClick(View host, ItemInfo info) {
Jon Mirandaff656742020-03-31 19:25:48 -0700160 return PopupContainerWithArrow.canShow(host, info)
161 || new CustomActionsPopup(mLauncher, host).canShow();
Jon Mirandacafe9182020-03-25 11:46:11 -0700162 }
163
vadimt5bc87ec2019-02-07 19:44:08 -0800164 private boolean itemSupportsAccessibleDrag(ItemInfo item) {
Sunny Goyal95899162019-03-27 16:03:06 -0700165 if (item instanceof WorkspaceItemInfo) {
vadimt5bc87ec2019-02-07 19:44:08 -0800166 // Support the action unless the item is in a context menu.
167 return item.screenId >= 0;
168 }
169 return (item instanceof LauncherAppWidgetInfo)
170 || (item instanceof FolderInfo);
171 }
172
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800173 @Override
174 public boolean performAccessibilityAction(View host, int action, Bundle args) {
175 if ((host.getTag() instanceof ItemInfo)
176 && performAction(host, (ItemInfo) host.getTag(), action)) {
177 return true;
178 }
179 return super.performAccessibilityAction(host, action, args);
180 }
181
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700182 public boolean performAction(final View host, final ItemInfo item, int action) {
Adam Cohen732b1d42020-01-30 10:46:52 -0800183 if (action == ACTION_LONG_CLICK) {
Jon Mirandaff656742020-03-31 19:25:48 -0700184 if (PopupContainerWithArrow.canShow(host, item)) {
Adam Cohen732b1d42020-01-30 10:46:52 -0800185 // Long press should be consumed for workspace items, and it should invoke the
186 // Shortcuts / Notifications / Actions pop-up menu, and not start a drag as the
187 // standard long press path does.
188 PopupContainerWithArrow.showForIcon((BubbleTextView) host);
Pinyao Ting8a739f92019-09-12 11:56:18 -0700189 return true;
Jon Mirandacafe9182020-03-25 11:46:11 -0700190 } else {
191 CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host);
192 if (popup.canShow()) {
193 popup.show();
194 return true;
195 }
Pinyao Ting8a739f92019-09-12 11:56:18 -0700196 }
197 }
Adam Cohen732b1d42020-01-30 10:46:52 -0800198
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700199 if (action == MOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800200 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800201 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700202 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700203 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700204 mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800205
206 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700207 public void run() {
208 if (item instanceof AppInfo) {
Sunny Goyal95899162019-03-27 16:03:06 -0700209 WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800210 mLauncher.getModelWriter().addItemToDatabase(info,
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700211 Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700212 screenId, coordinates[0], coordinates[1]);
213
214 ArrayList<ItemInfo> itemList = new ArrayList<>();
215 itemList.add(info);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700216 mLauncher.bindItems(itemList, true);
Pinyao Tingc9074272019-11-11 11:51:44 -0800217 announceConfirmation(R.string.item_added_to_workspace);
Sunny Goyala9116722015-04-29 13:55:58 -0700218 } else if (item instanceof PendingAddItemInfo) {
219 PendingAddItemInfo info = (PendingAddItemInfo) item;
220 Workspace workspace = mLauncher.getWorkspace();
221 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700222 mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP,
Sunny Goyala9116722015-04-29 13:55:58 -0700223 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800224 }
Sunny Goyala9116722015-04-29 13:55:58 -0700225 }
226 });
227 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700228 } else if (action == MOVE_TO_WORKSPACE) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700229 Folder folder = Folder.getOpen(mLauncher);
230 folder.close(true);
Sunny Goyal95899162019-03-27 16:03:06 -0700231 WorkspaceItemInfo info = (WorkspaceItemInfo) item;
Sunny Goyalc52ba712016-04-05 15:59:05 -0700232 folder.getInfo().remove(info, false);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700233
234 final int[] coordinates = new int[2];
Sunny Goyalefb7e842018-10-04 15:11:00 -0700235 final int screenId = findSpaceOnWorkspace(item, coordinates);
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800236 mLauncher.getModelWriter().moveItemInDatabase(info,
Sunny Goyal9ae77772015-04-29 16:30:23 -0700237 LauncherSettings.Favorites.CONTAINER_DESKTOP,
238 screenId, coordinates[0], coordinates[1]);
239
240 // Bind the item in next frame so that if a new workspace page was created,
241 // it will get laid out.
242 new Handler().post(new Runnable() {
243
244 @Override
245 public void run() {
246 ArrayList<ItemInfo> itemList = new ArrayList<>();
247 itemList.add(item);
Sunny Goyalb23980c2017-08-17 07:45:25 -0700248 mLauncher.bindItems(itemList, true);
Sunny Goyal9ae77772015-04-29 16:30:23 -0700249 announceConfirmation(R.string.item_moved);
250 }
251 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700252 } else if (action == RESIZE) {
253 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700254 final IntArray actions = getSupportedResizeActions(host, info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700255 CharSequence[] labels = new CharSequence[actions.size()];
256 for (int i = 0; i < actions.size(); i++) {
257 labels[i] = mLauncher.getText(actions.get(i));
258 }
259
260 new AlertDialog.Builder(mLauncher)
261 .setTitle(R.string.action_resize)
262 .setItems(labels, new DialogInterface.OnClickListener() {
263
264 @Override
265 public void onClick(DialogInterface dialog, int which) {
266 performResizeAction(actions.get(which), host, info);
267 dialog.dismiss();
268 }
269 })
270 .show();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700271 return true;
272 } else if (action == DEEP_SHORTCUTS) {
Tony Wickham540913e2017-01-23 11:47:51 -0800273 return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null;
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700274 } else {
275 for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) {
Winson Chung1054d4e2018-03-05 19:39:21 +0000276 if (dropTarget.supportsAccessibilityDrop(item, host) &&
277 action == dropTarget.getAccessibilityAction()) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700278 dropTarget.onAccessibilityDrop(host, item);
279 return true;
280 }
281 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800282 }
283 return false;
284 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800285
Sunny Goyalefb7e842018-10-04 15:11:00 -0700286 private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
287 IntArray actions = new IntArray();
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700288
Sunny Goyald3d8c952015-06-01 10:09:06 -0700289 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
290 if (providerInfo == null) {
291 return actions;
292 }
293
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700294 CellLayout layout = (CellLayout) host.getParent().getParent();
295 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
296 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
297 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
298 actions.add(R.string.action_increase_width);
299 }
300
301 if (info.spanX > info.minSpanX && info.spanX > 1) {
302 actions.add(R.string.action_decrease_width);
303 }
304 }
305
306 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
307 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
308 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
309 actions.add(R.string.action_increase_height);
310 }
311
312 if (info.spanY > info.minSpanY && info.spanY > 1) {
313 actions.add(R.string.action_decrease_height);
314 }
315 }
316 return actions;
317 }
318
Sunny Goyal316490e2015-06-02 09:38:28 -0700319 @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700320 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
321 CellLayout layout = (CellLayout) host.getParent().getParent();
322 layout.markCellsAsUnoccupiedForView(host);
323
324 if (action == R.string.action_increase_width) {
325 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
326 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
327 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
328 lp.cellX --;
329 info.cellX --;
330 }
331 lp.cellHSpan ++;
332 info.spanX ++;
333 } else if (action == R.string.action_decrease_width) {
334 lp.cellHSpan --;
335 info.spanX --;
336 } else if (action == R.string.action_increase_height) {
337 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
338 lp.cellY --;
339 info.cellY --;
340 }
341 lp.cellVSpan ++;
342 info.spanY ++;
343 } else if (action == R.string.action_decrease_height) {
344 lp.cellVSpan --;
345 info.spanY --;
346 }
347
348 layout.markCellsAsOccupiedForView(host);
349 Rect sizeRange = new Rect();
350 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
351 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
352 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
353 host.requestLayout();
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800354 mLauncher.getModelWriter().updateItemInDatabase(info);
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700355 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
356 }
357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800359 announceConfirmation(mLauncher.getResources().getString(resId));
360 }
361
Adam Cohen091440a2015-03-18 14:16:05 -0700362 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800363 mLauncher.getDragLayer().announceForAccessibility(confirmation);
364
365 }
366
367 public boolean isInAccessibleDrag() {
368 return mDragInfo != null;
369 }
370
371 public DragInfo getDragInfo() {
372 return mDragInfo;
373 }
374
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700375 /**
376 * @param clickedTarget the actual view that was clicked
377 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
378 * as the actual drop location otherwise the views center is used.
379 */
380 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800381 String confirmation) {
382 if (!isInAccessibleDrag()) return;
383
384 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700385 if (dropLocation == null) {
386 loc[0] = clickedTarget.getWidth() / 2;
387 loc[1] = clickedTarget.getHeight() / 2;
388 } else {
389 loc[0] = dropLocation.centerX();
390 loc[1] = dropLocation.centerY();
391 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800392
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700393 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800394 mLauncher.getDragController().completeAccessibleDrag(loc);
395
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700396 if (!TextUtils.isEmpty(confirmation)) {
397 announceConfirmation(confirmation);
398 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800399 }
400
401 public void beginAccessibleDrag(View item, ItemInfo info) {
402 mDragInfo = new DragInfo();
403 mDragInfo.info = info;
404 mDragInfo.item = item;
405 mDragInfo.dragType = DragType.ICON;
406 if (info instanceof FolderInfo) {
407 mDragInfo.dragType = DragType.FOLDER;
408 } else if (info instanceof LauncherAppWidgetInfo) {
409 mDragInfo.dragType = DragType.WIDGET;
410 }
411
Adam Cohenc9735cf2015-01-23 16:11:55 -0800412 Rect pos = new Rect();
413 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700414 mLauncher.getDragController().addDragListener(this);
415
416 DragOptions options = new DragOptions();
417 options.isAccessibleDrag = true;
Sunny Goyal9b180102020-03-11 10:02:29 -0700418 options.simulatedDndStartPoint = new Point(pos.centerX(), pos.centerY());
Sunny Goyal8a2a63b2018-03-06 22:15:18 -0800419 ItemLongClickListener.beginDrag(item, mLauncher, info, options);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800420 }
421
Sunny Goyal45478022015-06-08 16:52:41 -0700422 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700423 public void onDragStart(DragObject dragObject, DragOptions options) {
Sunny Goyal45478022015-06-08 16:52:41 -0700424 // No-op
Adam Cohenc9735cf2015-01-23 16:11:55 -0800425 }
426
Sunny Goyal45478022015-06-08 16:52:41 -0700427 @Override
428 public void onDragEnd() {
429 mLauncher.getDragController().removeDragListener(this);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800430 mDragInfo = null;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800431 }
Sunny Goyala9116722015-04-29 13:55:58 -0700432
433 /**
434 * Find empty space on the workspace and returns the screenId.
435 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700436 protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
Sunny Goyala9116722015-04-29 13:55:58 -0700437 Workspace workspace = mLauncher.getWorkspace();
Sunny Goyalefb7e842018-10-04 15:11:00 -0700438 IntArray workspaceScreens = workspace.getScreenOrder();
439 int screenId;
Sunny Goyala9116722015-04-29 13:55:58 -0700440
441 // First check if there is space on the current screen.
442 int screenIndex = workspace.getCurrentPage();
443 screenId = workspaceScreens.get(screenIndex);
444 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
445
446 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
Sunny Goyal7ce471b2017-08-02 03:37:39 -0700447 screenIndex = 0;
Sunny Goyala9116722015-04-29 13:55:58 -0700448 while (!found && screenIndex < workspaceScreens.size()) {
449 screenId = workspaceScreens.get(screenIndex);
450 layout = (CellLayout) workspace.getPageAt(screenIndex);
451 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
452 screenIndex++;
453 }
454
455 if (found) {
456 return screenId;
457 }
458
459 workspace.addExtraEmptyScreen();
460 screenId = workspace.commitExtraEmptyScreen();
461 layout = workspace.getScreenWithId(screenId);
462 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
463
464 if (!found) {
465 Log.wtf(TAG, "Not enough space on an empty screen");
466 }
467 return screenId;
468 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800469}