android-skills 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -58
- package/android-ui/SKILL.md +187 -0
- package/android-ui/references/anti-slop-checklist.md +61 -0
- package/android-ui/references/checklist.md +95 -0
- package/android-ui/references/clarifying-questions.md +55 -0
- package/android-ui/references/color-and-contrast.md +104 -0
- package/android-ui/references/components-intents.md +74 -0
- package/android-ui/references/compose-patterns.md +120 -0
- package/android-ui/references/compose-risks.md +86 -0
- package/android-ui/references/input-and-state.md +77 -0
- package/android-ui/references/network-storage.md +87 -0
- package/android-ui/references/reconstruction-workflow.md +74 -0
- package/android-ui/references/report-template.md +71 -0
- package/android-ui/references/sensitive-data-ui.md +73 -0
- package/android-ui/references/typography-spacing.md +93 -0
- package/android-ui/references/webview.md +105 -0
- package/{android-ui-minimal → android-ui}/scripts/contrast.py +8 -8
- package/{android-ui-minimal → android-ui}/scripts/gen_tokens.py +11 -11
- package/{android-ui-minimal → android-ui}/scripts/lint_ui.py +29 -29
- package/{android-ui-security-review → android-ui}/scripts/scan.py +146 -146
- package/bin/android-skills.js +10 -10
- package/install.py +79 -65
- package/opencode/commands/ui.md +13 -0
- package/package.json +3 -4
- package/android-ui-minimal/SKILL.md +0 -98
- package/android-ui-minimal/references/anti-slop-checklist.md +0 -61
- package/android-ui-minimal/references/clarifying-questions.md +0 -55
- package/android-ui-minimal/references/color-and-contrast.md +0 -104
- package/android-ui-minimal/references/compose-patterns.md +0 -120
- package/android-ui-minimal/references/reconstruction-workflow.md +0 -74
- package/android-ui-minimal/references/typography-spacing.md +0 -93
- package/android-ui-security-review/SKILL.md +0 -93
- package/android-ui-security-review/references/checklist.md +0 -95
- package/android-ui-security-review/references/components-intents.md +0 -74
- package/android-ui-security-review/references/compose-risks.md +0 -86
- package/android-ui-security-review/references/input-and-state.md +0 -77
- package/android-ui-security-review/references/network-storage.md +0 -87
- package/android-ui-security-review/references/report-template.md +0 -71
- package/android-ui-security-review/references/sensitive-data-ui.md +0 -73
- package/android-ui-security-review/references/webview.md +0 -105
- package/opencode/commands/ui-minimal.md +0 -9
- package/opencode/commands/ui-security.md +0 -9
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Report template
|
|
2
|
+
|
|
3
|
+
Use this format. Short, specific, actionable. No emoji.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
# UI security review report: <app name>
|
|
7
|
+
|
|
8
|
+
Date: <date>
|
|
9
|
+
Scope: <modules, variant, targetSdk>
|
|
10
|
+
Method: automated scan (scan.py) + manual review of <area>
|
|
11
|
+
Not checked: <backend, native code, third-party SDKs, etc.>
|
|
12
|
+
|
|
13
|
+
## Summary
|
|
14
|
+
<2–4 sentences: number of findings per level, the most urgent item>
|
|
15
|
+
|
|
16
|
+
| Level | Count | Confirmed | False positive |
|
|
17
|
+
|---|---|---|---|
|
|
18
|
+
|
|
19
|
+
## Findings
|
|
20
|
+
|
|
21
|
+
### [HIGH] W-INTERFACE: addJavascriptInterface on a WebView that loads external URLs
|
|
22
|
+
- Location: app/src/main/java/.../HelpScreen.kt:84
|
|
23
|
+
- Status: confirmed | possible | false positive
|
|
24
|
+
- Impact: a compromised page could call `bridge.*` methods and read <what>
|
|
25
|
+
- Scenario: <minimal, conceptual steps, on your own app>
|
|
26
|
+
- Fix: <concrete change + short code example>
|
|
27
|
+
- Notes: <side effects, API level>
|
|
28
|
+
|
|
29
|
+
(repeat per finding, ordered critical to info)
|
|
30
|
+
|
|
31
|
+
## What is already good
|
|
32
|
+
<short, evidence-based: e.g. "every exported component has a permission">
|
|
33
|
+
|
|
34
|
+
## Further recommendations
|
|
35
|
+
<out of scope: pentest, backend review, dependencies>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Rating severity
|
|
39
|
+
|
|
40
|
+
Rate with two questions:
|
|
41
|
+
|
|
42
|
+
1. **Exploitability**
|
|
43
|
+
- Easy: another app on the device or a web page can trigger it without special interaction
|
|
44
|
+
- Moderate: needs user interaction or a specific condition
|
|
45
|
+
- Hard: needs physical access, root, or a chain of weaknesses
|
|
46
|
+
2. **Impact**
|
|
47
|
+
- High: credential leak, code execution, account takeover, fund loss
|
|
48
|
+
- Moderate: limited data leak, feature bypass
|
|
49
|
+
- Low: technical information, hardening
|
|
50
|
+
|
|
51
|
+
| Impact \ Ease | Easy | Moderate | Hard |
|
|
52
|
+
|---|---|---|---|
|
|
53
|
+
| High | critical | high | medium |
|
|
54
|
+
| Moderate | high | medium | low |
|
|
55
|
+
| Low | medium | low | info |
|
|
56
|
+
|
|
57
|
+
Adjustments:
|
|
58
|
+
- Debug source set only: downgrade one level and note whether it can leak into release.
|
|
59
|
+
- App processes financial, health, or identity data: upgrade one level for data leaks.
|
|
60
|
+
- App with no user data and no accounts: downgrade one level for hardening.
|
|
61
|
+
- Low confidence (possible false positive): still report it as "possible" and explain what to check.
|
|
62
|
+
|
|
63
|
+
## Handling secrets
|
|
64
|
+
|
|
65
|
+
If you find a secret, write only the first four characters and its type in the report ("AIza****", Google API key). Recommend rotation before other fixes.
|
|
66
|
+
|
|
67
|
+
## After fixing
|
|
68
|
+
|
|
69
|
+
- Re-run `scan.py` and compare finding counts.
|
|
70
|
+
- Run the release build and test the touched flows.
|
|
71
|
+
- Note findings deliberately suppressed with `sec-ignore`, with the reason.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Sensitive data in the UI layer
|
|
2
|
+
|
|
3
|
+
Sensitive data: passwords, PINs, OTPs, tokens, card numbers, health data, identity, private messages.
|
|
4
|
+
|
|
5
|
+
## Screenshots and recents
|
|
6
|
+
|
|
7
|
+
```kotlin
|
|
8
|
+
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
- Set it on login, OTP, PIN, payment, and any screen showing personal data.
|
|
12
|
+
- Set it in `onCreate` before `setContent`. Clear it on non-sensitive screens if the flag is set on a shared Activity.
|
|
13
|
+
- FLAG_SECURE also hides content from the recents preview and screen recording.
|
|
14
|
+
- On Android 13+, `setRecentsScreenshotEnabled(false)` hides the recents preview without blocking screenshots.
|
|
15
|
+
|
|
16
|
+
## Clipboard
|
|
17
|
+
|
|
18
|
+
- Do not automatically copy passwords, tokens, or OTPs.
|
|
19
|
+
- If there is a "copy" button for sensitive data:
|
|
20
|
+
```kotlin
|
|
21
|
+
val clip = ClipData.newPlainText("label", value).apply {
|
|
22
|
+
description.extras = PersistableBundle().apply {
|
|
23
|
+
putBoolean("android.content.extra.IS_SENSITIVE", true)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
And clear the clipboard after a short delay.
|
|
28
|
+
- Do not read the clipboard without a user action.
|
|
29
|
+
|
|
30
|
+
## Input fields
|
|
31
|
+
|
|
32
|
+
- Password: `PasswordVisualTransformation`, `KeyboardType.Password`, `autoCorrect = false`.
|
|
33
|
+
- OTP and PIN: `KeyboardType.NumberPassword`.
|
|
34
|
+
- Disable keyboard suggestions for secret fields (`autoCorrectEnabled = false`).
|
|
35
|
+
- XML: `android:inputType="textPassword"`, `android:importantForAutofill` as needed.
|
|
36
|
+
- Autofill: allow legitimate password managers, but disable it for one-time fields that do not need it (`importantForAutofill="no"`).
|
|
37
|
+
|
|
38
|
+
## State and processes
|
|
39
|
+
|
|
40
|
+
- Do not store secrets in `rememberSaveable`, `SavedStateHandle`, or `onSaveInstanceState`. Bundles survive and can be read elsewhere.
|
|
41
|
+
- Clear sensitive ViewModel state on logout and when the screen is dismissed.
|
|
42
|
+
- A `Parcelable` with tokens passed between Activities can appear in logs or dumps. Pass an ID, not the secret value.
|
|
43
|
+
|
|
44
|
+
## Logs
|
|
45
|
+
|
|
46
|
+
- Never log tokens, passwords, OTPs, or full response bodies.
|
|
47
|
+
- Use a logger that is a no-op in release (the Timber tree is planted in debug only).
|
|
48
|
+
- Remove `println` and `Log.*` from the authentication path.
|
|
49
|
+
- R8 can remove `Log.d/v` with `-assumenosideeffects`, but do not make that your only defense.
|
|
50
|
+
|
|
51
|
+
## Notifications
|
|
52
|
+
|
|
53
|
+
- Do not show sensitive content on the lock screen: `setVisibility(NotificationCompat.VISIBILITY_PRIVATE)` with `setPublicVersion`.
|
|
54
|
+
- Do not embed tokens in notification PendingIntent extras.
|
|
55
|
+
|
|
56
|
+
## Errors and messages
|
|
57
|
+
|
|
58
|
+
- User-facing error messages are generic. Technical details only in debug logs.
|
|
59
|
+
- Login: the same message for "account does not exist" and "wrong password".
|
|
60
|
+
|
|
61
|
+
## Images and display
|
|
62
|
+
|
|
63
|
+
- Image caches (Coil, Glide) can keep sensitive images on disk. Disable disk caching for identity-document images, or clear them on logout.
|
|
64
|
+
- Do not show full sensitive data if not needed: mask card numbers, show only the last 4 digits.
|
|
65
|
+
|
|
66
|
+
## Biometrics
|
|
67
|
+
|
|
68
|
+
- Use `BiometricPrompt` with a `CryptoObject` for actions that guard a key, not just a "success" boolean.
|
|
69
|
+
- Do not store an "authenticated" status in a variable that can be tampered on rooted devices without server-side verification.
|
|
70
|
+
|
|
71
|
+
## Logout
|
|
72
|
+
|
|
73
|
+
Cleanup checklist: tokens, ViewModels, disk and memory caches, WebView cookies, `WebStorage`, clipboard, notifications, local databases with user data, preferences.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Typography, spacing, radius, elevation
|
|
2
|
+
|
|
3
|
+
## Spacing (multiples of 4dp)
|
|
4
|
+
|
|
5
|
+
| Token | Value | Use |
|
|
6
|
+
|---|---|---|
|
|
7
|
+
| `xs` | 4dp | Icon-to-label gap, tight line-to-line text |
|
|
8
|
+
| `sm` | 8dp | Gap inside a component |
|
|
9
|
+
| `md` | 12dp | Vertical padding of list rows |
|
|
10
|
+
| `lg` | 16dp | Screen margins, gap between small groups |
|
|
11
|
+
| `xl` | 24dp | Gap between sections |
|
|
12
|
+
| `xxl` | 32dp | Big section gaps, top space under a header |
|
|
13
|
+
| `touch` | 48dp | Minimum touch-target height |
|
|
14
|
+
|
|
15
|
+
Screen horizontal margins: 16dp on phones, 24dp on small tablets. Consistent across the app.
|
|
16
|
+
|
|
17
|
+
```kotlin
|
|
18
|
+
object Space {
|
|
19
|
+
val xs = 4.dp; val sm = 8.dp; val md = 12.dp
|
|
20
|
+
val lg = 16.dp; val xl = 24.dp; val xxl = 32.dp
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Rhythm rule: space inside a group is smaller than space between groups. If every gap is equal, the hierarchy is lost.
|
|
25
|
+
|
|
26
|
+
## Typography
|
|
27
|
+
|
|
28
|
+
Use the Material 3 scale through `MaterialTheme.typography`. Do not invent new sizes in screens.
|
|
29
|
+
|
|
30
|
+
| Role | Style | Notes |
|
|
31
|
+
|---|---|---|
|
|
32
|
+
| Screen title | `titleLarge` | One per screen |
|
|
33
|
+
| Section heading | `titleMedium` | Medium weight |
|
|
34
|
+
| Body | `bodyLarge` / `bodyMedium` | Regular weight |
|
|
35
|
+
| Description | `bodySmall` | `onSurfaceVariant` |
|
|
36
|
+
| Button | `labelLarge` | |
|
|
37
|
+
|
|
38
|
+
Rules:
|
|
39
|
+
- At most two weights per screen (regular and medium, or regular and semibold).
|
|
40
|
+
- No all-caps for long text. Short titles only, and avoid aggressive `letterSpacing`.
|
|
41
|
+
- Comfortable line length is 40–75 characters. On tablets, cap content width (`widthIn(max = 640.dp)`).
|
|
42
|
+
- Line height 1.4–1.6 for body.
|
|
43
|
+
- Font: the system default is good. At most one custom font family, loaded via a font resource, not from an unverified URL.
|
|
44
|
+
- Tabular figures (`fontFeatureSettings = "tnum"`) for numeric columns so they align.
|
|
45
|
+
|
|
46
|
+
## Radius
|
|
47
|
+
|
|
48
|
+
Pick one scale and use it consistently:
|
|
49
|
+
|
|
50
|
+
| Token | Value | Use |
|
|
51
|
+
|---|---|---|
|
|
52
|
+
| `small` | 8dp | Chips, fields |
|
|
53
|
+
| `medium` | 12dp | Cards, small dialogs |
|
|
54
|
+
| `large` | 16dp | Bottom sheets |
|
|
55
|
+
|
|
56
|
+
Do not use a uniform large radius (24dp+) on every element. It reads quickly as a template.
|
|
57
|
+
|
|
58
|
+
## Elevation and boundaries
|
|
59
|
+
|
|
60
|
+
Preference order for separating elements:
|
|
61
|
+
1. Space
|
|
62
|
+
2. Tonal difference (`surfaceContainer`)
|
|
63
|
+
3. A 1dp `outlineVariant` line
|
|
64
|
+
4. Shadow (only for elements that truly float: FAB, menus, dialogs)
|
|
65
|
+
|
|
66
|
+
No stacked shadows, no glows, no thick colored borders.
|
|
67
|
+
|
|
68
|
+
## Icons
|
|
69
|
+
|
|
70
|
+
- One set (Material Symbols or `Icons.Outlined`). Do not mix filled and outlined styles at random.
|
|
71
|
+
- 24dp standard, 20dp for inline with text.
|
|
72
|
+
- Unlabeled icons only for very common actions (back, close, search).
|
|
73
|
+
|
|
74
|
+
## Lists
|
|
75
|
+
|
|
76
|
+
- Plain rows with a thin divider or space are enough for homogeneous lists.
|
|
77
|
+
- Cards only when each item has several parts and its own action.
|
|
78
|
+
- A leading visual (avatar, icon) only when it identifies the item. Not decoration.
|
|
79
|
+
- Use a stable `key` in `LazyColumn`.
|
|
80
|
+
|
|
81
|
+
## Forms
|
|
82
|
+
|
|
83
|
+
- Labels always visible (not just a placeholder).
|
|
84
|
+
- Error messages below the field, clear, with text.
|
|
85
|
+
- One primary button. Secondary actions as text buttons.
|
|
86
|
+
- Set `KeyboardOptions` to match the content (email, number, password) and `imeAction`.
|
|
87
|
+
|
|
88
|
+
## Empty, loading, error
|
|
89
|
+
|
|
90
|
+
All three states are required for every data screen:
|
|
91
|
+
- Empty: one explanatory sentence + one action if relevant. No decorative illustration.
|
|
92
|
+
- Loading: a simple indicator or a neutral skeleton. Do not block the whole screen if partial data exists.
|
|
93
|
+
- Error: what failed and what can be done (retry).
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Safe WebView
|
|
2
|
+
|
|
3
|
+
## Principles
|
|
4
|
+
|
|
5
|
+
Treat all web content as untrusted unless it comes from your app's own assets. The more capabilities you give the WebView, the bigger the blast radius of one XSS.
|
|
6
|
+
|
|
7
|
+
## Base configuration
|
|
8
|
+
|
|
9
|
+
```kotlin
|
|
10
|
+
fun WebView.hardened() {
|
|
11
|
+
settings.apply {
|
|
12
|
+
javaScriptEnabled = false // enable only if needed
|
|
13
|
+
allowFileAccess = false
|
|
14
|
+
allowContentAccess = false
|
|
15
|
+
allowFileAccessFromFileURLs = false
|
|
16
|
+
allowUniversalAccessFromFileURLs = false
|
|
17
|
+
mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW
|
|
18
|
+
setSupportMultipleWindows(false)
|
|
19
|
+
safeBrowsingEnabled = true
|
|
20
|
+
domStorageEnabled = false // enable only if needed
|
|
21
|
+
javaScriptCanOpenWindowsAutomatically = false
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Load local content with WebViewAssetLoader
|
|
27
|
+
|
|
28
|
+
```kotlin
|
|
29
|
+
val loader = WebViewAssetLoader.Builder()
|
|
30
|
+
.addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(context))
|
|
31
|
+
.build()
|
|
32
|
+
|
|
33
|
+
webView.webViewClient = object : WebViewClient() {
|
|
34
|
+
override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest) =
|
|
35
|
+
loader.shouldInterceptRequest(request.url)
|
|
36
|
+
}
|
|
37
|
+
webView.loadUrl("https://appassets.androidplatform.net/assets/index.html")
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Avoid `file:///android_asset/` for content that uses JavaScript.
|
|
41
|
+
|
|
42
|
+
## Navigation allowlist
|
|
43
|
+
|
|
44
|
+
```kotlin
|
|
45
|
+
private val allowedHosts = setOf("example.com", "appassets.androidplatform.net")
|
|
46
|
+
|
|
47
|
+
override fun shouldOverrideUrlLoading(view: WebView, req: WebResourceRequest): Boolean {
|
|
48
|
+
val uri = req.url
|
|
49
|
+
val ok = uri.scheme == "https" && uri.host in allowedHosts
|
|
50
|
+
if (!ok) { /* open in an external browser or refuse */ }
|
|
51
|
+
return !ok
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Check hosts with full matching. Never `host.endsWith("example.com")`, because `evilexample.com` passes. Use `host == d || host.endsWith(".$d")`.
|
|
56
|
+
|
|
57
|
+
## JS-to-Kotlin bridge
|
|
58
|
+
|
|
59
|
+
Safer option than `addJavascriptInterface`:
|
|
60
|
+
|
|
61
|
+
```kotlin
|
|
62
|
+
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
|
|
63
|
+
WebViewCompat.addWebMessageListener(
|
|
64
|
+
webView, "bridge", setOf("https://appassets.androidplatform.net"),
|
|
65
|
+
) { _, message, origin, _, reply -> /* validate message.data */ }
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
If you must keep `addJavascriptInterface`:
|
|
70
|
+
- Only for local content you control.
|
|
71
|
+
- Methods annotated with `@JavascriptInterface`, as few as possible.
|
|
72
|
+
- Validate every argument as untrusted input.
|
|
73
|
+
- Do not expose sensitive capabilities (files, credentials, intents).
|
|
74
|
+
|
|
75
|
+
## SSL errors
|
|
76
|
+
|
|
77
|
+
```kotlin
|
|
78
|
+
override fun onReceivedSslError(v: WebView, h: SslErrorHandler, e: SslError) {
|
|
79
|
+
h.cancel() // never proceed()
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Cookies and storage
|
|
84
|
+
|
|
85
|
+
- Clear cookies, cache, and `WebStorage` on logout.
|
|
86
|
+
- Do not keep tokens in the WebView's `localStorage` if avoidable.
|
|
87
|
+
- `CookieManager.setAcceptThirdPartyCookies(webView, false)`.
|
|
88
|
+
|
|
89
|
+
## Downloads and permissions
|
|
90
|
+
|
|
91
|
+
- Handle `setDownloadListener` with type and source validation.
|
|
92
|
+
- Refuse `onPermissionRequest` (camera, microphone) except from trusted origins.
|
|
93
|
+
- Refuse `onShowFileChooser` from untrusted origins.
|
|
94
|
+
|
|
95
|
+
## Compose
|
|
96
|
+
|
|
97
|
+
An `AndroidView` wrapping a WebView inherits all risks above. Create the WebView in `factory`, release it in `onRelease` (`destroy()`), and do not hold references to the Activity.
|
|
98
|
+
|
|
99
|
+
## Anti-patterns
|
|
100
|
+
|
|
101
|
+
- `loadUrl(intent.data.toString())` without validation.
|
|
102
|
+
- `evaluateJavascript("setName('$name')")` with a user-supplied `name`.
|
|
103
|
+
- `setJavaScriptEnabled(true)` + `addJavascriptInterface` + loading external URLs.
|
|
104
|
+
- Ignoring `onReceivedSslError` so "the test works".
|
|
105
|
+
- WebView debugging enabled in release.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""WCAG 2.2 contrast calculator.
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
Usage:
|
|
5
5
|
python3 contrast.py <fg> <bg> [<fg2> <bg2> ...]
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
Colors: #RGB, #RRGGBB, 0xFFRRGGBB, or AARRGGBB (Android order).
|
|
8
|
+
If fg has an alpha, it is blended over bg before the calculation.
|
|
9
|
+
Exit code 1 if any pair fails AA for normal text (4.5:1).
|
|
10
10
|
"""
|
|
11
11
|
import sys
|
|
12
12
|
|
|
@@ -20,7 +20,7 @@ def parse(s):
|
|
|
20
20
|
if len(h) == 6:
|
|
21
21
|
h = "FF" + h
|
|
22
22
|
if len(h) != 8:
|
|
23
|
-
raise ValueError(f"
|
|
23
|
+
raise ValueError(f"unknown color format: {s}")
|
|
24
24
|
a = int(h[0:2], 16) / 255
|
|
25
25
|
return a, tuple(int(h[i:i + 2], 16) for i in (2, 4, 6))
|
|
26
26
|
|
|
@@ -59,8 +59,8 @@ def main(argv):
|
|
|
59
59
|
aaa = r >= 7.0
|
|
60
60
|
failed |= not aa
|
|
61
61
|
print(f"{argv[i]:>12} on {argv[i + 1]:<12} {r:5.2f}:1 "
|
|
62
|
-
f"
|
|
63
|
-
f"
|
|
62
|
+
f"normal text AA {'pass' if aa else 'FAIL'} | "
|
|
63
|
+
f"large text/component {'pass' if large else 'FAIL'} | AAA {'pass' if aaa else 'no'}")
|
|
64
64
|
return 1 if failed else 0
|
|
65
65
|
|
|
66
66
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""Color token generator from one accent color.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
Produces Color.kt and Theme.kt (Compose, Material 3) with light and dark
|
|
5
|
+
neutrals slightly tinted toward the accent, then verifies contrast.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
python3 gen_tokens.py "#3B5BA9" [--package com.
|
|
7
|
+
Usage:
|
|
8
|
+
python3 gen_tokens.py "#3B5BA9" [--package com.example.app] [--out app/src/main/java/com/example/app/ui/theme]
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Without --out, the result is printed to stdout.
|
|
11
11
|
"""
|
|
12
12
|
import argparse
|
|
13
13
|
import colorsys
|
|
@@ -77,13 +77,13 @@ def checks(light, dark):
|
|
|
77
77
|
("onSurfaceVariant", "background"), ("onSurfaceVariant", "surface"),
|
|
78
78
|
("onPrimary", "primary"), ("primary", "background"), ("error", "background")]
|
|
79
79
|
rows, ok = [], True
|
|
80
|
-
for name, scheme in (("
|
|
80
|
+
for name, scheme in (("light", light), ("dark", dark)):
|
|
81
81
|
for fg, bg in pairs:
|
|
82
82
|
r = ratio(scheme[fg], scheme[bg])
|
|
83
83
|
need = 3.0 if fg == "primary" else 4.5
|
|
84
84
|
good = r >= need
|
|
85
85
|
ok &= good
|
|
86
|
-
rows.append(f"{name:6} {fg:17} / {bg:11} {r:5.2f}:1 {'ok' if good else '
|
|
86
|
+
rows.append(f"{name:6} {fg:17} / {bg:11} {r:5.2f}:1 {'ok' if good else 'FAIL (needs ' + str(need) + ')'}")
|
|
87
87
|
return rows, ok
|
|
88
88
|
|
|
89
89
|
|
|
@@ -150,14 +150,14 @@ def main():
|
|
|
150
150
|
for name, body in (("Color.kt", ck), ("Theme.kt", th)):
|
|
151
151
|
with open(os.path.join(a.out, name), "w", encoding="utf-8") as fh:
|
|
152
152
|
fh.write(body)
|
|
153
|
-
print(f"
|
|
153
|
+
print(f"Written: {a.out}/Color.kt, Theme.kt")
|
|
154
154
|
else:
|
|
155
155
|
print("// ===== Color.kt =====\n" + ck + "\n// ===== Theme.kt =====\n" + th)
|
|
156
156
|
|
|
157
|
-
print("\
|
|
157
|
+
print("\nContrast verification:", file=sys.stderr)
|
|
158
158
|
print("\n".join(rows), file=sys.stderr)
|
|
159
159
|
if not ok:
|
|
160
|
-
print("\
|
|
160
|
+
print("\nSome pairs fail. Adjust the accent or the neutrals.", file=sys.stderr)
|
|
161
161
|
sys.exit(0 if ok else 1)
|
|
162
162
|
|
|
163
163
|
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""UI style linter for Android projects (Kotlin Compose and XML Views).
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
emoji,
|
|
6
|
-
radius
|
|
4
|
+
Checks violations of the android-ui skill rules (UI mode):
|
|
5
|
+
emoji, hardcoded colors, gradients, ad-hoc font sizes, spacing off the 4dp
|
|
6
|
+
scale, large radius, heavy shadows, empty contentDescription, hardcoded text.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
Usage:
|
|
9
9
|
python3 lint_ui.py <path> [--json] [--strict] [--include-tests]
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
Exit code: 1 if there are errors (or warns with --strict), else 0.
|
|
12
|
+
Suppress a rule on a line with a comment: // ui-ignore: RULE_ID
|
|
13
13
|
"""
|
|
14
14
|
import argparse
|
|
15
15
|
import json
|
|
@@ -64,42 +64,42 @@ def lint_kotlin(path, lines, out):
|
|
|
64
64
|
is_comment = s.startswith("//") or s.startswith("*") or s.startswith("/*")
|
|
65
65
|
if not theme and not is_comment:
|
|
66
66
|
if HEX_KT.search(line):
|
|
67
|
-
out.append((path, i, "error", "HEX_COLOR", "
|
|
67
|
+
out.append((path, i, "error", "HEX_COLOR", "Hardcoded color outside the theme. Use MaterialTheme.colorScheme."))
|
|
68
68
|
if NAMED.search(line):
|
|
69
|
-
out.append((path, i, "error", "NAMED_COLOR", "
|
|
69
|
+
out.append((path, i, "error", "NAMED_COLOR", "Named color (Color.Red etc.). Use theme tokens."))
|
|
70
70
|
if PURE.search(line):
|
|
71
|
-
out.append((path, i, "warn", "PURE_BW", "
|
|
71
|
+
out.append((path, i, "warn", "PURE_BW", "Pure black or white. Use theme tokens (onSurface, surface)."))
|
|
72
72
|
if FONT_SIZE.search(line):
|
|
73
|
-
out.append((path, i, "error", "FONT_SIZE", "fontSize
|
|
73
|
+
out.append((path, i, "error", "FONT_SIZE", "Ad-hoc fontSize. Use MaterialTheme.typography."))
|
|
74
74
|
if ALPHA_TEXT.search(line):
|
|
75
|
-
out.append((path, i, "warn", "FADED_TEXT", "
|
|
75
|
+
out.append((path, i, "warn", "FADED_TEXT", "Text with low alpha; contrast may fall below 4.5:1."))
|
|
76
76
|
no_radius = RADIUS.sub("", line)
|
|
77
77
|
if DP_CTX.search(no_radius):
|
|
78
78
|
for m in DP.finditer(no_radius):
|
|
79
79
|
if off_scale(m.group(1)):
|
|
80
80
|
out.append((path, i, "warn", "DP_OFF_SCALE",
|
|
81
|
-
f"{m.group(1)}.dp
|
|
81
|
+
f"{m.group(1)}.dp is off the 4dp scale (4, 8, 12, 16, 24, 32, 48)."))
|
|
82
82
|
break
|
|
83
83
|
for m in RADIUS.finditer(line):
|
|
84
84
|
if float(m.group(1)) >= 24:
|
|
85
|
-
out.append((path, i, "warn", "RADIUS_LARGE", "Radius 24dp
|
|
85
|
+
out.append((path, i, "warn", "RADIUS_LARGE", "Radius of 24dp or more. Use the 8/12/16 scale."))
|
|
86
86
|
m = ELEVATION.search(line)
|
|
87
87
|
if m and (m.group(0).startswith(".shadow") or float(m.group(1)) >= 8):
|
|
88
|
-
out.append((path, i, "warn", "HEAVY_SHADOW", "
|
|
88
|
+
out.append((path, i, "warn", "HEAVY_SHADOW", "Heavy shadow. Prefer tonal surfaces or a thin line."))
|
|
89
89
|
m = DURATION.search(line)
|
|
90
90
|
if m and int(m.group(1)) > 400:
|
|
91
|
-
out.append((path, i, "warn", "SLOW_ANIM", f"
|
|
91
|
+
out.append((path, i, "warn", "SLOW_ANIM", f"Animation of {m.group(1)} ms. Target 150–250 ms."))
|
|
92
92
|
if not is_comment:
|
|
93
93
|
if GRADIENT.search(line):
|
|
94
|
-
out.append((path, i, "warn", "GRADIENT", "
|
|
94
|
+
out.append((path, i, "warn", "GRADIENT", "Gradient. Avoid unless asked."))
|
|
95
95
|
if CD_EMPTY.search(line):
|
|
96
|
-
out.append((path, i, "warn", "CD_EMPTY", 'contentDescription = ""
|
|
96
|
+
out.append((path, i, "warn", "CD_EMPTY", 'contentDescription = "". Use null for decorative elements.'))
|
|
97
97
|
if TEXT_LIT.search(line):
|
|
98
|
-
out.append((path, i, "info", "HARDCODED_TEXT", "
|
|
98
|
+
out.append((path, i, "info", "HARDCODED_TEXT", "Hardcoded text. Move it to strings.xml."))
|
|
99
99
|
if CARD.search(line):
|
|
100
100
|
cards += 1
|
|
101
101
|
if cards > 4:
|
|
102
|
-
out.append((path, 1, "info", "CARD_HEAVY", f"{cards}
|
|
102
|
+
out.append((path, 1, "info", "CARD_HEAVY", f"{cards} Cards in one file. Consider plain rows."))
|
|
103
103
|
|
|
104
104
|
|
|
105
105
|
def lint_xml(path, lines, out):
|
|
@@ -111,16 +111,16 @@ def lint_xml(path, lines, out):
|
|
|
111
111
|
continue
|
|
112
112
|
if X_HEX.search(line):
|
|
113
113
|
sev = "warn" if "/res/drawable" in p else "error"
|
|
114
|
-
out.append((path, i, sev, "HEX_COLOR_XML", "
|
|
114
|
+
out.append((path, i, sev, "HEX_COLOR_XML", "Hardcoded color in a layout/drawable. Reference a theme attribute."))
|
|
115
115
|
if X_TEXTSIZE.search(line):
|
|
116
|
-
out.append((path, i, "warn", "FONT_SIZE_XML", "textSize
|
|
116
|
+
out.append((path, i, "warn", "FONT_SIZE_XML", "Hardcoded textSize. Use textAppearance."))
|
|
117
117
|
if X_TEXT.search(line):
|
|
118
|
-
out.append((path, i, "info", "HARDCODED_TEXT", "
|
|
118
|
+
out.append((path, i, "info", "HARDCODED_TEXT", "Hardcoded text. Move it to strings.xml."))
|
|
119
119
|
for m in X_DP.finditer(line):
|
|
120
120
|
if off_scale(m.group(2)):
|
|
121
|
-
out.append((path, i, "warn", "DP_OFF_SCALE", f"{m.group(2)}dp
|
|
121
|
+
out.append((path, i, "warn", "DP_OFF_SCALE", f"{m.group(2)}dp is off the 4dp scale."))
|
|
122
122
|
if "<gradient" in line:
|
|
123
|
-
out.append((path, i, "warn", "GRADIENT", "
|
|
123
|
+
out.append((path, i, "warn", "GRADIENT", "Gradient. Avoid unless asked."))
|
|
124
124
|
|
|
125
125
|
|
|
126
126
|
def walk(root, include_tests):
|
|
@@ -139,7 +139,7 @@ def main():
|
|
|
139
139
|
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
140
140
|
ap.add_argument("path")
|
|
141
141
|
ap.add_argument("--json", action="store_true")
|
|
142
|
-
ap.add_argument("--strict", action="store_true", help="
|
|
142
|
+
ap.add_argument("--strict", action="store_true", help="warns also fail (exit 1)")
|
|
143
143
|
ap.add_argument("--include-tests", action="store_true")
|
|
144
144
|
a = ap.parse_args()
|
|
145
145
|
|
|
@@ -153,9 +153,9 @@ def main():
|
|
|
153
153
|
local = []
|
|
154
154
|
for i, line in enumerate(lines, 1):
|
|
155
155
|
if EMOJI.search(line):
|
|
156
|
-
local.append((path, i, "error", "EMOJI", "Emoji
|
|
156
|
+
local.append((path, i, "error", "EMOJI", "Emoji or decorative symbol. Remove it."))
|
|
157
157
|
elif SYMBOL.search(line):
|
|
158
|
-
local.append((path, i, "warn", "SYMBOL", "
|
|
158
|
+
local.append((path, i, "warn", "SYMBOL", "Decorative unicode character (arrow, bullet). Use an icon or text."))
|
|
159
159
|
if path.endswith((".kt", ".java")):
|
|
160
160
|
lint_kotlin(path, lines, local)
|
|
161
161
|
elif path.endswith(".xml"):
|
|
@@ -179,7 +179,7 @@ def main():
|
|
|
179
179
|
else:
|
|
180
180
|
for f in found:
|
|
181
181
|
print(f"[{f[2].upper():5}] {f[3]:15} {f[0]}:{f[1]} {f[4]}")
|
|
182
|
-
print(f"\
|
|
182
|
+
print(f"\nSummary: {counts['error']} error, {counts['warn']} warn, {counts['info']} info")
|
|
183
183
|
|
|
184
184
|
bad = counts["error"] + (counts["warn"] if a.strict else 0)
|
|
185
185
|
sys.exit(1 if bad else 0)
|