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.AppInfo; |
| 23 | import com.android.launcher3.AppWidgetResizeFrame; |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 24 | import com.android.launcher3.BubbleTextView; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 25 | import com.android.launcher3.ButtonDropTarget; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 26 | import com.android.launcher3.CellLayout; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 27 | import com.android.launcher3.DropTarget.DragObject; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 28 | import com.android.launcher3.FolderInfo; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 29 | import com.android.launcher3.ItemInfo; |
| 30 | import com.android.launcher3.Launcher; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 31 | import com.android.launcher3.LauncherAppWidgetInfo; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 32 | import com.android.launcher3.LauncherSettings; |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 33 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 34 | import com.android.launcher3.PendingAddItemInfo; |
| 35 | import com.android.launcher3.R; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 36 | import com.android.launcher3.Workspace; |
Pinyao Ting | 8a739f9 | 2019-09-12 11:56:18 -0700 | [diff] [blame] | 37 | import com.android.launcher3.WorkspaceItemInfo; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 38 | import com.android.launcher3.dragndrop.DragController.DragListener; |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 39 | import com.android.launcher3.dragndrop.DragOptions; |
| 40 | import com.android.launcher3.folder.Folder; |
Pinyao Ting | 8a739f9 | 2019-09-12 11:56:18 -0700 | [diff] [blame] | 41 | import com.android.launcher3.keyboard.CustomActionsPopup; |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 42 | import com.android.launcher3.notification.NotificationListener; |
Mario Bertschler | c06af33 | 2017-03-28 12:23:22 -0700 | [diff] [blame] | 43 | import com.android.launcher3.popup.PopupContainerWithArrow; |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 44 | import com.android.launcher3.touch.ItemLongClickListener; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 45 | import com.android.launcher3.util.IntArray; |
Pinyao Ting | 49a3e69 | 2019-07-26 12:28:38 -0700 | [diff] [blame] | 46 | import com.android.launcher3.util.ShortcutUtil; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 47 | import com.android.launcher3.util.Thunk; |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 48 | import com.android.launcher3.widget.LauncherAppWidgetHostView; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 49 | |
| 50 | import java.util.ArrayList; |
| 51 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 52 | public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 53 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 54 | private static final String TAG = "LauncherAccessibilityDelegate"; |
| 55 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 56 | public static final int REMOVE = R.id.action_remove; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 57 | public static final int UNINSTALL = R.id.action_uninstall; |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 58 | public static final int DISMISS_PREDICTION = R.id.action_dismiss_prediction; |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame^] | 59 | public static final int PIN_PREDICTION = R.id.action_pin_prediction; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 60 | public static final int RECONFIGURE = R.id.action_reconfigure; |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 61 | 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 Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 65 | public static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts; |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 66 | 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] | 67 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 68 | public enum DragType { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 69 | ICON, |
| 70 | FOLDER, |
| 71 | WIDGET |
| 72 | } |
| 73 | |
| 74 | public static class DragInfo { |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 75 | public DragType dragType; |
| 76 | public ItemInfo info; |
| 77 | public View item; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 80 | protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>(); |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 81 | @Thunk final Launcher mLauncher; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 82 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 83 | private DragInfo mDragInfo = null; |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 84 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 85 | public LauncherAccessibilityDelegate(Launcher launcher) { |
| 86 | mLauncher = launcher; |
| 87 | |
| 88 | mActions.put(REMOVE, new AccessibilityAction(REMOVE, |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 89 | launcher.getText(R.string.remove_drop_target_label))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 90 | mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL, |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 91 | launcher.getText(R.string.uninstall_drop_target_label))); |
Samuel Fufa | 5cf3e86 | 2020-02-03 20:22:54 -0800 | [diff] [blame] | 92 | mActions.put(DISMISS_PREDICTION, new AccessibilityAction(DISMISS_PREDICTION, |
| 93 | launcher.getText(R.string.dismiss_prediction_label))); |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 94 | mActions.put(RECONFIGURE, new AccessibilityAction(RECONFIGURE, |
| 95 | launcher.getText(R.string.gadget_setup_text))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 96 | mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE, |
| 97 | launcher.getText(R.string.action_add_to_workspace))); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 98 | mActions.put(MOVE, new AccessibilityAction(MOVE, |
| 99 | launcher.getText(R.string.action_move))); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 100 | mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE, |
| 101 | launcher.getText(R.string.action_move_to_workspace))); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 102 | mActions.put(RESIZE, new AccessibilityAction(RESIZE, |
| 103 | launcher.getText(R.string.action_resize))); |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 104 | mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS, |
| 105 | launcher.getText(R.string.action_deep_shortcut))); |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 106 | mActions.put(SHORTCUTS_AND_NOTIFICATIONS, new AccessibilityAction(DEEP_SHORTCUTS, |
| 107 | launcher.getText(R.string.shortcuts_menu_with_notifications_description))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Mehdi Alizadeh | 8588fd0 | 2018-04-27 14:08:04 -0700 | [diff] [blame] | 110 | public void addAccessibilityAction(int action, int actionLabel) { |
| 111 | mActions.put(action, new AccessibilityAction(action, mLauncher.getText(actionLabel))); |
| 112 | } |
| 113 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 114 | @Override |
| 115 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 116 | super.onInitializeAccessibilityNodeInfo(host, info); |
Sunny Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 117 | addSupportedActions(host, info, false); |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Sunny Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 120 | public void addSupportedActions(View host, AccessibilityNodeInfo info, boolean fromKeyboard) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 121 | if (!(host.getTag() instanceof ItemInfo)) return; |
| 122 | ItemInfo item = (ItemInfo) host.getTag(); |
| 123 | |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame^] | 124 | if (host instanceof AccessibilityActionHandler) { |
| 125 | ((AccessibilityActionHandler) host).addSupportedAccessibilityActions(info); |
| 126 | } |
| 127 | |
Sunny Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 128 | // If the request came from keyboard, do not add custom shortcuts as that is already |
| 129 | // exposed as a direct shortcut |
Pinyao Ting | 49a3e69 | 2019-07-26 12:28:38 -0700 | [diff] [blame] | 130 | if (!fromKeyboard && ShortcutUtil.supportsShortcuts(item)) { |
Vadim Tryshev | a50d99f | 2018-05-04 17:58:29 -0700 | [diff] [blame] | 131 | info.addAction(mActions.get(NotificationListener.getInstanceIfConnected() != null |
| 132 | ? SHORTCUTS_AND_NOTIFICATIONS : DEEP_SHORTCUTS)); |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 135 | for (ButtonDropTarget target : mLauncher.getDropTargetBar().getDropTargets()) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 136 | if (target.supportsAccessibilityDrop(item, host)) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 137 | info.addAction(mActions.get(target.getAccessibilityAction())); |
| 138 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Sunny Goyal | 66b2457 | 2016-09-21 15:57:55 -0700 | [diff] [blame] | 141 | // Do not add move actions for keyboard request as this uses virtual nodes. |
vadimt | 5bc87ec | 2019-02-07 19:44:08 -0800 | [diff] [blame] | 142 | if (!fromKeyboard && itemSupportsAccessibleDrag(item)) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 143 | info.addAction(mActions.get(MOVE)); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 144 | |
| 145 | if (item.container >= 0) { |
| 146 | info.addAction(mActions.get(MOVE_TO_WORKSPACE)); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 147 | } else if (item instanceof LauncherAppWidgetInfo) { |
| 148 | if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) { |
| 149 | info.addAction(mActions.get(RESIZE)); |
| 150 | } |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 151 | } |
Adam Cohen | 6e92f05 | 2016-06-07 14:30:10 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 155 | info.addAction(mActions.get(ADD_TO_WORKSPACE)); |
| 156 | } |
| 157 | } |
| 158 | |
vadimt | 5bc87ec | 2019-02-07 19:44:08 -0800 | [diff] [blame] | 159 | private boolean itemSupportsAccessibleDrag(ItemInfo item) { |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 160 | if (item instanceof WorkspaceItemInfo) { |
vadimt | 5bc87ec | 2019-02-07 19:44:08 -0800 | [diff] [blame] | 161 | // Support the action unless the item is in a context menu. |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame^] | 162 | return item.screenId >= 0 && item.container != Favorites.CONTAINER_HOTSEAT_PREDICTION; |
vadimt | 5bc87ec | 2019-02-07 19:44:08 -0800 | [diff] [blame] | 163 | } |
| 164 | return (item instanceof LauncherAppWidgetInfo) |
| 165 | || (item instanceof FolderInfo); |
| 166 | } |
| 167 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 168 | @Override |
| 169 | public boolean performAccessibilityAction(View host, int action, Bundle args) { |
| 170 | if ((host.getTag() instanceof ItemInfo) |
| 171 | && performAction(host, (ItemInfo) host.getTag(), action)) { |
| 172 | return true; |
| 173 | } |
| 174 | return super.performAccessibilityAction(host, action, args); |
| 175 | } |
| 176 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 177 | public boolean performAction(final View host, final ItemInfo item, int action) { |
Adam Cohen | 732b1d4 | 2020-01-30 10:46:52 -0800 | [diff] [blame] | 178 | if (action == ACTION_LONG_CLICK) { |
| 179 | if (ShortcutUtil.isDeepShortcut(item)) { |
| 180 | CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host); |
| 181 | if (popup.canShow()) { |
| 182 | popup.show(); |
| 183 | return true; |
| 184 | } |
| 185 | } else if (host instanceof BubbleTextView) { |
| 186 | // Long press should be consumed for workspace items, and it should invoke the |
| 187 | // Shortcuts / Notifications / Actions pop-up menu, and not start a drag as the |
| 188 | // standard long press path does. |
| 189 | PopupContainerWithArrow.showForIcon((BubbleTextView) host); |
Pinyao Ting | 8a739f9 | 2019-09-12 11:56:18 -0700 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | } |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame^] | 193 | if (host instanceof AccessibilityActionHandler |
| 194 | && ((AccessibilityActionHandler) host).performAccessibilityAction(action, item)) { |
| 195 | return true; |
| 196 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 197 | if (action == MOVE) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 198 | beginAccessibleDrag(host, item); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 199 | } else if (action == ADD_TO_WORKSPACE) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 200 | final int[] coordinates = new int[2]; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 201 | final int screenId = findSpaceOnWorkspace(item, coordinates); |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 202 | mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 203 | |
| 204 | @Override |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 205 | public void run() { |
| 206 | if (item instanceof AppInfo) { |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 207 | WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem(); |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 208 | mLauncher.getModelWriter().addItemToDatabase(info, |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 209 | Favorites.CONTAINER_DESKTOP, |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 210 | screenId, coordinates[0], coordinates[1]); |
| 211 | |
| 212 | ArrayList<ItemInfo> itemList = new ArrayList<>(); |
| 213 | itemList.add(info); |
Sunny Goyal | b23980c | 2017-08-17 07:45:25 -0700 | [diff] [blame] | 214 | mLauncher.bindItems(itemList, true); |
Pinyao Ting | c907427 | 2019-11-11 11:51:44 -0800 | [diff] [blame] | 215 | announceConfirmation(R.string.item_added_to_workspace); |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 216 | } else if (item instanceof PendingAddItemInfo) { |
| 217 | PendingAddItemInfo info = (PendingAddItemInfo) item; |
| 218 | Workspace workspace = mLauncher.getWorkspace(); |
| 219 | workspace.snapToPage(workspace.getPageIndexForScreenId(screenId)); |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 220 | mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP, |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 221 | screenId, coordinates, info.spanX, info.spanY); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 222 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 223 | } |
| 224 | }); |
| 225 | return true; |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 226 | } else if (action == MOVE_TO_WORKSPACE) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 227 | Folder folder = Folder.getOpen(mLauncher); |
| 228 | folder.close(true); |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 229 | WorkspaceItemInfo info = (WorkspaceItemInfo) item; |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 230 | folder.getInfo().remove(info, false); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 231 | |
| 232 | final int[] coordinates = new int[2]; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 233 | final int screenId = findSpaceOnWorkspace(item, coordinates); |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 234 | mLauncher.getModelWriter().moveItemInDatabase(info, |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 235 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 236 | screenId, coordinates[0], coordinates[1]); |
| 237 | |
| 238 | // Bind the item in next frame so that if a new workspace page was created, |
| 239 | // it will get laid out. |
| 240 | new Handler().post(new Runnable() { |
| 241 | |
| 242 | @Override |
| 243 | public void run() { |
| 244 | ArrayList<ItemInfo> itemList = new ArrayList<>(); |
| 245 | itemList.add(item); |
Sunny Goyal | b23980c | 2017-08-17 07:45:25 -0700 | [diff] [blame] | 246 | mLauncher.bindItems(itemList, true); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 247 | announceConfirmation(R.string.item_moved); |
| 248 | } |
| 249 | }); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 250 | } else if (action == RESIZE) { |
| 251 | final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 252 | final IntArray actions = getSupportedResizeActions(host, info); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 253 | CharSequence[] labels = new CharSequence[actions.size()]; |
| 254 | for (int i = 0; i < actions.size(); i++) { |
| 255 | labels[i] = mLauncher.getText(actions.get(i)); |
| 256 | } |
| 257 | |
| 258 | new AlertDialog.Builder(mLauncher) |
| 259 | .setTitle(R.string.action_resize) |
| 260 | .setItems(labels, new DialogInterface.OnClickListener() { |
| 261 | |
| 262 | @Override |
| 263 | public void onClick(DialogInterface dialog, int which) { |
| 264 | performResizeAction(actions.get(which), host, info); |
| 265 | dialog.dismiss(); |
| 266 | } |
| 267 | }) |
| 268 | .show(); |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 269 | return true; |
| 270 | } else if (action == DEEP_SHORTCUTS) { |
Tony Wickham | 540913e | 2017-01-23 11:47:51 -0800 | [diff] [blame] | 271 | return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 272 | } else { |
| 273 | for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 274 | if (dropTarget.supportsAccessibilityDrop(item, host) && |
| 275 | action == dropTarget.getAccessibilityAction()) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 276 | dropTarget.onAccessibilityDrop(host, item); |
| 277 | return true; |
| 278 | } |
| 279 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 280 | } |
| 281 | return false; |
| 282 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 283 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 284 | private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) { |
| 285 | IntArray actions = new IntArray(); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 286 | |
Sunny Goyal | d3d8c95 | 2015-06-01 10:09:06 -0700 | [diff] [blame] | 287 | AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo(); |
| 288 | if (providerInfo == null) { |
| 289 | return actions; |
| 290 | } |
| 291 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 292 | CellLayout layout = (CellLayout) host.getParent().getParent(); |
| 293 | if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) { |
| 294 | if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) || |
| 295 | layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) { |
| 296 | actions.add(R.string.action_increase_width); |
| 297 | } |
| 298 | |
| 299 | if (info.spanX > info.minSpanX && info.spanX > 1) { |
| 300 | actions.add(R.string.action_decrease_width); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) { |
| 305 | if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) || |
| 306 | layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) { |
| 307 | actions.add(R.string.action_increase_height); |
| 308 | } |
| 309 | |
| 310 | if (info.spanY > info.minSpanY && info.spanY > 1) { |
| 311 | actions.add(R.string.action_decrease_height); |
| 312 | } |
| 313 | } |
| 314 | return actions; |
| 315 | } |
| 316 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 317 | @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) { |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 318 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams(); |
| 319 | CellLayout layout = (CellLayout) host.getParent().getParent(); |
| 320 | layout.markCellsAsUnoccupiedForView(host); |
| 321 | |
| 322 | if (action == R.string.action_increase_width) { |
| 323 | if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) |
| 324 | && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) |
| 325 | || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) { |
| 326 | lp.cellX --; |
| 327 | info.cellX --; |
| 328 | } |
| 329 | lp.cellHSpan ++; |
| 330 | info.spanX ++; |
| 331 | } else if (action == R.string.action_decrease_width) { |
| 332 | lp.cellHSpan --; |
| 333 | info.spanX --; |
| 334 | } else if (action == R.string.action_increase_height) { |
| 335 | if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) { |
| 336 | lp.cellY --; |
| 337 | info.cellY --; |
| 338 | } |
| 339 | lp.cellVSpan ++; |
| 340 | info.spanY ++; |
| 341 | } else if (action == R.string.action_decrease_height) { |
| 342 | lp.cellVSpan --; |
| 343 | info.spanY --; |
| 344 | } |
| 345 | |
| 346 | layout.markCellsAsOccupiedForView(host); |
| 347 | Rect sizeRange = new Rect(); |
| 348 | AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange); |
| 349 | ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null, |
| 350 | sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom); |
| 351 | host.requestLayout(); |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 352 | mLauncher.getModelWriter().updateItemInDatabase(info); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 353 | announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY)); |
| 354 | } |
| 355 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 356 | @Thunk void announceConfirmation(int resId) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 357 | announceConfirmation(mLauncher.getResources().getString(resId)); |
| 358 | } |
| 359 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 360 | @Thunk void announceConfirmation(String confirmation) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 361 | mLauncher.getDragLayer().announceForAccessibility(confirmation); |
| 362 | |
| 363 | } |
| 364 | |
| 365 | public boolean isInAccessibleDrag() { |
| 366 | return mDragInfo != null; |
| 367 | } |
| 368 | |
| 369 | public DragInfo getDragInfo() { |
| 370 | return mDragInfo; |
| 371 | } |
| 372 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 373 | /** |
| 374 | * @param clickedTarget the actual view that was clicked |
| 375 | * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used |
| 376 | * as the actual drop location otherwise the views center is used. |
| 377 | */ |
| 378 | public void handleAccessibleDrop(View clickedTarget, Rect dropLocation, |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 379 | String confirmation) { |
| 380 | if (!isInAccessibleDrag()) return; |
| 381 | |
| 382 | int[] loc = new int[2]; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 383 | if (dropLocation == null) { |
| 384 | loc[0] = clickedTarget.getWidth() / 2; |
| 385 | loc[1] = clickedTarget.getHeight() / 2; |
| 386 | } else { |
| 387 | loc[0] = dropLocation.centerX(); |
| 388 | loc[1] = dropLocation.centerY(); |
| 389 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 390 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 391 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 392 | mLauncher.getDragController().completeAccessibleDrag(loc); |
| 393 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 394 | if (!TextUtils.isEmpty(confirmation)) { |
| 395 | announceConfirmation(confirmation); |
| 396 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | public void beginAccessibleDrag(View item, ItemInfo info) { |
| 400 | mDragInfo = new DragInfo(); |
| 401 | mDragInfo.info = info; |
| 402 | mDragInfo.item = item; |
| 403 | mDragInfo.dragType = DragType.ICON; |
| 404 | if (info instanceof FolderInfo) { |
| 405 | mDragInfo.dragType = DragType.FOLDER; |
| 406 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 407 | mDragInfo.dragType = DragType.WIDGET; |
| 408 | } |
| 409 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 410 | Rect pos = new Rect(); |
| 411 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos); |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 412 | mLauncher.getDragController().addDragListener(this); |
| 413 | |
| 414 | DragOptions options = new DragOptions(); |
| 415 | options.isAccessibleDrag = true; |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 416 | options.simulatedDndStartPoint = new Point(pos.centerX(), pos.centerY()); |
Sunny Goyal | 8a2a63b | 2018-03-06 22:15:18 -0800 | [diff] [blame] | 417 | ItemLongClickListener.beginDrag(item, mLauncher, info, options); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 420 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 421 | public void onDragStart(DragObject dragObject, DragOptions options) { |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 422 | // No-op |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 425 | @Override |
| 426 | public void onDragEnd() { |
| 427 | mLauncher.getDragController().removeDragListener(this); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 428 | mDragInfo = null; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 429 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 430 | |
| 431 | /** |
| 432 | * Find empty space on the workspace and returns the screenId. |
| 433 | */ |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 434 | protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 435 | Workspace workspace = mLauncher.getWorkspace(); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 436 | IntArray workspaceScreens = workspace.getScreenOrder(); |
| 437 | int screenId; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 438 | |
| 439 | // First check if there is space on the current screen. |
| 440 | int screenIndex = workspace.getCurrentPage(); |
| 441 | screenId = workspaceScreens.get(screenIndex); |
| 442 | CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex); |
| 443 | |
| 444 | boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 445 | screenIndex = 0; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 446 | while (!found && screenIndex < workspaceScreens.size()) { |
| 447 | screenId = workspaceScreens.get(screenIndex); |
| 448 | layout = (CellLayout) workspace.getPageAt(screenIndex); |
| 449 | found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 450 | screenIndex++; |
| 451 | } |
| 452 | |
| 453 | if (found) { |
| 454 | return screenId; |
| 455 | } |
| 456 | |
| 457 | workspace.addExtraEmptyScreen(); |
| 458 | screenId = workspace.commitExtraEmptyScreen(); |
| 459 | layout = workspace.getScreenWithId(screenId); |
| 460 | found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 461 | |
| 462 | if (!found) { |
| 463 | Log.wtf(TAG, "Not enough space on an empty screen"); |
| 464 | } |
| 465 | return screenId; |
| 466 | } |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame^] | 467 | |
| 468 | /** |
| 469 | * An interface allowing views to handle their own action. |
| 470 | */ |
| 471 | public interface AccessibilityActionHandler { |
| 472 | |
| 473 | /** |
| 474 | * performs accessibility action and returns true on success |
| 475 | */ |
| 476 | boolean performAccessibilityAction(int action, ItemInfo itemInfo); |
| 477 | |
| 478 | /** |
| 479 | * adds all the accessibility actions that can be handled. |
| 480 | */ |
| 481 | void addSupportedAccessibilityActions(AccessibilityNodeInfo accessibilityNodeInfo); |
| 482 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 483 | } |