made muc passwords and prefereced encryption method persistant across restarts

This commit is contained in:
iNPUTmice 2014-09-27 18:16:31 +02:00
parent 4a9ed0e208
commit 1ae9338fc9
8 changed files with 75 additions and 12 deletions

View File

@ -4,6 +4,9 @@ import java.security.interfaces.DSAPublicKey;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import org.json.JSONException;
import org.json.JSONObject;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
@ -36,6 +39,10 @@ public class Conversation extends AbstractEntity {
public static final String STATUS = "status"; public static final String STATUS = "status";
public static final String CREATED = "created"; public static final String CREATED = "created";
public static final String MODE = "mode"; public static final String MODE = "mode";
public static final String ATTRIBUTES = "attributes";
public static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption";
public static final String ATTRIBUTE_MUC_PASSWORD = "muc_password";
private String name; private String name;
private String contactUuid; private String contactUuid;
@ -45,6 +52,8 @@ public class Conversation extends AbstractEntity {
private long created; private long created;
private int mode; private int mode;
private JSONObject attributes = new JSONObject();
private long mutedTill = 0; private long mutedTill = 0;
private String nextPresence; private String nextPresence;
@ -56,7 +65,6 @@ public class Conversation extends AbstractEntity {
private transient String otrFingerprint = null; private transient String otrFingerprint = null;
private int nextMessageEncryption = -1;
private String nextMessage; private String nextMessage;
private transient MucOptions mucOptions = null; private transient MucOptions mucOptions = null;
@ -73,13 +81,13 @@ public class Conversation extends AbstractEntity {
int mode) { int mode) {
this(java.util.UUID.randomUUID().toString(), name, null, account this(java.util.UUID.randomUUID().toString(), name, null, account
.getUuid(), contactJid, System.currentTimeMillis(), .getUuid(), contactJid, System.currentTimeMillis(),
STATUS_AVAILABLE, mode); STATUS_AVAILABLE, mode,"");
this.account = account; this.account = account;
} }
public Conversation(String uuid, String name, String contactUuid, public Conversation(String uuid, String name, String contactUuid,
String accountUuid, String contactJid, long created, int status, String accountUuid, String contactJid, long created, int status,
int mode) { int mode, String attributes) {
this.uuid = uuid; this.uuid = uuid;
this.name = name; this.name = name;
this.contactUuid = contactUuid; this.contactUuid = contactUuid;
@ -88,6 +96,14 @@ public class Conversation extends AbstractEntity {
this.created = created; this.created = created;
this.status = status; this.status = status;
this.mode = mode; this.mode = mode;
try {
if (attributes==null) {
attributes = new String();
}
this.attributes = new JSONObject(attributes);
} catch (JSONException e) {
this.attributes = new JSONObject();
}
} }
public List<Message> getMessages() { public List<Message> getMessages() {
@ -198,6 +214,7 @@ public class Conversation extends AbstractEntity {
values.put(CREATED, created); values.put(CREATED, created);
values.put(STATUS, status); values.put(STATUS, status);
values.put(MODE, mode); values.put(MODE, mode);
values.put(ATTRIBUTES,attributes.toString());
return values; return values;
} }
@ -209,7 +226,8 @@ public class Conversation extends AbstractEntity {
cursor.getString(cursor.getColumnIndex(CONTACTJID)), cursor.getString(cursor.getColumnIndex(CONTACTJID)),
cursor.getLong(cursor.getColumnIndex(CREATED)), cursor.getLong(cursor.getColumnIndex(CREATED)),
cursor.getInt(cursor.getColumnIndex(STATUS)), cursor.getInt(cursor.getColumnIndex(STATUS)),
cursor.getInt(cursor.getColumnIndex(MODE))); cursor.getInt(cursor.getColumnIndex(MODE)),
cursor.getString(cursor.getColumnIndex(ATTRIBUTES)));
} }
public void setStatus(int status) { public void setStatus(int status) {
@ -345,7 +363,8 @@ public class Conversation extends AbstractEntity {
} }
public int getNextEncryption(boolean force) { public int getNextEncryption(boolean force) {
if (this.nextMessageEncryption == -1) { int next = this.getIntAttribute(ATTRIBUTE_NEXT_ENCRYPTION, -1);
if (next == -1) {
int latest = this.getLatestEncryption(); int latest = this.getLatestEncryption();
if (latest == Message.ENCRYPTION_NONE) { if (latest == Message.ENCRYPTION_NONE) {
if (force && getMode() == MODE_SINGLE) { if (force && getMode() == MODE_SINGLE) {
@ -363,16 +382,16 @@ public class Conversation extends AbstractEntity {
return latest; return latest;
} }
} }
if (this.nextMessageEncryption == Message.ENCRYPTION_NONE && force if (next == Message.ENCRYPTION_NONE && force
&& getMode() == MODE_SINGLE) { && getMode() == MODE_SINGLE) {
return Message.ENCRYPTION_OTR; return Message.ENCRYPTION_OTR;
} else { } else {
return this.nextMessageEncryption; return next;
} }
} }
public void setNextEncryption(int encryption) { public void setNextEncryption(int encryption) {
this.nextMessageEncryption = encryption; this.setAttribute(ATTRIBUTE_NEXT_ENCRYPTION, String.valueOf(encryption));
} }
public String getNextMessage() { public String getNextMessage() {
@ -440,4 +459,34 @@ public class Conversation extends AbstractEntity {
public boolean isMuted() { public boolean isMuted() {
return SystemClock.elapsedRealtime() < this.mutedTill; return SystemClock.elapsedRealtime() < this.mutedTill;
} }
public boolean setAttribute(String key, String value) {
try {
this.attributes.put(key, value);
return true;
} catch (JSONException e) {
return false;
}
}
public String getAttribute(String key) {
try {
return this.attributes.getString(key);
} catch (JSONException e) {
return null;
}
}
public int getIntAttribute(String key, int defaultValue) {
String value = this.getAttribute(key);
if (value==null) {
return defaultValue;
} else {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
return defaultValue;
}
}
}
} }

View File

@ -323,7 +323,8 @@ public class MucOptions {
} }
public String getPassword() { public String getPassword() {
if (conversation.getBookmark() != null this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
if (this.password == null && conversation.getBookmark() != null
&& conversation.getBookmark().getPassword() != null) { && conversation.getBookmark().getPassword() != null) {
return conversation.getBookmark().getPassword(); return conversation.getBookmark().getPassword();
} else { } else {
@ -338,6 +339,7 @@ public class MucOptions {
} else { } else {
this.password = password; this.password = password;
} }
conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
} }
public boolean isPasswordChanged() { public boolean isPasswordChanged() {

View File

@ -298,6 +298,7 @@ public class MessageParser extends AbstractParser implements
Element password = x.findChild("password"); Element password = x.findChild("password");
conversation.getMucOptions().setPassword( conversation.getMucOptions().setPassword(
password.getContent()); password.getContent());
mXmppConnectionService.databaseBackend.updateConversation(conversation);
} }
mXmppConnectionService.joinMuc(conversation); mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
@ -313,6 +314,7 @@ public class MessageParser extends AbstractParser implements
if (!conversation.getMucOptions().online()) { if (!conversation.getMucOptions().online()) {
if (password != null) { if (password != null) {
conversation.getMucOptions().setPassword(password); conversation.getMucOptions().setPassword(password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
} }
mXmppConnectionService.joinMuc(conversation); mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();

View File

@ -19,7 +19,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 = 7; private static final int DATABASE_VERSION = 8;
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, "
@ -50,8 +50,8 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ " TEXT, " + Conversation.CONTACT + " TEXT, " + " TEXT, " + Conversation.CONTACT + " TEXT, "
+ Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACTJID + Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACTJID
+ " TEXT, " + Conversation.CREATED + " NUMBER, " + " TEXT, " + Conversation.CREATED + " NUMBER, "
+ Conversation.STATUS + " NUMBER," + Conversation.MODE + Conversation.STATUS + " NUMBER, " + Conversation.MODE
+ " NUMBER," + "FOREIGN KEY(" + Conversation.ACCOUNT + " NUMBER, "+Conversation.ATTRIBUTES + " TEXT, FOREIGN KEY(" + Conversation.ACCOUNT
+ ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID
+ ") ON DELETE CASCADE);"); + ") ON DELETE CASCADE);");
db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID
@ -96,6 +96,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN "
+ Account.AVATAR + " TEXT"); + Account.AVATAR + " TEXT");
} }
if (oldVersion < 8 && newVersion >= 8) {
db.execSQL("ALTER TABLE " + Conversation.TABLENAME + " ADD COLUMN "
+ Conversation.ATTRIBUTES + " TEXT");
}
} }
public static synchronized DatabaseBackend getInstance(Context context) { public static synchronized DatabaseBackend getInstance(Context context) {

View File

@ -1119,6 +1119,7 @@ public class XmppConnectionService extends Service {
} }
pushBookmarks(conversation.getAccount()); pushBookmarks(conversation.getAccount());
} }
databaseBackend.updateConversation(conversation);
joinMuc(conversation); joinMuc(conversation);
} }
} }

