Allow the VM callback to be cleared

When a client no longer wants to receive callbacks about the VM, they
can clear the callback. This allows the client to shutdown their
ExecutorService without any more callbacks trying, and failing, to make
use of it again.

Bug: 220700986
Test: atest MicrodroidTests
Change-Id: Ib2c418a170c080764fde83e08f389982772e7cfc
diff --git a/javalib/src/android/system/virtualmachine/VirtualMachine.java b/javalib/src/android/system/virtualmachine/VirtualMachine.java
index 14ff111..01052c4 100644
--- a/javalib/src/android/system/virtualmachine/VirtualMachine.java
+++ b/javalib/src/android/system/virtualmachine/VirtualMachine.java
@@ -138,7 +138,7 @@
     private @Nullable VirtualMachineCallback mCallback;
 
     /** The executor on which the callback will be executed */
-    private @NonNull Executor mCallbackExecutor;
+    private @Nullable Executor mCallbackExecutor;
 
     private @Nullable ParcelFileDescriptor mConsoleReader;
     private @Nullable ParcelFileDescriptor mConsoleWriter;
@@ -298,11 +298,18 @@
      */
     public void setCallback(
             @NonNull @CallbackExecutor Executor executor,
-            @Nullable VirtualMachineCallback callback) {
+            @NonNull VirtualMachineCallback callback) {
         mCallbackExecutor = executor;
         mCallback = callback;
     }
 
+    /** Clears the currently registered callback. */
+    public void clearCallback() {
+        // TODO(b/220730550): synchronize with the callers of the callback
+        mCallback = null;
+        mCallbackExecutor = null;
+    }
+
     /** Returns the currently registered callback. */
     public @Nullable VirtualMachineCallback getCallback() {
         return mCallback;
diff --git a/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java b/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
index 14ea402..cd9f284 100644
--- a/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
+++ b/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
@@ -143,6 +143,7 @@
 
         void forceStop(VirtualMachine vm) {
             try {
+                vm.clearCallback();
                 vm.stop();
                 mExecutorService.shutdown();
             } catch (VirtualMachineException e) {