Skip to content

Commit 4a4fc7f

Browse files
committed
ANDROID: added android.request
1 parent db7e190 commit 4a4fc7f

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

src/platform/android/app/proguard-rules.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44

55
-keep public class net.sourceforge.smallbasic.** { public *; }
6-
-keep public class ioio.lib.** { *; }
6+
-keep public class ioio.** { *; }
77
-keepclasseswithmembernames class * { native <methods>; }
88
-printmapping build/outputs/mapping/release/mapping.txt
99
-keepattributes LineNumberTable,SourceFile

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@
5858
import java.io.InputStreamReader;
5959
import java.io.OutputStream;
6060
import java.io.UnsupportedEncodingException;
61+
import java.net.HttpURLConnection;
6162
import java.net.Inet4Address;
6263
import java.net.InetAddress;
6364
import java.net.NetworkInterface;
6465
import java.net.SocketException;
66+
import java.net.URL;
6567
import java.net.URLDecoder;
68+
import java.nio.charset.StandardCharsets;
6669
import java.util.ArrayList;
6770
import java.util.Collection;
6871
import java.util.Date;
@@ -115,7 +118,6 @@ public class MainActivity extends NativeActivity {
115118
System.loadLibrary("smallbasic");
116119
}
117120

118-
public native long getActivity();
119121
public static native boolean libraryMode();
120122
public static native void onActivityPaused(boolean paused);
121123
public static native void onResize(int width, int height);
@@ -233,6 +235,8 @@ public Class<?> findClass(String className) {
233235
}
234236
}
235237

238+
public native long getActivity();
239+
236240
public byte[] getClipboardText() {
237241
final StringBuilder text = new StringBuilder();
238242
final Context context = this;
@@ -372,9 +376,10 @@ public boolean loadModules() {
372376
Log.i(TAG, "loadModules: " + getActivity());
373377
boolean result;
374378
try {
379+
// this would ideally be done with some kind of dependency injection
375380
System.loadLibrary("ioio");
376-
Class<?> clazz = Class.forName("ioio.smallbasic.android.IOIOLoader");
377-
clazz.getDeclaredConstructor(Long.class, Context.class).newInstance(getActivity(), this);
381+
Class.forName("ioio.smallbasic.android.ModuleLoader")
382+
.getDeclaredConstructor(Long.class, Context.class).newInstance(getActivity(), this);
378383
Log.e(TAG, "loadModules - success");
379384
result = true;
380385
} catch (Exception | UnsatisfiedLinkError e) {
@@ -502,6 +507,39 @@ public boolean removeLocationUpdates() {
502507
return result;
503508
}
504509

510+
public String request(String endPoint, String method, String data, String apiKey) throws IOException {
511+
String result = null;
512+
try {
513+
URL url = new URL(endPoint);
514+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
515+
conn.setRequestMethod(method == null || method.isEmpty() ? "POST" : method);
516+
if (apiKey != null && !apiKey.isEmpty()) {
517+
conn.setRequestProperty("Content-Type", "application/json");
518+
conn.setRequestProperty("Authorization", "Bearer " + apiKey);
519+
}
520+
conn.setDoOutput(true);
521+
if (data != null && !data.isEmpty()) {
522+
OutputStream os = conn.getOutputStream();
523+
os.write(data.getBytes(StandardCharsets.UTF_8));
524+
os.flush();
525+
os.close();
526+
}
527+
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
528+
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
529+
String inputLine;
530+
StringBuilder response = new StringBuilder();
531+
while ((inputLine = in.readLine()) != null) {
532+
response.append(inputLine);
533+
}
534+
in.close();
535+
result = response.toString();
536+
}
537+
} catch (Exception e) {
538+
Log.d(TAG, e.toString());
539+
}
540+
return result;
541+
}
542+
505543
public boolean requestLocationUpdates() {
506544
final LocationManager locationService = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
507545
boolean result = false;

src/platform/android/jni/runtime.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,46 @@ int Runtime::getFontId() {
964964
return result;
965965
}
966966

967+
int Runtime::invokeRequest(int argc, slib_par_t *params, var_t *retval) {
968+
int result = 0;
969+
if (argc >= 1 && argc < 5 &&
970+
v_is_type(params[0].var_p, V_STR) &&
971+
v_is_type(params[1].var_p, V_STR) &&
972+
v_is_type(params[2].var_p, V_STR) &&
973+
v_is_type(params[3].var_p, V_STR)) {
974+
JNIEnv *env;
975+
_app->activity->vm->AttachCurrentThread(&env, nullptr);
976+
977+
auto endPoint = env->NewStringUTF(v_getstr(params[0].var_p));
978+
auto method = env->NewStringUTF(v_getstr(params[1].var_p));
979+
auto data = env->NewStringUTF(v_getstr(params[2].var_p));
980+
auto apiKey = env->NewStringUTF(v_getstr(params[3].var_p));
981+
982+
jclass clazz = env->GetObjectClass(_app->activity->clazz);
983+
const char *signature = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;";
984+
jmethodID methodId = env->GetMethodID(clazz, "request", signature);
985+
jstring resultStr = (jstring)env->CallObjectMethod(_app->activity->clazz, methodId, endPoint, method, data, apiKey);
986+
const char *str = env->GetStringUTFChars(resultStr, JNI_FALSE);
987+
v_init(retval);
988+
v_setstr(retval, str);
989+
result = 1;
990+
991+
env->ReleaseStringUTFChars(resultStr, str);
992+
env->DeleteLocalRef(resultStr);
993+
env->DeleteLocalRef(clazz);
994+
env->DeleteLocalRef(endPoint);
995+
env->DeleteLocalRef(method);
996+
env->DeleteLocalRef(data);
997+
env->DeleteLocalRef(apiKey);
998+
999+
_app->activity->vm->DetachCurrentThread();
1000+
}
1001+
if (!result) {
1002+
v_setstr(retval, "invalid arguments");
1003+
}
1004+
return result;
1005+
}
1006+
9671007
//
9681008
// System platform methods
9691009
//
@@ -1409,7 +1449,8 @@ int sblib_proc_exec(int index, int param_count, slib_par_t *params, var_t *retva
14091449

14101450
const char *lib_funcs[] = {
14111451
"LOCATION",
1412-
"SENSOR"
1452+
"SENSOR",
1453+
"REQUEST"
14131454
};
14141455

14151456
int sblib_func_count(void) {
@@ -1438,6 +1479,9 @@ int sblib_func_exec(int index, int param_count, slib_par_t *params, var_t *retva
14381479
runtime->setSensorData(retval);
14391480
result = 1;
14401481
break;
1482+
case 2:
1483+
result = runtime->invokeRequest(param_count, params, retval);
1484+
break;
14411485
default:
14421486
result = 0;
14431487
break;

src/platform/android/jni/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ struct Runtime : public System {
8181
char *getClipboardText();
8282
void setFocus(bool focus) { _hasFocus = focus; }
8383
int getFontId();
84+
int invokeRequest(int param_count, slib_par_t *params, var_t *retval);
8485

8586
private:
8687
bool _keypadActive;

0 commit comments

Comments
 (0)