السحب والإفلات في وضع النوافذ المتعددة

تتوافق الأجهزة التي تعمل بالإصدار 7.0 من نظام التشغيل Android (المستوى 24 من واجهة برمجة التطبيقات) أو الإصدارات الأحدث مع وضع النوافذ المتعددة، الذي يتيح للمستخدمين نقل البيانات من تطبيق إلى آخر عن طريق السحب والإفلات.

يوفِّر التطبيق المصدر البيانات، حيث تبدأ العملية. يتلقى التطبيق المستهدف البيانات حيث تنتهي العملية.

عندما يبدأ المستخدم في سحب المحتوى، يجب أن يضبط التطبيق المصدر علامة DRAG_FLAG_GLOBAL للإشارة إلى أنه يمكنه سحب البيانات إلى تطبيق آخر.

وبسبب انتقال البيانات عبر حدود التطبيق، تشارك التطبيقات الوصول إلى البيانات باستخدام معرّف الموارد المنتظم (URI) للمحتوى. ويتطلب ذلك ما يلي:

  • يجب أن يضبط التطبيق المصدر أحد علامتَي DRAG_FLAG_GLOBAL_URI_READ وDRAG_FLAG_GLOBAL_URI_WRITE أو كليهما، وذلك استنادًا إلى إذن الوصول للقراءة أو الكتابة إلى البيانات التي يريد التطبيق المصدر منحها للتطبيق المستهدَف.
  • يجب أن يطلب التطبيق المستهدَف الرمز requestDragAndDropPermissions() مباشرةً قبل معالجة البيانات التي يسحبها المستخدم إلى التطبيق. إذا لم يعُد التطبيق المستهدَف بحاجة إلى الوصول إلى البيانات التي تم سحبها، يمكن للتطبيق بعد ذلك استدعاء رمز release() على العنصر الذي تم عرضه من requestDragAndDropPermissions(). وبخلاف ذلك، يتم إلغاء الأذونات عند إتلاف النشاط الذي يتضمنه. إذا كانت عملية التنفيذ تتضمّن بدء نشاط جديد لمعالجة عناوين URL التي تم إسقاطها، يجب منح النشاط الجديد الأذونات نفسها. يجب تعيين بيانات المقطع وعلامة:

    Kotlin

    intent.setClipData(clipData)
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    

    Java

    intent.setClipData(clipData);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    

توضح مقتطفات التعليمات البرمجية التالية كيفية إصدار حق الوصول للقراءة فقط إلى البيانات التي تم سحبها مباشرةً بعد إجراء عملية إفلات المستخدم. راجع نموذج السحب والإفلات على GitHub للحصول على مثال أكثر اكتمالاً.

نشاط المصدر

Kotlin

// Drag a file stored in an images/ directory in internal storage.
val internalImagesDir = File(context.filesDir, "images")
val imageFile = File(internalImagesDir, imageFilename)
val uri = FileProvider.getUriForFile(context, contentAuthority, imageFile)

val listener = OnDragStartListener@{ view: View, _: DragStartHelper ->
    val clipData = ClipData(ClipDescription("Image Description",
                                            arrayOf("image/*")),
                            ClipData.Item(uri))
    // Must include DRAG_FLAG_GLOBAL to permit dragging data between apps.
    // This example provides read-only access to the data.
    val flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ
    return@OnDragStartListener view.startDragAndDrop(clipData,
                                                     View.DragShadowBuilder(view),
                                                     null,
                                                     flags)
}

// Container where the image originally appears in the source app.
val srcImageView = findViewById<ImageView>(R.id.imageView)

// Detect and start the drag event.
DragStartHelper(srcImageView, listener).apply {
    attach()
}

Java

// Drag a file stored in an images/ directory in internal storage.
File internalImagesDir = new File(context.getFilesDir(), "images");
File imageFile = new File(internalImagesDir, imageFilename);
final Uri uri = FileProvider.getUriForFile(context, contentAuthority, imageFile);

// Container where the image originally appears in the source app.
ImageView srcImageView = findViewById(R.id.imageView);

// Enable the view to detect and start the drag event.
new DragStartHelper(srcImageView, (view, helper) -> {
    ClipData clipData = new ClipData(new ClipDescription("Image Description",
                                                          new String[] {"image/*"}),
                                     new ClipData.Item(uri));
    // Must include DRAG_FLAG_GLOBAL to permit dragging data between apps.
    // This example provides read-only access to the data.
    int flags = View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ;
    return view.startDragAndDrop(clipData,
                                 new View.DragShadowBuilder(view),
                                 null,
                                 flags);
}).attach();

النشاط المستهدف

Kotlin

// Container where the image is to be dropped in the target app.
val targetImageView = findViewById<ImageView>(R.id.imageView)

targetImageView.setOnDragListener { view, event ->

    when (event.action) {

        ACTION_DROP -> {
            val imageItem: ClipData.Item = event.clipData.getItemAt(0)
            val uri = imageItem.uri

            // Request permission to access the image data being dragged into
            // the target activity's ImageView element.
            val dropPermissions = requestDragAndDropPermissions(event)
            (view as ImageView).setImageURI(uri)

            // Release the permission immediately afterward because it's no
            // longer needed.
            dropPermissions.release()
            return@setOnDragListener true
        }

        // Implement logic for other DragEvent cases here.

        // An unknown action type is received.
        else -> {
            Log.e("DragDrop Example", "Unknown action type received by View.OnDragListener.")
            return@setOnDragListener false
        }

    }
}

Java

// Container where the image is to be dropped in the target app.
ImageView targetImageView = findViewById(R.id.imageView);

targetImageView.setOnDragListener( (view, event) -> {

    switch (event.getAction()) {

        case ACTION_DROP:
            ClipData.Item imageItem = event.getClipData().getItemAt(0);
            Uri uri = imageItem.getUri();

            // Request permission to access the image data being dragged into
            // the target activity's ImageView element.
            DragAndDropPermissions dropPermissions =
                requestDragAndDropPermissions(event);

            ((ImageView)view).setImageURI(uri);

            // Release the permission immediately afterward because it's no
            // longer needed.
            dropPermissions.release();

            return true;

        // Implement logic for other DragEvent cases here.

        // An unknown action type was received.
        default:
            Log.e("DragDrop Example","Unknown action type received by View.OnDragListener.");
            break;
    }

    return false;
});