Commit 821b7902 authored by Geoffrey Métais's avatar Geoffrey Métais

Extract OnDragListener from Activity code

(cherry picked from commit 2a2d72dc)
parent 495b1b93
......@@ -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
......
......@@ -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;
}
}
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment