Added null checks to am.communication device

Logging improvements
This commit is contained in:
Juha Heinanen
2026-04-30 09:37:56 +03:00
parent 7154c5edcb
commit e2140cef50
5 changed files with 37 additions and 31 deletions

View File

@ -41,8 +41,7 @@ class Blocked (
val jsonString = Json.encodeToString(BaresipService.blocked) val jsonString = Json.encodeToString(BaresipService.blocked)
file.writeText(jsonString) file.writeText(jsonString)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Serialization exception: $e") Log.e(TAG, "Serialization exception", e)
e.printStackTrace()
} }
} }

View File

@ -30,6 +30,7 @@ class CallHistoryNew(val aor: String, val peerUri: String, val direction: String
companion object { companion object {
@Suppress("unused")
private const val serialVersionUID: Long = 3 private const val serialVersionUID: Long = 3
private const val CALL_HISTORY_SIZE = 256 private const val CALL_HISTORY_SIZE = 256
@ -124,6 +125,7 @@ class CallHistory(val aor: String, val peerUri: String, val direction: String) :
companion object { companion object {
@Suppress("unused")
private const val serialVersionUID: Long = 2 private const val serialVersionUID: Long = 2
fun get(): ArrayList<CallHistory> { fun get(): ArrayList<CallHistory> {
@ -142,7 +144,7 @@ class CallHistory(val aor: String, val peerUri: String, val direction: String) :
Log.d(TAG, "Got history of ${result.size} calls") Log.d(TAG, "Got history of ${result.size} calls")
file.delete() file.delete()
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "InputStream exception: - $e") Log.e(TAG, "InputStream exception", e)
} }
} }
return result return result

View File

@ -31,6 +31,10 @@ object Log {
} }
fun e(tag: String, msg: String) { fun e(tag: String, msg: String) {
if (logLevel < LogLevel.OFF) android.util.Log.w(tag, msg) if (logLevel < LogLevel.OFF) android.util.Log.e(tag, msg)
} }
}
fun e(tag: String, msg: String, tr: Throwable) {
if (logLevel < LogLevel.OFF) android.util.Log.e(tag, msg, tr)
}
}

View File

@ -1,7 +1,6 @@
package com.tutpro.baresip package com.tutpro.baresip
import java.io.* import java.io.*
import java.util.ArrayList
class Message(val aor: String, val peerUri: String, val message: String, val timeStamp: Long, class Message(val aor: String, val peerUri: String, val message: String, val timeStamp: Long,
var direction: Int, var responseCode: Int, var responseReason: String, var direction: Int, var responseCode: Int, var responseReason: String,
@ -111,8 +110,7 @@ class Message(val aor: String, val peerUri: String, val message: String, val tim
fos.close() fos.close()
Log.d(TAG, "Saved ${BaresipService.messages.size} messages") Log.d(TAG, "Saved ${BaresipService.messages.size} messages")
} catch (e: IOException) { } catch (e: IOException) {
Log.e(TAG, "OutputStream exception: $e") Log.e(TAG, "OutputStream exception", e)
e.printStackTrace()
} }
} }

View File

