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