| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.draganddrop; |
| 18 |
|
| 19 |
import static android.content.ClipDescription.MIMETYPE_APPLICATION_ACTIVITY; |
| 20 |
import static android.content.ClipDescription.MIMETYPE_APPLICATION_SHORTCUT; |
| 21 |
import static android.content.ClipDescription.MIMETYPE_APPLICATION_TASK; |
| 22 |
import static android.view.Display.DEFAULT_DISPLAY; |
| 23 |
import static android.view.DragEvent.ACTION_DRAG_ENDED; |
| 24 |
import static android.view.DragEvent.ACTION_DRAG_ENTERED; |
| 25 |
import static android.view.DragEvent.ACTION_DRAG_EXITED; |
| 26 |
import static android.view.DragEvent.ACTION_DRAG_LOCATION; |
| 27 |
import static android.view.DragEvent.ACTION_DRAG_STARTED; |
| 28 |
import static android.view.DragEvent.ACTION_DROP; |
| 29 |
import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; |
| 30 |
import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 31 |
import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
| 32 |
import static android.view.WindowManager.LayoutParams.MATCH_PARENT; |
| 33 |
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_INTERCEPT_GLOBAL_DRAG_AND_DROP; |
| 34 |
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION; |
| 35 |
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS; |
| 36 |
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; |
| 37 |
|
| 38 |
import android.content.ClipDescription; |
| 39 |
import android.content.Context; |
| 40 |
import android.content.res.Configuration; |
| 41 |
import android.graphics.PixelFormat; |
| 42 |
import android.util.Slog; |
| 43 |
import android.util.SparseArray; |
| 44 |
import android.view.DragEvent; |
| 45 |
import android.view.LayoutInflater; |
| 46 |
import android.view.SurfaceControl; |
| 47 |
import android.view.View; |
| 48 |
import android.view.ViewGroup; |
| 49 |
import android.view.WindowManager; |
| 50 |
import android.widget.FrameLayout; |
| 51 |
|
| 52 |
import androidx.annotation.VisibleForTesting; |
| 53 |
|
| 54 |
import com.android.internal.logging.InstanceId; |
| 55 |
import com.android.internal.logging.UiEventLogger; |
| 56 |
import com.android.internal.protolog.common.ProtoLog; |
| 57 |
import com.android.launcher3.icons.IconProvider; |
| 58 |
import com.android.wm.shell.R; |
| 59 |
import com.android.wm.shell.common.DisplayController; |
| 60 |
import com.android.wm.shell.common.ShellExecutor; |
| 61 |
import com.android.wm.shell.protolog.ShellProtoLogGroup; |
| 62 |
import com.android.wm.shell.splitscreen.SplitScreenController; |
| 63 |
import com.android.wm.shell.sysui.ConfigurationChangeListener; |
| 64 |
import com.android.wm.shell.sysui.ShellController; |
| 65 |
import com.android.wm.shell.sysui.ShellInit; |
| 66 |
|
| 67 |
import java.util.ArrayList; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public class DragAndDropController implements DisplayController.OnDisplaysChangedListener, |
| 73 |
View.OnDragListener, ConfigurationChangeListener { |
| 74 |
|
| 75 |
private static final String TAG = DragAndDropController.class.getSimpleName(); |
| 76 |
|
| 77 |
private final Context mContext; |
| 78 |
private final ShellController mShellController; |
| 79 |
private final DisplayController mDisplayController; |
| 80 |
private final DragAndDropEventLogger mLogger; |
| 81 |
private final IconProvider mIconProvider; |
| 82 |
private SplitScreenController mSplitScreen; |
| 83 |
private ShellExecutor mMainExecutor; |
| 84 |
private ArrayList<DragAndDropListener> mListeners = new ArrayList<>(); |
| 85 |
|
| 86 |
private final SparseArray<PerDisplay> mDisplayDropTargets = new SparseArray<>(); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public interface DragAndDropListener { |
| 92 |
|
| 93 |
void onDragStarted(); |
| 94 |
} |
| 95 |
|
| 96 |
public DragAndDropController(Context context, |
| 97 |
ShellInit shellInit, |
| 98 |
ShellController shellController, |
| 99 |
DisplayController displayController, |
| 100 |
UiEventLogger uiEventLogger, |
| 101 |
IconProvider iconProvider, |
| 102 |
ShellExecutor mainExecutor) { |
| 103 |
mContext = context; |
| 104 |
mShellController = shellController; |
| 105 |
mDisplayController = displayController; |
| 106 |
mLogger = new DragAndDropEventLogger(uiEventLogger); |
| 107 |
mIconProvider = iconProvider; |
| 108 |
mMainExecutor = mainExecutor; |
| 109 |
shellInit.addInitCallback(this::onInit, this); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void onInit() { |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
mMainExecutor.executeDelayed(() -> { |
| 120 |
mDisplayController.addDisplayWindowListener(this); |
| 121 |
}, 0); |
| 122 |
mShellController.addConfigurationChangeListener(this); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public void setSplitScreenController(SplitScreenController splitscreen) { |
| 129 |
mSplitScreen = splitscreen; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
public void addListener(DragAndDropListener listener) { |
| 134 |
mListeners.add(listener); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
public void removeListener(DragAndDropListener listener) { |
| 139 |
mListeners.remove(listener); |
| 140 |
} |
| 141 |
|
| 142 |
private void notifyListeners() { |
| 143 |
for (int i = 0; i < mListeners.size(); i++) { |
| 144 |
mListeners.get(i).onDragStarted(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public void onDisplayAdded(int displayId) { |
| 150 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_DRAG_AND_DROP, "Display added: %d", displayId); |
| 151 |
if (displayId != DEFAULT_DISPLAY) { |
| 152 |
|
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
final Context context = mDisplayController.getDisplayContext(displayId) |
| 157 |
.createWindowContext(TYPE_APPLICATION_OVERLAY, null); |
| 158 |
final WindowManager wm = context.getSystemService(WindowManager.class); |
| 159 |
|
| 160 |
|
| 161 |
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( |
| 162 |
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, |
| 163 |
TYPE_APPLICATION_OVERLAY, |
| 164 |
FLAG_NOT_FOCUSABLE | FLAG_HARDWARE_ACCELERATED, |
| 165 |
PixelFormat.TRANSLUCENT); |
| 166 |
layoutParams.privateFlags |= SYSTEM_FLAG_SHOW_FOR_ALL_USERS |
| 167 |
| PRIVATE_FLAG_INTERCEPT_GLOBAL_DRAG_AND_DROP |
| 168 |
| PRIVATE_FLAG_NO_MOVE_ANIMATION; |
| 169 |
layoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
| 170 |
layoutParams.setFitInsetsTypes(0); |
| 171 |
layoutParams.setTitle("ShellDropTarget"); |
| 172 |
|
| 173 |
FrameLayout rootView = (FrameLayout) LayoutInflater.from(context).inflate( |
| 174 |
R.layout.global_drop_target, null); |
| 175 |
rootView.setOnDragListener(this); |
| 176 |
rootView.setVisibility(View.INVISIBLE); |
| 177 |
DragLayout dragLayout = new DragLayout(context, mSplitScreen, mIconProvider); |
| 178 |
rootView.addView(dragLayout, |
| 179 |
new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
| 180 |
try { |
| 181 |
wm.addView(rootView, layoutParams); |
| 182 |
addDisplayDropTarget(displayId, context, wm, rootView, dragLayout); |
| 183 |
} catch (WindowManager.InvalidDisplayException e) { |
| 184 |
Slog.w(TAG, "Unable to add view for display id: " + displayId); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
@VisibleForTesting |
| 189 |
void addDisplayDropTarget(int displayId, Context context, WindowManager wm, |
| 190 |
FrameLayout rootView, DragLayout dragLayout) { |
| 191 |
mDisplayDropTargets.put(displayId, |
| 192 |
new PerDisplay(displayId, context, wm, rootView, dragLayout)); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) { |
| 197 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_DRAG_AND_DROP, "Display changed: %d", displayId); |
| 198 |
final PerDisplay pd = mDisplayDropTargets.get(displayId); |
| 199 |
if (pd == null) { |
| 200 |
return; |
| 201 |
} |
| 202 |
pd.rootView.requestApplyInsets(); |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public void onDisplayRemoved(int displayId) { |
| 207 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_DRAG_AND_DROP, "Display removed: %d", displayId); |
| 208 |
final PerDisplay pd = mDisplayDropTargets.get(displayId); |
| 209 |
if (pd == null) { |
| 210 |
return; |
| 211 |
} |
| 212 |
pd.wm.removeViewImmediate(pd.rootView); |
| 213 |
mDisplayDropTargets.remove(displayId); |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public boolean onDrag(View target, DragEvent event) { |
| 218 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_DRAG_AND_DROP, |
| 219 |
"Drag event: action=%s x=%f y=%f xOffset=%f yOffset=%f", |
| 220 |
DragEvent.actionToString(event.getAction()), event.getX(), event.getY(), |
| 221 |
event.getOffsetX(), event.getOffsetY()); |
| 222 |
final int displayId = target.getDisplay().getDisplayId(); |
| 223 |
final PerDisplay pd = mDisplayDropTargets.get(displayId); |
| 224 |
final ClipDescription description = event.getClipDescription(); |
| 225 |
|
| 226 |
if (pd == null) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
if (event.getAction() == ACTION_DRAG_STARTED) { |
| 231 |
final boolean hasValidClipData = event.getClipData().getItemCount() > 0 |
| 232 |
&& (description.hasMimeType(MIMETYPE_APPLICATION_ACTIVITY) |
| 233 |
|| description.hasMimeType(MIMETYPE_APPLICATION_SHORTCUT) |
| 234 |
|| description.hasMimeType(MIMETYPE_APPLICATION_TASK)); |
| 235 |
pd.isHandlingDrag = hasValidClipData; |
| 236 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_DRAG_AND_DROP, |
| 237 |
"Clip description: handlingDrag=%b itemCount=%d mimeTypes=%s", |
| 238 |
pd.isHandlingDrag, event.getClipData().getItemCount(), |
| 239 |
getMimeTypes(description)); |
| 240 |
} |
| 241 |
|
| 242 |
if (!pd.isHandlingDrag) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
switch (event.getAction()) { |
| 247 |
case ACTION_DRAG_STARTED: |
| 248 |
if (pd.activeDragCount != 0) { |
| 249 |
Slog.w(TAG, "Unexpected drag start during an active drag"); |
| 250 |
return false; |
| 251 |
} |
| 252 |
InstanceId loggerSessionId = mLogger.logStart(event); |
| 253 |
pd.activeDragCount++; |
| 254 |
pd.dragLayout.prepare(mDisplayController.getDisplayLayout(displayId), |
| 255 |
event.getClipData(), loggerSessionId); |
| 256 |
setDropTargetWindowVisibility(pd, View.VISIBLE); |
| 257 |
notifyListeners(); |
| 258 |
break; |
| 259 |
case ACTION_DRAG_ENTERED: |
| 260 |
pd.dragLayout.show(); |
| 261 |
break; |
| 262 |
case ACTION_DRAG_LOCATION: |
| 263 |
pd.dragLayout.update(event); |
| 264 |
break; |
| 265 |
case ACTION_DROP: { |
| 266 |
pd.dragLayout.update(event); |
| 267 |
return handleDrop(event, pd); |
| 268 |
} |
| 269 |
case ACTION_DRAG_EXITED: { |
| 270 |
|
| 271 |
|
| 272 |
pd.dragLayout.hide(event, null); |
| 273 |
break; |
| 274 |
} |
| 275 |
case ACTION_DRAG_ENDED: |
| 276 |
|
| 277 |
|
| 278 |
if (pd.dragLayout.hasDropped()) { |
| 279 |
mLogger.logDrop(); |
| 280 |
} else { |
| 281 |
pd.activeDragCount--; |
| 282 |
pd.dragLayout.hide(event, () -> { |
| 283 |
if (pd.activeDragCount == 0) { |
| 284 |
|
| 285 |
|
| 286 |
setDropTargetWindowVisibility(pd, View.INVISIBLE); |
| 287 |
} |
| 288 |
}); |
| 289 |
} |
| 290 |
mLogger.logEnd(); |
| 291 |
break; |
| 292 |
} |
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private boolean handleDrop(DragEvent event, PerDisplay pd) { |
| 300 |
final SurfaceControl dragSurface = event.getDragSurface(); |
| 301 |
pd.activeDragCount--; |
| 302 |
return pd.dragLayout.drop(event, dragSurface, () -> { |
| 303 |
if (pd.activeDragCount == 0) { |
| 304 |
|
| 305 |
setDropTargetWindowVisibility(pd, View.INVISIBLE); |
| 306 |
} |
| 307 |
}); |
| 308 |
} |
| 309 |
|
| 310 |
private void setDropTargetWindowVisibility(PerDisplay pd, int visibility) { |
| 311 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_DRAG_AND_DROP, |
| 312 |
"Set drop target window visibility: displayId=%d visibility=%d", |
| 313 |
pd.displayId, visibility); |
| 314 |
pd.rootView.setVisibility(visibility); |
| 315 |
if (visibility == View.VISIBLE) { |
| 316 |
pd.rootView.requestApplyInsets(); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
private String getMimeTypes(ClipDescription description) { |
| 321 |
String mimeTypes = ""; |
| 322 |
for (int i = 0; i < description.getMimeTypeCount(); i++) { |
| 323 |
if (i > 0) { |
| 324 |
mimeTypes += ", "; |
| 325 |
} |
| 326 |
mimeTypes += description.getMimeType(i); |
| 327 |
} |
| 328 |
return mimeTypes; |
| 329 |
} |
| 330 |
|
| 331 |
@Override |
| 332 |
public void onThemeChanged() { |
| 333 |
for (int i = 0; i < mDisplayDropTargets.size(); i++) { |
| 334 |
mDisplayDropTargets.get(i).dragLayout.onThemeChange(); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
@Override |
| 339 |
public void onConfigurationChanged(Configuration newConfig) { |
| 340 |
for (int i = 0; i < mDisplayDropTargets.size(); i++) { |
| 341 |
mDisplayDropTargets.get(i).dragLayout.onConfigChanged(newConfig); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
private static class PerDisplay { |
| 346 |
final int displayId; |
| 347 |
final Context context; |
| 348 |
final WindowManager wm; |
| 349 |
final FrameLayout rootView; |
| 350 |
final DragLayout dragLayout; |
| 351 |
|
| 352 |
boolean isHandlingDrag; |
| 353 |
|
| 354 |
|
| 355 |
int activeDragCount; |
| 356 |
|
| 357 |
PerDisplay(int dispId, Context c, WindowManager w, FrameLayout rv, DragLayout dl) { |
| 358 |
displayId = dispId; |
| 359 |
context = c; |
| 360 |
wm = w; |
| 361 |
rootView = rv; |
| 362 |
dragLayout = dl; |
| 363 |
} |
| 364 |
} |
| 365 |
} |