From 4875b52f09eba8e32285e92372f076413d4f649f Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Thu, 21 Aug 2014 12:32:50 +0200 Subject: [PATCH] make avatars persisent / available even without internet --- .../siacs/conversations/entities/Account.java | 19 ++++++++++++----- .../siacs/conversations/entities/Contact.java | 18 +++++++++++----- .../siacs/conversations/entities/Message.java | 21 +++++++++++++++---- .../conversations/parser/MessageParser.java | 4 +++- .../persistance/DatabaseBackend.java | 10 ++++++++- .../services/XmppConnectionService.java | 12 ++++++++--- 6 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/eu/siacs/conversations/entities/Account.java b/src/eu/siacs/conversations/entities/Account.java index 19da1fafa..d31d23249 100644 --- a/src/eu/siacs/conversations/entities/Account.java +++ b/src/eu/siacs/conversations/entities/Account.java @@ -31,6 +31,7 @@ public class Account extends AbstractEntity { public static final String OPTIONS = "options"; public static final String ROSTERVERSION = "rosterversion"; public static final String KEYS = "keys"; + public static final String AVATAR = "avatar"; public static final int OPTION_USETLS = 0; public static final int OPTION_DISABLED = 1; @@ -81,11 +82,11 @@ public class Account extends AbstractEntity { public Account(String username, String server, String password) { this(java.util.UUID.randomUUID().toString(), username, server, - password, 0, null, ""); + password, 0, null, "",null); } public Account(String uuid, String username, String server, - String password, int options, String rosterVersion, String keys) { + String password, int options, String rosterVersion, String keys, String avatar) { this.uuid = uuid; this.username = username; this.server = server; @@ -97,6 +98,7 @@ public class Account extends AbstractEntity { } catch (JSONException e) { } + this.avatar = avatar; } public boolean isOptionSet(int option) { @@ -209,6 +211,7 @@ public class Account extends AbstractEntity { values.put(OPTIONS, options); values.put(KEYS, this.keys.toString()); values.put(ROSTERVERSION, rosterVersion); + values.put(AVATAR, avatar); return values; } @@ -219,7 +222,8 @@ public class Account extends AbstractEntity { cursor.getString(cursor.getColumnIndex(PASSWORD)), cursor.getInt(cursor.getColumnIndex(OPTIONS)), cursor.getString(cursor.getColumnIndex(ROSTERVERSION)), - cursor.getString(cursor.getColumnIndex(KEYS))); + cursor.getString(cursor.getColumnIndex(KEYS)), + cursor.getString(cursor.getColumnIndex(AVATAR))); } public OtrEngine getOtrEngine(Context context) { @@ -346,8 +350,13 @@ public class Account extends AbstractEntity { } } - public void setAvatar(String filename) { - this.avatar = filename; + public boolean setAvatar(String filename) { + if (this.avatar != null && this.avatar.equals(filename)) { + return false; + } else { + this.avatar = filename; + return true; + } } public String getAvatar() { diff --git a/src/eu/siacs/conversations/entities/Contact.java b/src/eu/siacs/conversations/entities/Contact.java index 47a3a0d7f..ab05b9d10 100644 --- a/src/eu/siacs/conversations/entities/Contact.java +++ b/src/eu/siacs/conversations/entities/Contact.java @@ -15,7 +15,6 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; public class Contact implements ListItem { public static final String TABLENAME = "contacts"; @@ -28,6 +27,7 @@ public class Contact implements ListItem { public static final String PHOTOURI = "photouri"; public static final String KEYS = "pgpkey"; public static final String ACCOUNT = "accountUuid"; + public static final String AVATAR = "avatar"; protected String accountUuid; protected String systemName; @@ -48,7 +48,7 @@ public class Contact implements ListItem { public Contact(String account, String systemName, String serverName, String jid, int subscription, String photoUri, - String systemAccount, String keys) { + String systemAccount, String keys, String avatar) { this.accountUuid = account; this.systemName = systemName; this.serverName = serverName; @@ -64,6 +64,7 @@ public class Contact implements ListItem { } catch (JSONException e) { this.keys = new JSONObject(); } + this.avatar = avatar; } public Contact(String jid) { @@ -105,6 +106,7 @@ public class Contact implements ListItem { values.put(SYSTEMACCOUNT, systemAccount); values.put(PHOTOURI, photoUri); values.put(KEYS, keys.toString()); + values.put(AVATAR,avatar); return values; } @@ -116,7 +118,8 @@ public class Contact implements ListItem { cursor.getInt(cursor.getColumnIndex(OPTIONS)), cursor.getString(cursor.getColumnIndex(PHOTOURI)), cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)), - cursor.getString(cursor.getColumnIndex(KEYS))); + cursor.getString(cursor.getColumnIndex(KEYS)), + cursor.getString(cursor.getColumnIndex(AVATAR))); } public int getSubscription() { @@ -332,7 +335,12 @@ public class Contact implements ListItem { } } - public void setAvatar(String filename) { - this.avatar = filename; + public boolean setAvatar(String filename) { + if (this.avatar != null && this.avatar.equals(filename)) { + return false; + } else { + this.avatar = filename; + return true; + } } } diff --git a/src/eu/siacs/conversations/entities/Message.java b/src/eu/siacs/conversations/entities/Message.java index 17b4e5b8a..b669efc5a 100644 --- a/src/eu/siacs/conversations/entities/Message.java +++ b/src/eu/siacs/conversations/entities/Message.java @@ -43,6 +43,7 @@ public class Message extends AbstractEntity { public static String ENCRYPTION = "encryption"; public static String STATUS = "status"; public static String TYPE = "type"; + public static String REMOTE_MSG_ID = "remoteMsgId"; protected String conversationUuid; protected String counterpart; @@ -54,6 +55,7 @@ public class Message extends AbstractEntity { protected int status; protected int type; protected boolean read = true; + protected String remoteMsgId = null; protected transient Conversation conversation = null; @@ -66,17 +68,17 @@ public class Message extends AbstractEntity { public Message(Conversation conversation, String body, int encryption) { this(java.util.UUID.randomUUID().toString(), conversation.getUuid(), conversation.getContactJid(), null, body, System.currentTimeMillis(), encryption, - Message.STATUS_UNSEND,TYPE_TEXT); + Message.STATUS_UNSEND,TYPE_TEXT,null); this.conversation = conversation; } public Message(Conversation conversation, String counterpart, String body, int encryption, int status) { - this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, null, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT); + this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, null, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT,null); this.conversation = conversation; } public Message(String uuid, String conversationUUid, String counterpart, String trueCounterpart, - String body, long timeSent, int encryption, int status, int type) { + String body, long timeSent, int encryption, int status, int type, String remoteMsgId) { this.uuid = uuid; this.conversationUuid = conversationUUid; this.counterpart = counterpart; @@ -86,6 +88,7 @@ public class Message extends AbstractEntity { this.encryption = encryption; this.status = status; this.type = type; + this.remoteMsgId = remoteMsgId; } @Override @@ -100,6 +103,7 @@ public class Message extends AbstractEntity { values.put(ENCRYPTION, encryption); values.put(STATUS, status); values.put(TYPE, type); + values.put(REMOTE_MSG_ID,remoteMsgId); return values; } @@ -162,6 +166,14 @@ public class Message extends AbstractEntity { public int getStatus() { return status; } + + public String getRemoteMsgId() { + return this.remoteMsgId; + } + + public void setRemoteMsgId(String id) { + this.remoteMsgId = id; + } public static Message fromCursor(Cursor cursor) { return new Message(cursor.getString(cursor.getColumnIndex(UUID)), @@ -172,7 +184,8 @@ public class Message extends AbstractEntity { cursor.getLong(cursor.getColumnIndex(TIME_SENT)), cursor.getInt(cursor.getColumnIndex(ENCRYPTION)), cursor.getInt(cursor.getColumnIndex(STATUS)), - cursor.getInt(cursor.getColumnIndex(TYPE))); + cursor.getInt(cursor.getColumnIndex(TYPE)), + cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID))); } public void setConversation(Conversation conv) { diff --git a/src/eu/siacs/conversations/parser/MessageParser.java b/src/eu/siacs/conversations/parser/MessageParser.java index 7de7e9e13..e3cb1a1e2 100644 --- a/src/eu/siacs/conversations/parser/MessageParser.java +++ b/src/eu/siacs/conversations/parser/MessageParser.java @@ -301,7 +301,9 @@ public class MessageParser extends AbstractParser implements if (mXmppConnectionService.getFileBackend().isAvatarCached( avatar)) { if (account.getJid().equals(from)) { - account.setAvatar(avatar.getFilename()); + if (account.setAvatar(avatar.getFilename())) { + mXmppConnectionService.databaseBackend.updateAccount(account); + } } else { Contact contact = account.getRoster().getContact(from); contact.setAvatar(avatar.getFilename()); diff --git a/src/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/eu/siacs/conversations/persistance/DatabaseBackend.java index c3f0d9ee4..20cc6fb75 100644 --- a/src/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -20,7 +20,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { private static DatabaseBackend instance = null; private static final String DATABASE_NAME = "history"; - private static final int DATABASE_VERSION = 6; + private static final int DATABASE_VERSION = 7; private static String CREATE_CONTATCS_STATEMENT = "create table " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " @@ -86,6 +86,14 @@ public class DatabaseBackend extends SQLiteOpenHelper { db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.TRUE_COUNTERPART + " TEXT"); } + if (oldVersion < 7 && newVersion >= 7) { + db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + + Message.REMOTE_MSG_ID + " TEXT"); + db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + + Contact.AVATAR + " TEXT"); + db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + + Account.AVATAR + " TEXT"); + } } public static synchronized DatabaseBackend getInstance(Context context) { diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 593b1fd09..a6853f8ad 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -1218,7 +1218,9 @@ public class XmppConnectionService extends Service { @Override public void onIqPacketReceived(Account account, IqPacket result) { if (result.getType() == IqPacket.TYPE_RESULT) { - account.setAvatar(avatar.getFilename()); + if (account.setAvatar(avatar.getFilename())) { + databaseBackend.updateAccount(account); + } callback.success(avatar); } else { callback.error(R.string.error_publish_avatar_server_reject, avatar); @@ -1250,7 +1252,9 @@ public class XmppConnectionService extends Service { if (avatar.image!=null) { if (getFileBackend().save(avatar)) { if (account.getJid().equals(avatar.owner)) { - account.setAvatar(avatar.getFilename()); + if (account.setAvatar(avatar.getFilename())) { + databaseBackend.updateAccount(account); + } } else { Contact contact = account.getRoster().getContact(avatar.owner); contact.setAvatar(avatar.getFilename()); @@ -1283,7 +1287,9 @@ public class XmppConnectionService extends Service { if (avatar!=null) { avatar.owner = account.getJid(); if (fileBackend.isAvatarCached(avatar)) { - account.setAvatar(avatar.getFilename()); + if (account.setAvatar(avatar.getFilename())) { + databaseBackend.updateAccount(account); + } callback.success(avatar); } else { fetchAvatar(account, avatar,callback);