Implement the ZimContentProvider getType()

This commit is contained in:
Kelson42 2014-12-08 01:37:25 +01:00
parent 271d3a31a2
commit 5789bae505
6 changed files with 51 additions and 3 deletions

19
kiwix.c
View File

@ -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);

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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);

View File

@ -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) {