Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/merged with arbile26 fork #203

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ fun SmsMessage.toMap(): HashMap<String, Any?> {
smsMap[SERVICE_CENTER_ADDRESS] = serviceCenterAddress
}
return smsMap

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,17 @@ class SmsController(private val context: Context) {

private fun getSmsManager(): SmsManager {
val subscriptionId = SmsManager.getDefaultSmsSubscriptionId()
val smsManager = getSystemService(context, SmsManager::class.java)
?: 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,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(
Expand Down Expand Up @@ -390,9 +392,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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 22 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -29,6 +30,7 @@ class _MyAppState extends State<MyApp> {
setState(() {
_message = message.body ?? "Error reading message body.";
});
debugPrint('service center address ${message.serviceCenterAddress}');
}

onSendStatus(SendStatus status) {
Expand Down Expand Up @@ -69,7 +71,25 @@ class _MyAppState extends State<MyApp> {
onPressed: () async {
await telephony.openDialer("123413453");
},
child: Text('Open Dialer'))
child: Text('Open Dialer')),
TextButton(
onPressed: () async {
List<SmsMessage> 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'))
],
),
));
Expand Down
3 changes: 2 additions & 1 deletion lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -252,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 }
4 changes: 3 additions & 1 deletion lib/telephony.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -149,6 +149,8 @@ class Telephony {
return _statusListener(SendStatus.SENT);
case SMS_DELIVERED:
return _statusListener(SendStatus.DELIVERED);
case SMS_FAIL:
return _statusListener(SendStatus.FAIL);
}
}

Expand Down
Loading