Use one notification per chat room + remove it when going in the chatroom even without clicking on it

This commit is contained in:
Sylvain Berfini 2018-04-12 13:37:57 +02:00
parent 2faeabd57c
commit eaf1974501
4 changed files with 54 additions and 47 deletions

View file

@ -1121,6 +1121,7 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
public void setCurrentChatRoomAddress(Address address) { public void setCurrentChatRoomAddress(Address address) {
mCurrentChatRoomAddress = address; mCurrentChatRoomAddress = address;
LinphoneService.instance().setCurrentlyDisplayedChatRoom(address.asStringUriOnly());
} }
@Override @Override

View file

@ -1148,7 +1148,8 @@ public class LinphonePreferences {
lpc.edit(); lpc.edit();
lpc.setContactUriParameters(contactInfos); lpc.setContactUriParameters(contactInfos);
lpc.done(); 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(); lc.refreshRegisters();
@ -1159,7 +1160,8 @@ public class LinphonePreferences {
lpc.edit(); lpc.edit();
lpc.setContactUriParameters(null); lpc.setContactUriParameters(null);
lpc.done(); 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(); lc.refreshRegisters();
} }

View file

@ -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.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.linphone.activities.LinphoneActivity; import org.linphone.activities.LinphoneActivity;
import org.linphone.compatibility.Compatibility; import org.linphone.compatibility.Compatibility;
@ -67,6 +69,7 @@ import android.os.IBinder;
import android.os.SystemClock; import android.os.SystemClock;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.util.ArrayMap;
import android.view.WindowManager; 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 NOTIF_ID=1;
private final static int INCALL_NOTIF_ID=2; 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 CUSTOM_NOTIF_ID=4;
private final static int MISSED_NOTIF_ID=5; private final static int MISSED_NOTIF_ID=5;
private final static int SAS_NOTIF_ID=6; private final static int SAS_NOTIF_ID=6;
@ -118,10 +120,8 @@ public final class LinphoneService extends Service {
private Notification mNotif; private Notification mNotif;
private Notification mIncallNotif; private Notification mIncallNotif;
private Notification mMsgNotif;
private Notification mCustomNotif; private Notification mCustomNotif;
private Notification mSasNotif; private Notification mSasNotif;
private int mMsgNotifCount;
private PendingIntent mNotifContentIntent; private PendingIntent mNotifContentIntent;
private String mNotificationTitle; private String mNotificationTitle;
private boolean mDisableRegistrationStatus; private boolean mDisableRegistrationStatus;
@ -131,7 +131,27 @@ public final class LinphoneService extends Service {
private LinphoneOverlay mOverlay; private LinphoneOverlay mOverlay;
private Application.ActivityLifecycleCallbacks activityCallbacks; private Application.ActivityLifecycleCallbacks activityCallbacks;
private class Notified {
int notificationId;
int numberOfUnreadMessage;
}
private HashMap<String, Notified> 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. /*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. 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()); getApplication().registerActivityLifecycleCallbacks(activityCallbacks = new ActivityMonitor());
} }
public void resetMessageNotifCount() {
mMsgNotifCount = 0;
}
public boolean displayServiceNotification() { public boolean displayServiceNotification() {
return LinphonePreferences.instance().getServiceNotificationVisibility(); return LinphonePreferences.instance().getServiceNotificationVisibility();
} }
@ -292,6 +308,8 @@ public final class LinphoneService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
mLastNotificationId = 8; // To not interfere with other notifs ids
mChatNotifMap = new HashMap<String, Notified>();
setupActivityMonitor(); setupActivityMonitor();
// In case restart after a crash. Main in LinphoneActivity // In case restart after a crash. Main in LinphoneActivity
@ -584,11 +602,6 @@ public final class LinphoneService extends Service {
notifyWrapper(CUSTOM_NOTIF_ID, mCustomNotif); 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) { public void displayGroupChatMessageNotification(String subject, String conferenceAddress, String fromName, Uri fromPictureUri, String message) {
Intent notifIntent = new Intent(this, LinphoneActivity.class); Intent notifIntent = new Intent(this, LinphoneActivity.class);
notifIntent.putExtra("GoToChat", true); 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); PendingIntent notifContentIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (mMsgNotif == null) { Notified notif = mChatNotifMap.get(conferenceAddress);
mMsgNotifCount = 1; if (notif != null) {
notif.numberOfUnreadMessage += 1;
} else { } else {
mMsgNotifCount++; notif = new Notified();
notif.numberOfUnreadMessage = 1;
notif.notificationId = mLastNotificationId;
mLastNotificationId += 1;
mChatNotifMap.put(conferenceAddress, notif);
} }
Bitmap bm = null; Bitmap bm = null;
@ -612,10 +630,10 @@ public final class LinphoneService extends Service {
} else { } else {
bm = BitmapFactory.decodeResource(getResources(), R.drawable.topbar_avatar); 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); 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) { public void displayMessageNotification(String fromSipUri, String fromName, Uri fromPictureUri, String message) {
@ -629,10 +647,15 @@ public final class LinphoneService extends Service {
fromName = fromSipUri; fromName = fromSipUri;
} }
if (mMsgNotif == null) { Notified notif = mChatNotifMap.get(fromSipUri);
mMsgNotifCount = 1; if (notif != null) {
notif.numberOfUnreadMessage += 1;
} else { } else {
mMsgNotifCount++; notif = new Notified();
notif.numberOfUnreadMessage = 1;
notif.notificationId = mLastNotificationId;
mLastNotificationId += 1;
mChatNotifMap.put(fromSipUri, notif);
} }
Bitmap bm = null; Bitmap bm = null;
@ -645,9 +668,9 @@ public final class LinphoneService extends Service {
} else { } else {
bm = BitmapFactory.decodeResource(getResources(), R.drawable.topbar_avatar); 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) { public void displayInappNotification(String message) {
@ -660,11 +683,6 @@ public final class LinphoneService extends Service {
notifyWrapper(NOTIF_ID, mNotif); notifyWrapper(NOTIF_ID, mNotif);
} }
public void removeMessageNotification() {
mNM.cancel(MESSAGE_NOTIF_ID);
resetIntentLaunchedOnNotificationClick();
}
public void displaySasNotification(String sas) { public void displaySasNotification(String sas) {
mSasNotif = Compatibility.createSimpleNotification(getApplicationContext(), mSasNotif = Compatibility.createSimpleNotification(getApplicationContext(),
getString(R.string.zrtp_notification_title), getString(R.string.zrtp_notification_title),
@ -839,7 +857,10 @@ public final class LinphoneService extends Service {
// Make sure our notification is gone. // Make sure our notification is gone.
stopForegroundCompat(NOTIF_ID); stopForegroundCompat(NOTIF_ID);
mNM.cancel(INCALL_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 // This will prevent the app from crashing if the service gets killed in background mode
if (LinphoneActivity.isInstanciated()) { if (LinphoneActivity.isInstanciated()) {
@ -859,17 +880,6 @@ public final class LinphoneService extends Service {
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
Log.e(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() { protected void onIncomingReceived() {

View file

@ -730,8 +730,6 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
changeCurrentFragment(FragmentsAvailable.GROUP_CHAT, extras); changeCurrentFragment(FragmentsAvailable.GROUP_CHAT, extras);
} }
LinphoneService.instance().resetMessageNotifCount();
LinphoneService.instance().removeMessageNotification();
LinphoneManager.getInstance().updateUnreadCountForChatRoom(sipUri, 0); LinphoneManager.getInstance().updateUnreadCountForChatRoom(sipUri, 0);
displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount()); 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); LinphoneManager.getInstance().updateUnreadCountForChatRoom(sipUri, 0);
displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount()); displayMissedChats(LinphoneManager.getInstance().getUnreadMessageCount());
} }
@ -1470,7 +1466,6 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
} }
Bundle extras = intent.getExtras(); Bundle extras = intent.getExtras();
if (extras != null && extras.getBoolean("GoToChat", false)) { if (extras != null && extras.getBoolean("GoToChat", false)) {
LinphoneService.instance().removeMessageNotification();
String sipUri = extras.getString("ChatContactSipUri"); String sipUri = extras.getString("ChatContactSipUri");
doNotGoToCallActivity = true; doNotGoToCallActivity = true;
displayChat(sipUri, null, null); displayChat(sipUri, null, null);
@ -1478,7 +1473,6 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
doNotGoToCallActivity = true; doNotGoToCallActivity = true;
changeCurrentFragment(FragmentsAvailable.HISTORY_LIST, null); changeCurrentFragment(FragmentsAvailable.HISTORY_LIST, null);
} else if (extras != null && extras.getBoolean("GoToInapp", false)) { } else if (extras != null && extras.getBoolean("GoToInapp", false)) {
LinphoneService.instance().removeMessageNotification();
doNotGoToCallActivity = true; doNotGoToCallActivity = true;
displayInapp(); displayInapp();
} else if (extras != null && extras.getBoolean("Notification", false)) { } else if (extras != null && extras.getBoolean("Notification", false)) {