offline crud of contacts
This commit is contained in:
parent
ad960b393d
commit
b99779432c
|
@ -3,12 +3,15 @@ package eu.siacs.conversations.entities;
|
|||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Set;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import eu.siacs.conversations.xml.Element;
|
||||
import android.content.ContentValues;
|
||||
import android.database.Cursor;
|
||||
import android.util.Log;
|
||||
|
||||
public class Contact {
|
||||
public static final String TABLENAME = "contacts";
|
||||
|
@ -36,8 +39,8 @@ public class Contact {
|
|||
|
||||
protected boolean inRoster = true;
|
||||
|
||||
public Contact(String account, String systemName,
|
||||
String serverName, String jid, int subscription, String photoUri,
|
||||
public Contact(String account, String systemName, String serverName,
|
||||
String jid, int subscription, String photoUri,
|
||||
String systemAccount, String keys) {
|
||||
this.accountUuid = account;
|
||||
this.systemName = systemName;
|
||||
|
@ -247,6 +250,12 @@ public class Contact {
|
|||
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) {
|
||||
String ask = item.getAttribute("ask");
|
||||
String subscription = item.getAttribute("subscription");
|
||||
|
@ -261,13 +270,19 @@ public class Contact {
|
|||
} else if (subscription.equals("both")) {
|
||||
this.setOption(Contact.Options.TO);
|
||||
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"))) {
|
||||
this.setOption(Contact.Options.ASKING);
|
||||
} else {
|
||||
this.resetOption(Contact.Options.ASKING);
|
||||
// do NOT override asking if pending push request
|
||||
if (!this.getOption(Contact.Options.DIRTY_PUSH)) {
|
||||
if ((ask != null) && (ask.equals("subscribe"))) {
|
||||
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 FROM = 1;
|
||||
public static final int ASKING = 2;
|
||||
public static final int PREEMPTIVE_GRANT = 4;
|
||||
public static final int IN_ROSTER = 8;
|
||||
public static final int PENDING_SUBSCRIPTION_REQUEST = 16;
|
||||
public static final int DIRTY_PUSH = 32;
|
||||
public static final int DIRTY_DELETE = 64;
|
||||
public static final int PREEMPTIVE_GRANT = 3;
|
||||
public static final int IN_ROSTER = 4;
|
||||
public static final int PENDING_SUBSCRIPTION_REQUEST = 5;
|
||||
public static final int DIRTY_PUSH = 6;
|
||||
public static final int DIRTY_DELETE = 7;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -234,6 +234,7 @@ public class XmppConnectionService extends Service {
|
|||
sendUnsendMessages(conversations.get(i));
|
||||
}
|
||||
}
|
||||
syncDirtyContacts(account);
|
||||
scheduleWakeupCall(PING_MAX_INTERVAL, true);
|
||||
} else if (account.getStatus() == Account.STATUS_OFFLINE) {
|
||||
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
|
||||
|
@ -490,9 +491,12 @@ public class XmppConnectionService extends Service {
|
|||
String name = item.getAttribute("name");
|
||||
String subscription = item.getAttribute("subscription");
|
||||
Contact contact = account.getRoster().getContact(jid);
|
||||
contact.setServerName(name);
|
||||
if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
|
||||
contact.setServerName(name);
|
||||
}
|
||||
if (subscription.equals("remove")) {
|
||||
contact.resetOption(Contact.Options.IN_ROSTER);
|
||||
contact.resetOption(Contact.Options.DIRTY_DELETE);
|
||||
} else {
|
||||
contact.setOption(Contact.Options.IN_ROSTER);
|
||||
contact.parseSubscriptionFromElement(item);
|
||||
|
@ -1192,6 +1196,18 @@ public class XmppConnectionService extends Service {
|
|||
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) {
|
||||
SharedPreferences sharedPref = getPreferences();
|
||||
boolean autoGrant = sharedPref.getBoolean("grant_new_contacts", true);
|
||||
|
@ -1203,12 +1219,12 @@ public class XmppConnectionService extends Service {
|
|||
}
|
||||
|
||||
public void pushContactToServer(Contact contact) {
|
||||
contact.resetOption(Contact.Options.DIRTY_DELETE);
|
||||
Account account = contact.getAccount();
|
||||
if (account.getStatus() == Account.STATUS_ONLINE) {
|
||||
IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
|
||||
iq.query("jabber:iq:roster").addChild(contact.asElement());
|
||||
account.getXmppConnection().sendIqPacket(iq, null);
|
||||
contact.resetOption(Contact.Options.DIRTY_PUSH);
|
||||
if (contact.getOption(Contact.Options.ASKING)) {
|
||||
requestPresenceUpdatesFrom(contact);
|
||||
}
|
||||
|
@ -1223,6 +1239,7 @@ public class XmppConnectionService extends Service {
|
|||
}
|
||||
|
||||
public void deleteContactOnServer(Contact contact) {
|
||||
contact.resetOption(Contact.Options.DIRTY_PUSH);
|
||||
Account account = contact.getAccount();
|
||||
if (account.getStatus() == Account.STATUS_ONLINE) {
|
||||
IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
|
||||
|
@ -1266,7 +1283,6 @@ public class XmppConnectionService extends Service {
|
|||
packet.setAttribute("to", contact.getJid());
|
||||
packet.setAttribute("from", contact.getAccount().getJid());
|
||||
contact.getAccount().getXmppConnection().sendPresencePacket(packet);
|
||||
contact.resetOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
|
||||
}
|
||||
|
||||
public void sendPresence(Account account) {
|
||||
|
|
|
@ -269,7 +269,7 @@ public class ContactsActivity extends XmppActivity {
|
|||
|
||||
aggregatedContacts.clear();
|
||||
for (Contact contact : rosterContacts) {
|
||||
if (contact.match(searchString)&&(contact.getOption(Contact.Options.IN_ROSTER)))
|
||||
if (contact.match(searchString)&&(contact.showInRoster()))
|
||||
aggregatedContacts.add(contact);
|
||||
}
|
||||
|
||||
|
|
|
@ -474,7 +474,7 @@ public class ConversationActivity extends XmppActivity {
|
|||
break;
|
||||
case R.id.action_contact_details:
|
||||
Contact contact = this.getSelectedConversation().getContact();
|
||||
if (contact.getOption(Contact.Options.IN_ROSTER)) {
|
||||
if (contact.showInRoster()) {
|
||||
Intent intent = new Intent(this, ContactDetailsActivity.class);
|
||||
intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
|
||||
intent.putExtra("account", this.getSelectedConversation().getAccount().getJid());
|
||||
|
|
|
@ -91,7 +91,7 @@ public class ShareWithActivity extends XmppActivity {
|
|||
List<Contact> contactsList = new ArrayList<Contact>();
|
||||
for(Account account : xmppConnectionService.getAccounts()) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue