init
This commit is contained in:
@@ -1,36 +1,60 @@
|
||||
import 'dart:async';
|
||||
import 'package:sms_maintained/sms_maintained.dart';
|
||||
import 'package:telephony/telephony.dart' as tel;
|
||||
import '../models/sms_message.dart';
|
||||
|
||||
class SmsReaderService {
|
||||
final _telephony = tel.Telephony.instance;
|
||||
|
||||
Future<List<SmsMessage>> queryAllSms() async {
|
||||
final messages = <SmsMessage>[];
|
||||
final all = await SmsQuery().querySms(
|
||||
kinds: [SmsQueryKind.Inbox, SmsQueryKind.Sent],
|
||||
|
||||
final inbox = await _telephony.getInboxSms(
|
||||
columns: [tel.SmsColumn.ADDRESS, tel.SmsColumn.BODY, tel.SmsColumn.DATE],
|
||||
);
|
||||
for (final s in all) {
|
||||
for (final s in inbox) {
|
||||
messages.add(SmsMessage(
|
||||
phoneNumber: s.address ?? '',
|
||||
contactName: s.sender ?? s.address,
|
||||
contactName: s.address,
|
||||
content: s.body ?? '',
|
||||
type: s.kind == SmsQueryKind.Sent ? 'sent' : 'received',
|
||||
smsDate: DateTime.fromMillisecondsSinceEpoch(s.date!).toUtc().toIso8601String(),
|
||||
type: 'received',
|
||||
smsDate: s.date != null
|
||||
? DateTime.fromMillisecondsSinceEpoch(s.date!).toUtc().toIso8601String()
|
||||
: DateTime.now().toUtc().toIso8601String(),
|
||||
));
|
||||
}
|
||||
|
||||
final sent = await _telephony.getSentSms(
|
||||
columns: [tel.SmsColumn.ADDRESS, tel.SmsColumn.BODY, tel.SmsColumn.DATE],
|
||||
);
|
||||
for (final s in sent) {
|
||||
messages.add(SmsMessage(
|
||||
phoneNumber: s.address ?? '',
|
||||
contactName: s.address,
|
||||
content: s.body ?? '',
|
||||
type: 'sent',
|
||||
smsDate: s.date != null
|
||||
? DateTime.fromMillisecondsSinceEpoch(s.date!).toUtc().toIso8601String()
|
||||
: DateTime.now().toUtc().toIso8601String(),
|
||||
));
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
Stream<SmsMessage> listenToIncoming() {
|
||||
final controller = StreamController<SmsMessage>.broadcast();
|
||||
SmsReceiver().onSmsReceived!.listen((SmsMessage sms) {
|
||||
controller.add(SmsMessage(
|
||||
phoneNumber: sms.address ?? '',
|
||||
contactName: sms.sender ?? sms.address,
|
||||
content: sms.body ?? '',
|
||||
type: 'received',
|
||||
smsDate: DateTime.now().toUtc().toIso8601String(),
|
||||
));
|
||||
});
|
||||
_telephony.listenIncomingSms(
|
||||
onNewMessage: (tel.SmsMessage sms) {
|
||||
controller.add(SmsMessage(
|
||||
phoneNumber: sms.address ?? '',
|
||||
contactName: sms.address,
|
||||
content: sms.body ?? '',
|
||||
type: 'received',
|
||||
smsDate: DateTime.now().toUtc().toIso8601String(),
|
||||
));
|
||||
},
|
||||
listenInBackground: false,
|
||||
);
|
||||
return controller.stream;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user