mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 18:56:44 -04:00
Implement the ZimContentProvider getType()
This commit is contained in:
parent
271d3a31a2
commit
5789bae505
19
kiwix.c
19
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);
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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);
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user