Browse Source

sms, phone number, contact name

David 9 years ago
parent
commit
f638960ec7

+ 4 - 0
README.md

@@ -17,6 +17,10 @@ Supported events (and their conditions):
 	- InvertMinute: boolean (whether to invert the match on the 'Minute' field)
 - Screen:
 	- OFF: boolean (whether to trigger on ON or OFF )
+- SMS:
+	- BodyRegex (String, Regex)
+	- NameRegex (String, Regex)
+	- Sender (String, matching Phone number exactly)
 
 There's an special event called 'State'. State takes a Key (String) and Value (Object). This gets triggered automatically when an Action with type 'State' **changes** its value.
 

+ 1 - 0
app/src/main/AndroidManifest.xml

@@ -9,6 +9,7 @@
     <uses-permission android:name="android.permission.RECEIVE_SMS" />
     <uses-permission android:name="android.permission.READ_SMS" />
     <uses-permission android:name="android.permission.SEND_SMS" />
+    <uses-permission android:name="android.permission.READ_CONTACTS"/>
 
     <application
         android:allowBackup="true"

+ 38 - 6
app/src/main/java/com/example/david/libretasker/Events/SMSEvent.java

@@ -1,9 +1,13 @@
 package com.example.david.libretasker.Events;
 
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
+import android.database.Cursor;
+import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
+import android.provider.ContactsContract;
 import android.provider.Telephony;
 import android.telephony.SmsMessage;
 
@@ -29,24 +33,34 @@ public class SMSEvent implements Event {
             return false;
         }
 
+        if ( hm.get("NameRegex") == null )
+            hm.put("NameRegex", ".*");
+
         if ( hm.get("BodyRegex") == null )
             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 {
-            p = Pattern.compile(regex);
+            b = Pattern.compile(bregex);
+            n = Pattern.compile(nregex);
         } catch (PatternSyntaxException ex) {
-            failureReason = "Failed to compile regex";
+            failureReason = "Failed to compile regex(es)";
             return false;
         }
 
         for (SmsMessage s : messages) {
+            String number = s.getOriginatingAddress();
+
             if ((String)hm.get("Sender") != null )
-                if ( ! s.getOriginatingAddress().equals((String)hm.get("Sender")) )
+                if ( ! number.equals((String)hm.get("Sender")) )
                     continue;
 
-            if (!p.matcher(s.getMessageBody()).matches())
+            if (!b.matcher(s.getMessageBody()).matches())
+                continue;
+
+            if (!n.matcher(getContactName(ctx, number)).matches())
                 continue;
 
             return true;
@@ -55,6 +69,24 @@ public class SMSEvent implements Event {
         failureReason = "Couldn't match Sender nor body";
         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
     public String getFailureReason() {

+ 1 - 0
app/src/main/java/com/example/david/libretasker/UserData/UserData.java

@@ -79,6 +79,7 @@ public class UserData {
                 //action2.put("Type", "Toast");
                 //action2.put("Value", "Screen ON!");
                 break;
+
         }
         assoc.put("Event", event);
         assoc.put("Actions", actions);