Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add detekt.yml for kotlin code style analysis (#846)

Leendert de Borstc9c692ce6ebd
diff --git a/apps/mobile-app/android/.editorconfig b/apps/mobile-app/android/.editorconfig
index 11ec7cd16..af1b8d059 100644
--- a/apps/mobile-app/android/.editorconfig
+++ b/apps/mobile-app/android/.editorconfig
@@ -8,47 +8,14 @@ max_line_length = 160
trim_trailing_whitespace = true
[*.{kt,kts}]
-ktlint_standard_max-line-length = 160
+ktlint_standard_no-wildcard-imports = true
+ktlint_standard_filename = true
ktlint_standard_trailing-comma-on-call-site = true
ktlint_standard_trailing-comma-on-declaration-site = true
-ktlint_standard_wrapping = true
-ktlint_standard_filename = true
ktlint_standard_import-ordering = true
+ktlint_standard_comment-wrapping = true
ktlint_standard_annotation = true
-ktlint_standard_comment-spacing = true
-ktlint_standard_curly-spacing = true
-ktlint_standard_discouraged-comment-location = true
-ktlint_standard_empty-blocks = true
-ktlint_standard_enum-entry-name-case = true
-ktlint_standard_final-newline = true
-ktlint_standard_fun-keyword-spacing = true
-ktlint_standard_import-ordering = true
-ktlint_standard_import-spacing = true
-ktlint_standard_indent = true
-ktlint_standard_initializer-list = true
-ktlint_standard_ktlint_standard_max-line-length = true
-ktlint_standard_modifier-order = true
-ktlint_standard_no-blank-line-before-rbrace = true
-ktlint_standard_no-consecutive-blank-lines = true
-ktlint_standard_no-empty-file = true
-ktlint_standard_no-line-break-after-else = true
-ktlint_standard_no-line-break-before-assignment = true
-ktlint_standard_no-multi-spaces = true
-ktlint_standard_no-semi = true
-ktlint_standard_no-trailing-spaces = true
-ktlint_standard_no-unit-return = true
-ktlint_standard_no-unused-imports = true
-ktlint_standard_no-wildcard-imports = true
-ktlint_standard_parameter-list-wrapping = true
-ktlint_standard_spacing-around-colon = true
-ktlint_standard_spacing-around-comma = true
-ktlint_standard_spacing-around-dot = true
-ktlint_standard_spacing-around-keyword = true
-ktlint_standard_spacing-around-operators = true
-ktlint_standard_spacing-around-parens = true
-ktlint_standard_spacing-around-range = true
-ktlint_standard_string-formatting = true
-ktlint_standard_trailing-comma-on-call-site = true
-ktlint_standard_trailing-comma-on-declaration-site = true
-ktlint_standard_trailing-spaces = true
-ktlint_standard_wrapping = true
\ No newline at end of file
+ktlint_standard_public-method-documentation = true
+
+# Detekt rules
+detekt.style.PublicFunctionDocumentation = enabled
diff --git a/apps/mobile-app/android/app/build.gradle b/apps/mobile-app/android/app/build.gradle
index 52957b678..1e83c715b 100644
--- a/apps/mobile-app/android/app/build.gradle
+++ b/apps/mobile-app/android/app/build.gradle
@@ -2,6 +2,7 @@ apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"
apply plugin: "org.jlleitschuh.gradle.ktlint"
+apply plugin: "io.gitlab.arturbosch.detekt"
def projectRoot = rootDir.getAbsoluteFile().getParentFile().getAbsolutePath()
@@ -230,9 +231,10 @@ task lintFixAll {
}
task lintAll {
- description = "Run all linting checks (ktlint and Android lint)"
+ description = "Run all linting checks (ktlint, Detekt and Android lint)"
group = "formatting"
dependsOn 'ktlintCheck'
+ dependsOn 'detekt'
}
// Fix for expo-dev-menu lint model generation
@@ -256,3 +258,17 @@ afterEvaluate {
dependsOn("generateCodegenArtifactsFromSchema")
}
}
+
+detekt {
+ buildUponDefaultConfig = true
+ config = files("$rootProject.projectDir/detekt.yml")
+ baseline = file("$rootProject.projectDir/baseline.xml")
+
+ source.setFrom(files("src/main/java"))
+ reports {
+ html.required.set(true)
+ xml.required.set(true)
+ txt.required.set(false)
+ sarif.required.set(false)
+ }
+}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/autofill/utils/ImageUtils.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/autofill/utils/ImageUtils.kt
index de9fd1a2a..e4d268ca7 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/autofill/utils/ImageUtils.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/autofill/utils/ImageUtils.kt
@@ -9,11 +9,19 @@ import android.util.Log
import com.caverock.androidsvg.SVG
import java.io.ByteArrayOutputStream
+/**
+ * Utility class for image operations.
+ */
object ImageUtils {
private const val TAG = "ImageUtils"
private const val TARGET_SIZE_DP = 24
private const val RENDER_SCALE_FACTOR = 4
+ /**
+ * Detect the mime type of the bytes.
+ * @param bytes The bytes to detect the mime type of
+ * @return The mime type
+ */
fun detectMimeType(bytes: ByteArray): String {
// SVG heuristic
if (bytes.size >= 5) {
@@ -43,6 +51,11 @@ object ImageUtils {
return "image/x-icon"
}
+ /**
+ * Convert bytes to a bitmap.
+ * @param bytes The bytes to convert
+ * @return The bitmap
+ */
fun bytesToBitmap(bytes: ByteArray): Bitmap? {
return try {
when (detectMimeType(bytes)) {
@@ -55,6 +68,11 @@ object ImageUtils {
}
}
+ /**
+ * Convert SVG bytes to a bitmap.
+ * @param bytes The bytes to convert
+ * @return The bitmap
+ */
private fun svgToBitmap(bytes: ByteArray): Bitmap? {
return try {
val svg = SVG.getFromString(String(bytes, Charsets.UTF_8))
@@ -87,6 +105,11 @@ object ImageUtils {
}
}
- fun base64ToBytes(base64: String): ByteArray? =
- try { Base64.decode(base64, Base64.DEFAULT) } catch (e: Exception) { null }
+ fun base64ToBytes(base64: String): ByteArray? {
+ return try {
+ Base64.decode(base64, Base64.DEFAULT)
+ } catch (e: Exception) {
+ null
+ }
+ }
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
index e0b7608f0..f5b8d46de 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
@@ -484,7 +484,7 @@ class VaultStore(
// Throw any error that occurred or return the result
error?.let { throw it }
- return decryptedResult ?: throw IllegalStateException("Decryption failed")
+ return decryptedResult ?: error("Decryption failed")
}
private fun encryptData(data: String): String {
@@ -588,6 +588,10 @@ class VaultStore(
}
}
+ /**
+ * Get all credentials from the vault.
+ * @return The list of credentials
+ */
fun getAllCredentials(): List<Credential> {
if (dbConnection == null) {
throw IllegalStateException("Database not initialized")
@@ -752,6 +756,9 @@ class VaultStore(
return null
}
+ /**
+ * Called when the app enters the background.
+ */
fun onAppBackgrounded() {
Log.d(TAG, "App entered background, starting auto-lock timer with ${getAutoLockTimeout()}s")
if (getAutoLockTimeout() > 0) {
@@ -767,6 +774,9 @@ class VaultStore(
}
}
+ /**
+ * Called when the app enters the foreground.
+ */
fun onAppForegrounded() {
Log.d(TAG, "App entered foreground, canceling auto-lock timer")
// Cancel the auto-lock timer
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/StorageProvider.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/StorageProvider.kt
index b93e2d4ee..af0f8eacc 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/StorageProvider.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/StorageProvider.kt
@@ -38,7 +38,7 @@ interface StorageProvider {
fun getMetadata(): String
/**
- * Set the metadata
+ * Set the metadata.
* @param metadata The metadata as a string
*/
fun setMetadata(metadata: String)
diff --git a/apps/mobile-app/android/build.gradle b/apps/mobile-app/android/build.gradle
index 7f725bdaa..755b32d4e 100644
--- a/apps/mobile-app/android/build.gradle
+++ b/apps/mobile-app/android/build.gradle
@@ -7,6 +7,7 @@ buildscript {
compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '35')
targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '34')
kotlinVersion = findProperty('android.kotlinVersion') ?: '1.9.25'
+ detektVersion = '1.23.5'
ndkVersion = "26.1.10909125"
}
@@ -20,11 +21,26 @@ buildscript {
classpath('com.facebook.react:react-native-gradle-plugin')
classpath('org.jetbrains.kotlin:kotlin-gradle-plugin')
classpath('org.jlleitschuh.gradle:ktlint-gradle:11.6.1')
+ classpath("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detektVersion")
}
}
apply plugin: "com.facebook.react.rootproject"
apply plugin: "org.jlleitschuh.gradle.ktlint"
+apply plugin: "io.gitlab.arturbosch.detekt"
+
+detekt {
+ buildUponDefaultConfig = true
+ config = files("$projectDir/detekt.yml")
+ baseline = file("$projectDir/baseline.xml")
+}
+
+tasks.register('lintAll') {
+ dependsOn 'ktlintCheck'
+ dependsOn 'detekt'
+ group = 'verification'
+ description = 'Run all linting checks'
+}
allprojects {
repositories {
diff --git a/apps/mobile-app/android/detekt.yml b/apps/mobile-app/android/detekt.yml
new file mode 100644
index 000000000..bec790fcb
--- /dev/null
+++ b/apps/mobile-app/android/detekt.yml
@@ -0,0 +1,43 @@
+build:
+ maxIssues: 0
+ weights:
+ complexity: 2
+ formatting: 1
+ LongParameterList: 1
+ comments: 1
+
+complexity:
+ active: true
+ LongParameterList:
+ active: true
+ functionThreshold: 6
+ constructorThreshold: 6
+ TooManyFunctions:
+ active: true
+ thresholdInFiles: 11
+ thresholdInClasses: 11
+ thresholdInInterfaces: 11
+ thresholdInObjects: 11
+
+comments:
+ UndocumentedPublicClass:
+ active: true
+ UndocumentedPublicFunction:
+ active: true
+ UndocumentedPublicProperty:
+ active: true
+ EndOfSentenceFormat:
+ active: true
+ endOfSentenceFormat: ([.?!][ \t\n\r\f<])|([.?!:]$)
+
+style:
+ MaxLineLength:
+ active: true
+ maxLineLength: 160
+ ReturnCount:
+ active: true
+ max: 6
+
+exceptions:
+ TooGenericExceptionCaught:
+ active: false
\ No newline at end of file