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