diff --git a/kiwix.c b/kiwix.c index b7b654a13..b84adc039 100644 --- a/kiwix.c +++ b/kiwix.c @@ -99,6 +99,25 @@ JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixmobile_JNIKiwix_getId(JNIEnv *env, return id; } +JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixmobile_JNIKiwix_getMimeType(JNIEnv *env, jobject obj, jstring url) { + jstring mimeType; + + pthread_mutex_lock(&readerLock); + if (reader != NULL) { + std::string cUrl = jni2c(url, env); + try { + std::string cMimeType; + reader->getMimeTypeByUrl(cUrl, cMimeType); + mimeType = c2jni(cMimeType, env); + } catch (exception &e) { + std::cerr << e.what() << std::endl; + } + } + pthread_mutex_unlock(&readerLock); + + return mimeType; +} + JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixmobile_JNIKiwix_loadZIM(JNIEnv *env, jobject obj, jstring path) { jboolean retVal = JNI_TRUE; std::string cPath = jni2c(path, env); diff --git a/libs/armeabi/libkiwix.so b/libs/armeabi/libkiwix.so index 8f39e7ae9..13d24d99f 100755 Binary files a/libs/armeabi/libkiwix.so and b/libs/armeabi/libkiwix.so differ diff --git a/libs/mips/libkiwix.so b/libs/mips/libkiwix.so index ba124e937..9fcaa0f05 100755 Binary files a/libs/mips/libkiwix.so and b/libs/mips/libkiwix.so differ diff --git a/libs/x86/libkiwix.so b/libs/x86/libkiwix.so index 0004e29fd..2f2d4f183 100755 Binary files a/libs/x86/libkiwix.so and b/libs/x86/libkiwix.so differ diff --git a/src/org/kiwix/kiwixmobile/JNIKiwix.java b/src/org/kiwix/kiwixmobile/JNIKiwix.java index 9e9d1d532..4bc45edd1 100644 --- a/src/org/kiwix/kiwixmobile/JNIKiwix.java +++ b/src/org/kiwix/kiwixmobile/JNIKiwix.java @@ -22,6 +22,7 @@ package org.kiwix.kiwixmobile; public class JNIKiwix { public native String getMainPage(); public native String getId(); + public native String getMimeType(String url); public native boolean loadZIM(String path); public native byte[] getContent(String url, JNIKiwixString mimeType, JNIKiwixInt size); public native boolean searchSuggestions(String prefix, int count); diff --git a/src/org/kiwix/kiwixmobile/ZimContentProvider.java b/src/org/kiwix/kiwixmobile/ZimContentProvider.java index dcbf2b129..cf587337a 100644 --- a/src/org/kiwix/kiwixmobile/ZimContentProvider.java +++ b/src/org/kiwix/kiwixmobile/ZimContentProvider.java @@ -23,6 +23,7 @@ import android.content.ContentProvider; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.webkit.MimeTypeMap; import android.net.Uri; import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor.AutoCloseOutputStream; @@ -147,8 +148,35 @@ public class ZimContentProvider extends ContentProvider { @Override public String getType(Uri uri) { - Log.w(TAG_KIWIX, "ZimContentProvider.getType() (not implemented) called"); - return null; + String mimeType; + + // This is the code which makes a guess based on the file extenstion + String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString().toLowerCase()); + mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); + + // This is the code which retrieve the mimeType from the libzim + // "slow" and still bugyy + if (mimeType.isEmpty() && jniKiwix != null && uri == null) { + String t = uri.toString(); + int pos = uri.toString().indexOf(CONTENT_URI.toString()); + if (pos != -1) { + t = uri.toString().substring( + CONTENT_URI.toString().length()); + } + // Remove fragment (#...) as not supported by zimlib + pos = t.indexOf("#"); + if (pos != -1) { + t = t.substring(0, pos); + } + + mimeType = jniKiwix.getMimeType(t); + + // Truncate mime-type (everything after the first space + mimeType = mimeType.replaceAll("^([^ ]+).*$", "$1"); + } + + Log.d(TAG_KIWIX, "Getting mime-type for " + uri.toString() + " = " + mimeType); + return mimeType; } @Override @@ -247,7 +275,7 @@ public class ZimContentProvider extends ContentProvider { out.flush(); Log.d(TAG_KIWIX, "reading " + articleZimUrl - + "(mime " + mime.value + ", size: " + size.value + ") finished."); + + "(mime: " + mime.value + ", size: " + size.value + ") finished."); } catch (IOException e) { Log.e(TAG_KIWIX, "Exception reading article " + articleZimUrl + " from zim file", e); } catch (NullPointerException e) {