View File

@ -297,6 +297,7 @@ public class ConversationActivity extends XmppActivity implements
int which) { int which) {
conversation conversation
.setNextEncryption(Message.ENCRYPTION_NONE); .setNextEncryption(Message.ENCRYPTION_NONE);
xmppConnectionService.databaseBackend.updateConversation(conversation);
selectPresenceToAttachFile(attachmentChoice); selectPresenceToAttachFile(attachmentChoice);
} }
}); });
@ -472,6 +473,7 @@ public class ConversationActivity extends XmppActivity implements
conversation.setNextEncryption(Message.ENCRYPTION_NONE); conversation.setNextEncryption(Message.ENCRYPTION_NONE);
break; break;
} }
xmppConnectionService.databaseBackend.updateConversation(conversation);
fragment.updateChatMsgHint(); fragment.updateChatMsgHint();
return true; return true;
} }

View File

@ -668,6 +668,7 @@ public class ConversationFragment extends Fragment {
int which) { int which) {
conversation conversation
.setNextEncryption(Message.ENCRYPTION_NONE); .setNextEncryption(Message.ENCRYPTION_NONE);
xmppService.databaseBackend.updateConversation(conversation);
message.setEncryption(Message.ENCRYPTION_NONE); message.setEncryption(Message.ENCRYPTION_NONE);
xmppService.sendMessage(message); xmppService.sendMessage(message);
messageSent(); messageSent();
@ -696,6 +697,7 @@ public class ConversationFragment extends Fragment {
conversation conversation
.setNextEncryption(Message.ENCRYPTION_NONE); .setNextEncryption(Message.ENCRYPTION_NONE);
message.setEncryption(Message.ENCRYPTION_NONE); message.setEncryption(Message.ENCRYPTION_NONE);
xmppService.databaseBackend.updateConversation(conversation);
xmppService.sendMessage(message); xmppService.sendMessage(message);
messageSent(); messageSent();
} }

View File

@ -294,6 +294,7 @@ public abstract class XmppActivity extends Activity {
if (conversation != null) { if (conversation != null) {
conversation conversation
.setNextEncryption(Message.ENCRYPTION_PGP); .setNextEncryption(Message.ENCRYPTION_PGP);
xmppConnectionService.databaseBackend.updateConversation(conversation);
} }
} }