settings to force encryption and setting for not saving messages to disk. fixed #353

This commit is contained in:
iNPUTmice 2014-09-08 13:37:22 +02:00
parent 3b9fd28ef4
commit 58953e6193
7 changed files with 96 additions and 46 deletions

View file

@ -245,5 +245,10 @@
<string name="sure_delete_fingerprint">Are you sure you would like to delete this fingerprint?</string> <string name="sure_delete_fingerprint">Are you sure you would like to delete this fingerprint?</string>
<string name="ignore">Ignore</string> <string name="ignore">Ignore</string>
<string name="without_mutual_presence_updates"><b>Warning:</b> Sending this without mutual presence updates could cause unexpected problems.\n\n<small>Go to contact details to verify your presence subscriptions.</small></string> <string name="without_mutual_presence_updates"><b>Warning:</b> Sending this without mutual presence updates could cause unexpected problems.\n\n<small>Go to contact details to verify your presence subscriptions.</small></string>
<string name="pref_encryption_settings">Encryption settings</string>
<string name="pref_force_encryption">Force end-to-end encryption</string>
<string name="pref_force_encryption_summary">Always send messages encrypted (execpt for conferences)</string>
<string name="pref_dont_save_encrypted">Dont save encrypted messages</string>
<string name="pref_dont_save_encrypted_summary">Warning: This could lead to message loss</string>
</resources> </resources>

View file

@ -60,6 +60,19 @@
android:summary="@string/pref_notification_grace_period_summary" android:summary="@string/pref_notification_grace_period_summary"
android:defaultValue="true"/> android:defaultValue="true"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_encryption_settings">
<CheckBoxPreference
android:key="force_encryption"
android:title="@string/pref_force_encryption"
android:summary="@string/pref_force_encryption_summary"
android:defaultValue="false"/>
<CheckBoxPreference
android:key="dont_save_encrypted"
android:title="@string/pref_dont_save_encrypted"
android:summary="@string/pref_dont_save_encrypted_summary"
android:defaultValue="false"/>
</PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:title="@string/pref_advanced_options"> android:title="@string/pref_advanced_options">
<CheckBoxPreference <CheckBoxPreference

View file

@ -339,24 +339,29 @@ public class Conversation extends AbstractEntity {
if ((latestEncryption == Message.ENCRYPTION_DECRYPTED) if ((latestEncryption == Message.ENCRYPTION_DECRYPTED)
|| (latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) { || (latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) {
return Message.ENCRYPTION_PGP; return Message.ENCRYPTION_PGP;
} else if (latestEncryption == Message.ENCRYPTION_NONE) {
if (getContact().getPresences().size() == 1) {
if (getContact().getOtrFingerprints().size() >= 1) {
return Message.ENCRYPTION_OTR;
} else {
return latestEncryption;
}
} else {
return latestEncryption;
}
} else { } else {
return latestEncryption; return latestEncryption;
} }
} }
public int getNextEncryption() { public int getNextEncryption(boolean force) {
if (this.nextMessageEncryption == -1) { if (this.nextMessageEncryption == -1) {
return this.getLatestEncryption(); int latest = this.getLatestEncryption();
if (latest == Message.ENCRYPTION_NONE) {
if (force && getMode() == MODE_SINGLE) {
return Message.ENCRYPTION_OTR;
} else if (getContact().getPresences().size() == 1) {
if (getContact().getOtrFingerprints().size() >= 1) {
return Message.ENCRYPTION_OTR;
} else {
return latest;
}
} else {
return latest;
}
} else {
return latest;
}
} }
return this.nextMessageEncryption; return this.nextMessageEncryption;
} }

View file

@ -453,8 +453,10 @@ public class MessageParser extends AbstractParser implements
Conversation conversation = message.getConversation(); Conversation conversation = message.getConversation();
conversation.getMessages().add(message); conversation.getMessages().add(message);
if (packet.getType() != MessagePacket.TYPE_ERROR) { if (packet.getType() != MessagePacket.TYPE_ERROR) {
if (message.getEncryption() == Message.ENCRYPTION_NONE || mXmppConnectionService.saveEncryptedMessages()) {
mXmppConnectionService.databaseBackend.createMessage(message); mXmppConnectionService.databaseBackend.createMessage(message);
} }
}
notify = notify && !conversation.isMuted(); notify = notify && !conversation.isMuted();
mXmppConnectionService.notifyUi(conversation, notify); mXmppConnectionService.notifyUi(conversation, notify);
} }

