# Liderit Printer Daemon — Android Wrapper

This directory contains the Kotlin wrapper app for the Liderit printer daemon on Android.

## Architecture

The Android implementation uses a **Go library compiled for Android (NDK)** wrapped in a **Kotlin Service app**.

```
┌────────────────┐
│  Android App   │
│  (Kotlin)      │
└────────┬───────┘
         │
         │  JNI / cgo
         ▼
┌────────────────────┐
│  Go Library (.so)  │
│  (printer daemon   │
│   logic)           │
└────────┬───────────┘
         │
         │  (via HTTP/mTLS)
         ▼
┌────────────────────┐
│  go-daemon server  │
│  receive jobs      │
└────────────────────┘
```

## Requirements

- **Android SDK** (API level 28+; tested on 33+)
- **Android NDK** (r25+; for Go compilation)
- **Kotlin** (1.8+)
- **Go** (1.23+)
- **gradle** (7.5+)

## Build Steps

### 1. Build the Go Library for Android

```bash
cd ..

# Build for arm64-v8a (primary Android architecture):
GOOS=android GOARCH=arm64 CGO_CFLAGS="-I$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" \
  CC="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android33-clang" \
  go build -buildmode=c-shared -o android/app/src/main/jniLibs/arm64-v8a/libprinter.so ./cmd/printer

# Build for armeabi-v7a (legacy support):
GOOS=android GOARCH=arm CGO_CFLAGS="-I$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" \
  CC="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi33-clang" \
  go build -buildmode=c-shared -o android/app/src/main/jniLibs/armeabi-v7a/libprinter.so ./cmd/printer
```

### 2. Build the Kotlin App

```bash
cd android
gradle build --variant release
```

Output: `android/app/build/outputs/apk/release/app-release.apk`

### 3. Install on Device

```bash
adb install android/app/build/outputs/apk/release/app-release.apk
```

## Implementation Notes

### Configuration Interface (scaffold)

The repository now includes a minimal Android configuration UI scaffold under:

- `app/src/main/java/ro/liderit/printer/config/AppConfig.kt`
- `app/src/main/java/ro/liderit/printer/config/ConfigRepository.kt`
- `app/src/main/java/ro/liderit/printer/config/ConfigActivity.kt`
- `app/src/main/res/layout/activity_config.xml`

This interface is configuration-only for now: server address, client key, and default printer mapping.
It persists values to `files/printer-config.json` and does not yet include full JNI print execution wiring.

### Service Management

The Kotlin app declares a **foreground Service** (`LideritPrinterService`) that:
- Loads the Go library via JNI
- Calls the Go `Run()` function (which starts the printer loop)
- Displays a persistent notification (required for Android 8.0+)
- Handles signals (SIGTERM, SIGINT) for clean shutdown

### Printing

The Android Print Framework (`android.print.PrintManager`) is used to:
- Enumerate available printers (via app's USB & network printer discovery)
- Submit jobs to the Print Spooler
- Track job status

Go code receives print jobs and forwards them to the Android Print Framework through JNI callbacks (implemented in the Kotlin wrapper).

### Permissions Required

The app requires the following AndroidManifest.xml permissions:
```xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
</uses-permission>
```

## Config & TLS

The app reads `config.toml` from:
- `~/LideritFiles/config.toml` (user home directory, internal storage)
- Or `/sdcard/Android/data/ro.liderit.printer/files/config.toml` (app cache)

Certificates (`client.crt`, `client.key`, `ca.crt`) are stored in:
- `/data/data/ro.liderit.printer/files/certs/` (Android private app storage)

## Troubleshooting

### App Won't Start

```bash
adb logcat -s LideritPrinter
```

### Library Load Failed

Ensure the Go library is compiled for the target architecture:
```bash
file android/app/src/main/jniLibs/arm64-v8a/libprinter.so
# Expected: ELF 64-bit LSB shared object, ARM aarch64, ...
```

### Printer Detection Failed

1. Open the app and check the printer list
2. Ensure at least one printer is connected via USB or network
3. Check logs: `adb logcat | grep -i printer`

## Next Steps

- Implement JNI bindings in Kotlin (`PinterService.kt`) for Go callback functions
- Integrate Android Print Framework (`PrintManager` API)
- Add auto-printer detection on app startup
- Create an Android UI for printer configuration and status display
- Publish to Google Play (requires app signing & developer account)
