Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions app/src/main/java/com/anindra/messages/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand Down Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -2795,12 +2796,16 @@ onOpenAccessibility = { navRoute = "accessibility" },
*/
private fun sharedMediaFromIntent(intent: Intent): List<Uri>? {
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<Uri>(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<Uri>(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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/com/anindra/messages/data/LockSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/anindra/messages/ui/ChatScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3779,7 +3779,6 @@ internal fun VideoPlayerDialog(
onDismiss: () -> Unit,
onSave: () -> Unit
) {
val cs = MaterialTheme.colorScheme
Dialog(
onDismissRequest = onDismiss,
properties = DialogProperties(usePlatformDefaultWidth = false)
Expand Down
25 changes: 20 additions & 5 deletions app/src/test/java/com/anindra/messages/data/LockSessionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand All @@ -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()")
)
}

Expand All @@ -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()"))
}
}
Original file line number Diff line number Diff line change
@@ -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 {"))
}
}