From e2140cef50946296c4bfc893e0b4e3bb48277b38 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Thu, 30 Apr 2026 09:37:56 +0300 Subject: [PATCH] Added null checks to am.communication device Logging improvements --- .../main/kotlin/com/tutpro/baresip/Blocked.kt | 3 +- .../kotlin/com/tutpro/baresip/CallHistory.kt | 4 +- app/src/main/kotlin/com/tutpro/baresip/Log.kt | 8 ++- .../main/kotlin/com/tutpro/baresip/Message.kt | 4 +- .../main/kotlin/com/tutpro/baresip/Utils.kt | 49 ++++++++++--------- 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/Blocked.kt b/app/src/main/kotlin/com/tutpro/baresip/Blocked.kt index 1f5b3e56..921b18f5 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Blocked.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Blocked.kt @@ -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) } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt b/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt index 6be81d16..d4bff17b 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt @@ -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 { @@ -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 diff --git a/app/src/main/kotlin/com/tutpro/baresip/Log.kt b/app/src/main/kotlin/com/tutpro/baresip/Log.kt index 1c711778..f8ac36fd 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Log.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Log.kt @@ -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) } -} \ No newline at end of file + + fun e(tag: String, msg: String, tr: Throwable) { + if (logLevel < LogLevel.OFF) android.util.Log.e(tag, msg, tr) + } +} diff --git a/app/src/main/kotlin/com/tutpro/baresip/Message.kt b/app/src/main/kotlin/com/tutpro/baresip/Message.kt index bf27a753..8bc512f9 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Message.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Message.kt @@ -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) } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt index 1600274a..1256dedf 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt @@ -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}") } } }