View file

@ -270,12 +270,12 @@ public class XmppConnectionService extends Service {
public Message attachImageToConversation(final Conversation conversation, public Message 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() == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption(forceEncryption()) == 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.setPresence(conversation.getNextPresence()); message.setPresence(conversation.getNextPresence());
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
@ -286,7 +286,7 @@ public class XmppConnectionService extends Service {
public void run() { public void run() {
try { try {
getFileBackend().copyImageToPrivateStorage(message, uri); getFileBackend().copyImageToPrivateStorage(message, uri);
if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
getPgpEngine().encrypt(message, callback); getPgpEngine().encrypt(message, callback);
} else { } else {
callback.success(message); callback.success(message);
@ -567,9 +567,11 @@ public class XmppConnectionService extends Service {
String pgpBody = message.getEncryptedBody(); String pgpBody = message.getEncryptedBody();
String decryptedBody = message.getBody(); String decryptedBody = message.getBody();
message.setBody(pgpBody); message.setBody(pgpBody);
message.setEncryption(Message.ENCRYPTION_PGP);
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
saveInDb = false; saveInDb = false;
message.setBody(decryptedBody); message.setBody(decryptedBody);
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) { } else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (conv.hasValidOtrSession()) { if (conv.hasValidOtrSession()) {
message.setPresence(conv.getOtrSession().getSessionID() message.setPresence(conv.getOtrSession().getSessionID()
@ -583,8 +585,10 @@ public class XmppConnectionService extends Service {
} }
if (saveInDb) { if (saveInDb) {
if (message.getEncryption() == Message.ENCRYPTION_NONE || saveEncryptedMessages()) {
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
} }
}
conv.getMessages().add(message); conv.getMessages().add(message);
if ((send) && (packet != null)) { if ((send) && (packet != null)) {
if (!account.getXmppConnection().getFeatures().sm() if (!account.getXmppConnection().getFeatures().sm()
@ -1543,10 +1547,18 @@ public class XmppConnectionService extends Service {
.getDefaultSharedPreferences(getApplicationContext()); .getDefaultSharedPreferences(getApplicationContext());
} }
public boolean forceEncryption() {
return getPreferences().getBoolean("force_encryption", false);
}
public boolean confirmMessages() { public boolean confirmMessages() {
return getPreferences().getBoolean("confirm_messages", true); return getPreferences().getBoolean("confirm_messages", true);
} }
public boolean saveEncryptedMessages() {
return !getPreferences().getBoolean("dont_save_encrypted", false);
}
public void notifyUi(Conversation conversation, boolean notify) { public void notifyUi(Conversation conversation, boolean notify) {
if (mOnConversationUpdate != null) { if (mOnConversationUpdate != null) {
mOnConversationUpdate.onConversationUpdate(); mOnConversationUpdate.onConversationUpdate();

View file

@ -14,6 +14,7 @@ import eu.siacs.conversations.utils.UIHelper;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.AlertDialog; import android.app.AlertDialog;
@ -279,7 +280,7 @@ public class ConversationActivity extends XmppActivity {
private void attachFile(final int attachmentChoice) { private void attachFile(final int attachmentChoice) {
final Conversation conversation = getSelectedConversation(); final Conversation conversation = getSelectedConversation();
if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
if (hasPgp()) { if (hasPgp()) {
if (conversation.getContact().getPgpKeyId() != 0) { if (conversation.getContact().getPgpKeyId() != 0) {
xmppConnectionService.getPgpEngine().hasKey( xmppConnectionService.getPgpEngine().hasKey(
@ -323,7 +324,7 @@ public class ConversationActivity extends XmppActivity {
} else { } else {
showInstallPgpDialog(); showInstallPgpDialog();
} }
} else if (getSelectedConversation().getNextEncryption() == Message.ENCRYPTION_NONE) { } else if (getSelectedConversation().getNextEncryption(forceEncryption()) == Message.ENCRYPTION_NONE) {
selectPresenceToAttachFile(attachmentChoice); selectPresenceToAttachFile(attachmentChoice);
} else { } else {
selectPresenceToAttachFile(attachmentChoice); selectPresenceToAttachFile(attachmentChoice);
@ -439,13 +440,17 @@ public class ConversationActivity extends XmppActivity {
popup.inflate(R.menu.encryption_choices); popup.inflate(R.menu.encryption_choices);
MenuItem otr = popup.getMenu().findItem( MenuItem otr = popup.getMenu().findItem(
R.id.encryption_choice_otr); R.id.encryption_choice_otr);
MenuItem none = popup.getMenu().findItem(R.id.encryption_choice_none);
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
otr.setEnabled(false); otr.setEnabled(false);
} else {
if (forceEncryption()) {
none.setVisible(false);
} }
switch (conversation.getNextEncryption()) { }
switch (conversation.getNextEncryption(forceEncryption())) {
case Message.ENCRYPTION_NONE: case Message.ENCRYPTION_NONE:
popup.getMenu().findItem(R.id.encryption_choice_none) none.setChecked(true);
.setChecked(true);
break; break;
case Message.ENCRYPTION_OTR: case Message.ENCRYPTION_OTR:
otr.setChecked(true); otr.setChecked(true);
@ -514,21 +519,24 @@ 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_for_this_conversation); builder.setTitle(R.string.disable_notifications_for_this_conversation);
final int[] durations = getResources().getIntArray(R.array.mute_options_durations); final int[] durations = getResources().getIntArray(
builder.setItems(R.array.mute_options_descriptions, new OnClickListener() { R.array.mute_options_durations);
builder.setItems(R.array.mute_options_descriptions,
new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
long till; long till;
if (durations[which]==-1) { if (durations[which] == -1) {
till = Long.MAX_VALUE; till = Long.MAX_VALUE;
} else { } else {
till = SystemClock.elapsedRealtime() + (durations[which] * 1000); till = SystemClock.elapsedRealtime()
+ (durations[which] * 1000);
} }
conversation.setMutedTill(till); conversation.setMutedTill(till);
ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager() ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
.findFragmentByTag("conversation"); .findFragmentByTag("conversation");
if (selectedFragment!=null) { if (selectedFragment != null) {
selectedFragment.updateMessages(); selectedFragment.updateMessages();
} }
} }
@ -794,4 +802,9 @@ public class ConversationActivity extends XmppActivity {
} }
}); });
} }
public boolean forceEncryption() {
return PreferenceManager.getDefaultSharedPreferences(
getApplicationContext()).getBoolean("force_encryption", false);
}
} }

View file

@ -188,7 +188,7 @@ public class ConversationFragment extends Fragment {
return; return;
} }
Message message = new Message(conversation, mEditMessage.getText() Message message = new Message(conversation, mEditMessage.getText()
.toString(), conversation.getNextEncryption()); .toString(), conversation.getNextEncryption(activity.forceEncryption()));
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
if (conversation.getNextPresence() != null) { if (conversation.getNextPresence() != null) {
message.setPresence(conversation.getNextPresence()); message.setPresence(conversation.getNextPresence());
@ -196,9 +196,9 @@ public class ConversationFragment extends Fragment {
conversation.setNextPresence(null); conversation.setNextPresence(null);
} }
} }
if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) { if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_OTR) {
sendOtrMessage(message); sendOtrMessage(message);
} else if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { } else if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_PGP) {
sendPgpMessage(message); sendPgpMessage(message);
} else { } else {
sendPlainTextMessage(message); sendPlainTextMessage(message);
@ -212,7 +212,7 @@ public class ConversationFragment extends Fragment {
R.string.send_private_message_to, R.string.send_private_message_to,
conversation.getNextPresence())); conversation.getNextPresence()));
} else { } else {
switch (conversation.getNextEncryption()) { switch (conversation.getNextEncryption(activity.forceEncryption())) {
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));