Skip to content

Target static shortcuts at the active app variant - #146

Merged
hawkff merged 1 commit into
mainfrom
fix/014-variant-safe-shortcuts
Jul 13, 2026
Merged

Target static shortcuts at the active app variant#146
hawkff merged 1 commit into
mainfrom
fix/014-variant-safe-shortcuts

Conversation

@hawkff

@hawkff hawkff commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Summary

  • derive the shortcut target package from application-ID configuration
  • override the generated value for debug variants
  • verify compiled shortcut targets against the active application ID

Validation

  • Namespace CI run 29212282118 passed
  • local read-only review reported no correctness findings
  • launcher validation remains pending until a compatible device is available

Greptile Summary

This PR makes static shortcuts point at the active app package. The main changes are:

  • Generate shortcuts.xml per Android variant.
  • Replace the hardcoded shortcut package with a template value.
  • Add a Robolectric test for compiled shortcut targets.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
buildSrc/src/main/kotlin/Helpers.kt Adds variant-specific shortcut resource generation and wires the output into Android resources.
buildSrc/src/main/resources/shortcuts-template.xml Moves the shortcut XML into a template with a generated target package.
app/src/test/java/io/nekohasekai/sagernet/ui/ShortcutTargetPackageTest.kt Adds a test that checks compiled shortcut intents target the active app package.

Reviews (4): Last reviewed commit: "fix(shortcuts): target the active applic..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The build now generates variant-specific shortcut resources with the correct application package. A Robolectric test parses the generated shortcuts and verifies package attributes, target classes, shortcut counts, and expected identifiers.

Changes

Shortcut Resource Generation

Layer / File(s) Summary
Variant shortcut resource generation
buildSrc/src/main/kotlin/Helpers.kt, buildSrc/src/main/resources/shortcuts-template.xml
Each Android variant generates shortcuts.xml with its package name through a cacheable Gradle task and generated resource directory.
Generated shortcut validation
app/src/test/java/io/nekohasekai/sagernet/ui/ShortcutTargetPackageTest.kt
A Robolectric test parses shortcut resources and verifies package attributes, target classes, shortcut counts, and expected IDs.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AndroidVariant
  participant GenerateShortcutResourcesTask
  participant shortcuts_template_xml
  participant GeneratedResources
  AndroidVariant->>GenerateShortcutResourcesTask: configure targetPackage
  GenerateShortcutResourcesTask->>shortcuts_template_xml: load and replace placeholder
  GenerateShortcutResourcesTask->>GeneratedResources: write shortcuts.xml
  AndroidVariant->>GeneratedResources: add generated res source
Loading

Poem

I’m a rabbit with shortcuts, four buttons in line,
Each package now matches its variant by design.
The build plants the XML,
Tests ring their bell—
Hop, toggle and scan, everything’s fine!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: making static shortcuts target the active app variant.
Description check ✅ Passed The description matches the changeset and describes the generated shortcut package, debug override, and validation test.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands.

@hawkff
hawkff force-pushed the fix/014-variant-safe-shortcuts branch from eb71084 to 131583f Compare July 13, 2026 00:29

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
buildSrc/src/main/kotlin/Helpers.kt (1)

281-326: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider extracting the XML template out of the Kotlin string literal.

Embedding the full shortcuts XML as a raw Kotlin string means no XML/Android-resource tooling (syntax highlighting, @string/@drawable reference validation, lint) applies to it, and future edits to the shortcut list happen inside build logic rather than a resource file. Moving the markup to a template resource with a placeholder keeps the content declarative and easier to review/diff.

♻️ Proposed refactor
-    private fun renderShortcuts(packageName: String) =
-        """
-        <?xml version="1.0" encoding="utf-8"?>
-        <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
-            <shortcut
-                android:icon="`@drawable/ic_qu_shadowsocks_launcher`"
-                android:shortcutId="toggle"
-                android:shortcutLongLabel="`@string/quick_toggle`"
-                android:shortcutShortLabel="`@string/quick_toggle`">
-                <intent
-                    android:action="android.intent.action.MAIN"
-                    android:targetClass="io.nekohasekai.sagernet.QuickToggleShortcut"
-                    android:targetPackage="$packageName" />
-            </shortcut>
-            ...
-        </shortcuts>
-        """.trimIndent() + "\n"
+    private fun renderShortcuts(packageName: String): String {
+        val template = javaClass.getResourceAsStream("/shortcuts-template.xml")
+            ?.bufferedReader()
+            ?.readText()
+            ?: error("shortcuts-template.xml resource not found on classpath")
+        return template.replace("{{TARGET_PACKAGE}}", packageName)
+    }

New file buildSrc/src/main/resources/shortcuts-template.xml would hold the same markup with {{TARGET_PACKAGE}} in place of $packageName.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@buildSrc/src/main/kotlin/Helpers.kt` around lines 281 - 326, Extract the
shortcuts XML from the Kotlin string in renderShortcuts into
buildSrc/src/main/resources/shortcuts-template.xml, preserving the existing
markup and replacing the package interpolation with {{TARGET_PACKAGE}}. Update
renderShortcuts to load that template resource and substitute the target package
placeholder, retaining the trailing newline behavior and all existing shortcut
definitions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@buildSrc/src/main/kotlin/Helpers.kt`:
- Around line 281-326: Extract the shortcuts XML from the Kotlin string in
renderShortcuts into buildSrc/src/main/resources/shortcuts-template.xml,
preserving the existing markup and replacing the package interpolation with
{{TARGET_PACKAGE}}. Update renderShortcuts to load that template resource and
substitute the target package placeholder, retaining the trailing newline
behavior and all existing shortcut definitions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 740645bb-af71-4b6f-9ce6-5fed53046d92

📥 Commits

Reviewing files that changed from the base of the PR and between ee2e9de and 131583f.

📒 Files selected for processing (3)
  • app/src/main/res/xml/shortcuts.xml
  • app/src/test/java/io/nekohasekai/sagernet/ui/ShortcutTargetPackageTest.kt
  • buildSrc/src/main/kotlin/Helpers.kt
💤 Files with no reviewable changes (1)
  • app/src/main/res/xml/shortcuts.xml

@hawkff
hawkff force-pushed the fix/014-variant-safe-shortcuts branch from 131583f to 46da1d3 Compare July 13, 2026 00:52
@hawkff
hawkff force-pushed the fix/014-variant-safe-shortcuts branch from 46da1d3 to 79788ce Compare July 13, 2026 01:02
@hawkff

hawkff commented Jul 13, 2026

Copy link
Copy Markdown
Owner Author

Verified on Android: all four static shortcuts resolved inside the installed debug variant without launch errors. Existing app data remained intact.

@hawkff
hawkff merged commit 3055b52 into main Jul 13, 2026
9 checks passed
@hawkff
hawkff deleted the fix/014-variant-safe-shortcuts branch July 13, 2026 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant