mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-09 15:27:55 -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;
|
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) {
|
JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixmobile_JNIKiwix_loadZIM(JNIEnv *env, jobject obj, jstring path) {
|
||||||
jboolean retVal = JNI_TRUE;
|
jboolean retVal = JNI_TRUE;
|
||||||
std::string cPath = jni2c(path, env);
|
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 class JNIKiwix {
|
||||||
public native String getMainPage();
|
public native String getMainPage();
|
||||||
public native String getId();
|
public native String getId();
|
||||||
|
public native String getMimeType(String url);
|
||||||
public native boolean loadZIM(String path);
|
public native boolean loadZIM(String path);
|
||||||
public native byte[] getContent(String url, JNIKiwixString mimeType, JNIKiwixInt size);
|
public native byte[] getContent(String url, JNIKiwixString mimeType, JNIKiwixInt size);
|
||||||
public native boolean searchSuggestions(String prefix, int count);
|
public native boolean searchSuggestions(String prefix, int count);
|
||||||
|
@ -23,6 +23,7 @@ import android.content.ContentProvider;
|
|||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.webkit.MimeTypeMap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.ParcelFileDescriptor;
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.os.ParcelFileDescriptor.AutoCloseOutputStream;
|
import android.os.ParcelFileDescriptor.AutoCloseOutputStream;
|
||||||
@ -147,8 +148,35 @@ public class ZimContentProvider extends ContentProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getType(Uri uri) {
|
public String getType(Uri uri) {
|
||||||
Log.w(TAG_KIWIX, "ZimContentProvider.getType() (not implemented) called");
|
String mimeType;
|
||||||
return null;
|
|
||||||
|
// 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
|
@Override
|
||||||
@ -247,7 +275,7 @@ public class ZimContentProvider extends ContentProvider {
|
|||||||
out.flush();
|
out.flush();
|
||||||
|
|
||||||
Log.d(TAG_KIWIX, "reading " + articleZimUrl
|
Log.d(TAG_KIWIX, "reading " + articleZimUrl
|
||||||
+ "(mime " + mime.value + ", size: " + size.value + ") finished.");
|
+ "(mime: " + mime.value + ", size: " + size.value + ") finished.");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG_KIWIX, "Exception reading article " + articleZimUrl + " from zim file", e);
|
Log.e(TAG_KIWIX, "Exception reading article " + articleZimUrl + " from zim file", e);
|
||||||
} catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user