-
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.
Merge pull request #163 from android/dnd
Drag and Drop samples update
- Loading branch information
Showing
13 changed files
with
910 additions
and
1 deletion.
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
101 changes: 101 additions & 0 deletions
101
...e/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDropMultiWindow.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,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 | ||
} | ||
} | ||
} |
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() | ||
} | ||
} |
Oops, something went wrong.