1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-09 23:21:34 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java
Andreas Shimokawa 6f162c593b Pebble message intent notifications default to "never" now
If these get turned on, Conversations notifications will be handled through Pebble message intents and get filtered out from generic notifcation handling.
2015-05-08 12:50:42 +02:00

116 lines
4.0 KiB
Java

package nodomain.freeyourgadget.gadgetbridge.externalevents;
import android.app.ActivityManager;
import android.app.Notification;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import nodomain.freeyourgadget.gadgetbridge.BluetoothCommunicationService;
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) {
/*
* 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;
}
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!sharedPrefs.getBoolean("notifications_generic_whenscreenon", false)) {
PowerManager powermanager = (PowerManager) getSystemService(POWER_SERVICE);
if (powermanager.isScreenOn()) {
return;
}
}
String source = sbn.getPackageName();
Notification notification = sbn.getNotification();
if ((notification.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT) {
return;
}
/* 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...
*/
if (source.equals("android") ||
source.equals("com.android.systemui") ||
source.equals("com.android.dialer") ||
source.equals("com.android.mms") ||
source.equals("com.fsck.k9")) {
return;
}
if (source.equals("eu.siacs.conversations")) {
if (!"never".equals(sharedPrefs.getString("notification_mode_pebblemsg", "never"))) {
return;
}
}
Log.i(TAG, "Processing notification from source " + source);
Bundle extras = notification.extras;
String title = extras.getCharSequence(Notification.EXTRA_TITLE).toString();
String content = null;
if (extras.containsKey(Notification.EXTRA_TEXT))
{
CharSequence contentCS = extras.getCharSequence(Notification.EXTRA_TEXT);
if (contentCS != null) {
content = contentCS.toString();
}
}
if (content != null)
{
Intent startIntent = new Intent(NotificationListener.this, BluetoothCommunicationService.class);
startIntent.setAction(BluetoothCommunicationService.ACTION_NOTIFICATION_GENERIC);
startIntent.putExtra("notification_title", title);
startIntent.putExtra("notification_body", content);
startService(startIntent);
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}