Skip to content

Commit d0ef8d0

Browse files
tiwizRob Orgiu
andauthored
Fix Drag&Drop code to also support external apps (#546)
* Fix code to also support external apps * Sort chronologically the snippets * Move the Activity to be used without the nullability check --------- Co-authored-by: Rob Orgiu <[email protected]>
1 parent b198bf0 commit d0ef8d0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.content.ClipData
2020
import android.content.ClipDescription
2121
import android.os.Build
2222
import android.view.View
23+
import androidx.activity.compose.LocalActivity
2324
import androidx.annotation.RequiresApi
2425
import androidx.compose.foundation.ExperimentalFoundationApi
2526
import androidx.compose.foundation.draganddrop.dragAndDropSource
@@ -31,6 +32,7 @@ import androidx.compose.ui.draganddrop.DragAndDropEvent
3132
import androidx.compose.ui.draganddrop.DragAndDropTarget
3233
import androidx.compose.ui.draganddrop.DragAndDropTransferData
3334
import androidx.compose.ui.draganddrop.mimeTypes
35+
import androidx.compose.ui.draganddrop.toAndroidDragEvent
3436

3537
@RequiresApi(Build.VERSION_CODES.N)
3638
@OptIn(ExperimentalFoundationApi::class)
@@ -71,6 +73,22 @@ private fun DragAndDropSnippet() {
7173
}
7274
// [END android_compose_drag_and_drop_4]
7375

76+
LocalActivity.current?.let { activity ->
77+
// [START android_compose_drag_and_drop_7]
78+
val externalAppCallback = remember {
79+
object : DragAndDropTarget {
80+
override fun onDrop(event: DragAndDropEvent): Boolean {
81+
val permission =
82+
activity.requestDragAndDropPermissions(event.toAndroidDragEvent())
83+
// Parse received data
84+
permission?.release()
85+
return true
86+
}
87+
}
88+
}
89+
// [END android_compose_drag_and_drop_7]
90+
}
91+
7492
// [START android_compose_drag_and_drop_5]
7593
Modifier.dragAndDropTarget(
7694
shouldStartDragAndDrop = { event ->

0 commit comments

Comments
 (0)