improved 'next encryption' selection

This commit is contained in:
Daniel Gultsch 2015-07-31 00:52:46 +02:00
parent 5191e4ce95
commit 658919f239
6 changed files with 72 additions and 76 deletions

View file

@ -549,43 +549,52 @@ public class Conversation extends AbstractEntity implements Blockable {
return this.nextCounterpart; return this.nextCounterpart;
} }
public int getLatestEncryption() { private int getMostRecentlyUsedOutgoingEncryption() {
int latestEncryption = this.getLatestMessage().getEncryption(); synchronized (this.messages) {
if ((latestEncryption == Message.ENCRYPTION_DECRYPTED) for(int i = this.messages.size() -1; i >= 0; --i) {
|| (latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) { final Message m = this.messages.get(0);
if (!m.isCarbon() && m.getStatus() != Message.STATUS_RECEIVED) {
final int e = m.getEncryption();
if (e == Message.ENCRYPTION_DECRYPTED || e == Message.ENCRYPTION_DECRYPTION_FAILED) {
return Message.ENCRYPTION_PGP; return Message.ENCRYPTION_PGP;
} else { } else {
return latestEncryption; return e;
} }
} }
}
}
return Message.ENCRYPTION_NONE;
}
public int getNextEncryption(boolean force) { private int getMostRecentlyUsedIncomingEncryption() {
synchronized (this.messages) {
for(int i = this.messages.size() -1; i >= 0; --i) {
final Message m = this.messages.get(0);
if (m.getStatus() == Message.STATUS_RECEIVED) {
final int e = m.getEncryption();
if (e == Message.ENCRYPTION_DECRYPTED || e == Message.ENCRYPTION_DECRYPTION_FAILED) {
return Message.ENCRYPTION_PGP;
} else {
return e;
}
}
}
}
return Message.ENCRYPTION_NONE;
}
public int getNextEncryption() {
int next = this.getIntAttribute(ATTRIBUTE_NEXT_ENCRYPTION, -1); int next = this.getIntAttribute(ATTRIBUTE_NEXT_ENCRYPTION, -1);
if (next == -1) { if (next == -1) {
int latest = this.getLatestEncryption(); int outgoing = this.getMostRecentlyUsedOutgoingEncryption();
if (latest == Message.ENCRYPTION_NONE) { if (outgoing == Message.ENCRYPTION_NONE) {
if (force && getMode() == MODE_SINGLE) { return this.getMostRecentlyUsedIncomingEncryption();
return Message.ENCRYPTION_OTR;
} else if (getContact().getPresences().size() == 1) {
if (getContact().getOtrFingerprints().size() >= 1) {
return Message.ENCRYPTION_OTR;
} else { } else {
return latest; return outgoing;
}
} else {
return latest;
}
} else {
return latest;
} }
} }
if (next == Message.ENCRYPTION_NONE && force
&& getMode() == MODE_SINGLE) {
return Message.ENCRYPTION_OTR;
} else {
return next; return next;
} }
}
public void setNextEncryption(int encryption) { public void setNextEncryption(int encryption) {
this.setAttribute(ATTRIBUTE_NEXT_ENCRYPTION, String.valueOf(encryption)); this.setAttribute(ATTRIBUTE_NEXT_ENCRYPTION, String.valueOf(encryption));

View file

@ -727,7 +727,7 @@ public class Message extends AbstractEntity {
} }
return iterator.getEncryption(); return iterator.getEncryption();
} }
return conversation.getNextEncryption(false); return conversation.getNextEncryption();
} }
public boolean isValidInSession() { public boolean isValidInSession() {

View file

@ -73,11 +73,9 @@ public class MessageParser extends AbstractParser implements
body = otrSession.transformReceiving(body); body = otrSession.transformReceiving(body);
SessionStatus status = otrSession.getSessionStatus(); SessionStatus status = otrSession.getSessionStatus();
if (body == null && status == SessionStatus.ENCRYPTED) { if (body == null && status == SessionStatus.ENCRYPTED) {
conversation.setNextEncryption(Message.ENCRYPTION_OTR);
mXmppConnectionService.onOtrSessionEstablished(conversation); mXmppConnectionService.onOtrSessionEstablished(conversation);
return null; return null;
} else if (body == null && status == SessionStatus.FINISHED) { } else if (body == null && status == SessionStatus.FINISHED) {
conversation.setNextEncryption(Message.ENCRYPTION_NONE);
conversation.resetOtrSession(); conversation.resetOtrSession();
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
return null; return null;

View file

@ -349,7 +349,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void attachLocationToConversation(final Conversation conversation, public void attachLocationToConversation(final Conversation conversation,
final Uri uri, final Uri uri,
final UiCallback<Message> callback) { final UiCallback<Message> callback) {
int encryption = conversation.getNextEncryption(forceEncryption()); int encryption = conversation.getNextEncryption();
if (encryption == Message.ENCRYPTION_PGP) { if (encryption == Message.ENCRYPTION_PGP) {
encryption = Message.ENCRYPTION_DECRYPTED; encryption = Message.ENCRYPTION_DECRYPTED;
} }
@ -368,12 +368,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
final Uri uri, final Uri uri,
final UiCallback<Message> callback) { final UiCallback<Message> callback) {
final Message message; final Message message;
if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
message = new Message(conversation, "", message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
Message.ENCRYPTION_DECRYPTED);
} else { } else {
message = new Message(conversation, "", message = new Message(conversation, "", conversation.getNextEncryption());
conversation.getNextEncryption(forceEncryption()));
} }
message.setCounterpart(conversation.getNextCounterpart()); message.setCounterpart(conversation.getNextCounterpart());
message.setType(Message.TYPE_FILE); message.setType(Message.TYPE_FILE);
@ -409,12 +407,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void attachImageToConversation(final Conversation conversation, public void attachImageToConversation(final Conversation conversation,
final Uri uri, final UiCallback<Message> callback) { final Uri uri, final UiCallback<Message> callback) {
final Message message; final Message message;
if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
message = new Message(conversation, "", message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
Message.ENCRYPTION_DECRYPTED);
} else { } else {
message = new Message(conversation, "", message = new Message(conversation, "",conversation.getNextEncryption());
conversation.getNextEncryption(forceEncryption()));
} }
message.setCounterpart(conversation.getNextCounterpart()); message.setCounterpart(conversation.getNextCounterpart());
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
@ -424,7 +420,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void run() { public void run() {
try { try {
getFileBackend().copyImageToPrivateStorage(message, uri); getFileBackend().copyImageToPrivateStorage(message, uri);
if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
getPgpEngine().encrypt(message, callback); getPgpEngine().encrypt(message, callback);
} else { } else {
callback.success(message); callback.success(message);

View file

@ -385,7 +385,7 @@ public class ConversationActivity extends XmppActivity
} else { } else {
menuAdd.setVisible(!isConversationsOverviewHideable()); menuAdd.setVisible(!isConversationsOverviewHideable());
if (this.getSelectedConversation() != null) { if (this.getSelectedConversation() != null) {
if (this.getSelectedConversation().getNextEncryption(forceEncryption()) != Message.ENCRYPTION_NONE) { if (this.getSelectedConversation().getNextEncryption() != Message.ENCRYPTION_NONE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
menuSecure.setIcon(R.drawable.ic_lock_white_24dp); menuSecure.setIcon(R.drawable.ic_lock_white_24dp);
} else { } else {
@ -498,7 +498,7 @@ public class ConversationActivity extends XmppActivity
break; break;
} }
final Conversation conversation = getSelectedConversation(); final Conversation conversation = getSelectedConversation();
final int encryption = conversation.getNextEncryption(forceEncryption()); final int encryption = conversation.getNextEncryption();
if (encryption == Message.ENCRYPTION_PGP) { if (encryption == Message.ENCRYPTION_PGP) {
if (hasPgp()) { if (hasPgp()) {
if (conversation.getContact().getPgpKeyId() != 0) { if (conversation.getContact().getPgpKeyId() != 0) {
@ -787,15 +787,10 @@ public class ConversationActivity extends XmppActivity
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
otr.setEnabled(false); otr.setEnabled(false);
axolotl.setEnabled(false); axolotl.setEnabled(false);
} else { } else if (!conversation.getAccount().getAxolotlService().isContactAxolotlCapable(conversation.getContact())) {
if (forceEncryption()) {
none.setVisible(false);
}
}
if (!conversation.getAccount().getAxolotlService().isContactAxolotlCapable(conversation.getContact())) {
axolotl.setEnabled(false); axolotl.setEnabled(false);
} }
switch (conversation.getNextEncryption(forceEncryption())) { switch (conversation.getNextEncryption()) {
case Message.ENCRYPTION_NONE: case Message.ENCRYPTION_NONE:
none.setChecked(true); none.setChecked(true);
break; break;
@ -806,8 +801,7 @@ public class ConversationActivity extends XmppActivity
pgp.setChecked(true); pgp.setChecked(true);
break; break;
case Message.ENCRYPTION_AXOLOTL: case Message.ENCRYPTION_AXOLOTL:
popup.getMenu().findItem(R.id.encryption_choice_axolotl) axolotl.setChecked(true);
.setChecked(true);
break; break;
default: default:
none.setChecked(true); none.setChecked(true);
@ -820,8 +814,7 @@ public class ConversationActivity extends XmppActivity
protected void muteConversationDialog(final Conversation conversation) { protected void muteConversationDialog(final Conversation conversation) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.disable_notifications); builder.setTitle(R.string.disable_notifications);
final int[] durations = getResources().getIntArray( final int[] durations = getResources().getIntArray(R.array.mute_options_durations);
R.array.mute_options_durations);
builder.setItems(R.array.mute_options_descriptions, builder.setItems(R.array.mute_options_descriptions,
new OnClickListener() { new OnClickListener() {
@ -1253,10 +1246,6 @@ public class ConversationActivity extends XmppActivity
}); });
} }
public boolean forceEncryption() {
return getPreferences().getBoolean("force_encryption", false);
}
public boolean useSendButtonToIndicateStatus() { public boolean useSendButtonToIndicateStatus() {
return getPreferences().getBoolean("send_button_status", false); return getPreferences().getBoolean("send_button_status", false);
} }

View file

@ -293,22 +293,26 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
if (body.length() == 0 || this.conversation == null) { if (body.length() == 0 || this.conversation == null) {
return; return;
} }
Message message = new Message(conversation, body, conversation.getNextEncryption(activity.forceEncryption())); Message message = new Message(conversation, body, conversation.getNextEncryption());
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
if (conversation.getNextCounterpart() != null) { if (conversation.getNextCounterpart() != null) {
message.setCounterpart(conversation.getNextCounterpart()); message.setCounterpart(conversation.getNextCounterpart());
message.setType(Message.TYPE_PRIVATE); message.setType(Message.TYPE_PRIVATE);
} }
} }
if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_OTR) { switch (conversation.getNextEncryption()) {
case Message.ENCRYPTION_OTR:
sendOtrMessage(message); sendOtrMessage(message);
} else if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_PGP) { break;
case Message.ENCRYPTION_PGP:
sendPgpMessage(message); sendPgpMessage(message);
} else if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_AXOLOTL) { break;
case Message.ENCRYPTION_AXOLOTL:
if(!activity.trustKeysIfNeeded(ConversationActivity.REQUEST_TRUST_KEYS_TEXT)) { if(!activity.trustKeysIfNeeded(ConversationActivity.REQUEST_TRUST_KEYS_TEXT)) {
sendAxolotlMessage(message); sendAxolotlMessage(message);
} }
} else { break;
default:
sendPlainTextMessage(message); sendPlainTextMessage(message);
} }
} }
@ -320,7 +324,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
R.string.send_private_message_to, R.string.send_private_message_to,
conversation.getNextCounterpart().getResourcepart())); conversation.getNextCounterpart().getResourcepart()));
} else { } else {
switch (conversation.getNextEncryption(activity.forceEncryption())) { switch (conversation.getNextEncryption()) {
case Message.ENCRYPTION_NONE: case Message.ENCRYPTION_NONE:
mEditMessage mEditMessage
.setHint(getString(R.string.send_plain_text_message)); .setHint(getString(R.string.send_plain_text_message));
@ -1211,11 +1215,11 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
if (requestCode == ConversationActivity.REQUEST_TRUST_KEYS_TEXT) { if (requestCode == ConversationActivity.REQUEST_TRUST_KEYS_TEXT) {
final String body = mEditMessage.getText().toString(); final String body = mEditMessage.getText().toString();
Message message = new Message(conversation, body, conversation.getNextEncryption(activity.forceEncryption())); Message message = new Message(conversation, body, conversation.getNextEncryption());
sendAxolotlMessage(message); sendAxolotlMessage(message);
} else if (requestCode == ConversationActivity.REQUEST_TRUST_KEYS_MENU) { } else if (requestCode == ConversationActivity.REQUEST_TRUST_KEYS_MENU) {
int choice = data.getIntExtra("choice", ConversationActivity.ATTACHMENT_CHOICE_INVALID); int choice = data.getIntExtra("choice", ConversationActivity.ATTACHMENT_CHOICE_INVALID);
activity.selectPresenceToAttachFile(choice, conversation.getNextEncryption(activity.forceEncryption())); activity.selectPresenceToAttachFile(choice, conversation.getNextEncryption());
} }
} }
} }