/ android / README.md
README.md
 1  # RESTai Mobile — Android client
 2  
 3  This is the **Android** implementation of RESTai's Mobile pairing
 4  protocol. A minimal Android app that pairs with a RESTai project via
 5  QR code (generated from the project's **Mobile** tab) and exposes it as
 6  a streaming chat — ChatGPT-style.
 7  
 8  Future platforms (iOS, etc.) can implement the same QR payload and will
 9  live in sibling folders.
10  
11  ## Architecture
12  
13  - **Kotlin + Jetpack Compose + Material 3** for the UI.
14  - **ML Kit Barcode Scanning** + **CameraX** for the one-time QR scan.
15  - **OkHttp** `fetchEventSource`-style reader for SSE streaming against
16    `POST /projects/{id}/chat` with `stream=true`.
17  - Credentials persisted in **EncryptedSharedPreferences**
18    (AES-256-GCM, androidx.security.crypto).
19  
20  ## QR payload
21  
22  The RESTai project **Mobile** tab generates a QR with this JSON:
23  
24  ```json
25  {
26    "host": "https://restai.example.com",
27    "project_id": 42,
28    "project_name": "my-support-bot",
29    "api_key": "xxxxxxxxxx"
30  }
31  ```
32  
33  - `host` is used as the base URL.
34  - `api_key` is a read-only project-scoped key. Rotation from the RESTai
35    side immediately invalidates the app's session → the app falls back to
36    the QR-scan screen.
37  
38  ## First run
39  
40  1. App opens → camera permission → QR scan screen.
41  2. On scan, credentials are validated (`GET /auth/whoami` + Bearer),
42     persisted encrypted, and the chat screen is shown.
43  3. Subsequent launches skip straight to the chat screen; if the stored
44     key is rejected (401), the app returns to the QR screen.
45  
46  ## Build
47  
48  Open `android/` in Android Studio (Giraffe or newer) and press ▶︎.
49  CLI build:
50  
51  ```bash
52  cd android
53  ./gradlew assembleDebug
54  ```
55  
56  APK lands in `android/app/build/outputs/apk/debug/`.
57  
58  ## Files of interest
59  
60  ```
61  android/
62    build.gradle.kts                   project-level config
63    settings.gradle.kts                module list
64    app/
65      build.gradle.kts                 dependencies, Compose, min SDK 26
66      src/main/AndroidManifest.xml     camera permission, theme, activity
67      src/main/java/cloud/restai/mobile/
68        MainActivity.kt                entry point
69        Nav.kt                         QR ↔ Chat routing
70        QrScreen.kt                    CameraX + ML Kit scan
71        ChatScreen.kt                  Compose chat UI
72        ChatClient.kt                  OkHttp SSE client
73        Credentials.kt                 EncryptedSharedPreferences wrapper
74        Models.kt                      QR payload, Message, UI state
75        Theme.kt                       Material 3 theme
76  ```