tree: d2467fff2b59bfa7ff7055a0b462510347ccc417 [search]
  1. aidl/
  2. assets/
  3. assets-arm64/
  4. assets-wayland/
  5. assets-x86_64/
  6. client/
  7. java/
  8. libandroid_rs/
  9. res/
  10. res-enabled/
  11. smol-usernet/
  12. tests/
  13. wayland-bridge/
  14. .gitignore
  15. .knowledge_id
  16. Android.bp
  17. AndroidManifest-tests.xml
  18. AndroidManifest.xml
  19. AndroidTest.xml
  20. fetch-cidata.sh
  21. jarjar-rules.txt
  22. proguard-test.flags
  23. proguard.flags
  24. README.md
  25. TerminalAppTests-precondition-checker.sh
  26. TEST.md
android/TerminalApp/README.md

Terminal App Architecture Guide

This document provides an overview of the code structure for the Android Terminal application, focusing on the new architecture located in the new2 package and the application entry point.

Development features

Custom image

On debuggable build, custom image can be placed at /sdcard/linux/images.tar.gz. The local image would be installed instead when TerminalApp needs installation.

Plus, /sdcard/linux/cidata.iso can be used to install cidata.iso outside of the images.tar.gz. If the file is empty, bundled cidata.iso (a.k.a. soong-generated CIDATA or AIDL guest agent) would be installed instead.

Entry Point

New Architecture (new2 package)

The [com.android.virtualization.terminal.new2] package contains the refactored application architecture, which utilizes Jetpack Compose for the UI and Kotlin Coroutines/Flow for state management.

Core Components (new2/core)

This package contains the core business logic, state management, and service integration.

  • VmController (VmController.kt)

    • A singleton object that manages the lifecycle of the Virtual Machine (VM).
    • Interacts with the VirtualMachineManager system service to create, start, and stop the VM.
    • Uses NsdManager to discover the port where the terminal service (ttyd) is running inside the VM.
    • Exposes a vmState (StateFlow<VmState>) to observe changes in VM status (Ready, Starting, Running, Stopped, Error).
  • Installer (Installer.kt)

    • A singleton object responsible for downloading and installing the Linux VM image.
    • Manages the installation process in the background.
    • Exposes an installState (StateFlow<InstallState>) to track progress (Checking, NotInstalled, Installing, Installed, Error).
  • VmService (VmService.kt)

    • A ForegroundService that ensures critical operations continue running even when the application is in the background.
    • Handles actions for installing the image (ACTION_INSTALL), starting the VM (ACTION_START), and stopping the VM (ACTION_STOP).
    • Delegates actual logic to Installer and VmController.
  • TtydView (TtydView.kt)

    • A custom WebView implementation (extending TerminalView) that renders the terminal interface.
    • Connects to the ttyd service running inside the guest VM.
    • Handles SSL/TLS connections, client certificate authentication, and JavaScript injection to bridge xterm.js events with Android.
  • InstallState & VmState

    • Sealed interfaces defining the finite states for the installation process and the VM lifecycle, respectively.

UI Layer (new2/ui)

The UI layer is built using Jetpack Compose and follows the MVVM (Model-View-ViewModel) pattern.

  • MainActivity (ui/MainActivity.kt)

    • The container Activity for the new Compose-based UI.
    • Sets up the MaterialTheme and hosts the MainScreen.
  • MainScreen (ui/MainScreen.kt)

    • The top-level Composable function.
    • Observes the MainUiState from MainViewModel and orchestrates navigation between different screens:
      • SplashScreen: Loading state.
      • InstallStartScreen: Prompts user to install the VM image.
      • InstallProgressScreen: Shows download/installation progress.
      • TerminalScreen: Displays the running terminal.
      • BootingScreen: Shows VM boot progress.

ViewModels (new2/ui/main)

Utilities (new2/util)

Terminal app test

This test wipes terminal app data and requires an active network connection.

For AOSP, run atest VmTerminalAppTests. For non-AOSP, run atest VmTerminalAppGoogleTests.