Skip to content

Commit

Permalink
Merge pull request thunderbird#3075 from k9mail/prefer-encrypt-setting
Browse files Browse the repository at this point in the history
Prefer encrypt setting
  • Loading branch information
Valodim authored Feb 23, 2018
2 parents 04f18d8 + f1fd332 commit 8e12633
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 12 deletions.
9 changes: 9 additions & 0 deletions k9mail/src/main/java/com/fsck/k9/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public boolean isDefaultAscending() {
private boolean stripSignature;
private boolean syncRemoteDeletions;
private long pgpCryptoKey;
private boolean autocryptPreferEncryptMutual;
private boolean markMessageAsReadOnView;
private boolean alwaysShowCcBcc;
private boolean allowRemoteSearch;
Expand Down Expand Up @@ -1644,6 +1645,14 @@ public void setCryptoKey(long keyId) {
pgpCryptoKey = keyId;
}

public boolean getAutocryptPreferEncryptMutual() {
return autocryptPreferEncryptMutual;
}

public void setAutocryptPreferEncryptMutual(boolean autocryptPreferEncryptMutual) {
this.autocryptPreferEncryptMutual = autocryptPreferEncryptMutual;
}

public boolean allowRemoteSearch() {
return allowRemoteSearch;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ CryptoStatusDisplayType getCryptoStatusDisplayType() {
case NO_CHOICE:
if (recipientAutocryptStatusType == RecipientAutocryptStatusType.NO_RECIPIENTS) {
return CryptoStatusDisplayType.NO_CHOICE_EMPTY;
} else if (canEncryptAndIsMutual()) {
} else if (canEncryptAndIsMutualDefault()) {
if (recipientAutocryptStatusType.isConfirmed()) {
return CryptoStatusDisplayType.NO_CHOICE_MUTUAL_TRUSTED;
} else {
Expand Down Expand Up @@ -136,7 +136,7 @@ public boolean isEncryptionEnabled() {
}

boolean isExplicitlyEnabled = (cryptoMode == CryptoMode.CHOICE_ENABLED);
boolean isMutualAndNotDisabled = (cryptoMode != CryptoMode.CHOICE_DISABLED && canEncryptAndIsMutual());
boolean isMutualAndNotDisabled = (cryptoMode != CryptoMode.CHOICE_DISABLED && canEncryptAndIsMutualDefault());
return isExplicitlyEnabled || isMutualAndNotDisabled;
}

Expand Down Expand Up @@ -168,8 +168,16 @@ public boolean hasRecipients() {
return recipientAddresses.length > 0;
}

boolean canEncryptAndIsMutual() {
return allRecipientsCanEncrypt() && preferEncryptMutual && recipientAutocryptStatus.type.isMutual();
public boolean isSenderPreferEncryptMutual() {
return preferEncryptMutual;
}

private boolean isRecipientsPreferEncryptMutual() {
return recipientAutocryptStatus.type.isMutual();
}

boolean canEncryptAndIsMutualDefault() {
return allRecipientsCanEncrypt() && isSenderPreferEncryptMutual() && isRecipientsPreferEncryptMutual();
}

boolean hasAutocryptPendingIntent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public void asyncUpdateCryptoStatus() {
.setCryptoProviderState(cryptoProviderState)
.setCryptoMode(currentCryptoMode)
.setEnablePgpInline(cryptoEnablePgpInline)
.setPreferEncryptMutual(false) // TODO introduce a setting
.setPreferEncryptMutual(account.getAutocryptPreferEncryptMutual())
.setRecipients(getAllRecipients())
.setOpenPgpKeyId(accountCryptoKey)
.build();
Expand Down Expand Up @@ -640,15 +640,15 @@ void onClickCryptoStatus() {
if (currentCryptoStatus.hasAutocryptPendingIntent()) {
recipientMvpView.launchUserInteractionPendingIntent(
currentCryptoStatus.getAutocryptPendingIntent(), REQUEST_CODE_AUTOCRYPT);
} else if (currentCryptoStatus.canEncryptAndIsMutual()) {
} else if (currentCryptoStatus.canEncryptAndIsMutualDefault()) {
onCryptoModeChanged(CryptoMode.CHOICE_DISABLED);
} else {
onCryptoModeChanged(CryptoMode.CHOICE_ENABLED);
}
return;
}

if (currentCryptoMode == CryptoMode.CHOICE_DISABLED && !currentCryptoStatus.canEncryptAndIsMutual()) {
if (currentCryptoMode == CryptoMode.CHOICE_DISABLED && !currentCryptoStatus.canEncryptAndIsMutualDefault()) {
onCryptoModeChanged(CryptoMode.CHOICE_ENABLED);
return;
}
Expand Down Expand Up @@ -902,14 +902,14 @@ public void onMenuSetEnableEncryption(boolean enableEncryption) {
if (!cachedCryptoStatus.allRecipientsCanEncrypt()) {
onCryptoModeChanged(CryptoMode.CHOICE_ENABLED);
recipientMvpView.showOpenPgpEnabledErrorDialog(true);
} else if (cachedCryptoStatus.canEncryptAndIsMutual()) {
} else if (cachedCryptoStatus.canEncryptAndIsMutualDefault()) {
onCryptoModeChanged(CryptoMode.NO_CHOICE);
} else {
recipientMvpView.showOpenPgpEncryptExplanationDialog();
onCryptoModeChanged(CryptoMode.CHOICE_ENABLED);
}
} else {
if (cachedCryptoStatus.canEncryptAndIsMutual()) {
if (cachedCryptoStatus.canEncryptAndIsMutualDefault()) {
onCryptoModeChanged(CryptoMode.CHOICE_DISABLED);
} else {
onCryptoModeChanged(CryptoMode.NO_CHOICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
Expand All @@ -22,6 +23,8 @@
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen;
import android.preference.RingtonePreference;
import android.widget.CompoundButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.fsck.k9.Account;
Expand All @@ -46,6 +49,8 @@
import com.fsck.k9.mail.Store;
import com.fsck.k9.mailstore.StorageManager;
import com.fsck.k9.service.MailService;
import com.fsck.k9.ui.dialog.AutocryptPreferEncryptDialog;
import com.fsck.k9.ui.dialog.AutocryptPreferEncryptDialog.OnPreferEncryptChangedListener;
import org.openintents.openpgp.util.OpenPgpKeyPreference;
import timber.log.Timber;

Expand All @@ -55,6 +60,7 @@ public class AccountSettings extends K9PreferenceActivity {

private static final int DIALOG_COLOR_PICKER_ACCOUNT = 1;
private static final int DIALOG_COLOR_PICKER_LED = 2;
private static final int DIALOG_AUTOCRYPT_PREFER_ENCRYPT = 3;

private static final int SELECT_AUTO_EXPAND_FOLDER = 1;

Expand Down Expand Up @@ -112,6 +118,7 @@ public class AccountSettings extends K9PreferenceActivity {
private static final String PREFERENCE_SYNC_REMOTE_DELETIONS = "account_sync_remote_deletetions";
private static final String PREFERENCE_CRYPTO = "crypto";
private static final String PREFERENCE_CRYPTO_KEY = "crypto_key";
private static final String PREFERENCE_AUTOCRYPT_PREFER_ENCRYPT = "autocrypt_prefer_encrypt";
private static final String PREFERENCE_CLOUD_SEARCH_ENABLED = "remote_search_enabled";
private static final String PREFERENCE_REMOTE_SEARCH_NUM_RESULTS = "account_remote_search_num_results";
private static final String PREFERENCE_REMOTE_SEARCH_FULL_TEXT = "account_remote_search_full_text";
Expand Down Expand Up @@ -178,7 +185,7 @@ public class AccountSettings extends K9PreferenceActivity {
private ListPreference mMaxPushFolders;
private boolean hasPgpCrypto = false;
private OpenPgpKeyPreference pgpCryptoKey;
private CheckBoxPreference pgpSupportSignOnly;
private Preference autocryptPreferEncryptMutual;

private PreferenceScreen searchScreen;
private CheckBoxPreference cloudSearchEnabled;
Expand Down Expand Up @@ -711,6 +718,15 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
});

cryptoMenu.setOnPreferenceClickListener(null);

autocryptPreferEncryptMutual = findPreference(PREFERENCE_AUTOCRYPT_PREFER_ENCRYPT);
autocryptPreferEncryptMutual.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
showDialog(DIALOG_AUTOCRYPT_PREFER_ENCRYPT);
return false;
}
});
} else {
cryptoMenu.setSummary(R.string.account_settings_no_openpgp_provider_configured);
cryptoMenu.setOnPreferenceClickListener(new OnPreferenceClickListener() {
Expand Down Expand Up @@ -930,6 +946,16 @@ public void colorChanged(int color) {

break;
}
case DIALOG_AUTOCRYPT_PREFER_ENCRYPT: {
dialog = new AutocryptPreferEncryptDialog(this, account.getAutocryptPreferEncryptMutual(),
new OnPreferEncryptChangedListener() {
@Override
public void onPreferEncryptChanged(boolean enabled) {
account.setAutocryptPreferEncryptMutual(enabled);
}
});
break;
}
}

return dialog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ protected void buildMessageInternal() {
byte[] keyData = autocryptOpenPgpApiInteractor.getKeyMaterialForKeyId(
openPgpApi, openPgpKeyId, address.getAddress());
if (keyData != null) {
autocryptOperations.addAutocryptHeaderToMessage(
currentProcessedMimeMessage, keyData, address.getAddress(), false);
autocryptOperations.addAutocryptHeaderToMessage(currentProcessedMimeMessage, keyData,
address.getAddress(), cryptoStatus.isSenderPreferEncryptMutual());
}

startOrContinueBuildMessage(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.fsck.k9.ui.dialog;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

import com.fsck.k9.R;


public class AutocryptPreferEncryptDialog extends AlertDialog implements OnClickListener {

private final CheckBox preferEncryptCheckbox;
private final OnPreferEncryptChangedListener onPreferEncryptChangedListener;

private boolean preferEncryptEnabled;

public AutocryptPreferEncryptDialog(Context context, boolean preferEncryptEnabled,
OnPreferEncryptChangedListener onPreferEncryptChangedListener) {
super(context);

this.onPreferEncryptChangedListener = onPreferEncryptChangedListener;
this.preferEncryptEnabled = preferEncryptEnabled;

LayoutInflater inflater = LayoutInflater.from(context);

@SuppressLint("InflateParams")
View contentView = inflater.inflate(R.layout.dialog_autocrypt_prefer_encrypt, null);

preferEncryptCheckbox = (CheckBox) contentView.findViewById(R.id.prefer_encrypt_check);
preferEncryptCheckbox.setChecked(preferEncryptEnabled);

contentView.findViewById(R.id.prefer_encrypt).setOnClickListener(this);

// TODO add autocrypt logo?
// setIcon(R.drawable.autocrypt);
setView(contentView);
setButton(Dialog.BUTTON_NEUTRAL, context.getString(R.string.done_action), new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
cancel();
}
});
}

@Override
public void onClick(View v) {
toggleCheck();
}

private void toggleCheck() {
preferEncryptEnabled = !preferEncryptEnabled;
preferEncryptCheckbox.setChecked(preferEncryptEnabled);

onPreferEncryptChangedListener.onPreferEncryptChanged(preferEncryptEnabled);
}

public interface OnPreferEncryptChangedListener {
void onPreferEncryptChanged(boolean enabled);
}
}
57 changes: 57 additions & 0 deletions k9mail/src/main/res/layout/dialog_autocrypt_prefer_encrypt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="20dp"
android:text="@string/dialog_autocrypt_mutual_title"
style="?android:textAppearanceLarge" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:text="@string/dialog_autocrypt_mutual_description_1" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:gravity="center_vertical"
android:minHeight="?android:listPreferredItemHeight"
android:orientation="horizontal"
android:background="?android:selectableItemBackground"
android:id="@+id/prefer_encrypt">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dialog_autocrypt_mutual_description_2" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prefer_encrypt_check"
android:clickable="false"
/>

</LinearLayout>

</LinearLayout>

</ScrollView>
5 changes: 5 additions & 0 deletions k9mail/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1284,4 +1284,9 @@ Please submit bug reports, contribute new features and ask questions at
<string name="openpgp_enabled_error_back">Back</string>
<string name="openpgp_enabled_error_disable">Disable Encryption</string>
<string name="openpgp_encryption">OpenPGP Encryption</string>

<string name="account_settings_crypto_prefer_encrypt">Autocrypt mutual mode</string>
<string name="dialog_autocrypt_mutual_title">Autocrypt mutual mode</string>
<string name="dialog_autocrypt_mutual_description_1">Messages will normally be encrypted by choice, or when replying to an encrypted message.</string>
<string name="dialog_autocrypt_mutual_description_2">If both sender and recipients enable mutual mode, encryption will be enabled by default.</string>
</resources>
5 changes: 5 additions & 0 deletions k9mail/src/main/res/xml/account_settings_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@
android:key="crypto_key"
android:title="@string/account_settings_crypto_key" />

<Preference
android:key="autocrypt_prefer_encrypt"
android:title="@string/account_settings_crypto_prefer_encrypt"
/>

</PreferenceScreen>

</PreferenceScreen>

0 comments on commit 8e12633

Please sign in to comment.