From 15053ace61158698fa7bcf5cff685d3c55ef2d05 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 12 Dec 2018 14:29:32 +0100 Subject: [PATCH] Added mark as read notification action for chat messages + refactoring --- .../compatibility/ApiTwentyEightPlus.java | 34 +---- .../compatibility/ApiTwentyFourPlus.java | 143 ++++++++++++------ .../compatibility/ApiTwentySixPlus.java | 61 +------- .../linphone/compatibility/Compatibility.java | 1 + .../NotificationBroadcastReceiver.java | 74 +++++---- .../notifications/NotificationsManager.java | 4 + app/src/main/res/values/strings.xml | 1 + 7 files changed, 150 insertions(+), 168 deletions(-) diff --git a/app/src/main/java/org/linphone/compatibility/ApiTwentyEightPlus.java b/app/src/main/java/org/linphone/compatibility/ApiTwentyEightPlus.java index 37c736d0c..730789250 100644 --- a/app/src/main/java/org/linphone/compatibility/ApiTwentyEightPlus.java +++ b/app/src/main/java/org/linphone/compatibility/ApiTwentyEightPlus.java @@ -20,53 +20,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import static org.linphone.compatibility.Compatibility.CHAT_NOTIFICATIONS_GROUP; -import static org.linphone.compatibility.Compatibility.INTENT_LOCAL_IDENTITY; -import static org.linphone.compatibility.Compatibility.INTENT_NOTIF_ID; -import static org.linphone.compatibility.Compatibility.INTENT_REPLY_NOTIF_ACTION; -import static org.linphone.compatibility.Compatibility.KEY_TEXT_REPLY; import android.annotation.TargetApi; import android.app.Notification; import android.app.PendingIntent; import android.app.Person; -import android.app.RemoteInput; import android.content.Context; -import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.Icon; import org.linphone.R; import org.linphone.notifications.Notifiable; import org.linphone.notifications.NotifiableMessage; -import org.linphone.notifications.NotificationBroadcastReceiver; @TargetApi(28) class ApiTwentyEightPlus { public static Notification createMessageNotification( Context context, Notifiable notif, Bitmap contactIcon, PendingIntent intent) { - String replyLabel = context.getResources().getString(R.string.notification_reply_label); - RemoteInput remoteInput = - new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel(replyLabel).build(); - - Intent replyIntent = new Intent(context, NotificationBroadcastReceiver.class); - replyIntent.setAction(INTENT_REPLY_NOTIF_ACTION); - replyIntent.putExtra(INTENT_NOTIF_ID, notif.getNotificationId()); - replyIntent.putExtra(INTENT_LOCAL_IDENTITY, notif.getLocalIdentity()); - - PendingIntent replyPendingIntent = - PendingIntent.getBroadcast( - context, - notif.getNotificationId(), - replyIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - - Notification.Action action = - new Notification.Action.Builder( - R.drawable.chat_send_over, - context.getString(R.string.notification_reply_label), - replyPendingIntent) - .addRemoteInput(remoteInput) - .setAllowGeneratedReplies(true) - .build(); Person me = new Person.Builder().setName(notif.getMyself()).build(); Notification.MessagingStyle style = new Notification.MessagingStyle(me); @@ -105,7 +74,8 @@ class ApiTwentyEightPlus { .setShowWhen(true) .setColor(context.getColor(R.color.notification_color_led)) .setStyle(style) - .addAction(action) + .addAction(ApiTwentyFourPlus.getReplyMessageAction(context, notif)) + .addAction(ApiTwentyFourPlus.getMarkMessageAsReadAction(context, notif)) .build(); } } diff --git a/app/src/main/java/org/linphone/compatibility/ApiTwentyFourPlus.java b/app/src/main/java/org/linphone/compatibility/ApiTwentyFourPlus.java index c4f3c7d31..7e7242722 100644 --- a/app/src/main/java/org/linphone/compatibility/ApiTwentyFourPlus.java +++ b/app/src/main/java/org/linphone/compatibility/ApiTwentyFourPlus.java @@ -23,6 +23,7 @@ import static org.linphone.compatibility.Compatibility.CHAT_NOTIFICATIONS_GROUP; import static org.linphone.compatibility.Compatibility.INTENT_ANSWER_CALL_NOTIF_ACTION; import static org.linphone.compatibility.Compatibility.INTENT_HANGUP_CALL_NOTIF_ACTION; import static org.linphone.compatibility.Compatibility.INTENT_LOCAL_IDENTITY; +import static org.linphone.compatibility.Compatibility.INTENT_MARK_AS_READ_ACTION; import static org.linphone.compatibility.Compatibility.INTENT_NOTIF_ID; import static org.linphone.compatibility.Compatibility.INTENT_REPLY_NOTIF_ACTION; import static org.linphone.compatibility.Compatibility.KEY_TEXT_REPLY; @@ -52,30 +53,6 @@ class ApiTwentyFourPlus { public static Notification createMessageNotification( Context context, Notifiable notif, Bitmap contactIcon, PendingIntent intent) { - String replyLabel = context.getResources().getString(R.string.notification_reply_label); - RemoteInput remoteInput = - new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel(replyLabel).build(); - - Intent replyIntent = new Intent(context, NotificationBroadcastReceiver.class); - replyIntent.setAction(INTENT_REPLY_NOTIF_ACTION); - replyIntent.putExtra(INTENT_NOTIF_ID, notif.getNotificationId()); - replyIntent.putExtra(INTENT_LOCAL_IDENTITY, notif.getLocalIdentity()); - - PendingIntent replyPendingIntent = - PendingIntent.getBroadcast( - context, - notif.getNotificationId(), - replyIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - - Notification.Action action = - new Notification.Action.Builder( - R.drawable.chat_send_over, - context.getString(R.string.notification_reply_label), - replyPendingIntent) - .addRemoteInput(remoteInput) - .setAllowGeneratedReplies(true) - .build(); Notification.MessagingStyle style = new Notification.MessagingStyle(notif.getMyself()); for (NotifiableMessage message : notif.getMessages()) { @@ -108,7 +85,8 @@ class ApiTwentyFourPlus { .setShowWhen(true) .setColor(context.getColor(R.color.notification_color_led)) .setStyle(style) - .addAction(action) + .addAction(getReplyMessageAction(context, notif)) + .addAction(getMarkMessageAsReadAction(context, notif)) .build(); } @@ -122,14 +100,6 @@ class ApiTwentyFourPlus { String contactName, PendingIntent intent) { - Intent hangupIntent = new Intent(context, NotificationBroadcastReceiver.class); - hangupIntent.setAction(INTENT_HANGUP_CALL_NOTIF_ACTION); - hangupIntent.putExtra(INTENT_NOTIF_ID, callId); - - PendingIntent hangupPendingIntent = - PendingIntent.getBroadcast( - context, callId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT); - Notification.Builder builder = new Notification.Builder(context) .setContentTitle(contactName) @@ -144,26 +114,101 @@ class ApiTwentyFourPlus { .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); + .addAction(getCallDeclineAction(context, callId)); if (showAnswerAction) { - Intent answerIntent = new Intent(context, NotificationBroadcastReceiver.class); - answerIntent.setAction(INTENT_ANSWER_CALL_NOTIF_ACTION); - answerIntent.putExtra(INTENT_NOTIF_ID, callId); - - PendingIntent answerPendingIntent = - PendingIntent.getBroadcast( - context, callId, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT); - - builder.addAction( - R.drawable.call_audio_start, - context.getString(R.string.notification_call_answer_label), - answerPendingIntent); + builder.addAction(getCallAnswerAction(context, callId)); } return builder.build(); } + + public static Notification.Action getReplyMessageAction(Context context, Notifiable notif) { + String replyLabel = context.getResources().getString(R.string.notification_reply_label); + RemoteInput remoteInput = + new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel(replyLabel).build(); + + Intent replyIntent = new Intent(context, NotificationBroadcastReceiver.class); + replyIntent.setAction(INTENT_REPLY_NOTIF_ACTION); + replyIntent.putExtra(INTENT_NOTIF_ID, notif.getNotificationId()); + replyIntent.putExtra(INTENT_LOCAL_IDENTITY, notif.getLocalIdentity()); + + PendingIntent replyPendingIntent = + PendingIntent.getBroadcast( + context, + notif.getNotificationId(), + replyIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + + Notification.Action replyAction = + new Notification.Action.Builder( + R.drawable.chat_send_over, + context.getString(R.string.notification_reply_label), + replyPendingIntent) + .addRemoteInput(remoteInput) + .setAllowGeneratedReplies(true) + .build(); + return replyAction; + } + + public static Notification.Action getMarkMessageAsReadAction( + Context context, Notifiable notif) { + Intent markAsReadIntent = new Intent(context, NotificationBroadcastReceiver.class); + markAsReadIntent.setAction(INTENT_MARK_AS_READ_ACTION); + markAsReadIntent.putExtra(INTENT_NOTIF_ID, notif.getNotificationId()); + markAsReadIntent.putExtra(INTENT_LOCAL_IDENTITY, notif.getLocalIdentity()); + + PendingIntent markAsReadPendingIntent = + PendingIntent.getBroadcast( + context, + notif.getNotificationId(), + markAsReadIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + + Notification.Action markAsReadAction = + new Notification.Action.Builder( + R.drawable.chat_send_over, + context.getString(R.string.notification_mark_as_read_label), + markAsReadPendingIntent) + .build(); + + return markAsReadAction; + } + + public static Notification.Action getCallAnswerAction(Context context, int callId) { + Intent answerIntent = new Intent(context, NotificationBroadcastReceiver.class); + answerIntent.setAction(INTENT_ANSWER_CALL_NOTIF_ACTION); + answerIntent.putExtra(INTENT_NOTIF_ID, callId); + + PendingIntent answerPendingIntent = + PendingIntent.getBroadcast( + context, callId, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT); + + Notification.Action answerAction = + new Notification.Action.Builder( + R.drawable.call_audio_start, + context.getString(R.string.notification_call_answer_label), + answerPendingIntent) + .build(); + + return answerAction; + } + + public static Notification.Action getCallDeclineAction(Context context, int callId) { + Intent hangupIntent = new Intent(context, NotificationBroadcastReceiver.class); + hangupIntent.setAction(INTENT_HANGUP_CALL_NOTIF_ACTION); + hangupIntent.putExtra(INTENT_NOTIF_ID, callId); + + PendingIntent hangupPendingIntent = + PendingIntent.getBroadcast( + context, callId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT); + + Notification.Action declineAction = + new Notification.Action.Builder( + R.drawable.call_hangup, + context.getString(R.string.notification_call_hangup_label), + hangupPendingIntent) + .build(); + return declineAction; + } } diff --git a/app/src/main/java/org/linphone/compatibility/ApiTwentySixPlus.java b/app/src/main/java/org/linphone/compatibility/ApiTwentySixPlus.java index e25780428..a632839a0 100644 --- a/app/src/main/java/org/linphone/compatibility/ApiTwentySixPlus.java +++ b/app/src/main/java/org/linphone/compatibility/ApiTwentySixPlus.java @@ -20,12 +20,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import static org.linphone.compatibility.Compatibility.CHAT_NOTIFICATIONS_GROUP; -import static org.linphone.compatibility.Compatibility.INTENT_ANSWER_CALL_NOTIF_ACTION; -import static org.linphone.compatibility.Compatibility.INTENT_HANGUP_CALL_NOTIF_ACTION; -import static org.linphone.compatibility.Compatibility.INTENT_LOCAL_IDENTITY; -import static org.linphone.compatibility.Compatibility.INTENT_NOTIF_ID; -import static org.linphone.compatibility.Compatibility.INTENT_REPLY_NOTIF_ACTION; -import static org.linphone.compatibility.Compatibility.KEY_TEXT_REPLY; import android.annotation.TargetApi; import android.app.FragmentTransaction; @@ -33,7 +27,6 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; -import android.app.RemoteInput; import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.content.Intent; @@ -43,7 +36,6 @@ import android.provider.Settings; import org.linphone.R; import org.linphone.notifications.Notifiable; import org.linphone.notifications.NotifiableMessage; -import org.linphone.notifications.NotificationBroadcastReceiver; @TargetApi(26) class ApiTwentySixPlus { @@ -107,30 +99,6 @@ class ApiTwentySixPlus { public static Notification createMessageNotification( Context context, Notifiable notif, Bitmap contactIcon, PendingIntent intent) { - String replyLabel = context.getResources().getString(R.string.notification_reply_label); - RemoteInput remoteInput = - new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel(replyLabel).build(); - - Intent replyIntent = new Intent(context, NotificationBroadcastReceiver.class); - replyIntent.setAction(INTENT_REPLY_NOTIF_ACTION); - replyIntent.putExtra(INTENT_NOTIF_ID, notif.getNotificationId()); - replyIntent.putExtra(INTENT_LOCAL_IDENTITY, notif.getLocalIdentity()); - - PendingIntent replyPendingIntent = - PendingIntent.getBroadcast( - context, - notif.getNotificationId(), - replyIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - - Notification.Action action = - new Notification.Action.Builder( - R.drawable.chat_send_over, - context.getString(R.string.notification_reply_label), - replyPendingIntent) - .addRemoteInput(remoteInput) - .setAllowGeneratedReplies(true) - .build(); Notification.MessagingStyle style = new Notification.MessagingStyle(notif.getMyself()); for (NotifiableMessage message : notif.getMessages()) { @@ -164,7 +132,8 @@ class ApiTwentySixPlus { .setShowWhen(true) .setColor(context.getColor(R.color.notification_color_led)) .setStyle(style) - .addAction(action) + .addAction(ApiTwentyFourPlus.getReplyMessageAction(context, notif)) + .addAction(ApiTwentyFourPlus.getMarkMessageAsReadAction(context, notif)) .build(); } @@ -178,14 +147,6 @@ class ApiTwentySixPlus { String contactName, PendingIntent intent) { - Intent hangupIntent = new Intent(context, NotificationBroadcastReceiver.class); - hangupIntent.setAction(INTENT_HANGUP_CALL_NOTIF_ACTION); - hangupIntent.putExtra(INTENT_NOTIF_ID, callId); - - PendingIntent hangupPendingIntent = - PendingIntent.getBroadcast( - context, callId, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT); - Notification.Builder builder = new Notification.Builder( context, @@ -202,24 +163,10 @@ class ApiTwentySixPlus { .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); + .addAction(ApiTwentyFourPlus.getCallDeclineAction(context, callId)); if (showAnswerAction) { - Intent answerIntent = new Intent(context, NotificationBroadcastReceiver.class); - answerIntent.setAction(INTENT_ANSWER_CALL_NOTIF_ACTION); - answerIntent.putExtra(INTENT_NOTIF_ID, callId); - - PendingIntent answerPendingIntent = - PendingIntent.getBroadcast( - context, callId, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT); - - builder.addAction( - R.drawable.call_audio_start, - context.getString(R.string.notification_call_answer_label), - answerPendingIntent); + builder.addAction(ApiTwentyFourPlus.getCallAnswerAction(context, callId)); } 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 d72159a5c..f811d56e3 100644 --- a/app/src/main/java/org/linphone/compatibility/Compatibility.java +++ b/app/src/main/java/org/linphone/compatibility/Compatibility.java @@ -39,6 +39,7 @@ public class Compatibility { public static final String INTENT_HANGUP_CALL_NOTIF_ACTION = "org.linphone.HANGUP_CALL_ACTION"; public static final String INTENT_ANSWER_CALL_NOTIF_ACTION = "org.linphone.ANSWER_CALL_ACTION"; public static final String INTENT_LOCAL_IDENTITY = "LOCAL_IDENTITY"; + public static final String INTENT_MARK_AS_READ_ACTION = "org.linphone.MARK_AS_READ_ACTION"; public static String getDeviceName(Context context) { if (Version.sdkAboveOrEqual(25)) { diff --git a/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.java b/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.java index f40beb306..1cdb13c95 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.java +++ b/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.java @@ -45,13 +45,8 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { final int notifId = intent.getIntExtra(Compatibility.INTENT_NOTIF_ID, 0); final String localyIdentity = intent.getStringExtra(Compatibility.INTENT_LOCAL_IDENTITY); - if (intent.getAction().equals(Compatibility.INTENT_REPLY_NOTIF_ACTION)) { - final String reply = getMessageText(intent).toString(); - if (reply == null) { - Log.e("Couldn't get reply text"); - onError(context, notifId); - return; - } + if (intent.getAction().equals(Compatibility.INTENT_REPLY_NOTIF_ACTION) + || intent.getAction().equals(Compatibility.INTENT_MARK_AS_READ_ACTION)) { String remoteSipAddr = LinphoneService.instance() .getNotificationManager() @@ -59,21 +54,25 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { Core core = LinphoneManager.getLc(); if (core == null) { - Log.e("Couldn't get Core instance"); + Log.e("[Notification Broadcast Receiver] Couldn't get Core instance"); onError(context, notifId); return; } Address remoteAddr = core.interpretUrl(remoteSipAddr); if (remoteAddr == null) { - Log.e("Couldn't interpret remote address " + remoteSipAddr); + Log.e( + "[Notification Broadcast Receiver] Couldn't interpret remote address " + + remoteSipAddr); onError(context, notifId); return; } Address localAddr = core.interpretUrl(localyIdentity); if (localAddr == null) { - Log.e("Couldn't interpret local address " + localyIdentity); + Log.e( + "[Notification Broadcast Receiver] Couldn't interpret local address " + + localyIdentity); onError(context, notifId); return; } @@ -81,7 +80,7 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { ChatRoom room = core.findChatRoom(remoteAddr, localAddr); if (room == null) { Log.e( - "Couldn't find chat room for remote address " + "[Notification Broadcast Receiver] Couldn't find chat room for remote address " + remoteSipAddr + " and local address " + localyIdentity); @@ -95,24 +94,37 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { .displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount()); } - ChatMessage msg = room.createMessage(reply); - msg.send(); - msg.setListener( - new ChatMessageListenerStub() { - @Override - public void onMsgStateChanged(ChatMessage msg, ChatMessage.State state) { - if (state == ChatMessage.State.Delivered) { - Notification replied = - Compatibility.createRepliedNotification(context, reply); - LinphoneService.instance() - .getNotificationManager() - .sendNotification(notifId, replied); - } else if (state == ChatMessage.State.NotDelivered) { - Log.e("Couldn't send reply, message is not delivered"); - onError(context, notifId); + if (intent.getAction().equals(Compatibility.INTENT_REPLY_NOTIF_ACTION)) { + final String reply = getMessageText(intent).toString(); + if (reply == null) { + Log.e("[Notification Broadcast Receiver] Couldn't get reply text"); + onError(context, notifId); + return; + } + + ChatMessage msg = room.createMessage(reply); + msg.send(); + msg.setListener( + new ChatMessageListenerStub() { + @Override + public void onMsgStateChanged( + ChatMessage msg, ChatMessage.State state) { + if (state == ChatMessage.State.Delivered) { + Notification replied = + Compatibility.createRepliedNotification(context, reply); + LinphoneService.instance() + .getNotificationManager() + .sendNotification(notifId, replied); + } else if (state == ChatMessage.State.NotDelivered) { + Log.e( + "[Notification Broadcast Receiver] Couldn't send reply, message is not delivered"); + onError(context, notifId); + } } - } - }); + }); + } else { + LinphoneService.instance().getNotificationManager().dismissNotification(notifId); + } } else if (intent.getAction().equals(Compatibility.INTENT_ANSWER_CALL_NOTIF_ACTION) || intent.getAction().equals(Compatibility.INTENT_HANGUP_CALL_NOTIF_ACTION)) { String remoteAddr = @@ -122,12 +134,14 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver { Core core = LinphoneManager.getLc(); if (core == null) { - Log.e("Couldn't get Core instance"); + Log.e("[Notification Broadcast Receiver] Couldn't get Core instance"); return; } Call call = core.findCallFromUri(remoteAddr); if (call == null) { - Log.e("Couldn't find call from remote address " + remoteAddr); + Log.e( + "[Notification Broadcast Receiver] Couldn't find call from remote address " + + remoteAddr); return; } diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.java b/app/src/main/java/org/linphone/notifications/NotificationsManager.java index 5ba10b9e1..b243f1e27 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.java +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.java @@ -361,4 +361,8 @@ public class NotificationsManager { pendingIntent); sendNotification(IN_APP_NOTIF_ID, notif); } + + public void dismissNotification(int notifId) { + mNM.cancel(notifId); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b77895331..52df750bb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -21,6 +21,7 @@ Sent reply: %s Hangup Answer + Mark as read Username