From bfee69b00b2762a910f7f3f1714d18fddc99a9ad Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 23 Feb 2014 21:33:37 +0100 Subject: [PATCH] add contact to roster if a new conversation is created --- res/xml/preferences.xml | 23 +-- src/de/gultsch/chat/entities/Contact.java | 11 ++ .../chat/services/XmppConnectionService.java | 145 ++++++++++-------- .../chat/ui/NewConversationActivity.java | 5 +- 4 files changed, 109 insertions(+), 75 deletions(-) diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 390dc3a95..e8f5d9826 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -1,22 +1,23 @@ - + android:title="Security and Privacy Options"> + android:defaultValue="none"/> + + android:title="Notification Settings"> + + android:title="UI Options"> items = roster.getChildren(); - for(int i = 0; i < items.size(); ++i) { - mWhere.append(DatabaseUtils.sqlEscapeString(items.get(i).getAttribute("jid"))); - if (i != items.size() - 1) { - mWhere.append(","); - } - } - mWhere.append(") and accountUuid = \""); - mWhere.append(account.getUuid()); - mWhere.append("\""); - Log.d(LOGTAG,mWhere.toString()); - List contactsToDelete = databaseBackend.getContats(mWhere.toString()); - for(Contact contact : contactsToDelete) { - databaseBackend.deleteContact(contact); - replaceContactInConversation(contact.getJid(), null); + StringBuilder mWhere = new StringBuilder(); + mWhere.append("jid NOT IN("); + List items = roster.getChildren(); + for (int i = 0; i < items.size(); ++i) { + mWhere.append(DatabaseUtils + .sqlEscapeString(items.get(i) + .getAttribute("jid"))); + if (i != items.size() - 1) { + mWhere.append(","); } } - mergePhoneContactsWithRoster(new OnPhoneContactsMerged() { - - @Override - public void phoneContactsMerged() { - if (listener != null) { - getRoster(account, listener); - } - } - }); - } else { - if (listener != null) { - getRoster(account, listener); + mWhere.append(") and accountUuid = \""); + mWhere.append(account.getUuid()); + mWhere.append("\""); + List contactsToDelete = databaseBackend + .getContats(mWhere.toString()); + for (Contact contact : contactsToDelete) { + databaseBackend.deleteContact(contact); + replaceContactInConversation(contact.getJid(), + null); } + } + mergePhoneContactsWithRoster(new OnPhoneContactsMerged() { + + @Override + public void phoneContactsMerged() { + if (listener != null) { + getRoster(account, listener); + } + } + }); } }); } - public void mergePhoneContactsWithRoster(final OnPhoneContactsMerged listener) { + public void mergePhoneContactsWithRoster( + final OnPhoneContactsMerged listener) { PhoneHelper.loadPhoneContacts(getApplicationContext(), new OnPhoneContactsLoadedListener() { @Override @@ -520,18 +524,20 @@ public class XmppConnectionService extends Service { contact.setDisplayName(phoneContact .getString("displayname")); databaseBackend.updateContact(contact); - replaceContactInConversation(contact.getJid(), contact); + replaceContactInConversation(contact.getJid(), + contact); } else { if ((contact.getSystemAccount() != null) || (contact.getProfilePhoto() != null)) { contact.setSystemAccount(null); contact.setPhotoUri(null); databaseBackend.updateContact(contact); - replaceContactInConversation(contact.getJid(), contact); + replaceContactInConversation( + contact.getJid(), contact); } } } - if (listener!=null) { + if (listener != null) { listener.phoneContactsMerged(); } } @@ -562,7 +568,7 @@ public class XmppConnectionService extends Service { public Contact findContact(Account account, String jid) { Contact contact = databaseBackend.findContact(account, jid); - if (contact!=null) { + if (contact != null) { contact.setAccount(account); } return contact; @@ -650,7 +656,7 @@ public class XmppConnectionService extends Service { if (accountChangedListener != null) accountChangedListener.onAccountListChangedListener(); } - + public void deleteContact(Contact contact) { IqPacket iq = new IqPacket(IqPacket.TYPE_SET); Element query = new Element("query"); @@ -772,6 +778,14 @@ public class XmppConnectionService extends Service { } public void createContact(Contact contact) { + SharedPreferences sharedPref = PreferenceManager + .getDefaultSharedPreferences(getApplicationContext()); + boolean autoGrant = sharedPref.getBoolean("grant_new_contacts", true); + if (autoGrant) { + contact.setSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT); + contact.setSubscriptionOption(Contact.Subscription.ASKING); + } + databaseBackend.createContact(contact); IqPacket iq = new IqPacket(IqPacket.TYPE_SET); Element query = new Element("query"); query.setAttribute("xmlns", "jabber:iq:roster"); @@ -781,49 +795,50 @@ public class XmppConnectionService extends Service { query.addChild(item); iq.addChild(query); Account account = contact.getAccount(); - Log.d(LOGTAG,account.getJid()+": adding "+contact.getJid()+" to roster"); account.getXmppConnection().sendIqPacket(iq, null); + if (autoGrant) { + requestPresenceUpdatesFrom(contact); + } replaceContactInConversation(contact.getJid(), contact); - databaseBackend.createContact(contact); } public void requestPresenceUpdatesFrom(Contact contact) { - //Requesting a Subscription type=subscribe + // Requesting a Subscription type=subscribe PresencePacket packet = new PresencePacket(); packet.setAttribute("type", "subscribe"); packet.setAttribute("to", contact.getJid()); - packet.setAttribute("from",contact.getAccount().getJid()); - Log.d(LOGTAG,packet.toString()); + packet.setAttribute("from", contact.getAccount().getJid()); + Log.d(LOGTAG, packet.toString()); contact.getAccount().getXmppConnection().sendPresencePacket(packet); } - + public void stopPresenceUpdatesFrom(Contact contact) { - //Unsubscribing type='unsubscribe' + // Unsubscribing type='unsubscribe' PresencePacket packet = new PresencePacket(); packet.setAttribute("type", "unsubscribe"); packet.setAttribute("to", contact.getJid()); - packet.setAttribute("from",contact.getAccount().getJid()); - Log.d(LOGTAG,packet.toString()); + packet.setAttribute("from", contact.getAccount().getJid()); + Log.d(LOGTAG, packet.toString()); contact.getAccount().getXmppConnection().sendPresencePacket(packet); } - + public void stopPresenceUpdatesTo(Contact contact) { - //Canceling a Subscription type=unsubscribed + // Canceling a Subscription type=unsubscribed PresencePacket packet = new PresencePacket(); packet.setAttribute("type", "unsubscribed"); packet.setAttribute("to", contact.getJid()); - packet.setAttribute("from",contact.getAccount().getJid()); - Log.d(LOGTAG,packet.toString()); + packet.setAttribute("from", contact.getAccount().getJid()); + Log.d(LOGTAG, packet.toString()); contact.getAccount().getXmppConnection().sendPresencePacket(packet); } - + public void sendPresenceUpdatesTo(Contact contact) { - //type='subscribed' + // type='subscribed' PresencePacket packet = new PresencePacket(); packet.setAttribute("type", "subscribed"); packet.setAttribute("to", contact.getJid()); - packet.setAttribute("from",contact.getAccount().getJid()); - Log.d(LOGTAG,packet.toString()); + packet.setAttribute("from", contact.getAccount().getJid()); + Log.d(LOGTAG, packet.toString()); contact.getAccount().getXmppConnection().sendPresencePacket(packet); } } \ No newline at end of file diff --git a/src/de/gultsch/chat/ui/NewConversationActivity.java b/src/de/gultsch/chat/ui/NewConversationActivity.java index a41e3e39d..ac178fad7 100644 --- a/src/de/gultsch/chat/ui/NewConversationActivity.java +++ b/src/de/gultsch/chat/ui/NewConversationActivity.java @@ -79,6 +79,7 @@ public class NewConversationActivity extends XmppActivity { if (Validator.isValidJid(searchString)) { String name = searchString.split("@")[0]; Contact newContact = new Contact(null, name, searchString, null); + newContact.flagAsNotInRoster(); aggregatedContacts.add(newContact); contactsHeader.setText("Create new contact"); } else { @@ -228,7 +229,9 @@ public class NewConversationActivity extends XmppActivity { } public void startConversation(Contact contact, Account account, boolean muc) { - Log.d("xmppService","starting conversation for account:"+account.getJid()+" and contact:"+contact.getJid()); + if (!contact.isInRoster()) { + xmppConnectionService.createContact(contact); + } Conversation conversation = xmppConnectionService .findOrCreateConversation(account, contact.getJid(), muc);