diff --git a/app/src/main/java/com/etesync/syncadapter/NotificationHelper.kt b/app/src/main/java/com/etesync/syncadapter/NotificationHelper.kt index f9a1795f..5ecb127a 100644 --- a/app/src/main/java/com/etesync/syncadapter/NotificationHelper.kt +++ b/app/src/main/java/com/etesync/syncadapter/NotificationHelper.kt @@ -72,8 +72,8 @@ class NotificationHelper(internal val context: Context, internal val notificatio } @JvmOverloads - fun notify(title: String, content: String, bigText: String?, intent: Intent, icon: Int = -1) { - var icon = icon + fun notify(title: String, content: String, bigText: String?, intent: Intent, _icon: Int = -1) { + var icon = _icon createNotificationChannel() val builder = NotificationCompat.Builder(context) val category = if (throwable == null) diff --git a/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.kt b/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.kt index 78540a3c..86aab047 100644 --- a/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.kt @@ -3,7 +3,6 @@ package com.etesync.syncadapter.journalmanager import com.etesync.syncadapter.App import com.etesync.syncadapter.GsonHelper import okhttp3.* -import org.apache.commons.codec.Charsets import java.io.ByteArrayOutputStream import java.io.IOException import java.net.HttpURLConnection @@ -51,11 +50,11 @@ abstract class BaseManager { var uid: String? = null fun getContent(crypto: Crypto.CryptoManager): String { - return String(crypto.decrypt(content!!)!!, Charsets.UTF_8) + return String(crypto.decrypt(content!!)!!) } fun setContent(crypto: Crypto.CryptoManager, content: String) { - this.content = crypto.encrypt(content.toByteArray(Charsets.UTF_8)) + this.content = crypto.encrypt(content.toByteArray()) } fun calculateHmac(crypto: Crypto.CryptoManager, uuid: String?): ByteArray { @@ -63,7 +62,7 @@ abstract class BaseManager { try { if (uuid != null) { - hashContent.write(uuid.toByteArray(Charsets.UTF_8)) + hashContent.write(uuid.toByteArray()) } hashContent.write(content!!) diff --git a/app/src/main/java/com/etesync/syncadapter/journalmanager/Crypto.kt b/app/src/main/java/com/etesync/syncadapter/journalmanager/Crypto.kt index 3f14c9cc..a810cfb8 100644 --- a/app/src/main/java/com/etesync/syncadapter/journalmanager/Crypto.kt +++ b/app/src/main/java/com/etesync/syncadapter/journalmanager/Crypto.kt @@ -37,7 +37,7 @@ object Crypto { fun deriveKey(salt: String, password: String): String { val keySize = 190 - return Base64.encodeToString(SCrypt.generate(password.toByteArray(Charsets.UTF_8), salt.toByteArray(Charsets.UTF_8), 16384, 8, 1, keySize), Base64.NO_WRAP) + return Base64.encodeToString(SCrypt.generate(password.toByteArray(), salt.toByteArray(), 16384, 8, 1, keySize), Base64.NO_WRAP) } @JvmStatic @@ -130,8 +130,8 @@ object Crypto { get() = SecureRandom() private fun setDerivedKey(derivedKey: ByteArray?) { - cipherKey = hmac256("aes".toByteArray(Charsets.UTF_8), derivedKey) - hmacKey = hmac256("hmac".toByteArray(Charsets.UTF_8), derivedKey) + cipherKey = hmac256("aes".toByteArray(), derivedKey) + hmacKey = hmac256("hmac".toByteArray(), derivedKey) } constructor(version: Int, keyPair: AsymmetricKeyPair, encryptedKey: ByteArray) { @@ -151,7 +151,7 @@ object Crypto { } else if (version == 1) { derivedKey = Base64.decode(keyBase64, Base64.NO_WRAP) } else { - derivedKey = hmac256(salt.toByteArray(Charsets.UTF_8), Base64.decode(keyBase64, Base64.NO_WRAP)) + derivedKey = hmac256(salt.toByteArray(), Base64.decode(keyBase64, Base64.NO_WRAP)) } this.version = version.toByte() @@ -246,7 +246,7 @@ object Crypto { } internal fun sha256(base: String): String { - return toHex(sha256(base.toByteArray(Charsets.UTF_8))) + return toHex(sha256(base.toByteArray())) } private fun sha256(base: ByteArray): ByteArray { diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt index efdaeab3..1909795a 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt @@ -102,7 +102,7 @@ class LocalAddressBook( // HELPERS fun accountName(mainAccount: Account, info: CollectionInfo): String { - val displayName = if (info.displayName != null) info.displayName else info.uid + val displayName = info.displayName ?: info.uid!! val sb = StringBuilder(displayName) sb.append(" (") .append(mainAccount.name) diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt index 9afaa613..e5a97025 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt @@ -121,7 +121,7 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra @Throws(CalendarStorageException::class, ContactsStorageException::class, IOException::class) private fun createInviteAttendeesNotification() { for (local in localDirty) { - val event = (local as LocalEvent).event + val event = local.event if (event?.attendees?.isEmpty()!!) { return @@ -144,8 +144,8 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra } @Throws(IOException::class, ContactsStorageException::class, CalendarStorageException::class) - private fun processEvent(newData: Event, localEvent: LocalEvent?): LocalEvent { - var localEvent = localEvent + private fun processEvent(newData: Event, _localEvent: LocalEvent?): LocalEvent { + var localEvent = _localEvent // delete local event, if it exists if (localEvent != null) { App.log.info("Updating " + newData.uid + " in local calendar") diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncManager.kt index 6572d198..d4995a25 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncManager.kt @@ -52,7 +52,7 @@ import java.io.StringReader * Synchronization manager for CardDAV collections; handles contacts and groups. */ class ContactsSyncManager @Throws(Exceptions.IntegrityException::class, Exceptions.GenericCryptoException::class, ContactsStorageException::class) -constructor(context: Context, account: Account, settings: AccountSettings, extras: Bundle, authority: String, private val provider: ContentProviderClient, result: SyncResult, localAddressBook: LocalAddressBook, private val remote: HttpUrl) : SyncManager(context, account, settings, extras, authority, result, localAddressBook.url!!, CollectionInfo.Type.ADDRESS_BOOK, localAddressBook.mainAccount.name) { +constructor(context: Context, account: Account, settings: AccountSettings, extras: Bundle, authority: String, private val provider: ContentProviderClient, result: SyncResult, localAddressBook: LocalAddressBook, private val remote: HttpUrl) : SyncManager(context, account, settings, extras, authority, result, localAddressBook.url, CollectionInfo.Type.ADDRESS_BOOK, localAddressBook.mainAccount.name) { protected override val syncErrorTitle: String get() = context.getString(R.string.sync_error_contacts, account.name) @@ -171,8 +171,8 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra } @Throws(IOException::class, ContactsStorageException::class) - private fun processContact(newData: Contact, local: LocalAddress?): LocalAddress { - var local = local + private fun processContact(newData: Contact, _local: LocalAddress?): LocalAddress { + var local = _local val uuid = newData.uid // update local contact, if it exists if (local != null) { @@ -181,14 +181,14 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra if (local is LocalGroup && newData.group) { // update group val group: LocalGroup = local - group!!.eTag = uuid + group.eTag = uuid group.update(newData) syncResult.stats.numUpdates++ } else if (local is LocalContact && !newData.group) { // update contact val contact: LocalContact = local - contact!!.eTag = uuid + contact.eTag = uuid contact.update(newData) syncResult.stats.numUpdates++ diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt index 8e36e9f2..dbdac24d 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt @@ -99,8 +99,8 @@ class TasksSyncManager( } } - private fun processTask(newData: Task, localTask: LocalTask?): LocalTask { - var localTask = localTask + private fun processTask(newData: Task, _localTask: LocalTask?): LocalTask { + var localTask = _localTask // delete local Task, if it exists if (localTask != null) { App.log.info("Updating " + newData.uid + " in local calendar") diff --git a/app/src/main/java/com/etesync/syncadapter/ui/AboutActivity.kt b/app/src/main/java/com/etesync/syncadapter/ui/AboutActivity.kt index 55edaa48..924c922d 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/AboutActivity.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/AboutActivity.kt @@ -105,7 +105,7 @@ class AboutActivity : BaseActivity() { override fun onLoadFinished(loader: Loader, license: Spanned) { if (view != null) { - val tv = view!!.findViewById(R.id.license_text) as TextView + val tv = view!!.findViewById(R.id.license_text) as TextView? if (tv != null) { tv.autoLinkMask = Linkify.EMAIL_ADDRESSES or Linkify.WEB_URLS tv.text = license diff --git a/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.kt b/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.kt index ca5b0144..06a7757f 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.kt @@ -50,20 +50,19 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe internal var listCardDAV: ListView? = null internal var listTaskDAV: ListView? = null - private val onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id -> + private val onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, _ -> val list = parent as ListView val adapter = list.adapter as ArrayAdapter<*> val journalEntity = adapter.getItem(position) as JournalEntity val info = journalEntity.getInfo() - startActivity(ViewCollectionActivity.newIntent(this@AccountActivity, account!!, info)) + startActivity(ViewCollectionActivity.newIntent(this@AccountActivity, account, info)) } private val formattedFingerprint: String? get() { - var settings: AccountSettings? = null try { - settings = AccountSettings(this, account!!) + val settings = AccountSettings(this, account) return Crypto.AsymmetricCryptoManager.getPrettyKeyFingerprint(settings.keyPair!!.publicKey) } catch (e: Exception) { e.printStackTrace() @@ -76,7 +75,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe super.onCreate(savedInstanceState) account = intent.getParcelableExtra(EXTRA_ACCOUNT) - title = account!!.name + title = account.name setContentView(R.layout.activity_account) @@ -113,8 +112,8 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe HintManager.setHintSeen(this, HINT_VIEW_COLLECTION, true) } - if (!SetupUserInfoFragment.hasUserInfo(this, account!!)) { - SetupUserInfoFragment.newInstance(account!!).show(supportFragmentManager, null) + if (!SetupUserInfoFragment.hasUserInfo(this, account)) { + SetupUserInfoFragment.newInstance(account).show(supportFragmentManager, null) } } @@ -136,7 +135,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe .setTitle(R.string.account_delete_confirmation_title) .setMessage(R.string.account_delete_confirmation_text) .setNegativeButton(android.R.string.no, null) - .setPositiveButton(android.R.string.yes) { dialog, which -> deleteAccount() } + .setPositiveButton(android.R.string.yes) { _, _ -> deleteAccount() } .show() R.id.show_fingerprint -> { val view = layoutInflater.inflate(R.layout.fingerprint_alertdialog, null) @@ -146,7 +145,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe .setIcon(R.drawable.ic_fingerprint_dark) .setTitle(R.string.show_fingperprint_title) .setView(view) - .setPositiveButton(android.R.string.yes) { dialog, which -> }.create() + .setPositiveButton(android.R.string.yes) { _, _ -> }.create() dialog.show() } else -> return super.onOptionsItemSelected(item) @@ -160,17 +159,17 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe R.id.create_calendar -> { info = CollectionInfo() info.type = CollectionInfo.Type.CALENDAR - startActivity(CreateCollectionActivity.newIntent(this@AccountActivity, account!!, info)) + startActivity(CreateCollectionActivity.newIntent(this@AccountActivity, account, info)) } R.id.create_tasklist -> { info = CollectionInfo() info.type = CollectionInfo.Type.TASKS - startActivity(CreateCollectionActivity.newIntent(this@AccountActivity, account!!, info)) + startActivity(CreateCollectionActivity.newIntent(this@AccountActivity, account, info)) } R.id.create_addressbook -> { info = CollectionInfo() info.type = CollectionInfo.Type.ADDRESS_BOOK - startActivity(CreateCollectionActivity.newIntent(this@AccountActivity, account!!, info)) + startActivity(CreateCollectionActivity.newIntent(this@AccountActivity, account, info)) } } return false @@ -192,7 +191,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe } override fun onCreateLoader(id: Int, args: Bundle?): Loader { - return AccountLoader(this, account!!) + return AccountLoader(this, account) } override fun refresh() { @@ -211,7 +210,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe listCardDAV!!.isEnabled = !info.carddav!!.refreshing listCardDAV!!.setAlpha(if (info.carddav!!.refreshing) 0.5f else 1f) - val adapter = CollectionListAdapter(this, account!!) + val adapter = CollectionListAdapter(this, account) adapter.addAll(info.carddav!!.journals!!) listCardDAV!!.adapter = adapter listCardDAV!!.onItemClickListener = onItemClickListener @@ -227,7 +226,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe listCalDAV!!.isEnabled = !info.caldav!!.refreshing listCalDAV!!.setAlpha(if (info.caldav!!.refreshing) 0.5f else 1f) - val adapter = CollectionListAdapter(this, account!!) + val adapter = CollectionListAdapter(this, account) adapter.addAll(info.caldav!!.journals!!) listCalDAV!!.adapter = adapter listCalDAV!!.onItemClickListener = onItemClickListener @@ -243,7 +242,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe listTaskDAV!!.isEnabled = !info.taskdav!!.refreshing listTaskDAV!!.setAlpha(if (info.taskdav!!.refreshing) 0.5f else 1f) - val adapter = CollectionListAdapter(this, account!!) + val adapter = CollectionListAdapter(this, account) adapter.addAll(info.taskdav!!.journals!!) listTaskDAV!!.adapter = adapter listTaskDAV!!.onItemClickListener = onItemClickListener diff --git a/app/src/main/java/com/etesync/syncadapter/ui/RemoveMemberFragment.kt b/app/src/main/java/com/etesync/syncadapter/ui/RemoveMemberFragment.kt index 09ed1940..9dd731ca 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/RemoveMemberFragment.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/RemoveMemberFragment.kt @@ -54,7 +54,7 @@ class RemoveMemberFragment : DialogFragment() { val journalsManager = JournalManager(httpClient!!, remote!!) val journal = JournalManager.Journal.fakeWithUid(info!!.uid!!) - val member = JournalManager.Member(memberEmail!!, "placeholder".toByteArray(Charsets.UTF_8)) + val member = JournalManager.Member(memberEmail!!, "placeholder".toByteArray()) journalsManager.deleteMember(journal, member) return RemoveResult(null) diff --git a/app/src/test/java/com/etesync/syncadapter/journalmanager/EncryptionTest.kt b/app/src/test/java/com/etesync/syncadapter/journalmanager/EncryptionTest.kt index 1ca9de6f..f447def8 100644 --- a/app/src/test/java/com/etesync/syncadapter/journalmanager/EncryptionTest.kt +++ b/app/src/test/java/com/etesync/syncadapter/journalmanager/EncryptionTest.kt @@ -44,11 +44,11 @@ class EncryptionTest { val cryptoManager = Crypto.CryptoManager(1, Helpers.keyBase64, "TestSaltShouldBeJournalId") val clearText = "This Is Some Test Cleartext." - val cipher = cryptoManager.encrypt(clearText.toByteArray(Charsets.UTF_8)) - assertEquals(clearText, String(cryptoManager.decrypt(cipher!!)!!, Charsets.UTF_8)) + val cipher = cryptoManager.encrypt(clearText.toByteArray()) + assertEquals(clearText, String(cryptoManager.decrypt(cipher!!)!!)) val expected = "Lz+HUFzh1HdjxuGdQrBwBG1IzHT0ug6mO8fwePSbXtc=" - assertEquals(expected, Base64.encodeToString(cryptoManager.hmac("Some test data".toByteArray(Charsets.UTF_8)), Base64.NO_WRAP)) + assertEquals(expected, Base64.encodeToString(cryptoManager.hmac("Some test data".toByteArray()), Base64.NO_WRAP)) } @Test @@ -57,11 +57,11 @@ class EncryptionTest { val cryptoManager = Crypto.CryptoManager(2, Helpers.keyBase64, "TestSaltShouldBeJournalId") val clearText = "This Is Some Test Cleartext." - val cipher = cryptoManager.encrypt(clearText.toByteArray(Charsets.UTF_8)) - assertEquals(clearText, String(cryptoManager.decrypt(cipher!!)!!, Charsets.UTF_8)) + val cipher = cryptoManager.encrypt(clearText.toByteArray()) + assertEquals(clearText, String(cryptoManager.decrypt(cipher!!)!!)) val expected = "XQ/A0gentOaE98R9wzf3zEIAHj4OH1GF8J4C6JiJupo=" - assertEquals(expected, Base64.encodeToString(cryptoManager.hmac("Some test data".toByteArray(Charsets.UTF_8)), Base64.NO_WRAP)) + assertEquals(expected, Base64.encodeToString(cryptoManager.hmac("Some test data".toByteArray()), Base64.NO_WRAP)) } @Test(expected = Exceptions.VersionTooNewException::class) @@ -82,13 +82,13 @@ class EncryptionTest { val keyPair = Crypto.generateKeyPair() val cryptoManager = Crypto.AsymmetricCryptoManager(keyPair!!) - val clearText = "This Is Some Test Cleartext.".toByteArray(Charsets.UTF_8) + val clearText = "This Is Some Test Cleartext.".toByteArray() val cipher = cryptoManager.encrypt(keyPair.publicKey, clearText) val clearText2 = cryptoManager.decrypt(cipher!!) assertArrayEquals(clearText, clearText2) // Mostly for coverage. Make sure it's the expected sha256 value. assertEquals("ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", - Hex.toHexString(Crypto.AsymmetricCryptoManager.getKeyFingerprint("a".toByteArray(Charsets.UTF_8))).toLowerCase()) + Hex.toHexString(Crypto.AsymmetricCryptoManager.getKeyFingerprint("a".toByteArray())).toLowerCase()) } } diff --git a/app/src/test/java/com/etesync/syncadapter/journalmanager/ServiceTest.kt b/app/src/test/java/com/etesync/syncadapter/journalmanager/ServiceTest.kt index 5ba11cfa..9611ab5a 100644 --- a/app/src/test/java/com/etesync/syncadapter/journalmanager/ServiceTest.kt +++ b/app/src/test/java/com/etesync/syncadapter/journalmanager/ServiceTest.kt @@ -232,7 +232,7 @@ class ServiceTest { assertArrayEquals(userInfo.getContent(cryptoManager), userInfo2!!.getContent(cryptoManager)) // Update - userInfo.setContent(cryptoManager, "test".toByteArray(Charsets.UTF_8)) + userInfo.setContent(cryptoManager, "test".toByteArray()) manager.update(userInfo) userInfo2 = manager[Helpers.USER] assertNotNull(userInfo2) @@ -260,7 +260,7 @@ class ServiceTest { assertEquals(journalManager.listMembers(journal).size.toLong(), 0) // Test inviting ourselves - val member = JournalManager.Member(Helpers.USER, "test".toByteArray(Charsets.UTF_8)) + val member = JournalManager.Member(Helpers.USER, "test".toByteArray()) try { caught = null journalManager.addMember(journal, member) @@ -270,7 +270,7 @@ class ServiceTest { assertNotNull(caught) - val member2 = JournalManager.Member(Helpers.USER2, "test".toByteArray(Charsets.UTF_8)) + val member2 = JournalManager.Member(Helpers.USER2, "test".toByteArray()) journalManager.addMember(journal, member2) assertEquals(journalManager.listMembers(journal).size.toLong(), 1)