Modern Apps git

← Modern-Apps.git

files: detekt clean (105 tests)

Commit
2643a38681fb8711a047699e3e840658f47b3db6
Author
Vayun Mathur <[email protected]>
When
2026-11-14 23:15 UTC (21 days ago)
Parents
4275f0d
Fix all 12 detekt findings: real KDoc for MainActivity, Route.Home, FilesActions companion, ZipWorker, UnzipWorker and TextEditorActivity; atomicMoveTo (NestedBlockDepth, ThrowsCount) split into checkMoveTargetAbsent/fallbackMoveTo/copyMoveTo/copySourceTo helpers, behaviour-preserving; resolveTrashableUris per-file query extracted to queryTrashableUri (NestedBlockDepth); unused dir parameter removed from the zipWorker() test helper.

Two narrow @Suppress(FunctionOnlyReturningConstant) added on the Robolectric isExternalStorageManager() shadows in ShadowEnvironmentFix.kt, each with a comment: Robolectric only dispatches through an @Implementation @JvmStatic method matching the framework signature, so declaring a constant as detekt suggests would break the shadow.

Verified: ./gradlew :files:detekt :files:testDevUnitTest :files:lint --max-workers=32 — BUILD SUCCESSFUL, 105 unit tests pass.

Diff

files/src/main/java/com/vayunmathur/files/MainActivity.kt
diff --git a/files/src/main/java/com/vayunmathur/files/MainActivity.kt b/files/src/main/java/com/vayunmathur/files/MainActivity.ktindex 2b28cc29c4..c112a198cf 100644--- a/files/src/main/java/com/vayunmathur/files/MainActivity.kt+++ b/files/src/main/java/com/vayunmathur/files/MainActivity.kt@@ -12,6 +12,11 @@ import com.vayunmathur.files.platform.FilesViewModel import com.vayunmathur.library.log.Log import com.vayunmathur.library.ui.MaActivity +/**+ * Entry activity for the file browser: enables the SAF picker when privileged+ * and routes files shared into the app toward the browser.+ */+ class MainActivity : MaActivity() {     private val viewModel: FilesViewModel by viewModels() 
files/src/main/java/com/vayunmathur/files/Route.kt
diff --git a/files/src/main/java/com/vayunmathur/files/Route.kt b/files/src/main/java/com/vayunmathur/files/Route.ktindex 8fda21a733..73ef33f75a 100644--- a/files/src/main/java/com/vayunmathur/files/Route.kt+++ b/files/src/main/java/com/vayunmathur/files/Route.kt@@ -15,6 +15,7 @@ import kotlinx.serialization.Serializable  */ @Serializable sealed interface Route : NavKey {+    /** The home screen: storage meter, shortcuts, recents, and bookmarks. */     @Serializable     data object Home : Route 
files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.ktindex 7b6f135fb0..85cf267399 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.kt@@ -282,11 +282,7 @@ internal fun sortItemsList(  */ internal fun File.atomicMoveTo(target: File) {     if (canonicalFile == target.canonicalFile) return-    if (target.exists()) {-        throw java.nio.file.FileAlreadyExistsException(-            absolutePath, target.absolutePath, "destination already exists"-        )-    }+    checkMoveTargetAbsent(target)     try {         java.nio.file.Files.move(             this.toPath(),@@ -297,34 +293,54 @@ internal fun File.atomicMoveTo(target: File) {         throw e     } catch (_: IOException) {         // Cross-volume (or non-atomic) move: rename, else copy then delete the source.-        // The copy never overwrites, and the source is only deleted after it succeeds.-        // A failed copy deletes the partial destination before rethrowing, so a-        // retry sees a clean slate instead of "already exists".-        if (target.exists()) {-            throw java.nio.file.FileAlreadyExistsException(-                absolutePath, target.absolutePath, "destination already exists"-            )-        }-        if (!this.renameTo(target)) {-            try {-                if (this.isDirectory) {-                    if (!this.copyRecursively(target, overwrite = false)) {-                        throw IOException("Could not move $absolutePath to ${target.absolutePath}")-                    }-                    this.deleteRecursively()-                } else {-                    this.copyTo(target, overwrite = false)-                    this.delete()-                }-            } catch (e: java.nio.file.FileAlreadyExistsException) {-                // A concurrent creation raced the copy: never delete a file we did not create.-                throw e-            } catch (e: IOException) {-                // A failed copy must not leave a partial destination behind to-                // block a retry with "already exists".-                target.deleteRecursively()-                throw e-            }+        fallbackMoveTo(target)+    }+}++/** Refuses a move whose destination already exists, leaving both files untouched. */+private fun File.checkMoveTargetAbsent(target: File) {+    if (target.exists()) {+        throw java.nio.file.FileAlreadyExistsException(+            absolutePath, target.absolutePath, "destination already exists"+        )+    }+}++/**+ * Rename-or-copy fallback for a move `ATOMIC_MOVE` could not do. The copy never+ * overwrites, and the source is only deleted after it succeeds.+ */+private fun File.fallbackMoveTo(target: File) {+    // A concurrent creation may have landed while the atomic move was tried.+    checkMoveTargetAbsent(target)+    if (!renameTo(target)) {+        copyMoveTo(target)+    }+}++/** Copies this file or directory tree to [target], then deletes the source. */+private fun File.copyMoveTo(target: File) {+    try {+        copySourceTo(target)+    } catch (e: java.nio.file.FileAlreadyExistsException) {+        // A concurrent creation raced the copy: never delete a file we did not create.+        throw e+    } catch (e: IOException) {+        // A failed copy must not leave a partial destination behind to+        // block a retry with "already exists".+        target.deleteRecursively()+        throw e+    }+}++private fun File.copySourceTo(target: File) {+    if (isDirectory) {+        if (!copyRecursively(target, overwrite = false)) {+            throw IOException("Could not move $absolutePath to ${target.absolutePath}")         }+        deleteRecursively()+    } else {+        copyTo(target, overwrite = false)+        delete()     } }
files/src/main/java/com/vayunmathur/files/platform/FilesTrash.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesTrash.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesTrash.ktindex 4698bc9d4c..3e9d0ecec9 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesTrash.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesTrash.kt@@ -28,24 +28,27 @@ internal fun resolveTrashableUris(context: Context, files: List<File>): Map<File     val out = mutableMapOf<File, Uri>()     for (file in files) {         if (file.isDirectory) continue-        try {-            context.contentResolver.query(-                collection,-                arrayOf(MediaStore.MediaColumns._ID, MediaStore.Files.FileColumns.MEDIA_TYPE),-                "${MediaStore.MediaColumns.DATA}=?",-                arrayOf(file.absolutePath),-                null,-            )?.use { c ->-                if (c.moveToFirst()) {-                    mediaItemUri(c.getInt(1), c.getLong(0))?.let { out[file] = it }-                }-            }-        } catch (_: IllegalArgumentException) {-        }+        queryTrashableUri(context, collection, file)?.let { out[file] = it }     }     return out } +/** The MediaStore Uri for one file, or null when it is unindexed or not a media item. */+private fun queryTrashableUri(context: Context, collection: Uri, file: File): Uri? =+    try {+        context.contentResolver.query(+            collection,+            arrayOf(MediaStore.MediaColumns._ID, MediaStore.Files.FileColumns.MEDIA_TYPE),+            "${MediaStore.MediaColumns.DATA}=?",+            arrayOf(file.absolutePath),+            null,+        )?.use { c ->+            if (c.moveToFirst()) mediaItemUri(c.getInt(1), c.getLong(0)) else null+        }+    } catch (_: IllegalArgumentException) {+        null+    }+ /**  * The per-collection Uri MediaStore's trash/delete requests require. They reject generic  * `Files` Uris ("All requested items must be Media items"), so non-media files get `null`.
files/src/main/java/com/vayunmathur/files/platform/FilesUiContract.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesUiContract.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesUiContract.ktindex c4abd3f8d7..21c67847b4 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesUiContract.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesUiContract.kt@@ -192,6 +192,7 @@ interface FilesActions :     HomeActions,     TrashActions,     FeedbackActions {+    /** Shared no-op actions, so previews can render the browser without behaviour. */     companion object {         val Noop: FilesActions = object : FilesActions {}     }
files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.kt b/files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.ktindex 78292c2e29..179135d9bc 100644--- a/files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/UnzipWorker.kt@@ -15,6 +15,7 @@ import java.util.zip.ZipInputStream  private const val TAG_UNZIP = "UnzipWorker" +/** Background archive extraction with a byte budget, zip-slip checks, and all-or-nothing cleanup of partial output. */ class UnzipWorker(context: Context, params: WorkerParameters) : ProgressNotificationWorker(     context,     params,
files/src/main/java/com/vayunmathur/files/platform/ZipWorker.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/ZipWorker.kt b/files/src/main/java/com/vayunmathur/files/platform/ZipWorker.ktindex 928ab473e7..7251534bcc 100644--- a/files/src/main/java/com/vayunmathur/files/platform/ZipWorker.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/ZipWorker.kt@@ -15,6 +15,11 @@ import java.util.zip.ZipOutputStream  private const val TAG_ZIP = "ZipWorker" +/**+ * Background archive creation that refuses to overwrite or archive its own+ * destination and removes partial output on failure.+ */+ class ZipWorker(context: Context, params: WorkerParameters) : ProgressNotificationWorker(     context,     params,
files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.kt
diff --git a/files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.kt b/files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.ktindex 42f3680b85..d243535446 100644--- a/files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.kt+++ b/files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.kt@@ -37,6 +37,7 @@ import com.vayunmathur.library.ui.IconSave import com.vayunmathur.library.ui.IconVisible import com.vayunmathur.library.ui.appBarScrollBehavior +/** Built-in text editor for a single file Uri, with view/edit toggling and save-on-exit from edit mode. */ class TextEditorActivity : ComponentActivity() {     private val viewModel: TextEditorViewModel by viewModels() 
files/src/test/java/com/vayunmathur/files/platform/ShadowEnvironmentFix.kt
diff --git a/files/src/test/java/com/vayunmathur/files/platform/ShadowEnvironmentFix.kt b/files/src/test/java/com/vayunmathur/files/platform/ShadowEnvironmentFix.ktindex 979f0a3795..34b9c57d1c 100644--- a/files/src/test/java/com/vayunmathur/files/platform/ShadowEnvironmentFix.kt+++ b/files/src/test/java/com/vayunmathur/files/platform/ShadowEnvironmentFix.kt@@ -13,6 +13,9 @@ import org.robolectric.shadows.ShadowEnvironment @Implements(android.os.Environment::class) class ShadowEnvironmentFix : ShadowEnvironment() {     companion object {+        // Robolectric dispatches to a shadow only through an @Implementation method matching+        // the framework signature, so this cannot be a constant property as detekt suggests.+        @Suppress("FunctionOnlyReturningConstant")         @Implementation         @JvmStatic         fun isExternalStorageManager(): Boolean = true@@ -23,6 +26,9 @@ class ShadowEnvironmentFix : ShadowEnvironment() { @Implements(android.os.Environment::class) class ShadowEnvironmentDeniedFix : ShadowEnvironment() {     companion object {+        // Robolectric dispatches to a shadow only through an @Implementation method matching+        // the framework signature, so this cannot be a constant property as detekt suggests.+        @Suppress("FunctionOnlyReturningConstant")         @Implementation         @JvmStatic         fun isExternalStorageManager(): Boolean = false
files/src/test/java/com/vayunmathur/files/platform/WorkersAndStorageTest.kt
diff --git a/files/src/test/java/com/vayunmathur/files/platform/WorkersAndStorageTest.kt b/files/src/test/java/com/vayunmathur/files/platform/WorkersAndStorageTest.ktindex 5f70e00ef9..9a58c10761 100644--- a/files/src/test/java/com/vayunmathur/files/platform/WorkersAndStorageTest.kt+++ b/files/src/test/java/com/vayunmathur/files/platform/WorkersAndStorageTest.kt@@ -54,7 +54,7 @@ class WorkersAndStorageTest {         dir.deleteRecursively()     } } -    private fun zipWorker(dir: File, sources: Array<String>, dest: File) =+    private fun zipWorker(sources: Array<String>, dest: File) =         TestListenableWorkerBuilder<ZipWorker>(ctx)             .setInputData(                 workDataOf(@@ -71,14 +71,14 @@ class WorkersAndStorageTest {         val existing = File(dir, "out.zip").apply { writeText("keep me") }         assertEquals(             ListenableWorker.Result.failure(),-            zipWorker(dir, arrayOf(srcFile.absolutePath), existing).doWork(),+            zipWorker(arrayOf(srcFile.absolutePath), existing).doWork(),         )         assertEquals("keep me", existing.readText())         // the archive must never be one of its own sources (it would be         // truncated before being read)         assertEquals(             ListenableWorker.Result.failure(),-            zipWorker(dir, arrayOf(srcFile.absolutePath), srcFile).doWork(),+            zipWorker(arrayOf(srcFile.absolutePath), srcFile).doWork(),         )         assertEquals("alpha", srcFile.readText())         dir.deleteRecursively()@@ -92,7 +92,7 @@ class WorkersAndStorageTest {         // the same source twice makes ZipOutputStream reject the duplicate entry         assertEquals(             ListenableWorker.Result.failure(),-            zipWorker(dir, arrayOf(srcFile.absolutePath, srcFile.absolutePath), dest).doWork(),+            zipWorker(arrayOf(srcFile.absolutePath, srcFile.absolutePath), dest).doWork(),         )         assertFalse(dest.exists())         dir.deleteRecursively()