diff --git a/src/android/com/unprompted/tildefriends/MainActivity.java b/src/android/com/unprompted/tildefriends/MainActivity.java index 6cf02f37..bda9dc73 100644 --- a/src/android/com/unprompted/tildefriends/MainActivity.java +++ b/src/android/com/unprompted/tildefriends/MainActivity.java @@ -41,10 +41,14 @@ public class MainActivity extends Activity { WebView web_view; String base_url; Process process; - WatchService watcher; + Thread thread; @Override protected void onCreate(Bundle savedInstanceState) { + StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() + .detectLeakedClosableObjects() + .penaltyLog() + .build()); super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); @@ -55,8 +59,7 @@ public class MainActivity extends Activity { Log.w("tildefriends", String.format("getPackageResourcePath() is %s", getPackageResourcePath().toString())); Log.w("tildefriends", String.format("os.arch is %s", arch)); - try { - ZipInputStream zip = new ZipInputStream(new BufferedInputStream(new FileInputStream(getPackageResourcePath().toString()))); + try (ZipInputStream zip = new ZipInputStream(new BufferedInputStream(new FileInputStream(getPackageResourcePath().toString())))) { ZipEntry entry = null; String lookup = String.format("bin/%s/tildefriends", arch); Log.w("tildefriends", "Looking for " + lookup); @@ -71,7 +74,6 @@ public class MainActivity extends Activity { } out.close(); new File(getFilesDir().toString() + "/tildefriends").setExecutable(true); - } finally { } } zip.closeEntry(); @@ -89,12 +91,11 @@ public class MainActivity extends Activity { MainActivity activity = this; - new Thread(new Runnable() { + thread = new Thread(new Runnable() { @Override public void run() { - try { - Log.w("tildefriends", "Watching for changes in: " + getFilesDir().toString()); - watcher = FileSystems.getDefault().newWatchService(); + Log.w("tildefriends", "Watching for changes in: " + getFilesDir().toString()); + try (WatchService watcher = FileSystems.getDefault().newWatchService()) { Paths.get(getFilesDir().toString()).register( watcher, StandardWatchEventKinds.ENTRY_CREATE, @@ -108,8 +109,6 @@ public class MainActivity extends Activity { activity.runOnUiThread(() -> { web_view.loadUrl(base_url); }); - watcher.close(); - watcher = null; break; } } @@ -124,7 +123,8 @@ public class MainActivity extends Activity { Log.w("tildefriends", "InterruptedException: " + e.toString()); } } - }).start(); + }); + thread.start(); String exe = getFilesDir().toString() + "/tildefriends"; ProcessBuilder builder = new ProcessBuilder(exe, "run", "-z", getPackageResourcePath().toString(), "-a", "out_http_port_file=" + port_file_path, "-p", "0"); @@ -237,21 +237,12 @@ public class MainActivity extends Activity { } private int read_port(String path) { - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(path)); + try (BufferedReader reader = new BufferedReader(new FileReader(path))) { return Integer.parseInt(reader.readLine()); } catch (java.io.FileNotFoundException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); - } finally { - try { - if (reader != null) { - reader.close(); - } - } catch (java.io.IOException e) { - } } return -1; }