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  *      https://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.MediatorLiveData
23 import androidx.lifecycle.MutableLiveData
24 import com.android.packageinstaller.v2.model.UninstallRepository
25 import com.android.packageinstaller.v2.model.UninstallStage
26 
27 class UninstallViewModel(application: Application, val repository: UninstallRepository) :
28     AndroidViewModel(application) {
29 
30     companion object {
31         private val LOG_TAG = UninstallViewModel::class.java.simpleName
32     }
33 
34     private val _currentUninstallStage = MediatorLiveData<UninstallStage>()
35     val currentUninstallStage: MutableLiveData<UninstallStage>
36         get() = _currentUninstallStage
37 
38     fun preprocessIntent(intent: Intent, callerInfo: UninstallRepository.CallerInfo) {
39         var stage = repository.performPreUninstallChecks(intent, callerInfo)
40         if (stage.stageCode != UninstallStage.STAGE_ABORTED) {
41             stage = repository.generateUninstallDetails()
42         }
43         _currentUninstallStage.value = stage
44     }
45 
46     fun initiateUninstall(keepData: Boolean) {
47         repository.initiateUninstall(keepData)
48         // Since uninstall is an async operation, we will get the uninstall result later in time.
49         // Result of the uninstall will be set in UninstallRepository#mUninstallResult.
50         // As such, _currentUninstallStage will need to add another MutableLiveData
51         // as a data source
52         _currentUninstallStage.addSource(repository.uninstallResult) { uninstallStage: UninstallStage? ->
53             if (uninstallStage != null) {
54                 _currentUninstallStage.value = uninstallStage
55             }
56         }
57     }
58 
59     fun cancelUninstall() {
60         repository.cancelUninstall()
61     }
62 }
63