Skip to content

Commit

Permalink
Drag and Drop using Rich Content Receiver
Browse files Browse the repository at this point in the history
Change-Id: I214fc45de5f7e80e355bb75e689d17c7ae3644b3
  • Loading branch information
satishshendeg committed Apr 30, 2024
1 parent 38929ee commit b720b8b
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 8 deletions.
2 changes: 2 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Demonstrates basic Drag and Drop functionality.
Drag and Drop using the DragHelper and DropHelper from DragAndDropHelper library
- [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
3 changes: 3 additions & 0 deletions samples/user-interface/draganddrop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ dependencies {
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DragAndDropMultiWindow : Fragment(R.layout.fragment_dnd_multiwindow) {
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.targer_image_url))
Glide.with(this).asBitmap().load(resources.getString(R.string.target_image_url))
.into(binding.ivTarget)
setupDrag(binding.ivSource)
setupDrop(binding.ivTarget)
Expand All @@ -72,7 +72,7 @@ class DragAndDropMultiWindow : Fragment(R.layout.fragment_dnd_multiwindow) {
// view can directly specify the flags in this helper method.
// additionally if you are using [View.startDragAndDrop] for drag implementation
// you can set the flags in similar fashion
var flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ
val flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ
view.startDragAndDrop(
dragData,
View.DragShadowBuilder(view),
Expand Down
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DragAndDropWithHelper : Fragment(R.layout.fragment_drag_and_drop_with_help
binding = FragmentDragAndDropWithHelperBinding.bind(view)
ConstraintSet().clone(binding.root)

binding.tvGreeting.text = getString(R.string.greeting_text)
binding.tvGreeting.text = getString(R.string.dnd_helper_greeting)
/*
data for drag
for simplicity we have considered text as a data type to drag
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.VISIBLE
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.example.platform.ui.draganddrop.databinding.GridItemBinding

class RichContentReceiverAdapter(private val items: List<GridItem>) :
RecyclerView.Adapter<RichContentReceiverAdapter.DroppedItem>() {
inner class DroppedItem(val binding: GridItemBinding) : ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DroppedItem {
val binding = GridItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return DroppedItem(binding)
}

override fun getItemCount(): Int {
return items.size
}

override fun onBindViewHolder(holder: DroppedItem, position: Int) {
with(holder) {
binding.droppedText.text = position.toString()

when (items[position].mimeType) {
"image/*" -> {
Glide.with(itemView.context).asBitmap().load(items[position].value)
.into(binding.ivDroppedImage)
binding.ivDroppedImage.visibility = VISIBLE
}

"text/*" -> {
binding.droppedText.text = items[position].value
binding.droppedText.visibility = VISIBLE
}
}
}
}
}

data class GridItem(val mimeType: String, val value: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ 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.
-->

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_drop_target"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/cardview_shadow_start_color"
app:layout_constraintBottom_toTopOf="@id/guideline"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/drag_source" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/drag_source"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/divider"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">

<ImageView
android:id="@+id/iv_source"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:contentDescription="@string/drag_image"
app:layout_constraintBottom_toTopOf="@id/iv_source_1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv_drag"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/iv_source_1"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:contentDescription="@string/drag_image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/ed_drag"
app:layout_constraintTop_toBottomOf="@id/iv_source" />

<EditText
android:id="@+id/ed_drag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/editable_drag_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/iv_source_1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_drag"
android:inputType="text"/>

<TextView
android:id="@+id/tv_drag"
android:layout_width="128dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/ed_drag"
app:layout_constraintLeft_toRightOf="@id/iv_source"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit b720b8b

Please sign in to comment.