clearDisplayService before launching the vm

clearDisplayService is called when display service is destroyed, but
sometimes there is a timing issue. so call clearDisplayService to ensure
the previous service is removed. this code will be also removed when
rust-based display service is landing.

Bug: 331708504
Test: check if the display shows after killing and then re-running app
Change-Id: I13b3758222da2b0f4d40ee6c45b03ac7a200ed26
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;