Merge pull request #626 from SamWhited/issue612

Add last seen info to the database
This commit is contained in:
Daniel Gultsch 2014-11-04 22:59:32 +01:00
commit 0c94804813
2 changed files with 44 additions and 14 deletions

View File

@ -24,6 +24,8 @@ public class Contact implements ListItem {
public static final String KEYS = "pgpkey"; public static final String KEYS = "pgpkey";
public static final String ACCOUNT = "accountUuid"; public static final String ACCOUNT = "accountUuid";
public static final String AVATAR = "avatar"; public static final String AVATAR = "avatar";
public static final String LAST_PRESENCE = "last_presence";
public static final String LAST_TIME = "last_time";
protected String accountUuid; protected String accountUuid;
protected String systemName; protected String systemName;
@ -43,9 +45,18 @@ public class Contact implements ListItem {
public Lastseen lastseen = new Lastseen(); public Lastseen lastseen = new Lastseen();
public Contact(String account, String systemName, String serverName, public Contact(final String account, final String systemName, final String serverName,
String jid, int subscription, String photoUri, final String jid, final int subscription, final String photoUri,
String systemAccount, String keys, String avatar) { final String systemAccount, final String keys, final String avatar,
final Lastseen lastseen) {
this(account, systemName, serverName, jid, subscription, photoUri, systemAccount, keys,
avatar);
this.lastseen = lastseen;
}
public Contact(final String account, final String systemName, final String serverName,
final String jid, final int subscription, final String photoUri,
final String systemAccount, final String keys, final String avatar) {
this.accountUuid = account; this.accountUuid = account;
this.systemName = systemName; this.systemName = systemName;
this.serverName = serverName; this.serverName = serverName;
@ -53,18 +64,15 @@ public class Contact implements ListItem {
this.subscription = subscription; this.subscription = subscription;
this.photoUri = photoUri; this.photoUri = photoUri;
this.systemAccount = systemAccount; this.systemAccount = systemAccount;
if (keys == null) {
keys = "";
}
try { try {
this.keys = new JSONObject(keys); this.keys = (keys == null ? new JSONObject("") : new JSONObject(keys));
} catch (JSONException e) { } catch (JSONException e) {
this.keys = new JSONObject(); this.keys = new JSONObject();
} }
this.avatar = avatar; this.avatar = avatar;
} }
public Contact(String jid) { public Contact(final String jid) {
this.jid = jid; this.jid = jid;
} }
@ -106,10 +114,15 @@ public class Contact implements ListItem {
values.put(PHOTOURI, photoUri); values.put(PHOTOURI, photoUri);
values.put(KEYS, keys.toString()); values.put(KEYS, keys.toString());
values.put(AVATAR, avatar); values.put(AVATAR, avatar);
values.put(LAST_PRESENCE, lastseen.presence);
values.put(LAST_TIME, lastseen.time);
return values; return values;
} }
public static Contact fromCursor(Cursor cursor) { public static Contact fromCursor(final Cursor cursor) {
final Lastseen lastseen = new Lastseen(
cursor.getString(cursor.getColumnIndex(LAST_PRESENCE)),
cursor.getLong(cursor.getColumnIndex(LAST_TIME)));
return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)), return new Contact(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)),
@ -118,7 +131,8 @@ public class Contact implements ListItem {
cursor.getString(cursor.getColumnIndex(PHOTOURI)), cursor.getString(cursor.getColumnIndex(PHOTOURI)),
cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)), cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
cursor.getString(cursor.getColumnIndex(KEYS)), cursor.getString(cursor.getColumnIndex(KEYS)),
cursor.getString(cursor.getColumnIndex(AVATAR))); cursor.getString(cursor.getColumnIndex(AVATAR)),
lastseen);
} }
public int getSubscription() { public int getSubscription() {
@ -306,9 +320,18 @@ public class Contact implements ListItem {
public static final int DIRTY_DELETE = 7; public static final int DIRTY_DELETE = 7;
} }
public class Lastseen { public static class Lastseen {
public long time = 0; public long time;
public String presence = null; public String presence;
public Lastseen() {
time = 0;
presence = null;
}
public Lastseen(final String presence, final long time) {
this.time = time;
this.presence = presence;
}
} }
@Override @Override

View File

@ -20,7 +20,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static DatabaseBackend instance = null; private static DatabaseBackend instance = null;
private static final String DATABASE_NAME = "history"; private static final String DATABASE_NAME = "history";
private static final int DATABASE_VERSION = 8; private static final int DATABASE_VERSION = 9;
private static String CREATE_CONTATCS_STATEMENT = "create table " private static String CREATE_CONTATCS_STATEMENT = "create table "
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
@ -28,6 +28,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ Contact.JID + " TEXT," + Contact.KEYS + " TEXT," + Contact.JID + " TEXT," + Contact.KEYS + " TEXT,"
+ Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER," + Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER,"
+ Contact.SYSTEMACCOUNT + " NUMBER, " + Contact.AVATAR + " TEXT, " + Contact.SYSTEMACCOUNT + " NUMBER, " + Contact.AVATAR + " TEXT, "
+ Contact.LAST_PRESENCE + " TEXT, " + Contact.LAST_TIME + " NUMBER, "
+ "FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES " + "FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
+ Account.TABLENAME + "(" + Account.UUID + Account.TABLENAME + "(" + Account.UUID
+ ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT + ", " + ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT + ", "
@ -101,6 +102,12 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + Conversation.TABLENAME + " ADD COLUMN " db.execSQL("ALTER TABLE " + Conversation.TABLENAME + " ADD COLUMN "
+ Conversation.ATTRIBUTES + " TEXT"); + Conversation.ATTRIBUTES + " TEXT");
} }
if (oldVersion < 9 && newVersion >= 9) {
db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
+ Contact.LAST_TIME + " NUMBER");
db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
+ Contact.LAST_PRESENCE + " TEXT");
}
} }
public static synchronized DatabaseBackend getInstance(Context context) { public static synchronized DatabaseBackend getInstance(Context context) {