2015-04-20 22:39:35 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
2015-01-07 14:00:18 +01:00
|
|
|
|
2015-04-04 23:20:28 +02:00
|
|
|
import android.app.ActivityManager;
|
2015-01-07 14:00:18 +01:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.content.Intent;
|
2015-03-06 14:00:56 +01:00
|
|
|
import android.content.SharedPreferences;
|
2015-01-07 14:00:18 +01:00
|
|
|
import android.os.Bundle;
|
2015-03-04 23:47:47 +01:00
|
|
|
import android.os.PowerManager;
|
2015-03-06 14:00:56 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2015-01-07 14:00:18 +01:00
|
|
|
import android.service.notification.NotificationListenerService;
|
|
|
|
import android.service.notification.StatusBarNotification;
|
2015-01-22 22:49:50 +01:00
|
|
|
import android.util.Log;
|
2015-01-07 14:00:18 +01:00
|
|
|
|
2015-04-20 22:39:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.BluetoothCommunicationService;
|
|
|
|
|
2015-01-07 14:00:18 +01:00
|
|
|
public class NotificationListener extends NotificationListenerService {
|
|
|
|
|
|
|
|
private String TAG = this.getClass().getSimpleName();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotificationPosted(StatusBarNotification sbn) {
|
2015-04-04 23:20:28 +02:00
|
|
|
/*
|
|
|
|
* return early if BluetoothCommunicationService is not running,
|
|
|
|
* else the service would get started every time we get a notification.
|
|
|
|
* unfortunately we cannot enable/disable NotificationListener at runtime like we do with
|
|
|
|
* broadcast receivers because it seems to invalidate the permissions that are
|
|
|
|
* neccessery for NotificationListenerService
|
|
|
|
*/
|
|
|
|
boolean isServiceRunning = false;
|
|
|
|
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
|
|
|
|
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
|
|
|
if (BluetoothCommunicationService.class.getName().equals(service.service.getClassName())) {
|
|
|
|
isServiceRunning = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isServiceRunning) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-04 23:47:47 +01:00
|
|
|
|
|
|
|
|
2015-03-06 14:00:56 +01:00
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
if (!sharedPrefs.getBoolean("notifications_generic_whenscreenon", false)) {
|
|
|
|
PowerManager powermanager = (PowerManager) getSystemService(POWER_SERVICE);
|
|
|
|
if (powermanager.isScreenOn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 23:47:47 +01:00
|
|
|
|
2015-05-08 12:50:42 +02:00
|
|
|
String source = sbn.getPackageName();
|
2015-01-07 14:00:18 +01:00
|
|
|
Notification notification = sbn.getNotification();
|
2015-01-18 22:44:38 +01:00
|
|
|
|
2015-05-08 12:50:42 +02:00
|
|
|
if ((notification.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-18 22:44:38 +01:00
|
|
|
/* do not display messages from "android"
|
|
|
|
* This includes keyboard selection message, usb connection messages, etc
|
|
|
|
* Hope it does not filter out too much, we will see...
|
|
|
|
*/
|
2015-01-24 12:21:15 +01:00
|
|
|
|
|
|
|
if (source.equals("android") ||
|
2015-04-09 18:48:52 +02:00
|
|
|
source.equals("com.android.systemui") ||
|
2015-01-24 12:21:15 +01:00
|
|
|
source.equals("com.android.dialer") ||
|
2015-05-04 01:03:56 +02:00
|
|
|
source.equals("com.android.mms") ||
|
2015-05-08 12:50:42 +02:00
|
|
|
source.equals("com.fsck.k9")) {
|
2015-01-18 22:44:38 +01:00
|
|
|
return;
|
2015-01-24 12:21:15 +01:00
|
|
|
}
|
2015-01-18 22:44:38 +01:00
|
|
|
|
2015-05-08 12:50:42 +02:00
|
|
|
if (source.equals("eu.siacs.conversations")) {
|
|
|
|
if (!"never".equals(sharedPrefs.getString("notification_mode_pebblemsg", "never"))) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-04 23:47:47 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 12:21:15 +01:00
|
|
|
Log.i(TAG, "Processing notification from source " + source);
|
2015-01-22 22:49:50 +01:00
|
|
|
|
2015-01-07 14:00:18 +01:00
|
|
|
Bundle extras = notification.extras;
|
2015-01-26 18:52:19 +01:00
|
|
|
String title = extras.getCharSequence(Notification.EXTRA_TITLE).toString();
|
2015-05-04 01:03:56 +02:00
|
|
|
String content = null;
|
2015-05-08 12:50:42 +02:00
|
|
|
if (extras.containsKey(Notification.EXTRA_TEXT))
|
|
|
|
|
|
|
|
{
|
2015-01-26 18:52:19 +01:00
|
|
|
CharSequence contentCS = extras.getCharSequence(Notification.EXTRA_TEXT);
|
|
|
|
if (contentCS != null) {
|
|
|
|
content = contentCS.toString();
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
|
2015-05-08 12:50:42 +02:00
|
|
|
if (content != null)
|
|
|
|
|
|
|
|
{
|
2015-01-12 00:35:15 +01:00
|
|
|
Intent startIntent = new Intent(NotificationListener.this, BluetoothCommunicationService.class);
|
2015-01-24 12:21:15 +01:00
|
|
|
startIntent.setAction(BluetoothCommunicationService.ACTION_NOTIFICATION_GENERIC);
|
2015-01-12 00:35:15 +01:00
|
|
|
startIntent.putExtra("notification_title", title);
|
2015-01-24 12:21:15 +01:00
|
|
|
startIntent.putExtra("notification_body", content);
|
2015-01-12 00:35:15 +01:00
|
|
|
startService(startIntent);
|
|
|
|
}
|
2015-05-08 12:50:42 +02:00
|
|
|
|
2015-01-07 14:00:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotificationRemoved(StatusBarNotification sbn) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|