diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt index 7b09286..cdd2120 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt @@ -1,13 +1,19 @@ package com.rookiedev.hexapod import android.annotation.SuppressLint +import android.app.AlertDialog +import android.content.DialogInterface +import android.content.Intent import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.view.* import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import com.rookiedev.hexapod.network.TCPClient import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived +import com.rookiedev.hexapod.network.TCPClient.OnDisconnected import kotlinx.coroutines.* import kotlin.math.PI import kotlin.math.atan2 @@ -196,9 +202,17 @@ class ControlActivity : AppCompatActivity() { // udpClient.start() println("connected") } + }, object : OnDisconnected{ + override fun onDisconnected() { + Handler(Looper.getMainLooper()).post { + alertDialog(0) + } + } } ) this.tcpClient!!.start() + +// alertDialog(0) } @@ -226,6 +240,23 @@ class ControlActivity : AppCompatActivity() { } } } + + fun alertDialog(type: Int) { + val alert: AlertDialog = AlertDialog.Builder(this).create() + when (type) { + 0 -> { + alert.setTitle("Failed to connect") + alert.setMessage("Failed to connect to the Hexapod" + ) + alert.setOnCancelListener(DialogInterface.OnCancelListener { finish() }) + alert.setButton(AlertDialog.BUTTON_POSITIVE, + "OK", + DialogInterface.OnClickListener { dialog, which -> finish() }) + } + } + alert.show() + } + } diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/network/TCPClient.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/network/TCPClient.kt index 6c40723..a72e382 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/network/TCPClient.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/network/TCPClient.kt @@ -15,7 +15,8 @@ class TCPClient( ip: String?, port: Int, messagelistener: OnMessageReceived?, - onconnected: OnConnectEstablished? + onconnected: OnConnectEstablished?, + ondisconnect: OnDisconnected? ) : Thread() { private val controller: ControlActivity = c @@ -28,6 +29,7 @@ class TCPClient( private var TCPMessage: String? = null private var mMessageListener: OnMessageReceived? = null private var onConnected: OnConnectEstablished? = null + private var onDisconnected: OnDisconnected?=null private var isConnected = false private var pause = false // if the thread is paused by system private var isNewData = false @@ -61,6 +63,8 @@ class TCPClient( } catch (e: Exception) { // controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT) println("unable to connect") +// controller.alertDialog(0) + onDisconnected!!.onDisconnected() } } @@ -102,10 +106,15 @@ class TCPClient( fun onConnected() } + interface OnDisconnected { + fun onDisconnected() + } + init { SERVERPORT = port mMessageListener = messagelistener onConnected = onconnected + onDisconnected = ondisconnect try { SERVERIP = InetAddress.getByName(ip) serverAddr = InetSocketAddress(SERVERIP, SERVERPORT) diff --git a/software/android/app/src/main/res/layout/activity_control.xml b/software/android/app/src/main/res/layout/activity_control.xml index 650337e..3880795 100644 --- a/software/android/app/src/main/res/layout/activity_control.xml +++ b/software/android/app/src/main/res/layout/activity_control.xml @@ -117,4 +117,5 @@ app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/ic_control_circle" /> + \ No newline at end of file