From 83bd25aa3bd29935cf2a1087ceaac3590767498b Mon Sep 17 00:00:00 2001 From: weishu Date: Mon, 17 Aug 2026 11:22:35 +0800 Subject: [PATCH] feat(android): scaffold Compose app + core modules + CI (B-M0) --- .github/workflows/android.yml | 42 +++ .gitignore | 9 + android/README.md | 77 ++++++ android/app/build.gradle.kts | 63 +++++ android/app/proguard-rules.pro | 3 + android/app/src/main/AndroidManifest.xml | 30 +++ .../kotlin/app/hapi/companion/AppGraph.kt | 15 ++ .../kotlin/app/hapi/companion/MainActivity.kt | 69 +++++ .../app/hapi/companion/ui/theme/Theme.kt | 51 ++++ android/app/src/main/res/values/strings.xml | 4 + android/app/src/main/res/values/themes.xml | 6 + android/build.gradle.kts | 9 + android/core/data/build.gradle.kts | 38 +++ .../main/kotlin/app/hapi/data/DataModule.kt | 27 ++ android/core/protocol/build.gradle.kts | 35 +++ .../app/hapi/protocol/pairing/BindLink.kt | 93 +++++++ .../app/hapi/protocol/wire/JsonExtensions.kt | 53 ++++ .../app/hapi/protocol/wire/ProtocolVersion.kt | 11 + .../app/hapi/protocol/pairing/BindLinkTest.kt | 102 +++++++ .../hapi/protocol/wire/JsonExtensionsTest.kt | 113 ++++++++ android/gradle.properties | 12 + android/gradle/libs.versions.toml | 42 +++ android/gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 43764 bytes .../gradle/wrapper/gradle-wrapper.properties | 8 + android/gradlew | 251 ++++++++++++++++++ android/gradlew.bat | 94 +++++++ android/settings.gradle.kts | 27 ++ 27 files changed, 1284 insertions(+) create mode 100644 .github/workflows/android.yml create mode 100644 android/README.md create mode 100644 android/app/build.gradle.kts create mode 100644 android/app/proguard-rules.pro create mode 100644 android/app/src/main/AndroidManifest.xml create mode 100644 android/app/src/main/kotlin/app/hapi/companion/AppGraph.kt create mode 100644 android/app/src/main/kotlin/app/hapi/companion/MainActivity.kt create mode 100644 android/app/src/main/kotlin/app/hapi/companion/ui/theme/Theme.kt create mode 100644 android/app/src/main/res/values/strings.xml create mode 100644 android/app/src/main/res/values/themes.xml create mode 100644 android/build.gradle.kts create mode 100644 android/core/data/build.gradle.kts create mode 100644 android/core/data/src/main/kotlin/app/hapi/data/DataModule.kt create mode 100644 android/core/protocol/build.gradle.kts create mode 100644 android/core/protocol/src/main/kotlin/app/hapi/protocol/pairing/BindLink.kt create mode 100644 android/core/protocol/src/main/kotlin/app/hapi/protocol/wire/JsonExtensions.kt create mode 100644 android/core/protocol/src/main/kotlin/app/hapi/protocol/wire/ProtocolVersion.kt create mode 100644 android/core/protocol/src/test/kotlin/app/hapi/protocol/pairing/BindLinkTest.kt create mode 100644 android/core/protocol/src/test/kotlin/app/hapi/protocol/wire/JsonExtensionsTest.kt create mode 100644 android/gradle.properties create mode 100644 android/gradle/libs.versions.toml create mode 100644 android/gradle/wrapper/gradle-wrapper.jar create mode 100644 android/gradle/wrapper/gradle-wrapper.properties create mode 100755 android/gradlew create mode 100644 android/gradlew.bat create mode 100644 android/settings.gradle.kts diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml new file mode 100644 index 00000000..6b92d7b5 --- /dev/null +++ b/.github/workflows/android.yml @@ -0,0 +1,42 @@ +name: android + +on: + push: + branches: + - main + paths: + - 'android/**' + - 'shared/fixtures/**' + - '.github/workflows/android.yml' + pull_request: + paths: + - 'android/**' + - 'shared/fixtures/**' + - '.github/workflows/android.yml' + +permissions: + contents: read + +concurrency: + group: android-${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + runs-on: ubuntu-latest + defaults: + run: + working-directory: android + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: '21' + # Android SDK is preinstalled on GitHub ubuntu runners (licenses + # accepted); AGP auto-installs any missing platform/build-tools. + - uses: gradle/actions/setup-gradle@v4 + - name: Protocol tests (pure JVM, fixtures-driven) + run: ./gradlew :core:protocol:test + - name: Assemble debug APK + run: ./gradlew :app:assembleDebug diff --git a/.gitignore b/.gitignore index 7c12eb73..af4dd748 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,12 @@ e2e-output/ .xyz-harness .agents/ .pi/ + +# Android (android/) +android/**/build/ +android/.gradle/ +android/.kotlin/ +android/local.properties +android/captures/ +*.keystore +*.jks diff --git a/android/README.md b/android/README.md new file mode 100644 index 00000000..74b6f442 --- /dev/null +++ b/android/README.md @@ -0,0 +1,77 @@ +# HAPI Android Companion + +Native Android client (Kotlin + Jetpack Compose) for the HAPI hub. Fully +independent from the web app; shares only the protocol contract +(`docs/api/`) and the golden fixtures (`shared/fixtures/`). + +- **applicationId**: `run.hapi.companion` · **minSdk** 26 · **target/compileSdk** 36 +- **Toolchain**: Gradle 8.14.2 (wrapper) · AGP 8.11.1 · Kotlin 2.1.21 · Compose BOM 2025.05.00 · JDK 17+ (CI uses 21) + +## Modules + +| Module | Type | Responsibility | +|---|---|---| +| `:core:protocol` | **pure Kotlin/JVM** (no Android) | Hub wire types (kotlinx.serialization), chat pipeline port (normalize → reduce → tool groups), message-window/pagination logic, versioned patch application, modes catalog, git output parsers, `BindLink` pairing-link parsing. | +| `:core:data` | Android library | Transport + persistence: `HapiApi` (OkHttp REST, single-flight 401 re-auth), `SseEngine` (okhttp-sse, resume/backoff state machine), StateFlow stores + AtomicFile JSON snapshots, credential store, FCM registration, WorkManager workers. Placeholder in M0 — see `DataModule.kt`. | +| `:app` | Android application | Compose UI, navigation, deep links (`hapicompanion://bind`), FCM service (M4), hand-rolled DI (`AppGraph`, no Hilt). | + +Dependency direction: `:app` → `:core:data` → `:core:protocol`. + +## Protocol conformance fixtures + +`:core:protocol` is the porting target for `web/src/chat/` and is verified +against golden fixtures generated from the web implementation (track K). +The test task already passes the fixtures location as a system property: + +```kotlin +// core/protocol/build.gradle.kts +tasks.test { + systemProperty("hapi.fixtures.dir", rootDir.parentFile.resolve("shared/fixtures").absolutePath) +} +``` + +Fixture-driven tests (M2) read `System.getProperty("hapi.fixtures.dir")` — +no further build changes are needed when `shared/fixtures/**` lands. CI +re-runs this suite whenever `android/**` or `shared/fixtures/**` change. + +## Building + +Requires an Android SDK for `:app`/`:core:data` (set `ANDROID_HOME` or +`android/local.properties` with `sdk.dir=...`). `:core:protocol` alone needs +only a JDK. + +```sh +cd android +./gradlew :core:protocol:test # pure JVM protocol tests (fast) +./gradlew :app:assembleDebug # debug APK +./gradlew :app:installDebug # install on a connected device +``` + +Without an Android SDK you can still run the protocol suite by configuring +only the needed projects: + +```sh +./gradlew --no-configuration-cache --configure-on-demand :core:protocol:test +``` + +CI (`.github/workflows/android.yml`) runs the protocol tests and +`:app:assembleDebug` on every PR touching `android/**` or `shared/fixtures/**`. + +## Milestones (track B of the native-clients plan) + +- **M0** — this scaffold: modules, version catalog, CI, placeholder screen. +- **M1** — foundations: wire types + modes catalog; auth + `HapiApi` (MockWebServer-tested); `SseEngine` reconnect state machine + versioned patches (gzip streaming verified); pairing UI + `hapicompanion://bind` deep link. +- **M2** — read-only chat: chat pipeline port gated on fixtures all-green; session list; `MessageWindowStore` port; Markdown renderer; read-only chat screen (`LazyColumn(reverseLayout = true)`). +- **M3** — interaction: composer (optimistic send/queue/steer/drafts), permission approvals UX, session controls (mode/model/abort/resume/rename/archive), new session, dictation. +- **M4** — FCM push (register → notification actions via expedited WorkManager) + files/git viewer, Scratchlist, usage/storage stats. +- **M5** — polish: zh-CN i18n, OLED/Material You theming, predictive back, LeakCanary pass, Play listing + self-build docs. + +## Firebase / push (self-build note) + +M0 deliberately does **not** apply the `com.google.gms.google-services` +plugin and has no Firebase dependency, so the project builds without any +`google-services.json`. In M4a the plugin lands together with the FCM +service: official builds inject the default Firebase project config in CI, +while self-builders drop in their own `app/google-services.json` (docs will +accompany M4a; a `PushBinding` seam for hub-provided `FirebaseOptions` is +planned for v1.x). diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts new file mode 100644 index 00000000..99a21d70 --- /dev/null +++ b/android/app/build.gradle.kts @@ -0,0 +1,63 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget + +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.kotlin.compose) + // NOTE: com.google.gms.google-services is deliberately NOT applied in M0. + // It is added in M4a together with google-services.json + the FCM service, + // so the scaffold builds green without any Firebase project configured. +} + +android { + namespace = "app.hapi.companion" + compileSdk = 36 + + defaultConfig { + applicationId = "run.hapi.companion" + minSdk = 26 + targetSdk = 36 + versionCode = 1 + versionName = "0.1.0" + } + + buildTypes { + release { + isMinifyEnabled = true + isShrinkResources = true + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } + + buildFeatures { + compose = true + } +} + +kotlin { + compilerOptions { + jvmTarget.set(JvmTarget.JVM_17) + } +} + +dependencies { + implementation(project(":core:protocol")) + implementation(project(":core:data")) + + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.activity.compose) + + implementation(platform(libs.androidx.compose.bom)) + implementation(libs.androidx.compose.ui) + implementation(libs.androidx.compose.material3) + implementation(libs.androidx.compose.ui.tooling.preview) + debugImplementation(libs.androidx.compose.ui.tooling) +} diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro new file mode 100644 index 00000000..dcccef4d --- /dev/null +++ b/android/app/proguard-rules.pro @@ -0,0 +1,3 @@ +# App-specific R8/ProGuard rules. +# kotlinx.serialization ships its own consumer rules; add rules here only when a +# release build actually needs them (verified via :app:assembleRelease + smoke test). diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..359d0d7f --- /dev/null +++ b/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/android/app/src/main/kotlin/app/hapi/companion/AppGraph.kt b/android/app/src/main/kotlin/app/hapi/companion/AppGraph.kt new file mode 100644 index 00000000..cc445280 --- /dev/null +++ b/android/app/src/main/kotlin/app/hapi/companion/AppGraph.kt @@ -0,0 +1,15 @@ +package app.hapi.companion + +/** + * Hand-rolled dependency graph seed (no Hilt/Dagger by design -- the app wires + * roughly 15 long-lived types, which does not justify a DI framework). + * + * From M1 on this becomes a class instantiated in `HapiApplication.onCreate()` + * that owns the singletons in construction order: + * CredentialStore -> AuthManager -> HapiApi -> SseEngine -> stores + * (session list / session detail / message window) -> push registration. + * Compose reads it via a CompositionLocal; workers/services reach it through + * the Application instance. Everything behind it stays constructor-injected + * and unit-testable without this object. + */ +object AppGraph diff --git a/android/app/src/main/kotlin/app/hapi/companion/MainActivity.kt b/android/app/src/main/kotlin/app/hapi/companion/MainActivity.kt new file mode 100644 index 00000000..3633f306 --- /dev/null +++ b/android/app/src/main/kotlin/app/hapi/companion/MainActivity.kt @@ -0,0 +1,69 @@ +package app.hapi.companion + +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.enableEdgeToEdge +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.height +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import app.hapi.companion.ui.theme.HapiTheme +import app.hapi.protocol.wire.SUPPORTED_PROTOCOL_VERSION + +/** + * Single-activity entry point. M1d replaces the placeholder with the pairing + * flow (QR scan + `hapicompanion://bind` deep link handling -- the intent + * filter is already declared in the manifest) and Navigation Compose. + */ +class MainActivity : ComponentActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContent { + HapiTheme { + PlaceholderScreen() + } + } + } +} + +@Composable +private fun PlaceholderScreen() { + Surface(modifier = Modifier.fillMaxSize()) { + Column( + modifier = Modifier.fillMaxSize(), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally, + ) { + Text( + text = "HAPI", + style = MaterialTheme.typography.displayLarge, + color = MaterialTheme.colorScheme.primary, + ) + Spacer(modifier = Modifier.height(8.dp)) + Text( + text = "Companion scaffold · protocol v$SUPPORTED_PROTOCOL_VERSION", + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + } +} + +@Preview(showBackground = true) +@Composable +private fun PlaceholderScreenPreview() { + HapiTheme { + PlaceholderScreen() + } +} diff --git a/android/app/src/main/kotlin/app/hapi/companion/ui/theme/Theme.kt b/android/app/src/main/kotlin/app/hapi/companion/ui/theme/Theme.kt new file mode 100644 index 00000000..2c11f192 --- /dev/null +++ b/android/app/src/main/kotlin/app/hapi/companion/ui/theme/Theme.kt @@ -0,0 +1,51 @@ +package app.hapi.companion.ui.theme + +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext + +private val LightColorScheme = lightColorScheme( + primary = Color(0xFF3D6837), + secondary = Color(0xFF54634D), + tertiary = Color(0xFF386569), +) + +private val DarkColorScheme = darkColorScheme( + primary = Color(0xFFA3D397), + secondary = Color(0xFFBCCBB2), + tertiary = Color(0xFFA0CFD2), +) + +/** + * Material3 theme for the HAPI companion app. + * + * Dynamic color (Material You) on Android 12+, static fallback schemes below. + * M5 revisits this for OLED-black surfaces and full token coverage. + */ +@Composable +fun HapiTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + dynamicColor: Boolean = true, + content: @Composable () -> Unit, +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + content = content, + ) +} diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml new file mode 100644 index 00000000..972ceafb --- /dev/null +++ b/android/app/src/main/res/values/strings.xml @@ -0,0 +1,4 @@ + + + HAPI + diff --git a/android/app/src/main/res/values/themes.xml b/android/app/src/main/res/values/themes.xml new file mode 100644 index 00000000..2da5ac2a --- /dev/null +++ b/android/app/src/main/res/values/themes.xml @@ -0,0 +1,6 @@ + + + +