[Android] Check if quick access folders exist

Geoffrey Métais git at videolan.org
Fri Nov 3 16:25:51 CET 2017


vlc-android | branch: 2.5.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Nov  3 15:30:44 2017 +0100| [d03322498d41573dbe09a3da9d55ccec04b79835] | committer: Geoffrey Métais

Check if quick access folders exist

(cherry picked from commit b24fd8cdf0566f90cafb21b0dec81a51293f1f20)

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

 .../vlc/gui/browser/FileBrowserFragment.java       | 32 ++++++++++++++--------
 .../src/org/videolan/vlc/util/AndroidDevices.java  | 21 ++++++++------
 2 files changed, 32 insertions(+), 21 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 04790e5f8..59f4464cc 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -103,19 +103,27 @@ public class FileBrowserFragment extends BaseBrowserFragment {
                 }
                 // Set folders shortcuts
                 devices.add(new DummyItem(getString(R.string.browser_quick_access)));
-                final MediaWrapper movies = new MediaWrapper(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MOVIES_DIRECTORY_URI);
-                movies.setType(MediaWrapper.TYPE_DIR);
-                devices.add(movies);
+                if (AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MOVIES_DIRECTORY_FILE.exists()) {
+                    final MediaWrapper movies = new MediaWrapper(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MOVIES_DIRECTORY_URI);
+                    movies.setType(MediaWrapper.TYPE_DIR);
+                    devices.add(movies);
+                }
                 if (!(FileBrowserFragment.this instanceof FilePickerFragment)) {
-                    final MediaWrapper music = new MediaWrapper(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MUSIC_DIRECTORY_URI);
-                    music.setType(MediaWrapper.TYPE_DIR);
-                    devices.add(music);
-                    final MediaWrapper podcasts = new MediaWrapper(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_PODCAST_DIRECTORY_URI);
-                    podcasts.setType(MediaWrapper.TYPE_DIR);
-                    devices.add(podcasts);
-                    final MediaWrapper downloads = new MediaWrapper(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_DOWNLOAD_DIRECTORY_URI);
-                    downloads.setType(MediaWrapper.TYPE_DIR);
-                    devices.add(downloads);
+                    if (AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MUSIC_DIRECTORY_FILE.exists()) {
+                        final MediaWrapper music = new MediaWrapper(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MUSIC_DIRECTORY_URI);
+                        music.setType(MediaWrapper.TYPE_DIR);
+                        devices.add(music);
+                    }
+                    if (AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_PODCAST_DIRECTORY_FILE.exists()) {
+                        final MediaWrapper podcasts = new MediaWrapper(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_PODCAST_DIRECTORY_URI);
+                        podcasts.setType(MediaWrapper.TYPE_DIR);
+                        devices.add(podcasts);
+                    }
+                    if (AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_DOWNLOAD_DIRECTORY_FILE.exists()) {
+                        final MediaWrapper downloads = new MediaWrapper(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_DOWNLOAD_DIRECTORY_URI);
+                        downloads.setType(MediaWrapper.TYPE_DIR);
+                        devices.add(downloads);
+                    }
                 }
                 VLCApplication.runOnMainThread(new Runnable() {
                     @Override
diff --git a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
index e6350c542..9670fc479 100644
--- a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
+++ b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
@@ -28,7 +28,6 @@ import android.net.Uri;
 import android.os.Build;
 import android.os.Build.VERSION_CODES;
 import android.os.Environment;
-import android.support.annotation.NonNull;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.view.InputDevice;
@@ -221,17 +220,21 @@ public class AndroidDevices {
     }
 
     public static class MediaFolders {
-        public final static Uri EXTERNAL_PUBLIC_MOVIES_DIRECTORY_URI = getFolderUri(Environment.DIRECTORY_MOVIES);
-        public final static Uri EXTERNAL_PUBLIC_MUSIC_DIRECTORY_URI = getFolderUri(Environment.DIRECTORY_MUSIC);
-        public final static Uri EXTERNAL_PUBLIC_PODCAST_DIRECTORY_URI = getFolderUri(Environment.DIRECTORY_PODCASTS);
-        public final static Uri EXTERNAL_PUBLIC_DOWNLOAD_DIRECTORY_URI = getFolderUri(Environment.DIRECTORY_DOWNLOADS);
+        public final static File EXTERNAL_PUBLIC_MOVIES_DIRECTORY_FILE = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
+        public final static File EXTERNAL_PUBLIC_MUSIC_DIRECTORY_FILE = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
+        public final static File EXTERNAL_PUBLIC_PODCAST_DIRECTORY_FILE = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS);
+        public final static File EXTERNAL_PUBLIC_DOWNLOAD_DIRECTORY_FILE = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
 
-        @NonNull
-        private static Uri getFolderUri(String type) {
+        public final static Uri EXTERNAL_PUBLIC_MOVIES_DIRECTORY_URI = getFolderUri(EXTERNAL_PUBLIC_MOVIES_DIRECTORY_FILE);
+        public final static Uri EXTERNAL_PUBLIC_MUSIC_DIRECTORY_URI = getFolderUri(EXTERNAL_PUBLIC_MUSIC_DIRECTORY_FILE);
+        public final static Uri EXTERNAL_PUBLIC_PODCAST_DIRECTORY_URI = getFolderUri(EXTERNAL_PUBLIC_PODCAST_DIRECTORY_FILE);
+        public final static Uri EXTERNAL_PUBLIC_DOWNLOAD_DIRECTORY_URI = getFolderUri(EXTERNAL_PUBLIC_DOWNLOAD_DIRECTORY_FILE);
+
+        private static Uri getFolderUri(File file) {
             try {
-                return Uri.parse("file://"+ Environment.getExternalStoragePublicDirectory(type).getCanonicalPath());
+                return Uri.parse("file://"+ file.getCanonicalPath());
             } catch (IOException ignored) {
-                return Uri.parse("file://"+Environment.getExternalStoragePublicDirectory(type).getPath());
+                return Uri.parse("file://"+ file.getPath());
             }
         }
     }



More information about the Android mailing list