Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.content.pm; |
| 18 | |
| 19 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 20 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED; |
| 21 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED; |
| 22 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER; |
| 23 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; |
| 25 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
Todd Kennedy | 5eb5a7d | 2017-08-01 07:42:47 -0700 | [diff] [blame] | 26 | import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS; |
| 27 | import static android.content.pm.PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS; |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 28 | import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 29 | |
Jeff Sharkey | 9f837a9 | 2014-10-24 12:07:24 -0700 | [diff] [blame] | 30 | import android.util.ArraySet; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 31 | |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 32 | import com.android.internal.util.ArrayUtils; |
| 33 | |
Mårten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 34 | import java.util.Arrays; |
| 35 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Per-user state information about a package. |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 38 | * @hide |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 39 | */ |
| 40 | public class PackageUserState { |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 41 | public long ceDataInode; |
| 42 | public boolean installed; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 43 | public boolean stopped; |
| 44 | public boolean notLaunched; |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 45 | public boolean hidden; // Is the app restricted by owner / admin |
Andrei Stingaceanu | 1e28391 | 2015-11-26 15:26:28 +0000 | [diff] [blame] | 46 | public boolean suspended; |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 47 | public boolean instantApp; |
Todd Kennedy | 5eb5a7d | 2017-08-01 07:42:47 -0700 | [diff] [blame] | 48 | public boolean virtualPreload; |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 49 | public int enabled; |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 50 | public String lastDisableAppCaller; |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 51 | public int domainVerificationStatus; |
| 52 | public int appLinkGeneration; |
Jeff Sharkey | 9bc89af | 2017-01-11 11:25:50 -0700 | [diff] [blame] | 53 | public int categoryHint = ApplicationInfo.CATEGORY_UNDEFINED; |
Bartosz Fabianowski | a34f53f | 2017-01-11 18:08:47 +0100 | [diff] [blame] | 54 | public int installReason; |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 55 | |
Jeff Sharkey | 9f837a9 | 2014-10-24 12:07:24 -0700 | [diff] [blame] | 56 | public ArraySet<String> disabledComponents; |
| 57 | public ArraySet<String> enabledComponents; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 58 | |
Todd Kennedy | b274947 | 2017-06-13 08:24:32 -0700 | [diff] [blame] | 59 | public String[] overlayPaths; |
Mårten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 60 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 61 | public PackageUserState() { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 62 | installed = true; |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 63 | hidden = false; |
Andrei Stingaceanu | 1e28391 | 2015-11-26 15:26:28 +0000 | [diff] [blame] | 64 | suspended = false; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 65 | enabled = COMPONENT_ENABLED_STATE_DEFAULT; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 66 | domainVerificationStatus = |
| 67 | PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED; |
Bartosz Fabianowski | a34f53f | 2017-01-11 18:08:47 +0100 | [diff] [blame] | 68 | installReason = PackageManager.INSTALL_REASON_UNKNOWN; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public PackageUserState(PackageUserState o) { |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 72 | ceDataInode = o.ceDataInode; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 73 | installed = o.installed; |
| 74 | stopped = o.stopped; |
| 75 | notLaunched = o.notLaunched; |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 76 | hidden = o.hidden; |
Andrei Stingaceanu | 1e28391 | 2015-11-26 15:26:28 +0000 | [diff] [blame] | 77 | suspended = o.suspended; |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 78 | instantApp = o.instantApp; |
Todd Kennedy | 5eb5a7d | 2017-08-01 07:42:47 -0700 | [diff] [blame] | 79 | virtualPreload = o.virtualPreload; |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 80 | enabled = o.enabled; |
| 81 | lastDisableAppCaller = o.lastDisableAppCaller; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 82 | domainVerificationStatus = o.domainVerificationStatus; |
Christopher Tate | f0d6cb3 | 2015-07-10 17:44:53 -0700 | [diff] [blame] | 83 | appLinkGeneration = o.appLinkGeneration; |
Jeff Sharkey | 9bc89af | 2017-01-11 11:25:50 -0700 | [diff] [blame] | 84 | categoryHint = o.categoryHint; |
Bartosz Fabianowski | a34f53f | 2017-01-11 18:08:47 +0100 | [diff] [blame] | 85 | installReason = o.installReason; |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 86 | disabledComponents = ArrayUtils.cloneOrNull(o.disabledComponents); |
| 87 | enabledComponents = ArrayUtils.cloneOrNull(o.enabledComponents); |
Todd Kennedy | b274947 | 2017-06-13 08:24:32 -0700 | [diff] [blame] | 88 | overlayPaths = |
| 89 | o.overlayPaths == null ? null : Arrays.copyOf(o.overlayPaths, o.overlayPaths.length); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 90 | } |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Test if this package is installed. |
| 94 | */ |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 95 | public boolean isAvailable(int flags) { |
| 96 | // True if it is installed for this user and it is not hidden. If it is hidden, |
| 97 | // still return true if the caller requested MATCH_UNINSTALLED_PACKAGES |
| 98 | final boolean matchAnyUser = (flags & PackageManager.MATCH_ANY_USER) != 0; |
| 99 | final boolean matchUninstalled = (flags & PackageManager.MATCH_UNINSTALLED_PACKAGES) != 0; |
| 100 | return matchAnyUser |
| 101 | || (this.installed |
| 102 | && (!this.hidden || matchUninstalled)); |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Test if the given component is considered installed, enabled and a match |
| 107 | * for the given flags. |
Bernard Chau | 186f29b | 2016-04-13 15:58:38 +0100 | [diff] [blame] | 108 | * |
| 109 | * <p> |
| 110 | * Expects at least one of {@link PackageManager#MATCH_DIRECT_BOOT_AWARE} and |
| 111 | * {@link PackageManager#MATCH_DIRECT_BOOT_UNAWARE} are specified in {@code flags}. |
| 112 | * </p> |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 113 | */ |
| 114 | public boolean isMatch(ComponentInfo componentInfo, int flags) { |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 115 | final boolean isSystemApp = componentInfo.applicationInfo.isSystemApp(); |
| 116 | final boolean matchUninstalled = (flags & PackageManager.MATCH_KNOWN_PACKAGES) != 0; |
| 117 | if (!isAvailable(flags) |
| 118 | && !(isSystemApp && matchUninstalled)) return false; |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 119 | if (!isEnabled(componentInfo, flags)) return false; |
| 120 | |
| 121 | if ((flags & MATCH_SYSTEM_ONLY) != 0) { |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 122 | if (!isSystemApp) { |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 127 | final boolean matchesUnaware = ((flags & MATCH_DIRECT_BOOT_UNAWARE) != 0) |
| 128 | && !componentInfo.directBootAware; |
| 129 | final boolean matchesAware = ((flags & MATCH_DIRECT_BOOT_AWARE) != 0) |
| 130 | && componentInfo.directBootAware; |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 131 | return matchesUnaware || matchesAware; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Test if the given component is considered enabled. |
| 136 | */ |
| 137 | public boolean isEnabled(ComponentInfo componentInfo, int flags) { |
| 138 | if ((flags & MATCH_DISABLED_COMPONENTS) != 0) { |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | // First check if the overall package is disabled; if the package is |
| 143 | // enabled then fall through to check specific component |
| 144 | switch (this.enabled) { |
| 145 | case COMPONENT_ENABLED_STATE_DISABLED: |
| 146 | case COMPONENT_ENABLED_STATE_DISABLED_USER: |
| 147 | return false; |
| 148 | case COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED: |
| 149 | if ((flags & MATCH_DISABLED_UNTIL_USED_COMPONENTS) == 0) { |
| 150 | return false; |
| 151 | } |
| 152 | case COMPONENT_ENABLED_STATE_DEFAULT: |
| 153 | if (!componentInfo.applicationInfo.enabled) { |
| 154 | return false; |
| 155 | } |
| 156 | case COMPONENT_ENABLED_STATE_ENABLED: |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | // Check if component has explicit state before falling through to |
| 161 | // the manifest default |
| 162 | if (ArrayUtils.contains(this.enabledComponents, componentInfo.name)) { |
| 163 | return true; |
| 164 | } |
| 165 | if (ArrayUtils.contains(this.disabledComponents, componentInfo.name)) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | return componentInfo.enabled; |
| 170 | } |
Todd Kennedy | 13715d5 | 2016-08-01 13:38:57 -0700 | [diff] [blame] | 171 | |
| 172 | @Override |
| 173 | final public boolean equals(Object obj) { |
| 174 | if (!(obj instanceof PackageUserState)) { |
| 175 | return false; |
| 176 | } |
| 177 | final PackageUserState oldState = (PackageUserState) obj; |
| 178 | if (ceDataInode != oldState.ceDataInode) { |
| 179 | return false; |
| 180 | } |
| 181 | if (installed != oldState.installed) { |
| 182 | return false; |
| 183 | } |
| 184 | if (stopped != oldState.stopped) { |
| 185 | return false; |
| 186 | } |
| 187 | if (notLaunched != oldState.notLaunched) { |
| 188 | return false; |
| 189 | } |
| 190 | if (hidden != oldState.hidden) { |
| 191 | return false; |
| 192 | } |
| 193 | if (suspended != oldState.suspended) { |
| 194 | return false; |
| 195 | } |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 196 | if (instantApp != oldState.instantApp) { |
| 197 | return false; |
| 198 | } |
Todd Kennedy | 5eb5a7d | 2017-08-01 07:42:47 -0700 | [diff] [blame] | 199 | if (virtualPreload != oldState.virtualPreload) { |
| 200 | return false; |
| 201 | } |
Todd Kennedy | 13715d5 | 2016-08-01 13:38:57 -0700 | [diff] [blame] | 202 | if (enabled != oldState.enabled) { |
| 203 | return false; |
| 204 | } |
| 205 | if ((lastDisableAppCaller == null && oldState.lastDisableAppCaller != null) |
| 206 | || (lastDisableAppCaller != null |
| 207 | && !lastDisableAppCaller.equals(oldState.lastDisableAppCaller))) { |
| 208 | return false; |
| 209 | } |
| 210 | if (domainVerificationStatus != oldState.domainVerificationStatus) { |
| 211 | return false; |
| 212 | } |
| 213 | if (appLinkGeneration != oldState.appLinkGeneration) { |
| 214 | return false; |
| 215 | } |
Jeff Sharkey | 9bc89af | 2017-01-11 11:25:50 -0700 | [diff] [blame] | 216 | if (categoryHint != oldState.categoryHint) { |
| 217 | return false; |
| 218 | } |
Bartosz Fabianowski | a34f53f | 2017-01-11 18:08:47 +0100 | [diff] [blame] | 219 | if (installReason != oldState.installReason) { |
| 220 | return false; |
| 221 | } |
Todd Kennedy | 13715d5 | 2016-08-01 13:38:57 -0700 | [diff] [blame] | 222 | if ((disabledComponents == null && oldState.disabledComponents != null) |
| 223 | || (disabledComponents != null && oldState.disabledComponents == null)) { |
| 224 | return false; |
| 225 | } |
| 226 | if (disabledComponents != null) { |
| 227 | if (disabledComponents.size() != oldState.disabledComponents.size()) { |
| 228 | return false; |
| 229 | } |
| 230 | for (int i = disabledComponents.size() - 1; i >=0; --i) { |
| 231 | if (!oldState.disabledComponents.contains(disabledComponents.valueAt(i))) { |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | if ((enabledComponents == null && oldState.enabledComponents != null) |
| 237 | || (enabledComponents != null && oldState.enabledComponents == null)) { |
| 238 | return false; |
| 239 | } |
| 240 | if (enabledComponents != null) { |
| 241 | if (enabledComponents.size() != oldState.enabledComponents.size()) { |
| 242 | return false; |
| 243 | } |
| 244 | for (int i = enabledComponents.size() - 1; i >=0; --i) { |
| 245 | if (!oldState.enabledComponents.contains(enabledComponents.valueAt(i))) { |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | return true; |
| 251 | } |
Jeff Sharkey | 9f837a9 | 2014-10-24 12:07:24 -0700 | [diff] [blame] | 252 | } |