[Android] Run storages checks from ML service
Geoffrey Métais
git at videolan.org
Thu Dec 7 16:55:37 CET 2017
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Dec 7 16:41:19 2017 +0100| [b464bb6441ecac2df2f2d423fdd719a46b81ccbe] | committer: Geoffrey Métais
Run storages checks from ML service
This is for preventing race conditions and keep ML service as a
controller of medialibrary
> https://code.videolan.org/videolan/vlc-android/commit/b464bb6441ecac2df2f2d423fdd719a46b81ccbe
---
.../src/org/videolan/vlc/ExternalMonitor.java | 43 ++--------------------
.../src/org/videolan/vlc/MediaParsingService.java | 42 ++++++++++++++++++++-
2 files changed, 43 insertions(+), 42 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
index 04f65a3b9..bb4187807 100644
--- a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
+++ b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
@@ -28,7 +28,6 @@ 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;
@@ -43,8 +42,6 @@ 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;
@@ -82,42 +79,8 @@ public class ExternalMonitor extends BroadcastReceiver {
}
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();
- devices.remove(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY);
- final String[] knownDevices = ml.getDevices();
- final List<String> missingDevices = Util.arrayToArrayList(knownDevices);
- missingDevices.remove("file://"+AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY);
- boolean refresh = false;
- for (final String device : devices) {
- final String uuid = FileUtils.getFileNameFromPath(device);
- if (TextUtils.isEmpty(device) || TextUtils.isEmpty(uuid))
- continue;
- if (containsDevice(knownDevices, device)) {
- missingDevices.remove("file://"+device);
- continue;
- }
- final boolean isNew = ml.addDevice(uuid, device, true);
- final boolean isIgnored = sharedPreferences.getBoolean("ignore_"+ uuid, false);
- if (!isIgnored) {
- if (isNew)
- LocalBroadcastManager.getInstance(ctx).sendBroadcast(new Intent(MediaParsingService.ACTION_NEW_STORAGE).putExtra(MediaParsingService.EXTRA_PATH, device));
- else
- refresh = true;
- }
- }
- for (String device : missingDevices)
- ml.removeDevice(FileUtils.getFileNameFromPath(device));
- if (refresh || !missingDevices.isEmpty())
- LocalBroadcastManager.getInstance(ctx).sendBroadcast(new Intent(MediaParsingService.ACTION_SERVICE_ENDED));
- }
- });
+ if (VLCApplication.getMLInstance().isInitiated())
+ ctx.startService(new Intent(MediaParsingService.ACTION_CHECK_STORAGES, null,ctx, MediaParsingService.class));
}
static void unregister(Context ctx) {
@@ -262,7 +225,7 @@ public class ExternalMonitor extends BroadcastReceiver {
}
}
- private static boolean containsDevice(String[] devices, String device) {
+ public static boolean containsDevice(String[] devices, String device) {
if (Util.isArrayEmpty(devices))
return false;
for (String dev : devices)
diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.java b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
index 8dd8ef22a..2db5947d2 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.java
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
@@ -27,6 +27,7 @@ import org.videolan.vlc.gui.helpers.NotificationHelper;
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.ArrayList;
@@ -42,6 +43,7 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
public final static String ACTION_RELOAD = "medialibrary_reload";
public final static String ACTION_DISCOVER = "medialibrary_discover";
public final static String ACTION_DISCOVER_DEVICE = "medialibrary_discover_device";
+ public final static String ACTION_CHECK_STORAGES = "medialibrary_check_storages";
public final static String EXTRA_PATH = "extra_path";
public final static String EXTRA_UUID = "extra_uuid";
@@ -87,7 +89,7 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
case Medialibrary.ACTION_IDLE:
if (intent.getBooleanExtra(Medialibrary.STATE_IDLE, true)) {
if (!mScanPaused) {
- stopSelf();
+ exitCommand();
return;
}
}
@@ -145,6 +147,9 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
case ACTION_DISCOVER_DEVICE:
discoverStorage(intent.getStringExtra(EXTRA_PATH));
break;
+ case ACTION_CHECK_STORAGES:
+ updateStorages();
+ break;
default:
exitCommand();
return START_NOT_STICKY;
@@ -275,6 +280,39 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
}
}
+ private volatile boolean serviceLock = false;
+ private void updateStorages() {
+ mCallsExecutor.execute(new Runnable() {
+ @Override
+ public void run() {
+ serviceLock = true;
+ final Context ctx = VLCApplication.getAppContext();
+ final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
+ final List<String> devices = AndroidDevices.getExternalStorageDirectories();
+ devices.remove(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY);
+ final String[] knownDevices = mMedialibrary.getDevices();
+ final List<String> missingDevices = Util.arrayToArrayList(knownDevices);
+ missingDevices.remove("file://"+AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY);
+ for (final String device : devices) {
+ final String uuid = FileUtils.getFileNameFromPath(device);
+ if (TextUtils.isEmpty(device) || TextUtils.isEmpty(uuid))
+ continue;
+ if (ExternalMonitor.containsDevice(knownDevices, device)) {
+ missingDevices.remove("file://"+device);
+ continue;
+ }
+ final boolean isNew = mMedialibrary.addDevice(uuid, device, true);
+ final boolean isIgnored = sharedPreferences.getBoolean("ignore_"+ uuid, false);
+ if (!isIgnored && isNew)
+ LocalBroadcastManager.getInstance(ctx).sendBroadcast(new Intent(ACTION_NEW_STORAGE).putExtra(EXTRA_PATH, device));
+ }
+ for (String device : missingDevices)
+ mMedialibrary.removeDevice(FileUtils.getFileNameFromPath(device));
+ serviceLock = false;
+ exitCommand();
+ }
+ });
+ }
private boolean wasWorking;
final StringBuilder sb = new StringBuilder();
private final Intent progessIntent = new Intent(ACTION_PROGRESS);
@@ -362,7 +400,7 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
}
private void exitCommand() {
- if (!mMedialibrary.isWorking())
+ if (!mMedialibrary.isWorking() && !serviceLock)
stopSelf();
}
More information about the Android
mailing list