Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/draganddrop/DragAndDropPolicy.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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
17 package com.android.wm.shell.draganddrop;
18
19 import static android.app.ActivityTaskManager.INVALID_TASK_ID;
20 import static android.app.ComponentOptions.KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED;
21 import static android.app.ComponentOptions.KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED_BY_PERMISSION;
22 import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
23 import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
24 import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
25 import static android.content.ClipDescription.EXTRA_ACTIVITY_OPTIONS;
26 import static android.content.ClipDescription.EXTRA_PENDING_INTENT;
27 import static android.content.ClipDescription.MIMETYPE_APPLICATION_SHORTCUT;
28 import static android.content.ClipDescription.MIMETYPE_APPLICATION_TASK;
29 import static android.content.Intent.EXTRA_PACKAGE_NAME;
30 import static android.content.Intent.EXTRA_SHORTCUT_ID;
31 import static android.content.Intent.EXTRA_TASK_ID;
32 import static android.content.Intent.EXTRA_USER;
33
34 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_BOTTOM_OR_RIGHT;
35 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_TOP_OR_LEFT;
36 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_UNDEFINED;
37 import static com.android.wm.shell.draganddrop.DragAndDropPolicy.Target.TYPE_FULLSCREEN;
38 import static com.android.wm.shell.draganddrop.DragAndDropPolicy.Target.TYPE_SPLIT_BOTTOM;
39 import static com.android.wm.shell.draganddrop.DragAndDropPolicy.Target.TYPE_SPLIT_LEFT;
40 import static com.android.wm.shell.draganddrop.DragAndDropPolicy.Target.TYPE_SPLIT_RIGHT;
41 import static com.android.wm.shell.draganddrop.DragAndDropPolicy.Target.TYPE_SPLIT_TOP;
42
43 import android.app.ActivityManager;
44 import android.app.ActivityTaskManager;
45 import android.app.PendingIntent;
46 import android.app.WindowConfiguration;
47 import android.content.ActivityNotFoundException;
48 import android.content.ClipData;
49 import android.content.ClipDescription;
50 import android.content.Context;
51 import android.content.Intent;
52 import android.content.pm.ActivityInfo;
53 import android.content.pm.LauncherApps;
54 import android.graphics.Insets;
55 import android.graphics.Rect;
56 import android.os.Bundle;
57 import android.os.RemoteException;
58 import android.os.UserHandle;
59 import android.util.Slog;
60
61 import androidx.annotation.IntDef;
62 import androidx.annotation.NonNull;
63 import androidx.annotation.Nullable;
64 import androidx.annotation.VisibleForTesting;
65
66 import com.android.internal.logging.InstanceId;
67 import com.android.wm.shell.R;
68 import com.android.wm.shell.common.DisplayLayout;
69 import com.android.wm.shell.common.split.SplitScreenConstants.SplitPosition;
70 import com.android.wm.shell.splitscreen.SplitScreenController;
71
72 import java.lang.annotation.Retention;
73 import java.lang.annotation.RetentionPolicy;
74 import java.util.ArrayList;
75 import java.util.List;
76
77 /**
78 * The policy for handling drag and drop operations to shell.
79 */
80 public class DragAndDropPolicy {
81
82 private static final String TAG = DragAndDropPolicy.class.getSimpleName();
83
84 private final Context mContext;
85 private final ActivityTaskManager mActivityTaskManager;
86 private final Starter mStarter;
87 private final SplitScreenController mSplitScreen;
88 private final ArrayList<DragAndDropPolicy.Target> mTargets = new ArrayList<>();
89
90 private InstanceId mLoggerSessionId;
91 private DragSession mSession;
92
93 public DragAndDropPolicy(Context context, SplitScreenController splitScreen) {
94 this(context, ActivityTaskManager.getInstance(), splitScreen, new DefaultStarter(context));
95 }
96
97 @VisibleForTesting
98 DragAndDropPolicy(Context context, ActivityTaskManager activityTaskManager,
99 SplitScreenController splitScreen, Starter starter) {
100 mContext = context;
101 mActivityTaskManager = activityTaskManager;
102 mSplitScreen = splitScreen;
103 mStarter = mSplitScreen != null ? mSplitScreen : starter;
104 }
105
106 /**
107 * Starts a new drag session with the given initial drag data.
108 */
109 void start(DisplayLayout displayLayout, ClipData data, InstanceId loggerSessionId) {
110 mLoggerSessionId = loggerSessionId;
111 mSession = new DragSession(mActivityTaskManager, displayLayout, data);
112 // TODO(b/169894807): Also update the session data with task stack changes
113 mSession.update();
114 }
115
116 /**
117 * Returns the last running task.
118 */
119 ActivityManager.RunningTaskInfo getLatestRunningTask() {
120 return mSession.runningTaskInfo;
121 }
122
123 /**
124 * Returns the number of targets.
125 */
126 int getNumTargets() {
127 return mTargets.size();
128 }
129
130 /**
131 * Returns the target's regions based on the current state of the device and display.
132 */
133 @NonNull
134 ArrayList<Target> getTargets(Insets insets) {
135 mTargets.clear();
136 if (mSession == null) {
137 // Return early if this isn't an app drag
138 return mTargets;
139 }
140
141 final int w = mSession.displayLayout.width();
142 final int h = mSession.displayLayout.height();
143 final int iw = w - insets.left - insets.right;
144 final int ih = h - insets.top - insets.bottom;
145 final int l = insets.left;
146 final int t = insets.top;
147 final Rect displayRegion = new Rect(l, t, l + iw, t + ih);
148 final Rect fullscreenDrawRegion = new Rect(displayRegion);
149 final Rect fullscreenHitRegion = new Rect(displayRegion);
150 final boolean inLandscape = mSession.displayLayout.isLandscape();
151 final boolean inSplitScreen = mSplitScreen != null && mSplitScreen.isSplitScreenVisible();
152 final float dividerWidth = mContext.getResources().getDimensionPixelSize(
153 R.dimen.split_divider_bar_width);
154 // We allow splitting if we are already in split-screen or the running task is a standard
155 // task in fullscreen mode.
156 final boolean allowSplit = inSplitScreen
157 || (mSession.runningTaskActType == ACTIVITY_TYPE_STANDARD
158 && mSession.runningTaskWinMode == WINDOWING_MODE_FULLSCREEN);
159 if (allowSplit) {
160 // Already split, allow replacing existing split task
161 final Rect topOrLeftBounds = new Rect();
162 final Rect bottomOrRightBounds = new Rect();
163 mSplitScreen.getStageBounds(topOrLeftBounds, bottomOrRightBounds);
164 topOrLeftBounds.intersect(displayRegion);
165 bottomOrRightBounds.intersect(displayRegion);
166
167 if (inLandscape) {
168 final Rect leftHitRegion = new Rect();
169 final Rect rightHitRegion = new Rect();
170
171 // If we have existing split regions use those bounds, otherwise split it 50/50
172 if (inSplitScreen) {
173 // The bounds of the existing split will have a divider bar, the hit region
174 // should include that space. Find the center of the divider bar:
175 float centerX = topOrLeftBounds.right + (dividerWidth / 2);
176 // Now set the hit regions using that center.
177 leftHitRegion.set(displayRegion);
178 leftHitRegion.right = (int) centerX;
179 rightHitRegion.set(displayRegion);
180 rightHitRegion.left = (int) centerX;
181 } else {
182 displayRegion.splitVertically(leftHitRegion, rightHitRegion);
183 }
184
185 mTargets.add(new Target(TYPE_SPLIT_LEFT, leftHitRegion, topOrLeftBounds));
186 mTargets.add(new Target(TYPE_SPLIT_RIGHT, rightHitRegion, bottomOrRightBounds));
187
188 } else {
189 final Rect topHitRegion = new Rect();
190 final Rect bottomHitRegion = new Rect();
191
192 // If we have existing split regions use those bounds, otherwise split it 50/50
193 if (inSplitScreen) {
194 // The bounds of the existing split will have a divider bar, the hit region
195 // should include that space. Find the center of the divider bar:
196 float centerX = topOrLeftBounds.bottom + (dividerWidth / 2);
197 // Now set the hit regions using that center.
198 topHitRegion.set(displayRegion);
199 topHitRegion.bottom = (int) centerX;
200 bottomHitRegion.set(displayRegion);
201 bottomHitRegion.top = (int) centerX;
202 } else {
203 displayRegion.splitHorizontally(topHitRegion, bottomHitRegion);
204 }
205
206 mTargets.add(new Target(TYPE_SPLIT_TOP, topHitRegion, topOrLeftBounds));
207 mTargets.add(new Target(TYPE_SPLIT_BOTTOM, bottomHitRegion, bottomOrRightBounds));
208 }
209 } else {
210 // Split-screen not allowed, so only show the fullscreen target
211 mTargets.add(new Target(TYPE_FULLSCREEN, fullscreenHitRegion, fullscreenDrawRegion));
212 }
213 return mTargets;
214 }
215
216 /**
217 * Returns the target at the given position based on the targets previously calculated.
218 */
219 @Nullable
220 Target getTargetAtLocation(int x, int y) {
221 for (int i = mTargets.size() - 1; i >= 0; i--) {
222 DragAndDropPolicy.Target t = mTargets.get(i);
223 if (t.hitRegion.contains(x, y)) {
224 return t;
225 }
226 }
227 return null;
228 }
229
230 @VisibleForTesting
231 void handleDrop(Target target, ClipData data) {
232 if (target == null || !mTargets.contains(target)) {
233 return;
234 }
235
236 final boolean leftOrTop = target.type == TYPE_SPLIT_TOP || target.type == TYPE_SPLIT_LEFT;
237
238 @SplitPosition int position = SPLIT_POSITION_UNDEFINED;
239 if (target.type != TYPE_FULLSCREEN && mSplitScreen != null) {
240 // Update launch options for the split side we are targeting.
241 position = leftOrTop ? SPLIT_POSITION_TOP_OR_LEFT : SPLIT_POSITION_BOTTOM_OR_RIGHT;
242 // Add some data for logging splitscreen once it is invoked
243 mSplitScreen.onDroppedToSplit(position, mLoggerSessionId);
244 }
245
246 final ClipDescription description = data.getDescription();
247 final Intent dragData = mSession.dragData;
248 startClipDescription(description, dragData, position);
249 }
250
251 private void startClipDescription(ClipDescription description, Intent intent,
252 @SplitPosition int position) {
253 final boolean isTask = description.hasMimeType(MIMETYPE_APPLICATION_TASK);
254 final boolean isShortcut = description.hasMimeType(MIMETYPE_APPLICATION_SHORTCUT);
255 final Bundle opts = intent.hasExtra(EXTRA_ACTIVITY_OPTIONS)
256 ? intent.getBundleExtra(EXTRA_ACTIVITY_OPTIONS) : new Bundle();
257
258 if (isTask) {
259 final int taskId = intent.getIntExtra(EXTRA_TASK_ID, INVALID_TASK_ID);
260 mStarter.startTask(taskId, position, opts);
261 } else if (isShortcut) {
262 final String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
263 final String id = intent.getStringExtra(EXTRA_SHORTCUT_ID);
264 final UserHandle user = intent.getParcelableExtra(EXTRA_USER);
265 mStarter.startShortcut(packageName, id, position, opts, user);
266 } else {
267 final PendingIntent launchIntent = intent.getParcelableExtra(EXTRA_PENDING_INTENT);
268 // Put BAL flags to avoid activity start aborted.
269 opts.putBoolean(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED, true);
270 opts.putBoolean(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED_BY_PERMISSION, true);
271 mStarter.startIntent(launchIntent, null /* fillIntent */, position, opts);
272 }
273 }
274
275 /**
276 * Per-drag session data.
277 */
278 private static class DragSession {
279 private final ActivityTaskManager mActivityTaskManager;
280 private final ClipData mInitialDragData;
281
282 final DisplayLayout displayLayout;
283 Intent dragData;
284 ActivityManager.RunningTaskInfo runningTaskInfo;
285 @WindowConfiguration.WindowingMode
286 int runningTaskWinMode = WINDOWING_MODE_UNDEFINED;
287 @WindowConfiguration.ActivityType
288 int runningTaskActType = ACTIVITY_TYPE_STANDARD;
289 boolean dragItemSupportsSplitscreen;
290
291 DragSession(ActivityTaskManager activityTaskManager,
292 DisplayLayout dispLayout, ClipData data) {
293 mActivityTaskManager = activityTaskManager;
294 mInitialDragData = data;
295 displayLayout = dispLayout;
296 }
297
298 /**
299 * Updates the session data based on the current state of the system.
300 */
301 void update() {
302 List<ActivityManager.RunningTaskInfo> tasks =
303 mActivityTaskManager.getTasks(1, false /* filterOnlyVisibleRecents */);
304 if (!tasks.isEmpty()) {
305 final ActivityManager.RunningTaskInfo task = tasks.get(0);
306 runningTaskInfo = task;
307 runningTaskWinMode = task.getWindowingMode();
308 runningTaskActType = task.getActivityType();
309 }
310
311 final ActivityInfo info = mInitialDragData.getItemAt(0).getActivityInfo();
312 dragItemSupportsSplitscreen = info == null
313 || ActivityInfo.isResizeableMode(info.resizeMode);
314 dragData = mInitialDragData.getItemAt(0).getIntent();
315 }
316 }
317
318 /**
319 * Interface for actually committing the task launches.
320 */
321 public interface Starter {
322 void startTask(int taskId, @SplitPosition int position, @Nullable Bundle options);
323 void startShortcut(String packageName, String shortcutId, @SplitPosition int position,
324 @Nullable Bundle options, UserHandle user);
325 void startIntent(PendingIntent intent, Intent fillInIntent, @SplitPosition int position,
326 @Nullable Bundle options);
327 void enterSplitScreen(int taskId, boolean leftOrTop);
328
329 /**
330 * Exits splitscreen, with an associated exit trigger from the SplitscreenUIChanged proto
331 * for logging.
332 */
333 void exitSplitScreen(int toTopTaskId, int exitTrigger);
334 }
335
336 /**
337 * Default implementation of the starter which calls through the system services to launch the
338 * tasks.
339 */
340 private static class DefaultStarter implements Starter {
341 private final Context mContext;
342
343 public DefaultStarter(Context context) {
344 mContext = context;
345 }
346
347 @Override
348 public void startTask(int taskId, int position, @Nullable Bundle options) {
349 try {
350 ActivityTaskManager.getService().startActivityFromRecents(taskId, options);
351 } catch (RemoteException e) {
352 Slog.e(TAG, "Failed to launch task", e);
353 }
354 }
355
356 @Override
357 public void startShortcut(String packageName, String shortcutId, int position,
358 @Nullable Bundle options, UserHandle user) {
359 try {
360 LauncherApps launcherApps =
361 mContext.getSystemService(LauncherApps.class);
362 launcherApps.startShortcut(packageName, shortcutId, null /* sourceBounds */,
363 options, user);
364 } catch (ActivityNotFoundException e) {
365 Slog.e(TAG, "Failed to launch shortcut", e);
366 }
367 }
368
369 @Override
370 public void startIntent(PendingIntent intent, @Nullable Intent fillInIntent, int position,
371 @Nullable Bundle options) {
372 try {
373 intent.send(mContext, 0, fillInIntent, null, null, null, options);
374 } catch (PendingIntent.CanceledException e) {
375 Slog.e(TAG, "Failed to launch activity", e);
376 }
377 }
378
379 @Override
380 public void enterSplitScreen(int taskId, boolean leftOrTop) {
381 throw new UnsupportedOperationException("enterSplitScreen not implemented by starter");
382 }
383
384 @Override
385 public void exitSplitScreen(int toTopTaskId, int exitTrigger) {
386 throw new UnsupportedOperationException("exitSplitScreen not implemented by starter");
387 }
388 }
389
390 /**
391 * Represents a drop target.
392 */
393 static class Target {
394 static final int TYPE_FULLSCREEN = 0;
395 static final int TYPE_SPLIT_LEFT = 1;
396 static final int TYPE_SPLIT_TOP = 2;
397 static final int TYPE_SPLIT_RIGHT = 3;
398 static final int TYPE_SPLIT_BOTTOM = 4;
399 @IntDef(value = {
400 TYPE_FULLSCREEN,
401 TYPE_SPLIT_LEFT,
402 TYPE_SPLIT_TOP,
403 TYPE_SPLIT_RIGHT,
404 TYPE_SPLIT_BOTTOM
405 })
406 @Retention(RetentionPolicy.SOURCE)
407 @interface Type{}
408
409 final @Type int type;
410
411 // The actual hit region for this region
412 final Rect hitRegion;
413 // The approximate visual region for where the task will start
414 final Rect drawRegion;
415
416 public Target(@Type int t, Rect hit, Rect draw) {
417 type = t;
418 hitRegion = hit;
419 drawRegion = draw;
420 }
421
422 @Override
423 public String toString() {
424 return "Target {hit=" + hitRegion + " draw=" + drawRegion + "}";
425 }
426 }
427 }