Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / ToastDetector.kt

ToastDetector.kt80 lines · 4.0 KiBRaw
1package com.vayunmathur.lint
2
3import com.android.tools.lint.detector.api.Category
4import com.android.tools.lint.detector.api.Detector
5import com.android.tools.lint.detector.api.Implementation
6import com.android.tools.lint.detector.api.Issue
7import com.android.tools.lint.detector.api.JavaContext
8import com.android.tools.lint.detector.api.Scope
9import com.android.tools.lint.detector.api.Severity
10import com.android.tools.lint.detector.api.SourceCodeScanner
11import com.intellij.psi.PsiMethod
12import org.jetbrains.kotlin.psi.KtFile
13import org.jetbrains.uast.UCallExpression
14
15/**
16 * Flags any use of `android.widget.Toast`.
17 *
18 * Toasts are banned here. They render outside the app's Material surface and
19 * ignore its theme, they cannot carry an action so a failure message leaves
20 * the user with nowhere to go, they are invisible to accessibility services in
21 * the way a snackbar is not, and since Android 12 the system silently drops
22 * them when the app is not in the foreground - so the one case people reach
23 * for a Toast in, reporting from background work, is exactly the case where it
24 * may never appear.
25 *
26 * Use instead:
27 * - `rememberMessenger().show(...)` from a composable
28 * - `AppMessages.show(...)` from a ViewModel, worker, or plain Activity
29 * - a notification, if the message must survive the user leaving the screen
30 *
31 * Per-file opt-out: a file carrying `// TOAST EXCEPTION: <reason>` is skipped. This exists for
32 * the case the replacements cannot cover - a UI-less component with no scaffold collecting
33 * `AppMessages`, such as a notification trampoline that finishes immediately.
34 */
35class ToastDetector : Detector(), SourceCodeScanner {
36
37 override fun getApplicableMethodNames(): List<String> = listOf("makeText")
38
39 override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
40 if (!context.evaluator.isMemberInClass(method, TOAST_CLASS)) return
41
42 // Documented per-file opt-out, matching the repo's other escape hatches.
43 // Comment-only: a marker inside a string literal must not opt the file out.
44 val ktFile = context.uastFile?.sourcePsi as? KtFile
45 if (ktFile != null && CommentOnlyMatcher.containsMarkerInComments(ktFile, EXCEPTION_MARKER)) return
46
47 context.report(
48 ISSUE,
49 node,
50 context.getLocation(node),
51 "Toast is not allowed. Use `AppMessages.show(...)`, or " +
52 "`rememberMessenger().show(...)` in a composable. If nothing can collect either " +
53 "here, add the `$EXCEPTION_MARKER <reason>` marker comment to the file.",
54 )
55 }
56
57 companion object {
58 private const val TOAST_CLASS = "android.widget.Toast"
59
60 private const val EXCEPTION_MARKER = "// TOAST EXCEPTION:"
61
62 val ISSUE: Issue = Issue.create(
63 id = "ToastUsage",
64 briefDescription = "Toast is not allowed",
65 explanation = """
66 Toasts render outside the app's Material surface and ignore the app theme, cannot carry an action, and are silently suppressed by the system when the app is backgrounded on Android 12 and above - so the case people reach for a Toast in, reporting from background work, is the case where it may never appear.
67
68 Use AppMessages.show(...) from anywhere without a Context, rememberMessenger().show(...) inside a composable, or a notification when the message has to outlive the current screen.
69
70 If none of those can work - a UI-less component with no scaffold collecting \
71 AppMessages, such as a notification trampoline that finishes immediately - add \
72 the marker comment `// TOAST EXCEPTION: <reason>` to the file to opt it out.
73 """.trimIndent(),
74 category = Category.USABILITY,
75 priority = 7,
76 severity = Severity.ERROR,
77 implementation = Implementation(ToastDetector::class.java, Scope.JAVA_FILE_SCOPE),
78 )
79 }
80}