# App Management System

The device-side app management system handles installation, updates, and launching of third-party apps.

## Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                      AppManager                              │
│  - Install/Uninstall apps from .mosis packages              │
│  - Track installed apps in JSON registry                    │
│  - Manage app data/cache directories                        │
└─────────────────────────┬───────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────────┐
│                   LuaSandboxManager                          │
│  - StartApp/StopApp lifecycle                               │
│  - Per-app isolated Lua environments                        │
│  - Resource limits (memory, CPU, timers)                    │
└─────────────────────────────────────────────────────────────┘
```

## App Package Format (.mosis)

Apps are distributed as ZIP files with `.mosis` extension:

```
myapp.mosis
├── manifest.json      # Required: app metadata
├── main.rml          # Entry point (RmlUi document)
├── styles.rcss       # Stylesheets
├── scripts/          # Lua scripts
│   └── app.lua
└── assets/           # Icons, images, etc.
```

## Manifest Format

```json
{
  "id": "com.example.myapp",
  "name": "My App",
  "version": "1.0.0",
  "version_code": 1,
  "entry": "main.rml",
  "icon": "icon.tga",
  "description": "App description",
  "developer": {
    "name": "Developer Name",
    "email": "dev@example.com"
  },
  "permissions": [
    "network",
    "storage",
    "camera"
  ],
  "min_api_version": 1
}
```

## App Lifecycle

```cpp
// Install from local file
app_manager->InstallFromFile("/path/to/app.mosis", [](auto progress) {
    LOG_INFO("Install progress: %s %.0f%%",
             InstallProgress::StageName(progress.stage),
             progress.progress * 100);
});

// Launch app (starts sandbox)
app_manager->LaunchApp("com.example.myapp");

// Check if running
bool running = app_manager->IsAppRunning("com.example.myapp");

// Stop app (cleanup sandbox)
app_manager->StopApp("com.example.myapp");

// Uninstall (stops if running, removes files)
app_manager->Uninstall("com.example.myapp", false);  // keep_data=false
```

## Directory Structure

```
/data/data/com.omixlab.mosis/files/
├── apps/
│   └── com.example.myapp/
│       ├── package/      # Extracted app files
│       ├── data/         # App persistent data (VirtualFS)
│       ├── cache/        # App cache (clearable)
│       └── db/           # SQLite databases
├── downloads/            # Temporary download location
├── backups/              # App data backups
└── config/
    └── apps.json         # Installed apps registry
```
