Add onConnectFailed UI callback

This commit is contained in:
Matéo Duparc 2021-05-31 11:53:27 +02:00
parent 5b439898d8
commit fee2cfaf3d
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
4 changed files with 25 additions and 6 deletions

View File

@ -130,6 +130,7 @@ class ChatActivity : ServiceBoundActivity() {
binding.bottomPanel.visibility = View.VISIBLE
}
airaService.uiCallbacks = object : AIRAService.UiCallbacks {
override fun onConnectFailed(ip: String, errorMsg: String?) {}
override fun onNewSession(sessionId: Int, ip: String) {
if (this@ChatActivity.sessionId == sessionId) {
runOnUiThread {

View File

@ -39,6 +39,15 @@ class MainActivity : ServiceBoundActivity() {
}
}
private val uiCallbacks = object : AIRAService.UiCallbacks {
override fun onConnectFailed(ip: String, errorMsg: String?) {
var msg = getString(R.string.unable_to_connect_to, ip)
errorMsg?.let {
msg += ": $it"
}
runOnUiThread {
Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
}
}
override fun onNewSession(sessionId: Int, ip: String) {
runOnUiThread {
handleNewSession(sessionId, ip)

View File

@ -103,6 +103,7 @@ class AIRAService : Service() {
}
interface UiCallbacks {
fun onConnectFailed(ip: String, errorMsg: String?)
fun onNewSession(sessionId: Int, ip: String)
fun onSessionDisconnect(sessionId: Int)
fun onNameTold(sessionId: Int, name: String)
@ -456,13 +457,20 @@ class AIRAService : Service() {
MESSAGE_CONNECT_TO -> {
msg.data.getString("ip")?.let { ip ->
Thread {
try {
val socket = SocketChannel.open()
if (socket.connect(InetSocketAddress(ip, Constants.port))) {
handleNewSocket(socket, true)
val addr = InetSocketAddress(ip, Constants.port)
if (addr.isUnresolved) {
uiCallbacks?.onConnectFailed(ip, getString(R.string.invalid_ip))
} else {
try {
val socket = SocketChannel.open()
if (socket.connect(addr)) {
handleNewSocket(socket, true)
}
} catch (e: NoRouteToHostException) {
uiCallbacks?.onConnectFailed(ip, e.message)
} catch (e: ConnectException) {
uiCallbacks?.onConnectFailed(ip, e.message)
}
} catch (e: ConnectException) {
Log.w("Connect failed", "$ip: "+e.message)
}
}.start()
}

View File

@ -95,6 +95,7 @@
<string name="message_hint">Send a message…</string>
<string name="no_name_error">This session has no name !</string>
<string name="invalid_ip">Invalid IP address</string>
<string name="unable_to_connect_to">Unable to connect to %s</string>
<!--accessibility strings-->
<string name="send_file">Send file</string>