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