From 0cda4630878b1fcaf1c68e9b598fc7a4a42bb1cf Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 7 Mar 2019 16:23:47 +0100 Subject: [PATCH] Use call notification as service's foreground notification instead of having two --- .../linphone/LinphoneLauncherActivity.java | 2 + .../java/org/linphone/LinphoneService.java | 4 ++ .../notifications/NotificationsManager.java | 42 +++++++++++++++---- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/linphone/LinphoneLauncherActivity.java b/app/src/main/java/org/linphone/LinphoneLauncherActivity.java index f2f1d5283..7ef097b6b 100644 --- a/app/src/main/java/org/linphone/LinphoneLauncherActivity.java +++ b/app/src/main/java/org/linphone/LinphoneLauncherActivity.java @@ -68,6 +68,8 @@ public class LinphoneLauncherActivity extends Activity { classToStart = LinphoneActivity.class; } + LinphoneService.instance().removeForegroundServiceNotificationIfPossible(); + mHandler.postDelayed( new Runnable() { @Override diff --git a/app/src/main/java/org/linphone/LinphoneService.java b/app/src/main/java/org/linphone/LinphoneService.java index 077f710ef..d87e4898d 100644 --- a/app/src/main/java/org/linphone/LinphoneService.java +++ b/app/src/main/java/org/linphone/LinphoneService.java @@ -128,6 +128,10 @@ public final class LinphoneService extends Service { return mNotificationManager; } + public void removeForegroundServiceNotificationIfPossible() { + mNotificationManager.removeForegroundServiceNotificationIfPossible(); + } + public Class getIncomingReceivedActivity() { return mIncomingReceivedActivity; } diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.java b/app/src/main/java/org/linphone/notifications/NotificationsManager.java index b7d1011d5..8cd4f7cb6 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.java +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.java @@ -56,11 +56,13 @@ public class NotificationsManager { private final HashMap mCallNotifMap; private int mLastNotificationId; private final Notification mServiceNotification; + private int mCurrentForegroundServiceNotification; public NotificationsManager(Context context) { mContext = context; mChatNotifMap = new HashMap<>(); mCallNotifMap = new HashMap<>(); + mCurrentForegroundServiceNotification = 0; mNM = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE); mNM.cancelAll(); @@ -110,10 +112,24 @@ public class NotificationsManager { public void startForeground() { LinphoneService.instance().startForeground(SERVICE_NOTIF_ID, mServiceNotification); + mCurrentForegroundServiceNotification = SERVICE_NOTIF_ID; + } + + public void startForeground(Notification notification, int id) { + LinphoneService.instance().startForeground(id, notification); + mCurrentForegroundServiceNotification = id; } public void stopForeground() { LinphoneService.instance().stopForeground(true); + mCurrentForegroundServiceNotification = 0; + } + + public void removeForegroundServiceNotificationIfPossible() { + if (!LinphonePreferences.instance().getServiceNotificationVisibility() + && mCurrentForegroundServiceNotification == SERVICE_NOTIF_ID) { + stopForeground(); + } } public void sendNotification(int id, Notification notif) { @@ -295,19 +311,16 @@ public class NotificationsManager { mCallNotifMap.put(addressAsString, notif); } - if (!isServiceNotificationDisplayed()) { - if (call.getCore().getCallsNb() == 0) { - stopForeground(); - } else { - startForeground(); - } - } - int notificationTextId; int iconId; switch (call.getState()) { case Released: case End: + if (mCurrentForegroundServiceNotification == notif.getNotificationId()) { + // Call is released, remove service notification to allow for an other call to + // be service notification + stopForeground(); + } mNM.cancel(notif.getNotificationId()); mCallNotifMap.remove(addressAsString); return; @@ -346,7 +359,18 @@ public class NotificationsManager { bm, name, pendingIntent); - sendNotification(notif.getNotificationId(), notification); + + if (!isServiceNotificationDisplayed()) { + if (call.getCore().getCallsNb() == 0) { + stopForeground(); + } else { + if (mCurrentForegroundServiceNotification == 0) { + startForeground(notification, notif.getNotificationId()); + } else { + sendNotification(notif.getNotificationId(), notification); + } + } + } } public String getSipUriForCallNotificationId(int notificationId) {