From 94c73ef20eb347e7cfe80ba01bc94d0163eb401b Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sat, 24 Jan 2015 12:21:15 +0100 Subject: [PATCH] Receive SMS the proper way, filter them out in the generic NotificationListener. --- README.md | 21 ++++++++++--- app/src/main/AndroidManifest.xml | 6 ++++ .../BluetoothCommunicationService.java | 21 ++++++++++--- .../gadgetbridge/ControlCenter.java | 4 +-- .../gadgetbridge/NotificationListener.java | 16 ++++++---- .../gadgetbridge/SMSReceiver.java | 31 +++++++++++++++++++ 6 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SMSReceiver.java diff --git a/README.md b/README.md index d49145cc..c4b43610 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,24 @@ Right now this is in very early testing stages and only supports the Pebble. USE IT AT YOUR OWN RISK. It will problably not work. And if it works it will annoy you more than it helps you ;) -Known Visible Issues: +Features: +* Notification for incoming calls with caller name and number +* SMS notification with sender name +* Generic Notificaions (android system and telephony notifications filtered out) -* No special notifications, EVERYTHING will be send as a Chat/SMS message +Known Issues: + +* Can't reject or hang up phone calls yet +* Phone calls that are taken or rejected both count as rejected to make the + Pebble stop vibrating. (No in-call display yet) +* No outgoing call support (No in-call display yet) +* Complex notifications (eg. K-9 Mail) do not work yet, maybe we should implement + special K-9 Mail support? Or just support taking complex notificaion apart? * Notifications are not properly queued, if two arrive at about the same time, one of them will get lost (TODO: confirm) -* Android 4.4+ only, we can only change this by implementing an - AccessibiltyService. Don't know if it is worth the hassle. +* Android 4.4+ only, we can only change this by not handling generic + notifications or by using AccessibiltyService. Don't know if it is worth the + hassle. Apart from that there are many internal design flaws which we will discuss using -the issue tracker. +the issue tracker. \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 80ee36ac..32642ee5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ + + + + + + diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index 2111e5da..1bf2a0e2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -33,8 +33,10 @@ public class BluetoothCommunicationService extends Service { = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.start"; public static final String ACTION_STOP = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.stop"; - public static final String ACTION_SENDMESSAGE - = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.sendmessage"; + public static final String ACTION_NOTIFICATION_GENERIC + = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.notification_generic"; + public static final String ACTION_NOTIFICATION_SMS + = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.notification_sms"; public static final String ACTION_INCOMINGCALL = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.incomingcall"; public static final String ACTION_SETTIME @@ -121,11 +123,20 @@ public class BluetoothCommunicationService extends Service { stopForeground(true); stopSelf(); - } else if (intent.getAction().equals(ACTION_SENDMESSAGE)) { + } else if (intent.getAction().equals(ACTION_NOTIFICATION_GENERIC)) { String title = intent.getStringExtra("notification_title"); - String content = intent.getStringExtra("notification_content"); + String body = intent.getStringExtra("notification_body"); if (mBtSocketIoThread != null) { - byte[] msg = PebbleProtocol.encodeSMS(title, content); + byte[] msg = PebbleProtocol.encodeSMS(title, body); + mBtSocketIoThread.write(msg); + } + } else if (intent.getAction().equals(ACTION_NOTIFICATION_SMS)) { + String sender = intent.getStringExtra("notification_sender"); + String body = intent.getStringExtra("notification_body"); + String senderName = getContactDisplayNameByNumber(sender); + + if (mBtSocketIoThread != null) { + byte[] msg = PebbleProtocol.encodeSMS(senderName, body); mBtSocketIoThread.write(msg); } } else if (intent.getAction().equals(ACTION_INCOMINGCALL)) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java index 6d3c184e..30032a2e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java @@ -48,9 +48,9 @@ public class ControlCenter extends ActionBarActivity { @Override public void onClick(View v) { Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class); - startIntent.setAction(BluetoothCommunicationService.ACTION_SENDMESSAGE); + startIntent.setAction(BluetoothCommunicationService.ACTION_NOTIFICATION_GENERIC); startIntent.putExtra("notification_title", editTitle.getText().toString()); - startIntent.putExtra("notification_content", editContent.getText().toString()); + startIntent.putExtra("notification_body", editContent.getText().toString()); startService(startIntent); } }); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java index 82632e62..1a721507 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java @@ -30,10 +30,15 @@ public class NotificationListener extends NotificationListenerService { * Hope it does not filter out too much, we will see... */ String source = sbn.getPackageName(); - if (source.equals("android") || source.equals("com.android.dialer")) - return; + Log.i(TAG, source); - Log.i(TAG, sbn.getPackageName()); + if (source.equals("android") || + source.equals("com.android.dialer") || + source.equals("com.android.mms")) { + return; + } + + Log.i(TAG, "Processing notification from source " + source); Bundle extras = notification.extras; String title = extras.getString(Notification.EXTRA_TITLE); @@ -41,12 +46,11 @@ public class NotificationListener extends NotificationListenerService { if (extras.containsKey(Notification.EXTRA_TEXT)) content = extras.getString(Notification.EXTRA_TEXT); - if (content != null) { Intent startIntent = new Intent(NotificationListener.this, BluetoothCommunicationService.class); - startIntent.setAction(BluetoothCommunicationService.ACTION_SENDMESSAGE); + startIntent.setAction(BluetoothCommunicationService.ACTION_NOTIFICATION_GENERIC); startIntent.putExtra("notification_title", title); - startIntent.putExtra("notification_content", content); + startIntent.putExtra("notification_body", content); startService(startIntent); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SMSReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SMSReceiver.java new file mode 100644 index 00000000..8821d47f --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SMSReceiver.java @@ -0,0 +1,31 @@ +package nodomain.freeyourgadget.gadgetbridge; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.telephony.SmsMessage; + +public class SMSReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + Bundle bundle = intent.getExtras(); + if (bundle != null) { + Object[] pdus = (Object[]) bundle.get("pdus"); + for (int i = 0; i < pdus.length; i++) { + byte[] pdu = (byte[]) pdus[i]; + SmsMessage message = SmsMessage.createFromPdu(pdu); + String body = message.getDisplayMessageBody(); + String sender = message.getOriginatingAddress(); + if (sender != null && body != null) { + Intent startIntent = new Intent(context, BluetoothCommunicationService.class); + startIntent.setAction(BluetoothCommunicationService.ACTION_NOTIFICATION_SMS); + startIntent.putExtra("notification_sender", sender); + startIntent.putExtra("notification_body", body); + context.startService(startIntent); + } + } + } + } +}