blob: bb32fa112177810f0bb70f9de44c2567f8f6533c [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 Goyal1a70cef2015-04-22 11:29:51 -07007import android.text.TextUtils;
Sunny Goyala9116722015-04-29 13:55:58 -07008import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08009import android.util.SparseArray;
10import android.view.View;
11import android.view.View.AccessibilityDelegate;
12import android.view.accessibility.AccessibilityNodeInfo;
13import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
14
Adam Cohen091440a2015-03-18 14:16:05 -070015import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080016
17import java.util.ArrayList;
18
19@TargetApi(Build.VERSION_CODES.LOLLIPOP)
20public class LauncherAccessibilityDelegate extends AccessibilityDelegate {
21
Sunny Goyala9116722015-04-29 13:55:58 -070022 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 Cohenc9735cf2015-01-23 16:11:55 -080029
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 Goyal71b5c0b2015-01-08 16:59:04 -080043
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
47 public LauncherAccessibilityDelegate(Launcher launcher) {
48 mLauncher = launcher;
49
50 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
51 launcher.getText(R.string.delete_target_label)));
52 mActions.put(INFO, new AccessibilityAction(INFO,
53 launcher.getText(R.string.info_target_label)));
54 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
55 launcher.getText(R.string.delete_target_uninstall_label)));
56 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
57 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080058 mActions.put(MOVE, new AccessibilityAction(MOVE,
59 launcher.getText(R.string.action_move)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080060 }
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 Goyal1a70cef2015-04-22 11:29:51 -070068 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 Goyal71b5c0b2015-01-08 16:59:04 -080078 if ((item instanceof ShortcutInfo)
79 || (item instanceof LauncherAppWidgetInfo)
80 || (item instanceof FolderInfo)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -080081 info.addAction(mActions.get(MOVE));
Sunny Goyal1a70cef2015-04-22 11:29:51 -070082 } if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080083 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
Sunny Goyala9116722015-04-29 13:55:58 -070096 public boolean performAction(View host, final ItemInfo item, int action) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080097 if (action == REMOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -080098 if (DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070099 announceConfirmation(R.string.item_removed);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800100 return true;
101 }
102 return false;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800103 } else if (action == INFO) {
104 InfoDropTarget.startDetailsActivityForInfo(item, mLauncher);
105 return true;
106 } else if (action == UNINSTALL) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700107 return UninstallDropTarget.startUninstallActivity(mLauncher, item);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800108 } else if (action == MOVE) {
109 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800110 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700111 final int[] coordinates = new int[2];
112 final long screenId = findSpaceOnWorkspace(item, coordinates);
113 mLauncher.showWorkspace(true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800114
115 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700116 public void run() {
117 if (item instanceof AppInfo) {
118 ShortcutInfo info = ((AppInfo) item).makeShortcut();
119 LauncherModel.addItemToDatabase(mLauncher, info,
120 LauncherSettings.Favorites.CONTAINER_DESKTOP,
121 screenId, coordinates[0], coordinates[1]);
122
123 ArrayList<ItemInfo> itemList = new ArrayList<>();
124 itemList.add(info);
125 mLauncher.bindItems(itemList, 0, itemList.size(), true);
126 } else if (item instanceof PendingAddItemInfo) {
127 PendingAddItemInfo info = (PendingAddItemInfo) item;
128 Workspace workspace = mLauncher.getWorkspace();
129 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
130 mLauncher.addPendingItem(info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
131 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800132 }
Sunny Goyala9116722015-04-29 13:55:58 -0700133 announceConfirmation(R.string.item_added_to_workspace);
134 }
135 });
136 return true;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800137 }
138 return false;
139 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800140
Adam Cohen091440a2015-03-18 14:16:05 -0700141 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800142 announceConfirmation(mLauncher.getResources().getString(resId));
143 }
144
Adam Cohen091440a2015-03-18 14:16:05 -0700145 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800146 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 Goyal1a70cef2015-04-22 11:29:51 -0700158 /**
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 Cohenc9735cf2015-01-23 16:11:55 -0800164 String confirmation) {
165 if (!isInAccessibleDrag()) return;
166
167 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700168 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 Cohenc9735cf2015-01-23 16:11:55 -0800175
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700176 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800177 mLauncher.getDragController().completeAccessibleDrag(loc);
178
179 endAccessibleDrag();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700180 if (!TextUtils.isEmpty(confirmation)) {
181 announceConfirmation(confirmation);
182 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800183 }
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 Goyala9116722015-04-29 13:55:58 -0700223
224 /**
225 * Find empty space on the workspace and returns the screenId.
226 */
227 private long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
228 Workspace workspace = mLauncher.getWorkspace();
229 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
230 long screenId;
231
232 // First check if there is space on the current screen.
233 int screenIndex = workspace.getCurrentPage();
234 screenId = workspaceScreens.get(screenIndex);
235 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
236
237 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
238 screenIndex = workspace.hasCustomContent() ? 1 : 0;
239 while (!found && screenIndex < workspaceScreens.size()) {
240 screenId = workspaceScreens.get(screenIndex);
241 layout = (CellLayout) workspace.getPageAt(screenIndex);
242 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
243 screenIndex++;
244 }
245
246 if (found) {
247 return screenId;
248 }
249
250 workspace.addExtraEmptyScreen();
251 screenId = workspace.commitExtraEmptyScreen();
252 layout = workspace.getScreenWithId(screenId);
253 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
254
255 if (!found) {
256 Log.wtf(TAG, "Not enough space on an empty screen");
257 }
258 return screenId;
259 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800260}