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 // Since staging is an async operation, we will get the staging result later in time. 54 // Result of the file staging will be set in InstallRepository#mStagingResult. 55 // As such, mCurrentInstallStage will need to add another MutableLiveData 56 // as a data source 57 _currentInstallStage.addSource( 58 repository.stagingResult.distinctUntilChanged() 59 ) { installStage: InstallStage -> 60 if (installStage.stageCode != InstallStage.STAGE_READY) { 61 _currentInstallStage.value = installStage 62 } else { 63 checkIfAllowedAndInitiateInstall() 64 } 65 } 66 } 67 68 fun preprocessIntent(intent: Intent, callerInfo: InstallRepository.CallerInfo) { 69 val stage = repository.performPreInstallChecks(intent, callerInfo) 70 if (stage.stageCode == InstallStage.STAGE_ABORTED) { 71 _currentInstallStage.value = stage 72 } else { 73 repository.stageForInstall() 74 } 75 } 76 77 val stagingProgress: LiveData<Int> 78 get() = repository.stagingProgress 79 80 private fun checkIfAllowedAndInitiateInstall() { 81 val stage = repository.requestUserConfirmation() 82 if (stage != null) { 83 _currentInstallStage.value = stage 84 } 85 } 86 87 fun forcedSkipSourceCheck() { 88 val stage = repository.forcedSkipSourceCheck() 89 if (stage != null) { 90 _currentInstallStage.value = stage 91 } 92 } 93 94 fun cleanupInstall() { 95 repository.cleanupInstall() 96 } 97 98 fun reattemptInstall() { 99 val stage = repository.reattemptInstall() 100 _currentInstallStage.value = stage 101 } 102 103 fun initiateInstall() { 104 repository.initiateInstall() 105 } 106 107 val stagedSessionId: Int 108 get() = repository.stagedSessionId 109 } 110