From ea229db71afc88f9a48382dd39249bea0fe33e09 Mon Sep 17 00:00:00 2001 From: Faizan Naqvi Date: Sat, 23 Apr 2022 22:54:03 +0200 Subject: [PATCH 01/10] Added Service Center in SmsColumn to receive ServiceCenter for both incoming SMS as well as getInboxSms calls --- .../com/shounakmulay/telephony/sms/IncomingSmsHandler.kt | 2 ++ .../kotlin/com/shounakmulay/telephony/utils/Constants.kt | 1 + lib/constants.dart | 4 ++++ lib/telephony.dart | 8 +++++++- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/android/src/main/kotlin/com/shounakmulay/telephony/sms/IncomingSmsHandler.kt b/android/src/main/kotlin/com/shounakmulay/telephony/sms/IncomingSmsHandler.kt index 577f8cf..a534fec 100644 --- a/android/src/main/kotlin/com/shounakmulay/telephony/sms/IncomingSmsHandler.kt +++ b/android/src/main/kotlin/com/shounakmulay/telephony/sms/IncomingSmsHandler.kt @@ -15,6 +15,7 @@ import com.shounakmulay.telephony.utils.Constants.MESSAGE import com.shounakmulay.telephony.utils.Constants.MESSAGE_BODY import com.shounakmulay.telephony.utils.Constants.ON_MESSAGE import com.shounakmulay.telephony.utils.Constants.ORIGINATING_ADDRESS +import com.shounakmulay.telephony.utils.Constants.SERVICE_CENTER_ADDRESS import com.shounakmulay.telephony.utils.Constants.SHARED_PREFERENCES_NAME import com.shounakmulay.telephony.utils.Constants.SHARED_PREFS_BACKGROUND_MESSAGE_HANDLE import com.shounakmulay.telephony.utils.Constants.SHARED_PREFS_BACKGROUND_SETUP_HANDLE @@ -105,6 +106,7 @@ fun SmsMessage.toMap(): HashMap { smsMap[TIMESTAMP] = timestampMillis.toString() smsMap[ORIGINATING_ADDRESS] = originatingAddress smsMap[STATUS] = status.toString() + smsMap[SERVICE_CENTER_ADDRESS] = serviceCenterAddress } return smsMap } diff --git a/android/src/main/kotlin/com/shounakmulay/telephony/utils/Constants.kt b/android/src/main/kotlin/com/shounakmulay/telephony/utils/Constants.kt index c2ec0cd..df79791 100644 --- a/android/src/main/kotlin/com/shounakmulay/telephony/utils/Constants.kt +++ b/android/src/main/kotlin/com/shounakmulay/telephony/utils/Constants.kt @@ -48,6 +48,7 @@ object Constants { const val MESSAGE_BODY = "message_body" const val ADDRESS = "address" const val LISTEN_STATUS = "listen_status" + const val SERVICE_CENTER_ADDRESS = "service_center" const val TIMESTAMP = "timestamp" const val ORIGINATING_ADDRESS = "originating_address" diff --git a/lib/constants.dart b/lib/constants.dart index 9dcc93c..31992eb 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -45,6 +45,7 @@ class _SmsProjections { static const String ADDRESS = "address"; static const String MESSAGE_BODY = "message_body"; static const String BODY = "body"; + static const String SERVICE_CENTER_ADDRESS = "service_center"; // static const String CREATOR = "creator"; static const String TIMESTAMP = "timestamp"; @@ -102,6 +103,8 @@ class SmsColumn extends _TelephonyColumn { static const ID = SmsColumn._(_SmsProjections.ID); static const ADDRESS = SmsColumn._(_SmsProjections.ADDRESS); + static const SERVICE_CENTER_ADDRESS = + SmsColumn._(_SmsProjections.SERVICE_CENTER_ADDRESS); static const BODY = SmsColumn._(_SmsProjections.BODY); static const DATE = SmsColumn._(_SmsProjections.DATE); static const DATE_SENT = SmsColumn._(_SmsProjections.DATE_SENT); @@ -144,6 +147,7 @@ const INCOMING_SMS_COLUMNS = [ SmsColumn._(_SmsProjections.ORIGINATING_ADDRESS), SmsColumn._(_SmsProjections.MESSAGE_BODY), SmsColumn._(_SmsProjections.TIMESTAMP), + SmsColumn._(_SmsProjections.SERVICE_CENTER_ADDRESS), SmsColumn.STATUS ]; diff --git a/lib/telephony.dart b/lib/telephony.dart index f2e2ad2..d15eed6 100644 --- a/lib/telephony.dart +++ b/lib/telephony.dart @@ -573,12 +573,14 @@ class SmsMessage { int? threadId; SmsType? type; SmsStatus? status; + String? serviceCenterAddress; /// ## Do not call this method. This method is visible only for testing. @visibleForTesting SmsMessage.fromMap(Map rawMessage, List columns) { final message = Map.castFrom(rawMessage); for (var column in columns) { + debugPrint('Column is ${column._columnName}'); final value = message[column._columnName]; switch (column._columnName) { case _SmsProjections.ID: @@ -633,7 +635,11 @@ class SmsMessage { break; case _SmsProjections.TYPE: var smsTypeIndex = int.tryParse(value); - this.type = smsTypeIndex != null ? SmsType.values[smsTypeIndex] : null; + this.type = + smsTypeIndex != null ? SmsType.values[smsTypeIndex] : null; + break; + case _SmsProjections.SERVICE_CENTER_ADDRESS: + this.serviceCenterAddress = value; break; } } From 7dcfc4f4856fab400ee807597bdf20e6459600bc Mon Sep 17 00:00:00 2001 From: Faizan Naqvi Date: Sat, 23 Apr 2022 22:59:02 +0200 Subject: [PATCH 02/10] Bumped up gradle compileSdk --- android/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/build.gradle b/android/build.gradle index 5805f98..35be12f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -25,7 +25,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion 29 + compileSdkVersion 31 kotlinOptions { jvmTarget = "1.8" From d2d67ccba5498677b6e0023d6509fe256bc2b39b Mon Sep 17 00:00:00 2001 From: Faizan Naqvi Date: Thu, 30 Jun 2022 21:14:37 +0200 Subject: [PATCH 03/10] Added SMS stuff --- example/android/app/build.gradle | 2 +- example/lib/main.dart | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 54668df..305b045 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 30 + compileSdkVersion 31 sourceSets { main.java.srcDirs += 'src/main/kotlin' diff --git a/example/lib/main.dart b/example/lib/main.dart index 404cd18..56efe61 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -3,7 +3,8 @@ import 'dart:async'; import 'package:telephony/telephony.dart'; onBackgroundMessage(SmsMessage message) { - debugPrint("onBackgroundMessage called"); + debugPrint('onBackgroundMessage called'); + debugPrint('service center address ${message.serviceCenterAddress}'); } void main() { @@ -29,6 +30,7 @@ class _MyAppState extends State { setState(() { _message = message.body ?? "Error reading message body."; }); + debugPrint('service center address ${message.serviceCenterAddress}'); } onSendStatus(SendStatus status) { @@ -69,7 +71,25 @@ class _MyAppState extends State { onPressed: () async { await telephony.openDialer("123413453"); }, - child: Text('Open Dialer')) + child: Text('Open Dialer')), + TextButton( + onPressed: () async { + List messages = await telephony.getInboxSms( + columns: [ + SmsColumn.SERVICE_CENTER_ADDRESS, + SmsColumn.ADDRESS + ], + sortOrder: [ + OrderBy(SmsColumn.DATE) + ]); + if (messages.isNotEmpty && messages.length > 2) { + debugPrint( + 'first message ${messages[2].serviceCenterAddress} with sender ${messages[2].address}'); + } else { + debugPrint('no messages were retrieved'); + } + }, + child: Text('Get Messages')) ], ), )); From 95bc3a1d40c7f462f4ee22ba1eb5c9d9f8ccaaa8 Mon Sep 17 00:00:00 2001 From: suhardik Date: Fri, 1 Jul 2022 18:45:01 +0700 Subject: [PATCH 04/10] fix sms controller error --- .../kotlin/com/shounakmulay/telephony/sms/SmsController.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsController.kt b/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsController.kt index fed64ae..dea8ce4 100644 --- a/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsController.kt +++ b/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsController.kt @@ -141,7 +141,8 @@ class SmsController(private val context: Context) { private fun getSmsManager(): SmsManager { val subscriptionId = SmsManager.getDefaultSmsSubscriptionId() - val smsManager = getSystemService(context, SmsManager::class.java) +// val smsManager = getSystemService(context, SmsManager::class.java) + val smsManager = SmsManager.getDefault() ?: throw RuntimeException("Flutter Telephony: Error getting SmsManager") if (subscriptionId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { From 78a83137120ce23b252454825272624ff37a8e1b Mon Sep 17 00:00:00 2001 From: suhardik Date: Fri, 23 Sep 2022 00:46:07 +0700 Subject: [PATCH 05/10] add api level check on sms controller --- .../shounakmulay/telephony/sms/SmsController.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsController.kt b/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsController.kt index dea8ce4..6571472 100644 --- a/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsController.kt +++ b/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsController.kt @@ -141,9 +141,17 @@ class SmsController(private val context: Context) { private fun getSmsManager(): SmsManager { val subscriptionId = SmsManager.getDefaultSmsSubscriptionId() -// val smsManager = getSystemService(context, SmsManager::class.java) - val smsManager = SmsManager.getDefault() - ?: throw RuntimeException("Flutter Telephony: Error getting SmsManager") + val smsManager : SmsManager? + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + smsManager = getSystemService(context, SmsManager::class.java) + } else { + smsManager = SmsManager.getDefault() + } + + if(smsManager == null) { + throw RuntimeException("Flutter Telephony: Error getting SmsManager") + } + if (subscriptionId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { smsManager.createForSubscriptionId(subscriptionId) From b0495407c43574ae39650cbbae680f29b9d4e9fb Mon Sep 17 00:00:00 2001 From: definev Date: Sun, 4 Dec 2022 01:25:42 +0700 Subject: [PATCH 06/10] Prevent tree shaking init background function --- lib/telephony.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/telephony.dart b/lib/telephony.dart index d15eed6..1209b67 100644 --- a/lib/telephony.dart +++ b/lib/telephony.dart @@ -6,12 +6,12 @@ import 'package:flutter/widgets.dart'; import 'package:platform/platform.dart'; part 'constants.dart'; - part 'filter.dart'; typedef MessageHandler(SmsMessage message); typedef SmsSendStatusListener(SendStatus status); +@pragma('vm:entry-point') void _flutterSmsSetupBackgroundChannel( {MethodChannel backgroundChannel = const MethodChannel(_BACKGROUND_CHANNEL)}) async { From ad3220aaed22b68b0f9370f141bec639221fb038 Mon Sep 17 00:00:00 2001 From: "redanium@gmail.com" Date: Tue, 24 Jan 2023 11:07:51 +0100 Subject: [PATCH 07/10] Handle SMS send failure --- .../telephony/sms/SmsMethodCallHandler.kt | 17 +++++++++++++++-- .../shounakmulay/telephony/utils/Constants.kt | 3 ++- lib/constants.dart | 1 + lib/telephony.dart | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt b/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt index e220ba2..655edde 100644 --- a/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt +++ b/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt @@ -34,6 +34,7 @@ import com.shounakmulay.telephony.utils.Constants.SHARED_PREFERENCES_NAME import com.shounakmulay.telephony.utils.Constants.SHARED_PREFS_DISABLE_BACKGROUND_EXE import com.shounakmulay.telephony.utils.Constants.SMS_BACKGROUND_REQUEST_CODE import com.shounakmulay.telephony.utils.Constants.SMS_DELIVERED +import com.shounakmulay.telephony.utils.Constants.SMS_FAIL import com.shounakmulay.telephony.utils.Constants.SMS_QUERY_REQUEST_CODE import com.shounakmulay.telephony.utils.Constants.SMS_SEND_REQUEST_CODE import com.shounakmulay.telephony.utils.Constants.SMS_SENT @@ -390,9 +391,21 @@ class SmsMethodCallHandler( override fun onReceive(ctx: Context?, intent: Intent?) { if (intent != null) { when (intent.action) { - Constants.ACTION_SMS_SENT -> foregroundChannel.invokeMethod(SMS_SENT, null) + Constants.ACTION_SMS_SENT -> { + when(resultCode) { + Activity.RESULT_OK -> foregroundChannel.invokeMethod(SMS_SENT, null) + SmsManager.RESULT_ERROR_GENERIC_FAILURE -> foregroundChannel.invokeMethod(SMS_FAIL, null) + SmsManager.RESULT_ERROR_NO_SERVICE -> foregroundChannel.invokeMethod(SMS_FAIL, null) + SmsManager.RESULT_ERROR_NULL_PDU -> foregroundChannel.invokeMethod(SMS_FAIL, null) + SmsManager.RESULT_ERROR_RADIO_OFF -> foregroundChannel.invokeMethod(SMS_FAIL, null) + + } + } Constants.ACTION_SMS_DELIVERED -> { - foregroundChannel.invokeMethod(SMS_DELIVERED, null) + when (resultCode) { + Activity.RESULT_OK -> foregroundChannel.invokeMethod(SMS_DELIVERED, null) + Activity.RESULT_CANCELED -> foregroundChannel.invokeMethod(SMS_FAIL, null) + } context.unregisterReceiver(this) } } diff --git a/android/src/main/kotlin/com/shounakmulay/telephony/utils/Constants.kt b/android/src/main/kotlin/com/shounakmulay/telephony/utils/Constants.kt index df79791..388b745 100644 --- a/android/src/main/kotlin/com/shounakmulay/telephony/utils/Constants.kt +++ b/android/src/main/kotlin/com/shounakmulay/telephony/utils/Constants.kt @@ -35,7 +35,8 @@ object Constants { const val HANDLE_BACKGROUND_MESSAGE = "handleBackgroundMessage" const val SMS_SENT = "smsSent" const val SMS_DELIVERED = "smsDelivered" - + const val SMS_FAIL = "smsFail" + // Invoke Method Arguments const val HANDLE = "handle" const val MESSAGE = "message" diff --git a/lib/constants.dart b/lib/constants.dart index 31992eb..43df1cb 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -35,6 +35,7 @@ const DIAL_PHONE_NUMBER = "dialPhoneNumber"; const ON_MESSAGE = "onMessage"; const SMS_SENT = "smsSent"; const SMS_DELIVERED = "smsDelivered"; +const SMS_FAIL = "smsFail"; /// /// Possible parameters that can be fetched during a SMS query operation. diff --git a/lib/telephony.dart b/lib/telephony.dart index d15eed6..8496a93 100644 --- a/lib/telephony.dart +++ b/lib/telephony.dart @@ -149,6 +149,8 @@ class Telephony { return _statusListener(SendStatus.SENT); case SMS_DELIVERED: return _statusListener(SendStatus.DELIVERED); + case SMS_FAIL: + return _statusListener(SendStatus.FAIL); } } From 15460dfa74bed439d18caeabc9ea89f206f707b1 Mon Sep 17 00:00:00 2001 From: "redanium@gmail.com" Date: Tue, 24 Jan 2023 11:16:49 +0100 Subject: [PATCH 08/10] Add FAIL to sendStatus --- lib/constants.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/constants.dart b/lib/constants.dart index 43df1cb..6f69eea 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -253,4 +253,4 @@ extension Value on Sort { } /// Represents the status of a sms message sent from the device. -enum SendStatus { SENT, DELIVERED } +enum SendStatus { SENT, DELIVERED, FAIL } From 4c7ef60995529e11c0b77da68a42ba7baf1605ee Mon Sep 17 00:00:00 2001 From: Yusif Jasim <74053601+arbile26@users.noreply.github.com> Date: Mon, 7 Aug 2023 14:50:54 +0300 Subject: [PATCH 09/10] Update SmsMethodCallHandler.kt Fix 'Unresolved reference: SmsManager'. --- .../com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt b/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt index 655edde..80c4093 100644 --- a/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt +++ b/android/src/main/kotlin/com/shounakmulay/telephony/sms/SmsMethodCallHandler.kt @@ -45,6 +45,7 @@ import com.shounakmulay.telephony.utils.SmsAction import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.PluginRegistry +import android.telephony.SmsManager; class SmsMethodCallHandler( From edcb03aec842de208ac08aa3081c4fea5e066a77 Mon Sep 17 00:00:00 2001 From: Faizan Naqvi Date: Tue, 28 May 2024 23:37:29 +0200 Subject: [PATCH 10/10] fix: commented out code for nulling the foregroundSmsChannel --- .../kotlin/com/shounakmulay/telephony/TelephonyPlugin.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/android/src/main/kotlin/com/shounakmulay/telephony/TelephonyPlugin.kt b/android/src/main/kotlin/com/shounakmulay/telephony/TelephonyPlugin.kt index 58fc693..8b63631 100644 --- a/android/src/main/kotlin/com/shounakmulay/telephony/TelephonyPlugin.kt +++ b/android/src/main/kotlin/com/shounakmulay/telephony/TelephonyPlugin.kt @@ -66,8 +66,9 @@ class TelephonyPlugin : FlutterPlugin, ActivityAware { } private fun tearDownPlugin() { - IncomingSmsReceiver.foregroundSmsChannel = null - smsChannel.setMethodCallHandler(null) + // Note: due to background workers the tearDown is called and then foreground channel is set to null. +// IncomingSmsReceiver.foregroundSmsChannel = null +// smsChannel.setMethodCallHandler(null) } }