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.content.res.Resources; |
| 5 | import android.graphics.Rect; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 6 | import android.os.Build; |
| 7 | import android.os.Bundle; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 8 | import android.support.v4.view.accessibility.AccessibilityEventCompat; |
| 9 | import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; |
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; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 13 | import android.view.accessibility.AccessibilityEvent; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 14 | import android.view.accessibility.AccessibilityNodeInfo; |
| 15 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
| 16 | |
| 17 | import com.android.launcher3.LauncherModel.ScreenPosProvider; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | |
| 21 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 22 | public class LauncherAccessibilityDelegate extends AccessibilityDelegate { |
| 23 | |
| 24 | public static final int REMOVE = R.id.action_remove; |
| 25 | public static final int INFO = R.id.action_info; |
| 26 | public static final int UNINSTALL = R.id.action_uninstall; |
| 27 | 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^] | 28 | public static final int MOVE = R.id.action_move; |
| 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 | |
| 44 | private final SparseArray<AccessibilityAction> mActions = |
| 45 | new SparseArray<AccessibilityAction>(); |
| 46 | private final Launcher mLauncher; |
| 47 | |
| 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))); |
| 61 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 66 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 67 | if (!(host.getTag() instanceof ItemInfo)) return; |
| 68 | ItemInfo item = (ItemInfo) host.getTag(); |
| 69 | |
| 70 | if ((item instanceof ShortcutInfo) |
| 71 | || (item instanceof LauncherAppWidgetInfo) |
| 72 | || (item instanceof FolderInfo)) { |
| 73 | // Workspace shortcut / widget |
| 74 | info.addAction(mActions.get(REMOVE)); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 75 | info.addAction(mActions.get(MOVE)); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 76 | } else if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) { |
| 77 | // App or Widget from customization tray |
| 78 | if (item instanceof AppInfo) { |
| 79 | info.addAction(mActions.get(UNINSTALL)); |
| 80 | } |
| 81 | info.addAction(mActions.get(INFO)); |
| 82 | info.addAction(mActions.get(ADD_TO_WORKSPACE)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean performAccessibilityAction(View host, int action, Bundle args) { |
| 88 | if ((host.getTag() instanceof ItemInfo) |
| 89 | && performAction(host, (ItemInfo) host.getTag(), action)) { |
| 90 | return true; |
| 91 | } |
| 92 | return super.performAccessibilityAction(host, action, args); |
| 93 | } |
| 94 | |
| 95 | public boolean performAction(View host, ItemInfo item, int action) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 96 | Resources res = mLauncher.getResources(); |
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)) { |
| 99 | announceConfirmation(R.string.item_removed_from_workspace); |
| 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) { |
| 107 | DeleteDropTarget.uninstallApp(mLauncher, (AppInfo) item); |
| 108 | return true; |
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) { |
| 112 | final int preferredPage = mLauncher.getWorkspace().getCurrentPage(); |
| 113 | final ScreenPosProvider screenProvider = new ScreenPosProvider() { |
| 114 | |
| 115 | @Override |
| 116 | public int getScreenIndex(ArrayList<Long> screenIDs) { |
| 117 | return preferredPage; |
| 118 | } |
| 119 | }; |
| 120 | if (item instanceof AppInfo) { |
| 121 | final ArrayList<ItemInfo> addShortcuts = new ArrayList<ItemInfo>(); |
| 122 | addShortcuts.add(((AppInfo) item).makeShortcut()); |
| 123 | mLauncher.showWorkspace(true, new Runnable() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 124 | @Override |
| 125 | public void run() { |
| 126 | mLauncher.getModel().addAndBindAddedWorkspaceApps( |
| 127 | mLauncher, addShortcuts, screenProvider, 0, true); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 128 | announceConfirmation(R.string.item_added_to_workspace); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 129 | } |
| 130 | }); |
| 131 | return true; |
| 132 | } else if (item instanceof PendingAddItemInfo) { |
| 133 | mLauncher.getModel().addAndBindPendingItem( |
| 134 | mLauncher, (PendingAddItemInfo) item, screenProvider, 0); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 135 | announceConfirmation(R.string.item_added_to_workspace); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 141 | |
| 142 | private void announceConfirmation(int resId) { |
| 143 | announceConfirmation(mLauncher.getResources().getString(resId)); |
| 144 | } |
| 145 | |
| 146 | private void announceConfirmation(String confirmation) { |
| 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 | |
| 159 | public void handleAccessibleDrop(CellLayout targetContainer, Rect dropLocation, |
| 160 | String confirmation) { |
| 161 | if (!isInAccessibleDrag()) return; |
| 162 | |
| 163 | int[] loc = new int[2]; |
| 164 | loc[0] = dropLocation.centerX(); |
| 165 | loc[1] = dropLocation.centerY(); |
| 166 | |
| 167 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(targetContainer, loc); |
| 168 | mLauncher.getDragController().completeAccessibleDrag(loc); |
| 169 | |
| 170 | endAccessibleDrag(); |
| 171 | announceConfirmation(confirmation); |
| 172 | } |
| 173 | |
| 174 | public void beginAccessibleDrag(View item, ItemInfo info) { |
| 175 | mDragInfo = new DragInfo(); |
| 176 | mDragInfo.info = info; |
| 177 | mDragInfo.item = item; |
| 178 | mDragInfo.dragType = DragType.ICON; |
| 179 | if (info instanceof FolderInfo) { |
| 180 | mDragInfo.dragType = DragType.FOLDER; |
| 181 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 182 | mDragInfo.dragType = DragType.WIDGET; |
| 183 | } |
| 184 | |
| 185 | CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info); |
| 186 | |
| 187 | Rect pos = new Rect(); |
| 188 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos); |
| 189 | |
| 190 | mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY()); |
| 191 | mLauncher.getWorkspace().enableAccessibleDrag(true); |
| 192 | mLauncher.getWorkspace().startDrag(cellInfo, true); |
| 193 | } |
| 194 | |
| 195 | public boolean onBackPressed() { |
| 196 | if (isInAccessibleDrag()) { |
| 197 | cancelAccessibleDrag(); |
| 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | private void cancelAccessibleDrag() { |
| 204 | mLauncher.getDragController().cancelDrag(); |
| 205 | endAccessibleDrag(); |
| 206 | } |
| 207 | |
| 208 | private void endAccessibleDrag() { |
| 209 | mDragInfo = null; |
| 210 | mLauncher.getWorkspace().enableAccessibleDrag(false); |
| 211 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 212 | } |