[Android] Update Medialibrary JNI to match new API
Geoffrey Métais
git at videolan.org
Mon Nov 13 18:18:44 CET 2017
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Nov 13 17:26:42 2017 +0100| [134aa98a2850699e995323f11d28c3b13063709b] | committer: Geoffrey Métais
Update Medialibrary JNI to match new API
> https://code.videolan.org/videolan/vlc-android/commit/134aa98a2850699e995323f11d28c3b13063709b
---
medialibrary/jni/AndroidMediaLibrary.cpp | 2 +-
medialibrary/jni/AndroidMediaLibrary.h | 2 +-
medialibrary/jni/medialibrary.cpp | 9 +++++----
.../src/org/videolan/medialibrary/Medialibrary.java | 18 ++++++++++++------
.../src/org/videolan/vlc/MediaParsingService.java | 14 +++++++++-----
5 files changed, 28 insertions(+), 17 deletions(-)
diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index 8ab5a4158..22d8c0185 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -55,7 +55,7 @@ AndroidMediaLibrary::~AndroidMediaLibrary()
delete p_ml;
}
-bool
+medialibrary::InitializeResult
AndroidMediaLibrary::initML(const std::string& dbPath, const std::string& thumbsPath)
{
p_DeviceListerCb = p_ml->setDeviceLister(p_lister);
diff --git a/medialibrary/jni/AndroidMediaLibrary.h b/medialibrary/jni/AndroidMediaLibrary.h
index 96b1e0e54..d21e3681a 100644
--- a/medialibrary/jni/AndroidMediaLibrary.h
+++ b/medialibrary/jni/AndroidMediaLibrary.h
@@ -28,7 +28,7 @@ public:
AndroidMediaLibrary(JavaVM *vm, fields *ref_fields, jobject thiz);
~AndroidMediaLibrary();
- bool initML(const std::string& dbPath, const std::string& thumbsPath);
+ medialibrary::InitializeResult initML(const std::string& dbPath, const std::string& thumbsPath);
void start();
bool addDevice(const std::string& uuid, const std::string& path, bool removable);
std::vector<std::tuple<std::string, std::string, bool>> devices();
diff --git a/medialibrary/jni/medialibrary.cpp b/medialibrary/jni/medialibrary.cpp
index 7556b46e3..fef4d8d1c 100644
--- a/medialibrary/jni/medialibrary.cpp
+++ b/medialibrary/jni/medialibrary.cpp
@@ -22,17 +22,18 @@ AndroidMediaLibrary *MediaLibrary_getInstance(JNIEnv *env, jobject thiz);
static void
MediaLibrary_setInstance(JNIEnv *env, jobject thiz, AndroidMediaLibrary *p_obj);
-jboolean
+jint
init(JNIEnv* env, jobject thiz, jstring dbPath, jstring thumbsPath)
{
AndroidMediaLibrary *aml = new AndroidMediaLibrary(myVm, &ml_fields, thiz);
MediaLibrary_setInstance(env, thiz, aml);
const char *db_utfchars = env->GetStringUTFChars(dbPath, JNI_FALSE);
const char *thumbs_utfchars = env->GetStringUTFChars(thumbsPath, JNI_FALSE);
- m_IsInitialized = aml->initML(db_utfchars, thumbs_utfchars);
+ medialibrary::InitializeResult initCode = aml->initML(db_utfchars, thumbs_utfchars);
+ m_IsInitialized = initCode != medialibrary::InitializeResult::Failed;
env->ReleaseStringUTFChars(dbPath, db_utfchars);
env->ReleaseStringUTFChars(thumbsPath, thumbs_utfchars);
- return m_IsInitialized;
+ return (int) initCode;
}
void
@@ -756,7 +757,7 @@ playlistDelete(JNIEnv* env, jobject thiz, jobject medialibrary, jlong playlistId
* JNI stuff
*/
static JNINativeMethod methods[] = {
- {"nativeInit", "(Ljava/lang/String;Ljava/lang/String;)Z", (void*)init },
+ {"nativeInit", "(Ljava/lang/String;Ljava/lang/String;)I", (void*)init },
{"nativeStart", "()V", (void*)start },
{"nativeRelease", "()V", (void*)release },
{"nativeAddDevice", "(Ljava/lang/String;Ljava/lang/String;Z)Z", (void*)addDevice },
diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index 56d5d7d7e..9599f5be3 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -46,6 +46,11 @@ public class Medialibrary {
public static final int FLAG_MEDIA_ADDED_AUDIO_EMPTY = 1 << 4;
public static final int FLAG_MEDIA_ADDED_VIDEO = 1 << 5;
+ public static final int ML_INIT_SUCCESS = 0;
+ public static final int ML_INIT_ALREADY_INITIALIZED = 1;
+ public static final int ML_INIT_FAILED = 2;
+ public static final int ML_INIT_DB_RESET = 3;
+
public static final String ACTION_IDLE = "action_idle";
public static final String STATE_IDLE = "state_idle";
@@ -74,17 +79,18 @@ public class Medialibrary {
return sContext;
}
- public boolean init(Context context) {
+ public int init(Context context) {
if (context == null)
- return false;
+ return ML_INIT_FAILED;
sContext = context;
File extFilesDir = context.getExternalFilesDir(null);
File dbDirectory = context.getDir("db", Context.MODE_PRIVATE);
if (extFilesDir == null || !extFilesDir.exists()
|| dbDirectory == null || !dbDirectory.canWrite())
- return false;
- mIsInitiated = nativeInit(dbDirectory+ VLC_MEDIA_DB_NAME, extFilesDir+ THUMBS_FOLDER_NAME);
- return mIsInitiated;
+ return ML_INIT_FAILED;
+ int initCode = nativeInit(dbDirectory+ VLC_MEDIA_DB_NAME, extFilesDir+ THUMBS_FOLDER_NAME);
+ mIsInitiated = initCode != ML_INIT_FAILED;
+ return initCode;
}
public void start() {
@@ -585,7 +591,7 @@ public class Medialibrary {
// Native methods
- private native boolean nativeInit(String dbPath, String thumbsPath);
+ private native int nativeInit(String dbPath, String thumbsPath);
private native void nativeStart();
private native void nativeRelease();
private native void nativeBanFolder(String path);
diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.java b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
index aa1ee2797..e0dfb8013 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.java
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
@@ -215,24 +215,27 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
if (mMedialibrary.isInitiated()) {
mMedialibrary.resumeBackgroundOperations();
exitCommand();
- } else
+ } else {
+ final Context context = getApplicationContext();
mCallsExecutor.execute(new Runnable() {
Handler handler = null;
@Override
public void run() {
- boolean shouldInit = !(new File(MediaParsingService.this.getDir("db", Context.MODE_PRIVATE)+Medialibrary.VLC_MEDIA_DB_NAME).exists());
- if (mMedialibrary.init(getApplicationContext())) {
+ boolean shouldInit = !(new File(MediaParsingService.this.getDir("db", Context.MODE_PRIVATE) + Medialibrary.VLC_MEDIA_DB_NAME).exists());
+ int initCode = mMedialibrary.init(context);
+ shouldInit |= initCode == Medialibrary.ML_INIT_DB_RESET;
+ if (initCode != Medialibrary.ML_INIT_FAILED) {
final List<String> devices = new ArrayList<>();
devices.add(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY);
devices.addAll(AndroidDevices.getExternalStorageDirectories());
- final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MediaParsingService.this);
+ final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
for (final String device : devices) {
final boolean isMainStorage = TextUtils.equals(device, AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY);
final String uuid = FileUtils.getFileNameFromPath(device);
if (TextUtils.isEmpty(device) || TextUtils.isEmpty(uuid))
continue;
final boolean isNew = mMedialibrary.addDevice(isMainStorage ? "main-storage" : uuid, device, !isMainStorage);
- final boolean isIgnored = sharedPreferences.getBoolean("ignore_"+ uuid, false);
+ final boolean isIgnored = sharedPreferences.getBoolean("ignore_" + uuid, false);
if (!isMainStorage && isNew && !isIgnored)
showStorageNotification(device);
}
@@ -265,6 +268,7 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
}, 2000);
}
});
+ }
}
private boolean wasWorking;
More information about the Android
mailing list