[Android] Ask device discover for unknown external storages

Geoffrey Métais git at videolan.org
Fri Apr 14 17:29:09 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Apr 14 14:48:52 2017 +0200| [3f2e4538dadfaab86f38f3d34eaf33f9ea7bbe95] | committer: Geoffrey Métais

Ask device discover for unknown external storages

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

 .../src/org/videolan/vlc/MediaParsingService.java  | 21 ++++++------------
 .../vlc/gui/AudioPlayerContainerActivity.java      | 25 +++++++++++-----------
 .../vlc/gui/tv/browser/BaseTvActivity.java         | 15 +++++++------
 3 files changed, 28 insertions(+), 33 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.java b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
index 5fad65c..693f175 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.java
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
@@ -25,7 +25,6 @@ import org.videolan.vlc.gui.DialogActivity;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.FileUtils;
 import org.videolan.vlc.util.Strings;
-import org.videolan.vlc.util.Util;
 
 import java.io.File;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -178,7 +177,13 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
                     String[] storages = AndroidDevices.getMediaDirectories();
                     for (String storage : storages) {
                         boolean isMainStorage = TextUtils.equals(storage, AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY);
-                        mMedialibrary.addDevice(isMainStorage ? "main-storage" : FileUtils.getFileNameFromPath(storage), storage, !isMainStorage);
+                        boolean isNew = mMedialibrary.addDevice(isMainStorage ? "main-storage" : FileUtils.getFileNameFromPath(storage), storage, !isMainStorage);
+                        if (!isMainStorage && isNew) {
+                            startActivity(new Intent(MediaParsingService.this, DialogActivity.class)
+                                        .setAction(DialogActivity.KEY_STORAGE)
+                                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+                                        .putExtra(EXTRA_PATH, storage));
+                        }
                     }
                     if (mMedialibrary.init(MediaParsingService.this)) {
                         LocalBroadcastManager.getInstance(MediaParsingService.this).sendBroadcast(new Intent(VLCApplication.ACTION_MEDIALIBRARY_READY));
@@ -189,18 +194,6 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
                         } else if (upgrade) {
                             mMedialibrary.forceParserRetry();
                         }
-                        final String[] foldersList = mMedialibrary.getFoldersList();
-                        final SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(MediaParsingService.this);
-                        for (String externalStorage : AndroidDevices.getExternalStorageDirectories()) {
-                            if (!TextUtils.equals(externalStorage, AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)
-                                    && !Util.arrayContains(foldersList, "file://" + externalStorage + "/")
-                                    && !mSettings.getBoolean("ignore_"+FileUtils.getFileNameFromPath(externalStorage), false)) {
-                                startActivity(new Intent(MediaParsingService.this, DialogActivity.class)
-                                        .setAction(DialogActivity.KEY_STORAGE)
-                                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
-                                        .putExtra(EXTRA_PATH, externalStorage));
-                            }
-                        }
                     }
                 }
             });
diff --git a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
index a6b68d7..cb0f473 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
@@ -53,14 +53,12 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.audio.AudioPlayer;
 import org.videolan.vlc.gui.browser.StorageBrowserFragment;
+import org.videolan.vlc.gui.tv.browser.BaseTvActivity;
 import org.videolan.vlc.interfaces.IRefreshable;
 import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.Strings;
 import org.videolan.vlc.util.WeakHandler;
 
-import static org.videolan.vlc.MediaParsingService.EXTRA_PATH;
-import static org.videolan.vlc.MediaParsingService.EXTRA_UUID;
-
 public class AudioPlayerContainerActivity extends BaseActivity implements PlaybackService.Client.Callback {
 
     public static final String TAG = "VLC/AudioPlayerContainerActivity";
@@ -348,21 +346,24 @@ public class AudioPlayerContainerActivity extends BaseActivity implements Playba
         @Override
         public void handleMessage(Message msg) {
             super.handleMessage(msg);
+            AudioPlayerContainerActivity owner = getOwner();
+            if (owner == null)
+                return;
+            String uuid = ((Uri) msg.obj).getLastPathSegment();
             switch (msg.what){
                 case ACTION_MEDIA_MOUNTED:
                     String path = ((Uri) msg.obj).getPath();
-                    String uuid = ((Uri) msg.obj).getLastPathSegment();
                     removeMessages(ACTION_MEDIA_UNMOUNTED);
-                    getOwner().updateLib();
-                    getOwner().startActivity(new Intent(getOwner(), DialogActivity.class)
-                            .setAction(DialogActivity.KEY_STORAGE)
-                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
-                            .putExtra(EXTRA_PATH, path)
-                            .putExtra(EXTRA_UUID, uuid));
+                    if (VLCApplication.getMLInstance().addDevice(uuid, path, true)) {
+                        owner.startActivity(new Intent(owner, DialogActivity.class)
+                                .setAction(DialogActivity.KEY_STORAGE)
+                                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+                                .putExtra(MediaParsingService.EXTRA_PATH, path));
+                    }
                     break;
                 case ACTION_MEDIA_UNMOUNTED:
-                    getOwner().startService(new Intent(MediaParsingService.ACTION_RELOAD, null, getOwner(), MediaParsingService.class));
-                    getOwner().updateLib();
+                    VLCApplication.getMLInstance().removeDevice(uuid);
+                    owner.startService(new Intent(MediaParsingService.ACTION_RELOAD, null, owner, MediaParsingService.class));
                     break;
             }
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java
index a03bb3c..8e5a176 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java
@@ -48,6 +48,7 @@ import org.videolan.vlc.util.WeakHandler;
 
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
 public abstract class BaseTvActivity extends PlaybackServiceActivity {
+
     protected Medialibrary mMediaLibrary;
     protected SharedPreferences mSettings;
     boolean mRegistering = false;
@@ -110,15 +111,15 @@ public abstract class BaseTvActivity extends PlaybackServiceActivity {
             } else if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_MOUNTED)) {
                 String path = intent.getData().getPath();
                 String uuid = intent.getData().getLastPathSegment();
-                startActivity(new Intent(BaseTvActivity.this, DialogActivity.class)
-                        .setAction(DialogActivity.KEY_STORAGE)
-                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
-                        .putExtra(MediaParsingService.EXTRA_PATH, path)
-                        .putExtra(MediaParsingService.EXTRA_UUID, uuid));
-                mStorageHandlerHandler.sendEmptyMessageDelayed(ACTION_MEDIA_MOUNTED, 500);
+                if (mMediaLibrary.addDevice(uuid, path, true)) {
+                    startActivity(new Intent(BaseTvActivity.this, DialogActivity.class)
+                            .setAction(DialogActivity.KEY_STORAGE)
+                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+                            .putExtra(MediaParsingService.EXTRA_PATH, path));
+                }
             } else if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT) || action.equalsIgnoreCase(Intent.ACTION_MEDIA_REMOVED)) {
                 mMediaLibrary.removeDevice(intent.getData().getLastPathSegment());
-                mMediaLibrary.reload();
+                startService(new Intent(MediaParsingService.ACTION_RELOAD, null, BaseTvActivity.this, MediaParsingService.class));
                 mStorageHandlerHandler.sendEmptyMessageDelayed(ACTION_MEDIA_UNMOUNTED, 2000); //Delay to cancel it in case of MOUNT
             }
         }



More information about the Android mailing list