Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 1 | package com.android.launcher3.accessibility; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 2 | |
| 3 | import android.annotation.TargetApi; |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 4 | import android.app.AlertDialog; |
| 5 | import android.appwidget.AppWidgetProviderInfo; |
| 6 | import android.content.DialogInterface; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 7 | import android.graphics.Rect; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 8 | import android.os.Build; |
| 9 | import android.os.Bundle; |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 10 | import android.os.Handler; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 11 | import android.text.TextUtils; |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 12 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 13 | import android.util.SparseArray; |
| 14 | import android.view.View; |
| 15 | import android.view.View.AccessibilityDelegate; |
| 16 | import android.view.accessibility.AccessibilityNodeInfo; |
| 17 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
| 18 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 19 | import com.android.launcher3.AppInfo; |
| 20 | import com.android.launcher3.AppWidgetResizeFrame; |
| 21 | import com.android.launcher3.CellLayout; |
| 22 | import com.android.launcher3.DeleteDropTarget; |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 23 | import com.android.launcher3.DragSource; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 24 | import com.android.launcher3.folder.Folder; |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 25 | import com.android.launcher3.FolderInfo; |
| 26 | import com.android.launcher3.InfoDropTarget; |
| 27 | import com.android.launcher3.ItemInfo; |
| 28 | import com.android.launcher3.Launcher; |
| 29 | import com.android.launcher3.LauncherAppWidgetHostView; |
| 30 | import com.android.launcher3.LauncherAppWidgetInfo; |
| 31 | import com.android.launcher3.LauncherModel; |
| 32 | import com.android.launcher3.LauncherSettings; |
| 33 | import com.android.launcher3.PendingAddItemInfo; |
| 34 | import com.android.launcher3.R; |
| 35 | import com.android.launcher3.ShortcutInfo; |
| 36 | import com.android.launcher3.UninstallDropTarget; |
| 37 | import com.android.launcher3.Workspace; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 38 | import com.android.launcher3.dragndrop.DragController.DragListener; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 39 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 40 | |
| 41 | import java.util.ArrayList; |
| 42 | |
| 43 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 44 | public class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 45 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 46 | private static final String TAG = "LauncherAccessibilityDelegate"; |
| 47 | |
| 48 | private static final int REMOVE = R.id.action_remove; |
| 49 | private static final int INFO = R.id.action_info; |
| 50 | private static final int UNINSTALL = R.id.action_uninstall; |
| 51 | private static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace; |
| 52 | private static final int MOVE = R.id.action_move; |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 53 | private static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace; |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 54 | private static final int RESIZE = R.id.action_resize; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 55 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 56 | public enum DragType { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 57 | ICON, |
| 58 | FOLDER, |
| 59 | WIDGET |
| 60 | } |
| 61 | |
| 62 | public static class DragInfo { |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 63 | public DragType dragType; |
| 64 | public ItemInfo info; |
| 65 | public View item; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 68 | private final SparseArray<AccessibilityAction> mActions = new SparseArray<>(); |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 69 | @Thunk final Launcher mLauncher; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 70 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 71 | private DragInfo mDragInfo = null; |
| 72 | private AccessibilityDragSource mDragSource = null; |
| 73 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 74 | public LauncherAccessibilityDelegate(Launcher launcher) { |
| 75 | mLauncher = launcher; |
| 76 | |
| 77 | mActions.put(REMOVE, new AccessibilityAction(REMOVE, |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 78 | launcher.getText(R.string.remove_drop_target_label))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 79 | mActions.put(INFO, new AccessibilityAction(INFO, |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 80 | launcher.getText(R.string.app_info_drop_target_label))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 81 | mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL, |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 82 | launcher.getText(R.string.uninstall_drop_target_label))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 83 | mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE, |
| 84 | launcher.getText(R.string.action_add_to_workspace))); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 85 | mActions.put(MOVE, new AccessibilityAction(MOVE, |
| 86 | launcher.getText(R.string.action_move))); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 87 | mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE, |
| 88 | launcher.getText(R.string.action_move_to_workspace))); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 89 | mActions.put(RESIZE, new AccessibilityAction(RESIZE, |
| 90 | launcher.getText(R.string.action_resize))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 95 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 96 | if (!(host.getTag() instanceof ItemInfo)) return; |
| 97 | ItemInfo item = (ItemInfo) host.getTag(); |
| 98 | |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 99 | if (DeleteDropTarget.supportsAccessibleDrop(item)) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 100 | info.addAction(mActions.get(REMOVE)); |
| 101 | } |
| 102 | if (UninstallDropTarget.supportsDrop(host.getContext(), item)) { |
| 103 | info.addAction(mActions.get(UNINSTALL)); |
| 104 | } |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 105 | if (InfoDropTarget.supportsDrop(item)) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 106 | info.addAction(mActions.get(INFO)); |
| 107 | } |
| 108 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 109 | if ((item instanceof ShortcutInfo) |
| 110 | || (item instanceof LauncherAppWidgetInfo) |
| 111 | || (item instanceof FolderInfo)) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 112 | info.addAction(mActions.get(MOVE)); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 113 | |
| 114 | if (item.container >= 0) { |
| 115 | info.addAction(mActions.get(MOVE_TO_WORKSPACE)); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 116 | } else if (item instanceof LauncherAppWidgetInfo) { |
| 117 | if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) { |
| 118 | info.addAction(mActions.get(RESIZE)); |
| 119 | } |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 120 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 121 | } if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 122 | info.addAction(mActions.get(ADD_TO_WORKSPACE)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean performAccessibilityAction(View host, int action, Bundle args) { |
| 128 | if ((host.getTag() instanceof ItemInfo) |
| 129 | && performAction(host, (ItemInfo) host.getTag(), action)) { |
| 130 | return true; |
| 131 | } |
| 132 | return super.performAccessibilityAction(host, action, args); |
| 133 | } |
| 134 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 135 | public boolean performAction(final View host, final ItemInfo item, int action) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 136 | if (action == REMOVE) { |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 137 | DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host); |
| 138 | return true; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 139 | } else if (action == INFO) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 140 | InfoDropTarget.startDetailsActivityForInfo(item, mLauncher, null); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 141 | return true; |
| 142 | } else if (action == UNINSTALL) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 143 | return UninstallDropTarget.startUninstallActivity(mLauncher, item); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 144 | } else if (action == MOVE) { |
| 145 | beginAccessibleDrag(host, item); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 146 | } else if (action == ADD_TO_WORKSPACE) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 147 | final int[] coordinates = new int[2]; |
| 148 | final long screenId = findSpaceOnWorkspace(item, coordinates); |
| 149 | mLauncher.showWorkspace(true, new Runnable() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 150 | |
| 151 | @Override |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 152 | public void run() { |
| 153 | if (item instanceof AppInfo) { |
| 154 | ShortcutInfo info = ((AppInfo) item).makeShortcut(); |
| 155 | LauncherModel.addItemToDatabase(mLauncher, info, |
| 156 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 157 | screenId, coordinates[0], coordinates[1]); |
| 158 | |
| 159 | ArrayList<ItemInfo> itemList = new ArrayList<>(); |
| 160 | itemList.add(info); |
| 161 | mLauncher.bindItems(itemList, 0, itemList.size(), true); |
| 162 | } else if (item instanceof PendingAddItemInfo) { |
| 163 | PendingAddItemInfo info = (PendingAddItemInfo) item; |
| 164 | Workspace workspace = mLauncher.getWorkspace(); |
| 165 | workspace.snapToPage(workspace.getPageIndexForScreenId(screenId)); |
| 166 | mLauncher.addPendingItem(info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 167 | screenId, coordinates, info.spanX, info.spanY); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 168 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 169 | announceConfirmation(R.string.item_added_to_workspace); |
| 170 | } |
| 171 | }); |
| 172 | return true; |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 173 | } else if (action == MOVE_TO_WORKSPACE) { |
| 174 | Folder folder = mLauncher.getWorkspace().getOpenFolder(); |
Sunny Goyal | 935fca1 | 2015-10-13 10:19:01 -0700 | [diff] [blame] | 175 | mLauncher.closeFolder(folder, true); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 176 | ShortcutInfo info = (ShortcutInfo) item; |
Sunny Goyal | c52ba71 | 2016-04-05 15:59:05 -0700 | [diff] [blame^] | 177 | folder.getInfo().remove(info, false); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 178 | |
| 179 | final int[] coordinates = new int[2]; |
| 180 | final long screenId = findSpaceOnWorkspace(item, coordinates); |
| 181 | LauncherModel.moveItemInDatabase(mLauncher, info, |
| 182 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 183 | screenId, coordinates[0], coordinates[1]); |
| 184 | |
| 185 | // Bind the item in next frame so that if a new workspace page was created, |
| 186 | // it will get laid out. |
| 187 | new Handler().post(new Runnable() { |
| 188 | |
| 189 | @Override |
| 190 | public void run() { |
| 191 | ArrayList<ItemInfo> itemList = new ArrayList<>(); |
| 192 | itemList.add(item); |
| 193 | mLauncher.bindItems(itemList, 0, itemList.size(), true); |
| 194 | announceConfirmation(R.string.item_moved); |
| 195 | } |
| 196 | }); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 197 | } else if (action == RESIZE) { |
| 198 | final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item; |
| 199 | final ArrayList<Integer> actions = getSupportedResizeActions(host, info); |
| 200 | CharSequence[] labels = new CharSequence[actions.size()]; |
| 201 | for (int i = 0; i < actions.size(); i++) { |
| 202 | labels[i] = mLauncher.getText(actions.get(i)); |
| 203 | } |
| 204 | |
| 205 | new AlertDialog.Builder(mLauncher) |
| 206 | .setTitle(R.string.action_resize) |
| 207 | .setItems(labels, new DialogInterface.OnClickListener() { |
| 208 | |
| 209 | @Override |
| 210 | public void onClick(DialogInterface dialog, int which) { |
| 211 | performResizeAction(actions.get(which), host, info); |
| 212 | dialog.dismiss(); |
| 213 | } |
| 214 | }) |
| 215 | .show(); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 216 | } |
| 217 | return false; |
| 218 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 219 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 220 | private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) { |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 221 | ArrayList<Integer> actions = new ArrayList<>(); |
| 222 | |
Sunny Goyal | d3d8c95 | 2015-06-01 10:09:06 -0700 | [diff] [blame] | 223 | AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo(); |
| 224 | if (providerInfo == null) { |
| 225 | return actions; |
| 226 | } |
| 227 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 228 | CellLayout layout = (CellLayout) host.getParent().getParent(); |
| 229 | if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) { |
| 230 | if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) || |
| 231 | layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) { |
| 232 | actions.add(R.string.action_increase_width); |
| 233 | } |
| 234 | |
| 235 | if (info.spanX > info.minSpanX && info.spanX > 1) { |
| 236 | actions.add(R.string.action_decrease_width); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) { |
| 241 | if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) || |
| 242 | layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) { |
| 243 | actions.add(R.string.action_increase_height); |
| 244 | } |
| 245 | |
| 246 | if (info.spanY > info.minSpanY && info.spanY > 1) { |
| 247 | actions.add(R.string.action_decrease_height); |
| 248 | } |
| 249 | } |
| 250 | return actions; |
| 251 | } |
| 252 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 253 | @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) { |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 254 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams(); |
| 255 | CellLayout layout = (CellLayout) host.getParent().getParent(); |
| 256 | layout.markCellsAsUnoccupiedForView(host); |
| 257 | |
| 258 | if (action == R.string.action_increase_width) { |
| 259 | if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) |
| 260 | && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) |
| 261 | || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) { |
| 262 | lp.cellX --; |
| 263 | info.cellX --; |
| 264 | } |
| 265 | lp.cellHSpan ++; |
| 266 | info.spanX ++; |
| 267 | } else if (action == R.string.action_decrease_width) { |
| 268 | lp.cellHSpan --; |
| 269 | info.spanX --; |
| 270 | } else if (action == R.string.action_increase_height) { |
| 271 | if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) { |
| 272 | lp.cellY --; |
| 273 | info.cellY --; |
| 274 | } |
| 275 | lp.cellVSpan ++; |
| 276 | info.spanY ++; |
| 277 | } else if (action == R.string.action_decrease_height) { |
| 278 | lp.cellVSpan --; |
| 279 | info.spanY --; |
| 280 | } |
| 281 | |
| 282 | layout.markCellsAsOccupiedForView(host); |
| 283 | Rect sizeRange = new Rect(); |
| 284 | AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange); |
| 285 | ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null, |
| 286 | sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom); |
| 287 | host.requestLayout(); |
| 288 | LauncherModel.updateItemInDatabase(mLauncher, info); |
| 289 | announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY)); |
| 290 | } |
| 291 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 292 | @Thunk void announceConfirmation(int resId) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 293 | announceConfirmation(mLauncher.getResources().getString(resId)); |
| 294 | } |
| 295 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 296 | @Thunk void announceConfirmation(String confirmation) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 297 | mLauncher.getDragLayer().announceForAccessibility(confirmation); |
| 298 | |
| 299 | } |
| 300 | |
| 301 | public boolean isInAccessibleDrag() { |
| 302 | return mDragInfo != null; |
| 303 | } |
| 304 | |
| 305 | public DragInfo getDragInfo() { |
| 306 | return mDragInfo; |
| 307 | } |
| 308 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 309 | /** |
| 310 | * @param clickedTarget the actual view that was clicked |
| 311 | * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used |
| 312 | * as the actual drop location otherwise the views center is used. |
| 313 | */ |
| 314 | public void handleAccessibleDrop(View clickedTarget, Rect dropLocation, |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 315 | String confirmation) { |
| 316 | if (!isInAccessibleDrag()) return; |
| 317 | |
| 318 | int[] loc = new int[2]; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 319 | if (dropLocation == null) { |
| 320 | loc[0] = clickedTarget.getWidth() / 2; |
| 321 | loc[1] = clickedTarget.getHeight() / 2; |
| 322 | } else { |
| 323 | loc[0] = dropLocation.centerX(); |
| 324 | loc[1] = dropLocation.centerY(); |
| 325 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 326 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 327 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 328 | mLauncher.getDragController().completeAccessibleDrag(loc); |
| 329 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 330 | if (!TextUtils.isEmpty(confirmation)) { |
| 331 | announceConfirmation(confirmation); |
| 332 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | public void beginAccessibleDrag(View item, ItemInfo info) { |
| 336 | mDragInfo = new DragInfo(); |
| 337 | mDragInfo.info = info; |
| 338 | mDragInfo.item = item; |
| 339 | mDragInfo.dragType = DragType.ICON; |
| 340 | if (info instanceof FolderInfo) { |
| 341 | mDragInfo.dragType = DragType.FOLDER; |
| 342 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 343 | mDragInfo.dragType = DragType.WIDGET; |
| 344 | } |
| 345 | |
| 346 | CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info); |
| 347 | |
| 348 | Rect pos = new Rect(); |
| 349 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 350 | mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY()); |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 351 | |
| 352 | Workspace workspace = mLauncher.getWorkspace(); |
| 353 | |
| 354 | Folder folder = workspace.getOpenFolder(); |
| 355 | if (folder != null) { |
| 356 | if (folder.getItemsInReadingOrder().contains(item)) { |
| 357 | mDragSource = folder; |
| 358 | } else { |
| 359 | mLauncher.closeFolder(); |
| 360 | } |
| 361 | } |
| 362 | if (mDragSource == null) { |
| 363 | mDragSource = workspace; |
| 364 | } |
| 365 | mDragSource.enableAccessibleDrag(true); |
| 366 | mDragSource.startDrag(cellInfo, true); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 367 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 368 | if (mLauncher.getDragController().isDragging()) { |
| 369 | mLauncher.getDragController().addDragListener(this); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 370 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 373 | |
| 374 | @Override |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 375 | public void onDragStart(DragSource source, ItemInfo info, int dragAction) { |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 376 | // No-op |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Sunny Goyal | 4547802 | 2015-06-08 16:52:41 -0700 | [diff] [blame] | 379 | @Override |
| 380 | public void onDragEnd() { |
| 381 | mLauncher.getDragController().removeDragListener(this); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 382 | mDragInfo = null; |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 383 | if (mDragSource != null) { |
| 384 | mDragSource.enableAccessibleDrag(false); |
| 385 | mDragSource = null; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | public static interface AccessibilityDragSource { |
| 390 | void startDrag(CellLayout.CellInfo cellInfo, boolean accessible); |
| 391 | |
| 392 | void enableAccessibleDrag(boolean enable); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 393 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 394 | |
| 395 | /** |
| 396 | * Find empty space on the workspace and returns the screenId. |
| 397 | */ |
| 398 | private long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) { |
| 399 | Workspace workspace = mLauncher.getWorkspace(); |
| 400 | ArrayList<Long> workspaceScreens = workspace.getScreenOrder(); |
| 401 | long screenId; |
| 402 | |
| 403 | // First check if there is space on the current screen. |
| 404 | int screenIndex = workspace.getCurrentPage(); |
| 405 | screenId = workspaceScreens.get(screenIndex); |
| 406 | CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex); |
| 407 | |
| 408 | boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 409 | screenIndex = workspace.hasCustomContent() ? 1 : 0; |
| 410 | while (!found && screenIndex < workspaceScreens.size()) { |
| 411 | screenId = workspaceScreens.get(screenIndex); |
| 412 | layout = (CellLayout) workspace.getPageAt(screenIndex); |
| 413 | found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 414 | screenIndex++; |
| 415 | } |
| 416 | |
| 417 | if (found) { |
| 418 | return screenId; |
| 419 | } |
| 420 | |
| 421 | workspace.addExtraEmptyScreen(); |
| 422 | screenId = workspace.commitExtraEmptyScreen(); |
| 423 | layout = workspace.getScreenWithId(screenId); |
| 424 | found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY); |
| 425 | |
| 426 | if (!found) { |
| 427 | Log.wtf(TAG, "Not enough space on an empty screen"); |
| 428 | } |
| 429 | return screenId; |
| 430 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 431 | } |