refactored grace period

This commit is contained in:
iNPUTmice 2014-10-07 13:37:50 +02:00
parent 7f52435273
commit d5227e5c25
5 changed files with 32 additions and 17 deletions

View File

@ -10,7 +10,7 @@ public final class Config {
public static final int PING_MIN_INTERVAL = 30; public static final int PING_MIN_INTERVAL = 30;
public static final int PING_TIMEOUT = 10; public static final int PING_TIMEOUT = 10;
public static final int CONNECT_TIMEOUT = 90; 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 int AVATAR_SIZE = 192;
public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.WEBP; public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.WEBP;

View File

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

View File

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

View File

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

View File

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