i2pd_android.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <jni.h>
  2. #include "org_purplei2p_i2pd_I2PD_JNI.h"
  3. #include "DaemonAndroid.h"
  4. #include "RouterContext.h"
  5. #include "Transports.h"
  6. JNIEXPORT jstring JNICALL Java_org_purplei2p_i2pd_I2PD_1JNI_getABICompiledWith
  7. (JNIEnv *env, jclass clazz) {
  8. #if defined(__arm__)
  9. #if defined(__ARM_ARCH_7A__)
  10. #if defined(__ARM_NEON__)
  11. #if defined(__ARM_PCS_VFP)
  12. #define ABI "armeabi-v7a/NEON (hard-float)"
  13. #else
  14. #define ABI "armeabi-v7a/NEON"
  15. #endif
  16. #else
  17. #if defined(__ARM_PCS_VFP)
  18. #define ABI "armeabi-v7a (hard-float)"
  19. #else
  20. #define ABI "armeabi-v7a"
  21. #endif
  22. #endif
  23. #else
  24. #define ABI "armeabi"
  25. #endif
  26. #elif defined(__i386__)
  27. #define ABI "x86"
  28. #elif defined(__x86_64__)
  29. #define ABI "x86_64"
  30. #elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */
  31. #define ABI "mips64"
  32. #elif defined(__mips__)
  33. #define ABI "mips"
  34. #elif defined(__aarch64__)
  35. #define ABI "arm64-v8a"
  36. #else
  37. #define ABI "unknown"
  38. #endif
  39. return env->NewStringUTF(ABI);
  40. }
  41. JNIEXPORT jstring JNICALL Java_org_purplei2p_i2pd_I2PD_1JNI_startDaemon
  42. (JNIEnv *env, jclass clazz) {
  43. return env->NewStringUTF(i2p::android::start().c_str());
  44. }
  45. JNIEXPORT void JNICALL Java_org_purplei2p_i2pd_I2PD_1JNI_stopDaemon
  46. (JNIEnv *env, jclass clazz) {
  47. i2p::android::stop();
  48. }
  49. JNIEXPORT void JNICALL Java_org_purplei2p_i2pd_I2PD_1JNI_stopAcceptingTunnels
  50. (JNIEnv *env, jclass clazz) {
  51. i2p::context.SetAcceptsTunnels (false);
  52. }
  53. JNIEXPORT void JNICALL Java_org_purplei2p_i2pd_I2PD_1JNI_startAcceptingTunnels
  54. (JNIEnv *env, jclass clazz) {
  55. i2p::context.SetAcceptsTunnels (true);
  56. }
  57. JNIEXPORT void JNICALL Java_org_purplei2p_i2pd_I2PD_1JNI_onNetworkStateChanged
  58. (JNIEnv *env, jclass clazz, jboolean isConnected) {
  59. bool isConnectedBool = (bool) isConnected;
  60. i2p::transport::transports.SetOnline (isConnectedBool);
  61. }
  62. JNIEXPORT void JNICALL Java_org_purplei2p_i2pd_I2PD_1JNI_setDataDir
  63. (JNIEnv *env, jclass clazz, jstring jdataDir) {
  64. /*
  65. // Method 1: convert UTF-16 jstring to std::string (https://stackoverflow.com/a/41820336)
  66. const jclass stringClass = env->GetObjectClass(jdataDir);
  67. const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
  68. const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jdataDir, getBytes, env->NewStringUTF("UTF-8"));
  69. size_t length = (size_t) env->GetArrayLength(stringJbytes);
  70. jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);
  71. std::string dataDir = std::string((char *)pBytes, length);
  72. env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
  73. env->DeleteLocalRef(stringJbytes);
  74. env->DeleteLocalRef(stringClass); */
  75. // Method 2: get string chars and make char array.
  76. auto dataDir = env->GetStringUTFChars(jdataDir, NULL);
  77. env->ReleaseStringUTFChars(jdataDir, dataDir);
  78. // Set DataDir
  79. i2p::android::SetDataDir(dataDir);
  80. }