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