Skip to content

Commit d96b287

Browse files
committed
Add JNI service startup control and camera capture options
Add startup configuration, ADB discovery, startup RPCs, and client commands for on-device service launch flows. Expand Android permission and foreground service handling, expose camera capture controls, and improve JNI reference and callback management. Test Plan: - gofmt on changed Go files: no files reported. - git diff --cached --check: clean. - CI Go vet subset: passed. - go vet ./...: could not complete because this Linux/arm64 cgo setup has no host JNI development headers; the available jni.h is in the Android NDK sysroot and is incompatible with this host target. - Tests not run for this commit-only request.
1 parent 3e0d2e6 commit d96b287

61 files changed

Lines changed: 6647 additions & 389 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/ci.yml‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,23 @@ jobs:
2121
go-version: "1.25.x"
2222

2323
- name: Run unit tests
24-
run: go test ./grpc/server/acl/ ./grpc/server/certauth/ ./mcp/ ./tools/...
24+
run: go test ./adb/ ./startup/ ./grpc/server/startup/ ./grpc/server/acl/ ./grpc/server/certauth/ ./mcp/ ./tools/...
25+
26+
apk-policy:
27+
name: APK foreground policy
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v4
31+
- uses: actions/setup-go@v5
32+
with:
33+
go-version: "1.25.x"
34+
- uses: actions/setup-java@v4
35+
with:
36+
distribution: temurin
37+
java-version: "21"
38+
- uses: android-actions/setup-android@v3
39+
- run: sdkmanager "platforms;android-36"
40+
- run: go test ./tests/apk/
2541

2642
vet:
2743
name: Vet
@@ -35,7 +51,7 @@ jobs:
3551

3652
- name: Vet packages
3753
# handlestore requires CGO (imports jni core with JNI headers)
38-
run: go vet ./grpc/server/acl/ ./grpc/server/certauth/ ./mcp/ ./tools/...
54+
run: go vet ./adb/ ./startup/ ./grpc/server/startup/ ./grpc/server/acl/ ./grpc/server/certauth/ ./mcp/ ./tools/...
3955

4056
build:
4157
name: Build binaries
@@ -75,4 +91,4 @@ jobs:
7591

7692
- name: Lint
7793
# handlestore requires CGO (imports jni core with JNI headers)
78-
run: golangci-lint run --timeout 10m --build-tags="" ./grpc/server/acl/ ./grpc/server/certauth/ ./mcp/ ./tools/... ./cmd/jnicli/ ./cmd/jnimcp/ ./cmd/jniserviceadmin/
94+
run: golangci-lint run --timeout 10m --build-tags="" ./adb/ ./startup/ ./grpc/server/startup/ ./grpc/server/acl/ ./grpc/server/certauth/ ./mcp/ ./tools/... ./cmd/jnicli/ ./cmd/jnimcp/ ./cmd/jniserviceadmin/

‎Makefile‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: generate callbacks proto protoc grpc cli clean lint test test-tools test-e2e test-emulator \
22
build build-server list-commands dist dist-jnicli-linux dist-jnicli-android dist-jnimcp-linux dist-jnimcp-android dist-jniserviceadmin dist-jniservice dist-dex \
3-
magisk apk deploy push start-server stop-server forward
3+
magisk apk deploy push start-server stop-server require-listen
44

55
# Path to the jni repo checkout (sibling directory by default).
66
JNI_DIR ?= ../jni
@@ -98,10 +98,11 @@ endif
9898
# ---- Device deployment ----
9999
ADB ?= $(ANDROID_SDK)/platform-tools/adb
100100
PORT ?= 50051
101-
HOST_PORT ?= $(PORT)
102101
REMOTE_DIR := /data/adb/jniservice
103102

104-
deploy: push start-server forward
103+
deploy: require-listen
104+
$(MAKE) push
105+
$(MAKE) start-server
105106

