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
13 changes: 11 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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**.

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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**.
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
20 changes: 20 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
20 changes: 20 additions & 0 deletions app/src/accessibility/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Merged into the main manifest only when the build has the Gradle property avg.accessibility=true.
See app/build.gradle.kts. The default build does not have the accessibility service. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<!-- Non-root backend 2: accessibility key interception -->
<service
android:name=".accessibility.AgpAccessibilityService"
android:exported="true"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
16 changes: 3 additions & 13 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,9 @@
android:exported="false"
tools:ignore="Instantiatable" />

<!-- Non-root backend 2: accessibility key interception -->
<service
android:name=".accessibility.AgpAccessibilityService"
android:exported="true"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
<!-- The accessibility service (Accessibility mode) is not in the default build.
Google Play Protect can block a sideloaded APK that declares it.
It is in app/src/accessibility and the build adds it with -Pavg.accessibility=true. -->

<!-- Non-root backend 1: Shizuku -->
<provider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.avgamepad.accessibility

import com.avgamepad.BuildConfig

/**
* State of the optional accessibility service.
* The default build does not have the service (see the Gradle property avg.accessibility).
*/
object AccessibilityState {
/** true if this build has the accessibility service. */
val available: Boolean get() = BuildConfig.ACCESSIBILITY

/** true while the system has the service connected. The service sets this value. */
@Volatile var connected: Boolean = false
}
7 changes: 6 additions & 1 deletion app/src/main/java/com/avgamepad/data/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.avgamepad.data

import android.content.Context
import android.content.SharedPreferences
import com.avgamepad.accessibility.AccessibilityState
import com.avgamepad.model.AccRule
import com.avgamepad.model.BackendType
import com.avgamepad.model.EngineConfig
Expand All @@ -21,7 +22,11 @@ object Store {
}

var backend: BackendType
get() = runCatching { BackendType.valueOf(sp.getString("backend", "")!!) }.getOrDefault(BackendType.SHIZUKU)
get() {
val b = runCatching { BackendType.valueOf(sp.getString("backend", "")!!) }.getOrDefault(BackendType.SHIZUKU)
// A build without the accessibility service has no Accessibility mode.
return if (b == BackendType.ACCESSIBILITY && !AccessibilityState.available) BackendType.SHIZUKU else b
}
set(v) { sp.edit().putString("backend", v.name).apply() }

var accEnabled: Boolean
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/avgamepad/ui/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.Intent
import androidx.core.content.ContextCompat
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.avgamepad.accessibility.AgpAccessibilityService
import com.avgamepad.accessibility.AccessibilityState
import com.avgamepad.backend.BackendManager
import com.avgamepad.data.Store
import com.avgamepad.model.*
Expand Down Expand Up @@ -311,7 +311,7 @@ class AppViewModel(private val app: Application) : AndroidViewModel(app) {
}

// ---- accessibility mode ----
fun checkAcc() = _ui.update { it.copy(accServiceOn = AgpAccessibilityService.instance != null) }
fun checkAcc() = _ui.update { it.copy(accServiceOn = AccessibilityState.connected) }

fun setAccEnabled(v: Boolean) {
Store.accEnabled = v
Expand Down
Loading
Loading