offline crud of contacts

This commit is contained in:
Daniel Gultsch 2014-05-22 14:33:17 +02:00
parent ad960b393d
commit b99779432c
5 changed files with 48 additions and 17 deletions

View file

@ -3,12 +3,15 @@ package eu.siacs.conversations.entities;
import java.util.HashSet; import java.util.HashSet;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Set; import java.util.Set;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.util.Log;
public class Contact { public class Contact {
public static final String TABLENAME = "contacts"; public static final String TABLENAME = "contacts";
@ -36,8 +39,8 @@ public class Contact {
protected boolean inRoster = true; protected boolean inRoster = true;
public Contact(String account, String systemName, public Contact(String account, String systemName, String serverName,
String serverName, String jid, int subscription, String photoUri, String jid, int subscription, String photoUri,
String systemAccount, String keys) { String systemAccount, String keys) {
this.accountUuid = account; this.accountUuid = account;
this.systemName = systemName; this.systemName = systemName;
@ -247,6 +250,12 @@ public class Contact {
return ((this.subscription & (1 << option)) != 0); return ((this.subscription & (1 << option)) != 0);
} }
public boolean showInRoster() {
return (this.getOption(Contact.Options.IN_ROSTER) && (!this
.getOption(Contact.Options.DIRTY_DELETE)))
|| (this.getOption(Contact.Options.DIRTY_PUSH));
}
public void parseSubscriptionFromElement(Element item) { public void parseSubscriptionFromElement(Element item) {
String ask = item.getAttribute("ask"); String ask = item.getAttribute("ask");
String subscription = item.getAttribute("subscription"); String subscription = item.getAttribute("subscription");
@ -261,13 +270,19 @@ public class Contact {
} else if (subscription.equals("both")) { } else if (subscription.equals("both")) {
this.setOption(Contact.Options.TO); this.setOption(Contact.Options.TO);
this.setOption(Contact.Options.FROM); this.setOption(Contact.Options.FROM);
} else if (subscription.equals("none")) {
this.resetOption(Contact.Options.FROM);
this.resetOption(Contact.Options.TO);
} }
} }
if ((ask != null) && (ask.equals("subscribe"))) { // do NOT override asking if pending push request
this.setOption(Contact.Options.ASKING); if (!this.getOption(Contact.Options.DIRTY_PUSH)) {
} else { if ((ask != null) && (ask.equals("subscribe"))) {
this.resetOption(Contact.Options.ASKING); this.setOption(Contact.Options.ASKING);
} else {
this.resetOption(Contact.Options.ASKING);
}
} }
} }
@ -284,10 +299,10 @@ public class Contact {
public static final int TO = 0; public static final int TO = 0;
public static final int FROM = 1; public static final int FROM = 1;
public static final int ASKING = 2; public static final int ASKING = 2;
public static final int PREEMPTIVE_GRANT = 4; public static final int PREEMPTIVE_GRANT = 3;
public static final int IN_ROSTER = 8; public static final int IN_ROSTER = 4;
public static final int PENDING_SUBSCRIPTION_REQUEST = 16; public static final int PENDING_SUBSCRIPTION_REQUEST = 5;
public static final int DIRTY_PUSH = 32; public static final int DIRTY_PUSH = 6;
public static final int DIRTY_DELETE = 64; public static final int DIRTY_DELETE = 7;
} }
} }

View file

