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