|
@@ -1,9 +1,13 @@
|
|
|
package com.example.david.libretasker.Events;
|
|
package com.example.david.libretasker.Events;
|
|
|
|
|
|
|
|
|
|
+import android.content.ContentResolver;
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
import android.content.Intent;
|
|
|
|
|
+import android.database.Cursor;
|
|
|
|
|
+import android.net.Uri;
|
|
|
import android.os.Build;
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
|
|
+import android.provider.ContactsContract;
|
|
|
import android.provider.Telephony;
|
|
import android.provider.Telephony;
|
|
|
import android.telephony.SmsMessage;
|
|
import android.telephony.SmsMessage;
|
|
|
|
|
|
|
@@ -29,24 +33,34 @@ public class SMSEvent implements Event {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if ( hm.get("NameRegex") == null )
|
|
|
|
|
+ hm.put("NameRegex", ".*");
|
|
|
|
|
+
|
|
|
if ( hm.get("BodyRegex") == null )
|
|
if ( hm.get("BodyRegex") == null )
|
|
|
hm.put("BodyRegex", ".*");
|
|
hm.put("BodyRegex", ".*");
|
|
|
|
|
|
|
|
- String regex = (String)hm.get("BodyRegex");
|
|
|
|
|
- Pattern p;
|
|
|
|
|
|
|
+ String bregex = (String)hm.get("BodyRegex");
|
|
|
|
|
+ String nregex = (String)hm.get("NameRegex");
|
|
|
|
|
+ Pattern b,n;
|
|
|
try {
|
|
try {
|
|
|
- p = Pattern.compile(regex);
|
|
|
|
|
|
|
+ b = Pattern.compile(bregex);
|
|
|
|
|
+ n = Pattern.compile(nregex);
|
|
|
} catch (PatternSyntaxException ex) {
|
|
} catch (PatternSyntaxException ex) {
|
|
|
- failureReason = "Failed to compile regex";
|
|
|
|
|
|
|
+ failureReason = "Failed to compile regex(es)";
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for (SmsMessage s : messages) {
|
|
for (SmsMessage s : messages) {
|
|
|
|
|
+ String number = s.getOriginatingAddress();
|
|
|
|
|
+
|
|
|
if ((String)hm.get("Sender") != null )
|
|
if ((String)hm.get("Sender") != null )
|
|
|
- if ( ! s.getOriginatingAddress().equals((String)hm.get("Sender")) )
|
|
|
|
|
|
|
+ if ( ! number.equals((String)hm.get("Sender")) )
|
|
|
continue;
|
|
continue;
|
|
|
|
|
|
|
|
- if (!p.matcher(s.getMessageBody()).matches())
|
|
|
|
|
|
|
+ if (!b.matcher(s.getMessageBody()).matches())
|
|
|
|
|
+ continue;
|
|
|
|
|
+
|
|
|
|
|
+ if (!n.matcher(getContactName(ctx, number)).matches())
|
|
|
continue;
|
|
continue;
|
|
|
|
|
|
|
|
return true;
|
|
return true;
|
|
@@ -55,6 +69,24 @@ public class SMSEvent implements Event {
|
|
|
failureReason = "Couldn't match Sender nor body";
|
|
failureReason = "Couldn't match Sender nor body";
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
+ private String getContactName(Context context, String phoneNumber) {
|
|
|
|
|
+ ContentResolver cr = context.getContentResolver();
|
|
|
|
|
+ Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
|
|
|
|
|
+ Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
|
|
|
|
|
+ if (cursor == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ String contactName = null;
|
|
|
|
|
+ if(cursor.moveToFirst()) {
|
|
|
|
|
+ contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(cursor != null && !cursor.isClosed()) {
|
|
|
|
|
+ cursor.close();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return contactName;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public String getFailureReason() {
|
|
public String getFailureReason() {
|