[Android] Extract OnDragListener from Activity code

Geoffrey Métais git at videolan.org
Fri Mar 30 16:51:04 CEST 2018


vlc-android | branch: 3.0.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Mar  8 14:32:11 2018 +0100| [821b790200aea1d60c8dced3a79ac9f89ad6f841] | committer: Geoffrey Métais

Extract OnDragListener from Activity code

(cherry picked from commit 2a2d72dc418fdb0b4f6be9f90aa4c4769de299f3)

> https://code.videolan.org/videolan/vlc-android/commit/821b790200aea1d60c8dced3a79ac9f89ad6f841
---

 .../src/org/videolan/vlc/gui/ContentActivity.java  | 39 +-------------------
 .../src/org/videolan/vlc/gui/helpers/UiTools.java  | 42 ++++++++++++++++++++--
 2 files changed, 41 insertions(+), 40 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java b/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
index 61fd3d478..445da6ce7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
@@ -24,23 +24,17 @@
 package org.videolan.vlc.gui;
 
 import android.app.SearchManager;
-import android.content.ClipData;
 import android.content.Intent;
-import android.net.Uri;
 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;
 
 import org.videolan.libvlc.RendererItem;
-import org.videolan.libvlc.util.AndroidUtil;
-import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.vlc.R;
 import org.videolan.vlc.RendererDelegate;
 import org.videolan.vlc.gui.audio.AudioBrowserFragment;
@@ -51,7 +45,6 @@ import org.videolan.vlc.gui.dialogs.RenderersDialog;
 import org.videolan.vlc.gui.helpers.UiTools;
 import org.videolan.vlc.gui.video.VideoGridFragment;
 import org.videolan.vlc.interfaces.Filterable;
-import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.AndroidDevices;
 
 public class ContentActivity extends AudioPlayerContainerActivity implements SearchView.OnQueryTextListener, MenuItemCompat.OnActionExpandListener, RendererDelegate.RendererListener, RendererDelegate.RendererPlayer {
@@ -64,37 +57,7 @@ public class ContentActivity extends AudioPlayerContainerActivity implements Sea
     @Override
     protected void onPostCreate(@Nullable Bundle savedInstanceState) {
         super.onPostCreate(savedInstanceState);
-        final View view = AndroidUtil.isNougatOrLater ? getWindow().peekDecorView() : null;
-        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)  {
-                                final ClipData.Item item = clipData.getItemAt(i);
-                                if (item.getUri() != null) MediaUtils.openUri(ContentActivity.this, item.getUri());
-                                else if (item.getText() != null) {
-                                    final Uri uri = Uri.parse(item.getText().toString());
-                                    final MediaWrapper media = new MediaWrapper(uri);
-                                    if (!"file".equals(uri.getScheme())) media.setType(MediaWrapper.TYPE_STREAM);
-                                    MediaUtils.openMedia(ContentActivity.this, media);
-                                }
-                                return true;
-                            }
-                        }
-                        return false;
-                    default:
-                        return false;
-                }
-            }
-        });
+        UiTools.setOnDragListener(this);
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
index 0a5825643..ad013e3e7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
@@ -25,6 +25,7 @@ package org.videolan.vlc.gui.helpers;
 
 import android.annotation.TargetApi;
 import android.app.Activity;
+import android.content.ClipData;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -33,6 +34,7 @@ import android.content.res.TypedArray;
 import android.databinding.BindingAdapter;
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
+import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
@@ -52,6 +54,8 @@ import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.text.Html;
 import android.text.TextUtils;
+import android.view.DragAndDropPermissions;
+import android.view.DragEvent;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
@@ -73,6 +77,7 @@ import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.audio.BaseAudioBrowser;
 import org.videolan.vlc.gui.browser.SortableFragment;
 import org.videolan.vlc.gui.dialogs.SavePlaylistDialog;
+import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.Constants;
 import org.videolan.vlc.util.FileUtils;
 import org.videolan.vlc.util.MediaLibraryItemComparator;
@@ -391,8 +396,7 @@ public class UiTools {
     }
 
     public static void newStorageDetected(final Activity activity, final String path) {
-        if (activity == null)
-            return;
+        if (activity == null) return;
         final String uuid = FileUtils.getFileNameFromPath(path);
         final String deviceName = FileUtils.getStorageTag(uuid);
         final String message = String.format(activity.getString(R.string.ml_external_storage_msg), deviceName != null ? deviceName : uuid);
@@ -442,4 +446,38 @@ public class UiTools {
             builder.show();
         }
     }
+
+    public static void setOnDragListener(final Activity activity) {
+        final View view = AndroidUtil.isNougatOrLater ? activity.getWindow().peekDecorView() : null;
+        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 = activity.requestDragAndDropPermissions(event);
+                            if (permissions != null)  {
+                                final ClipData.Item item = clipData.getItemAt(i);
+                                if (item.getUri() != null) MediaUtils.openUri(activity, item.getUri());
+                                else if (item.getText() != null) {
+                                    final Uri uri = Uri.parse(item.getText().toString());
+                                    final MediaWrapper media = new MediaWrapper(uri);
+                                    if (!"file".equals(uri.getScheme())) media.setType(MediaWrapper.TYPE_STREAM);
+                                    MediaUtils.openMedia(activity, media);
+                                }
+                                return true;
+                            }
+                        }
+                        return false;
+                    default:
+                        return false;
+                }
+            }
+        });
+    }
 }



More information about the Android mailing list