Android: Make http methods of Activity class static

This commit is contained in:
UnknownShadow200 2021-09-29 11:58:50 +10:00
parent 6954840462
commit 4515316c90
4 changed files with 40 additions and 24 deletions

View file

@ -791,11 +791,11 @@ public class MainActivity extends Activity {
// ---------------- HTTP ----------------
// ======================================
// Implements java Android side of the Android HTTP backend (See Http.c)
HttpURLConnection conn;
InputStream src;
byte[] readCache = new byte[8192];
static HttpURLConnection conn;
static InputStream src;
static byte[] readCache = new byte[8192];
public int httpInit(String url, String method) {
public static int httpInit(String url, String method) {
try {
conn = (HttpURLConnection)new URL(url).openConnection();
conn.setDoInput(true);
@ -807,11 +807,11 @@ public class MainActivity extends Activity {
}
}
public void httpSetHeader(String name, String value) {
public static void httpSetHeader(String name, String value) {
conn.setRequestProperty(name, value);
}
public int httpSetData(byte[] data) {
public static int httpSetData(byte[] data) {
try {
conn.setDoOutput(true);
conn.getOutputStream().write(data);
@ -822,7 +822,7 @@ public class MainActivity extends Activity {
}
}
public int httpPerform() {
public static int httpPerform() {
int len;
try {
conn.connect();
@ -854,7 +854,7 @@ public class MainActivity extends Activity {
}
}
void httpFinish() {
static void httpFinish() {
conn = null;
try {
src.close();
@ -863,20 +863,20 @@ public class MainActivity extends Activity {
}
// TODO: Should we prune this list?
List<String> errors = new ArrayList<String>();
static List<String> errors = new ArrayList<String>();
int httpOnError(Exception ex) {
static int httpOnError(Exception ex) {
ex.printStackTrace();
httpFinish();
errors.add(ex.getMessage());
return -errors.size(); // don't want 0 as an error code
}
public String httpDescribeError(int res) {
public static String httpDescribeError(int res) {
res = -res - 1;
return res >= 0 && res < errors.size() ? errors.get(res) : null;
}
native void httpParseHeader(String header);
native void httpAppendData(byte[] data, int len);
native static void httpParseHeader(String header);
native static void httpAppendData(byte[] data, int len);
}

View file

@ -159,7 +159,8 @@ build_android() {
$TOOLS_ROOT/aapt add -f obj/cc-unsigned.apk classes.dex lib/armeabi/libclassicube.so lib/armeabi-v7a/libclassicube.so lib/arm64-v8a/libclassicube.so lib/x86/libclassicube.so lib/x86_64/libclassicube.so
# sign the apk with debug key (https://stackoverflow.com/questions/16711233/)
cp obj/cc-unsigned.apk obj/cc-signed.apk
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore debug.keystore -storepass android -keypass android obj/cc-signed.apk androiddebugkey
jarsigner -sigalg SHA1withRSA -digestalg SHA1 -keystore debug.keystore -storepass android -keypass android obj/cc-signed.apk androiddebugkey
# jarsigner -verbose
# create aligned .apk file
$TOOLS_ROOT/zipalign -f 4 obj/cc-signed.apk $ROOT_DIR/src/cc.apk
}

View file

@ -643,7 +643,7 @@ cc_bool Http_DescribeError(cc_result res, cc_string* dst) {
JavaGetCurrentEnv(env);
args[0].i = res;
obj = JavaICall_Obj(env, JAVA_httpDescribeError, args);
obj = JavaSCall_Obj(env, JAVA_httpDescribeError, args);
if (!obj) return false;
err = JavaGetString(env, obj, buffer);
@ -660,7 +660,7 @@ static void Http_AddHeader(struct HttpRequest* req, const char* key, const cc_st
args[0].l = JavaMakeConst(env, key);
args[1].l = JavaMakeString(env, value);
JavaICall_Void(env, JAVA_httpSetHeader, args);
JavaSCall_Void(env, JAVA_httpSetHeader, args);
(*env)->DeleteLocalRef(env, args[0].l);
(*env)->DeleteLocalRef(env, args[1].l);
}
@ -687,12 +687,12 @@ static const JNINativeMethod methods[] = {
{ "httpAppendData", "([BI)V", java_HttpAppendData }
};
static void CacheMethodRefs(JNIEnv* env) {
JAVA_httpInit = JavaGetIMethod(env, "httpInit", "(Ljava/lang/String;Ljava/lang/String;)I");
JAVA_httpSetHeader = JavaGetIMethod(env, "httpSetHeader", "(Ljava/lang/String;Ljava/lang/String;)V");
JAVA_httpPerform = JavaGetIMethod(env, "httpPerform", "()I");
JAVA_httpSetData = JavaGetIMethod(env, "httpSetData", "([B)I");
JAVA_httpInit = JavaGetSMethod(env, "httpInit", "(Ljava/lang/String;Ljava/lang/String;)I");
JAVA_httpSetHeader = JavaGetSMethod(env, "httpSetHeader", "(Ljava/lang/String;Ljava/lang/String;)V");
JAVA_httpPerform = JavaGetSMethod(env, "httpPerform", "()I");
JAVA_httpSetData = JavaGetSMethod(env, "httpSetData", "([B)I");
JAVA_httpDescribeError = JavaGetIMethod(env, "httpDescribeError", "(I)Ljava/lang/String;");
JAVA_httpDescribeError = JavaGetSMethod(env, "httpDescribeError", "(I)Ljava/lang/String;");
}
static void HttpBackend_Init(void) {
@ -710,7 +710,7 @@ static cc_result Http_InitReq(JNIEnv* env, struct HttpRequest* req, cc_string* u
args[0].l = JavaMakeString(env, url);
args[1].l = JavaMakeConst(env, verbs[req->requestType]);
res = JavaICall_Int(env, JAVA_httpInit, args);
res = JavaSCall_Int(env, JAVA_httpInit, args);
(*env)->DeleteLocalRef(env, args[0].l);
(*env)->DeleteLocalRef(env, args[1].l);
return res;
@ -721,7 +721,7 @@ static cc_result Http_SetData(JNIEnv* env, struct HttpRequest* req) {
jint res;
args[0].l = JavaMakeBytes(env, req->data, req->size);
res = JavaICall_Int(env, JAVA_httpSetData, args);
res = JavaSCall_Int(env, JAVA_httpSetData, args);
(*env)->DeleteLocalRef(env, args[0].l);
return res;
}
@ -741,7 +741,7 @@ static cc_result HttpBackend_Do(struct HttpRequest* req, cc_string* url) {
req->_capacity = 0;
http_curProgress = HTTP_PROGRESS_FETCHING_DATA;
res = JavaICall_Int(env, JAVA_httpPerform, NULL);
res = JavaSCall_Int(env, JAVA_httpPerform, NULL);
http_curProgress = 100;
return res;
}

View file

@ -253,6 +253,7 @@ extern JavaVM* VM_Ptr;
#define JavaRegisterNatives(env, methods) (*env)->RegisterNatives(env, App_Class, methods, Array_Elems(methods));
#define JavaGetIMethod(env, name, sig) (*env)->GetMethodID(env, App_Class, name, sig)
#define JavaGetSMethod(env, name, sig) (*env)->GetStaticMethodID(env, App_Class, name, sig)
/* Creates a string from the given java string. buffer must be at least NATIVE_STR_LEN long. */
/* NOTE: Don't forget to call env->ReleaseStringUTFChars. Only works with ASCII strings. */
@ -274,12 +275,26 @@ void JavaCall_Void_String(const char* name, cc_string* dst);
/* Calls a method in the activity class that takes a string and returns a string. */
void JavaCall_String_String(const char* name, const cc_string* arg, cc_string* dst);
/* Calls an instance method in the activity class that returns nothing */
#define JavaICall_Void(env, method, args) (*env)->CallVoidMethodA(env, App_Instance, method, args)
/* Calls an instance method in the activity class that returns a jint */
#define JavaICall_Int(env, method, args) (*env)->CallIntMethodA(env, App_Instance, method, args)
/* Calls an instance method in the activity class that returns a jlong */
#define JavaICall_Long(env, method, args) (*env)->CallLongMethodA(env, App_Instance, method, args)
/* Calls an instance method in the activity class that returns a jfloat */
#define JavaICall_Float(env,method, args) (*env)->CallFloatMethodA(env, App_Instance, method, args)
/* Calls an instance method in the activity class that returns a jobject */
#define JavaICall_Obj(env, method, args) (*env)->CallObjectMethodA(env,App_Instance, method, args)
/* Calls a static method in the activity class that returns nothing */
#define JavaSCall_Void(env, method, args) (*env)->CallStaticVoidMethodA(env, App_Class, method, args)
/* Calls a static method in the activity class that returns a jint */
#define JavaSCall_Int(env, method, args) (*env)->CallStaticIntMethodA(env, App_Class, method, args)
/* Calls a static method in the activity class that returns a jlong */
#define JavaSCall_Long(env, method, args) (*env)->CallStaticLongMethodA(env, App_Class, method, args)
/* Calls a static method in the activity class that returns a jfloat */
#define JavaSCall_Float(env,method, args) (*env)->CallStaticFloatMethodA(env, App_Class, method, args)
/* Calls a static method in the activity class that returns a jobject */
#define JavaSCall_Obj(env, method, args) (*env)->CallStaticObjectMethodA(env,App_Class, method, args)
#endif
#endif