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)
file.writeText(jsonString)
} catch (e: Exception) {
Log.e(TAG, "Serialization exception: $e")
e.printStackTrace()
Log.e(TAG, "Serialization exception", e)
}
}

View File

@ -30,6 +30,7 @@ class CallHistoryNew(val aor: String, val peerUri: String, val direction: String
companion object {
@Suppress("unused")
private const val serialVersionUID: Long = 3
private const val CALL_HISTORY_SIZE = 256
@ -124,6 +125,7 @@ class CallHistory(val aor: String, val peerUri: String, val direction: String) :
companion object {
@Suppress("unused")
private const val serialVersionUID: Long = 2
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")
file.delete()
} catch (e: Exception) {
Log.e(TAG, "InputStream exception: - $e")
Log.e(TAG, "InputStream exception", e)
}
}
return result

View File

@ -31,6 +31,10 @@ object Log {
}
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
import java.io.*
import java.util.ArrayList
class Message(val aor: String, val peerUri: String, val message: String, val timeStamp: Long,
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()
Log.d(TAG, "Saved ${BaresipService.messages.size} messages")
} catch (e: IOException) {
Log.e(TAG, "OutputStream exception: $e")
e.printStackTrace()
Log.e(TAG, "OutputStream exception", e)
}
}

View File

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