refactored grace period

iNPUTmice created

Change summary

src/eu/siacs/conversations/Config.java                         |  2 
src/eu/siacs/conversations/parser/MessageParser.java           | 15 -
src/eu/siacs/conversations/parser/PresenceParser.java          |  2 
src/eu/siacs/conversations/services/NotificationService.java   | 27 +++
src/eu/siacs/conversations/services/XmppConnectionService.java |  3 
5 files changed, 32 insertions(+), 17 deletions(-)

Detailed changes

src/eu/siacs/conversations/Config.java 🔗

@@ -10,7 +10,7 @@ public final class Config {
 	public static final int PING_MIN_INTERVAL = 30;
 	public static final int PING_TIMEOUT = 10;
 	public static final int CONNECT_TIMEOUT = 90;
-	public static final int CARBON_GRACE_PERIOD = 60;
+	public static final int CARBON_GRACE_PERIOD = 120;
 
 	public static final int AVATAR_SIZE = 192;
 	public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.WEBP;

src/eu/siacs/conversations/parser/MessageParser.java 🔗

@@ -1,10 +1,7 @@
 package eu.siacs.conversations.parser;
 
-import android.os.SystemClock;
-import android.util.Log;
 import net.java.otr4j.session.Session;
 import net.java.otr4j.session.SessionStatus;
-import eu.siacs.conversations.Config;
 import eu.siacs.conversations.entities.Account;
 import eu.siacs.conversations.entities.Contact;
 import eu.siacs.conversations.entities.Conversation;
@@ -19,9 +16,6 @@ import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 
 public class MessageParser extends AbstractParser implements
 		OnMessagePacketReceived {
-
-	private long lastCarbonMessageReceived = -(Config.CARBON_GRACE_PERIOD * 1000);
-
 	public MessageParser(XmppConnectionService service) {
 		super(service);
 	}
@@ -404,8 +398,6 @@ public class MessageParser extends AbstractParser implements
 		Message message = null;
 		boolean notify = mXmppConnectionService.getPreferences().getBoolean(
 				"show_notification", true);
-		notify = notify
-				&& (SystemClock.elapsedRealtime() - lastCarbonMessageReceived) > (Config.CARBON_GRACE_PERIOD * 1000);
 		boolean alwaysNotifyInConference = notify
 				&& mXmppConnectionService.getPreferences().getBoolean(
 						"always_notify_in_conference", false);
@@ -431,8 +423,8 @@ public class MessageParser extends AbstractParser implements
 				message = this.parseCarbonMessage(packet, account);
 				if (message != null) {
 					if (message.getStatus() == Message.STATUS_SEND) {
-						lastCarbonMessageReceived = SystemClock
-								.elapsedRealtime();
+						mXmppConnectionService.getNotificationService()
+								.activateGracePeriod();
 						notify = false;
 						mXmppConnectionService.markRead(
 								message.getConversation(), false);
@@ -454,7 +446,8 @@ public class MessageParser extends AbstractParser implements
 				} else {
 					mXmppConnectionService.markRead(message.getConversation(),
 							false);
-					lastCarbonMessageReceived = SystemClock.elapsedRealtime();
+					mXmppConnectionService.getNotificationService()
+							.activateGracePeriod();
 					notify = false;
 				}
 			}

src/eu/siacs/conversations/parser/PresenceParser.java 🔗

@@ -58,6 +58,8 @@ public class PresenceParser extends AbstractParser implements
 							Presences.parseShow(packet.findChild("show")));
 				} else if (type.equals("unavailable")) {
 					account.removePresence(fromParts[1]);
+					mXmppConnectionService.getNotificationService()
+							.deactivateGracePeriod();
 				}
 			}
 		} else {

src/eu/siacs/conversations/services/NotificationService.java 🔗

@@ -13,10 +13,12 @@ import android.content.Intent;
 import android.content.SharedPreferences;
 import android.net.Uri;
 import android.os.PowerManager;
+import android.os.SystemClock;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.app.TaskStackBuilder;
 import android.text.Html;
 
+import eu.siacs.conversations.Config;
 import eu.siacs.conversations.R;
 import eu.siacs.conversations.entities.Conversation;
 import eu.siacs.conversations.entities.Message;
@@ -32,6 +34,8 @@ public class NotificationService {
 	public int NOTIFICATION_ID = 0x2342;
 	private Conversation mOpenConversation;
 	private boolean mIsInForeground;
+	
+	private long mEndGracePeriod = 0L;
 
 	public NotificationService(XmppConnectionService service) {
 		this.mXmppConnectionService = service;
@@ -44,10 +48,9 @@ public class NotificationService {
 		PowerManager pm = (PowerManager) mXmppConnectionService
 				.getSystemService(Context.POWER_SERVICE);
 		boolean isScreenOn = pm.isScreenOn();
-
 		if (this.mIsInForeground && isScreenOn
 				&& this.mOpenConversation == message.getConversation()) {
-			return; // simply ignore
+			return;
 		}
 		String conversationUuid = message.getConversationUuid();
 		if (notifications.containsKey(conversationUuid)) {
@@ -57,8 +60,8 @@ public class NotificationService {
 			mList.add(message);
 			notifications.put(conversationUuid, mList);
 		}
-		updateNotification(!(this.mIsInForeground && this.mOpenConversation == null)
-				|| !isScreenOn);
+		updateNotification((!(this.mIsInForeground && this.mOpenConversation == null)
+				|| !isScreenOn) && !inGracePeriod());
 	}
 
 	public void clear() {
@@ -161,7 +164,9 @@ public class NotificationService {
 				}
 			}
 			mBuilder.setDeleteIntent(createDeleteIntent());
-			mBuilder.setLights(0xffffffff, 2000, 4000);
+			if (!inGracePeriod()) {
+				mBuilder.setLights(0xffffffff, 2000, 4000);
+			}
 			Notification notification = mBuilder.build();
 			mNotificationManager.notify(NOTIFICATION_ID, notification);
 		}
@@ -221,4 +226,16 @@ public class NotificationService {
 		this.mIsInForeground = foreground;
 	}
 
+	
+	public void activateGracePeriod() {
+		this.mEndGracePeriod = SystemClock.elapsedRealtime() + (Config.CARBON_GRACE_PERIOD * 1000);
+	}
+	
+	public void deactivateGracePeriod() {
+		this.mEndGracePeriod = 0L;
+	}
+	
+	private boolean inGracePeriod() {
+		return SystemClock.elapsedRealtime() < this.mEndGracePeriod;
+	}
 }

src/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -970,6 +970,7 @@ public class XmppConnectionService extends Service {
 
 	public void setOnConversationListChangedListener(
 			OnConversationUpdate listener) {
+		this.mNotificationService.deactivateGracePeriod();
 		if (checkListeners()) {
 			switchToForeground();
 		}
@@ -990,6 +991,7 @@ public class XmppConnectionService extends Service {
 	}
 
 	public void setOnAccountListChangedListener(OnAccountUpdate listener) {
+		this.mNotificationService.deactivateGracePeriod();
 		if (checkListeners()) {
 			switchToForeground();
 		}
@@ -1008,6 +1010,7 @@ public class XmppConnectionService extends Service {
 	}
 
 	public void setOnRosterUpdateListener(OnRosterUpdate listener) {
+		this.mNotificationService.deactivateGracePeriod();
 		if (checkListeners()) {
 			switchToForeground();
 		}