put Conversations into background mode earlier

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/parser/MessageParser.java           |  8 
src/main/java/eu/siacs/conversations/services/NotificationService.java   |  4 
src/main/java/eu/siacs/conversations/services/XmppConnectionService.java | 18 
src/main/java/eu/siacs/conversations/ui/ConversationActivity.java        | 30 
src/main/java/eu/siacs/conversations/ui/ConversationFragment.java        |  3 
5 files changed, 44 insertions(+), 19 deletions(-)

Detailed changes

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

@@ -226,7 +226,7 @@ public class MessageParser extends AbstractParser implements
 							mXmppConnectionService.getConversations(), account,
 							to.toBareJid());
 					if (conversation != null) {
-						mXmppConnectionService.markRead(conversation, false);
+						mXmppConnectionService.markRead(conversation);
 					}
 				}
 			}
@@ -503,8 +503,7 @@ public class MessageParser extends AbstractParser implements
 				if (message != null) {
 					if (message.getStatus() == Message.STATUS_SEND) {
 						account.activateGracePeriod();
-						mXmppConnectionService.markRead(
-								message.getConversation(), false);
+						mXmppConnectionService.markRead(message.getConversation());
 					} else {
 						message.markUnread();
 					}
@@ -529,8 +528,7 @@ public class MessageParser extends AbstractParser implements
 				if (message.getStatus() == Message.STATUS_RECEIVED) {
 					message.markUnread();
 				} else {
-					mXmppConnectionService.markRead(message.getConversation(),
-							false);
+					mXmppConnectionService.markRead(message.getConversation());
 					account.activateGracePeriod();
 				}
 			}

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

@@ -16,6 +16,7 @@ import android.support.v4.app.NotificationCompat.Builder;
 import android.support.v4.app.TaskStackBuilder;
 import android.text.Html;
 import android.util.DisplayMetrics;
+import android.util.Log;
 
 import java.io.FileNotFoundException;
 import java.util.ArrayList;
@@ -368,6 +369,9 @@ public class NotificationService {
 	}
 
 	public void setIsInForeground(final boolean foreground) {
+		if (foreground != this.mIsInForeground) {
+			Log.d(Config.LOGTAG,"setIsInForeground("+Boolean.toString(foreground)+")");
+		}
 		this.mIsInForeground = foreground;
 	}
 

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

@@ -1272,7 +1272,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
 
 	private boolean checkListeners() {
 		return (this.mOnAccountUpdate == null
-				&& this.mOnConversationUpdate == null && this.mOnRosterUpdate == null);
+				&& this.mOnConversationUpdate == null
+				&& this.mOnRosterUpdate == null
+				&& this.mOnUpdateBlocklist == null);
 	}
 
 	private void switchToForeground() {
@@ -1996,20 +1998,22 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
 		return null;
 	}
 
-	public void markRead(Conversation conversation, boolean calledByUi) {
+	public void markRead(final Conversation conversation) {
 		mNotificationService.clear(conversation);
-		final Message markable = conversation.getLatestMarkableMessage();
 		conversation.markRead();
-		if (confirmMessages() && markable != null && markable.getRemoteMsgId() != null && calledByUi) {
+	}
+
+	public void sendReadMarker(final Conversation conversation) {
+		final Message markable = conversation.getLatestMarkableMessage();
+		this.markRead(conversation);
+		if (confirmMessages() && markable != null && markable.getRemoteMsgId() != null) {
 			Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid()+ ": sending read marker to " + markable.getCounterpart().toString());
 			Account account = conversation.getAccount();
 			final Jid to = markable.getCounterpart();
 			MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId());
 			this.sendMessagePacket(conversation.getAccount(),packet);
 		}
-		if (!calledByUi) {
-			updateConversationUi();
-		}
+		updateConversationUi();
 	}
 
 	public SecureRandom getRNG() {

src/main/java/eu/siacs/conversations/ui/ConversationActivity.java 🔗

@@ -85,6 +85,8 @@ public class ConversationActivity extends XmppActivity
 
 	private Toast prepareFileToast;
 
+	private boolean mActivityPaused = false;
+
 
 	public List<Conversation> getConversationList() {
 		return this.conversationList;
@@ -262,11 +264,15 @@ public class ConversationActivity extends XmppActivity
 		this.updateActionBarTitle();
 		this.invalidateOptionsMenu();
 		if (xmppConnectionServiceBound) {
-			xmppConnectionService.getNotificationService().setOpenConversation(getSelectedConversation());
-			if (!getSelectedConversation().isRead()) {
-				xmppConnectionService.markRead(getSelectedConversation(), true);
-				listView.invalidateViews();
-			}
+			final Conversation conversation = getSelectedConversation();
+			xmppConnectionService.getNotificationService().setOpenConversation(conversation);
+			sendReadMarkerIfNecessary(conversation);
+		}
+	}
+
+	public void sendReadMarkerIfNecessary(final Conversation conversation) {
+		if (!mActivityPaused && !conversation.isRead()) {
+			xmppConnectionService.sendReadMarker(conversation);
 		}
 	}
 
@@ -737,6 +743,15 @@ public class ConversationActivity extends XmppActivity
 		}
 	}
 
+	@Override
+	public void onPause() {
+		super.onPause();
+		this.mActivityPaused = true;
+		if (this.xmppConnectionServiceBound) {
+			this.xmppConnectionService.getNotificationService().setIsInForeground(false);
+		}
+	}
+
 	@Override
 	public void onResume() {
 		super.onResume();
@@ -744,6 +759,10 @@ public class ConversationActivity extends XmppActivity
 		if (this.mTheme != theme) {
 			recreate();
 		}
+		this.mActivityPaused = false;
+		if (this.xmppConnectionServiceBound) {
+			this.xmppConnectionService.getNotificationService().setIsInForeground(true);
+		}
 	}
 
 	@Override
@@ -763,6 +782,7 @@ public class ConversationActivity extends XmppActivity
 
 	@Override
 	void onBackendConnected() {
+		this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 		updateConversationList();
 		if (xmppConnectionService.getAccounts().size() == 0) {
 			startActivity(new Intent(this, EditAccountActivity.class));

src/main/java/eu/siacs/conversations/ui/ConversationFragment.java 🔗

@@ -665,8 +665,7 @@ public class ConversationFragment extends Fragment {
 				this.messageListAdapter.notifyDataSetChanged();
 				updateChatMsgHint();
 				if (!activity.isConversationsOverviewVisable() || !activity.isConversationsOverviewHideable()) {
-					activity.xmppConnectionService.markRead(conversation, true);
-					activity.updateConversationList();
+					activity.sendReadMarkerIfNecessary(conversation);
 				}
 				this.updateSendButton();
 			}