1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 19:06:53 +01:00

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.
This commit is contained in:
Andreas Shimokawa 2015-05-04 01:03:56 +02:00
parent 6d1a4312ef
commit e859ece7c6
10 changed files with 142 additions and 55 deletions

View File

@ -93,6 +93,13 @@
<action android:name="com.fsck.k9.intent.action.EMAIL_RECEIVED" /> <action android:name="com.fsck.k9.intent.action.EMAIL_RECEIVED" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver
android:name=".externalevents.PebbleReceiver"
android:enabled="false">
<intent-filter>
<action android:name="com.getpebble.action.SEND_NOTIFICATION" />
</intent-filter>
</receiver>
<receiver <receiver
android:name=".externalevents.MusicPlaybackReceiver" android:name=".externalevents.MusicPlaybackReceiver"
android:enabled="false"> android:enabled="false">

View File

@ -12,6 +12,7 @@ import android.util.Log;
import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver;
@ -48,6 +49,7 @@ public class GB {
PhoneCallReceiver.class, PhoneCallReceiver.class,
SMSReceiver.class, SMSReceiver.class,
K9Receiver.class, K9Receiver.class,
PebbleReceiver.class,
MusicPlaybackReceiver.class, MusicPlaybackReceiver.class,
//NotificationListener.class, // disabling this leads to loss of permission to read notifications //NotificationListener.class, // disabling this leads to loss of permission to read notifications
}; };

View File

@ -20,11 +20,11 @@ public class K9Receiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!sharedPrefs.getBoolean("notifications_k9mail", true)) { if ("never".equals(sharedPrefs.getString("notification_mode_k9mail", "when_screen_off"))) {
return; return;
} }
if (!sharedPrefs.getBoolean("notifications_k9mail_whenscreenon", false)) { if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_k9mail", "when_screen_off"))) {
PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE); PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (powermanager.isScreenOn()) { if (powermanager.isScreenOn()) {
return; return;
} }

View File

