blob: eeec8c580539108399f540db1385968e647aa369 [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) {
222 AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
223 ArrayList<Integer> actions = new ArrayList<>();
224
225 CellLayout layout = (CellLayout) host.getParent().getParent();
226 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
227 if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
228 layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
229 actions.add(R.string.action_increase_width);
230 }
231
232 if (info.spanX > info.minSpanX && info.spanX > 1) {
233 actions.add(R.string.action_decrease_width);
234 }
235 }
236
237 if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
238 if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
239 layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
240 actions.add(R.string.action_increase_height);
241 }
242
243 if (info.spanY > info.minSpanY && info.spanY > 1) {
244 actions.add(R.string.action_decrease_height);
245 }
246 }
247 return actions;
248 }
249
250 private void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
251 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
252 CellLayout layout = (CellLayout) host.getParent().getParent();
253 layout.markCellsAsUnoccupiedForView(host);
254
255 if (action == R.string.action_increase_width) {
256 if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
257 && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
258 || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
259 lp.cellX --;
260 info.cellX --;
261 }
262 lp.cellHSpan ++;
263 info.spanX ++;
264 } else if (action == R.string.action_decrease_width) {
265 lp.cellHSpan --;
266 info.spanX --;
267 } else if (action == R.string.action_increase_height) {
268 if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
269 lp.cellY --;
270 info.cellY --;
271 }
272 lp.cellVSpan ++;
273 info.spanY ++;
274 } else if (action == R.string.action_decrease_height) {
275 lp.cellVSpan --;
276 info.spanY --;
277 }
278
279 layout.markCellsAsOccupiedForView(host);
280 Rect sizeRange = new Rect();
281 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
282 ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
283 sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
284 host.requestLayout();
285 LauncherModel.updateItemInDatabase(mLauncher, info);
286 announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
287 }
288
Adam Cohen091440a2015-03-18 14:16:05 -0700289 @Thunk void announceConfirmation(int resId) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800290 announceConfirmation(mLauncher.getResources().getString(resId));
291 }
292
Adam Cohen091440a2015-03-18 14:16:05 -0700293 @Thunk void announceConfirmation(String confirmation) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800294 mLauncher.getDragLayer().announceForAccessibility(confirmation);
295
296 }
297
298 public boolean isInAccessibleDrag() {
299 return mDragInfo != null;
300 }
301
302 public DragInfo getDragInfo() {
303 return mDragInfo;
304 }
305
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700306 /**
307 * @param clickedTarget the actual view that was clicked
308 * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
309 * as the actual drop location otherwise the views center is used.
310 */
311 public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800312 String confirmation) {
313 if (!isInAccessibleDrag()) return;
314
315 int[] loc = new int[2];
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700316 if (dropLocation == null) {
317 loc[0] = clickedTarget.getWidth() / 2;
318 loc[1] = clickedTarget.getHeight() / 2;
319 } else {
320 loc[0] = dropLocation.centerX();
321 loc[1] = dropLocation.centerY();
322 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800323
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700324 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800325 mLauncher.getDragController().completeAccessibleDrag(loc);
326
327 endAccessibleDrag();
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700328 if (!TextUtils.isEmpty(confirmation)) {
329 announceConfirmation(confirmation);
330 }
Adam Cohenc9735cf2015-01-23 16:11:55 -0800331 }
332
333 public void beginAccessibleDrag(View item, ItemInfo info) {
334 mDragInfo = new DragInfo();
335 mDragInfo.info = info;
336 mDragInfo.item = item;
337 mDragInfo.dragType = DragType.ICON;
338 if (info instanceof FolderInfo) {
339 mDragInfo.dragType = DragType.FOLDER;
340 } else if (info instanceof LauncherAppWidgetInfo) {
341 mDragInfo.dragType = DragType.WIDGET;
342 }
343
344 CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info);
345
346 Rect pos = new Rect();
347 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800348 mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
Sunny Goyale9b651e2015-04-24 11:44:51 -0700349
350 Workspace workspace = mLauncher.getWorkspace();
351
352 Folder folder = workspace.getOpenFolder();
353 if (folder != null) {
354 if (folder.getItemsInReadingOrder().contains(item)) {
355 mDragSource = folder;
356 } else {
357 mLauncher.closeFolder();
358 }
359 }
360 if (mDragSource == null) {
361 mDragSource = workspace;
362 }
363 mDragSource.enableAccessibleDrag(true);
364 mDragSource.startDrag(cellInfo, true);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800365 }
366
367 public boolean onBackPressed() {
368 if (isInAccessibleDrag()) {
369 cancelAccessibleDrag();
370 return true;
371 }
372 return false;
373 }
374
375 private void cancelAccessibleDrag() {
376 mLauncher.getDragController().cancelDrag();
377 endAccessibleDrag();
378 }
379
380 private void endAccessibleDrag() {
381 mDragInfo = null;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700382 if (mDragSource != null) {
383 mDragSource.enableAccessibleDrag(false);
384 mDragSource = null;
385 }
386 }
387
388 public static interface AccessibilityDragSource {
389 void startDrag(CellLayout.CellInfo cellInfo, boolean accessible);
390
391 void enableAccessibleDrag(boolean enable);
Adam Cohenc9735cf2015-01-23 16:11:55 -0800392 }
Sunny Goyala9116722015-04-29 13:55:58 -0700393
394 /**
395 * Find empty space on the workspace and returns the screenId.
396 */
397 private long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
398 Workspace workspace = mLauncher.getWorkspace();
399 ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
400 long screenId;
401
402 // First check if there is space on the current screen.
403 int screenIndex = workspace.getCurrentPage();
404 screenId = workspaceScreens.get(screenIndex);
405 CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
406
407 boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
408 screenIndex = workspace.hasCustomContent() ? 1 : 0;
409 while (!found && screenIndex < workspaceScreens.size()) {
410 screenId = workspaceScreens.get(screenIndex);
411 layout = (CellLayout) workspace.getPageAt(screenIndex);
412 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
413 screenIndex++;
414 }
415
416 if (found) {
417 return screenId;
418 }
419
420 workspace.addExtraEmptyScreen();
421 screenId = workspace.commitExtraEmptyScreen();
422 layout = workspace.getScreenWithId(screenId);
423 found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
424
425 if (!found) {
426 Log.wtf(TAG, "Not enough space on an empty screen");
427 }
428 return screenId;
429 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430}