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 | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 8 | import android.util.SparseArray; |
| 9 | import android.view.View; |
| 10 | import android.view.View.AccessibilityDelegate; |
| 11 | import android.view.accessibility.AccessibilityNodeInfo; |
| 12 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
| 13 | |
| 14 | import com.android.launcher3.LauncherModel.ScreenPosProvider; |
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 | |
| 22 | public static final int REMOVE = R.id.action_remove; |
| 23 | public static final int INFO = R.id.action_info; |
| 24 | public static final int UNINSTALL = R.id.action_uninstall; |
| 25 | public static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 26 | public static final int MOVE = R.id.action_move; |
| 27 | |
| 28 | enum DragType { |
| 29 | ICON, |
| 30 | FOLDER, |
| 31 | WIDGET |
| 32 | } |
| 33 | |
| 34 | public static class DragInfo { |
| 35 | DragType dragType; |
| 36 | ItemInfo info; |
| 37 | View item; |
| 38 | } |
| 39 | |
| 40 | private DragInfo mDragInfo = null; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 41 | |
| 42 | private final SparseArray<AccessibilityAction> mActions = |
| 43 | new SparseArray<AccessibilityAction>(); |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 44 | @Thunk final Launcher mLauncher; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 45 | |
| 46 | public LauncherAccessibilityDelegate(Launcher launcher) { |
| 47 | mLauncher = launcher; |
| 48 | |
| 49 | mActions.put(REMOVE, new AccessibilityAction(REMOVE, |
| 50 | launcher.getText(R.string.delete_target_label))); |
| 51 | mActions.put(INFO, new AccessibilityAction(INFO, |
| 52 | launcher.getText(R.string.info_target_label))); |
| 53 | mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL, |
| 54 | launcher.getText(R.string.delete_target_uninstall_label))); |
| 55 | mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE, |
| 56 | launcher.getText(R.string.action_add_to_workspace))); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 57 | mActions.put(MOVE, new AccessibilityAction(MOVE, |
| 58 | launcher.getText(R.string.action_move))); |
| 59 | |
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 | |
| 96 | public boolean performAction(View host, ItemInfo item, int action) { |
| 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) { |
| 111 | final int preferredPage = mLauncher.getWorkspace().getCurrentPage(); |
| 112 | final ScreenPosProvider screenProvider = new ScreenPosProvider() { |
| 113 | |
| 114 | @Override |
| 115 | public int getScreenIndex(ArrayList<Long> screenIDs) { |
| 116 | return preferredPage; |
| 117 | } |
| 118 | }; |
| 119 | if (item instanceof AppInfo) { |
| 120 | final ArrayList<ItemInfo> addShortcuts = new ArrayList<ItemInfo>(); |
| 121 | addShortcuts.add(((AppInfo) item).makeShortcut()); |
| 122 | mLauncher.showWorkspace(true, new Runnable() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 123 | @Override |
| 124 | public void run() { |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 125 | mLauncher.getModel().addAndBindAddedWorkspaceItems( |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 126 | mLauncher, addShortcuts, screenProvider, 0, true); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 127 | announceConfirmation(R.string.item_added_to_workspace); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 128 | } |
| 129 | }); |
| 130 | return true; |
| 131 | } else if (item instanceof PendingAddItemInfo) { |
| 132 | mLauncher.getModel().addAndBindPendingItem( |
| 133 | mLauncher, (PendingAddItemInfo) item, screenProvider, 0); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 134 | announceConfirmation(R.string.item_added_to_workspace); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 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 | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 223 | } |