blob: 93cf8d05083a3fc3e7ef3b44275977d49f16c49c [file] [log] [blame]
Sunny Goyal83a8f042015-05-19 12:52:12 -07001package com.android.launcher3.accessibility;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002
3import android.annotation.TargetApi;
Sunny Goyal9ca9c132015-04-29 14:57:22 -07004import android.app.AlertDialog;
5import android.appwidget.AppWidgetProviderInfo;
6import android.content.DialogInterface;
Adam Cohenc9735cf2015-01-23 16:11:55 -08007import android.graphics.Rect;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08008import android.os.Build;
9import android.os.Bundle;
Sunny Goyal9ae77772015-04-29 16:30:23 -070010import android.os.Handler;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070011import android.text.TextUtils;
Sunny Goyala9116722015-04-29 13:55:58 -070012import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080013import android.util.SparseArray;
14import android.view.View;
15import android.view.View.AccessibilityDelegate;
16import android.view.accessibility.AccessibilityNodeInfo;
17import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
18
Sunny Goyal83a8f042015-05-19 12:52:12 -070019import com.android.launcher3.AppInfo;
20import com.android.launcher3.AppWidgetResizeFrame;
21import com.android.launcher3.CellLayout;
22import com.android.launcher3.DeleteDropTarget;
23import com.android.launcher3.Folder;
24import com.android.launcher3.FolderInfo;
25import com.android.launcher3.InfoDropTarget;
26import com.android.launcher3.ItemInfo;
27import com.android.launcher3.Launcher;
28import com.android.launcher3.LauncherAppWidgetHostView;
29import com.android.launcher3.LauncherAppWidgetInfo;
30import com.android.launcher3.LauncherModel;
31import com.android.launcher3.LauncherSettings;
32import com.android.launcher3.PendingAddItemInfo;
33import com.android.launcher3.R;
34import com.android.launcher3.ShortcutInfo;
35import com.android.launcher3.UninstallDropTarget;
36import com.android.launcher3.Workspace;
Adam Cohen091440a2015-03-18 14:16:05 -070037import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080038
39import java.util.ArrayList;
40
41@TargetApi(Build.VERSION_CODES.LOLLIPOP)
42public class LauncherAccessibilityDelegate extends AccessibilityDelegate {
43
Sunny Goyala9116722015-04-29 13:55:58 -070044 private static final String TAG = "LauncherAccessibilityDelegate";
45
46 private static final int REMOVE = R.id.action_remove;
47 private static final int INFO = R.id.action_info;
48 private static final int UNINSTALL = R.id.action_uninstall;
49 private static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
50 private static final int MOVE = R.id.action_move;
Sunny Goyal9ae77772015-04-29 16:30:23 -070051 private static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
Sunny Goyal9ca9c132015-04-29 14:57:22 -070052 private static final int RESIZE = R.id.action_resize;
Adam Cohenc9735cf2015-01-23 16:11:55 -080053
Sunny Goyale9b651e2015-04-24 11:44:51 -070054 public enum DragType {
Adam Cohenc9735cf2015-01-23 16:11:55 -080055 ICON,
56 FOLDER,
57 WIDGET
58 }
59
60 public static class DragInfo {
Sunny Goyale9b651e2015-04-24 11:44:51 -070061 public DragType dragType;
62 public ItemInfo info;
63 public View item;
Adam Cohenc9735cf2015-01-23 16:11:55 -080064 }
65
Sunny Goyala9116722015-04-29 13:55:58 -070066 private final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
Adam Cohen091440a2015-03-18 14:16:05 -070067 @Thunk final Launcher mLauncher;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080068
Sunny Goyale9b651e2015-04-24 11:44:51 -070069 private DragInfo mDragInfo = null;
70 private AccessibilityDragSource mDragSource = null;
71
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080072 public LauncherAccessibilityDelegate(Launcher launcher) {
73 mLauncher = launcher;
74
75 mActions.put(REMOVE, new AccessibilityAction(REMOVE,
76 launcher.getText(R.string.delete_target_label)));
77 mActions.put(INFO, new AccessibilityAction(INFO,
78 launcher.getText(R.string.info_target_label)));
79 mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
80 launcher.getText(R.string.delete_target_uninstall_label)));
81 mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
82 launcher.getText(R.string.action_add_to_workspace)));
Adam Cohenc9735cf2015-01-23 16:11:55 -080083 mActions.put(MOVE, new AccessibilityAction(MOVE,
84 launcher.getText(R.string.action_move)));
Sunny Goyal9ae77772015-04-29 16:30:23 -070085 mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
86 launcher.getText(R.string.action_move_to_workspace)));
Sunny Goyal9ca9c132015-04-29 14:57:22 -070087 mActions.put(RESIZE, new AccessibilityAction(RESIZE,
88 launcher.getText(R.string.action_resize)));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080089 }
90
91 @Override
92 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
93 super.onInitializeAccessibilityNodeInfo(host, info);
94 if (!(host.getTag() instanceof ItemInfo)) return;
95 ItemInfo item = (ItemInfo) host.getTag();
96
Sunny Goyal1a70cef2015-04-22 11:29:51 -070097 if (DeleteDropTarget.supportsDrop(item)) {
98 info.addAction(mActions.get(REMOVE));
99 }
100 if (UninstallDropTarget.supportsDrop(host.getContext(), item)) {
101 info.addAction(mActions.get(UNINSTALL));
102 }
103 if (InfoDropTarget.supportsDrop(host.getContext(), item)) {
104 info.addAction(mActions.get(INFO));
105 }
106
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800107 if ((item instanceof ShortcutInfo)
108 || (item instanceof LauncherAppWidgetInfo)
109 || (item instanceof FolderInfo)) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800110 info.addAction(mActions.get(MOVE));
Sunny Goyal9ae77772015-04-29 16:30:23 -0700111
112 if (item.container >= 0) {
113 info.addAction(mActions.get(MOVE_TO_WORKSPACE));
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700114 } else if (item instanceof LauncherAppWidgetInfo) {
115 if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
116 info.addAction(mActions.get(RESIZE));
117 }
Sunny Goyal9ae77772015-04-29 16:30:23 -0700118 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700119 } if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800120 info.addAction(mActions.get(ADD_TO_WORKSPACE));
121 }
122 }
123
124 @Override
125 public boolean performAccessibilityAction(View host, int action, Bundle args) {
126 if ((host.getTag() instanceof ItemInfo)
127 && performAction(host, (ItemInfo) host.getTag(), action)) {
128 return true;
129 }
130 return super.performAccessibilityAction(host, action, args);
131 }
132
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700133 public boolean performAction(final View host, final ItemInfo item, int action) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800134 if (action == REMOVE) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800135 if (DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host)) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700136 announceConfirmation(R.string.item_removed);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800137 return true;
138 }
139 return false;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800140 } else if (action == INFO) {
141 InfoDropTarget.startDetailsActivityForInfo(item, mLauncher);
142 return true;
143 } else if (action == UNINSTALL) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700144 return UninstallDropTarget.startUninstallActivity(mLauncher, item);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800145 } else if (action == MOVE) {
146 beginAccessibleDrag(host, item);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800147 } else if (action == ADD_TO_WORKSPACE) {
Sunny Goyala9116722015-04-29 13:55:58 -0700148 final int[] coordinates = new int[2];
149 final long screenId = findSpaceOnWorkspace(item, coordinates);
150 mLauncher.showWorkspace(true, new Runnable() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800151
152 @Override
Sunny Goyala9116722015-04-29 13:55:58 -0700153 public void run() {
154 if (item instanceof AppInfo) {
155 ShortcutInfo info = ((AppInfo) item).makeShortcut();
156 LauncherModel.addItemToDatabase(mLauncher, info,
157 LauncherSettings.Favorites.CONTAINER_DESKTOP,
158 screenId, coordinates[0], coordinates[1]);
159
160 ArrayList<ItemInfo> itemList = new ArrayList<>();
161 itemList.add(info);
162 mLauncher.bindItems(itemList, 0, itemList.size(), true);
163 } else if (item instanceof PendingAddItemInfo) {
164 PendingAddItemInfo info = (PendingAddItemInfo) item;
165 Workspace workspace = mLauncher.getWorkspace();
166 workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
167 mLauncher.addPendingItem(info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
168 screenId, coordinates, info.spanX, info.spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800169 }
Sunny Goyala9116722015-04-29 13:55:58 -0700170 announceConfirmation(R.string.item_added_to_workspace);
171 }
172 });
173 return true;
Sunny Goyal9ae77772015-04-29 16:30:23 -0700174 } else if (action == MOVE_TO_WORKSPACE) {
175 Folder folder = mLauncher.getWorkspace().getOpenFolder();
176 mLauncher.closeFolder(folder);
177 ShortcutInfo info = (ShortcutInfo) item;
178 folder.getInfo().remove(info);
179
180 final int[] coordinates = new int[2];
181 final long screenId = findSpaceOnWorkspace(item, coordinates);
182 LauncherModel.moveItemInDatabase(mLauncher, info,
183 LauncherSettings.Favorites.CONTAINER_DESKTOP,
184 screenId, coordinates[0], coordinates[1]);
185
186 // Bind the item in next frame so that if a new workspace page was created,
187 // it will get laid out.
188 new Handler().post(new Runnable() {
189
190 @Override
191 public void run() {
192 ArrayList<ItemInfo> itemList = new ArrayList<>();
193 itemList.add(item);
194 mLauncher.bindItems(itemList, 0, itemList.size(), true);
195 announceConfirmation(R.string.item_moved);
196 }
197 });
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700198 } else if (action == RESIZE) {
199 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
200 final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
201 CharSequence[] labels = new CharSequence[actions.size()];
202 for (int i = 0; i < actions.size(); i++) {
203 labels[i] = mLauncher.getText(actions.get(i));
204 }
205
206 new AlertDialog.Builder(mLauncher)
207 .setTitle(R.string.action_resize)
208 .setItems(labels, new DialogInterface.OnClickListener() {
209
210 @Override
211 public void onClick(DialogInterface dialog, int which) {
212 performResizeAction(actions.get(which), host, info);
213 dialog.dismiss();
214 }
215 })
216 .show();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800217 }
218 return false;
219 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800220
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700221 private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700222 ArrayList<Integer> actions = new ArrayList<>();
223
Sunny Goyald3d8c952015-06-01 10:09:06 -0700224 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
225 if (providerInfo == null) {
226 return actions;
227 }
228
Sunny Goyal9ca9c132015-04-29 14:57:22 -0700229 CellLayout layout = (CellLayout) host.getParent().getParent();
230 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
231 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
232 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
233 actions.add(R.string.action_increase_width);
234 }
235
236 if (info.spanX > info.minSpanX && info.spanX > 1) {
237 actions.add(R.string.action_decrease_width);
238 }
239 }
240
241 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
242 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
243 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
244 actions.add(R.string.action_increase_height);
245 }
246
247 if (info.spanY > info.minSpanY && info.spanY > 1) {
248 actions.add(R.string.action_decrease_height);
249 }
250 }
251 return actions;
252 }
253
254 private void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
255 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
256 CellLayout layout = (CellLayout) host.getParent().getParent();
257 layout.markCellsAsUnoccupiedForView(host);
258
259 if (action == R.string.action_increase_width) {
260 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
261 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
262 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
263 lp.cellX --;
264 info.cellX --;
265 }
266 lp.cellHSpan ++;
267 info.spanX ++;
268 } else if (action == R.string.action_decrease_width) {
269 lp.cellHSpan --;
270 info.spanX --;
271 } else if (action == R.string.action_increase_height) {
272 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
273 lp.cellY --;
274 info.cellY --;
275 }
276 lp.cellVSpan ++;
277 info.spanY ++;
278 } else if (action == R.string.action_decrease_height) {
279 lp.cellVSpan --;
280 info.spanY --;
281 }
282
283 layout.markCellsAsOccupiedForView(host);
284 Rect sizeRange = new Rect();
285 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
286 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
287 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
288 host.requestLayout();
289 LauncherModel.updateItemInDatabase(mLauncher, info);
290 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
291 }
292
Adam Cohen091440a2015-03-18 14:16:05 -0700293 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800294 announceConfirmation(mLauncher.getResources().getString(resId));
295 }
296
Adam Cohen091440a2015-03-18 14:16:05 -0700297 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800298 mLauncher.getDragLayer().announceForAccessibility(confirmation);
299
300 }
301
302 public boolean isInAccessibleDrag() {
303 return mDragInfo != null;
304 }
305
306 public DragInfo getDragInfo() {
307 return mDragInfo;
308 }
309
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700310 /**
311 * @param clickedTarget the actual view that was clicked
312 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
313 * as the actual drop location otherwise the views center is used.
314 */
315 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800316 String confirmation) {
317 if (!isInAccessibleDrag()) return;
318
319 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700320 if (dropLocation == null) {
321 loc[0] = clickedTarget.getWidth() / 2;
322 loc[1] = clickedTarget.getHeight() / 2;
323 } else {
324 loc[0] = dropLocation.centerX();
325 loc[1] = dropLocation.centerY();
326 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800327
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700328 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800329 mLauncher.getDragController().completeAccessibleDrag(loc);
330
331 endAccessibleDrag();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700332 if (!TextUtils.isEmpty(confirmation)) {
333 announceConfirmation(confirmation);
334 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800335 }
336
337 public void beginAccessibleDrag(View item, ItemInfo info) {
338 mDragInfo = new DragInfo();
339 mDragInfo.info = info;
340 mDragInfo.item = item;
341 mDragInfo.dragType = DragType.ICON;
342 if (info instanceof FolderInfo) {
343 mDragInfo.dragType = DragType.FOLDER;
344 } else if (info instanceof LauncherAppWidgetInfo) {
345 mDragInfo.dragType = DragType.WIDGET;
346 }
347
348 CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info);
349
350 Rect pos = new Rect();
351 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800352 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyale9b651e2015-04-24 11:44:51 -0700353
354 Workspace workspace = mLauncher.getWorkspace();
355
356 Folder folder = workspace.getOpenFolder();
357 if (folder != null) {
358 if (folder.getItemsInReadingOrder().contains(item)) {
359 mDragSource = folder;
360 } else {
361 mLauncher.closeFolder();
362 }
363 }
364 if (mDragSource == null) {
365 mDragSource = workspace;
366 }
367 mDragSource.enableAccessibleDrag(true);
368 mDragSource.startDrag(cellInfo, true);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800369 }
370
371 public boolean onBackPressed() {
372 if (isInAccessibleDrag()) {
373 cancelAccessibleDrag();
374 return true;
375 }
376 return false;
377 }
378
379 private void cancelAccessibleDrag() {
380 mLauncher.getDragController().cancelDrag();
381 endAccessibleDrag();
382 }
383
384 private void endAccessibleDrag() {
385 mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700386 if (mDragSource != null) {
387 mDragSource.enableAccessibleDrag(false);
388 mDragSource = null;
389 }
390 }
391
392 public static interface AccessibilityDragSource {
393 void startDrag(CellLayout.CellInfo cellInfo, boolean accessible);
394
395 void enableAccessibleDrag(boolean enable);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800396 }
Sunny Goyala9116722015-04-29 13:55:58 -0700397
398 /**
399 * Find empty space on the workspace and returns the screenId.
400 */
401 private long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
402 Workspace workspace = mLauncher.getWorkspace();
403 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
404 long screenId;
405
406 // First check if there is space on the current screen.
407 int screenIndex = workspace.getCurrentPage();
408 screenId = workspaceScreens.get(screenIndex);
409 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
410
411 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
412 screenIndex = workspace.hasCustomContent() ? 1 : 0;
413 while (!found && screenIndex < workspaceScreens.size()) {
414 screenId = workspaceScreens.get(screenIndex);
415 layout = (CellLayout) workspace.getPageAt(screenIndex);
416 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
417 screenIndex++;
418 }
419
420 if (found) {
421 return screenId;
422 }
423
424 workspace.addExtraEmptyScreen();
425 screenId = workspace.commitExtraEmptyScreen();
426 layout = workspace.getScreenWithId(screenId);
427 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
428
429 if (!found) {
430 Log.wtf(TAG, "Not enough space on an empty screen");
431 }
432 return screenId;
433 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434}