[Android] Extract OnDragListener from Activity code
Geoffrey Métais
git at videolan.org
Thu Mar 8 14:32:31 CET 2018
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Mar 8 14:32:11 2018 +0100| [1c6b06912fcd5ac37ea0495895b0c65ac757ae75] | committer: Geoffrey Métais
Extract OnDragListener from Activity code
> https://code.videolan.org/videolan/vlc-android/commit/1c6b06912fcd5ac37ea0495895b0c65ac757ae75
---
.../src/org/videolan/vlc/gui/ContentActivity.java | 39 +-------------------
.../src/org/videolan/vlc/gui/helpers/UiTools.java | 43 +++++++++++++++++++++-
2 files changed, 42 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..3fcf25591 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;
@@ -70,9 +74,11 @@ import org.videolan.vlc.BuildConfig;
import org.videolan.vlc.MediaParsingService;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
+import org.videolan.vlc.gui.ContentActivity;
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 +397,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 +447,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(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;
+ }
+ }
+ });
+ }
}
More information about the Android
mailing list