1 /*
2  * Copyright (C) 2024 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.systemui.common.shared.model
18 
19 import android.annotation.CurrentTimeMillisLong
20 import android.content.Intent
21 import android.content.pm.PackageManager
22 import android.os.UserHandle
23 
24 /** Represents changes to an installed package. */
25 sealed interface PackageChangeModel {
26     /** The package which this change corresponds to, eg "com.android.systemui". */
27     val packageName: String
28 
29     /**
30      * The uid for this package, which uniquely identifies this package and user. The same package
31      * running on different users will have differing uids.
32      */
33     val packageUid: Int
34 
35     /**
36      * The time in milliseconds that this update was received from [PackageManager].
37      *
38      * @see System.currentTimeMillis()
39      */
40     @get:CurrentTimeMillisLong val timeMillis: Long
41 
42     /** The user from which this change originated. */
43     val user: UserHandle
44         get() = UserHandle.getUserHandleForUid(packageUid)
45 
46     /** Empty change, provided for convenience when a sensible default value is needed. */
47     data object Empty : PackageChangeModel {
48         override val packageName: String
49             get() = ""
50 
51         override val packageUid: Int
52             get() = 0
53 
54         override val timeMillis: Long
55             get() = 0
56     }
57 
58     /**
59      * An existing application package was uninstalled.
60      *
61      * Equivalent to receiving the [Intent.ACTION_PACKAGE_REMOVED] broadcast with
62      * [Intent.EXTRA_REPLACING] set to false.
63      */
64     data class Uninstalled(
65         override val packageName: String,
66         override val packageUid: Int,
67         @CurrentTimeMillisLong override val timeMillis: Long = 0,
68     ) : PackageChangeModel
69 
70     /**
71      * A new version of an existing application is going to be installed.
72      *
73      * Equivalent to receiving the [Intent.ACTION_PACKAGE_REMOVED] broadcast with
74      * [Intent.EXTRA_REPLACING] set to true.
75      */
76     data class UpdateStarted(
77         override val packageName: String,
78         override val packageUid: Int,
79         @CurrentTimeMillisLong override val timeMillis: Long = 0,
80     ) : PackageChangeModel
81 
82     /**
83      * A new version of an existing application package has been installed, replacing the old
84      * version.
85      *
86      * Equivalent to receiving the [Intent.ACTION_PACKAGE_ADDED] broadcast with
87      * [Intent.EXTRA_REPLACING] set to true.
88      */
89     data class UpdateFinished(
90         override val packageName: String,
91         override val packageUid: Int,
92         @CurrentTimeMillisLong override val timeMillis: Long = 0,
93     ) : PackageChangeModel
94 
95     /**
96      * A new application package has been installed.
97      *
98      * Equivalent to receiving the [Intent.ACTION_PACKAGE_ADDED] broadcast with
99      * [Intent.EXTRA_REPLACING] set to false.
100      */
101     data class Installed(
102         override val packageName: String,
103         override val packageUid: Int,
104         @CurrentTimeMillisLong override val timeMillis: Long = 0,
105     ) : PackageChangeModel
106 
107     /**
108      * An existing application package has been changed (for example, a component has been enabled
109      * or disabled).
110      *
111      * Equivalent to receiving the [Intent.ACTION_PACKAGE_CHANGED] broadcast.
112      */
113     data class Changed(
114         override val packageName: String,
115         override val packageUid: Int,
116         @CurrentTimeMillisLong override val timeMillis: Long = 0,
117     ) : PackageChangeModel
118 
isSamePackagenull119     fun isSamePackage(other: PackageChangeModel) =
120         this.packageName == other.packageName && this.packageUid == other.packageUid
121 }
122