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.
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.
LauncherActivityjava/com/android/virtualization/terminal/LauncherActivity.kt
The LauncherActivity serves as the main entry point for the application. Its primary responsibility is to route the user to the appropriate UI implementation based on the system property ro.terminal.new_ui.enabled.
com.android.virtualization.terminal.MainActivity.com.android.virtualization.terminal.new2.ui.MainActivity.You need to enable the new feature flag to use the new UI:
adb root adb shell aflags enable com.android.system.virtualmachine.flags.terminal_newui_jetpack adb reboot
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.
new2/core)This package contains the core business logic, state management, and service integration.
VmController (VmController.kt)
VirtualMachineManager system service to create, start, and stop the VM.NsdManager to discover the port where the terminal service (ttyd) is running inside the VM.vmState (StateFlow<VmState>) to observe changes in VM status (Ready, Starting, Running, Stopped, Error).installState (StateFlow<InstallState>) to track progress (Checking, NotInstalled, Installing, Installed, Error).ForegroundService that ensures critical operations continue running even when the application is in the background.ACTION_INSTALL), starting the VM (ACTION_START), and stopping the VM (ACTION_STOP).Installer and VmController.WebView implementation (extending TerminalView) that renders the terminal interface.ttyd service running inside the guest VM.xterm.js events with Android.new2/ui)The UI layer is built using Jetpack Compose and follows the MVVM (Model-View-ViewModel) pattern.
MainActivity (ui/MainActivity.kt)
MaterialTheme and hosts the MainScreen.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.new2/ui/main)MainViewModel (ui/main/MainViewModel.kt)
Installer.installState and VmController.vmState into a single, unified MainUiState.installVm(), startVm(), and stopVm().TerminalViewModel (ui/main/TerminalViewModel.kt)
TerminalUiState).TtydView instance, ensuring it is attached to the correct DisplayContext.new2/util)LoggingMutableStateFlow (util/LoggingMutableStateFlow.kt)MutableStateFlow that logs all state changes to logcat. This is useful for debugging state transitions in the reactive architecture.