|
| 1 | +/* |
| 2 | + * Copyright 2023 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.platform.ui.draganddrop |
| 18 | + |
| 19 | +import android.content.ClipData |
| 20 | +import android.graphics.Bitmap |
| 21 | +import android.os.Build |
| 22 | +import android.os.Bundle |
| 23 | +import android.util.Log |
| 24 | +import android.view.View |
| 25 | +import android.widget.ImageView |
| 26 | +import android.widget.TextView |
| 27 | +import androidx.annotation.RequiresApi |
| 28 | +import androidx.constraintlayout.widget.ConstraintSet |
| 29 | +import androidx.core.content.ContextCompat |
| 30 | +import androidx.core.content.FileProvider |
| 31 | +import androidx.core.graphics.drawable.toBitmap |
| 32 | +import androidx.core.util.component1 |
| 33 | +import androidx.core.util.component2 |
| 34 | +import androidx.core.view.ContentInfoCompat |
| 35 | +import androidx.core.view.DragStartHelper |
| 36 | +import androidx.draganddrop.DropHelper |
| 37 | +import androidx.fragment.app.Fragment |
| 38 | +import androidx.recyclerview.widget.LinearLayoutManager |
| 39 | +import com.bumptech.glide.Glide |
| 40 | +import com.example.platform.ui.draganddrop.databinding.FragmentDndRichcontentBinding |
| 41 | +import com.google.android.catalog.framework.annotations.Sample |
| 42 | +import java.io.ByteArrayOutputStream |
| 43 | +import java.io.File |
| 44 | +import java.io.FileOutputStream |
| 45 | + |
| 46 | + |
| 47 | +@Sample( |
| 48 | + name = "Drag and Drop using the RichContentReceiver", |
| 49 | + description = "Using RichContentReceiverInterface for implementing Drop for rich data types", |
| 50 | + documentation = "https://developer.android.com/develop/ui/views/receive-rich-content", |
| 51 | +) |
| 52 | +class DragAndDropRichContentReceiverFragment : Fragment(R.layout.fragment_dnd_richcontent) { |
| 53 | + private val TAG = DragAndDropRichContentReceiverFragment::class.java.simpleName |
| 54 | + private lateinit var binding: FragmentDndRichcontentBinding |
| 55 | + |
| 56 | + @RequiresApi(Build.VERSION_CODES.S) |
| 57 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 58 | + super.onViewCreated(view, savedInstanceState) |
| 59 | + binding = FragmentDndRichcontentBinding.bind(view) |
| 60 | + ConstraintSet().clone(binding.root) |
| 61 | + val items = mutableListOf<GridItem>() |
| 62 | + initViews(items) |
| 63 | + initDrag() |
| 64 | + |
| 65 | + // setOnReceiveContentListener accepts data from various input sources |
| 66 | + // based on the type of view. |
| 67 | + // here Target View is made to handle Drag and Drop Events |
| 68 | + binding.rvDropTarget.setOnReceiveContentListener( |
| 69 | + arrayOf("image/*", "text/*", "video/*"), |
| 70 | + ) { _, payload -> |
| 71 | + val (uriContent, remaining) = ContentInfoCompat.partition( |
| 72 | + payload, |
| 73 | + ) { item: ClipData.Item -> item.uri != null } |
| 74 | + |
| 75 | + if (uriContent != null) { |
| 76 | + val item = uriContent.clip.getItemAt(0) |
| 77 | + item.uri?.let { uri -> |
| 78 | + val size = 160.px |
| 79 | + decodeSampledBitmapFromUri( |
| 80 | + requireActivity().contentResolver, |
| 81 | + uri, |
| 82 | + size, |
| 83 | + size, |
| 84 | + )?.let { bitmap -> |
| 85 | + items.add(GridItem("image/*", uri.toString())) |
| 86 | + (binding.rvDropTarget.adapter as RichContentReceiverAdapter).notifyItemInserted( |
| 87 | + items.size - 1, |
| 88 | + ) |
| 89 | + } |
| 90 | + } ?: run { |
| 91 | + Log.e(TAG, "Clip data is missing URI") |
| 92 | + } |
| 93 | + } |
| 94 | + if (remaining != null) { |
| 95 | + val item = remaining.clip.getItemAt(0) |
| 96 | + items.add(GridItem("text/*", item.text.toString())) |
| 97 | + (binding.rvDropTarget.adapter as RichContentReceiverAdapter).notifyItemInserted( |
| 98 | + items.size - 1, |
| 99 | + ) |
| 100 | + |
| 101 | + } |
| 102 | + null |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private fun initViews(items: MutableList<GridItem>) { |
| 107 | + val layoutManager = LinearLayoutManager(requireContext()) |
| 108 | + layoutManager.orientation = LinearLayoutManager.VERTICAL |
| 109 | + binding.rvDropTarget.layoutManager = layoutManager |
| 110 | + val adapter = RichContentReceiverAdapter(items) |
| 111 | + binding.rvDropTarget.adapter = adapter |
| 112 | + |
| 113 | + //using DropHelper to define shadow for drop area |
| 114 | + DropHelper.configureView( |
| 115 | + requireActivity(), |
| 116 | + binding.rvDropTarget, |
| 117 | + arrayOf("image/*", "text/*", "video/*"), |
| 118 | + DropHelper.Options.Builder() |
| 119 | + .setHighlightColor(ContextCompat.getColor(requireContext(), R.color.purple_300)) |
| 120 | + .setHighlightCornerRadiusPx(16.px).build(), |
| 121 | + ) { _, payload: ContentInfoCompat -> |
| 122 | + payload |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @RequiresApi(Build.VERSION_CODES.N) |
| 127 | + fun initDrag() { |
| 128 | + Glide.with(this).asBitmap().load(getString(R.string.target_image_url)) |
| 129 | + .into(binding.ivSource1) |
| 130 | + addDragStartHelperForImageView(binding.ivSource1) |
| 131 | + Glide.with(this).asBitmap().load(getString(R.string.target_image_url1)) |
| 132 | + .into(binding.ivSource) |
| 133 | + addDragStartHelperForImageView(binding.ivSource) |
| 134 | + |
| 135 | + binding.tvDrag.text = getString(R.string.dnd_helper_greeting) |
| 136 | + addDragStartHelperForTextView(binding.tvDrag) |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + @RequiresApi(Build.VERSION_CODES.N) |
| 141 | + fun addDragStartHelperForTextView(draggbleView: View) { |
| 142 | + DragStartHelper(draggbleView) { view: View, _: DragStartHelper -> |
| 143 | + val dragData = ClipData.newPlainText("DraggedText", (view as TextView).text) |
| 144 | + val flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ |
| 145 | + view.startDragAndDrop( |
| 146 | + dragData, |
| 147 | + View.DragShadowBuilder(view), |
| 148 | + null, |
| 149 | + flags, |
| 150 | + ) |
| 151 | + }.attach() |
| 152 | + } |
| 153 | + |
| 154 | + @RequiresApi(Build.VERSION_CODES.N) |
| 155 | + fun addDragStartHelperForImageView(draggableView: View) { |
| 156 | + DragStartHelper(draggableView) { view: View, _: DragStartHelper -> |
| 157 | + val filename = System.currentTimeMillis().toString() |
| 158 | + val imageFile = File(File(requireActivity().filesDir, "images"), filename + ".png") |
| 159 | + ByteArrayOutputStream().use { bos -> |
| 160 | + (view as ImageView).drawable.toBitmap().compress(Bitmap.CompressFormat.PNG, 0, bos) |
| 161 | + FileOutputStream(imageFile).use { fos -> |
| 162 | + fos.write(bos.toByteArray()) |
| 163 | + fos.flush() |
| 164 | + } |
| 165 | + } |
| 166 | + val imageUri = FileProvider.getUriForFile( |
| 167 | + requireContext(), |
| 168 | + "${requireActivity().packageName}.draganddrop.provider", |
| 169 | + imageFile, |
| 170 | + ) |
| 171 | + val dragData = ClipData.newUri( |
| 172 | + requireActivity().contentResolver, |
| 173 | + "Image", |
| 174 | + imageUri, |
| 175 | + ) |
| 176 | + val flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ |
| 177 | + view.startDragAndDrop( |
| 178 | + dragData, |
| 179 | + View.DragShadowBuilder(view), |
| 180 | + null, |
| 181 | + flags, |
| 182 | + ) |
| 183 | + }.attach() |
| 184 | + } |
| 185 | +} |
0 commit comments