183a8f042adda926489494dff217c15ab696139b4Sunny Goyalpackage com.android.launcher3.accessibility;
271b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
371b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport android.annotation.TargetApi;
49ca9c1316da8382c1f663973072731033b5e533aSunny Goyalimport android.app.AlertDialog;
59ca9c1316da8382c1f663973072731033b5e533aSunny Goyalimport android.appwidget.AppWidgetProviderInfo;
69ca9c1316da8382c1f663973072731033b5e533aSunny Goyalimport android.content.DialogInterface;
7c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohenimport android.graphics.Rect;
871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport android.os.Build;
971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport android.os.Bundle;
109ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyalimport android.os.Handler;
111a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyalimport android.text.TextUtils;
12a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyalimport android.util.Log;
1371b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport android.util.SparseArray;
1471b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport android.view.View;
1571b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport android.view.View.AccessibilityDelegate;
1671b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport android.view.accessibility.AccessibilityNodeInfo;
1771b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
1871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
1983a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.AppInfo;
2083a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.AppWidgetResizeFrame;
213ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyalimport com.android.launcher3.BubbleTextView;
2283a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.CellLayout;
2383a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.DeleteDropTarget;
2445478022977de9025dfc887cc1507d90d15febf8Sunny Goyalimport com.android.launcher3.DragSource;
2594b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyalimport com.android.launcher3.DropTarget.DragObject;
2694b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyalimport com.android.launcher3.dragndrop.DragOptions;
27261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.folder.Folder;
2883a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.FolderInfo;
2983a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.InfoDropTarget;
3083a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.ItemInfo;
3183a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.Launcher;
3283a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.LauncherAppWidgetHostView;
3383a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.LauncherAppWidgetInfo;
3483a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.LauncherModel;
3583a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.LauncherSettings;
3683a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.PendingAddItemInfo;
3783a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.R;
3883a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.ShortcutInfo;
3983a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.UninstallDropTarget;
4083a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.Workspace;
41fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragController.DragListener;
423ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyalimport com.android.launcher3.shortcuts.DeepShortcutTextView;
433ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyalimport com.android.launcher3.shortcuts.DeepShortcutsContainer;
44091440a9cb9d4f42406631004aa484cbb79214caAdam Cohenimport com.android.launcher3.util.Thunk;
4571b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
4671b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyalimport java.util.ArrayList;
4771b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
4871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal@TargetApi(Build.VERSION_CODES.LOLLIPOP)
4945478022977de9025dfc887cc1507d90d15febf8Sunny Goyalpublic class LauncherAccessibilityDelegate extends AccessibilityDelegate implements DragListener {
5071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
51a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal    private static final String TAG = "LauncherAccessibilityDelegate";
52a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal
533ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected static final int REMOVE = R.id.action_remove;
543ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected static final int INFO = R.id.action_info;
553ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected static final int UNINSTALL = R.id.action_uninstall;
563ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
573ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected static final int MOVE = R.id.action_move;
583ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected static final int MOVE_TO_WORKSPACE = R.id.action_move_to_workspace;
593ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected static final int RESIZE = R.id.action_resize;
603ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected static final int DEEP_SHORTCUTS = R.id.action_deep_shortcuts;
61c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
62e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    public enum DragType {
63c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        ICON,
64c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        FOLDER,
65c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        WIDGET
66c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
67c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
68c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    public static class DragInfo {
69e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        public DragType dragType;
70e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        public ItemInfo info;
71e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        public View item;
72c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
73c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
743ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected final SparseArray<AccessibilityAction> mActions = new SparseArray<>();
75091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk final Launcher mLauncher;
7671b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
77e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    private DragInfo mDragInfo = null;
78e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
7971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    public LauncherAccessibilityDelegate(Launcher launcher) {
8071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        mLauncher = launcher;
8171b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
8271b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        mActions.put(REMOVE, new AccessibilityAction(REMOVE,
839aae47f8a6e23f1805ee3d44512fc1f80286e6ddTony Wickham                launcher.getText(R.string.remove_drop_target_label)));
8471b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        mActions.put(INFO, new AccessibilityAction(INFO,
859aae47f8a6e23f1805ee3d44512fc1f80286e6ddTony Wickham                launcher.getText(R.string.app_info_drop_target_label)));
8671b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
879aae47f8a6e23f1805ee3d44512fc1f80286e6ddTony Wickham                launcher.getText(R.string.uninstall_drop_target_label)));
8871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
8971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                launcher.getText(R.string.action_add_to_workspace)));
90c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mActions.put(MOVE, new AccessibilityAction(MOVE,
91c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen                launcher.getText(R.string.action_move)));
929ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal        mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,
939ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                launcher.getText(R.string.action_move_to_workspace)));
949ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        mActions.put(RESIZE, new AccessibilityAction(RESIZE,
959ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                        launcher.getText(R.string.action_resize)));
963ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal        mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,
973ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal                launcher.getText(R.string.action_deep_shortcut)));
9871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    }
9971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
10071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    @Override
10171b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
10271b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        super.onInitializeAccessibilityNodeInfo(host, info);
1033ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal        addActions(host, info);
1043ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    }
1053ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal
1063ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected void addActions(View host, AccessibilityNodeInfo info) {
10771b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        if (!(host.getTag() instanceof ItemInfo)) return;
10871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        ItemInfo item = (ItemInfo) host.getTag();
10971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
1103ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal        if (host instanceof BubbleTextView && ((BubbleTextView) host).hasDeepShortcuts()) {
1113ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal            info.addAction(mActions.get(DEEP_SHORTCUTS));
1123ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal        }
1133ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal
1149aae47f8a6e23f1805ee3d44512fc1f80286e6ddTony Wickham        if (DeleteDropTarget.supportsAccessibleDrop(item)) {
1151a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            info.addAction(mActions.get(REMOVE));
1161a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        }
1171a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        if (UninstallDropTarget.supportsDrop(host.getContext(), item)) {
1181a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            info.addAction(mActions.get(UNINSTALL));
1191a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        }
120d5bd67dfa9ee5fda2384a75231b7a68ceb8e9bd5Sunny Goyal        if (InfoDropTarget.supportsDrop(item)) {
1211a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            info.addAction(mActions.get(INFO));
1221a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        }
1231a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal
12471b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        if ((item instanceof ShortcutInfo)
12571b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                || (item instanceof LauncherAppWidgetInfo)
12671b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                || (item instanceof FolderInfo)) {
127c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            info.addAction(mActions.get(MOVE));
1289ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal
1299ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            if (item.container >= 0) {
1309ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                info.addAction(mActions.get(MOVE_TO_WORKSPACE));
1319ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            } else if (item instanceof LauncherAppWidgetInfo) {
1329ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                if (!getSupportedResizeActions(host, (LauncherAppWidgetInfo) item).isEmpty()) {
1339ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                    info.addAction(mActions.get(RESIZE));
1349ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                }
1359ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            }
1366e92f05314ae240dccd11347ea86529066b528b3Adam Cohen        }
1376e92f05314ae240dccd11347ea86529066b528b3Adam Cohen
1386e92f05314ae240dccd11347ea86529066b528b3Adam Cohen        if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
13971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal            info.addAction(mActions.get(ADD_TO_WORKSPACE));
14071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        }
14171b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    }
14271b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
14371b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    @Override
14471b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    public boolean performAccessibilityAction(View host, int action, Bundle args) {
14571b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        if ((host.getTag() instanceof ItemInfo)
14671b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                && performAction(host, (ItemInfo) host.getTag(), action)) {
14771b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal            return true;
14871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        }
14971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        return super.performAccessibilityAction(host, action, args);
15071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    }
15171b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
1529ca9c1316da8382c1f663973072731033b5e533aSunny Goyal    public boolean performAction(final View host, final ItemInfo item, int action) {
15371b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        if (action == REMOVE) {
154e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host);
155e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            return true;
15671b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        } else if (action == INFO) {
157d5bd67dfa9ee5fda2384a75231b7a68ceb8e9bd5Sunny Goyal            InfoDropTarget.startDetailsActivityForInfo(item, mLauncher, null);
15871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal            return true;
15971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        } else if (action == UNINSTALL) {
1601a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            return UninstallDropTarget.startUninstallActivity(mLauncher, item);
161c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        } else if (action == MOVE) {
162c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            beginAccessibleDrag(host, item);
16371b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        } else if (action == ADD_TO_WORKSPACE) {
164a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            final int[] coordinates = new int[2];
165a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            final long screenId = findSpaceOnWorkspace(item, coordinates);
166a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            mLauncher.showWorkspace(true, new Runnable() {
16771b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
16871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                @Override
169a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                public void run() {
170a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                    if (item instanceof AppInfo) {
171a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        ShortcutInfo info = ((AppInfo) item).makeShortcut();
172a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        LauncherModel.addItemToDatabase(mLauncher, info,
173a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                                LauncherSettings.Favorites.CONTAINER_DESKTOP,
174a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                                screenId, coordinates[0], coordinates[1]);
175a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal
176a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        ArrayList<ItemInfo> itemList = new ArrayList<>();
177a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        itemList.add(info);
178a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        mLauncher.bindItems(itemList, 0, itemList.size(), true);
179a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                    } else if (item instanceof PendingAddItemInfo) {
180a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        PendingAddItemInfo info = (PendingAddItemInfo) item;
181a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        Workspace workspace = mLauncher.getWorkspace();
182a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        workspace.snapToPage(workspace.getPageIndexForScreenId(screenId));
183a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                        mLauncher.addPendingItem(info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
184a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                                screenId, coordinates, info.spanX, info.spanY);
18571b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                    }
186a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                    announceConfirmation(R.string.item_added_to_workspace);
187a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal                }
188a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            });
189a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            return true;
1909ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal        } else if (action == MOVE_TO_WORKSPACE) {
1919ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            Folder folder = mLauncher.getWorkspace().getOpenFolder();
192935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal            mLauncher.closeFolder(folder, true);
1939ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            ShortcutInfo info = (ShortcutInfo) item;
194c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            folder.getInfo().remove(info, false);
1959ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal
1969ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            final int[] coordinates = new int[2];
1979ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            final long screenId = findSpaceOnWorkspace(item, coordinates);
1989ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            LauncherModel.moveItemInDatabase(mLauncher, info,
1999ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                    LauncherSettings.Favorites.CONTAINER_DESKTOP,
2009ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                    screenId, coordinates[0], coordinates[1]);
2019ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal
2029ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            // Bind the item in next frame so that if a new workspace page was created,
2039ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            // it will get laid out.
2049ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            new Handler().post(new Runnable() {
2059ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal
2069ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                @Override
2079ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                public void run() {
2089ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                    ArrayList<ItemInfo> itemList = new ArrayList<>();
2099ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                    itemList.add(item);
2109ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                    mLauncher.bindItems(itemList, 0, itemList.size(), true);
2119ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                    announceConfirmation(R.string.item_moved);
2129ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal                }
2139ae77771d751a5a7fe87f660ab70908b5f55d9fcSunny Goyal            });
2149ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        } else if (action == RESIZE) {
2159ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) item;
2169ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            final ArrayList<Integer> actions = getSupportedResizeActions(host, info);
2179ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            CharSequence[] labels = new CharSequence[actions.size()];
2189ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            for (int i = 0; i < actions.size(); i++) {
2199ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                labels[i] = mLauncher.getText(actions.get(i));
2209ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            }
2219ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
2229ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            new AlertDialog.Builder(mLauncher)
2239ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                .setTitle(R.string.action_resize)
2249ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                .setItems(labels, new DialogInterface.OnClickListener() {
2259ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
2269ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                    @Override
2279ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                    public void onClick(DialogInterface dialog, int which) {
2289ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                        performResizeAction(actions.get(which), host, info);
2299ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                        dialog.dismiss();
2309ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                    }
2319ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                })
2329ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                .show();
2333ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal            return true;
2343ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal        } else if (action == DEEP_SHORTCUTS) {
2353ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal            return DeepShortcutsContainer.showForIcon((BubbleTextView) host) != null;
23671b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        }
23771b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        return false;
23871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    }
239c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
2409ca9c1316da8382c1f663973072731033b5e533aSunny Goyal    private ArrayList<Integer> getSupportedResizeActions(View host, LauncherAppWidgetInfo info) {
2419ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        ArrayList<Integer> actions = new ArrayList<>();
2429ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
243d3d8c958a28aee7815fa2bc81b5c0b79903d51adSunny Goyal        AppWidgetProviderInfo providerInfo = ((LauncherAppWidgetHostView) host).getAppWidgetInfo();
244d3d8c958a28aee7815fa2bc81b5c0b79903d51adSunny Goyal        if (providerInfo == null) {
245d3d8c958a28aee7815fa2bc81b5c0b79903d51adSunny Goyal            return actions;
246d3d8c958a28aee7815fa2bc81b5c0b79903d51adSunny Goyal        }
247d3d8c958a28aee7815fa2bc81b5c0b79903d51adSunny Goyal
2489ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        CellLayout layout = (CellLayout) host.getParent().getParent();
2499ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
2509ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
2519ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                    layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
2529ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                actions.add(R.string.action_increase_width);
2539ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            }
2549ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
2559ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            if (info.spanX > info.minSpanX && info.spanX > 1) {
2569ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                actions.add(R.string.action_decrease_width);
2579ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            }
2589ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        }
2599ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
2609ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
2619ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
2629ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                    layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
2639ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                actions.add(R.string.action_increase_height);
2649ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            }
2659ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
2669ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            if (info.spanY > info.minSpanY && info.spanY > 1) {
2679ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                actions.add(R.string.action_decrease_height);
2689ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            }
2699ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        }
2709ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        return actions;
2719ca9c1316da8382c1f663973072731033b5e533aSunny Goyal    }
2729ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
273316490e636aad788fcfbfc2e04dd4f0e145bdd00Sunny Goyal    @Thunk void performResizeAction(int action, View host, LauncherAppWidgetInfo info) {
2749ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) host.getLayoutParams();
2759ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        CellLayout layout = (CellLayout) host.getParent().getParent();
2769ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        layout.markCellsAsUnoccupiedForView(host);
2779ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
2789ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        if (action == R.string.action_increase_width) {
2799ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            if (((host.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
2809ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                    && layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY))
2819ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                    || !layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY)) {
2829ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                lp.cellX --;
2839ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                info.cellX --;
2849ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            }
2859ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            lp.cellHSpan ++;
2869ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            info.spanX ++;
2879ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        } else if (action == R.string.action_decrease_width) {
2889ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            lp.cellHSpan --;
2899ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            info.spanX --;
2909ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        } else if (action == R.string.action_increase_height) {
2919ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            if (!layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1)) {
2929ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                lp.cellY --;
2939ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                info.cellY --;
2949ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            }
2959ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            lp.cellVSpan ++;
2969ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            info.spanY ++;
2979ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        } else if (action == R.string.action_decrease_height) {
2989ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            lp.cellVSpan --;
2999ca9c1316da8382c1f663973072731033b5e533aSunny Goyal            info.spanY --;
3009ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        }
3019ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
3029ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        layout.markCellsAsOccupiedForView(host);
3039ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        Rect sizeRange = new Rect();
3049ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, sizeRange);
3059ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        ((LauncherAppWidgetHostView) host).updateAppWidgetSize(null,
3069ca9c1316da8382c1f663973072731033b5e533aSunny Goyal                sizeRange.left, sizeRange.top, sizeRange.right, sizeRange.bottom);
3079ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        host.requestLayout();
3089ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        LauncherModel.updateItemInDatabase(mLauncher, info);
3099ca9c1316da8382c1f663973072731033b5e533aSunny Goyal        announceConfirmation(mLauncher.getString(R.string.widget_resized, info.spanX, info.spanY));
3109ca9c1316da8382c1f663973072731033b5e533aSunny Goyal    }
3119ca9c1316da8382c1f663973072731033b5e533aSunny Goyal
312091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk void announceConfirmation(int resId) {
313c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        announceConfirmation(mLauncher.getResources().getString(resId));
314c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
315c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
316091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk void announceConfirmation(String confirmation) {
317c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mLauncher.getDragLayer().announceForAccessibility(confirmation);
318c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
319c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
320c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
321c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    public boolean isInAccessibleDrag() {
322c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        return mDragInfo != null;
323c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
324c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
325c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    public DragInfo getDragInfo() {
326c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        return mDragInfo;
327c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
328c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
3291a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    /**
3301a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal     * @param clickedTarget the actual view that was clicked
3311a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal     * @param dropLocation relative to {@param clickedTarget}. If provided, its center is used
3321a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal     * as the actual drop location otherwise the views center is used.
3331a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal     */
3341a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    public void handleAccessibleDrop(View clickedTarget, Rect dropLocation,
335c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            String confirmation) {
336c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        if (!isInAccessibleDrag()) return;
337c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
338c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        int[] loc = new int[2];
3391a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        if (dropLocation == null) {
3401a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            loc[0] = clickedTarget.getWidth() / 2;
3411a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            loc[1] = clickedTarget.getHeight() / 2;
3421a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        } else {
3431a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            loc[0] = dropLocation.centerX();
3441a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            loc[1] = dropLocation.centerY();
3451a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        }
346c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
3471a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(clickedTarget, loc);
348c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mLauncher.getDragController().completeAccessibleDrag(loc);
349c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
3501a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        if (!TextUtils.isEmpty(confirmation)) {
3511a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            announceConfirmation(confirmation);
3521a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        }
353c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
354c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
355c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    public void beginAccessibleDrag(View item, ItemInfo info) {
356c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mDragInfo = new DragInfo();
357c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mDragInfo.info = info;
358c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mDragInfo.item = item;
359c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mDragInfo.dragType = DragType.ICON;
360c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        if (info instanceof FolderInfo) {
361c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            mDragInfo.dragType = DragType.FOLDER;
362c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        } else if (info instanceof LauncherAppWidgetInfo) {
363c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            mDragInfo.dragType = DragType.WIDGET;
364c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        }
365c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
366c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info);
367c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
368c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        Rect pos = new Rect();
369c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);
370c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
371e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
372e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        Workspace workspace = mLauncher.getWorkspace();
373e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
374e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        Folder folder = workspace.getOpenFolder();
375e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        if (folder != null) {
37694b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            if (!folder.getItemsInReadingOrder().contains(item)) {
377e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal                mLauncher.closeFolder();
37894b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                folder = null;
379e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal            }
380e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        }
381c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
38294b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        mLauncher.getDragController().addDragListener(this);
38394b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal
38494b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        DragOptions options = new DragOptions();
38594b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        options.isAccessibleDrag = true;
38694b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        if (folder != null) {
38794b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            folder.startDrag(cellInfo.cell, options);
38894b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        } else {
38994b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            workspace.startDrag(cellInfo, options);
390c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        }
391c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
392c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
39345478022977de9025dfc887cc1507d90d15febf8Sunny Goyal    @Override
39494b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal    public void onDragStart(DragObject dragObject, DragOptions options) {
39545478022977de9025dfc887cc1507d90d15febf8Sunny Goyal        // No-op
396c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
397c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
39845478022977de9025dfc887cc1507d90d15febf8Sunny Goyal    @Override
39945478022977de9025dfc887cc1507d90d15febf8Sunny Goyal    public void onDragEnd() {
40045478022977de9025dfc887cc1507d90d15febf8Sunny Goyal        mLauncher.getDragController().removeDragListener(this);
401c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        mDragInfo = null;
402c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
403a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal
404a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal    /**
405a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal     * Find empty space on the workspace and returns the screenId.
406a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal     */
4073ffa64df36221a7ae3946f11ced4dcfc487e18dcSunny Goyal    protected long findSpaceOnWorkspace(ItemInfo info, int[] outCoordinates) {
408a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        Workspace workspace = mLauncher.getWorkspace();
409a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        ArrayList<Long> workspaceScreens = workspace.getScreenOrder();
410a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        long screenId;
411a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal
412a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        // First check if there is space on the current screen.
413a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        int screenIndex = workspace.getCurrentPage();
414a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        screenId = workspaceScreens.get(screenIndex);
415a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        CellLayout layout = (CellLayout) workspace.getPageAt(screenIndex);
416a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal
417a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        boolean found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
418a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        screenIndex = workspace.hasCustomContent() ? 1 : 0;
419a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        while (!found && screenIndex < workspaceScreens.size()) {
420a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            screenId = workspaceScreens.get(screenIndex);
421a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            layout = (CellLayout) workspace.getPageAt(screenIndex);
422a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
423a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            screenIndex++;
424a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        }
425a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal
426a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        if (found) {
427a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            return screenId;
428a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        }
429a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal
430a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        workspace.addExtraEmptyScreen();
431a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        screenId = workspace.commitExtraEmptyScreen();
432a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        layout = workspace.getScreenWithId(screenId);
433a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        found = layout.findCellForSpan(outCoordinates, info.spanX, info.spanY);
434a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal
435a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        if (!found) {
436a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal            Log.wtf(TAG, "Not enough space on an empty screen");
437a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        }
438a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal        return screenId;
439a911672f45900fc0ed746e0d84c43c6e5ad89b6aSunny Goyal    }
44071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal}
441