blob: 3d0d787d47c4f7d97c03e086f7f1d1dd5a30a4b5 [file] [log] [blame]
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001/*
2 * Copyright (C) 2012 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
17package android.content.pm;
18
19import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
Jeff Sharkey2bd31db2016-01-09 16:58:14 -070020import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
21import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
22import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
23import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Jeff Sharkey8a372a02016-03-16 16:25:45 -060024import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE;
25import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
Todd Kennedy5eb5a7d2017-08-01 07:42:47 -070026import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
27import static android.content.pm.PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS;
Jeff Sharkey2bd31db2016-01-09 16:58:14 -070028import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070029
Mathew Inwood1c77a112018-08-14 14:06:26 +010030import android.annotation.UnsupportedAppUsage;
Suprabh Shukla021b57a2018-03-08 18:21:50 -080031import android.os.BaseBundle;
32import android.os.PersistableBundle;
Jeff Sharkey9f837a92014-10-24 12:07:24 -070033import android.util.ArraySet;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070034
Jeff Sharkey2bd31db2016-01-09 16:58:14 -070035import com.android.internal.util.ArrayUtils;
36
Mårten Kongstad2e0d0f32016-06-02 09:35:31 +020037import java.util.Arrays;
Suprabh Shukla3c3af142018-03-30 00:28:37 -070038import java.util.Objects;
Mårten Kongstad2e0d0f32016-06-02 09:35:31 +020039
Dianne Hackborn7767eac2012-08-23 18:25:40 -070040/**
41 * Per-user state information about a package.
Dianne Hackborn8832c182012-09-17 17:20:24 -070042 * @hide
Dianne Hackborn7767eac2012-08-23 18:25:40 -070043 */
44public class PackageUserState {
Jeff Sharkey42884192016-04-09 16:12:01 -060045 public long ceDataInode;
46 public boolean installed;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070047 public boolean stopped;
48 public boolean notLaunched;
Amith Yamasanie5bcff62014-07-19 15:44:09 -070049 public boolean hidden; // Is the app restricted by owner / admin
Andrei Stingaceanu1e283912015-11-26 15:26:28 +000050 public boolean suspended;
Suprabh Shukla021b57a2018-03-08 18:21:50 -080051 public String suspendingPackage;
Suprabh Shukla3c3af142018-03-30 00:28:37 -070052 public String dialogMessage; // Message to show when a suspended package launch attempt is made
Suprabh Shukla021b57a2018-03-08 18:21:50 -080053 public PersistableBundle suspendedAppExtras;
54 public PersistableBundle suspendedLauncherExtras;
Todd Kennedybe0b8892017-02-15 14:13:52 -080055 public boolean instantApp;
Todd Kennedy5eb5a7d2017-08-01 07:42:47 -070056 public boolean virtualPreload;
Jeff Sharkey42884192016-04-09 16:12:01 -060057 public int enabled;
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -070058 public String lastDisableAppCaller;
Jeff Sharkey42884192016-04-09 16:12:01 -060059 public int domainVerificationStatus;
60 public int appLinkGeneration;
Jeff Sharkey9bc89af2017-01-11 11:25:50 -070061 public int categoryHint = ApplicationInfo.CATEGORY_UNDEFINED;
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +010062 public int installReason;
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080063 public String harmfulAppWarning;
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -070064
Jeff Sharkey9f837a92014-10-24 12:07:24 -070065 public ArraySet<String> disabledComponents;
66 public ArraySet<String> enabledComponents;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070067
Todd Kennedyb2749472017-06-13 08:24:32 -070068 public String[] overlayPaths;
Mårten Kongstad2e0d0f32016-06-02 09:35:31 +020069
Mathew Inwood1c77a112018-08-14 14:06:26 +010070 @UnsupportedAppUsage
Dianne Hackborn7767eac2012-08-23 18:25:40 -070071 public PackageUserState() {
Dianne Hackborn7767eac2012-08-23 18:25:40 -070072 installed = true;
Amith Yamasanie5bcff62014-07-19 15:44:09 -070073 hidden = false;
Andrei Stingaceanu1e283912015-11-26 15:26:28 +000074 suspended = false;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070075 enabled = COMPONENT_ENABLED_STATE_DEFAULT;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080076 domainVerificationStatus =
77 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +010078 installReason = PackageManager.INSTALL_REASON_UNKNOWN;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070079 }
80
81 public PackageUserState(PackageUserState o) {
Jeff Sharkey42884192016-04-09 16:12:01 -060082 ceDataInode = o.ceDataInode;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070083 installed = o.installed;
84 stopped = o.stopped;
85 notLaunched = o.notLaunched;
Amith Yamasanie5bcff62014-07-19 15:44:09 -070086 hidden = o.hidden;
Andrei Stingaceanu1e283912015-11-26 15:26:28 +000087 suspended = o.suspended;
Suprabh Shukla021b57a2018-03-08 18:21:50 -080088 suspendingPackage = o.suspendingPackage;
Suprabh Shukla3c3af142018-03-30 00:28:37 -070089 dialogMessage = o.dialogMessage;
Suprabh Shukla021b57a2018-03-08 18:21:50 -080090 suspendedAppExtras = o.suspendedAppExtras;
91 suspendedLauncherExtras = o.suspendedLauncherExtras;
Todd Kennedybe0b8892017-02-15 14:13:52 -080092 instantApp = o.instantApp;
Todd Kennedy5eb5a7d2017-08-01 07:42:47 -070093 virtualPreload = o.virtualPreload;
Jeff Sharkey42884192016-04-09 16:12:01 -060094 enabled = o.enabled;
95 lastDisableAppCaller = o.lastDisableAppCaller;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080096 domainVerificationStatus = o.domainVerificationStatus;
Christopher Tatef0d6cb32015-07-10 17:44:53 -070097 appLinkGeneration = o.appLinkGeneration;
Jeff Sharkey9bc89af2017-01-11 11:25:50 -070098 categoryHint = o.categoryHint;
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +010099 installReason = o.installReason;
Jeff Sharkey42884192016-04-09 16:12:01 -0600100 disabledComponents = ArrayUtils.cloneOrNull(o.disabledComponents);
101 enabledComponents = ArrayUtils.cloneOrNull(o.enabledComponents);
Todd Kennedyb2749472017-06-13 08:24:32 -0700102 overlayPaths =
103 o.overlayPaths == null ? null : Arrays.copyOf(o.overlayPaths, o.overlayPaths.length);
Ben Gruver1ab3d6e2017-12-07 13:45:08 -0800104 harmfulAppWarning = o.harmfulAppWarning;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700105 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700106
107 /**
108 * Test if this package is installed.
109 */
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700110 public boolean isAvailable(int flags) {
111 // True if it is installed for this user and it is not hidden. If it is hidden,
112 // still return true if the caller requested MATCH_UNINSTALLED_PACKAGES
113 final boolean matchAnyUser = (flags & PackageManager.MATCH_ANY_USER) != 0;
114 final boolean matchUninstalled = (flags & PackageManager.MATCH_UNINSTALLED_PACKAGES) != 0;
115 return matchAnyUser
116 || (this.installed
117 && (!this.hidden || matchUninstalled));
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700118 }
119
120 /**
121 * Test if the given component is considered installed, enabled and a match
122 * for the given flags.
Bernard Chau186f29b2016-04-13 15:58:38 +0100123 *
124 * <p>
125 * Expects at least one of {@link PackageManager#MATCH_DIRECT_BOOT_AWARE} and
126 * {@link PackageManager#MATCH_DIRECT_BOOT_UNAWARE} are specified in {@code flags}.
127 * </p>
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700128 */
129 public boolean isMatch(ComponentInfo componentInfo, int flags) {
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700130 final boolean isSystemApp = componentInfo.applicationInfo.isSystemApp();
131 final boolean matchUninstalled = (flags & PackageManager.MATCH_KNOWN_PACKAGES) != 0;
132 if (!isAvailable(flags)
133 && !(isSystemApp && matchUninstalled)) return false;
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700134 if (!isEnabled(componentInfo, flags)) return false;
135
136 if ((flags & MATCH_SYSTEM_ONLY) != 0) {
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -0700137 if (!isSystemApp) {
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700138 return false;
139 }
140 }
141
Jeff Sharkey8a372a02016-03-16 16:25:45 -0600142 final boolean matchesUnaware = ((flags & MATCH_DIRECT_BOOT_UNAWARE) != 0)
143 && !componentInfo.directBootAware;
144 final boolean matchesAware = ((flags & MATCH_DIRECT_BOOT_AWARE) != 0)
145 && componentInfo.directBootAware;
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700146 return matchesUnaware || matchesAware;
147 }
148
149 /**
150 * Test if the given component is considered enabled.
151 */
152 public boolean isEnabled(ComponentInfo componentInfo, int flags) {
153 if ((flags & MATCH_DISABLED_COMPONENTS) != 0) {
154 return true;
155 }
156
157 // First check if the overall package is disabled; if the package is
158 // enabled then fall through to check specific component
159 switch (this.enabled) {
160 case COMPONENT_ENABLED_STATE_DISABLED:
161 case COMPONENT_ENABLED_STATE_DISABLED_USER:
162 return false;
163 case COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED:
164 if ((flags & MATCH_DISABLED_UNTIL_USED_COMPONENTS) == 0) {
165 return false;
166 }
167 case COMPONENT_ENABLED_STATE_DEFAULT:
168 if (!componentInfo.applicationInfo.enabled) {
169 return false;
170 }
171 case COMPONENT_ENABLED_STATE_ENABLED:
172 break;
173 }
174
175 // Check if component has explicit state before falling through to
176 // the manifest default
177 if (ArrayUtils.contains(this.enabledComponents, componentInfo.name)) {
178 return true;
179 }
180 if (ArrayUtils.contains(this.disabledComponents, componentInfo.name)) {
181 return false;
182 }
183
184 return componentInfo.enabled;
185 }
Todd Kennedy13715d52016-08-01 13:38:57 -0700186
187 @Override
188 final public boolean equals(Object obj) {
189 if (!(obj instanceof PackageUserState)) {
190 return false;
191 }
192 final PackageUserState oldState = (PackageUserState) obj;
193 if (ceDataInode != oldState.ceDataInode) {
194 return false;
195 }
196 if (installed != oldState.installed) {
197 return false;
198 }
199 if (stopped != oldState.stopped) {
200 return false;
201 }
202 if (notLaunched != oldState.notLaunched) {
203 return false;
204 }
205 if (hidden != oldState.hidden) {
206 return false;
207 }
208 if (suspended != oldState.suspended) {
209 return false;
210 }
Suprabh Shukla021b57a2018-03-08 18:21:50 -0800211 if (suspended) {
212 if (suspendingPackage == null
213 || !suspendingPackage.equals(oldState.suspendingPackage)) {
214 return false;
215 }
Suprabh Shukla3c3af142018-03-30 00:28:37 -0700216 if (!Objects.equals(dialogMessage, oldState.dialogMessage)) {
217 return false;
218 }
Suprabh Shukla021b57a2018-03-08 18:21:50 -0800219 if (!BaseBundle.kindofEquals(suspendedAppExtras,
220 oldState.suspendedAppExtras)) {
221 return false;
222 }
223 if (!BaseBundle.kindofEquals(suspendedLauncherExtras,
224 oldState.suspendedLauncherExtras)) {
225 return false;
226 }
227 }
Todd Kennedybe0b8892017-02-15 14:13:52 -0800228 if (instantApp != oldState.instantApp) {
229 return false;
230 }
Todd Kennedy5eb5a7d2017-08-01 07:42:47 -0700231 if (virtualPreload != oldState.virtualPreload) {
232 return false;
233 }
Todd Kennedy13715d52016-08-01 13:38:57 -0700234 if (enabled != oldState.enabled) {
235 return false;
236 }
237 if ((lastDisableAppCaller == null && oldState.lastDisableAppCaller != null)
238 || (lastDisableAppCaller != null
239 && !lastDisableAppCaller.equals(oldState.lastDisableAppCaller))) {
240 return false;
241 }
242 if (domainVerificationStatus != oldState.domainVerificationStatus) {
243 return false;
244 }
245 if (appLinkGeneration != oldState.appLinkGeneration) {
246 return false;
247 }
Jeff Sharkey9bc89af2017-01-11 11:25:50 -0700248 if (categoryHint != oldState.categoryHint) {
249 return false;
250 }
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +0100251 if (installReason != oldState.installReason) {
252 return false;
253 }
Todd Kennedy13715d52016-08-01 13:38:57 -0700254 if ((disabledComponents == null && oldState.disabledComponents != null)
255 || (disabledComponents != null && oldState.disabledComponents == null)) {
256 return false;
257 }
258 if (disabledComponents != null) {
259 if (disabledComponents.size() != oldState.disabledComponents.size()) {
260 return false;
261 }
262 for (int i = disabledComponents.size() - 1; i >=0; --i) {
263 if (!oldState.disabledComponents.contains(disabledComponents.valueAt(i))) {
264 return false;
265 }
266 }
267 }
268 if ((enabledComponents == null && oldState.enabledComponents != null)
269 || (enabledComponents != null && oldState.enabledComponents == null)) {
270 return false;
271 }
272 if (enabledComponents != null) {
273 if (enabledComponents.size() != oldState.enabledComponents.size()) {
274 return false;
275 }
276 for (int i = enabledComponents.size() - 1; i >=0; --i) {
277 if (!oldState.enabledComponents.contains(enabledComponents.valueAt(i))) {
278 return false;
279 }
280 }
281 }
Ben Gruver1ab3d6e2017-12-07 13:45:08 -0800282 if (harmfulAppWarning == null && oldState.harmfulAppWarning != null
283 || (harmfulAppWarning != null
284 && !harmfulAppWarning.equals(oldState.harmfulAppWarning))) {
285 return false;
286 }
Todd Kennedy13715d52016-08-01 13:38:57 -0700287 return true;
288 }
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700289}