@ -503,16 +503,16 @@ object Utils {
fun copyAssetToFile(context: Context, asset: String, path: String) { fun copyAssetToFile(context: Context, asset: String, path: String) {
try { try {
val `is` = context.assets.open(asset) context.assets.open(asset).use { `is` ->
val os = FileOutputStream(path) FileOutputStream(path).use { os ->
val buffer = ByteArray(512) val buffer = ByteArray(512)
var byteRead: Int = `is`.read(buffer) var byteRead: Int = `is`.read(buffer)
while (byteRead != -1) { while (byteRead != -1) {
os.write(buffer, 0, byteRead) os.write(buffer, 0, byteRead)
byteRead = `is`.read(buffer) byteRead = `is`.read(buffer)
}
}
} }
os.close()
`is`.close()
} catch (e: IOException) { } catch (e: IOException) {
Log.e(TAG, "Failed to copy asset '$asset' to file: $e") Log.e(TAG, "Failed to copy asset '$asset' to file: $e")
} }
@ -556,10 +556,10 @@ object Utils {
return try { return try {
File(filePath).readBytes() File(filePath).readBytes()
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
Log.e(TAG, "File '$filePath' not found: ${e.printStackTrace()}") Log.e(TAG, "File '$filePath' not found", e)
null null
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to read file '$filePath': ${e.printStackTrace()}") Log.e(TAG, "Failed to read file '$filePath'", e)
null null
} }
} }
@ -620,7 +620,7 @@ object Utils {
if (cursor != null) { if (cursor != null) {
val index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) val index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
cursor.moveToFirst() cursor.moveToFirst()
name = cursor.getString(index) name = cursor.getString(index) ?: ""
cursor.close() cursor.close()
} }
return if (name == "") return if (name == "")
@ -666,7 +666,7 @@ object Utils {
cipherData.copyInto(res, salt.size + 2 + iv.size) cipherData.copyInto(res, salt.size + 2 + iv.size)
return res return res
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Encrypt failed: ${e.printStackTrace()}") Log.e(TAG, "Encrypt failed", e)
} }
return null return null
} }
@ -689,7 +689,7 @@ object Utils {
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec) cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec)
return cipher.doFinal(data) return cipher.doFinal(data)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Decrypt failed: ${e.printStackTrace()}") Log.e(TAG, "Decrypt failed", e)
} }
return null return null
} }
@ -706,7 +706,7 @@ object Utils {
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec) cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec)
plainData = cipher.doFinal(obj.data) plainData = cipher.doFinal(obj.data)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Decrypt failed: ${e.printStackTrace()}") Log.e(TAG, "Decrypt failed", e)
} }
return plainData return plainData
} }
@ -734,7 +734,8 @@ object Utils {
var plainData: ByteArray? = null var plainData: ByteArray? = null
var stream: FileInputStream var stream: FileInputStream
try { try {
stream = ctx.contentResolver.openInputStream(uri) as FileInputStream stream = (ctx.contentResolver.openInputStream(uri) as? FileInputStream)
?: return null
} catch(e: Exception) { } catch(e: Exception) {
Log.w(TAG, "decryptFromUri could not open stream: $e") Log.w(TAG, "decryptFromUri could not open stream: $e")
return null return null
@ -912,7 +913,7 @@ object Utils {
} }
return return
} }
val current = am.communicationDevice!!.type val current = am.communicationDevice?.type ?: AudioDeviceInfo.TYPE_UNKNOWN
Log.d(TAG, "Current com dev/mode is $current/${am.mode}") Log.d(TAG, "Current com dev/mode is $current/${am.mode}")
var speakerDevice: AudioDeviceInfo? = null var speakerDevice: AudioDeviceInfo? = null
for (device in am.availableCommunicationDevices) for (device in am.availableCommunicationDevices)
@ -948,7 +949,8 @@ object Utils {
Log.d(TAG, "Setting mode to NORMAL") Log.d(TAG, "Setting mode to NORMAL")
am.mode = AudioManager.MODE_NORMAL am.mode = AudioManager.MODE_NORMAL
} }
Log.d(TAG, "New com device/mode is ${am.communicationDevice!!.type}/${am.mode}") Log.d(TAG, "New com device/mode is " +
"${am.communicationDevice?.type ?: AudioDeviceInfo.TYPE_UNKNOWN}/${am.mode}")
} }
} else { } else {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@ -959,14 +961,15 @@ object Utils {
@RequiresApi(Build.VERSION_CODES.S) @RequiresApi(Build.VERSION_CODES.S)
fun setCommunicationDevice(am: AudioManager, type: Int) { fun setCommunicationDevice(am: AudioManager, type: Int) {
val current = am.communicationDevice!!.type val current = am.communicationDevice?.type ?: AudioDeviceInfo.TYPE_UNKNOWN
Log.d(TAG, "Current com dev/mode $current/${am.mode}") Log.d(TAG, "Current com dev/mode $current/${am.mode}")
for (device in am.availableCommunicationDevices) for (device in am.availableCommunicationDevices)
if (device.type == type) { if (device.type == type) {
am.setCommunicationDevice(device) am.setCommunicationDevice(device)
break break
} }
Log.d(TAG, "New com dev/mode ${am.communicationDevice!!.type}/${am.mode}") Log.d(TAG, "New com dev/mode is " +
"${am.communicationDevice?.type ?: AudioDeviceInfo.TYPE_UNKNOWN}/${am.mode}")
} }
fun clearCommunicationDevice(am: AudioManager) { fun clearCommunicationDevice(am: AudioManager) {
@ -1003,11 +1006,11 @@ object Utils {
setDataSource(ctx, path.toUri()) setDataSource(ctx, path.toUri())
prepareAsync() prepareAsync()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Log.e(TAG, "MediaPlayer IllegalArgumentException: ${e.printStackTrace()}") Log.e(TAG, "MediaPlayer IllegalArgumentException: ${e.message}")
} catch (e: IOException) { } catch (e: IOException) {
Log.e(TAG, "MediaPlayer IOException: ${e.printStackTrace()}") Log.e(TAG, "MediaPlayer IOException: ${e.message}")
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "MediaPlayer Exception: ${e.printStackTrace()}") Log.e(TAG, "MediaPlayer Exception: ${e.message}")
} }
} }
} }