Clear task when VM shuts down
note: finishAffinity doesn't work because the vm launcher app and the
ferrochrome app have different affinities (although they are in the same
task).
Bug: 348304018
Test: run a VM, shut it down, check the all activities are finished and
the task is closed.
Change-Id: If421872c9bb71feac0c65a21c12b2fe497f10aaf
diff --git a/ferrochrome_app/java/com/android/virtualization/ferrochrome/FerrochromeActivity.java b/ferrochrome_app/java/com/android/virtualization/ferrochrome/FerrochromeActivity.java
index d9e5229..7c18537 100644
--- a/ferrochrome_app/java/com/android/virtualization/ferrochrome/FerrochromeActivity.java
+++ b/ferrochrome_app/java/com/android/virtualization/ferrochrome/FerrochromeActivity.java
@@ -56,6 +56,7 @@
private static final Path IMAGE_VERSION_INFO =
Path.of(EXTERNAL_STORAGE_DIR + "ferrochrome_image_version");
private static final Path VM_CONFIG_PATH = Path.of(EXTERNAL_STORAGE_DIR + "vm_config.json");
+ private static final int REQUEST_CODE_VMLAUNCHER = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -109,10 +110,17 @@
}
updateStatus("Done.");
updateStatus("Starting Ferrochrome...");
- runOnUiThread(() -> startActivity(intent));
+ runOnUiThread(() -> startActivityForResult(intent, REQUEST_CODE_VMLAUNCHER));
});
}
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ if (requestCode == REQUEST_CODE_VMLAUNCHER) {
+ finishAndRemoveTask();
+ }
+ }
+
private void updateStatus(String line) {
Log.d(TAG, line);
runOnUiThread(
diff --git a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
index c2f218a..3ef8dfe 100644
--- a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
+++ b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
@@ -235,41 +235,34 @@
@Override
public void onPayloadStarted(VirtualMachine vm) {
- Log.e(TAG, "payload start");
+ // This event is only from Microdroid-based VM. Custom VM shouldn't emit
+ // this.
}
@Override
public void onPayloadReady(VirtualMachine vm) {
- // This check doesn't 100% prevent race condition or UI hang.
- // However, it's fine for demo.
- if (mService.isShutdown()) {
- return;
- }
- Log.d(TAG, "(Payload is ready. Testing VM service...)");
+ // This event is only from Microdroid-based VM. Custom VM shouldn't emit
+ // this.
}
@Override
public void onPayloadFinished(VirtualMachine vm, int exitCode) {
- // This check doesn't 100% prevent race condition, but is fine for demo.
- if (!mService.isShutdown()) {
- Log.d(
- TAG,
- String.format("(Payload finished. exit code: %d)", exitCode));
- }
+ // This event is only from Microdroid-based VM. Custom VM shouldn't emit
+ // this.
}
@Override
public void onError(VirtualMachine vm, int errorCode, String message) {
- Log.d(
- TAG,
- String.format(
- "(Error occurred. code: %d, message: %s)",
- errorCode, message));
+ Log.e(TAG, "Error from VM. code: " + errorCode + " (" + message + ")");
+ setResult(RESULT_CANCELED);
+ finish();
}
@Override
public void onStopped(VirtualMachine vm, int reason) {
- Log.e(TAG, "vm stop");
+ Log.d(TAG, "VM stopped. Reason: " + reason);
+ setResult(RESULT_OK);
+ finish();
}
};