put Conversations into background mode earlier

This commit is contained in:
Daniel Gultsch 2015-01-02 12:04:33 +01:00
parent 8d2f454479
commit bcd3be59ce
5 changed files with 44 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

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