android: Use FileObserver, which is actually compatible with api level 24 which we claim to support.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 15m39s

This commit is contained in:
Cory McWilliams 2024-09-29 00:17:38 -04:00
parent 195920e476
commit bce263a928

View File

@ -11,6 +11,7 @@ import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
@ -42,12 +43,6 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
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;
public class TildeFriendsActivity extends Activity {
@ -55,9 +50,9 @@ public class TildeFriendsActivity extends Activity {
TildeFriendsWebView web_view;
String base_url;
String port_file_path;
Thread thread;
Thread server_thread;
ServiceConnection service_connection;
FileObserver observer;
private ValueCallback<Uri[]> upload_message;
private final static int FILECHOOSER_RESULT = 1;
@ -95,57 +90,9 @@ public class TildeFriendsActivity extends Activity {
TildeFriendsActivity activity = this;
thread = new Thread(new Runnable() {
@Override
public void run() {
Log.w("tildefriends", "Watching for changes in: " + getFilesDir().toString());
try {
WatchService watcher = FileSystems.getDefault().newWatchService();
Paths.get(getFilesDir().toString()).register(
watcher,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
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 != null && !key.reset()) {
Log.w("tildefriends", "watcher is no longer valid");
break;
}
}
} catch (java.io.IOException e) {
Log.w("tildefriends", "IOException: " + e.toString());
} catch (InterruptedException e) {
Log.w("tildefriends", "InterruptedException: " + e.toString());
}
}
});
thread.start();
Log.w("tildefriends", "Watching for changes in: " + getFilesDir().toString());
observer = make_file_observer(getFilesDir().toString(), port_file_path);
observer.startWatching();
set_status("Starting server...");
server_thread = new Thread(new Runnable() {
@ -393,11 +340,14 @@ public class TildeFriendsActivity extends Activity {
private int read_port(String path) {
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
return Integer.parseInt(reader.readLine());
String line = reader.readLine();
if (line != null) {
return Integer.parseInt(line);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
Log.w("tildefriends", "Port file does not yet exist.");
} catch (java.io.IOException e) {
e.printStackTrace();
}
@ -462,6 +412,36 @@ public class TildeFriendsActivity extends Activity {
}
}
private void check_port_file(String path) {
int port = read_port(port_file_path);
if (port >= 0) {
base_url = "http://127.0.0.1:" + String.valueOf(port) + "/";
runOnUiThread(() -> {
hide_status();
web_view.loadUrl(base_url);
});
observer = null;
} else {
runOnUiThread(() -> {
set_status("Waiting to connect...");
});
}
}
@SuppressWarnings("deprecation")
private FileObserver make_file_observer(String dir, String path) {
FileObserver file_observer = new FileObserver(dir, FileObserver.ALL_EVENTS) {
@Override
public void onEvent(int event, String file) {
if (observer != null) {
check_port_file(path);
}
}
};
check_port_file(path);
return file_observer;
}
@SuppressWarnings("deprecation")
private void set_database_path()
{