SMSReceiver Code

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
public class SmsReceiver extends BroadcastReceiver {
private static final String CHANNEL_ID = "mysms";
@Override
public void onReceive(Context context, Intent intent) {
//Log.e("sms", "mms");
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String senderNum = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
Spammer spammer = Constants.getDbHelper(context).getSpammerByNumber(senderNum);
if(spammer == null){
saveSms(context, senderNum, message, 0, System.currentTimeMillis(), "inbox");
}else {
SpamBox spamBox = new SpamBox();
spamBox.setBody(message);
spamBox.setDate(System.currentTimeMillis());
spamBox.setCreated_at(System.currentTimeMillis());
spamBox.setUpdated_at(System.currentTimeMillis());
spamBox.setSpammer_id(spammer.getId());
Constants.getDbHelper(context).addToSmapBox(spamBox);
showBlockedSmsNotification(context, message, senderNum);
break;
}
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
public boolean saveSms(Context context, String phoneNumber, String message, int readState, long time, String folderName) {
boolean ret = false;
try {
//Telephony.Sms.MESSAGE_TYPE_DRAFT
ContentValues values = new ContentValues();
values.put(Telephony.Sms.ADDRESS, phoneNumber);
values.put(Telephony.Sms.BODY, message);
values.put(Telephony.Sms.READ, readState); //"0" for have not read sms and "1" for have read sms
values.put(Telephony.Sms.DATE, time);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Uri uri = Telephony.Sms.Sent.CONTENT_URI;
if(folderName.equals("inbox")){
uri = Telephony.Sms.Inbox.CONTENT_URI;
}
context.getContentResolver().insert(uri, values);
}
else {
context.getContentResolver().insert(Uri.parse("content://sms/" + folderName), values);
}
ret = true;
OnMessage om = new OnMessage(phoneNumber, message, readState, time);
EventBus.getDefault().post(om);
showNotification(context, message, phoneNumber);
} catch (Exception ex) {
ex.printStackTrace();
ret = false;
}
return ret;
}
public String getContactName(Context context, final String phoneNumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
String contactName = phoneNumber;
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
contactName = cursor.getString(0);
}
cursor.close();
}
return contactName;
}
private void showNotification(Context context, String msg, String address) {
Intent notificationIntent;
String name = getContactName(context, address);
notificationIntent = new Intent(context, MessageDetailsActivity.class);
notificationIntent.putExtra("header", address);
notificationIntent.putExtra("body", msg);
notificationIntent.putExtra("reply", true);
notificationIntent.putExtra("name", name);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis() / 3600, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setContentTitle(name)
.setContentText(msg)
.setSmallIcon(R.drawable.ic_stat_name)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle())
.setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
.setChannelId(CHANNEL_ID)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(contentIntent)
//.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setPriority(NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
String Description = msg;
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify((int) System.currentTimeMillis() / 3600, builder.build());
}
private void showBlockedSmsNotification(Context context, String msg, String address) {
Intent notificationIntent;
String name = getContactName(context, address);
notificationIntent = new Intent(context, MessageDetailsActivity.class);
notificationIntent.putExtra("header", address);
notificationIntent.putExtra("body", msg);
notificationIntent.putExtra("reply", true);
notificationIntent.putExtra("name", name);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setContentTitle("SMS blocked: " + name)
.setContentText(msg)
.setSmallIcon(R.drawable.ic_stat_name)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle())
.setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
.setChannelId(CHANNEL_ID)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(contentIntent)
//.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setPriority(NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
String Description = msg;
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(0, builder.build());
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX