Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/draganddrop/DragLayout.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.StatusBarManager.DISABLE_NONE;
20 import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
21 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
22
23 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_BOTTOM_OR_RIGHT;
24 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_TOP_OR_LEFT;
25
26 import android.animation.Animator;
27 import android.animation.AnimatorListenerAdapter;
28 import android.animation.ValueAnimator;
29 import android.annotation.SuppressLint;
30 import android.app.ActivityManager;
31 import android.app.StatusBarManager;
32 import android.content.ClipData;
33 import android.content.Context;
34 import android.content.res.Configuration;
35 import android.graphics.Color;
36 import android.graphics.Insets;
37 import android.graphics.Rect;
38 import android.graphics.drawable.Drawable;
39 import android.view.DragEvent;
40 import android.view.SurfaceControl;
41 import android.view.WindowInsets;
42 import android.view.WindowInsets.Type;
43 import android.widget.LinearLayout;
44
45 import com.android.internal.logging.InstanceId;
46 import com.android.internal.protolog.common.ProtoLog;
47 import com.android.launcher3.icons.IconProvider;
48 import com.android.wm.shell.R;
49 import com.android.wm.shell.animation.Interpolators;
50 import com.android.wm.shell.common.DisplayLayout;
51 import com.android.wm.shell.protolog.ShellProtoLogGroup;
52 import com.android.wm.shell.splitscreen.SplitScreenController;
53
54 import java.util.ArrayList;
55
56 /**
57 * Coordinates the visible drop targets for the current drag.
58 */
59 public class DragLayout extends LinearLayout {
60
61 // While dragging the status bar is hidden.
62 private static final int HIDE_STATUS_BAR_FLAGS = StatusBarManager.DISABLE_NOTIFICATION_ICONS
63 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
64 | StatusBarManager.DISABLE_CLOCK
65 | StatusBarManager.DISABLE_SYSTEM_INFO;
66
67 private final DragAndDropPolicy mPolicy;
68 private final SplitScreenController mSplitScreenController;
69 private final IconProvider mIconProvider;
70 private final StatusBarManager mStatusBarManager;
71
72 private DragAndDropPolicy.Target mCurrentTarget = null;
73 private DropZoneView mDropZoneView1;
74 private DropZoneView mDropZoneView2;
75
76 private int mDisplayMargin;
77 private int mDividerSize;
78 private Insets mInsets = Insets.NONE;
79
80 private boolean mIsShowing;
81 private boolean mHasDropped;
82
83 @SuppressLint("WrongConstant")
84 public DragLayout(Context context, SplitScreenController splitScreenController,
85 IconProvider iconProvider) {
86 super(context);
87 mSplitScreenController = splitScreenController;
88 mIconProvider = iconProvider;
89 mPolicy = new DragAndDropPolicy(context, splitScreenController);
90 mStatusBarManager = context.getSystemService(StatusBarManager.class);
91
92 mDisplayMargin = context.getResources().getDimensionPixelSize(
93 R.dimen.drop_layout_display_margin);
94 mDividerSize = context.getResources().getDimensionPixelSize(
95 R.dimen.split_divider_bar_width);
96
97 // Always use LTR because we assume dropZoneView1 is on the left and 2 is on the right when
98 // showing the highlight.
99 setLayoutDirection(LAYOUT_DIRECTION_LTR);
100 mDropZoneView1 = new DropZoneView(context);
101 mDropZoneView2 = new DropZoneView(context);
102 addView(mDropZoneView1, new LinearLayout.LayoutParams(MATCH_PARENT,
103 MATCH_PARENT));
104 addView(mDropZoneView2, new LinearLayout.LayoutParams(MATCH_PARENT,
105 MATCH_PARENT));
106 ((LayoutParams) mDropZoneView1.getLayoutParams()).weight = 1;
107 ((LayoutParams) mDropZoneView2.getLayoutParams()).weight = 1;
108 int orientation = getResources().getConfiguration().orientation;
109 setOrientation(orientation == Configuration.ORIENTATION_LANDSCAPE
110 ? LinearLayout.HORIZONTAL
111 : LinearLayout.VERTICAL);
112 updateContainerMargins(getResources().getConfiguration().orientation);
113 }
114
115 @Override
116 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
117 mInsets = insets.getInsets(Type.systemBars() | Type.displayCutout());
118 recomputeDropTargets();
119
120 final int orientation = getResources().getConfiguration().orientation;
121 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
122 mDropZoneView1.setBottomInset(mInsets.bottom);
123 mDropZoneView2.setBottomInset(mInsets.bottom);
124 } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
125 mDropZoneView1.setBottomInset(0);
126 mDropZoneView2.setBottomInset(mInsets.bottom);
127 }
128 return super.onApplyWindowInsets(insets);
129 }
130
131 public void onThemeChange() {
132 mDropZoneView1.onThemeChange();
133 mDropZoneView2.onThemeChange();
134 }
135
136 public void onConfigChanged(Configuration newConfig) {
137 if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
138 && getOrientation() != HORIZONTAL) {
139 setOrientation(LinearLayout.HORIZONTAL);
140 updateContainerMargins(newConfig.orientation);
141 } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT
142 && getOrientation() != VERTICAL) {
143 setOrientation(LinearLayout.VERTICAL);
144 updateContainerMargins(newConfig.orientation);
145 }
146 }
147
148 private void updateContainerMarginsForSingleTask() {
149 mDropZoneView1.setContainerMargin(
150 mDisplayMargin, mDisplayMargin, mDisplayMargin, mDisplayMargin);
151 mDropZoneView2.setContainerMargin(0, 0, 0, 0);
152 }
153
154 private void updateContainerMargins(int orientation) {
155 final float halfMargin = mDisplayMargin / 2f;
156 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
157 mDropZoneView1.setContainerMargin(
158 mDisplayMargin, mDisplayMargin, halfMargin, mDisplayMargin);
159 mDropZoneView2.setContainerMargin(
160 halfMargin, mDisplayMargin, mDisplayMargin, mDisplayMargin);
161 } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
162 mDropZoneView1.setContainerMargin(
163 mDisplayMargin, mDisplayMargin, mDisplayMargin, halfMargin);
164 mDropZoneView2.setContainerMargin(
165 mDisplayMargin, halfMargin, mDisplayMargin, mDisplayMargin);
166 }
167 }
168
169 public boolean hasDropped() {
170 return mHasDropped;
171 }
172
173 public void prepare(DisplayLayout displayLayout, ClipData initialData,
174 InstanceId loggerSessionId) {
175 mPolicy.start(displayLayout, initialData, loggerSessionId);
176 mHasDropped = false;
177 mCurrentTarget = null;
178
179 boolean alreadyInSplit = mSplitScreenController != null
180 && mSplitScreenController.isSplitScreenVisible();
181 if (!alreadyInSplit) {
182 ActivityManager.RunningTaskInfo taskInfo1 = mPolicy.getLatestRunningTask();
183 if (taskInfo1 != null) {
184 final int activityType = taskInfo1.getActivityType();
185 if (activityType == ACTIVITY_TYPE_STANDARD) {
186 Drawable icon1 = mIconProvider.getIcon(taskInfo1.topActivityInfo);
187 int bgColor1 = getResizingBackgroundColor(taskInfo1);
188 mDropZoneView1.setAppInfo(bgColor1, icon1);
189 mDropZoneView2.setAppInfo(bgColor1, icon1);
190 updateDropZoneSizes(null, null); // passing null splits the views evenly
191 } else {
192 // We use the first drop zone to show the fullscreen highlight, and don't need
193 // to set additional info
194 mDropZoneView1.setForceIgnoreBottomMargin(true);
195 updateDropZoneSizesForSingleTask();
196 updateContainerMarginsForSingleTask();
197 }
198 }
199 } else {
200 // We're already in split so get taskInfo from the controller to populate icon / color.
201 ActivityManager.RunningTaskInfo topOrLeftTask =
202 mSplitScreenController.getTaskInfo(SPLIT_POSITION_TOP_OR_LEFT);
203 ActivityManager.RunningTaskInfo bottomOrRightTask =
204 mSplitScreenController.getTaskInfo(SPLIT_POSITION_BOTTOM_OR_RIGHT);
205 if (topOrLeftTask != null && bottomOrRightTask != null) {
206 Drawable topOrLeftIcon = mIconProvider.getIcon(topOrLeftTask.topActivityInfo);
207 int topOrLeftColor = getResizingBackgroundColor(topOrLeftTask);
208 Drawable bottomOrRightIcon = mIconProvider.getIcon(
209 bottomOrRightTask.topActivityInfo);
210 int bottomOrRightColor = getResizingBackgroundColor(bottomOrRightTask);
211 mDropZoneView1.setAppInfo(topOrLeftColor, topOrLeftIcon);
212 mDropZoneView2.setAppInfo(bottomOrRightColor, bottomOrRightIcon);
213 }
214
215 // Update the dropzones to match existing split sizes
216 Rect topOrLeftBounds = new Rect();
217 Rect bottomOrRightBounds = new Rect();
218 mSplitScreenController.getStageBounds(topOrLeftBounds, bottomOrRightBounds);
219 updateDropZoneSizes(topOrLeftBounds, bottomOrRightBounds);
220 }
221 }
222
223 private void updateDropZoneSizesForSingleTask() {
224 final LinearLayout.LayoutParams dropZoneView1 =
225 (LayoutParams) mDropZoneView1.getLayoutParams();
226 final LinearLayout.LayoutParams dropZoneView2 =
227 (LayoutParams) mDropZoneView2.getLayoutParams();
228 dropZoneView1.width = MATCH_PARENT;
229 dropZoneView1.height = MATCH_PARENT;
230 dropZoneView2.width = 0;
231 dropZoneView2.height = 0;
232 dropZoneView1.weight = 1;
233 dropZoneView2.weight = 0;
234 mDropZoneView1.setLayoutParams(dropZoneView1);
235 mDropZoneView2.setLayoutParams(dropZoneView2);
236 }
237
238 /**
239 * Sets the size of the two drop zones based on the provided bounds. The divider sits between
240 * the views and its size is included in the calculations.
241 *
242 * @param bounds1 bounds to apply to the first dropzone view, null if split in half.
243 * @param bounds2 bounds to apply to the second dropzone view, null if split in half.
244 */
245 private void updateDropZoneSizes(Rect bounds1, Rect bounds2) {
246 final int orientation = getResources().getConfiguration().orientation;
247 final boolean isPortrait = orientation == Configuration.ORIENTATION_PORTRAIT;
248 final int halfDivider = mDividerSize / 2;
249 final LinearLayout.LayoutParams dropZoneView1 =
250 (LayoutParams) mDropZoneView1.getLayoutParams();
251 final LinearLayout.LayoutParams dropZoneView2 =
252 (LayoutParams) mDropZoneView2.getLayoutParams();
253 if (isPortrait) {
254 dropZoneView1.width = MATCH_PARENT;
255 dropZoneView2.width = MATCH_PARENT;
256 dropZoneView1.height = bounds1 != null ? bounds1.height() + halfDivider : MATCH_PARENT;
257 dropZoneView2.height = bounds2 != null ? bounds2.height() + halfDivider : MATCH_PARENT;
258 } else {
259 dropZoneView1.width = bounds1 != null ? bounds1.width() + halfDivider : MATCH_PARENT;
260 dropZoneView2.width = bounds2 != null ? bounds2.width() + halfDivider : MATCH_PARENT;
261 dropZoneView1.height = MATCH_PARENT;
262 dropZoneView2.height = MATCH_PARENT;
263 }
264 dropZoneView1.weight = bounds1 != null ? 0 : 1;
265 dropZoneView2.weight = bounds2 != null ? 0 : 1;
266 mDropZoneView1.setLayoutParams(dropZoneView1);
267 mDropZoneView2.setLayoutParams(dropZoneView2);
268 }
269
270 public void show() {
271 mIsShowing = true;
272 recomputeDropTargets();
273 }
274
275 /**
276 * Recalculates the drop targets based on the current policy.
277 */
278 private void recomputeDropTargets() {
279 if (!mIsShowing) {
280 return;
281 }
282 final ArrayList<DragAndDropPolicy.Target> targets = mPolicy.getTargets(mInsets);
283 for (int i = 0; i < targets.size(); i++) {
284 final DragAndDropPolicy.Target target = targets.get(i);
285 ProtoLog.v(ShellProtoLogGroup.WM_SHELL_DRAG_AND_DROP, "Add target: %s", target);
286 // Inset the draw region by a little bit
287 target.drawRegion.inset(mDisplayMargin, mDisplayMargin);
288 }
289 }
290
291 /**
292 * Updates the visible drop target as the user drags.
293 */
294 public void update(DragEvent event) {
295 if (mHasDropped) {
296 return;
297 }
298 // Find containing region, if the same as mCurrentRegion, then skip, otherwise, animate the
299 // visibility of the current region
300 DragAndDropPolicy.Target target = mPolicy.getTargetAtLocation(
301 (int) event.getX(), (int) event.getY());
302 if (mCurrentTarget != target) {
303 ProtoLog.v(ShellProtoLogGroup.WM_SHELL_DRAG_AND_DROP, "Current target: %s", target);
304 if (target == null) {
305 // Animating to no target
306 animateSplitContainers(false, null /* animCompleteCallback */);
307 } else if (mCurrentTarget == null) {
308 if (mPolicy.getNumTargets() == 1) {
309 animateFullscreenContainer(true);
310 } else {
311 animateSplitContainers(true, null /* animCompleteCallback */);
312 animateHighlight(target);
313 }
314 } else if (mCurrentTarget.type != target.type) {
315 // Switching between targets
316 mDropZoneView1.animateSwitch();
317 mDropZoneView2.animateSwitch();
318 }
319 mCurrentTarget = target;
320 }
321 }
322
323 /**
324 * Hides the drag layout and animates out the visible drop targets.
325 */
326 public void hide(DragEvent event, Runnable hideCompleteCallback) {
327 mIsShowing = false;
328 animateSplitContainers(false, hideCompleteCallback);
329 // Reset the state if we previously force-ignore the bottom margin
330 mDropZoneView1.setForceIgnoreBottomMargin(false);
331 mDropZoneView2.setForceIgnoreBottomMargin(false);
332 updateContainerMargins(getResources().getConfiguration().orientation);
333 mCurrentTarget = null;
334 }
335
336 /**
337 * Handles the drop onto a target and animates out the visible drop targets.
338 */
339 public boolean drop(DragEvent event, SurfaceControl dragSurface,
340 Runnable dropCompleteCallback) {
341 final boolean handledDrop = mCurrentTarget != null;
342 mHasDropped = true;
343
344 // Process the drop
345 mPolicy.handleDrop(mCurrentTarget, event.getClipData());
346
347 // Start animating the drop UI out with the drag surface
348 hide(event, dropCompleteCallback);
349 hideDragSurface(dragSurface);
350 return handledDrop;
351 }
352
353 private void hideDragSurface(SurfaceControl dragSurface) {
354 final SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
355 final ValueAnimator dragSurfaceAnimator = ValueAnimator.ofFloat(0f, 1f);
356 // Currently the splash icon animation runs with the default ValueAnimator duration of
357 // 300ms
358 dragSurfaceAnimator.setDuration(300);
359 dragSurfaceAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
360 dragSurfaceAnimator.addUpdateListener(animation -> {
361 float t = animation.getAnimatedFraction();
362 float alpha = 1f - t;
363 // TODO: Scale the drag surface as well once we make all the source surfaces
364 // consistent
365 tx.setAlpha(dragSurface, alpha);
366 tx.apply();
367 });
368 dragSurfaceAnimator.addListener(new AnimatorListenerAdapter() {
369 private boolean mCanceled = false;
370
371 @Override
372 public void onAnimationCancel(Animator animation) {
373 cleanUpSurface();
374 mCanceled = true;
375 }
376
377 @Override
378 public void onAnimationEnd(Animator animation) {
379 if (mCanceled) {
380 // Already handled above
381 return;
382 }
383 cleanUpSurface();
384 }
385
386 private void cleanUpSurface() {
387 // Clean up the drag surface
388 tx.remove(dragSurface);
389 tx.apply();
390 }
391 });
392 dragSurfaceAnimator.start();
393 }
394
395 private void animateFullscreenContainer(boolean visible) {
396 mStatusBarManager.disable(visible
397 ? HIDE_STATUS_BAR_FLAGS
398 : DISABLE_NONE);
399 // We're only using the first drop zone if there is one fullscreen target
400 mDropZoneView1.setShowingMargin(visible);
401 mDropZoneView1.setShowingHighlight(visible);
402 }
403
404 private void animateSplitContainers(boolean visible, Runnable animCompleteCallback) {
405 mStatusBarManager.disable(visible
406 ? HIDE_STATUS_BAR_FLAGS
407 : DISABLE_NONE);
408 mDropZoneView1.setShowingMargin(visible);
409 mDropZoneView2.setShowingMargin(visible);
410 Animator animator = mDropZoneView1.getAnimator();
411 if (animCompleteCallback != null) {
412 if (animator != null) {
413 animator.addListener(new AnimatorListenerAdapter() {
414 @Override
415 public void onAnimationEnd(Animator animation) {
416 animCompleteCallback.run();
417 }
418 });
419 } else {
420 // If there's no animator the animation is done so run immediately
421 animCompleteCallback.run();
422 }
423 }
424 }
425
426 private void animateHighlight(DragAndDropPolicy.Target target) {
427 if (target.type == DragAndDropPolicy.Target.TYPE_SPLIT_LEFT
428 || target.type == DragAndDropPolicy.Target.TYPE_SPLIT_TOP) {
429 mDropZoneView1.setShowingHighlight(true);
430 mDropZoneView2.setShowingHighlight(false);
431 } else if (target.type == DragAndDropPolicy.Target.TYPE_SPLIT_RIGHT
432 || target.type == DragAndDropPolicy.Target.TYPE_SPLIT_BOTTOM) {
433 mDropZoneView1.setShowingHighlight(false);
434 mDropZoneView2.setShowingHighlight(true);
435 }
436 }
437
438 private static int getResizingBackgroundColor(ActivityManager.RunningTaskInfo taskInfo) {
439 final int taskBgColor = taskInfo.taskDescription.getBackgroundColor();
440 return Color.valueOf(taskBgColor == -1 ? Color.WHITE : taskBgColor).toArgb();
441 }
442 }