From eaf1974501333c132cf195e489467a04a89833d8 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 12 Apr 2018 13:37:57 +0200 Subject: [PATCH] Use one notification per chat room + remove it when going in the chatroom even without clicking on it --- src/android/org/linphone/LinphoneManager.java | 1 + .../org/linphone/LinphonePreferences.java | 6 +- src/android/org/linphone/LinphoneService.java | 88 +++++++++++-------- .../linphone/activities/LinphoneActivity.java | 6 -- 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/android/org/linphone/LinphoneManager.java b/src/android/org/linphone/LinphoneManager.java index 1c18c9f54..7b1fc2bcb 100644 --- a/src/android/org/linphone/LinphoneManager.java +++ b/src/android/org/linphone/LinphoneManager.java @@ -1121,6 +1121,7 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou public void setCurrentChatRoomAddress(Address address) { mCurrentChatRoomAddress = address; + LinphoneService.instance().setCurrentlyDisplayedChatRoom(address.asStringUriOnly()); } @Override diff --git a/src/android/org/linphone/LinphonePreferences.java b/src/android/org/linphone/LinphonePreferences.java index bc8dcf49c..2b30de7a8 100644 --- a/src/android/org/linphone/LinphonePreferences.java +++ b/src/android/org/linphone/LinphonePreferences.java @@ -1148,7 +1148,8 @@ public class LinphonePreferences { lpc.edit(); lpc.setContactUriParameters(contactInfos); lpc.done(); - Log.d("Push notif infos added to proxy config " + lpc.getIdentityAddress().asStringUriOnly()); + if (lpc.getIdentityAddress() != null) + Log.d("Push notif infos added to proxy config " + lpc.getIdentityAddress().asStringUriOnly()); } } lc.refreshRegisters(); @@ -1159,7 +1160,8 @@ public class LinphonePreferences { lpc.edit(); lpc.setContactUriParameters(null); lpc.done(); - Log.d("Push notif infos removed from proxy config " + lpc.getIdentityAddress().asStringUriOnly()); + if (lpc.getIdentityAddress() != null) + Log.d("Push notif infos removed from proxy config " + lpc.getIdentityAddress().asStringUriOnly()); } lc.refreshRegisters(); } diff --git a/src/android/org/linphone/LinphoneService.java b/src/android/org/linphone/LinphoneService.java index 80de37a75..af710db1b 100644 --- a/src/android/org/linphone/LinphoneService.java +++ b/src/android/org/linphone/LinphoneService.java @@ -22,6 +22,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import org.linphone.activities.LinphoneActivity; import org.linphone.compatibility.Compatibility; @@ -67,6 +69,7 @@ import android.os.IBinder; import android.os.SystemClock; import android.provider.ContactsContract; import android.provider.MediaStore; +import android.util.ArrayMap; import android.view.WindowManager; /** @@ -92,7 +95,6 @@ public final class LinphoneService extends Service { private final static int NOTIF_ID=1; private final static int INCALL_NOTIF_ID=2; - private final static int MESSAGE_NOTIF_ID=3; private final static int CUSTOM_NOTIF_ID=4; private final static int MISSED_NOTIF_ID=5; private final static int SAS_NOTIF_ID=6; @@ -118,10 +120,8 @@ public final class LinphoneService extends Service { private Notification mNotif; private Notification mIncallNotif; - private Notification mMsgNotif; private Notification mCustomNotif; private Notification mSasNotif; - private int mMsgNotifCount; private PendingIntent mNotifContentIntent; private String mNotificationTitle; private boolean mDisableRegistrationStatus; @@ -131,7 +131,27 @@ public final class LinphoneService extends Service { private LinphoneOverlay mOverlay; private Application.ActivityLifecycleCallbacks activityCallbacks; + private class Notified { + int notificationId; + int numberOfUnreadMessage; + } + private HashMap mChatNotifMap; + private int mLastNotificationId; + + public void setCurrentlyDisplayedChatRoom(String address) { + if (address != null) { + resetMessageNotifCount(address); + } + } + + private void resetMessageNotifCount(String address) { + Notified notif = mChatNotifMap.get(address); + if (notif != null) { + notif.numberOfUnreadMessage = 0; + mNM.cancel(notif.notificationId); + } + } /*Believe me or not, but knowing the application visibility state on Android is a nightmare. After two days of hard work I ended with the following class, that does the job more or less reliabily. @@ -259,10 +279,6 @@ public final class LinphoneService extends Service { getApplication().registerActivityLifecycleCallbacks(activityCallbacks = new ActivityMonitor()); } - public void resetMessageNotifCount() { - mMsgNotifCount = 0; - } - public boolean displayServiceNotification() { return LinphonePreferences.instance().getServiceNotificationVisibility(); } @@ -292,6 +308,8 @@ public final class LinphoneService extends Service { @Override public void onCreate() { super.onCreate(); + mLastNotificationId = 8; // To not interfere with other notifs ids + mChatNotifMap = new HashMap(); setupActivityMonitor(); // In case restart after a crash. Main in LinphoneActivity @@ -584,11 +602,6 @@ public final class LinphoneService extends Service { notifyWrapper(CUSTOM_NOTIF_ID, mCustomNotif); } - public void removeCustomNotification() { - mNM.cancel(CUSTOM_NOTIF_ID); - resetIntentLaunchedOnNotificationClick(); - } - public void displayGroupChatMessageNotification(String subject, String conferenceAddress, String fromName, Uri fromPictureUri, String message) { Intent notifIntent = new Intent(this, LinphoneActivity.class); notifIntent.putExtra("GoToChat", true); @@ -596,10 +609,15 @@ public final class LinphoneService extends Service { PendingIntent notifContentIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT); - if (mMsgNotif == null) { - mMsgNotifCount = 1; + Notified notif = mChatNotifMap.get(conferenceAddress); + if (notif != null) { + notif.numberOfUnreadMessage += 1; } else { - mMsgNotifCount++; + notif = new Notified(); + notif.numberOfUnreadMessage = 1; + notif.notificationId = mLastNotificationId; + mLastNotificationId += 1; + mChatNotifMap.put(conferenceAddress, notif); } Bitmap bm = null; @@ -612,10 +630,10 @@ public final class LinphoneService extends Service { } else { bm = BitmapFactory.decodeResource(getResources(), R.drawable.topbar_avatar); } - mMsgNotif = Compatibility.createMessageNotification(getApplicationContext(), mMsgNotifCount, subject, + Notification notification = Compatibility.createMessageNotification(getApplicationContext(), notif.numberOfUnreadMessage, subject, getString(R.string.group_chat_notif).replace("%1", fromName).replace("%2", message), bm, notifContentIntent); - notifyWrapper(MESSAGE_NOTIF_ID, mMsgNotif); + notifyWrapper(notif.notificationId, notification); } public void displayMessageNotification(String fromSipUri, String fromName, Uri fromPictureUri, String message) { @@ -629,10 +647,15 @@ public final class LinphoneService extends Service { fromName = fromSipUri; } - if (mMsgNotif == null) { - mMsgNotifCount = 1; + Notified notif = mChatNotifMap.get(fromSipUri); + if (notif != null) { + notif.numberOfUnreadMessage += 1; } else { - mMsgNotifCount++; + notif = new Notified(); + notif.numberOfUnreadMessage = 1; + notif.notificationId = mLastNotificationId; + mLastNotificationId += 1; + mChatNotifMap.put(fromSipUri, notif); } Bitmap bm = null; @@ -645,9 +668,9 @@ public final class LinphoneService extends Service { } else { bm = BitmapFactory.decodeResource(getResources(), R.drawable.topbar_avatar); } - mMsgNotif = Compatibility.createMessageNotification(getApplicationContext(), mMsgNotifCount, fromName, message, bm, notifContentIntent); + Notification notification = Compatibility.createMessageNotification(getApplicationContext(), notif.numberOfUnreadMessage, fromName, message, bm, notifContentIntent); - notifyWrapper(MESSAGE_NOTIF_ID, mMsgNotif); + notifyWrapper(notif.notificationId, notification); } public void displayInappNotification(String message) { @@ -660,11 +683,6 @@ public final class LinphoneService extends Service { notifyWrapper(NOTIF_ID, mNotif); } - public void removeMessageNotification() { - mNM.cancel(MESSAGE_NOTIF_ID); - resetIntentLaunchedOnNotificationClick(); - } - public void displaySasNotification(String sas) { mSasNotif = Compatibility.createSimpleNotification(getApplicationContext(), getString(R.string.zrtp_notification_title), @@ -839,7 +857,10 @@ public final class LinphoneService extends Service { // Make sure our notification is gone. stopForegroundCompat(NOTIF_ID); mNM.cancel(INCALL_NOTIF_ID); - mNM.cancel(MESSAGE_NOTIF_ID); + for (Notified notif : mChatNotifMap.values()) { + mNM.cancel(notif.notificationId); + } + // This will prevent the app from crashing if the service gets killed in background mode if (LinphoneActivity.isInstanciated()) { @@ -859,17 +880,6 @@ public final class LinphoneService extends Service { } catch (ClassNotFoundException e) { Log.e(e); } - resetIntentLaunchedOnNotificationClick(); - } - - private void resetIntentLaunchedOnNotificationClick() { - Intent notifIntent = new Intent(this, incomingReceivedActivity); - mNotifContentIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT); - - /*if (mNotif != null) { - mNotif.contentIntent = mNotifContentIntent; - } - notifyWrapper(NOTIF_ID, mNotif);*/ } protected void onIncomingReceived() { diff --git a/src/android/org/linphone/activities/LinphoneActivity.java b/src/android/org/linphone/activities/LinphoneActivity.java index dc388dcd7..b51a9faa7 100644 --- a/src/android/org/linphone/activities/LinphoneActivity.java +++ b/src/android/org/linphone/activities/LinphoneActivity.java @@ -730,8 +730,6 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick changeCurrentFragment(FragmentsAvailable.GROUP_CHAT, extras); } - LinphoneService.instance().resetMessageNotifCount(); - LinphoneService.instance().removeMessageNotification(); LinphoneManager.getInstance().updateUnreadCountForChatRoom(sipUri, 0); displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount()); } @@ -798,8 +796,6 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick } } - LinphoneService.instance().resetMessageNotifCount(); - LinphoneService.instance().removeMessageNotification(); LinphoneManager.getInstance().updateUnreadCountForChatRoom(sipUri, 0); displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount()); } @@ -1470,7 +1466,6 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick } Bundle extras = intent.getExtras(); if (extras != null && extras.getBoolean("GoToChat", false)) { - LinphoneService.instance().removeMessageNotification(); String sipUri = extras.getString("ChatContactSipUri"); doNotGoToCallActivity = true; displayChat(sipUri, null, null); @@ -1478,7 +1473,6 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick doNotGoToCallActivity = true; changeCurrentFragment(FragmentsAvailable.HISTORY_LIST, null); } else if (extras != null && extras.getBoolean("GoToInapp", false)) { - LinphoneService.instance().removeMessageNotification(); doNotGoToCallActivity = true; displayInapp(); } else if (extras != null && extras.getBoolean("Notification", false)) {