From d00fb78d59b9d7aa0e1b7fa71fade72daf6da896 Mon Sep 17 00:00:00 2001 From: Anindra Date: Thu, 19 Nov 2026 18:19:04 +0530 Subject: [PATCH] fix: the save toast, the session's names, and a shared-media read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Four things CodeQL found on Develop, three of them real: - Saving an attachment showed nothing. The outcome was worked out into a `message` and then dropped: a save that wrote the file and one that failed looked identical. It is shown now. - `LockSession.lock()`/`unlock()` are a process-wide boolean, not a lock, but the names read as one to any tool that looks for a critical section — CodeQL reported the debug probe's `lock()` as an unreleased lock on a flag that is meant to stay set. Renamed to `clear()`/`grant()`, which is what they do. The probe's intent extras are unchanged. - An unused `colorScheme` in the video player dialog is gone. - The shared-media read uses `IntentCompat` instead of the getters deprecated from API 33, which also removes the `@Suppress` it needed. The rest of the open alerts are API-level compat calls (MediaRecorder's no-arg constructor, CarrierConfig's one-argument getter, SubscriptionInfo's number, bubbles) that have no non-deprecated path at minSdk 29, and three `$stable` fields generated by the Compose compiler shadowing each other. Those need a dismissal or a query filter, not a code change. JUnit: AttachmentSaveFeedbackTest (the save shows its outcome) and a LockSessionTest case holding the session's API to the names that describe it, so the false positive cannot come back. --- .../java/com/anindra/messages/MainActivity.kt | 17 ++++++---- .../anindra/messages/MessagesApplication.kt | 2 +- .../com/anindra/messages/data/LockSession.kt | 10 ++++-- .../com/anindra/messages/ui/ChatScreen.kt | 1 - .../anindra/messages/data/LockSessionTest.kt | 25 ++++++++++++--- .../messages/ui/AttachmentSaveFeedbackTest.kt | 31 +++++++++++++++++++ 6 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 app/src/test/java/com/anindra/messages/ui/AttachmentSaveFeedbackTest.kt diff --git a/app/src/main/java/com/anindra/messages/MainActivity.kt b/app/src/main/java/com/anindra/messages/MainActivity.kt index a896bfd5..14694ef9 100644 --- a/app/src/main/java/com/anindra/messages/MainActivity.kt +++ b/app/src/main/java/com/anindra/messages/MainActivity.kt @@ -847,6 +847,7 @@ class AppViewModel(app: Application) : AndroidViewModel(app) { savedAll -> R.string.chat_image_saved else -> R.string.chat_image_save_failed } + Toast.makeText(app, message, Toast.LENGTH_SHORT).show() } } } @@ -1687,7 +1688,7 @@ open class MainActivity : FragmentActivity() { object : BiometricPrompt.AuthenticationCallback() { override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { result.cryptoObject - com.anindra.messages.data.LockSession.unlock() + com.anindra.messages.data.LockSession.grant() android.util.Log.d("LockSession", "unlocked=true") runOnUiThread { appUnlocked = true } } @@ -2607,11 +2608,11 @@ onOpenAccessibility = { navRoute = "accessibility" }, if (!debuggable) return when (intent.getStringExtra("lock_session_probe")) { "unlock" -> { - com.anindra.messages.data.LockSession.unlock() + com.anindra.messages.data.LockSession.grant() android.util.Log.d("LockSessionProbe", "unlocked=true") } "lock" -> { - com.anindra.messages.data.LockSession.lock() + com.anindra.messages.data.LockSession.clear() android.util.Log.d("LockSessionProbe", "unlocked=false") } "query" -> android.util.Log.d( @@ -2795,12 +2796,16 @@ onOpenAccessibility = { navRoute = "accessibility" }, */ private fun sharedMediaFromIntent(intent: Intent): List? { val shared = when (intent.action) { + // IntentCompat is the version-safe read: the plain getters are + // deprecated from API 33 and the replacement only exists there. Intent.ACTION_SEND -> - @Suppress("DEPRECATION") - intent.getParcelableExtra(Intent.EXTRA_STREAM)?.let(::listOf) + androidx.core.content.IntentCompat + .getParcelableExtra(intent, Intent.EXTRA_STREAM, Uri::class.java) + ?.let(::listOf) ?: intent.clipDataUris() Intent.ACTION_SEND_MULTIPLE -> - intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM) + androidx.core.content.IntentCompat + .getParcelableArrayListExtra(intent, Intent.EXTRA_STREAM, Uri::class.java) ?: intent.clipDataUris() else -> null }?.filter { it.scheme == "content" || it.scheme == "file" } diff --git a/app/src/main/java/com/anindra/messages/MessagesApplication.kt b/app/src/main/java/com/anindra/messages/MessagesApplication.kt index 45a86807..f3193b91 100644 --- a/app/src/main/java/com/anindra/messages/MessagesApplication.kt +++ b/app/src/main/java/com/anindra/messages/MessagesApplication.kt @@ -60,7 +60,7 @@ class MessagesApplication : Application() { object : android.content.BroadcastReceiver() { override fun onReceive(context: android.content.Context?, intent: android.content.Intent?) { if (intent?.action == Intent.ACTION_SCREEN_OFF) { - LockSession.lock() + LockSession.clear() android.util.Log.d("LockSession", "unlocked=false reason=screen_off") } } diff --git a/app/src/main/java/com/anindra/messages/data/LockSession.kt b/app/src/main/java/com/anindra/messages/data/LockSession.kt index dcc85bba..43800147 100644 --- a/app/src/main/java/com/anindra/messages/data/LockSession.kt +++ b/app/src/main/java/com/anindra/messages/data/LockSession.kt @@ -20,12 +20,16 @@ object LockSession { fun isUnlocked(): Boolean = unlocked - fun unlock() { + /** The owner proved it is them: the session holds until the screen goes off. + * Named for the session rather than for a lock — it is a flag, not a + * critical section, and `unlock()` read as one to every tool that looks. */ + fun grant() { unlocked = true } - /** Called when the screen goes off, and by anything that invalidates the session. */ - fun lock() { + /** Called when the screen goes off, and by anything that invalidates the + * session. */ + fun clear() { unlocked = false } } diff --git a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt index 78a5b658..1f047700 100644 --- a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt +++ b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt @@ -3779,7 +3779,6 @@ internal fun VideoPlayerDialog( onDismiss: () -> Unit, onSave: () -> Unit ) { - val cs = MaterialTheme.colorScheme Dialog( onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false) diff --git a/app/src/test/java/com/anindra/messages/data/LockSessionTest.kt b/app/src/test/java/com/anindra/messages/data/LockSessionTest.kt index 32688e62..930e24e0 100644 --- a/app/src/test/java/com/anindra/messages/data/LockSessionTest.kt +++ b/app/src/test/java/com/anindra/messages/data/LockSessionTest.kt @@ -21,11 +21,11 @@ class LockSessionTest { @Test fun theSessionStartsLockedAndEndsOnLock() { - LockSession.lock() + LockSession.clear() assertFalse("a fresh session is unlocked", LockSession.isUnlocked()) - LockSession.unlock() + LockSession.grant() assertTrue("unlock did not stick", LockSession.isUnlocked()) - LockSession.lock() + LockSession.clear() assertFalse("lock did not end the session", LockSession.isUnlocked()) } @@ -38,7 +38,7 @@ class LockSessionTest { ) assertTrue( "a successful prompt does not open the session", - activity.contains("com.anindra.messages.data.LockSession.unlock()") + activity.contains("com.anindra.messages.data.LockSession.grant()") ) } @@ -51,7 +51,22 @@ class LockSessionTest { ) assertTrue( "screen off does not end the unlocked session", - app.contains("LockSession.lock()") + app.contains("LockSession.clear()") ) } + + @Test + fun theSessionIsNotNamedLikeALock() { + // `lock()`/`unlock()` read as a critical section to anything that looks + // for one, and CodeQL reported the probe's lock() as an unreleased lock + // on a flag that is meant to stay set. The names say what they are. + val source = File("app/src/main/java/com/anindra/messages/data/LockSession.kt") + .takeIf { it.exists() } + ?.readText() + ?: File("../app/src/main/java/com/anindra/messages/data/LockSession.kt").readText() + assertTrue(source.contains("fun grant()")) + assertTrue(source.contains("fun clear()")) + assertFalse(source.contains("fun lock()")) + assertFalse(source.contains("fun unlock()")) + } } diff --git a/app/src/test/java/com/anindra/messages/ui/AttachmentSaveFeedbackTest.kt b/app/src/test/java/com/anindra/messages/ui/AttachmentSaveFeedbackTest.kt new file mode 100644 index 00000000..8c49e586 --- /dev/null +++ b/app/src/test/java/com/anindra/messages/ui/AttachmentSaveFeedbackTest.kt @@ -0,0 +1,31 @@ +package com.anindra.messages.ui + +import java.io.File +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * Saving an attachment has to say what happened. + * + * The outcome was worked out into a `message` and then thrown away — no toast + * was ever shown, so a save that wrote the file and one that failed looked + * identical to the user (and, as a warning, to CodeQL). + */ +class AttachmentSaveFeedbackTest { + + private val main: String by lazy { + val app = generateSequence(File("").absoluteFile) { it.parentFile } + .map { File(it, "app") } + .firstOrNull { File(it, "src").isDirectory } + ?: error("app/src not found") + File(app, "src/main/java/com/anindra/messages/MainActivity.kt").readText() + } + + @Test + fun theSaveSaysWhatHappened() { + val save = main.substringAfter("fun saveMessageImages(") + .substringBefore("fun retryMessage(") + assertTrue("the outcome must be shown", save.contains("Toast.makeText(app, message")) + assertTrue("and the outcome must be computed", save.contains("val message = when {")) + } +}