From 9266d8816f6a3d2f0d58eef2f8da0dc277a23a0c Mon Sep 17 00:00:00 2001 From: "Adam.N" Date: Wed, 18 Nov 2026 06:35:31 +0000 Subject: [PATCH 1/2] Build without the accessibility service by default Google Play Protect blocks the installation of a sideloaded APK that declares an accessibility service. The block message says that the application can request access to sensitive data. The only match in the manifest was AgpAccessibilityService (BIND_ACCESSIBILITY_SERVICE). The default build now has no accessibility service. Use the Gradle property -Pavg.accessibility=true to add it (Accessibility mode). - Move the service, its manifest entry and its XML configuration to app/src/accessibility. The build adds this source set to the debug and release build types only when the property is true. The output path of the APK does not change. - Add BuildConfig.ACCESSIBILITY and AccessibilityState (main source set). The service sets AccessibilityState.connected. The view model reads it, so the main code does not use the service class. - Hide Accessibility mode in the UI when the build does not have it. Stop the one-second poll of the service state in that case. - Store.backend returns Shizuku when the saved mode is Accessibility and the build does not have it. - Workflow "Build APK": add the input "Add Accessibility mode". The APK file name ends with -accessibility when it is true. - Workflow "CodeQL": build with the option, so that the service is still analyzed. - Update the README (STE100). --- .github/workflows/build.yml | 13 ++++++-- .github/workflows/codeql.yml | 3 +- README.md | 31 ++++++++++++++++--- app/build.gradle.kts | 20 ++++++++++++ app/src/accessibility/AndroidManifest.xml | 20 ++++++++++++ .../accessibility/AgpAccessibilityService.kt | 7 ++++- .../res/xml/accessibility_service_config.xml | 0 app/src/main/AndroidManifest.xml | 16 ++-------- .../accessibility/AccessibilityState.kt | 15 +++++++++ app/src/main/java/com/avgamepad/data/Store.kt | 7 ++++- .../java/com/avgamepad/ui/AppViewModel.kt | 4 +-- app/src/main/java/com/avgamepad/ui/Screens.kt | 7 +++-- 12 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 app/src/accessibility/AndroidManifest.xml rename app/src/{main => accessibility}/java/com/avgamepad/accessibility/AgpAccessibilityService.kt (90%) rename app/src/{main => accessibility}/res/xml/accessibility_service_config.xml (100%) create mode 100644 app/src/main/java/com/avgamepad/accessibility/AccessibilityState.kt diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a9ea01f..d7628a4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,6 +11,10 @@ on: description: "Version, for example 1.2.0 (empty = avg.versionName in gradle.properties)" type: string default: "" + accessibility: + description: "Add Accessibility mode (Google Play Protect can block an APK that has it)" + type: boolean + default: false permissions: contents: write @@ -101,7 +105,8 @@ jobs: env: AVG_VERSION_CODE: ${{ github.run_number }} APP_VERSION: ${{ steps.meta.outputs.version }} - run: gradle :app:assembleRelease -Pavg.versionName="$APP_VERSION" --stacktrace + ACCESSIBILITY: ${{ inputs.accessibility }} + run: gradle :app:assembleRelease -Pavg.versionName="$APP_VERSION" -Pavg.accessibility="$ACCESSIBILITY" --stacktrace - name: Check the APK version env: @@ -117,8 +122,12 @@ jobs: - name: Name the APK id: apk + env: + ACCESSIBILITY: ${{ inputs.accessibility }} run: | - NAME="AndroidVirtualGamepad-${{ steps.meta.outputs.version }}.apk" + SUFFIX="" + if [ "$ACCESSIBILITY" = "true" ]; then SUFFIX="-accessibility"; fi + NAME="AndroidVirtualGamepad-${{ steps.meta.outputs.version }}${SUFFIX}.apk" cp app/build/outputs/apk/release/app-release.apk "$NAME" echo "name=$NAME" >> "$GITHUB_OUTPUT" diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index e34df07..2d06bf1 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -68,9 +68,10 @@ jobs: build-mode: ${{ matrix.build-mode }} # CodeQL must see the compiler run. Use clean and --no-daemon so that nothing comes from a cache. + # The option avg.accessibility adds the accessibility service, so that CodeQL analyzes it too. - name: Build debug APK if: matrix.build-mode == 'manual' - run: gradle clean :app:assembleDebug -Pavg.abis=arm64-v8a --no-daemon --stacktrace + run: gradle clean :app:assembleDebug -Pavg.abis=arm64-v8a -Pavg.accessibility=true --no-daemon --stacktrace - name: Analyze uses: github/codeql-action/analyze@v3 diff --git a/README.md b/README.md index 5d4c174..bea1fa7 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,13 @@ A pad is one virtual gamepad. Each pad has its own source device, mapping, ident ### 1.4 Access modes -An Android application cannot read `/dev/input` or write to `/dev/uinput` without more permission. The application has three access modes. +An Android application cannot read `/dev/input` or write to `/dev/uinput` without more permission. The application has two access modes. A third mode, Accessibility, is optional. The default build does not have it (see section 3.3). | Mode | Requirement | Read `/dev/input` | Virtual gamepad | Existing device | Key injection | |---|---|---|---|---|---| | Root | A root manager (`su`) | Yes | Yes | Yes | Yes | | Shizuku | Shizuku is in operation | Yes | Depends on the device (note 1) | Depends on the device (note 1) | Yes | -| Accessibility | None | No | No | No | No (note 2) | +| Accessibility (optional) | None | No | No | No | No (note 2) | Keyboard keys and rumble need Root or Shizuku mode. Accessibility mode does not support them. @@ -64,7 +64,7 @@ The helper process contains the complete event loop. The loop has low latency be - One of these: - A rooted device. - [Shizuku](https://shizuku.rikka.app/) in operation (Android 11 or later: wireless debugging. Other versions: ADB). - - Neither of these (Accessibility mode, limited function). + - Neither of these (Accessibility mode, limited function). This mode needs a special build (see section 3.3). - To build the project: Android Studio (Jellyfish or later), JDK 17, Android SDK 34, Android NDK 26.3.11579264 and CMake 3.22.1. Install the NDK and CMake with the SDK Manager. ## 3 Build @@ -90,6 +90,7 @@ The file `.github/workflows/build.yml` builds the release APK. You start it by h 3. Select **Run workflow** and select the branch. 4. Optional: enter a **version**, for example `1.2.0`. If you leave this field empty, the workflow uses the value of `avg.versionName` in `gradle.properties`. 5. To make a GitHub release, set **Publish the APK as a GitHub release** to true. +6. To add Accessibility mode, set **Add Accessibility mode** to true (see section 3.3). The workflow always saves the APK as a workflow artifact. It makes a release only if you set the option **publish**. @@ -158,6 +159,22 @@ The workflow builds the debug APK with the command `gradle clean :app:assembleDe To build one CPU architecture only, add `-Pavg.abis=arm64-v8a` to the Gradle command. +### 3.3 Accessibility mode (optional) + +The default build does not have Accessibility mode. The APK has no accessibility service. Google Play Protect can block the installation of an APK that declares an accessibility service. The message says that the application can request access to sensitive data. + +To add Accessibility mode, set the Gradle property `avg.accessibility` to `true`: + +``` +./gradlew assembleDebug -Pavg.accessibility=true +``` + +The build then adds the files in `app/src/accessibility/` to the application. These files are the service, its manifest entry and its resources. + +In the GitHub workflow **Build APK**, set **Add Accessibility mode** to true. The name of the APK file then ends with `-accessibility`. The CodeQL workflow always builds with this option, so CodeQL also analyzes the service. + +**NOTE:** The tag of a release does not have the suffix `-accessibility`. Use a different version when you publish the two kinds of APK. + ## 4 Installation 1. Copy the APK to the device. @@ -190,6 +207,8 @@ To build one CPU architecture only, add `-Pavg.abis=arm64-v8a` to the Gradle com ### 5.3 Accessibility mode +This mode is only in a build that you make with `avg.accessibility=true` (see section 3.3). The default build does not have it. + 1. In the section **Access mode**, select **Accessibility**. 2. Select **Open accessibility settings**. 3. Enable the service **Android Virtual Gamepad**. @@ -413,6 +432,7 @@ The application opens the source device for reading and writing, and it uses the | The pads do not start at boot | Root is not allowed without a question, Shizuku does not operate, or the device maker stopped the application | Do the steps of section 5.4. Look at the log with the tag `AVG-boot`. | | The remapper stops after a short time | Android stopped the application | Disable battery optimization for the application. | | The status shows `source lost` | The controller is disconnected | Connect the controller again. The remapper reconnects automatically. | +| Google Play Protect blocks the installation | The APK has an accessibility service | Use the default build. It has no accessibility service. | ## 10 Project structure @@ -425,16 +445,17 @@ app/src/main/ engine/ Remapper, gamepad and keyboard sinks, rumble bridge, device scanner, event loop service/ Helper implementation, Shizuku and root wrappers backend/ Connection to the helper (root or Shizuku) - accessibility/ Accessibility service (non-root fallback) + accessibility/ State of the accessibility service ui/ Compose screens (phone and TV), pad selection data/ Saved settings (one configuration for each pad) +app/src/accessibility/ Optional: accessibility service, its manifest entry and its resources ``` ## 11 Security **WARNING:** The helper process has root or shell permission. Install the application only from a source that you trust. -The accessibility service reads only key events of gamepads. It does not read the content of the screen. +In a build with Accessibility mode, the accessibility service reads only key events of gamepads. It does not read the content of the screen. ## 12 Licence diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 116b233..ce1dbd3 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -4,6 +4,11 @@ plugins { id("org.jetbrains.kotlin.plugin.compose") } +// The default build has no accessibility service. Google Play Protect can block the installation of a +// sideloaded APK that declares an accessibility service. Use -Pavg.accessibility=true to add the service +// (Accessibility mode). The files of the service are in app/src/accessibility. +val withAccessibility = providers.gradleProperty("avg.accessibility").map { it.toBoolean() }.getOrElse(false) + android { namespace = "com.avgamepad" compileSdk = 34 @@ -19,6 +24,8 @@ android { val abis = providers.gradleProperty("avg.abis").getOrElse("arm64-v8a,armeabi-v7a,x86_64,x86").split(",") ndk { abiFilters += abis } externalNativeBuild { cmake { cppFlags += "-std=c++17" } } + // The UI uses this value to show or hide Accessibility mode. + buildConfigField("boolean", "ACCESSIBILITY", withAccessibility.toString()) } buildFeatures { @@ -38,6 +45,19 @@ android { // The library must exist as a real file on disk. packaging { jniLibs { useLegacyPackaging = true } } + // Accessibility mode (optional). The manifest of a build type is merged into the main manifest. + sourceSets { + if (withAccessibility) { + listOf("debug", "release").forEach { type -> + getByName(type) { + manifest.srcFile("src/accessibility/AndroidManifest.xml") + res.srcDir("src/accessibility/res") + java.srcDir("src/accessibility/java") + } + } + } + } + // Release signing comes from environment variables (set by the GitHub workflow). // Without a keystore, the release APK uses the debug key. It is installable but not a trusted release key. val keystorePath: String? = System.getenv("AVG_KEYSTORE_PATH") diff --git a/app/src/accessibility/AndroidManifest.xml b/app/src/accessibility/AndroidManifest.xml new file mode 100644 index 0000000..ab3c090 --- /dev/null +++ b/app/src/accessibility/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + diff --git a/app/src/main/java/com/avgamepad/accessibility/AgpAccessibilityService.kt b/app/src/accessibility/java/com/avgamepad/accessibility/AgpAccessibilityService.kt similarity index 90% rename from app/src/main/java/com/avgamepad/accessibility/AgpAccessibilityService.kt rename to app/src/accessibility/java/com/avgamepad/accessibility/AgpAccessibilityService.kt index 6f4498a..854b972 100644 --- a/app/src/main/java/com/avgamepad/accessibility/AgpAccessibilityService.kt +++ b/app/src/accessibility/java/com/avgamepad/accessibility/AgpAccessibilityService.kt @@ -14,16 +14,21 @@ import com.avgamepad.model.AccRule * Non-root fallback. The system gives this service the key events of gamepads. * Android does not let an accessibility service create gamepad events. * This service can only run Android actions (Back, Home, taps) and block keys. + * The default build does not have this file (see the Gradle property avg.accessibility). */ class AgpAccessibilityService : AccessibilityService() { companion object { @Volatile var instance: AgpAccessibilityService? = null } - override fun onServiceConnected() { instance = this } + override fun onServiceConnected() { + instance = this + AccessibilityState.connected = true + } override fun onUnbind(intent: Intent?): Boolean { instance = null + AccessibilityState.connected = false return super.onUnbind(intent) } diff --git a/app/src/main/res/xml/accessibility_service_config.xml b/app/src/accessibility/res/xml/accessibility_service_config.xml similarity index 100% rename from app/src/main/res/xml/accessibility_service_config.xml rename to app/src/accessibility/res/xml/accessibility_service_config.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index debba7e..c503a35 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -64,19 +64,9 @@ android:exported="false" tools:ignore="Instantiatable" /> - - - - - - - + + BackendType.entries.filter { it != BackendType.ACCESSIBILITY || AccessibilityState.available }.forEach { t -> FilterChip(selected = ui.backend == t, onClick = { vm.setBackend(t) }, label = { Text(t.title) }) } } From 337d6bf01f6180c4425ee1a7cc901666bf255b2c Mon Sep 17 00:00:00 2001 From: "Adam.N" Date: Wed, 18 Nov 2026 06:35:31 +0000 Subject: [PATCH 2/2] Move the focus to the other button after Start or Stop The buttons Start and Stop are disabled when their action is not possible. After a click, the pressed button becomes disabled and loses the focus. The focus then moved to a distant button. Add StartStopRow. A click on Start sets a pending state. When the pad starts, the focus moves to Stop. A click on Stop or Stop all moves the focus to Start when the pad stops. If the action fails, the state does not change, the focus stays, and the pending state expires after 5 seconds. The focus does not move when the state changes for another reason, for example when a pad stops by itself. --- app/src/main/java/com/avgamepad/ui/Screens.kt | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/avgamepad/ui/Screens.kt b/app/src/main/java/com/avgamepad/ui/Screens.kt index 2213d75..096be6a 100644 --- a/app/src/main/java/com/avgamepad/ui/Screens.kt +++ b/app/src/main/java/com/avgamepad/ui/Screens.kt @@ -12,6 +12,8 @@ import androidx.compose.foundation.lazy.items import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontWeight @@ -111,11 +113,7 @@ fun AppScreen(vm: AppViewModel) { Text("The settings below belong to Pad ${ui.slot + 1}.", style = MaterialTheme.typography.bodySmall) Text(if (ui.status.isNotBlank()) ui.status else if (ui.running) "Running" else "Stopped") if (ui.log.isNotBlank()) Text(ui.log, style = MaterialTheme.typography.bodySmall) - Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { - Button(onClick = vm::start, enabled = !ui.running && be.connected) { Text("Start pad ${ui.slot + 1}") } - OutlinedButton(onClick = vm::stop, enabled = ui.running) { Text("Stop") } - if (ui.runningSlots.size > 1) TextButton(onClick = vm::stopAll) { Text("Stop all") } - } + StartStopRow(ui, vm, be.connected) } } item { @@ -281,6 +279,51 @@ private fun PadRow(pad: Pad, ui: UiState, vm: AppViewModel) { } } +/** + * The buttons Start, Stop and Stop all. A button is disabled when its action is not possible. + * A disabled button loses the focus, and the focus then moves to a distant button. + * After a click, the focus moves to the other button when the state of the pad changes: + * to Stop after Start, and to Start after Stop or Stop all. + */ +@Composable +private fun StartStopRow(ui: UiState, vm: AppViewModel, connected: Boolean) { + val startFocus = remember { FocusRequester() } + val stopFocus = remember { FocusRequester() } + // Set by a click. true = the pad starts (focus Stop). false = the pad stops (focus Start). + var pending by remember { mutableStateOf(null) } + + LaunchedEffect(ui.runningSlots) { + val p = pending + if (p != null && p == ui.running) { + runCatching { (if (p) stopFocus else startFocus).requestFocus() } + pending = null + } + } + // A start or a stop that fails does not change the state. Forget the click after 5 seconds. + LaunchedEffect(pending) { + if (pending != null) { + delay(5_000) + pending = null + } + } + + Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + Button( + onClick = { pending = true; vm.start() }, + enabled = !ui.running && connected, + modifier = Modifier.focusRequester(startFocus), + ) { Text("Start pad ${ui.slot + 1}") } + OutlinedButton( + onClick = { pending = false; vm.stop() }, + enabled = ui.running, + modifier = Modifier.focusRequester(stopFocus), + ) { Text("Stop") } + if (ui.runningSlots.size > 1) { + TextButton(onClick = { pending = false; vm.stopAll() }) { Text("Stop all") } + } + } +} + @Composable private fun AccessibilitySection(ui: UiState, vm: AppViewModel) { val ctx = LocalContext.current