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 cdd2120..e662520 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 @@ -3,17 +3,15 @@ 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 androidx.constraintlayout.widget.ConstraintLayout 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 com.rookiedev.hexapod.network.TCPClient.* import kotlinx.coroutines.* import kotlin.math.PI import kotlin.math.atan2 @@ -75,12 +73,13 @@ class ControlActivity : AppCompatActivity() { private var radius = 0f private var tcpClient: TCPClient? = null - private var ip:String = "" + private var ip: String = "" private var port = 0 private val scope = CoroutineScope(Job() + Dispatchers.IO) private var currentState: String = "standby" + private lateinit var progressBar: ConstraintLayout @SuppressLint("ClickableViewAccessibility") @@ -96,6 +95,7 @@ class ControlActivity : AppCompatActivity() { controlWindowInsets(true) val controlCircle = findViewById(R.id.control_image) + progressBar = findViewById(R.id.progressBar) val vto: ViewTreeObserver = controlCircle.viewTreeObserver vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener { @@ -190,7 +190,40 @@ class ControlActivity : AppCompatActivity() { } } ) - this.tcpClient = TCPClient(this, ip, port, object : OnMessageReceived { +// this.tcpClient = TCPClient(this, ip, port, object : OnMessageReceived { +// override fun messageReceived(message: String?) { +// if (message == null) { +//// alertDialog(DISCONNECTED) +// println("no message") +// } +// } +// }, object : OnConnectEstablished { +// override fun onConnected() { +//// udpClient.start() +// println("connected") +// Handler(Looper.getMainLooper()).post { +// progressBar.visibility = View.GONE +// } +// } +// }, object : OnDisconnected{ +// override fun onDisconnected() { +// Handler(Looper.getMainLooper()).post { +// progressBar.visibility = View.GONE +// alertDialog(0) +// } +// } +// } +// ) +// this.tcpClient!!.start() + +// alertDialog(0) + } + + override fun onResume() { + super.onResume() + progressBar.visibility = View.VISIBLE + + this.tcpClient = TCPClient(ip, port, object : OnMessageReceived { override fun messageReceived(message: String?) { if (message == null) { // alertDialog(DISCONNECTED) @@ -201,10 +234,14 @@ class ControlActivity : AppCompatActivity() { override fun onConnected() { // udpClient.start() println("connected") + Handler(Looper.getMainLooper()).post { + progressBar.visibility = View.GONE + } } - }, object : OnDisconnected{ + }, object : OnDisconnected { override fun onDisconnected() { Handler(Looper.getMainLooper()).post { + progressBar.visibility = View.GONE alertDialog(0) } } @@ -212,7 +249,16 @@ class ControlActivity : AppCompatActivity() { ) this.tcpClient!!.start() -// alertDialog(0) + } + + override fun onPause() { + super.onPause() + println("on Pause") + +// saveSharedPref() + tcpClient!!.stopClient() + tcpClient!!.interrupt() + } @@ -246,7 +292,8 @@ class ControlActivity : AppCompatActivity() { when (type) { 0 -> { alert.setTitle("Failed to connect") - alert.setMessage("Failed to connect to the Hexapod" + alert.setMessage( + "Failed to connect to the Hexapod" ) alert.setOnCancelListener(DialogInterface.OnCancelListener { finish() }) alert.setButton(AlertDialog.BUTTON_POSITIVE, @@ -256,7 +303,6 @@ class ControlActivity : AppCompatActivity() { } 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 a72e382..8b45861 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 @@ -1,17 +1,11 @@ package com.rookiedev.hexapod.network import com.rookiedev.hexapod.ControlActivity -import android.util.Log import java.io.* -import java.net.InetAddress -import java.net.InetSocketAddress -import java.net.Socket -import java.net.UnknownHostException -import java.util.concurrent.locks.ReentrantLock +import java.net.* class TCPClient( - c: ControlActivity, ip: String?, port: Int, messagelistener: OnMessageReceived?, @@ -19,21 +13,17 @@ class TCPClient( ondisconnect: OnDisconnected? ) : Thread() { - private val controller: ControlActivity = c - private var TCPSocket:Socket? = null + private var TCPSocket: Socket? = null private var SERVERIP: InetAddress? = null private val SERVERPORT: Int private var serverAddr: InetSocketAddress? = null private var TCPOut: PrintWriter? = null private var TCPIn: BufferedReader? = null - private var TCPMessage: String? = null private var mMessageListener: OnMessageReceived? = null private var onConnected: OnConnectEstablished? = null - private var onDisconnected: OnDisconnected?=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 - private var newMessage:String? = null override fun run() { try { this.TCPSocket = Socket() @@ -41,43 +31,26 @@ class TCPClient( this.TCPSocket!!.connect(serverAddr, 3000) // connecting socket and set timeout in 3s onConnected!!.onConnected() TCPOut = PrintWriter( - BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())), - true - ) + BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())), + true + ) + TCPIn = BufferedReader(InputStreamReader(this.TCPSocket!!.getInputStream())) // sendMessage("test") isConnected = true - while(isConnected) - { -// if (isNewData) -// { -// if (TCPOut != null && !TCPOut!!.checkError()) { -// TCPOut!!.println(newMessage) -// TCPOut!!.flush() -// } -// isNewData = false -// }else{ -// sleep(100) -// } + while (isConnected) { sleep(1000) } - } catch (e: Exception) { + } catch (e: InterruptedException) { // controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT) - println("unable to connect") + println(e) // controller.alertDialog(0) +// onDisconnected!!.onDisconnected() + } catch (e: SocketTimeoutException) { + println(e) onDisconnected!!.onDisconnected() } } -// private fun keepAlive() { -// sendMessage(Constants.requestMessage(Constants.REQUEST_ISALIVE)) -// try { -// TCPMessage = TCPIn!!.readLine() -// mMessageListener!!.messageReceived(TCPMessage) -// } catch (e: IOException) { -// controller.alertDialog(java.lang.ModuleLayer.Controller.DISCONNECTED) -// } -// } - /** * Sends the message entered by client to the server * @@ -86,17 +59,20 @@ class TCPClient( fun sendMessage(message: String?) { // newMessage = message // isNewData = true - if (TCPOut != null && !TCPOut!!.checkError()) { - TCPOut!!.println(message) - TCPOut!!.flush() + + if (this.TCPOut != null && !this.TCPOut!!.checkError()) { + println("send message") + this.TCPOut!!.println(message) + this.TCPOut!!.flush() } } -// fun stopClient() { + fun stopClient() { // sendMessage(Constants.requestMessage(Constants.REQUEST_DISCONNECT)) -// pause = true -// isConnected = false -// } + pause = true + isConnected = false + this.TCPSocket!!.close() + } interface OnMessageReceived { fun messageReceived(message: String?) @@ -124,4 +100,5 @@ class TCPClient( } pause = false } + } \ No newline at end of file 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 3880795..952f347 100644 --- a/software/android/app/src/main/res/layout/activity_control.xml +++ b/software/android/app/src/main/res/layout/activity_control.xml @@ -1,121 +1,177 @@ - - + -