From e859ece7c6fc83f19a074835be5ae324c68138be Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Mon, 4 May 2015 01:03:56 +0200 Subject: [PATCH] Add Support for notifications send to Pebble from 3rd party applications This enables support for Conversations without using generic notificaion support. Other applications could also work partly but are untested. This commit also changes the SettingsActivity to use Comboboxes instead of two Checkboxes for each notification source. --- app/src/main/AndroidManifest.xml | 7 ++ .../freeyourgadget/gadgetbridge/GB.java | 2 + .../externalevents/K9Receiver.java | 6 +- .../externalevents/NotificationListener.java | 5 +- .../externalevents/PebbleReceiver.java | 60 +++++++++++++++ .../externalevents/SMSReceiver.java | 10 +-- app/src/main/res/values-de/strings.xml | 9 ++- app/src/main/res/values/arrays.xml | 14 ++++ app/src/main/res/values/strings.xml | 10 ++- app/src/main/res/xml/preferences.xml | 74 +++++++++---------- 10 files changed, 142 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PebbleReceiver.java create mode 100644 app/src/main/res/values/arrays.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8de6802b5..835e1f4c9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -93,6 +93,13 @@ + + + + + diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GB.java index fbe209f23..136437cef 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GB.java @@ -12,6 +12,7 @@ import android.util.Log; import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver; +import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver; @@ -48,6 +49,7 @@ public class GB { PhoneCallReceiver.class, SMSReceiver.class, K9Receiver.class, + PebbleReceiver.class, MusicPlaybackReceiver.class, //NotificationListener.class, // disabling this leads to loss of permission to read notifications }; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java index 3997886c5..b5992e695 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java @@ -20,11 +20,11 @@ public class K9Receiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - if (!sharedPrefs.getBoolean("notifications_k9mail", true)) { + if ("never".equals(sharedPrefs.getString("notification_mode_k9mail", "when_screen_off"))) { return; } - if (!sharedPrefs.getBoolean("notifications_k9mail_whenscreenon", false)) { - PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE); + if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_k9mail", "when_screen_off"))) { + PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (powermanager.isScreenOn()) { return; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java index df51e1e2f..18662c400 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java @@ -69,8 +69,9 @@ public class NotificationListener extends NotificationListenerService { if (source.equals("android") || source.equals("com.android.systemui") || source.equals("com.android.dialer") || + source.equals("com.android.mms") || source.equals("com.fsck.k9") || - source.equals("com.android.mms")) { + source.equals("eu.siacs.conversations")) { return; } @@ -82,7 +83,7 @@ public class NotificationListener extends NotificationListenerService { Bundle extras = notification.extras; String title = extras.getCharSequence(Notification.EXTRA_TITLE).toString(); - String content = ""; + String content = null; if (extras.containsKey(Notification.EXTRA_TEXT)) { CharSequence contentCS = extras.getCharSequence(Notification.EXTRA_TEXT); if (contentCS != null) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PebbleReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PebbleReceiver.java new file mode 100644 index 000000000..fe45002bf --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PebbleReceiver.java @@ -0,0 +1,60 @@ +package nodomain.freeyourgadget.gadgetbridge.externalevents; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.PowerManager; +import android.preference.PreferenceManager; +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; + +import nodomain.freeyourgadget.gadgetbridge.BluetoothCommunicationService; + +public class PebbleReceiver extends BroadcastReceiver { + + private final String TAG = this.getClass().getSimpleName(); + + @Override + public void onReceive(Context context, Intent intent) { + + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + if ("never".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) { + return; + } + if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) { + PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + if (powermanager.isScreenOn()) { + return; + } + } + + String title; + String body; + + String messageType = intent.getStringExtra("messageType"); + if (!messageType.equals("PEBBLE_ALERT")) { + Log.i(TAG, "non PEBBLE_ALERT message type not supported"); + return; + } + String notificationData = intent.getStringExtra("notificationData"); + try { + JSONArray notificationJSON = new JSONArray(notificationData); + title = notificationJSON.getJSONObject(0).getString("title"); + body = notificationJSON.getJSONObject(0).getString("body"); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + if (title != null && body != null) { + Intent startIntent = new Intent(context, BluetoothCommunicationService.class); + startIntent.setAction(BluetoothCommunicationService.ACTION_NOTIFICATION_SMS); + startIntent.putExtra("notification_sender", title); + startIntent.putExtra("notification_body", body); + context.startService(startIntent); + } + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java index 709d3485a..e22836c0c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java @@ -17,11 +17,11 @@ public class SMSReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - if (!sharedPrefs.getBoolean("notifications_sms", true)) { + if ("never".equals(sharedPrefs.getString("notification_mode_sms", "when_screen_off"))) { return; } - if (!sharedPrefs.getBoolean("notifications_sms_whenscreenon", false)) { - PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE); + if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_sms", "when_screen_off"))) { + PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (powermanager.isScreenOn()) { return; } @@ -30,8 +30,8 @@ public class SMSReceiver extends BroadcastReceiver { 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]; + for (Object pdu1 : pdus) { + byte[] pdu = (byte[]) pdu1; SmsMessage message = SmsMessage.createFromPdu(pdu); String body = message.getDisplayMessageBody(); String sender = message.getOriginatingAddress(); diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index b276cbf61..9e8ffc10c 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -27,10 +27,15 @@ Synchronisiere die Uhrzeit nach dem Verbindungsaufbau Benachrichtigungen - SMS Benachrichtigungen - K9-Mail Benachrichtigungen + SMS + K9-Mail + Pebble Benachrichtigungen + Unterstützung für Applikationen, die Benachrichtigungnen per Intent an die Pebble senden. Das wird für Conversations benötigt. Andere Benachrichtigungen … auch wenn der Bilschrim an ist + immer + wenn der Bilschirm ausgeschaltet ist + nie Entwickleroptionen Miband MAC-Adresse diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 000000000..c808fd1f2 --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,14 @@ + + + + @string/always + @string/when_screen_off + @string/never + + + always + when_screen_off + never + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0e0387dcc..47a07f259 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -27,11 +27,17 @@ Sync time when connecting Notifications - Notification for SMS - Notification for K9-Mail + SMS + K9-Mail + Pebble Messages + Support for applications which send Notifications to the Pebble via Intent. Needed for Conversation support. Generic notification support … also when screen is on + always + when screen is off + never + Developer Options Miband address diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index d53fde119..ca0024956 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -1,59 +1,51 @@ - + android:key="pref_key_general" + android:title="@string/pref_header_general"> + + android:key="pref_key_datetime" + android:title="@string/pref_header_datetime"> - - + + - - - - - - - + - - + android:key="pref_key_development" + android:title="@string/pref_header_development"> + android:key="development_miaddr" + android:maxLength="17" + android:title="@string/pref_title_development_miaddr" /> \ No newline at end of file