blob: 310576a6e8b5f4f1fafb437b1bae2e1927003b89 [file] [log] [blame]
Filipefc05f242015-07-29 16:16:39 +00001package com.fairphone.mycontacts.receivers;
2
3import com.fairphone.mycontacts.service.CallListener;
4import com.fairphone.mycontacts.service.CommunicationMonitorService;
5
6import android.content.Context;
7import android.content.SharedPreferences;
8import android.database.ContentObserver;
9import android.database.Cursor;
10import android.net.Uri;
11import android.text.TextUtils;
12import android.util.Log;
13
14import java.util.Date;
15
16public class SmsObserver extends ContentObserver
17{
18
19 private static final String TAG = SmsObserver.class.getSimpleName();
20 public static final String CONTENT_SMS = "content://sms/sent";
21
22 public static final int MESSAGE_TYPE_SENT = 2;
23
24 private Context mContext;
25
26 CallListener mListener;
27
28 public SmsObserver(Context context, CallListener listener)
29 {
30 super(null);
31 mContext = context;
32 mListener = listener;
33 }
34
35 @Override
36 public void onChange(boolean selfChange)
37 {
38 super.onChange(selfChange);
39
40 String[] reqCols = new String[] {
41 "address", "protocol", "type", "_id", "body", "date"
42 };
43
44 String selection = "_id > " + getLastSentSmsId(mContext);
45
46 Cursor cursor = mContext.getContentResolver().query(Uri.parse(CONTENT_SMS), reqCols, selection, null, "_id desc");
47
48 if (cursor != null) {
49
50 try {
51 while (cursor.moveToNext()) {
52 // Log.d(TAG, DatabaseUtils.dumpCursorToString(cursor));
53 String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
54 int type = cursor.getInt(cursor.getColumnIndex("type"));
55
56 Log.d(TAG, "Protocol: " + protocol + " - Type: " + type);
57 // Only processing outgoing sms event & only when it
58 // is sent successfully (available in SENT box).
59 if (type == MESSAGE_TYPE_SENT) {
60 Log.d(TAG, "Sms Sent");
61 int idColumn = cursor.getColumnIndex("_id");
62 int bodyColumn = cursor.getColumnIndex("body");
63
64 int addressColumn = cursor.getColumnIndex("address");
65 int dateColumn = cursor.getColumnIndex("date");
66 long id = cursor.getLong(idColumn);
67 String to = cursor.getString(addressColumn);
68 String message = cursor.getString(bodyColumn);
69 long date = cursor.getLong(dateColumn);
70
71 Log.d(TAG, "To: " + to + " - Message: " + message + " - Date: " + date);
72 if (!TextUtils.isEmpty(to)) {
73 setLastSentSmsId(mContext, id);
74 mListener.onOutgoingSMS(to);
75 }
76 }
77 }
78 } catch (RuntimeException e) {
Filipe Gonçalves22d6bbe2016-05-11 14:49:18 +010079 Log.e(TAG, "Ex" + e.toString());
Filipefc05f242015-07-29 16:16:39 +000080 }finally {
81 cursor.close();
82 }
83 }
84 }
85
86 private long getLastSentSmsId(Context context)
87 {
88 long id = 0;
89
90 SharedPreferences prefs = context.getSharedPreferences(CommunicationMonitorService.PREFS_PEOPLE_WIDGET_CONTACTS_DATA, 0);
91 if(prefs.getLong(CommunicationMonitorService.LAST_SMS_ID, 0) == 0)
92 {
93 String[] reqCols = new String[] { "_id", "date" };
94
95 String selection = "date < " + (System.currentTimeMillis() - 5000);
96
97 Cursor cursor = context.getContentResolver().query(Uri.parse(CONTENT_SMS), reqCols, selection, null, "_id desc");
98
99 if(cursor != null && cursor.moveToNext()) {
100 int idColumn = cursor.getColumnIndex("_id");
101 id = cursor.getLong(idColumn);
102 cursor.close();
103 }
104
105 return id;
106 }
107 else
108 {
109 return prefs.getLong(CommunicationMonitorService.LAST_SMS_ID, id);
110 }
111 }
112
113 private void setLastSentSmsId(Context context, long id)
114 {
115 SharedPreferences prefs = context.getSharedPreferences(CommunicationMonitorService.PREFS_PEOPLE_WIDGET_CONTACTS_DATA, 0);
116
117 SharedPreferences.Editor editor = prefs.edit();
118 editor.putLong(CommunicationMonitorService.LAST_SMS_ID, id);
119 editor.commit();
120 }
121}