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; |
| 23 | import com.android.launcher3.Folder; |
| 24 | import com.android.launcher3.FolderInfo; |
| 25 | import com.android.launcher3.InfoDropTarget; |
| 26 | import com.android.launcher3.ItemInfo; |
| 27 | import com.android.launcher3.Launcher; |
| 28 | import com.android.launcher3.LauncherAppWidgetHostView; |
| 29 | import com.android.launcher3.LauncherAppWidgetInfo; |
| 30 | import com.android.launcher3.LauncherModel; |
| 31 | import com.android.launcher3.LauncherSettings; |
| 32 | import com.android.launcher3.PendingAddItemInfo; |
| 33 | import com.android.launcher3.R; |
| 34 | import com.android.launcher3.ShortcutInfo; |
| 35 | import com.android.launcher3.UninstallDropTarget; |
| 36 | import com.android.launcher3.Workspace; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 37 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 38 | |
| 39 | import java.util.ArrayList; |
| 40 | |
| 41 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 42 | public class LauncherAccessibilityDelegate extends AccessibilityDelegate { |
| 43 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 44 | 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 Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 51 | 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] | 52 | private static final int RESIZE = R.id.action_resize; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 53 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 54 | public enum DragType { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 55 | ICON, |
| 56 | FOLDER, |
| 57 | WIDGET |
| 58 | } |
| 59 | |
| 60 | public static class DragInfo { |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 61 | public DragType dragType; |
| 62 | public ItemInfo info; |
| 63 | public View item; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 66 | private final SparseArray<AccessibilityAction> mActions = new SparseArray<>(); |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 67 | @Thunk final Launcher mLauncher; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 68 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 69 | private DragInfo mDragInfo = null; |
| 70 | private AccessibilityDragSource mDragSource = null; |
| 71 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 72 | 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 83 | mActions.put(MOVE, new AccessibilityAction(MOVE, |
| 84 | launcher.getText(R.string.action_move))); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 85 | mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE, |
| 86 | launcher.getText(R.string.action_move_to_workspace))); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 87 | mActions.put(RESIZE, new AccessibilityAction(RESIZE, |
| 88 | launcher.getText(R.string.action_resize))); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 89 | } |
| 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 Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 97 | 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 Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 107 | if ((item instanceof ShortcutInfo) |
| 108 | || (item instanceof LauncherAppWidgetInfo) |
| 109 | || (item instanceof FolderInfo)) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 110 | info.addAction(mActions.get(MOVE)); |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 111 | |
| 112 | if (item.container >= 0) { |
| 113 | info.addAction(mActions.get(MOVE_TO_WORKSPACE)); |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 114 | } else if (item instanceof LauncherAppWidgetInfo) { |
| 115 | if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) { |
| 116 | info.addAction(mActions.get(RESIZE)); |
| 117 | } |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 118 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 119 | } if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 120 | 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 Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 133 | public boolean performAction(final View host, final ItemInfo item, int action) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 134 | if (action == REMOVE) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 135 | if (DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host)) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 136 | announceConfirmation(R.string.item_removed); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | return false; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 140 | } else if (action == INFO) { |
| 141 | InfoDropTarget.startDetailsActivityForInfo(item, mLauncher); |
| 142 | return true; |
| 143 | } else if (action == UNINSTALL) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 144 | return UninstallDropTarget.startUninstallActivity(mLauncher, item); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 145 | } else if (action == MOVE) { |
| 146 | beginAccessibleDrag(host, item); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 147 | } else if (action == ADD_TO_WORKSPACE) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 148 | final int[] coordinates = new int[2]; |
| 149 | final long screenId = findSpaceOnWorkspace(item, coordinates); |
| 150 | mLauncher.showWorkspace(true, new Runnable() { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 151 | |
| 152 | @Override |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 153 | 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 Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 169 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 170 | announceConfirmation(R.string.item_added_to_workspace); |
| 171 | } |
| 172 | }); |
| 173 | return true; |
Sunny Goyal | 9ae7777 | 2015-04-29 16:30:23 -0700 | [diff] [blame] | 174 | } 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 Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 198 | } 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 Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 217 | } |
| 218 | return false; |
| 219 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 220 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 221 | private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) { |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 222 | ArrayList<Integer> actions = new ArrayList<>(); |
| 223 | |
Sunny Goyal | d3d8c95 | 2015-06-01 10:09:06 -0700 | [diff] [blame^] | 224 | AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo(); |
| 225 | if (providerInfo == null) { |
| 226 | return actions; |
| 227 | } |
| 228 | |
Sunny Goyal | 9ca9c13 | 2015-04-29 14:57:22 -0700 | [diff] [blame] | 229 | 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 Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 293 | @Thunk void announceConfirmation(int resId) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 294 | announceConfirmation(mLauncher.getResources().getString(resId)); |
| 295 | } |
| 296 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 297 | @Thunk void announceConfirmation(String confirmation) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 298 | 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 Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 310 | /** |
| 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 316 | String confirmation) { |
| 317 | if (!isInAccessibleDrag()) return; |
| 318 | |
| 319 | int[] loc = new int[2]; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 320 | 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 327 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 328 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc); |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 329 | mLauncher.getDragController().completeAccessibleDrag(loc); |
| 330 | |
| 331 | endAccessibleDrag(); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 332 | if (!TextUtils.isEmpty(confirmation)) { |
| 333 | announceConfirmation(confirmation); |
| 334 | } |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 335 | } |
| 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 352 | mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY()); |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 353 | |
| 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 369 | } |
| 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 Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 386 | 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 Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame] | 396 | } |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 397 | |
| 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 Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 434 | } |