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) { |
Pinyao Ting | 8a739f9 | 2019-09-12 11:56:18 -0700 | [diff] [blame^] | 169 | if (action == ACTION_LONG_CLICK && ShortcutUtil.isDeepShortcut(item)) { |
| 170 | CustomActionsPopup popup = new CustomActionsPopup(mLauncher, host); |
| 171 | if (popup.canShow()) { |
| 172 | popup.show(); |
| 173 | return true; |
| 174 | } |
| 175 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 176 | if (action == MOVE) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 177 | beginAccessibleDrag(host, item); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 178 | } else if (action == ADD_TO_WORKSPACE) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 179 | final int[] coordinates = new int[2]; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 180 | final int screenId = findSpaceOnWorkspace(item, coordinates); |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 181 | mLauncher.getStateManager().goToState(NORMAL, true, new Runnable() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 182 | |
| 183 | @Override |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 184 | public void run() { |
| 185 | if (item instanceof AppInfo) { |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 186 | WorkspaceItemInfo info = ((AppInfo) item).makeWorkspaceItem(); |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 187 | mLauncher.getModelWriter().addItemToDatabase(info, |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 188 | Favorites.CONTAINER_DESKTOP, |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 189 | screenId, coordinates[0], coordinates[1]); |
| 190 | |
| 191 | ArrayList<ItemInfo> itemList = new ArrayList<>(); |
| 192 | itemList.add(info); |
Sunny Goyal | b23980c | 2017-08-17 07:45:25 -0700 | [diff] [blame] | 193 | mLauncher.bindItems(itemList, true); |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 194 | } else if (item instanceof PendingAddItemInfo) { |
| 195 | PendingAddItemInfo info = (PendingAddItemInfo) item; |
| 196 | Workspace workspace = mLauncher.getWorkspace(); |
| 197 | workspace.snapToPage(workspace.getPageIndexForScreenId(screenId)); |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 198 | mLauncher.addPendingItem(info, Favorites.CONTAINER_DESKTOP, |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 199 | screenId, coordinates, info.spanX, info.spanY); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 200 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 201 | announceConfirmation(R.string.item_added_to_workspace); |
| 202 | } |
| 203 | }); |
| 204 | return true; |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 205 | } else if (action == MOVE_TO_WORKSPACE) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 206 | Folder folder = Folder.getOpen(mLauncher); |
| 207 | folder.close(true); |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 208 | WorkspaceItemInfo info = (WorkspaceItemInfo) item; |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame] | 209 | folder.getInfo().remove(info, false); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 210 | |
| 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 | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 213 | mLauncher.getModelWriter().moveItemInDatabase(info, |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 214 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 215 | screenId, coordinates[0], coordinates[1]); |
| 216 | |
| 217 | // Bind the item in next frame so that if a new workspace page was created, |
| 218 | // it will get laid out. |
| 219 | new Handler().post(new Runnable() { |
| 220 | |
| 221 | @Override |
| 222 | public void run() { |
| 223 | ArrayList<ItemInfo> itemList = new ArrayList<>(); |
| 224 | itemList.add(item); |
Sunny Goyal | b23980c | 2017-08-17 07:45:25 -0700 | [diff] [blame] | 225 | mLauncher.bindItems(itemList, true); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 226 | announceConfirmation(R.string.item_moved); |
| 227 | } |
| 228 | }); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 229 | } else if (action == RESIZE) { |
| 230 | final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 231 | final IntArray actions = getSupportedResizeActions(host, info); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 232 | CharSequence[] labels = new CharSequence[actions.size()]; |
| 233 | for (int i = 0; i < actions.size(); i++) { |
| 234 | labels[i] = mLauncher.getText(actions.get(i)); |
| 235 | } |
| 236 | |
| 237 | new AlertDialog.Builder(mLauncher) |
| 238 | .setTitle(R.string.action_resize) |
| 239 | .setItems(labels, new DialogInterface.OnClickListener() { |
| 240 | |
| 241 | @Override |
| 242 | public void onClick(DialogInterface dialog, int which) { |
| 243 | performResizeAction(actions.get(which), host, info); |
| 244 | dialog.dismiss(); |
| 245 | } |
| 246 | }) |
| 247 | .show(); |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 248 | return true; |
| 249 | } else if (action == DEEP_SHORTCUTS) { |
Tony Wickham | 540913e | 2017-01-23 11:47:51 -0800 | [diff] [blame] | 250 | return PopupContainerWithArrow.showForIcon((BubbleTextView) host) != null; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 251 | } else { |
| 252 | for (ButtonDropTarget dropTarget : mLauncher.getDropTargetBar().getDropTargets()) { |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 253 | if (dropTarget.supportsAccessibilityDrop(item, host) && |
| 254 | action == dropTarget.getAccessibilityAction()) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 255 | dropTarget.onAccessibilityDrop(host, item); |
| 256 | return true; |
| 257 | } |
| 258 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 259 | } |
| 260 | return false; |
| 261 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 262 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 263 | private IntArray getSupportedResizeActions(View host, LauncherAppWidgetInfo info) { |
| 264 | IntArray actions = new IntArray(); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 265 | |
Sunny Goyal | d3d8c95 | 2015-06-01 10:09:06 -0700 | [diff] [blame] | 266 | AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo(); |
| 267 | if (providerInfo == null) { |
| 268 | return actions; |
| 269 | } |
| 270 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 271 | CellLayout layout = (CellLayout) host.getParent().getParent(); |
| 272 | if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) { |
| 273 | if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) || |
| 274 | layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) { |
| 275 | actions.add(R.string.action_increase_width); |
| 276 | } |
| 277 | |
| 278 | if (info.spanX > info.minSpanX && info.spanX > 1) { |
| 279 | actions.add(R.string.action_decrease_width); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) { |
| 284 | if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) || |
| 285 | layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) { |
| 286 | actions.add(R.string.action_increase_height); |
| 287 | } |
| 288 | |
| 289 | if (info.spanY > info.minSpanY && info.spanY > 1) { |
| 290 | actions.add(R.string.action_decrease_height); |
| 291 | } |
| 292 | } |
| 293 | return actions; |
| 294 | } |
| 295 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 296 | @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) { |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 297 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams(); |
| 298 | CellLayout layout = (CellLayout) host.getParent().getParent(); |
| 299 | layout.markCellsAsUnoccupiedForView(host); |
| 300 | |
| 301 | if (action == R.string.action_increase_width) { |
| 302 | if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) |
| 303 | && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) |
| 304 | || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) { |
| 305 | lp.cellX --; |
| 306 | info.cellX --; |
| 307 | } |
| 308 | lp.cellHSpan ++; |
| 309 | info.spanX ++; |
| 310 | } else if (action == R.string.action_decrease_width) { |
| 311 | lp.cellHSpan --; |
| 312 | info.spanX --; |
| 313 | } else if (action == R.string.action_increase_height) { |
| 314 | if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) { |
| 315 | lp.cellY --; |
| 316 | info.cellY --; |
| 317 | } |
| 318 | lp.cellVSpan ++; |
| 319 | info.spanY ++; |
| 320 | } else if (action == R.string.action_decrease_height) { |
| 321 | lp.cellVSpan --; |
| 322 | info.spanY --; |
| 323 | } |
| 324 | |
| 325 | layout.markCellsAsOccupiedForView(host); |
| 326 | Rect sizeRange = new Rect(); |
| 327 | AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange); |
| 328 | ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null, |
| 329 | sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom); |
| 330 | host.requestLayout(); |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 331 | mLauncher.getModelWriter().updateItemInDatabase(info); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 332 | announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY)); |
| 333 | } |
| 334 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 335 | @Thunk void announceConfirmation(int resId) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 336 | announceConfirmation(mLauncher.getResources().getString(resId)); |
| 337 | } |
| 338 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 339 | @Thunk void announceConfirmation(String confirmation) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 340 | mLauncher.getDragLayer().announceForAccessibility(confirmation); |
| 341 | |
| 342 | } |
| 343 | |
| 344 | public boolean isInAccessibleDrag() { |
| 345 | return mDragInfo != null; |
| 346 | } |
| 347 | |
| 348 | public DragInfo getDragInfo() { |
| 349 | return mDragInfo; |
| 350 | } |
| 351 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 352 | /** |
| 353 | * @param clickedTarget the actual view that was clicked |
| 354 | * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used |
| 355 | * as the actual drop location otherwise the views center is used. |
| 356 | */ |
| 357 | public void handleAccessibleDrop(View clickedTarget, Rect dropLocation, |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 358 | String confirmation) { |
| 359 | if (!isInAccessibleDrag()) return; |
| 360 | |
| 361 | int[] loc = new int[2]; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 362 | if (dropLocation == null) { |
| 363 | loc[0] = clickedTarget.getWidth() / 2; |
| 364 | loc[1] = clickedTarget.getHeight() / 2; |
| 365 | } else { |
| 366 | loc[0] = dropLocation.centerX(); |
| 367 | loc[1] = dropLocation.centerY(); |
| 368 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 369 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 370 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 371 | mLauncher.getDragController().completeAccessibleDrag(loc); |
| 372 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 373 | if (!TextUtils.isEmpty(confirmation)) { |
| 374 | announceConfirmation(confirmation); |
| 375 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | public void beginAccessibleDrag(View item, ItemInfo info) { |
| 379 | mDragInfo = new DragInfo(); |
| 380 | mDragInfo.info = info; |
| 381 | mDragInfo.item = item; |
| 382 | mDragInfo.dragType = DragType.ICON; |
| 383 | if (info instanceof FolderInfo) { |
| 384 | mDragInfo.dragType = DragType.FOLDER; |
| 385 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 386 | mDragInfo.dragType = DragType.WIDGET; |
| 387 | } |
| 388 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 389 | Rect pos = new Rect(); |
| 390 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 391 | mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY()); |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 392 | mLauncher.getDragController().addDragListener(this); |
| 393 | |
| 394 | DragOptions options = new DragOptions(); |
| 395 | options.isAccessibleDrag = true; |
Sunny Goyal | 8a2a63b | 2018-03-06 22:15:18 -0800 | [diff] [blame] | 396 | ItemLongClickListener.beginDrag(item, mLauncher, info, options); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 399 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 400 | public void onDragStart(DragObject dragObject, DragOptions options) { |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 401 | // No-op |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 404 | @Override |
| 405 | public void onDragEnd() { |
| 406 | mLauncher.getDragController().removeDragListener(this); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 407 | mDragInfo = null; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 408 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 409 | |
| 410 | /** |
| 411 | * Find empty space on the workspace and returns the screenId. |
| 412 | */ |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 413 | protected int findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 414 | Workspace workspace = mLauncher.getWorkspace(); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 415 | IntArray workspaceScreens = workspace.getScreenOrder(); |
| 416 | int screenId; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 417 | |
| 418 | // First check if there is space on the current screen. |
| 419 | int screenIndex = workspace.getCurrentPage(); |
| 420 | screenId = workspaceScreens.get(screenIndex); |
| 421 | CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex); |
| 422 | |
| 423 | boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
Sunny Goyal | 7ce471b | 2017-08-02 03:37:39 -0700 | [diff] [blame] | 424 | screenIndex = 0; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 425 | while (!found && screenIndex < workspaceScreens.size()) { |
| 426 | screenId = workspaceScreens.get(screenIndex); |
| 427 | layout = (CellLayout) workspace.getPageAt(screenIndex); |
| 428 | found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 429 | screenIndex++; |
| 430 | } |
| 431 | |
| 432 | if (found) { |
| 433 | return screenId; |
| 434 | } |
| 435 | |
| 436 | workspace.addExtraEmptyScreen(); |
| 437 | screenId = workspace.commitExtraEmptyScreen(); |
| 438 | layout = workspace.getScreenWithId(screenId); |
| 439 | found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 440 | |
| 441 | if (!found) { |
| 442 | Log.wtf(TAG, "Not enough space on an empty screen"); |
| 443 | } |
| 444 | return screenId; |
| 445 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 446 | } |