1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.tapl; 17 18 import android.graphics.Point; 19 20 import java.util.function.Supplier; 21 22 /** {@link Launchable} that can serve as a source for dragging and dropping to the workspace. */ 23 interface WorkspaceDragSource { 24 25 /** 26 * Drags an object to the center of homescreen. 27 * 28 * @param startsActivity whether it's expected to start an activity. 29 * @param isWidgetShortcut whether we drag a widget shortcut 30 */ dragToWorkspace(boolean startsActivity, boolean isWidgetShortcut)31 default void dragToWorkspace(boolean startsActivity, boolean isWidgetShortcut) { 32 Launchable launchable = getLaunchable(); 33 LauncherInstrumentation launcher = launchable.mLauncher; 34 try (LauncherInstrumentation.Closable e = launcher.eventsCheck()) { 35 internalDragToWorkspace(startsActivity, isWidgetShortcut); 36 } 37 } 38 39 /** 40 * TODO(Redesign WorkspaceDragSource to have actual private methods) 41 * Temporary private method 42 * 43 * @param startsActivity whether it's expected to start an activity. 44 * @param isWidgetShortcut whether we drag a widget shortcut 45 */ internalDragToWorkspace(boolean startsActivity, boolean isWidgetShortcut)46 default void internalDragToWorkspace(boolean startsActivity, boolean isWidgetShortcut) { 47 Launchable launchable = getLaunchable(); 48 LauncherInstrumentation launcher = launchable.mLauncher; 49 final Point launchableCenter = launchable.getObject().getVisibleCenter(); 50 final Point displaySize = launcher.getRealDisplaySize(); 51 final int width = displaySize.x / 2; 52 Workspace.dragIconToWorkspace( 53 launcher, 54 launchable, 55 () -> new Point( 56 launchableCenter.x >= width 57 ? launchableCenter.x - width / 2 58 : launchableCenter.x + width / 2, 59 displaySize.y / 2), 60 startsActivity, 61 isWidgetShortcut, 62 launchable::addExpectedEventsForLongClick); 63 } 64 65 /** 66 * Drag an object to the given cell in workspace. The target cell must be empty. 67 * 68 * @param cellX zero based column number, starting from the left of the screen. 69 * @param cellY zero based row number, starting from the top of the screen. * 70 */ dragToWorkspace(int cellX, int cellY)71 default HomeAppIcon dragToWorkspace(int cellX, int cellY) { 72 Launchable launchable = getLaunchable(); 73 final String iconName = launchable.getObject().getText(); 74 LauncherInstrumentation launcher = launchable.mLauncher; 75 try (LauncherInstrumentation.Closable e = launcher.eventsCheck(); 76 LauncherInstrumentation.Closable c = launcher.addContextLayer( 77 String.format("want to drag the icon to cell(%d, %d)", cellX, cellY))) { 78 final Supplier<Point> dest = () -> Workspace.getCellCenter(launcher, cellX, cellY); 79 Workspace.dragIconToWorkspace( 80 launcher, 81 launchable, 82 dest, 83 launchable::addExpectedEventsForLongClick, 84 /*expectDropEvents= */ null, 85 /* startsActivity = */ false); 86 87 try (LauncherInstrumentation.Closable ignore = launcher.addContextLayer("dragged")) { 88 WorkspaceAppIcon appIcon = 89 (WorkspaceAppIcon) launcher.getWorkspace().getWorkspaceAppIcon(iconName); 90 launcher.assertTrue( 91 String.format( 92 "The %s icon should be in the cell (%d, %d).", iconName, cellX, 93 cellY), 94 appIcon.isInCell(cellX, cellY)); 95 return appIcon; 96 } 97 } 98 } 99 100 /** This method requires public access, however should not be called in tests. */ getLaunchable()101 Launchable getLaunchable(); 102 } 103