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