made roster persistant aka enabled versioning again

This commit is contained in:
Daniel Gultsch 2014-05-19 21:05:17 +02:00
parent a6f9a742c0
commit 371c985c25
3 changed files with 30 additions and 41 deletions

View file

@ -1,6 +1,5 @@
package eu.siacs.conversations.entities; package eu.siacs.conversations.entities;
import java.io.Serializable;
import java.util.HashSet; import java.util.HashSet;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Set; import java.util.Set;
@ -11,9 +10,7 @@ import eu.siacs.conversations.xml.Element;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
public class Contact extends AbstractEntity implements Serializable { public class Contact {
private static final long serialVersionUID = -4570817093119419962L;
public static final String TABLENAME = "contacts"; public static final String TABLENAME = "contacts";
public static final String SYSTEMNAME = "systemname"; public static final String SYSTEMNAME = "systemname";
@ -39,10 +36,9 @@ public class Contact extends AbstractEntity implements Serializable {
protected boolean inRoster = true; protected boolean inRoster = true;
public Contact(String uuid, String account, String systemName, public Contact(String account, String systemName,
String serverName, String jid, int subscription, String photoUri, String serverName, String jid, int subscription, String photoUri,
String systemAccount, String keys) { String systemAccount, String keys) {
this.uuid = uuid;
this.accountUuid = account; this.accountUuid = account;
this.systemName = systemName; this.systemName = systemName;
this.serverName = serverName; this.serverName = serverName;
@ -87,10 +83,8 @@ public class Contact extends AbstractEntity implements Serializable {
.toLowerCase().contains(needle.toLowerCase()))); .toLowerCase().contains(needle.toLowerCase())));
} }
@Override
public ContentValues getContentValues() { public ContentValues getContentValues() {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(UUID, uuid);
values.put(ACCOUNT, accountUuid); values.put(ACCOUNT, accountUuid);
values.put(SYSTEMNAME, systemName); values.put(SYSTEMNAME, systemName);
values.put(SERVERNAME, serverName); values.put(SERVERNAME, serverName);
@ -103,8 +97,7 @@ public class Contact extends AbstractEntity implements Serializable {
} }
public static Contact fromCursor(Cursor cursor) { public static Contact fromCursor(Cursor cursor) {
return new Contact(cursor.getString(cursor.getColumnIndex(UUID)), return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)),
cursor.getString(cursor.getColumnIndex(ACCOUNT)),
cursor.getString(cursor.getColumnIndex(SYSTEMNAME)), cursor.getString(cursor.getColumnIndex(SYSTEMNAME)),
cursor.getString(cursor.getColumnIndex(SERVERNAME)), cursor.getString(cursor.getColumnIndex(SERVERNAME)),
cursor.getString(cursor.getColumnIndex(JID)), cursor.getString(cursor.getColumnIndex(JID)),
@ -131,10 +124,6 @@ public class Contact extends AbstractEntity implements Serializable {
return this.account; return this.account;
} }
public void setUuid(String uuid) {
this.uuid = uuid;
}
public boolean couldBeMuc() { public boolean couldBeMuc() {
String[] split = this.getJid().split("@"); String[] split = this.getJid().split("@");
if (split.length != 2) { if (split.length != 2) {

View file

@ -2,21 +2,17 @@ package eu.siacs.conversations.persistance;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.entities.Roster; import eu.siacs.conversations.entities.Roster;
import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log; import android.util.Log;
public class DatabaseBackend extends SQLiteOpenHelper { public class DatabaseBackend extends SQLiteOpenHelper {
@ -27,16 +23,16 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 5; private static final int DATABASE_VERSION = 5;
private static String CREATE_CONTATCS_STATEMENT = "create table " private static String CREATE_CONTATCS_STATEMENT = "create table "
+ Contact.TABLENAME + "(" + Contact.UUID + " TEXT PRIMARY KEY, " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " + Contact.SERVERNAME + " TEXT, "
+ Contact.ACCOUNT + " TEXT, " + Contact.SERVERNAME + " TEXT, "
+ Contact.SYSTEMNAME + " TEXT," + Contact.JID + " TEXT," + Contact.SYSTEMNAME + " TEXT," + Contact.JID + " TEXT,"
+ Contact.KEYS + " TEXT," + Contact.PHOTOURI + " TEXT," + Contact.KEYS + " TEXT," + Contact.PHOTOURI + " TEXT,"
+ Contact.OPTIONS + " NUMBER," + Contact.SYSTEMACCOUNT + Contact.OPTIONS + " NUMBER," + Contact.SYSTEMACCOUNT
+ " NUMBER, " + "FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES " + " NUMBER, " + "FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
+ Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE);"; + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, UNIQUE("+Contact.ACCOUNT+", "+Contact.JID+") ON CONFLICT REPLACE);";
public DatabaseBackend(Context context) { public DatabaseBackend(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("xmppService",CREATE_CONTATCS_STATEMENT);
} }
@Override @Override
@ -220,7 +216,19 @@ public class DatabaseBackend extends SQLiteOpenHelper {
} }
public void writeRoster(Roster roster) { public void writeRoster(Roster roster) {
Account account = roster.getAccount();
SQLiteDatabase db = this.getWritableDatabase();
for(Contact contact : roster.getContacts()) {
if (contact.getOption(Contact.Options.IN_ROSTER)) {
db.insert(Contact.TABLENAME, null, contact.getContentValues());
} else {
String where = Contact.ACCOUNT + "=? AND "+Contact.JID+"=?";
String[] whereArgs = {account.getUuid(), contact.getJid()};
db.delete(Contact.TABLENAME, where, whereArgs);
}
}
account.setRosterVersion(roster.getVersion());
updateAccount(account);
} }
public void deleteMessage(Message message) { public void deleteMessage(Message message) {
@ -235,24 +243,6 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args); db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
} }
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
String[] args = { contact.getUuid() };
db.delete(Contact.TABLENAME, Contact.UUID + "=?", args);
}
public Contact getContact(String uuid) {
SQLiteDatabase db = this.getWritableDatabase();
String[] args = { uuid };
Cursor cursor = db.query(Contact.TABLENAME, null, Contact.UUID + "=?",
args, null, null, null);
if (cursor.getCount() == 0) {
return null;
}
cursor.moveToFirst();
return Contact.fromCursor(cursor);
}
public Conversation findConversationByUuid(String conversationUuid) { public Conversation findConversationByUuid(String conversationUuid) {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { conversationUuid }; String[] selectionArgs = { conversationUuid };

View file

@ -616,14 +616,24 @@ public class XmppConnectionService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.d(LOGTAG, "stopping service");
super.onDestroy(); super.onDestroy();
this.logoutAndSave();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
this.logoutAndSave();
}
private void logoutAndSave() {
for (Account account : accounts) { for (Account account : accounts) {
databaseBackend.writeRoster(account.getRoster()); databaseBackend.writeRoster(account.getRoster());
if (account.getXmppConnection() != null) { if (account.getXmppConnection() != null) {
disconnect(account, true); disconnect(account, true);
} }
} }
Log.d(LOGTAG,"good bye");
} }
protected void scheduleWakeupCall(int seconds, boolean ping) { protected void scheduleWakeupCall(int seconds, boolean ping) {