Merge "clearDisplayService before launching the vm" into main
diff --git a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
index 2af1e2f..e365c46 100644
--- a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
+++ b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
@@ -117,6 +117,14 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        try {
+            // To ensure that the previous display service is removed.
+            IVirtualizationServiceInternal.Stub.asInterface(
+                            ServiceManager.waitForService("android.system.virtualizationservice"))
+                    .clearDisplayService();
+        } catch (RemoteException e) {
+            Log.d(TAG, "failed to clearDisplayService");
+        }
         getWindow().setDecorFitsSystemWindows(false);
         setContentView(R.layout.activity_main);
         VirtualMachineCallback callback =
@@ -246,10 +254,9 @@
             RemoteExceptionCheckedFunction<ICrosvmAndroidDisplayService> func) {
         IVirtualizationServiceInternal vs =
                 IVirtualizationServiceInternal.Stub.asInterface(
-                        ServiceManager.getService("android.system.virtualizationservice"));
+                        ServiceManager.waitForService("android.system.virtualizationservice"));
         try {
-            assert vs != null;
-            Log.d(TAG, "wait for the service");
+            Log.d(TAG, "wait for the display service");
             ICrosvmAndroidDisplayService service =
                     ICrosvmAndroidDisplayService.Stub.asInterface(vs.waitDisplayService());
             assert service != null;