sync roster to disk after roster push

This commit is contained in:
Daniel Gultsch 2018-03-18 12:24:28 +01:00
parent ff2b1fad51
commit 9908af6286
6 changed files with 36 additions and 36 deletions

View File

@ -98,8 +98,7 @@ public class IqGenerator extends AbstractGenerator {
protected IqPacket publish(final String node, final Element item, final Bundle options) { protected IqPacket publish(final String node, final Element item, final Bundle options) {
final IqPacket packet = new IqPacket(IqPacket.TYPE.SET); final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
final Element pubsub = packet.addChild("pubsub", final Element pubsub = packet.addChild("pubsub",Namespace.PUBSUB);
"http://jabber.org/protocol/pubsub");
final Element publish = pubsub.addChild("publish"); final Element publish = pubsub.addChild("publish");
publish.setAttribute("node", node); publish.setAttribute("node", node);
publish.addChild(item); publish.addChild(item);
@ -116,8 +115,7 @@ public class IqGenerator extends AbstractGenerator {
protected IqPacket retrieve(String node, Element item) { protected IqPacket retrieve(String node, Element item) {
final IqPacket packet = new IqPacket(IqPacket.TYPE.GET); final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
final Element pubsub = packet.addChild("pubsub", final Element pubsub = packet.addChild("pubsub",Namespace.PUBSUB);
"http://jabber.org/protocol/pubsub");
final Element items = pubsub.addChild("items"); final Element items = pubsub.addChild("items");
items.setAttribute("node", node); items.setAttribute("node", node);
if (item != null) { if (item != null) {
@ -397,7 +395,7 @@ public class IqGenerator extends AbstractGenerator {
enable.setAttribute("jid",jid.toString()); enable.setAttribute("jid",jid.toString());
enable.setAttribute("node", node); enable.setAttribute("node", node);
Data data = new Data(); Data data = new Data();
data.setFormType("http://jabber.org/protocol/pubsub#publish-options"); data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS);
data.put("secret",secret); data.put("secret",secret);
data.submit(); data.submit();
enable.addChild(data); enable.addChild(data);

View File

@ -82,11 +82,11 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateRosterUi(); mXmppConnectionService.updateRosterUi();
mXmppConnectionService.getShortcutService().refresh(); mXmppConnectionService.getShortcutService().refresh();
mXmppConnectionService.syncRoster(account);
} }
public String avatarData(final IqPacket packet) { public String avatarData(final IqPacket packet) {
final Element pubsub = packet.findChild("pubsub", final Element pubsub = packet.findChild("pubsub", Namespace.PUBSUB);
"http://jabber.org/protocol/pubsub");
if (pubsub == null) { if (pubsub == null) {
return null; return null;
} }
@ -98,8 +98,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
} }
public Element getItem(final IqPacket packet) { public Element getItem(final IqPacket packet) {
final Element pubsub = packet.findChild("pubsub", final Element pubsub = packet.findChild("pubsub", Namespace.PUBSUB);
"http://jabber.org/protocol/pubsub");
if (pubsub == null) { if (pubsub == null) {
return null; return null;
} }

View File

@ -7,6 +7,7 @@ import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment; import android.os.Environment;
import android.os.SystemClock;
import android.util.Base64; import android.util.Base64;
import android.util.Log; import android.util.Log;
@ -850,6 +851,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
} }
public void writeRoster(final Roster roster) { public void writeRoster(final Roster roster) {
long start = SystemClock.elapsedRealtime();
final Account account = roster.getAccount(); final Account account = roster.getAccount();
final SQLiteDatabase db = this.getWritableDatabase(); final SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction(); db.beginTransaction();
@ -866,6 +868,8 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.endTransaction(); db.endTransaction();
account.setRosterVersion(roster.getVersion()); account.setRosterVersion(roster.getVersion());
updateAccount(account); updateAccount(account);
long duration = SystemClock.elapsedRealtime() - start;
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": persisted roster in "+duration+"ms");
} }
public void deleteMessagesInConversation(Conversation conversation) { public void deleteMessagesInConversation(Conversation conversation) {

View File

@ -184,29 +184,22 @@ public class XmppConnectionService extends Service {
private OnMessagePacketReceived mMessageParser = new MessageParser(this); private OnMessagePacketReceived mMessageParser = new MessageParser(this);
private OnPresencePacketReceived mPresenceParser = new PresenceParser(this); private OnPresencePacketReceived mPresenceParser = new PresenceParser(this);
private IqParser mIqParser = new IqParser(this); private IqParser mIqParser = new IqParser(this);
private OnIqPacketReceived mDefaultIqHandler = new OnIqPacketReceived() { private final OnIqPacketReceived mDefaultIqHandler = (account, packet) -> {
@Override if (packet.getType() != IqPacket.TYPE.RESULT) {
public void onIqPacketReceived(Account account, IqPacket packet) { Element error = packet.findChild("error");
if (packet.getType() != IqPacket.TYPE.RESULT) { String text = error != null ? error.findChildContent("text") : null;
Element error = packet.findChild("error"); if (text != null) {
String text = error != null ? error.findChildContent("text") : null; Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received iq error - " + text);
if (text != null) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received iq error - " + text);
}
} }
} }
}; };
private MessageGenerator mMessageGenerator = new MessageGenerator(this); private MessageGenerator mMessageGenerator = new MessageGenerator(this);
public OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() { public OnContactStatusChanged onContactStatusChanged = (contact, online) -> {
Conversation conversation = find(getConversations(), contact);
@Override if (conversation != null) {
public void onContactStatusChanged(Contact contact, boolean online) { if (online) {
Conversation conversation = find(getConversations(), contact); if (contact.getPresences().size() == 1) {
if (conversation != null) { sendUnsentMessages(conversation);
if (online) {
if (contact.getPresences().size() == 1) {
sendUnsentMessages(conversation);
}
} }
} }
} }
@ -966,7 +959,7 @@ public class XmppConnectionService extends Service {
restoreFromDatabase(); restoreFromDatabase();
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver); getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
new Thread(() -> fileObserver.startWatching()).start(); new Thread(fileObserver::startWatching).start();
if (Config.supportOpenPgp()) { if (Config.supportOpenPgp()) {
this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() { this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() {
@Override @Override
@ -1464,6 +1457,11 @@ public class XmppConnectionService extends Service {
})); }));
} }
public void syncRoster(final Account account) {
mDatabaseWriterExecutor.execute(() -> databaseBackend.writeRoster(account.getRoster()));
}
public List<Conversation> getConversations() { public List<Conversation> getConversations() {
return this.conversations; return this.conversations;
} }
@ -2706,13 +2704,13 @@ public class XmppConnectionService extends Service {
iq.query(Namespace.ROSTER).addChild(contact.asElement()); iq.query(Namespace.ROSTER).addChild(contact.asElement());
account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler); account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
if (sendUpdates) { if (sendUpdates) {
sendPresencePacket(account, sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
mPresenceGenerator.sendPresenceUpdatesTo(contact));
} }
if (ask) { if (ask) {
sendPresencePacket(account, sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact));
mPresenceGenerator.requestPresenceUpdatesFrom(contact));
} }
} else {
syncRoster(contact.getAccount());
} }
} }

