Skip to content

Commit 806dc6c

Browse files
committed
fix: added check that port 5600 is not already in use
1 parent 6671bd8 commit 806dc6c

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

mobile/src/main/java/net/activitywatch/android/RustInterface.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ class RustInterface constructor(context: Context? = null) {
5353

5454
fun startServerTask(context: Context) {
5555
if(!serverStarted) {
56+
// check if port 5600 is already in use
57+
try {
58+
val socket = java.net.ServerSocket(5600)
59+
socket.close()
60+
} catch(e: java.net.BindException) {
61+
Log.e(TAG, "Port 5600 is already in use, server probably already started")
62+
return
63+
}
64+
5665
serverStarted = true
5766
val executor = Executors.newSingleThreadExecutor()
5867
val handler = Handler(Looper.getMainLooper())
@@ -65,6 +74,8 @@ class RustInterface constructor(context: Context? = null) {
6574

6675
handler.post {
6776
// will run on UI thread after the task is done
77+
Log.i(TAG, "Server finished")
78+
serverStarted = false
6879
}
6980
}
7081
Log.w(TAG, "Server started")

0 commit comments

Comments
 (0)