From f94ad81a5d7d32c98f8ffc9a47c87fc170d1393f Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 2 Nov 2017 12:08:31 +0100 Subject: [PATCH] Added group chat message notification + some cleanup in notification code --- res/values/strings.xml | 1 + src/android/org/linphone/LinphoneManager.java | 23 +++++----- src/android/org/linphone/LinphoneService.java | 42 +++++++++++++++---- .../linphone/chat/ChatCreationFragment.java | 1 + .../org/linphone/chat/ChatEventsAdapter.java | 2 +- .../org/linphone/chat/GroupChatFragment.java | 9 ++-- .../linphone/compatibility/Compatibility.java | 2 +- 7 files changed, 54 insertions(+), 26 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 5ae4cf95f..9fc3a3d6a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -208,6 +208,7 @@ new subject : %s %s is admin %s is no longer admin + %1: %2 Registered diff --git a/src/android/org/linphone/LinphoneManager.java b/src/android/org/linphone/LinphoneManager.java index ec97b36e8..e903a3921 100644 --- a/src/android/org/linphone/LinphoneManager.java +++ b/src/android/org/linphone/LinphoneManager.java @@ -1229,21 +1229,24 @@ public class LinphoneManager implements CoreListener, ChatMessageListener, Senso } Address from = message.getFromAddress(); - String to = message.getToAddress().asString(); + LinphoneContact contact = ContactsManager.getInstance().findContactFromAddress(from); + String textMessage = (message.getFileTransferInformation() != null) ? getString(R.string.content_description_incoming_file) : message.getText(); - String textMessage = (message.getFileTransferInformation() != null) ? - getString(R.string.content_description_incoming_file) : message.getText(); - try { - LinphoneContact contact = ContactsManager.getInstance().findContactFromAddress(from); - if (!mServiceContext.getResources().getBoolean(R.bool.disable_chat_message_notification)) { + if (!mServiceContext.getResources().getBoolean(R.bool.disable_chat_message_notification)) { + if (cr.canHandleParticipants()) { + String subject = cr.getSubject(); if (contact != null) { - LinphoneService.instance().displayMessageNotification(to, from.asStringUriOnly(), contact.getFullName(), textMessage); + LinphoneService.instance().displayGroupChatMessageNotification(subject, cr.getConferenceAddress().asString(), contact.getFullName(), contact.getThumbnailUri(), textMessage); } else { - LinphoneService.instance().displayMessageNotification(to, from.asStringUriOnly(), from.getUsername(), textMessage); + LinphoneService.instance().displayGroupChatMessageNotification(subject, cr.getConferenceAddress().asString(), from.getUsername(), null, textMessage); + } + } else { + if (contact != null) { + LinphoneService.instance().displayMessageNotification(from.asStringUriOnly(), contact.getFullName(), contact.getThumbnailUri(), textMessage); + } else { + LinphoneService.instance().displayMessageNotification(from.asStringUriOnly(), from.getUsername(), null, textMessage); } } - } catch (Exception e) { - Log.e(e); } } diff --git a/src/android/org/linphone/LinphoneService.java b/src/android/org/linphone/LinphoneService.java index 288accbb7..e70b0d1dd 100644 --- a/src/android/org/linphone/LinphoneService.java +++ b/src/android/org/linphone/LinphoneService.java @@ -595,7 +595,36 @@ public final class LinphoneService extends Service { resetIntentLaunchedOnNotificationClick(); } - public void displayMessageNotification(String to, String fromSipUri, String fromName, String message) { + public void displayGroupChatMessageNotification(String subject, String conferenceAddress, String fromName, Uri fromPictureUri, String message) { + Intent notifIntent = new Intent(this, LinphoneActivity.class); + notifIntent.putExtra("GoToChat", true); + notifIntent.putExtra("ChatContactSipUri", conferenceAddress); + + PendingIntent notifContentIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT); + + if (mMsgNotif == null) { + mMsgNotifCount = 1; + } else { + mMsgNotifCount++; + } + + Bitmap bm = null; + if (fromPictureUri != null) { + try { + bm = MediaStore.Images.Media.getBitmap(getContentResolver(), fromPictureUri); + } catch (Exception e) { + bm = BitmapFactory.decodeResource(getResources(), R.drawable.topbar_avatar); + } + } else { + bm = BitmapFactory.decodeResource(getResources(), R.drawable.topbar_avatar); + } + mMsgNotif = Compatibility.createMessageNotification(getApplicationContext(), mMsgNotifCount, subject, + getString(R.string.group_chat_notif).replace("%1", fromName).replace("%2", message), bm, notifContentIntent); + + notifyWrapper(MESSAGE_NOTIF_ID, mMsgNotif); + } + + public void displayMessageNotification(String fromSipUri, String fromName, Uri fromPictureUri, String message) { Intent notifIntent = new Intent(this, LinphoneActivity.class); notifIntent.putExtra("GoToChat", true); notifIntent.putExtra("ChatContactSipUri", fromSipUri); @@ -612,22 +641,17 @@ public final class LinphoneService extends Service { mMsgNotifCount++; } - Uri pictureUri = null; - LinphoneContact contact = ContactsManager.getInstance().findContactFromAddress(Factory.instance().createAddress(fromSipUri)); - if (contact != null) - pictureUri = contact.getThumbnailUri(); - Bitmap bm = null; - if (pictureUri != null) { + if (fromPictureUri != null) { try { - bm = MediaStore.Images.Media.getBitmap(getContentResolver(), pictureUri); + bm = MediaStore.Images.Media.getBitmap(getContentResolver(), fromPictureUri); } catch (Exception e) { bm = BitmapFactory.decodeResource(getResources(), R.drawable.topbar_avatar); } } else { bm = BitmapFactory.decodeResource(getResources(), R.drawable.topbar_avatar); } - mMsgNotif = Compatibility.createMessageNotification(getApplicationContext(), mMsgNotifCount, to, fromName, message, bm, notifContentIntent); + mMsgNotif = Compatibility.createMessageNotification(getApplicationContext(), mMsgNotifCount, fromName, message, bm, notifContentIntent); notifyWrapper(MESSAGE_NOTIF_ID, mMsgNotif); } diff --git a/src/android/org/linphone/chat/ChatCreationFragment.java b/src/android/org/linphone/chat/ChatCreationFragment.java index f91612eb4..de6d1363e 100644 --- a/src/android/org/linphone/chat/ChatCreationFragment.java +++ b/src/android/org/linphone/chat/ChatCreationFragment.java @@ -278,6 +278,7 @@ public class ChatCreationFragment extends Fragment implements View.OnClickListen if (contactsSelected.size() == 1) { contactsSelectedLayout.removeAllViews(); LinphoneActivity.instance().displayChat(contactsSelected.get(0).getAddress(), "", ""); + //TODO create group chat room with only two participants ? } else { contactsSelectedLayout.removeAllViews(); LinphoneActivity.instance().goToChatGroupInfos(contactsSelected, null, false, true); diff --git a/src/android/org/linphone/chat/ChatEventsAdapter.java b/src/android/org/linphone/chat/ChatEventsAdapter.java index 9c009190f..eecfc13b1 100644 --- a/src/android/org/linphone/chat/ChatEventsAdapter.java +++ b/src/android/org/linphone/chat/ChatEventsAdapter.java @@ -195,7 +195,7 @@ public class ChatEventsAdapter extends BaseAdapter implements ChatMessageListene } else { LinphoneContact contact = null; for (LinphoneContact c : mParticipants) { - if (contact.hasAddress(remoteSender.asStringUriOnly())) { + if (c != null && c.hasAddress(remoteSender.asStringUriOnly())) { contact = c; break; } diff --git a/src/android/org/linphone/chat/GroupChatFragment.java b/src/android/org/linphone/chat/GroupChatFragment.java index b1393e747..719ae3326 100644 --- a/src/android/org/linphone/chat/GroupChatFragment.java +++ b/src/android/org/linphone/chat/GroupChatFragment.java @@ -68,7 +68,6 @@ import org.linphone.core.Factory; import org.linphone.core.Friend; import org.linphone.core.FriendList; import org.linphone.core.Participant; -import org.linphone.mediastream.Log; import org.linphone.receivers.ContactsUpdatedListener; import java.io.File; @@ -625,11 +624,11 @@ public class GroupChatFragment extends Fragment implements ChatRoomListener, Con if (!getResources().getBoolean(R.bool.disable_chat_message_notification)) { String to = msg.getToAddress().asString(); if (contact != null) { - LinphoneService.instance().displayMessageNotification(to, from.asStringUriOnly(), - contact.getFullName(), getString(R.string.message_cant_be_decrypted_notif)); + LinphoneService.instance().displayMessageNotification(from.asStringUriOnly(), + contact.getFullName(), contact.getThumbnailUri(), getString(R.string.message_cant_be_decrypted_notif)); } else { - LinphoneService.instance().displayMessageNotification(to, from.asStringUriOnly(), - from.getUsername(), getString(R.string.message_cant_be_decrypted_notif)); + LinphoneService.instance().displayMessageNotification(from.asStringUriOnly(), + from.getUsername(), null, getString(R.string.message_cant_be_decrypted_notif)); } } } else if (LinphoneManager.getLc().limeEnabled() == Core.LimeState.Mandatory) { diff --git a/src/android/org/linphone/compatibility/Compatibility.java b/src/android/org/linphone/compatibility/Compatibility.java index 171bf5444..f24240809 100644 --- a/src/android/org/linphone/compatibility/Compatibility.java +++ b/src/android/org/linphone/compatibility/Compatibility.java @@ -62,7 +62,7 @@ public class Compatibility { } } - public static Notification createMessageNotification(Context context, int msgCount,String to, String msgSender, String msg, Bitmap contactIcon, PendingIntent intent) { + public static Notification createMessageNotification(Context context, int msgCount, String msgSender, String msg, Bitmap contactIcon, PendingIntent intent) { if (Version.sdkAboveOrEqual(Version.API26_O_80)) { return ApiTwentySixPlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent); } else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) {