blob: 39cae3706cd2a9ddee8c34a8ebe2b4c22e3b8110 [file] [log] [blame]
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001/*
2 * Copyright (C) 2011 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 com.android.server.pm;
18
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070019import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070020import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
21import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
22import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Christopher Tatee9fd1fa2015-09-15 16:43:07 -070023import static android.content.pm.PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE;
Jeff Sharkey2271ba32016-02-01 17:57:08 -070024import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_FIXED;
25import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_SET;
Fabrice Di Meglio07885952015-04-06 19:41:28 -070026import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080027import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070028import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY;
Jeff Sharkey184a0102013-07-10 16:19:52 -070029import static android.os.Process.PACKAGE_INFO_GID;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070030import static android.os.Process.SYSTEM_UID;
Jeff Sharkey2271ba32016-02-01 17:57:08 -070031
Christopher Tate72c10a22015-06-12 18:31:24 -070032import static com.android.server.pm.PackageManagerService.DEBUG_DOMAIN_VERIFICATION;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070033
Xiaohui Chen594f2082015-08-18 11:04:20 -070034import android.annotation.NonNull;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070035import android.content.ComponentName;
36import android.content.Intent;
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080037import android.content.IntentFilter;
38import android.content.pm.ActivityInfo;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070039import android.content.pm.ApplicationInfo;
40import android.content.pm.ComponentInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080041import android.content.pm.IntentFilterVerificationInfo;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070042import android.content.pm.PackageCleanItem;
43import android.content.pm.PackageManager;
44import android.content.pm.PackageParser;
45import android.content.pm.PackageUserState;
46import android.content.pm.PermissionInfo;
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080047import android.content.pm.ResolveInfo;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070048import android.content.pm.Signature;
49import android.content.pm.UserInfo;
50import android.content.pm.VerifierDeviceIdentity;
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080051import android.net.Uri;
Narayan Kamathff110bd2014-07-04 18:30:45 +010052import android.os.Binder;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -070053import android.os.Build;
Narayan Kamathff110bd2014-07-04 18:30:45 +010054import android.os.Environment;
55import android.os.FileUtils;
Svetoslavc6d1c342015-02-26 14:44:43 -080056import android.os.Handler;
57import android.os.Message;
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080058import android.os.PatternMatcher;
Narayan Kamathff110bd2014-07-04 18:30:45 +010059import android.os.Process;
Svetoslavc6d1c342015-02-26 14:44:43 -080060import android.os.SystemClock;
Narayan Kamathff110bd2014-07-04 18:30:45 +010061import android.os.UserHandle;
Amith Yamasani26f9ea32014-09-11 10:56:51 -070062import android.os.UserManager;
Paul Crowleybcf48ed2015-04-22 13:36:59 +010063import android.os.storage.StorageManager;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070064import android.os.storage.VolumeInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080065import android.text.TextUtils;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070066import android.util.ArrayMap;
67import android.util.ArraySet;
68import android.util.AtomicFile;
69import android.util.Log;
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080070import android.util.LogPrinter;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070071import android.util.Slog;
72import android.util.SparseArray;
Svetoslavc6d1c342015-02-26 14:44:43 -080073import android.util.SparseBooleanArray;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070074import android.util.SparseIntArray;
Svetoslavc6d1c342015-02-26 14:44:43 -080075import android.util.SparseLongArray;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070076import android.util.Xml;
Christopher Tatee012a232015-04-01 17:18:50 -070077
Svetoslavc6d1c342015-02-26 14:44:43 -080078import com.android.internal.annotations.GuardedBy;
79import com.android.internal.os.BackgroundThread;
80import com.android.internal.util.ArrayUtils;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070081import com.android.internal.util.FastXmlSerializer;
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -070082import com.android.internal.util.IndentingPrintWriter;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070083import com.android.internal.util.JournaledFile;
84import com.android.internal.util.XmlUtils;
Christopher Tatee012a232015-04-01 17:18:50 -070085import com.android.server.backup.PreferredActivityBackupHelper;
Jeff Sharkey740f5232016-12-09 14:31:26 -070086import com.android.server.pm.Installer.InstallerException;
Kenny Root447106f2011-03-23 11:00:15 -070087import com.android.server.pm.PackageManagerService.DumpState;
Svet Ganov8c7f7002015-05-07 10:48:44 -070088import com.android.server.pm.PermissionsState.PermissionState;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070089
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070090import libcore.io.IoUtils;
Alexandra Gherghina6e2ae252014-06-12 16:03:58 +010091
Kenny Rootcf0b38c2011-03-22 14:17:59 -070092import org.xmlpull.v1.XmlPullParser;
93import org.xmlpull.v1.XmlPullParserException;
94import org.xmlpull.v1.XmlSerializer;
95
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070096import java.io.BufferedInputStream;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070097import java.io.BufferedOutputStream;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -070098import java.io.BufferedWriter;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070099import java.io.File;
100import java.io.FileInputStream;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -0700101import java.io.FileNotFoundException;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700102import java.io.FileOutputStream;
103import java.io.IOException;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -0700104import java.io.InputStream;
105import java.io.OutputStreamWriter;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700106import java.io.PrintWriter;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -0700107import java.nio.charset.Charset;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100108import java.nio.charset.StandardCharsets;
Kenny Root447106f2011-03-23 11:00:15 -0700109import java.text.SimpleDateFormat;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700110import java.util.ArrayList;
111import java.util.Arrays;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -0700112import java.util.Collection;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800113import java.util.Collections;
Kenny Root447106f2011-03-23 11:00:15 -0700114import java.util.Date;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700115import java.util.Iterator;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700116import java.util.List;
Andy McFadden2f362292012-01-20 14:43:38 -0800117import java.util.Map;
Jeff Sharkey2a90f6732016-01-06 12:26:11 -0700118import java.util.Map.Entry;
Jeff Sharkey84f12942014-07-10 17:48:11 -0700119import java.util.Objects;
Dianne Hackborn63092712012-10-07 14:45:35 -0700120import java.util.Set;
Kenny Root62539e92011-11-10 09:35:38 -0800121
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700122/**
123 * Holds information about dynamic settings.
124 */
125final class Settings {
Kenny Root447106f2011-03-23 11:00:15 -0700126 private static final String TAG = "PackageSettings";
127
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700128 /**
129 * Current version of the package database. Set it to the latest version in
130 * the {@link DatabaseVersion} class below to ensure the database upgrade
131 * doesn't happen repeatedly.
132 * <p>
133 * Note that care should be taken to make sure all database upgrades are
134 * idempotent.
135 */
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700136 public static final int CURRENT_DATABASE_VERSION = DatabaseVersion.SIGNATURE_MALFORMED_RECOVER;
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700137
138 /**
139 * This class contains constants that can be referred to from upgrade code.
140 * Insert constant values here that describe the upgrade reason. The version
141 * code must be monotonically increasing.
142 */
143 public static class DatabaseVersion {
144 /**
145 * The initial version of the database.
146 */
147 public static final int FIRST_VERSION = 1;
Kenny Roota8e65fd2014-04-24 11:44:47 -0700148
149 /**
150 * Migrating the Signature array from the entire certificate chain to
151 * just the signing certificate.
152 */
153 public static final int SIGNATURE_END_ENTITY = 2;
Jeff Sharkeyd68b87c2014-11-12 12:18:11 -0800154
155 /**
156 * There was a window of time in
157 * {@link android.os.Build.VERSION_CODES#LOLLIPOP} where we persisted
158 * certificates after potentially mutating them. To switch back to the
159 * original untouched certificates, we need to force a collection pass.
160 */
161 public static final int SIGNATURE_MALFORMED_RECOVER = 3;
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700162 }
163
Kenny Root447106f2011-03-23 11:00:15 -0700164 private static final boolean DEBUG_STOPPED = false;
Amith Yamasani258848d2012-08-10 17:06:33 -0700165 private static final boolean DEBUG_MU = false;
Jeff Sharkey2271ba32016-02-01 17:57:08 -0700166 private static final boolean DEBUG_KERNEL = false;
Kenny Root447106f2011-03-23 11:00:15 -0700167
Svetoslavc6d1c342015-02-26 14:44:43 -0800168 private static final String RUNTIME_PERMISSIONS_FILE_NAME = "runtime-permissions.xml";
169
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -0700170 private static final String TAG_READ_EXTERNAL_STORAGE = "read-external-storage";
171 private static final String ATTR_ENFORCEMENT = "enforcement";
172
Amith Yamasani483f3b02012-03-13 16:08:00 -0700173 private static final String TAG_ITEM = "item";
174 private static final String TAG_DISABLED_COMPONENTS = "disabled-components";
175 private static final String TAG_ENABLED_COMPONENTS = "enabled-components";
176 private static final String TAG_PACKAGE_RESTRICTIONS = "package-restrictions";
177 private static final String TAG_PACKAGE = "pkg";
Svetoslavc6d1c342015-02-26 14:44:43 -0800178 private static final String TAG_SHARED_USER = "shared-user";
179 private static final String TAG_RUNTIME_PERMISSIONS = "runtime-permissions";
180 private static final String TAG_PERMISSIONS = "perms";
Svet Ganov354cd3c2015-12-17 11:35:04 -0800181 private static final String TAG_CHILD_PACKAGE = "child-package";
182
Sander Alewijnseaf597622014-03-20 18:44:57 +0000183 private static final String TAG_PERSISTENT_PREFERRED_ACTIVITIES =
184 "persistent-preferred-activities";
Nicolas Prevot81948992014-05-16 18:25:26 +0100185 static final String TAG_CROSS_PROFILE_INTENT_FILTERS =
186 "crossProfile-intent-filters";
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700187 private static final String TAG_DOMAIN_VERIFICATION = "domain-verification";
188 private static final String TAG_DEFAULT_APPS = "default-apps";
189 private static final String TAG_ALL_INTENT_FILTER_VERIFICATION =
Christopher Tate6038d152015-06-17 13:07:46 -0700190 "all-intent-filter-verifications";
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700191 private static final String TAG_DEFAULT_BROWSER = "default-browser";
Robin Lee0dc591b2016-05-04 13:01:46 +0100192 private static final String TAG_DEFAULT_DIALER = "default-dialer";
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700193 private static final String TAG_VERSION = "version";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700194
195 private static final String ATTR_NAME = "name";
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700196 private static final String ATTR_USER = "user";
197 private static final String ATTR_CODE = "code";
Svet Ganov8c7f7002015-05-07 10:48:44 -0700198 private static final String ATTR_GRANTED = "granted";
199 private static final String ATTR_FLAGS = "flags";
Jeff Sharkey42884192016-04-09 16:12:01 -0600200
201 private static final String ATTR_CE_DATA_INODE = "ceDataInode";
202 private static final String ATTR_INSTALLED = "inst";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700203 private static final String ATTR_STOPPED = "stopped";
Jeff Sharkey42884192016-04-09 16:12:01 -0600204 private static final String ATTR_NOT_LAUNCHED = "nl";
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700205 // Legacy, here for reading older versions of the package-restrictions.
Amith Yamasani655d0e22013-06-12 14:19:10 -0700206 private static final String ATTR_BLOCKED = "blocked";
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700207 // New name for the above attribute.
208 private static final String ATTR_HIDDEN = "hidden";
Andrei Stingaceanu1e283912015-11-26 15:26:28 +0000209 private static final String ATTR_SUSPENDED = "suspended";
Kenny Guyc13053b2014-05-29 14:17:17 +0100210 private static final String ATTR_BLOCK_UNINSTALL = "blockUninstall";
Jeff Sharkey42884192016-04-09 16:12:01 -0600211 private static final String ATTR_ENABLED = "enabled";
212 private static final String ATTR_ENABLED_CALLER = "enabledCaller";
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800213 private static final String ATTR_DOMAIN_VERIFICATON_STATE = "domainVerificationStatus";
Jeff Sharkey42884192016-04-09 16:12:01 -0600214 private static final String ATTR_APP_LINK_GENERATION = "app-link-generation";
215
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700216 private static final String ATTR_PACKAGE_NAME = "packageName";
Svet Ganovba3ba812015-06-26 10:54:06 -0700217 private static final String ATTR_FINGERPRINT = "fingerprint";
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700218 private static final String ATTR_VOLUME_UUID = "volumeUuid";
219 private static final String ATTR_SDK_VERSION = "sdkVersion";
220 private static final String ATTR_DATABASE_VERSION = "databaseVersion";
Andreas Gampefc41ea32016-03-25 20:03:51 -0700221 private static final String ATTR_DONE = "done";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700222
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700223 // Bookkeeping for restored permission grants
224 private static final String TAG_RESTORED_RUNTIME_PERMISSIONS = "restored-perms";
225 // package name: ATTR_PACKAGE_NAME
226 private static final String TAG_PERMISSION_ENTRY = "perm";
227 // permission name: ATTR_NAME
228 // permission granted (boolean): ATTR_GRANTED
229 private static final String ATTR_USER_SET = "set";
230 private static final String ATTR_USER_FIXED = "fixed";
231 private static final String ATTR_REVOKE_ON_UPGRADE = "rou";
232
233 // Flag mask of restored permission grants that are applied at install time
234 private static final int USER_RUNTIME_GRANT_MASK =
235 FLAG_PERMISSION_USER_SET
236 | FLAG_PERMISSION_USER_FIXED
237 | FLAG_PERMISSION_REVOKE_ON_UPGRADE;
238
Svetoslavc6d1c342015-02-26 14:44:43 -0800239 private final Object mLock;
Svetoslavc6d1c342015-02-26 14:44:43 -0800240
241 private final RuntimePermissionPersistence mRuntimePermissionsPersistence;
242
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700243 private final File mSettingsFilename;
244 private final File mBackupSettingsFilename;
245 private final File mPackageListFilename;
246 private final File mStoppedPackagesFilename;
247 private final File mBackupStoppedPackagesFilename;
Jeff Sharkey2271ba32016-02-01 17:57:08 -0700248 private final File mKernelMappingFilename;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700249
Jeff Sharkey2271ba32016-02-01 17:57:08 -0700250 /** Map from package name to settings */
251 final ArrayMap<String, PackageSetting> mPackages = new ArrayMap<>();
252
Sudheer Shanka29283372016-04-04 20:56:27 -0700253 /** List of packages that installed other packages */
254 final ArraySet<String> mInstallerPackages = new ArraySet<>();
255
Jeff Sharkey2271ba32016-02-01 17:57:08 -0700256 /** Map from package name to appId */
257 private final ArrayMap<String, Integer> mKernelMapping = new ArrayMap<>();
Christopher Tate6038d152015-06-17 13:07:46 -0700258
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700259 // List of replaced system applications
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700260 private final ArrayMap<String, PackageSetting> mDisabledSysPackages =
261 new ArrayMap<String, PackageSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700262
Christopher Tate6038d152015-06-17 13:07:46 -0700263 // Set of restored intent-filter verification states
264 private final ArrayMap<String, IntentFilterVerificationInfo> mRestoredIntentFilterVerifications =
265 new ArrayMap<String, IntentFilterVerificationInfo>();
266
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700267 // Bookkeeping for restored user permission grants
268 final class RestoredPermissionGrant {
269 String permissionName;
270 boolean granted;
271 int grantBits;
272
273 RestoredPermissionGrant(String name, boolean isGranted, int theGrantBits) {
274 permissionName = name;
275 granted = isGranted;
276 grantBits = theGrantBits;
277 }
278 }
279
280 // This would be more compact as a flat array of restored grants or something, but we
281 // may have quite a few, especially during early device lifetime, and avoiding all those
282 // linear lookups will be important.
283 private final SparseArray<ArrayMap<String, ArraySet<RestoredPermissionGrant>>>
284 mRestoredUserGrants =
285 new SparseArray<ArrayMap<String, ArraySet<RestoredPermissionGrant>>>();
286
Geremy Condra12c18382013-03-06 16:49:06 -0800287 private static int mFirstAvailableUid = 0;
288
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700289 /** Map from volume UUID to {@link VersionInfo} */
290 private ArrayMap<String, VersionInfo> mVersion = new ArrayMap<>();
Kenny Root0aaa0d92011-09-12 16:42:55 -0700291
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700292 /**
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700293 * Version details for a storage volume that may hold apps.
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700294 */
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700295 public static class VersionInfo {
296 /**
297 * These are the last platform API version we were using for the apps
298 * installed on internal and external storage. It is used to grant newer
299 * permissions one time during a system upgrade.
300 */
301 int sdkVersion;
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700302
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700303 /**
304 * The current database version for apps on internal storage. This is
305 * used to upgrade the format of the packages.xml database not
306 * necessarily tied to an SDK version.
307 */
308 int databaseVersion;
309
310 /**
311 * Last known value of {@link Build#FINGERPRINT}. Used to determine when
312 * an system update has occurred, meaning we need to clear code caches.
313 */
314 String fingerprint;
315
316 /**
317 * Force all version information to match current system values,
318 * typically after resolving any required upgrade steps.
319 */
320 public void forceCurrent() {
321 sdkVersion = Build.VERSION.SDK_INT;
322 databaseVersion = CURRENT_DATABASE_VERSION;
323 fingerprint = Build.FINGERPRINT;
324 }
325 }
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700326
Jeff Sharkeyf5385772012-05-11 14:04:41 -0700327 Boolean mReadExternalStorageEnforced;
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -0700328
Kenny Root0aaa0d92011-09-12 16:42:55 -0700329 /** Device identity for the purpose of package verification. */
330 private VerifierDeviceIdentity mVerifierDeviceIdentity;
331
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700332 // The user's preferred activities associated with particular intent
333 // filters.
Dianne Hackborn63092712012-10-07 14:45:35 -0700334 final SparseArray<PreferredIntentResolver> mPreferredActivities =
335 new SparseArray<PreferredIntentResolver>();
336
Sander Alewijnsef475ca32014-02-17 15:13:58 +0000337 // The persistent preferred activities of the user's profile/device owner
338 // associated with particular intent filters.
339 final SparseArray<PersistentPreferredIntentResolver> mPersistentPreferredActivities =
340 new SparseArray<PersistentPreferredIntentResolver>();
341
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000342 // For every user, it is used to find to which other users the intent can be forwarded.
Nicolas Prevot81948992014-05-16 18:25:26 +0100343 final SparseArray<CrossProfileIntentResolver> mCrossProfileIntentResolvers =
344 new SparseArray<CrossProfileIntentResolver>();
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000345
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700346 final ArrayMap<String, SharedUserSetting> mSharedUsers =
347 new ArrayMap<String, SharedUserSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700348 private final ArrayList<Object> mUserIds = new ArrayList<Object>();
349 private final SparseArray<Object> mOtherUserIds =
350 new SparseArray<Object>();
351
352 // For reading/writing settings file.
353 private final ArrayList<Signature> mPastSignatures =
354 new ArrayList<Signature>();
dcashman8c04fac2015-03-23 11:39:42 -0700355 private final ArrayMap<Long, Integer> mKeySetRefs =
356 new ArrayMap<Long, Integer>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700357
358 // Mapping from permission names to info about them.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700359 final ArrayMap<String, BasePermission> mPermissions =
360 new ArrayMap<String, BasePermission>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700361
362 // Mapping from permission tree names to info about them.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700363 final ArrayMap<String, BasePermission> mPermissionTrees =
364 new ArrayMap<String, BasePermission>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700365
366 // Packages that have been uninstalled and still need their external
367 // storage data deleted.
Jeff Sharkey752cd922012-09-23 16:25:12 -0700368 final ArrayList<PackageCleanItem> mPackagesToBeCleaned = new ArrayList<PackageCleanItem>();
Selim Gurun5c8acb42016-04-07 16:34:02 -0700369
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700370 // Packages that have been renamed since they were first installed.
371 // Keys are the new names of the packages, values are the original
372 // names. The packages appear everwhere else under their original
373 // names.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700374 final ArrayMap<String, String> mRenamedPackages = new ArrayMap<String, String>();
Fabrice Di Meglio62271722015-04-10 17:24:02 -0700375
376 // For every user, it is used to find the package name of the default Browser App.
377 final SparseArray<String> mDefaultBrowserApp = new SparseArray<String>();
378
Robin Lee0dc591b2016-05-04 13:01:46 +0100379 // For every user, a record of the package name of the default Dialer App.
380 final SparseArray<String> mDefaultDialerApp = new SparseArray<String>();
381
Christopher Tatef0d6cb32015-07-10 17:44:53 -0700382 // App-link priority tracking, per-user
383 final SparseIntArray mNextAppLinkGeneration = new SparseIntArray();
384
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700385 final StringBuilder mReadMessages = new StringBuilder();
386
Kenny Root447106f2011-03-23 11:00:15 -0700387 /**
388 * Used to track packages that have a shared user ID that hasn't been read
389 * in yet.
390 * <p>
391 * TODO: make this just a local variable that is passed in during package
392 * scanning to make it less confusing.
393 */
394 private final ArrayList<PendingPackage> mPendingPackages = new ArrayList<PendingPackage>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700395
Amith Yamasani483f3b02012-03-13 16:08:00 -0700396 private final File mSystemDir;
Geremy Condraf1bcca82013-01-07 22:35:24 -0800397
dcashman55b10782014-04-09 14:20:38 -0700398 public final KeySetManagerService mKeySetManagerService = new KeySetManagerService(mPackages);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800399
Svet Ganov12a692a2015-03-28 19:34:15 -0700400 Settings(Object lock) {
401 this(Environment.getDataDirectory(), lock);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700402 }
403
Svet Ganov12a692a2015-03-28 19:34:15 -0700404 Settings(File dataDir, Object lock) {
Svetoslavc6d1c342015-02-26 14:44:43 -0800405 mLock = lock;
406
407 mRuntimePermissionsPersistence = new RuntimePermissionPersistence(mLock);
408
Amith Yamasani483f3b02012-03-13 16:08:00 -0700409 mSystemDir = new File(dataDir, "system");
410 mSystemDir.mkdirs();
411 FileUtils.setPermissions(mSystemDir.toString(),
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700412 FileUtils.S_IRWXU|FileUtils.S_IRWXG
413 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
414 -1, -1);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700415 mSettingsFilename = new File(mSystemDir, "packages.xml");
416 mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
417 mPackageListFilename = new File(mSystemDir, "packages.list");
Jeff Sharkeyaebb65c2014-11-24 15:00:13 -0800418 FileUtils.setPermissions(mPackageListFilename, 0640, SYSTEM_UID, PACKAGE_INFO_GID);
Jeff Sharkey184a0102013-07-10 16:19:52 -0700419
Jeff Sharkey2271ba32016-02-01 17:57:08 -0700420 final File kernelDir = new File("/config/sdcardfs");
421 mKernelMappingFilename = kernelDir.exists() ? kernelDir : null;
422
Amith Yamasani483f3b02012-03-13 16:08:00 -0700423 // Deprecated: Needed for migration
424 mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
425 mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700426 }
427
Kenny Root447106f2011-03-23 11:00:15 -0700428 PackageSetting getPackageLPw(PackageParser.Package pkg, PackageSetting origPackage,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700429 String realName, SharedUserSetting sharedUser, File codePath, File resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100430 String legacyNativeLibraryPathString, String primaryCpuAbi, String secondaryCpuAbi,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800431 int pkgFlags, int pkgPrivateFlags, UserHandle user, boolean add) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700432 final String name = pkg.packageName;
Svet Ganov354cd3c2015-12-17 11:35:04 -0800433 final String parentPackageName = (pkg.parentPackage != null)
434 ? pkg.parentPackage.packageName : null;
435
436 List<String> childPackageNames = null;
437 if (pkg.childPackages != null) {
438 final int childCount = pkg.childPackages.size();
439 childPackageNames = new ArrayList<>(childCount);
440 for (int i = 0; i < childCount; i++) {
441 String childPackageName = pkg.childPackages.get(i).packageName;
442 childPackageNames.add(childPackageName);
443 }
444 }
445
Kenny Root447106f2011-03-23 11:00:15 -0700446 PackageSetting p = getPackageLPw(name, origPackage, realName, sharedUser, codePath,
Jeff Sharkey84f12942014-07-10 17:48:11 -0700447 resourcePath, legacyNativeLibraryPathString, primaryCpuAbi, secondaryCpuAbi,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800448 pkg.mVersionCode, pkgFlags, pkgPrivateFlags, user, add, true /* allowInstall */,
449 parentPackageName, childPackageNames);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700450 return p;
451 }
452
Kenny Root447106f2011-03-23 11:00:15 -0700453 PackageSetting peekPackageLPr(String name) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700454 return mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700455 }
456
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700457 void setInstallStatus(String pkgName, final int status) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700458 PackageSetting p = mPackages.get(pkgName);
459 if(p != null) {
460 if(p.getInstallStatus() != status) {
461 p.setInstallStatus(status);
462 }
463 }
464 }
465
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700466 void applyPendingPermissionGrantsLPw(String packageName, int userId) {
467 ArrayMap<String, ArraySet<RestoredPermissionGrant>> grantsByPackage =
468 mRestoredUserGrants.get(userId);
469 if (grantsByPackage == null || grantsByPackage.size() == 0) {
470 return;
471 }
472
473 ArraySet<RestoredPermissionGrant> grants = grantsByPackage.get(packageName);
474 if (grants == null || grants.size() == 0) {
475 return;
476 }
477
478 final PackageSetting ps = mPackages.get(packageName);
479 if (ps == null) {
480 Slog.e(TAG, "Can't find supposedly installed package " + packageName);
481 return;
482 }
483 final PermissionsState perms = ps.getPermissionsState();
484
485 for (RestoredPermissionGrant grant : grants) {
486 BasePermission bp = mPermissions.get(grant.permissionName);
487 if (bp != null) {
488 if (grant.granted) {
489 perms.grantRuntimePermission(bp, userId);
490 }
491 perms.updatePermissionFlags(bp, userId, USER_RUNTIME_GRANT_MASK, grant.grantBits);
492 }
493 }
494
495 // And remove it from the pending-grant bookkeeping
496 grantsByPackage.remove(packageName);
497 if (grantsByPackage.size() < 1) {
498 mRestoredUserGrants.remove(userId);
499 }
500 writeRuntimePermissionsForUserLPr(userId, false);
501 }
502
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -0700503 void setInstallerPackageName(String pkgName, String installerPkgName) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700504 PackageSetting p = mPackages.get(pkgName);
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -0700505 if (p != null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700506 p.setInstallerPackageName(installerPkgName);
Sudheer Shanka29283372016-04-04 20:56:27 -0700507 if (installerPkgName != null) {
508 mInstallerPackages.add(installerPkgName);
509 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700510 }
511 }
512
Kenny Root447106f2011-03-23 11:00:15 -0700513 SharedUserSetting getSharedUserLPw(String name,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800514 int pkgFlags, int pkgPrivateFlags, boolean create) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700515 SharedUserSetting s = mSharedUsers.get(name);
516 if (s == null) {
517 if (!create) {
518 return null;
519 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800520 s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800521 s.userId = newUserIdLPw(s);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700522 Log.i(PackageManagerService.TAG, "New shared user " + name + ": id=" + s.userId);
523 // < 0 means we couldn't assign a userid; fall out and return
524 // s, which is currently null
525 if (s.userId >= 0) {
526 mSharedUsers.put(name, s);
527 }
528 }
529
530 return s;
531 }
532
Narayan Kamathdf6d6dc2014-04-28 14:18:34 +0100533 Collection<SharedUserSetting> getAllSharedUsersLPw() {
534 return mSharedUsers.values();
535 }
536
Svet Ganov354cd3c2015-12-17 11:35:04 -0800537 boolean disableSystemPackageLPw(String name, boolean replaced) {
Kenny Root447106f2011-03-23 11:00:15 -0700538 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700539 if(p == null) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700540 Log.w(PackageManagerService.TAG, "Package " + name + " is not an installed package");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700541 return false;
542 }
Kenny Root447106f2011-03-23 11:00:15 -0700543 final PackageSetting dp = mDisabledSysPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700544 // always make sure the system package code and resource paths dont change
Svet Ganov354cd3c2015-12-17 11:35:04 -0800545 if (dp == null && p.pkg != null && p.pkg.isSystemApp() && !p.pkg.isUpdatedSystemApp()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700546 if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
547 p.pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
548 }
549 mDisabledSysPackages.put(name, p);
550
Svet Ganov354cd3c2015-12-17 11:35:04 -0800551 if (replaced) {
552 // a little trick... when we install the new package, we don't
553 // want to modify the existing PackageSetting for the built-in
554 // version. so at this point we need a new PackageSetting that
555 // is okay to muck with.
556 PackageSetting newp = new PackageSetting(p);
557 replacePackageLPw(name, newp);
Svet Ganov354cd3c2015-12-17 11:35:04 -0800558 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700559 return true;
560 }
561 return false;
562 }
563
Kenny Root447106f2011-03-23 11:00:15 -0700564 PackageSetting enableSystemPackageLPw(String name) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700565 PackageSetting p = mDisabledSysPackages.get(name);
566 if(p == null) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700567 Log.w(PackageManagerService.TAG, "Package " + name + " is not disabled");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700568 return null;
569 }
570 // Reset flag in ApplicationInfo object
571 if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
572 p.pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
573 }
Kenny Root447106f2011-03-23 11:00:15 -0700574 PackageSetting ret = addPackageLPw(name, p.realName, p.codePath, p.resourcePath,
Narayan Kamathff110bd2014-07-04 18:30:45 +0100575 p.legacyNativeLibraryPathString, p.primaryCpuAbiString,
Selim Gurun5c8acb42016-04-07 16:34:02 -0700576 p.secondaryCpuAbiString, p.cpuAbiOverrideString,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800577 p.appId, p.versionCode, p.pkgFlags, p.pkgPrivateFlags,
578 p.parentPackageName, p.childPackageNames);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700579 mDisabledSysPackages.remove(name);
580 return ret;
581 }
582
Kenny Rootc52d6fd2012-05-07 23:04:52 -0700583 boolean isDisabledSystemPackageLPr(String name) {
584 return mDisabledSysPackages.containsKey(name);
585 }
586
587 void removeDisabledSystemPackageLPw(String name) {
588 mDisabledSysPackages.remove(name);
589 }
590
Kenny Root447106f2011-03-23 11:00:15 -0700591 PackageSetting addPackageLPw(String name, String realName, File codePath, File resourcePath,
Svet Ganovf4ff39c2016-02-04 14:27:05 -0800592 String legacyNativeLibraryPathString, String primaryCpuAbiString,
593 String secondaryCpuAbiString, String cpuAbiOverrideString, int uid, int vc, int
594 pkgFlags, int pkgPrivateFlags, String parentPackageName,
595 List<String> childPackageNames) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700596 PackageSetting p = mPackages.get(name);
597 if (p != null) {
Amith Yamasani13593602012-03-22 16:16:17 -0700598 if (p.appId == uid) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700599 return p;
600 }
601 PackageManagerService.reportSettingsProblem(Log.ERROR,
602 "Adding duplicate package, keeping first: " + name);
603 return null;
604 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100605 p = new PackageSetting(name, realName, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100606 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800607 cpuAbiOverrideString, vc, pkgFlags, pkgPrivateFlags, parentPackageName,
608 childPackageNames);
Amith Yamasani13593602012-03-22 16:16:17 -0700609 p.appId = uid;
Kenny Root447106f2011-03-23 11:00:15 -0700610 if (addUserIdLPw(uid, p, name)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700611 mPackages.put(name, p);
612 return p;
613 }
614 return null;
615 }
616
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800617 SharedUserSetting addSharedUserLPw(String name, int uid, int pkgFlags, int pkgPrivateFlags) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700618 SharedUserSetting s = mSharedUsers.get(name);
619 if (s != null) {
620 if (s.userId == uid) {
621 return s;
622 }
623 PackageManagerService.reportSettingsProblem(Log.ERROR,
624 "Adding duplicate shared user, keeping first: " + name);
625 return null;
626 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800627 s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700628 s.userId = uid;
Kenny Root447106f2011-03-23 11:00:15 -0700629 if (addUserIdLPw(uid, s, name)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700630 mSharedUsers.put(name, s);
631 return s;
632 }
633 return null;
634 }
635
Christopher Tate86b391c2013-09-13 16:58:36 -0700636 void pruneSharedUsersLPw() {
637 ArrayList<String> removeStage = new ArrayList<String>();
638 for (Map.Entry<String,SharedUserSetting> entry : mSharedUsers.entrySet()) {
639 final SharedUserSetting sus = entry.getValue();
Todd Kennedy68f67152015-10-21 13:09:10 -0700640 if (sus == null) {
641 removeStage.add(entry.getKey());
642 continue;
643 }
644 // remove packages that are no longer installed
645 for (Iterator<PackageSetting> iter = sus.packages.iterator(); iter.hasNext();) {
646 PackageSetting ps = iter.next();
647 if (mPackages.get(ps.name) == null) {
648 iter.remove();
649 }
650 }
651 if (sus.packages.size() == 0) {
Christopher Tate86b391c2013-09-13 16:58:36 -0700652 removeStage.add(entry.getKey());
653 }
654 }
655 for (int i = 0; i < removeStage.size(); i++) {
656 mSharedUsers.remove(removeStage.get(i));
657 }
658 }
659
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700660 // Transfer ownership of permissions from one package to another.
Kenny Root447106f2011-03-23 11:00:15 -0700661 void transferPermissionsLPw(String origPkg, String newPkg) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700662 // Transfer ownership of permissions to the new package.
663 for (int i=0; i<2; i++) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700664 ArrayMap<String, BasePermission> permissions =
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700665 i == 0 ? mPermissionTrees : mPermissions;
666 for (BasePermission bp : permissions.values()) {
667 if (origPkg.equals(bp.sourcePackage)) {
668 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG,
669 "Moving permission " + bp.name
670 + " from pkg " + bp.sourcePackage
671 + " to " + newPkg);
672 bp.sourcePackage = newPkg;
673 bp.packageSetting = null;
674 bp.perm = null;
675 if (bp.pendingInfo != null) {
676 bp.pendingInfo.packageName = newPkg;
677 }
678 bp.uid = 0;
Jeff Sharkey00f39042015-03-23 16:51:22 -0700679 bp.setGids(null, false);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700680 }
681 }
682 }
683 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700684
Kenny Root447106f2011-03-23 11:00:15 -0700685 private PackageSetting getPackageLPw(String name, PackageSetting origPackage,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700686 String realName, SharedUserSetting sharedUser, File codePath, File resourcePath,
Svetoslavc6d1c342015-02-26 14:44:43 -0800687 String legacyNativeLibraryPathString, String primaryCpuAbiString,
688 String secondaryCpuAbiString, int vc, int pkgFlags, int pkgPrivateFlags,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800689 UserHandle installUser, boolean add, boolean allowInstall, String parentPackage,
690 List<String> childPackageNames) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700691 PackageSetting p = mPackages.get(name);
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700692 UserManagerService userManager = UserManagerService.getInstance();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700693 if (p != null) {
Narayan Kamathff110bd2014-07-04 18:30:45 +0100694 p.primaryCpuAbiString = primaryCpuAbiString;
695 p.secondaryCpuAbiString = secondaryCpuAbiString;
Svet Ganovf4ff39c2016-02-04 14:27:05 -0800696 if (childPackageNames != null) {
697 p.childPackageNames = new ArrayList<>(childPackageNames);
698 }
Narayan Kamath4903f642014-08-11 13:33:45 +0100699
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700700 if (!p.codePath.equals(codePath)) {
701 // Check to see if its a disabled system app
702 if ((p.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0) {
703 // This is an updated system app with versions in both system
704 // and data partition. Just let the most recent version
705 // take precedence.
Amith Yamasani483f3b02012-03-13 16:08:00 -0700706 Slog.w(PackageManagerService.TAG, "Trying to update system app code path from "
707 + p.codePathString + " to " + codePath.toString());
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700708 } else {
709 // Just a change in the code path is not an issue, but
710 // let's log a message about it.
Amith Yamasani483f3b02012-03-13 16:08:00 -0700711 Slog.i(PackageManagerService.TAG, "Package " + name + " codePath changed from "
712 + p.codePath + " to " + codePath + "; Retaining data and using new");
Kenji Sugimotoea3064a2015-02-19 15:03:30 +0900713
714 // The owner user's installed flag is set false
715 // when the application was installed by other user
716 // and the installed flag is not updated
717 // when the application is appended as system app later.
718 if ((pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0 &&
719 getDisabledSystemPkgLPr(name) == null) {
720 List<UserInfo> allUserInfos = getAllUsers();
721 if (allUserInfos != null) {
722 for (UserInfo userInfo : allUserInfos) {
723 p.setInstalled(true, userInfo.id);
724 }
725 }
726 }
727
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700728 /*
729 * Since we've changed paths, we need to prefer the new
730 * native library path over the one stored in the
731 * package settings since we might have moved from
732 * internal to external storage or vice versa.
733 */
Narayan Kamathff110bd2014-07-04 18:30:45 +0100734 p.legacyNativeLibraryPathString = legacyNativeLibraryPathString;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700735 }
736 }
737 if (p.sharedUser != sharedUser) {
738 PackageManagerService.reportSettingsProblem(Log.WARN,
739 "Package " + name + " shared user changed from "
740 + (p.sharedUser != null ? p.sharedUser.name : "<nothing>")
741 + " to "
742 + (sharedUser != null ? sharedUser.name : "<nothing>")
743 + "; replacing with new");
744 p = null;
745 } else {
Christopher Tate9f088202013-10-22 15:36:01 -0700746 // If what we are scanning is a system (and possibly privileged) package,
747 // then make it so, regardless of whether it was previously installed only
748 // in the data partition.
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800749 p.pkgFlags |= pkgFlags & ApplicationInfo.FLAG_SYSTEM;
750 p.pkgPrivateFlags |= pkgPrivateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700751 }
752 }
753 if (p == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700754 if (origPackage != null) {
755 // We are consuming the data from an existing package.
756 p = new PackageSetting(origPackage.name, name, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100757 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800758 null /* cpuAbiOverrideString */, vc, pkgFlags, pkgPrivateFlags,
759 parentPackage, childPackageNames);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700760 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG, "Package "
761 + name + " is adopting original package " + origPackage.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700762 // Note that we will retain the new package's signature so
763 // that we can keep its data.
764 PackageSignatures s = p.signatures;
765 p.copyFrom(origPackage);
766 p.signatures = s;
767 p.sharedUser = origPackage.sharedUser;
Amith Yamasani13593602012-03-22 16:16:17 -0700768 p.appId = origPackage.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700769 p.origPackage = origPackage;
Svet Ganove7af1942015-08-04 11:13:44 -0700770 p.getPermissionsState().copyFrom(origPackage.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700771 mRenamedPackages.put(name, origPackage.name);
772 name = origPackage.name;
773 // Update new package state.
774 p.setTimeStamp(codePath.lastModified());
775 } else {
776 p = new PackageSetting(name, realName, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100777 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800778 null /* cpuAbiOverrideString */, vc, pkgFlags, pkgPrivateFlags,
779 parentPackage, childPackageNames);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700780 p.setTimeStamp(codePath.lastModified());
781 p.sharedUser = sharedUser;
782 // If this is not a system app, it starts out stopped.
783 if ((pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) {
Kenny Root447106f2011-03-23 11:00:15 -0700784 if (DEBUG_STOPPED) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700785 RuntimeException e = new RuntimeException("here");
786 e.fillInStackTrace();
787 Slog.i(PackageManagerService.TAG, "Stopping package " + name, e);
788 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700789 List<UserInfo> users = getAllUsers();
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700790 final int installUserId = installUser != null ? installUser.getIdentifier() : 0;
Amith Yamasanif031f232012-10-26 15:35:21 -0700791 if (users != null && allowInstall) {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700792 for (UserInfo user : users) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700793 // By default we consider this app to be installed
794 // for the user if no user has been specified (which
795 // means to leave it at its original value, and the
796 // original default value is true), or we are being
797 // asked to install for all users, or this is the
798 // user we are installing for.
799 final boolean installed = installUser == null
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700800 || (installUserId == UserHandle.USER_ALL
801 && !isAdbInstallDisallowed(userManager, user.id))
802 || installUserId == user.id;
Jeff Sharkey42884192016-04-09 16:12:01 -0600803 p.setUserState(user.id, 0, COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700804 installed,
805 true, // stopped,
806 true, // notLaunched
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700807 false, // hidden
Andrei Stingaceanu1e283912015-11-26 15:26:28 +0000808 false, // suspended
Kenny Guyc13053b2014-05-29 14:17:17 +0100809 null, null, null,
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800810 false, // blockUninstall
Christopher Tatef0d6cb32015-07-10 17:44:53 -0700811 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED, 0);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700812 writePackageRestrictionsLPr(user.id);
813 }
814 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700815 }
816 if (sharedUser != null) {
Amith Yamasani13593602012-03-22 16:16:17 -0700817 p.appId = sharedUser.userId;
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800818 } else {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700819 // Clone the setting here for disabled system packages
820 PackageSetting dis = mDisabledSysPackages.get(name);
821 if (dis != null) {
822 // For disabled packages a new setting is created
823 // from the existing user id. This still has to be
824 // added to list of user id's
825 // Copy signatures from previous setting
826 if (dis.signatures.mSignatures != null) {
827 p.signatures.mSignatures = dis.signatures.mSignatures.clone();
828 }
Amith Yamasani13593602012-03-22 16:16:17 -0700829 p.appId = dis.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700830 // Clone permissions
Svetoslavc6d1c342015-02-26 14:44:43 -0800831 p.getPermissionsState().copyFrom(dis.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700832 // Clone component info
Amith Yamasani483f3b02012-03-13 16:08:00 -0700833 List<UserInfo> users = getAllUsers();
834 if (users != null) {
835 for (UserInfo user : users) {
836 int userId = user.id;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700837 p.setDisabledComponentsCopy(
838 dis.getDisabledComponents(userId), userId);
839 p.setEnabledComponentsCopy(
840 dis.getEnabledComponents(userId), userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700841 }
842 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700843 // Add new setting to list of user ids
Amith Yamasani13593602012-03-22 16:16:17 -0700844 addUserIdLPw(p.appId, p, name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700845 } else {
846 // Assign new user id
Amith Yamasani13593602012-03-22 16:16:17 -0700847 p.appId = newUserIdLPw(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700848 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700849 }
850 }
Amith Yamasani13593602012-03-22 16:16:17 -0700851 if (p.appId < 0) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700852 PackageManagerService.reportSettingsProblem(Log.WARN,
853 "Package " + name + " could not be assigned a valid uid");
854 return null;
855 }
856 if (add) {
857 // Finish adding new package by adding it and updating shared
858 // user preferences
Kenny Root447106f2011-03-23 11:00:15 -0700859 addPackageSettingLPw(p, name, sharedUser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700860 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700861 } else {
Amith Yamasanif031f232012-10-26 15:35:21 -0700862 if (installUser != null && allowInstall) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700863 // The caller has explicitly specified the user they want this
864 // package installed for, and the package already exists.
865 // Make sure it conforms to the new request.
866 List<UserInfo> users = getAllUsers();
867 if (users != null) {
868 for (UserInfo user : users) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700869 if ((installUser.getIdentifier() == UserHandle.USER_ALL
870 && !isAdbInstallDisallowed(userManager, user.id))
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700871 || installUser.getIdentifier() == user.id) {
872 boolean installed = p.getInstalled(user.id);
873 if (!installed) {
874 p.setInstalled(true, user.id);
875 writePackageRestrictionsLPr(user.id);
876 }
877 }
878 }
879 }
880 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700881 }
882 return p;
883 }
884
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700885 boolean isAdbInstallDisallowed(UserManagerService userManager, int userId) {
886 return userManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES,
887 userId);
888 }
889
Kenny Root447106f2011-03-23 11:00:15 -0700890 void insertPackageSettingLPw(PackageSetting p, PackageParser.Package pkg) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700891 p.pkg = pkg;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700892 // pkg.mSetEnabled = p.getEnabled(userId);
893 // pkg.mSetStopped = p.getStopped(userId);
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700894 final String volumeUuid = pkg.applicationInfo.volumeUuid;
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700895 final String codePath = pkg.applicationInfo.getCodePath();
896 final String resourcePath = pkg.applicationInfo.getResourcePath();
Jeff Sharkey84f12942014-07-10 17:48:11 -0700897 final String legacyNativeLibraryPath = pkg.applicationInfo.nativeLibraryRootDir;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700898 // Update volume if needed
899 if (!Objects.equals(volumeUuid, p.volumeUuid)) {
900 Slog.w(PackageManagerService.TAG, "Volume for " + p.pkg.packageName +
901 " changing from " + p.volumeUuid + " to " + volumeUuid);
902 p.volumeUuid = volumeUuid;
903 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700904 // Update code path if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700905 if (!Objects.equals(codePath, p.codePathString)) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700906 Slog.w(PackageManagerService.TAG, "Code path for " + p.pkg.packageName +
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700907 " changing from " + p.codePathString + " to " + codePath);
908 p.codePath = new File(codePath);
909 p.codePathString = codePath;
910 }
911 //Update resource path if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700912 if (!Objects.equals(resourcePath, p.resourcePathString)) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700913 Slog.w(PackageManagerService.TAG, "Resource path for " + p.pkg.packageName +
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700914 " changing from " + p.resourcePathString + " to " + resourcePath);
915 p.resourcePath = new File(resourcePath);
916 p.resourcePathString = resourcePath;
917 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100918 // Update the native library paths if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700919 if (!Objects.equals(legacyNativeLibraryPath, p.legacyNativeLibraryPathString)) {
920 p.legacyNativeLibraryPathString = legacyNativeLibraryPath;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700921 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100922
Narayan Kamath9e289d72014-04-10 09:26:59 +0000923 // Update the required Cpu Abi
Narayan Kamathff110bd2014-07-04 18:30:45 +0100924 p.primaryCpuAbiString = pkg.applicationInfo.primaryCpuAbi;
925 p.secondaryCpuAbiString = pkg.applicationInfo.secondaryCpuAbi;
Narayan Kamath4903f642014-08-11 13:33:45 +0100926 p.cpuAbiOverrideString = pkg.cpuAbiOverride;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700927 // Update version code if needed
Amith Yamasani483f3b02012-03-13 16:08:00 -0700928 if (pkg.mVersionCode != p.versionCode) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700929 p.versionCode = pkg.mVersionCode;
930 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700931 // Update signatures if needed.
932 if (p.signatures.mSignatures == null) {
933 p.signatures.assignSignatures(pkg.mSignatures);
934 }
Kenny Root5455f682012-09-09 14:52:10 -0700935 // Update flags if needed.
936 if (pkg.applicationInfo.flags != p.pkgFlags) {
937 p.pkgFlags = pkg.applicationInfo.flags;
938 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700939 // If this app defines a shared user id initialize
940 // the shared user signatures as well.
941 if (p.sharedUser != null && p.sharedUser.signatures.mSignatures == null) {
942 p.sharedUser.signatures.assignSignatures(pkg.mSignatures);
943 }
Kenny Root447106f2011-03-23 11:00:15 -0700944 addPackageSettingLPw(p, pkg.packageName, p.sharedUser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700945 }
946
947 // Utility method that adds a PackageSetting to mPackages and
Christopher Tate6038d152015-06-17 13:07:46 -0700948 // completes updating the shared user attributes and any restored
949 // app link verification state
Kenny Root447106f2011-03-23 11:00:15 -0700950 private void addPackageSettingLPw(PackageSetting p, String name,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700951 SharedUserSetting sharedUser) {
952 mPackages.put(name, p);
953 if (sharedUser != null) {
954 if (p.sharedUser != null && p.sharedUser != sharedUser) {
955 PackageManagerService.reportSettingsProblem(Log.ERROR,
956 "Package " + p.name + " was user "
957 + p.sharedUser + " but is now " + sharedUser
958 + "; I am not changing its files so it will probably fail!");
Ben Gruverdd72c9e2013-08-06 12:34:17 -0700959 p.sharedUser.removePackage(p);
Amith Yamasani13593602012-03-22 16:16:17 -0700960 } else if (p.appId != sharedUser.userId) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700961 PackageManagerService.reportSettingsProblem(Log.ERROR,
Amith Yamasani13593602012-03-22 16:16:17 -0700962 "Package " + p.name + " was user id " + p.appId
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700963 + " but is now user " + sharedUser
964 + " with id " + sharedUser.userId
965 + "; I am not changing its files so it will probably fail!");
966 }
967
Ben Gruverdd72c9e2013-08-06 12:34:17 -0700968 sharedUser.addPackage(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700969 p.sharedUser = sharedUser;
Amith Yamasani13593602012-03-22 16:16:17 -0700970 p.appId = sharedUser.userId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700971 }
Svet Ganove7af1942015-08-04 11:13:44 -0700972
973 // If the we know about this user id, we have to update it as it
974 // has to point to the same PackageSetting instance as the package.
975 Object userIdPs = getUserIdLPr(p.appId);
976 if (sharedUser == null) {
977 if (userIdPs != null && userIdPs != p) {
978 replaceUserIdLPw(p.appId, p);
979 }
980 } else {
981 if (userIdPs != null && userIdPs != sharedUser) {
982 replaceUserIdLPw(p.appId, sharedUser);
983 }
984 }
985
Christopher Tate6038d152015-06-17 13:07:46 -0700986 IntentFilterVerificationInfo ivi = mRestoredIntentFilterVerifications.get(name);
987 if (ivi != null) {
988 if (DEBUG_DOMAIN_VERIFICATION) {
989 Slog.i(TAG, "Applying restored IVI for " + name + " : " + ivi.getStatusString());
990 }
991 mRestoredIntentFilterVerifications.remove(name);
992 p.setIntentFilterVerificationInfo(ivi);
993 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700994 }
995
996 /*
997 * Update the shared user setting when a package using
998 * specifying the shared user id is removed. The gids
999 * associated with each permission of the deleted package
1000 * are removed from the shared user's gid list only if its
1001 * not in use by other permissions of packages in the
1002 * shared user setting.
1003 */
Svetoslavc6d1c342015-02-26 14:44:43 -08001004 int updateSharedUserPermsLPw(PackageSetting deletedPs, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -07001005 if ((deletedPs == null) || (deletedPs.pkg == null)) {
1006 Slog.i(PackageManagerService.TAG,
1007 "Trying to update info for null package. Just ignoring");
Svetoslavc6d1c342015-02-26 14:44:43 -08001008 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001009 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001010
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001011 // No sharedUserId
1012 if (deletedPs.sharedUser == null) {
Svetoslavc6d1c342015-02-26 14:44:43 -08001013 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001014 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001015
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001016 SharedUserSetting sus = deletedPs.sharedUser;
Svetoslavc6d1c342015-02-26 14:44:43 -08001017
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001018 // Update permissions
Kenny Root447106f2011-03-23 11:00:15 -07001019 for (String eachPerm : deletedPs.pkg.requestedPermissions) {
Svetoslavc6d1c342015-02-26 14:44:43 -08001020 BasePermission bp = mPermissions.get(eachPerm);
1021 if (bp == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001022 continue;
1023 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001024
Svetoslavc6d1c342015-02-26 14:44:43 -08001025 // Check if another package in the shared user needs the permission.
Svetoslav4a5f4a22015-07-07 18:18:15 -07001026 boolean used = false;
Svetoslavc6d1c342015-02-26 14:44:43 -08001027 for (PackageSetting pkg : sus.packages) {
1028 if (pkg.pkg != null
1029 && !pkg.pkg.packageName.equals(deletedPs.pkg.packageName)
1030 && pkg.pkg.requestedPermissions.contains(eachPerm)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001031 used = true;
1032 break;
1033 }
1034 }
Svetoslav4a5f4a22015-07-07 18:18:15 -07001035 if (used) {
1036 continue;
1037 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001038
Svetoslav4a5f4a22015-07-07 18:18:15 -07001039 PermissionsState permissionsState = sus.getPermissionsState();
1040 PackageSetting disabledPs = getDisabledSystemPkgLPr(deletedPs.pkg.packageName);
Svet Ganov8c7f7002015-05-07 10:48:44 -07001041
Svetoslav4a5f4a22015-07-07 18:18:15 -07001042 // If the package is shadowing is a disabled system package,
1043 // do not drop permissions that the shadowed package requests.
1044 if (disabledPs != null) {
1045 boolean reqByDisabledSysPkg = false;
1046 for (String permission : disabledPs.pkg.requestedPermissions) {
1047 if (permission.equals(eachPerm)) {
1048 reqByDisabledSysPkg = true;
1049 break;
1050 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001051 }
Svetoslav4a5f4a22015-07-07 18:18:15 -07001052 if (reqByDisabledSysPkg) {
1053 continue;
Svetoslavc6d1c342015-02-26 14:44:43 -08001054 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001055 }
Svetoslav4a5f4a22015-07-07 18:18:15 -07001056
1057 // Try to revoke as an install permission which is for all users.
1058 // The package is gone - no need to keep flags for applying policy.
1059 permissionsState.updatePermissionFlags(bp, userId,
1060 PackageManager.MASK_PERMISSION_FLAGS, 0);
1061
1062 if (permissionsState.revokeInstallPermission(bp) ==
1063 PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED) {
1064 return UserHandle.USER_ALL;
1065 }
1066
1067 // Try to revoke as an install permission which is per user.
1068 if (permissionsState.revokeRuntimePermission(bp, userId) ==
1069 PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED) {
1070 return userId;
1071 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001072 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001073
1074 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001075 }
1076
Kenny Root447106f2011-03-23 11:00:15 -07001077 int removePackageLPw(String name) {
1078 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001079 if (p != null) {
1080 mPackages.remove(name);
Sudheer Shanka29283372016-04-04 20:56:27 -07001081 removeInstallerPackageStatus(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001082 if (p.sharedUser != null) {
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001083 p.sharedUser.removePackage(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001084 if (p.sharedUser.packages.size() == 0) {
1085 mSharedUsers.remove(p.sharedUser.name);
Kenny Root447106f2011-03-23 11:00:15 -07001086 removeUserIdLPw(p.sharedUser.userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001087 return p.sharedUser.userId;
1088 }
1089 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07001090 removeUserIdLPw(p.appId);
1091 return p.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001092 }
1093 }
1094 return -1;
1095 }
1096
Sudheer Shanka29283372016-04-04 20:56:27 -07001097 /**
1098 * Checks if {@param packageName} is an installer package and if so, clear the installer
1099 * package name of the packages that are installed by this.
1100 */
1101 private void removeInstallerPackageStatus(String packageName) {
1102 // Check if the package to be removed is an installer package.
1103 if (!mInstallerPackages.contains(packageName)) {
1104 return;
1105 }
1106 for (int i = 0; i < mPackages.size(); i++) {
1107 final PackageSetting ps = mPackages.valueAt(i);
1108 final String installerPackageName = ps.getInstallerPackageName();
1109 if (installerPackageName != null
1110 && installerPackageName.equals(packageName)) {
1111 ps.setInstallerPackageName(null);
Sudheer Shanka8c57aea2016-04-20 16:47:50 -07001112 ps.isOrphaned = true;
Sudheer Shanka29283372016-04-04 20:56:27 -07001113 }
1114 }
1115 mInstallerPackages.remove(packageName);
1116 }
1117
Kenny Root447106f2011-03-23 11:00:15 -07001118 private void replacePackageLPw(String name, PackageSetting newp) {
1119 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001120 if (p != null) {
1121 if (p.sharedUser != null) {
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001122 p.sharedUser.removePackage(p);
1123 p.sharedUser.addPackage(newp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001124 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07001125 replaceUserIdLPw(p.appId, newp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001126 }
1127 }
1128 mPackages.put(name, newp);
1129 }
1130
Kenny Root447106f2011-03-23 11:00:15 -07001131 private boolean addUserIdLPw(int uid, Object obj, Object name) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001132 if (uid > Process.LAST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001133 return false;
1134 }
1135
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001136 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001137 int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001138 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001139 while (index >= N) {
1140 mUserIds.add(null);
1141 N++;
1142 }
1143 if (mUserIds.get(index) != null) {
1144 PackageManagerService.reportSettingsProblem(Log.ERROR,
1145 "Adding duplicate user id: " + uid
1146 + " name=" + name);
1147 return false;
1148 }
1149 mUserIds.set(index, obj);
1150 } else {
1151 if (mOtherUserIds.get(uid) != null) {
1152 PackageManagerService.reportSettingsProblem(Log.ERROR,
1153 "Adding duplicate shared id: " + uid
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001154 + " name=" + name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001155 return false;
1156 }
1157 mOtherUserIds.put(uid, obj);
1158 }
1159 return true;
1160 }
1161
Kenny Root447106f2011-03-23 11:00:15 -07001162 public Object getUserIdLPr(int uid) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001163 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001164 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001165 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001166 return index < N ? mUserIds.get(index) : null;
1167 } else {
1168 return mOtherUserIds.get(uid);
1169 }
1170 }
1171
Kenny Root447106f2011-03-23 11:00:15 -07001172 private void removeUserIdLPw(int uid) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001173 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001174 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001175 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001176 if (index < N) mUserIds.set(index, null);
1177 } else {
1178 mOtherUserIds.remove(uid);
1179 }
Geremy Condra12c18382013-03-06 16:49:06 -08001180 setFirstAvailableUid(uid+1);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001181 }
1182
Kenny Root447106f2011-03-23 11:00:15 -07001183 private void replaceUserIdLPw(int uid, Object obj) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001184 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001185 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001186 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001187 if (index < N) mUserIds.set(index, obj);
1188 } else {
1189 mOtherUserIds.put(uid, obj);
1190 }
1191 }
1192
Dianne Hackborn63092712012-10-07 14:45:35 -07001193 PreferredIntentResolver editPreferredActivitiesLPw(int userId) {
1194 PreferredIntentResolver pir = mPreferredActivities.get(userId);
1195 if (pir == null) {
1196 pir = new PreferredIntentResolver();
1197 mPreferredActivities.put(userId, pir);
1198 }
1199 return pir;
1200 }
1201
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001202 PersistentPreferredIntentResolver editPersistentPreferredActivitiesLPw(int userId) {
1203 PersistentPreferredIntentResolver ppir = mPersistentPreferredActivities.get(userId);
1204 if (ppir == null) {
1205 ppir = new PersistentPreferredIntentResolver();
1206 mPersistentPreferredActivities.put(userId, ppir);
1207 }
1208 return ppir;
1209 }
1210
Nicolas Prevot81948992014-05-16 18:25:26 +01001211 CrossProfileIntentResolver editCrossProfileIntentResolverLPw(int userId) {
1212 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(userId);
1213 if (cpir == null) {
1214 cpir = new CrossProfileIntentResolver();
1215 mCrossProfileIntentResolvers.put(userId, cpir);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001216 }
Nicolas Prevot81948992014-05-16 18:25:26 +01001217 return cpir;
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001218 }
1219
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001220 /**
1221 * The following functions suppose that you have a lock for managing access to the
1222 * mIntentFiltersVerifications map.
1223 */
1224
1225 /* package protected */
1226 IntentFilterVerificationInfo getIntentFilterVerificationLPr(String packageName) {
1227 PackageSetting ps = mPackages.get(packageName);
1228 if (ps == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001229 if (DEBUG_DOMAIN_VERIFICATION) {
1230 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1231 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001232 return null;
1233 }
1234 return ps.getIntentFilterVerificationInfo();
1235 }
1236
1237 /* package protected */
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -07001238 IntentFilterVerificationInfo createIntentFilterVerificationIfNeededLPw(String packageName,
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001239 ArrayList<String> domains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001240 PackageSetting ps = mPackages.get(packageName);
1241 if (ps == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001242 if (DEBUG_DOMAIN_VERIFICATION) {
1243 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1244 }
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -07001245 return null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001246 }
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -07001247 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
1248 if (ivi == null) {
1249 ivi = new IntentFilterVerificationInfo(packageName, domains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001250 ps.setIntentFilterVerificationInfo(ivi);
Christopher Tate72c10a22015-06-12 18:31:24 -07001251 if (DEBUG_DOMAIN_VERIFICATION) {
1252 Slog.d(PackageManagerService.TAG,
1253 "Creating new IntentFilterVerificationInfo for pkg: " + packageName);
1254 }
Fabrice Di Meglio1de3f0d2015-04-29 19:42:41 -07001255 } else {
1256 ivi.setDomains(domains);
Christopher Tate72c10a22015-06-12 18:31:24 -07001257 if (DEBUG_DOMAIN_VERIFICATION) {
1258 Slog.d(PackageManagerService.TAG,
1259 "Setting domains to existing IntentFilterVerificationInfo for pkg: " +
1260 packageName + " and with domains: " + ivi.getDomainsString());
1261 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001262 }
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -07001263 return ivi;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001264 }
1265
1266 int getIntentFilterVerificationStatusLPr(String packageName, int userId) {
1267 PackageSetting ps = mPackages.get(packageName);
1268 if (ps == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001269 if (DEBUG_DOMAIN_VERIFICATION) {
1270 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1271 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001272 return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1273 }
Christopher Tated85a6642015-07-17 15:39:08 -07001274 return (int)(ps.getDomainVerificationStatusForUser(userId) >> 32);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001275 }
1276
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001277 boolean updateIntentFilterVerificationStatusLPw(String packageName, final int status, int userId) {
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001278 // Update the status for the current package
1279 PackageSetting current = mPackages.get(packageName);
1280 if (current == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001281 if (DEBUG_DOMAIN_VERIFICATION) {
1282 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1283 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001284 return false;
1285 }
Christopher Tate050aee22015-07-01 16:50:43 -07001286
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001287 final int alwaysGeneration;
1288 if (status == INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS) {
1289 alwaysGeneration = mNextAppLinkGeneration.get(userId) + 1;
1290 mNextAppLinkGeneration.put(userId, alwaysGeneration);
1291 } else {
1292 alwaysGeneration = 0;
1293 }
1294
1295 current.setDomainVerificationStatusForUser(status, alwaysGeneration, userId);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001296 return true;
1297 }
1298
1299 /**
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001300 * Used for Settings App and PackageManagerService dump. Should be read only.
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001301 */
1302 List<IntentFilterVerificationInfo> getIntentFilterVerificationsLPr(
1303 String packageName) {
1304 if (packageName == null) {
1305 return Collections.<IntentFilterVerificationInfo>emptyList();
1306 }
1307 ArrayList<IntentFilterVerificationInfo> result = new ArrayList<>();
1308 for (PackageSetting ps : mPackages.values()) {
1309 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
1310 if (ivi == null || TextUtils.isEmpty(ivi.getPackageName()) ||
1311 !ivi.getPackageName().equalsIgnoreCase(packageName)) {
1312 continue;
1313 }
1314 result.add(ivi);
1315 }
1316 return result;
1317 }
1318
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001319 boolean removeIntentFilterVerificationLPw(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001320 PackageSetting ps = mPackages.get(packageName);
1321 if (ps == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001322 if (DEBUG_DOMAIN_VERIFICATION) {
1323 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1324 }
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001325 return false;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001326 }
1327 ps.clearDomainVerificationStatusForUser(userId);
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001328 return true;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001329 }
1330
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001331 boolean removeIntentFilterVerificationLPw(String packageName, int[] userIds) {
1332 boolean result = false;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001333 for (int userId : userIds) {
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001334 result |= removeIntentFilterVerificationLPw(packageName, userId);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001335 }
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001336 return result;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001337 }
1338
Christopher Tatedb3fe812015-06-24 16:15:48 -07001339 boolean setDefaultBrowserPackageNameLPw(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001340 if (userId == UserHandle.USER_ALL) {
1341 return false;
1342 }
1343 mDefaultBrowserApp.put(userId, packageName);
1344 writePackageRestrictionsLPr(userId);
1345 return true;
1346 }
1347
1348 String getDefaultBrowserPackageNameLPw(int userId) {
1349 return (userId == UserHandle.USER_ALL) ? null : mDefaultBrowserApp.get(userId);
1350 }
1351
Robin Lee0dc591b2016-05-04 13:01:46 +01001352 boolean setDefaultDialerPackageNameLPw(String packageName, int userId) {
1353 if (userId == UserHandle.USER_ALL) {
1354 return false;
1355 }
1356 mDefaultDialerApp.put(userId, packageName);
1357 writePackageRestrictionsLPr(userId);
1358 return true;
1359 }
1360
1361 String getDefaultDialerPackageNameLPw(int userId) {
1362 return (userId == UserHandle.USER_ALL) ? null : mDefaultDialerApp.get(userId);
1363 }
1364
Amith Yamasani483f3b02012-03-13 16:08:00 -07001365 private File getUserPackagesStateFile(int userId) {
Svetoslavc6d1c342015-02-26 14:44:43 -08001366 // TODO: Implement a cleaner solution when adding tests.
1367 // This instead of Environment.getUserSystemDirectory(userId) to support testing.
1368 File userDir = new File(new File(mSystemDir, "users"), Integer.toString(userId));
1369 return new File(userDir, "package-restrictions.xml");
1370 }
1371
1372 private File getUserRuntimePermissionsFile(int userId) {
1373 // TODO: Implement a cleaner solution when adding tests.
1374 // This instead of Environment.getUserSystemDirectory(userId) to support testing.
1375 File userDir = new File(new File(mSystemDir, "users"), Integer.toString(userId));
1376 return new File(userDir, RUNTIME_PERMISSIONS_FILE_NAME);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001377 }
1378
1379 private File getUserPackagesStateBackupFile(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -07001380 return new File(Environment.getUserSystemDirectory(userId),
1381 "package-restrictions-backup.xml");
Amith Yamasani483f3b02012-03-13 16:08:00 -07001382 }
1383
1384 void writeAllUsersPackageRestrictionsLPr() {
1385 List<UserInfo> users = getAllUsers();
1386 if (users == null) return;
1387
1388 for (UserInfo user : users) {
1389 writePackageRestrictionsLPr(user.id);
1390 }
1391 }
1392
Svetoslavc6d1c342015-02-26 14:44:43 -08001393 void writeAllRuntimePermissionsLPr() {
1394 for (int userId : UserManagerService.getInstance().getUserIds()) {
1395 mRuntimePermissionsPersistence.writePermissionsForUserAsyncLPr(userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001396 }
1397 }
1398
Svet Ganovba3ba812015-06-26 10:54:06 -07001399 boolean areDefaultRuntimePermissionsGrantedLPr(int userId) {
1400 return mRuntimePermissionsPersistence
1401 .areDefaultRuntimPermissionsGrantedLPr(userId);
1402 }
1403
1404 void onDefaultRuntimePermissionsGrantedLPr(int userId) {
1405 mRuntimePermissionsPersistence
1406 .onDefaultRuntimePermissionsGrantedLPr(userId);
1407 }
1408
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001409 public VersionInfo findOrCreateVersion(String volumeUuid) {
1410 VersionInfo ver = mVersion.get(volumeUuid);
1411 if (ver == null) {
1412 ver = new VersionInfo();
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001413 mVersion.put(volumeUuid, ver);
1414 }
1415 return ver;
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001416 }
1417
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001418 public VersionInfo getInternalVersion() {
1419 return mVersion.get(StorageManager.UUID_PRIVATE_INTERNAL);
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001420 }
1421
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001422 public VersionInfo getExternalVersion() {
1423 return mVersion.get(StorageManager.UUID_PRIMARY_PHYSICAL);
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001424 }
1425
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001426 public void onVolumeForgotten(String fsUuid) {
1427 mVersion.remove(fsUuid);
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001428 }
1429
Christopher Tatee012a232015-04-01 17:18:50 -07001430 /**
1431 * Applies the preferred activity state described by the given XML. This code
1432 * also supports the restore-from-backup code path.
1433 *
1434 * @see PreferredActivityBackupHelper
1435 */
1436 void readPreferredActivitiesLPw(XmlPullParser parser, int userId)
Dianne Hackborn63092712012-10-07 14:45:35 -07001437 throws XmlPullParserException, IOException {
1438 int outerDepth = parser.getDepth();
1439 int type;
1440 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1441 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1442 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1443 continue;
1444 }
1445
1446 String tagName = parser.getName();
1447 if (tagName.equals(TAG_ITEM)) {
1448 PreferredActivity pa = new PreferredActivity(parser);
1449 if (pa.mPref.getParseError() == null) {
1450 editPreferredActivitiesLPw(userId).addFilter(pa);
1451 } else {
1452 PackageManagerService.reportSettingsProblem(Log.WARN,
1453 "Error in package manager settings: <preferred-activity> "
1454 + pa.mPref.getParseError() + " at "
1455 + parser.getPositionDescription());
1456 }
1457 } else {
1458 PackageManagerService.reportSettingsProblem(Log.WARN,
1459 "Unknown element under <preferred-activities>: " + parser.getName());
1460 XmlUtils.skipCurrentTag(parser);
1461 }
1462 }
1463 }
1464
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001465 private void readPersistentPreferredActivitiesLPw(XmlPullParser parser, int userId)
1466 throws XmlPullParserException, IOException {
1467 int outerDepth = parser.getDepth();
1468 int type;
1469 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1470 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1471 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1472 continue;
1473 }
1474 String tagName = parser.getName();
1475 if (tagName.equals(TAG_ITEM)) {
1476 PersistentPreferredActivity ppa = new PersistentPreferredActivity(parser);
1477 editPersistentPreferredActivitiesLPw(userId).addFilter(ppa);
1478 } else {
1479 PackageManagerService.reportSettingsProblem(Log.WARN,
Sander Alewijnseaf597622014-03-20 18:44:57 +00001480 "Unknown element under <" + TAG_PERSISTENT_PREFERRED_ACTIVITIES + ">: "
1481 + parser.getName());
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001482 XmlUtils.skipCurrentTag(parser);
1483 }
1484 }
1485 }
1486
Nicolas Prevot81948992014-05-16 18:25:26 +01001487 private void readCrossProfileIntentFiltersLPw(XmlPullParser parser, int userId)
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001488 throws XmlPullParserException, IOException {
1489 int outerDepth = parser.getDepth();
1490 int type;
1491 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1492 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1493 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1494 continue;
1495 }
Christopher Tate6038d152015-06-17 13:07:46 -07001496 final String tagName = parser.getName();
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001497 if (tagName.equals(TAG_ITEM)) {
Nicolas Prevot81948992014-05-16 18:25:26 +01001498 CrossProfileIntentFilter cpif = new CrossProfileIntentFilter(parser);
1499 editCrossProfileIntentResolverLPw(userId).addFilter(cpif);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001500 } else {
Nicolas Prevot81948992014-05-16 18:25:26 +01001501 String msg = "Unknown element under " + TAG_CROSS_PROFILE_INTENT_FILTERS + ": " +
Christopher Tate6038d152015-06-17 13:07:46 -07001502 tagName;
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001503 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
1504 XmlUtils.skipCurrentTag(parser);
1505 }
1506 }
1507 }
1508
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001509 private void readDomainVerificationLPw(XmlPullParser parser, PackageSettingBase packageSetting)
1510 throws XmlPullParserException, IOException {
1511 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser);
1512 packageSetting.setIntentFilterVerificationInfo(ivi);
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001513 Log.d(TAG, "Read domain verification for package: " + ivi.getPackageName());
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001514 }
1515
Christopher Tate6038d152015-06-17 13:07:46 -07001516 private void readRestoredIntentFilterVerifications(XmlPullParser parser)
1517 throws XmlPullParserException, IOException {
1518 int outerDepth = parser.getDepth();
1519 int type;
1520 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1521 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1522 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1523 continue;
1524 }
1525 final String tagName = parser.getName();
1526 if (tagName.equals(TAG_DOMAIN_VERIFICATION)) {
1527 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser);
1528 if (DEBUG_DOMAIN_VERIFICATION) {
1529 Slog.i(TAG, "Restored IVI for " + ivi.getPackageName()
1530 + " status=" + ivi.getStatusString());
1531 }
1532 mRestoredIntentFilterVerifications.put(ivi.getPackageName(), ivi);
1533 } else {
1534 Slog.w(TAG, "Unknown element: " + tagName);
1535 XmlUtils.skipCurrentTag(parser);
1536 }
1537 }
1538 }
1539
1540 void readDefaultAppsLPw(XmlPullParser parser, int userId)
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001541 throws XmlPullParserException, IOException {
1542 int outerDepth = parser.getDepth();
1543 int type;
1544 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1545 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1546 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1547 continue;
1548 }
1549 String tagName = parser.getName();
1550 if (tagName.equals(TAG_DEFAULT_BROWSER)) {
1551 String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
1552 mDefaultBrowserApp.put(userId, packageName);
Robin Lee0dc591b2016-05-04 13:01:46 +01001553 } else if (tagName.equals(TAG_DEFAULT_DIALER)) {
1554 String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
1555 mDefaultDialerApp.put(userId, packageName);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001556 } else {
1557 String msg = "Unknown element under " + TAG_DEFAULT_APPS + ": " +
1558 parser.getName();
1559 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
1560 XmlUtils.skipCurrentTag(parser);
1561 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001562 }
1563 }
1564
Amith Yamasani483f3b02012-03-13 16:08:00 -07001565 void readPackageRestrictionsLPr(int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001566 if (DEBUG_MU) {
1567 Log.i(TAG, "Reading package restrictions for user=" + userId);
1568 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07001569 FileInputStream str = null;
1570 File userPackagesStateFile = getUserPackagesStateFile(userId);
1571 File backupFile = getUserPackagesStateBackupFile(userId);
1572 if (backupFile.exists()) {
1573 try {
1574 str = new FileInputStream(backupFile);
1575 mReadMessages.append("Reading from backup stopped packages file\n");
1576 PackageManagerService.reportSettingsProblem(Log.INFO,
1577 "Need to read from backup stopped packages file");
1578 if (userPackagesStateFile.exists()) {
1579 // If both the backup and normal file exist, we
1580 // ignore the normal one since it might have been
1581 // corrupted.
1582 Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file "
1583 + userPackagesStateFile);
1584 userPackagesStateFile.delete();
1585 }
1586 } catch (java.io.IOException e) {
1587 // We'll try for the normal settings file.
1588 }
1589 }
1590
1591 try {
1592 if (str == null) {
1593 if (!userPackagesStateFile.exists()) {
1594 mReadMessages.append("No stopped packages file found\n");
1595 PackageManagerService.reportSettingsProblem(Log.INFO,
1596 "No stopped packages file; "
1597 + "assuming all started");
1598 // At first boot, make sure no packages are stopped.
1599 // We usually want to have third party apps initialize
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001600 // in the stopped state, but not at first boot. Also
1601 // consider all applications to be installed.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001602 for (PackageSetting pkg : mPackages.values()) {
Jeff Sharkey42884192016-04-09 16:12:01 -06001603 pkg.setUserState(userId, 0, COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001604 true, // installed
1605 false, // stopped
1606 false, // notLaunched
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001607 false, // hidden
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001608 false, // suspended
Kenny Guyc13053b2014-05-29 14:17:17 +01001609 null, null, null,
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001610 false, // blockUninstall
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001611 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED, 0);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001612 }
1613 return;
1614 }
1615 str = new FileInputStream(userPackagesStateFile);
1616 }
1617 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001618 parser.setInput(str, StandardCharsets.UTF_8.name());
Amith Yamasani483f3b02012-03-13 16:08:00 -07001619
1620 int type;
1621 while ((type=parser.next()) != XmlPullParser.START_TAG
1622 && type != XmlPullParser.END_DOCUMENT) {
1623 ;
1624 }
1625
1626 if (type != XmlPullParser.START_TAG) {
1627 mReadMessages.append("No start tag found in package restrictions file\n");
1628 PackageManagerService.reportSettingsProblem(Log.WARN,
1629 "No start tag found in package manager stopped packages");
1630 return;
1631 }
1632
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001633 int maxAppLinkGeneration = 0;
1634
Amith Yamasani483f3b02012-03-13 16:08:00 -07001635 int outerDepth = parser.getDepth();
1636 PackageSetting ps = null;
1637 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1638 && (type != XmlPullParser.END_TAG
1639 || parser.getDepth() > outerDepth)) {
1640 if (type == XmlPullParser.END_TAG
1641 || type == XmlPullParser.TEXT) {
1642 continue;
1643 }
1644
1645 String tagName = parser.getName();
1646 if (tagName.equals(TAG_PACKAGE)) {
1647 String name = parser.getAttributeValue(null, ATTR_NAME);
1648 ps = mPackages.get(name);
1649 if (ps == null) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001650 Slog.w(PackageManagerService.TAG, "No package known for stopped package "
Amith Yamasani483f3b02012-03-13 16:08:00 -07001651 + name);
1652 XmlUtils.skipCurrentTag(parser);
1653 continue;
1654 }
Jeff Sharkey42884192016-04-09 16:12:01 -06001655
1656 final long ceDataInode = XmlUtils.readLongAttribute(parser, ATTR_CE_DATA_INODE,
1657 0);
1658 final boolean installed = XmlUtils.readBooleanAttribute(parser, ATTR_INSTALLED,
1659 true);
1660 final boolean stopped = XmlUtils.readBooleanAttribute(parser, ATTR_STOPPED,
1661 false);
1662 final boolean notLaunched = XmlUtils.readBooleanAttribute(parser,
1663 ATTR_NOT_LAUNCHED, false);
1664
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001665 // For backwards compatibility with the previous name of "blocked", which
1666 // now means hidden, read the old attribute as well.
Amith Yamasani655d0e22013-06-12 14:19:10 -07001667 final String blockedStr = parser.getAttributeValue(null, ATTR_BLOCKED);
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001668 boolean hidden = blockedStr == null
Amith Yamasani655d0e22013-06-12 14:19:10 -07001669 ? false : Boolean.parseBoolean(blockedStr);
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001670 final String hiddenStr = parser.getAttributeValue(null, ATTR_HIDDEN);
1671 hidden = hiddenStr == null
1672 ? hidden : Boolean.parseBoolean(hiddenStr);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001673
Jeff Sharkey42884192016-04-09 16:12:01 -06001674 final boolean suspended = XmlUtils.readBooleanAttribute(parser, ATTR_SUSPENDED,
1675 false);
1676 final boolean blockUninstall = XmlUtils.readBooleanAttribute(parser,
1677 ATTR_BLOCK_UNINSTALL, false);
1678 final int enabled = XmlUtils.readIntAttribute(parser, ATTR_ENABLED,
1679 COMPONENT_ENABLED_STATE_DEFAULT);
1680 final String enabledCaller = parser.getAttributeValue(null,
1681 ATTR_ENABLED_CALLER);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001682
Jeff Sharkey42884192016-04-09 16:12:01 -06001683 final int verifState = XmlUtils.readIntAttribute(parser,
1684 ATTR_DOMAIN_VERIFICATON_STATE,
1685 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED);
1686 final int linkGeneration = XmlUtils.readIntAttribute(parser,
1687 ATTR_APP_LINK_GENERATION, 0);
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001688 if (linkGeneration > maxAppLinkGeneration) {
1689 maxAppLinkGeneration = linkGeneration;
1690 }
1691
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001692 ArraySet<String> enabledComponents = null;
1693 ArraySet<String> disabledComponents = null;
Amith Yamasani483f3b02012-03-13 16:08:00 -07001694
1695 int packageDepth = parser.getDepth();
1696 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1697 && (type != XmlPullParser.END_TAG
1698 || parser.getDepth() > packageDepth)) {
1699 if (type == XmlPullParser.END_TAG
1700 || type == XmlPullParser.TEXT) {
1701 continue;
1702 }
1703 tagName = parser.getName();
1704 if (tagName.equals(TAG_ENABLED_COMPONENTS)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001705 enabledComponents = readComponentsLPr(parser);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001706 } else if (tagName.equals(TAG_DISABLED_COMPONENTS)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001707 disabledComponents = readComponentsLPr(parser);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001708 }
1709 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001710
Jeff Sharkey42884192016-04-09 16:12:01 -06001711 ps.setUserState(userId, ceDataInode, enabled, installed, stopped, notLaunched,
1712 hidden, suspended, enabledCaller, enabledComponents, disabledComponents,
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001713 blockUninstall, verifState, linkGeneration);
Dianne Hackborn63092712012-10-07 14:45:35 -07001714 } else if (tagName.equals("preferred-activities")) {
1715 readPreferredActivitiesLPw(parser, userId);
Sander Alewijnseaf597622014-03-20 18:44:57 +00001716 } else if (tagName.equals(TAG_PERSISTENT_PREFERRED_ACTIVITIES)) {
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001717 readPersistentPreferredActivitiesLPw(parser, userId);
Nicolas Prevot29762c32014-07-29 18:51:32 +01001718 } else if (tagName.equals(TAG_CROSS_PROFILE_INTENT_FILTERS)) {
Nicolas Prevot81948992014-05-16 18:25:26 +01001719 readCrossProfileIntentFiltersLPw(parser, userId);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001720 } else if (tagName.equals(TAG_DEFAULT_APPS)) {
1721 readDefaultAppsLPw(parser, userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001722 } else {
1723 Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: "
1724 + parser.getName());
1725 XmlUtils.skipCurrentTag(parser);
1726 }
1727 }
1728
1729 str.close();
1730
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001731 mNextAppLinkGeneration.put(userId, maxAppLinkGeneration + 1);
1732
Amith Yamasani483f3b02012-03-13 16:08:00 -07001733 } catch (XmlPullParserException e) {
1734 mReadMessages.append("Error reading: " + e.toString());
1735 PackageManagerService.reportSettingsProblem(Log.ERROR,
1736 "Error reading stopped packages: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07001737 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
1738 e);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001739
1740 } catch (java.io.IOException e) {
1741 mReadMessages.append("Error reading: " + e.toString());
1742 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07001743 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
1744 e);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001745 }
1746 }
1747
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001748 private ArraySet<String> readComponentsLPr(XmlPullParser parser)
Amith Yamasani483f3b02012-03-13 16:08:00 -07001749 throws IOException, XmlPullParserException {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001750 ArraySet<String> components = null;
Amith Yamasani483f3b02012-03-13 16:08:00 -07001751 int type;
1752 int outerDepth = parser.getDepth();
1753 String tagName;
1754 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1755 && (type != XmlPullParser.END_TAG
1756 || parser.getDepth() > outerDepth)) {
1757 if (type == XmlPullParser.END_TAG
1758 || type == XmlPullParser.TEXT) {
1759 continue;
1760 }
1761 tagName = parser.getName();
1762 if (tagName.equals(TAG_ITEM)) {
1763 String componentName = parser.getAttributeValue(null, ATTR_NAME);
1764 if (componentName != null) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001765 if (components == null) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001766 components = new ArraySet<String>();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001767 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07001768 components.add(componentName);
1769 }
1770 }
1771 }
1772 return components;
1773 }
1774
Christopher Tatee012a232015-04-01 17:18:50 -07001775 /**
1776 * Record the state of preferred activity configuration into XML. This is used both
1777 * for recording packages.xml internally and for supporting backup/restore of the
1778 * preferred activity configuration.
1779 */
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08001780 void writePreferredActivitiesLPr(XmlSerializer serializer, int userId, boolean full)
Dianne Hackborn63092712012-10-07 14:45:35 -07001781 throws IllegalArgumentException, IllegalStateException, IOException {
1782 serializer.startTag(null, "preferred-activities");
1783 PreferredIntentResolver pir = mPreferredActivities.get(userId);
1784 if (pir != null) {
1785 for (final PreferredActivity pa : pir.filterSet()) {
1786 serializer.startTag(null, TAG_ITEM);
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08001787 pa.writeToXml(serializer, full);
Dianne Hackborn63092712012-10-07 14:45:35 -07001788 serializer.endTag(null, TAG_ITEM);
1789 }
1790 }
1791 serializer.endTag(null, "preferred-activities");
1792 }
1793
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001794 void writePersistentPreferredActivitiesLPr(XmlSerializer serializer, int userId)
1795 throws IllegalArgumentException, IllegalStateException, IOException {
Sander Alewijnseaf597622014-03-20 18:44:57 +00001796 serializer.startTag(null, TAG_PERSISTENT_PREFERRED_ACTIVITIES);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001797 PersistentPreferredIntentResolver ppir = mPersistentPreferredActivities.get(userId);
1798 if (ppir != null) {
1799 for (final PersistentPreferredActivity ppa : ppir.filterSet()) {
1800 serializer.startTag(null, TAG_ITEM);
1801 ppa.writeToXml(serializer);
1802 serializer.endTag(null, TAG_ITEM);
1803 }
1804 }
Sander Alewijnseaf597622014-03-20 18:44:57 +00001805 serializer.endTag(null, TAG_PERSISTENT_PREFERRED_ACTIVITIES);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001806 }
1807
Nicolas Prevot81948992014-05-16 18:25:26 +01001808 void writeCrossProfileIntentFiltersLPr(XmlSerializer serializer, int userId)
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001809 throws IllegalArgumentException, IllegalStateException, IOException {
Nicolas Prevot81948992014-05-16 18:25:26 +01001810 serializer.startTag(null, TAG_CROSS_PROFILE_INTENT_FILTERS);
1811 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(userId);
1812 if (cpir != null) {
1813 for (final CrossProfileIntentFilter cpif : cpir.filterSet()) {
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001814 serializer.startTag(null, TAG_ITEM);
Nicolas Prevot81948992014-05-16 18:25:26 +01001815 cpif.writeToXml(serializer);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001816 serializer.endTag(null, TAG_ITEM);
1817 }
1818 }
Nicolas Prevot81948992014-05-16 18:25:26 +01001819 serializer.endTag(null, TAG_CROSS_PROFILE_INTENT_FILTERS);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001820 }
1821
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001822 void writeDomainVerificationsLPr(XmlSerializer serializer,
1823 IntentFilterVerificationInfo verificationInfo)
1824 throws IllegalArgumentException, IllegalStateException, IOException {
1825 if (verificationInfo != null && verificationInfo.getPackageName() != null) {
1826 serializer.startTag(null, TAG_DOMAIN_VERIFICATION);
1827 verificationInfo.writeToXml(serializer);
Christopher Tate72c10a22015-06-12 18:31:24 -07001828 if (DEBUG_DOMAIN_VERIFICATION) {
1829 Slog.d(TAG, "Wrote domain verification for package: "
1830 + verificationInfo.getPackageName());
1831 }
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001832 serializer.endTag(null, TAG_DOMAIN_VERIFICATION);
1833 }
1834 }
1835
Christopher Tate6038d152015-06-17 13:07:46 -07001836 // Specifically for backup/restore
1837 void writeAllDomainVerificationsLPr(XmlSerializer serializer, int userId)
1838 throws IllegalArgumentException, IllegalStateException, IOException {
1839 serializer.startTag(null, TAG_ALL_INTENT_FILTER_VERIFICATION);
1840 final int N = mPackages.size();
1841 for (int i = 0; i < N; i++) {
1842 PackageSetting ps = mPackages.valueAt(i);
1843 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
1844 if (ivi != null) {
1845 writeDomainVerificationsLPr(serializer, ivi);
1846 }
1847 }
1848 serializer.endTag(null, TAG_ALL_INTENT_FILTER_VERIFICATION);
1849 }
1850
1851 // Specifically for backup/restore
1852 void readAllDomainVerificationsLPr(XmlPullParser parser, int userId)
1853 throws XmlPullParserException, IOException {
1854 mRestoredIntentFilterVerifications.clear();
1855
1856 int outerDepth = parser.getDepth();
1857 int type;
1858 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1859 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1860 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1861 continue;
1862 }
1863
1864 String tagName = parser.getName();
1865 if (tagName.equals(TAG_DOMAIN_VERIFICATION)) {
1866 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser);
1867 final String pkgName = ivi.getPackageName();
1868 final PackageSetting ps = mPackages.get(pkgName);
1869 if (ps != null) {
1870 // known/existing package; update in place
1871 ps.setIntentFilterVerificationInfo(ivi);
1872 if (DEBUG_DOMAIN_VERIFICATION) {
1873 Slog.d(TAG, "Restored IVI for existing app " + pkgName
1874 + " status=" + ivi.getStatusString());
1875 }
1876 } else {
1877 mRestoredIntentFilterVerifications.put(pkgName, ivi);
1878 if (DEBUG_DOMAIN_VERIFICATION) {
1879 Slog.d(TAG, "Restored IVI for pending app " + pkgName
1880 + " status=" + ivi.getStatusString());
1881 }
1882 }
1883 } else {
1884 PackageManagerService.reportSettingsProblem(Log.WARN,
1885 "Unknown element under <all-intent-filter-verification>: "
1886 + parser.getName());
1887 XmlUtils.skipCurrentTag(parser);
1888 }
1889 }
1890 }
1891
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07001892 // Specifically for backup/restore
1893 public void processRestoredPermissionGrantLPr(String pkgName, String permission,
1894 boolean isGranted, int restoredFlagSet, int userId)
1895 throws IOException, XmlPullParserException {
1896 mRuntimePermissionsPersistence.rememberRestoredUserGrantLPr(
1897 pkgName, permission, isGranted, restoredFlagSet, userId);
1898 }
1899
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001900 void writeDefaultAppsLPr(XmlSerializer serializer, int userId)
1901 throws IllegalArgumentException, IllegalStateException, IOException {
1902 serializer.startTag(null, TAG_DEFAULT_APPS);
Robin Lee0dc591b2016-05-04 13:01:46 +01001903 String defaultBrowser = mDefaultBrowserApp.get(userId);
1904 if (!TextUtils.isEmpty(defaultBrowser)) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001905 serializer.startTag(null, TAG_DEFAULT_BROWSER);
Robin Lee0dc591b2016-05-04 13:01:46 +01001906 serializer.attribute(null, ATTR_PACKAGE_NAME, defaultBrowser);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001907 serializer.endTag(null, TAG_DEFAULT_BROWSER);
1908 }
Robin Lee0dc591b2016-05-04 13:01:46 +01001909 String defaultDialer = mDefaultDialerApp.get(userId);
1910 if (!TextUtils.isEmpty(defaultDialer)) {
1911 serializer.startTag(null, TAG_DEFAULT_DIALER);
1912 serializer.attribute(null, ATTR_PACKAGE_NAME, defaultDialer);
1913 serializer.endTag(null, TAG_DEFAULT_DIALER);
1914 }
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001915 serializer.endTag(null, TAG_DEFAULT_APPS);
1916 }
1917
Amith Yamasani483f3b02012-03-13 16:08:00 -07001918 void writePackageRestrictionsLPr(int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001919 if (DEBUG_MU) {
1920 Log.i(TAG, "Writing package restrictions for user=" + userId);
1921 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001922 // Keep the old stopped packages around until we know the new ones have
1923 // been successfully written.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001924 File userPackagesStateFile = getUserPackagesStateFile(userId);
1925 File backupFile = getUserPackagesStateBackupFile(userId);
1926 new File(userPackagesStateFile.getParent()).mkdirs();
1927 if (userPackagesStateFile.exists()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001928 // Presence of backup settings file indicates that we failed
1929 // to persist packages earlier. So preserve the older
1930 // backup for future reference since the current packages
1931 // might have been corrupted.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001932 if (!backupFile.exists()) {
1933 if (!userPackagesStateFile.renameTo(backupFile)) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001934 Slog.wtf(PackageManagerService.TAG,
1935 "Unable to backup user packages state file, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001936 + "current changes will be lost at reboot");
1937 return;
1938 }
1939 } else {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001940 userPackagesStateFile.delete();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001941 Slog.w(PackageManagerService.TAG, "Preserving older stopped packages backup");
1942 }
1943 }
1944
1945 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001946 final FileOutputStream fstr = new FileOutputStream(userPackagesStateFile);
Kenny Root447106f2011-03-23 11:00:15 -07001947 final BufferedOutputStream str = new BufferedOutputStream(fstr);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001948
Kenny Root447106f2011-03-23 11:00:15 -07001949 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001950 serializer.setOutput(str, StandardCharsets.UTF_8.name());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001951 serializer.startDocument(null, true);
1952 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1953
Amith Yamasani483f3b02012-03-13 16:08:00 -07001954 serializer.startTag(null, TAG_PACKAGE_RESTRICTIONS);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001955
Kenny Root447106f2011-03-23 11:00:15 -07001956 for (final PackageSetting pkg : mPackages.values()) {
Jeff Sharkey42884192016-04-09 16:12:01 -06001957 final PackageUserState ustate = pkg.readUserState(userId);
1958 if (DEBUG_MU) Log.i(TAG, " pkg=" + pkg.name + ", state=" + ustate.enabled);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001959
Jeff Sharkey42884192016-04-09 16:12:01 -06001960 serializer.startTag(null, TAG_PACKAGE);
1961 serializer.attribute(null, ATTR_NAME, pkg.name);
1962 if (ustate.ceDataInode != 0) {
1963 XmlUtils.writeLongAttribute(serializer, ATTR_CE_DATA_INODE, ustate.ceDataInode);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001964 }
Jeff Sharkey42884192016-04-09 16:12:01 -06001965 if (!ustate.installed) {
1966 serializer.attribute(null, ATTR_INSTALLED, "false");
1967 }
1968 if (ustate.stopped) {
1969 serializer.attribute(null, ATTR_STOPPED, "true");
1970 }
1971 if (ustate.notLaunched) {
1972 serializer.attribute(null, ATTR_NOT_LAUNCHED, "true");
1973 }
1974 if (ustate.hidden) {
1975 serializer.attribute(null, ATTR_HIDDEN, "true");
1976 }
1977 if (ustate.suspended) {
1978 serializer.attribute(null, ATTR_SUSPENDED, "true");
1979 }
1980 if (ustate.blockUninstall) {
1981 serializer.attribute(null, ATTR_BLOCK_UNINSTALL, "true");
1982 }
1983 if (ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT) {
1984 serializer.attribute(null, ATTR_ENABLED,
1985 Integer.toString(ustate.enabled));
1986 if (ustate.lastDisableAppCaller != null) {
1987 serializer.attribute(null, ATTR_ENABLED_CALLER,
1988 ustate.lastDisableAppCaller);
1989 }
1990 }
1991 if (ustate.domainVerificationStatus !=
1992 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED) {
1993 XmlUtils.writeIntAttribute(serializer, ATTR_DOMAIN_VERIFICATON_STATE,
1994 ustate.domainVerificationStatus);
1995 }
1996 if (ustate.appLinkGeneration != 0) {
1997 XmlUtils.writeIntAttribute(serializer, ATTR_APP_LINK_GENERATION,
1998 ustate.appLinkGeneration);
1999 }
2000 if (!ArrayUtils.isEmpty(ustate.enabledComponents)) {
2001 serializer.startTag(null, TAG_ENABLED_COMPONENTS);
2002 for (final String name : ustate.enabledComponents) {
2003 serializer.startTag(null, TAG_ITEM);
2004 serializer.attribute(null, ATTR_NAME, name);
2005 serializer.endTag(null, TAG_ITEM);
2006 }
2007 serializer.endTag(null, TAG_ENABLED_COMPONENTS);
2008 }
2009 if (!ArrayUtils.isEmpty(ustate.disabledComponents)) {
2010 serializer.startTag(null, TAG_DISABLED_COMPONENTS);
2011 for (final String name : ustate.disabledComponents) {
2012 serializer.startTag(null, TAG_ITEM);
2013 serializer.attribute(null, ATTR_NAME, name);
2014 serializer.endTag(null, TAG_ITEM);
2015 }
2016 serializer.endTag(null, TAG_DISABLED_COMPONENTS);
2017 }
2018
2019 serializer.endTag(null, TAG_PACKAGE);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002020 }
2021
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08002022 writePreferredActivitiesLPr(serializer, userId, true);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00002023 writePersistentPreferredActivitiesLPr(serializer, userId);
Nicolas Prevot81948992014-05-16 18:25:26 +01002024 writeCrossProfileIntentFiltersLPr(serializer, userId);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07002025 writeDefaultAppsLPr(serializer, userId);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00002026
Amith Yamasani483f3b02012-03-13 16:08:00 -07002027 serializer.endTag(null, TAG_PACKAGE_RESTRICTIONS);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002028
2029 serializer.endDocument();
2030
2031 str.flush();
2032 FileUtils.sync(fstr);
2033 str.close();
2034
2035 // New settings successfully written, old ones are no longer
2036 // needed.
Amith Yamasani483f3b02012-03-13 16:08:00 -07002037 backupFile.delete();
2038 FileUtils.setPermissions(userPackagesStateFile.toString(),
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002039 FileUtils.S_IRUSR|FileUtils.S_IWUSR
Nick Kralevich70522ad2012-01-06 13:58:01 -08002040 |FileUtils.S_IRGRP|FileUtils.S_IWGRP,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002041 -1, -1);
2042
2043 // Done, all is good!
2044 return;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002045 } catch(java.io.IOException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002046 Slog.wtf(PackageManagerService.TAG,
Amith Yamasani483f3b02012-03-13 16:08:00 -07002047 "Unable to write package manager user packages state, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002048 + " current changes will be lost at reboot", e);
2049 }
2050
2051 // Clean up partially written files
Amith Yamasani483f3b02012-03-13 16:08:00 -07002052 if (userPackagesStateFile.exists()) {
2053 if (!userPackagesStateFile.delete()) {
2054 Log.i(PackageManagerService.TAG, "Failed to clean up mangled file: "
2055 + mStoppedPackagesFilename);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002056 }
2057 }
2058 }
2059
Svetoslavc6d1c342015-02-26 14:44:43 -08002060 void readInstallPermissionsLPr(XmlPullParser parser,
2061 PermissionsState permissionsState) throws IOException, XmlPullParserException {
2062 int outerDepth = parser.getDepth();
2063 int type;
2064 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2065 && (type != XmlPullParser.END_TAG
2066 || parser.getDepth() > outerDepth)) {
2067 if (type == XmlPullParser.END_TAG
2068 || type == XmlPullParser.TEXT) {
2069 continue;
2070 }
2071 String tagName = parser.getName();
2072 if (tagName.equals(TAG_ITEM)) {
2073 String name = parser.getAttributeValue(null, ATTR_NAME);
2074
2075 BasePermission bp = mPermissions.get(name);
2076 if (bp == null) {
2077 Slog.w(PackageManagerService.TAG, "Unknown permission: " + name);
2078 XmlUtils.skipCurrentTag(parser);
2079 continue;
2080 }
2081
Svet Ganov8c7f7002015-05-07 10:48:44 -07002082 String grantedStr = parser.getAttributeValue(null, ATTR_GRANTED);
2083 final boolean granted = grantedStr == null
2084 || Boolean.parseBoolean(grantedStr);
2085
2086 String flagsStr = parser.getAttributeValue(null, ATTR_FLAGS);
2087 final int flags = (flagsStr != null)
2088 ? Integer.parseInt(flagsStr, 16) : 0;
2089
2090 if (granted) {
2091 if (permissionsState.grantInstallPermission(bp) ==
2092 PermissionsState.PERMISSION_OPERATION_FAILURE) {
2093 Slog.w(PackageManagerService.TAG, "Permission already added: " + name);
2094 XmlUtils.skipCurrentTag(parser);
2095 } else {
2096 permissionsState.updatePermissionFlags(bp, UserHandle.USER_ALL,
2097 PackageManager.MASK_PERMISSION_FLAGS, flags);
2098 }
2099 } else {
2100 if (permissionsState.revokeInstallPermission(bp) ==
2101 PermissionsState.PERMISSION_OPERATION_FAILURE) {
2102 Slog.w(PackageManagerService.TAG, "Permission already added: " + name);
2103 XmlUtils.skipCurrentTag(parser);
2104 } else {
2105 permissionsState.updatePermissionFlags(bp, UserHandle.USER_ALL,
2106 PackageManager.MASK_PERMISSION_FLAGS, flags);
2107 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002108 }
2109 } else {
2110 Slog.w(PackageManagerService.TAG, "Unknown element under <permissions>: "
2111 + parser.getName());
2112 XmlUtils.skipCurrentTag(parser);
2113 }
2114 }
2115 }
2116
Svet Ganov8c7f7002015-05-07 10:48:44 -07002117 void writePermissionsLPr(XmlSerializer serializer, List<PermissionState> permissionStates)
Svetoslavc6d1c342015-02-26 14:44:43 -08002118 throws IOException {
Svet Ganov8c7f7002015-05-07 10:48:44 -07002119 if (permissionStates.isEmpty()) {
Svetoslavc6d1c342015-02-26 14:44:43 -08002120 return;
2121 }
2122
2123 serializer.startTag(null, TAG_PERMISSIONS);
2124
Svet Ganov8c7f7002015-05-07 10:48:44 -07002125 for (PermissionState permissionState : permissionStates) {
Svetoslavc6d1c342015-02-26 14:44:43 -08002126 serializer.startTag(null, TAG_ITEM);
Svet Ganov8c7f7002015-05-07 10:48:44 -07002127 serializer.attribute(null, ATTR_NAME, permissionState.getName());
2128 serializer.attribute(null, ATTR_GRANTED, String.valueOf(permissionState.isGranted()));
2129 serializer.attribute(null, ATTR_FLAGS, Integer.toHexString(permissionState.getFlags()));
Svetoslavc6d1c342015-02-26 14:44:43 -08002130 serializer.endTag(null, TAG_ITEM);
2131 }
2132
2133 serializer.endTag(null, TAG_PERMISSIONS);
2134 }
2135
Svet Ganov354cd3c2015-12-17 11:35:04 -08002136 void writeChildPackagesLPw(XmlSerializer serializer, List<String> childPackageNames)
2137 throws IOException {
2138 if (childPackageNames == null) {
2139 return;
2140 }
2141 final int childCount = childPackageNames.size();
2142 for (int i = 0; i < childCount; i++) {
2143 String childPackageName = childPackageNames.get(i);
2144 serializer.startTag(null, TAG_CHILD_PACKAGE);
2145 serializer.attribute(null, ATTR_NAME, childPackageName);
2146 serializer.endTag(null, TAG_CHILD_PACKAGE);
2147 }
2148 }
2149
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002150 // Note: assumed "stopped" field is already cleared in all packages.
Amith Yamasani483f3b02012-03-13 16:08:00 -07002151 // Legacy reader, used to read in the old file format after an upgrade. Not used after that.
Kenny Root447106f2011-03-23 11:00:15 -07002152 void readStoppedLPw() {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002153 FileInputStream str = null;
2154 if (mBackupStoppedPackagesFilename.exists()) {
2155 try {
2156 str = new FileInputStream(mBackupStoppedPackagesFilename);
2157 mReadMessages.append("Reading from backup stopped packages file\n");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002158 PackageManagerService.reportSettingsProblem(Log.INFO,
2159 "Need to read from backup stopped packages file");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002160 if (mSettingsFilename.exists()) {
2161 // If both the backup and normal file exist, we
2162 // ignore the normal one since it might have been
2163 // corrupted.
2164 Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file "
2165 + mStoppedPackagesFilename);
2166 mStoppedPackagesFilename.delete();
2167 }
2168 } catch (java.io.IOException e) {
2169 // We'll try for the normal settings file.
2170 }
2171 }
2172
2173 try {
2174 if (str == null) {
2175 if (!mStoppedPackagesFilename.exists()) {
2176 mReadMessages.append("No stopped packages file found\n");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002177 PackageManagerService.reportSettingsProblem(Log.INFO,
2178 "No stopped packages file file; assuming all started");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002179 // At first boot, make sure no packages are stopped.
2180 // We usually want to have third party apps initialize
2181 // in the stopped state, but not at first boot.
2182 for (PackageSetting pkg : mPackages.values()) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002183 pkg.setStopped(false, 0);
2184 pkg.setNotLaunched(false, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002185 }
2186 return;
2187 }
2188 str = new FileInputStream(mStoppedPackagesFilename);
2189 }
Kenny Root447106f2011-03-23 11:00:15 -07002190 final XmlPullParser parser = Xml.newPullParser();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002191 parser.setInput(str, null);
2192
2193 int type;
2194 while ((type=parser.next()) != XmlPullParser.START_TAG
2195 && type != XmlPullParser.END_DOCUMENT) {
2196 ;
2197 }
2198
2199 if (type != XmlPullParser.START_TAG) {
2200 mReadMessages.append("No start tag found in stopped packages file\n");
2201 PackageManagerService.reportSettingsProblem(Log.WARN,
2202 "No start tag found in package manager stopped packages");
2203 return;
2204 }
2205
2206 int outerDepth = parser.getDepth();
2207 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2208 && (type != XmlPullParser.END_TAG
2209 || parser.getDepth() > outerDepth)) {
2210 if (type == XmlPullParser.END_TAG
2211 || type == XmlPullParser.TEXT) {
2212 continue;
2213 }
2214
2215 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07002216 if (tagName.equals(TAG_PACKAGE)) {
2217 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002218 PackageSetting ps = mPackages.get(name);
2219 if (ps != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002220 ps.setStopped(true, 0);
2221 if ("1".equals(parser.getAttributeValue(null, ATTR_NOT_LAUNCHED))) {
2222 ps.setNotLaunched(true, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002223 }
2224 } else {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002225 Slog.w(PackageManagerService.TAG,
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002226 "No package known for stopped package " + name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002227 }
2228 XmlUtils.skipCurrentTag(parser);
2229 } else {
2230 Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: "
2231 + parser.getName());
2232 XmlUtils.skipCurrentTag(parser);
2233 }
2234 }
2235
2236 str.close();
2237
Amith Yamasani483f3b02012-03-13 16:08:00 -07002238 } catch (XmlPullParserException e) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002239 mReadMessages.append("Error reading: " + e.toString());
Amith Yamasani483f3b02012-03-13 16:08:00 -07002240 PackageManagerService.reportSettingsProblem(Log.ERROR,
2241 "Error reading stopped packages: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002242 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
2243 e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002244
Amith Yamasani483f3b02012-03-13 16:08:00 -07002245 } catch (java.io.IOException e) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002246 mReadMessages.append("Error reading: " + e.toString());
2247 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002248 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
2249 e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002250
2251 }
2252 }
2253
Kenny Root447106f2011-03-23 11:00:15 -07002254 void writeLPr() {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002255 //Debug.startMethodTracing("/data/system/packageprof", 8 * 1024 * 1024);
2256
2257 // Keep the old settings around until we know the new ones have
2258 // been successfully written.
2259 if (mSettingsFilename.exists()) {
2260 // Presence of backup settings file indicates that we failed
2261 // to persist settings earlier. So preserve the older
2262 // backup for future reference since the current settings
2263 // might have been corrupted.
2264 if (!mBackupSettingsFilename.exists()) {
2265 if (!mSettingsFilename.renameTo(mBackupSettingsFilename)) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002266 Slog.wtf(PackageManagerService.TAG,
2267 "Unable to backup package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002268 + " current changes will be lost at reboot");
2269 return;
2270 }
2271 } else {
2272 mSettingsFilename.delete();
2273 Slog.w(PackageManagerService.TAG, "Preserving older settings backup");
2274 }
2275 }
2276
2277 mPastSignatures.clear();
2278
2279 try {
2280 FileOutputStream fstr = new FileOutputStream(mSettingsFilename);
2281 BufferedOutputStream str = new BufferedOutputStream(fstr);
2282
2283 //XmlSerializer serializer = XmlUtils.serializerInstance();
2284 XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002285 serializer.setOutput(str, StandardCharsets.UTF_8.name());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002286 serializer.startDocument(null, true);
2287 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
2288
2289 serializer.startTag(null, "packages");
2290
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002291 for (int i = 0; i < mVersion.size(); i++) {
2292 final String volumeUuid = mVersion.keyAt(i);
2293 final VersionInfo ver = mVersion.valueAt(i);
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002294
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002295 serializer.startTag(null, TAG_VERSION);
2296 XmlUtils.writeStringAttribute(serializer, ATTR_VOLUME_UUID, volumeUuid);
2297 XmlUtils.writeIntAttribute(serializer, ATTR_SDK_VERSION, ver.sdkVersion);
2298 XmlUtils.writeIntAttribute(serializer, ATTR_DATABASE_VERSION, ver.databaseVersion);
2299 XmlUtils.writeStringAttribute(serializer, ATTR_FINGERPRINT, ver.fingerprint);
2300 serializer.endTag(null, TAG_VERSION);
2301 }
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07002302
Kenny Root0aaa0d92011-09-12 16:42:55 -07002303 if (mVerifierDeviceIdentity != null) {
2304 serializer.startTag(null, "verifier");
2305 serializer.attribute(null, "device", mVerifierDeviceIdentity.toString());
2306 serializer.endTag(null, "verifier");
2307 }
2308
Jeff Sharkeyf5385772012-05-11 14:04:41 -07002309 if (mReadExternalStorageEnforced != null) {
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002310 serializer.startTag(null, TAG_READ_EXTERNAL_STORAGE);
2311 serializer.attribute(
Jeff Sharkey5d32e772012-04-12 15:59:23 -07002312 null, ATTR_ENFORCEMENT, mReadExternalStorageEnforced ? "1" : "0");
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002313 serializer.endTag(null, TAG_READ_EXTERNAL_STORAGE);
2314 }
2315
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002316 serializer.startTag(null, "permission-trees");
2317 for (BasePermission bp : mPermissionTrees.values()) {
Kenny Root447106f2011-03-23 11:00:15 -07002318 writePermissionLPr(serializer, bp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002319 }
2320 serializer.endTag(null, "permission-trees");
2321
2322 serializer.startTag(null, "permissions");
2323 for (BasePermission bp : mPermissions.values()) {
Kenny Root447106f2011-03-23 11:00:15 -07002324 writePermissionLPr(serializer, bp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002325 }
2326 serializer.endTag(null, "permissions");
2327
Kenny Root447106f2011-03-23 11:00:15 -07002328 for (final PackageSetting pkg : mPackages.values()) {
2329 writePackageLPr(serializer, pkg);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002330 }
2331
Kenny Root447106f2011-03-23 11:00:15 -07002332 for (final PackageSetting pkg : mDisabledSysPackages.values()) {
2333 writeDisabledSysPackageLPr(serializer, pkg);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002334 }
2335
Kenny Root447106f2011-03-23 11:00:15 -07002336 for (final SharedUserSetting usr : mSharedUsers.values()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002337 serializer.startTag(null, "shared-user");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002338 serializer.attribute(null, ATTR_NAME, usr.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002339 serializer.attribute(null, "userId",
2340 Integer.toString(usr.userId));
2341 usr.signatures.writeXml(serializer, "sigs", mPastSignatures);
Svet Ganov8c7f7002015-05-07 10:48:44 -07002342 writePermissionsLPr(serializer, usr.getPermissionsState()
2343 .getInstallPermissionStates());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002344 serializer.endTag(null, "shared-user");
2345 }
2346
2347 if (mPackagesToBeCleaned.size() > 0) {
Jeff Sharkey752cd922012-09-23 16:25:12 -07002348 for (PackageCleanItem item : mPackagesToBeCleaned) {
2349 final String userStr = Integer.toString(item.userId);
2350 serializer.startTag(null, "cleaning-package");
2351 serializer.attribute(null, ATTR_NAME, item.packageName);
2352 serializer.attribute(null, ATTR_CODE, item.andCode ? "true" : "false");
2353 serializer.attribute(null, ATTR_USER, userStr);
2354 serializer.endTag(null, "cleaning-package");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002355 }
2356 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002357
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002358 if (mRenamedPackages.size() > 0) {
Andy McFadden2f362292012-01-20 14:43:38 -08002359 for (Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002360 serializer.startTag(null, "renamed-package");
2361 serializer.attribute(null, "new", e.getKey());
2362 serializer.attribute(null, "old", e.getValue());
2363 serializer.endTag(null, "renamed-package");
2364 }
2365 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002366
Christopher Tate6038d152015-06-17 13:07:46 -07002367 final int numIVIs = mRestoredIntentFilterVerifications.size();
2368 if (numIVIs > 0) {
2369 if (DEBUG_DOMAIN_VERIFICATION) {
2370 Slog.i(TAG, "Writing restored-ivi entries to packages.xml");
2371 }
2372 serializer.startTag(null, "restored-ivi");
2373 for (int i = 0; i < numIVIs; i++) {
2374 IntentFilterVerificationInfo ivi = mRestoredIntentFilterVerifications.valueAt(i);
2375 writeDomainVerificationsLPr(serializer, ivi);
2376 }
2377 serializer.endTag(null, "restored-ivi");
2378 } else {
2379 if (DEBUG_DOMAIN_VERIFICATION) {
2380 Slog.i(TAG, " no restored IVI entries to write");
2381 }
2382 }
2383
dcashman55b10782014-04-09 14:20:38 -07002384 mKeySetManagerService.writeKeySetManagerServiceLPr(serializer);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002385
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002386 serializer.endTag(null, "packages");
2387
2388 serializer.endDocument();
2389
2390 str.flush();
2391 FileUtils.sync(fstr);
2392 str.close();
2393
2394 // New settings successfully written, old ones are no longer
2395 // needed.
2396 mBackupSettingsFilename.delete();
2397 FileUtils.setPermissions(mSettingsFilename.toString(),
2398 FileUtils.S_IRUSR|FileUtils.S_IWUSR
Nick Kralevich70522ad2012-01-06 13:58:01 -08002399 |FileUtils.S_IRGRP|FileUtils.S_IWGRP,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002400 -1, -1);
2401
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002402 writeKernelMappingLPr();
Jeff Sharkey91edde22015-05-20 12:04:42 -07002403 writePackageListLPr();
Amith Yamasani483f3b02012-03-13 16:08:00 -07002404 writeAllUsersPackageRestrictionsLPr();
Svetoslavc6d1c342015-02-26 14:44:43 -08002405 writeAllRuntimePermissionsLPr();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002406 return;
2407
2408 } catch(XmlPullParserException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002409 Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002410 + "current changes will be lost at reboot", e);
2411 } catch(java.io.IOException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002412 Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002413 + "current changes will be lost at reboot", e);
2414 }
2415 // Clean up partially written files
2416 if (mSettingsFilename.exists()) {
2417 if (!mSettingsFilename.delete()) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002418 Slog.wtf(PackageManagerService.TAG, "Failed to clean up mangled file: "
Amith Yamasani483f3b02012-03-13 16:08:00 -07002419 + mSettingsFilename);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002420 }
2421 }
2422 //Debug.stopMethodTracing();
2423 }
2424
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002425 void writeKernelMappingLPr() {
2426 if (mKernelMappingFilename == null) return;
2427
2428 final String[] known = mKernelMappingFilename.list();
2429 final ArraySet<String> knownSet = new ArraySet<>(known.length);
2430 for (String name : known) {
2431 knownSet.add(name);
2432 }
2433
2434 for (final PackageSetting ps : mPackages.values()) {
2435 // Package is actively claimed
2436 knownSet.remove(ps.name);
2437 writeKernelMappingLPr(ps);
2438 }
2439
2440 // Remove any unclaimed mappings
2441 for (int i = 0; i < knownSet.size(); i++) {
2442 final String name = knownSet.valueAt(i);
2443 if (DEBUG_KERNEL) Slog.d(TAG, "Dropping mapping " + name);
2444
2445 mKernelMapping.remove(name);
Jeff Sharkey800efcc2016-02-25 17:08:11 -07002446 new File(mKernelMappingFilename, name).delete();
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002447 }
2448 }
2449
2450 void writeKernelMappingLPr(PackageSetting ps) {
2451 if (mKernelMappingFilename == null) return;
2452
2453 final Integer cur = mKernelMapping.get(ps.name);
2454 if (cur != null && cur.intValue() == ps.appId) {
2455 // Ignore when mapping already matches
2456 return;
2457 }
2458
2459 if (DEBUG_KERNEL) Slog.d(TAG, "Mapping " + ps.name + " to " + ps.appId);
2460
2461 final File dir = new File(mKernelMappingFilename, ps.name);
2462 dir.mkdir();
2463
2464 final File file = new File(dir, "appid");
2465 try {
2466 FileUtils.stringToFile(file, Integer.toString(ps.appId));
2467 mKernelMapping.put(ps.name, ps.appId);
2468 } catch (IOException ignored) {
2469 }
2470 }
2471
Jeff Sharkey91edde22015-05-20 12:04:42 -07002472 void writePackageListLPr() {
2473 writePackageListLPr(-1);
2474 }
2475
2476 void writePackageListLPr(int creatingUserId) {
2477 // Only derive GIDs for active users (not dying)
2478 final List<UserInfo> users = UserManagerService.getInstance().getUsers(true);
2479 int[] userIds = new int[users.size()];
2480 for (int i = 0; i < userIds.length; i++) {
2481 userIds[i] = users.get(i).id;
2482 }
2483 if (creatingUserId != -1) {
2484 userIds = ArrayUtils.appendInt(userIds, creatingUserId);
2485 }
2486
2487 // Write package list file now, use a JournaledFile.
2488 File tempFile = new File(mPackageListFilename.getAbsolutePath() + ".tmp");
2489 JournaledFile journal = new JournaledFile(mPackageListFilename, tempFile);
2490
2491 final File writeTarget = journal.chooseForWrite();
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002492 FileOutputStream fstr;
2493 BufferedWriter writer = null;
Jeff Sharkey91edde22015-05-20 12:04:42 -07002494 try {
2495 fstr = new FileOutputStream(writeTarget);
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002496 writer = new BufferedWriter(new OutputStreamWriter(fstr, Charset.defaultCharset()));
Jeff Sharkey91edde22015-05-20 12:04:42 -07002497 FileUtils.setPermissions(fstr.getFD(), 0640, SYSTEM_UID, PACKAGE_INFO_GID);
2498
2499 StringBuilder sb = new StringBuilder();
2500 for (final PackageSetting pkg : mPackages.values()) {
Jeff Sharkeye17ac152015-11-06 22:40:29 -08002501 if (pkg.pkg == null || pkg.pkg.applicationInfo == null
2502 || pkg.pkg.applicationInfo.dataDir == null) {
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002503 if (!"android".equals(pkg.name)) {
2504 Slog.w(TAG, "Skipping " + pkg + " due to missing metadata");
2505 }
Jeff Sharkey91edde22015-05-20 12:04:42 -07002506 continue;
2507 }
2508
2509 final ApplicationInfo ai = pkg.pkg.applicationInfo;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07002510 final String dataPath = ai.dataDir;
Jeff Sharkey91edde22015-05-20 12:04:42 -07002511 final boolean isDebug = (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
2512 final int[] gids = pkg.getPermissionsState().computeGids(userIds);
2513
2514 // Avoid any application that has a space in its path.
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002515 if (dataPath.indexOf(' ') >= 0)
Jeff Sharkey91edde22015-05-20 12:04:42 -07002516 continue;
2517
2518 // we store on each line the following information for now:
2519 //
2520 // pkgName - package name
2521 // userId - application-specific user id
2522 // debugFlag - 0 or 1 if the package is debuggable.
2523 // dataPath - path to package's data path
2524 // seinfo - seinfo label for the app (assigned at install time)
2525 // gids - supplementary gids this app launches with
2526 //
2527 // NOTE: We prefer not to expose all ApplicationInfo flags for now.
2528 //
2529 // DO NOT MODIFY THIS FORMAT UNLESS YOU CAN ALSO MODIFY ITS USERS
2530 // FROM NATIVE CODE. AT THE MOMENT, LOOK AT THE FOLLOWING SOURCES:
William Robertsdc06bb02015-07-24 10:05:06 -07002531 // frameworks/base/libs/packagelistparser
Jeff Sharkey91edde22015-05-20 12:04:42 -07002532 // system/core/run-as/run-as.c
Jeff Sharkey91edde22015-05-20 12:04:42 -07002533 //
2534 sb.setLength(0);
2535 sb.append(ai.packageName);
2536 sb.append(" ");
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002537 sb.append(ai.uid);
Jeff Sharkey91edde22015-05-20 12:04:42 -07002538 sb.append(isDebug ? " 1 " : " 0 ");
2539 sb.append(dataPath);
2540 sb.append(" ");
2541 sb.append(ai.seinfo);
2542 sb.append(" ");
2543 if (gids != null && gids.length > 0) {
2544 sb.append(gids[0]);
2545 for (int i = 1; i < gids.length; i++) {
2546 sb.append(",");
2547 sb.append(gids[i]);
2548 }
2549 } else {
2550 sb.append("none");
2551 }
2552 sb.append("\n");
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002553 writer.append(sb);
Jeff Sharkey91edde22015-05-20 12:04:42 -07002554 }
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002555 writer.flush();
Jeff Sharkey91edde22015-05-20 12:04:42 -07002556 FileUtils.sync(fstr);
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002557 writer.close();
Jeff Sharkey91edde22015-05-20 12:04:42 -07002558 journal.commit();
2559 } catch (Exception e) {
2560 Slog.wtf(TAG, "Failed to write packages.list", e);
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002561 IoUtils.closeQuietly(writer);
Jeff Sharkey91edde22015-05-20 12:04:42 -07002562 journal.rollback();
2563 }
2564 }
2565
Kenny Root447106f2011-03-23 11:00:15 -07002566 void writeDisabledSysPackageLPr(XmlSerializer serializer, final PackageSetting pkg)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002567 throws java.io.IOException {
2568 serializer.startTag(null, "updated-package");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002569 serializer.attribute(null, ATTR_NAME, pkg.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002570 if (pkg.realName != null) {
2571 serializer.attribute(null, "realName", pkg.realName);
2572 }
2573 serializer.attribute(null, "codePath", pkg.codePathString);
2574 serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp));
2575 serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime));
2576 serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime));
2577 serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
2578 if (!pkg.resourcePathString.equals(pkg.codePathString)) {
2579 serializer.attribute(null, "resourcePath", pkg.resourcePathString);
2580 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002581 if (pkg.legacyNativeLibraryPathString != null) {
2582 serializer.attribute(null, "nativeLibraryPath", pkg.legacyNativeLibraryPathString);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002583 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002584 if (pkg.primaryCpuAbiString != null) {
2585 serializer.attribute(null, "primaryCpuAbi", pkg.primaryCpuAbiString);
Narayan Kamath9e289d72014-04-10 09:26:59 +00002586 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002587 if (pkg.secondaryCpuAbiString != null) {
2588 serializer.attribute(null, "secondaryCpuAbi", pkg.secondaryCpuAbiString);
2589 }
Narayan Kamath4903f642014-08-11 13:33:45 +01002590 if (pkg.cpuAbiOverrideString != null) {
2591 serializer.attribute(null, "cpuAbiOverride", pkg.cpuAbiOverrideString);
2592 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002593
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002594 if (pkg.sharedUser == null) {
Amith Yamasani13593602012-03-22 16:16:17 -07002595 serializer.attribute(null, "userId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002596 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07002597 serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002598 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002599
Svet Ganov354cd3c2015-12-17 11:35:04 -08002600 if (pkg.parentPackageName != null) {
2601 serializer.attribute(null, "parentPackageName", pkg.parentPackageName);
2602 }
2603
2604 writeChildPackagesLPw(serializer, pkg.childPackageNames);
2605
Svetoslavc6d1c342015-02-26 14:44:43 -08002606 // If this is a shared user, the permissions will be written there.
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002607 if (pkg.sharedUser == null) {
Svet Ganov8c7f7002015-05-07 10:48:44 -07002608 writePermissionsLPr(serializer, pkg.getPermissionsState()
2609 .getInstallPermissionStates());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002610 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002611
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002612 serializer.endTag(null, "updated-package");
2613 }
2614
Kenny Root447106f2011-03-23 11:00:15 -07002615 void writePackageLPr(XmlSerializer serializer, final PackageSetting pkg)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002616 throws java.io.IOException {
2617 serializer.startTag(null, "package");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002618 serializer.attribute(null, ATTR_NAME, pkg.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002619 if (pkg.realName != null) {
2620 serializer.attribute(null, "realName", pkg.realName);
2621 }
2622 serializer.attribute(null, "codePath", pkg.codePathString);
2623 if (!pkg.resourcePathString.equals(pkg.codePathString)) {
2624 serializer.attribute(null, "resourcePath", pkg.resourcePathString);
2625 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002626
2627 if (pkg.legacyNativeLibraryPathString != null) {
2628 serializer.attribute(null, "nativeLibraryPath", pkg.legacyNativeLibraryPathString);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002629 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002630 if (pkg.primaryCpuAbiString != null) {
2631 serializer.attribute(null, "primaryCpuAbi", pkg.primaryCpuAbiString);
Narayan Kamath9e289d72014-04-10 09:26:59 +00002632 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002633 if (pkg.secondaryCpuAbiString != null) {
2634 serializer.attribute(null, "secondaryCpuAbi", pkg.secondaryCpuAbiString);
2635 }
Narayan Kamath4903f642014-08-11 13:33:45 +01002636 if (pkg.cpuAbiOverrideString != null) {
2637 serializer.attribute(null, "cpuAbiOverride", pkg.cpuAbiOverrideString);
2638 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002639
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002640 serializer.attribute(null, "publicFlags", Integer.toString(pkg.pkgFlags));
2641 serializer.attribute(null, "privateFlags", Integer.toString(pkg.pkgPrivateFlags));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002642 serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp));
2643 serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime));
2644 serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime));
2645 serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
2646 if (pkg.sharedUser == null) {
Amith Yamasani13593602012-03-22 16:16:17 -07002647 serializer.attribute(null, "userId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002648 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07002649 serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002650 }
2651 if (pkg.uidError) {
2652 serializer.attribute(null, "uidError", "true");
2653 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002654 if (pkg.installStatus == PackageSettingBase.PKG_INSTALL_INCOMPLETE) {
2655 serializer.attribute(null, "installStatus", "false");
2656 }
2657 if (pkg.installerPackageName != null) {
2658 serializer.attribute(null, "installer", pkg.installerPackageName);
2659 }
Sudheer Shanka8c57aea2016-04-20 16:47:50 -07002660 if (pkg.isOrphaned) {
2661 serializer.attribute(null, "isOrphaned", "true");
2662 }
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07002663 if (pkg.volumeUuid != null) {
2664 serializer.attribute(null, "volumeUuid", pkg.volumeUuid);
2665 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08002666 if (pkg.parentPackageName != null) {
2667 serializer.attribute(null, "parentPackageName", pkg.parentPackageName);
2668 }
2669
2670 writeChildPackagesLPw(serializer, pkg.childPackageNames);
2671
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002672 pkg.signatures.writeXml(serializer, "sigs", mPastSignatures);
Svet Ganov8c7f7002015-05-07 10:48:44 -07002673
2674 writePermissionsLPr(serializer, pkg.getPermissionsState()
2675 .getInstallPermissionStates());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002676
dcashman8c04fac2015-03-23 11:39:42 -07002677 writeSigningKeySetLPr(serializer, pkg.keySetData);
dcashman55b10782014-04-09 14:20:38 -07002678 writeUpgradeKeySetsLPr(serializer, pkg.keySetData);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002679 writeKeySetAliasesLPr(serializer, pkg.keySetData);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07002680 writeDomainVerificationsLPr(serializer, pkg.verificationInfo);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002681
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002682 serializer.endTag(null, "package");
2683 }
2684
dcashman8c04fac2015-03-23 11:39:42 -07002685 void writeSigningKeySetLPr(XmlSerializer serializer,
Geremy Condraf1bcca82013-01-07 22:35:24 -08002686 PackageKeySetData data) throws IOException {
dcashman8c04fac2015-03-23 11:39:42 -07002687 serializer.startTag(null, "proper-signing-keyset");
2688 serializer.attribute(null, "identifier",
2689 Long.toString(data.getProperSigningKeySet()));
2690 serializer.endTag(null, "proper-signing-keyset");
dcashman55b10782014-04-09 14:20:38 -07002691 }
2692
2693 void writeUpgradeKeySetsLPr(XmlSerializer serializer,
2694 PackageKeySetData data) throws IOException {
dcashman8c04fac2015-03-23 11:39:42 -07002695 long properSigning = data.getProperSigningKeySet();
dcashman55b10782014-04-09 14:20:38 -07002696 if (data.isUsingUpgradeKeySets()) {
2697 for (long id : data.getUpgradeKeySets()) {
2698 serializer.startTag(null, "upgrade-keyset");
2699 serializer.attribute(null, "identifier", Long.toString(id));
2700 serializer.endTag(null, "upgrade-keyset");
2701 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08002702 }
2703 }
2704
2705 void writeKeySetAliasesLPr(XmlSerializer serializer,
2706 PackageKeySetData data) throws IOException {
2707 for (Map.Entry<String, Long> e: data.getAliases().entrySet()) {
2708 serializer.startTag(null, "defined-keyset");
2709 serializer.attribute(null, "alias", e.getKey());
2710 serializer.attribute(null, "identifier", Long.toString(e.getValue()));
2711 serializer.endTag(null, "defined-keyset");
2712 }
2713 }
2714
Kenny Root447106f2011-03-23 11:00:15 -07002715 void writePermissionLPr(XmlSerializer serializer, BasePermission bp)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002716 throws XmlPullParserException, java.io.IOException {
Svet Ganove5313a82016-04-11 09:59:56 -07002717 if (bp.sourcePackage != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002718 serializer.startTag(null, TAG_ITEM);
2719 serializer.attribute(null, ATTR_NAME, bp.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002720 serializer.attribute(null, "package", bp.sourcePackage);
2721 if (bp.protectionLevel != PermissionInfo.PROTECTION_NORMAL) {
2722 serializer.attribute(null, "protection", Integer.toString(bp.protectionLevel));
2723 }
2724 if (PackageManagerService.DEBUG_SETTINGS)
2725 Log.v(PackageManagerService.TAG, "Writing perm: name=" + bp.name + " type="
2726 + bp.type);
2727 if (bp.type == BasePermission.TYPE_DYNAMIC) {
Kenny Root447106f2011-03-23 11:00:15 -07002728 final PermissionInfo pi = bp.perm != null ? bp.perm.info : bp.pendingInfo;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002729 if (pi != null) {
2730 serializer.attribute(null, "type", "dynamic");
2731 if (pi.icon != 0) {
2732 serializer.attribute(null, "icon", Integer.toString(pi.icon));
2733 }
2734 if (pi.nonLocalizedLabel != null) {
2735 serializer.attribute(null, "label", pi.nonLocalizedLabel.toString());
2736 }
2737 }
2738 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07002739 serializer.endTag(null, TAG_ITEM);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002740 }
2741 }
2742
Kenny Root447106f2011-03-23 11:00:15 -07002743 ArrayList<PackageSetting> getListOfIncompleteInstallPackagesLPr() {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07002744 final ArraySet<String> kList = new ArraySet<String>(mPackages.keySet());
Kenny Root447106f2011-03-23 11:00:15 -07002745 final Iterator<String> its = kList.iterator();
2746 final ArrayList<PackageSetting> ret = new ArrayList<PackageSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002747 while (its.hasNext()) {
Kenny Root447106f2011-03-23 11:00:15 -07002748 final String key = its.next();
2749 final PackageSetting ps = mPackages.get(key);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002750 if (ps.getInstallStatus() == PackageSettingBase.PKG_INSTALL_INCOMPLETE) {
2751 ret.add(ps);
2752 }
2753 }
2754 return ret;
2755 }
2756
Jeff Sharkey752cd922012-09-23 16:25:12 -07002757 void addPackageToCleanLPw(PackageCleanItem pkg) {
2758 if (!mPackagesToBeCleaned.contains(pkg)) {
2759 mPackagesToBeCleaned.add(pkg);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002760 }
2761 }
2762
Xiaohui Chen594f2082015-08-18 11:04:20 -07002763 boolean readLPw(@NonNull List<UserInfo> users) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002764 FileInputStream str = null;
2765 if (mBackupSettingsFilename.exists()) {
2766 try {
2767 str = new FileInputStream(mBackupSettingsFilename);
2768 mReadMessages.append("Reading from backup settings file\n");
2769 PackageManagerService.reportSettingsProblem(Log.INFO,
2770 "Need to read from backup settings file");
2771 if (mSettingsFilename.exists()) {
2772 // If both the backup and settings file exist, we
2773 // ignore the settings since it might have been
2774 // corrupted.
2775 Slog.w(PackageManagerService.TAG, "Cleaning up settings file "
2776 + mSettingsFilename);
2777 mSettingsFilename.delete();
2778 }
2779 } catch (java.io.IOException e) {
2780 // We'll try for the normal settings file.
2781 }
2782 }
2783
Kenny Root447106f2011-03-23 11:00:15 -07002784 mPendingPackages.clear();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002785 mPastSignatures.clear();
dcashman8c04fac2015-03-23 11:39:42 -07002786 mKeySetRefs.clear();
Sudheer Shanka29283372016-04-04 20:56:27 -07002787 mInstallerPackages.clear();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002788
2789 try {
2790 if (str == null) {
2791 if (!mSettingsFilename.exists()) {
2792 mReadMessages.append("No settings file found\n");
2793 PackageManagerService.reportSettingsProblem(Log.INFO,
2794 "No settings file; creating initial state");
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002795 // It's enough to just touch version details to create them
2796 // with default values
longhaibc62b212016-01-29 14:22:22 +08002797 findOrCreateVersion(StorageManager.UUID_PRIVATE_INTERNAL).forceCurrent();
2798 findOrCreateVersion(StorageManager.UUID_PRIMARY_PHYSICAL).forceCurrent();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002799 return false;
2800 }
2801 str = new FileInputStream(mSettingsFilename);
2802 }
2803 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002804 parser.setInput(str, StandardCharsets.UTF_8.name());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002805
2806 int type;
2807 while ((type = parser.next()) != XmlPullParser.START_TAG
2808 && type != XmlPullParser.END_DOCUMENT) {
2809 ;
2810 }
2811
2812 if (type != XmlPullParser.START_TAG) {
2813 mReadMessages.append("No start tag found in settings file\n");
2814 PackageManagerService.reportSettingsProblem(Log.WARN,
2815 "No start tag found in package manager settings");
Dianne Hackborn8d051722014-10-01 14:59:58 -07002816 Slog.wtf(PackageManagerService.TAG,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002817 "No start tag found in package manager settings");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002818 return false;
2819 }
2820
2821 int outerDepth = parser.getDepth();
2822 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2823 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2824 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2825 continue;
2826 }
2827
2828 String tagName = parser.getName();
2829 if (tagName.equals("package")) {
Kenny Root447106f2011-03-23 11:00:15 -07002830 readPackageLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002831 } else if (tagName.equals("permissions")) {
Kenny Root447106f2011-03-23 11:00:15 -07002832 readPermissionsLPw(mPermissions, parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002833 } else if (tagName.equals("permission-trees")) {
Kenny Root447106f2011-03-23 11:00:15 -07002834 readPermissionsLPw(mPermissionTrees, parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002835 } else if (tagName.equals("shared-user")) {
Kenny Root447106f2011-03-23 11:00:15 -07002836 readSharedUserLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002837 } else if (tagName.equals("preferred-packages")) {
2838 // no longer used.
2839 } else if (tagName.equals("preferred-activities")) {
Dianne Hackborn63092712012-10-07 14:45:35 -07002840 // Upgrading from old single-user implementation;
2841 // these are the preferred activities for user 0.
2842 readPreferredActivitiesLPw(parser, 0);
Sander Alewijnseaf597622014-03-20 18:44:57 +00002843 } else if (tagName.equals(TAG_PERSISTENT_PREFERRED_ACTIVITIES)) {
Sander Alewijnsef475ca32014-02-17 15:13:58 +00002844 // TODO: check whether this is okay! as it is very
2845 // similar to how preferred-activities are treated
2846 readPersistentPreferredActivitiesLPw(parser, 0);
Nicolas Prevot29762c32014-07-29 18:51:32 +01002847 } else if (tagName.equals(TAG_CROSS_PROFILE_INTENT_FILTERS)) {
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00002848 // TODO: check whether this is okay! as it is very
2849 // similar to how preferred-activities are treated
Nicolas Prevot81948992014-05-16 18:25:26 +01002850 readCrossProfileIntentFiltersLPw(parser, 0);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07002851 } else if (tagName.equals(TAG_DEFAULT_BROWSER)) {
2852 readDefaultAppsLPw(parser, 0);
2853 } else if (tagName.equals("updated-package")) {
Kenny Root447106f2011-03-23 11:00:15 -07002854 readDisabledSysPackageLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002855 } else if (tagName.equals("cleaning-package")) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002856 String name = parser.getAttributeValue(null, ATTR_NAME);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002857 String userStr = parser.getAttributeValue(null, ATTR_USER);
2858 String codeStr = parser.getAttributeValue(null, ATTR_CODE);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002859 if (name != null) {
Xiaohui Chen594f2082015-08-18 11:04:20 -07002860 int userId = UserHandle.USER_SYSTEM;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002861 boolean andCode = true;
2862 try {
2863 if (userStr != null) {
Jeff Sharkey752cd922012-09-23 16:25:12 -07002864 userId = Integer.parseInt(userStr);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002865 }
2866 } catch (NumberFormatException e) {
2867 }
2868 if (codeStr != null) {
2869 andCode = Boolean.parseBoolean(codeStr);
2870 }
Jeff Sharkey752cd922012-09-23 16:25:12 -07002871 addPackageToCleanLPw(new PackageCleanItem(userId, name, andCode));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002872 }
2873 } else if (tagName.equals("renamed-package")) {
2874 String nname = parser.getAttributeValue(null, "new");
2875 String oname = parser.getAttributeValue(null, "old");
2876 if (nname != null && oname != null) {
2877 mRenamedPackages.put(nname, oname);
2878 }
Christopher Tate6038d152015-06-17 13:07:46 -07002879 } else if (tagName.equals("restored-ivi")) {
2880 readRestoredIntentFilterVerifications(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002881 } else if (tagName.equals("last-platform-version")) {
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002882 // Upgrade from older XML schema
2883 final VersionInfo internal = findOrCreateVersion(
2884 StorageManager.UUID_PRIVATE_INTERNAL);
2885 final VersionInfo external = findOrCreateVersion(
2886 StorageManager.UUID_PRIMARY_PHYSICAL);
Svet Ganovadc1cf42015-06-15 16:36:24 -07002887
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002888 internal.sdkVersion = XmlUtils.readIntAttribute(parser, "internal", 0);
2889 external.sdkVersion = XmlUtils.readIntAttribute(parser, "external", 0);
2890 internal.fingerprint = external.fingerprint =
2891 XmlUtils.readStringAttribute(parser, "fingerprint");
2892
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07002893 } else if (tagName.equals("database-version")) {
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002894 // Upgrade from older XML schema
2895 final VersionInfo internal = findOrCreateVersion(
2896 StorageManager.UUID_PRIVATE_INTERNAL);
2897 final VersionInfo external = findOrCreateVersion(
2898 StorageManager.UUID_PRIMARY_PHYSICAL);
2899
2900 internal.databaseVersion = XmlUtils.readIntAttribute(parser, "internal", 0);
2901 external.databaseVersion = XmlUtils.readIntAttribute(parser, "external", 0);
2902
Kenny Root0aaa0d92011-09-12 16:42:55 -07002903 } else if (tagName.equals("verifier")) {
2904 final String deviceIdentity = parser.getAttributeValue(null, "device");
2905 try {
2906 mVerifierDeviceIdentity = VerifierDeviceIdentity.parse(deviceIdentity);
2907 } catch (IllegalArgumentException e) {
2908 Slog.w(PackageManagerService.TAG, "Discard invalid verifier device id: "
2909 + e.getMessage());
2910 }
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002911 } else if (TAG_READ_EXTERNAL_STORAGE.equals(tagName)) {
2912 final String enforcement = parser.getAttributeValue(null, ATTR_ENFORCEMENT);
Jeff Sharkey5d32e772012-04-12 15:59:23 -07002913 mReadExternalStorageEnforced = "1".equals(enforcement);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002914 } else if (tagName.equals("keyset-settings")) {
dcashman8c04fac2015-03-23 11:39:42 -07002915 mKeySetManagerService.readKeySetsLPw(parser, mKeySetRefs);
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002916 } else if (TAG_VERSION.equals(tagName)) {
2917 final String volumeUuid = XmlUtils.readStringAttribute(parser,
2918 ATTR_VOLUME_UUID);
2919 final VersionInfo ver = findOrCreateVersion(volumeUuid);
2920 ver.sdkVersion = XmlUtils.readIntAttribute(parser, ATTR_SDK_VERSION);
2921 ver.databaseVersion = XmlUtils.readIntAttribute(parser, ATTR_SDK_VERSION);
2922 ver.fingerprint = XmlUtils.readStringAttribute(parser, ATTR_FINGERPRINT);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002923 } else {
2924 Slog.w(PackageManagerService.TAG, "Unknown element under <packages>: "
2925 + parser.getName());
2926 XmlUtils.skipCurrentTag(parser);
2927 }
2928 }
2929
2930 str.close();
2931
2932 } catch (XmlPullParserException e) {
2933 mReadMessages.append("Error reading: " + e.toString());
2934 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002935 Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002936
2937 } catch (java.io.IOException e) {
2938 mReadMessages.append("Error reading: " + e.toString());
2939 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002940 Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
Amith Yamasani258848d2012-08-10 17:06:33 -07002941 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002942
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002943 // If the build is setup to drop runtime permissions
2944 // on update drop the files before loading them.
2945 if (PackageManagerService.CLEAR_RUNTIME_PERMISSIONS_ON_UPGRADE) {
2946 final VersionInfo internal = getInternalVersion();
2947 if (!Build.FINGERPRINT.equals(internal.fingerprint)) {
Xiaohui Chen594f2082015-08-18 11:04:20 -07002948 for (UserInfo user : users) {
2949 mRuntimePermissionsPersistence.deleteUserRuntimePermissionsFile(user.id);
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002950 }
2951 }
2952 }
2953
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002954 final int N = mPendingPackages.size();
dcashman8c04fac2015-03-23 11:39:42 -07002955
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002956 for (int i = 0; i < N; i++) {
2957 final PendingPackage pp = mPendingPackages.get(i);
Kenny Root447106f2011-03-23 11:00:15 -07002958 Object idObj = getUserIdLPr(pp.sharedId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002959 if (idObj != null && idObj instanceof SharedUserSetting) {
Kenny Root447106f2011-03-23 11:00:15 -07002960 PackageSetting p = getPackageLPw(pp.name, null, pp.realName,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002961 (SharedUserSetting) idObj, pp.codePath, pp.resourcePath,
Narayan Kamathff110bd2014-07-04 18:30:45 +01002962 pp.legacyNativeLibraryPathString, pp.primaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002963 pp.secondaryCpuAbiString, pp.versionCode, pp.pkgFlags, pp.pkgPrivateFlags,
Svet Ganov354cd3c2015-12-17 11:35:04 -08002964 null, true /* add */, false /* allowInstall */, pp.parentPackageName,
2965 pp.childPackageNames);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002966 if (p == null) {
2967 PackageManagerService.reportSettingsProblem(Log.WARN,
2968 "Unable to create application package for " + pp.name);
2969 continue;
2970 }
2971 p.copyFrom(pp);
2972 } else if (idObj != null) {
2973 String msg = "Bad package setting: package " + pp.name + " has shared uid "
2974 + pp.sharedId + " that is not a shared uid\n";
2975 mReadMessages.append(msg);
2976 PackageManagerService.reportSettingsProblem(Log.ERROR, msg);
2977 } else {
2978 String msg = "Bad package setting: package " + pp.name + " has shared uid "
2979 + pp.sharedId + " that is not defined\n";
2980 mReadMessages.append(msg);
2981 PackageManagerService.reportSettingsProblem(Log.ERROR, msg);
2982 }
2983 }
2984 mPendingPackages.clear();
2985
Amith Yamasanif031f232012-10-26 15:35:21 -07002986 if (mBackupStoppedPackagesFilename.exists()
2987 || mStoppedPackagesFilename.exists()) {
2988 // Read old file
2989 readStoppedLPw();
2990 mBackupStoppedPackagesFilename.delete();
2991 mStoppedPackagesFilename.delete();
2992 // Migrate to new file format
Xiaohui Chen594f2082015-08-18 11:04:20 -07002993 writePackageRestrictionsLPr(UserHandle.USER_SYSTEM);
Amith Yamasanif031f232012-10-26 15:35:21 -07002994 } else {
Xiaohui Chen594f2082015-08-18 11:04:20 -07002995 for (UserInfo user : users) {
2996 readPackageRestrictionsLPr(user.id);
Amith Yamasanif031f232012-10-26 15:35:21 -07002997 }
2998 }
2999
Xiaohui Chen594f2082015-08-18 11:04:20 -07003000 for (UserInfo user : users) {
3001 mRuntimePermissionsPersistence.readStateForUserSyncLPr(user.id);
Svet Ganovadc1cf42015-06-15 16:36:24 -07003002 }
3003
Kenny Root1d1b4892011-04-08 14:25:24 -07003004 /*
3005 * Make sure all the updated system packages have their shared users
3006 * associated with them.
3007 */
3008 final Iterator<PackageSetting> disabledIt = mDisabledSysPackages.values().iterator();
3009 while (disabledIt.hasNext()) {
3010 final PackageSetting disabledPs = disabledIt.next();
Amith Yamasani13593602012-03-22 16:16:17 -07003011 final Object id = getUserIdLPr(disabledPs.appId);
Kenny Root1d1b4892011-04-08 14:25:24 -07003012 if (id != null && id instanceof SharedUserSetting) {
3013 disabledPs.sharedUser = (SharedUserSetting) id;
3014 }
3015 }
3016
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003017 mReadMessages.append("Read completed successfully: " + mPackages.size() + " packages, "
3018 + mSharedUsers.size() + " shared uids\n");
3019
Jeff Sharkey2271ba32016-02-01 17:57:08 -07003020 writeKernelMappingLPr();
3021
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003022 return true;
3023 }
3024
Christopher Tatedb3fe812015-06-24 16:15:48 -07003025 void applyDefaultPreferredAppsLPw(PackageManagerService service, int userId) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003026 // First pull data from any pre-installed apps.
3027 for (PackageSetting ps : mPackages.values()) {
3028 if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0 && ps.pkg != null
3029 && ps.pkg.preferredActivityFilters != null) {
3030 ArrayList<PackageParser.ActivityIntentInfo> intents
3031 = ps.pkg.preferredActivityFilters;
3032 for (int i=0; i<intents.size(); i++) {
3033 PackageParser.ActivityIntentInfo aii = intents.get(i);
3034 applyDefaultPreferredActivityLPw(service, aii, new ComponentName(
3035 ps.name, aii.activity.className), userId);
3036 }
3037 }
3038 }
3039
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07003040 // Read preferred apps from .../etc/preferred-apps directory.
3041 File preferredDir = new File(Environment.getRootDirectory(), "etc/preferred-apps");
3042 if (!preferredDir.exists() || !preferredDir.isDirectory()) {
3043 return;
3044 }
3045 if (!preferredDir.canRead()) {
3046 Slog.w(TAG, "Directory " + preferredDir + " cannot be read");
3047 return;
3048 }
3049
3050 // Iterate over the files in the directory and scan .xml files
3051 for (File f : preferredDir.listFiles()) {
3052 if (!f.getPath().endsWith(".xml")) {
3053 Slog.i(TAG, "Non-xml file " + f + " in " + preferredDir + " directory, ignoring");
3054 continue;
3055 }
3056 if (!f.canRead()) {
3057 Slog.w(TAG, "Preferred apps file " + f + " cannot be read");
3058 continue;
3059 }
3060
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003061 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Reading default preferred " + f);
Fyodor Kupolovb35b34c2015-12-17 14:16:51 -08003062 InputStream str = null;
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07003063 try {
Fyodor Kupolovb35b34c2015-12-17 14:16:51 -08003064 str = new BufferedInputStream(new FileInputStream(f));
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07003065 XmlPullParser parser = Xml.newPullParser();
3066 parser.setInput(str, null);
3067
3068 int type;
3069 while ((type = parser.next()) != XmlPullParser.START_TAG
3070 && type != XmlPullParser.END_DOCUMENT) {
3071 ;
3072 }
3073
3074 if (type != XmlPullParser.START_TAG) {
3075 Slog.w(TAG, "Preferred apps file " + f + " does not have start tag");
3076 continue;
3077 }
3078 if (!"preferred-activities".equals(parser.getName())) {
3079 Slog.w(TAG, "Preferred apps file " + f
3080 + " does not start with 'preferred-activities'");
3081 continue;
3082 }
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003083 readDefaultPreferredActivitiesLPw(service, parser, userId);
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07003084 } catch (XmlPullParserException e) {
3085 Slog.w(TAG, "Error reading apps file " + f, e);
3086 } catch (IOException e) {
3087 Slog.w(TAG, "Error reading apps file " + f, e);
3088 } finally {
3089 if (str != null) {
3090 try {
3091 str.close();
3092 } catch (IOException e) {
3093 }
3094 }
3095 }
3096 }
3097 }
3098
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003099 private void applyDefaultPreferredActivityLPw(PackageManagerService service,
3100 IntentFilter tmpPa, ComponentName cn, int userId) {
3101 // The initial preferences only specify the target activity
3102 // component and intent-filter, not the set of matches. So we
3103 // now need to query for the matches to build the correct
3104 // preferred activity entry.
3105 if (PackageManagerService.DEBUG_PREFERRED) {
3106 Log.d(TAG, "Processing preferred:");
3107 tmpPa.dump(new LogPrinter(Log.DEBUG, TAG), " ");
3108 }
3109 Intent intent = new Intent();
Jeff Sharkey8a372a02016-03-16 16:25:45 -06003110 int flags = PackageManager.MATCH_DIRECT_BOOT_AWARE
3111 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003112 intent.setAction(tmpPa.getAction(0));
3113 for (int i=0; i<tmpPa.countCategories(); i++) {
3114 String cat = tmpPa.getCategory(i);
3115 if (cat.equals(Intent.CATEGORY_DEFAULT)) {
Jeff Sharkey2a90f6732016-01-06 12:26:11 -07003116 flags |= MATCH_DEFAULT_ONLY;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003117 } else {
3118 intent.addCategory(cat);
3119 }
3120 }
3121
3122 boolean doNonData = true;
Christopher Tate2298ef22013-11-04 17:02:10 -08003123 boolean hasSchemes = false;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003124
3125 for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) {
3126 boolean doScheme = true;
3127 String scheme = tmpPa.getDataScheme(ischeme);
Christopher Tate2298ef22013-11-04 17:02:10 -08003128 if (scheme != null && !scheme.isEmpty()) {
3129 hasSchemes = true;
3130 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003131 for (int issp=0; issp<tmpPa.countDataSchemeSpecificParts(); issp++) {
3132 Uri.Builder builder = new Uri.Builder();
3133 builder.scheme(scheme);
3134 PatternMatcher ssp = tmpPa.getDataSchemeSpecificPart(issp);
3135 builder.opaquePart(ssp.getPath());
3136 Intent finalIntent = new Intent(intent);
3137 finalIntent.setData(builder.build());
3138 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003139 scheme, ssp, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003140 doScheme = false;
3141 }
3142 for (int iauth=0; iauth<tmpPa.countDataAuthorities(); iauth++) {
3143 boolean doAuth = true;
3144 IntentFilter.AuthorityEntry auth = tmpPa.getDataAuthority(iauth);
3145 for (int ipath=0; ipath<tmpPa.countDataPaths(); ipath++) {
3146 Uri.Builder builder = new Uri.Builder();
3147 builder.scheme(scheme);
3148 if (auth.getHost() != null) {
3149 builder.authority(auth.getHost());
3150 }
3151 PatternMatcher path = tmpPa.getDataPath(ipath);
3152 builder.path(path.getPath());
3153 Intent finalIntent = new Intent(intent);
3154 finalIntent.setData(builder.build());
3155 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003156 scheme, null, auth, path, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003157 doAuth = doScheme = false;
3158 }
3159 if (doAuth) {
3160 Uri.Builder builder = new Uri.Builder();
3161 builder.scheme(scheme);
3162 if (auth.getHost() != null) {
3163 builder.authority(auth.getHost());
3164 }
3165 Intent finalIntent = new Intent(intent);
3166 finalIntent.setData(builder.build());
3167 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003168 scheme, null, auth, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003169 doScheme = false;
3170 }
3171 }
3172 if (doScheme) {
3173 Uri.Builder builder = new Uri.Builder();
3174 builder.scheme(scheme);
3175 Intent finalIntent = new Intent(intent);
3176 finalIntent.setData(builder.build());
3177 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003178 scheme, null, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003179 }
3180 doNonData = false;
3181 }
3182
3183 for (int idata=0; idata<tmpPa.countDataTypes(); idata++) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003184 String mimeType = tmpPa.getDataType(idata);
Christopher Tate2298ef22013-11-04 17:02:10 -08003185 if (hasSchemes) {
3186 Uri.Builder builder = new Uri.Builder();
3187 for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) {
3188 String scheme = tmpPa.getDataScheme(ischeme);
3189 if (scheme != null && !scheme.isEmpty()) {
3190 Intent finalIntent = new Intent(intent);
3191 builder.scheme(scheme);
3192 finalIntent.setDataAndType(builder.build(), mimeType);
3193 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003194 scheme, null, null, null, userId);
Christopher Tate2298ef22013-11-04 17:02:10 -08003195 }
3196 }
3197 } else {
3198 Intent finalIntent = new Intent(intent);
3199 finalIntent.setType(mimeType);
3200 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003201 null, null, null, null, userId);
Christopher Tate2298ef22013-11-04 17:02:10 -08003202 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003203 doNonData = false;
3204 }
3205
3206 if (doNonData) {
3207 applyDefaultPreferredActivityLPw(service, intent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003208 null, null, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003209 }
3210 }
3211
3212 private void applyDefaultPreferredActivityLPw(PackageManagerService service,
3213 Intent intent, int flags, ComponentName cn, String scheme, PatternMatcher ssp,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003214 IntentFilter.AuthorityEntry auth, PatternMatcher path, int userId) {
Svetoslav Ganov5fd83dc2016-01-20 17:27:48 -08003215 flags = service.updateFlagsForResolve(flags, userId, intent);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003216 List<ResolveInfo> ri = service.mActivities.queryIntent(intent,
3217 intent.getType(), flags, 0);
3218 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Queried " + intent
3219 + " results: " + ri);
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003220 int systemMatch = 0;
3221 int thirdPartyMatch = 0;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003222 if (ri != null && ri.size() > 1) {
3223 boolean haveAct = false;
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003224 ComponentName haveNonSys = null;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003225 ComponentName[] set = new ComponentName[ri.size()];
3226 for (int i=0; i<ri.size(); i++) {
3227 ActivityInfo ai = ri.get(i).activityInfo;
3228 set[i] = new ComponentName(ai.packageName, ai.name);
3229 if ((ai.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003230 if (ri.get(i).match >= thirdPartyMatch) {
3231 // Keep track of the best match we find of all third
3232 // party apps, for use later to determine if we actually
3233 // want to set a preferred app for this intent.
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003234 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
3235 + ai.packageName + "/" + ai.name + ": non-system!");
3236 haveNonSys = set[i];
3237 break;
3238 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003239 } else if (cn.getPackageName().equals(ai.packageName)
3240 && cn.getClassName().equals(ai.name)) {
3241 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
3242 + ai.packageName + "/" + ai.name + ": default!");
3243 haveAct = true;
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003244 systemMatch = ri.get(i).match;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003245 } else {
3246 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
3247 + ai.packageName + "/" + ai.name + ": skipped");
3248 }
3249 }
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003250 if (haveNonSys != null && thirdPartyMatch < systemMatch) {
3251 // If we have a matching third party app, but its match is not as
3252 // good as the built-in system app, then we don't want to actually
3253 // consider it a match because presumably the built-in app is still
3254 // the thing we want users to see by default.
3255 haveNonSys = null;
3256 }
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003257 if (haveAct && haveNonSys == null) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003258 IntentFilter filter = new IntentFilter();
3259 if (intent.getAction() != null) {
3260 filter.addAction(intent.getAction());
3261 }
Erin Dahlgren707a59d2013-10-24 15:13:39 -07003262 if (intent.getCategories() != null) {
3263 for (String cat : intent.getCategories()) {
3264 filter.addCategory(cat);
3265 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003266 }
Jeff Sharkey2a90f6732016-01-06 12:26:11 -07003267 if ((flags & MATCH_DEFAULT_ONLY) != 0) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003268 filter.addCategory(Intent.CATEGORY_DEFAULT);
3269 }
3270 if (scheme != null) {
3271 filter.addDataScheme(scheme);
3272 }
3273 if (ssp != null) {
3274 filter.addDataSchemeSpecificPart(ssp.getPath(), ssp.getType());
3275 }
3276 if (auth != null) {
3277 filter.addDataAuthority(auth);
3278 }
3279 if (path != null) {
3280 filter.addDataPath(path);
3281 }
Erin Dahlgren707a59d2013-10-24 15:13:39 -07003282 if (intent.getType() != null) {
3283 try {
3284 filter.addDataType(intent.getType());
3285 } catch (IntentFilter.MalformedMimeTypeException ex) {
3286 Slog.w(TAG, "Malformed mimetype " + intent.getType() + " for " + cn);
3287 }
3288 }
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003289 PreferredActivity pa = new PreferredActivity(filter, systemMatch, set, cn, true);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003290 editPreferredActivitiesLPw(userId).addFilter(pa);
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003291 } else if (haveNonSys == null) {
3292 StringBuilder sb = new StringBuilder();
3293 sb.append("No component ");
3294 sb.append(cn.flattenToShortString());
3295 sb.append(" found setting preferred ");
3296 sb.append(intent);
3297 sb.append("; possible matches are ");
3298 for (int i=0; i<set.length; i++) {
3299 if (i > 0) sb.append(", ");
3300 sb.append(set[i].flattenToShortString());
3301 }
3302 Slog.w(TAG, sb.toString());
3303 } else {
3304 Slog.i(TAG, "Not setting preferred " + intent + "; found third party match "
3305 + haveNonSys.flattenToShortString());
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003306 }
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003307 } else {
3308 Slog.w(TAG, "No potential matches found for " + intent + " while setting preferred "
3309 + cn.flattenToShortString());
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003310 }
3311 }
3312
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003313 private void readDefaultPreferredActivitiesLPw(PackageManagerService service,
3314 XmlPullParser parser, int userId)
3315 throws XmlPullParserException, IOException {
3316 int outerDepth = parser.getDepth();
3317 int type;
3318 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3319 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3320 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3321 continue;
3322 }
3323
3324 String tagName = parser.getName();
3325 if (tagName.equals(TAG_ITEM)) {
3326 PreferredActivity tmpPa = new PreferredActivity(parser);
3327 if (tmpPa.mPref.getParseError() == null) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003328 applyDefaultPreferredActivityLPw(service, tmpPa, tmpPa.mPref.mComponent,
3329 userId);
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003330 } else {
3331 PackageManagerService.reportSettingsProblem(Log.WARN,
3332 "Error in package manager settings: <preferred-activity> "
3333 + tmpPa.mPref.getParseError() + " at "
3334 + parser.getPositionDescription());
3335 }
3336 } else {
3337 PackageManagerService.reportSettingsProblem(Log.WARN,
3338 "Unknown element under <preferred-activities>: " + parser.getName());
3339 XmlUtils.skipCurrentTag(parser);
3340 }
3341 }
3342 }
3343
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003344 private int readInt(XmlPullParser parser, String ns, String name, int defValue) {
3345 String v = parser.getAttributeValue(ns, name);
3346 try {
3347 if (v == null) {
3348 return defValue;
3349 }
3350 return Integer.parseInt(v);
3351 } catch (NumberFormatException e) {
3352 PackageManagerService.reportSettingsProblem(Log.WARN,
3353 "Error in package manager settings: attribute " + name
3354 + " has bad integer value " + v + " at "
3355 + parser.getPositionDescription());
3356 }
3357 return defValue;
3358 }
3359
Jeff Sharkey9f837a92014-10-24 12:07:24 -07003360 private void readPermissionsLPw(ArrayMap<String, BasePermission> out, XmlPullParser parser)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003361 throws IOException, XmlPullParserException {
3362 int outerDepth = parser.getDepth();
3363 int type;
3364 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3365 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3366 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3367 continue;
3368 }
3369
Kenny Root447106f2011-03-23 11:00:15 -07003370 final String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003371 if (tagName.equals(TAG_ITEM)) {
3372 final String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Root447106f2011-03-23 11:00:15 -07003373 final String sourcePackage = parser.getAttributeValue(null, "package");
3374 final String ptype = parser.getAttributeValue(null, "type");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003375 if (name != null && sourcePackage != null) {
Kenny Root447106f2011-03-23 11:00:15 -07003376 final boolean dynamic = "dynamic".equals(ptype);
Svet Ganove5313a82016-04-11 09:59:56 -07003377 BasePermission bp = out.get(name);
3378 // If the permission is builtin, do not clobber it.
3379 if (bp == null || bp.type != BasePermission.TYPE_BUILTIN) {
3380 bp = new BasePermission(name.intern(), sourcePackage,
3381 dynamic ? BasePermission.TYPE_DYNAMIC : BasePermission.TYPE_NORMAL);
3382 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003383 bp.protectionLevel = readInt(parser, null, "protection",
3384 PermissionInfo.PROTECTION_NORMAL);
Dianne Hackborne639da72012-02-21 15:11:13 -08003385 bp.protectionLevel = PermissionInfo.fixProtectionLevel(bp.protectionLevel);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003386 if (dynamic) {
3387 PermissionInfo pi = new PermissionInfo();
3388 pi.packageName = sourcePackage.intern();
3389 pi.name = name.intern();
3390 pi.icon = readInt(parser, null, "icon", 0);
3391 pi.nonLocalizedLabel = parser.getAttributeValue(null, "label");
3392 pi.protectionLevel = bp.protectionLevel;
3393 bp.pendingInfo = pi;
3394 }
3395 out.put(bp.name, bp);
3396 } else {
3397 PackageManagerService.reportSettingsProblem(Log.WARN,
3398 "Error in package manager settings: permissions has" + " no name at "
3399 + parser.getPositionDescription());
3400 }
3401 } else {
3402 PackageManagerService.reportSettingsProblem(Log.WARN,
3403 "Unknown element reading permissions: " + parser.getName() + " at "
3404 + parser.getPositionDescription());
3405 }
3406 XmlUtils.skipCurrentTag(parser);
3407 }
3408 }
3409
Kenny Root447106f2011-03-23 11:00:15 -07003410 private void readDisabledSysPackageLPw(XmlPullParser parser) throws XmlPullParserException,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003411 IOException {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003412 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003413 String realName = parser.getAttributeValue(null, "realName");
3414 String codePathStr = parser.getAttributeValue(null, "codePath");
3415 String resourcePathStr = parser.getAttributeValue(null, "resourcePath");
Narayan Kamathff110bd2014-07-04 18:30:45 +01003416
3417 String legacyCpuAbiStr = parser.getAttributeValue(null, "requiredCpuAbi");
3418 String legacyNativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath");
3419
Svet Ganov354cd3c2015-12-17 11:35:04 -08003420 String parentPackageName = parser.getAttributeValue(null, "parentPackageName");
3421
Narayan Kamathff110bd2014-07-04 18:30:45 +01003422 String primaryCpuAbiStr = parser.getAttributeValue(null, "primaryCpuAbi");
3423 String secondaryCpuAbiStr = parser.getAttributeValue(null, "secondaryCpuAbi");
Narayan Kamath4903f642014-08-11 13:33:45 +01003424 String cpuAbiOverrideStr = parser.getAttributeValue(null, "cpuAbiOverride");
Narayan Kamathff110bd2014-07-04 18:30:45 +01003425
3426 if (primaryCpuAbiStr == null && legacyCpuAbiStr != null) {
3427 primaryCpuAbiStr = legacyCpuAbiStr;
3428 }
Narayan Kamath9e289d72014-04-10 09:26:59 +00003429
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003430 if (resourcePathStr == null) {
3431 resourcePathStr = codePathStr;
3432 }
3433 String version = parser.getAttributeValue(null, "version");
3434 int versionCode = 0;
3435 if (version != null) {
3436 try {
3437 versionCode = Integer.parseInt(version);
3438 } catch (NumberFormatException e) {
3439 }
3440 }
3441
3442 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003443 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003444 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
Christopher Tate628946a2013-10-18 18:11:05 -07003445 final File codePathFile = new File(codePathStr);
3446 if (PackageManagerService.locationIsPrivileged(codePathFile)) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003447 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Christopher Tate628946a2013-10-18 18:11:05 -07003448 }
3449 PackageSetting ps = new PackageSetting(name, realName, codePathFile,
Narayan Kamathff110bd2014-07-04 18:30:45 +01003450 new File(resourcePathStr), legacyNativeLibraryPathStr, primaryCpuAbiStr,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003451 secondaryCpuAbiStr, cpuAbiOverrideStr, versionCode, pkgFlags, pkgPrivateFlags,
3452 parentPackageName, null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003453 String timeStampStr = parser.getAttributeValue(null, "ft");
3454 if (timeStampStr != null) {
3455 try {
3456 long timeStamp = Long.parseLong(timeStampStr, 16);
3457 ps.setTimeStamp(timeStamp);
3458 } catch (NumberFormatException e) {
3459 }
3460 } else {
3461 timeStampStr = parser.getAttributeValue(null, "ts");
3462 if (timeStampStr != null) {
3463 try {
3464 long timeStamp = Long.parseLong(timeStampStr);
3465 ps.setTimeStamp(timeStamp);
3466 } catch (NumberFormatException e) {
3467 }
3468 }
3469 }
3470 timeStampStr = parser.getAttributeValue(null, "it");
3471 if (timeStampStr != null) {
3472 try {
3473 ps.firstInstallTime = Long.parseLong(timeStampStr, 16);
3474 } catch (NumberFormatException e) {
3475 }
3476 }
3477 timeStampStr = parser.getAttributeValue(null, "ut");
3478 if (timeStampStr != null) {
3479 try {
3480 ps.lastUpdateTime = Long.parseLong(timeStampStr, 16);
3481 } catch (NumberFormatException e) {
3482 }
3483 }
3484 String idStr = parser.getAttributeValue(null, "userId");
Amith Yamasani13593602012-03-22 16:16:17 -07003485 ps.appId = idStr != null ? Integer.parseInt(idStr) : 0;
3486 if (ps.appId <= 0) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003487 String sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
Amith Yamasani13593602012-03-22 16:16:17 -07003488 ps.appId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003489 }
Svetoslavc6d1c342015-02-26 14:44:43 -08003490
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003491 int outerDepth = parser.getDepth();
3492 int type;
3493 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3494 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3495 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3496 continue;
3497 }
3498
Svetoslavc6d1c342015-02-26 14:44:43 -08003499 if (parser.getName().equals(TAG_PERMISSIONS)) {
3500 readInstallPermissionsLPr(parser, ps.getPermissionsState());
Svet Ganov354cd3c2015-12-17 11:35:04 -08003501 } else if (parser.getName().equals(TAG_CHILD_PACKAGE)) {
3502 String childPackageName = parser.getAttributeValue(null, ATTR_NAME);
3503 if (ps.childPackageNames == null) {
3504 ps.childPackageNames = new ArrayList<>();
3505 }
3506 ps.childPackageNames.add(childPackageName);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003507 } else {
3508 PackageManagerService.reportSettingsProblem(Log.WARN,
3509 "Unknown element under <updated-package>: " + parser.getName());
3510 XmlUtils.skipCurrentTag(parser);
3511 }
3512 }
Christopher Tate628946a2013-10-18 18:11:05 -07003513
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003514 mDisabledSysPackages.put(name, ps);
3515 }
3516
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003517 private static int PRE_M_APP_INFO_FLAG_HIDDEN = 1<<27;
3518 private static int PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE = 1<<28;
3519 private static int PRE_M_APP_INFO_FLAG_FORWARD_LOCK = 1<<29;
3520 private static int PRE_M_APP_INFO_FLAG_PRIVILEGED = 1<<30;
3521
Kenny Root447106f2011-03-23 11:00:15 -07003522 private void readPackageLPw(XmlPullParser parser) throws XmlPullParserException, IOException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003523 String name = null;
3524 String realName = null;
3525 String idStr = null;
3526 String sharedIdStr = null;
3527 String codePathStr = null;
3528 String resourcePathStr = null;
Narayan Kamathff110bd2014-07-04 18:30:45 +01003529 String legacyCpuAbiString = null;
3530 String legacyNativeLibraryPathStr = null;
3531 String primaryCpuAbiString = null;
3532 String secondaryCpuAbiString = null;
Narayan Kamath4903f642014-08-11 13:33:45 +01003533 String cpuAbiOverrideString = null;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003534 String systemStr = null;
3535 String installerPackageName = null;
Sudheer Shanka8c57aea2016-04-20 16:47:50 -07003536 String isOrphaned = null;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003537 String volumeUuid = null;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003538 String uidError = null;
3539 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003540 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003541 long timeStamp = 0;
3542 long firstInstallTime = 0;
3543 long lastUpdateTime = 0;
3544 PackageSettingBase packageSetting = null;
3545 String version = null;
3546 int versionCode = 0;
Svet Ganov354cd3c2015-12-17 11:35:04 -08003547 String parentPackageName;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003548 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003549 name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003550 realName = parser.getAttributeValue(null, "realName");
3551 idStr = parser.getAttributeValue(null, "userId");
3552 uidError = parser.getAttributeValue(null, "uidError");
3553 sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
3554 codePathStr = parser.getAttributeValue(null, "codePath");
3555 resourcePathStr = parser.getAttributeValue(null, "resourcePath");
Narayan Kamath9e289d72014-04-10 09:26:59 +00003556
Narayan Kamathff110bd2014-07-04 18:30:45 +01003557 legacyCpuAbiString = parser.getAttributeValue(null, "requiredCpuAbi");
3558
Svet Ganov354cd3c2015-12-17 11:35:04 -08003559 parentPackageName = parser.getAttributeValue(null, "parentPackageName");
3560
Narayan Kamathff110bd2014-07-04 18:30:45 +01003561 legacyNativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath");
3562 primaryCpuAbiString = parser.getAttributeValue(null, "primaryCpuAbi");
3563 secondaryCpuAbiString = parser.getAttributeValue(null, "secondaryCpuAbi");
Narayan Kamath4903f642014-08-11 13:33:45 +01003564 cpuAbiOverrideString = parser.getAttributeValue(null, "cpuAbiOverride");
Narayan Kamathff110bd2014-07-04 18:30:45 +01003565
3566 if (primaryCpuAbiString == null && legacyCpuAbiString != null) {
3567 primaryCpuAbiString = legacyCpuAbiString;
3568 }
Svetoslavc6d1c342015-02-26 14:44:43 -08003569
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003570 version = parser.getAttributeValue(null, "version");
3571 if (version != null) {
3572 try {
3573 versionCode = Integer.parseInt(version);
3574 } catch (NumberFormatException e) {
3575 }
3576 }
3577 installerPackageName = parser.getAttributeValue(null, "installer");
Sudheer Shanka8c57aea2016-04-20 16:47:50 -07003578 isOrphaned = parser.getAttributeValue(null, "isOrphaned");
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003579 volumeUuid = parser.getAttributeValue(null, "volumeUuid");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003580
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003581 systemStr = parser.getAttributeValue(null, "publicFlags");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003582 if (systemStr != null) {
3583 try {
3584 pkgFlags = Integer.parseInt(systemStr);
3585 } catch (NumberFormatException e) {
3586 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003587 systemStr = parser.getAttributeValue(null, "privateFlags");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003588 if (systemStr != null) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003589 try {
3590 pkgPrivateFlags = Integer.parseInt(systemStr);
3591 } catch (NumberFormatException e) {
3592 }
3593 }
3594 } else {
3595 // Pre-M -- both public and private flags were stored in one "flags" field.
3596 systemStr = parser.getAttributeValue(null, "flags");
3597 if (systemStr != null) {
3598 try {
3599 pkgFlags = Integer.parseInt(systemStr);
3600 } catch (NumberFormatException e) {
3601 }
3602 if ((pkgFlags & PRE_M_APP_INFO_FLAG_HIDDEN) != 0) {
3603 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_HIDDEN;
3604 }
3605 if ((pkgFlags & PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE) != 0) {
3606 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE;
3607 }
3608 if ((pkgFlags & PRE_M_APP_INFO_FLAG_FORWARD_LOCK) != 0) {
3609 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK;
3610 }
3611 if ((pkgFlags & PRE_M_APP_INFO_FLAG_PRIVILEGED) != 0) {
3612 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
3613 }
3614 pkgFlags &= ~(PRE_M_APP_INFO_FLAG_HIDDEN
3615 | PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE
3616 | PRE_M_APP_INFO_FLAG_FORWARD_LOCK
3617 | PRE_M_APP_INFO_FLAG_PRIVILEGED);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003618 } else {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003619 // For backward compatibility
3620 systemStr = parser.getAttributeValue(null, "system");
3621 if (systemStr != null) {
3622 pkgFlags |= ("true".equalsIgnoreCase(systemStr)) ? ApplicationInfo.FLAG_SYSTEM
3623 : 0;
3624 } else {
3625 // Old settings that don't specify system... just treat
3626 // them as system, good enough.
3627 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
3628 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003629 }
3630 }
3631 String timeStampStr = parser.getAttributeValue(null, "ft");
3632 if (timeStampStr != null) {
3633 try {
3634 timeStamp = Long.parseLong(timeStampStr, 16);
3635 } catch (NumberFormatException e) {
3636 }
3637 } else {
3638 timeStampStr = parser.getAttributeValue(null, "ts");
3639 if (timeStampStr != null) {
3640 try {
3641 timeStamp = Long.parseLong(timeStampStr);
3642 } catch (NumberFormatException e) {
3643 }
3644 }
3645 }
3646 timeStampStr = parser.getAttributeValue(null, "it");
3647 if (timeStampStr != null) {
3648 try {
3649 firstInstallTime = Long.parseLong(timeStampStr, 16);
3650 } catch (NumberFormatException e) {
3651 }
3652 }
3653 timeStampStr = parser.getAttributeValue(null, "ut");
3654 if (timeStampStr != null) {
3655 try {
3656 lastUpdateTime = Long.parseLong(timeStampStr, 16);
3657 } catch (NumberFormatException e) {
3658 }
3659 }
3660 if (PackageManagerService.DEBUG_SETTINGS)
3661 Log.v(PackageManagerService.TAG, "Reading package: " + name + " userId=" + idStr
3662 + " sharedUserId=" + sharedIdStr);
3663 int userId = idStr != null ? Integer.parseInt(idStr) : 0;
3664 if (resourcePathStr == null) {
3665 resourcePathStr = codePathStr;
3666 }
3667 if (realName != null) {
3668 realName = realName.intern();
3669 }
3670 if (name == null) {
3671 PackageManagerService.reportSettingsProblem(Log.WARN,
3672 "Error in package manager settings: <package> has no name at "
3673 + parser.getPositionDescription());
3674 } else if (codePathStr == null) {
3675 PackageManagerService.reportSettingsProblem(Log.WARN,
3676 "Error in package manager settings: <package> has no codePath at "
3677 + parser.getPositionDescription());
3678 } else if (userId > 0) {
Kenny Root447106f2011-03-23 11:00:15 -07003679 packageSetting = addPackageLPw(name.intern(), realName, new File(codePathStr),
Narayan Kamathff110bd2014-07-04 18:30:45 +01003680 new File(resourcePathStr), legacyNativeLibraryPathStr, primaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003681 secondaryCpuAbiString, cpuAbiOverrideString, userId, versionCode, pkgFlags,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003682 pkgPrivateFlags, parentPackageName, null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003683 if (PackageManagerService.DEBUG_SETTINGS)
3684 Log.i(PackageManagerService.TAG, "Reading package " + name + ": userId="
3685 + userId + " pkg=" + packageSetting);
3686 if (packageSetting == null) {
3687 PackageManagerService.reportSettingsProblem(Log.ERROR, "Failure adding uid "
3688 + userId + " while parsing settings at "
3689 + parser.getPositionDescription());
3690 } else {
3691 packageSetting.setTimeStamp(timeStamp);
3692 packageSetting.firstInstallTime = firstInstallTime;
3693 packageSetting.lastUpdateTime = lastUpdateTime;
3694 }
3695 } else if (sharedIdStr != null) {
3696 userId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
3697 if (userId > 0) {
3698 packageSetting = new PendingPackage(name.intern(), realName, new File(
Narayan Kamathff110bd2014-07-04 18:30:45 +01003699 codePathStr), new File(resourcePathStr), legacyNativeLibraryPathStr,
Narayan Kamath4903f642014-08-11 13:33:45 +01003700 primaryCpuAbiString, secondaryCpuAbiString, cpuAbiOverrideString,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003701 userId, versionCode, pkgFlags, pkgPrivateFlags, parentPackageName,
3702 null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003703 packageSetting.setTimeStamp(timeStamp);
3704 packageSetting.firstInstallTime = firstInstallTime;
3705 packageSetting.lastUpdateTime = lastUpdateTime;
3706 mPendingPackages.add((PendingPackage) packageSetting);
3707 if (PackageManagerService.DEBUG_SETTINGS)
3708 Log.i(PackageManagerService.TAG, "Reading package " + name
3709 + ": sharedUserId=" + userId + " pkg=" + packageSetting);
3710 } else {
3711 PackageManagerService.reportSettingsProblem(Log.WARN,
3712 "Error in package manager settings: package " + name
3713 + " has bad sharedId " + sharedIdStr + " at "
3714 + parser.getPositionDescription());
3715 }
3716 } else {
3717 PackageManagerService.reportSettingsProblem(Log.WARN,
3718 "Error in package manager settings: package " + name + " has bad userId "
3719 + idStr + " at " + parser.getPositionDescription());
3720 }
3721 } catch (NumberFormatException e) {
3722 PackageManagerService.reportSettingsProblem(Log.WARN,
3723 "Error in package manager settings: package " + name + " has bad userId "
3724 + idStr + " at " + parser.getPositionDescription());
3725 }
3726 if (packageSetting != null) {
3727 packageSetting.uidError = "true".equals(uidError);
3728 packageSetting.installerPackageName = installerPackageName;
Sudheer Shanka8c57aea2016-04-20 16:47:50 -07003729 packageSetting.isOrphaned = "true".equals(isOrphaned);
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003730 packageSetting.volumeUuid = volumeUuid;
Narayan Kamathff110bd2014-07-04 18:30:45 +01003731 packageSetting.legacyNativeLibraryPathString = legacyNativeLibraryPathStr;
3732 packageSetting.primaryCpuAbiString = primaryCpuAbiString;
3733 packageSetting.secondaryCpuAbiString = secondaryCpuAbiString;
Amith Yamasani483f3b02012-03-13 16:08:00 -07003734 // Handle legacy string here for single-user mode
3735 final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003736 if (enabledStr != null) {
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003737 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003738 packageSetting.setEnabled(Integer.parseInt(enabledStr), 0 /* userId */, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003739 } catch (NumberFormatException e) {
3740 if (enabledStr.equalsIgnoreCase("true")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003741 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_ENABLED, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003742 } else if (enabledStr.equalsIgnoreCase("false")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003743 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DISABLED, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003744 } else if (enabledStr.equalsIgnoreCase("default")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003745 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003746 } else {
3747 PackageManagerService.reportSettingsProblem(Log.WARN,
3748 "Error in package manager settings: package " + name
3749 + " has bad enabled value: " + idStr + " at "
3750 + parser.getPositionDescription());
3751 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003752 }
3753 } else {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003754 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003755 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003756
Sudheer Shanka29283372016-04-04 20:56:27 -07003757 if (installerPackageName != null) {
3758 mInstallerPackages.add(installerPackageName);
3759 }
3760
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003761 final String installStatusStr = parser.getAttributeValue(null, "installStatus");
3762 if (installStatusStr != null) {
3763 if (installStatusStr.equalsIgnoreCase("false")) {
3764 packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_INCOMPLETE;
3765 } else {
3766 packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_COMPLETE;
3767 }
3768 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08003769
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003770 int outerDepth = parser.getDepth();
3771 int type;
3772 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3773 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3774 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3775 continue;
3776 }
3777
3778 String tagName = parser.getName();
Selim Gurun5c8acb42016-04-07 16:34:02 -07003779 // Legacy
Amith Yamasani483f3b02012-03-13 16:08:00 -07003780 if (tagName.equals(TAG_DISABLED_COMPONENTS)) {
3781 readDisabledComponentsLPw(packageSetting, parser, 0);
3782 } else if (tagName.equals(TAG_ENABLED_COMPONENTS)) {
3783 readEnabledComponentsLPw(packageSetting, parser, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003784 } else if (tagName.equals("sigs")) {
3785 packageSetting.signatures.readXml(parser, mPastSignatures);
Svetoslavc6d1c342015-02-26 14:44:43 -08003786 } else if (tagName.equals(TAG_PERMISSIONS)) {
3787 readInstallPermissionsLPr(parser,
3788 packageSetting.getPermissionsState());
Svetoslavcf959f62015-03-26 20:53:34 -07003789 packageSetting.installPermissionsFixed = true;
dcashman3a0dbfb2014-07-07 13:53:36 -07003790 } else if (tagName.equals("proper-signing-keyset")) {
3791 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
dcashman8c04fac2015-03-23 11:39:42 -07003792 Integer refCt = mKeySetRefs.get(id);
3793 if (refCt != null) {
3794 mKeySetRefs.put(id, refCt + 1);
3795 } else {
3796 mKeySetRefs.put(id, 1);
3797 }
dcashman3a0dbfb2014-07-07 13:53:36 -07003798 packageSetting.keySetData.setProperSigningKeySet(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -08003799 } else if (tagName.equals("signing-keyset")) {
dcashman8c04fac2015-03-23 11:39:42 -07003800 // from v1 of keysetmanagerservice - no longer used
dcashman55b10782014-04-09 14:20:38 -07003801 } else if (tagName.equals("upgrade-keyset")) {
3802 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
3803 packageSetting.keySetData.addUpgradeKeySetById(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -08003804 } else if (tagName.equals("defined-keyset")) {
3805 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
3806 String alias = parser.getAttributeValue(null, "alias");
dcashman8c04fac2015-03-23 11:39:42 -07003807 Integer refCt = mKeySetRefs.get(id);
3808 if (refCt != null) {
3809 mKeySetRefs.put(id, refCt + 1);
3810 } else {
3811 mKeySetRefs.put(id, 1);
3812 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08003813 packageSetting.keySetData.addDefinedKeySet(id, alias);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08003814 } else if (tagName.equals(TAG_DOMAIN_VERIFICATION)) {
3815 readDomainVerificationLPw(parser, packageSetting);
Svet Ganov354cd3c2015-12-17 11:35:04 -08003816 } else if (tagName.equals(TAG_CHILD_PACKAGE)) {
3817 String childPackageName = parser.getAttributeValue(null, ATTR_NAME);
3818 if (packageSetting.childPackageNames == null) {
3819 packageSetting.childPackageNames = new ArrayList<>();
3820 }
3821 packageSetting.childPackageNames.add(childPackageName);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003822 } else {
3823 PackageManagerService.reportSettingsProblem(Log.WARN,
3824 "Unknown element under <package>: " + parser.getName());
3825 XmlUtils.skipCurrentTag(parser);
3826 }
3827 }
3828 } else {
3829 XmlUtils.skipCurrentTag(parser);
3830 }
3831 }
3832
Amith Yamasani483f3b02012-03-13 16:08:00 -07003833 private void readDisabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser,
3834 int userId) throws IOException, XmlPullParserException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003835 int outerDepth = parser.getDepth();
3836 int type;
3837 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3838 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3839 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3840 continue;
3841 }
3842
3843 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003844 if (tagName.equals(TAG_ITEM)) {
3845 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003846 if (name != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003847 packageSetting.addDisabledComponent(name.intern(), userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003848 } else {
3849 PackageManagerService.reportSettingsProblem(Log.WARN,
3850 "Error in package manager settings: <disabled-components> has"
3851 + " no name at " + parser.getPositionDescription());
3852 }
3853 } else {
3854 PackageManagerService.reportSettingsProblem(Log.WARN,
3855 "Unknown element under <disabled-components>: " + parser.getName());
3856 }
3857 XmlUtils.skipCurrentTag(parser);
3858 }
3859 }
3860
Amith Yamasani483f3b02012-03-13 16:08:00 -07003861 private void readEnabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser,
3862 int userId) throws IOException, XmlPullParserException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003863 int outerDepth = parser.getDepth();
3864 int type;
3865 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3866 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3867 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3868 continue;
3869 }
3870
3871 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003872 if (tagName.equals(TAG_ITEM)) {
3873 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003874 if (name != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003875 packageSetting.addEnabledComponent(name.intern(), userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003876 } else {
3877 PackageManagerService.reportSettingsProblem(Log.WARN,
3878 "Error in package manager settings: <enabled-components> has"
3879 + " no name at " + parser.getPositionDescription());
3880 }
3881 } else {
3882 PackageManagerService.reportSettingsProblem(Log.WARN,
3883 "Unknown element under <enabled-components>: " + parser.getName());
3884 }
3885 XmlUtils.skipCurrentTag(parser);
3886 }
3887 }
3888
Amith Yamasani483f3b02012-03-13 16:08:00 -07003889 private void readSharedUserLPw(XmlPullParser parser) throws XmlPullParserException,IOException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003890 String name = null;
3891 String idStr = null;
3892 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003893 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003894 SharedUserSetting su = null;
3895 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003896 name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003897 idStr = parser.getAttributeValue(null, "userId");
3898 int userId = idStr != null ? Integer.parseInt(idStr) : 0;
3899 if ("true".equals(parser.getAttributeValue(null, "system"))) {
3900 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
3901 }
3902 if (name == null) {
3903 PackageManagerService.reportSettingsProblem(Log.WARN,
3904 "Error in package manager settings: <shared-user> has no name at "
3905 + parser.getPositionDescription());
3906 } else if (userId == 0) {
3907 PackageManagerService.reportSettingsProblem(Log.WARN,
3908 "Error in package manager settings: shared-user " + name
3909 + " has bad userId " + idStr + " at "
3910 + parser.getPositionDescription());
3911 } else {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003912 if ((su = addSharedUserLPw(name.intern(), userId, pkgFlags, pkgPrivateFlags))
3913 == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003914 PackageManagerService
3915 .reportSettingsProblem(Log.ERROR, "Occurred while parsing settings at "
3916 + parser.getPositionDescription());
3917 }
3918 }
3919 } catch (NumberFormatException e) {
3920 PackageManagerService.reportSettingsProblem(Log.WARN,
3921 "Error in package manager settings: package " + name + " has bad userId "
3922 + idStr + " at " + parser.getPositionDescription());
3923 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003924
3925 if (su != null) {
3926 int outerDepth = parser.getDepth();
3927 int type;
3928 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3929 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3930 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3931 continue;
3932 }
3933
3934 String tagName = parser.getName();
3935 if (tagName.equals("sigs")) {
3936 su.signatures.readXml(parser, mPastSignatures);
3937 } else if (tagName.equals("perms")) {
Svetoslavc6d1c342015-02-26 14:44:43 -08003938 readInstallPermissionsLPr(parser, su.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003939 } else {
3940 PackageManagerService.reportSettingsProblem(Log.WARN,
3941 "Unknown element under <shared-user>: " + parser.getName());
3942 XmlUtils.skipCurrentTag(parser);
3943 }
3944 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003945 } else {
3946 XmlUtils.skipCurrentTag(parser);
3947 }
3948 }
3949
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003950 void createNewUserLI(@NonNull PackageManagerService service, @NonNull Installer installer,
3951 int userHandle) {
3952 String[] volumeUuids;
3953 String[] names;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003954 int[] appIds;
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003955 String[] seinfos;
Janis Danisevskisf3409742016-01-12 14:46:33 +00003956 int[] targetSdkVersions;
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003957 int packagesCount;
3958 synchronized (mPackages) {
3959 Collection<PackageSetting> packages = mPackages.values();
3960 packagesCount = packages.size();
3961 volumeUuids = new String[packagesCount];
3962 names = new String[packagesCount];
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003963 appIds = new int[packagesCount];
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003964 seinfos = new String[packagesCount];
Janis Danisevskisf3409742016-01-12 14:46:33 +00003965 targetSdkVersions = new int[packagesCount];
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003966 Iterator<PackageSetting> packagesIterator = packages.iterator();
3967 for (int i = 0; i < packagesCount; i++) {
3968 PackageSetting ps = packagesIterator.next();
3969 if (ps.pkg == null || ps.pkg.applicationInfo == null) {
3970 continue;
3971 }
3972 // Only system apps are initially installed.
3973 ps.setInstalled(ps.isSystem(), userHandle);
3974 // Need to create a data directory for all apps under this user. Accumulate all
3975 // required args and call the installer after mPackages lock has been released
3976 volumeUuids[i] = ps.volumeUuid;
3977 names[i] = ps.name;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003978 appIds[i] = ps.appId;
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003979 seinfos[i] = ps.pkg.applicationInfo.seinfo;
Janis Danisevskisf3409742016-01-12 14:46:33 +00003980 targetSdkVersions[i] = ps.pkg.applicationInfo.targetSdkVersion;
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003981 }
3982 }
3983 for (int i = 0; i < packagesCount; i++) {
3984 if (names[i] == null) {
Amith Yamasani41cd5772014-07-10 15:26:00 -07003985 continue;
3986 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003987 // TODO: triage flags!
Jeff Sharkey47f71082016-02-01 17:03:54 -07003988 final int flags = StorageManager.FLAG_STORAGE_CE | StorageManager.FLAG_STORAGE_DE;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003989 try {
3990 installer.createAppData(volumeUuids[i], names[i], userHandle, flags, appIds[i],
Janis Danisevskisf3409742016-01-12 14:46:33 +00003991 seinfos[i], targetSdkVersions[i]);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003992 } catch (InstallerException e) {
3993 Slog.w(TAG, "Failed to prepare app data", e);
3994 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003995 }
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003996 synchronized (mPackages) {
3997 applyDefaultPreferredAppsLPw(service, userHandle);
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003998 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003999 }
4000
Alexandra Gherghina539a7ef2014-07-07 12:27:54 +01004001 void removeUserLPw(int userId) {
Dianne Hackborn63092712012-10-07 14:45:35 -07004002 Set<Entry<String, PackageSetting>> entries = mPackages.entrySet();
4003 for (Entry<String, PackageSetting> entry : entries) {
4004 entry.getValue().removeUser(userId);
4005 }
4006 mPreferredActivities.remove(userId);
Amith Yamasani13593602012-03-22 16:16:17 -07004007 File file = getUserPackagesStateFile(userId);
4008 file.delete();
4009 file = getUserPackagesStateBackupFile(userId);
4010 file.delete();
Nicolas Prevote7024042014-07-08 15:47:17 +01004011 removeCrossProfileIntentFiltersLPw(userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08004012
Fyodor Kupolov69542592016-05-23 14:22:38 -07004013 mRuntimePermissionsPersistence.onUserRemovedLPw(userId);
Jeff Sharkey91edde22015-05-20 12:04:42 -07004014
4015 writePackageListLPr();
Nicolas Prevota0f48852014-05-27 11:21:11 +01004016 }
4017
Nicolas Prevote7024042014-07-08 15:47:17 +01004018 void removeCrossProfileIntentFiltersLPw(int userId) {
4019 synchronized (mCrossProfileIntentResolvers) {
4020 // userId is the source user
4021 if (mCrossProfileIntentResolvers.get(userId) != null) {
4022 mCrossProfileIntentResolvers.remove(userId);
4023 writePackageRestrictionsLPr(userId);
Nicolas Prevota0f48852014-05-27 11:21:11 +01004024 }
Nicolas Prevote7024042014-07-08 15:47:17 +01004025 // userId is the target user
4026 int count = mCrossProfileIntentResolvers.size();
4027 for (int i = 0; i < count; i++) {
4028 int sourceUserId = mCrossProfileIntentResolvers.keyAt(i);
4029 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(sourceUserId);
4030 boolean needsWriting = false;
Jeff Sharkey9f837a92014-10-24 12:07:24 -07004031 ArraySet<CrossProfileIntentFilter> cpifs =
4032 new ArraySet<CrossProfileIntentFilter>(cpir.filterSet());
Nicolas Prevote7024042014-07-08 15:47:17 +01004033 for (CrossProfileIntentFilter cpif : cpifs) {
4034 if (cpif.getTargetUserId() == userId) {
4035 needsWriting = true;
4036 cpir.removeFilter(cpif);
4037 }
4038 }
4039 if (needsWriting) {
4040 writePackageRestrictionsLPr(sourceUserId);
4041 }
Nicolas Prevota0f48852014-05-27 11:21:11 +01004042 }
4043 }
Amith Yamasani13593602012-03-22 16:16:17 -07004044 }
4045
Geremy Condra12c18382013-03-06 16:49:06 -08004046 // This should be called (at least) whenever an application is removed
4047 private void setFirstAvailableUid(int uid) {
4048 if (uid > mFirstAvailableUid) {
4049 mFirstAvailableUid = uid;
4050 }
4051 }
4052
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004053 // Returns -1 if we could not find an available UserId to assign
Kenny Root447106f2011-03-23 11:00:15 -07004054 private int newUserIdLPw(Object obj) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004055 // Let's be stupidly inefficient for now...
4056 final int N = mUserIds.size();
Geremy Condra12c18382013-03-06 16:49:06 -08004057 for (int i = mFirstAvailableUid; i < N; i++) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004058 if (mUserIds.get(i) == null) {
4059 mUserIds.set(i, obj);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08004060 return Process.FIRST_APPLICATION_UID + i;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004061 }
4062 }
4063
4064 // None left?
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08004065 if (N > (Process.LAST_APPLICATION_UID-Process.FIRST_APPLICATION_UID)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004066 return -1;
4067 }
4068
4069 mUserIds.add(obj);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08004070 return Process.FIRST_APPLICATION_UID + N;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004071 }
4072
Kenny Root0aaa0d92011-09-12 16:42:55 -07004073 public VerifierDeviceIdentity getVerifierDeviceIdentityLPw() {
4074 if (mVerifierDeviceIdentity == null) {
4075 mVerifierDeviceIdentity = VerifierDeviceIdentity.generate();
4076
4077 writeLPr();
4078 }
4079
4080 return mVerifierDeviceIdentity;
4081 }
4082
Svet Ganov354cd3c2015-12-17 11:35:04 -08004083 public boolean hasOtherDisabledSystemPkgWithChildLPr(String parentPackageName,
4084 String childPackageName) {
4085 final int packageCount = mDisabledSysPackages.size();
4086 for (int i = 0; i < packageCount; i++) {
4087 PackageSetting disabledPs = mDisabledSysPackages.valueAt(i);
4088 if (disabledPs.childPackageNames == null || disabledPs.childPackageNames.isEmpty()) {
4089 continue;
4090 }
4091 if (disabledPs.name.equals(parentPackageName)) {
4092 continue;
4093 }
4094 final int childCount = disabledPs.childPackageNames.size();
4095 for (int j = 0; j < childCount; j++) {
4096 String currChildPackageName = disabledPs.childPackageNames.get(j);
4097 if (currChildPackageName.equals(childPackageName)) {
4098 return true;
4099 }
4100 }
4101 }
4102 return false;
4103 }
4104
Kenny Root447106f2011-03-23 11:00:15 -07004105 public PackageSetting getDisabledSystemPkgLPr(String name) {
4106 PackageSetting ps = mDisabledSysPackages.get(name);
4107 return ps;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004108 }
4109
Jeff Sharkey9f837a92014-10-24 12:07:24 -07004110 private String compToString(ArraySet<String> cmp) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07004111 return cmp != null ? Arrays.toString(cmp.toArray()) : "[]";
4112 }
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004113
Jeff Sharkey5217cac2015-12-20 15:34:01 -07004114 boolean isEnabledAndMatchLPr(ComponentInfo componentInfo, int flags, int userId) {
Jeff Sharkey2bd31db2016-01-09 16:58:14 -07004115 final PackageSetting ps = mPackages.get(componentInfo.packageName);
4116 if (ps == null) return false;
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004117
Jeff Sharkey2bd31db2016-01-09 16:58:14 -07004118 final PackageUserState userState = ps.readUserState(userId);
4119 return userState.isMatch(componentInfo, flags);
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004120 }
4121
Kenny Root447106f2011-03-23 11:00:15 -07004122 String getInstallerPackageNameLPr(String packageName) {
4123 final PackageSetting pkg = mPackages.get(packageName);
4124 if (pkg == null) {
4125 throw new IllegalArgumentException("Unknown package: " + packageName);
4126 }
4127 return pkg.installerPackageName;
4128 }
4129
Sudheer Shanka8c57aea2016-04-20 16:47:50 -07004130 boolean isOrphaned(String packageName) {
4131 final PackageSetting pkg = mPackages.get(packageName);
4132 if (pkg == null) {
4133 throw new IllegalArgumentException("Unknown package: " + packageName);
4134 }
4135 return pkg.isOrphaned;
4136 }
4137
Amith Yamasani483f3b02012-03-13 16:08:00 -07004138 int getApplicationEnabledSettingLPr(String packageName, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -07004139 final PackageSetting pkg = mPackages.get(packageName);
4140 if (pkg == null) {
4141 throw new IllegalArgumentException("Unknown package: " + packageName);
4142 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004143 return pkg.getEnabled(userId);
Kenny Root447106f2011-03-23 11:00:15 -07004144 }
4145
Amith Yamasani483f3b02012-03-13 16:08:00 -07004146 int getComponentEnabledSettingLPr(ComponentName componentName, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -07004147 final String packageName = componentName.getPackageName();
4148 final PackageSetting pkg = mPackages.get(packageName);
4149 if (pkg == null) {
4150 throw new IllegalArgumentException("Unknown component: " + componentName);
4151 }
4152 final String classNameStr = componentName.getClassName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07004153 return pkg.getCurrentEnabledStateLPr(classNameStr, userId);
Kenny Root447106f2011-03-23 11:00:15 -07004154 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004155
Svet Ganovd63cde72016-09-08 20:15:55 -07004156 boolean wasPackageEverLaunchedLPr(String packageName, int userId) {
4157 final PackageSetting pkgSetting = mPackages.get(packageName);
4158 if (pkgSetting == null) {
4159 throw new IllegalArgumentException("Unknown package: " + packageName);
4160 }
4161 return !pkgSetting.getNotLaunched(userId);
4162 }
4163
Christopher Tate5cf55782016-04-25 17:08:56 -07004164 boolean setPackageStoppedStateLPw(PackageManagerService pm, String packageName,
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -07004165 boolean stopped, boolean allowedByPermission, int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004166 int appId = UserHandle.getAppId(uid);
Kenny Root447106f2011-03-23 11:00:15 -07004167 final PackageSetting pkgSetting = mPackages.get(packageName);
4168 if (pkgSetting == null) {
4169 throw new IllegalArgumentException("Unknown package: " + packageName);
4170 }
Amith Yamasani13593602012-03-22 16:16:17 -07004171 if (!allowedByPermission && (appId != pkgSetting.appId)) {
Kenny Root447106f2011-03-23 11:00:15 -07004172 throw new SecurityException(
4173 "Permission Denial: attempt to change stopped state from pid="
4174 + Binder.getCallingPid()
Amith Yamasani13593602012-03-22 16:16:17 -07004175 + ", uid=" + uid + ", package uid=" + pkgSetting.appId);
Kenny Root447106f2011-03-23 11:00:15 -07004176 }
4177 if (DEBUG_STOPPED) {
4178 if (stopped) {
4179 RuntimeException e = new RuntimeException("here");
4180 e.fillInStackTrace();
4181 Slog.i(TAG, "Stopping package " + packageName, e);
4182 }
4183 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004184 if (pkgSetting.getStopped(userId) != stopped) {
4185 pkgSetting.setStopped(stopped, userId);
4186 // pkgSetting.pkg.mSetStopped = stopped;
4187 if (pkgSetting.getNotLaunched(userId)) {
Kenny Root447106f2011-03-23 11:00:15 -07004188 if (pkgSetting.installerPackageName != null) {
Christopher Tate5cf55782016-04-25 17:08:56 -07004189 pm.notifyFirstLaunch(pkgSetting.name, pkgSetting.installerPackageName, userId);
Kenny Root447106f2011-03-23 11:00:15 -07004190 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004191 pkgSetting.setNotLaunched(false, userId);
Kenny Root447106f2011-03-23 11:00:15 -07004192 }
4193 return true;
4194 }
4195 return false;
4196 }
4197
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08004198 List<UserInfo> getAllUsers() {
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07004199 long id = Binder.clearCallingIdentity();
Amith Yamasani483f3b02012-03-13 16:08:00 -07004200 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07004201 return UserManagerService.getInstance().getUsers(false);
Amith Yamasani483f3b02012-03-13 16:08:00 -07004202 } catch (NullPointerException npe) {
4203 // packagemanager not yet initialized
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07004204 } finally {
4205 Binder.restoreCallingIdentity(id);
Amith Yamasani483f3b02012-03-13 16:08:00 -07004206 }
4207 return null;
4208 }
4209
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07004210 /**
4211 * Return all {@link PackageSetting} that are actively installed on the
4212 * given {@link VolumeInfo#fsUuid}.
4213 */
4214 List<PackageSetting> getVolumePackagesLPr(String volumeUuid) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07004215 ArrayList<PackageSetting> res = new ArrayList<>();
4216 for (int i = 0; i < mPackages.size(); i++) {
4217 final PackageSetting setting = mPackages.valueAt(i);
4218 if (Objects.equals(volumeUuid, setting.volumeUuid)) {
4219 res.add(setting);
4220 }
4221 }
4222 return res;
4223 }
4224
Svetoslavc6d1c342015-02-26 14:44:43 -08004225 static void printFlags(PrintWriter pw, int val, Object[] spec) {
Joe Onorato20963df2012-01-04 18:13:24 -08004226 pw.print("[ ");
4227 for (int i=0; i<spec.length; i+=2) {
4228 int mask = (Integer)spec[i];
4229 if ((val & mask) != 0) {
4230 pw.print(spec[i+1]);
4231 pw.print(" ");
4232 }
4233 }
4234 pw.print("]");
4235 }
4236
4237 static final Object[] FLAG_DUMP_SPEC = new Object[] {
4238 ApplicationInfo.FLAG_SYSTEM, "SYSTEM",
4239 ApplicationInfo.FLAG_DEBUGGABLE, "DEBUGGABLE",
4240 ApplicationInfo.FLAG_HAS_CODE, "HAS_CODE",
4241 ApplicationInfo.FLAG_PERSISTENT, "PERSISTENT",
4242 ApplicationInfo.FLAG_FACTORY_TEST, "FACTORY_TEST",
4243 ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING, "ALLOW_TASK_REPARENTING",
4244 ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA, "ALLOW_CLEAR_USER_DATA",
4245 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP, "UPDATED_SYSTEM_APP",
4246 ApplicationInfo.FLAG_TEST_ONLY, "TEST_ONLY",
4247 ApplicationInfo.FLAG_VM_SAFE_MODE, "VM_SAFE_MODE",
4248 ApplicationInfo.FLAG_ALLOW_BACKUP, "ALLOW_BACKUP",
4249 ApplicationInfo.FLAG_KILL_AFTER_RESTORE, "KILL_AFTER_RESTORE",
4250 ApplicationInfo.FLAG_RESTORE_ANY_VERSION, "RESTORE_ANY_VERSION",
4251 ApplicationInfo.FLAG_EXTERNAL_STORAGE, "EXTERNAL_STORAGE",
4252 ApplicationInfo.FLAG_LARGE_HEAP, "LARGE_HEAP",
Alex Klyubinb9f8a522015-02-03 11:12:59 -08004253 };
4254
4255 static final Object[] PRIVATE_FLAG_DUMP_SPEC = new Object[] {
Jeff Sharkeyeffcd932016-03-04 12:52:36 -07004256 ApplicationInfo.PRIVATE_FLAG_HIDDEN, "HIDDEN",
Alex Klyubinb9f8a522015-02-03 11:12:59 -08004257 ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE, "CANT_SAVE_STATE",
Jeff Sharkeyeffcd932016-03-04 12:52:36 -07004258 ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK, "FORWARD_LOCK",
4259 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED, "PRIVILEGED",
4260 ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS, "HAS_DOMAIN_URLS",
Jeff Sharkey8a372a02016-03-16 16:25:45 -06004261 ApplicationInfo.PRIVATE_FLAG_DEFAULT_TO_DEVICE_PROTECTED_STORAGE, "DEFAULT_TO_DEVICE_PROTECTED_STORAGE",
4262 ApplicationInfo.PRIVATE_FLAG_DIRECT_BOOT_AWARE, "DIRECT_BOOT_AWARE",
Jeff Sharkeyeffcd932016-03-04 12:52:36 -07004263 ApplicationInfo.PRIVATE_FLAG_AUTOPLAY, "AUTOPLAY",
Jeff Sharkey8a372a02016-03-16 16:25:45 -06004264 ApplicationInfo.PRIVATE_FLAG_PARTIALLY_DIRECT_BOOT_AWARE, "PARTIALLY_DIRECT_BOOT_AWARE",
Jeff Sharkeyeffcd932016-03-04 12:52:36 -07004265 ApplicationInfo.PRIVATE_FLAG_EPHEMERAL, "EPHEMERAL",
4266 ApplicationInfo.PRIVATE_FLAG_REQUIRED_FOR_SYSTEM_USER, "REQUIRED_FOR_SYSTEM_USER",
4267 ApplicationInfo.PRIVATE_FLAG_RESIZEABLE_ACTIVITIES, "RESIZEABLE_ACTIVITIES",
4268 ApplicationInfo.PRIVATE_FLAG_BACKUP_IN_FOREGROUND, "BACKUP_IN_FOREGROUND",
Joe Onorato20963df2012-01-04 18:13:24 -08004269 };
4270
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07004271 void dumpVersionLPr(IndentingPrintWriter pw) {
4272 pw.increaseIndent();
4273 for (int i= 0; i < mVersion.size(); i++) {
4274 final String volumeUuid = mVersion.keyAt(i);
4275 final VersionInfo ver = mVersion.valueAt(i);
4276 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) {
4277 pw.println("Internal:");
4278 } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) {
4279 pw.println("External:");
4280 } else {
4281 pw.println("UUID " + volumeUuid + ":");
4282 }
4283 pw.increaseIndent();
4284 pw.printPair("sdkVersion", ver.sdkVersion);
4285 pw.printPair("databaseVersion", ver.databaseVersion);
4286 pw.println();
4287 pw.printPair("fingerprint", ver.fingerprint);
4288 pw.println();
4289 pw.decreaseIndent();
4290 }
4291 pw.decreaseIndent();
4292 }
4293
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004294 void dumpPackageLPr(PrintWriter pw, String prefix, String checkinTag,
4295 ArraySet<String> permissionNames, PackageSetting ps, SimpleDateFormat sdf,
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004296 Date date, List<UserInfo> users, boolean dumpAll) {
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004297 if (checkinTag != null) {
4298 pw.print(checkinTag);
4299 pw.print(",");
4300 pw.print(ps.realName != null ? ps.realName : ps.name);
4301 pw.print(",");
4302 pw.print(ps.appId);
4303 pw.print(",");
4304 pw.print(ps.versionCode);
4305 pw.print(",");
4306 pw.print(ps.firstInstallTime);
4307 pw.print(",");
4308 pw.print(ps.lastUpdateTime);
4309 pw.print(",");
4310 pw.print(ps.installerPackageName != null ? ps.installerPackageName : "?");
4311 pw.println();
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004312 if (ps.pkg != null) {
4313 pw.print(checkinTag); pw.print("-"); pw.print("splt,");
4314 pw.print("base,");
4315 pw.println(ps.pkg.baseRevisionCode);
4316 if (ps.pkg.splitNames != null) {
4317 for (int i = 0; i < ps.pkg.splitNames.length; i++) {
4318 pw.print(checkinTag); pw.print("-"); pw.print("splt,");
4319 pw.print(ps.pkg.splitNames[i]); pw.print(",");
4320 pw.println(ps.pkg.splitRevisionCodes[i]);
4321 }
4322 }
4323 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004324 for (UserInfo user : users) {
4325 pw.print(checkinTag);
4326 pw.print("-");
4327 pw.print("usr");
4328 pw.print(",");
4329 pw.print(user.id);
4330 pw.print(",");
4331 pw.print(ps.getInstalled(user.id) ? "I" : "i");
Amith Yamasanie5bcff62014-07-19 15:44:09 -07004332 pw.print(ps.getHidden(user.id) ? "B" : "b");
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00004333 pw.print(ps.getSuspended(user.id) ? "SU" : "su");
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004334 pw.print(ps.getStopped(user.id) ? "S" : "s");
4335 pw.print(ps.getNotLaunched(user.id) ? "l" : "L");
4336 pw.print(",");
4337 pw.print(ps.getEnabled(user.id));
4338 String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id);
4339 pw.print(",");
4340 pw.print(lastDisabledAppCaller != null ? lastDisabledAppCaller : "?");
4341 pw.println();
4342 }
4343 return;
4344 }
4345
Dianne Hackbornc895be72013-03-11 17:48:43 -07004346 pw.print(prefix); pw.print("Package [");
4347 pw.print(ps.realName != null ? ps.realName : ps.name);
4348 pw.print("] (");
4349 pw.print(Integer.toHexString(System.identityHashCode(ps)));
4350 pw.println("):");
4351
4352 if (ps.realName != null) {
4353 pw.print(prefix); pw.print(" compat name=");
4354 pw.println(ps.name);
4355 }
4356
Svetoslavc6d1c342015-02-26 14:44:43 -08004357 pw.print(prefix); pw.print(" userId="); pw.println(ps.appId);
4358
Dianne Hackbornc895be72013-03-11 17:48:43 -07004359 if (ps.sharedUser != null) {
4360 pw.print(prefix); pw.print(" sharedUser="); pw.println(ps.sharedUser);
4361 }
4362 pw.print(prefix); pw.print(" pkg="); pw.println(ps.pkg);
4363 pw.print(prefix); pw.print(" codePath="); pw.println(ps.codePathString);
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004364 if (permissionNames == null) {
4365 pw.print(prefix); pw.print(" resourcePath="); pw.println(ps.resourcePathString);
4366 pw.print(prefix); pw.print(" legacyNativeLibraryDir=");
4367 pw.println(ps.legacyNativeLibraryPathString);
4368 pw.print(prefix); pw.print(" primaryCpuAbi="); pw.println(ps.primaryCpuAbiString);
4369 pw.print(prefix); pw.print(" secondaryCpuAbi="); pw.println(ps.secondaryCpuAbiString);
4370 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004371 pw.print(prefix); pw.print(" versionCode="); pw.print(ps.versionCode);
4372 if (ps.pkg != null) {
Todd Kennedy89d60182016-03-11 11:18:32 -08004373 pw.print(" minSdk="); pw.print(ps.pkg.applicationInfo.minSdkVersion);
Dianne Hackbornc895be72013-03-11 17:48:43 -07004374 pw.print(" targetSdk="); pw.print(ps.pkg.applicationInfo.targetSdkVersion);
4375 }
4376 pw.println();
4377 if (ps.pkg != null) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004378 if (ps.pkg.parentPackage != null) {
4379 PackageParser.Package parentPkg = ps.pkg.parentPackage;
4380 PackageSetting pps = mPackages.get(parentPkg.packageName);
4381 if (pps == null || !pps.codePathString.equals(parentPkg.codePath)) {
4382 pps = mDisabledSysPackages.get(parentPkg.packageName);
4383 }
4384 if (pps != null) {
4385 pw.print(prefix); pw.print(" parentPackage=");
4386 pw.println(pps.realName != null ? pps.realName : pps.name);
4387 }
4388 } else if (ps.pkg.childPackages != null) {
4389 pw.print(prefix); pw.print(" childPackages=[");
4390 final int childCount = ps.pkg.childPackages.size();
4391 for (int i = 0; i < childCount; i++) {
4392 PackageParser.Package childPkg = ps.pkg.childPackages.get(i);
4393 PackageSetting cps = mPackages.get(childPkg.packageName);
4394 if (cps == null || !cps.codePathString.equals(childPkg.codePath)) {
4395 cps = mDisabledSysPackages.get(childPkg.packageName);
4396 }
4397 if (cps != null) {
4398 if (i > 0) {
4399 pw.print(", ");
4400 }
4401 pw.print(cps.realName != null ? cps.realName : cps.name);
4402 }
4403 }
4404 pw.println("]");
4405 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004406 pw.print(prefix); pw.print(" versionName="); pw.println(ps.pkg.mVersionName);
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004407 pw.print(prefix); pw.print(" splits="); dumpSplitNames(pw, ps.pkg); pw.println();
Todd Kennedy66c55532016-02-26 16:22:11 -08004408 final int apkSigningVersion = PackageParser.getApkSigningVersion(ps.pkg);
4409 if (apkSigningVersion != PackageParser.APK_SIGNING_UNKNOWN) {
4410 pw.print(prefix); pw.print(" apkSigningVersion="); pw.println(apkSigningVersion);
4411 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004412 pw.print(prefix); pw.print(" applicationInfo=");
4413 pw.println(ps.pkg.applicationInfo.toString());
4414 pw.print(prefix); pw.print(" flags="); printFlags(pw, ps.pkg.applicationInfo.flags,
4415 FLAG_DUMP_SPEC); pw.println();
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004416 if (ps.pkg.applicationInfo.privateFlags != 0) {
4417 pw.print(prefix); pw.print(" privateFlags="); printFlags(pw,
4418 ps.pkg.applicationInfo.privateFlags, PRIVATE_FLAG_DUMP_SPEC); pw.println();
4419 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004420 pw.print(prefix); pw.print(" dataDir="); pw.println(ps.pkg.applicationInfo.dataDir);
Dianne Hackbornc895be72013-03-11 17:48:43 -07004421 pw.print(prefix); pw.print(" supportsScreens=[");
4422 boolean first = true;
4423 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS) != 0) {
4424 if (!first)
4425 pw.print(", ");
4426 first = false;
4427 pw.print("small");
4428 }
4429 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS) != 0) {
4430 if (!first)
4431 pw.print(", ");
4432 first = false;
4433 pw.print("medium");
4434 }
4435 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0) {
4436 if (!first)
4437 pw.print(", ");
4438 first = false;
4439 pw.print("large");
4440 }
4441 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS) != 0) {
4442 if (!first)
4443 pw.print(", ");
4444 first = false;
4445 pw.print("xlarge");
4446 }
4447 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS) != 0) {
4448 if (!first)
4449 pw.print(", ");
4450 first = false;
4451 pw.print("resizeable");
4452 }
4453 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES) != 0) {
4454 if (!first)
4455 pw.print(", ");
4456 first = false;
4457 pw.print("anyDensity");
4458 }
4459 pw.println("]");
4460 if (ps.pkg.libraryNames != null && ps.pkg.libraryNames.size() > 0) {
4461 pw.print(prefix); pw.println(" libraries:");
4462 for (int i=0; i<ps.pkg.libraryNames.size(); i++) {
4463 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.libraryNames.get(i));
4464 }
4465 }
4466 if (ps.pkg.usesLibraries != null && ps.pkg.usesLibraries.size() > 0) {
4467 pw.print(prefix); pw.println(" usesLibraries:");
4468 for (int i=0; i<ps.pkg.usesLibraries.size(); i++) {
4469 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.usesLibraries.get(i));
4470 }
4471 }
4472 if (ps.pkg.usesOptionalLibraries != null
4473 && ps.pkg.usesOptionalLibraries.size() > 0) {
4474 pw.print(prefix); pw.println(" usesOptionalLibraries:");
4475 for (int i=0; i<ps.pkg.usesOptionalLibraries.size(); i++) {
4476 pw.print(prefix); pw.print(" ");
4477 pw.println(ps.pkg.usesOptionalLibraries.get(i));
4478 }
4479 }
4480 if (ps.pkg.usesLibraryFiles != null
4481 && ps.pkg.usesLibraryFiles.length > 0) {
4482 pw.print(prefix); pw.println(" usesLibraryFiles:");
4483 for (int i=0; i<ps.pkg.usesLibraryFiles.length; i++) {
4484 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.usesLibraryFiles[i]);
4485 }
4486 }
4487 }
4488 pw.print(prefix); pw.print(" timeStamp=");
4489 date.setTime(ps.timeStamp);
4490 pw.println(sdf.format(date));
4491 pw.print(prefix); pw.print(" firstInstallTime=");
4492 date.setTime(ps.firstInstallTime);
4493 pw.println(sdf.format(date));
4494 pw.print(prefix); pw.print(" lastUpdateTime=");
4495 date.setTime(ps.lastUpdateTime);
4496 pw.println(sdf.format(date));
4497 if (ps.installerPackageName != null) {
4498 pw.print(prefix); pw.print(" installerPackageName=");
4499 pw.println(ps.installerPackageName);
4500 }
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07004501 if (ps.volumeUuid != null) {
4502 pw.print(prefix); pw.print(" volumeUuid=");
4503 pw.println(ps.volumeUuid);
4504 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004505 pw.print(prefix); pw.print(" signatures="); pw.println(ps.signatures);
Svetoslavcf959f62015-03-26 20:53:34 -07004506 pw.print(prefix); pw.print(" installPermissionsFixed=");
4507 pw.print(ps.installPermissionsFixed);
Dianne Hackbornc895be72013-03-11 17:48:43 -07004508 pw.print(" installStatus="); pw.println(ps.installStatus);
4509 pw.print(prefix); pw.print(" pkgFlags="); printFlags(pw, ps.pkgFlags, FLAG_DUMP_SPEC);
4510 pw.println();
Svetoslavc6d1c342015-02-26 14:44:43 -08004511
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004512 if (ps.pkg != null && ps.pkg.permissions != null && ps.pkg.permissions.size() > 0) {
4513 final ArrayList<PackageParser.Permission> perms = ps.pkg.permissions;
4514 pw.print(prefix); pw.println(" declared permissions:");
4515 for (int i=0; i<perms.size(); i++) {
4516 PackageParser.Permission perm = perms.get(i);
4517 if (permissionNames != null
4518 && !permissionNames.contains(perm.info.name)) {
4519 continue;
4520 }
4521 pw.print(prefix); pw.print(" "); pw.print(perm.info.name);
4522 pw.print(": prot=");
4523 pw.print(PermissionInfo.protectionToString(perm.info.protectionLevel));
4524 if ((perm.info.flags&PermissionInfo.FLAG_COSTS_MONEY) != 0) {
4525 pw.print(", COSTS_MONEY");
4526 }
Svet Ganov2a1376d2016-02-22 17:20:35 -08004527 if ((perm.info.flags&PermissionInfo.FLAG_REMOVED) != 0) {
Svet Ganov52153f42015-08-11 08:59:12 -07004528 pw.print(", HIDDEN");
Svet Ganov3e0be742015-08-07 23:06:00 -07004529 }
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004530 if ((perm.info.flags&PermissionInfo.FLAG_INSTALLED) != 0) {
4531 pw.print(", INSTALLED");
4532 }
4533 pw.println();
4534 }
4535 }
4536
Dianne Hackborn9cfba352016-03-24 17:31:28 -07004537 if ((permissionNames != null || dumpAll) && ps.pkg != null
4538 && ps.pkg.requestedPermissions != null
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004539 && ps.pkg.requestedPermissions.size() > 0) {
4540 final ArrayList<String> perms = ps.pkg.requestedPermissions;
4541 pw.print(prefix); pw.println(" requested permissions:");
4542 for (int i=0; i<perms.size(); i++) {
4543 String perm = perms.get(i);
4544 if (permissionNames != null
4545 && !permissionNames.contains(perm)) {
4546 continue;
4547 }
4548 pw.print(prefix); pw.print(" "); pw.println(perm);
4549 }
4550 }
4551
4552 if (ps.sharedUser == null || permissionNames != null || dumpAll) {
Svetoslavc6d1c342015-02-26 14:44:43 -08004553 PermissionsState permissionsState = ps.getPermissionsState();
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004554 dumpInstallPermissionsLPr(pw, prefix + " ", permissionNames, permissionsState);
Svetoslavc6d1c342015-02-26 14:44:43 -08004555 }
4556
Dianne Hackbornc895be72013-03-11 17:48:43 -07004557 for (UserInfo user : users) {
4558 pw.print(prefix); pw.print(" User "); pw.print(user.id); pw.print(": ");
Jeff Sharkey42884192016-04-09 16:12:01 -06004559 pw.print("ceDataInode=");
4560 pw.print(ps.getCeDataInode(user.id));
Dianne Hackbornc895be72013-03-11 17:48:43 -07004561 pw.print(" installed=");
4562 pw.print(ps.getInstalled(user.id));
Amith Yamasanie5bcff62014-07-19 15:44:09 -07004563 pw.print(" hidden=");
4564 pw.print(ps.getHidden(user.id));
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00004565 pw.print(" suspended=");
4566 pw.print(ps.getSuspended(user.id));
Dianne Hackbornc895be72013-03-11 17:48:43 -07004567 pw.print(" stopped=");
4568 pw.print(ps.getStopped(user.id));
4569 pw.print(" notLaunched=");
4570 pw.print(ps.getNotLaunched(user.id));
4571 pw.print(" enabled=");
4572 pw.println(ps.getEnabled(user.id));
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07004573 String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id);
4574 if (lastDisabledAppCaller != null) {
4575 pw.print(prefix); pw.print(" lastDisabledCaller: ");
4576 pw.println(lastDisabledAppCaller);
4577 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004578
4579 if (ps.sharedUser == null) {
4580 PermissionsState permissionsState = ps.getPermissionsState();
4581 dumpGidsLPr(pw, prefix + " ", permissionsState.computeGids(user.id));
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004582 dumpRuntimePermissionsLPr(pw, prefix + " ", permissionNames, permissionsState
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004583 .getRuntimePermissionStates(user.id), dumpAll);
Svetoslavc6d1c342015-02-26 14:44:43 -08004584 }
4585
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004586 if (permissionNames == null) {
4587 ArraySet<String> cmp = ps.getDisabledComponents(user.id);
4588 if (cmp != null && cmp.size() > 0) {
4589 pw.print(prefix); pw.println(" disabledComponents:");
4590 for (String s : cmp) {
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004591 pw.print(prefix); pw.print(" "); pw.println(s);
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004592 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004593 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004594 cmp = ps.getEnabledComponents(user.id);
4595 if (cmp != null && cmp.size() > 0) {
4596 pw.print(prefix); pw.println(" enabledComponents:");
4597 for (String s : cmp) {
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004598 pw.print(prefix); pw.print(" "); pw.println(s);
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004599 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004600 }
4601 }
4602 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004603 }
4604
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004605 void dumpPackagesLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames,
4606 DumpState dumpState, boolean checkin) {
Kenny Root447106f2011-03-23 11:00:15 -07004607 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
4608 final Date date = new Date();
4609 boolean printedSomething = false;
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07004610 List<UserInfo> users = getAllUsers();
Kenny Root447106f2011-03-23 11:00:15 -07004611 for (final PackageSetting ps : mPackages.values()) {
4612 if (packageName != null && !packageName.equals(ps.realName)
4613 && !packageName.equals(ps.name)) {
4614 continue;
4615 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004616 if (permissionNames != null
4617 && !ps.getPermissionsState().hasRequestedPermission(permissionNames)) {
4618 continue;
4619 }
Kenny Root447106f2011-03-23 11:00:15 -07004620
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004621 if (!checkin && packageName != null) {
Kenny Root447106f2011-03-23 11:00:15 -07004622 dumpState.setSharedUser(ps.sharedUser);
4623 }
4624
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004625 if (!checkin && !printedSomething) {
Kenny Root447106f2011-03-23 11:00:15 -07004626 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004627 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07004628 pw.println("Packages:");
4629 printedSomething = true;
4630 }
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004631 dumpPackageLPr(pw, " ", checkin ? "pkg" : null, permissionNames, ps, sdf, date, users,
4632 packageName != null);
Kenny Root447106f2011-03-23 11:00:15 -07004633 }
4634
4635 printedSomething = false;
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004636 if (mRenamedPackages.size() > 0 && permissionNames == null) {
Andy McFadden2f362292012-01-20 14:43:38 -08004637 for (final Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
Kenny Root447106f2011-03-23 11:00:15 -07004638 if (packageName != null && !packageName.equals(e.getKey())
4639 && !packageName.equals(e.getValue())) {
4640 continue;
4641 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004642 if (!checkin) {
4643 if (!printedSomething) {
4644 if (dumpState.onTitlePrinted())
4645 pw.println();
4646 pw.println("Renamed packages:");
4647 printedSomething = true;
4648 }
4649 pw.print(" ");
4650 } else {
4651 pw.print("ren,");
Kenny Root447106f2011-03-23 11:00:15 -07004652 }
Kenny Root447106f2011-03-23 11:00:15 -07004653 pw.print(e.getKey());
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004654 pw.print(checkin ? " -> " : ",");
Kenny Root447106f2011-03-23 11:00:15 -07004655 pw.println(e.getValue());
4656 }
4657 }
4658
4659 printedSomething = false;
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004660 if (mDisabledSysPackages.size() > 0 && permissionNames == null) {
Kenny Root447106f2011-03-23 11:00:15 -07004661 for (final PackageSetting ps : mDisabledSysPackages.values()) {
4662 if (packageName != null && !packageName.equals(ps.realName)
4663 && !packageName.equals(ps.name)) {
4664 continue;
4665 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004666 if (!checkin && !printedSomething) {
Kenny Root447106f2011-03-23 11:00:15 -07004667 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004668 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07004669 pw.println("Hidden system packages:");
4670 printedSomething = true;
4671 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004672 dumpPackageLPr(pw, " ", checkin ? "dis" : null, permissionNames, ps, sdf, date,
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004673 users, packageName != null);
Kenny Root447106f2011-03-23 11:00:15 -07004674 }
4675 }
4676 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004677
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004678 void dumpPermissionsLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames,
4679 DumpState dumpState) {
Kenny Root447106f2011-03-23 11:00:15 -07004680 boolean printedSomething = false;
4681 for (BasePermission p : mPermissions.values()) {
4682 if (packageName != null && !packageName.equals(p.sourcePackage)) {
4683 continue;
4684 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004685 if (permissionNames != null && !permissionNames.contains(p.name)) {
4686 continue;
4687 }
Kenny Root447106f2011-03-23 11:00:15 -07004688 if (!printedSomething) {
4689 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004690 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07004691 pw.println("Permissions:");
4692 printedSomething = true;
4693 }
4694 pw.print(" Permission ["); pw.print(p.name); pw.print("] (");
4695 pw.print(Integer.toHexString(System.identityHashCode(p)));
4696 pw.println("):");
4697 pw.print(" sourcePackage="); pw.println(p.sourcePackage);
4698 pw.print(" uid="); pw.print(p.uid);
Jeff Sharkey00f39042015-03-23 16:51:22 -07004699 pw.print(" gids="); pw.print(Arrays.toString(
Xiaohui Chen594f2082015-08-18 11:04:20 -07004700 p.computeGids(UserHandle.USER_SYSTEM)));
Kenny Root447106f2011-03-23 11:00:15 -07004701 pw.print(" type="); pw.print(p.type);
Dianne Hackborne639da72012-02-21 15:11:13 -08004702 pw.print(" prot=");
4703 pw.println(PermissionInfo.protectionToString(p.protectionLevel));
Kenny Root447106f2011-03-23 11:00:15 -07004704 if (p.perm != null) {
4705 pw.print(" perm="); pw.println(p.perm);
Svet Ganov3e0be742015-08-07 23:06:00 -07004706 if ((p.perm.info.flags & PermissionInfo.FLAG_INSTALLED) == 0
Svet Ganov2a1376d2016-02-22 17:20:35 -08004707 || (p.perm.info.flags & PermissionInfo.FLAG_REMOVED) != 0) {
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004708 pw.print(" flags=0x"); pw.println(Integer.toHexString(p.perm.info.flags));
4709 }
4710 }
4711 if (p.packageSetting != null) {
4712 pw.print(" packageSetting="); pw.println(p.packageSetting);
Kenny Root447106f2011-03-23 11:00:15 -07004713 }
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004714 if (READ_EXTERNAL_STORAGE.equals(p.name)) {
Jeff Sharkey5d32e772012-04-12 15:59:23 -07004715 pw.print(" enforced=");
4716 pw.println(mReadExternalStorageEnforced);
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004717 }
Kenny Root447106f2011-03-23 11:00:15 -07004718 }
4719 }
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004720
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004721 void dumpSharedUsersLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames,
4722 DumpState dumpState, boolean checkin) {
Kenny Root447106f2011-03-23 11:00:15 -07004723 boolean printedSomething = false;
4724 for (SharedUserSetting su : mSharedUsers.values()) {
4725 if (packageName != null && su != dumpState.getSharedUser()) {
4726 continue;
4727 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004728 if (permissionNames != null
4729 && !su.getPermissionsState().hasRequestedPermission(permissionNames)) {
4730 continue;
4731 }
Dianne Hackbornd052a942014-11-21 15:23:13 -08004732 if (!checkin) {
4733 if (!printedSomething) {
4734 if (dumpState.onTitlePrinted())
4735 pw.println();
4736 pw.println("Shared users:");
4737 printedSomething = true;
4738 }
4739 pw.print(" SharedUser [");
4740 pw.print(su.name);
4741 pw.print("] (");
4742 pw.print(Integer.toHexString(System.identityHashCode(su)));
4743 pw.println("):");
Svetoslavc6d1c342015-02-26 14:44:43 -08004744
4745 String prefix = " ";
4746 pw.print(prefix); pw.print("userId="); pw.println(su.userId);
4747
4748 PermissionsState permissionsState = su.getPermissionsState();
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004749 dumpInstallPermissionsLPr(pw, prefix, permissionNames, permissionsState);
Svetoslavc6d1c342015-02-26 14:44:43 -08004750
4751 for (int userId : UserManagerService.getInstance().getUserIds()) {
4752 final int[] gids = permissionsState.computeGids(userId);
Svet Ganov8c7f7002015-05-07 10:48:44 -07004753 List<PermissionState> permissions = permissionsState
4754 .getRuntimePermissionStates(userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08004755 if (!ArrayUtils.isEmpty(gids) || !permissions.isEmpty()) {
4756 pw.print(prefix); pw.print("User "); pw.print(userId); pw.println(": ");
4757 dumpGidsLPr(pw, prefix + " ", gids);
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004758 dumpRuntimePermissionsLPr(pw, prefix + " ", permissionNames, permissions,
4759 packageName != null);
Svetoslavc6d1c342015-02-26 14:44:43 -08004760 }
Dianne Hackbornd052a942014-11-21 15:23:13 -08004761 }
4762 } else {
4763 pw.print("suid,"); pw.print(su.userId); pw.print(","); pw.println(su.name);
Kenny Root447106f2011-03-23 11:00:15 -07004764 }
4765 }
4766 }
4767
4768 void dumpReadMessagesLPr(PrintWriter pw, DumpState dumpState) {
4769 pw.println("Settings parse messages:");
4770 pw.print(mReadMessages.toString());
4771 }
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004772
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07004773 void dumpRestoredPermissionGrantsLPr(PrintWriter pw, DumpState dumpState) {
4774 if (mRestoredUserGrants.size() > 0) {
4775 pw.println();
4776 pw.println("Restored (pending) permission grants:");
4777 for (int userIndex = 0; userIndex < mRestoredUserGrants.size(); userIndex++) {
4778 ArrayMap<String, ArraySet<RestoredPermissionGrant>> grantsByPackage =
4779 mRestoredUserGrants.valueAt(userIndex);
4780 if (grantsByPackage != null && grantsByPackage.size() > 0) {
4781 final int userId = mRestoredUserGrants.keyAt(userIndex);
4782 pw.print(" User "); pw.println(userId);
4783
4784 for (int pkgIndex = 0; pkgIndex < grantsByPackage.size(); pkgIndex++) {
4785 ArraySet<RestoredPermissionGrant> grants = grantsByPackage.valueAt(pkgIndex);
4786 if (grants != null && grants.size() > 0) {
4787 final String pkgName = grantsByPackage.keyAt(pkgIndex);
4788 pw.print(" "); pw.print(pkgName); pw.println(" :");
4789
4790 for (RestoredPermissionGrant g : grants) {
4791 pw.print(" ");
4792 pw.print(g.permissionName);
4793 if (g.granted) {
4794 pw.print(" GRANTED");
4795 }
4796 if ((g.grantBits&FLAG_PERMISSION_USER_SET) != 0) {
4797 pw.print(" user_set");
4798 }
4799 if ((g.grantBits&FLAG_PERMISSION_USER_FIXED) != 0) {
4800 pw.print(" user_fixed");
4801 }
4802 if ((g.grantBits&FLAG_PERMISSION_REVOKE_ON_UPGRADE) != 0) {
4803 pw.print(" revoke_on_upgrade");
4804 }
4805 pw.println();
4806 }
4807 }
4808 }
4809 }
4810 }
4811 pw.println();
4812 }
4813 }
4814
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004815 private static void dumpSplitNames(PrintWriter pw, PackageParser.Package pkg) {
4816 if (pkg == null) {
4817 pw.print("unknown");
4818 } else {
4819 // [base:10, config.mdpi, config.xhdpi:12]
4820 pw.print("[");
4821 pw.print("base");
4822 if (pkg.baseRevisionCode != 0) {
4823 pw.print(":"); pw.print(pkg.baseRevisionCode);
4824 }
4825 if (pkg.splitNames != null) {
4826 for (int i = 0; i < pkg.splitNames.length; i++) {
4827 pw.print(", ");
4828 pw.print(pkg.splitNames[i]);
4829 if (pkg.splitRevisionCodes[i] != 0) {
4830 pw.print(":"); pw.print(pkg.splitRevisionCodes[i]);
4831 }
4832 }
4833 }
4834 pw.print("]");
4835 }
4836 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004837
4838 void dumpGidsLPr(PrintWriter pw, String prefix, int[] gids) {
4839 if (!ArrayUtils.isEmpty(gids)) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07004840 pw.print(prefix);
4841 pw.print("gids="); pw.println(
Svetoslavc6d1c342015-02-26 14:44:43 -08004842 PackageManagerService.arrayToString(gids));
4843 }
4844 }
4845
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004846 void dumpRuntimePermissionsLPr(PrintWriter pw, String prefix, ArraySet<String> permissionNames,
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004847 List<PermissionState> permissionStates, boolean dumpAll) {
4848 if (!permissionStates.isEmpty() || dumpAll) {
Svetoslavc6d1c342015-02-26 14:44:43 -08004849 pw.print(prefix); pw.println("runtime permissions:");
Svet Ganov8c7f7002015-05-07 10:48:44 -07004850 for (PermissionState permissionState : permissionStates) {
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004851 if (permissionNames != null
4852 && !permissionNames.contains(permissionState.getName())) {
4853 continue;
4854 }
Svet Ganov8c7f7002015-05-07 10:48:44 -07004855 pw.print(prefix); pw.print(" "); pw.print(permissionState.getName());
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004856 pw.print(": granted="); pw.print(permissionState.isGranted());
4857 pw.println(permissionFlagsToString(", flags=",
4858 permissionState.getFlags()));
Svetoslavc6d1c342015-02-26 14:44:43 -08004859 }
4860 }
4861 }
4862
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004863 private static String permissionFlagsToString(String prefix, int flags) {
4864 StringBuilder flagsString = null;
Svet Ganov77ab6a82015-07-03 12:03:02 -07004865 while (flags != 0) {
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004866 if (flagsString == null) {
4867 flagsString = new StringBuilder();
4868 flagsString.append(prefix);
4869 flagsString.append("[ ");
4870 }
Svet Ganov77ab6a82015-07-03 12:03:02 -07004871 final int flag = 1 << Integer.numberOfTrailingZeros(flags);
4872 flags &= ~flag;
4873 flagsString.append(PackageManager.permissionFlagToString(flag));
4874 flagsString.append(' ');
4875 }
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004876 if (flagsString != null) {
4877 flagsString.append(']');
4878 return flagsString.toString();
4879 } else {
4880 return "";
4881 }
Svet Ganov77ab6a82015-07-03 12:03:02 -07004882 }
4883
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004884 void dumpInstallPermissionsLPr(PrintWriter pw, String prefix, ArraySet<String> permissionNames,
Svetoslavc6d1c342015-02-26 14:44:43 -08004885 PermissionsState permissionsState) {
Svet Ganov8c7f7002015-05-07 10:48:44 -07004886 List<PermissionState> permissionStates = permissionsState.getInstallPermissionStates();
4887 if (!permissionStates.isEmpty()) {
Svetoslavc6d1c342015-02-26 14:44:43 -08004888 pw.print(prefix); pw.println("install permissions:");
Svet Ganov8c7f7002015-05-07 10:48:44 -07004889 for (PermissionState permissionState : permissionStates) {
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004890 if (permissionNames != null
4891 && !permissionNames.contains(permissionState.getName())) {
4892 continue;
4893 }
Svet Ganov8c7f7002015-05-07 10:48:44 -07004894 pw.print(prefix); pw.print(" "); pw.print(permissionState.getName());
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004895 pw.print(": granted="); pw.print(permissionState.isGranted());
4896 pw.println(permissionFlagsToString(", flags=",
Svet Ganov8c7f7002015-05-07 10:48:44 -07004897 permissionState.getFlags()));
Svetoslavc6d1c342015-02-26 14:44:43 -08004898 }
4899 }
4900 }
4901
4902 public void writeRuntimePermissionsForUserLPr(int userId, boolean sync) {
4903 if (sync) {
4904 mRuntimePermissionsPersistence.writePermissionsForUserSyncLPr(userId);
4905 } else {
4906 mRuntimePermissionsPersistence.writePermissionsForUserAsyncLPr(userId);
4907 }
4908 }
4909
4910 private final class RuntimePermissionPersistence {
4911 private static final long WRITE_PERMISSIONS_DELAY_MILLIS = 200;
Svetoslavc6d1c342015-02-26 14:44:43 -08004912 private static final long MAX_WRITE_PERMISSIONS_DELAY_MILLIS = 2000;
4913
4914 private final Handler mHandler = new MyHandler();
4915
4916 private final Object mLock;
4917
4918 @GuardedBy("mLock")
Svet Ganovba3ba812015-06-26 10:54:06 -07004919 private final SparseBooleanArray mWriteScheduled = new SparseBooleanArray();
Svetoslavc6d1c342015-02-26 14:44:43 -08004920
4921 @GuardedBy("mLock")
Svet Ganovba3ba812015-06-26 10:54:06 -07004922 // The mapping keys are user ids.
4923 private final SparseLongArray mLastNotWrittenMutationTimesMillis = new SparseLongArray();
4924
4925 @GuardedBy("mLock")
4926 // The mapping keys are user ids.
4927 private final SparseArray<String> mFingerprints = new SparseArray<>();
4928
4929 @GuardedBy("mLock")
4930 // The mapping keys are user ids.
4931 private final SparseBooleanArray mDefaultPermissionsGranted = new SparseBooleanArray();
Svetoslavc6d1c342015-02-26 14:44:43 -08004932
4933 public RuntimePermissionPersistence(Object lock) {
4934 mLock = lock;
4935 }
4936
Svet Ganovba3ba812015-06-26 10:54:06 -07004937 public boolean areDefaultRuntimPermissionsGrantedLPr(int userId) {
4938 return mDefaultPermissionsGranted.get(userId);
4939 }
4940
4941 public void onDefaultRuntimePermissionsGrantedLPr(int userId) {
4942 mFingerprints.put(userId, Build.FINGERPRINT);
4943 writePermissionsForUserAsyncLPr(userId);
4944 }
4945
Svetoslavc6d1c342015-02-26 14:44:43 -08004946 public void writePermissionsForUserSyncLPr(int userId) {
4947 mHandler.removeMessages(userId);
4948 writePermissionsSync(userId);
4949 }
4950
4951 public void writePermissionsForUserAsyncLPr(int userId) {
4952 final long currentTimeMillis = SystemClock.uptimeMillis();
4953
4954 if (mWriteScheduled.get(userId)) {
4955 mHandler.removeMessages(userId);
4956
4957 // If enough time passed, write without holding off anymore.
4958 final long lastNotWrittenMutationTimeMillis = mLastNotWrittenMutationTimesMillis
4959 .get(userId);
4960 final long timeSinceLastNotWrittenMutationMillis = currentTimeMillis
4961 - lastNotWrittenMutationTimeMillis;
4962 if (timeSinceLastNotWrittenMutationMillis >= MAX_WRITE_PERMISSIONS_DELAY_MILLIS) {
4963 mHandler.obtainMessage(userId).sendToTarget();
4964 return;
4965 }
4966
4967 // Hold off a bit more as settings are frequently changing.
4968 final long maxDelayMillis = Math.max(lastNotWrittenMutationTimeMillis
4969 + MAX_WRITE_PERMISSIONS_DELAY_MILLIS - currentTimeMillis, 0);
4970 final long writeDelayMillis = Math.min(WRITE_PERMISSIONS_DELAY_MILLIS,
4971 maxDelayMillis);
4972
4973 Message message = mHandler.obtainMessage(userId);
4974 mHandler.sendMessageDelayed(message, writeDelayMillis);
4975 } else {
4976 mLastNotWrittenMutationTimesMillis.put(userId, currentTimeMillis);
4977 Message message = mHandler.obtainMessage(userId);
4978 mHandler.sendMessageDelayed(message, WRITE_PERMISSIONS_DELAY_MILLIS);
4979 mWriteScheduled.put(userId, true);
4980 }
4981 }
4982
4983 private void writePermissionsSync(int userId) {
4984 AtomicFile destination = new AtomicFile(getUserRuntimePermissionsFile(userId));
4985
Svet Ganov8c7f7002015-05-07 10:48:44 -07004986 ArrayMap<String, List<PermissionState>> permissionsForPackage = new ArrayMap<>();
4987 ArrayMap<String, List<PermissionState>> permissionsForSharedUser = new ArrayMap<>();
Svetoslavc6d1c342015-02-26 14:44:43 -08004988
4989 synchronized (mLock) {
4990 mWriteScheduled.delete(userId);
4991
4992 final int packageCount = mPackages.size();
4993 for (int i = 0; i < packageCount; i++) {
4994 String packageName = mPackages.keyAt(i);
4995 PackageSetting packageSetting = mPackages.valueAt(i);
4996 if (packageSetting.sharedUser == null) {
4997 PermissionsState permissionsState = packageSetting.getPermissionsState();
Svet Ganov8c7f7002015-05-07 10:48:44 -07004998 List<PermissionState> permissionsStates = permissionsState
4999 .getRuntimePermissionStates(userId);
5000 if (!permissionsStates.isEmpty()) {
5001 permissionsForPackage.put(packageName, permissionsStates);
Svetoslavc6d1c342015-02-26 14:44:43 -08005002 }
5003 }
5004 }
5005
5006 final int sharedUserCount = mSharedUsers.size();
5007 for (int i = 0; i < sharedUserCount; i++) {
5008 String sharedUserName = mSharedUsers.keyAt(i);
5009 SharedUserSetting sharedUser = mSharedUsers.valueAt(i);
5010 PermissionsState permissionsState = sharedUser.getPermissionsState();
Svet Ganov8c7f7002015-05-07 10:48:44 -07005011 List<PermissionState> permissionsStates = permissionsState
5012 .getRuntimePermissionStates(userId);
5013 if (!permissionsStates.isEmpty()) {
5014 permissionsForSharedUser.put(sharedUserName, permissionsStates);
Svetoslavc6d1c342015-02-26 14:44:43 -08005015 }
5016 }
5017 }
5018
5019 FileOutputStream out = null;
5020 try {
5021 out = destination.startWrite();
5022
5023 XmlSerializer serializer = Xml.newSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01005024 serializer.setOutput(out, StandardCharsets.UTF_8.name());
Svetoslavc6d1c342015-02-26 14:44:43 -08005025 serializer.setFeature(
5026 "http://xmlpull.org/v1/doc/features.html#indent-output", true);
5027 serializer.startDocument(null, true);
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07005028
Svetoslavc6d1c342015-02-26 14:44:43 -08005029 serializer.startTag(null, TAG_RUNTIME_PERMISSIONS);
5030
Svet Ganovba3ba812015-06-26 10:54:06 -07005031 String fingerprint = mFingerprints.get(userId);
Svetoslavcdfd2302015-06-25 19:07:31 -07005032 if (fingerprint != null) {
5033 serializer.attribute(null, ATTR_FINGERPRINT, fingerprint);
5034 }
Svet Ganovba3ba812015-06-26 10:54:06 -07005035
Svetoslavc6d1c342015-02-26 14:44:43 -08005036 final int packageCount = permissionsForPackage.size();
5037 for (int i = 0; i < packageCount; i++) {
5038 String packageName = permissionsForPackage.keyAt(i);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005039 List<PermissionState> permissionStates = permissionsForPackage.valueAt(i);
Svetoslavc6d1c342015-02-26 14:44:43 -08005040 serializer.startTag(null, TAG_PACKAGE);
5041 serializer.attribute(null, ATTR_NAME, packageName);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005042 writePermissions(serializer, permissionStates);
Svetoslavc6d1c342015-02-26 14:44:43 -08005043 serializer.endTag(null, TAG_PACKAGE);
5044 }
5045
5046 final int sharedUserCount = permissionsForSharedUser.size();
5047 for (int i = 0; i < sharedUserCount; i++) {
5048 String packageName = permissionsForSharedUser.keyAt(i);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005049 List<PermissionState> permissionStates = permissionsForSharedUser.valueAt(i);
Svetoslavc6d1c342015-02-26 14:44:43 -08005050 serializer.startTag(null, TAG_SHARED_USER);
5051 serializer.attribute(null, ATTR_NAME, packageName);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005052 writePermissions(serializer, permissionStates);
Svetoslavc6d1c342015-02-26 14:44:43 -08005053 serializer.endTag(null, TAG_SHARED_USER);
5054 }
5055
5056 serializer.endTag(null, TAG_RUNTIME_PERMISSIONS);
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07005057
5058 // Now any restored permission grants that are waiting for the apps
5059 // in question to be installed. These are stored as per-package
5060 // TAG_RESTORED_RUNTIME_PERMISSIONS blocks, each containing some
5061 // number of individual permission grant entities.
5062 if (mRestoredUserGrants.get(userId) != null) {
5063 ArrayMap<String, ArraySet<RestoredPermissionGrant>> restoredGrants =
5064 mRestoredUserGrants.get(userId);
5065 if (restoredGrants != null) {
5066 final int pkgCount = restoredGrants.size();
5067 for (int i = 0; i < pkgCount; i++) {
5068 final ArraySet<RestoredPermissionGrant> pkgGrants =
5069 restoredGrants.valueAt(i);
5070 if (pkgGrants != null && pkgGrants.size() > 0) {
5071 final String pkgName = restoredGrants.keyAt(i);
5072 serializer.startTag(null, TAG_RESTORED_RUNTIME_PERMISSIONS);
5073 serializer.attribute(null, ATTR_PACKAGE_NAME, pkgName);
5074
5075 final int N = pkgGrants.size();
5076 for (int z = 0; z < N; z++) {
5077 RestoredPermissionGrant g = pkgGrants.valueAt(z);
5078 serializer.startTag(null, TAG_PERMISSION_ENTRY);
5079 serializer.attribute(null, ATTR_NAME, g.permissionName);
5080
5081 if (g.granted) {
5082 serializer.attribute(null, ATTR_GRANTED, "true");
5083 }
5084
5085 if ((g.grantBits&FLAG_PERMISSION_USER_SET) != 0) {
5086 serializer.attribute(null, ATTR_USER_SET, "true");
5087 }
5088 if ((g.grantBits&FLAG_PERMISSION_USER_FIXED) != 0) {
5089 serializer.attribute(null, ATTR_USER_FIXED, "true");
5090 }
5091 if ((g.grantBits&FLAG_PERMISSION_REVOKE_ON_UPGRADE) != 0) {
5092 serializer.attribute(null, ATTR_REVOKE_ON_UPGRADE, "true");
5093 }
5094 serializer.endTag(null, TAG_PERMISSION_ENTRY);
5095 }
5096 serializer.endTag(null, TAG_RESTORED_RUNTIME_PERMISSIONS);
5097 }
5098 }
5099 }
5100 }
5101
Svetoslavc6d1c342015-02-26 14:44:43 -08005102 serializer.endDocument();
5103 destination.finishWrite(out);
Svet Ganovba0821e2015-04-22 13:34:31 -07005104
Svet Ganovba3ba812015-06-26 10:54:06 -07005105 if (Build.FINGERPRINT.equals(fingerprint)) {
5106 mDefaultPermissionsGranted.put(userId, true);
5107 }
5108 // Any error while writing is fatal.
Svet Ganovba0821e2015-04-22 13:34:31 -07005109 } catch (Throwable t) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005110 Slog.wtf(PackageManagerService.TAG,
Svet Ganovba0821e2015-04-22 13:34:31 -07005111 "Failed to write settings, restoring backup", t);
Svetoslavc6d1c342015-02-26 14:44:43 -08005112 destination.failWrite(out);
5113 } finally {
5114 IoUtils.closeQuietly(out);
5115 }
5116 }
5117
Fyodor Kupolov69542592016-05-23 14:22:38 -07005118 private void onUserRemovedLPw(int userId) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005119 // Make sure we do not
5120 mHandler.removeMessages(userId);
5121
5122 for (SettingBase sb : mPackages.values()) {
Svet Ganov8c7f7002015-05-07 10:48:44 -07005123 revokeRuntimePermissionsAndClearFlags(sb, userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08005124 }
5125
5126 for (SettingBase sb : mSharedUsers.values()) {
Svet Ganov8c7f7002015-05-07 10:48:44 -07005127 revokeRuntimePermissionsAndClearFlags(sb, userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08005128 }
Fyodor Kupolov69542592016-05-23 14:22:38 -07005129
5130 mDefaultPermissionsGranted.delete(userId);
5131 mFingerprints.remove(userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08005132 }
5133
Svet Ganov8c7f7002015-05-07 10:48:44 -07005134 private void revokeRuntimePermissionsAndClearFlags(SettingBase sb, int userId) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005135 PermissionsState permissionsState = sb.getPermissionsState();
Svet Ganov8c7f7002015-05-07 10:48:44 -07005136 for (PermissionState permissionState
5137 : permissionsState.getRuntimePermissionStates(userId)) {
5138 BasePermission bp = mPermissions.get(permissionState.getName());
Svetoslavc6d1c342015-02-26 14:44:43 -08005139 if (bp != null) {
5140 permissionsState.revokeRuntimePermission(bp, userId);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005141 permissionsState.updatePermissionFlags(bp, userId,
5142 PackageManager.MASK_PERMISSION_FLAGS, 0);
Svetoslavc6d1c342015-02-26 14:44:43 -08005143 }
5144 }
5145 }
5146
Svet Ganovadc1cf42015-06-15 16:36:24 -07005147 public void deleteUserRuntimePermissionsFile(int userId) {
5148 getUserRuntimePermissionsFile(userId).delete();
5149 }
5150
Svetoslavc6d1c342015-02-26 14:44:43 -08005151 public void readStateForUserSyncLPr(int userId) {
5152 File permissionsFile = getUserRuntimePermissionsFile(userId);
5153 if (!permissionsFile.exists()) {
5154 return;
5155 }
5156
5157 FileInputStream in;
5158 try {
Svetoslav3dcdd372015-05-29 13:00:32 -07005159 in = new AtomicFile(permissionsFile).openRead();
Svetoslavc6d1c342015-02-26 14:44:43 -08005160 } catch (FileNotFoundException fnfe) {
5161 Slog.i(PackageManagerService.TAG, "No permissions state");
5162 return;
5163 }
5164
5165 try {
5166 XmlPullParser parser = Xml.newPullParser();
5167 parser.setInput(in, null);
5168 parseRuntimePermissionsLPr(parser, userId);
Svet Ganovba0821e2015-04-22 13:34:31 -07005169
Svet Ganove723e542015-04-23 11:58:26 -07005170 } catch (XmlPullParserException | IOException e) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005171 throw new IllegalStateException("Failed parsing permissions file: "
Svet Ganove723e542015-04-23 11:58:26 -07005172 + permissionsFile , e);
Svetoslavc6d1c342015-02-26 14:44:43 -08005173 } finally {
5174 IoUtils.closeQuietly(in);
5175 }
5176 }
5177
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07005178 // Backup/restore support
5179
5180 public void rememberRestoredUserGrantLPr(String pkgName, String permission,
5181 boolean isGranted, int restoredFlagSet, int userId) {
5182 // This change will be remembered at write-settings time
5183 ArrayMap<String, ArraySet<RestoredPermissionGrant>> grantsByPackage =
5184 mRestoredUserGrants.get(userId);
5185 if (grantsByPackage == null) {
5186 grantsByPackage = new ArrayMap<String, ArraySet<RestoredPermissionGrant>>();
5187 mRestoredUserGrants.put(userId, grantsByPackage);
5188 }
5189
5190 ArraySet<RestoredPermissionGrant> grants = grantsByPackage.get(pkgName);
5191 if (grants == null) {
5192 grants = new ArraySet<RestoredPermissionGrant>();
5193 grantsByPackage.put(pkgName, grants);
5194 }
5195
5196 RestoredPermissionGrant grant = new RestoredPermissionGrant(permission,
5197 isGranted, restoredFlagSet);
5198 grants.add(grant);
5199 }
5200
5201 // Private internals
5202
Svetoslavc6d1c342015-02-26 14:44:43 -08005203 private void parseRuntimePermissionsLPr(XmlPullParser parser, int userId)
5204 throws IOException, XmlPullParserException {
Svet Ganov12a692a2015-03-28 19:34:15 -07005205 final int outerDepth = parser.getDepth();
5206 int type;
5207 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
5208 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
5209 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
5210 continue;
5211 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005212
Svet Ganov12a692a2015-03-28 19:34:15 -07005213 switch (parser.getName()) {
Svet Ganovba3ba812015-06-26 10:54:06 -07005214 case TAG_RUNTIME_PERMISSIONS: {
5215 String fingerprint = parser.getAttributeValue(null, ATTR_FINGERPRINT);
5216 mFingerprints.put(userId, fingerprint);
5217 final boolean defaultsGranted = Build.FINGERPRINT.equals(fingerprint);
5218 mDefaultPermissionsGranted.put(userId, defaultsGranted);
5219 } break;
5220
Svet Ganov12a692a2015-03-28 19:34:15 -07005221 case TAG_PACKAGE: {
5222 String name = parser.getAttributeValue(null, ATTR_NAME);
5223 PackageSetting ps = mPackages.get(name);
5224 if (ps == null) {
5225 Slog.w(PackageManagerService.TAG, "Unknown package:" + name);
5226 XmlUtils.skipCurrentTag(parser);
5227 continue;
5228 }
5229 parsePermissionsLPr(parser, ps.getPermissionsState(), userId);
5230 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08005231
Svet Ganov12a692a2015-03-28 19:34:15 -07005232 case TAG_SHARED_USER: {
5233 String name = parser.getAttributeValue(null, ATTR_NAME);
5234 SharedUserSetting sus = mSharedUsers.get(name);
5235 if (sus == null) {
5236 Slog.w(PackageManagerService.TAG, "Unknown shared user:" + name);
5237 XmlUtils.skipCurrentTag(parser);
5238 continue;
5239 }
5240 parsePermissionsLPr(parser, sus.getPermissionsState(), userId);
5241 } break;
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07005242
5243 case TAG_RESTORED_RUNTIME_PERMISSIONS: {
5244 final String pkgName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
5245 parseRestoredRuntimePermissionsLPr(parser, pkgName, userId);
5246 } break;
5247 }
5248 }
5249 }
5250
5251 private void parseRestoredRuntimePermissionsLPr(XmlPullParser parser,
5252 final String pkgName, final int userId) throws IOException, XmlPullParserException {
5253 final int outerDepth = parser.getDepth();
5254 int type;
5255 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
5256 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
5257 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
5258 continue;
5259 }
5260
5261 switch (parser.getName()) {
5262 case TAG_PERMISSION_ENTRY: {
5263 final String permName = parser.getAttributeValue(null, ATTR_NAME);
5264 final boolean isGranted = "true".equals(
5265 parser.getAttributeValue(null, ATTR_GRANTED));
5266
5267 int permBits = 0;
5268 if ("true".equals(parser.getAttributeValue(null, ATTR_USER_SET))) {
5269 permBits |= FLAG_PERMISSION_USER_SET;
5270 }
5271 if ("true".equals(parser.getAttributeValue(null, ATTR_USER_FIXED))) {
5272 permBits |= FLAG_PERMISSION_USER_FIXED;
5273 }
5274 if ("true".equals(parser.getAttributeValue(null, ATTR_REVOKE_ON_UPGRADE))) {
5275 permBits |= FLAG_PERMISSION_REVOKE_ON_UPGRADE;
5276 }
5277
5278 if (isGranted || permBits != 0) {
5279 rememberRestoredUserGrantLPr(pkgName, permName, isGranted, permBits, userId);
5280 }
5281 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08005282 }
5283 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005284 }
5285
Svet Ganov12a692a2015-03-28 19:34:15 -07005286 private void parsePermissionsLPr(XmlPullParser parser, PermissionsState permissionsState,
Svetoslavc6d1c342015-02-26 14:44:43 -08005287 int userId) throws IOException, XmlPullParserException {
Svet Ganov12a692a2015-03-28 19:34:15 -07005288 final int outerDepth = parser.getDepth();
5289 int type;
5290 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
5291 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
5292 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
5293 continue;
Svetoslavc6d1c342015-02-26 14:44:43 -08005294 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005295
Svet Ganov12a692a2015-03-28 19:34:15 -07005296 switch (parser.getName()) {
5297 case TAG_ITEM: {
5298 String name = parser.getAttributeValue(null, ATTR_NAME);
5299 BasePermission bp = mPermissions.get(name);
5300 if (bp == null) {
5301 Slog.w(PackageManagerService.TAG, "Unknown permission:" + name);
5302 XmlUtils.skipCurrentTag(parser);
5303 continue;
5304 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005305
Svet Ganov8c7f7002015-05-07 10:48:44 -07005306 String grantedStr = parser.getAttributeValue(null, ATTR_GRANTED);
5307 final boolean granted = grantedStr == null
5308 || Boolean.parseBoolean(grantedStr);
5309
5310 String flagsStr = parser.getAttributeValue(null, ATTR_FLAGS);
5311 final int flags = (flagsStr != null)
5312 ? Integer.parseInt(flagsStr, 16) : 0;
5313
5314 if (granted) {
Svet Ganovadc1cf42015-06-15 16:36:24 -07005315 permissionsState.grantRuntimePermission(bp, userId);
5316 permissionsState.updatePermissionFlags(bp, userId,
Svet Ganov8c7f7002015-05-07 10:48:44 -07005317 PackageManager.MASK_PERMISSION_FLAGS, flags);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005318 } else {
Svet Ganovadc1cf42015-06-15 16:36:24 -07005319 permissionsState.updatePermissionFlags(bp, userId,
5320 PackageManager.MASK_PERMISSION_FLAGS, flags);
Svet Ganov12a692a2015-03-28 19:34:15 -07005321 }
Svet Ganov8c7f7002015-05-07 10:48:44 -07005322
Svet Ganov12a692a2015-03-28 19:34:15 -07005323 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08005324 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005325 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005326 }
5327
Svet Ganov8c7f7002015-05-07 10:48:44 -07005328 private void writePermissions(XmlSerializer serializer,
5329 List<PermissionState> permissionStates) throws IOException {
5330 for (PermissionState permissionState : permissionStates) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005331 serializer.startTag(null, TAG_ITEM);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005332 serializer.attribute(null, ATTR_NAME,permissionState.getName());
5333 serializer.attribute(null, ATTR_GRANTED,
5334 String.valueOf(permissionState.isGranted()));
5335 serializer.attribute(null, ATTR_FLAGS,
5336 Integer.toHexString(permissionState.getFlags()));
Svetoslavc6d1c342015-02-26 14:44:43 -08005337 serializer.endTag(null, TAG_ITEM);
5338 }
5339 }
5340
5341 private final class MyHandler extends Handler {
5342 public MyHandler() {
5343 super(BackgroundThread.getHandler().getLooper());
5344 }
5345
5346 @Override
5347 public void handleMessage(Message message) {
5348 final int userId = message.what;
5349 Runnable callback = (Runnable) message.obj;
5350 writePermissionsSync(userId);
5351 if (callback != null) {
5352 callback.run();
5353 }
5354 }
5355 }
5356 }
Andy McFadden2f362292012-01-20 14:43:38 -08005357}