[Android] Implement drag&drop support from other apps

Geoffrey Métais git at videolan.org
Thu Dec 21 14:05:47 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Dec 21 13:50:01 2017 +0100| [854d0c17dcfb02fc5c15d3fba19c3a3a05eae17f] | committer: Geoffrey Métais

Implement drag&drop support from other apps

> https://code.videolan.org/videolan/vlc-android/commit/854d0c17dcfb02fc5c15d3fba19c3a3a05eae17f
---

 .../src/org/videolan/vlc/gui/ContentActivity.java  | 35 ++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java b/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
index 8c7023201..a73eb8b2a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
@@ -25,10 +25,15 @@ package org.videolan.vlc.gui;
 
 
 import android.app.SearchManager;
+import android.content.ClipData;
 import android.content.Intent;
+import android.os.Bundle;
+import android.support.annotation.Nullable;
 import android.support.v4.app.Fragment;
 import android.support.v4.view.MenuItemCompat;
 import android.support.v7.widget.SearchView;
+import android.view.DragAndDropPermissions;
+import android.view.DragEvent;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
@@ -37,6 +42,7 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.gui.audio.EqualizerFragment;
 import org.videolan.vlc.gui.browser.ExtensionBrowser;
 import org.videolan.vlc.interfaces.Filterable;
+import org.videolan.vlc.media.MediaUtils;
 
 public class ContentActivity extends AudioPlayerContainerActivity implements SearchView.OnQueryTextListener, MenuItemCompat.OnActionExpandListener {
     public static final String TAG = "VLC/ContentActivity";
@@ -45,6 +51,35 @@ public class ContentActivity extends AudioPlayerContainerActivity implements Sea
     private SearchView mSearchView;
 
     @Override
+    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
+        super.onPostCreate(savedInstanceState);
+        final View view = getWindow().peekDecorView();
+        if (view != null) view.setOnDragListener(new View.OnDragListener() {
+            @Override
+            public boolean onDrag(View v, DragEvent event) {
+                switch (event.getAction()) {
+                    case DragEvent.ACTION_DRAG_STARTED:
+                        return true;
+                    case DragEvent.ACTION_DROP:
+                        final ClipData clipData = event.getClipData();
+                        if (clipData == null) return false;
+                        final int itemsCount = clipData.getItemCount();
+                        for (int i = 0; i < itemsCount; i++) {
+                            final DragAndDropPermissions permissions = requestDragAndDropPermissions(event);
+                            if (permissions != null)  {
+                                MediaUtils.openMediaNoUi(clipData.getItemAt(i).getUri());
+                                return true;
+                            }
+                        }
+                        return false;
+                    default:
+                        return false;
+                }
+            }
+        });
+    }
+
+    @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         mMenu = menu;
         if (getSupportFragmentManager().findFragmentById(R.id.fragment_placeholder) instanceof AboutFragment)



More information about the Android mailing list