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