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