From 371c985c25d0b7ed348a1f2328673636acb939d7 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 19 May 2014 21:05:17 +0200 Subject: made roster persistant aka enabled versioning again --- src/eu/siacs/conversations/entities/Contact.java | 17 ++------- .../conversations/persistance/DatabaseBackend.java | 42 +++++++++------------- .../services/XmppConnectionService.java | 12 ++++++- 3 files changed, 30 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/eu/siacs/conversations/entities/Contact.java b/src/eu/siacs/conversations/entities/Contact.java index 17cead63..ff1459a5 100644 --- a/src/eu/siacs/conversations/entities/Contact.java +++ b/src/eu/siacs/conversations/entities/Contact.java @@ -1,6 +1,5 @@ package eu.siacs.conversations.entities; -import java.io.Serializable; import java.util.HashSet; import java.util.Hashtable; import java.util.Set; @@ -11,9 +10,7 @@ import eu.siacs.conversations.xml.Element; import android.content.ContentValues; import android.database.Cursor; -public class Contact extends AbstractEntity implements Serializable { - private static final long serialVersionUID = -4570817093119419962L; - +public class Contact { public static final String TABLENAME = "contacts"; public static final String SYSTEMNAME = "systemname"; @@ -39,10 +36,9 @@ public class Contact extends AbstractEntity implements Serializable { 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 systemAccount, String keys) { - this.uuid = uuid; this.accountUuid = account; this.systemName = systemName; this.serverName = serverName; @@ -87,10 +83,8 @@ public class Contact extends AbstractEntity implements Serializable { .toLowerCase().contains(needle.toLowerCase()))); } - @Override public ContentValues getContentValues() { ContentValues values = new ContentValues(); - values.put(UUID, uuid); values.put(ACCOUNT, accountUuid); values.put(SYSTEMNAME, systemName); values.put(SERVERNAME, serverName); @@ -103,8 +97,7 @@ public class Contact extends AbstractEntity implements Serializable { } public static Contact fromCursor(Cursor cursor) { - return new Contact(cursor.getString(cursor.getColumnIndex(UUID)), - cursor.getString(cursor.getColumnIndex(ACCOUNT)), + return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)), cursor.getString(cursor.getColumnIndex(SYSTEMNAME)), cursor.getString(cursor.getColumnIndex(SERVERNAME)), cursor.getString(cursor.getColumnIndex(JID)), @@ -131,10 +124,6 @@ public class Contact extends AbstractEntity implements Serializable { return this.account; } - public void setUuid(String uuid) { - this.uuid = uuid; - } - public boolean couldBeMuc() { String[] split = this.getJid().split("@"); if (split.length != 2) { diff --git a/src/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/eu/siacs/conversations/persistance/DatabaseBackend.java index 63e050e3..771027f2 100644 --- a/src/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -2,21 +2,17 @@ package eu.siacs.conversations.persistance; import java.util.ArrayList; import java.util.List; -import java.util.UUID; import java.util.concurrent.CopyOnWriteArrayList; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; -import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Roster; -import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import android.os.Bundle; import android.util.Log; public class DatabaseBackend extends SQLiteOpenHelper { @@ -27,16 +23,16 @@ public class DatabaseBackend extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 5; private static String CREATE_CONTATCS_STATEMENT = "create table " - + Contact.TABLENAME + "(" + Contact.UUID + " TEXT PRIMARY KEY, " - + Contact.ACCOUNT + " TEXT, " + Contact.SERVERNAME + " TEXT, " + + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " + Contact.SERVERNAME + " TEXT, " + Contact.SYSTEMNAME + " TEXT," + Contact.JID + " TEXT," + Contact.KEYS + " TEXT," + Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER," + Contact.SYSTEMACCOUNT + " 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) { super(context, DATABASE_NAME, null, DATABASE_VERSION); + Log.d("xmppService",CREATE_CONTATCS_STATEMENT); } @Override @@ -220,7 +216,19 @@ public class DatabaseBackend extends SQLiteOpenHelper { } 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) { @@ -235,24 +243,6 @@ public class DatabaseBackend extends SQLiteOpenHelper { 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) { SQLiteDatabase db = this.getReadableDatabase(); String[] selectionArgs = { conversationUuid }; diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 5af53a3c..35e5fb51 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -616,14 +616,24 @@ public class XmppConnectionService extends Service { @Override public void onDestroy() { - Log.d(LOGTAG, "stopping service"); super.onDestroy(); + this.logoutAndSave(); + } + + @Override + public void onTaskRemoved(Intent rootIntent) { + super.onTaskRemoved(rootIntent); + this.logoutAndSave(); + } + + private void logoutAndSave() { for (Account account : accounts) { databaseBackend.writeRoster(account.getRoster()); if (account.getXmppConnection() != null) { disconnect(account, true); } } + Log.d(LOGTAG,"good bye"); } protected void scheduleWakeupCall(int seconds, boolean ping) { -- cgit v1.2.3