@ -234,6 +234,7 @@ public class XmppConnectionService extends Service {
sendUnsendMessages(conversations.get(i)); sendUnsendMessages(conversations.get(i));
} }
} }
syncDirtyContacts(account);
scheduleWakeupCall(PING_MAX_INTERVAL, true); scheduleWakeupCall(PING_MAX_INTERVAL, true);
} else if (account.getStatus() == Account.STATUS_OFFLINE) { } else if (account.getStatus() == Account.STATUS_OFFLINE) {
if (!account.isOptionSet(Account.OPTION_DISABLED)) { if (!account.isOptionSet(Account.OPTION_DISABLED)) {
@ -490,9 +491,12 @@ public class XmppConnectionService extends Service {
String name = item.getAttribute("name"); String name = item.getAttribute("name");
String subscription = item.getAttribute("subscription"); String subscription = item.getAttribute("subscription");
Contact contact = account.getRoster().getContact(jid); Contact contact = account.getRoster().getContact(jid);
contact.setServerName(name); if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
contact.setServerName(name);
}
if (subscription.equals("remove")) { if (subscription.equals("remove")) {
contact.resetOption(Contact.Options.IN_ROSTER); contact.resetOption(Contact.Options.IN_ROSTER);
contact.resetOption(Contact.Options.DIRTY_DELETE);
} else { } else {
contact.setOption(Contact.Options.IN_ROSTER); contact.setOption(Contact.Options.IN_ROSTER);
contact.parseSubscriptionFromElement(item); contact.parseSubscriptionFromElement(item);
@ -1191,6 +1195,18 @@ public class XmppConnectionService extends Service {
public void updateMessage(Message message) { public void updateMessage(Message message) {
databaseBackend.updateMessage(message); databaseBackend.updateMessage(message);
} }
protected void syncDirtyContacts(Account account) {
for(Contact contact : account.getRoster().getContacts()) {
if (contact.getOption(Contact.Options.DIRTY_PUSH)) {
pushContactToServer(contact);
}
if (contact.getOption(Contact.Options.DIRTY_DELETE)) {
Log.d(LOGTAG,"dirty delete");
deleteContactOnServer(contact);
}
}
}
public void createContact(Contact contact) { public void createContact(Contact contact) {
SharedPreferences sharedPref = getPreferences(); SharedPreferences sharedPref = getPreferences();
@ -1203,12 +1219,12 @@ public class XmppConnectionService extends Service {
} }
public void pushContactToServer(Contact contact) { public void pushContactToServer(Contact contact) {
contact.resetOption(Contact.Options.DIRTY_DELETE);
Account account = contact.getAccount(); Account account = contact.getAccount();
if (account.getStatus() == Account.STATUS_ONLINE) { if (account.getStatus() == Account.STATUS_ONLINE) {
IqPacket iq = new IqPacket(IqPacket.TYPE_SET); IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
iq.query("jabber:iq:roster").addChild(contact.asElement()); iq.query("jabber:iq:roster").addChild(contact.asElement());
account.getXmppConnection().sendIqPacket(iq, null); account.getXmppConnection().sendIqPacket(iq, null);
contact.resetOption(Contact.Options.DIRTY_PUSH);
if (contact.getOption(Contact.Options.ASKING)) { if (contact.getOption(Contact.Options.ASKING)) {
requestPresenceUpdatesFrom(contact); requestPresenceUpdatesFrom(contact);
} }
@ -1223,6 +1239,7 @@ public class XmppConnectionService extends Service {
} }
public void deleteContactOnServer(Contact contact) { public void deleteContactOnServer(Contact contact) {
contact.resetOption(Contact.Options.DIRTY_PUSH);
Account account = contact.getAccount(); Account account = contact.getAccount();
if (account.getStatus() == Account.STATUS_ONLINE) { if (account.getStatus() == Account.STATUS_ONLINE) {
IqPacket iq = new IqPacket(IqPacket.TYPE_SET); IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
@ -1266,7 +1283,6 @@ public class XmppConnectionService extends Service {
packet.setAttribute("to", contact.getJid()); packet.setAttribute("to", contact.getJid());
packet.setAttribute("from", contact.getAccount().getJid()); packet.setAttribute("from", contact.getAccount().getJid());
contact.getAccount().getXmppConnection().sendPresencePacket(packet); contact.getAccount().getXmppConnection().sendPresencePacket(packet);
contact.resetOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
} }
public void sendPresence(Account account) { public void sendPresence(Account account) {

View file

@ -269,7 +269,7 @@ public class ContactsActivity extends XmppActivity {
aggregatedContacts.clear(); aggregatedContacts.clear();
for (Contact contact : rosterContacts) { for (Contact contact : rosterContacts) {
if (contact.match(searchString)&&(contact.getOption(Contact.Options.IN_ROSTER))) if (contact.match(searchString)&&(contact.showInRoster()))
aggregatedContacts.add(contact); aggregatedContacts.add(contact);
} }

View file

@ -474,7 +474,7 @@ public class ConversationActivity extends XmppActivity {
break; break;
case R.id.action_contact_details: case R.id.action_contact_details:
Contact contact = this.getSelectedConversation().getContact(); Contact contact = this.getSelectedConversation().getContact();
if (contact.getOption(Contact.Options.IN_ROSTER)) { if (contact.showInRoster()) {
Intent intent = new Intent(this, ContactDetailsActivity.class); Intent intent = new Intent(this, ContactDetailsActivity.class);
intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT); intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
intent.putExtra("account", this.getSelectedConversation().getAccount().getJid()); intent.putExtra("account", this.getSelectedConversation().getAccount().getJid());

View file

@ -91,7 +91,7 @@ public class ShareWithActivity extends XmppActivity {
List<Contact> contactsList = new ArrayList<Contact>(); List<Contact> contactsList = new ArrayList<Contact>();
for(Account account : xmppConnectionService.getAccounts()) { for(Account account : xmppConnectionService.getAccounts()) {
for(Contact contact : account.getRoster().getContacts()) { for(Contact contact : account.getRoster().getContacts()) {
if (!displayedContacts.contains(contact)&&(contact.getOption(Contact.Options.IN_ROSTER))) { if (!displayedContacts.contains(contact)&&(contact.showInRoster())) {
contactsList.add(contact); contactsList.add(contact);
} }
} }