|
58 | 58 | import java.io.InputStreamReader; |
59 | 59 | import java.io.OutputStream; |
60 | 60 | import java.io.UnsupportedEncodingException; |
| 61 | +import java.net.HttpURLConnection; |
61 | 62 | import java.net.Inet4Address; |
62 | 63 | import java.net.InetAddress; |
63 | 64 | import java.net.NetworkInterface; |
64 | 65 | import java.net.SocketException; |
| 66 | +import java.net.URL; |
65 | 67 | import java.net.URLDecoder; |
| 68 | +import java.nio.charset.StandardCharsets; |
66 | 69 | import java.util.ArrayList; |
67 | 70 | import java.util.Collection; |
68 | 71 | import java.util.Date; |
@@ -115,7 +118,6 @@ public class MainActivity extends NativeActivity { |
115 | 118 | System.loadLibrary("smallbasic"); |
116 | 119 | } |
117 | 120 |
|
118 | | - public native long getActivity(); |
119 | 121 | public static native boolean libraryMode(); |
120 | 122 | public static native void onActivityPaused(boolean paused); |
121 | 123 | public static native void onResize(int width, int height); |
@@ -233,6 +235,8 @@ public Class<?> findClass(String className) { |
233 | 235 | } |
234 | 236 | } |
235 | 237 |
|
| 238 | + public native long getActivity(); |
| 239 | + |
236 | 240 | public byte[] getClipboardText() { |
237 | 241 | final StringBuilder text = new StringBuilder(); |
238 | 242 | final Context context = this; |
@@ -372,9 +376,10 @@ public boolean loadModules() { |
372 | 376 | Log.i(TAG, "loadModules: " + getActivity()); |
373 | 377 | boolean result; |
374 | 378 | try { |
| 379 | + // this would ideally be done with some kind of dependency injection |
375 | 380 | 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); |
378 | 383 | Log.e(TAG, "loadModules - success"); |
379 | 384 | result = true; |
380 | 385 | } catch (Exception | UnsatisfiedLinkError e) { |
@@ -502,6 +507,39 @@ public boolean removeLocationUpdates() { |
502 | 507 | return result; |
503 | 508 | } |
504 | 509 |
|
| 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 | + |
505 | 543 | public boolean requestLocationUpdates() { |
506 | 544 | final LocationManager locationService = (LocationManager) getSystemService(Context.LOCATION_SERVICE); |
507 | 545 | boolean result = false; |
|
0 commit comments