106107
push:
107108
@ARCH=$${DEVICE_ARCH:-$$($(ADB) shell getprop ro.product.cpu.abi)}; \
@@ -115,8 +116,16 @@ push:
115116
$(ADB) push build/libjniservice-$$ABI.so $(REMOTE_DIR)/libjniservice.so && \
116117
$(ADB) push build/classes.dex $(REMOTE_DIR)/jniservice.dex
117118

118-
start-server: stop-server
119-
$(ADB) shell "JNISERVICE_PORT=$(PORT) \
119+
require-listen:
120+
@test -n "$(JNISERVICE_LISTEN)" || { \
121+
echo "JNISERVICE_LISTEN must be set to this phone's current Wi-Fi IPv4 address"; \
122+
exit 2; \
123+
}
124+
125+
start-server: require-listen
126+
$(MAKE) stop-server
127+
$(ADB) shell "JNISERVICE_LISTEN='$(JNISERVICE_LISTEN)' \
128+
JNISERVICE_PORT='$(PORT)' \
120129
JNISERVICE_DATA_DIR=$(REMOTE_DIR)/data \
121130
LD_LIBRARY_PATH=$(REMOTE_DIR) \
122131
app_process \
@@ -138,10 +147,6 @@ stop-server:
138147
-@$(ADB) shell "pkill -f JNIService" 2>/dev/null || true
139148
@sleep 1
140149

141-
forward:
142-
$(ADB) forward tcp:$(HOST_PORT) tcp:$(PORT)
143-
@echo "Port forwarding: localhost:$(HOST_PORT) -> device:$(PORT)"
144-
145150
# ---- Tests ----
146151

147152
test-e2e:

‎README.md‎

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ flowchart LR
7474

7575
jniservice is a gRPC server that exposes the JNI surface and Android APIs over the network.
7676

77-
### Development (via adb)
78-
79-
```bash
80-
make deploy # build, push, start, forward port
81-
make deploy HOST_PORT=50052 # use different host port
82-
make stop-server # stop the server
83-
```
84-
8577
### Rooted devices (Magisk module)
8678

8779
Auto-starts on boot.
@@ -97,12 +89,28 @@ Configuration (optional): create `/data/adb/modules/jniservice/jniservice.env`:
9789

9890
```bash
9991
JNISERVICE_PORT=50051
100-
JNISERVICE_LISTEN=0.0.0.0
92+
# Use this device's current Wi-Fi address.
93+
JNISERVICE_LISTEN=192.168.1.20
10194
```
10295

96+
In Magisk app-process mode, the launcher passes the selected file path to
97+
`app_process` as `JNISERVICE_CONFIG`; it is parsed as `KEY=VALUE` data, not
98+
sourced as shell code. For APK mode, the selected durable file is staged as
99+
`jniservice.env` at the package-private path described below. Nonempty listener
100+
environment overrides are rebuilt separately in `jniservice.launch.env` on
101+
every launch; an empty file clears prior transient overrides without changing
102+
the durable file. The APK resolves defaults, durable file, staged launch file,
103+
then nonempty process environment values. `app_process` resolves defaults,
104+
selected file, then its nonempty process environment. Only
105+
`JNISERVICE_LISTEN` and `JNISERVICE_PORT` are read from these files;
106+
`JNISERVICE_DATA_DIR` remains environment-only. The default is
107+
`127.0.0.1:50051`; unspecified addresses such as `0.0.0.0` and `::` are
108+
rejected. Bind to the phone's current Wi-Fi IPv4 address for direct-LAN access.
109+
The listener reads its settings when the app process starts.
110+
103111
### Non-rooted devices (APK)
104112

105-
Auto-starts on boot via foreground service.
113+
Auto-starts after the first unlock following boot via a foreground service.
106114

