Skip to content

Commit

Permalink
Merge pull request #163 from android/dnd
Browse files Browse the repository at this point in the history
Drag and Drop samples update
  • Loading branch information
satishshendeg authored May 6, 2024
2 parents d65c1d5 + a063171 commit ab694a1
Show file tree
Hide file tree
Showing 13 changed files with 910 additions and 1 deletion.
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ media3 = "1.2.1"
appcompat = "1.6.1"
material = "1.12.0-beta01"
constraintlayout = "2.1.4"
glide-compose = "1.0.0-beta01"

[libraries]

Expand Down Expand Up @@ -145,6 +146,8 @@ androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "medi
fresco = "com.facebook.fresco:fresco:3.0.0"
fresco-nativeimagetranscoder = "com.facebook.fresco:nativeimagetranscoder:2.6.0!!"
glide = "com.github.bumptech.glide:glide:4.15.1"
glide-compose = { group = "com.github.bumptech.glide", name = "compose", version.ref = "glide-compose" }


appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
Expand Down
8 changes: 8 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ This sample demonstrates displaying an UltraHDR image in a Compose View and an A
Download fonts instead of bundling them in the app resources.
- [Drag and Drop](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDrop.kt):
Demonstrates basic Drag and Drop functionality.
- [Drag and Drop - Helper](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDropWithHelper.kt):
Drag and Drop using the DragHelper and DropHelper from DragAndDropHelper library
- [Drag and Drop in Compose](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDropUsingCompose.kt):
Drag and drop in Compose
- [Drag and Drop in MultiWindow mode](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDropMultiWindow.kt):
Drag and drop to another app visible in multiwindow mode
- [Drag and Drop using the RichContentReceiver](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDropRichContentReceiverFragment.kt):
Using RichContentReceiverInterface for implementing Drop for rich data types
- [Editing UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/edit/EditingUltraHDR.kt):
This sample demonstrates editing an UltraHDR image and the resulting gainmap as well. Spatial edit operations like crop, rotate, scale are supported
- [Find devices sample](connectivity/bluetooth/ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/FindBLEDevicesSample.kt):
Expand Down
7 changes: 6 additions & 1 deletion samples/user-interface/draganddrop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
plugins {
id("com.example.platform.sample")
}

