diff options
Diffstat (limited to 'src/eu/siacs/conversations/services/XmppConnectionService.java')
-rw-r--r-- | src/eu/siacs/conversations/services/XmppConnectionService.java | 538 |
1 files changed, 357 insertions, 181 deletions
diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index c535f1a3..5c5288de 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -20,6 +20,7 @@ import de.duenndns.ssl.MemorizingTrustManager; import net.java.otr4j.OtrException; import net.java.otr4j.session.Session; import net.java.otr4j.session.SessionStatus; +import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.crypto.PgpEngine; import eu.siacs.conversations.entities.Account; @@ -49,6 +50,7 @@ import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.OnBindListener; import eu.siacs.conversations.xmpp.OnContactStatusChanged; import eu.siacs.conversations.xmpp.OnIqPacketReceived; +import eu.siacs.conversations.xmpp.OnMessageAcknowledged; import eu.siacs.conversations.xmpp.OnStatusChanged; import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager; @@ -82,20 +84,13 @@ import android.util.Log; public class XmppConnectionService extends Service { - protected static final String LOGTAG = "xmppService"; public DatabaseBackend databaseBackend; private FileBackend fileBackend; public long startDate; - private static final int PING_MAX_INTERVAL = 300; - private static final int PING_MIN_INTERVAL = 30; - private static final int PING_TIMEOUT = 10; - private static final int CONNECT_TIMEOUT = 90; - public static final long CARBON_GRACE_PERIOD = 60000L; - private static String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; - + private MemorizingTrustManager mMemorizingTrustManager; private MessageParser mMessageParser = new MessageParser(this); @@ -103,7 +98,7 @@ public class XmppConnectionService extends Service { private IqParser mIqParser = new IqParser(this); private MessageGenerator mMessageGenerator = new MessageGenerator(); private PresenceGenerator mPresenceGenerator = new PresenceGenerator(); - + private List<Account> accounts; private CopyOnWriteArrayList<Conversation> conversations = null; private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager( @@ -118,9 +113,9 @@ public class XmppConnectionService extends Service { @Override public void onContactStatusChanged(Contact contact, boolean online) { - Conversation conversation = find(getConversations(),contact); + Conversation conversation = find(getConversations(), contact); if (conversation != null) { - conversation.endOtrIfNeeded(); + conversation.resetOtrSession(); if (online && (contact.getPresences().size() == 1)) { sendUnsendMessages(conversation); } @@ -146,14 +141,16 @@ public class XmppConnectionService extends Service { @Override public void onStatusChanged(Account account) { + XmppConnection connection = account.getXmppConnection(); if (mOnAccountUpdate != null) { - mOnAccountUpdate.onAccountUpdate();; + mOnAccountUpdate.onAccountUpdate(); + ; } if (account.getStatus() == Account.STATUS_ONLINE) { - for(Conversation conversation : account.pendingConferenceLeaves) { + for (Conversation conversation : account.pendingConferenceLeaves) { leaveMuc(conversation); } - for(Conversation conversation : account.pendingConferenceJoins) { + for (Conversation conversation : account.pendingConferenceJoins) { joinMuc(conversation); } mJingleConnectionManager.cancelInTransmission(); @@ -164,27 +161,36 @@ public class XmppConnectionService extends Service { sendUnsendMessages(conversations.get(i)); } } + if (connection != null && connection.getFeatures().csi()) { + if (checkListeners()) { + Log.d(Config.LOGTAG, account.getJid() + + " sending csi//inactive"); + connection.sendInactive(); + } else { + Log.d(Config.LOGTAG, account.getJid() + + " sending csi//active"); + connection.sendActive(); + } + } syncDirtyContacts(account); - scheduleWakeupCall(PING_MAX_INTERVAL, true); + scheduleWakeupCall(Config.PING_MAX_INTERVAL, true); } else if (account.getStatus() == Account.STATUS_OFFLINE) { + resetSendingToWaiting(account); if (!account.isOptionSet(Account.OPTION_DISABLED)) { int timeToReconnect = mRandom.nextInt(50) + 10; scheduleWakeupCall(timeToReconnect, false); } - } else if (account.getStatus() == Account.STATUS_REGISTRATION_SUCCESSFULL) { databaseBackend.updateAccount(account); reconnectAccount(account, true); } else if ((account.getStatus() != Account.STATUS_CONNECTING) && (account.getStatus() != Account.STATUS_NO_INTERNET)) { - XmppConnection connection = account.getXmppConnection(); - if (connection!=null) { + if (connection != null) { int next = connection.getTimeToNextAttempt(); - Log.d(LOGTAG, account.getJid() - + ": error connecting account. try again in " + next - + "s for the " - + (connection.getAttempt() + 1) - + " time"); + Log.d(Config.LOGTAG, account.getJid() + + ": error connecting account. try again in " + + next + "s for the " + + (connection.getAttempt() + 1) + " time"); scheduleWakeupCall((int) (next * 1.2), false); } } @@ -209,19 +215,39 @@ public class XmppConnectionService extends Service { private PowerManager pm; private OnBindListener mOnBindListener = new OnBindListener() { - @Override - public void onBind(final Account account) { - account.getRoster().clearPresences(); - account.clearPresences(); // self presences - account.pendingConferenceJoins.clear(); - account.pendingConferenceLeaves.clear(); - fetchRosterFromServer(account); - fetchBookmarks(account); - sendPresencePacket(account, mPresenceGenerator.sendPresence(account)); - connectMultiModeConversations(account); - updateConversationUi(); + @Override + public void onBind(final Account account) { + account.getRoster().clearPresences(); + account.clearPresences(); // self presences + account.pendingConferenceJoins.clear(); + account.pendingConferenceLeaves.clear(); + fetchRosterFromServer(account); + fetchBookmarks(account); + sendPresencePacket(account, + mPresenceGenerator.sendPresence(account)); + connectMultiModeConversations(account); + updateConversationUi(); + } + }; + + private OnMessageAcknowledged mOnMessageAcknowledgedListener = new OnMessageAcknowledged() { + + @Override + public void onMessageAcknowledged(Account account, String uuid) { + for (Conversation conversation : getConversations()) { + if (conversation.getAccount() == account) { + for (Message message : conversation.getMessages()) { + if ((message.getStatus() == Message.STATUS_UNSEND || message + .getStatus() == Message.STATUS_WAITING) + && message.getUuid().equals(uuid)) { + markMessage(message, Message.STATUS_SEND); + return; + } + } + } } - }; + } + }; public PgpEngine getPgpEngine() { if (pgpServiceConnection.isBound()) { @@ -244,12 +270,12 @@ public class XmppConnectionService extends Service { public Message attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) { final Message message; - if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { + if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED); } else { message = new Message(conversation, "", - conversation.getNextEncryption()); + conversation.getNextEncryption(forceEncryption())); } message.setPresence(conversation.getNextPresence()); message.setType(Message.TYPE_IMAGE); @@ -260,7 +286,7 @@ public class XmppConnectionService extends Service { public void run() { try { getFileBackend().copyImageToPrivateStorage(message, uri); - if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { + if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { getPgpEngine().encrypt(message, callback); } else { callback.success(message); @@ -274,11 +300,11 @@ public class XmppConnectionService extends Service { } public Conversation find(Bookmark bookmark) { - return find(bookmark.getAccount(),bookmark.getJid()); + return find(bookmark.getAccount(), bookmark.getJid()); } - + public Conversation find(Account account, String jid) { - return find(getConversations(),account,jid); + return find(getConversations(), account, jid); } public class XmppConnectionBinder extends Binder { @@ -320,12 +346,15 @@ public class XmppConnectionService extends Service { } } if (account.getStatus() == Account.STATUS_ONLINE) { - long lastReceived = account.getXmppConnection().getLastPacketReceived(); - long lastSent = account.getXmppConnection().getLastPingSent(); - if (lastSent - lastReceived >= PING_TIMEOUT * 1000) { - Log.d(LOGTAG, account.getJid() + ": ping timeout"); + long lastReceived = account.getXmppConnection() + .getLastPacketReceived(); + long lastSent = account.getXmppConnection() + .getLastPingSent(); + if (lastSent - lastReceived >= Config.PING_TIMEOUT * 1000) { + Log.d(Config.LOGTAG, account.getJid() + + ": ping timeout"); this.reconnectAccount(account, true); - } else if (SystemClock.elapsedRealtime() - lastReceived >= PING_MIN_INTERVAL * 1000) { + } else if (SystemClock.elapsedRealtime() - lastReceived >= Config.PING_MIN_INTERVAL * 1000) { account.getXmppConnection().sendPing(); this.scheduleWakeupCall(2, false); } @@ -337,8 +366,8 @@ public class XmppConnectionService extends Service { new Thread(account.getXmppConnection()).start(); } else if ((account.getStatus() == Account.STATUS_CONNECTING) && ((SystemClock.elapsedRealtime() - account - .getXmppConnection().getLastConnect()) / 1000 >= CONNECT_TIMEOUT)) { - Log.d(LOGTAG, account.getJid() + .getXmppConnection().getLastConnect()) / 1000 >= Config.CONNECT_TIMEOUT)) { + Log.d(Config.LOGTAG, account.getJid() + ": time out during connect reconnecting"); reconnectAccount(account, true); } else { @@ -347,7 +376,7 @@ public class XmppConnectionService extends Service { } } // in any case. reschedule wakup call - this.scheduleWakeupCall(PING_MAX_INTERVAL, true); + this.scheduleWakeupCall(Config.PING_MAX_INTERVAL, true); } if (mOnAccountUpdate != null) { mOnAccountUpdate.onAccountUpdate(); @@ -369,7 +398,8 @@ public class XmppConnectionService extends Service { ExceptionHelper.init(getApplicationContext()); PRNGFixes.apply(); this.mRandom = new SecureRandom(); - this.mMemorizingTrustManager = new MemorizingTrustManager(getApplicationContext()); + this.mMemorizingTrustManager = new MemorizingTrustManager( + getApplicationContext()); this.databaseBackend = DatabaseBackend .getInstance(getApplicationContext()); this.fileBackend = new FileBackend(getApplicationContext()); @@ -416,7 +446,7 @@ public class XmppConnectionService extends Service { .getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, EventReceiver.class); alarmManager.cancel(PendingIntent.getBroadcast(context, 0, intent, 0)); - Log.d(LOGTAG, "good bye"); + Log.d(Config.LOGTAG, "good bye"); stopSelf(); } @@ -466,10 +496,11 @@ public class XmppConnectionService extends Service { connection.setOnMessagePacketReceivedListener(this.mMessageParser); connection.setOnStatusChangedListener(this.statusListener); connection.setOnPresencePacketReceivedListener(this.mPresenceParser); - connection - .setOnUnregisteredIqPacketReceivedListener(this.mIqParser); + connection.setOnUnregisteredIqPacketReceivedListener(this.mIqParser); connection.setOnJinglePacketReceivedListener(this.jingleListener); connection.setOnBindListener(this.mOnBindListener); + connection + .setOnMessageAcknowledgeListener(this.mOnMessageAcknowledgedListener); return connection; } @@ -485,8 +516,8 @@ public class XmppConnectionService extends Service { if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (!conv.hasValidOtrSession() && (message.getPresence() != null)) { - conv.startOtrSession(getApplicationContext(), - message.getPresence(), true); + conv.startOtrSession(this, message.getPresence(), + true); message.setStatus(Message.STATUS_WAITING); } else if (conv.hasValidOtrSession() && conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) { @@ -505,8 +536,7 @@ public class XmppConnectionService extends Service { if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (!conv.hasValidOtrSession() && (message.getPresence() != null)) { - conv.startOtrSession(getApplicationContext(), - message.getPresence(), true); + conv.startOtrSession(this, message.getPresence(), true); message.setStatus(Message.STATUS_WAITING); } else if (conv.hasValidOtrSession() && conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) { @@ -514,8 +544,7 @@ public class XmppConnectionService extends Service { .getUserID()); packet = mMessageGenerator.generateOtrChat(message); send = true; - message.setStatus(Message.STATUS_SEND); - + } else if (message.getPresence() == null) { message.setStatus(Message.STATUS_WAITING); } @@ -523,14 +552,10 @@ public class XmppConnectionService extends Service { message.getConversation().endOtrIfNeeded(); failWaitingOtrMessages(message.getConversation()); packet = mMessageGenerator.generatePgpChat(message); - message.setStatus(Message.STATUS_SEND); send = true; } else { message.getConversation().endOtrIfNeeded(); failWaitingOtrMessages(message.getConversation()); - if (message.getConversation().getMode() == Conversation.MODE_SINGLE || message.getType() == Message.TYPE_PRIVATE) { - message.setStatus(Message.STATUS_SEND); - } packet = mMessageGenerator.generateChat(message); send = true; } @@ -542,31 +567,38 @@ public class XmppConnectionService extends Service { String pgpBody = message.getEncryptedBody(); String decryptedBody = message.getBody(); message.setBody(pgpBody); + message.setEncryption(Message.ENCRYPTION_PGP); databaseBackend.createMessage(message); saveInDb = false; message.setBody(decryptedBody); + message.setEncryption(Message.ENCRYPTION_DECRYPTED); } else if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (conv.hasValidOtrSession()) { message.setPresence(conv.getOtrSession().getSessionID() .getUserID()); } else if (!conv.hasValidOtrSession() && message.getPresence() != null) { - conv.startOtrSession(getApplicationContext(), - message.getPresence(), false); + conv.startOtrSession(this, message.getPresence(), false); } } } } if (saveInDb) { - databaseBackend.createMessage(message); + if (message.getEncryption() == Message.ENCRYPTION_NONE + || saveEncryptedMessages()) { + databaseBackend.createMessage(message); + } } conv.getMessages().add(message); - updateConversationUi(); if ((send) && (packet != null)) { + if (!account.getXmppConnection().getFeatures().sm() + && conv.getMode() != Conversation.MODE_MULTI) { + message.setStatus(Message.STATUS_SEND); + } sendMessagePacket(account, packet); } - + updateConversationUi(); } private void sendUnsendMessages(Conversation conversation) { @@ -587,14 +619,13 @@ public class XmppConnectionService extends Service { if (!message.getConversation().hasValidOtrSession()) { if ((message.getPresence() != null) && (presences.has(message.getPresence()))) { - message.getConversation().startOtrSession( - getApplicationContext(), message.getPresence(), - true); + message.getConversation().startOtrSession(this, + message.getPresence(), true); } else { if (presences.size() == 1) { String presence = presences.asStringArray()[0]; - message.getConversation().startOtrSession( - getApplicationContext(), presence, true); + message.getConversation().startOtrSession(this, + presence, true); } } } else { @@ -632,7 +663,7 @@ public class XmppConnectionService extends Service { } } if (packet != null) { - sendMessagePacket(account,packet); + sendMessagePacket(account, packet); markMessage(message, Message.STATUS_SEND); } } @@ -640,10 +671,10 @@ public class XmppConnectionService extends Service { public void fetchRosterFromServer(Account account) { IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); if (!"".equals(account.getRosterVersion())) { - Log.d(LOGTAG, account.getJid() + ": fetching roster version " - + account.getRosterVersion()); + Log.d(Config.LOGTAG, account.getJid() + + ": fetching roster version " + account.getRosterVersion()); } else { - Log.d(LOGTAG, account.getJid() + ": fetching roster"); + Log.d(Config.LOGTAG, account.getJid() + ": fetching roster"); } iqPacket.query("jabber:iq:roster").setAttribute("ver", account.getRosterVersion()); @@ -661,29 +692,31 @@ public class XmppConnectionService extends Service { } }); } - + public void fetchBookmarks(Account account) { IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); Element query = iqPacket.query("jabber:iq:private"); query.addChild("storage", "storage:bookmarks"); OnIqPacketReceived callback = new OnIqPacketReceived() { - + @Override public void onIqPacketReceived(Account account, IqPacket packet) { Element query = packet.query(); List<Bookmark> bookmarks = new CopyOnWriteArrayList<Bookmark>(); - Element storage = query.findChild("storage", "storage:bookmarks"); - if (storage!=null) { - for(Element item : storage.getChildren()) { + Element storage = query.findChild("storage", + "storage:bookmarks"); + if (storage != null) { + for (Element item : storage.getChildren()) { if (item.getName().equals("conference")) { - Bookmark bookmark = Bookmark.parse(item,account); + Bookmark bookmark = Bookmark.parse(item, account); bookmarks.add(bookmark); Conversation conversation = find(bookmark); - if (conversation!=null) { + if (conversation != null) { conversation.setBookmark(bookmark); } else { if (bookmark.autojoin()) { - conversation = findOrCreateConversation(account, bookmark.getJid(), true); + conversation = findOrCreateConversation( + account, bookmark.getJid(), true); conversation.setBookmark(bookmark); joinMuc(conversation); } @@ -695,17 +728,17 @@ public class XmppConnectionService extends Service { } }; sendIqPacket(account, iqPacket, callback); - + } - + public void pushBookmarks(Account account) { IqPacket iqPacket = new IqPacket(IqPacket.TYPE_SET); Element query = iqPacket.query("jabber:iq:private"); Element storage = query.addChild("storage", "storage:bookmarks"); - for(Bookmark bookmark : account.getBookmarks()) { + for (Bookmark bookmark : account.getBookmarks()) { storage.addChild(bookmark.toElement()); } - sendIqPacket(account, iqPacket,null); + sendIqPacket(account, iqPacket, null); } private void mergePhoneContactsWithRoster() { @@ -750,13 +783,26 @@ public class XmppConnectionService extends Service { conv.setMessages(databaseBackend.getMessages(conv, 50)); } } - + return this.conversations; } - + public void populateWithOrderedConversations(List<Conversation> list) { + populateWithOrderedConversations(list, true); + } + + public void populateWithOrderedConversations(List<Conversation> list, + boolean includeConferences) { list.clear(); - list.addAll(getConversations()); + if (includeConferences) { + list.addAll(getConversations()); + } else { + for (Conversation conversation : getConversations()) { + if (conversation.getMode() == Conversation.MODE_SINGLE) { + list.add(conversation); + } + } + } Collections.sort(list, new Comparator<Conversation>() { @Override public int compare(Conversation lhs, Conversation rhs) { @@ -796,7 +842,8 @@ public class XmppConnectionService extends Service { return null; } - public Conversation find(List<Conversation> haystack, Account account, String jid) { + public Conversation find(List<Conversation> haystack, Account account, + String jid) { for (Conversation conversation : haystack) { if ((conversation.getAccount().equals(account)) && (conversation.getContactJid().split("/")[0].equals(jid))) { @@ -805,15 +852,14 @@ public class XmppConnectionService extends Service { } return null; } - - + public Conversation findOrCreateConversation(Account account, String jid, boolean muc) { Conversation conversation = find(account, jid); if (conversation != null) { return conversation; } - conversation = databaseBackend.findConversation(account,jid); + conversation = databaseBackend.findConversation(account, jid); if (conversation != null) { conversation.setStatus(Conversation.STATUS_AVAILABLE); conversation.setAccount(account); @@ -850,7 +896,7 @@ 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()) { + if (bookmark != null && bookmark.autojoin()) { bookmark.setAutojoin(false); pushBookmarks(bookmark.getAccount()); } @@ -890,7 +936,7 @@ public class XmppConnectionService extends Service { } public void deleteAccount(Account account) { - for(Conversation conversation : conversations) { + for (Conversation conversation : conversations) { if (conversation.getAccount() == account) { if (conversation.getMode() == Conversation.MODE_MULTI) { leaveMuc(conversation); @@ -911,6 +957,9 @@ public class XmppConnectionService extends Service { public void setOnConversationListChangedListener( OnConversationUpdate listener) { + if (checkListeners()) { + switchToForeground(); + } this.mOnConversationUpdate = listener; this.convChangedListenerCount++; } @@ -919,10 +968,16 @@ public class XmppConnectionService extends Service { this.convChangedListenerCount--; if (this.convChangedListenerCount == 0) { this.mOnConversationUpdate = null; + if (checkListeners()) { + switchToBackground(); + } } } public void setOnAccountListChangedListener(OnAccountUpdate listener) { + if (checkListeners()) { + switchToForeground(); + } this.mOnAccountUpdate = listener; this.accountChangedListenerCount++; } @@ -931,15 +986,55 @@ public class XmppConnectionService extends Service { this.accountChangedListenerCount--; if (this.accountChangedListenerCount == 0) { this.mOnAccountUpdate = null; + if (checkListeners()) { + switchToBackground(); + } } } - + public void setOnRosterUpdateListener(OnRosterUpdate listener) { + if (checkListeners()) { + switchToForeground(); + } this.mOnRosterUpdate = listener; } public void removeOnRosterUpdateListener() { this.mOnRosterUpdate = null; + if (checkListeners()) { + switchToBackground(); + } + } + + private boolean checkListeners() { + return (this.mOnAccountUpdate == null + && this.mOnConversationUpdate == null && this.mOnRosterUpdate == null); + } + + private void switchToForeground() { + for (Account account : getAccounts()) { + if (account.getStatus() == Account.STATUS_ONLINE) { + XmppConnection connection = account.getXmppConnection(); + if (connection != null && connection.getFeatures().csi()) { + connection.sendActive(); + Log.d(Config.LOGTAG, account.getJid() + + " sending csi//active"); + } + } + } + } + + private void switchToBackground() { + for (Account account : getAccounts()) { + if (account.getStatus() == Account.STATUS_ONLINE) { + XmppConnection connection = account.getXmppConnection(); + if (connection != null && connection.getFeatures().csi()) { + connection.sendInactive(); + Log.d(Config.LOGTAG, account.getJid() + + " sending csi//inactive"); + } + } + } } public void connectMultiModeConversations(Account account) { @@ -958,14 +1053,19 @@ public class XmppConnectionService extends Service { account.pendingConferenceJoins.remove(conversation); account.pendingConferenceLeaves.remove(conversation); if (account.getStatus() == Account.STATUS_ONLINE) { - Log.d(LOGTAG,"joining conversation "+conversation.getContactJid()); + Log.d(Config.LOGTAG, + "joining conversation " + conversation.getContactJid()); String nick = conversation.getMucOptions().getProposedNick(); conversation.getMucOptions().setJoinNick(nick); PresencePacket packet = new PresencePacket(); String joinJid = conversation.getMucOptions().getJoinJid(); - packet.setAttribute("to",conversation.getMucOptions().getJoinJid()); + packet.setAttribute("to", conversation.getMucOptions().getJoinJid()); Element x = new Element("x"); x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); + if (conversation.getMucOptions().getPassword() != null) { + Element password = x.addChild("password"); + password.setContent(conversation.getMucOptions().getPassword()); + } String sig = account.getPgpSignature(); if (sig != null) { packet.addChild("status").setContent("online"); @@ -975,8 +1075,8 @@ public class XmppConnectionService extends Service { final SimpleDateFormat mDateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); mDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - Date date = new Date( - conversation.getLatestMessage().getTimeSent() + 1000); + Date date = new Date(conversation.getLatestMessage() + .getTimeSent() + 1000); x.addChild("history").setAttribute("since", mDateFormat.format(date)); } @@ -998,6 +1098,20 @@ public class XmppConnectionService extends Service { this.renameListener = listener; } + 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); + } + pushBookmarks(conversation.getAccount()); + } + joinMuc(conversation); + } + } + public void renameInMuc(final Conversation conversation, final String nick) { final MucOptions options = conversation.getMucOptions(); options.setJoinNick(nick); @@ -1011,10 +1125,11 @@ public class XmppConnectionService extends Service { renameListener.onRename(success); } if (success) { - conversation.setContactJid(conversation.getMucOptions().getJoinJid()); + conversation.setContactJid(conversation.getMucOptions() + .getJoinJid()); databaseBackend.updateConversation(conversation); Bookmark bookmark = conversation.getBookmark(); - if (bookmark!=null) { + if (bookmark != null) { bookmark.setNick(nick); pushBookmarks(bookmark.getAccount()); } @@ -1023,7 +1138,7 @@ public class XmppConnectionService extends Service { }); options.flagAboutToRename(); PresencePacket packet = new PresencePacket(); - packet.setAttribute("to",options.getJoinJid()); + packet.setAttribute("to", options.getJoinJid()); packet.setAttribute("from", conversation.getAccount().getFullJid()); String sig = account.getPgpSignature(); @@ -1031,13 +1146,13 @@ public class XmppConnectionService extends Service { packet.addChild("status").setContent("online"); packet.addChild("x", "jabber:x:signed").setContent(sig); } - sendPresencePacket(account,packet); + sendPresencePacket(account, packet); } else { conversation.setContactJid(options.getJoinJid()); databaseBackend.updateConversation(conversation); if (conversation.getAccount().getStatus() == Account.STATUS_ONLINE) { Bookmark bookmark = conversation.getBookmark(); - if (bookmark!=null) { + if (bookmark != null) { bookmark.setNick(nick); pushBookmarks(bookmark.getAccount()); } @@ -1055,10 +1170,11 @@ public class XmppConnectionService extends Service { packet.setAttribute("to", conversation.getMucOptions().getJoinJid()); packet.setAttribute("from", conversation.getAccount().getFullJid()); packet.setAttribute("type", "unavailable"); - sendPresencePacket(conversation.getAccount(),packet); + sendPresencePacket(conversation.getAccount(), packet); conversation.getMucOptions().setOffline(); conversation.deregisterWithBookmark(); - Log.d(LOGTAG,conversation.getAccount().getJid()+" leaving muc "+conversation.getContactJid()); + Log.d(Config.LOGTAG, conversation.getAccount().getJid() + + " leaving muc " + conversation.getContactJid()); } else { account.pendingConferenceLeaves.add(conversation); } @@ -1099,7 +1215,6 @@ public class XmppConnectionService extends Service { pushContactToServer(contact); } if (contact.getOption(Contact.Options.DIRTY_DELETE)) { - Log.d(LOGTAG, "dirty delete"); deleteContactOnServer(contact); } } @@ -1119,9 +1234,10 @@ public class XmppConnectionService extends Service { Account account = conversation.getAccount(); List<Message> messages = conversation.getMessages(); Session otrSession = conversation.getOtrSession(); - Log.d(LOGTAG, account.getJid() + " otr session established with " - + conversation.getContactJid() + "/" - + otrSession.getSessionID().getUserID()); + Log.d(Config.LOGTAG, + account.getJid() + " otr session established with " + + conversation.getContactJid() + "/" + + otrSession.getSessionID().getUserID()); for (int i = 0; i < messages.size(); ++i) { Message msg = messages.get(i); if ((msg.getStatus() == Message.STATUS_UNSEND || msg.getStatus() == Message.STATUS_WAITING) @@ -1133,7 +1249,7 @@ public class XmppConnectionService extends Service { if (outPacket != null) { msg.setStatus(Message.STATUS_SEND); databaseBackend.updateMessage(msg); - sendMessagePacket(account,outPacket); + sendMessagePacket(account, outPacket); } } else if (msg.getType() == Message.TYPE_IMAGE) { mJingleConnectionManager.createNewConnection(msg); @@ -1160,7 +1276,7 @@ public class XmppConnectionService extends Service { packet.setBody(otrSession .transformSending(CryptoHelper.FILETRANSFER + CryptoHelper.bytesToHex(symmetricKey))); - sendMessagePacket(account,packet); + sendMessagePacket(account, packet); conversation.setSymmetricKey(symmetricKey); return true; } catch (OtrException e) { @@ -1176,26 +1292,30 @@ public class XmppConnectionService extends Service { Account account = contact.getAccount(); if (account.getStatus() == Account.STATUS_ONLINE) { boolean ask = contact.getOption(Contact.Options.ASKING); - boolean sendUpdates = contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST) + boolean sendUpdates = contact + .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST) && contact.getOption(Contact.Options.PREEMPTIVE_GRANT); IqPacket iq = new IqPacket(IqPacket.TYPE_SET); iq.query("jabber:iq:roster").addChild(contact.asElement()); account.getXmppConnection().sendIqPacket(iq, null); if (sendUpdates) { - sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact)); + sendPresencePacket(account, + mPresenceGenerator.sendPresenceUpdatesTo(contact)); } if (ask) { - sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact)); + sendPresencePacket(account, + mPresenceGenerator.requestPresenceUpdatesFrom(contact)); } } } - - public void publishAvatar(Account account, Uri image, final UiCallback<Avatar> callback) { - final Bitmap.CompressFormat format = Defaults.AVATAR_FORMAT; - final int size = Defaults.AVATAR_SIZE; - final Avatar avatar = getFileBackend().getPepAvatar(image, size, format); - if (avatar!=null) { + public void publishAvatar(Account account, Uri image, + final UiCallback<Avatar> callback) { + final Bitmap.CompressFormat format = Config.AVATAR_FORMAT; + final int size = Config.AVATAR_SIZE; + final Avatar avatar = getFileBackend() + .getPepAvatar(image, size, format); + if (avatar != null) { avatar.height = size; avatar.width = size; if (format.equals(Bitmap.CompressFormat.WEBP)) { @@ -1211,27 +1331,33 @@ public class XmppConnectionService extends Service { } IqPacket packet = this.mIqGenerator.publishAvatar(avatar); this.sendIqPacket(account, packet, new OnIqPacketReceived() { - + @Override public void onIqPacketReceived(Account account, IqPacket result) { if (result.getType() == IqPacket.TYPE_RESULT) { - IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar); + IqPacket packet = XmppConnectionService.this.mIqGenerator + .publishAvatarMetadata(avatar); sendIqPacket(account, packet, new OnIqPacketReceived() { - + @Override - public void onIqPacketReceived(Account account, IqPacket result) { + public void onIqPacketReceived(Account account, + IqPacket result) { if (result.getType() == IqPacket.TYPE_RESULT) { if (account.setAvatar(avatar.getFilename())) { databaseBackend.updateAccount(account); } callback.success(avatar); } else { - callback.error(R.string.error_publish_avatar_server_reject, avatar); + callback.error( + R.string.error_publish_avatar_server_reject, + avatar); } } }); } else { - callback.error(R.string.error_publish_avatar_server_reject, avatar); + callback.error( + R.string.error_publish_avatar_server_reject, + avatar); } } }); @@ -1239,55 +1365,76 @@ public class XmppConnectionService extends Service { callback.error(R.string.error_publish_avatar_converting, null); } } - + public void fetchAvatar(Account account, Avatar avatar) { fetchAvatar(account, avatar, null); } - - public void fetchAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) { - Log.d(LOGTAG,account.getJid()+": retrieving avatar for "+avatar.owner); + + public void fetchAvatar(Account account, final Avatar avatar, + final UiCallback<Avatar> callback) { IqPacket packet = this.mIqGenerator.retrieveAvatar(avatar); sendIqPacket(account, packet, new OnIqPacketReceived() { - + @Override public void onIqPacketReceived(Account account, IqPacket result) { - avatar.image = mIqParser.avatarData(result); - if (avatar.image!=null) { - if (getFileBackend().save(avatar)) { - if (account.getJid().equals(avatar.owner)) { - if (account.setAvatar(avatar.getFilename())) { - databaseBackend.updateAccount(account); + final String ERROR = account.getJid() + + ": fetching avatar for " + avatar.owner + " failed "; + if (result.getType() == IqPacket.TYPE_RESULT) { + avatar.image = mIqParser.avatarData(result); + if (avatar.image != null) { + if (getFileBackend().save(avatar)) { + if (account.getJid().equals(avatar.owner)) { + if (account.setAvatar(avatar.getFilename())) { + databaseBackend.updateAccount(account); + } + } else { + Contact contact = account.getRoster() + .getContact(avatar.owner); + contact.setAvatar(avatar.getFilename()); } - } else { - Contact contact = account.getRoster().getContact(avatar.owner); - contact.setAvatar(avatar.getFilename()); - } - if (callback!=null) { - callback.success(avatar); + if (callback != null) { + callback.success(avatar); + } + Log.d(Config.LOGTAG, account.getJid() + + ": succesfully fetched avatar for " + + avatar.owner); + return; } - return; + } else { + + Log.d(Config.LOGTAG, ERROR + "(parsing error)"); + } + } else { + Element error = result.findChild("error"); + if (error == null) { + Log.d(Config.LOGTAG, ERROR + "(server error)"); + } else { + Log.d(Config.LOGTAG, ERROR + error.toString()); } } - if (callback!=null) { + if (callback != null) { callback.error(0, null); } + } }); } - - public void checkForAvatar(Account account, final UiCallback<Avatar> callback) { + + public void checkForAvatar(Account account, + final UiCallback<Avatar> callback) { IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null); this.sendIqPacket(account, packet, new OnIqPacketReceived() { - + @Override public void onIqPacketReceived(Account account, IqPacket packet) { if (packet.getType() == IqPacket.TYPE_RESULT) { - Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub"); - if (pubsub!=null) { + Element pubsub = packet.findChild("pubsub", + "http://jabber.org/protocol/pubsub"); + if (pubsub != null) { Element items = pubsub.findChild("items"); - if (items!=null) { + if (items != null) { Avatar avatar = Avatar.parseMetadata(items); - if (avatar!=null) { + if (avatar != null) { avatar.owner = account.getJid(); if (fileBackend.isAvatarCached(avatar)) { if (account.setAvatar(avatar.getFilename())) { @@ -1295,7 +1442,7 @@ public class XmppConnectionService extends Service { } callback.success(avatar); } else { - fetchAvatar(account, avatar,callback); + fetchAvatar(account, avatar, callback); } return; } @@ -1306,7 +1453,7 @@ public class XmppConnectionService extends Service { } }); } - + public void deleteContactOnServer(Contact contact) { contact.resetOption(Contact.Options.PREEMPTIVE_GRANT); contact.resetOption(Contact.Options.DIRTY_PUSH); @@ -1339,7 +1486,8 @@ public class XmppConnectionService extends Service { } Thread thread = new Thread(account.getXmppConnection()); thread.start(); - scheduleWakeupCall((int) (CONNECT_TIMEOUT * 1.2), false); + scheduleWakeupCall((int) (Config.CONNECT_TIMEOUT * 1.2), + false); } } }).start(); @@ -1347,7 +1495,20 @@ public class XmppConnectionService extends Service { public void invite(Conversation conversation, String contact) { MessagePacket packet = mMessageGenerator.invite(conversation, contact); - sendMessagePacket(conversation.getAccount(),packet); + sendMessagePacket(conversation.getAccount(), packet); + } + + public void resetSendingToWaiting(Account account) { + for (Conversation conversation : getConversations()) { + if (conversation.getAccount() == account) { + for (Message message : conversation.getMessages()) { + if (message.getType() != Message.TYPE_IMAGE + && message.getStatus() == Message.STATUS_UNSEND) { + markMessage(message, Message.STATUS_WAITING); + } + } + } + } } public boolean markMessage(Account account, String recipient, String uuid, @@ -1373,6 +1534,11 @@ public class XmppConnectionService extends Service { } public void markMessage(Message message, int status) { + if (status == Message.STATUS_SEND_FAILED + && (message.getStatus() == Message.STATUS_SEND_RECEIVED || message + .getStatus() == Message.STATUS_SEND_DISPLAYED)) { + return; + } message.setStatus(status); databaseBackend.updateMessage(message); updateConversationUi(); @@ -1383,10 +1549,18 @@ public class XmppConnectionService extends Service { .getDefaultSharedPreferences(getApplicationContext()); } + public boolean forceEncryption() { + return getPreferences().getBoolean("force_encryption", false); + } + public boolean confirmMessages() { return getPreferences().getBoolean("confirm_messages", true); } + public boolean saveEncryptedMessages() { + return !getPreferences().getBoolean("dont_save_encrypted", false); + } + public void notifyUi(Conversation conversation, boolean notify) { if (mOnConversationUpdate != null) { mOnConversationUpdate.onConversationUpdate(); @@ -1395,19 +1569,19 @@ public class XmppConnectionService extends Service { getConversations(), conversation, notify); } } - + public void updateConversationUi() { if (mOnConversationUpdate != null) { mOnConversationUpdate.onConversationUpdate(); } } - + public void updateAccountUi() { if (mOnAccountUpdate != null) { mOnAccountUpdate.onAccountUpdate(); } } - + public void updateRosterUi() { if (mOnRosterUpdate != null) { mOnRosterUpdate.onRosterUpdate(); @@ -1422,7 +1596,7 @@ public class XmppConnectionService extends Service { } return null; } - + public Conversation findConversationByUuid(String uuid) { for (Conversation conversation : getConversations()) { if (conversation.getUuid().equals(uuid)) { @@ -1438,10 +1612,11 @@ public class XmppConnectionService extends Service { if (confirmMessages() && id != null) { Account account = conversation.getAccount(); String to = conversation.getContactJid(); - this.sendMessagePacket(conversation.getAccount(), mMessageGenerator.confirm(account, to, id)); + this.sendMessagePacket(conversation.getAccount(), + mMessageGenerator.confirm(account, to, id)); } } - + public void failWaitingOtrMessages(Conversation conversation) { for (Message message : conversation.getMessages()) { if (message.getEncryption() == Message.ENCRYPTION_OTR @@ -1454,7 +1629,7 @@ public class XmppConnectionService extends Service { public SecureRandom getRNG() { return this.mRandom; } - + public MemorizingTrustManager getMemorizingTrustManager() { return this.mMemorizingTrustManager; } @@ -1467,7 +1642,7 @@ public class XmppConnectionService extends Service { if (account.getStatus() == Account.STATUS_ONLINE) { MessagePacket error = this.mMessageGenerator .generateNotAcceptable(packet); - sendMessagePacket(account,error); + sendMessagePacket(account, error); } } @@ -1512,43 +1687,44 @@ public class XmppConnectionService extends Service { } return mucServers; } - + public void sendMessagePacket(Account account, MessagePacket packet) { account.getXmppConnection().sendMessagePacket(packet); } - + public void sendPresencePacket(Account account, PresencePacket packet) { account.getXmppConnection().sendPresencePacket(packet); } - - public void sendIqPacket(Account account, IqPacket packet, OnIqPacketReceived callback) { + + public void sendIqPacket(Account account, IqPacket packet, + OnIqPacketReceived callback) { account.getXmppConnection().sendIqPacket(packet, callback); } - + public MessageGenerator getMessageGenerator() { return this.mMessageGenerator; } - + public PresenceGenerator getPresenceGenerator() { return this.mPresenceGenerator; } - + public IqGenerator getIqGenerator() { - return this.mIqGenerator ; + return this.mIqGenerator; } - + public JingleConnectionManager getJingleConnectionManager() { return this.mJingleConnectionManager; } - + public interface OnConversationUpdate { public void onConversationUpdate(); } - + public interface OnAccountUpdate { public void onAccountUpdate(); } - + public interface OnRosterUpdate { public void onRosterUpdate(); } |