1 /*
2  * 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 package com.android.packageinstaller.v2.model
17 
18 import android.app.Activity
19 import android.app.Notification
20 import android.content.Intent
21 import com.android.packageinstaller.R
22 
23 sealed class UninstallStage(val stageCode: Int) {
24 
25     companion object {
26         const val STAGE_DEFAULT = -1
27         const val STAGE_ABORTED = 0
28         const val STAGE_READY = 1
29         const val STAGE_USER_ACTION_REQUIRED = 2
30         const val STAGE_UNINSTALLING = 3
31         const val STAGE_SUCCESS = 4
32         const val STAGE_FAILED = 5
33     }
34 }
35 
36 class UninstallReady : UninstallStage(STAGE_READY)
37 
38 data class UninstallUserActionRequired(
39     val title: String? = null,
40     val message: String? = null,
41     val appDataSize: Long = 0,
42     val isArchive: Boolean = false
43 ) : UninstallStage(STAGE_USER_ACTION_REQUIRED)
44 
45 data class UninstallUninstalling(val appLabel: CharSequence, val isCloneUser: Boolean) :
46     UninstallStage(STAGE_UNINSTALLING)
47 
48 data class UninstallSuccess(
49     val resultIntent: Intent? = null,
50     val activityResultCode: Int = 0,
51     val message: String? = null,
52 ) : UninstallStage(STAGE_SUCCESS)
53 
54 data class UninstallFailed(
55     val returnResult: Boolean,
56     /**
57      * If the caller wants the result back, the intent will hold the uninstall failure status code
58      * and legacy code.
59      */
60     val resultIntent: Intent? = null,
61     val activityResultCode: Int = Activity.RESULT_CANCELED,
62     /**
63      * ID used to show [uninstallNotification]
64      */
65     val uninstallNotificationId: Int? = null,
66     /**
67      * When the user does not request a result back, this notification will be shown indicating the
68      * reason for uninstall failure.
69      */
70     val uninstallNotification: Notification? = null,
71 ) : UninstallStage(STAGE_FAILED) {
72 
73     init {
74         if (uninstallNotification != null && uninstallNotificationId == null) {
75             throw IllegalArgumentException(
76                 "uninstallNotification cannot be set without uninstallNotificationId"
77             )
78         }
79     }
80 }
81 
82 data class UninstallAborted(val abortReason: Int) : UninstallStage(STAGE_ABORTED) {
83 
84     var dialogTitleResource = 0
85     var dialogTextResource = 0
86     val activityResultCode = Activity.RESULT_FIRST_USER
87 
88     init {
89         when (abortReason) {
90             ABORT_REASON_APP_UNAVAILABLE -> {
91                 dialogTitleResource = R.string.app_not_found_dlg_title
92                 dialogTextResource = R.string.app_not_found_dlg_text
93             }
94 
95             ABORT_REASON_USER_NOT_ALLOWED -> {
96                 dialogTitleResource = 0
97                 dialogTextResource = R.string.user_is_not_allowed_dlg_text
98             }
99 
100             ABORT_REASON_UNINSTALL_DONE -> {
101                 dialogTitleResource = 0
102                 dialogTextResource = 0
103             }
104 
105             else -> {
106                 dialogTitleResource = 0
107                 dialogTextResource = R.string.generic_error_dlg_text
108             }
109         }
110     }
111 
112     companion object {
113         const val ABORT_REASON_GENERIC_ERROR = 0
114         const val ABORT_REASON_APP_UNAVAILABLE = 1
115         const val ABORT_REASON_USER_NOT_ALLOWED = 2
116         const val ABORT_REASON_UNINSTALL_DONE = 3
117     }
118 }
119 
120