@@ -36,7 +36,7 @@ class SmsManagerService {
3636 } else {
3737 context.getSystemService(SubscriptionManager ::class .java)
3838 }
39- return localSubscriptionManager.activeSubscriptionInfoList!! .size > 1
39+ return ( localSubscriptionManager.activeSubscriptionInfoList? .size ? : 0 ) > 1
4040 }
4141 }
4242
@@ -45,11 +45,38 @@ class SmsManagerService {
4545 }
4646
4747 fun sendMultipartMessage (context : Context , contact : String , parts : ArrayList <String >, sim : String , sendIntents : ArrayList <PendingIntent >, deliveryIntents : ArrayList <PendingIntent >) {
48- getSmsManager(context, sim).sendMultipartTextMessage(contact, null , parts, sendIntents, deliveryIntents)
48+ try {
49+ getSmsManager(context, sim).sendMultipartTextMessage(contact, null , parts, sendIntents, deliveryIntents)
50+ } catch (e: NullPointerException ) {
51+ if (e.message?.contains(" EmergencyNumber.getNumber()" ) == true ) {
52+ Timber .w(e, " Caught EmergencyNumber NPE, falling back to default SmsManager" )
53+ getDefaultSmsManager(context).sendMultipartTextMessage(contact, null , parts, sendIntents, deliveryIntents)
54+ } else {
55+ throw e
56+ }
57+ }
4958 }
5059
5160 fun sendTextMessage (context : Context , contact : String , content : String , sim : String , sentIntent : PendingIntent , deliveryIntent : PendingIntent ) {
52- getSmsManager(context, sim).sendTextMessage(contact, null , content, sentIntent, deliveryIntent)
61+ try {
62+ getSmsManager(context, sim).sendTextMessage(contact, null , content, sentIntent, deliveryIntent)
63+ } catch (e: NullPointerException ) {
64+ if (e.message?.contains(" EmergencyNumber.getNumber()" ) == true ) {
65+ Timber .w(e, " Caught EmergencyNumber NPE, falling back to default SmsManager" )
66+ getDefaultSmsManager(context).sendTextMessage(contact, null , content, sentIntent, deliveryIntent)
67+ } else {
68+ throw e
69+ }
70+ }
71+ }
72+
73+ @Suppress(" DEPRECATION" )
74+ private fun getDefaultSmsManager (context : Context ): SmsManager {
75+ return if (Build .VERSION .SDK_INT >= 31 ) {
76+ context.getSystemService(SmsManager ::class .java)
77+ } else {
78+ SmsManager .getDefault()
79+ }
5380 }
5481
5582 @Suppress(" DEPRECATION" )
@@ -61,15 +88,21 @@ class SmsManagerService {
6188 context.getSystemService(SubscriptionManager ::class .java)
6289 }
6390
64- Timber .d(" active subscription info size: [${localSubscriptionManager.activeSubscriptionInfoList!! .size} ]" )
65- val subscriptionId = if (sim == Constants .SIM1 && localSubscriptionManager.activeSubscriptionInfoList!! .isNotEmpty()) {
66- localSubscriptionManager.activeSubscriptionInfoList!! [0 ].subscriptionId
67- } else if (sim == Constants .SIM2 && localSubscriptionManager.activeSubscriptionInfoList!! .size > 1 ) {
68- localSubscriptionManager.activeSubscriptionInfoList!! [1 ].subscriptionId
91+ val infoList = localSubscriptionManager.activeSubscriptionInfoList
92+ Timber .d(" active subscription info size: [${infoList?.size ? : 0 } ]" )
93+
94+ val subscriptionId = if (sim == Constants .SIM1 && ! infoList.isNullOrEmpty()) {
95+ infoList[0 ].subscriptionId
96+ } else if (sim == Constants .SIM2 && (infoList?.size ? : 0 ) > 1 ) {
97+ infoList!! [1 ].subscriptionId
6998 } else {
7099 SubscriptionManager .getDefaultSmsSubscriptionId()
71100 }
72101
102+ if (subscriptionId == SubscriptionManager .INVALID_SUBSCRIPTION_ID ) {
103+ return getDefaultSmsManager(context)
104+ }
105+
73106 return if (Build .VERSION .SDK_INT < 31 ) {
74107 SmsManager .getSmsManagerForSubscriptionId(subscriptionId)
75108 } else {
0 commit comments