Skip to content

Commit

Permalink
use dialog for prefer-encrypt setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Valodim committed Feb 21, 2018
1 parent d606475 commit f1fd332
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 10 deletions.
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 @@ -179,7 +185,7 @@ public class AccountSettings extends K9PreferenceActivity {
private ListPreference mMaxPushFolders;
private boolean hasPgpCrypto = false;
private OpenPgpKeyPreference pgpCryptoKey;
private CheckBoxPreference autocryptPreferEncryptMutual;
private Preference autocryptPreferEncryptMutual;

private PreferenceScreen searchScreen;
private CheckBoxPreference cloudSearchEnabled;
Expand Down Expand Up @@ -713,8 +719,14 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {

cryptoMenu.setOnPreferenceClickListener(null);

autocryptPreferEncryptMutual = (CheckBoxPreference) findPreference(PREFERENCE_AUTOCRYPT_PREFER_ENCRYPT);
autocryptPreferEncryptMutual.setChecked(account.getAutocryptPreferEncryptMutual());
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 @@ -795,7 +807,6 @@ private void saveSettings() {
} else {
account.setCryptoKey(Account.NO_OPENPGP_KEY);
}
account.setAutocryptPreferEncryptMutual(autocryptPreferEncryptMutual.isChecked());

// In webdav account we use the exact folder name also for inbox,
// since it varies because of internationalization
Expand Down Expand Up @@ -935,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
@@ -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>
8 changes: 5 additions & 3 deletions k9mail/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,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">Encryption preference</string>
<string name="account_settings_crypto_prefer_encrypt_on">By default when replying to encrypted, or if all recipients also checked this option</string>
<string name="account_settings_crypto_prefer_encrypt_off">By default, when replying to encrypted messages</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: 2 additions & 3 deletions k9mail/src/main/res/xml/account_settings_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,11 @@
android:key="crypto_key"
android:title="@string/account_settings_crypto_key" />

<CheckBoxPreference
<Preference
android:key="autocrypt_prefer_encrypt"
android:title="@string/account_settings_crypto_prefer_encrypt"
android:summaryOn="@string/account_settings_crypto_prefer_encrypt_on"
android:summaryOff="@string/account_settings_crypto_prefer_encrypt_off"
/>

</PreferenceScreen>

</PreferenceScreen>

0 comments on commit f1fd332

Please sign in to comment.