From 0aec40bf213ef302044676ffa0628d6f6d344b74 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 26 Nov 2019 15:09:59 +0100 Subject: [PATCH 1/4] convert EditIdentity to Kotlin --- .../com/fsck/k9/activity/EditIdentity.java | 132 ------------------ .../java/com/fsck/k9/activity/EditIdentity.kt | 106 ++++++++++++++ 2 files changed, 106 insertions(+), 132 deletions(-) delete mode 100644 app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.java create mode 100644 app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt diff --git a/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.java b/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.java deleted file mode 100644 index 2db0a2c88..000000000 --- a/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.java +++ /dev/null @@ -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 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); - } -} diff --git a/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt b/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt new file mode 100644 index 000000000..ad71e0758 --- /dev/null +++ b/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt @@ -0,0 +1,106 @@ +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 mAccount: Account + private lateinit var mIdentity: Identity + private var mIdentityIndex: Int = 0 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + mIdentity = intent.getSerializableExtra(EXTRA_IDENTITY) as Identity + mIdentityIndex = intent.getIntExtra(EXTRA_IDENTITY_INDEX, -1) + val accountUuid = intent.getStringExtra(EXTRA_ACCOUNT) + mAccount = Preferences.getPreferences(this).getAccount(accountUuid) + + if (mIdentityIndex == -1) { + mIdentity = 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 = savedInstanceState.getSerializable(EXTRA_IDENTITY) as Identity + } + + description.setText(mIdentity.description) + name.setText(mIdentity.name) + email.setText(mIdentity.email) + reply_to.setText(mIdentity.replyTo) + + // mAccountAlwaysBcc = (EditText)findViewById(R.id.bcc); + // mAccountAlwaysBcc.setText(mIdentity.getAlwaysBcc()); + + signature_use.isChecked = mIdentity.signatureUse + signature_use.setOnCheckedChangeListener { _, isChecked -> + if (isChecked) { + signature_layout.visibility = View.VISIBLE + signature.setText(mIdentity.signature) + } else { + signature_layout.visibility = View.GONE + } + } + + if (signature_use.isChecked) { + signature.setText(mIdentity.signature) + } else { + signature_layout.visibility = View.GONE + } + } + + private fun saveIdentity() { + + mIdentity.description = description.text.toString() + mIdentity.email = email.text.toString() + // mIdentity.setAlwaysBcc(mAccountAlwaysBcc.getText().toString()); + mIdentity.name = name.text.toString() + mIdentity.signatureUse = signature_use.isChecked + mIdentity.signature = signature.text.toString() + + if (reply_to.text.isEmpty()) { + mIdentity.replyTo = null + } else { + mIdentity.replyTo = reply_to.text.toString() + } + + val identities = mAccount.identities + if (mIdentityIndex == -1) { + identities.add(mIdentity) + } else { + identities.removeAt(mIdentityIndex) + identities.add(mIdentityIndex, mIdentity) + } + + Preferences.getPreferences(applicationContext).saveAccount(mAccount) + + finish() + } + + override fun onBackPressed() { + saveIdentity() + super.onBackPressed() + } + + public override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putSerializable(EXTRA_IDENTITY, mIdentity) + } + + 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" + } +} From 2296c6f757ae2607be7af646cf8f25255f7b7de7 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 26 Nov 2019 15:31:19 +0100 Subject: [PATCH 2/4] convert Identity to Kotlin --- .../src/main/java/com/fsck/k9/Account.java | 15 ++-- .../fsck/k9/AccountPreferenceSerializer.kt | 53 ++++++--------- .../src/main/java/com/fsck/k9/Identity.java | 68 ------------------- .../src/main/java/com/fsck/k9/Identity.kt | 55 +++++++++++++++ .../fsck/k9/crypto/OpenPgpApiHelperTest.java | 33 --------- .../fsck/k9/crypto/OpenPgpApiHelperTest.kt | 34 ++++++++++ .../com/fsck/k9/helper/IdentityHelperTest.kt | 8 +-- .../fsck/k9/message/MessageBuilderTest.java | 14 ++-- .../java/com/fsck/k9/activity/EditIdentity.kt | 34 ++++------ .../com/fsck/k9/activity/MessageCompose.java | 21 +++--- .../endtoend/AutocryptKeyTransferPresenter.kt | 2 +- .../fsck/k9/message/PgpMessageBuilderTest.kt | 11 +-- 12 files changed, 163 insertions(+), 185 deletions(-) delete mode 100644 app/core/src/main/java/com/fsck/k9/Identity.java create mode 100644 app/core/src/main/java/com/fsck/k9/Identity.kt delete mode 100644 app/core/src/test/java/com/fsck/k9/crypto/OpenPgpApiHelperTest.java create mode 100644 app/core/src/test/java/com/fsck/k9/crypto/OpenPgpApiHelperTest.kt diff --git a/app/core/src/main/java/com/fsck/k9/Account.java b/app/core/src/main/java/com/fsck/k9/Account.java index f1d3609c5..f2b7182aa 100644 --- a/app/core/src/main/java/com/fsck/k9/Account.java +++ b/app/core/src/main/java/com/fsck/k9/Account.java @@ -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() { diff --git a/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt b/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt index 7916907bb..c47634ed4 100644 --- a/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt +++ b/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt @@ -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() - 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) { diff --git a/app/core/src/main/java/com/fsck/k9/Identity.java b/app/core/src/main/java/com/fsck/k9/Identity.java deleted file mode 100644 index cdd7de67a..000000000 --- a/app/core/src/main/java/com/fsck/k9/Identity.java +++ /dev/null @@ -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; - } -} diff --git a/app/core/src/main/java/com/fsck/k9/Identity.kt b/app/core/src/main/java/com/fsck/k9/Identity.kt new file mode 100644 index 000000000..5af1e7fe4 --- /dev/null +++ b/app/core/src/main/java/com/fsck/k9/Identity.kt @@ -0,0 +1,55 @@ +package com.fsck.k9 + +import android.os.Parcel +import android.os.Parcelable + +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 { + override fun toString(): String { + return "Account.Identity(description=" + description + ", name=" + name + ", email=" + email + ", replyTo=" + replyTo + ", signature=" + + signature + } + + // 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) + + constructor(parcel: Parcel) : this( + parcel.readString(), + parcel.readString(), + parcel.readString(), + parcel.readString(), + parcel.readByte() != 0.toByte(), + parcel.readString()) + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeString(description) + parcel.writeString(name) + parcel.writeString(email) + parcel.writeString(signature) + parcel.writeByte(if (signatureUse) 1 else 0) + parcel.writeString(replyTo) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): Identity { + return Identity(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } +} diff --git a/app/core/src/test/java/com/fsck/k9/crypto/OpenPgpApiHelperTest.java b/app/core/src/test/java/com/fsck/k9/crypto/OpenPgpApiHelperTest.java deleted file mode 100644 index abb50e14f..000000000 --- a/app/core/src/test/java/com/fsck/k9/crypto/OpenPgpApiHelperTest.java +++ /dev/null @@ -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 ", result); - } - - @Test - public void buildUserId_withoutName_shouldCreateOpenPgpAccountName() { - Identity identity = new Identity(); - identity.setEmail("user@domain.com"); - - String result = OpenPgpApiHelper.buildUserId(identity); - - assertEquals("", result); - } - -} diff --git a/app/core/src/test/java/com/fsck/k9/crypto/OpenPgpApiHelperTest.kt b/app/core/src/test/java/com/fsck/k9/crypto/OpenPgpApiHelperTest.kt new file mode 100644 index 000000000..5703951fb --- /dev/null +++ b/app/core/src/test/java/com/fsck/k9/crypto/OpenPgpApiHelperTest.kt @@ -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 ", result) + } + + @Test + fun buildUserId_withoutName_shouldCreateOpenPgpAccountName() { + val identity = Identity( + email = "user@domain.com" + ) + + val result = OpenPgpApiHelper.buildUserId(identity) + + assertEquals("", result) + } + +} diff --git a/app/core/src/test/java/com/fsck/k9/helper/IdentityHelperTest.kt b/app/core/src/test/java/com/fsck/k9/helper/IdentityHelperTest.kt index 5a2a0adb2..d7ade0cdd 100644 --- a/app/core/src/test/java/com/fsck/k9/helper/IdentityHelperTest.kt +++ b/app/core/src/test/java/com/fsck/k9/helper/IdentityHelperTest.kt @@ -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): Message { return MimeMessage().apply { diff --git a/app/core/src/test/java/com/fsck/k9/message/MessageBuilderTest.java b/app/core/src/test/java/com/fsck/k9/message/MessageBuilderTest.java index cdea293cc..97b090bcd 100644 --- a/app/core/src/test/java/com/fsck/k9/message/MessageBuilderTest.java +++ b/app/core/src/test/java/com/fsck/k9/message/MessageBuilderTest.java @@ -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 + ); } } diff --git a/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt b/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt index ad71e0758..dc9634031 100644 --- a/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt +++ b/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt @@ -16,7 +16,7 @@ class EditIdentity : K9Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - mIdentity = intent.getSerializableExtra(EXTRA_IDENTITY) as Identity + mIdentity = intent.getParcelableExtra(EXTRA_IDENTITY) mIdentityIndex = intent.getIntExtra(EXTRA_IDENTITY_INDEX, -1) val accountUuid = intent.getStringExtra(EXTRA_ACCOUNT) mAccount = Preferences.getPreferences(this).getAccount(accountUuid) @@ -27,12 +27,8 @@ class EditIdentity : K9Activity() { 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 = savedInstanceState.getSerializable(EXTRA_IDENTITY) as Identity + savedInstanceState?.getParcelable(EXTRA_IDENTITY)?.let { + mIdentity = it } description.setText(mIdentity.description) @@ -61,19 +57,15 @@ class EditIdentity : K9Activity() { } private fun saveIdentity() { - - mIdentity.description = description.text.toString() - mIdentity.email = email.text.toString() - // mIdentity.setAlwaysBcc(mAccountAlwaysBcc.getText().toString()); - mIdentity.name = name.text.toString() - mIdentity.signatureUse = signature_use.isChecked - mIdentity.signature = signature.text.toString() - - if (reply_to.text.isEmpty()) { - mIdentity.replyTo = null - } else { - mIdentity.replyTo = reply_to.text.toString() - } + mIdentity = mIdentity.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 + ) + // mIdentity.setAlwaysBcc(mAccountAlwaysBcc.getText().toString()); val identities = mAccount.identities if (mIdentityIndex == -1) { @@ -95,7 +87,7 @@ class EditIdentity : K9Activity() { public override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putSerializable(EXTRA_IDENTITY, mIdentity) + outState.putParcelable(EXTRA_IDENTITY, mIdentity) } companion object { diff --git a/app/ui/src/main/java/com/fsck/k9/activity/MessageCompose.java b/app/ui/src/main/java/com/fsck/k9/activity/MessageCompose.java index 33d70565d..348c310b4 100644 --- a/app/ui/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/app/ui/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -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)) { diff --git a/app/ui/src/main/java/com/fsck/k9/ui/endtoend/AutocryptKeyTransferPresenter.kt b/app/ui/src/main/java/com/fsck/k9/ui/endtoend/AutocryptKeyTransferPresenter.kt index 8cf322b58..abbe8a7c7 100644 --- a/app/ui/src/main/java/com/fsck/k9/ui/endtoend/AutocryptKeyTransferPresenter.kt +++ b/app/ui/src/main/java/com/fsck/k9/ui/endtoend/AutocryptKeyTransferPresenter.kt @@ -51,7 +51,7 @@ class AutocryptKeyTransferPresenter internal constructor( } }) - view.setAddress(account.identities[0].email) + view.setAddress(account.identities[0].email!!) viewModel.autocryptSetupTransferLiveEvent.recall() } diff --git a/app/ui/src/test/java/com/fsck/k9/message/PgpMessageBuilderTest.kt b/app/ui/src/test/java/com/fsck/k9/message/PgpMessageBuilderTest.kt index 48cc483c3..ee68b52ac 100644 --- a/app/ui/src/test/java/com/fsck/k9/message/PgpMessageBuilderTest.kt +++ b/app/ui/src/test/java/com/fsck/k9/message/PgpMessageBuilderTest.kt @@ -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()) From 85a0c1af4d0749ee856cd1fbc90b87d8298f23a7 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 26 Nov 2019 16:30:23 +0100 Subject: [PATCH 3/4] use Parcelize instead of manual Parcelable implementation --- .../src/main/java/com/fsck/k9/Identity.kt | 39 +------------------ 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/Identity.kt b/app/core/src/main/java/com/fsck/k9/Identity.kt index 5af1e7fe4..fa6c55b94 100644 --- a/app/core/src/main/java/com/fsck/k9/Identity.kt +++ b/app/core/src/main/java/com/fsck/k9/Identity.kt @@ -1,8 +1,9 @@ package com.fsck.k9 -import android.os.Parcel import android.os.Parcelable +import kotlinx.android.parcel.Parcelize +@Parcelize data class Identity( val description: String? = null, val name: String? = null, @@ -11,45 +12,9 @@ data class Identity( val signatureUse: Boolean = false, val replyTo: String? = null ) : Parcelable { - override fun toString(): String { - return "Account.Identity(description=" + description + ", name=" + name + ", email=" + email + ", replyTo=" + replyTo + ", signature=" + - signature - } - // 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) - - constructor(parcel: Parcel) : this( - parcel.readString(), - parcel.readString(), - parcel.readString(), - parcel.readString(), - parcel.readByte() != 0.toByte(), - parcel.readString()) - - override fun writeToParcel(parcel: Parcel, flags: Int) { - parcel.writeString(description) - parcel.writeString(name) - parcel.writeString(email) - parcel.writeString(signature) - parcel.writeByte(if (signatureUse) 1 else 0) - parcel.writeString(replyTo) - } - - override fun describeContents(): Int { - return 0 - } - - companion object CREATOR : Parcelable.Creator { - override fun createFromParcel(parcel: Parcel): Identity { - return Identity(parcel) - } - - override fun newArray(size: Int): Array { - return arrayOfNulls(size) - } - } } From ac85f8af5437241e31f9c6ab0914c51ecdcca1e2 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 26 Nov 2019 16:32:56 +0100 Subject: [PATCH 4/4] remove hungarian notation from EditIdentity --- .../java/com/fsck/k9/activity/EditIdentity.kt | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt b/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt index dc9634031..41e62d039 100644 --- a/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt +++ b/app/ui/src/main/java/com/fsck/k9/activity/EditIdentity.kt @@ -9,55 +9,55 @@ import com.fsck.k9.ui.R import kotlinx.android.synthetic.main.edit_identity.* class EditIdentity : K9Activity() { - private lateinit var mAccount: Account - private lateinit var mIdentity: Identity - private var mIdentityIndex: Int = 0 + private lateinit var account: Account + private lateinit var identity: Identity + private var identityIndex: Int = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - mIdentity = intent.getParcelableExtra(EXTRA_IDENTITY) - mIdentityIndex = intent.getIntExtra(EXTRA_IDENTITY_INDEX, -1) + identity = intent.getParcelableExtra(EXTRA_IDENTITY) + identityIndex = intent.getIntExtra(EXTRA_IDENTITY_INDEX, -1) val accountUuid = intent.getStringExtra(EXTRA_ACCOUNT) - mAccount = Preferences.getPreferences(this).getAccount(accountUuid) + account = Preferences.getPreferences(this).getAccount(accountUuid) - if (mIdentityIndex == -1) { - mIdentity = Identity() + if (identityIndex == -1) { + identity = Identity() } setLayout(R.layout.edit_identity) savedInstanceState?.getParcelable(EXTRA_IDENTITY)?.let { - mIdentity = it + identity = it } - description.setText(mIdentity.description) - name.setText(mIdentity.name) - email.setText(mIdentity.email) - reply_to.setText(mIdentity.replyTo) + 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(mIdentity.getAlwaysBcc()); + // mAccountAlwaysBcc.setText(identity.getAlwaysBcc()); - signature_use.isChecked = mIdentity.signatureUse + signature_use.isChecked = identity.signatureUse signature_use.setOnCheckedChangeListener { _, isChecked -> if (isChecked) { signature_layout.visibility = View.VISIBLE - signature.setText(mIdentity.signature) + signature.setText(identity.signature) } else { signature_layout.visibility = View.GONE } } if (signature_use.isChecked) { - signature.setText(mIdentity.signature) + signature.setText(identity.signature) } else { signature_layout.visibility = View.GONE } } private fun saveIdentity() { - mIdentity = mIdentity.copy( + identity = identity.copy( description = description.text.toString(), email = email.text.toString(), name = name.text.toString(), @@ -65,17 +65,17 @@ class EditIdentity : K9Activity() { signature = signature.text.toString(), replyTo = if (reply_to.text.isNotEmpty()) reply_to.text.toString() else null ) - // mIdentity.setAlwaysBcc(mAccountAlwaysBcc.getText().toString()); + // identity.setAlwaysBcc(mAccountAlwaysBcc.getText().toString()); - val identities = mAccount.identities - if (mIdentityIndex == -1) { - identities.add(mIdentity) + val identities = account.identities + if (identityIndex == -1) { + identities.add(identity) } else { - identities.removeAt(mIdentityIndex) - identities.add(mIdentityIndex, mIdentity) + identities.removeAt(identityIndex) + identities.add(identityIndex, identity) } - Preferences.getPreferences(applicationContext).saveAccount(mAccount) + Preferences.getPreferences(applicationContext).saveAccount(account) finish() } @@ -87,7 +87,7 @@ class EditIdentity : K9Activity() { public override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putParcelable(EXTRA_IDENTITY, mIdentity) + outState.putParcelable(EXTRA_IDENTITY, identity) } companion object {