fixed android example project

This commit is contained in:
Joseph Henry
2016-06-24 12:10:47 -07:00
parent b545aed175
commit b9a2427825
6 changed files with 29 additions and 6 deletions

View File

@@ -2,12 +2,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.joseph.example_app"> package="com.example.joseph.example_app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".MainActivity"> <activity android:name=".MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View File

@@ -1,4 +1,4 @@
package SDK; package ZeroTier;
public class SDK { public class SDK {
public native void startOneService(); public native void startOneService();
static { System.loadLibrary("ZeroTierOneJNI"); } // Loads JNI code static { System.loadLibrary("ZeroTierOneJNI"); } // Loads JNI code

View File

@@ -3,7 +3,7 @@ package com.example.joseph.example_app;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import SDK.SDK; import ZeroTier.SDK;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {

View File

@@ -129,20 +129,26 @@ extern "C" {
* Starts a new service instance * Starts a new service instance
*/ */
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT void JNICALL Java_ZeroTierSDK_startOneService(JNIEnv *env, jobject thisObj) // JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
//JNIEXPORT void JNICALL Java_Netcon_NetconWrapper_startOneService(JNIEnv *env, jobject thisObj) /* If you define anything else in this file it *must* follow that convention
and any corresponding Java package/classes in your Android project must match this as well */
JNIEXPORT void JNICALL Java_ZeroTier_SDK_startOneService(JNIEnv *env, jobject thisObj)
{ {
#else #else
void *startOneService(void *thread_id) void *startOneService(void *thread_id)
{ {
set_intercept_status(INTERCEPT_DISABLED); set_intercept_status(INTERCEPT_DISABLED);
#endif #endif
#if defined(__UNITY_3D__)
int MAX_DIR_SZ = 256; int MAX_DIR_SZ = 256;
char current_dir[MAX_DIR_SZ]; char current_dir[MAX_DIR_SZ];
getcwd(current_dir, MAX_DIR_SZ); getcwd(current_dir, MAX_DIR_SZ);
chdir(service_path.c_str()); chdir(service_path.c_str());
zt1Service = (ZeroTier::OneService *)0; zt1Service = (ZeroTier::OneService *)0;
#endif
#if defined(__ANDROID__) #if defined(__ANDROID__)
homeDir = "/sdcard/zerotier"; homeDir = "/sdcard/zerotier";
#endif #endif
@@ -171,6 +177,8 @@ extern "C" {
return NULL; return NULL;
#endif #endif
} else { } else {
LOGV("constructing path...\n");
std::vector<std::string> hpsp(ZeroTier::Utils::split(homeDir.c_str(),ZT_PATH_SEPARATOR_S,"","")); std::vector<std::string> hpsp(ZeroTier::Utils::split(homeDir.c_str(),ZT_PATH_SEPARATOR_S,"",""));
std::string ptmp; std::string ptmp;
if (homeDir[0] == ZT_PATH_SEPARATOR) if (homeDir[0] == ZT_PATH_SEPARATOR)
@@ -189,7 +197,7 @@ extern "C" {
} }
} }
chdir(current_dir); // Return to previous current working directory (at the request of Unity3D) //chdir(current_dir); // Return to previous current working directory (at the request of Unity3D)
LOGV("homeDir = %s", homeDir.c_str()); LOGV("homeDir = %s", homeDir.c_str());
//Debug(homeDir.c_str()); //Debug(homeDir.c_str());
@@ -199,8 +207,13 @@ extern "C" {
ZeroTier::Utils::getSecureRandom(&randp,sizeof(randp)); ZeroTier::Utils::getSecureRandom(&randp,sizeof(randp));
int servicePort = 9000 + (randp % 1000); int servicePort = 9000 + (randp % 1000);
LOGV("generated port\n");
for(;;) { for(;;) {
zt1Service = ZeroTier::OneService::newInstance(homeDir.c_str(),servicePort); zt1Service = ZeroTier::OneService::newInstance(homeDir.c_str(),servicePort);
LOGV("created new instance\n");
switch(zt1Service->run()) { switch(zt1Service->run()) {
case ZeroTier::OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done case ZeroTier::OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done
case ZeroTier::OneService::ONE_NORMAL_TERMINATION: case ZeroTier::OneService::ONE_NORMAL_TERMINATION:

View File

@@ -40,6 +40,9 @@ extern "C" {
#define INTERCEPT_DISABLED 222 #define INTERCEPT_DISABLED 222
#if defined(__ANDROID__) #if defined(__ANDROID__)
// JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
/* If you define anything else in this file it *must* follow that convention
and any corresponding Java package/classes in your Android project must match this as well */
JNIEXPORT void JNICALL Java_ZeroTier_SDK_startOneService(JNIEnv *env, jobject thisObj); JNIEXPORT void JNICALL Java_ZeroTier_SDK_startOneService(JNIEnv *env, jobject thisObj);
#else #else
void *startOneService(void *thread_id); void *startOneService(void *thread_id);