From af1a74b084cf67cb93c83ba083ea6a5362142b5e Mon Sep 17 00:00:00 2001 From: Jiyong Park Date: Wed, 7 Jul 2021 12:23:52 +0900 Subject: [PATCH] Add API for configuring a VM to run in debug mode Bug: 185211964 Test: run the demo app Change-Id: I960839037b2f23dbce1552199d9c9e59c36053e2 --- .../android/microdroid/demo/MainActivity.java | 72 +++++++++++++++++-- demo/res/layout/activity_main.xml | 43 ++++++++--- .../virtualmachine/VirtualMachineConfig.java | 21 +++++- 3 files changed, 118 insertions(+), 18 deletions(-) diff --git a/demo/java/com/android/microdroid/demo/MainActivity.java b/demo/java/com/android/microdroid/demo/MainActivity.java index 976e37e8..6373b55c 100644 --- a/demo/java/com/android/microdroid/demo/MainActivity.java +++ b/demo/java/com/android/microdroid/demo/MainActivity.java @@ -22,6 +22,10 @@ import android.system.virtualmachine.VirtualMachine; import android.system.virtualmachine.VirtualMachineConfig; import android.system.virtualmachine.VirtualMachineException; import android.system.virtualmachine.VirtualMachineManager; +import android.view.View; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.ScrollView; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; @@ -47,38 +51,77 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + TextView consoleView = (TextView) findViewById(R.id.consoleOutput); + Button runStopButton = (Button) findViewById(R.id.runStopButton); + ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview); - // Whenthe console model is updated, append the new line to the text view. - TextView view = (TextView) findViewById(R.id.textview); + // When the console model is updated, append the new line to the text view. VirtualMachineModel model = new ViewModelProvider(this).get(VirtualMachineModel.class); model.getConsoleOutput() .observeForever( new Observer() { @Override public void onChanged(String line) { - view.append(line + "\n"); + consoleView.append(line + "\n"); + scrollView.fullScroll(View.FOCUS_DOWN); } }); + + // When the VM status is updated, change the label of the button + model.getStatus() + .observeForever( + new Observer() { + @Override + public void onChanged(VirtualMachine.Status status) { + if (status == VirtualMachine.Status.RUNNING) { + runStopButton.setText("Stop"); + } else { + runStopButton.setText("Run"); + consoleView.setText(""); + } + } + }); + + // When the button is clicked, run or stop the VM + runStopButton.setOnClickListener( + new View.OnClickListener() { + public void onClick(View v) { + if (model.getStatus().getValue() == VirtualMachine.Status.RUNNING) { + model.stop(); + } else { + CheckBox debugModeCheckBox = (CheckBox) findViewById(R.id.debugMode); + final boolean debug = debugModeCheckBox.isChecked(); + model.run(debug); + } + } + }); } /** Models a virtual machine and console output from it. */ public static class VirtualMachineModel extends AndroidViewModel { - private final VirtualMachine mVirtualMachine; + private VirtualMachine mVirtualMachine; private final MutableLiveData mConsoleOutput = new MutableLiveData<>(); + private final MutableLiveData mStatus = new MutableLiveData<>(); public VirtualMachineModel(Application app) { super(app); + mStatus.setValue(VirtualMachine.Status.DELETED); + } + /** Runs a VM */ + public void run(boolean debug) { // Create a VM and run it. // TODO(jiyong): remove the call to idsigPath try { - VirtualMachineConfig config = + VirtualMachineConfig.Builder builder = new VirtualMachineConfig.Builder(getApplication(), "assets/vm_config.json") .idsigPath("/data/local/tmp/virt/MicrodroidDemoApp.apk.idsig") - .build(); + .debugMode(debug); + VirtualMachineConfig config = builder.build(); VirtualMachineManager vmm = VirtualMachineManager.getInstance(getApplication()); mVirtualMachine = vmm.create("demo_vm", config); mVirtualMachine.run(); + mStatus.postValue(mVirtualMachine.getStatus()); } catch (VirtualMachineException e) { throw new RuntimeException(e); } @@ -105,8 +148,25 @@ public class MainActivity extends AppCompatActivity { }); } + /** Stops the running VM */ + public void stop() { + try { + mVirtualMachine.stop(); + } catch (VirtualMachineException e) { + // Consume + } + mVirtualMachine = null; + mStatus.postValue(VirtualMachine.Status.STOPPED); + } + + /** Returns the console output from the VM */ public LiveData getConsoleOutput() { return mConsoleOutput; } + + /** Returns the status of the VM */ + public LiveData getStatus() { + return mStatus; + } } } diff --git a/demo/res/layout/activity_main.xml b/demo/res/layout/activity_main.xml index 026382f8..cd30f353 100644 --- a/demo/res/layout/activity_main.xml +++ b/demo/res/layout/activity_main.xml @@ -9,18 +9,43 @@ android:textAlignment="textStart" tools:context=".MainActivity"> - + android:layout_height="match_parent" + android:orientation="vertical"> - - + android:orientation="horizontal"> + +