since this is a fun learning project, moved from java to kotlin for

more fun (or desperation)
This commit is contained in:
Juha Heinanen
2018-03-17 11:18:44 +13:00
parent 2a3802a951
commit afb4635af7
31 changed files with 1352 additions and 1593 deletions
@@ -0,0 +1,67 @@
package com.tutpro.baresip
import android.content.Context
import android.content.DialogInterface
import android.support.v7.app.AlertDialog
import android.util.Log
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.OutputStreamWriter
object Utils {
fun getFileContents(file: File): String {
if (!file.exists()) {
Log.e("Baresip", "Failed to find file: " + file.path)
return ""
} else {
Log.e("Baresip", "Found file: " + file.path)
val length = file.length().toInt()
val bytes = ByteArray(length)
try {
val `in` = FileInputStream(file)
try {
`in`.read(bytes)
} finally {
`in`.close()
}
return String(bytes)
} catch (e: java.io.IOException) {
Log.e("Baresip", "Failed to read file: " + file.path + ": " +
e.toString())
return ""
}
}
}
fun putFileContents(file: File, contents: String) {
try {
val fOut = FileOutputStream(file.absoluteFile, false)
val fWriter = OutputStreamWriter(fOut)
try {
fWriter.write(contents)
fWriter.close()
fOut.close()
} catch (e: java.io.IOException) {
Log.e("Baresip", "Failed to put contents to file: " + e.toString())
}
} catch (e: java.io.FileNotFoundException) {
Log.e("Baresip", "Failed to find contents file: " + e.toString())
}
}
fun alertView(context: Context, message: String) {
val alertDialog = AlertDialog.Builder(context).create()
alertDialog.setTitle("Alert")
alertDialog.setMessage(message)
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK"
) { dialog, _ -> dialog.dismiss() }
alertDialog.show()
}
}