diff --git a/src/android/com/unprompted/tildefriends/MainActivity.java b/src/android/com/unprompted/tildefriends/MainActivity.java index c5376989..598fb788 100644 --- a/src/android/com/unprompted/tildefriends/MainActivity.java +++ b/src/android/com/unprompted/tildefriends/MainActivity.java @@ -1,7 +1,5 @@ package com.unprompted.tildefriends; -import android.os.StrictMode; -import android.os.strictmode.Violation; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; @@ -9,6 +7,8 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.CountDownTimer; +import android.os.StrictMode; +import android.os.strictmode.Violation; import android.os.SystemClock; import android.util.Log; import android.view.KeyEvent; @@ -24,9 +24,9 @@ import android.widget.TextView; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileReader; import java.lang.Process; import java.lang.Thread; import java.nio.file.FileSystems; @@ -35,6 +35,7 @@ import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; +import java.util.concurrent.TimeUnit; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -66,6 +67,7 @@ public class MainActivity extends Activity { } String port_file_path = getFilesDir().toString() + "/port.txt"; + new File(port_file_path).delete(); base_url = "http://127.0.0.1:12345/"; MainActivity activity = this; @@ -80,25 +82,33 @@ public class MainActivity extends Activity { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY); while (true) { - WatchKey key = watcher.take(); - for (WatchEvent event : key.pollEvents()) { - if (event.context().toString().equals("port.txt")) { - Log.w("tildefriends", "Observed file write: " + event.context().toString()); - int port = read_port(port_file_path); - if (port >= 0) { - base_url = "http://127.0.0.1:" + String.valueOf(port) + "/"; - activity.runOnUiThread(() -> { - activity.hide_status(); - web_view.loadUrl(base_url); - }); - break; - } else { - activity.runOnUiThread(() -> { - activity.set_status("Waiting to connect..."); - }); + WatchKey key = watcher.poll(100, TimeUnit.MILLISECONDS); + boolean attempt_it = true; + if (key != null) + { + attempt_it = false; + for (WatchEvent event : key.pollEvents()) { + if (event.context().toString().equals("port.txt")) { + Log.w("tildefriends", "Observed file write: " + event.context().toString()); + attempt_it = true; } } } + if (attempt_it) { + int port = read_port(port_file_path); + if (port >= 0) { + base_url = "http://127.0.0.1:" + String.valueOf(port) + "/"; + activity.runOnUiThread(() -> { + activity.hide_status(); + web_view.loadUrl(base_url); + }); + break; + } else { + activity.runOnUiThread(() -> { + activity.set_status("Waiting to connect..."); + }); + } + } if (!key.reset()) { Log.w("tildefriends", "watcher is no longer valid"); break;