mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-05-17 15:32:48 +00:00
dont try loading user-supplied library if it doesn't exist
This commit is contained in:
parent
2dc9b12b13
commit
fce6805018
1 changed files with 29 additions and 23 deletions
|
@ -9,44 +9,50 @@ package org.dslul.openboard.inputmethod.latin.utils;
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.dslul.openboard.inputmethod.latin.BuildConfig;
|
||||||
import org.dslul.openboard.inputmethod.latin.define.JniLibName;
|
import org.dslul.openboard.inputmethod.latin.define.JniLibName;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public final class JniUtils {
|
public final class JniUtils {
|
||||||
private static final String TAG = JniUtils.class.getSimpleName();
|
private static final String TAG = JniUtils.class.getSimpleName();
|
||||||
|
|
||||||
// try loading keyboard libraries
|
|
||||||
// first try user-provided library
|
|
||||||
// then try google library for gesture typing (needs library in system, and app as system app)
|
|
||||||
// finally fall back to internal library
|
|
||||||
public static boolean sHaveGestureLib = false;
|
public static boolean sHaveGestureLib = false;
|
||||||
static {
|
static {
|
||||||
|
String filesDir;
|
||||||
try {
|
try {
|
||||||
// first try loading imported library, and fall back to default
|
// try using reflection to get (app)context: https://stackoverflow.com/a/38967293
|
||||||
String filesDir;
|
final Application app = (Application) Class.forName("android.app.ActivityThread")
|
||||||
|
.getMethod("currentApplication").invoke(null, (Object[]) null);
|
||||||
|
filesDir = app.getFilesDir().getAbsolutePath();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// fall back to hardcoded default path, may not work on all phones
|
||||||
|
filesDir = "/data/data/" + BuildConfig.APPLICATION_ID + "/files";
|
||||||
|
}
|
||||||
|
final File userSuppliedLibrary = new File(filesDir + File.separator + JniLibName.JNI_LIB_IMPORT_FILE_NAME);
|
||||||
|
if (userSuppliedLibrary.exists()) {
|
||||||
try {
|
try {
|
||||||
// try using reflection to get (app)context: https://stackoverflow.com/a/38967293
|
System.load(filesDir + File.separator + JniLibName.JNI_LIB_IMPORT_FILE_NAME);
|
||||||
final Application app = (Application) Class.forName("android.app.ActivityThread")
|
sHaveGestureLib = true; // this is an assumption, any way to actually check?
|
||||||
.getMethod("currentApplication").invoke(null, (Object[]) null);
|
} catch (Throwable t) { // catch everything, maybe provided library simply doesn't work
|
||||||
filesDir = app.getFilesDir().getAbsolutePath();
|
Log.w(TAG, "Could not load user-supplied library", t);
|
||||||
} catch (Exception e) {
|
|
||||||
// fall back to hardcoded default path, may not work on all phones
|
|
||||||
filesDir = "/data/data/org.dslul.openboard.inputmethod.latin/files";
|
|
||||||
}
|
}
|
||||||
System.load(filesDir + File.separator + JniLibName.JNI_LIB_IMPORT_FILE_NAME);
|
}
|
||||||
sHaveGestureLib = true; // this is an assumption, any way to actually check?
|
|
||||||
} catch (Throwable t) { // catch everything, maybe provided library simply doesn't work
|
if (!sHaveGestureLib) {
|
||||||
Log.e(TAG, "Could not load native library " + JniLibName.JNI_LIB_IMPORT_FILE_NAME, t);
|
// try loading google library, will fail unless it's in system and this is a system app
|
||||||
try {
|
try {
|
||||||
System.loadLibrary(JniLibName.JNI_LIB_NAME_GOOGLE);
|
System.loadLibrary(JniLibName.JNI_LIB_NAME_GOOGLE);
|
||||||
sHaveGestureLib = true;
|
sHaveGestureLib = true;
|
||||||
} catch (UnsatisfiedLinkError ul) {
|
} catch (UnsatisfiedLinkError ul) {
|
||||||
Log.e(TAG, "Could not load native library " + JniLibName.JNI_LIB_NAME_GOOGLE, ul);
|
Log.w(TAG, "Could not load system glide typing library " + JniLibName.JNI_LIB_NAME_GOOGLE, ul);
|
||||||
try {
|
}
|
||||||
System.loadLibrary(JniLibName.JNI_LIB_NAME);
|
}
|
||||||
} catch (UnsatisfiedLinkError ule) {
|
if (!sHaveGestureLib) {
|
||||||
Log.e(TAG, "Could not load native library " + JniLibName.JNI_LIB_NAME, ule);
|
// try loading built-in library
|
||||||
}
|
try {
|
||||||
|
System.loadLibrary(JniLibName.JNI_LIB_NAME);
|
||||||
|
} catch (UnsatisfiedLinkError ul) {
|
||||||
|
Log.w(TAG, "Could not load native library " + JniLibName.JNI_LIB_NAME, ul);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue