Merge pull request #4289 from k9mail/kotlin-identity

Convert Identity to Kotlin data class
This commit is contained in:
Vincent Breitmoser 2019-11-26 17:05:37 +01:00 committed by GitHub
commit d2165da8a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 213 additions and 296 deletions

View file

@ -12,7 +12,6 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import android.content.Context;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import com.fsck.k9.backend.api.SyncConfig.ExpungePolicy;
@ -22,6 +21,8 @@ import com.fsck.k9.mail.store.StoreConfig;
import com.fsck.k9.mailstore.StorageManager;
import com.fsck.k9.mailstore.StorageManager.StorageProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Account stores all of the settings for a single account defined by the user. Each account is defined by a UUID.
@ -291,7 +292,8 @@ public class Account implements BaseAccount, StoreConfig {
}
public synchronized void setName(String name) {
identities.get(0).setName(name);
Identity newIdentity = identities.get(0).withName(name);
identities.set(0, newIdentity);
}
public synchronized boolean getSignatureUse() {
@ -299,7 +301,8 @@ public class Account implements BaseAccount, StoreConfig {
}
public synchronized void setSignatureUse(boolean signatureUse) {
identities.get(0).setSignatureUse(signatureUse);
Identity newIdentity = identities.get(0).withSignatureUse(signatureUse);
identities.set(0, newIdentity);
}
public synchronized String getSignature() {
@ -307,7 +310,8 @@ public class Account implements BaseAccount, StoreConfig {
}
public synchronized void setSignature(String signature) {
identities.get(0).setSignature(signature);
Identity newIdentity = identities.get(0).withSignature(signature);
identities.set(0, newIdentity);
}
@Override
@ -317,7 +321,8 @@ public class Account implements BaseAccount, StoreConfig {
@Override
public synchronized void setEmail(String email) {
identities.get(0).setEmail(email);
Identity newIdentity = identities.get(0).withEmail(email);
identities.set(0, newIdentity);
}
public synchronized String getAlwaysBcc() {

View file

@ -1,19 +1,6 @@
package com.fsck.k9
import com.fsck.k9.Account.DEFAULT_SORT_ASCENDING
import com.fsck.k9.Account.DEFAULT_SORT_TYPE
import com.fsck.k9.Account.DeletePolicy
import com.fsck.k9.Account.Expunge
import com.fsck.k9.Account.FolderMode
import com.fsck.k9.Account.INBOX
import com.fsck.k9.Account.MessageFormat
import com.fsck.k9.Account.NO_OPENPGP_KEY
import com.fsck.k9.Account.QuoteStyle
import com.fsck.k9.Account.Searchable
import com.fsck.k9.Account.ShowPictures
import com.fsck.k9.Account.SortType
import com.fsck.k9.Account.SpecialFolderSelection
import com.fsck.k9.Account.UNASSIGNED_ACCOUNT_NUMBER
import com.fsck.k9.Account.*
import com.fsck.k9.helper.Utility
import com.fsck.k9.mail.NetworkType
import com.fsck.k9.mail.filter.Base64
@ -21,7 +8,7 @@ import com.fsck.k9.mailstore.StorageManager
import com.fsck.k9.preferences.Storage
import com.fsck.k9.preferences.StorageEditor
import timber.log.Timber
import java.util.ArrayList
import java.util.*
class AccountPreferenceSerializer(
private val storageManager: StorageManager,
@ -174,13 +161,14 @@ class AccountPreferenceSerializer(
val description = storage.getString("$accountUuid.$IDENTITY_DESCRIPTION_KEY.$ident", null)
val replyTo = storage.getString("$accountUuid.replyTo.$ident", null)
if (email != null) {
val identity = Identity()
identity.name = name
identity.email = email
identity.signatureUse = signatureUse
identity.signature = signature
identity.description = description
identity.replyTo = replyTo
val identity = Identity(
name = name,
email = email,
signatureUse = signatureUse,
signature = signature,
description = description,
replyTo = replyTo
)
newIdentities.add(identity)
gotOne = true
}
@ -192,12 +180,13 @@ class AccountPreferenceSerializer(
val email = storage.getString("$accountUuid.email", null)
val signatureUse = storage.getBoolean("$accountUuid.signatureUse", true)
val signature = storage.getString("$accountUuid.signature", null)
val identity = Identity()
identity.name = name
identity.email = email
identity.signatureUse = signatureUse
identity.signature = signature
identity.description = email
val identity = Identity(
name = name,
email = email,
signatureUse = signatureUse,
signature = signature,
description = email
)
newIdentities.add(identity)
}
@ -561,11 +550,11 @@ class AccountPreferenceSerializer(
identities = ArrayList<Identity>()
val identity = Identity().apply {
signatureUse = true
signature = resourceProvider.defaultSignature()
val identity = Identity(
signatureUse = true,
signature = resourceProvider.defaultSignature(),
description = resourceProvider.defaultIdentityDescription()
}
)
identities.add(identity)
with (notificationSetting) {

View file

@ -1,68 +0,0 @@
package com.fsck.k9;
import java.io.Serializable;
public class Identity implements Serializable {
private static final long serialVersionUID = -1666669071480985760L;
private String description;
private String name;
private String email;
private String signature;
private boolean signatureUse;
private String replyTo;
public synchronized String getName() {
return name;
}
public synchronized void setName(String name) {
this.name = name;
}
public synchronized String getEmail() {
return email;
}
public synchronized void setEmail(String email) {
this.email = email;
}
public synchronized boolean getSignatureUse() {
return signatureUse;
}
public synchronized void setSignatureUse(boolean signatureUse) {
this.signatureUse = signatureUse;
}
public synchronized String getSignature() {
return signature;
}
public synchronized void setSignature(String signature) {
this.signature = signature;
}
public synchronized String getDescription() {
return description;
}
public synchronized void setDescription(String description) {
this.description = description;
}
public synchronized String getReplyTo() {
return replyTo;
}
public synchronized void setReplyTo(String replyTo) {
this.replyTo = replyTo;
}
@Override
public synchronized String toString() {
return "Account.Identity(description=" + description + ", name=" + name + ", email=" + email + ", replyTo=" + replyTo + ", signature=" +
signature;
}
}

View file

@ -0,0 +1,20 @@
package com.fsck.k9
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
data class Identity(
val description: String? = null,
val name: String? = null,
val email: String? = null,
val signature: String? = null,
val signatureUse: Boolean = false,
val replyTo: String? = null
) : Parcelable {
// TODO remove when callers are converted to Kotlin
fun withName(name: String?) = copy(name = name)
fun withSignature(signature: String?) = copy(signature = signature)
fun withSignatureUse(signatureUse: Boolean) = copy(signatureUse = signatureUse)
fun withEmail(email: String?) = copy(email = email)
}

View file

@ -1,33 +0,0 @@
package com.fsck.k9.crypto;
import com.fsck.k9.Identity;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class OpenPgpApiHelperTest {
@Test
public void buildUserId_withName_shouldCreateOpenPgpAccountName() {
Identity identity = new Identity();
identity.setEmail("user@domain.com");
identity.setName("Name");
String result = OpenPgpApiHelper.buildUserId(identity);
assertEquals("Name <user@domain.com>", result);
}
@Test
public void buildUserId_withoutName_shouldCreateOpenPgpAccountName() {
Identity identity = new Identity();
identity.setEmail("user@domain.com");
String result = OpenPgpApiHelper.buildUserId(identity);
assertEquals("<user@domain.com>", result);
}
}

View file

@ -0,0 +1,34 @@
package com.fsck.k9.crypto
import com.fsck.k9.Identity
import org.junit.Assert.assertEquals
import org.junit.Test
class OpenPgpApiHelperTest {
@Test
fun buildUserId_withName_shouldCreateOpenPgpAccountName() {
val identity = Identity(
email = "user@domain.com",
name = "Name"
)
val result = OpenPgpApiHelper.buildUserId(identity)
assertEquals("Name <user@domain.com>", result)
}
@Test
fun buildUserId_withoutName_shouldCreateOpenPgpAccountName() {
val identity = Identity(
email = "user@domain.com"
)
val result = OpenPgpApiHelper.buildUserId(identity)
assertEquals("<user@domain.com>", result)
}
}

View file

@ -121,10 +121,10 @@ class IdentityHelperTest : RobolectricTest() {
)
}
private fun newIdentity(name: String, email: String) = Identity().apply {
this.name = name
this.email = email
}
private fun newIdentity(name: String, email: String) = Identity(
name = name,
email = email
)
private fun messageWithRecipients(vararg recipients: Pair<RecipientType, String>): Message {
return MimeMessage().apply {

View file

@ -424,11 +424,13 @@ public class MessageBuilderTest extends RobolectricTest {
}
private Identity createIdentity() {
Identity identity = new Identity();
identity.setName(TEST_IDENTITY_ADDRESS.getPersonal());
identity.setEmail(TEST_IDENTITY_ADDRESS.getAddress());
identity.setDescription("test identity");
identity.setSignatureUse(false);
return identity;
return new Identity(
"test identity",
TEST_IDENTITY_ADDRESS.getPersonal(),
TEST_IDENTITY_ADDRESS.getAddress(),
null,
false,
null
);
}
}

View file

@ -1,132 +0,0 @@
package com.fsck.k9.activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.fsck.k9.Account;
import com.fsck.k9.Identity;
import com.fsck.k9.Preferences;
import com.fsck.k9.ui.R;
import java.util.List;
public class EditIdentity extends K9Activity {
public static final String EXTRA_IDENTITY = "com.fsck.k9.EditIdentity_identity";
public static final String EXTRA_IDENTITY_INDEX = "com.fsck.k9.EditIdentity_identity_index";
public static final String EXTRA_ACCOUNT = "com.fsck.k9.EditIdentity_account";
private Account mAccount;
private Identity mIdentity;
private int mIdentityIndex;
private EditText mDescriptionView;
private CheckBox mSignatureUse;
private EditText mSignatureView;
private LinearLayout mSignatureLayout;
private EditText mEmailView;
// private EditText mAlwaysBccView;
private EditText mNameView;
private EditText mReplyTo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIdentity = (Identity)getIntent().getSerializableExtra(EXTRA_IDENTITY);
mIdentityIndex = getIntent().getIntExtra(EXTRA_IDENTITY_INDEX, -1);
String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
if (mIdentityIndex == -1) {
mIdentity = new Identity();
}
setLayout(R.layout.edit_identity);
/*
* If we're being reloaded we override the original account with the one
* we saved
*/
if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_IDENTITY)) {
mIdentity = (Identity)savedInstanceState.getSerializable(EXTRA_IDENTITY);
}
mDescriptionView = findViewById(R.id.description);
mDescriptionView.setText(mIdentity.getDescription());
mNameView = findViewById(R.id.name);
mNameView.setText(mIdentity.getName());
mEmailView = findViewById(R.id.email);
mEmailView.setText(mIdentity.getEmail());
mReplyTo = findViewById(R.id.reply_to);
mReplyTo.setText(mIdentity.getReplyTo());
// mAccountAlwaysBcc = (EditText)findViewById(R.id.bcc);
// mAccountAlwaysBcc.setText(mIdentity.getAlwaysBcc());
mSignatureLayout = findViewById(R.id.signature_layout);
mSignatureUse = findViewById(R.id.signature_use);
mSignatureView = findViewById(R.id.signature);
mSignatureUse.setChecked(mIdentity.getSignatureUse());
mSignatureUse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mSignatureLayout.setVisibility(View.VISIBLE);
mSignatureView.setText(mIdentity.getSignature());
} else {
mSignatureLayout.setVisibility(View.GONE);
}
}
});
if (mSignatureUse.isChecked()) {
mSignatureView.setText(mIdentity.getSignature());
} else {
mSignatureLayout.setVisibility(View.GONE);
}
}
private void saveIdentity() {
mIdentity.setDescription(mDescriptionView.getText().toString());
mIdentity.setEmail(mEmailView.getText().toString());
// mIdentity.setAlwaysBcc(mAccountAlwaysBcc.getText().toString());
mIdentity.setName(mNameView.getText().toString());
mIdentity.setSignatureUse(mSignatureUse.isChecked());
mIdentity.setSignature(mSignatureView.getText().toString());
if (mReplyTo.getText().length() == 0) {
mIdentity.setReplyTo(null);
} else {
mIdentity.setReplyTo(mReplyTo.getText().toString());
}
List<Identity> identities = mAccount.getIdentities();
if (mIdentityIndex == -1) {
identities.add(mIdentity);
} else {
identities.remove(mIdentityIndex);
identities.add(mIdentityIndex, mIdentity);
}
Preferences.getPreferences(getApplicationContext()).saveAccount(mAccount);
finish();
}
@Override
public void onBackPressed() {
saveIdentity();
super.onBackPressed();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_IDENTITY, mIdentity);
}
}

View file

@ -0,0 +1,98 @@
package com.fsck.k9.activity
import android.os.Bundle
import android.view.View
import com.fsck.k9.Account
import com.fsck.k9.Identity
import com.fsck.k9.Preferences
import com.fsck.k9.ui.R
import kotlinx.android.synthetic.main.edit_identity.*
class EditIdentity : K9Activity() {
private lateinit var account: Account
private lateinit var identity: Identity
private var identityIndex: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
identity = intent.getParcelableExtra(EXTRA_IDENTITY)
identityIndex = intent.getIntExtra(EXTRA_IDENTITY_INDEX, -1)
val accountUuid = intent.getStringExtra(EXTRA_ACCOUNT)
account = Preferences.getPreferences(this).getAccount(accountUuid)
if (identityIndex == -1) {
identity = Identity()
}
setLayout(R.layout.edit_identity)
savedInstanceState?.getParcelable<Identity>(EXTRA_IDENTITY)?.let {
identity = it
}
description.setText(identity.description)
name.setText(identity.name)
email.setText(identity.email)
reply_to.setText(identity.replyTo)
// mAccountAlwaysBcc = (EditText)findViewById(R.id.bcc);
// mAccountAlwaysBcc.setText(identity.getAlwaysBcc());
signature_use.isChecked = identity.signatureUse
signature_use.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
signature_layout.visibility = View.VISIBLE
signature.setText(identity.signature)
} else {
signature_layout.visibility = View.GONE
}
}
if (signature_use.isChecked) {
signature.setText(identity.signature)
} else {
signature_layout.visibility = View.GONE
}
}
private fun saveIdentity() {
identity = identity.copy(
description = description.text.toString(),
email = email.text.toString(),
name = name.text.toString(),
signatureUse = signature_use.isChecked,
signature = signature.text.toString(),
replyTo = if (reply_to.text.isNotEmpty()) reply_to.text.toString() else null
)
// identity.setAlwaysBcc(mAccountAlwaysBcc.getText().toString());
val identities = account.identities
if (identityIndex == -1) {
identities.add(identity)
} else {
identities.removeAt(identityIndex)
identities.add(identityIndex, identity)
}
Preferences.getPreferences(applicationContext).saveAccount(account)
finish()
}
override fun onBackPressed() {
saveIdentity()
super.onBackPressed()
}
public override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable(EXTRA_IDENTITY, identity)
}
companion object {
const val EXTRA_IDENTITY = "com.fsck.k9.EditIdentity_identity"
const val EXTRA_IDENTITY_INDEX = "com.fsck.k9.EditIdentity_identity_index"
const val EXTRA_ACCOUNT = "com.fsck.k9.EditIdentity_account"
}
}

View file

@ -608,7 +608,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
outState.putBoolean(STATE_KEY_SOURCE_MESSAGE_PROCED, relatedMessageProcessed);
outState.putLong(STATE_KEY_DRAFT_ID, draftId);
outState.putSerializable(STATE_IDENTITY, identity);
outState.putParcelable(STATE_IDENTITY, identity);
outState.putBoolean(STATE_IDENTITY_CHANGED, identityChanged);
outState.putString(STATE_IN_REPLY_TO, repliedToMessageId);
outState.putString(STATE_REFERENCES, referencedMessageIds);
@ -642,7 +642,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
attachmentPresenter.onRestoreInstanceState(savedInstanceState);
draftId = savedInstanceState.getLong(STATE_KEY_DRAFT_ID);
identity = (Identity) savedInstanceState.getSerializable(STATE_IDENTITY);
identity = savedInstanceState.getParcelable(STATE_IDENTITY);
identityChanged = savedInstanceState.getBoolean(STATE_IDENTITY_CHANGED);
repliedToMessageId = savedInstanceState.getString(STATE_IN_REPLY_TO);
referencedMessageIds = savedInstanceState.getString(STATE_REFERENCES);
@ -1344,28 +1344,29 @@ public class MessageCompose extends K9Activity implements OnClickListener,
Identity newIdentity = new Identity();
if (k9identity.containsKey(IdentityField.SIGNATURE)) {
newIdentity.setSignatureUse(true);
newIdentity.setSignature(k9identity.get(IdentityField.SIGNATURE));
newIdentity = newIdentity
.withSignatureUse(true)
.withSignature(k9identity.get(IdentityField.SIGNATURE));
signatureChanged = true;
} else {
if (message instanceof LocalMessage) {
newIdentity.setSignatureUse(((LocalMessage) message).getFolder().getSignatureUse());
newIdentity = newIdentity.withSignatureUse(((LocalMessage) message).getFolder().getSignatureUse());
}
newIdentity.setSignature(identity.getSignature());
newIdentity = newIdentity.withSignature(identity.getSignature());
}
if (k9identity.containsKey(IdentityField.NAME)) {
newIdentity.setName(k9identity.get(IdentityField.NAME));
newIdentity = newIdentity.withName(k9identity.get(IdentityField.NAME));
identityChanged = true;
} else {
newIdentity.setName(identity.getName());
newIdentity = newIdentity.withName(identity.getName());
}
if (k9identity.containsKey(IdentityField.EMAIL)) {
newIdentity.setEmail(k9identity.get(IdentityField.EMAIL));
newIdentity = newIdentity.withEmail(k9identity.get(IdentityField.EMAIL));
identityChanged = true;
} else {
newIdentity.setEmail(identity.getEmail());
newIdentity = newIdentity.withEmail(identity.getEmail());
}
if (k9identity.containsKey(IdentityField.ORIGINAL_MESSAGE)) {

View file

@ -51,7 +51,7 @@ class AutocryptKeyTransferPresenter internal constructor(
}
})
view.setAddress(account.identities[0].email)
view.setAddress(account.identities[0].email!!)
viewModel.autocryptSetupTransferLiveEvent.recall()
}

View file

@ -659,11 +659,12 @@ class PgpMessageBuilderTest : K9RobolectricTest() {
AutocryptOperations.getInstance(), autocryptOpenPgpApiInteractor, resourceProvider)
builder.setOpenPgpApi(openPgpApi)
val identity = Identity()
identity.name = "tester"
identity.email = SENDER_EMAIL
identity.description = "test identity"
identity.signatureUse = false
val identity = Identity(
name = "tester",
email = SENDER_EMAIL,
description = "test identity",
signatureUse = false
)
builder.setSubject("subject")
.setSentDate(Date())