# Inside an app: the Android library

The library lets the user of your Android app share the phone's connection with Coronium. Only that user turns it on, on a screen the library shows with the sharing notice unchanged. Your app cannot turn sharing on, hide it or bring it back after a no, may always turn it off, and may not reach into the library's inner classes. It is a preview with a proposed notice: until the owner approves it, the supply site enrols no device from an app, and an app that is not debuggable opens no consent screen. Calls go to https://supply.coronium.ai.

## Needs

- An app whose stated main purpose is sharing its user's connection: [store declarations](/guides/device-android-store.md) says where it may ship and what to declare.
- Your account's app token, minted as [enrolment by token](/guides/device-tokens.md) says and kept on your server, never in the app, and that server's HTTPS address for step 4.
- `/kit/index.json` listing an `aar` file: until it does, nothing is published.
- `minSdk` 26, `compileSdk` 34 or later, Kotlin 2.1 or later.
- A 64-bit app (ARM or x86-64): in a 32-bit process the library pauses and enrols nothing.
- No other gomobile library (`libgojni.so`) in the app.

## Steps

1. Add the repository, for the group `ai.coronium` only, and the dependency. Its permissions and components merge into your manifest, none exported.

```kotlin
// settings.gradle.kts, in dependencyResolutionManagement { repositories { ... } }
val supply = "https://supply.coronium.ai"
maven { url = uri("$supply/kit/maven/"); content { includeGroup("ai.coronium") } }

// app/build.gradle.kts
dependencies { implementation("ai.coronium:device:0.1.0") }
```

Name the SHA-256 of its `.aar`, `.module` and `.pom` from `/kit/SHA256SUMS` in `gradle/verification-metadata.xml`.

2. Initialize it once, in `Application.onCreate`. It starts nothing, and does nothing outside the app's main process. `dailyByteCap` may lower the notice's daily limit, never raise it.

```kotlin
Coronium.initialize(this, CoroniumConfig(enrolUrl = getString(R.string.enrol_url)))
```

3. Offer the library's screen and its settings from buttons of your own.

```kotlin
share.setOnClickListener { Coronium.showConsent(this) }
settings.setOnClickListener { Coronium.openSettings(this) }
```

Expect `Shown`, `AlreadyOn`, or `NoticeNotApproved` (a proposed notice in a build that is not debuggable). `Turn sharing on` makes the install's random id and secret, asks for the notification permission and calls step 4; back, home or `Not now` is a no. `Coronium.state` follows sharing; `Coronium.turnOff()` turns it off.

4. On your server, send the library's `POST` of `{deviceId, secretSha256, app}` to the exchange with your token and the consent kept from the mint, and give the app Coronium's status and body unchanged:

```python
import json, os, urllib.error, urllib.request

SUPPLY = "https://supply.coronium.ai"

def enrol(body: bytes) -> tuple[int, bytes]:
    asked = json.loads(body)
    sent = {"secretSha256": asked["secretSha256"], "app": asked["app"], "consent": json.loads(os.environ["CORONIUM_CONSENT"])}
    request = urllib.request.Request(SUPPLY + "/app/api/devices/" + asked["deviceId"], method="PUT",
        data=json.dumps(sent).encode(), headers={"Authorization": "Bearer " + os.environ["CORONIUM_APP_TOKEN"], "Content-Type": "application/json"})
    try:
        with urllib.request.urlopen(request, timeout=30) as answer:
            return answer.status, answer.read()
    except urllib.error.HTTPError as refused:
        return refused.code, refused.read()
```

Expect `201`, and `200` on a repeat. The secret never leaves the phone. Keep the id beside your own user: rewards are yours to pay, and a reinstall is a new id.

## States

- `Off`: no yes for this notice, or turned off; only the settings switch turns it on again. Also when the supply site holds no record of the yes (a plain token's exchange): the notice is offered again.
- `Waiting(reasons)`: on, and the phone is not charging or its network is metered, mobile data, roaming, a VPN, not validated or gone. The notification says so.
- `Connecting`, then `Sharing`, with the notification and its `Turn off`, which comes back if swiped away.
- `Paused(reason, code, cause, fix)`: the daily limit, notifications refused, a refusal of enrolment or the relay, or a phone that cannot run the core or keep its files; the next resumed screen tries again.
- `Withdrawn(by)`: removed by the user, by you, by a revoked token or by Coronium. A new yes makes a new id.

## Limits

- At most 2 GB a day, counted on the phone as a stop and sent nowhere, and `Limits.MAX_STREAMS` connections at once. The edge alone meters.
- After a crash, an update or a restart it comes back only at the app's next resumed activity. A force stop counts as `Turn off`.

## Each file

The library's state and limits are in `/kit/index.json`, and `/kit/APP-NOTICE.md` is its notice, byte for byte. It was run end to end on a development Android instance, never on a phone.

## Reinstall and removing a device

When your user enrols again with a new id, or leaves, withdraw the old id from your server as [device states](/guides/device-states.md) says. The user's own `Withdraw` removes the phone without you.
