Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | import android.annotation.TargetApi; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 4 | import android.graphics.Rect; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 5 | import android.os.Build; |
| 6 | import android.os.Bundle; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 7 | import android.text.TextUtils; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame^] | 8 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 9 | import android.util.SparseArray; |
| 10 | import android.view.View; |
| 11 | import android.view.View.AccessibilityDelegate; |
| 12 | import android.view.accessibility.AccessibilityNodeInfo; |
| 13 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
| 14 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 15 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 16 | |
| 17 | import java.util.ArrayList; |
| 18 | |
| 19 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 20 | public class LauncherAccessibilityDelegate extends AccessibilityDelegate { |
| 21 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame^] | 22 | private static final String TAG = "LauncherAccessibilityDelegate"; |
| 23 | |
| 24 | private static final int REMOVE = R.id.action_remove; |
| 25 | private static final int INFO = R.id.action_info; |
| 26 | private static final int UNINSTALL = R.id.action_uninstall; |
| 27 | private static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace; |
| 28 | private static final int MOVE = R.id.action_move; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 29 | |
| 30 | enum DragType { |
| 31 | ICON, |
| 32 | FOLDER, |
| 33 | WIDGET |
| 34 | } |
| 35 | |
| 36 | public static class DragInfo { |
| 37 | DragType dragType; |
| 38 | ItemInfo info; |
| 39 | View item; |
| 40 | } |
| 41 | |
| 42 | private DragInfo mDragInfo = null; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 43 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame^] | 44 | private final SparseArray<AccessibilityAction> mActions = new SparseArray<>(); |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 45 | @Thunk final Launcher mLauncher; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 46 | |
| 47 | public LauncherAccessibilityDelegate(Launcher launcher) { |
| 48 | mLauncher = launcher; |
| 49 | |
| 50 | mActions.put(REMOVE, new AccessibilityAction(REMOVE, |
| 51 | launcher.getText(R.string.delete_target_label))); |
| 52 | mActions.put(INFO, new AccessibilityAction(INFO, |
| 53 | launcher.getText(R.string.info_target_label))); |
| 54 | mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL, |
| 55 | launcher.getText(R.string.delete_target_uninstall_label))); |
| 56 | mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE, |
| 57 | launcher.getText(R.string.action_add_to_workspace))); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 58 | mActions.put(MOVE, new AccessibilityAction(MOVE, |
| 59 | launcher.getText(R.string.action_move))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 64 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 65 | if (!(host.getTag() instanceof ItemInfo)) return; |
| 66 | ItemInfo item = (ItemInfo) host.getTag(); |
| 67 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 68 | if (DeleteDropTarget.supportsDrop(item)) { |
| 69 | info.addAction(mActions.get(REMOVE)); |
| 70 | } |
| 71 | if (UninstallDropTarget.supportsDrop(host.getContext(), item)) { |
| 72 | info.addAction(mActions.get(UNINSTALL)); |
| 73 | } |
| 74 | if (InfoDropTarget.supportsDrop(host.getContext(), item)) { |
| 75 | info.addAction(mActions.get(INFO)); |
| 76 | } |
| 77 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 78 | if ((item instanceof ShortcutInfo) |
| 79 | || (item instanceof LauncherAppWidgetInfo) |
| 80 | || (item instanceof FolderInfo)) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 81 | info.addAction(mActions.get(MOVE)); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 82 | } if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 83 | info.addAction(mActions.get(ADD_TO_WORKSPACE)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean performAccessibilityAction(View host, int action, Bundle args) { |
| 89 | if ((host.getTag() instanceof ItemInfo) |
| 90 | && performAction(host, (ItemInfo) host.getTag(), action)) { |
| 91 | return true; |
| 92 | } |
| 93 | return super.performAccessibilityAction(host, action, args); |
| 94 | } |
| 95 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame^] | 96 | public boolean performAction(View host, final ItemInfo item, int action) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 97 | if (action == REMOVE) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 98 | if (DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host)) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 99 | announceConfirmation(R.string.item_removed); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | return false; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 103 | } else if (action == INFO) { |
| 104 | InfoDropTarget.startDetailsActivityForInfo(item, mLauncher); |
| 105 | return true; |
| 106 | } else if (action == UNINSTALL) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 107 | return UninstallDropTarget.startUninstallActivity(mLauncher, item); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 108 | } else if (action == MOVE) { |
| 109 | beginAccessibleDrag(host, item); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 110 | } else if (action == ADD_TO_WORKSPACE) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame^] | 111 | final int[] coordinates = new int[2]; |
| 112 | final long screenId = findSpaceOnWorkspace(item, coordinates); |
| 113 | mLauncher.showWorkspace(true, new Runnable() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 114 | |
| 115 | @Override |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame^] | 116 | public void run() { |
| 117 | if (item instanceof AppInfo) { |
| 118 | ShortcutInfo info = ((AppInfo) item).makeShortcut(); |
| 119 | LauncherModel.addItemToDatabase(mLauncher, info, |
| 120 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 121 | screenId, coordinates[0], coordinates[1]); |
| 122 | |
| 123 | ArrayList<ItemInfo> itemList = new ArrayList<>(); |
| 124 | itemList.add(info); |
| 125 | mLauncher.bindItems(itemList, 0, itemList.size(), true); |
| 126 | } else if (item instanceof PendingAddItemInfo) { |
| 127 | PendingAddItemInfo info = (PendingAddItemInfo) item; |
| 128 | Workspace workspace = mLauncher.getWorkspace(); |
| 129 | workspace.snapToPage(workspace.getPageIndexForScreenId(screenId)); |
| 130 | mLauncher.addPendingItem(info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 131 | screenId, coordinates, info.spanX, info.spanY); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 132 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame^] | 133 | announceConfirmation(R.string.item_added_to_workspace); |
| 134 | } |
| 135 | }); |
| 136 | return true; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 137 | } |
| 138 | return false; |
| 139 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 140 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 141 | @Thunk void announceConfirmation(int resId) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 142 | announceConfirmation(mLauncher.getResources().getString(resId)); |
| 143 | } |
| 144 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 145 | @Thunk void announceConfirmation(String confirmation) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 146 | mLauncher.getDragLayer().announceForAccessibility(confirmation); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | public boolean isInAccessibleDrag() { |
| 151 | return mDragInfo != null; |
| 152 | } |
| 153 | |
| 154 | public DragInfo getDragInfo() { |
| 155 | return mDragInfo; |
| 156 | } |
| 157 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 158 | /** |
| 159 | * @param clickedTarget the actual view that was clicked |
| 160 | * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used |
| 161 | * as the actual drop location otherwise the views center is used. |
| 162 | */ |
| 163 | public void handleAccessibleDrop(View clickedTarget, Rect dropLocation, |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 164 | String confirmation) { |
| 165 | if (!isInAccessibleDrag()) return; |
| 166 | |
| 167 | int[] loc = new int[2]; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 168 | if (dropLocation == null) { |
| 169 | loc[0] = clickedTarget.getWidth() / 2; |
| 170 | loc[1] = clickedTarget.getHeight() / 2; |
| 171 | } else { |
| 172 | loc[0] = dropLocation.centerX(); |
| 173 | loc[1] = dropLocation.centerY(); |
| 174 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 175 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 176 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 177 | mLauncher.getDragController().completeAccessibleDrag(loc); |
| 178 | |
| 179 | endAccessibleDrag(); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 180 | if (!TextUtils.isEmpty(confirmation)) { |
| 181 | announceConfirmation(confirmation); |
| 182 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | public void beginAccessibleDrag(View item, ItemInfo info) { |
| 186 | mDragInfo = new DragInfo(); |
| 187 | mDragInfo.info = info; |
| 188 | mDragInfo.item = item; |
| 189 | mDragInfo.dragType = DragType.ICON; |
| 190 | if (info instanceof FolderInfo) { |
| 191 | mDragInfo.dragType = DragType.FOLDER; |
| 192 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 193 | mDragInfo.dragType = DragType.WIDGET; |
| 194 | } |
| 195 | |
| 196 | CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info); |
| 197 | |
| 198 | Rect pos = new Rect(); |
| 199 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos); |
| 200 | |
| 201 | mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY()); |
| 202 | mLauncher.getWorkspace().enableAccessibleDrag(true); |
| 203 | mLauncher.getWorkspace().startDrag(cellInfo, true); |
| 204 | } |
| 205 | |
| 206 | public boolean onBackPressed() { |
| 207 | if (isInAccessibleDrag()) { |
| 208 | cancelAccessibleDrag(); |
| 209 | return true; |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | private void cancelAccessibleDrag() { |
| 215 | mLauncher.getDragController().cancelDrag(); |
| 216 | endAccessibleDrag(); |
| 217 | } |
| 218 | |
| 219 | private void endAccessibleDrag() { |
| 220 | mDragInfo = null; |
| 221 | mLauncher.getWorkspace().enableAccessibleDrag(false); |
| 222 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame^] | 223 | |
| 224 | /** |
| 225 | * Find empty space on the workspace and returns the screenId. |
| 226 | */ |
| 227 | private long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) { |
| 228 | Workspace workspace = mLauncher.getWorkspace(); |
| 229 | ArrayList<Long> workspaceScreens = workspace.getScreenOrder(); |
| 230 | long screenId; |
| 231 | |
| 232 | // First check if there is space on the current screen. |
| 233 | int screenIndex = workspace.getCurrentPage(); |
| 234 | screenId = workspaceScreens.get(screenIndex); |
| 235 | CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex); |
| 236 | |
| 237 | boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 238 | screenIndex = workspace.hasCustomContent() ? 1 : 0; |
| 239 | while (!found && screenIndex < workspaceScreens.size()) { |
| 240 | screenId = workspaceScreens.get(screenIndex); |
| 241 | layout = (CellLayout) workspace.getPageAt(screenIndex); |
| 242 | found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 243 | screenIndex++; |
| 244 | } |
| 245 | |
| 246 | if (found) { |
| 247 | return screenId; |
| 248 | } |
| 249 | |
| 250 | workspace.addExtraEmptyScreen(); |
| 251 | screenId = workspace.commitExtraEmptyScreen(); |
| 252 | layout = workspace.getScreenWithId(screenId); |
| 253 | found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 254 | |
| 255 | if (!found) { |
| 256 | Log.wtf(TAG, "Not enough space on an empty screen"); |
| 257 | } |
| 258 | return screenId; |
| 259 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 260 | } |