Add microdroid demo app
Add a demo app showing how an app can start a VM.
Bug: N/A
Test: TARGET_BUILD_APPS=MicrodroidDemoApp m apps_only dist
adb install out/dist/MicrodroidDemoApp.apk
adb push out/dist/MicrodroidDemoApp.apk.idsig /data/local/tmp/virt/
adb root
adb shell start virtualizationservice
<then launch the app>
Change-Id: If46b2f8910b98b2b29ca0c629e1f7eacc01a477a
diff --git a/demo/Android.bp b/demo/Android.bp
new file mode 100644
index 0000000..77049de
--- /dev/null
+++ b/demo/Android.bp
@@ -0,0 +1,21 @@
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_app {
+ name: "MicrodroidDemoApp",
+ srcs: ["java/**/*.java"],
+ resource_dirs: ["res"],
+ static_libs: [
+ "androidx-constraintlayout_constraintlayout",
+ "androidx.appcompat_appcompat",
+ "com.google.android.material_material",
+ ],
+ libs: [
+ "android.system.virtualmachine",
+ ],
+ jni_libs: ["MicrodroidTestNativeLib"],
+ platform_apis: true,
+ use_embedded_native_libs: true,
+ v4_signature: true,
+}
diff --git a/demo/AndroidManifest.xml b/demo/AndroidManifest.xml
new file mode 100644
index 0000000..ae4f734
--- /dev/null
+++ b/demo/AndroidManifest.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.microdroid.demo">
+
+ <application
+ android:label="MicrodroidDemo"
+ android:theme="@style/Theme.MicrodroidDemo">
+ <uses-library android:name="android.system.virtualmachine" android:required="true" />
+ <activity android:name=".MainActivity" android:exported="true">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+
+</manifest>
diff --git a/demo/assets/vm_config.json b/demo/assets/vm_config.json
new file mode 100644
index 0000000..b814394
--- /dev/null
+++ b/demo/assets/vm_config.json
@@ -0,0 +1,13 @@
+{
+ "os": {
+ "name": "microdroid"
+ },
+ "task": {
+ "type": "microdroid_launcher",
+ "command": "MicrodroidTestNativeLib.so",
+ "args": [
+ "hello",
+ "microdroid"
+ ]
+ }
+}
diff --git a/demo/java/com/android/microdroid/demo/MainActivity.java b/demo/java/com/android/microdroid/demo/MainActivity.java
new file mode 100644
index 0000000..976e37e
--- /dev/null
+++ b/demo/java/com/android/microdroid/demo/MainActivity.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.microdroid.demo;
+
+import android.app.Application;
+import android.os.Bundle;
+import android.system.virtualmachine.VirtualMachine;
+import android.system.virtualmachine.VirtualMachineConfig;
+import android.system.virtualmachine.VirtualMachineException;
+import android.system.virtualmachine.VirtualMachineManager;
+import android.widget.TextView;
+
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.lifecycle.AndroidViewModel;
+import androidx.lifecycle.LiveData;
+import androidx.lifecycle.MutableLiveData;
+import androidx.lifecycle.Observer;
+import androidx.lifecycle.ViewModelProvider;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * This app is to demonstrate the use of APIs in the android.system.virtualmachine library.
+ * Currently, this app starts a virtual machine running Microdroid and shows the console output from
+ * the virtual machine to the UI.
+ */
+public class MainActivity extends AppCompatActivity {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+
+ // Whenthe console model is updated, append the new line to the text view.
+ TextView view = (TextView) findViewById(R.id.textview);
+ VirtualMachineModel model = new ViewModelProvider(this).get(VirtualMachineModel.class);
+ model.getConsoleOutput()
+ .observeForever(
+ new Observer<String>() {
+ @Override
+ public void onChanged(String line) {
+ view.append(line + "\n");
+ }
+ });
+ }
+
+ /** Models a virtual machine and console output from it. */
+ public static class VirtualMachineModel extends AndroidViewModel {
+ private final VirtualMachine mVirtualMachine;
+ private final MutableLiveData<String> mConsoleOutput = new MutableLiveData<>();
+
+ public VirtualMachineModel(Application app) {
+ super(app);
+
+ // Create a VM and run it.
+ // TODO(jiyong): remove the call to idsigPath
+ try {
+ VirtualMachineConfig config =
+ new VirtualMachineConfig.Builder(getApplication(), "assets/vm_config.json")
+ .idsigPath("/data/local/tmp/virt/MicrodroidDemoApp.apk.idsig")
+ .build();
+ VirtualMachineManager vmm = VirtualMachineManager.getInstance(getApplication());
+ mVirtualMachine = vmm.create("demo_vm", config);
+ mVirtualMachine.run();
+ } catch (VirtualMachineException e) {
+ throw new RuntimeException(e);
+ }
+
+ // Read console output from the VM in the background
+ ExecutorService executorService = Executors.newFixedThreadPool(1);
+ executorService.execute(
+ new Runnable() {
+ @Override
+ public void run() {
+ try {
+ BufferedReader reader =
+ new BufferedReader(
+ new InputStreamReader(
+ mVirtualMachine.getConsoleOutputStream()));
+ while (true) {
+ String line = reader.readLine();
+ mConsoleOutput.postValue(line);
+ }
+ } catch (IOException | VirtualMachineException e) {
+ // Consume
+ }
+ }
+ });
+ }
+
+ public LiveData<String> getConsoleOutput() {
+ return mConsoleOutput;
+ }
+ }
+}
diff --git a/demo/res/layout/activity_main.xml b/demo/res/layout/activity_main.xml
new file mode 100644
index 0000000..026382f
--- /dev/null
+++ b/demo/res/layout/activity_main.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="#FFC107"
+ android:scrollbars="horizontal|vertical"
+ android:textAlignment="textStart"
+ tools:context=".MainActivity">
+
+ <ScrollView
+ android:id="@+id/scrollview"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <TextView
+ android:id="@+id/textview"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:background="#FFEB3B"
+ android:fontFamily="monospace"
+ android:textColor="#000000" />
+ </ScrollView>
+
+</androidx.constraintlayout.widget.ConstraintLayout>
diff --git a/demo/res/values/colors.xml b/demo/res/values/colors.xml
new file mode 100644
index 0000000..f8c6127
--- /dev/null
+++ b/demo/res/values/colors.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <color name="purple_200">#FFBB86FC</color>
+ <color name="purple_500">#FF6200EE</color>
+ <color name="purple_700">#FF3700B3</color>
+ <color name="teal_200">#FF03DAC5</color>
+ <color name="teal_700">#FF018786</color>
+ <color name="black">#FF000000</color>
+ <color name="white">#FFFFFFFF</color>
+</resources>
\ No newline at end of file
diff --git a/demo/res/values/themes.xml b/demo/res/values/themes.xml
new file mode 100644
index 0000000..16b8ab3
--- /dev/null
+++ b/demo/res/values/themes.xml
@@ -0,0 +1,16 @@
+<resources xmlns:tools="http://schemas.android.com/tools">
+ <!-- Base application theme. -->
+ <style name="Theme.MicrodroidDemo" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
+ <!-- Primary brand color. -->
+ <item name="colorPrimary">@color/purple_500</item>
+ <item name="colorPrimaryVariant">@color/purple_700</item>
+ <item name="colorOnPrimary">@color/white</item>
+ <!-- Secondary brand color. -->
+ <item name="colorSecondary">@color/teal_200</item>
+ <item name="colorSecondaryVariant">@color/teal_700</item>
+ <item name="colorOnSecondary">@color/black</item>
+ <!-- Status bar color. -->
+ <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
+ <!-- Customize your theme here. -->
+ </style>
+</resources>