1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 01:42:16 -05:00

PGP: Introduce key preference per account for OpenPGP APIv7

This commit is contained in:
Dominik Schürmann 2015-03-20 15:14:48 +01:00
parent dc95f5feab
commit 16f09611fe
9 changed files with 68 additions and 13 deletions

View File

@ -220,6 +220,7 @@ public class Account implements BaseAccount, StoreConfig {
private boolean mStripSignature;
private boolean mSyncRemoteDeletions;
private String mCryptoApp;
private long mCryptoKey;
private boolean mMarkMessageAsReadOnView;
private boolean mAlwaysShowCcBcc;
private boolean mAllowRemoteSearch;
@ -314,6 +315,7 @@ public class Account implements BaseAccount, StoreConfig {
mStripSignature = DEFAULT_STRIP_SIGNATURE;
mSyncRemoteDeletions = true;
mCryptoApp = NO_OPENPGP_PROVIDER;
mCryptoKey = 0;
mAllowRemoteSearch = false;
mRemoteSearchFullText = false;
mRemoteSearchNumResults = DEFAULT_REMOTE_SEARCH_NUM_RESULTS;
@ -724,6 +726,7 @@ public class Account implements BaseAccount, StoreConfig {
editor.putBoolean(mUuid + ".replyAfterQuote", mReplyAfterQuote);
editor.putBoolean(mUuid + ".stripSignature", mStripSignature);
editor.putString(mUuid + ".cryptoApp", mCryptoApp);
editor.putLong(mUuid + ".cryptoKey", mCryptoKey);
editor.putBoolean(mUuid + ".allowRemoteSearch", mAllowRemoteSearch);
editor.putBoolean(mUuid + ".remoteSearchFullText", mRemoteSearchFullText);
editor.putInt(mUuid + ".remoteSearchNumResults", mRemoteSearchNumResults);
@ -1605,6 +1608,14 @@ public class Account implements BaseAccount, StoreConfig {
}
}
public long getCryptoKey() {
return mCryptoKey;
}
public void setCryptoKey(long keyId) {
mCryptoKey = keyId;
}
public boolean allowRemoteSearch() {
return mAllowRemoteSearch;
}

View File

@ -1298,9 +1298,11 @@ public class MessageCompose extends K9Activity implements OnClickListener,
if (mEncryptCheckbox.isChecked() && mCryptoSignatureCheckbox.isChecked()) {
Intent intent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, emailsArray);
intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, mAccount.getCryptoKey());
executeOpenPgpMethod(intent);
} else if (mCryptoSignatureCheckbox.isChecked()) {
Intent intent = new Intent(OpenPgpApi.ACTION_SIGN);
intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, mAccount.getCryptoKey());
executeOpenPgpMethod(intent);
} else if (mEncryptCheckbox.isChecked()) {
Intent intent = new Intent(OpenPgpApi.ACTION_ENCRYPT);
@ -1338,10 +1340,6 @@ public class MessageCompose extends K9Activity implements OnClickListener,
private void executeOpenPgpMethod(Intent intent) {
intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
// this follows user id format of OpenPGP to allow key generation based on it
// includes account number to make it unique
String accName = OpenPgpApiHelper.buildAccountName(mIdentity);
intent.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, accName);
final InputStream is = getOpenPgpInputStream();
final ByteArrayOutputStream os = new ByteArrayOutputStream();

View File

@ -39,6 +39,7 @@ import com.fsck.k9.activity.ChooseIdentity;
import com.fsck.k9.activity.ColorPickerDialog;
import com.fsck.k9.activity.K9PreferenceActivity;
import com.fsck.k9.activity.ManageIdentities;
import com.fsck.k9.crypto.OpenPgpApiHelper;
import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.Store;
import com.fsck.k9.mailstore.LocalFolder;
@ -46,6 +47,7 @@ import com.fsck.k9.mailstore.StorageManager;
import com.fsck.k9.service.MailService;
import org.openintents.openpgp.util.OpenPgpAppPreference;
import org.openintents.openpgp.util.OpenPgpKeyPreference;
import org.openintents.openpgp.util.OpenPgpUtils;
@ -110,6 +112,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_APP = "crypto_app";
private static final String PREFERENCE_CRYPTO_KEY = "crypto_key";
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";
@ -175,6 +178,7 @@ public class AccountSettings extends K9PreferenceActivity {
private ListPreference mMaxPushFolders;
private boolean mHasCrypto = false;
private OpenPgpAppPreference mCryptoApp;
private OpenPgpKeyPreference mCryptoKey;
private PreferenceScreen mSearchScreen;
private CheckBoxPreference mCloudSearchEnabled;
@ -688,14 +692,27 @@ public class AccountSettings extends K9PreferenceActivity {
mHasCrypto = OpenPgpUtils.isAvailable(this);
if (mHasCrypto) {
mCryptoApp = (OpenPgpAppPreference) findPreference(PREFERENCE_CRYPTO_APP);
mCryptoKey = (OpenPgpKeyPreference) findPreference(PREFERENCE_CRYPTO_KEY);
mCryptoApp.setValue(String.valueOf(mAccount.getCryptoApp()));
mCryptoApp.setSummary(mCryptoApp.getEntry());
mCryptoApp.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
String value = newValue.toString();
mCryptoApp.setSummary(mCryptoApp.getEntryByValue(value));
mCryptoApp.setValue(value);
mCryptoKey.setOpenPgpProvider(value);
return false;
}
});
mCryptoKey.setValue(mAccount.getCryptoKey());
mCryptoKey.setOpenPgpProvider(mCryptoApp.getValue());
// TODO: other identities?
mCryptoKey.setDefaultUserId(OpenPgpApiHelper.buildUserId(mAccount.getIdentity(0)));
mCryptoKey.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
long value = (Long) newValue;
mCryptoKey.setValue(value);
return false;
}
});
@ -765,6 +782,7 @@ public class AccountSettings extends K9PreferenceActivity {
mAccount.setLocalStorageProviderId(mLocalStorageProvider.getValue());
if (mHasCrypto) {
mAccount.setCryptoApp(mCryptoApp.getValue());
mAccount.setCryptoKey(mCryptoKey.getValue());
}
// In webdav account we use the exact folder name also for inbox,
@ -832,6 +850,9 @@ public class AccountSettings extends K9PreferenceActivity {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mCryptoKey != null && mCryptoKey.handleOnActivityResult(requestCode, resultCode, data)) {
return;
}
if (resultCode == RESULT_OK) {
switch (requestCode) {
case SELECT_AUTO_EXPAND_FOLDER:

View File

@ -16,7 +16,7 @@ public class OpenPgpApiHelper {
*
* @see org.openintents.openpgp.util.OpenPgpApi#EXTRA_ACCOUNT_NAME
*/
public static String buildAccountName(Identity identity) {
public static String buildUserId(Identity identity) {
StringBuilder sb = new StringBuilder();
String name = identity.getName();

View File

@ -170,10 +170,6 @@ public class MessageCryptoHelper {
private void decryptVerify(Intent intent) {
intent.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
Identity identity = IdentityHelper.getRecipientIdentityFromMessage(account, message);
String accountName = OpenPgpApiHelper.buildAccountName(identity);
intent.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, accountName);
try {
CryptoPartType cryptoPartType = currentCryptoPart.type;
switch (cryptoPartType) {

View File

@ -569,8 +569,9 @@ Please submit bug reports, contribute new features and ask questions at
<string name="account_settings_folders">Folders</string>
<string name="account_settings_quote_prefix_label">Quoted text prefix</string>
<string name="account_settings_crypto">Cryptography</string>
<string name="account_settings_crypto_app">OpenPGP Provider</string>
<string name="account_settings_no_openpgp_provider_installed">No OpenPGP Provider installed</string>
<string name="account_settings_crypto_app">OpenPGP app</string>
<string name="account_settings_crypto_key">My Key</string>
<string name="account_settings_no_openpgp_provider_installed">No OpenPGP app installed</string>
<string name="account_settings_mail_check_frequency_label">Folder poll frequency</string>

View File

@ -471,6 +471,11 @@
android:key="crypto_app"
android:title="@string/account_settings_crypto_app" />
<org.openintents.openpgp.util.OpenPgpKeyPreference
android:persistent="false"
android:key="crypto_key"
android:title="@string/account_settings_crypto_key" />
</PreferenceScreen>
</PreferenceScreen>

View File

@ -201,14 +201,23 @@ public class OpenPgpAppPreference extends DialogPreference {
return 0;
}
/**
* Public API
*/
public String getEntry() {
return getEntryByValue(mSelectedPackage);
}
/**
* Public API
*/
public String getValue() {
return mSelectedPackage;
}
/**
* Public API
*/
public void setValue(String packageName) {
setAndPersist(packageName);
}

View File

@ -145,6 +145,20 @@ public class OpenPgpKeyPreference extends Preference {
setAndPersist(newValue);
}
/**
* Public API
*/
public void setValue(long keyId) {
setAndPersist(keyId);
}
/**
* Public API
*/
public long getValue() {
return mKeyId;
}
private void setAndPersist(long newValue) {
mKeyId = newValue;