-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drag and Drop using Rich Content Receiver
Change-Id: I214fc45de5f7e80e355bb75e689d17c7ae3644b3
- Loading branch information
1 parent
38929ee
commit b720b8b
Showing
9 changed files
with
393 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
...c/main/java/com/example/platform/ui/draganddrop/DragAndDropRichContentReceiverFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...aganddrop/src/main/java/com/example/platform/ui/draganddrop/RichContentReceiverAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
99 changes: 99 additions & 0 deletions
99
samples/user-interface/draganddrop/src/main/res/layout/fragment_dnd_richcontent.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.