107115
```bash
108116
make apk DIST_GOARCH=arm64 # build APK
@@ -111,21 +119,71 @@ adb install build/jniservice-arm64-v8a.apk
111119

112120
Open "jniservice" from the launcher once to start the service and register the boot receiver.
113121

122+
The APK reads `jniservice.env` from
123+
`/data/data/center.dx.jni.jniservice/files/jniservice/jniservice.env`. A
124+
registered, already-approved `jnicli` identity with permission to use the raw
125+
JNI API can write this private file through the existing authenticated API:
126+
use `java.io.FileWriter` for the path above and write the `KEY=VALUE` lines.
127+
That operation needs permission for the raw JNI `FindClass`, `NewStringUTF`,
128+
`GetMethodID`, `NewObject`, and `CallMethod` RPCs.
129+
The raw JNI calls use the normal mTLS identity and service ACL; `jniserviceadmin`
130+
continues to manage permissions, and no separate listener-settings RPC is
131+
provided. After editing the file, restart the app process so the native
132+
listener reloads it; restarting only the foreground service in an already
133+
running process does not reload the native library. For example:
134+
135+
```bash
136+
adb shell am force-stop center.dx.jni.jniservice
137+
adb shell am start -n center.dx.jni.jniservice/.JNIServiceActivity
138+
```
139+
140+
A missing optional file leaves the defaults in effect, while a malformed or
141+
unreadable file prevents startup rather than silently binding somewhere else.
142+
143+
The optional [phone-local fixed-performance startup action](docs/fixed-performance-startup.md)
144+
can re-enable fixed-performance mode after unlock and connection to trusted Wi-Fi,
145+
using a dedicated paired wireless-ADB identity. It is disabled until configured.
146+
114147
### Connecting
115148

116-
On the device itself:
149+
`jnicli` keeps client credentials in `~/.jnicli` by default. Use `--profile NAME`
150+
for separate devices; this loads `~/.jnicli/NAME/{client.crt,client.key,ca.crt}`.
151+
The `localhost:50051` examples below require jniservice to listen on loopback
152+
(the default); if `JNISERVICE_LISTEN` is set to the phone's Wi-Fi IPv4 address,
153+
phone-local calls must use that address too.
154+
Phone-local invocation example (run `jnicli` on the phone); hosts should use the
155+
phone-IP direct-LAN example below:
117156

118157
```bash
119-
jnicli --addr localhost:50051 --insecure jni get-version
158+
jnicli --profile pixel8pro --addr localhost:50051 auth list-permissions
159+
```
160+
161+
`auth register --profile NAME --insecure --cn NAME` writes the three files to
162+
that profile when the output flags are omitted. Explicit `--cert`, `--key`, and
163+
`--ca` paths always override the defaults.
164+
165+
For direct-LAN access, dial the phone's current Wi-Fi address and configured
166+
port while verifying the server certificate for `jniservice`:
167+
168+
```bash
169+
jnicli --profile pixel8pro --addr 192.168.0.242:50051 \
170+
--authority jniservice camera photo --index 0 --output photo.jpg
120171
```
121172

122-
From another machine (via adb port forwarding):
173+
Replace the address and port with the values configured on the phone. This
174+
uses the profile's mTLS credentials and keeps TLS certificate verification
175+
enabled; do not add `--insecure`.
176+
177+
Phone-local invocation example (run this on the device); hosts should use the
178+
phone-IP direct-LAN example above:
123179

124180
```bash
125-
adb forward tcp:50051 tcp:50051
126181
jnicli --addr localhost:50051 --insecure jni get-version
127182
```
128183

184+
For headless JPEG capture, including AE warm-up, flash, ISO, and exposure
185+
controls, see [camera photo controls](docs/camera-photo.md).
186+
129187
## E2E Test Verification
130188

131189
Run `make test-emulator` to test against a connected device or emulator. Tests skip when `JNICTL_E2E_ADDR` is not set.
@@ -146,9 +204,6 @@ Run `make test-emulator` to test against a connected device or emulator. Tests s
146204
# Build the CLI for Linux
147205
make dist-jnicli-linux
148206

149-
# Build and deploy to a connected Android device
150-
make deploy
151-
152207
# Run E2E tests
153208
make test-emulator
154209
```

0 commit comments

Comments
 (0)