@ -69,8 +69,9 @@ public class NotificationListener extends NotificationListenerService {
if (source.equals("android") || if (source.equals("android") ||
source.equals("com.android.systemui") || source.equals("com.android.systemui") ||
source.equals("com.android.dialer") || source.equals("com.android.dialer") ||
source.equals("com.android.mms") ||
source.equals("com.fsck.k9") || source.equals("com.fsck.k9") ||
source.equals("com.android.mms")) { source.equals("eu.siacs.conversations")) {
return; return;
} }
@ -82,7 +83,7 @@ public class NotificationListener extends NotificationListenerService {
Bundle extras = notification.extras; Bundle extras = notification.extras;
String title = extras.getCharSequence(Notification.EXTRA_TITLE).toString(); String title = extras.getCharSequence(Notification.EXTRA_TITLE).toString();
String content = ""; String content = null;
if (extras.containsKey(Notification.EXTRA_TEXT)) { if (extras.containsKey(Notification.EXTRA_TEXT)) {
CharSequence contentCS = extras.getCharSequence(Notification.EXTRA_TEXT); CharSequence contentCS = extras.getCharSequence(Notification.EXTRA_TEXT);
if (contentCS != null) { if (contentCS != null) {

View File

@ -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);
}
}
}

View File

@ -17,11 +17,11 @@ public class SMSReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!sharedPrefs.getBoolean("notifications_sms", true)) { if ("never".equals(sharedPrefs.getString("notification_mode_sms", "when_screen_off"))) {
return; return;
} }
if (!sharedPrefs.getBoolean("notifications_sms_whenscreenon", false)) { if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_sms", "when_screen_off"))) {
PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE); PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (powermanager.isScreenOn()) { if (powermanager.isScreenOn()) {
return; return;
} }
@ -30,8 +30,8 @@ public class SMSReceiver extends BroadcastReceiver {
Bundle bundle = intent.getExtras(); Bundle bundle = intent.getExtras();
if (bundle != null) { if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus"); Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) { for (Object pdu1 : pdus) {
byte[] pdu = (byte[]) pdus[i]; byte[] pdu = (byte[]) pdu1;
SmsMessage message = SmsMessage.createFromPdu(pdu); SmsMessage message = SmsMessage.createFromPdu(pdu);
String body = message.getDisplayMessageBody(); String body = message.getDisplayMessageBody();
String sender = message.getOriginatingAddress(); String sender = message.getOriginatingAddress();

View File

@ -27,10 +27,15 @@
<string name="pref_title_datetime_syctimeonconnect">Synchronisiere die Uhrzeit nach dem Verbindungsaufbau</string> <string name="pref_title_datetime_syctimeonconnect">Synchronisiere die Uhrzeit nach dem Verbindungsaufbau</string>
<string name="pref_header_notifications">Benachrichtigungen</string> <string name="pref_header_notifications">Benachrichtigungen</string>
<string name="pref_title_notifications_sms">SMS Benachrichtigungen</string> <string name="pref_title_notifications_sms">SMS</string>
<string name="pref_title_notifications_k9mail">K9-Mail Benachrichtigungen</string> <string name="pref_title_notifications_k9mail">K9-Mail</string>
<string name="pref_title_notifications_pebblemsg">Pebble Benachrichtigungen</string>
<string name="pref_summary_notifications_pebblemsg">Unterstützung für Applikationen, die Benachrichtigungnen per Intent an die Pebble senden. Das wird für Conversations benötigt.</string>
<string name="pref_title_notifications_generic">Andere Benachrichtigungen</string> <string name="pref_title_notifications_generic">Andere Benachrichtigungen</string>
<string name="pref_title_whenscreenon">… auch wenn der Bilschrim an ist</string> <string name="pref_title_whenscreenon">… auch wenn der Bilschrim an ist</string>
<string name="always">immer</string>
<string name="when_screen_off">wenn der Bilschirm ausgeschaltet ist</string>
<string name="never">nie</string>
<string name="pref_header_development">Entwickleroptionen</string> <string name="pref_header_development">Entwickleroptionen</string>
<string name="pref_title_development_miaddr">Miband MAC-Adresse</string> <string name="pref_title_development_miaddr">Miband MAC-Adresse</string>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="notification_mode">
<item>@string/always</item>
<item>@string/when_screen_off</item>
<item>@string/never</item>
</string-array>
<string-array name="notification_mode_values">
<item>always</item>
<item>when_screen_off</item>
<item>never</item>
</string-array>
</resources>

View File

@ -27,11 +27,17 @@
<string name="pref_title_datetime_syctimeonconnect">Sync time when connecting</string> <string name="pref_title_datetime_syctimeonconnect">Sync time when connecting</string>
<string name="pref_header_notifications">Notifications</string> <string name="pref_header_notifications">Notifications</string>
<string name="pref_title_notifications_sms">Notification for SMS</string> <string name="pref_title_notifications_sms">SMS</string>
<string name="pref_title_notifications_k9mail">Notification for K9-Mail</string> <string name="pref_title_notifications_k9mail">K9-Mail</string>
<string name="pref_title_notifications_pebblemsg">Pebble Messages</string>
<string name="pref_summary_notifications_pebblemsg">Support for applications which send Notifications to the Pebble via Intent. Needed for Conversation support.</string>
<string name="pref_title_notifications_generic">Generic notification support</string> <string name="pref_title_notifications_generic">Generic notification support</string>
<string name="pref_title_whenscreenon">… also when screen is on</string> <string name="pref_title_whenscreenon">… also when screen is on</string>
<string name="always">always</string>
<string name="when_screen_off">when screen is off</string>
<string name="never">never</string>
<string name="pref_header_development">Developer Options</string> <string name="pref_header_development">Developer Options</string>
<string name="pref_title_development_miaddr">Miband address</string> <string name="pref_title_development_miaddr">Miband address</string>

View File

@ -1,59 +1,51 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory <PreferenceCategory
android:title="@string/pref_header_general" android:key="pref_key_general"
android:key="pref_key_general"> android:title="@string/pref_header_general">
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="general_autoconnectonbluetooth" android:key="general_autoconnectonbluetooth"
android:title="@string/pref_title_general_autoconnectonbluetooth" /> android:title="@string/pref_title_general_autoconnectonbluetooth" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:title="@string/pref_header_datetime" android:key="pref_key_datetime"
android:key="pref_key_datetime"> android:title="@string/pref_header_datetime">
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="true" android:defaultValue="true"
android:key="datetime_synconconnect" android:key="datetime_synconconnect"
android:title="@string/pref_title_datetime_syctimeonconnect" /> android:title="@string/pref_title_datetime_syctimeonconnect" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:title="@string/pref_header_notifications" android:key="pref_key_notifications"
android:key="pref_key_notifications"> android:title="@string/pref_header_notifications">
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. --> <ListPreference
<CheckBoxPreference android:defaultValue="when_screen_off"
android:defaultValue="true" android:entries="@array/notification_mode"
android:key="notifications_sms" android:entryValues="@array/notification_mode_values"
android:key="notification_mode_sms"
android:title="@string/pref_title_notifications_sms" /> android:title="@string/pref_title_notifications_sms" />
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. --> <ListPreference
<CheckBoxPreference android:defaultValue="when_screen_off"
android:defaultValue="false" android:entries="@array/notification_mode"
android:dependency="notifications_sms" android:entryValues="@array/notification_mode_values"
android:key="notifications_sms_whenscreenon" android:key="notification_mode_k9mail"
android:title="@string/pref_title_whenscreenon" />
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<CheckBoxPreference
android:defaultValue="true"
android:key="notifications_k9mail"
android:title="@string/pref_title_notifications_k9mail" /> android:title="@string/pref_title_notifications_k9mail" />
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. --> <ListPreference
<CheckBoxPreference android:defaultValue="when_screen_off"
android:defaultValue="false" android:entries="@array/notification_mode"
android:dependency="notifications_k9mail" android:entryValues="@array/notification_mode_values"
android:key="notifications_k9mail_whenscreenon" android:key="notification_mode_pebblemsg"
android:title="@string/pref_title_whenscreenon" /> android:summary="@string/pref_summary_notifications_pebblemsg"
android:title="@string/pref_title_notifications_pebblemsg" />
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<Preference <Preference
android:key="notifications_generic" android:key="notifications_generic"
android:title="@string/pref_title_notifications_generic" /> android:title="@string/pref_title_notifications_generic" />
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="notifications_generic_whenscreenon" android:key="notifications_generic_whenscreenon"
@ -61,12 +53,12 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:title="@string/pref_header_development" android:key="pref_key_development"
android:key="pref_key_development"> android:title="@string/pref_header_development">
<EditTextPreference <EditTextPreference
android:key="development_miaddr"
android:title="@string/pref_title_development_miaddr"
android:digits="0123456789ABCDEF:" android:digits="0123456789ABCDEF:"
android:maxLength="17" /> android:key="development_miaddr"
android:maxLength="17"
android:title="@string/pref_title_development_miaddr" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>