-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms_manager.dart
90 lines (70 loc) · 2.19 KB
/
sms_manager.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import 'dart:async';
import 'package:sms/sms.dart';
class SmsManager {
static const allSmsTypes = const [SmsQueryKind.Sent, SmsQueryKind.Inbox, SmsQueryKind.Draft];
static SmsManager _sharedInstance;
SmsQuery _query;
Map<int, SmsThread> _loadedThreadsById;
Map<int, int> _messageCountsByDayOfWeek;
Map<int, int> _messageCountsByHourOfDay;
SmsManager() {
_loadedThreadsById = new Map();
_messageCountsByDayOfWeek = new Map();
_messageCountsByHourOfDay = new Map();
for (int i = 1; i <= 7; i++) {
_messageCountsByHourOfDay[i] = 0;
_messageCountsByDayOfWeek[i] = 0;
}
}
SmsThread getThreadById(int id) {
return _loadedThreadsById[id];
}
List<SmsMessage> getMessagesByThreadId(int id) {
return _loadedThreadsById[id]?.messages;
}
List<SmsMessage> getMessagesByThread(SmsThread thread) {
return getMessagesByThreadId(thread.id);
}
List<SmsThread> getThreads() {
return _loadedThreadsById.values.toList();
}
int getThreadCount() {
return _loadedThreadsById.length;
}
Future loadAllMessages({ bool loadContacts = true, void onProgress({ int current, int total }) }) async {
if (_query == null) {
_query = new SmsQuery();
}
List<SmsMessage> messages = await _query.querySms(
sort: false,
kinds: SmsManager.allSmsTypes
);
int totalMessages = messages.length;
for (int i = 0; i < totalMessages; i++) {
SmsMessage message = messages[i];
if (onProgress != null) {
onProgress(
current: i + 1,
total: totalMessages
);
}
if (!_loadedThreadsById.containsKey(message.threadId)) {
_loadedThreadsById[message.threadId] = new SmsThread(message.threadId);
}
_loadedThreadsById[message.threadId].addMessage(message);
_messageCountsByHourOfDay[message.dateSent.weekday]++;
_messageCountsByDayOfWeek[message.dateSent.weekday]++;
}
if (loadContacts) {
for (SmsThread thread in _loadedThreadsById.values) {
await thread.findContact();
}
}
}
static SmsManager getInstance() {
if (_sharedInstance == null) {
_sharedInstance = new SmsManager();
}
return _sharedInstance;
}
}