Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Apply suggestion from @Copilot

Acho Arnold1d00212a801a
diff --git a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
index 9581cedc..b252d1fd 100644
--- a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
+++ b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
@@ -188,37 +188,40 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
val request = Request.Builder().url(urlString).build()
try {
- val response = client.newCall(request).execute()
- if (!response.isSuccessful) {
- Timber.e("Failed to download attachment: ${response.code}")
- response.close()
- return null
- }
+ client.newCall(request).execute().use { response ->
+ if (!response.isSuccessful) {
+ Timber.e("Failed to download attachment: ${response.code}")
+ return null
+ }
- val maxSizeBytes = 1.5 * 1024 * 1024 // most (modern?) carriers have a 2MB limit, so targetting 1.5MB should be safe
- val contentLength = response.body?.contentLength() ?: -1L
- if (contentLength > maxSizeBytes) {
- Timber.e("Attachment is too large ($contentLength bytes).")
- response.close()
- return null
- }
+ val body = response.body
+ if (body == null) {
+ Timber.e("Failed to download attachment: response body is null")
+ return null
+ }
- val mmsDir = File(context.cacheDir, "mms_attachments")
- if (!mmsDir.exists()) {
- mmsDir.mkdirs()
- }
+ val maxSizeBytes = 1.5 * 1024 * 1024 // most (modern?) carriers have a 2MB limit, so targetting 1.5MB should be safe
+ val contentLength = body.contentLength()
+ if (contentLength > maxSizeBytes) {
+ Timber.e("Attachment is too large ($contentLength bytes).")
+ return null
+ }
- val tempFile = File(mmsDir, "mms_${messageId}_$attachmentIndex")
- val inputStream = response.body?.byteStream()
- val outputStream = FileOutputStream(tempFile)
-
- inputStream?.copyToWithLimit(outputStream, maxSizeBytes.toLong())
-
- outputStream.close()
- inputStream?.close()
- response.close()
+ val mmsDir = File(context.cacheDir, "mms_attachments")
+ if (!mmsDir.exists()) {
+ mmsDir.mkdirs()
+ }
+
+ val tempFile = File(mmsDir, "mms_${messageId}_$attachmentIndex")
+ val inputStream = body.byteStream()
+ FileOutputStream(tempFile).use { outputStream ->
+ inputStream.use { input ->
+ input.copyToWithLimit(outputStream, maxSizeBytes.toLong())
+ }
+ }
- return tempFile
+ return tempFile
+ }
} catch (e: Exception) {
Timber.e(e, "Exception while downloading attachment")
return null