[Android] Show real SDCard/USB storage name
Geoffrey Métais
git at videolan.org
Fri Feb 16 14:25:57 CET 2018
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Feb 16 11:56:50 2018 +0100| [5c7e37ccd64cbea8e1570700cee931b6d86612ca] | committer: Geoffrey Métais
Show real SDCard/USB storage name
Works only for Android 6+
Close #464
> https://code.videolan.org/videolan/vlc-android/commit/5c7e37ccd64cbea8e1570700cee931b6d86612ca
---
.../vlc/gui/browser/FileBrowserFragment.java | 4 ++++
.../src/org/videolan/vlc/gui/helpers/UiTools.java | 3 ++-
.../src/org/videolan/vlc/util/AndroidDevices.java | 7 +++++--
.../src/org/videolan/vlc/util/FileUtils.java | 21 +++++++++++++++++++++
4 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
index 155ebe151..a536d1123 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -110,6 +110,10 @@ public class FileBrowserFragment extends BaseBrowserFragment {
directory.setType(MediaWrapper.TYPE_DIR);
if (TextUtils.equals(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY, mediaDirLocation))
directory.setDisplayTitle(internalmemoryTitle);
+ else {
+ final String deviceName = FileUtils.getStorageTag(directory.getTitle());
+ if (deviceName != null) directory.setDisplayTitle(deviceName);
+ }
devices.add(directory);
}
// Set folders shortcuts
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 333a93b70..0a5825643 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
@@ -394,7 +394,8 @@ public class UiTools {
if (activity == null)
return;
final String uuid = FileUtils.getFileNameFromPath(path);
- final String message = String.format(activity.getString(R.string.ml_external_storage_msg), uuid);
+ final String deviceName = FileUtils.getStorageTag(uuid);
+ final String message = String.format(activity.getString(R.string.ml_external_storage_msg), deviceName != null ? deviceName : uuid);
final Intent serviceInent = new Intent(Constants.ACTION_DISCOVER_DEVICE, null, activity, MediaParsingService.class)
.putExtra(Constants.EXTRA_PATH, path);
if (activity instanceof AppCompatActivity) {
diff --git a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
index f527916c4..f696fa846 100644
--- a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
+++ b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
@@ -157,12 +157,15 @@ public class AndroidDevices {
final LinkedList<MediaWrapper> list = new LinkedList<>();
MediaWrapper directory;
for (String mediaDirLocation : storages) {
- if (!(new File(mediaDirLocation).exists()))
- continue;
+ if (!(new File(mediaDirLocation).exists())) continue;
directory = new MediaWrapper(AndroidUtil.PathToUri(mediaDirLocation));
directory.setType(MediaWrapper.TYPE_DIR);
if (TextUtils.equals(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY, mediaDirLocation))
directory.setDisplayTitle(VLCApplication.getAppResources().getString(R.string.internal_memory));
+ else {
+ final String deviceName = FileUtils.getStorageTag(directory.getTitle());
+ if (deviceName != null) directory.setDisplayTitle(deviceName);
+ }
list.add(directory);
}
return list;
diff --git a/vlc-android/src/org/videolan/vlc/util/FileUtils.java b/vlc-android/src/org/videolan/vlc/util/FileUtils.java
index 1342d1d4f..cb53af709 100644
--- a/vlc-android/src/org/videolan/vlc/util/FileUtils.java
+++ b/vlc-android/src/org/videolan/vlc/util/FileUtils.java
@@ -23,6 +23,7 @@
package org.videolan.vlc.util;
+import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.Context;
@@ -31,6 +32,7 @@ import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
+import android.os.storage.StorageManager;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v4.provider.DocumentFile;
@@ -51,6 +53,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.LongBuffer;
@@ -416,4 +419,22 @@ public class FileUtils {
}
return uri;
}
+
+ @SuppressLint("PrivateApi")
+ public static String getStorageTag(final String uuid) {
+ if (!AndroidUtil.isMarshMallowOrLater) return null;
+ String volumeDescription = null;
+ try {
+ final StorageManager storageManager = VLCApplication.getAppContext().getSystemService(StorageManager.class);
+ final Class<?> classType = storageManager.getClass();
+ final Method findVolumeByUuid = classType.getDeclaredMethod("findVolumeByUuid", uuid.getClass());
+ findVolumeByUuid.setAccessible(true);
+ final Object volumeInfo = findVolumeByUuid.invoke(storageManager, uuid);
+ final Class volumeInfoClass = Class.forName("android.os.storage.VolumeInfo");
+ final Method getBestVolumeDescription = classType.getDeclaredMethod("getBestVolumeDescription", volumeInfoClass);
+ getBestVolumeDescription.setAccessible(true);
+ volumeDescription = (String) getBestVolumeDescription.invoke(storageManager, volumeInfo);
+ } catch (Throwable ignored) {}
+ return volumeDescription;
+ }
}
More information about the Android
mailing list