Add onConnectFailed UI callback
This commit is contained in:
parent
5b439898d8
commit
fee2cfaf3d
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user