override fun onCreate()

in DragAndDrop/app/src/main/java/dev/hadrosaur/draganddropsample/MainActivity.kt [60:169]


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val mainConstraintLayout = binding.root
        setContentView(mainConstraintLayout)

        binding.buttonNewTask.setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java).apply {
                addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
            })
        }

        // Use the DragStartHelper class to easily support initiating drag and drop in response to
        // both long press and mouse drag events. Note the call to attach() at the end. Without it,
        // the listener would never actually be attached to the view. Also note that attach() replaces
        // any OnTouchListener or OnLongClickListener already attached to the view.
        DragStartHelper(binding.textDragItem) { view, _ ->
            val text = (view as TextView).text

            // Create the ClipData to be shared
            val dragClipData = ClipData.newPlainText(/*label*/"Text", text)

            // Use the default drag shadow
            val dragShadowBuilder = View.DragShadowBuilder(view)

            // Initiate the drag. Note the DRAG_FLAG_GLOBAL, which allows for drag events to be listened
            // to by apps other than the source app.
            view.startDragAndDrop(dragClipData, dragShadowBuilder, null, DRAG_FLAG_GLOBAL)
        }.attach()

        DragStartHelper(binding.imageDragItem) { view, _ ->
            val imageFile = File(File(filesDir, "images"), "earth.png")

            if (!imageFile.exists()) {
                // Local file doesn't exist, create it
                File(filesDir, "images").mkdirs()
                // Write the file to local storage
                ByteArrayOutputStream().use { bos ->
                    (view as ImageView).drawable.toBitmap()
                        .compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos)
                    FileOutputStream(imageFile).use { fos ->
                        fos.write(bos.toByteArray())
                        fos.flush()
                    }
                }
            }

            val imageUri =
                FileProvider.getUriForFile(
                    this,
                    "dev.hadrosaur.draganddropsample.images",
                    imageFile
                )

            // Sets the appropriate MIME types automatically
            val dragClipData = ClipData.newUri(contentResolver, "Image", imageUri)

            // Set the visual look of the dragged object
            // Can be extended and customized. We use the default here.
            val dragShadow = View.DragShadowBuilder(view)

            // Starts the drag, note: global flag allows for cross-application drag
            view.startDragAndDrop(
                dragClipData,
                dragShadow,
                null,
                // Since this is a "content:" URI and not just plain text, we can use the
                // DRAG_FLAG_GLOBAL_URI_READ to allow other apps to read from our content provider.
                // Without it, other apps won't receive the drag events
                DRAG_FLAG_GLOBAL.or(DRAG_FLAG_GLOBAL_URI_READ)
            )
        }.attach()

        DropHelper.configureView(
            this,
            binding.textDropTarget,
            arrayOf(
                MIMETYPE_TEXT_PLAIN,
                "image/*",
                "application/x-arc-uri-list" // Support external items on Chrome OS Android 9
            ),
            DropHelper.Options.Builder()
                .setHighlightColor(getColor(R.color.purple_300))
                // Match the radius of the view's background drawable
                .setHighlightCornerRadiusPx(resources.getDimensionPixelSize(R.dimen.drop_target_corner_radius))
                .build()
        ) { _, payload ->
            resetDropTarget()

            // For the purposes of this demo, only handle the first ClipData.Item
            val item = payload.clip.getItemAt(0)
            val (_, remaining) = payload.partition { it == item }

            when {
                payload.clip.description.hasMimeType(MIMETYPE_TEXT_PLAIN) ->
                    handlePlainTextDrop(item)
                else ->
                    handleImageDrop(item)
            }

            // Allow the system to handle any remaining ClipData.Item objects if applicable
            remaining
        }

        binding.buttonClear.setOnClickListener {
            resetDropTarget()
        }
    }