[Android] Check for new storages when app goes foreground

Geoffrey Métais git at videolan.org
Thu Oct 5 15:05:13 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Oct  5 12:01:42 2017 +0200| [d299aaa0675cff8fde4bfb866f5aa9f7545e1327] | committer: Geoffrey Métais

Check for new storages when app goes foreground

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

 .../src/org/videolan/vlc/ExternalMonitor.java      | 48 +++++++++++++++++++++-
 .../vlc/gui/AudioPlayerContainerActivity.java      |  7 ++--
 .../vlc/gui/tv/browser/BaseTvActivity.java         |  5 ++-
 3 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
index 7c85353fc..5896e1ca0 100644
--- a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
+++ b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
@@ -28,6 +28,7 @@ import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.SharedPreferences;
 import android.net.ConnectivityManager;
 import android.net.Network;
 import android.net.NetworkCapabilities;
@@ -40,7 +41,12 @@ import android.support.v7.preference.PreferenceManager;
 import android.text.TextUtils;
 
 import org.videolan.libvlc.util.AndroidUtil;
+import org.videolan.medialibrary.Medialibrary;
 import org.videolan.vlc.gui.helpers.UiTools;
+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.lang.ref.WeakReference;
 import java.net.NetworkInterface;
@@ -49,6 +55,9 @@ import java.util.Enumeration;
 import java.util.LinkedList;
 import java.util.List;
 
+import static org.videolan.vlc.MediaParsingService.ACTION_NEW_STORAGE;
+import static org.videolan.vlc.MediaParsingService.EXTRA_PATH;
+
 public class ExternalMonitor extends BroadcastReceiver {
     public final static String TAG = "VLC/ExternalMonitor";
     private static volatile boolean connected = true;
@@ -71,6 +80,43 @@ public class ExternalMonitor extends BroadcastReceiver {
         storageFilter.addDataScheme("file");
         ctx.registerReceiver(instance, networkFilter);
         ctx.registerReceiver(instance, storageFilter);
+        if (AndroidUtil.isICSOrLater)
+            checkNewStorages(ctx);
+    }
+
+    private static void checkNewStorages(final Context ctx) {
+        final Medialibrary ml = VLCApplication.getMLInstance();
+        if (!ml.isInitiated() || ml.isWorking())
+            return;
+        VLCApplication.runBackground(new Runnable() {
+            @Override
+            public void run() {
+                final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
+                final List<String> devices = AndroidDevices.getExternalStorageDirectories();
+                if (Util.isListEmpty(devices))
+                    return;
+                final String[] knownDevices = ml.getDevices();
+                for (final String device : devices) {
+                    final String uuid = FileUtils.getFileNameFromPath(device);
+                    if (TextUtils.isEmpty(device) || TextUtils.isEmpty(uuid)
+                            || containsDevice(knownDevices, device))
+                        continue;
+                    final boolean isNew = ml.addDevice(uuid, device, true, true);
+                    final boolean isIgnored = sharedPreferences.getBoolean("ignore_"+ uuid, false);
+                    if (isNew && !isIgnored)
+                        LocalBroadcastManager.getInstance(ctx).sendBroadcast(new Intent(ACTION_NEW_STORAGE).putExtra(EXTRA_PATH, device));
+                }
+            }
+
+            private boolean containsDevice(String[] devices, String device) {
+                if (Util.isArrayEmpty(devices))
+                    return false;
+                for (String dev : devices)
+                    if (device.startsWith(Strings.removeFileProtocole(dev)))
+                        return true;
+                return false;
+            }
+        });
     }
 
     static void unregister(Context ctx) {
@@ -130,7 +176,7 @@ public class ExternalMonitor extends BroadcastReceiver {
                             notifyStorageChanges(path);
                         } else
                             appCtx.startService(new Intent(MediaParsingService.ACTION_RELOAD, null, appCtx, MediaParsingService.class)
-                                    .putExtra(MediaParsingService.EXTRA_PATH, path));
+                                    .putExtra(EXTRA_PATH, path));
                     }
                     break;
                 case ACTION_MEDIA_UNMOUNTED:
diff --git a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
index a8a34950f..9c71f95c5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
@@ -49,8 +49,8 @@ import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import org.videolan.vlc.BuildConfig;
-import org.videolan.vlc.MediaParsingService;
 import org.videolan.vlc.ExternalMonitor;
+import org.videolan.vlc.MediaParsingService;
 import org.videolan.vlc.PlaybackService;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
@@ -129,8 +129,6 @@ public class AudioPlayerContainerActivity extends BaseActivity implements Playba
 
     @Override
     protected void onStart() {
-        super.onStart();
-        mHelper.onStart();
         ExternalMonitor.subscribeStorageCb(this);
 
         /* Prepare the progressBar */
@@ -142,6 +140,9 @@ public class AudioPlayerContainerActivity extends BaseActivity implements Playba
         progressFilter.addAction(MediaParsingService.ACTION_PROGRESS);
         progressFilter.addAction(MediaParsingService.ACTION_NEW_STORAGE);
         LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, progressFilter);
+        // super.onStart must be called after receiver registration
+        super.onStart();
+        mHelper.onStart();
     }
 
     @Override
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 fdf3616ec..421b8e170 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
@@ -66,8 +66,6 @@ public abstract class BaseTvActivity extends PlaybackServiceActivity implements
 
     @Override
     protected void onStart() {
-        super.onStart();
-        mIsVisible = true;
         ExternalMonitor.subscribeStorageCb(this);
 
         final IntentFilter parsingServiceFilter = new IntentFilter(MediaParsingService.ACTION_SERVICE_ENDED);
@@ -78,6 +76,9 @@ public abstract class BaseTvActivity extends PlaybackServiceActivity implements
         mRegistering = true;
         LocalBroadcastManager.getInstance(this).registerReceiver(mParsingServiceReceiver, parsingServiceFilter);
         ExternalMonitor.subscribeNetworkCb(this);
+        // super.onStart must be called after receiver registration
+        super.onStart();
+        mIsVisible = true;
     }
 
     @Override



More information about the Android mailing list