From 9d62a6f7456d5980433a1e4d58a35e2f4dbbccdb Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Sat, 17 Aug 2024 20:41:18 +0200 Subject: [PATCH] fix: improve detekt score and threshold Signed-off-by: Andy Scherzinger --- .../data/network/OfflineFirstChatRepository.kt | 8 ++++---- .../java/com/nextcloud/talk/utils/DisplayUtils.kt | 10 ++++++++-- .../java/com/nextcloud/talk/utils/PushUtils.kt | 14 +++++++++----- .../talk/utils/preferences/AppPreferencesImpl.kt | 12 ++++++++---- detekt.yml | 2 +- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt index e635ecb38e..01174fd24a 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt @@ -504,12 +504,12 @@ class OfflineFirstChatRepository @Inject constructor( chatBlock.newestMessageId ).first() - if (connectedChatBlocks.size == 1) { + return if (connectedChatBlocks.size == 1) { Log.d(TAG, "This chatBlock is not connected to others") val chatBlockFromDb = connectedChatBlocks[0] Log.d(TAG, "chatBlockFromDb.oldestMessageId: " + chatBlockFromDb.oldestMessageId) Log.d(TAG, "chatBlockFromDb.newestMessageId: " + chatBlockFromDb.newestMessageId) - return chatBlockFromDb + chatBlockFromDb } else if (connectedChatBlocks.size > 1) { Log.d(TAG, "Found " + connectedChatBlocks.size + " chat blocks that are connected") val oldestIdFromDbChatBlocks = @@ -536,10 +536,10 @@ class OfflineFirstChatRepository @Inject constructor( Log.d(TAG, "A new chat block was created that covers all the range of the found chatblocks") Log.d(TAG, "new chatBlock - oldest MessageId: $oldestIdFromDbChatBlocks") Log.d(TAG, "new chatBlock - newest MessageId: $newestIdFromDbChatBlocks") - return newChatBlock + newChatBlock } else { Log.d(TAG, "No chat block found ....") - return null + null } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.kt index d26239cd94..14af5df700 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.kt @@ -77,6 +77,8 @@ object DisplayUtils { private const val TWITTER_HANDLE_PREFIX = "@" private const val HTTP_PROTOCOL = "http://" private const val HTTPS_PROTOCOL = "https://" + private const val HTTP_MIN_LENGTH: Int = 7 + private const val HTTPS_MIN_LENGTH: Int = 7 private const val DATE_TIME_PARTS_SIZE = 2 fun isDarkModeOn(context: Context): Boolean { val currentNightMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK @@ -394,10 +396,14 @@ object DisplayUtils { if (TextUtils.isEmpty(url)) { return "" } - if (url!!.length >= 7 && HTTP_PROTOCOL.equals(url.substring(0, 7), ignoreCase = true)) { + if (url!!.length >= HTTP_MIN_LENGTH && + HTTP_PROTOCOL.equals(url.substring(0, HTTP_MIN_LENGTH), ignoreCase = true) + ) { return url.substring(HTTP_PROTOCOL.length).trim { it <= ' ' } } - return if (url.length >= 8 && HTTPS_PROTOCOL.equals(url.substring(0, 8), ignoreCase = true)) { + return if (url.length >= HTTPS_MIN_LENGTH && + HTTPS_PROTOCOL.equals(url.substring(0, HTTPS_MIN_LENGTH), ignoreCase = true) + ) { url.substring(HTTPS_PROTOCOL.length).trim { it <= ' ' } } else { url.trim { it <= ' ' } diff --git a/app/src/main/java/com/nextcloud/talk/utils/PushUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/PushUtils.kt index 68c79a02d1..d254361d4c 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/PushUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/PushUtils.kt @@ -162,26 +162,26 @@ class PushUtils { var keyGen: KeyPairGenerator? = null try { keyGen = KeyPairGenerator.getInstance("RSA") - keyGen.initialize(2048) + keyGen.initialize(RSA_KEY_SIZE) val pair = keyGen.generateKeyPair() val statusPrivate = saveKeyToFile(pair.private, privateKeyFile.absolutePath) val statusPublic = saveKeyToFile(pair.public, publicKeyFile.absolutePath) return if (statusPrivate == 0 && statusPublic == 0) { // all went well - 0 + RETURN_CODE_KEY_GENERATION_SUCCESSFUL } else { - -2 + RETURN_CODE_KEY_GENERATION_FAILED } } catch (e: NoSuchAlgorithmException) { Log.d(TAG, "RSA algorithm not supported") } } else { // We already have the key - return -1 + return RETURN_CODE_KEY_ALREADY_EXISTS } // we failed to generate the key - return -2 + return RETURN_CODE_KEY_GENERATION_FAILED } fun pushRegistrationToServer(ncApi: NcApi) { @@ -399,6 +399,10 @@ class PushUtils { companion object { private const val TAG = "PushUtils" + private const val RSA_KEY_SIZE: Int = 2048 + private const val RETURN_CODE_KEY_GENERATION_SUCCESSFUL: Int = 0 + private const val RETURN_CODE_KEY_ALREADY_EXISTS: Int = -1 + private const val RETURN_CODE_KEY_GENERATION_FAILED: Int = -2 const val LATEST_PUSH_REGISTRATION_AT_SERVER: String = "LATEST_PUSH_REGISTRATION_AT_SERVER" const val LATEST_PUSH_REGISTRATION_AT_PUSH_PROXY: String = "LATEST_PUSH_REGISTRATION_AT_PUSH_PROXY" } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt index 849755d8bf..267c86eb44 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt @@ -503,10 +503,10 @@ class AppPreferencesImpl(val context: Context) : AppPreferences { for (msgStr in queueStr.split("]")) { try { val msgArray = msgStr.replace("[", "").split(",") - val message = msgArray[0] - val replyTo = msgArray[1].toInt() - val displayName = msgArray[2] - val silent = msgArray[3].toBoolean() + val message = msgArray[MESSAGE_INDEX] + val replyTo = msgArray[REPLY_TO_INDEX].toInt() + val displayName = msgArray[DISPLY_NAME_INDEX] + val silent = msgArray[SILENT_INDEX].toBoolean() val qMsg = MessageInputViewModel.QueuedMessage(message, displayName, replyTo, silent) queue.add(qMsg) @@ -570,6 +570,10 @@ class AppPreferencesImpl(val context: Context) : AppPreferences { @Suppress("UnusedPrivateProperty") private val TAG = AppPreferencesImpl::class.simpleName private val Context.dataStore: DataStore by preferencesDataStore(name = "settings") + private const val MESSAGE_INDEX: Int = 0 + private const val REPLY_TO_INDEX: Int = 1 + private const val DISPLY_NAME_INDEX: Int = 2 + private const val SILENT_INDEX: Int = 3 const val PROXY_TYPE = "proxy_type" const val PROXY_SERVER = "proxy_server" const val PROXY_HOST = "proxy_host" diff --git a/detekt.yml b/detekt.yml index 5ef9ee8c47..af081fade4 100644 --- a/detekt.yml +++ b/detekt.yml @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors # SPDX-License-Identifier: GPL-3.0-or-later build: - maxIssues: 138 + maxIssues: 166 weights: # complexity: 2 # LongParameterList: 1