[piav2] Cleanup CL to prepare for single user-confirmation dialog change

This CL attempts to cleanup the code in PIAV2 to account for nullable
InstallStage returned by InstallRepository

Bug: 319320680
Test: Install an APK manually
Change-Id: Ib09fb91e02978181d5d70f77a63ffde8e0c9f35f
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallRepository.kt b/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallRepository.kt
index f7752ff..1538ab9 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallRepository.kt
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallRepository.kt
@@ -861,7 +861,12 @@
             }
             _installResult.setValue(InstallSuccess(appSnippet, shouldReturnResult, resultIntent))
         } else {
-            _installResult.setValue(InstallFailed(appSnippet, statusCode, legacyStatus, message))
+            if (statusCode != PackageInstaller.STATUS_FAILURE_ABORTED) {
+                _installResult.setValue(InstallFailed(appSnippet, statusCode, legacyStatus, message))
+            } else {
+                _installResult.setValue(InstallAborted(ABORT_REASON_INTERNAL_ERROR))
+            }
+
         }
     }
 
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/v2/viewmodel/InstallViewModel.kt b/packages/PackageInstaller/src/com/android/packageinstaller/v2/viewmodel/InstallViewModel.kt
index 072fb2d..388e03f 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/v2/viewmodel/InstallViewModel.kt
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/v2/viewmodel/InstallViewModel.kt
@@ -22,6 +22,7 @@
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.MediatorLiveData
 import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.distinctUntilChanged
 import com.android.packageinstaller.v2.model.InstallRepository
 import com.android.packageinstaller.v2.model.InstallStage
 import com.android.packageinstaller.v2.model.InstallStaging
@@ -37,6 +38,19 @@
     val currentInstallStage: MutableLiveData<InstallStage>
         get() = _currentInstallStage
 
+    init {
+        // Since installing is an async operation, we may get the install result later in time.
+        // Result of the installation will be set in InstallRepository#installResult.
+        // As such, currentInstallStage will need to add another MutableLiveData as a data source
+        _currentInstallStage.addSource(
+            repository.installResult.distinctUntilChanged()
+        ) { installStage: InstallStage? ->
+            if (installStage != null) {
+                _currentInstallStage.value = installStage
+            }
+        }
+    }
+
     fun preprocessIntent(intent: Intent, callerInfo: InstallRepository.CallerInfo) {
         val stage = repository.performPreInstallChecks(intent, callerInfo)
         if (stage.stageCode == InstallStage.STAGE_ABORTED) {
@@ -62,12 +76,16 @@
 
     private fun checkIfAllowedAndInitiateInstall() {
         val stage = repository.requestUserConfirmation()
-        _currentInstallStage.value = stage
+        if (stage != null) {
+            _currentInstallStage.value = stage
+        }
     }
 
     fun forcedSkipSourceCheck() {
         val stage = repository.forcedSkipSourceCheck()
-        _currentInstallStage.value = stage
+        if (stage != null) {
+            _currentInstallStage.value = stage
+        }
     }
 
     fun cleanupInstall() {
@@ -80,15 +98,7 @@
     }
 
     fun initiateInstall() {
-        // Since installing is an async operation, we will get the install result later in time.
-        // Result of the installation will be set in InstallRepository#mInstallResult.
-        // As such, mCurrentInstallStage will need to add another MutableLiveData as a data source
         repository.initiateInstall()
-        _currentInstallStage.addSource(repository.installResult) { installStage: InstallStage? ->
-            if (installStage != null) {
-                _currentInstallStage.value = installStage
-            }
-        }
     }
 
     val stagedSessionId: Int