View File

@ -619,7 +619,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
} }
private void hidePrepareFileToast(final Toast prepareFileToast) { private void hidePrepareFileToast(final Toast prepareFileToast) {
if (prepareFileToast != null) { if (prepareFileToast != null && activity != null) {
activity.runOnUiThread(prepareFileToast::cancel); activity.runOnUiThread(prepareFileToast::cancel);
} }
} }

View File

@ -14,8 +14,9 @@ public final class Namespace {
public static final String OOB = "jabber:x:oob"; public static final String OOB = "jabber:x:oob";
public static final String SASL = "urn:ietf:params:xml:ns:xmpp-sasl"; public static final String SASL = "urn:ietf:params:xml:ns:xmpp-sasl";
public static final String TLS = "urn:ietf:params:xml:ns:xmpp-tls"; public static final String TLS = "urn:ietf:params:xml:ns:xmpp-tls";
public static final String PUBSUB_PUBLISH_OPTIONS = "http://jabber.org/protocol/pubsub#publish-options"; public static final String PUBSUB = "http://jabber.org/protocol/pubsub";
public static final String PUBSUB_ERROR = "http://jabber.org/protocol/pubsub#errors"; public static final String PUBSUB_PUBLISH_OPTIONS = PUBSUB+"#publish-options";
public static final String PUBSUB_ERROR = PUBSUB+"#errors";
public static final String NICK = "http://jabber.org/protocol/nick"; public static final String NICK = "http://jabber.org/protocol/nick";
public static final String FLEXIBLE_OFFLINE_MESSAGE_RETRIEVAL = "http://jabber.org/protocol/offline"; public static final String FLEXIBLE_OFFLINE_MESSAGE_RETRIEVAL = "http://jabber.org/protocol/offline";
public static final String BIND = "urn:ietf:params:xml:ns:xmpp-bind"; public static final String BIND = "urn:ietf:params:xml:ns:xmpp-bind";