forked from cory/tildefriends
Steps toward following all the inconvenient, changing android rules:
* Set android:debuggable=false. * Call native code through JNI only. Having a native executable on disk and exec-ing it no longer seems possible. * Do all the Tilde Friends things in one process, without a proper sandbox, until I can wire up a restricted service worker process. * Jam Android App Bundle (.aab) building into the makefile. * Yuck.
This commit is contained in:
81
src/main.c
81
src/main.c
@ -34,6 +34,10 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
#include "jni.h"
|
||||
#endif
|
||||
|
||||
#if !defined(_countof)
|
||||
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
|
||||
#endif
|
||||
@ -726,6 +730,83 @@ static void _startup(int argc, char* argv[])
|
||||
}
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
static jint _tf_server_main(JNIEnv* env, jobject this_object, jstring files_dir, jstring apk_path, jstring out_port_file_path)
|
||||
{
|
||||
tf_printf("This is tf_server_main main.\n");
|
||||
_startup(0, (char*[]) { NULL });
|
||||
tf_printf("That was startup.\n");
|
||||
|
||||
const char* files = (*env)->GetStringUTFChars(env, files_dir, NULL);
|
||||
const char* apk = (*env)->GetStringUTFChars(env, apk_path, NULL);
|
||||
const char* out_port_file = (*env)->GetStringUTFChars(env, out_port_file_path, NULL);
|
||||
|
||||
tf_printf("FILES = %s\n", files);
|
||||
tf_printf("APK = %s\n", apk);
|
||||
tf_printf("OUT_PORT = %s\n", out_port_file);
|
||||
|
||||
int result = uv_chdir(files);
|
||||
if (result)
|
||||
{
|
||||
tf_printf("uv_chdir: %s\n", uv_strerror(result));
|
||||
}
|
||||
|
||||
size_t port_file_arg_length = strlen(out_port_file) + strlen("out_http_port_file=") + 1;
|
||||
char* port_file_arg = alloca(port_file_arg_length);
|
||||
snprintf(port_file_arg, port_file_arg_length, "out_http_port_file=%s", out_port_file);
|
||||
|
||||
const char* args[] =
|
||||
{
|
||||
"-z",
|
||||
apk,
|
||||
"-a",
|
||||
port_file_arg,
|
||||
"-p",
|
||||
"0",
|
||||
"-o", /* HACK! FIXME! */
|
||||
};
|
||||
|
||||
result = _tf_command_run(apk, _countof(args), (char**)args);
|
||||
|
||||
(*env)->ReleaseStringUTFChars(env, files_dir, files);
|
||||
(*env)->ReleaseStringUTFChars(env, apk_path, apk);
|
||||
(*env)->ReleaseStringUTFChars(env, out_port_file_path, out_port_file);
|
||||
|
||||
tf_printf("tf_server_main finished with %d.", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
|
||||
{
|
||||
tf_printf("JNI_Onload called.\n");
|
||||
JNIEnv* env;
|
||||
if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
|
||||
{
|
||||
tf_printf("Failed to get JNI environment.\n");
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
tf_printf("Finding class.\n");
|
||||
jclass c = (*env)->FindClass(env, "com/unprompted/tildefriends/TildeFriendsActivity");
|
||||
if (!c)
|
||||
{
|
||||
tf_printf("Failed to find TildeFriendsActivity class.\n");
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
tf_printf("Registering method.\n");
|
||||
static const JNINativeMethod methods[] = {
|
||||
{ "tf_server_main", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", _tf_server_main },
|
||||
};
|
||||
int result = (*env)->RegisterNatives(env, c, methods, (int)_countof(methods));
|
||||
if (result != JNI_OK)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
tf_printf("Done.\n");
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
_startup(argc, argv);
|
||||
|
Reference in New Issue
Block a user