Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 1 | package com.android.launcher3.accessibility; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 2 | |
Pinyao Ting | 8a739f9 | 2019-09-12 11:56:18 -0700 | [diff] [blame] | 3 | import static android.view.accessibility.AccessibilityNodeInfo.ACTION_LONG_CLICK; |
| 4 | |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 5 | import static com.android.launcher3.LauncherState.NORMAL; |
| 6 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 7 | import android.app.AlertDialog; |
| 8 | import android.appwidget.AppWidgetProviderInfo; |
| 9 | import android.content.DialogInterface; |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 10 | import android.graphics.Point; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 11 | import android.graphics.Rect; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 12 | import android.os.Bundle; |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 13 | import android.os.Handler; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 14 | import android.text.TextUtils; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 15 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 16 | import android.util.SparseArray; |
| 17 | import android.view.View; |
| 18 | import android.view.View.AccessibilityDelegate; |
| 19 | import android.view.accessibility.AccessibilityNodeInfo; |
| 20 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
| 21 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 22 | import com.android.launcher3.AppWidgetResizeFrame; |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 23 | import com.android.launcher3.BubbleTextView; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 24 | import com.android.launcher3.ButtonDropTarget; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 25 | import com.android.launcher3.CellLayout; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 26 | import com.android.launcher3.DropTarget.DragObject; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 27 | import com.android.launcher3.Launcher; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 28 | import com.android.launcher3.LauncherSettings; |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 29 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 30 | import com.android.launcher3.PendingAddItemInfo; |
| 31 | import com.android.launcher3.R; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 32 | import com.android.launcher3.Workspace; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 33 | import com.android.launcher3.dragndrop.DragController.DragListener; |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 34 | import com.android.launcher3.dragndrop.DragOptions; |
| 35 | import com.android.launcher3.folder.Folder; |
Pinyao Ting | 8a739f9 | 2019-09-12 11:56:18 -0700 | [diff] [blame] | 36 | import com.android.launcher3.keyboard.CustomActionsPopup; |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame^] | 37 | import com.android.launcher3.keyboard.KeyboardDragAndDropView; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 38 | import com.android.launcher3.model.data.AppInfo; |
| 39 | import com.android.launcher3.model.data.FolderInfo; |
| 40 | import com.android.launcher3.model.data.ItemInfo; |
| 41 | import com.android.launcher3.model.data.LauncherAppWidgetInfo; |
| 42 | import com.android.launcher3.model.data.WorkspaceItemInfo; |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 43 | import com.android.launcher3.notification.NotificationListener; |
Mario Bertschler | c06af33 | 2017-03-28 12:23:22 -0700 | [diff] [blame] | 44 | import com.android.launcher3.popup.PopupContainerWithArrow; |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 45 | import com.android.launcher3.touch.ItemLongClickListener; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 46 | import com.android.launcher3.util.IntArray; |
Pinyao Ting | 49a3e69 | 2019-07-26 12:28:38 -0700 | [diff] [blame] | 47 | import com.android.launcher3.util.ShortcutUtil; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 48 | import com.android.launcher3.util.Thunk; |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 49 | import com.android.launcher3.widget.LauncherAppWidgetHostView; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 50 | |
| 51 | import java.util.ArrayList; |
| 52 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 53 | public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 54 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 55 | private static final String TAG = "LauncherAccessibilityDelegate"; |
| 56 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 57 | public static final int REMOVE = R.id.action_remove; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 58 | public static final int UNINSTALL = R.id.action_uninstall; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 59 | public static final int DISMISS_PREDICTION = R.id.action_dismiss_prediction; |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame] | 60 | public static final int PIN_PREDICTION = R.id.action_pin_prediction; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 61 | public static final int RECONFIGURE = R.id.action_reconfigure; |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 62 | 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 Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 66 | public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts; |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 67 | public static final int SHORTCUTS_AND_NOTIFICATIONS = R.id.action_shortcuts_and_notifications; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 68 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 69 | public enum DragType { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 70 | ICON, |
| 71 | FOLDER, |
| 72 | WIDGET |
| 73 | } |
| 74 | |
| 75 | public static class DragInfo { |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 76 | public DragType dragType; |
| 77 | public ItemInfo info; |
| 78 | public View item; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 81 | protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>(); |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 82 | @Thunk final Launcher mLauncher; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 83 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 84 | private DragInfo mDragInfo = null; |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 85 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 86 | public LauncherAccessibilityDelegate(Launcher launcher) { |
| 87 | mLauncher = launcher; |
| 88 | |
| 89 | mActions.put(REMOVE, new AccessibilityAction(REMOVE, |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 90 | launcher.getText(R.string.remove_drop_target_label))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 91 | mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL, |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 92 | launcher.getText(R.string.uninstall_drop_target_label))); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 93 | mActions.put(DISMISS_PREDICTION, new AccessibilityAction(DISMISS_PREDICTION, |
| 94 | launcher.getText(R.string.dismiss_prediction_label))); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 95 | mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE, |
| 96 | launcher.getText(R.string.gadget_setup_text))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 97 | mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE, |
| 98 | launcher.getText(R.string.action_add_to_workspace))); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 99 | mActions.put(MOVE, new AccessibilityAction(MOVE, |
| 100 | launcher.getText(R.string.action_move))); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 101 | mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE, |
| 102 | launcher.getText(R.string.action_move_to_workspace))); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 103 | mActions.put(RESIZE, new AccessibilityAction(RESIZE, |
| 104 | launcher.getText(R.string.action_resize))); |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 105 | mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS, |
| 106 | launcher.getText(R.string.action_deep_shortcut))); |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 107 | mActions.put(SHORTCUTS_AND_NOTIFICATIONS, new AccessibilityAction(DEEP_SHORTCUTS, |
| 108 | launcher.getText(R.string.shortcuts_menu_with_notifications_description))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 113 | super.onInitializeAccessibilityNodeInfo(host, info); |
Sunny Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 114 | addSupportedActions(host, info, false); |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Sunny Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 117 | public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 118 | if (!(host.getTag() instanceof ItemInfo)) return; |
| 119 | ItemInfo item = (ItemInfo) host.getTag(); |
| 120 | |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame] | 121 | if (host instanceof AccessibilityActionHandler) { |
| 122 | ((AccessibilityActionHandler) host).addSupportedAccessibilityActions(info); |
| 123 | } |
| 124 | |
Sunny Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 125 | // If the request came from keyboard, do not add custom shortcuts as that is already |
| 126 | // exposed as a direct shortcut |
Pinyao Ting | 49a3e69 | 2019-07-26 12:28:38 -0700 | [diff] [blame] | 127 | if (!fromKeyboard && ShortcutUtil.supportsShortcuts(item)) { |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 128 | info.addAction(mActions.get(NotificationListener.getInstanceIfConnected() != null |
| 129 | ? SHORTCUTS_AND_NOTIFICATIONS : DEEP_SHORTCUTS)); |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 132 | for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 133 | if (target.supportsAccessibilityDrop(item, host)) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 134 | info.addAction(mActions.get(target.getAccessibilityAction())); |
| 135 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Sunny Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 138 | // Do not add move actions for keyboard request as this uses virtual nodes. |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame^] | 139 | if (itemSupportsAccessibleDrag(item)) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 140 | info.addAction(mActions.get(MOVE)); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 141 | |
| 142 | if (item.container >= 0) { |
| 143 | info.addAction(mActions.get(MOVE_TO_WORKSPACE)); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 144 | } else if (item instanceof LauncherAppWidgetInfo) { |
| 145 | if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) { |
| 146 | info.addAction(mActions.get(RESIZE)); |
| 147 | } |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 148 | } |
Adam Cohen | 6e92f05 | 2016-06-07 14:30:10 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jon Miranda | cafe918 | 2020-03-25 11:46:11 -0700 | [diff] [blame] | 151 | if (!fromKeyboard && !itemSupportsLongClick(host, item)) { |
| 152 | info.setLongClickable(false); |
| 153 | info.removeAction(AccessibilityAction.ACTION_LONG_CLICK); |
| 154 | } |
| 155 | |
Adam Cohen | 6e92f05 | 2016-06-07 14:30:10 -0700 | [diff] [blame] | 156 | if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 157 | info.addAction(mActions.get(ADD_TO_WORKSPACE)); |
| 158 | } |
| 159 | } |
| 160 | |
Jon Miranda | cafe918 | 2020-03-25 11:46:11 -0700 | [diff] [blame] | 161 | private boolean itemSupportsLongClick(View host, ItemInfo info) { |
Jon Miranda | ff65674 | 2020-03-31 19:25:48 -0700 | [diff] [blame] | 162 | return PopupContainerWithArrow.canShow(host, info) |
| 163 | || new CustomActionsPopup(mLauncher, host).canShow(); |
Jon Miranda | cafe918 | 2020-03-25 11:46:11 -0700 | [diff] [blame] | 164 | } |
| 165 | |
vadimt | 5bc87ec | 2019-02-07 19:44:08 -0800 | [diff] [blame] | 166 | private boolean itemSupportsAccessibleDrag(ItemInfo item) { |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 167 | if (item instanceof WorkspaceItemInfo) { |
vadimt | 5bc87ec | 2019-02-07 19:44:08 -0800 | [diff] [blame] | 168 | // Support the action unless the item is in a context menu. |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame] | 169 | return item.screenId >= 0 && item.container != Favorites.CONTAINER_HOTSEAT_PREDICTION; |
vadimt | 5bc87ec | 2019-02-07 19:44:08 -0800 | [diff] [blame] | 170 | } |
| 171 | return (item instanceof LauncherAppWidgetInfo) |
| 172 | || (item instanceof FolderInfo); |
| 173 | } |
| 174 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 175 | @Override |
| 176 | public boolean performAccessibilityAction(View host, int action, Bundle args) { |
| 177 | if ((host.getTag() instanceof ItemInfo) |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame^] | 178 | && performAction(host, (ItemInfo) host.getTag(), action, false)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | return super.performAccessibilityAction(host, action, args); |
| 182 | } |
| 183 | |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame^] | 184 | /** |
| 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 Cohen | 732b1d4 | 2020-01-30 10:46:52 -0800 | [diff] [blame] | 189 | if (action == ACTION_LONG_CLICK) { |
Jon Miranda | ff65674 | 2020-03-31 19:25:48 -0700 | [diff] [blame] | 190 | if (PopupContainerWithArrow.canShow(host, item)) { |
Adam Cohen | 732b1d4 | 2020-01-30 10:46:52 -0800 | [diff] [blame] | 191 | // 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 Ting | 8a739f9 | 2019-09-12 11:56:18 -0700 | [diff] [blame] | 195 | return true; |
Jon Miranda | cafe918 | 2020-03-25 11:46:11 -0700 | [diff] [blame] | 196 | } else { |
| 197 | CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host); |
| 198 | if (popup.canShow()) { |
| 199 | popup.show(); |
| 200 | return true; |
| 201 | } |
Pinyao Ting | 8a739f9 | 2019-09-12 11:56:18 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame] | 204 | if (host instanceof AccessibilityActionHandler |
| 205 | && ((AccessibilityActionHandler) host).performAccessibilityAction(action, item)) { |
| 206 | return true; |
| 207 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 208 | if (action == MOVE) { |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame^] | 209 | return beginAccessibleDrag(host, item, fromKeyboard); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 210 | } else if (action == ADD_TO_WORKSPACE) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 211 | final int[] coordinates = new int[2]; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 212 | final int screenId = findSpaceOnWorkspace(item, coordinates); |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 213 | mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 214 | |
| 215 | @Override |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 216 | public void run() { |
| 217 | if (item instanceof AppInfo) { |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 218 | WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem(); |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 219 | mLauncher.getModelWriter().addItemToDatabase(info, |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 220 | Favorites.CONTAINER_DESKTOP, |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 221 | screenId, coordinates[0], coordinates[1]); |
| 222 | |
| 223 | ArrayList<ItemInfo> itemList = new ArrayList<>(); |
| 224 | itemList.add(info); |
Sunny Goyal | b23980c | 2017-08-17 07:45:25 -0700 | [diff] [blame] | 225 | mLauncher.bindItems(itemList, true); |
Pinyao Ting | c907427 | 2019-11-11 11:51:44 -0800 | [diff] [blame] | 226 | announceConfirmation(R.string.item_added_to_workspace); |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 227 | } else if (item instanceof PendingAddItemInfo) { |
| 228 | PendingAddItemInfo info = (PendingAddItemInfo) item; |
| 229 | Workspace workspace = mLauncher.getWorkspace(); |
| 230 | workspace.snapToPage(workspace.getPageIndexForScreenId(screenId)); |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 231 | mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP, |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 232 | screenId, coordinates, info.spanX, info.spanY); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 233 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 234 | } |
| 235 | }); |
| 236 | return true; |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 237 | } else if (action == MOVE_TO_WORKSPACE) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 238 | Folder folder = Folder.getOpen(mLauncher); |
| 239 | folder.close(true); |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 240 | WorkspaceItemInfo info = (WorkspaceItemInfo) item; |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 241 | folder.getInfo().remove(info, false); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 242 | |
| 243 | final int[] coordinates = new int[2]; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 244 | final int screenId = findSpaceOnWorkspace(item, coordinates); |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 245 | mLauncher.getModelWriter().moveItemInDatabase(info, |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 246 | 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 Goyal | b23980c | 2017-08-17 07:45:25 -0700 | [diff] [blame] | 257 | mLauncher.bindItems(itemList, true); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 258 | announceConfirmation(R.string.item_moved); |
| 259 | } |
| 260 | }); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 261 | } else if (action == RESIZE) { |
| 262 | final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 263 | final IntArray actions = getSupportedResizeActions(host, info); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 264 | 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 Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 280 | return true; |
| 281 | } else if (action == DEEP_SHORTCUTS) { |
Tony Wickham | 540913e | 2017-01-23 11:47:51 -0800 | [diff] [blame] | 282 | return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 283 | } else { |
| 284 | for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 285 | if (dropTarget.supportsAccessibilityDrop(item, host) && |
| 286 | action == dropTarget.getAccessibilityAction()) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 287 | dropTarget.onAccessibilityDrop(host, item); |
| 288 | return true; |
| 289 | } |
| 290 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 291 | } |
| 292 | return false; |
| 293 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 294 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 295 | private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) { |
| 296 | IntArray actions = new IntArray(); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 297 | |
Sunny Goyal | d3d8c95 | 2015-06-01 10:09:06 -0700 | [diff] [blame] | 298 | AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo(); |
| 299 | if (providerInfo == null) { |
| 300 | return actions; |
| 301 | } |
| 302 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 303 | 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 Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 328 | @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) { |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 329 | 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 Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 363 | mLauncher.getModelWriter().updateItemInDatabase(info); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 364 | announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY)); |
| 365 | } |
| 366 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 367 | @Thunk void announceConfirmation(int resId) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 368 | announceConfirmation(mLauncher.getResources().getString(resId)); |
| 369 | } |
| 370 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 371 | @Thunk void announceConfirmation(String confirmation) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 372 | 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 Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 384 | /** |
| 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 390 | String confirmation) { |
| 391 | if (!isInAccessibleDrag()) return; |
| 392 | |
| 393 | int[] loc = new int[2]; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 394 | 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 401 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 402 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 403 | mLauncher.getDragController().completeAccessibleDrag(loc); |
| 404 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 405 | if (!TextUtils.isEmpty(confirmation)) { |
| 406 | announceConfirmation(confirmation); |
| 407 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame^] | 410 | private boolean beginAccessibleDrag(View item, ItemInfo info, boolean fromKeyboard) { |
| 411 | if (!itemSupportsAccessibleDrag(info)) { |
| 412 | return false; |
| 413 | } |
| 414 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 415 | 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 425 | Rect pos = new Rect(); |
| 426 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos); |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 427 | mLauncher.getDragController().addDragListener(this); |
| 428 | |
| 429 | DragOptions options = new DragOptions(); |
| 430 | options.isAccessibleDrag = true; |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame^] | 431 | options.isKeyboardDrag = fromKeyboard; |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 432 | options.simulatedDndStartPoint = new Point(pos.centerX(), pos.centerY()); |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame^] | 433 | |
| 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 444 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 445 | public void onDragStart(DragObject dragObject, DragOptions options) { |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 446 | // No-op |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 449 | @Override |
| 450 | public void onDragEnd() { |
| 451 | mLauncher.getDragController().removeDragListener(this); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 452 | mDragInfo = null; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 453 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 454 | |
| 455 | /** |
| 456 | * Find empty space on the workspace and returns the screenId. |
| 457 | */ |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 458 | protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 459 | Workspace workspace = mLauncher.getWorkspace(); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 460 | IntArray workspaceScreens = workspace.getScreenOrder(); |
| 461 | int screenId; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 462 | |
| 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 Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 469 | screenIndex = 0; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 470 | 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 Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame] | 491 | |
| 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 Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 507 | } |