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