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.concurrent.CopyOnWriteArrayList;
import org.json.JSONException;
import org.json.JSONObject;
import eu.siacs.conversations.services.XmppConnectionService;
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 CREATED = "created";
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 contactUuid;
@ -44,6 +51,8 @@ public class Conversation extends AbstractEntity {
private int status;
private long created;
private int mode;
private JSONObject attributes = new JSONObject();
private long mutedTill = 0;
@ -56,7 +65,6 @@ public class Conversation extends AbstractEntity {
private transient String otrFingerprint = null;
private int nextMessageEncryption = -1;
private String nextMessage;
private transient MucOptions mucOptions = null;
@ -73,13 +81,13 @@ public class Conversation extends AbstractEntity {
int mode) {
this(java.util.UUID.randomUUID().toString(), name, null, account
.getUuid(), contactJid, System.currentTimeMillis(),
STATUS_AVAILABLE, mode);
STATUS_AVAILABLE, mode,"");
this.account = account;
}
public Conversation(String uuid, String name, String contactUuid,
String accountUuid, String contactJid, long created, int status,
int mode) {
int mode, String attributes) {
this.uuid = uuid;
this.name = name;
this.contactUuid = contactUuid;
@ -88,6 +96,14 @@ public class Conversation extends AbstractEntity {
this.created = created;
this.status = status;
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() {
@ -198,6 +214,7 @@ public class Conversation extends AbstractEntity {
values.put(CREATED, created);
values.put(STATUS, status);
values.put(MODE, mode);
values.put(ATTRIBUTES,attributes.toString());
return values;
}
@ -209,7 +226,8 @@ public class Conversation extends AbstractEntity {
cursor.getString(cursor.getColumnIndex(CONTACTJID)),
cursor.getLong(cursor.getColumnIndex(CREATED)),
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) {
@ -345,7 +363,8 @@ public class Conversation extends AbstractEntity {
}
public int getNextEncryption(boolean force) {
if (this.nextMessageEncryption == -1) {
int next = this.getIntAttribute(ATTRIBUTE_NEXT_ENCRYPTION, -1);
if (next == -1) {
int latest = this.getLatestEncryption();
if (latest == Message.ENCRYPTION_NONE) {
if (force && getMode() == MODE_SINGLE) {
@ -363,16 +382,16 @@ public class Conversation extends AbstractEntity {
return latest;
}
}
if (this.nextMessageEncryption == Message.ENCRYPTION_NONE && force
if (next == Message.ENCRYPTION_NONE && force
&& getMode() == MODE_SINGLE) {
return Message.ENCRYPTION_OTR;
} else {
return this.nextMessageEncryption;
return next;
}
}
public void setNextEncryption(int encryption) {
this.nextMessageEncryption = encryption;
this.setAttribute(ATTRIBUTE_NEXT_ENCRYPTION, String.valueOf(encryption));
}
public String getNextMessage() {
@ -440,4 +459,34 @@ public class Conversation extends AbstractEntity {
public boolean isMuted() {
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() {
if (conversation.getBookmark() != null
this.password = conversation.getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD);
if (this.password == null && conversation.getBookmark() != null
&& conversation.getBookmark().getPassword() != null) {
return conversation.getBookmark().getPassword();
} else {
@ -338,6 +339,7 @@ public class MucOptions {
} else {
this.password = password;
}
conversation.setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password);
}
public boolean isPasswordChanged() {

View File

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

View File

@ -19,7 +19,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static DatabaseBackend instance = null;
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 "
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
@ -50,8 +50,8 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ " TEXT, " + Conversation.CONTACT + " TEXT, "
+ Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACTJID
+ " TEXT, " + Conversation.CREATED + " NUMBER, "
+ Conversation.STATUS + " NUMBER," + Conversation.MODE
+ " NUMBER," + "FOREIGN KEY(" + Conversation.ACCOUNT
+ Conversation.STATUS + " NUMBER, " + Conversation.MODE
+ " NUMBER, "+Conversation.ATTRIBUTES + " TEXT, FOREIGN KEY(" + Conversation.ACCOUNT
+ ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID
+ ") ON DELETE CASCADE);");
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 "
+ 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) {

View File

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

View File

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

View File

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

View File

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