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