Further cleanup of the Launcher app

* remove unnecessary null checks
* group related statements close together
* rename local vars
* don't send lid open/close events; no longer needed as surface's
  lifecycle is changed
* don't keep the screen on

Bug: N/A
Test: N/A
Change-Id: I8e7ba66550e611c9b86a5adf87d794b286e1d3b2
diff --git a/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/Logger.java b/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/Logger.java
index 006476d..ccfff95 100644
--- a/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/Logger.java
+++ b/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/Logger.java
@@ -17,6 +17,7 @@
 package com.android.virtualization.vmlauncher;
 
 import android.system.virtualmachine.VirtualMachine;
+import android.system.virtualmachine.VirtualMachineConfig;
 import android.system.virtualmachine.VirtualMachineException;
 import android.util.Log;
 
@@ -40,6 +41,10 @@
     private Logger() {}
 
     static void setup(VirtualMachine vm, Path path, ExecutorService executor) {
+        if (vm.getConfig().getDebugLevel() == VirtualMachineConfig.DEBUG_LEVEL_FULL) {
+            return;
+        }
+
         try {
             InputStream console = vm.getConsoleOutput();
             OutputStream file = Files.newOutputStream(path, StandardOpenOption.CREATE);
diff --git a/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/MainActivity.java b/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/MainActivity.java
index bf032d3..7c7d7ef 100644
--- a/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/MainActivity.java
+++ b/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/MainActivity.java
@@ -31,9 +31,9 @@
 import android.util.Log;
 import android.view.SurfaceView;
 import android.view.View;
+import android.view.Window;
 import android.view.WindowInsets;
 import android.view.WindowInsetsController;
-import android.view.WindowManager;
 
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -74,8 +74,6 @@
         }
         checkAndRequestRecordAudioPermission();
         mExecutorService = Executors.newCachedThreadPool();
-        getWindow().setDecorFitsSystemWindows(false);
-        setContentView(R.layout.activity_main);
 
         ConfigJson json = ConfigJson.from(VM_CONFIG_PATH);
         VirtualMachineConfig config = json.toConfig(this);
@@ -94,30 +92,28 @@
                             finish();
                         });
 
-        SurfaceView surfaceView = findViewById(R.id.surface_view);
-        SurfaceView cursorSurfaceView = findViewById(R.id.cursor_surface_view);
-        View backgroundTouchView = findViewById(R.id.background_touch_view);
-        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+        // Setup UI
+        setContentView(R.layout.activity_main);
+        SurfaceView mainView = findViewById(R.id.surface_view);
+        SurfaceView cursorView = findViewById(R.id.cursor_surface_view);
+        View touchView = findViewById(R.id.background_touch_view);
+        makeFullscreen();
 
-        // Fullscreen:
-        WindowInsetsController windowInsetsController = surfaceView.getWindowInsetsController();
-        windowInsetsController.setSystemBarsBehavior(
+        // Connect the views to the VM
+        mInputForwarder = new InputForwarder(this, mVirtualMachine, touchView, mainView, mainView);
+        mDisplayProvider = new DisplayProvider(mainView, cursorView);
+
+        Path logPath = getFileStreamPath(mVirtualMachine.getName() + ".log").toPath();
+        Logger.setup(mVirtualMachine, logPath, mExecutorService);
+    }
+
+    private void makeFullscreen() {
+        Window w = getWindow();
+        w.setDecorFitsSystemWindows(false);
+        WindowInsetsController insetsCtrl = w.getInsetsController();
+        insetsCtrl.hide(WindowInsets.Type.systemBars());
+        insetsCtrl.setSystemBarsBehavior(
                 WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
-        windowInsetsController.hide(WindowInsets.Type.systemBars());
-
-        View touchReceiver = backgroundTouchView;
-        View mouseReceiver = surfaceView;
-        View keyReceiver = surfaceView;
-        mInputForwarder =
-                new InputForwarder(
-                        this, mVirtualMachine, touchReceiver, mouseReceiver, keyReceiver);
-
-        mDisplayProvider = new DisplayProvider(surfaceView, cursorSurfaceView);
-
-        if (config.getDebugLevel() == VirtualMachineConfig.DEBUG_LEVEL_FULL) {
-            Path logPath = getFileStreamPath("console.log").toPath();
-            Logger.setup(mVirtualMachine, logPath, mExecutorService);
-        }
     }
 
     @Override
@@ -135,35 +131,27 @@
     @Override
     protected void onStop() {
         super.onStop();
-        if (mVirtualMachine != null) {
-            try {
-                mVirtualMachine.sendLidEvent(/* close */ true);
-                mVirtualMachine.suspend();
-            } catch (VirtualMachineException e) {
-                Log.e(TAG, "Failed to suspend VM" + e);
-            }
+        try {
+            mVirtualMachine.suspend();
+        } catch (VirtualMachineException e) {
+            Log.e(TAG, "Failed to suspend VM" + e);
         }
     }
 
     @Override
     protected void onRestart() {
         super.onRestart();
-        if (mVirtualMachine != null) {
-            try {
-                mVirtualMachine.resume();
-                mVirtualMachine.sendLidEvent(/* close */ false);
-            } catch (VirtualMachineException e) {
-                Log.e(TAG, "Failed to resume VM" + e);
-            }
+        try {
+            mVirtualMachine.resume();
+        } catch (VirtualMachineException e) {
+            Log.e(TAG, "Failed to resume VM" + e);
         }
     }
 
     @Override
     protected void onDestroy() {
         super.onDestroy();
-        if (mExecutorService != null) {
-            mExecutorService.shutdownNow();
-        }
+        mExecutorService.shutdownNow();
         mInputForwarder.cleanUp();
         Log.d(TAG, "destroyed");
     }