aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/services/XmppConnectionService.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/siacs/conversations/services/XmppConnectionService.java')
-rw-r--r--src/eu/siacs/conversations/services/XmppConnectionService.java154
1 files changed, 102 insertions, 52 deletions
diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java
index db3ee2b9..e6297f4f 100644
--- a/src/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/eu/siacs/conversations/services/XmppConnectionService.java
@@ -90,9 +90,12 @@ public class XmppConnectionService extends Service {
public long startDate;
private static String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
+ public static String ACTION_CLEAR_NOTIFICATION = "clear_notification";
private MemorizingTrustManager mMemorizingTrustManager;
+ private NotificationService mNotificationService;
+
private MessageParser mMessageParser = new MessageParser(this);
private PresenceParser mPresenceParser = new PresenceParser(this);
private IqParser mIqParser = new IqParser(this);
@@ -316,14 +319,16 @@ public class XmppConnectionService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
- if ((intent != null)
- && (ACTION_MERGE_PHONE_CONTACTS.equals(intent.getAction()))) {
- mergePhoneContactsWithRoster();
- return START_STICKY;
- } else if ((intent != null)
- && (Intent.ACTION_SHUTDOWN.equals(intent.getAction()))) {
- logoutAndSave();
- return START_NOT_STICKY;
+ if (intent != null && intent.getAction() != null) {
+ if (intent.getAction().equals(ACTION_MERGE_PHONE_CONTACTS)) {
+ mergePhoneContactsWithRoster();
+ return START_STICKY;
+ } else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
+ logoutAndSave();
+ return START_NOT_STICKY;
+ } else if (intent.getAction().equals(ACTION_CLEAR_NOTIFICATION)) {
+ mNotificationService.clear();
+ }
}
this.wakeLock.acquire();
ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
@@ -401,6 +406,7 @@ public class XmppConnectionService extends Service {
this.mRandom = new SecureRandom();
this.mMemorizingTrustManager = new MemorizingTrustManager(
getApplicationContext());
+ this.mNotificationService = new NotificationService(this);
this.databaseBackend = DatabaseBackend
.getInstance(getApplicationContext());
this.fileBackend = new FileBackend(getApplicationContext());
@@ -511,7 +517,8 @@ public class XmppConnectionService extends Service {
MessagePacket packet = null;
boolean saveInDb = true;
boolean send = false;
- if (account.getStatus() == Account.STATUS_ONLINE) {
+ if (account.getStatus() == Account.STATUS_ONLINE
+ && account.getXmppConnection() != null) {
if (message.getType() == Message.TYPE_IMAGE) {
if (message.getPresence() != null) {
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
@@ -561,6 +568,10 @@ public class XmppConnectionService extends Service {
send = true;
}
}
+ if (!account.getXmppConnection().getFeatures().sm()
+ && conv.getMode() != Conversation.MODE_MULTI) {
+ message.setStatus(Message.STATUS_SEND);
+ }
} else {
message.setStatus(Message.STATUS_WAITING);
if (message.getType() == Message.TYPE_TEXT) {
@@ -586,10 +597,6 @@ public class XmppConnectionService extends Service {
}
conv.getMessages().add(message);
- if (!account.getXmppConnection().getFeatures().sm()
- && conv.getMode() != Conversation.MODE_MULTI) {
- message.setStatus(Message.STATUS_SEND);
- }
if (saveInDb) {
if (message.getEncryption() == Message.ENCRYPTION_NONE
|| saveEncryptedMessages()) {
@@ -742,7 +749,7 @@ public class XmppConnectionService extends Service {
Element query = iqPacket.query("jabber:iq:private");
Element storage = query.addChild("storage", "storage:bookmarks");
for (Bookmark bookmark : account.getBookmarks()) {
- storage.addChild(bookmark.toElement());
+ storage.addChild(bookmark);
}
sendIqPacket(account, iqPacket, null);
}
@@ -824,7 +831,7 @@ public class XmppConnectionService extends Service {
}
});
}
-
+
public int loadMoreMessages(Conversation conversation, long timestamp) {
List<Message> messages = databaseBackend.getMessages(conversation, 50,
timestamp);
@@ -851,8 +858,9 @@ public class XmppConnectionService extends Service {
public Conversation find(List<Conversation> haystack, Account account,
String jid) {
for (Conversation conversation : haystack) {
- if ((conversation.getAccount().equals(account))
- && (conversation.getContactJid().split("/",2)[0].equals(jid))) {
+ if ((account == null || conversation.getAccount().equals(account))
+ && (conversation.getContactJid().split("/", 2)[0]
+ .equals(jid))) {
return conversation;
}
}
@@ -901,10 +909,12 @@ public class XmppConnectionService extends Service {
public void archiveConversation(Conversation conversation) {
if (conversation.getMode() == Conversation.MODE_MULTI) {
- Bookmark bookmark = conversation.getBookmark();
- if (bookmark != null && bookmark.autojoin()) {
- bookmark.setAutojoin(false);
- pushBookmarks(bookmark.getAccount());
+ if (conversation.getAccount().getStatus() == Account.STATUS_ONLINE) {
+ Bookmark bookmark = conversation.getBookmark();
+ if (bookmark != null && bookmark.autojoin()) {
+ bookmark.setAutojoin(false);
+ pushBookmarks(bookmark.getAccount());
+ }
}
leaveMuc(conversation);
} else {
@@ -963,10 +973,12 @@ public class XmppConnectionService extends Service {
public void setOnConversationListChangedListener(
OnConversationUpdate listener) {
+ this.mNotificationService.deactivateGracePeriod();
if (checkListeners()) {
switchToForeground();
}
this.mOnConversationUpdate = listener;
+ this.mNotificationService.setIsInForeground(true);
this.convChangedListenerCount++;
}
@@ -974,6 +986,7 @@ public class XmppConnectionService extends Service {
this.convChangedListenerCount--;
if (this.convChangedListenerCount == 0) {
this.mOnConversationUpdate = null;
+ this.mNotificationService.setIsInForeground(false);
if (checkListeners()) {
switchToBackground();
}
@@ -981,6 +994,7 @@ public class XmppConnectionService extends Service {
}
public void setOnAccountListChangedListener(OnAccountUpdate listener) {
+ this.mNotificationService.deactivateGracePeriod();
if (checkListeners()) {
switchToForeground();
}
@@ -999,6 +1013,7 @@ public class XmppConnectionService extends Service {
}
public void setOnRosterUpdateListener(OnRosterUpdate listener) {
+ this.mNotificationService.deactivateGracePeriod();
if (checkListeners()) {
switchToForeground();
}
@@ -1111,13 +1126,11 @@ public class XmppConnectionService extends Service {
public void providePasswordForMuc(Conversation conversation, String password) {
if (conversation.getMode() == Conversation.MODE_MULTI) {
conversation.getMucOptions().setPassword(password);
- if (conversation.getBookmark() != null
- && conversation.getMucOptions().isPasswordChanged()) {
- if (!conversation.getBookmark().autojoin()) {
- conversation.getBookmark().setAutojoin(true);
- }
+ if (conversation.getBookmark() != null) {
+ conversation.getBookmark().setAutojoin(true);
pushBookmarks(conversation.getAccount());
}
+ databaseBackend.updateConversation(conversation);
joinMuc(conversation);
}
}
@@ -1266,7 +1279,7 @@ public class XmppConnectionService extends Service {
}
}
}
- notifyUi(conversation, false);
+ updateConversationUi();
}
public boolean renewSymmetricKey(Conversation conversation) {
@@ -1498,6 +1511,9 @@ public class XmppConnectionService extends Service {
thread.start();
scheduleWakeupCall((int) (Config.CONNECT_TIMEOUT * 1.2),
false);
+ } else {
+ account.getRoster().clearPresences();
+ account.setXmppConnection(null);
}
}
}).start();
@@ -1523,24 +1539,34 @@ public class XmppConnectionService extends Service {
public boolean markMessage(Account account, String recipient, String uuid,
int status) {
- for (Conversation conversation : getConversations()) {
- if (conversation.getContactJid().equals(recipient)
- && conversation.getAccount().equals(account)) {
- return markMessage(conversation, uuid, status);
+ if (uuid == null) {
+ return false;
+ } else {
+ for (Conversation conversation : getConversations()) {
+ if (conversation.getContactJid().equals(recipient)
+ && conversation.getAccount().equals(account)) {
+ return markMessage(conversation, uuid, status);
+ }
}
+ return false;
}
- return false;
}
public boolean markMessage(Conversation conversation, String uuid,
int status) {
- for (Message message : conversation.getMessages()) {
- if (message.getUuid().equals(uuid)) {
- markMessage(message, status);
- return true;
+ if (uuid == null) {
+ return false;
+ } else {
+ for (Message message : conversation.getMessages()) {
+ if (uuid.equals(message.getUuid())
+ || (message.getStatus() >= Message.STATUS_SEND && uuid
+ .equals(message.getRemoteMsgId()))) {
+ markMessage(message, status);
+ return true;
+ }
}
+ return false;
}
- return false;
}
public void markMessage(Message message, int status) {
@@ -1575,15 +1601,6 @@ public class XmppConnectionService extends Service {
return getPreferences().getBoolean("indicate_received", false);
}
- public void notifyUi(Conversation conversation, boolean notify) {
- if (mOnConversationUpdate != null) {
- mOnConversationUpdate.onConversationUpdate();
- } else {
- UIHelper.updateNotification(getApplicationContext(),
- getConversations(), conversation, notify);
- }
- }
-
public void updateConversationUi() {
if (mOnConversationUpdate != null) {
mOnConversationUpdate.onConversationUpdate();
@@ -1620,15 +1637,21 @@ public class XmppConnectionService extends Service {
return null;
}
- public void markRead(Conversation conversation) {
+ public void markRead(Conversation conversation, boolean calledByUi) {
+ mNotificationService.clear(conversation);
+ String id = conversation.getLatestMarkableMessageId();
conversation.markRead();
- String id = conversation.popLatestMarkableMessageId();
- if (confirmMessages() && id != null) {
+ if (confirmMessages() && id != null && calledByUi) {
+ Log.d(Config.LOGTAG, conversation.getAccount().getJid()
+ + ": sending read marker for " + conversation.getName());
Account account = conversation.getAccount();
String to = conversation.getContactJid();
this.sendMessagePacket(conversation.getAccount(),
mMessageGenerator.confirm(account, to, id));
}
+ if (!calledByUi) {
+ updateConversationUi();
+ }
}
public void failWaitingOtrMessages(Conversation conversation) {
@@ -1703,16 +1726,25 @@ public class XmppConnectionService extends Service {
}
public void sendMessagePacket(Account account, MessagePacket packet) {
- account.getXmppConnection().sendMessagePacket(packet);
+ XmppConnection connection = account.getXmppConnection();
+ if (connection != null) {
+ connection.sendMessagePacket(packet);
+ }
}
public void sendPresencePacket(Account account, PresencePacket packet) {
- account.getXmppConnection().sendPresencePacket(packet);
+ XmppConnection connection = account.getXmppConnection();
+ if (connection != null) {
+ connection.sendPresencePacket(packet);
+ }
}
public void sendIqPacket(Account account, IqPacket packet,
OnIqPacketReceived callback) {
- account.getXmppConnection().sendIqPacket(packet, callback);
+ XmppConnection connection = account.getXmppConnection();
+ if (connection != null) {
+ connection.sendIqPacket(packet, callback);
+ }
}
public MessageGenerator getMessageGenerator() {
@@ -1742,4 +1774,22 @@ public class XmppConnectionService extends Service {
public interface OnRosterUpdate {
public void onRosterUpdate();
}
+
+ public List<Contact> findContacts(String jid) {
+ ArrayList<Contact> contacts = new ArrayList<Contact>();
+ for (Account account : getAccounts()) {
+ if (!account.isOptionSet(Account.OPTION_DISABLED)) {
+ Contact contact = account.getRoster()
+ .getContactAsShownInRoster(jid);
+ if (contact != null) {
+ contacts.add(contact);
+ }
+ }
+ }
+ return contacts;
+ }
+
+ public NotificationService getNotificationService() {
+ return this.mNotificationService;
+ }
}