Skip to content

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

More cosmetic fixes

Acho Arnolda8e7052ecd99
diff --git a/android/app/src/main/java/com/httpsms/SmsManagerService.kt b/android/app/src/main/java/com/httpsms/SmsManagerService.kt
index 18e1544b..6e34e57f 100644
--- a/android/app/src/main/java/com/httpsms/SmsManagerService.kt
+++ b/android/app/src/main/java/com/httpsms/SmsManagerService.kt
@@ -1,11 +1,14 @@
package com.httpsms
+import android.Manifest
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
+import android.content.pm.PackageManager
import android.os.Build
import android.telephony.SmsManager
import android.telephony.SubscriptionManager
+import androidx.core.app.ActivityCompat
class SmsManagerService {
@@ -21,8 +24,11 @@ class SmsManagerService {
return "$ACTION_SMS_DELIVERED.$messageID"
}
- @SuppressLint("MissingPermission")
fun isDualSIM(context: Context) : Boolean {
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
+ ) {
+ return false
+ }
val localSubscriptionManager: SubscriptionManager = if (Build.VERSION.SDK_INT < 31) {
SubscriptionManager.from(context)
} else {
diff --git a/android/app/src/main/java/com/httpsms/receivers/SimChangeReceiver.kt b/android/app/src/main/java/com/httpsms/receivers/SimChangeReceiver.kt
index 0085f104..2698ab9a 100644
--- a/android/app/src/main/java/com/httpsms/receivers/SimChangeReceiver.kt
+++ b/android/app/src/main/java/com/httpsms/receivers/SimChangeReceiver.kt
@@ -12,8 +12,14 @@ import timber.log.Timber
class SimChangeReceiver : BroadcastReceiver() {
private var lastDualSIMState: Boolean = false
override fun onReceive(context: Context, intent: Intent) {
- Timber.d("SIM state changed")
+ if (intent.action == "android.intent.action.SIM_STATE_CHANGED") {
+ updateDualSimState(context)
+ } else {
+ Timber.e("invalid intent [${intent.action}]")
+ }
+ }
+ private fun updateDualSimState(context: Context) {
Thread {
val currentTimeStamp = System.currentTimeMillis()
val isDualSIM = SmsManagerService.isDualSIM(context)
@@ -25,6 +31,7 @@ class SimChangeReceiver : BroadcastReceiver() {
Settings.getFcmToken(context) ?: "",
isDualSIM
)
+ Timber.d("SIM state change pushed to server")
if (updated) {
lastDualSIMState = isDualSIM
@@ -32,8 +39,7 @@ class SimChangeReceiver : BroadcastReceiver() {
Timber.i("fcm token uploaded successfully")
return@Thread
}
-
Timber.e("could not update fcm token")
}.start()
}
-}
\ No newline at end of file
+}
diff --git a/api/pkg/entities/message.go b/api/pkg/entities/message.go
index ce3c5049..4218bde8 100644
--- a/api/pkg/entities/message.go
+++ b/api/pkg/entities/message.go
@@ -60,15 +60,16 @@ const (
MessageEventNameFailed = MessageEventName("FAILED")
)
+// SIM is the SIM card to use to send the message
type SIM string
const (
- // SIM_1 use the SIM card in slot 1 to send the message
- SIM_1 = SIM("SIM1")
- // SIM_2 use the SIM card in slot 2 to send the message
- SIM_2 = SIM("SIM2")
- // SIM_DEFAULT use the SIM card configured as default communication card to send the message
- SIM_DEFAULT = SIM("DEFAULT")
+ // SIM1 use the SIM card in slot 1 to send the message
+ SIM1 = SIM("SIM1")
+ // SIM2 use the SIM card in slot 2 to send the message
+ SIM2 = SIM("SIM2")
+ // SIMDefault use the SIM card configured as default communication card to send the message
+ SIMDefault = SIM("DEFAULT")
)
// Message represents a message sent between 2 phone numbers
@@ -80,9 +81,9 @@ type Message struct {
Content string `json:"content" example:"This is a sample text message"`
Type MessageType `json:"type" example:"mobile-terminated"`
Status MessageStatus `json:"status" gorm:"index:idx_messages_status" example:"pending"`
- // SIM is the type of event
- // * ISMS: use the SIM card in slot 1
- // * ISMS2: use the SIM card in slot 2
+ // SIM is the SIM card to use to send the message
+ // * SMS1: use the SIM card in slot 1
+ // * SMS2: use the SIM card in slot 2
// * DEFAULT: used the default communication SIM card
SIM SIM `json:"sim" example:"DEFAULT"`
diff --git a/api/pkg/requests/message_bulk_send_request.go b/api/pkg/requests/message_bulk_send_request.go
index b9a7e46a..4343cf49 100644
--- a/api/pkg/requests/message_bulk_send_request.go
+++ b/api/pkg/requests/message_bulk_send_request.go
@@ -30,7 +30,7 @@ func (input *MessageBulkSend) Sanitize() MessageBulkSend {
input.To = to
input.From = input.sanitizeAddress(input.From)
if strings.TrimSpace(string(input.SIM)) == "" {
- input.SIM = entities.SIM_DEFAULT
+ input.SIM = entities.SIMDefault
}
return *input
}
diff --git a/api/pkg/requests/message_receive_request.go b/api/pkg/requests/message_receive_request.go
index 5c1ca019..e3bbe461 100644
--- a/api/pkg/requests/message_receive_request.go
+++ b/api/pkg/requests/message_receive_request.go
@@ -17,7 +17,7 @@ type MessageReceive struct {
From string `json:"from" example:"+18005550199"`
To string `json:"to" example:"+18005550100"`
Content string `json:"content" example:"This is a sample text message received on a phone"`
- // sim card that received the message
+ // SIM card that received the message
SIM entities.SIM `json:"sim" example:"DEFAULT"`
// Timestamp is the time when the event was emitted, Please send the timestamp in UTC with as much precision as possible
Timestamp time.Time `json:"timestamp" example:"2022-06-05T14:26:09.527976+03:00"`
@@ -28,13 +28,13 @@ func (input *MessageReceive) Sanitize() MessageReceive {
input.To = input.sanitizeAddress(input.To)
input.From = input.sanitizeAddress(input.From)
if strings.TrimSpace(string(input.SIM)) == "" {
- input.SIM = entities.SIM_DEFAULT
+ input.SIM = entities.SIMDefault
}
return *input
}
// ToMessageReceiveParams converts MessageReceive to services.MessageReceiveParams
-func (input MessageReceive) ToMessageReceiveParams(userID entities.UserID, source string) services.MessageReceiveParams {
+func (input *MessageReceive) ToMessageReceiveParams(userID entities.UserID, source string) services.MessageReceiveParams {
phone, _ := phonenumbers.Parse(input.To, phonenumbers.UNKNOWN_REGION)
return services.MessageReceiveParams{
Source: source,
diff --git a/api/pkg/requests/message_send_request.go b/api/pkg/requests/message_send_request.go
index b2098b76..4ae24028 100644
--- a/api/pkg/requests/message_send_request.go
+++ b/api/pkg/requests/message_send_request.go
@@ -26,7 +26,7 @@ func (input *MessageSend) Sanitize() MessageSend {
input.To = input.sanitizeAddress(input.To)
input.From = input.sanitizeAddress(input.From)
if strings.TrimSpace(string(input.SIM)) == "" {
- input.SIM = entities.SIM_DEFAULT
+ input.SIM = entities.SIMDefault
}
return *input
}
diff --git a/api/pkg/validators/message_handler_validator.go b/api/pkg/validators/message_handler_validator.go
index 0ca0ccf1..7aac6630 100644
--- a/api/pkg/validators/message_handler_validator.go
+++ b/api/pkg/validators/message_handler_validator.go
@@ -58,9 +58,9 @@ func (validator MessageHandlerValidator) ValidateMessageReceive(_ context.Contex
"sim": []string{
"required",
"in:" + strings.Join([]string{
- string(entities.SIM_1),
- string(entities.SIM_2),
- string(entities.SIM_DEFAULT),
+ string(entities.SIM1),
+ string(entities.SIM2),
+ string(entities.SIMDefault),
}, ","),
},
},
@@ -95,9 +95,9 @@ func (validator MessageHandlerValidator) ValidateMessageSend(ctx context.Context
"sim": []string{
"required",
"in:" + strings.Join([]string{
- string(entities.SIM_1),
- string(entities.SIM_2),
- string(entities.SIM_DEFAULT),
+ string(entities.SIM1),
+ string(entities.SIM2),
+ string(entities.SIMDefault),
}, ","),
},
},
@@ -149,9 +149,9 @@ func (validator MessageHandlerValidator) ValidateMessageBulkSend(ctx context.Con
"sim": []string{
"required",
"in:" + strings.Join([]string{
- string(entities.SIM_1),
- string(entities.SIM_2),
- string(entities.SIM_DEFAULT),
+ string(entities.SIM1),
+ string(entities.SIM2),
+ string(entities.SIMDefault),
}, ","),
},
},