android {
namespace = "com.example.platform.ui.draganddrop"
buildFeatures {
Expand All @@ -29,4 +29,9 @@ dependencies {
implementation(libs.material)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.draganddrop)
implementation(libs.glide)
implementation(libs.androidx.media3.common)
implementation(libs.androidx.media3.ui)
implementation(libs.androidx.media3.exoplayer)
implementation(libs.glide.compose)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.platform.ui.draganddrop

import android.content.ClipData
import android.content.ClipDescription
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.util.component1
import androidx.core.util.component2
import androidx.core.view.ContentInfoCompat
import androidx.core.view.DragStartHelper
import androidx.draganddrop.DropHelper
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.example.platform.ui.draganddrop.databinding.FragmentDndMultiwindowBinding
import com.google.android.catalog.framework.annotations.Sample

@Sample(
name = "Drag and Drop in MultiWindow mode",
description = "Drag and drop to another app visible in multiwindow mode",
documentation = "",
)
class DragAndDropMultiWindow : Fragment(R.layout.fragment_dnd_multiwindow) {
lateinit var binding: FragmentDndMultiwindowBinding

@RequiresApi(Build.VERSION_CODES.N)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentDndMultiwindowBinding.bind(view)
ConstraintSet().clone(binding.root)
binding.tvGreeting.text = resources.getString(R.string.dnd_multiwindow_greeting)
binding.ivSource.tag = resources.getString(R.string.source_image_url)
Glide.with(this).asBitmap().load(resources.getString(R.string.source_image_url))
.into(binding.ivSource)
Glide.with(this).asBitmap().load(resources.getString(R.string.target_image_url))
.into(binding.ivTarget)
setupDrag(binding.ivSource)
setupDrop(binding.ivTarget)

}

@RequiresApi(Build.VERSION_CODES.N)
private fun setupDrag(draggableView: ImageView) {
DragStartHelper(draggableView) { view: View, _: DragStartHelper ->
val item = ClipData.Item(view.tag as? CharSequence)
val dragData = ClipData(
view.tag as? CharSequence,
arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN),
item,
)
// Flag is required so that data from clipdata can be read by the drop target.
// view can directly specify the flags in this helper method.
val flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ
view.startDragAndDrop(
dragData,
View.DragShadowBuilder(view),
null,
flags,
)
}.attach()
}

private fun setupDrop(targetView: ImageView) {
/**
* DropHelper manages permission to read data across app, provided DRAG permission has been
* granted by drag source. No additional code is required as transient permission
* are granted and released
* Consider performing processing of [ClipData] in background if it is long-running
*/
DropHelper.configureView(
requireActivity(),
targetView,
arrayOf("text/*"),
) { _, payload: ContentInfoCompat ->
val item = payload.clip.getItemAt(0)
val dragData = item.text
Glide.with(this).load(dragData).centerCrop().into(targetView)
val (_, remaining) = payload.partition { it == item }
remaining
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.platform.ui.draganddrop

import android.content.ClipData
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.core.graphics.drawable.toBitmap
import androidx.core.util.component1
import androidx.core.util.component2
import androidx.core.view.ContentInfoCompat
import androidx.core.view.DragStartHelper
import androidx.draganddrop.DropHelper
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.example.platform.ui.draganddrop.databinding.FragmentDndRichcontentBinding
import com.google.android.catalog.framework.annotations.Sample
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream


@Sample(
name = "Drag and Drop using the RichContentReceiver",
description = "Using RichContentReceiverInterface for implementing Drop for rich data types",
documentation = "https://developer.android.com/develop/ui/views/receive-rich-content",
)
class DragAndDropRichContentReceiverFragment : Fragment(R.layout.fragment_dnd_richcontent) {
private val TAG = DragAndDropRichContentReceiverFragment::class.java.simpleName
private lateinit var binding: FragmentDndRichcontentBinding

@RequiresApi(Build.VERSION_CODES.S)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentDndRichcontentBinding.bind(view)
ConstraintSet().clone(binding.root)
val items = mutableListOf<GridItem>()
initViews(items)
initDrag()

// setOnReceiveContentListener accepts data from various input sources
// based on the type of view.
// here Target View is made to handle Drag and Drop Events
binding.rvDropTarget.setOnReceiveContentListener(
arrayOf("image/*", "text/*", "video/*"),
) { _, payload ->
val (uriContent, remaining) = ContentInfoCompat.partition(
payload,
) { item: ClipData.Item -> item.uri != null }

if (uriContent != null) {
val item = uriContent.clip.getItemAt(0)
item.uri?.let { uri ->
val size = 160.px
decodeSampledBitmapFromUri(
requireActivity().contentResolver,
uri,
size,
size,
)?.let { bitmap ->
items.add(GridItem("image/*", uri.toString()))
(binding.rvDropTarget.adapter as RichContentReceiverAdapter).notifyItemInserted(
items.size - 1,
)
}
} ?: run {
Log.e(TAG, "Clip data is missing URI")
}
}
if (remaining != null) {
val item = remaining.clip.getItemAt(0)
items.add(GridItem("text/*", item.text.toString()))
(binding.rvDropTarget.adapter as RichContentReceiverAdapter).notifyItemInserted(
items.size - 1,
)

}
null
}
}

private fun initViews(items: MutableList<GridItem>) {
val layoutManager = LinearLayoutManager(requireContext())
layoutManager.orientation = LinearLayoutManager.VERTICAL
binding.rvDropTarget.layoutManager = layoutManager
val adapter = RichContentReceiverAdapter(items)
binding.rvDropTarget.adapter = adapter

//using DropHelper to define shadow for drop area
DropHelper.configureView(
requireActivity(),
binding.rvDropTarget,
arrayOf("image/*", "text/*", "video/*"),
DropHelper.Options.Builder()
.setHighlightColor(ContextCompat.getColor(requireContext(), R.color.purple_300))
.setHighlightCornerRadiusPx(16.px).build(),
) { _, payload: ContentInfoCompat ->
payload
}
}

@RequiresApi(Build.VERSION_CODES.N)
fun initDrag() {
Glide.with(this).asBitmap().load(getString(R.string.target_image_url))
.into(binding.ivSource1)
addDragStartHelperForImageView(binding.ivSource1)
Glide.with(this).asBitmap().load(getString(R.string.target_image_url1))
.into(binding.ivSource)
addDragStartHelperForImageView(binding.ivSource)

binding.tvDrag.text = getString(R.string.dnd_helper_greeting)
addDragStartHelperForTextView(binding.tvDrag)

}

@RequiresApi(Build.VERSION_CODES.N)
fun addDragStartHelperForTextView(draggbleView: View) {
DragStartHelper(draggbleView) { view: View, _: DragStartHelper ->
val dragData = ClipData.newPlainText("DraggedText", (view as TextView).text)
val flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ
view.startDragAndDrop(
dragData,
View.DragShadowBuilder(view),
null,
flags,
)
}.attach()
}

@RequiresApi(Build.VERSION_CODES.N)
fun addDragStartHelperForImageView(draggableView: View) {
DragStartHelper(draggableView) { view: View, _: DragStartHelper ->
val filename = System.currentTimeMillis().toString()
val imageFile = File(File(requireActivity().filesDir, "images"), filename + ".png")
ByteArrayOutputStream().use { bos ->
(view as ImageView).drawable.toBitmap().compress(Bitmap.CompressFormat.PNG, 0, bos)
FileOutputStream(imageFile).use { fos ->
fos.write(bos.toByteArray())
fos.flush()
}
}
val imageUri = FileProvider.getUriForFile(
requireContext(),
"${requireActivity().packageName}.draganddrop.provider",
imageFile,
)
val dragData = ClipData.newUri(
requireActivity().contentResolver,
"Image",
imageUri,
)
val flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ
view.startDragAndDrop(
dragData,
View.DragShadowBuilder(view),
null,
flags,
)
}.attach()
}
}
Loading

0 comments on commit ab694a1

Please sign in to comment.