1 /* <lambda>null2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.packageinstaller.v2.viewmodel 18 19 import android.app.Application 20 import android.content.Intent 21 import androidx.lifecycle.AndroidViewModel 22 import androidx.lifecycle.LiveData 23 import androidx.lifecycle.MediatorLiveData 24 import androidx.lifecycle.MutableLiveData 25 import androidx.lifecycle.distinctUntilChanged 26 import com.android.packageinstaller.v2.model.InstallRepository 27 import com.android.packageinstaller.v2.model.InstallStage 28 import com.android.packageinstaller.v2.model.InstallStaging 29 30 class InstallViewModel(application: Application, val repository: InstallRepository) : 31 AndroidViewModel(application) { 32 33 companion object { 34 private val LOG_TAG = InstallViewModel::class.java.simpleName 35 } 36 37 private val _currentInstallStage = MediatorLiveData<InstallStage>(InstallStaging()) 38 val currentInstallStage: MutableLiveData<InstallStage> 39 get() = _currentInstallStage 40 41 init { 42 // Since installing is an async operation, we may get the install result later in time. 43 // Result of the installation will be set in InstallRepository#installResult. 44 // As such, currentInstallStage will need to add another MutableLiveData as a data source 45 _currentInstallStage.addSource( 46 repository.installResult.distinctUntilChanged() 47 ) { installStage: InstallStage? -> 48 if (installStage != null) { 49 _currentInstallStage.value = installStage 50 } 51 } 52 } 53 54 fun preprocessIntent(intent: Intent, callerInfo: InstallRepository.CallerInfo) { 55 val stage = repository.performPreInstallChecks(intent, callerInfo) 56 if (stage.stageCode == InstallStage.STAGE_ABORTED) { 57 _currentInstallStage.value = stage 58 } else { 59 // Since staging is an async operation, we will get the staging result later in time. 60 // Result of the file staging will be set in InstallRepository#mStagingResult. 61 // As such, mCurrentInstallStage will need to add another MutableLiveData 62 // as a data source 63 repository.stageForInstall() 64 _currentInstallStage.addSource(repository.stagingResult) { installStage: InstallStage -> 65 if (installStage.stageCode != InstallStage.STAGE_READY) { 66 _currentInstallStage.value = installStage 67 } else { 68 checkIfAllowedAndInitiateInstall() 69 } 70 } 71 } 72 } 73 74 val stagingProgress: LiveData<Int> 75 get() = repository.stagingProgress 76 77 private fun checkIfAllowedAndInitiateInstall() { 78 val stage = repository.requestUserConfirmation() 79 if (stage != null) { 80 _currentInstallStage.value = stage 81 } 82 } 83 84 fun forcedSkipSourceCheck() { 85 val stage = repository.forcedSkipSourceCheck() 86 if (stage != null) { 87 _currentInstallStage.value = stage 88 } 89 } 90 91 fun cleanupInstall() { 92 repository.cleanupInstall() 93 } 94 95 fun reattemptInstall() { 96 val stage = repository.reattemptInstall() 97 _currentInstallStage.value = stage 98 } 99 100 fun initiateInstall() { 101 repository.initiateInstall() 102 } 103 104 val stagedSessionId: Int 105 get() = repository.stagedSessionId 106 } 107