Skip to content

Instantly share code, notes, and snippets.

@71
Created November 27, 2022 15:43
Show Gist options
  • Save 71/a8289cd9e39cf1e62560f9db3f0e8e3e to your computer and use it in GitHub Desktop.
Save 71/a8289cd9e39cf1e62560f9db3f0e8e3e to your computer and use it in GitHub Desktop.
Snippet to show a text box that, when long-pressed, allows Chrome to be dropped into a split-screen.
// Inspired by Launcher3:
// https://cs.android.com/android/platform/superproject/+/35157974d49e70f03687edb9408e5897d04ff845:packages/apps/Launcher3/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java;l=293
// Jetpack Compose view with interoperability:
// https://developer.android.com/jetpack/compose/interop/interop-apis
AndroidView(factory = { context ->
TextView(context).apply {
text = "Drag me!"
// Drag and drop listener; see:
// https://developer.android.com/develop/ui/views/touch-and-input/drag-drop#StartDrag
setOnLongClickListener { v ->
// Intent source:
// https://cs.android.com/android/platform/superproject/+/35157974d49e70f03687edb9408e5897d04ff845:frameworks/base/services/core/java/com/android/server/pm/LauncherAppsService.java;l=1224;drc=a673c2e9218d29c87af12b123b53c26d0e444315;bpv=1;bpt=1
val intent = Intent().apply {
val launchIntent = Intent(Intent.ACTION_MAIN).apply {
component = ComponentName("com.android.chrome", "com.google.android.apps.chrome.Main")
addCategory(Intent.CATEGORY_LAUNCHER)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
}
// Not yet visible in SDK; source:
// https://cs.android.com/android/platform/superproject/+/35157974d49e70f03687edb9408e5897d04ff845:frameworks/base/core/java/android/content/ClipDescription.java;drc=a673c2e9218d29c87af12b123b53c26d0e444315;l=116
putExtra(
"android.intent.extra.PENDING_INTENT",
PendingIntent.getActivity(
this@MainActivity,
0,
launchIntent,
PendingIntent.FLAG_MUTABLE,
)
)
putExtra(Intent.EXTRA_USER, android.os.Process.myUserHandle())
}
val clipDescription = ClipDescription(
"Chrome",
// Not yet visible in SDK; source:
// https://cs.android.com/android/platform/superproject/+/35157974d49e70f03687edb9408e5897d04ff845:frameworks/base/core/java/android/content/ClipDescription.java;drc=a673c2e9218d29c87af12b123b53c26d0e444315;l=82
arrayOf("application/vnd.android.activity"),
)
val clipData = ClipData(clipDescription, ClipData.Item(intent))
v.startDragAndDrop(
clipData,
MyDragShadowBuilder(this),
null,
// Without these flags, nothing happens:
// https://cs.android.com/android/platform/superproject/+/35157974d49e70f03687edb9408e5897d04ff845:packages/apps/Launcher3/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java;l=371
View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_OPAQUE,
)
true
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment