blob: 8a9a0508c5243bca520b8b03cf2ddb71694f5beb [file] [log] [blame]
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001package com.android.launcher3;
2
3import android.annotation.TargetApi;
Adam Cohenc9735cf2015-01-23 16:11:55 -08004import android.graphics.Rect;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08005import android.os.Build;
6import android.os.Bundle;
Sunny Goyal9ae77772015-04-29 16:30:23 -07007import android.os.Handler;
Sunny Goyal1a70cef2015-04-22 11:29:51 -07008import android.text.TextUtils;
Sunny Goyala9116722015-04-29 13:55:58 -07009import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080010import android.util.SparseArray;
11import android.view.View;
12import android.view.View.AccessibilityDelegate;
13import android.view.accessibility.AccessibilityNodeInfo;
14import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
15
Adam Cohen091440a2015-03-18 14:16:05 -070016import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080017
18import java.util.ArrayList;
19
20@TargetApi(Build.VERSION_CODES.LOLLIPOP)
21public class LauncherAccessibilityDelegate extends AccessibilityDelegate {
22
Sunny Goyala9116722015-04-29 13:55:58 -070023 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 Goyal9ae77772015-04-29 16:30:23 -070030 private static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
Adam Cohenc9735cf2015-01-23 16:11:55 -080031
Sunny Goyale9b651e2015-04-24 11:44:51 -070032 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080033 ICON,
34 FOLDER,
35 WIDGET
36 }
37
38 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070039 public DragType dragType;
40 public ItemInfo info;
41 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080042 }
43
Sunny Goyala9116722015-04-29 13:55:58 -070044 private final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070045 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080046
Sunny Goyale9b651e2015-04-24 11:44:51 -070047 private DragInfo mDragInfo = null;
48 private AccessibilityDragSource mDragSource = null;
49
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080050 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 Cohenc9735cf2015-01-23 16:11:55 -080061 mActions.put(MOVE, new AccessibilityAction(MOVE,
62 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070063 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
64 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080065 }
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 Goyal1a70cef2015-04-22 11:29:51 -070073 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 Goyal71b5c0b2015-01-08 16:59:04 -080083 if ((item instanceof ShortcutInfo)
84 || (item instanceof LauncherAppWidgetInfo)
85 || (item instanceof FolderInfo)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -080086 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -070087
88 if (item.container >= 0) {
89 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
90 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -070091 } if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080092 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 Goyala9116722015-04-29 13:55:58 -0700105 public boolean performAction(View host, final ItemInfo item, int action) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800106 if (action == REMOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800107 if (DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700108 announceConfirmation(R.string.item_removed);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800109 return true;
110 }
111 return false;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800112 } else if (action == INFO) {
113 InfoDropTarget.startDetailsActivityForInfo(item, mLauncher);
114 return true;
115 } else if (action == UNINSTALL) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700116 return UninstallDropTarget.startUninstallActivity(mLauncher, item);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800117 } else if (action == MOVE) {
118 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800119 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700120 final int[] coordinates = new int[2];
121 final long screenId = findSpaceOnWorkspace(item, coordinates);
122 mLauncher.showWorkspace(true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800123
124 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700125 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 Goyal71b5c0b2015-01-08 16:59:04 -0800141 }
Sunny Goyala9116722015-04-29 13:55:58 -0700142 announceConfirmation(R.string.item_added_to_workspace);
143 }
144 });
145 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700146 } 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 Goyal71b5c0b2015-01-08 16:59:04 -0800170 }
171 return false;
172 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800173
Adam Cohen091440a2015-03-18 14:16:05 -0700174 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800175 announceConfirmation(mLauncher.getResources().getString(resId));
176 }
177
Adam Cohen091440a2015-03-18 14:16:05 -0700178 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800179 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 Goyal1a70cef2015-04-22 11:29:51 -0700191 /**
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 Cohenc9735cf2015-01-23 16:11:55 -0800197 String confirmation) {
198 if (!isInAccessibleDrag()) return;
199
200 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700201 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 Cohenc9735cf2015-01-23 16:11:55 -0800208
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700209 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800210 mLauncher.getDragController().completeAccessibleDrag(loc);
211
212 endAccessibleDrag();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700213 if (!TextUtils.isEmpty(confirmation)) {
214 announceConfirmation(confirmation);
215 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800216 }
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 Cohenc9735cf2015-01-23 16:11:55 -0800233 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyale9b651e2015-04-24 11:44:51 -0700234
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 Cohenc9735cf2015-01-23 16:11:55 -0800250 }
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 Goyale9b651e2015-04-24 11:44:51 -0700267 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 Cohenc9735cf2015-01-23 16:11:55 -0800277 }
Sunny Goyala9116722015-04-29 13:55:58 -0700278
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 Goyal71b5c0b2015-01-08 16:59:04 -0800315}