From 1491cb97109d933c29b885baeb86cb9c26076cd1 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 16 Nov 2018 15:43:42 +0100 Subject: [PATCH] Always keep hangup action in call notification + fixed multiple call notifications issue --- CHANGELOG.md | 2 +- .../java/org/linphone/LinphoneService.java | 37 +++++++-------- .../compatibility/ApiTwentyFourPlus.java | 22 ++++----- .../compatibility/ApiTwentySixPlus.java | 45 +++++++++---------- .../linphone/compatibility/Compatibility.java | 6 +-- .../NotificationBroadcastReceiver.java | 2 +- 6 files changed, 53 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02d03013b..3c37c909e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ Group changes to describe their impact on the project, as follows: - Use TextureView instead of GL2JNIView, easier to use and will fix issues. - Send SMS to invite your friends in using Linphone. - Reply to received chat message in notification. -- Answer or hangup incoming call in notification. +- Answer or hangup calls in notification. ## [4.0.1] - 2018-06-26 diff --git a/app/src/main/java/org/linphone/LinphoneService.java b/app/src/main/java/org/linphone/LinphoneService.java index 65933e308..3af103886 100644 --- a/app/src/main/java/org/linphone/LinphoneService.java +++ b/app/src/main/java/org/linphone/LinphoneService.java @@ -511,7 +511,6 @@ public final class LinphoneService extends Service { Address address = call.getRemoteAddress(); String addressAsString = address.asStringUriOnly(); Notified notif = mCallNotifMap.get(addressAsString); - if (notif == null) { notif = new Notified(); notif.notificationId = mLastNotificationId; @@ -519,18 +518,22 @@ public final class LinphoneService extends Service { mCallNotifMap.put(addressAsString, notif); } - int notificationTextId = 0; - int inconId = 0; + if (!displayServiceNotification()) { + if (call.getCore().getCallsNb() == 0) { + hideServiceNotification(); + } else { + showServiceNotification(); + } + } + + int notificationTextId; + int inconId; switch (call.getState()) { case Released: case End: - if (!displayServiceNotification()) { - stopForegroundCompat(notif.notificationId); - } else { - mNM.cancel(notif.notificationId); - } + mNM.cancel(notif.notificationId); mCallNotifMap.remove(addressAsString); - break; + return; case Paused: case PausedByRemote: case Pausing: @@ -558,17 +561,13 @@ public final class LinphoneService extends Service { } String name = LinphoneUtils.getAddressDisplayName(address); - boolean showActions = call.getState() == State.IncomingReceived || call.getState() == State.IncomingEarlyMedia; + boolean showAnswerAction = call.getState() == State.IncomingReceived || call.getState() == State.IncomingEarlyMedia; Intent notifIntent = new Intent(this, incomingReceivedActivity); notifIntent.putExtra("Notification", true); mNotifContentIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT); - Notification notification = Compatibility.createInCallNotification(getApplicationContext(), notif.notificationId, showActions, mNotificationTitle, getString(notificationTextId), inconId, bm, name, mNotifContentIntent); + Notification notification = Compatibility.createInCallNotification(getApplicationContext(), notif.notificationId, showAnswerAction, mNotificationTitle, getString(notificationTextId), inconId, bm, name, mNotifContentIntent); - if (!displayServiceNotification()) { - startForegroundCompat(notif.notificationId, notification); - } else { - notifyWrapper(notif.notificationId, notification); - } + notifyWrapper(notif.notificationId, notification); } public String getSipUriForCallNotificationId(int notificationId) { @@ -734,8 +733,6 @@ public final class LinphoneService extends Service { invokeMethod(mStartForeground, mStartForegroundArgs); return; } - - notifyWrapper(id, notification); } /** @@ -749,10 +746,6 @@ public final class LinphoneService extends Service { invokeMethod(mStopForeground, mStopForegroundArgs); return; } - - // Fall back on the old API. Note to cancel BEFORE changing the - // foreground state, since we could be killed at that point. - mNM.cancel(id); } private void dumpDeviceInformation() { diff --git a/app/src/main/java/org/linphone/compatibility/ApiTwentyFourPlus.java b/app/src/main/java/org/linphone/compatibility/ApiTwentyFourPlus.java index 7abea8c4f..2d98b393b 100644 --- a/app/src/main/java/org/linphone/compatibility/ApiTwentyFourPlus.java +++ b/app/src/main/java/org/linphone/compatibility/ApiTwentyFourPlus.java @@ -96,7 +96,14 @@ public class ApiTwentyFourPlus { } public static Notification createInCallNotification(Context context, - int callId, boolean showActions, String msg, int iconID, Bitmap contactIcon, String contactName, PendingIntent intent) { + int callId, boolean showAnswerAction, String msg, int iconID, Bitmap contactIcon, String contactName, PendingIntent intent) { + + Intent hangupIntent = new Intent(context, NotificationBroadcastReceiver.class); + hangupIntent.setAction(INTENT_HANGUP_CALL_NOTIF_ACTION); + hangupIntent.putExtra(INTENT_CALL_ID, callId); + + PendingIntent hangupPendingIntent = PendingIntent.getBroadcast(context, + callId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(context) .setContentTitle(contactName) @@ -110,16 +117,10 @@ public class ApiTwentyFourPlus { .setPriority(Notification.PRIORITY_HIGH) .setWhen(System.currentTimeMillis()) .setShowWhen(true) - .setColor(context.getColor(R.color.notification_color_led)); - - if (showActions) { - Intent hangupIntent = new Intent(context, NotificationBroadcastReceiver.class); - hangupIntent.setAction(INTENT_HANGUP_CALL_NOTIF_ACTION); - hangupIntent.putExtra(INTENT_CALL_ID, callId); - - PendingIntent hangupPendingIntent = PendingIntent.getBroadcast(context, - callId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT); + .setColor(context.getColor(R.color.notification_color_led)) + .addAction(R.drawable.call_hangup, context.getString(R.string.notification_call_hangup_label), hangupPendingIntent); + if (showAnswerAction) { Intent answerIntent = new Intent(context, NotificationBroadcastReceiver.class); answerIntent.setAction(INTENT_ANSWER_CALL_NOTIF_ACTION); answerIntent.putExtra(INTENT_CALL_ID, callId); @@ -127,7 +128,6 @@ public class ApiTwentyFourPlus { PendingIntent answerPendingIntent = PendingIntent.getBroadcast(context, callId, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT); - builder.addAction(R.drawable.call_hangup, context.getString(R.string.notification_call_hangup_label), hangupPendingIntent); builder.addAction(R.drawable.call_audio_start, context.getString(R.string.notification_call_answer_label), answerPendingIntent); } diff --git a/app/src/main/java/org/linphone/compatibility/ApiTwentySixPlus.java b/app/src/main/java/org/linphone/compatibility/ApiTwentySixPlus.java index 7e549c1e9..4dd09122f 100644 --- a/app/src/main/java/org/linphone/compatibility/ApiTwentySixPlus.java +++ b/app/src/main/java/org/linphone/compatibility/ApiTwentySixPlus.java @@ -127,31 +127,31 @@ public class ApiTwentySixPlus { } public static Notification createInCallNotification(Context context, - int callId, boolean showActions, String msg, int iconID, Bitmap contactIcon, String contactName, PendingIntent intent) { + int callId, boolean showAnswerAction, String msg, int iconID, Bitmap contactIcon, String contactName, PendingIntent intent) { + + Intent hangupIntent = new Intent(context, NotificationBroadcastReceiver.class); + hangupIntent.setAction(INTENT_HANGUP_CALL_NOTIF_ACTION); + hangupIntent.putExtra(INTENT_CALL_ID, callId); + + PendingIntent hangupPendingIntent = PendingIntent.getBroadcast(context, + callId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(context, context.getString(R.string.notification_service_channel_id)) - .setContentTitle(contactName) - .setContentText(msg) - .setSmallIcon(iconID) - .setAutoCancel(false) - .setContentIntent(intent) - .setLargeIcon(contactIcon) - .setCategory(Notification.CATEGORY_CALL) - .setVisibility(Notification.VISIBILITY_PUBLIC) - .setPriority(Notification.PRIORITY_HIGH) - .setWhen(System.currentTimeMillis()) - .setShowWhen(true) - .setColor(context.getColor(R.color.notification_color_led)); - - if (showActions) { - - Intent hangupIntent = new Intent(context, NotificationBroadcastReceiver.class); - hangupIntent.setAction(INTENT_HANGUP_CALL_NOTIF_ACTION); - hangupIntent.putExtra(INTENT_CALL_ID, callId); - - PendingIntent hangupPendingIntent = PendingIntent.getBroadcast(context, - callId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT); + .setContentTitle(contactName) + .setContentText(msg) + .setSmallIcon(iconID) + .setAutoCancel(false) + .setContentIntent(intent) + .setLargeIcon(contactIcon) + .setCategory(Notification.CATEGORY_CALL) + .setVisibility(Notification.VISIBILITY_PUBLIC) + .setPriority(Notification.PRIORITY_HIGH) + .setWhen(System.currentTimeMillis()) + .setShowWhen(true) + .setColor(context.getColor(R.color.notification_color_led)) + .addAction(R.drawable.call_hangup, context.getString(R.string.notification_call_hangup_label), hangupPendingIntent); + if (showAnswerAction) { Intent answerIntent = new Intent(context, NotificationBroadcastReceiver.class); answerIntent.setAction(INTENT_ANSWER_CALL_NOTIF_ACTION); answerIntent.putExtra(INTENT_CALL_ID, callId); @@ -159,7 +159,6 @@ public class ApiTwentySixPlus { PendingIntent answerPendingIntent = PendingIntent.getBroadcast(context, callId, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT); - builder.addAction(R.drawable.call_hangup, context.getString(R.string.notification_call_hangup_label), hangupPendingIntent); builder.addAction(R.drawable.call_audio_start, context.getString(R.string.notification_call_answer_label), answerPendingIntent); } return builder.build(); diff --git a/app/src/main/java/org/linphone/compatibility/Compatibility.java b/app/src/main/java/org/linphone/compatibility/Compatibility.java index aaa055d4e..522487f98 100644 --- a/app/src/main/java/org/linphone/compatibility/Compatibility.java +++ b/app/src/main/java/org/linphone/compatibility/Compatibility.java @@ -98,11 +98,11 @@ public class Compatibility { return null; } - public static Notification createInCallNotification(Context context, int callId, boolean showActions, String title, String msg, int iconID, Bitmap contactIcon, String contactName, PendingIntent intent) { + public static Notification createInCallNotification(Context context, int callId, boolean showAnswerAction, String title, String msg, int iconID, Bitmap contactIcon, String contactName, PendingIntent intent) { if (Version.sdkAboveOrEqual(Version.API26_O_80)) { - return ApiTwentySixPlus.createInCallNotification(context, callId, showActions, msg, iconID, contactIcon, contactName, intent); + return ApiTwentySixPlus.createInCallNotification(context, callId, showAnswerAction, msg, iconID, contactIcon, contactName, intent); } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) { - return ApiTwentyFourPlus.createInCallNotification(context, callId, showActions, msg, iconID, contactIcon, contactName, intent); + return ApiTwentyFourPlus.createInCallNotification(context, callId, showAnswerAction, msg, iconID, contactIcon, contactName, intent); } else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) { return ApiTwentyOnePlus.createInCallNotification(context, title, msg, iconID, contactIcon, contactName, intent); } else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) { diff --git a/app/src/main/java/org/linphone/receivers/NotificationBroadcastReceiver.java b/app/src/main/java/org/linphone/receivers/NotificationBroadcastReceiver.java index e13d4eaf7..746e624b5 100644 --- a/app/src/main/java/org/linphone/receivers/NotificationBroadcastReceiver.java +++ b/app/src/main/java/org/linphone/receivers/NotificationBroadcastReceiver.java @@ -76,7 +76,7 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { if (intent.getAction() == Compatibility.INTENT_ANSWER_CALL_NOTIF_ACTION) { call.accept(); } else { - call.decline(Reason.None); + call.terminate(); } } }