Commit 7cff04ec authored by Anon's avatar Anon
Browse files

FileTransferFragment: Add error handling.

parent be9f3535
......@@ -34,11 +34,14 @@ import android.widget.EditText
import android.widget.ProgressBar
import android.widget.TextView
import androidx.annotation.UiThread
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
import de.ccc.events.badge.card10.R
import de.ccc.events.badge.card10.common.ConnectionException
import de.ccc.events.badge.card10.common.ConnectionService
import de.ccc.events.badge.card10.common.GattListener
import de.ccc.events.badge.card10.main.MainFragment
import java.lang.Exception
import java.lang.IllegalStateException
......@@ -76,8 +79,15 @@ class FileTransferFragment : Fragment(), GattListener, FileTransferListener{
buttonStartStop = view.findViewById(R.id.button_start_stop_transfer)
initConnection()
toggleControls()
try {
initConnection()
toggleControls()
} catch (e: ConnectionException) {
showError(e.message)
} catch (e: Exception) {
showError(getString(R.string.connection_error_generic))
}
}
private fun initConnection() {
......@@ -160,4 +170,21 @@ class FileTransferFragment : Fragment(), GattListener, FileTransferListener{
}
toggleControls()
}
private fun showError(message: String?) {
val ctx = context ?: throw IllegalStateException()
val fm = fragmentManager ?: throw IllegalStateException()
val errorDialog =
AlertDialog.Builder(ctx)
.setMessage(message ?: getString(R.string.connection_error_generic))
.setPositiveButton(R.string.dialog_action_ok) {
dialog, _ -> dialog.dismiss()
fm.beginTransaction()
.replace(R.id.fragment_container, MainFragment())
.addToBackStack(null)
.commit()
}
.show()
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment