1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +01:00

Merge remote-tracking branch 'github/pr/1396'

This commit is contained in:
cpfeiffer 2019-01-30 22:20:24 +01:00
commit 92e92ae792

View File

@ -64,6 +64,7 @@ import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleColor;
import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilter;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilterDao;
import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilterEntry;
import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilterEntryDao;
@ -105,6 +106,8 @@ public class NotificationListener extends NotificationListenerService {
private HashMap<String, Long> notificationBurstPrevention = new HashMap<>();
private HashMap<String, Long> notificationOldRepeatPrevention = new HashMap<>();
private long activeCallPostTime;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
@ -223,10 +226,29 @@ public class NotificationListener extends NotificationListenerService {
super.onDestroy();
}
public String getAppName(String pkg) {
// determinate Source App Name ("Label")
PackageManager pm = getPackageManager();
try {
return (String)pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if (shouldIgnore(sbn))
if ("call".equals(sbn.getNotification().category)) {
handleCallNotification(sbn);
return;
}
if (shouldIgnore(sbn)) {
LOG.info("Ignore notification");
return;
}
Prefs prefs = GBApplication.getPrefs();
@ -264,15 +286,9 @@ public class NotificationListener extends NotificationListenerService {
NotificationSpec notificationSpec = new NotificationSpec();
// determinate Source App Name ("Label")
PackageManager pm = getPackageManager();
ApplicationInfo ai = null;
try {
ai = pm.getApplicationInfo(source, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (ai != null) {
notificationSpec.sourceName = (String) pm.getApplicationLabel(ai);
String name = getAppName(source);
if (name != null) {
notificationSpec.sourceName = name;
}
boolean preferBigText = false;
@ -415,6 +431,44 @@ public class NotificationListener extends NotificationListenerService {
return shouldContinueAfterFilter(body, wordsList, notificationFilter);
}
private void handleCallNotification(StatusBarNotification sbn) {
String app = sbn.getPackageName();
LOG.debug("got call from: " + app);
if(app.equals("com.android.dialer")) {
LOG.debug("Ignoring non-voip call");
return;
}
Notification noti = sbn.getNotification();
dumpExtras(noti.extras);
if(noti.actions != null && noti.actions.length > 0) {
for (Notification.Action action : noti.actions) {
LOG.info("Found call action: " + action.title);
}
/*try {
LOG.info("Executing first action");
noti.actions[0].actionIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}*/
}
// figure out sender
String number;
if(noti.extras.containsKey(Notification.EXTRA_PEOPLE)) {
number = noti.extras.getString(Notification.EXTRA_PEOPLE);
} else if(noti.extras.containsKey(Notification.EXTRA_TITLE)) {
number = noti.extras.getString(Notification.EXTRA_TITLE);
} else {
String appName = getAppName(app);
number = appName != null ? appName : app;
}
activeCallPostTime = sbn.getPostTime();
CallSpec callSpec = new CallSpec();
callSpec.number = number;
callSpec.command = CallSpec.CALL_INCOMING;
GBApplication.deviceService().onSetCallState(callSpec);
}
boolean shouldContinueAfterFilter(@NonNull String body, @NonNull List<String> wordsList, @NonNull NotificationFilter notificationFilter) {
LOG.debug("Mode: '{}' Submode: '{}' WordsList: '{}'", notificationFilter.getNotificationFilterMode(), notificationFilter.getNotificationFilterSubMode(), wordsList);
@ -571,6 +625,13 @@ public class NotificationListener extends NotificationListenerService {
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
LOG.info("Notification removed: " + sbn.getPackageName() + ": " + sbn.getNotification().category);
if(Notification.CATEGORY_CALL.equals(sbn.getNotification().category) && activeCallPostTime == sbn.getPostTime()) {
activeCallPostTime = 0;
CallSpec callSpec = new CallSpec();
callSpec.command = CallSpec.CALL_END;
GBApplication.deviceService().onSetCallState(callSpec);
}
// FIXME: DISABLED for now
/*
if (shouldIgnore(sbn))
@ -584,7 +645,7 @@ public class NotificationListener extends NotificationListenerService {
*/
}
/*
private void dumpExtras(Bundle bundle) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
@ -594,16 +655,16 @@ public class NotificationListener extends NotificationListenerService {
LOG.debug(String.format("Notification extra: %s %s (%s)", key, value.toString(), value.getClass().getName()));
}
}
*/
private boolean shouldIgnore(StatusBarNotification sbn) {
/*
* return early if DeviceCommunicationService 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
* necessary for NotificationListenerService
*/
* return early if DeviceCommunicationService 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
* necessary for NotificationListenerService
*/
if (!isServiceRunning() || sbn == null) {
return true;
}
@ -661,6 +722,7 @@ public class NotificationListener extends NotificationListenerService {
type != NotificationType.WECHAT &&
type != NotificationType.OUTLOOK &&
type != NotificationType.SKYPE) { //see https://github.com/Freeyourgadget/Gadgetbridge/issues/1109
LOG.info("local only");
return true;
}