blob: 310ad5353e32c6342c971fb0841385f70a70583f [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;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070080import com.android.internal.os.InstallerConnection.InstallerException;
Svetoslavc6d1c342015-02-26 14:44:43 -080081import com.android.internal.util.ArrayUtils;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070082import com.android.internal.util.FastXmlSerializer;
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -070083import com.android.internal.util.IndentingPrintWriter;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070084import com.android.internal.util.JournaledFile;
85import com.android.internal.util.XmlUtils;
Christopher Tatee012a232015-04-01 17:18:50 -070086import com.android.server.backup.PreferredActivityBackupHelper;
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";
192 private static final String TAG_VERSION = "version";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700193
194 private static final String ATTR_NAME = "name";
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700195 private static final String ATTR_USER = "user";
196 private static final String ATTR_CODE = "code";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700197 private static final String ATTR_NOT_LAUNCHED = "nl";
198 private static final String ATTR_ENABLED = "enabled";
Svet Ganov8c7f7002015-05-07 10:48:44 -0700199 private static final String ATTR_GRANTED = "granted";
200 private static final String ATTR_FLAGS = "flags";
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -0700201 private static final String ATTR_ENABLED_CALLER = "enabledCaller";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700202 private static final String ATTR_STOPPED = "stopped";
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700203 // Legacy, here for reading older versions of the package-restrictions.
Amith Yamasani655d0e22013-06-12 14:19:10 -0700204 private static final String ATTR_BLOCKED = "blocked";
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700205 // New name for the above attribute.
206 private static final String ATTR_HIDDEN = "hidden";
Andrei Stingaceanu1e283912015-11-26 15:26:28 +0000207 private static final String ATTR_SUSPENDED = "suspended";
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700208 private static final String ATTR_INSTALLED = "inst";
Kenny Guyc13053b2014-05-29 14:17:17 +0100209 private static final String ATTR_BLOCK_UNINSTALL = "blockUninstall";
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800210 private static final String ATTR_DOMAIN_VERIFICATON_STATE = "domainVerificationStatus";
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700211 private static final String ATTR_PACKAGE_NAME = "packageName";
Svet Ganovba3ba812015-06-26 10:54:06 -0700212 private static final String ATTR_FINGERPRINT = "fingerprint";
Christopher Tatef0d6cb32015-07-10 17:44:53 -0700213 private static final String ATTR_APP_LINK_GENERATION = "app-link-generation";
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700214 private static final String ATTR_VOLUME_UUID = "volumeUuid";
215 private static final String ATTR_SDK_VERSION = "sdkVersion";
216 private static final String ATTR_DATABASE_VERSION = "databaseVersion";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700217
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700218 // Bookkeeping for restored permission grants
219 private static final String TAG_RESTORED_RUNTIME_PERMISSIONS = "restored-perms";
220 // package name: ATTR_PACKAGE_NAME
221 private static final String TAG_PERMISSION_ENTRY = "perm";
222 // permission name: ATTR_NAME
223 // permission granted (boolean): ATTR_GRANTED
224 private static final String ATTR_USER_SET = "set";
225 private static final String ATTR_USER_FIXED = "fixed";
226 private static final String ATTR_REVOKE_ON_UPGRADE = "rou";
227
228 // Flag mask of restored permission grants that are applied at install time
229 private static final int USER_RUNTIME_GRANT_MASK =
230 FLAG_PERMISSION_USER_SET
231 | FLAG_PERMISSION_USER_FIXED
232 | FLAG_PERMISSION_REVOKE_ON_UPGRADE;
233
Svetoslavc6d1c342015-02-26 14:44:43 -0800234 private final Object mLock;
Svetoslavc6d1c342015-02-26 14:44:43 -0800235
236 private final RuntimePermissionPersistence mRuntimePermissionsPersistence;
237
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700238 private final File mSettingsFilename;
239 private final File mBackupSettingsFilename;
240 private final File mPackageListFilename;
241 private final File mStoppedPackagesFilename;
242 private final File mBackupStoppedPackagesFilename;
Jeff Sharkey2271ba32016-02-01 17:57:08 -0700243 private final File mKernelMappingFilename;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700244
Jeff Sharkey2271ba32016-02-01 17:57:08 -0700245 /** Map from package name to settings */
246 final ArrayMap<String, PackageSetting> mPackages = new ArrayMap<>();
247
248 /** Map from package name to appId */
249 private final ArrayMap<String, Integer> mKernelMapping = new ArrayMap<>();
Christopher Tate6038d152015-06-17 13:07:46 -0700250
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700251 // List of replaced system applications
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700252 private final ArrayMap<String, PackageSetting> mDisabledSysPackages =
253 new ArrayMap<String, PackageSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700254
Christopher Tate6038d152015-06-17 13:07:46 -0700255 // Set of restored intent-filter verification states
256 private final ArrayMap<String, IntentFilterVerificationInfo> mRestoredIntentFilterVerifications =
257 new ArrayMap<String, IntentFilterVerificationInfo>();
258
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700259 // Bookkeeping for restored user permission grants
260 final class RestoredPermissionGrant {
261 String permissionName;
262 boolean granted;
263 int grantBits;
264
265 RestoredPermissionGrant(String name, boolean isGranted, int theGrantBits) {
266 permissionName = name;
267 granted = isGranted;
268 grantBits = theGrantBits;
269 }
270 }
271
272 // This would be more compact as a flat array of restored grants or something, but we
273 // may have quite a few, especially during early device lifetime, and avoiding all those
274 // linear lookups will be important.
275 private final SparseArray<ArrayMap<String, ArraySet<RestoredPermissionGrant>>>
276 mRestoredUserGrants =
277 new SparseArray<ArrayMap<String, ArraySet<RestoredPermissionGrant>>>();
278
Geremy Condra12c18382013-03-06 16:49:06 -0800279 private static int mFirstAvailableUid = 0;
280
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700281 /** Map from volume UUID to {@link VersionInfo} */
282 private ArrayMap<String, VersionInfo> mVersion = new ArrayMap<>();
Kenny Root0aaa0d92011-09-12 16:42:55 -0700283
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700284 /**
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700285 * Version details for a storage volume that may hold apps.
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700286 */
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700287 public static class VersionInfo {
288 /**
289 * These are the last platform API version we were using for the apps
290 * installed on internal and external storage. It is used to grant newer
291 * permissions one time during a system upgrade.
292 */
293 int sdkVersion;
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700294
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -0700295 /**
296 * The current database version for apps on internal storage. This is
297 * used to upgrade the format of the packages.xml database not
298 * necessarily tied to an SDK version.
299 */
300 int databaseVersion;
301
302 /**
303 * Last known value of {@link Build#FINGERPRINT}. Used to determine when
304 * an system update has occurred, meaning we need to clear code caches.
305 */
306 String fingerprint;
307
308 /**
309 * Force all version information to match current system values,
310 * typically after resolving any required upgrade steps.
311 */
312 public void forceCurrent() {
313 sdkVersion = Build.VERSION.SDK_INT;
314 databaseVersion = CURRENT_DATABASE_VERSION;
315 fingerprint = Build.FINGERPRINT;
316 }
317 }
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700318
Jeff Sharkeyf5385772012-05-11 14:04:41 -0700319 Boolean mReadExternalStorageEnforced;
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -0700320
Kenny Root0aaa0d92011-09-12 16:42:55 -0700321 /** Device identity for the purpose of package verification. */
322 private VerifierDeviceIdentity mVerifierDeviceIdentity;
323
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700324 // The user's preferred activities associated with particular intent
325 // filters.
Dianne Hackborn63092712012-10-07 14:45:35 -0700326 final SparseArray<PreferredIntentResolver> mPreferredActivities =
327 new SparseArray<PreferredIntentResolver>();
328
Sander Alewijnsef475ca32014-02-17 15:13:58 +0000329 // The persistent preferred activities of the user's profile/device owner
330 // associated with particular intent filters.
331 final SparseArray<PersistentPreferredIntentResolver> mPersistentPreferredActivities =
332 new SparseArray<PersistentPreferredIntentResolver>();
333
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000334 // For every user, it is used to find to which other users the intent can be forwarded.
Nicolas Prevot81948992014-05-16 18:25:26 +0100335 final SparseArray<CrossProfileIntentResolver> mCrossProfileIntentResolvers =
336 new SparseArray<CrossProfileIntentResolver>();
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000337
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700338 final ArrayMap<String, SharedUserSetting> mSharedUsers =
339 new ArrayMap<String, SharedUserSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700340 private final ArrayList<Object> mUserIds = new ArrayList<Object>();
341 private final SparseArray<Object> mOtherUserIds =
342 new SparseArray<Object>();
343
344 // For reading/writing settings file.
345 private final ArrayList<Signature> mPastSignatures =
346 new ArrayList<Signature>();
dcashman8c04fac2015-03-23 11:39:42 -0700347 private final ArrayMap<Long, Integer> mKeySetRefs =
348 new ArrayMap<Long, Integer>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700349
350 // Mapping from permission names to info about them.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700351 final ArrayMap<String, BasePermission> mPermissions =
352 new ArrayMap<String, BasePermission>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700353
354 // Mapping from permission tree names to info about them.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700355 final ArrayMap<String, BasePermission> mPermissionTrees =
356 new ArrayMap<String, BasePermission>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700357
358 // Packages that have been uninstalled and still need their external
359 // storage data deleted.
Jeff Sharkey752cd922012-09-23 16:25:12 -0700360 final ArrayList<PackageCleanItem> mPackagesToBeCleaned = new ArrayList<PackageCleanItem>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700361
362 // Packages that have been renamed since they were first installed.
363 // Keys are the new names of the packages, values are the original
364 // names. The packages appear everwhere else under their original
365 // names.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700366 final ArrayMap<String, String> mRenamedPackages = new ArrayMap<String, String>();
Fabrice Di Meglio62271722015-04-10 17:24:02 -0700367
368 // For every user, it is used to find the package name of the default Browser App.
369 final SparseArray<String> mDefaultBrowserApp = new SparseArray<String>();
370
Christopher Tatef0d6cb32015-07-10 17:44:53 -0700371 // App-link priority tracking, per-user
372 final SparseIntArray mNextAppLinkGeneration = new SparseIntArray();
373
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700374 final StringBuilder mReadMessages = new StringBuilder();
375
Kenny Root447106f2011-03-23 11:00:15 -0700376 /**
377 * Used to track packages that have a shared user ID that hasn't been read
378 * in yet.
379 * <p>
380 * TODO: make this just a local variable that is passed in during package
381 * scanning to make it less confusing.
382 */
383 private final ArrayList<PendingPackage> mPendingPackages = new ArrayList<PendingPackage>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700384
Amith Yamasani483f3b02012-03-13 16:08:00 -0700385 private final File mSystemDir;
Geremy Condraf1bcca82013-01-07 22:35:24 -0800386
dcashman55b10782014-04-09 14:20:38 -0700387 public final KeySetManagerService mKeySetManagerService = new KeySetManagerService(mPackages);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800388
Svet Ganov12a692a2015-03-28 19:34:15 -0700389 Settings(Object lock) {
390 this(Environment.getDataDirectory(), lock);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700391 }
392
Svet Ganov12a692a2015-03-28 19:34:15 -0700393 Settings(File dataDir, Object lock) {
Svetoslavc6d1c342015-02-26 14:44:43 -0800394 mLock = lock;
395
396 mRuntimePermissionsPersistence = new RuntimePermissionPersistence(mLock);
397
Amith Yamasani483f3b02012-03-13 16:08:00 -0700398 mSystemDir = new File(dataDir, "system");
399 mSystemDir.mkdirs();
400 FileUtils.setPermissions(mSystemDir.toString(),
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700401 FileUtils.S_IRWXU|FileUtils.S_IRWXG
402 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
403 -1, -1);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700404 mSettingsFilename = new File(mSystemDir, "packages.xml");
405 mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
406 mPackageListFilename = new File(mSystemDir, "packages.list");
Jeff Sharkeyaebb65c2014-11-24 15:00:13 -0800407 FileUtils.setPermissions(mPackageListFilename, 0640, SYSTEM_UID, PACKAGE_INFO_GID);
Jeff Sharkey184a0102013-07-10 16:19:52 -0700408
Jeff Sharkey2271ba32016-02-01 17:57:08 -0700409 final File kernelDir = new File("/config/sdcardfs");
410 mKernelMappingFilename = kernelDir.exists() ? kernelDir : null;
411
Amith Yamasani483f3b02012-03-13 16:08:00 -0700412 // Deprecated: Needed for migration
413 mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
414 mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700415 }
416
Kenny Root447106f2011-03-23 11:00:15 -0700417 PackageSetting getPackageLPw(PackageParser.Package pkg, PackageSetting origPackage,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700418 String realName, SharedUserSetting sharedUser, File codePath, File resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100419 String legacyNativeLibraryPathString, String primaryCpuAbi, String secondaryCpuAbi,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800420 int pkgFlags, int pkgPrivateFlags, UserHandle user, boolean add) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700421 final String name = pkg.packageName;
Svet Ganov354cd3c2015-12-17 11:35:04 -0800422 final String parentPackageName = (pkg.parentPackage != null)
423 ? pkg.parentPackage.packageName : null;
424
425 List<String> childPackageNames = null;
426 if (pkg.childPackages != null) {
427 final int childCount = pkg.childPackages.size();
428 childPackageNames = new ArrayList<>(childCount);
429 for (int i = 0; i < childCount; i++) {
430 String childPackageName = pkg.childPackages.get(i).packageName;
431 childPackageNames.add(childPackageName);
432 }
433 }
434
Kenny Root447106f2011-03-23 11:00:15 -0700435 PackageSetting p = getPackageLPw(name, origPackage, realName, sharedUser, codePath,
Jeff Sharkey84f12942014-07-10 17:48:11 -0700436 resourcePath, legacyNativeLibraryPathString, primaryCpuAbi, secondaryCpuAbi,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800437 pkg.mVersionCode, pkgFlags, pkgPrivateFlags, user, add, true /* allowInstall */,
438 parentPackageName, childPackageNames);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700439 return p;
440 }
441
Kenny Root447106f2011-03-23 11:00:15 -0700442 PackageSetting peekPackageLPr(String name) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700443 return mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700444 }
445
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700446 void setInstallStatus(String pkgName, final int status) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700447 PackageSetting p = mPackages.get(pkgName);
448 if(p != null) {
449 if(p.getInstallStatus() != status) {
450 p.setInstallStatus(status);
451 }
452 }
453 }
454
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700455 void applyPendingPermissionGrantsLPw(String packageName, int userId) {
456 ArrayMap<String, ArraySet<RestoredPermissionGrant>> grantsByPackage =
457 mRestoredUserGrants.get(userId);
458 if (grantsByPackage == null || grantsByPackage.size() == 0) {
459 return;
460 }
461
462 ArraySet<RestoredPermissionGrant> grants = grantsByPackage.get(packageName);
463 if (grants == null || grants.size() == 0) {
464 return;
465 }
466
467 final PackageSetting ps = mPackages.get(packageName);
468 if (ps == null) {
469 Slog.e(TAG, "Can't find supposedly installed package " + packageName);
470 return;
471 }
472 final PermissionsState perms = ps.getPermissionsState();
473
474 for (RestoredPermissionGrant grant : grants) {
475 BasePermission bp = mPermissions.get(grant.permissionName);
476 if (bp != null) {
477 if (grant.granted) {
478 perms.grantRuntimePermission(bp, userId);
479 }
480 perms.updatePermissionFlags(bp, userId, USER_RUNTIME_GRANT_MASK, grant.grantBits);
481 }
482 }
483
484 // And remove it from the pending-grant bookkeeping
485 grantsByPackage.remove(packageName);
486 if (grantsByPackage.size() < 1) {
487 mRestoredUserGrants.remove(userId);
488 }
489 writeRuntimePermissionsForUserLPr(userId, false);
490 }
491
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -0700492 void setInstallerPackageName(String pkgName, String installerPkgName) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700493 PackageSetting p = mPackages.get(pkgName);
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -0700494 if (p != null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700495 p.setInstallerPackageName(installerPkgName);
496 }
497 }
498
Kenny Root447106f2011-03-23 11:00:15 -0700499 SharedUserSetting getSharedUserLPw(String name,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800500 int pkgFlags, int pkgPrivateFlags, boolean create) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700501 SharedUserSetting s = mSharedUsers.get(name);
502 if (s == null) {
503 if (!create) {
504 return null;
505 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800506 s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800507 s.userId = newUserIdLPw(s);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700508 Log.i(PackageManagerService.TAG, "New shared user " + name + ": id=" + s.userId);
509 // < 0 means we couldn't assign a userid; fall out and return
510 // s, which is currently null
511 if (s.userId >= 0) {
512 mSharedUsers.put(name, s);
513 }
514 }
515
516 return s;
517 }
518
Narayan Kamathdf6d6dc2014-04-28 14:18:34 +0100519 Collection<SharedUserSetting> getAllSharedUsersLPw() {
520 return mSharedUsers.values();
521 }
522
Svet Ganov354cd3c2015-12-17 11:35:04 -0800523 boolean disableSystemPackageLPw(String name, boolean replaced) {
Kenny Root447106f2011-03-23 11:00:15 -0700524 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700525 if(p == null) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700526 Log.w(PackageManagerService.TAG, "Package " + name + " is not an installed package");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700527 return false;
528 }
Kenny Root447106f2011-03-23 11:00:15 -0700529 final PackageSetting dp = mDisabledSysPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700530 // always make sure the system package code and resource paths dont change
Svet Ganov354cd3c2015-12-17 11:35:04 -0800531 if (dp == null && p.pkg != null && p.pkg.isSystemApp() && !p.pkg.isUpdatedSystemApp()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700532 if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
533 p.pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
534 }
535 mDisabledSysPackages.put(name, p);
536
Svet Ganov354cd3c2015-12-17 11:35:04 -0800537 if (replaced) {
538 // a little trick... when we install the new package, we don't
539 // want to modify the existing PackageSetting for the built-in
540 // version. so at this point we need a new PackageSetting that
541 // is okay to muck with.
542 PackageSetting newp = new PackageSetting(p);
543 replacePackageLPw(name, newp);
Svet Ganov354cd3c2015-12-17 11:35:04 -0800544 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700545 return true;
546 }
547 return false;
548 }
549
Kenny Root447106f2011-03-23 11:00:15 -0700550 PackageSetting enableSystemPackageLPw(String name) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700551 PackageSetting p = mDisabledSysPackages.get(name);
552 if(p == null) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700553 Log.w(PackageManagerService.TAG, "Package " + name + " is not disabled");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700554 return null;
555 }
556 // Reset flag in ApplicationInfo object
557 if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
558 p.pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
559 }
Kenny Root447106f2011-03-23 11:00:15 -0700560 PackageSetting ret = addPackageLPw(name, p.realName, p.codePath, p.resourcePath,
Narayan Kamathff110bd2014-07-04 18:30:45 +0100561 p.legacyNativeLibraryPathString, p.primaryCpuAbiString,
Narayan Kamath4903f642014-08-11 13:33:45 +0100562 p.secondaryCpuAbiString, p.secondaryCpuAbiString,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800563 p.appId, p.versionCode, p.pkgFlags, p.pkgPrivateFlags,
564 p.parentPackageName, p.childPackageNames);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700565 mDisabledSysPackages.remove(name);
566 return ret;
567 }
568
Kenny Rootc52d6fd2012-05-07 23:04:52 -0700569 boolean isDisabledSystemPackageLPr(String name) {
570 return mDisabledSysPackages.containsKey(name);
571 }
572
573 void removeDisabledSystemPackageLPw(String name) {
574 mDisabledSysPackages.remove(name);
575 }
576
Kenny Root447106f2011-03-23 11:00:15 -0700577 PackageSetting addPackageLPw(String name, String realName, File codePath, File resourcePath,
Svet Ganovf4ff39c2016-02-04 14:27:05 -0800578 String legacyNativeLibraryPathString, String primaryCpuAbiString,
579 String secondaryCpuAbiString, String cpuAbiOverrideString, int uid, int vc, int
580 pkgFlags, int pkgPrivateFlags, String parentPackageName,
581 List<String> childPackageNames) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700582 PackageSetting p = mPackages.get(name);
583 if (p != null) {
Amith Yamasani13593602012-03-22 16:16:17 -0700584 if (p.appId == uid) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700585 return p;
586 }
587 PackageManagerService.reportSettingsProblem(Log.ERROR,
588 "Adding duplicate package, keeping first: " + name);
589 return null;
590 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100591 p = new PackageSetting(name, realName, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100592 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800593 cpuAbiOverrideString, vc, pkgFlags, pkgPrivateFlags, parentPackageName,
594 childPackageNames);
Amith Yamasani13593602012-03-22 16:16:17 -0700595 p.appId = uid;
Kenny Root447106f2011-03-23 11:00:15 -0700596 if (addUserIdLPw(uid, p, name)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700597 mPackages.put(name, p);
598 return p;
599 }
600 return null;
601 }
602
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800603 SharedUserSetting addSharedUserLPw(String name, int uid, int pkgFlags, int pkgPrivateFlags) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700604 SharedUserSetting s = mSharedUsers.get(name);
605 if (s != null) {
606 if (s.userId == uid) {
607 return s;
608 }
609 PackageManagerService.reportSettingsProblem(Log.ERROR,
610 "Adding duplicate shared user, keeping first: " + name);
611 return null;
612 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800613 s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700614 s.userId = uid;
Kenny Root447106f2011-03-23 11:00:15 -0700615 if (addUserIdLPw(uid, s, name)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700616 mSharedUsers.put(name, s);
617 return s;
618 }
619 return null;
620 }
621
Christopher Tate86b391c2013-09-13 16:58:36 -0700622 void pruneSharedUsersLPw() {
623 ArrayList<String> removeStage = new ArrayList<String>();
624 for (Map.Entry<String,SharedUserSetting> entry : mSharedUsers.entrySet()) {
625 final SharedUserSetting sus = entry.getValue();
Todd Kennedy68f67152015-10-21 13:09:10 -0700626 if (sus == null) {
627 removeStage.add(entry.getKey());
628 continue;
629 }
630 // remove packages that are no longer installed
631 for (Iterator<PackageSetting> iter = sus.packages.iterator(); iter.hasNext();) {
632 PackageSetting ps = iter.next();
633 if (mPackages.get(ps.name) == null) {
634 iter.remove();
635 }
636 }
637 if (sus.packages.size() == 0) {
Christopher Tate86b391c2013-09-13 16:58:36 -0700638 removeStage.add(entry.getKey());
639 }
640 }
641 for (int i = 0; i < removeStage.size(); i++) {
642 mSharedUsers.remove(removeStage.get(i));
643 }
644 }
645
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700646 // Transfer ownership of permissions from one package to another.
Kenny Root447106f2011-03-23 11:00:15 -0700647 void transferPermissionsLPw(String origPkg, String newPkg) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700648 // Transfer ownership of permissions to the new package.
649 for (int i=0; i<2; i++) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700650 ArrayMap<String, BasePermission> permissions =
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700651 i == 0 ? mPermissionTrees : mPermissions;
652 for (BasePermission bp : permissions.values()) {
653 if (origPkg.equals(bp.sourcePackage)) {
654 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG,
655 "Moving permission " + bp.name
656 + " from pkg " + bp.sourcePackage
657 + " to " + newPkg);
658 bp.sourcePackage = newPkg;
659 bp.packageSetting = null;
660 bp.perm = null;
661 if (bp.pendingInfo != null) {
662 bp.pendingInfo.packageName = newPkg;
663 }
664 bp.uid = 0;
Jeff Sharkey00f39042015-03-23 16:51:22 -0700665 bp.setGids(null, false);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700666 }
667 }
668 }
669 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700670
Kenny Root447106f2011-03-23 11:00:15 -0700671 private PackageSetting getPackageLPw(String name, PackageSetting origPackage,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700672 String realName, SharedUserSetting sharedUser, File codePath, File resourcePath,
Svetoslavc6d1c342015-02-26 14:44:43 -0800673 String legacyNativeLibraryPathString, String primaryCpuAbiString,
674 String secondaryCpuAbiString, int vc, int pkgFlags, int pkgPrivateFlags,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800675 UserHandle installUser, boolean add, boolean allowInstall, String parentPackage,
676 List<String> childPackageNames) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700677 PackageSetting p = mPackages.get(name);
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700678 UserManagerService userManager = UserManagerService.getInstance();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700679 if (p != null) {
Narayan Kamathff110bd2014-07-04 18:30:45 +0100680 p.primaryCpuAbiString = primaryCpuAbiString;
681 p.secondaryCpuAbiString = secondaryCpuAbiString;
Svet Ganovf4ff39c2016-02-04 14:27:05 -0800682 if (childPackageNames != null) {
683 p.childPackageNames = new ArrayList<>(childPackageNames);
684 }
Narayan Kamath4903f642014-08-11 13:33:45 +0100685
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700686 if (!p.codePath.equals(codePath)) {
687 // Check to see if its a disabled system app
688 if ((p.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0) {
689 // This is an updated system app with versions in both system
690 // and data partition. Just let the most recent version
691 // take precedence.
Amith Yamasani483f3b02012-03-13 16:08:00 -0700692 Slog.w(PackageManagerService.TAG, "Trying to update system app code path from "
693 + p.codePathString + " to " + codePath.toString());
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700694 } else {
695 // Just a change in the code path is not an issue, but
696 // let's log a message about it.
Amith Yamasani483f3b02012-03-13 16:08:00 -0700697 Slog.i(PackageManagerService.TAG, "Package " + name + " codePath changed from "
698 + p.codePath + " to " + codePath + "; Retaining data and using new");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700699 /*
700 * Since we've changed paths, we need to prefer the new
701 * native library path over the one stored in the
702 * package settings since we might have moved from
703 * internal to external storage or vice versa.
704 */
Narayan Kamathff110bd2014-07-04 18:30:45 +0100705 p.legacyNativeLibraryPathString = legacyNativeLibraryPathString;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700706 }
707 }
708 if (p.sharedUser != sharedUser) {
709 PackageManagerService.reportSettingsProblem(Log.WARN,
710 "Package " + name + " shared user changed from "
711 + (p.sharedUser != null ? p.sharedUser.name : "<nothing>")
712 + " to "
713 + (sharedUser != null ? sharedUser.name : "<nothing>")
714 + "; replacing with new");
715 p = null;
716 } else {
Christopher Tate9f088202013-10-22 15:36:01 -0700717 // If what we are scanning is a system (and possibly privileged) package,
718 // then make it so, regardless of whether it was previously installed only
719 // in the data partition.
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800720 p.pkgFlags |= pkgFlags & ApplicationInfo.FLAG_SYSTEM;
721 p.pkgPrivateFlags |= pkgPrivateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700722 }
723 }
724 if (p == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700725 if (origPackage != null) {
726 // We are consuming the data from an existing package.
727 p = new PackageSetting(origPackage.name, name, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100728 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800729 null /* cpuAbiOverrideString */, vc, pkgFlags, pkgPrivateFlags,
730 parentPackage, childPackageNames);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700731 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG, "Package "
732 + name + " is adopting original package " + origPackage.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700733 // Note that we will retain the new package's signature so
734 // that we can keep its data.
735 PackageSignatures s = p.signatures;
736 p.copyFrom(origPackage);
737 p.signatures = s;
738 p.sharedUser = origPackage.sharedUser;
Amith Yamasani13593602012-03-22 16:16:17 -0700739 p.appId = origPackage.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700740 p.origPackage = origPackage;
Svet Ganove7af1942015-08-04 11:13:44 -0700741 p.getPermissionsState().copyFrom(origPackage.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700742 mRenamedPackages.put(name, origPackage.name);
743 name = origPackage.name;
744 // Update new package state.
745 p.setTimeStamp(codePath.lastModified());
746 } else {
747 p = new PackageSetting(name, realName, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100748 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Svet Ganov354cd3c2015-12-17 11:35:04 -0800749 null /* cpuAbiOverrideString */, vc, pkgFlags, pkgPrivateFlags,
750 parentPackage, childPackageNames);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700751 p.setTimeStamp(codePath.lastModified());
752 p.sharedUser = sharedUser;
753 // If this is not a system app, it starts out stopped.
754 if ((pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) {
Kenny Root447106f2011-03-23 11:00:15 -0700755 if (DEBUG_STOPPED) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700756 RuntimeException e = new RuntimeException("here");
757 e.fillInStackTrace();
758 Slog.i(PackageManagerService.TAG, "Stopping package " + name, e);
759 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700760 List<UserInfo> users = getAllUsers();
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700761 final int installUserId = installUser != null ? installUser.getIdentifier() : 0;
Amith Yamasanif031f232012-10-26 15:35:21 -0700762 if (users != null && allowInstall) {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700763 for (UserInfo user : users) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700764 // By default we consider this app to be installed
765 // for the user if no user has been specified (which
766 // means to leave it at its original value, and the
767 // original default value is true), or we are being
768 // asked to install for all users, or this is the
769 // user we are installing for.
770 final boolean installed = installUser == null
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700771 || (installUserId == UserHandle.USER_ALL
772 && !isAdbInstallDisallowed(userManager, user.id))
773 || installUserId == user.id;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700774 p.setUserState(user.id, COMPONENT_ENABLED_STATE_DEFAULT,
775 installed,
776 true, // stopped,
777 true, // notLaunched
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700778 false, // hidden
Andrei Stingaceanu1e283912015-11-26 15:26:28 +0000779 false, // suspended
Kenny Guyc13053b2014-05-29 14:17:17 +0100780 null, null, null,
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800781 false, // blockUninstall
Christopher Tatef0d6cb32015-07-10 17:44:53 -0700782 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED, 0);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700783 writePackageRestrictionsLPr(user.id);
784 }
785 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700786 }
787 if (sharedUser != null) {
Amith Yamasani13593602012-03-22 16:16:17 -0700788 p.appId = sharedUser.userId;
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800789 } else {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700790 // Clone the setting here for disabled system packages
791 PackageSetting dis = mDisabledSysPackages.get(name);
792 if (dis != null) {
793 // For disabled packages a new setting is created
794 // from the existing user id. This still has to be
795 // added to list of user id's
796 // Copy signatures from previous setting
797 if (dis.signatures.mSignatures != null) {
798 p.signatures.mSignatures = dis.signatures.mSignatures.clone();
799 }
Amith Yamasani13593602012-03-22 16:16:17 -0700800 p.appId = dis.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700801 // Clone permissions
Svetoslavc6d1c342015-02-26 14:44:43 -0800802 p.getPermissionsState().copyFrom(dis.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700803 // Clone component info
Amith Yamasani483f3b02012-03-13 16:08:00 -0700804 List<UserInfo> users = getAllUsers();
805 if (users != null) {
806 for (UserInfo user : users) {
807 int userId = user.id;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700808 p.setDisabledComponentsCopy(
809 dis.getDisabledComponents(userId), userId);
810 p.setEnabledComponentsCopy(
811 dis.getEnabledComponents(userId), userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700812 }
813 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700814 // Add new setting to list of user ids
Amith Yamasani13593602012-03-22 16:16:17 -0700815 addUserIdLPw(p.appId, p, name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700816 } else {
817 // Assign new user id
Amith Yamasani13593602012-03-22 16:16:17 -0700818 p.appId = newUserIdLPw(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700819 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700820 }
821 }
Amith Yamasani13593602012-03-22 16:16:17 -0700822 if (p.appId < 0) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700823 PackageManagerService.reportSettingsProblem(Log.WARN,
824 "Package " + name + " could not be assigned a valid uid");
825 return null;
826 }
827 if (add) {
828 // Finish adding new package by adding it and updating shared
829 // user preferences
Kenny Root447106f2011-03-23 11:00:15 -0700830 addPackageSettingLPw(p, name, sharedUser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700831 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700832 } else {
Amith Yamasanif031f232012-10-26 15:35:21 -0700833 if (installUser != null && allowInstall) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700834 // The caller has explicitly specified the user they want this
835 // package installed for, and the package already exists.
836 // Make sure it conforms to the new request.
837 List<UserInfo> users = getAllUsers();
838 if (users != null) {
839 for (UserInfo user : users) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700840 if ((installUser.getIdentifier() == UserHandle.USER_ALL
841 && !isAdbInstallDisallowed(userManager, user.id))
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700842 || installUser.getIdentifier() == user.id) {
843 boolean installed = p.getInstalled(user.id);
844 if (!installed) {
845 p.setInstalled(true, user.id);
846 writePackageRestrictionsLPr(user.id);
847 }
848 }
849 }
850 }
851 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700852 }
853 return p;
854 }
855
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700856 boolean isAdbInstallDisallowed(UserManagerService userManager, int userId) {
857 return userManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES,
858 userId);
859 }
860
Kenny Root447106f2011-03-23 11:00:15 -0700861 void insertPackageSettingLPw(PackageSetting p, PackageParser.Package pkg) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700862 p.pkg = pkg;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700863 // pkg.mSetEnabled = p.getEnabled(userId);
864 // pkg.mSetStopped = p.getStopped(userId);
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700865 final String volumeUuid = pkg.applicationInfo.volumeUuid;
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700866 final String codePath = pkg.applicationInfo.getCodePath();
867 final String resourcePath = pkg.applicationInfo.getResourcePath();
Jeff Sharkey84f12942014-07-10 17:48:11 -0700868 final String legacyNativeLibraryPath = pkg.applicationInfo.nativeLibraryRootDir;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700869 // Update volume if needed
870 if (!Objects.equals(volumeUuid, p.volumeUuid)) {
871 Slog.w(PackageManagerService.TAG, "Volume for " + p.pkg.packageName +
872 " changing from " + p.volumeUuid + " to " + volumeUuid);
873 p.volumeUuid = volumeUuid;
874 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700875 // Update code path if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700876 if (!Objects.equals(codePath, p.codePathString)) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700877 Slog.w(PackageManagerService.TAG, "Code path for " + p.pkg.packageName +
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700878 " changing from " + p.codePathString + " to " + codePath);
879 p.codePath = new File(codePath);
880 p.codePathString = codePath;
881 }
882 //Update resource path if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700883 if (!Objects.equals(resourcePath, p.resourcePathString)) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700884 Slog.w(PackageManagerService.TAG, "Resource path for " + p.pkg.packageName +
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700885 " changing from " + p.resourcePathString + " to " + resourcePath);
886 p.resourcePath = new File(resourcePath);
887 p.resourcePathString = resourcePath;
888 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100889 // Update the native library paths if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700890 if (!Objects.equals(legacyNativeLibraryPath, p.legacyNativeLibraryPathString)) {
891 p.legacyNativeLibraryPathString = legacyNativeLibraryPath;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700892 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100893
Narayan Kamath9e289d72014-04-10 09:26:59 +0000894 // Update the required Cpu Abi
Narayan Kamathff110bd2014-07-04 18:30:45 +0100895 p.primaryCpuAbiString = pkg.applicationInfo.primaryCpuAbi;
896 p.secondaryCpuAbiString = pkg.applicationInfo.secondaryCpuAbi;
Narayan Kamath4903f642014-08-11 13:33:45 +0100897 p.cpuAbiOverrideString = pkg.cpuAbiOverride;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700898 // Update version code if needed
Amith Yamasani483f3b02012-03-13 16:08:00 -0700899 if (pkg.mVersionCode != p.versionCode) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700900 p.versionCode = pkg.mVersionCode;
901 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700902 // Update signatures if needed.
903 if (p.signatures.mSignatures == null) {
904 p.signatures.assignSignatures(pkg.mSignatures);
905 }
Kenny Root5455f682012-09-09 14:52:10 -0700906 // Update flags if needed.
907 if (pkg.applicationInfo.flags != p.pkgFlags) {
908 p.pkgFlags = pkg.applicationInfo.flags;
909 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700910 // If this app defines a shared user id initialize
911 // the shared user signatures as well.
912 if (p.sharedUser != null && p.sharedUser.signatures.mSignatures == null) {
913 p.sharedUser.signatures.assignSignatures(pkg.mSignatures);
914 }
Kenny Root447106f2011-03-23 11:00:15 -0700915 addPackageSettingLPw(p, pkg.packageName, p.sharedUser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700916 }
917
918 // Utility method that adds a PackageSetting to mPackages and
Christopher Tate6038d152015-06-17 13:07:46 -0700919 // completes updating the shared user attributes and any restored
920 // app link verification state
Kenny Root447106f2011-03-23 11:00:15 -0700921 private void addPackageSettingLPw(PackageSetting p, String name,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700922 SharedUserSetting sharedUser) {
923 mPackages.put(name, p);
924 if (sharedUser != null) {
925 if (p.sharedUser != null && p.sharedUser != sharedUser) {
926 PackageManagerService.reportSettingsProblem(Log.ERROR,
927 "Package " + p.name + " was user "
928 + p.sharedUser + " but is now " + sharedUser
929 + "; I am not changing its files so it will probably fail!");
Ben Gruverdd72c9e2013-08-06 12:34:17 -0700930 p.sharedUser.removePackage(p);
Amith Yamasani13593602012-03-22 16:16:17 -0700931 } else if (p.appId != sharedUser.userId) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700932 PackageManagerService.reportSettingsProblem(Log.ERROR,
Amith Yamasani13593602012-03-22 16:16:17 -0700933 "Package " + p.name + " was user id " + p.appId
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700934 + " but is now user " + sharedUser
935 + " with id " + sharedUser.userId
936 + "; I am not changing its files so it will probably fail!");
937 }
938
Ben Gruverdd72c9e2013-08-06 12:34:17 -0700939 sharedUser.addPackage(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700940 p.sharedUser = sharedUser;
Amith Yamasani13593602012-03-22 16:16:17 -0700941 p.appId = sharedUser.userId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700942 }
Svet Ganove7af1942015-08-04 11:13:44 -0700943
944 // If the we know about this user id, we have to update it as it
945 // has to point to the same PackageSetting instance as the package.
946 Object userIdPs = getUserIdLPr(p.appId);
947 if (sharedUser == null) {
948 if (userIdPs != null && userIdPs != p) {
949 replaceUserIdLPw(p.appId, p);
950 }
951 } else {
952 if (userIdPs != null && userIdPs != sharedUser) {
953 replaceUserIdLPw(p.appId, sharedUser);
954 }
955 }
956
Christopher Tate6038d152015-06-17 13:07:46 -0700957 IntentFilterVerificationInfo ivi = mRestoredIntentFilterVerifications.get(name);
958 if (ivi != null) {
959 if (DEBUG_DOMAIN_VERIFICATION) {
960 Slog.i(TAG, "Applying restored IVI for " + name + " : " + ivi.getStatusString());
961 }
962 mRestoredIntentFilterVerifications.remove(name);
963 p.setIntentFilterVerificationInfo(ivi);
964 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700965 }
966
967 /*
968 * Update the shared user setting when a package using
969 * specifying the shared user id is removed. The gids
970 * associated with each permission of the deleted package
971 * are removed from the shared user's gid list only if its
972 * not in use by other permissions of packages in the
973 * shared user setting.
974 */
Svetoslavc6d1c342015-02-26 14:44:43 -0800975 int updateSharedUserPermsLPw(PackageSetting deletedPs, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -0700976 if ((deletedPs == null) || (deletedPs.pkg == null)) {
977 Slog.i(PackageManagerService.TAG,
978 "Trying to update info for null package. Just ignoring");
Svetoslavc6d1c342015-02-26 14:44:43 -0800979 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700980 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800981
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700982 // No sharedUserId
983 if (deletedPs.sharedUser == null) {
Svetoslavc6d1c342015-02-26 14:44:43 -0800984 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700985 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800986
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700987 SharedUserSetting sus = deletedPs.sharedUser;
Svetoslavc6d1c342015-02-26 14:44:43 -0800988
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700989 // Update permissions
Kenny Root447106f2011-03-23 11:00:15 -0700990 for (String eachPerm : deletedPs.pkg.requestedPermissions) {
Svetoslavc6d1c342015-02-26 14:44:43 -0800991 BasePermission bp = mPermissions.get(eachPerm);
992 if (bp == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700993 continue;
994 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800995
Svetoslavc6d1c342015-02-26 14:44:43 -0800996 // Check if another package in the shared user needs the permission.
Svetoslav4a5f4a22015-07-07 18:18:15 -0700997 boolean used = false;
Svetoslavc6d1c342015-02-26 14:44:43 -0800998 for (PackageSetting pkg : sus.packages) {
999 if (pkg.pkg != null
1000 && !pkg.pkg.packageName.equals(deletedPs.pkg.packageName)
1001 && pkg.pkg.requestedPermissions.contains(eachPerm)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001002 used = true;
1003 break;
1004 }
1005 }
Svetoslav4a5f4a22015-07-07 18:18:15 -07001006 if (used) {
1007 continue;
1008 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001009
Svetoslav4a5f4a22015-07-07 18:18:15 -07001010 PermissionsState permissionsState = sus.getPermissionsState();
1011 PackageSetting disabledPs = getDisabledSystemPkgLPr(deletedPs.pkg.packageName);
Svet Ganov8c7f7002015-05-07 10:48:44 -07001012
Svetoslav4a5f4a22015-07-07 18:18:15 -07001013 // If the package is shadowing is a disabled system package,
1014 // do not drop permissions that the shadowed package requests.
1015 if (disabledPs != null) {
1016 boolean reqByDisabledSysPkg = false;
1017 for (String permission : disabledPs.pkg.requestedPermissions) {
1018 if (permission.equals(eachPerm)) {
1019 reqByDisabledSysPkg = true;
1020 break;
1021 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001022 }
Svetoslav4a5f4a22015-07-07 18:18:15 -07001023 if (reqByDisabledSysPkg) {
1024 continue;
Svetoslavc6d1c342015-02-26 14:44:43 -08001025 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001026 }
Svetoslav4a5f4a22015-07-07 18:18:15 -07001027
1028 // Try to revoke as an install permission which is for all users.
1029 // The package is gone - no need to keep flags for applying policy.
1030 permissionsState.updatePermissionFlags(bp, userId,
1031 PackageManager.MASK_PERMISSION_FLAGS, 0);
1032
1033 if (permissionsState.revokeInstallPermission(bp) ==
1034 PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED) {
1035 return UserHandle.USER_ALL;
1036 }
1037
1038 // Try to revoke as an install permission which is per user.
1039 if (permissionsState.revokeRuntimePermission(bp, userId) ==
1040 PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED) {
1041 return userId;
1042 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001043 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001044
1045 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001046 }
1047
Kenny Root447106f2011-03-23 11:00:15 -07001048 int removePackageLPw(String name) {
1049 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001050 if (p != null) {
1051 mPackages.remove(name);
1052 if (p.sharedUser != null) {
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001053 p.sharedUser.removePackage(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001054 if (p.sharedUser.packages.size() == 0) {
1055 mSharedUsers.remove(p.sharedUser.name);
Kenny Root447106f2011-03-23 11:00:15 -07001056 removeUserIdLPw(p.sharedUser.userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001057 return p.sharedUser.userId;
1058 }
1059 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07001060 removeUserIdLPw(p.appId);
1061 return p.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001062 }
1063 }
1064 return -1;
1065 }
1066
Kenny Root447106f2011-03-23 11:00:15 -07001067 private void replacePackageLPw(String name, PackageSetting newp) {
1068 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001069 if (p != null) {
1070 if (p.sharedUser != null) {
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001071 p.sharedUser.removePackage(p);
1072 p.sharedUser.addPackage(newp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001073 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07001074 replaceUserIdLPw(p.appId, newp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001075 }
1076 }
1077 mPackages.put(name, newp);
1078 }
1079
Kenny Root447106f2011-03-23 11:00:15 -07001080 private boolean addUserIdLPw(int uid, Object obj, Object name) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001081 if (uid > Process.LAST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001082 return false;
1083 }
1084
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001085 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001086 int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001087 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001088 while (index >= N) {
1089 mUserIds.add(null);
1090 N++;
1091 }
1092 if (mUserIds.get(index) != null) {
1093 PackageManagerService.reportSettingsProblem(Log.ERROR,
1094 "Adding duplicate user id: " + uid
1095 + " name=" + name);
1096 return false;
1097 }
1098 mUserIds.set(index, obj);
1099 } else {
1100 if (mOtherUserIds.get(uid) != null) {
1101 PackageManagerService.reportSettingsProblem(Log.ERROR,
1102 "Adding duplicate shared id: " + uid
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001103 + " name=" + name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001104 return false;
1105 }
1106 mOtherUserIds.put(uid, obj);
1107 }
1108 return true;
1109 }
1110
Kenny Root447106f2011-03-23 11:00:15 -07001111 public Object getUserIdLPr(int uid) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001112 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001113 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001114 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001115 return index < N ? mUserIds.get(index) : null;
1116 } else {
1117 return mOtherUserIds.get(uid);
1118 }
1119 }
1120
Kenny Root447106f2011-03-23 11:00:15 -07001121 private void removeUserIdLPw(int uid) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001122 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001123 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001124 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001125 if (index < N) mUserIds.set(index, null);
1126 } else {
1127 mOtherUserIds.remove(uid);
1128 }
Geremy Condra12c18382013-03-06 16:49:06 -08001129 setFirstAvailableUid(uid+1);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001130 }
1131
Kenny Root447106f2011-03-23 11:00:15 -07001132 private void replaceUserIdLPw(int uid, Object obj) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001133 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001134 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08001135 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001136 if (index < N) mUserIds.set(index, obj);
1137 } else {
1138 mOtherUserIds.put(uid, obj);
1139 }
1140 }
1141
Dianne Hackborn63092712012-10-07 14:45:35 -07001142 PreferredIntentResolver editPreferredActivitiesLPw(int userId) {
1143 PreferredIntentResolver pir = mPreferredActivities.get(userId);
1144 if (pir == null) {
1145 pir = new PreferredIntentResolver();
1146 mPreferredActivities.put(userId, pir);
1147 }
1148 return pir;
1149 }
1150
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001151 PersistentPreferredIntentResolver editPersistentPreferredActivitiesLPw(int userId) {
1152 PersistentPreferredIntentResolver ppir = mPersistentPreferredActivities.get(userId);
1153 if (ppir == null) {
1154 ppir = new PersistentPreferredIntentResolver();
1155 mPersistentPreferredActivities.put(userId, ppir);
1156 }
1157 return ppir;
1158 }
1159
Nicolas Prevot81948992014-05-16 18:25:26 +01001160 CrossProfileIntentResolver editCrossProfileIntentResolverLPw(int userId) {
1161 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(userId);
1162 if (cpir == null) {
1163 cpir = new CrossProfileIntentResolver();
1164 mCrossProfileIntentResolvers.put(userId, cpir);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001165 }
Nicolas Prevot81948992014-05-16 18:25:26 +01001166 return cpir;
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001167 }
1168
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001169 /**
1170 * The following functions suppose that you have a lock for managing access to the
1171 * mIntentFiltersVerifications map.
1172 */
1173
1174 /* package protected */
1175 IntentFilterVerificationInfo getIntentFilterVerificationLPr(String packageName) {
1176 PackageSetting ps = mPackages.get(packageName);
1177 if (ps == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001178 if (DEBUG_DOMAIN_VERIFICATION) {
1179 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1180 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001181 return null;
1182 }
1183 return ps.getIntentFilterVerificationInfo();
1184 }
1185
1186 /* package protected */
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -07001187 IntentFilterVerificationInfo createIntentFilterVerificationIfNeededLPw(String packageName,
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001188 ArrayList<String> domains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001189 PackageSetting ps = mPackages.get(packageName);
1190 if (ps == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001191 if (DEBUG_DOMAIN_VERIFICATION) {
1192 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1193 }
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -07001194 return null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001195 }
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -07001196 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
1197 if (ivi == null) {
1198 ivi = new IntentFilterVerificationInfo(packageName, domains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001199 ps.setIntentFilterVerificationInfo(ivi);
Christopher Tate72c10a22015-06-12 18:31:24 -07001200 if (DEBUG_DOMAIN_VERIFICATION) {
1201 Slog.d(PackageManagerService.TAG,
1202 "Creating new IntentFilterVerificationInfo for pkg: " + packageName);
1203 }
Fabrice Di Meglio1de3f0d2015-04-29 19:42:41 -07001204 } else {
1205 ivi.setDomains(domains);
Christopher Tate72c10a22015-06-12 18:31:24 -07001206 if (DEBUG_DOMAIN_VERIFICATION) {
1207 Slog.d(PackageManagerService.TAG,
1208 "Setting domains to existing IntentFilterVerificationInfo for pkg: " +
1209 packageName + " and with domains: " + ivi.getDomainsString());
1210 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001211 }
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -07001212 return ivi;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001213 }
1214
1215 int getIntentFilterVerificationStatusLPr(String packageName, int userId) {
1216 PackageSetting ps = mPackages.get(packageName);
1217 if (ps == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001218 if (DEBUG_DOMAIN_VERIFICATION) {
1219 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1220 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001221 return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1222 }
Christopher Tated85a6642015-07-17 15:39:08 -07001223 return (int)(ps.getDomainVerificationStatusForUser(userId) >> 32);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001224 }
1225
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001226 boolean updateIntentFilterVerificationStatusLPw(String packageName, final int status, int userId) {
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001227 // Update the status for the current package
1228 PackageSetting current = mPackages.get(packageName);
1229 if (current == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001230 if (DEBUG_DOMAIN_VERIFICATION) {
1231 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1232 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001233 return false;
1234 }
Christopher Tate050aee22015-07-01 16:50:43 -07001235
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001236 final int alwaysGeneration;
1237 if (status == INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS) {
1238 alwaysGeneration = mNextAppLinkGeneration.get(userId) + 1;
1239 mNextAppLinkGeneration.put(userId, alwaysGeneration);
1240 } else {
1241 alwaysGeneration = 0;
1242 }
1243
1244 current.setDomainVerificationStatusForUser(status, alwaysGeneration, userId);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001245 return true;
1246 }
1247
1248 /**
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001249 * Used for Settings App and PackageManagerService dump. Should be read only.
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001250 */
1251 List<IntentFilterVerificationInfo> getIntentFilterVerificationsLPr(
1252 String packageName) {
1253 if (packageName == null) {
1254 return Collections.<IntentFilterVerificationInfo>emptyList();
1255 }
1256 ArrayList<IntentFilterVerificationInfo> result = new ArrayList<>();
1257 for (PackageSetting ps : mPackages.values()) {
1258 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
1259 if (ivi == null || TextUtils.isEmpty(ivi.getPackageName()) ||
1260 !ivi.getPackageName().equalsIgnoreCase(packageName)) {
1261 continue;
1262 }
1263 result.add(ivi);
1264 }
1265 return result;
1266 }
1267
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001268 boolean removeIntentFilterVerificationLPw(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001269 PackageSetting ps = mPackages.get(packageName);
1270 if (ps == null) {
Christopher Tate72c10a22015-06-12 18:31:24 -07001271 if (DEBUG_DOMAIN_VERIFICATION) {
1272 Slog.w(PackageManagerService.TAG, "No package known: " + packageName);
1273 }
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001274 return false;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001275 }
1276 ps.clearDomainVerificationStatusForUser(userId);
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001277 return true;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001278 }
1279
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001280 boolean removeIntentFilterVerificationLPw(String packageName, int[] userIds) {
1281 boolean result = false;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001282 for (int userId : userIds) {
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001283 result |= removeIntentFilterVerificationLPw(packageName, userId);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001284 }
Fabrice Di Meglio1f09b8c2015-05-06 18:51:21 -07001285 return result;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001286 }
1287
Christopher Tatedb3fe812015-06-24 16:15:48 -07001288 boolean setDefaultBrowserPackageNameLPw(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001289 if (userId == UserHandle.USER_ALL) {
1290 return false;
1291 }
1292 mDefaultBrowserApp.put(userId, packageName);
1293 writePackageRestrictionsLPr(userId);
1294 return true;
1295 }
1296
1297 String getDefaultBrowserPackageNameLPw(int userId) {
1298 return (userId == UserHandle.USER_ALL) ? null : mDefaultBrowserApp.get(userId);
1299 }
1300
Amith Yamasani483f3b02012-03-13 16:08:00 -07001301 private File getUserPackagesStateFile(int userId) {
Svetoslavc6d1c342015-02-26 14:44:43 -08001302 // TODO: Implement a cleaner solution when adding tests.
1303 // This instead of Environment.getUserSystemDirectory(userId) to support testing.
1304 File userDir = new File(new File(mSystemDir, "users"), Integer.toString(userId));
1305 return new File(userDir, "package-restrictions.xml");
1306 }
1307
1308 private File getUserRuntimePermissionsFile(int userId) {
1309 // TODO: Implement a cleaner solution when adding tests.
1310 // This instead of Environment.getUserSystemDirectory(userId) to support testing.
1311 File userDir = new File(new File(mSystemDir, "users"), Integer.toString(userId));
1312 return new File(userDir, RUNTIME_PERMISSIONS_FILE_NAME);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001313 }
1314
1315 private File getUserPackagesStateBackupFile(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -07001316 return new File(Environment.getUserSystemDirectory(userId),
1317 "package-restrictions-backup.xml");
Amith Yamasani483f3b02012-03-13 16:08:00 -07001318 }
1319
1320 void writeAllUsersPackageRestrictionsLPr() {
1321 List<UserInfo> users = getAllUsers();
1322 if (users == null) return;
1323
1324 for (UserInfo user : users) {
1325 writePackageRestrictionsLPr(user.id);
1326 }
1327 }
1328
Svetoslavc6d1c342015-02-26 14:44:43 -08001329 void writeAllRuntimePermissionsLPr() {
1330 for (int userId : UserManagerService.getInstance().getUserIds()) {
1331 mRuntimePermissionsPersistence.writePermissionsForUserAsyncLPr(userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001332 }
1333 }
1334
Svet Ganovba3ba812015-06-26 10:54:06 -07001335 boolean areDefaultRuntimePermissionsGrantedLPr(int userId) {
1336 return mRuntimePermissionsPersistence
1337 .areDefaultRuntimPermissionsGrantedLPr(userId);
1338 }
1339
1340 void onDefaultRuntimePermissionsGrantedLPr(int userId) {
1341 mRuntimePermissionsPersistence
1342 .onDefaultRuntimePermissionsGrantedLPr(userId);
1343 }
1344
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001345 public VersionInfo findOrCreateVersion(String volumeUuid) {
1346 VersionInfo ver = mVersion.get(volumeUuid);
1347 if (ver == null) {
1348 ver = new VersionInfo();
1349 ver.forceCurrent();
1350 mVersion.put(volumeUuid, ver);
1351 }
1352 return ver;
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001353 }
1354
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001355 public VersionInfo getInternalVersion() {
1356 return mVersion.get(StorageManager.UUID_PRIVATE_INTERNAL);
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001357 }
1358
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001359 public VersionInfo getExternalVersion() {
1360 return mVersion.get(StorageManager.UUID_PRIMARY_PHYSICAL);
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001361 }
1362
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07001363 public void onVolumeForgotten(String fsUuid) {
1364 mVersion.remove(fsUuid);
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001365 }
1366
Christopher Tatee012a232015-04-01 17:18:50 -07001367 /**
1368 * Applies the preferred activity state described by the given XML. This code
1369 * also supports the restore-from-backup code path.
1370 *
1371 * @see PreferredActivityBackupHelper
1372 */
1373 void readPreferredActivitiesLPw(XmlPullParser parser, int userId)
Dianne Hackborn63092712012-10-07 14:45:35 -07001374 throws XmlPullParserException, IOException {
1375 int outerDepth = parser.getDepth();
1376 int type;
1377 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1378 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1379 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1380 continue;
1381 }
1382
1383 String tagName = parser.getName();
1384 if (tagName.equals(TAG_ITEM)) {
1385 PreferredActivity pa = new PreferredActivity(parser);
1386 if (pa.mPref.getParseError() == null) {
1387 editPreferredActivitiesLPw(userId).addFilter(pa);
1388 } else {
1389 PackageManagerService.reportSettingsProblem(Log.WARN,
1390 "Error in package manager settings: <preferred-activity> "
1391 + pa.mPref.getParseError() + " at "
1392 + parser.getPositionDescription());
1393 }
1394 } else {
1395 PackageManagerService.reportSettingsProblem(Log.WARN,
1396 "Unknown element under <preferred-activities>: " + parser.getName());
1397 XmlUtils.skipCurrentTag(parser);
1398 }
1399 }
1400 }
1401
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001402 private void readPersistentPreferredActivitiesLPw(XmlPullParser parser, int userId)
1403 throws XmlPullParserException, IOException {
1404 int outerDepth = parser.getDepth();
1405 int type;
1406 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1407 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1408 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1409 continue;
1410 }
1411 String tagName = parser.getName();
1412 if (tagName.equals(TAG_ITEM)) {
1413 PersistentPreferredActivity ppa = new PersistentPreferredActivity(parser);
1414 editPersistentPreferredActivitiesLPw(userId).addFilter(ppa);
1415 } else {
1416 PackageManagerService.reportSettingsProblem(Log.WARN,
Sander Alewijnseaf597622014-03-20 18:44:57 +00001417 "Unknown element under <" + TAG_PERSISTENT_PREFERRED_ACTIVITIES + ">: "
1418 + parser.getName());
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001419 XmlUtils.skipCurrentTag(parser);
1420 }
1421 }
1422 }
1423
Nicolas Prevot81948992014-05-16 18:25:26 +01001424 private void readCrossProfileIntentFiltersLPw(XmlPullParser parser, int userId)
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001425 throws XmlPullParserException, IOException {
1426 int outerDepth = parser.getDepth();
1427 int type;
1428 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1429 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1430 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1431 continue;
1432 }
Christopher Tate6038d152015-06-17 13:07:46 -07001433 final String tagName = parser.getName();
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001434 if (tagName.equals(TAG_ITEM)) {
Nicolas Prevot81948992014-05-16 18:25:26 +01001435 CrossProfileIntentFilter cpif = new CrossProfileIntentFilter(parser);
1436 editCrossProfileIntentResolverLPw(userId).addFilter(cpif);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001437 } else {
Nicolas Prevot81948992014-05-16 18:25:26 +01001438 String msg = "Unknown element under " + TAG_CROSS_PROFILE_INTENT_FILTERS + ": " +
Christopher Tate6038d152015-06-17 13:07:46 -07001439 tagName;
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001440 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
1441 XmlUtils.skipCurrentTag(parser);
1442 }
1443 }
1444 }
1445
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001446 private void readDomainVerificationLPw(XmlPullParser parser, PackageSettingBase packageSetting)
1447 throws XmlPullParserException, IOException {
1448 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser);
1449 packageSetting.setIntentFilterVerificationInfo(ivi);
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001450 Log.d(TAG, "Read domain verification for package: " + ivi.getPackageName());
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001451 }
1452
Christopher Tate6038d152015-06-17 13:07:46 -07001453 private void readRestoredIntentFilterVerifications(XmlPullParser parser)
1454 throws XmlPullParserException, IOException {
1455 int outerDepth = parser.getDepth();
1456 int type;
1457 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1458 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1459 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1460 continue;
1461 }
1462 final String tagName = parser.getName();
1463 if (tagName.equals(TAG_DOMAIN_VERIFICATION)) {
1464 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser);
1465 if (DEBUG_DOMAIN_VERIFICATION) {
1466 Slog.i(TAG, "Restored IVI for " + ivi.getPackageName()
1467 + " status=" + ivi.getStatusString());
1468 }
1469 mRestoredIntentFilterVerifications.put(ivi.getPackageName(), ivi);
1470 } else {
1471 Slog.w(TAG, "Unknown element: " + tagName);
1472 XmlUtils.skipCurrentTag(parser);
1473 }
1474 }
1475 }
1476
1477 void readDefaultAppsLPw(XmlPullParser parser, int userId)
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001478 throws XmlPullParserException, IOException {
1479 int outerDepth = parser.getDepth();
1480 int type;
1481 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1482 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1483 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1484 continue;
1485 }
1486 String tagName = parser.getName();
1487 if (tagName.equals(TAG_DEFAULT_BROWSER)) {
1488 String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
1489 mDefaultBrowserApp.put(userId, packageName);
1490 } else {
1491 String msg = "Unknown element under " + TAG_DEFAULT_APPS + ": " +
1492 parser.getName();
1493 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
1494 XmlUtils.skipCurrentTag(parser);
1495 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001496 }
1497 }
1498
Amith Yamasani483f3b02012-03-13 16:08:00 -07001499 void readPackageRestrictionsLPr(int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001500 if (DEBUG_MU) {
1501 Log.i(TAG, "Reading package restrictions for user=" + userId);
1502 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07001503 FileInputStream str = null;
1504 File userPackagesStateFile = getUserPackagesStateFile(userId);
1505 File backupFile = getUserPackagesStateBackupFile(userId);
1506 if (backupFile.exists()) {
1507 try {
1508 str = new FileInputStream(backupFile);
1509 mReadMessages.append("Reading from backup stopped packages file\n");
1510 PackageManagerService.reportSettingsProblem(Log.INFO,
1511 "Need to read from backup stopped packages file");
1512 if (userPackagesStateFile.exists()) {
1513 // If both the backup and normal file exist, we
1514 // ignore the normal one since it might have been
1515 // corrupted.
1516 Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file "
1517 + userPackagesStateFile);
1518 userPackagesStateFile.delete();
1519 }
1520 } catch (java.io.IOException e) {
1521 // We'll try for the normal settings file.
1522 }
1523 }
1524
1525 try {
1526 if (str == null) {
1527 if (!userPackagesStateFile.exists()) {
1528 mReadMessages.append("No stopped packages file found\n");
1529 PackageManagerService.reportSettingsProblem(Log.INFO,
1530 "No stopped packages file; "
1531 + "assuming all started");
1532 // At first boot, make sure no packages are stopped.
1533 // We usually want to have third party apps initialize
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001534 // in the stopped state, but not at first boot. Also
1535 // consider all applications to be installed.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001536 for (PackageSetting pkg : mPackages.values()) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001537 pkg.setUserState(userId, COMPONENT_ENABLED_STATE_DEFAULT,
1538 true, // installed
1539 false, // stopped
1540 false, // notLaunched
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001541 false, // hidden
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001542 false, // suspended
Kenny Guyc13053b2014-05-29 14:17:17 +01001543 null, null, null,
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001544 false, // blockUninstall
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001545 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED, 0);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001546 }
1547 return;
1548 }
1549 str = new FileInputStream(userPackagesStateFile);
1550 }
1551 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001552 parser.setInput(str, StandardCharsets.UTF_8.name());
Amith Yamasani483f3b02012-03-13 16:08:00 -07001553
1554 int type;
1555 while ((type=parser.next()) != XmlPullParser.START_TAG
1556 && type != XmlPullParser.END_DOCUMENT) {
1557 ;
1558 }
1559
1560 if (type != XmlPullParser.START_TAG) {
1561 mReadMessages.append("No start tag found in package restrictions file\n");
1562 PackageManagerService.reportSettingsProblem(Log.WARN,
1563 "No start tag found in package manager stopped packages");
1564 return;
1565 }
1566
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001567 int maxAppLinkGeneration = 0;
1568
Amith Yamasani483f3b02012-03-13 16:08:00 -07001569 int outerDepth = parser.getDepth();
1570 PackageSetting ps = null;
1571 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1572 && (type != XmlPullParser.END_TAG
1573 || parser.getDepth() > outerDepth)) {
1574 if (type == XmlPullParser.END_TAG
1575 || type == XmlPullParser.TEXT) {
1576 continue;
1577 }
1578
1579 String tagName = parser.getName();
1580 if (tagName.equals(TAG_PACKAGE)) {
1581 String name = parser.getAttributeValue(null, ATTR_NAME);
1582 ps = mPackages.get(name);
1583 if (ps == null) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001584 Slog.w(PackageManagerService.TAG, "No package known for stopped package "
Amith Yamasani483f3b02012-03-13 16:08:00 -07001585 + name);
1586 XmlUtils.skipCurrentTag(parser);
1587 continue;
1588 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001589 final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED);
1590 final int enabled = enabledStr == null
1591 ? COMPONENT_ENABLED_STATE_DEFAULT : Integer.parseInt(enabledStr);
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001592 final String enabledCaller = parser.getAttributeValue(null,
1593 ATTR_ENABLED_CALLER);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001594 final String installedStr = parser.getAttributeValue(null, ATTR_INSTALLED);
1595 final boolean installed = installedStr == null
1596 ? true : Boolean.parseBoolean(installedStr);
1597 final String stoppedStr = parser.getAttributeValue(null, ATTR_STOPPED);
1598 final boolean stopped = stoppedStr == null
1599 ? false : Boolean.parseBoolean(stoppedStr);
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001600 // For backwards compatibility with the previous name of "blocked", which
1601 // now means hidden, read the old attribute as well.
Amith Yamasani655d0e22013-06-12 14:19:10 -07001602 final String blockedStr = parser.getAttributeValue(null, ATTR_BLOCKED);
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001603 boolean hidden = blockedStr == null
Amith Yamasani655d0e22013-06-12 14:19:10 -07001604 ? false : Boolean.parseBoolean(blockedStr);
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001605 final String hiddenStr = parser.getAttributeValue(null, ATTR_HIDDEN);
1606 hidden = hiddenStr == null
1607 ? hidden : Boolean.parseBoolean(hiddenStr);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001608 final String suspendedStr = parser.getAttributeValue(null, ATTR_SUSPENDED);
1609 final boolean suspended = suspendedStr == null
1610 ? false : Boolean.parseBoolean(suspendedStr);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001611 final String notLaunchedStr = parser.getAttributeValue(null, ATTR_NOT_LAUNCHED);
1612 final boolean notLaunched = stoppedStr == null
1613 ? false : Boolean.parseBoolean(notLaunchedStr);
Kenny Guyc13053b2014-05-29 14:17:17 +01001614 final String blockUninstallStr = parser.getAttributeValue(null,
1615 ATTR_BLOCK_UNINSTALL);
1616 final boolean blockUninstall = blockUninstallStr == null
1617 ? false : Boolean.parseBoolean(blockUninstallStr);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001618
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001619 final String verifStateStr =
1620 parser.getAttributeValue(null, ATTR_DOMAIN_VERIFICATON_STATE);
1621 final int verifState = (verifStateStr == null) ?
1622 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED :
1623 Integer.parseInt(verifStateStr);
1624
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001625 final String linkGenStr = parser.getAttributeValue(null, ATTR_APP_LINK_GENERATION);
1626 final int linkGeneration = linkGenStr == null ? 0 : Integer.parseInt(linkGenStr);
1627 if (linkGeneration > maxAppLinkGeneration) {
1628 maxAppLinkGeneration = linkGeneration;
1629 }
1630
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001631 ArraySet<String> enabledComponents = null;
1632 ArraySet<String> disabledComponents = null;
Amith Yamasani483f3b02012-03-13 16:08:00 -07001633
1634 int packageDepth = parser.getDepth();
1635 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1636 && (type != XmlPullParser.END_TAG
1637 || parser.getDepth() > packageDepth)) {
1638 if (type == XmlPullParser.END_TAG
1639 || type == XmlPullParser.TEXT) {
1640 continue;
1641 }
1642 tagName = parser.getName();
1643 if (tagName.equals(TAG_ENABLED_COMPONENTS)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001644 enabledComponents = readComponentsLPr(parser);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001645 } else if (tagName.equals(TAG_DISABLED_COMPONENTS)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001646 disabledComponents = readComponentsLPr(parser);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001647 }
1648 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001649
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001650 ps.setUserState(userId, enabled, installed, stopped, notLaunched, hidden,
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001651 suspended, enabledCaller, enabledComponents, disabledComponents,
1652 blockUninstall, verifState, linkGeneration);
Dianne Hackborn63092712012-10-07 14:45:35 -07001653 } else if (tagName.equals("preferred-activities")) {
1654 readPreferredActivitiesLPw(parser, userId);
Sander Alewijnseaf597622014-03-20 18:44:57 +00001655 } else if (tagName.equals(TAG_PERSISTENT_PREFERRED_ACTIVITIES)) {
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001656 readPersistentPreferredActivitiesLPw(parser, userId);
Nicolas Prevot29762c32014-07-29 18:51:32 +01001657 } else if (tagName.equals(TAG_CROSS_PROFILE_INTENT_FILTERS)) {
Nicolas Prevot81948992014-05-16 18:25:26 +01001658 readCrossProfileIntentFiltersLPw(parser, userId);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001659 } else if (tagName.equals(TAG_DEFAULT_APPS)) {
1660 readDefaultAppsLPw(parser, userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001661 } else {
1662 Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: "
1663 + parser.getName());
1664 XmlUtils.skipCurrentTag(parser);
1665 }
1666 }
1667
1668 str.close();
1669
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001670 mNextAppLinkGeneration.put(userId, maxAppLinkGeneration + 1);
1671
Amith Yamasani483f3b02012-03-13 16:08:00 -07001672 } catch (XmlPullParserException e) {
1673 mReadMessages.append("Error reading: " + e.toString());
1674 PackageManagerService.reportSettingsProblem(Log.ERROR,
1675 "Error reading stopped packages: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07001676 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
1677 e);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001678
1679 } catch (java.io.IOException e) {
1680 mReadMessages.append("Error reading: " + e.toString());
1681 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07001682 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
1683 e);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001684 }
1685 }
1686
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001687 private ArraySet<String> readComponentsLPr(XmlPullParser parser)
Amith Yamasani483f3b02012-03-13 16:08:00 -07001688 throws IOException, XmlPullParserException {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001689 ArraySet<String> components = null;
Amith Yamasani483f3b02012-03-13 16:08:00 -07001690 int type;
1691 int outerDepth = parser.getDepth();
1692 String tagName;
1693 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1694 && (type != XmlPullParser.END_TAG
1695 || parser.getDepth() > outerDepth)) {
1696 if (type == XmlPullParser.END_TAG
1697 || type == XmlPullParser.TEXT) {
1698 continue;
1699 }
1700 tagName = parser.getName();
1701 if (tagName.equals(TAG_ITEM)) {
1702 String componentName = parser.getAttributeValue(null, ATTR_NAME);
1703 if (componentName != null) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001704 if (components == null) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001705 components = new ArraySet<String>();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001706 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07001707 components.add(componentName);
1708 }
1709 }
1710 }
1711 return components;
1712 }
1713
Christopher Tatee012a232015-04-01 17:18:50 -07001714 /**
1715 * Record the state of preferred activity configuration into XML. This is used both
1716 * for recording packages.xml internally and for supporting backup/restore of the
1717 * preferred activity configuration.
1718 */
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08001719 void writePreferredActivitiesLPr(XmlSerializer serializer, int userId, boolean full)
Dianne Hackborn63092712012-10-07 14:45:35 -07001720 throws IllegalArgumentException, IllegalStateException, IOException {
1721 serializer.startTag(null, "preferred-activities");
1722 PreferredIntentResolver pir = mPreferredActivities.get(userId);
1723 if (pir != null) {
1724 for (final PreferredActivity pa : pir.filterSet()) {
1725 serializer.startTag(null, TAG_ITEM);
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08001726 pa.writeToXml(serializer, full);
Dianne Hackborn63092712012-10-07 14:45:35 -07001727 serializer.endTag(null, TAG_ITEM);
1728 }
1729 }
1730 serializer.endTag(null, "preferred-activities");
1731 }
1732
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001733 void writePersistentPreferredActivitiesLPr(XmlSerializer serializer, int userId)
1734 throws IllegalArgumentException, IllegalStateException, IOException {
Sander Alewijnseaf597622014-03-20 18:44:57 +00001735 serializer.startTag(null, TAG_PERSISTENT_PREFERRED_ACTIVITIES);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001736 PersistentPreferredIntentResolver ppir = mPersistentPreferredActivities.get(userId);
1737 if (ppir != null) {
1738 for (final PersistentPreferredActivity ppa : ppir.filterSet()) {
1739 serializer.startTag(null, TAG_ITEM);
1740 ppa.writeToXml(serializer);
1741 serializer.endTag(null, TAG_ITEM);
1742 }
1743 }
Sander Alewijnseaf597622014-03-20 18:44:57 +00001744 serializer.endTag(null, TAG_PERSISTENT_PREFERRED_ACTIVITIES);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001745 }
1746
Nicolas Prevot81948992014-05-16 18:25:26 +01001747 void writeCrossProfileIntentFiltersLPr(XmlSerializer serializer, int userId)
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001748 throws IllegalArgumentException, IllegalStateException, IOException {
Nicolas Prevot81948992014-05-16 18:25:26 +01001749 serializer.startTag(null, TAG_CROSS_PROFILE_INTENT_FILTERS);
1750 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(userId);
1751 if (cpir != null) {
1752 for (final CrossProfileIntentFilter cpif : cpir.filterSet()) {
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001753 serializer.startTag(null, TAG_ITEM);
Nicolas Prevot81948992014-05-16 18:25:26 +01001754 cpif.writeToXml(serializer);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001755 serializer.endTag(null, TAG_ITEM);
1756 }
1757 }
Nicolas Prevot81948992014-05-16 18:25:26 +01001758 serializer.endTag(null, TAG_CROSS_PROFILE_INTENT_FILTERS);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001759 }
1760
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001761 void writeDomainVerificationsLPr(XmlSerializer serializer,
1762 IntentFilterVerificationInfo verificationInfo)
1763 throws IllegalArgumentException, IllegalStateException, IOException {
1764 if (verificationInfo != null && verificationInfo.getPackageName() != null) {
1765 serializer.startTag(null, TAG_DOMAIN_VERIFICATION);
1766 verificationInfo.writeToXml(serializer);
Christopher Tate72c10a22015-06-12 18:31:24 -07001767 if (DEBUG_DOMAIN_VERIFICATION) {
1768 Slog.d(TAG, "Wrote domain verification for package: "
1769 + verificationInfo.getPackageName());
1770 }
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001771 serializer.endTag(null, TAG_DOMAIN_VERIFICATION);
1772 }
1773 }
1774
Christopher Tate6038d152015-06-17 13:07:46 -07001775 // Specifically for backup/restore
1776 void writeAllDomainVerificationsLPr(XmlSerializer serializer, int userId)
1777 throws IllegalArgumentException, IllegalStateException, IOException {
1778 serializer.startTag(null, TAG_ALL_INTENT_FILTER_VERIFICATION);
1779 final int N = mPackages.size();
1780 for (int i = 0; i < N; i++) {
1781 PackageSetting ps = mPackages.valueAt(i);
1782 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
1783 if (ivi != null) {
1784 writeDomainVerificationsLPr(serializer, ivi);
1785 }
1786 }
1787 serializer.endTag(null, TAG_ALL_INTENT_FILTER_VERIFICATION);
1788 }
1789
1790 // Specifically for backup/restore
1791 void readAllDomainVerificationsLPr(XmlPullParser parser, int userId)
1792 throws XmlPullParserException, IOException {
1793 mRestoredIntentFilterVerifications.clear();
1794
1795 int outerDepth = parser.getDepth();
1796 int type;
1797 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1798 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1799 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1800 continue;
1801 }
1802
1803 String tagName = parser.getName();
1804 if (tagName.equals(TAG_DOMAIN_VERIFICATION)) {
1805 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser);
1806 final String pkgName = ivi.getPackageName();
1807 final PackageSetting ps = mPackages.get(pkgName);
1808 if (ps != null) {
1809 // known/existing package; update in place
1810 ps.setIntentFilterVerificationInfo(ivi);
1811 if (DEBUG_DOMAIN_VERIFICATION) {
1812 Slog.d(TAG, "Restored IVI for existing app " + pkgName
1813 + " status=" + ivi.getStatusString());
1814 }
1815 } else {
1816 mRestoredIntentFilterVerifications.put(pkgName, ivi);
1817 if (DEBUG_DOMAIN_VERIFICATION) {
1818 Slog.d(TAG, "Restored IVI for pending app " + pkgName
1819 + " status=" + ivi.getStatusString());
1820 }
1821 }
1822 } else {
1823 PackageManagerService.reportSettingsProblem(Log.WARN,
1824 "Unknown element under <all-intent-filter-verification>: "
1825 + parser.getName());
1826 XmlUtils.skipCurrentTag(parser);
1827 }
1828 }
1829 }
1830
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07001831 // Specifically for backup/restore
1832 public void processRestoredPermissionGrantLPr(String pkgName, String permission,
1833 boolean isGranted, int restoredFlagSet, int userId)
1834 throws IOException, XmlPullParserException {
1835 mRuntimePermissionsPersistence.rememberRestoredUserGrantLPr(
1836 pkgName, permission, isGranted, restoredFlagSet, userId);
1837 }
1838
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001839 void writeDefaultAppsLPr(XmlSerializer serializer, int userId)
1840 throws IllegalArgumentException, IllegalStateException, IOException {
1841 serializer.startTag(null, TAG_DEFAULT_APPS);
1842 String packageName = mDefaultBrowserApp.get(userId);
1843 if (!TextUtils.isEmpty(packageName)) {
1844 serializer.startTag(null, TAG_DEFAULT_BROWSER);
1845 serializer.attribute(null, ATTR_PACKAGE_NAME, packageName);
1846 serializer.endTag(null, TAG_DEFAULT_BROWSER);
1847 }
1848 serializer.endTag(null, TAG_DEFAULT_APPS);
1849 }
1850
Amith Yamasani483f3b02012-03-13 16:08:00 -07001851 void writePackageRestrictionsLPr(int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001852 if (DEBUG_MU) {
1853 Log.i(TAG, "Writing package restrictions for user=" + userId);
1854 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001855 // Keep the old stopped packages around until we know the new ones have
1856 // been successfully written.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001857 File userPackagesStateFile = getUserPackagesStateFile(userId);
1858 File backupFile = getUserPackagesStateBackupFile(userId);
1859 new File(userPackagesStateFile.getParent()).mkdirs();
1860 if (userPackagesStateFile.exists()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001861 // Presence of backup settings file indicates that we failed
1862 // to persist packages earlier. So preserve the older
1863 // backup for future reference since the current packages
1864 // might have been corrupted.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001865 if (!backupFile.exists()) {
1866 if (!userPackagesStateFile.renameTo(backupFile)) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001867 Slog.wtf(PackageManagerService.TAG,
1868 "Unable to backup user packages state file, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001869 + "current changes will be lost at reboot");
1870 return;
1871 }
1872 } else {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001873 userPackagesStateFile.delete();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001874 Slog.w(PackageManagerService.TAG, "Preserving older stopped packages backup");
1875 }
1876 }
1877
1878 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001879 final FileOutputStream fstr = new FileOutputStream(userPackagesStateFile);
Kenny Root447106f2011-03-23 11:00:15 -07001880 final BufferedOutputStream str = new BufferedOutputStream(fstr);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001881
Kenny Root447106f2011-03-23 11:00:15 -07001882 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001883 serializer.setOutput(str, StandardCharsets.UTF_8.name());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001884 serializer.startDocument(null, true);
1885 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1886
Amith Yamasani483f3b02012-03-13 16:08:00 -07001887 serializer.startTag(null, TAG_PACKAGE_RESTRICTIONS);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001888
Kenny Root447106f2011-03-23 11:00:15 -07001889 for (final PackageSetting pkg : mPackages.values()) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001890 PackageUserState ustate = pkg.readUserState(userId);
1891 if (ustate.stopped || ustate.notLaunched || !ustate.installed
1892 || ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001893 || ustate.hidden
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001894 || ustate.suspended
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001895 || (ustate.enabledComponents != null
1896 && ustate.enabledComponents.size() > 0)
1897 || (ustate.disabledComponents != null
Kenny Guyc13053b2014-05-29 14:17:17 +01001898 && ustate.disabledComponents.size() > 0)
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001899 || ustate.blockUninstall
1900 || (ustate.domainVerificationStatus !=
1901 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED)) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001902 serializer.startTag(null, TAG_PACKAGE);
1903 serializer.attribute(null, ATTR_NAME, pkg.name);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001904 if (DEBUG_MU) Log.i(TAG, " pkg=" + pkg.name + ", state=" + ustate.enabled);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001905
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001906 if (!ustate.installed) {
1907 serializer.attribute(null, ATTR_INSTALLED, "false");
1908 }
1909 if (ustate.stopped) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001910 serializer.attribute(null, ATTR_STOPPED, "true");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001911 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001912 if (ustate.notLaunched) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001913 serializer.attribute(null, ATTR_NOT_LAUNCHED, "true");
1914 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001915 if (ustate.hidden) {
1916 serializer.attribute(null, ATTR_HIDDEN, "true");
Amith Yamasani655d0e22013-06-12 14:19:10 -07001917 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001918 if (ustate.suspended) {
1919 serializer.attribute(null, ATTR_SUSPENDED, "true");
1920 }
Kenny Guyc13053b2014-05-29 14:17:17 +01001921 if (ustate.blockUninstall) {
1922 serializer.attribute(null, ATTR_BLOCK_UNINSTALL, "true");
1923 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001924 if (ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT) {
1925 serializer.attribute(null, ATTR_ENABLED,
1926 Integer.toString(ustate.enabled));
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001927 if (ustate.lastDisableAppCaller != null) {
1928 serializer.attribute(null, ATTR_ENABLED_CALLER,
1929 ustate.lastDisableAppCaller);
1930 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07001931 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001932 if (ustate.domainVerificationStatus !=
1933 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED) {
1934 serializer.attribute(null, ATTR_DOMAIN_VERIFICATON_STATE,
1935 Integer.toString(ustate.domainVerificationStatus));
1936 }
Christopher Tatef0d6cb32015-07-10 17:44:53 -07001937 if (ustate.appLinkGeneration != 0) {
1938 serializer.attribute(null, ATTR_APP_LINK_GENERATION,
1939 Integer.toString(ustate.appLinkGeneration));
1940 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001941 if (ustate.enabledComponents != null
1942 && ustate.enabledComponents.size() > 0) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001943 serializer.startTag(null, TAG_ENABLED_COMPONENTS);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001944 for (final String name : ustate.enabledComponents) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001945 serializer.startTag(null, TAG_ITEM);
1946 serializer.attribute(null, ATTR_NAME, name);
1947 serializer.endTag(null, TAG_ITEM);
1948 }
1949 serializer.endTag(null, TAG_ENABLED_COMPONENTS);
1950 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001951 if (ustate.disabledComponents != null
1952 && ustate.disabledComponents.size() > 0) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001953 serializer.startTag(null, TAG_DISABLED_COMPONENTS);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001954 for (final String name : ustate.disabledComponents) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001955 serializer.startTag(null, TAG_ITEM);
1956 serializer.attribute(null, ATTR_NAME, name);
1957 serializer.endTag(null, TAG_ITEM);
1958 }
1959 serializer.endTag(null, TAG_DISABLED_COMPONENTS);
1960 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001961
Amith Yamasani483f3b02012-03-13 16:08:00 -07001962 serializer.endTag(null, TAG_PACKAGE);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001963 }
1964 }
1965
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08001966 writePreferredActivitiesLPr(serializer, userId, true);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001967 writePersistentPreferredActivitiesLPr(serializer, userId);
Nicolas Prevot81948992014-05-16 18:25:26 +01001968 writeCrossProfileIntentFiltersLPr(serializer, userId);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001969 writeDefaultAppsLPr(serializer, userId);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001970
Amith Yamasani483f3b02012-03-13 16:08:00 -07001971 serializer.endTag(null, TAG_PACKAGE_RESTRICTIONS);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001972
1973 serializer.endDocument();
1974
1975 str.flush();
1976 FileUtils.sync(fstr);
1977 str.close();
1978
1979 // New settings successfully written, old ones are no longer
1980 // needed.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001981 backupFile.delete();
1982 FileUtils.setPermissions(userPackagesStateFile.toString(),
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001983 FileUtils.S_IRUSR|FileUtils.S_IWUSR
Nick Kralevich70522ad2012-01-06 13:58:01 -08001984 |FileUtils.S_IRGRP|FileUtils.S_IWGRP,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001985 -1, -1);
1986
1987 // Done, all is good!
1988 return;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001989 } catch(java.io.IOException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001990 Slog.wtf(PackageManagerService.TAG,
Amith Yamasani483f3b02012-03-13 16:08:00 -07001991 "Unable to write package manager user packages state, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001992 + " current changes will be lost at reboot", e);
1993 }
1994
1995 // Clean up partially written files
Amith Yamasani483f3b02012-03-13 16:08:00 -07001996 if (userPackagesStateFile.exists()) {
1997 if (!userPackagesStateFile.delete()) {
1998 Log.i(PackageManagerService.TAG, "Failed to clean up mangled file: "
1999 + mStoppedPackagesFilename);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002000 }
2001 }
2002 }
2003
Svetoslavc6d1c342015-02-26 14:44:43 -08002004 void readInstallPermissionsLPr(XmlPullParser parser,
2005 PermissionsState permissionsState) throws IOException, XmlPullParserException {
2006 int outerDepth = parser.getDepth();
2007 int type;
2008 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2009 && (type != XmlPullParser.END_TAG
2010 || parser.getDepth() > outerDepth)) {
2011 if (type == XmlPullParser.END_TAG
2012 || type == XmlPullParser.TEXT) {
2013 continue;
2014 }
2015 String tagName = parser.getName();
2016 if (tagName.equals(TAG_ITEM)) {
2017 String name = parser.getAttributeValue(null, ATTR_NAME);
2018
2019 BasePermission bp = mPermissions.get(name);
2020 if (bp == null) {
2021 Slog.w(PackageManagerService.TAG, "Unknown permission: " + name);
2022 XmlUtils.skipCurrentTag(parser);
2023 continue;
2024 }
2025
Svet Ganov8c7f7002015-05-07 10:48:44 -07002026 String grantedStr = parser.getAttributeValue(null, ATTR_GRANTED);
2027 final boolean granted = grantedStr == null
2028 || Boolean.parseBoolean(grantedStr);
2029
2030 String flagsStr = parser.getAttributeValue(null, ATTR_FLAGS);
2031 final int flags = (flagsStr != null)
2032 ? Integer.parseInt(flagsStr, 16) : 0;
2033
2034 if (granted) {
2035 if (permissionsState.grantInstallPermission(bp) ==
2036 PermissionsState.PERMISSION_OPERATION_FAILURE) {
2037 Slog.w(PackageManagerService.TAG, "Permission already added: " + name);
2038 XmlUtils.skipCurrentTag(parser);
2039 } else {
2040 permissionsState.updatePermissionFlags(bp, UserHandle.USER_ALL,
2041 PackageManager.MASK_PERMISSION_FLAGS, flags);
2042 }
2043 } else {
2044 if (permissionsState.revokeInstallPermission(bp) ==
2045 PermissionsState.PERMISSION_OPERATION_FAILURE) {
2046 Slog.w(PackageManagerService.TAG, "Permission already added: " + name);
2047 XmlUtils.skipCurrentTag(parser);
2048 } else {
2049 permissionsState.updatePermissionFlags(bp, UserHandle.USER_ALL,
2050 PackageManager.MASK_PERMISSION_FLAGS, flags);
2051 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002052 }
2053 } else {
2054 Slog.w(PackageManagerService.TAG, "Unknown element under <permissions>: "
2055 + parser.getName());
2056 XmlUtils.skipCurrentTag(parser);
2057 }
2058 }
2059 }
2060
Svet Ganov8c7f7002015-05-07 10:48:44 -07002061 void writePermissionsLPr(XmlSerializer serializer, List<PermissionState> permissionStates)
Svetoslavc6d1c342015-02-26 14:44:43 -08002062 throws IOException {
Svet Ganov8c7f7002015-05-07 10:48:44 -07002063 if (permissionStates.isEmpty()) {
Svetoslavc6d1c342015-02-26 14:44:43 -08002064 return;
2065 }
2066
2067 serializer.startTag(null, TAG_PERMISSIONS);
2068
Svet Ganov8c7f7002015-05-07 10:48:44 -07002069 for (PermissionState permissionState : permissionStates) {
Svetoslavc6d1c342015-02-26 14:44:43 -08002070 serializer.startTag(null, TAG_ITEM);
Svet Ganov8c7f7002015-05-07 10:48:44 -07002071 serializer.attribute(null, ATTR_NAME, permissionState.getName());
2072 serializer.attribute(null, ATTR_GRANTED, String.valueOf(permissionState.isGranted()));
2073 serializer.attribute(null, ATTR_FLAGS, Integer.toHexString(permissionState.getFlags()));
Svetoslavc6d1c342015-02-26 14:44:43 -08002074 serializer.endTag(null, TAG_ITEM);
2075 }
2076
2077 serializer.endTag(null, TAG_PERMISSIONS);
2078 }
2079
Svet Ganov354cd3c2015-12-17 11:35:04 -08002080 void writeChildPackagesLPw(XmlSerializer serializer, List<String> childPackageNames)
2081 throws IOException {
2082 if (childPackageNames == null) {
2083 return;
2084 }
2085 final int childCount = childPackageNames.size();
2086 for (int i = 0; i < childCount; i++) {
2087 String childPackageName = childPackageNames.get(i);
2088 serializer.startTag(null, TAG_CHILD_PACKAGE);
2089 serializer.attribute(null, ATTR_NAME, childPackageName);
2090 serializer.endTag(null, TAG_CHILD_PACKAGE);
2091 }
2092 }
2093
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002094 // Note: assumed "stopped" field is already cleared in all packages.
Amith Yamasani483f3b02012-03-13 16:08:00 -07002095 // Legacy reader, used to read in the old file format after an upgrade. Not used after that.
Kenny Root447106f2011-03-23 11:00:15 -07002096 void readStoppedLPw() {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002097 FileInputStream str = null;
2098 if (mBackupStoppedPackagesFilename.exists()) {
2099 try {
2100 str = new FileInputStream(mBackupStoppedPackagesFilename);
2101 mReadMessages.append("Reading from backup stopped packages file\n");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002102 PackageManagerService.reportSettingsProblem(Log.INFO,
2103 "Need to read from backup stopped packages file");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002104 if (mSettingsFilename.exists()) {
2105 // If both the backup and normal file exist, we
2106 // ignore the normal one since it might have been
2107 // corrupted.
2108 Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file "
2109 + mStoppedPackagesFilename);
2110 mStoppedPackagesFilename.delete();
2111 }
2112 } catch (java.io.IOException e) {
2113 // We'll try for the normal settings file.
2114 }
2115 }
2116
2117 try {
2118 if (str == null) {
2119 if (!mStoppedPackagesFilename.exists()) {
2120 mReadMessages.append("No stopped packages file found\n");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002121 PackageManagerService.reportSettingsProblem(Log.INFO,
2122 "No stopped packages file file; assuming all started");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002123 // At first boot, make sure no packages are stopped.
2124 // We usually want to have third party apps initialize
2125 // in the stopped state, but not at first boot.
2126 for (PackageSetting pkg : mPackages.values()) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002127 pkg.setStopped(false, 0);
2128 pkg.setNotLaunched(false, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002129 }
2130 return;
2131 }
2132 str = new FileInputStream(mStoppedPackagesFilename);
2133 }
Kenny Root447106f2011-03-23 11:00:15 -07002134 final XmlPullParser parser = Xml.newPullParser();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002135 parser.setInput(str, null);
2136
2137 int type;
2138 while ((type=parser.next()) != XmlPullParser.START_TAG
2139 && type != XmlPullParser.END_DOCUMENT) {
2140 ;
2141 }
2142
2143 if (type != XmlPullParser.START_TAG) {
2144 mReadMessages.append("No start tag found in stopped packages file\n");
2145 PackageManagerService.reportSettingsProblem(Log.WARN,
2146 "No start tag found in package manager stopped packages");
2147 return;
2148 }
2149
2150 int outerDepth = parser.getDepth();
2151 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2152 && (type != XmlPullParser.END_TAG
2153 || parser.getDepth() > outerDepth)) {
2154 if (type == XmlPullParser.END_TAG
2155 || type == XmlPullParser.TEXT) {
2156 continue;
2157 }
2158
2159 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07002160 if (tagName.equals(TAG_PACKAGE)) {
2161 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002162 PackageSetting ps = mPackages.get(name);
2163 if (ps != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002164 ps.setStopped(true, 0);
2165 if ("1".equals(parser.getAttributeValue(null, ATTR_NOT_LAUNCHED))) {
2166 ps.setNotLaunched(true, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002167 }
2168 } else {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002169 Slog.w(PackageManagerService.TAG,
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002170 "No package known for stopped package " + name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002171 }
2172 XmlUtils.skipCurrentTag(parser);
2173 } else {
2174 Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: "
2175 + parser.getName());
2176 XmlUtils.skipCurrentTag(parser);
2177 }
2178 }
2179
2180 str.close();
2181
Amith Yamasani483f3b02012-03-13 16:08:00 -07002182 } catch (XmlPullParserException e) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002183 mReadMessages.append("Error reading: " + e.toString());
Amith Yamasani483f3b02012-03-13 16:08:00 -07002184 PackageManagerService.reportSettingsProblem(Log.ERROR,
2185 "Error reading stopped packages: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002186 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
2187 e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002188
Amith Yamasani483f3b02012-03-13 16:08:00 -07002189 } catch (java.io.IOException e) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002190 mReadMessages.append("Error reading: " + e.toString());
2191 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002192 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
2193 e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002194
2195 }
2196 }
2197
Kenny Root447106f2011-03-23 11:00:15 -07002198 void writeLPr() {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002199 //Debug.startMethodTracing("/data/system/packageprof", 8 * 1024 * 1024);
2200
2201 // Keep the old settings around until we know the new ones have
2202 // been successfully written.
2203 if (mSettingsFilename.exists()) {
2204 // Presence of backup settings file indicates that we failed
2205 // to persist settings earlier. So preserve the older
2206 // backup for future reference since the current settings
2207 // might have been corrupted.
2208 if (!mBackupSettingsFilename.exists()) {
2209 if (!mSettingsFilename.renameTo(mBackupSettingsFilename)) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002210 Slog.wtf(PackageManagerService.TAG,
2211 "Unable to backup package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002212 + " current changes will be lost at reboot");
2213 return;
2214 }
2215 } else {
2216 mSettingsFilename.delete();
2217 Slog.w(PackageManagerService.TAG, "Preserving older settings backup");
2218 }
2219 }
2220
2221 mPastSignatures.clear();
2222
2223 try {
2224 FileOutputStream fstr = new FileOutputStream(mSettingsFilename);
2225 BufferedOutputStream str = new BufferedOutputStream(fstr);
2226
2227 //XmlSerializer serializer = XmlUtils.serializerInstance();
2228 XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002229 serializer.setOutput(str, StandardCharsets.UTF_8.name());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002230 serializer.startDocument(null, true);
2231 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
2232
2233 serializer.startTag(null, "packages");
2234
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002235 for (int i = 0; i < mVersion.size(); i++) {
2236 final String volumeUuid = mVersion.keyAt(i);
2237 final VersionInfo ver = mVersion.valueAt(i);
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002238
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002239 serializer.startTag(null, TAG_VERSION);
2240 XmlUtils.writeStringAttribute(serializer, ATTR_VOLUME_UUID, volumeUuid);
2241 XmlUtils.writeIntAttribute(serializer, ATTR_SDK_VERSION, ver.sdkVersion);
2242 XmlUtils.writeIntAttribute(serializer, ATTR_DATABASE_VERSION, ver.databaseVersion);
2243 XmlUtils.writeStringAttribute(serializer, ATTR_FINGERPRINT, ver.fingerprint);
2244 serializer.endTag(null, TAG_VERSION);
2245 }
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07002246
Kenny Root0aaa0d92011-09-12 16:42:55 -07002247 if (mVerifierDeviceIdentity != null) {
2248 serializer.startTag(null, "verifier");
2249 serializer.attribute(null, "device", mVerifierDeviceIdentity.toString());
2250 serializer.endTag(null, "verifier");
2251 }
2252
Jeff Sharkeyf5385772012-05-11 14:04:41 -07002253 if (mReadExternalStorageEnforced != null) {
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002254 serializer.startTag(null, TAG_READ_EXTERNAL_STORAGE);
2255 serializer.attribute(
Jeff Sharkey5d32e772012-04-12 15:59:23 -07002256 null, ATTR_ENFORCEMENT, mReadExternalStorageEnforced ? "1" : "0");
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002257 serializer.endTag(null, TAG_READ_EXTERNAL_STORAGE);
2258 }
2259
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002260 serializer.startTag(null, "permission-trees");
2261 for (BasePermission bp : mPermissionTrees.values()) {
Kenny Root447106f2011-03-23 11:00:15 -07002262 writePermissionLPr(serializer, bp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002263 }
2264 serializer.endTag(null, "permission-trees");
2265
2266 serializer.startTag(null, "permissions");
2267 for (BasePermission bp : mPermissions.values()) {
Kenny Root447106f2011-03-23 11:00:15 -07002268 writePermissionLPr(serializer, bp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002269 }
2270 serializer.endTag(null, "permissions");
2271
Kenny Root447106f2011-03-23 11:00:15 -07002272 for (final PackageSetting pkg : mPackages.values()) {
2273 writePackageLPr(serializer, pkg);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002274 }
2275
Kenny Root447106f2011-03-23 11:00:15 -07002276 for (final PackageSetting pkg : mDisabledSysPackages.values()) {
2277 writeDisabledSysPackageLPr(serializer, pkg);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002278 }
2279
Kenny Root447106f2011-03-23 11:00:15 -07002280 for (final SharedUserSetting usr : mSharedUsers.values()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002281 serializer.startTag(null, "shared-user");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002282 serializer.attribute(null, ATTR_NAME, usr.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002283 serializer.attribute(null, "userId",
2284 Integer.toString(usr.userId));
2285 usr.signatures.writeXml(serializer, "sigs", mPastSignatures);
Svet Ganov8c7f7002015-05-07 10:48:44 -07002286 writePermissionsLPr(serializer, usr.getPermissionsState()
2287 .getInstallPermissionStates());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002288 serializer.endTag(null, "shared-user");
2289 }
2290
2291 if (mPackagesToBeCleaned.size() > 0) {
Jeff Sharkey752cd922012-09-23 16:25:12 -07002292 for (PackageCleanItem item : mPackagesToBeCleaned) {
2293 final String userStr = Integer.toString(item.userId);
2294 serializer.startTag(null, "cleaning-package");
2295 serializer.attribute(null, ATTR_NAME, item.packageName);
2296 serializer.attribute(null, ATTR_CODE, item.andCode ? "true" : "false");
2297 serializer.attribute(null, ATTR_USER, userStr);
2298 serializer.endTag(null, "cleaning-package");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002299 }
2300 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002301
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002302 if (mRenamedPackages.size() > 0) {
Andy McFadden2f362292012-01-20 14:43:38 -08002303 for (Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002304 serializer.startTag(null, "renamed-package");
2305 serializer.attribute(null, "new", e.getKey());
2306 serializer.attribute(null, "old", e.getValue());
2307 serializer.endTag(null, "renamed-package");
2308 }
2309 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002310
Christopher Tate6038d152015-06-17 13:07:46 -07002311 final int numIVIs = mRestoredIntentFilterVerifications.size();
2312 if (numIVIs > 0) {
2313 if (DEBUG_DOMAIN_VERIFICATION) {
2314 Slog.i(TAG, "Writing restored-ivi entries to packages.xml");
2315 }
2316 serializer.startTag(null, "restored-ivi");
2317 for (int i = 0; i < numIVIs; i++) {
2318 IntentFilterVerificationInfo ivi = mRestoredIntentFilterVerifications.valueAt(i);
2319 writeDomainVerificationsLPr(serializer, ivi);
2320 }
2321 serializer.endTag(null, "restored-ivi");
2322 } else {
2323 if (DEBUG_DOMAIN_VERIFICATION) {
2324 Slog.i(TAG, " no restored IVI entries to write");
2325 }
2326 }
2327
dcashman55b10782014-04-09 14:20:38 -07002328 mKeySetManagerService.writeKeySetManagerServiceLPr(serializer);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002329
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002330 serializer.endTag(null, "packages");
2331
2332 serializer.endDocument();
2333
2334 str.flush();
2335 FileUtils.sync(fstr);
2336 str.close();
2337
2338 // New settings successfully written, old ones are no longer
2339 // needed.
2340 mBackupSettingsFilename.delete();
2341 FileUtils.setPermissions(mSettingsFilename.toString(),
2342 FileUtils.S_IRUSR|FileUtils.S_IWUSR
Nick Kralevich70522ad2012-01-06 13:58:01 -08002343 |FileUtils.S_IRGRP|FileUtils.S_IWGRP,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002344 -1, -1);
2345
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002346 writeKernelMappingLPr();
Jeff Sharkey91edde22015-05-20 12:04:42 -07002347 writePackageListLPr();
Amith Yamasani483f3b02012-03-13 16:08:00 -07002348 writeAllUsersPackageRestrictionsLPr();
Svetoslavc6d1c342015-02-26 14:44:43 -08002349 writeAllRuntimePermissionsLPr();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002350 return;
2351
2352 } catch(XmlPullParserException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002353 Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002354 + "current changes will be lost at reboot", e);
2355 } catch(java.io.IOException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002356 Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002357 + "current changes will be lost at reboot", e);
2358 }
2359 // Clean up partially written files
2360 if (mSettingsFilename.exists()) {
2361 if (!mSettingsFilename.delete()) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002362 Slog.wtf(PackageManagerService.TAG, "Failed to clean up mangled file: "
Amith Yamasani483f3b02012-03-13 16:08:00 -07002363 + mSettingsFilename);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002364 }
2365 }
2366 //Debug.stopMethodTracing();
2367 }
2368
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002369 void writeKernelMappingLPr() {
2370 if (mKernelMappingFilename == null) return;
2371
2372 final String[] known = mKernelMappingFilename.list();
2373 final ArraySet<String> knownSet = new ArraySet<>(known.length);
2374 for (String name : known) {
2375 knownSet.add(name);
2376 }
2377
2378 for (final PackageSetting ps : mPackages.values()) {
2379 // Package is actively claimed
2380 knownSet.remove(ps.name);
2381 writeKernelMappingLPr(ps);
2382 }
2383
2384 // Remove any unclaimed mappings
2385 for (int i = 0; i < knownSet.size(); i++) {
2386 final String name = knownSet.valueAt(i);
2387 if (DEBUG_KERNEL) Slog.d(TAG, "Dropping mapping " + name);
2388
2389 mKernelMapping.remove(name);
Jeff Sharkey800efcc2016-02-25 17:08:11 -07002390 new File(mKernelMappingFilename, name).delete();
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002391 }
2392 }
2393
2394 void writeKernelMappingLPr(PackageSetting ps) {
2395 if (mKernelMappingFilename == null) return;
2396
2397 final Integer cur = mKernelMapping.get(ps.name);
2398 if (cur != null && cur.intValue() == ps.appId) {
2399 // Ignore when mapping already matches
2400 return;
2401 }
2402
2403 if (DEBUG_KERNEL) Slog.d(TAG, "Mapping " + ps.name + " to " + ps.appId);
2404
2405 final File dir = new File(mKernelMappingFilename, ps.name);
2406 dir.mkdir();
2407
2408 final File file = new File(dir, "appid");
2409 try {
2410 FileUtils.stringToFile(file, Integer.toString(ps.appId));
2411 mKernelMapping.put(ps.name, ps.appId);
2412 } catch (IOException ignored) {
2413 }
2414 }
2415
Jeff Sharkey91edde22015-05-20 12:04:42 -07002416 void writePackageListLPr() {
2417 writePackageListLPr(-1);
2418 }
2419
2420 void writePackageListLPr(int creatingUserId) {
2421 // Only derive GIDs for active users (not dying)
2422 final List<UserInfo> users = UserManagerService.getInstance().getUsers(true);
2423 int[] userIds = new int[users.size()];
2424 for (int i = 0; i < userIds.length; i++) {
2425 userIds[i] = users.get(i).id;
2426 }
2427 if (creatingUserId != -1) {
2428 userIds = ArrayUtils.appendInt(userIds, creatingUserId);
2429 }
2430
2431 // Write package list file now, use a JournaledFile.
2432 File tempFile = new File(mPackageListFilename.getAbsolutePath() + ".tmp");
2433 JournaledFile journal = new JournaledFile(mPackageListFilename, tempFile);
2434
2435 final File writeTarget = journal.chooseForWrite();
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002436 FileOutputStream fstr;
2437 BufferedWriter writer = null;
Jeff Sharkey91edde22015-05-20 12:04:42 -07002438 try {
2439 fstr = new FileOutputStream(writeTarget);
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002440 writer = new BufferedWriter(new OutputStreamWriter(fstr, Charset.defaultCharset()));
Jeff Sharkey91edde22015-05-20 12:04:42 -07002441 FileUtils.setPermissions(fstr.getFD(), 0640, SYSTEM_UID, PACKAGE_INFO_GID);
2442
2443 StringBuilder sb = new StringBuilder();
2444 for (final PackageSetting pkg : mPackages.values()) {
Jeff Sharkeye17ac152015-11-06 22:40:29 -08002445 if (pkg.pkg == null || pkg.pkg.applicationInfo == null
2446 || pkg.pkg.applicationInfo.dataDir == null) {
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002447 if (!"android".equals(pkg.name)) {
2448 Slog.w(TAG, "Skipping " + pkg + " due to missing metadata");
2449 }
Jeff Sharkey91edde22015-05-20 12:04:42 -07002450 continue;
2451 }
2452
2453 final ApplicationInfo ai = pkg.pkg.applicationInfo;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07002454 final String dataPath = ai.dataDir;
Jeff Sharkey91edde22015-05-20 12:04:42 -07002455 final boolean isDebug = (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
2456 final int[] gids = pkg.getPermissionsState().computeGids(userIds);
2457
2458 // Avoid any application that has a space in its path.
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002459 if (dataPath.indexOf(' ') >= 0)
Jeff Sharkey91edde22015-05-20 12:04:42 -07002460 continue;
2461
2462 // we store on each line the following information for now:
2463 //
2464 // pkgName - package name
2465 // userId - application-specific user id
2466 // debugFlag - 0 or 1 if the package is debuggable.
2467 // dataPath - path to package's data path
2468 // seinfo - seinfo label for the app (assigned at install time)
2469 // gids - supplementary gids this app launches with
2470 //
2471 // NOTE: We prefer not to expose all ApplicationInfo flags for now.
2472 //
2473 // DO NOT MODIFY THIS FORMAT UNLESS YOU CAN ALSO MODIFY ITS USERS
2474 // FROM NATIVE CODE. AT THE MOMENT, LOOK AT THE FOLLOWING SOURCES:
William Robertsdc06bb02015-07-24 10:05:06 -07002475 // frameworks/base/libs/packagelistparser
Jeff Sharkey91edde22015-05-20 12:04:42 -07002476 // system/core/run-as/run-as.c
Jeff Sharkey91edde22015-05-20 12:04:42 -07002477 //
2478 sb.setLength(0);
2479 sb.append(ai.packageName);
2480 sb.append(" ");
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002481 sb.append(ai.uid);
Jeff Sharkey91edde22015-05-20 12:04:42 -07002482 sb.append(isDebug ? " 1 " : " 0 ");
2483 sb.append(dataPath);
2484 sb.append(" ");
2485 sb.append(ai.seinfo);
2486 sb.append(" ");
2487 if (gids != null && gids.length > 0) {
2488 sb.append(gids[0]);
2489 for (int i = 1; i < gids.length; i++) {
2490 sb.append(",");
2491 sb.append(gids[i]);
2492 }
2493 } else {
2494 sb.append("none");
2495 }
2496 sb.append("\n");
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002497 writer.append(sb);
Jeff Sharkey91edde22015-05-20 12:04:42 -07002498 }
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002499 writer.flush();
Jeff Sharkey91edde22015-05-20 12:04:42 -07002500 FileUtils.sync(fstr);
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002501 writer.close();
Jeff Sharkey91edde22015-05-20 12:04:42 -07002502 journal.commit();
2503 } catch (Exception e) {
2504 Slog.wtf(TAG, "Failed to write packages.list", e);
Fyodor Kupolovb69056b2015-12-18 15:20:00 -08002505 IoUtils.closeQuietly(writer);
Jeff Sharkey91edde22015-05-20 12:04:42 -07002506 journal.rollback();
2507 }
2508 }
2509
Kenny Root447106f2011-03-23 11:00:15 -07002510 void writeDisabledSysPackageLPr(XmlSerializer serializer, final PackageSetting pkg)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002511 throws java.io.IOException {
2512 serializer.startTag(null, "updated-package");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002513 serializer.attribute(null, ATTR_NAME, pkg.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002514 if (pkg.realName != null) {
2515 serializer.attribute(null, "realName", pkg.realName);
2516 }
2517 serializer.attribute(null, "codePath", pkg.codePathString);
2518 serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp));
2519 serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime));
2520 serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime));
2521 serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
2522 if (!pkg.resourcePathString.equals(pkg.codePathString)) {
2523 serializer.attribute(null, "resourcePath", pkg.resourcePathString);
2524 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002525 if (pkg.legacyNativeLibraryPathString != null) {
2526 serializer.attribute(null, "nativeLibraryPath", pkg.legacyNativeLibraryPathString);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002527 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002528 if (pkg.primaryCpuAbiString != null) {
2529 serializer.attribute(null, "primaryCpuAbi", pkg.primaryCpuAbiString);
Narayan Kamath9e289d72014-04-10 09:26:59 +00002530 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002531 if (pkg.secondaryCpuAbiString != null) {
2532 serializer.attribute(null, "secondaryCpuAbi", pkg.secondaryCpuAbiString);
2533 }
Narayan Kamath4903f642014-08-11 13:33:45 +01002534 if (pkg.cpuAbiOverrideString != null) {
2535 serializer.attribute(null, "cpuAbiOverride", pkg.cpuAbiOverrideString);
2536 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002537
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002538 if (pkg.sharedUser == null) {
Amith Yamasani13593602012-03-22 16:16:17 -07002539 serializer.attribute(null, "userId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002540 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07002541 serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002542 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002543
Svet Ganov354cd3c2015-12-17 11:35:04 -08002544 if (pkg.parentPackageName != null) {
2545 serializer.attribute(null, "parentPackageName", pkg.parentPackageName);
2546 }
2547
2548 writeChildPackagesLPw(serializer, pkg.childPackageNames);
2549
Svetoslavc6d1c342015-02-26 14:44:43 -08002550 // If this is a shared user, the permissions will be written there.
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002551 if (pkg.sharedUser == null) {
Svet Ganov8c7f7002015-05-07 10:48:44 -07002552 writePermissionsLPr(serializer, pkg.getPermissionsState()
2553 .getInstallPermissionStates());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002554 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002555
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002556 serializer.endTag(null, "updated-package");
2557 }
2558
Kenny Root447106f2011-03-23 11:00:15 -07002559 void writePackageLPr(XmlSerializer serializer, final PackageSetting pkg)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002560 throws java.io.IOException {
2561 serializer.startTag(null, "package");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002562 serializer.attribute(null, ATTR_NAME, pkg.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002563 if (pkg.realName != null) {
2564 serializer.attribute(null, "realName", pkg.realName);
2565 }
2566 serializer.attribute(null, "codePath", pkg.codePathString);
2567 if (!pkg.resourcePathString.equals(pkg.codePathString)) {
2568 serializer.attribute(null, "resourcePath", pkg.resourcePathString);
2569 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002570
2571 if (pkg.legacyNativeLibraryPathString != null) {
2572 serializer.attribute(null, "nativeLibraryPath", pkg.legacyNativeLibraryPathString);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002573 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002574 if (pkg.primaryCpuAbiString != null) {
2575 serializer.attribute(null, "primaryCpuAbi", pkg.primaryCpuAbiString);
Narayan Kamath9e289d72014-04-10 09:26:59 +00002576 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002577 if (pkg.secondaryCpuAbiString != null) {
2578 serializer.attribute(null, "secondaryCpuAbi", pkg.secondaryCpuAbiString);
2579 }
Narayan Kamath4903f642014-08-11 13:33:45 +01002580 if (pkg.cpuAbiOverrideString != null) {
2581 serializer.attribute(null, "cpuAbiOverride", pkg.cpuAbiOverrideString);
2582 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002583
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002584 serializer.attribute(null, "publicFlags", Integer.toString(pkg.pkgFlags));
2585 serializer.attribute(null, "privateFlags", Integer.toString(pkg.pkgPrivateFlags));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002586 serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp));
2587 serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime));
2588 serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime));
2589 serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
2590 if (pkg.sharedUser == null) {
Amith Yamasani13593602012-03-22 16:16:17 -07002591 serializer.attribute(null, "userId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002592 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07002593 serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002594 }
2595 if (pkg.uidError) {
2596 serializer.attribute(null, "uidError", "true");
2597 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002598 if (pkg.installStatus == PackageSettingBase.PKG_INSTALL_INCOMPLETE) {
2599 serializer.attribute(null, "installStatus", "false");
2600 }
2601 if (pkg.installerPackageName != null) {
2602 serializer.attribute(null, "installer", pkg.installerPackageName);
2603 }
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07002604 if (pkg.volumeUuid != null) {
2605 serializer.attribute(null, "volumeUuid", pkg.volumeUuid);
2606 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08002607
2608 if (pkg.parentPackageName != null) {
2609 serializer.attribute(null, "parentPackageName", pkg.parentPackageName);
2610 }
2611
2612 writeChildPackagesLPw(serializer, pkg.childPackageNames);
2613
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002614 pkg.signatures.writeXml(serializer, "sigs", mPastSignatures);
Svet Ganov8c7f7002015-05-07 10:48:44 -07002615
2616 writePermissionsLPr(serializer, pkg.getPermissionsState()
2617 .getInstallPermissionStates());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002618
dcashman8c04fac2015-03-23 11:39:42 -07002619 writeSigningKeySetLPr(serializer, pkg.keySetData);
dcashman55b10782014-04-09 14:20:38 -07002620 writeUpgradeKeySetsLPr(serializer, pkg.keySetData);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002621 writeKeySetAliasesLPr(serializer, pkg.keySetData);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07002622 writeDomainVerificationsLPr(serializer, pkg.verificationInfo);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002623
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002624 serializer.endTag(null, "package");
2625 }
2626
dcashman8c04fac2015-03-23 11:39:42 -07002627 void writeSigningKeySetLPr(XmlSerializer serializer,
Geremy Condraf1bcca82013-01-07 22:35:24 -08002628 PackageKeySetData data) throws IOException {
dcashman8c04fac2015-03-23 11:39:42 -07002629 serializer.startTag(null, "proper-signing-keyset");
2630 serializer.attribute(null, "identifier",
2631 Long.toString(data.getProperSigningKeySet()));
2632 serializer.endTag(null, "proper-signing-keyset");
dcashman55b10782014-04-09 14:20:38 -07002633 }
2634
2635 void writeUpgradeKeySetsLPr(XmlSerializer serializer,
2636 PackageKeySetData data) throws IOException {
dcashman8c04fac2015-03-23 11:39:42 -07002637 long properSigning = data.getProperSigningKeySet();
dcashman55b10782014-04-09 14:20:38 -07002638 if (data.isUsingUpgradeKeySets()) {
2639 for (long id : data.getUpgradeKeySets()) {
2640 serializer.startTag(null, "upgrade-keyset");
2641 serializer.attribute(null, "identifier", Long.toString(id));
2642 serializer.endTag(null, "upgrade-keyset");
2643 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08002644 }
2645 }
2646
2647 void writeKeySetAliasesLPr(XmlSerializer serializer,
2648 PackageKeySetData data) throws IOException {
2649 for (Map.Entry<String, Long> e: data.getAliases().entrySet()) {
2650 serializer.startTag(null, "defined-keyset");
2651 serializer.attribute(null, "alias", e.getKey());
2652 serializer.attribute(null, "identifier", Long.toString(e.getValue()));
2653 serializer.endTag(null, "defined-keyset");
2654 }
2655 }
2656
Kenny Root447106f2011-03-23 11:00:15 -07002657 void writePermissionLPr(XmlSerializer serializer, BasePermission bp)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002658 throws XmlPullParserException, java.io.IOException {
2659 if (bp.type != BasePermission.TYPE_BUILTIN && bp.sourcePackage != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002660 serializer.startTag(null, TAG_ITEM);
2661 serializer.attribute(null, ATTR_NAME, bp.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002662 serializer.attribute(null, "package", bp.sourcePackage);
2663 if (bp.protectionLevel != PermissionInfo.PROTECTION_NORMAL) {
2664 serializer.attribute(null, "protection", Integer.toString(bp.protectionLevel));
2665 }
2666 if (PackageManagerService.DEBUG_SETTINGS)
2667 Log.v(PackageManagerService.TAG, "Writing perm: name=" + bp.name + " type="
2668 + bp.type);
2669 if (bp.type == BasePermission.TYPE_DYNAMIC) {
Kenny Root447106f2011-03-23 11:00:15 -07002670 final PermissionInfo pi = bp.perm != null ? bp.perm.info : bp.pendingInfo;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002671 if (pi != null) {
2672 serializer.attribute(null, "type", "dynamic");
2673 if (pi.icon != 0) {
2674 serializer.attribute(null, "icon", Integer.toString(pi.icon));
2675 }
2676 if (pi.nonLocalizedLabel != null) {
2677 serializer.attribute(null, "label", pi.nonLocalizedLabel.toString());
2678 }
2679 }
2680 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07002681 serializer.endTag(null, TAG_ITEM);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002682 }
2683 }
2684
Kenny Root447106f2011-03-23 11:00:15 -07002685 ArrayList<PackageSetting> getListOfIncompleteInstallPackagesLPr() {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07002686 final ArraySet<String> kList = new ArraySet<String>(mPackages.keySet());
Kenny Root447106f2011-03-23 11:00:15 -07002687 final Iterator<String> its = kList.iterator();
2688 final ArrayList<PackageSetting> ret = new ArrayList<PackageSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002689 while (its.hasNext()) {
Kenny Root447106f2011-03-23 11:00:15 -07002690 final String key = its.next();
2691 final PackageSetting ps = mPackages.get(key);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002692 if (ps.getInstallStatus() == PackageSettingBase.PKG_INSTALL_INCOMPLETE) {
2693 ret.add(ps);
2694 }
2695 }
2696 return ret;
2697 }
2698
Jeff Sharkey752cd922012-09-23 16:25:12 -07002699 void addPackageToCleanLPw(PackageCleanItem pkg) {
2700 if (!mPackagesToBeCleaned.contains(pkg)) {
2701 mPackagesToBeCleaned.add(pkg);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002702 }
2703 }
2704
Xiaohui Chen594f2082015-08-18 11:04:20 -07002705 boolean readLPw(@NonNull List<UserInfo> users) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002706 FileInputStream str = null;
2707 if (mBackupSettingsFilename.exists()) {
2708 try {
2709 str = new FileInputStream(mBackupSettingsFilename);
2710 mReadMessages.append("Reading from backup settings file\n");
2711 PackageManagerService.reportSettingsProblem(Log.INFO,
2712 "Need to read from backup settings file");
2713 if (mSettingsFilename.exists()) {
2714 // If both the backup and settings file exist, we
2715 // ignore the settings since it might have been
2716 // corrupted.
2717 Slog.w(PackageManagerService.TAG, "Cleaning up settings file "
2718 + mSettingsFilename);
2719 mSettingsFilename.delete();
2720 }
2721 } catch (java.io.IOException e) {
2722 // We'll try for the normal settings file.
2723 }
2724 }
2725
Kenny Root447106f2011-03-23 11:00:15 -07002726 mPendingPackages.clear();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002727 mPastSignatures.clear();
dcashman8c04fac2015-03-23 11:39:42 -07002728 mKeySetRefs.clear();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002729
2730 try {
2731 if (str == null) {
2732 if (!mSettingsFilename.exists()) {
2733 mReadMessages.append("No settings file found\n");
2734 PackageManagerService.reportSettingsProblem(Log.INFO,
2735 "No settings file; creating initial state");
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002736 // It's enough to just touch version details to create them
2737 // with default values
2738 findOrCreateVersion(StorageManager.UUID_PRIVATE_INTERNAL);
2739 findOrCreateVersion(StorageManager.UUID_PRIMARY_PHYSICAL);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002740 return false;
2741 }
2742 str = new FileInputStream(mSettingsFilename);
2743 }
2744 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002745 parser.setInput(str, StandardCharsets.UTF_8.name());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002746
2747 int type;
2748 while ((type = parser.next()) != XmlPullParser.START_TAG
2749 && type != XmlPullParser.END_DOCUMENT) {
2750 ;
2751 }
2752
2753 if (type != XmlPullParser.START_TAG) {
2754 mReadMessages.append("No start tag found in settings file\n");
2755 PackageManagerService.reportSettingsProblem(Log.WARN,
2756 "No start tag found in package manager settings");
Dianne Hackborn8d051722014-10-01 14:59:58 -07002757 Slog.wtf(PackageManagerService.TAG,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002758 "No start tag found in package manager settings");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002759 return false;
2760 }
2761
2762 int outerDepth = parser.getDepth();
2763 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2764 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2765 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2766 continue;
2767 }
2768
2769 String tagName = parser.getName();
2770 if (tagName.equals("package")) {
Kenny Root447106f2011-03-23 11:00:15 -07002771 readPackageLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002772 } else if (tagName.equals("permissions")) {
Kenny Root447106f2011-03-23 11:00:15 -07002773 readPermissionsLPw(mPermissions, parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002774 } else if (tagName.equals("permission-trees")) {
Kenny Root447106f2011-03-23 11:00:15 -07002775 readPermissionsLPw(mPermissionTrees, parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002776 } else if (tagName.equals("shared-user")) {
Kenny Root447106f2011-03-23 11:00:15 -07002777 readSharedUserLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002778 } else if (tagName.equals("preferred-packages")) {
2779 // no longer used.
2780 } else if (tagName.equals("preferred-activities")) {
Dianne Hackborn63092712012-10-07 14:45:35 -07002781 // Upgrading from old single-user implementation;
2782 // these are the preferred activities for user 0.
2783 readPreferredActivitiesLPw(parser, 0);
Sander Alewijnseaf597622014-03-20 18:44:57 +00002784 } else if (tagName.equals(TAG_PERSISTENT_PREFERRED_ACTIVITIES)) {
Sander Alewijnsef475ca32014-02-17 15:13:58 +00002785 // TODO: check whether this is okay! as it is very
2786 // similar to how preferred-activities are treated
2787 readPersistentPreferredActivitiesLPw(parser, 0);
Nicolas Prevot29762c32014-07-29 18:51:32 +01002788 } else if (tagName.equals(TAG_CROSS_PROFILE_INTENT_FILTERS)) {
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00002789 // TODO: check whether this is okay! as it is very
2790 // similar to how preferred-activities are treated
Nicolas Prevot81948992014-05-16 18:25:26 +01002791 readCrossProfileIntentFiltersLPw(parser, 0);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07002792 } else if (tagName.equals(TAG_DEFAULT_BROWSER)) {
2793 readDefaultAppsLPw(parser, 0);
2794 } else if (tagName.equals("updated-package")) {
Kenny Root447106f2011-03-23 11:00:15 -07002795 readDisabledSysPackageLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002796 } else if (tagName.equals("cleaning-package")) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002797 String name = parser.getAttributeValue(null, ATTR_NAME);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002798 String userStr = parser.getAttributeValue(null, ATTR_USER);
2799 String codeStr = parser.getAttributeValue(null, ATTR_CODE);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002800 if (name != null) {
Xiaohui Chen594f2082015-08-18 11:04:20 -07002801 int userId = UserHandle.USER_SYSTEM;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002802 boolean andCode = true;
2803 try {
2804 if (userStr != null) {
Jeff Sharkey752cd922012-09-23 16:25:12 -07002805 userId = Integer.parseInt(userStr);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002806 }
2807 } catch (NumberFormatException e) {
2808 }
2809 if (codeStr != null) {
2810 andCode = Boolean.parseBoolean(codeStr);
2811 }
Jeff Sharkey752cd922012-09-23 16:25:12 -07002812 addPackageToCleanLPw(new PackageCleanItem(userId, name, andCode));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002813 }
2814 } else if (tagName.equals("renamed-package")) {
2815 String nname = parser.getAttributeValue(null, "new");
2816 String oname = parser.getAttributeValue(null, "old");
2817 if (nname != null && oname != null) {
2818 mRenamedPackages.put(nname, oname);
2819 }
Christopher Tate6038d152015-06-17 13:07:46 -07002820 } else if (tagName.equals("restored-ivi")) {
2821 readRestoredIntentFilterVerifications(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002822 } else if (tagName.equals("last-platform-version")) {
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002823 // Upgrade from older XML schema
2824 final VersionInfo internal = findOrCreateVersion(
2825 StorageManager.UUID_PRIVATE_INTERNAL);
2826 final VersionInfo external = findOrCreateVersion(
2827 StorageManager.UUID_PRIMARY_PHYSICAL);
Svet Ganovadc1cf42015-06-15 16:36:24 -07002828
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002829 internal.sdkVersion = XmlUtils.readIntAttribute(parser, "internal", 0);
2830 external.sdkVersion = XmlUtils.readIntAttribute(parser, "external", 0);
2831 internal.fingerprint = external.fingerprint =
2832 XmlUtils.readStringAttribute(parser, "fingerprint");
2833
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07002834 } else if (tagName.equals("database-version")) {
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002835 // Upgrade from older XML schema
2836 final VersionInfo internal = findOrCreateVersion(
2837 StorageManager.UUID_PRIVATE_INTERNAL);
2838 final VersionInfo external = findOrCreateVersion(
2839 StorageManager.UUID_PRIMARY_PHYSICAL);
2840
2841 internal.databaseVersion = XmlUtils.readIntAttribute(parser, "internal", 0);
2842 external.databaseVersion = XmlUtils.readIntAttribute(parser, "external", 0);
2843
Kenny Root0aaa0d92011-09-12 16:42:55 -07002844 } else if (tagName.equals("verifier")) {
2845 final String deviceIdentity = parser.getAttributeValue(null, "device");
2846 try {
2847 mVerifierDeviceIdentity = VerifierDeviceIdentity.parse(deviceIdentity);
2848 } catch (IllegalArgumentException e) {
2849 Slog.w(PackageManagerService.TAG, "Discard invalid verifier device id: "
2850 + e.getMessage());
2851 }
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002852 } else if (TAG_READ_EXTERNAL_STORAGE.equals(tagName)) {
2853 final String enforcement = parser.getAttributeValue(null, ATTR_ENFORCEMENT);
Jeff Sharkey5d32e772012-04-12 15:59:23 -07002854 mReadExternalStorageEnforced = "1".equals(enforcement);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002855 } else if (tagName.equals("keyset-settings")) {
dcashman8c04fac2015-03-23 11:39:42 -07002856 mKeySetManagerService.readKeySetsLPw(parser, mKeySetRefs);
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002857 } else if (TAG_VERSION.equals(tagName)) {
2858 final String volumeUuid = XmlUtils.readStringAttribute(parser,
2859 ATTR_VOLUME_UUID);
2860 final VersionInfo ver = findOrCreateVersion(volumeUuid);
2861 ver.sdkVersion = XmlUtils.readIntAttribute(parser, ATTR_SDK_VERSION);
2862 ver.databaseVersion = XmlUtils.readIntAttribute(parser, ATTR_SDK_VERSION);
2863 ver.fingerprint = XmlUtils.readStringAttribute(parser, ATTR_FINGERPRINT);
2864
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002865 } else {
2866 Slog.w(PackageManagerService.TAG, "Unknown element under <packages>: "
2867 + parser.getName());
2868 XmlUtils.skipCurrentTag(parser);
2869 }
2870 }
2871
2872 str.close();
2873
2874 } catch (XmlPullParserException e) {
2875 mReadMessages.append("Error reading: " + e.toString());
2876 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002877 Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002878
2879 } catch (java.io.IOException e) {
2880 mReadMessages.append("Error reading: " + e.toString());
2881 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002882 Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
Amith Yamasani258848d2012-08-10 17:06:33 -07002883 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002884
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002885 // If the build is setup to drop runtime permissions
2886 // on update drop the files before loading them.
2887 if (PackageManagerService.CLEAR_RUNTIME_PERMISSIONS_ON_UPGRADE) {
2888 final VersionInfo internal = getInternalVersion();
2889 if (!Build.FINGERPRINT.equals(internal.fingerprint)) {
Xiaohui Chen594f2082015-08-18 11:04:20 -07002890 for (UserInfo user : users) {
2891 mRuntimePermissionsPersistence.deleteUserRuntimePermissionsFile(user.id);
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07002892 }
2893 }
2894 }
2895
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002896 final int N = mPendingPackages.size();
dcashman8c04fac2015-03-23 11:39:42 -07002897
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002898 for (int i = 0; i < N; i++) {
2899 final PendingPackage pp = mPendingPackages.get(i);
Kenny Root447106f2011-03-23 11:00:15 -07002900 Object idObj = getUserIdLPr(pp.sharedId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002901 if (idObj != null && idObj instanceof SharedUserSetting) {
Kenny Root447106f2011-03-23 11:00:15 -07002902 PackageSetting p = getPackageLPw(pp.name, null, pp.realName,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002903 (SharedUserSetting) idObj, pp.codePath, pp.resourcePath,
Narayan Kamathff110bd2014-07-04 18:30:45 +01002904 pp.legacyNativeLibraryPathString, pp.primaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002905 pp.secondaryCpuAbiString, pp.versionCode, pp.pkgFlags, pp.pkgPrivateFlags,
Svet Ganov354cd3c2015-12-17 11:35:04 -08002906 null, true /* add */, false /* allowInstall */, pp.parentPackageName,
2907 pp.childPackageNames);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002908 if (p == null) {
2909 PackageManagerService.reportSettingsProblem(Log.WARN,
2910 "Unable to create application package for " + pp.name);
2911 continue;
2912 }
2913 p.copyFrom(pp);
2914 } else if (idObj != null) {
2915 String msg = "Bad package setting: package " + pp.name + " has shared uid "
2916 + pp.sharedId + " that is not a shared uid\n";
2917 mReadMessages.append(msg);
2918 PackageManagerService.reportSettingsProblem(Log.ERROR, msg);
2919 } else {
2920 String msg = "Bad package setting: package " + pp.name + " has shared uid "
2921 + pp.sharedId + " that is not defined\n";
2922 mReadMessages.append(msg);
2923 PackageManagerService.reportSettingsProblem(Log.ERROR, msg);
2924 }
2925 }
2926 mPendingPackages.clear();
2927
Amith Yamasanif031f232012-10-26 15:35:21 -07002928 if (mBackupStoppedPackagesFilename.exists()
2929 || mStoppedPackagesFilename.exists()) {
2930 // Read old file
2931 readStoppedLPw();
2932 mBackupStoppedPackagesFilename.delete();
2933 mStoppedPackagesFilename.delete();
2934 // Migrate to new file format
Xiaohui Chen594f2082015-08-18 11:04:20 -07002935 writePackageRestrictionsLPr(UserHandle.USER_SYSTEM);
Amith Yamasanif031f232012-10-26 15:35:21 -07002936 } else {
Xiaohui Chen594f2082015-08-18 11:04:20 -07002937 for (UserInfo user : users) {
2938 readPackageRestrictionsLPr(user.id);
Amith Yamasanif031f232012-10-26 15:35:21 -07002939 }
2940 }
2941
Xiaohui Chen594f2082015-08-18 11:04:20 -07002942 for (UserInfo user : users) {
2943 mRuntimePermissionsPersistence.readStateForUserSyncLPr(user.id);
Svet Ganovadc1cf42015-06-15 16:36:24 -07002944 }
2945
Kenny Root1d1b4892011-04-08 14:25:24 -07002946 /*
2947 * Make sure all the updated system packages have their shared users
2948 * associated with them.
2949 */
2950 final Iterator<PackageSetting> disabledIt = mDisabledSysPackages.values().iterator();
2951 while (disabledIt.hasNext()) {
2952 final PackageSetting disabledPs = disabledIt.next();
Amith Yamasani13593602012-03-22 16:16:17 -07002953 final Object id = getUserIdLPr(disabledPs.appId);
Kenny Root1d1b4892011-04-08 14:25:24 -07002954 if (id != null && id instanceof SharedUserSetting) {
2955 disabledPs.sharedUser = (SharedUserSetting) id;
2956 }
2957 }
2958
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002959 mReadMessages.append("Read completed successfully: " + mPackages.size() + " packages, "
2960 + mSharedUsers.size() + " shared uids\n");
2961
Jeff Sharkey2271ba32016-02-01 17:57:08 -07002962 writeKernelMappingLPr();
2963
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002964 return true;
2965 }
2966
Christopher Tatedb3fe812015-06-24 16:15:48 -07002967 void applyDefaultPreferredAppsLPw(PackageManagerService service, int userId) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002968 // First pull data from any pre-installed apps.
2969 for (PackageSetting ps : mPackages.values()) {
2970 if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0 && ps.pkg != null
2971 && ps.pkg.preferredActivityFilters != null) {
2972 ArrayList<PackageParser.ActivityIntentInfo> intents
2973 = ps.pkg.preferredActivityFilters;
2974 for (int i=0; i<intents.size(); i++) {
2975 PackageParser.ActivityIntentInfo aii = intents.get(i);
2976 applyDefaultPreferredActivityLPw(service, aii, new ComponentName(
2977 ps.name, aii.activity.className), userId);
2978 }
2979 }
2980 }
2981
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07002982 // Read preferred apps from .../etc/preferred-apps directory.
2983 File preferredDir = new File(Environment.getRootDirectory(), "etc/preferred-apps");
2984 if (!preferredDir.exists() || !preferredDir.isDirectory()) {
2985 return;
2986 }
2987 if (!preferredDir.canRead()) {
2988 Slog.w(TAG, "Directory " + preferredDir + " cannot be read");
2989 return;
2990 }
2991
2992 // Iterate over the files in the directory and scan .xml files
2993 for (File f : preferredDir.listFiles()) {
2994 if (!f.getPath().endsWith(".xml")) {
2995 Slog.i(TAG, "Non-xml file " + f + " in " + preferredDir + " directory, ignoring");
2996 continue;
2997 }
2998 if (!f.canRead()) {
2999 Slog.w(TAG, "Preferred apps file " + f + " cannot be read");
3000 continue;
3001 }
3002
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003003 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Reading default preferred " + f);
Fyodor Kupolovb35b34c2015-12-17 14:16:51 -08003004 InputStream str = null;
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07003005 try {
Fyodor Kupolovb35b34c2015-12-17 14:16:51 -08003006 str = new BufferedInputStream(new FileInputStream(f));
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07003007 XmlPullParser parser = Xml.newPullParser();
3008 parser.setInput(str, null);
3009
3010 int type;
3011 while ((type = parser.next()) != XmlPullParser.START_TAG
3012 && type != XmlPullParser.END_DOCUMENT) {
3013 ;
3014 }
3015
3016 if (type != XmlPullParser.START_TAG) {
3017 Slog.w(TAG, "Preferred apps file " + f + " does not have start tag");
3018 continue;
3019 }
3020 if (!"preferred-activities".equals(parser.getName())) {
3021 Slog.w(TAG, "Preferred apps file " + f
3022 + " does not start with 'preferred-activities'");
3023 continue;
3024 }
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003025 readDefaultPreferredActivitiesLPw(service, parser, userId);
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07003026 } catch (XmlPullParserException e) {
3027 Slog.w(TAG, "Error reading apps file " + f, e);
3028 } catch (IOException e) {
3029 Slog.w(TAG, "Error reading apps file " + f, e);
3030 } finally {
3031 if (str != null) {
3032 try {
3033 str.close();
3034 } catch (IOException e) {
3035 }
3036 }
3037 }
3038 }
3039 }
3040
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003041 private void applyDefaultPreferredActivityLPw(PackageManagerService service,
3042 IntentFilter tmpPa, ComponentName cn, int userId) {
3043 // The initial preferences only specify the target activity
3044 // component and intent-filter, not the set of matches. So we
3045 // now need to query for the matches to build the correct
3046 // preferred activity entry.
3047 if (PackageManagerService.DEBUG_PREFERRED) {
3048 Log.d(TAG, "Processing preferred:");
3049 tmpPa.dump(new LogPrinter(Log.DEBUG, TAG), " ");
3050 }
3051 Intent intent = new Intent();
Jeff Sharkey258be562016-03-01 15:15:55 -07003052 int flags = PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003053 intent.setAction(tmpPa.getAction(0));
3054 for (int i=0; i<tmpPa.countCategories(); i++) {
3055 String cat = tmpPa.getCategory(i);
3056 if (cat.equals(Intent.CATEGORY_DEFAULT)) {
Jeff Sharkey2a90f6732016-01-06 12:26:11 -07003057 flags |= MATCH_DEFAULT_ONLY;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003058 } else {
3059 intent.addCategory(cat);
3060 }
3061 }
3062
3063 boolean doNonData = true;
Christopher Tate2298ef22013-11-04 17:02:10 -08003064 boolean hasSchemes = false;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003065
3066 for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) {
3067 boolean doScheme = true;
3068 String scheme = tmpPa.getDataScheme(ischeme);
Christopher Tate2298ef22013-11-04 17:02:10 -08003069 if (scheme != null && !scheme.isEmpty()) {
3070 hasSchemes = true;
3071 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003072 for (int issp=0; issp<tmpPa.countDataSchemeSpecificParts(); issp++) {
3073 Uri.Builder builder = new Uri.Builder();
3074 builder.scheme(scheme);
3075 PatternMatcher ssp = tmpPa.getDataSchemeSpecificPart(issp);
3076 builder.opaquePart(ssp.getPath());
3077 Intent finalIntent = new Intent(intent);
3078 finalIntent.setData(builder.build());
3079 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003080 scheme, ssp, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003081 doScheme = false;
3082 }
3083 for (int iauth=0; iauth<tmpPa.countDataAuthorities(); iauth++) {
3084 boolean doAuth = true;
3085 IntentFilter.AuthorityEntry auth = tmpPa.getDataAuthority(iauth);
3086 for (int ipath=0; ipath<tmpPa.countDataPaths(); ipath++) {
3087 Uri.Builder builder = new Uri.Builder();
3088 builder.scheme(scheme);
3089 if (auth.getHost() != null) {
3090 builder.authority(auth.getHost());
3091 }
3092 PatternMatcher path = tmpPa.getDataPath(ipath);
3093 builder.path(path.getPath());
3094 Intent finalIntent = new Intent(intent);
3095 finalIntent.setData(builder.build());
3096 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003097 scheme, null, auth, path, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003098 doAuth = doScheme = false;
3099 }
3100 if (doAuth) {
3101 Uri.Builder builder = new Uri.Builder();
3102 builder.scheme(scheme);
3103 if (auth.getHost() != null) {
3104 builder.authority(auth.getHost());
3105 }
3106 Intent finalIntent = new Intent(intent);
3107 finalIntent.setData(builder.build());
3108 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003109 scheme, null, auth, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003110 doScheme = false;
3111 }
3112 }
3113 if (doScheme) {
3114 Uri.Builder builder = new Uri.Builder();
3115 builder.scheme(scheme);
3116 Intent finalIntent = new Intent(intent);
3117 finalIntent.setData(builder.build());
3118 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003119 scheme, null, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003120 }
3121 doNonData = false;
3122 }
3123
3124 for (int idata=0; idata<tmpPa.countDataTypes(); idata++) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003125 String mimeType = tmpPa.getDataType(idata);
Christopher Tate2298ef22013-11-04 17:02:10 -08003126 if (hasSchemes) {
3127 Uri.Builder builder = new Uri.Builder();
3128 for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) {
3129 String scheme = tmpPa.getDataScheme(ischeme);
3130 if (scheme != null && !scheme.isEmpty()) {
3131 Intent finalIntent = new Intent(intent);
3132 builder.scheme(scheme);
3133 finalIntent.setDataAndType(builder.build(), mimeType);
3134 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003135 scheme, null, null, null, userId);
Christopher Tate2298ef22013-11-04 17:02:10 -08003136 }
3137 }
3138 } else {
3139 Intent finalIntent = new Intent(intent);
3140 finalIntent.setType(mimeType);
3141 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003142 null, null, null, null, userId);
Christopher Tate2298ef22013-11-04 17:02:10 -08003143 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003144 doNonData = false;
3145 }
3146
3147 if (doNonData) {
3148 applyDefaultPreferredActivityLPw(service, intent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003149 null, null, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003150 }
3151 }
3152
3153 private void applyDefaultPreferredActivityLPw(PackageManagerService service,
3154 Intent intent, int flags, ComponentName cn, String scheme, PatternMatcher ssp,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003155 IntentFilter.AuthorityEntry auth, PatternMatcher path, int userId) {
Svetoslav Ganov5fd83dc2016-01-20 17:27:48 -08003156 flags = service.updateFlagsForResolve(flags, userId, intent);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003157 List<ResolveInfo> ri = service.mActivities.queryIntent(intent,
3158 intent.getType(), flags, 0);
3159 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Queried " + intent
3160 + " results: " + ri);
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003161 int systemMatch = 0;
3162 int thirdPartyMatch = 0;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003163 if (ri != null && ri.size() > 1) {
3164 boolean haveAct = false;
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003165 ComponentName haveNonSys = null;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003166 ComponentName[] set = new ComponentName[ri.size()];
3167 for (int i=0; i<ri.size(); i++) {
3168 ActivityInfo ai = ri.get(i).activityInfo;
3169 set[i] = new ComponentName(ai.packageName, ai.name);
3170 if ((ai.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003171 if (ri.get(i).match >= thirdPartyMatch) {
3172 // Keep track of the best match we find of all third
3173 // party apps, for use later to determine if we actually
3174 // want to set a preferred app for this intent.
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003175 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
3176 + ai.packageName + "/" + ai.name + ": non-system!");
3177 haveNonSys = set[i];
3178 break;
3179 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003180 } else if (cn.getPackageName().equals(ai.packageName)
3181 && cn.getClassName().equals(ai.name)) {
3182 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
3183 + ai.packageName + "/" + ai.name + ": default!");
3184 haveAct = true;
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003185 systemMatch = ri.get(i).match;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003186 } else {
3187 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
3188 + ai.packageName + "/" + ai.name + ": skipped");
3189 }
3190 }
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003191 if (haveNonSys != null && thirdPartyMatch < systemMatch) {
3192 // If we have a matching third party app, but its match is not as
3193 // good as the built-in system app, then we don't want to actually
3194 // consider it a match because presumably the built-in app is still
3195 // the thing we want users to see by default.
3196 haveNonSys = null;
3197 }
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003198 if (haveAct && haveNonSys == null) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003199 IntentFilter filter = new IntentFilter();
3200 if (intent.getAction() != null) {
3201 filter.addAction(intent.getAction());
3202 }
Erin Dahlgren707a59d2013-10-24 15:13:39 -07003203 if (intent.getCategories() != null) {
3204 for (String cat : intent.getCategories()) {
3205 filter.addCategory(cat);
3206 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003207 }
Jeff Sharkey2a90f6732016-01-06 12:26:11 -07003208 if ((flags & MATCH_DEFAULT_ONLY) != 0) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003209 filter.addCategory(Intent.CATEGORY_DEFAULT);
3210 }
3211 if (scheme != null) {
3212 filter.addDataScheme(scheme);
3213 }
3214 if (ssp != null) {
3215 filter.addDataSchemeSpecificPart(ssp.getPath(), ssp.getType());
3216 }
3217 if (auth != null) {
3218 filter.addDataAuthority(auth);
3219 }
3220 if (path != null) {
3221 filter.addDataPath(path);
3222 }
Erin Dahlgren707a59d2013-10-24 15:13:39 -07003223 if (intent.getType() != null) {
3224 try {
3225 filter.addDataType(intent.getType());
3226 } catch (IntentFilter.MalformedMimeTypeException ex) {
3227 Slog.w(TAG, "Malformed mimetype " + intent.getType() + " for " + cn);
3228 }
3229 }
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07003230 PreferredActivity pa = new PreferredActivity(filter, systemMatch, set, cn, true);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003231 editPreferredActivitiesLPw(userId).addFilter(pa);
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003232 } else if (haveNonSys == null) {
3233 StringBuilder sb = new StringBuilder();
3234 sb.append("No component ");
3235 sb.append(cn.flattenToShortString());
3236 sb.append(" found setting preferred ");
3237 sb.append(intent);
3238 sb.append("; possible matches are ");
3239 for (int i=0; i<set.length; i++) {
3240 if (i > 0) sb.append(", ");
3241 sb.append(set[i].flattenToShortString());
3242 }
3243 Slog.w(TAG, sb.toString());
3244 } else {
3245 Slog.i(TAG, "Not setting preferred " + intent + "; found third party match "
3246 + haveNonSys.flattenToShortString());
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003247 }
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07003248 } else {
3249 Slog.w(TAG, "No potential matches found for " + intent + " while setting preferred "
3250 + cn.flattenToShortString());
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003251 }
3252 }
3253
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003254 private void readDefaultPreferredActivitiesLPw(PackageManagerService service,
3255 XmlPullParser parser, int userId)
3256 throws XmlPullParserException, IOException {
3257 int outerDepth = parser.getDepth();
3258 int type;
3259 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3260 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3261 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3262 continue;
3263 }
3264
3265 String tagName = parser.getName();
3266 if (tagName.equals(TAG_ITEM)) {
3267 PreferredActivity tmpPa = new PreferredActivity(parser);
3268 if (tmpPa.mPref.getParseError() == null) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003269 applyDefaultPreferredActivityLPw(service, tmpPa, tmpPa.mPref.mComponent,
3270 userId);
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003271 } else {
3272 PackageManagerService.reportSettingsProblem(Log.WARN,
3273 "Error in package manager settings: <preferred-activity> "
3274 + tmpPa.mPref.getParseError() + " at "
3275 + parser.getPositionDescription());
3276 }
3277 } else {
3278 PackageManagerService.reportSettingsProblem(Log.WARN,
3279 "Unknown element under <preferred-activities>: " + parser.getName());
3280 XmlUtils.skipCurrentTag(parser);
3281 }
3282 }
3283 }
3284
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003285 private int readInt(XmlPullParser parser, String ns, String name, int defValue) {
3286 String v = parser.getAttributeValue(ns, name);
3287 try {
3288 if (v == null) {
3289 return defValue;
3290 }
3291 return Integer.parseInt(v);
3292 } catch (NumberFormatException e) {
3293 PackageManagerService.reportSettingsProblem(Log.WARN,
3294 "Error in package manager settings: attribute " + name
3295 + " has bad integer value " + v + " at "
3296 + parser.getPositionDescription());
3297 }
3298 return defValue;
3299 }
3300
Jeff Sharkey9f837a92014-10-24 12:07:24 -07003301 private void readPermissionsLPw(ArrayMap<String, BasePermission> out, XmlPullParser parser)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003302 throws IOException, XmlPullParserException {
3303 int outerDepth = parser.getDepth();
3304 int type;
3305 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3306 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3307 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3308 continue;
3309 }
3310
Kenny Root447106f2011-03-23 11:00:15 -07003311 final String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003312 if (tagName.equals(TAG_ITEM)) {
3313 final String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Root447106f2011-03-23 11:00:15 -07003314 final String sourcePackage = parser.getAttributeValue(null, "package");
3315 final String ptype = parser.getAttributeValue(null, "type");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003316 if (name != null && sourcePackage != null) {
Kenny Root447106f2011-03-23 11:00:15 -07003317 final boolean dynamic = "dynamic".equals(ptype);
Svetoslavc6d1c342015-02-26 14:44:43 -08003318 final BasePermission bp = new BasePermission(name.intern(), sourcePackage,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003319 dynamic ? BasePermission.TYPE_DYNAMIC : BasePermission.TYPE_NORMAL);
3320 bp.protectionLevel = readInt(parser, null, "protection",
3321 PermissionInfo.PROTECTION_NORMAL);
Dianne Hackborne639da72012-02-21 15:11:13 -08003322 bp.protectionLevel = PermissionInfo.fixProtectionLevel(bp.protectionLevel);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003323 if (dynamic) {
3324 PermissionInfo pi = new PermissionInfo();
3325 pi.packageName = sourcePackage.intern();
3326 pi.name = name.intern();
3327 pi.icon = readInt(parser, null, "icon", 0);
3328 pi.nonLocalizedLabel = parser.getAttributeValue(null, "label");
3329 pi.protectionLevel = bp.protectionLevel;
3330 bp.pendingInfo = pi;
3331 }
3332 out.put(bp.name, bp);
3333 } else {
3334 PackageManagerService.reportSettingsProblem(Log.WARN,
3335 "Error in package manager settings: permissions has" + " no name at "
3336 + parser.getPositionDescription());
3337 }
3338 } else {
3339 PackageManagerService.reportSettingsProblem(Log.WARN,
3340 "Unknown element reading permissions: " + parser.getName() + " at "
3341 + parser.getPositionDescription());
3342 }
3343 XmlUtils.skipCurrentTag(parser);
3344 }
3345 }
3346
Kenny Root447106f2011-03-23 11:00:15 -07003347 private void readDisabledSysPackageLPw(XmlPullParser parser) throws XmlPullParserException,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003348 IOException {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003349 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003350 String realName = parser.getAttributeValue(null, "realName");
3351 String codePathStr = parser.getAttributeValue(null, "codePath");
3352 String resourcePathStr = parser.getAttributeValue(null, "resourcePath");
Narayan Kamathff110bd2014-07-04 18:30:45 +01003353
3354 String legacyCpuAbiStr = parser.getAttributeValue(null, "requiredCpuAbi");
3355 String legacyNativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath");
3356
Svet Ganov354cd3c2015-12-17 11:35:04 -08003357 String parentPackageName = parser.getAttributeValue(null, "parentPackageName");
3358
Narayan Kamathff110bd2014-07-04 18:30:45 +01003359 String primaryCpuAbiStr = parser.getAttributeValue(null, "primaryCpuAbi");
3360 String secondaryCpuAbiStr = parser.getAttributeValue(null, "secondaryCpuAbi");
Narayan Kamath4903f642014-08-11 13:33:45 +01003361 String cpuAbiOverrideStr = parser.getAttributeValue(null, "cpuAbiOverride");
Narayan Kamathff110bd2014-07-04 18:30:45 +01003362
3363 if (primaryCpuAbiStr == null && legacyCpuAbiStr != null) {
3364 primaryCpuAbiStr = legacyCpuAbiStr;
3365 }
Narayan Kamath9e289d72014-04-10 09:26:59 +00003366
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003367 if (resourcePathStr == null) {
3368 resourcePathStr = codePathStr;
3369 }
3370 String version = parser.getAttributeValue(null, "version");
3371 int versionCode = 0;
3372 if (version != null) {
3373 try {
3374 versionCode = Integer.parseInt(version);
3375 } catch (NumberFormatException e) {
3376 }
3377 }
3378
3379 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003380 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003381 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
Christopher Tate628946a2013-10-18 18:11:05 -07003382 final File codePathFile = new File(codePathStr);
3383 if (PackageManagerService.locationIsPrivileged(codePathFile)) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003384 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Christopher Tate628946a2013-10-18 18:11:05 -07003385 }
3386 PackageSetting ps = new PackageSetting(name, realName, codePathFile,
Narayan Kamathff110bd2014-07-04 18:30:45 +01003387 new File(resourcePathStr), legacyNativeLibraryPathStr, primaryCpuAbiStr,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003388 secondaryCpuAbiStr, cpuAbiOverrideStr, versionCode, pkgFlags, pkgPrivateFlags,
3389 parentPackageName, null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003390 String timeStampStr = parser.getAttributeValue(null, "ft");
3391 if (timeStampStr != null) {
3392 try {
3393 long timeStamp = Long.parseLong(timeStampStr, 16);
3394 ps.setTimeStamp(timeStamp);
3395 } catch (NumberFormatException e) {
3396 }
3397 } else {
3398 timeStampStr = parser.getAttributeValue(null, "ts");
3399 if (timeStampStr != null) {
3400 try {
3401 long timeStamp = Long.parseLong(timeStampStr);
3402 ps.setTimeStamp(timeStamp);
3403 } catch (NumberFormatException e) {
3404 }
3405 }
3406 }
3407 timeStampStr = parser.getAttributeValue(null, "it");
3408 if (timeStampStr != null) {
3409 try {
3410 ps.firstInstallTime = Long.parseLong(timeStampStr, 16);
3411 } catch (NumberFormatException e) {
3412 }
3413 }
3414 timeStampStr = parser.getAttributeValue(null, "ut");
3415 if (timeStampStr != null) {
3416 try {
3417 ps.lastUpdateTime = Long.parseLong(timeStampStr, 16);
3418 } catch (NumberFormatException e) {
3419 }
3420 }
3421 String idStr = parser.getAttributeValue(null, "userId");
Amith Yamasani13593602012-03-22 16:16:17 -07003422 ps.appId = idStr != null ? Integer.parseInt(idStr) : 0;
3423 if (ps.appId <= 0) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003424 String sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
Amith Yamasani13593602012-03-22 16:16:17 -07003425 ps.appId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003426 }
Svetoslavc6d1c342015-02-26 14:44:43 -08003427
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003428 int outerDepth = parser.getDepth();
3429 int type;
3430 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3431 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3432 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3433 continue;
3434 }
3435
Svetoslavc6d1c342015-02-26 14:44:43 -08003436 if (parser.getName().equals(TAG_PERMISSIONS)) {
3437 readInstallPermissionsLPr(parser, ps.getPermissionsState());
Svet Ganov354cd3c2015-12-17 11:35:04 -08003438 } else if (parser.getName().equals(TAG_CHILD_PACKAGE)) {
3439 String childPackageName = parser.getAttributeValue(null, ATTR_NAME);
3440 if (ps.childPackageNames == null) {
3441 ps.childPackageNames = new ArrayList<>();
3442 }
3443 ps.childPackageNames.add(childPackageName);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003444 } else {
3445 PackageManagerService.reportSettingsProblem(Log.WARN,
3446 "Unknown element under <updated-package>: " + parser.getName());
3447 XmlUtils.skipCurrentTag(parser);
3448 }
3449 }
Christopher Tate628946a2013-10-18 18:11:05 -07003450
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003451 mDisabledSysPackages.put(name, ps);
3452 }
3453
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003454 private static int PRE_M_APP_INFO_FLAG_HIDDEN = 1<<27;
3455 private static int PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE = 1<<28;
3456 private static int PRE_M_APP_INFO_FLAG_FORWARD_LOCK = 1<<29;
3457 private static int PRE_M_APP_INFO_FLAG_PRIVILEGED = 1<<30;
3458
Kenny Root447106f2011-03-23 11:00:15 -07003459 private void readPackageLPw(XmlPullParser parser) throws XmlPullParserException, IOException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003460 String name = null;
3461 String realName = null;
3462 String idStr = null;
3463 String sharedIdStr = null;
3464 String codePathStr = null;
3465 String resourcePathStr = null;
Narayan Kamathff110bd2014-07-04 18:30:45 +01003466 String legacyCpuAbiString = null;
3467 String legacyNativeLibraryPathStr = null;
3468 String primaryCpuAbiString = null;
3469 String secondaryCpuAbiString = null;
Narayan Kamath4903f642014-08-11 13:33:45 +01003470 String cpuAbiOverrideString = null;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003471 String systemStr = null;
3472 String installerPackageName = null;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003473 String volumeUuid = null;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003474 String uidError = null;
3475 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003476 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003477 long timeStamp = 0;
3478 long firstInstallTime = 0;
3479 long lastUpdateTime = 0;
3480 PackageSettingBase packageSetting = null;
3481 String version = null;
3482 int versionCode = 0;
Svet Ganov354cd3c2015-12-17 11:35:04 -08003483 String parentPackageName;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003484 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003485 name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003486 realName = parser.getAttributeValue(null, "realName");
3487 idStr = parser.getAttributeValue(null, "userId");
3488 uidError = parser.getAttributeValue(null, "uidError");
3489 sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
3490 codePathStr = parser.getAttributeValue(null, "codePath");
3491 resourcePathStr = parser.getAttributeValue(null, "resourcePath");
Narayan Kamath9e289d72014-04-10 09:26:59 +00003492
Narayan Kamathff110bd2014-07-04 18:30:45 +01003493 legacyCpuAbiString = parser.getAttributeValue(null, "requiredCpuAbi");
3494
Svet Ganov354cd3c2015-12-17 11:35:04 -08003495 parentPackageName = parser.getAttributeValue(null, "parentPackageName");
3496
Narayan Kamathff110bd2014-07-04 18:30:45 +01003497 legacyNativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath");
3498 primaryCpuAbiString = parser.getAttributeValue(null, "primaryCpuAbi");
3499 secondaryCpuAbiString = parser.getAttributeValue(null, "secondaryCpuAbi");
Narayan Kamath4903f642014-08-11 13:33:45 +01003500 cpuAbiOverrideString = parser.getAttributeValue(null, "cpuAbiOverride");
Narayan Kamathff110bd2014-07-04 18:30:45 +01003501
3502 if (primaryCpuAbiString == null && legacyCpuAbiString != null) {
3503 primaryCpuAbiString = legacyCpuAbiString;
3504 }
Svetoslavc6d1c342015-02-26 14:44:43 -08003505
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003506 version = parser.getAttributeValue(null, "version");
3507 if (version != null) {
3508 try {
3509 versionCode = Integer.parseInt(version);
3510 } catch (NumberFormatException e) {
3511 }
3512 }
3513 installerPackageName = parser.getAttributeValue(null, "installer");
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003514 volumeUuid = parser.getAttributeValue(null, "volumeUuid");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003515
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003516 systemStr = parser.getAttributeValue(null, "publicFlags");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003517 if (systemStr != null) {
3518 try {
3519 pkgFlags = Integer.parseInt(systemStr);
3520 } catch (NumberFormatException e) {
3521 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003522 systemStr = parser.getAttributeValue(null, "privateFlags");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003523 if (systemStr != null) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003524 try {
3525 pkgPrivateFlags = Integer.parseInt(systemStr);
3526 } catch (NumberFormatException e) {
3527 }
3528 }
3529 } else {
3530 // Pre-M -- both public and private flags were stored in one "flags" field.
3531 systemStr = parser.getAttributeValue(null, "flags");
3532 if (systemStr != null) {
3533 try {
3534 pkgFlags = Integer.parseInt(systemStr);
3535 } catch (NumberFormatException e) {
3536 }
3537 if ((pkgFlags & PRE_M_APP_INFO_FLAG_HIDDEN) != 0) {
3538 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_HIDDEN;
3539 }
3540 if ((pkgFlags & PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE) != 0) {
3541 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE;
3542 }
3543 if ((pkgFlags & PRE_M_APP_INFO_FLAG_FORWARD_LOCK) != 0) {
3544 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK;
3545 }
3546 if ((pkgFlags & PRE_M_APP_INFO_FLAG_PRIVILEGED) != 0) {
3547 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
3548 }
3549 pkgFlags &= ~(PRE_M_APP_INFO_FLAG_HIDDEN
3550 | PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE
3551 | PRE_M_APP_INFO_FLAG_FORWARD_LOCK
3552 | PRE_M_APP_INFO_FLAG_PRIVILEGED);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003553 } else {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003554 // For backward compatibility
3555 systemStr = parser.getAttributeValue(null, "system");
3556 if (systemStr != null) {
3557 pkgFlags |= ("true".equalsIgnoreCase(systemStr)) ? ApplicationInfo.FLAG_SYSTEM
3558 : 0;
3559 } else {
3560 // Old settings that don't specify system... just treat
3561 // them as system, good enough.
3562 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
3563 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003564 }
3565 }
3566 String timeStampStr = parser.getAttributeValue(null, "ft");
3567 if (timeStampStr != null) {
3568 try {
3569 timeStamp = Long.parseLong(timeStampStr, 16);
3570 } catch (NumberFormatException e) {
3571 }
3572 } else {
3573 timeStampStr = parser.getAttributeValue(null, "ts");
3574 if (timeStampStr != null) {
3575 try {
3576 timeStamp = Long.parseLong(timeStampStr);
3577 } catch (NumberFormatException e) {
3578 }
3579 }
3580 }
3581 timeStampStr = parser.getAttributeValue(null, "it");
3582 if (timeStampStr != null) {
3583 try {
3584 firstInstallTime = Long.parseLong(timeStampStr, 16);
3585 } catch (NumberFormatException e) {
3586 }
3587 }
3588 timeStampStr = parser.getAttributeValue(null, "ut");
3589 if (timeStampStr != null) {
3590 try {
3591 lastUpdateTime = Long.parseLong(timeStampStr, 16);
3592 } catch (NumberFormatException e) {
3593 }
3594 }
3595 if (PackageManagerService.DEBUG_SETTINGS)
3596 Log.v(PackageManagerService.TAG, "Reading package: " + name + " userId=" + idStr
3597 + " sharedUserId=" + sharedIdStr);
3598 int userId = idStr != null ? Integer.parseInt(idStr) : 0;
3599 if (resourcePathStr == null) {
3600 resourcePathStr = codePathStr;
3601 }
3602 if (realName != null) {
3603 realName = realName.intern();
3604 }
3605 if (name == null) {
3606 PackageManagerService.reportSettingsProblem(Log.WARN,
3607 "Error in package manager settings: <package> has no name at "
3608 + parser.getPositionDescription());
3609 } else if (codePathStr == null) {
3610 PackageManagerService.reportSettingsProblem(Log.WARN,
3611 "Error in package manager settings: <package> has no codePath at "
3612 + parser.getPositionDescription());
3613 } else if (userId > 0) {
Kenny Root447106f2011-03-23 11:00:15 -07003614 packageSetting = addPackageLPw(name.intern(), realName, new File(codePathStr),
Narayan Kamathff110bd2014-07-04 18:30:45 +01003615 new File(resourcePathStr), legacyNativeLibraryPathStr, primaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003616 secondaryCpuAbiString, cpuAbiOverrideString, userId, versionCode, pkgFlags,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003617 pkgPrivateFlags, parentPackageName, null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003618 if (PackageManagerService.DEBUG_SETTINGS)
3619 Log.i(PackageManagerService.TAG, "Reading package " + name + ": userId="
3620 + userId + " pkg=" + packageSetting);
3621 if (packageSetting == null) {
3622 PackageManagerService.reportSettingsProblem(Log.ERROR, "Failure adding uid "
3623 + userId + " while parsing settings at "
3624 + parser.getPositionDescription());
3625 } else {
3626 packageSetting.setTimeStamp(timeStamp);
3627 packageSetting.firstInstallTime = firstInstallTime;
3628 packageSetting.lastUpdateTime = lastUpdateTime;
3629 }
3630 } else if (sharedIdStr != null) {
3631 userId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
3632 if (userId > 0) {
3633 packageSetting = new PendingPackage(name.intern(), realName, new File(
Narayan Kamathff110bd2014-07-04 18:30:45 +01003634 codePathStr), new File(resourcePathStr), legacyNativeLibraryPathStr,
Narayan Kamath4903f642014-08-11 13:33:45 +01003635 primaryCpuAbiString, secondaryCpuAbiString, cpuAbiOverrideString,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003636 userId, versionCode, pkgFlags, pkgPrivateFlags, parentPackageName,
3637 null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003638 packageSetting.setTimeStamp(timeStamp);
3639 packageSetting.firstInstallTime = firstInstallTime;
3640 packageSetting.lastUpdateTime = lastUpdateTime;
3641 mPendingPackages.add((PendingPackage) packageSetting);
3642 if (PackageManagerService.DEBUG_SETTINGS)
3643 Log.i(PackageManagerService.TAG, "Reading package " + name
3644 + ": sharedUserId=" + userId + " pkg=" + packageSetting);
3645 } else {
3646 PackageManagerService.reportSettingsProblem(Log.WARN,
3647 "Error in package manager settings: package " + name
3648 + " has bad sharedId " + sharedIdStr + " at "
3649 + parser.getPositionDescription());
3650 }
3651 } else {
3652 PackageManagerService.reportSettingsProblem(Log.WARN,
3653 "Error in package manager settings: package " + name + " has bad userId "
3654 + idStr + " at " + parser.getPositionDescription());
3655 }
3656 } catch (NumberFormatException e) {
3657 PackageManagerService.reportSettingsProblem(Log.WARN,
3658 "Error in package manager settings: package " + name + " has bad userId "
3659 + idStr + " at " + parser.getPositionDescription());
3660 }
3661 if (packageSetting != null) {
3662 packageSetting.uidError = "true".equals(uidError);
3663 packageSetting.installerPackageName = installerPackageName;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003664 packageSetting.volumeUuid = volumeUuid;
Narayan Kamathff110bd2014-07-04 18:30:45 +01003665 packageSetting.legacyNativeLibraryPathString = legacyNativeLibraryPathStr;
3666 packageSetting.primaryCpuAbiString = primaryCpuAbiString;
3667 packageSetting.secondaryCpuAbiString = secondaryCpuAbiString;
Amith Yamasani483f3b02012-03-13 16:08:00 -07003668 // Handle legacy string here for single-user mode
3669 final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003670 if (enabledStr != null) {
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003671 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003672 packageSetting.setEnabled(Integer.parseInt(enabledStr), 0 /* userId */, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003673 } catch (NumberFormatException e) {
3674 if (enabledStr.equalsIgnoreCase("true")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003675 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_ENABLED, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003676 } else if (enabledStr.equalsIgnoreCase("false")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003677 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DISABLED, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003678 } else if (enabledStr.equalsIgnoreCase("default")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003679 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003680 } else {
3681 PackageManagerService.reportSettingsProblem(Log.WARN,
3682 "Error in package manager settings: package " + name
3683 + " has bad enabled value: " + idStr + " at "
3684 + parser.getPositionDescription());
3685 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003686 }
3687 } else {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003688 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003689 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003690
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003691 final String installStatusStr = parser.getAttributeValue(null, "installStatus");
3692 if (installStatusStr != null) {
3693 if (installStatusStr.equalsIgnoreCase("false")) {
3694 packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_INCOMPLETE;
3695 } else {
3696 packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_COMPLETE;
3697 }
3698 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08003699
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003700 int outerDepth = parser.getDepth();
3701 int type;
3702 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3703 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3704 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3705 continue;
3706 }
3707
3708 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003709 // Legacy
3710 if (tagName.equals(TAG_DISABLED_COMPONENTS)) {
3711 readDisabledComponentsLPw(packageSetting, parser, 0);
3712 } else if (tagName.equals(TAG_ENABLED_COMPONENTS)) {
3713 readEnabledComponentsLPw(packageSetting, parser, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003714 } else if (tagName.equals("sigs")) {
3715 packageSetting.signatures.readXml(parser, mPastSignatures);
Svetoslavc6d1c342015-02-26 14:44:43 -08003716 } else if (tagName.equals(TAG_PERMISSIONS)) {
3717 readInstallPermissionsLPr(parser,
3718 packageSetting.getPermissionsState());
Svetoslavcf959f62015-03-26 20:53:34 -07003719 packageSetting.installPermissionsFixed = true;
dcashman3a0dbfb2014-07-07 13:53:36 -07003720 } else if (tagName.equals("proper-signing-keyset")) {
3721 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
dcashman8c04fac2015-03-23 11:39:42 -07003722 Integer refCt = mKeySetRefs.get(id);
3723 if (refCt != null) {
3724 mKeySetRefs.put(id, refCt + 1);
3725 } else {
3726 mKeySetRefs.put(id, 1);
3727 }
dcashman3a0dbfb2014-07-07 13:53:36 -07003728 packageSetting.keySetData.setProperSigningKeySet(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -08003729 } else if (tagName.equals("signing-keyset")) {
dcashman8c04fac2015-03-23 11:39:42 -07003730 // from v1 of keysetmanagerservice - no longer used
dcashman55b10782014-04-09 14:20:38 -07003731 } else if (tagName.equals("upgrade-keyset")) {
3732 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
3733 packageSetting.keySetData.addUpgradeKeySetById(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -08003734 } else if (tagName.equals("defined-keyset")) {
3735 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
3736 String alias = parser.getAttributeValue(null, "alias");
dcashman8c04fac2015-03-23 11:39:42 -07003737 Integer refCt = mKeySetRefs.get(id);
3738 if (refCt != null) {
3739 mKeySetRefs.put(id, refCt + 1);
3740 } else {
3741 mKeySetRefs.put(id, 1);
3742 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08003743 packageSetting.keySetData.addDefinedKeySet(id, alias);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08003744 } else if (tagName.equals(TAG_DOMAIN_VERIFICATION)) {
3745 readDomainVerificationLPw(parser, packageSetting);
Svet Ganov354cd3c2015-12-17 11:35:04 -08003746 } else if (tagName.equals(TAG_CHILD_PACKAGE)) {
3747 String childPackageName = parser.getAttributeValue(null, ATTR_NAME);
3748 if (packageSetting.childPackageNames == null) {
3749 packageSetting.childPackageNames = new ArrayList<>();
3750 }
3751 packageSetting.childPackageNames.add(childPackageName);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003752 } else {
3753 PackageManagerService.reportSettingsProblem(Log.WARN,
3754 "Unknown element under <package>: " + parser.getName());
3755 XmlUtils.skipCurrentTag(parser);
3756 }
3757 }
3758 } else {
3759 XmlUtils.skipCurrentTag(parser);
3760 }
3761 }
3762
Amith Yamasani483f3b02012-03-13 16:08:00 -07003763 private void readDisabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser,
3764 int userId) throws IOException, XmlPullParserException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003765 int outerDepth = parser.getDepth();
3766 int type;
3767 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3768 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3769 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3770 continue;
3771 }
3772
3773 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003774 if (tagName.equals(TAG_ITEM)) {
3775 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003776 if (name != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003777 packageSetting.addDisabledComponent(name.intern(), userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003778 } else {
3779 PackageManagerService.reportSettingsProblem(Log.WARN,
3780 "Error in package manager settings: <disabled-components> has"
3781 + " no name at " + parser.getPositionDescription());
3782 }
3783 } else {
3784 PackageManagerService.reportSettingsProblem(Log.WARN,
3785 "Unknown element under <disabled-components>: " + parser.getName());
3786 }
3787 XmlUtils.skipCurrentTag(parser);
3788 }
3789 }
3790
Amith Yamasani483f3b02012-03-13 16:08:00 -07003791 private void readEnabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser,
3792 int userId) throws IOException, XmlPullParserException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003793 int outerDepth = parser.getDepth();
3794 int type;
3795 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3796 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3797 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3798 continue;
3799 }
3800
3801 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003802 if (tagName.equals(TAG_ITEM)) {
3803 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003804 if (name != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003805 packageSetting.addEnabledComponent(name.intern(), userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003806 } else {
3807 PackageManagerService.reportSettingsProblem(Log.WARN,
3808 "Error in package manager settings: <enabled-components> has"
3809 + " no name at " + parser.getPositionDescription());
3810 }
3811 } else {
3812 PackageManagerService.reportSettingsProblem(Log.WARN,
3813 "Unknown element under <enabled-components>: " + parser.getName());
3814 }
3815 XmlUtils.skipCurrentTag(parser);
3816 }
3817 }
3818
Amith Yamasani483f3b02012-03-13 16:08:00 -07003819 private void readSharedUserLPw(XmlPullParser parser) throws XmlPullParserException,IOException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003820 String name = null;
3821 String idStr = null;
3822 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003823 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003824 SharedUserSetting su = null;
3825 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003826 name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003827 idStr = parser.getAttributeValue(null, "userId");
3828 int userId = idStr != null ? Integer.parseInt(idStr) : 0;
3829 if ("true".equals(parser.getAttributeValue(null, "system"))) {
3830 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
3831 }
3832 if (name == null) {
3833 PackageManagerService.reportSettingsProblem(Log.WARN,
3834 "Error in package manager settings: <shared-user> has no name at "
3835 + parser.getPositionDescription());
3836 } else if (userId == 0) {
3837 PackageManagerService.reportSettingsProblem(Log.WARN,
3838 "Error in package manager settings: shared-user " + name
3839 + " has bad userId " + idStr + " at "
3840 + parser.getPositionDescription());
3841 } else {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003842 if ((su = addSharedUserLPw(name.intern(), userId, pkgFlags, pkgPrivateFlags))
3843 == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003844 PackageManagerService
3845 .reportSettingsProblem(Log.ERROR, "Occurred while parsing settings at "
3846 + parser.getPositionDescription());
3847 }
3848 }
3849 } catch (NumberFormatException e) {
3850 PackageManagerService.reportSettingsProblem(Log.WARN,
3851 "Error in package manager settings: package " + name + " has bad userId "
3852 + idStr + " at " + parser.getPositionDescription());
3853 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003854
3855 if (su != null) {
3856 int outerDepth = parser.getDepth();
3857 int type;
3858 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3859 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3860 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3861 continue;
3862 }
3863
3864 String tagName = parser.getName();
3865 if (tagName.equals("sigs")) {
3866 su.signatures.readXml(parser, mPastSignatures);
3867 } else if (tagName.equals("perms")) {
Svetoslavc6d1c342015-02-26 14:44:43 -08003868 readInstallPermissionsLPr(parser, su.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003869 } else {
3870 PackageManagerService.reportSettingsProblem(Log.WARN,
3871 "Unknown element under <shared-user>: " + parser.getName());
3872 XmlUtils.skipCurrentTag(parser);
3873 }
3874 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003875 } else {
3876 XmlUtils.skipCurrentTag(parser);
3877 }
3878 }
3879
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003880 void createNewUserLI(@NonNull PackageManagerService service, @NonNull Installer installer,
3881 int userHandle) {
3882 String[] volumeUuids;
3883 String[] names;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003884 int[] appIds;
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003885 String[] seinfos;
Janis Danisevskisf3409742016-01-12 14:46:33 +00003886 int[] targetSdkVersions;
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003887 int packagesCount;
3888 synchronized (mPackages) {
3889 Collection<PackageSetting> packages = mPackages.values();
3890 packagesCount = packages.size();
3891 volumeUuids = new String[packagesCount];
3892 names = new String[packagesCount];
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003893 appIds = new int[packagesCount];
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003894 seinfos = new String[packagesCount];
Janis Danisevskisf3409742016-01-12 14:46:33 +00003895 targetSdkVersions = new int[packagesCount];
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003896 Iterator<PackageSetting> packagesIterator = packages.iterator();
3897 for (int i = 0; i < packagesCount; i++) {
3898 PackageSetting ps = packagesIterator.next();
3899 if (ps.pkg == null || ps.pkg.applicationInfo == null) {
3900 continue;
3901 }
3902 // Only system apps are initially installed.
3903 ps.setInstalled(ps.isSystem(), userHandle);
3904 // Need to create a data directory for all apps under this user. Accumulate all
3905 // required args and call the installer after mPackages lock has been released
3906 volumeUuids[i] = ps.volumeUuid;
3907 names[i] = ps.name;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003908 appIds[i] = ps.appId;
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003909 seinfos[i] = ps.pkg.applicationInfo.seinfo;
Janis Danisevskisf3409742016-01-12 14:46:33 +00003910 targetSdkVersions[i] = ps.pkg.applicationInfo.targetSdkVersion;
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003911 }
3912 }
3913 for (int i = 0; i < packagesCount; i++) {
3914 if (names[i] == null) {
Amith Yamasani41cd5772014-07-10 15:26:00 -07003915 continue;
3916 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003917 // TODO: triage flags!
Jeff Sharkey47f71082016-02-01 17:03:54 -07003918 final int flags = StorageManager.FLAG_STORAGE_CE | StorageManager.FLAG_STORAGE_DE;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003919 try {
3920 installer.createAppData(volumeUuids[i], names[i], userHandle, flags, appIds[i],
Janis Danisevskisf3409742016-01-12 14:46:33 +00003921 seinfos[i], targetSdkVersions[i]);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -07003922 } catch (InstallerException e) {
3923 Slog.w(TAG, "Failed to prepare app data", e);
3924 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003925 }
Fyodor Kupolovd8327bd2015-11-30 17:42:43 -08003926 synchronized (mPackages) {
3927 applyDefaultPreferredAppsLPw(service, userHandle);
3928 writePackageRestrictionsLPr(userHandle);
3929 writePackageListLPr(userHandle);
3930 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003931 }
3932
Alexandra Gherghina539a7ef2014-07-07 12:27:54 +01003933 void removeUserLPw(int userId) {
Dianne Hackborn63092712012-10-07 14:45:35 -07003934 Set<Entry<String, PackageSetting>> entries = mPackages.entrySet();
3935 for (Entry<String, PackageSetting> entry : entries) {
3936 entry.getValue().removeUser(userId);
3937 }
3938 mPreferredActivities.remove(userId);
Amith Yamasani13593602012-03-22 16:16:17 -07003939 File file = getUserPackagesStateFile(userId);
3940 file.delete();
3941 file = getUserPackagesStateBackupFile(userId);
3942 file.delete();
Nicolas Prevote7024042014-07-08 15:47:17 +01003943 removeCrossProfileIntentFiltersLPw(userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08003944
3945 mRuntimePermissionsPersistence.onUserRemoved(userId);
Jeff Sharkey91edde22015-05-20 12:04:42 -07003946
3947 writePackageListLPr();
Nicolas Prevota0f48852014-05-27 11:21:11 +01003948 }
3949
Nicolas Prevote7024042014-07-08 15:47:17 +01003950 void removeCrossProfileIntentFiltersLPw(int userId) {
3951 synchronized (mCrossProfileIntentResolvers) {
3952 // userId is the source user
3953 if (mCrossProfileIntentResolvers.get(userId) != null) {
3954 mCrossProfileIntentResolvers.remove(userId);
3955 writePackageRestrictionsLPr(userId);
Nicolas Prevota0f48852014-05-27 11:21:11 +01003956 }
Nicolas Prevote7024042014-07-08 15:47:17 +01003957 // userId is the target user
3958 int count = mCrossProfileIntentResolvers.size();
3959 for (int i = 0; i < count; i++) {
3960 int sourceUserId = mCrossProfileIntentResolvers.keyAt(i);
3961 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(sourceUserId);
3962 boolean needsWriting = false;
Jeff Sharkey9f837a92014-10-24 12:07:24 -07003963 ArraySet<CrossProfileIntentFilter> cpifs =
3964 new ArraySet<CrossProfileIntentFilter>(cpir.filterSet());
Nicolas Prevote7024042014-07-08 15:47:17 +01003965 for (CrossProfileIntentFilter cpif : cpifs) {
3966 if (cpif.getTargetUserId() == userId) {
3967 needsWriting = true;
3968 cpir.removeFilter(cpif);
3969 }
3970 }
3971 if (needsWriting) {
3972 writePackageRestrictionsLPr(sourceUserId);
3973 }
Nicolas Prevota0f48852014-05-27 11:21:11 +01003974 }
3975 }
Amith Yamasani13593602012-03-22 16:16:17 -07003976 }
3977
Geremy Condra12c18382013-03-06 16:49:06 -08003978 // This should be called (at least) whenever an application is removed
3979 private void setFirstAvailableUid(int uid) {
3980 if (uid > mFirstAvailableUid) {
3981 mFirstAvailableUid = uid;
3982 }
3983 }
3984
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003985 // Returns -1 if we could not find an available UserId to assign
Kenny Root447106f2011-03-23 11:00:15 -07003986 private int newUserIdLPw(Object obj) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003987 // Let's be stupidly inefficient for now...
3988 final int N = mUserIds.size();
Geremy Condra12c18382013-03-06 16:49:06 -08003989 for (int i = mFirstAvailableUid; i < N; i++) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003990 if (mUserIds.get(i) == null) {
3991 mUserIds.set(i, obj);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08003992 return Process.FIRST_APPLICATION_UID + i;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003993 }
3994 }
3995
3996 // None left?
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08003997 if (N > (Process.LAST_APPLICATION_UID-Process.FIRST_APPLICATION_UID)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003998 return -1;
3999 }
4000
4001 mUserIds.add(obj);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08004002 return Process.FIRST_APPLICATION_UID + N;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004003 }
4004
Kenny Root0aaa0d92011-09-12 16:42:55 -07004005 public VerifierDeviceIdentity getVerifierDeviceIdentityLPw() {
4006 if (mVerifierDeviceIdentity == null) {
4007 mVerifierDeviceIdentity = VerifierDeviceIdentity.generate();
4008
4009 writeLPr();
4010 }
4011
4012 return mVerifierDeviceIdentity;
4013 }
4014
Svet Ganov354cd3c2015-12-17 11:35:04 -08004015 public boolean hasOtherDisabledSystemPkgWithChildLPr(String parentPackageName,
4016 String childPackageName) {
4017 final int packageCount = mDisabledSysPackages.size();
4018 for (int i = 0; i < packageCount; i++) {
4019 PackageSetting disabledPs = mDisabledSysPackages.valueAt(i);
4020 if (disabledPs.childPackageNames == null || disabledPs.childPackageNames.isEmpty()) {
4021 continue;
4022 }
4023 if (disabledPs.name.equals(parentPackageName)) {
4024 continue;
4025 }
4026 final int childCount = disabledPs.childPackageNames.size();
4027 for (int j = 0; j < childCount; j++) {
4028 String currChildPackageName = disabledPs.childPackageNames.get(j);
4029 if (currChildPackageName.equals(childPackageName)) {
4030 return true;
4031 }
4032 }
4033 }
4034 return false;
4035 }
4036
Kenny Root447106f2011-03-23 11:00:15 -07004037 public PackageSetting getDisabledSystemPkgLPr(String name) {
4038 PackageSetting ps = mDisabledSysPackages.get(name);
4039 return ps;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07004040 }
4041
Jeff Sharkey9f837a92014-10-24 12:07:24 -07004042 private String compToString(ArraySet<String> cmp) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07004043 return cmp != null ? Arrays.toString(cmp.toArray()) : "[]";
4044 }
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004045
Jeff Sharkey5217cac2015-12-20 15:34:01 -07004046 boolean isEnabledAndMatchLPr(ComponentInfo componentInfo, int flags, int userId) {
Jeff Sharkey2bd31db2016-01-09 16:58:14 -07004047 final PackageSetting ps = mPackages.get(componentInfo.packageName);
4048 if (ps == null) return false;
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004049
Jeff Sharkey2bd31db2016-01-09 16:58:14 -07004050 final PackageUserState userState = ps.readUserState(userId);
4051 return userState.isMatch(componentInfo, flags);
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004052 }
4053
Kenny Root447106f2011-03-23 11:00:15 -07004054 String getInstallerPackageNameLPr(String packageName) {
4055 final PackageSetting pkg = mPackages.get(packageName);
4056 if (pkg == null) {
4057 throw new IllegalArgumentException("Unknown package: " + packageName);
4058 }
4059 return pkg.installerPackageName;
4060 }
4061
Amith Yamasani483f3b02012-03-13 16:08:00 -07004062 int getApplicationEnabledSettingLPr(String packageName, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -07004063 final PackageSetting pkg = mPackages.get(packageName);
4064 if (pkg == null) {
4065 throw new IllegalArgumentException("Unknown package: " + packageName);
4066 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004067 return pkg.getEnabled(userId);
Kenny Root447106f2011-03-23 11:00:15 -07004068 }
4069
Amith Yamasani483f3b02012-03-13 16:08:00 -07004070 int getComponentEnabledSettingLPr(ComponentName componentName, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -07004071 final String packageName = componentName.getPackageName();
4072 final PackageSetting pkg = mPackages.get(packageName);
4073 if (pkg == null) {
4074 throw new IllegalArgumentException("Unknown component: " + componentName);
4075 }
4076 final String classNameStr = componentName.getClassName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07004077 return pkg.getCurrentEnabledStateLPr(classNameStr, userId);
Kenny Root447106f2011-03-23 11:00:15 -07004078 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004079
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -07004080 boolean setPackageStoppedStateLPw(PackageManagerService yucky, String packageName,
4081 boolean stopped, boolean allowedByPermission, int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004082 int appId = UserHandle.getAppId(uid);
Kenny Root447106f2011-03-23 11:00:15 -07004083 final PackageSetting pkgSetting = mPackages.get(packageName);
4084 if (pkgSetting == null) {
4085 throw new IllegalArgumentException("Unknown package: " + packageName);
4086 }
Amith Yamasani13593602012-03-22 16:16:17 -07004087 if (!allowedByPermission && (appId != pkgSetting.appId)) {
Kenny Root447106f2011-03-23 11:00:15 -07004088 throw new SecurityException(
4089 "Permission Denial: attempt to change stopped state from pid="
4090 + Binder.getCallingPid()
Amith Yamasani13593602012-03-22 16:16:17 -07004091 + ", uid=" + uid + ", package uid=" + pkgSetting.appId);
Kenny Root447106f2011-03-23 11:00:15 -07004092 }
4093 if (DEBUG_STOPPED) {
4094 if (stopped) {
4095 RuntimeException e = new RuntimeException("here");
4096 e.fillInStackTrace();
4097 Slog.i(TAG, "Stopping package " + packageName, e);
4098 }
4099 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004100 if (pkgSetting.getStopped(userId) != stopped) {
4101 pkgSetting.setStopped(stopped, userId);
4102 // pkgSetting.pkg.mSetStopped = stopped;
4103 if (pkgSetting.getNotLaunched(userId)) {
Kenny Root447106f2011-03-23 11:00:15 -07004104 if (pkgSetting.installerPackageName != null) {
Jeff Sharkeybd0e9e42015-04-30 16:04:50 -07004105 yucky.sendPackageBroadcast(Intent.ACTION_PACKAGE_FIRST_LAUNCH,
Dianne Hackbornf36003f2015-11-11 13:24:11 -08004106 pkgSetting.name, null, 0,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07004107 pkgSetting.installerPackageName, null, new int[] {userId});
Kenny Root447106f2011-03-23 11:00:15 -07004108 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004109 pkgSetting.setNotLaunched(false, userId);
Kenny Root447106f2011-03-23 11:00:15 -07004110 }
4111 return true;
4112 }
4113 return false;
4114 }
4115
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08004116 List<UserInfo> getAllUsers() {
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07004117 long id = Binder.clearCallingIdentity();
Amith Yamasani483f3b02012-03-13 16:08:00 -07004118 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07004119 return UserManagerService.getInstance().getUsers(false);
Amith Yamasani483f3b02012-03-13 16:08:00 -07004120 } catch (NullPointerException npe) {
4121 // packagemanager not yet initialized
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07004122 } finally {
4123 Binder.restoreCallingIdentity(id);
Amith Yamasani483f3b02012-03-13 16:08:00 -07004124 }
4125 return null;
4126 }
4127
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07004128 /**
4129 * Return all {@link PackageSetting} that are actively installed on the
4130 * given {@link VolumeInfo#fsUuid}.
4131 */
4132 List<PackageSetting> getVolumePackagesLPr(String volumeUuid) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07004133 ArrayList<PackageSetting> res = new ArrayList<>();
4134 for (int i = 0; i < mPackages.size(); i++) {
4135 final PackageSetting setting = mPackages.valueAt(i);
4136 if (Objects.equals(volumeUuid, setting.volumeUuid)) {
4137 res.add(setting);
4138 }
4139 }
4140 return res;
4141 }
4142
Svetoslavc6d1c342015-02-26 14:44:43 -08004143 static void printFlags(PrintWriter pw, int val, Object[] spec) {
Joe Onorato20963df2012-01-04 18:13:24 -08004144 pw.print("[ ");
4145 for (int i=0; i<spec.length; i+=2) {
4146 int mask = (Integer)spec[i];
4147 if ((val & mask) != 0) {
4148 pw.print(spec[i+1]);
4149 pw.print(" ");
4150 }
4151 }
4152 pw.print("]");
4153 }
4154
4155 static final Object[] FLAG_DUMP_SPEC = new Object[] {
4156 ApplicationInfo.FLAG_SYSTEM, "SYSTEM",
4157 ApplicationInfo.FLAG_DEBUGGABLE, "DEBUGGABLE",
4158 ApplicationInfo.FLAG_HAS_CODE, "HAS_CODE",
4159 ApplicationInfo.FLAG_PERSISTENT, "PERSISTENT",
4160 ApplicationInfo.FLAG_FACTORY_TEST, "FACTORY_TEST",
4161 ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING, "ALLOW_TASK_REPARENTING",
4162 ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA, "ALLOW_CLEAR_USER_DATA",
4163 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP, "UPDATED_SYSTEM_APP",
4164 ApplicationInfo.FLAG_TEST_ONLY, "TEST_ONLY",
4165 ApplicationInfo.FLAG_VM_SAFE_MODE, "VM_SAFE_MODE",
4166 ApplicationInfo.FLAG_ALLOW_BACKUP, "ALLOW_BACKUP",
4167 ApplicationInfo.FLAG_KILL_AFTER_RESTORE, "KILL_AFTER_RESTORE",
4168 ApplicationInfo.FLAG_RESTORE_ANY_VERSION, "RESTORE_ANY_VERSION",
4169 ApplicationInfo.FLAG_EXTERNAL_STORAGE, "EXTERNAL_STORAGE",
4170 ApplicationInfo.FLAG_LARGE_HEAP, "LARGE_HEAP",
Alex Klyubinb9f8a522015-02-03 11:12:59 -08004171 };
4172
4173 static final Object[] PRIVATE_FLAG_DUMP_SPEC = new Object[] {
4174 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED, "PRIVILEGED",
4175 ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK, "FORWARD_LOCK",
4176 ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE, "CANT_SAVE_STATE",
Joe Onorato20963df2012-01-04 18:13:24 -08004177 };
4178
Jeff Sharkeyf80b52b2015-07-17 14:56:34 -07004179 void dumpVersionLPr(IndentingPrintWriter pw) {
4180 pw.increaseIndent();
4181 for (int i= 0; i < mVersion.size(); i++) {
4182 final String volumeUuid = mVersion.keyAt(i);
4183 final VersionInfo ver = mVersion.valueAt(i);
4184 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) {
4185 pw.println("Internal:");
4186 } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) {
4187 pw.println("External:");
4188 } else {
4189 pw.println("UUID " + volumeUuid + ":");
4190 }
4191 pw.increaseIndent();
4192 pw.printPair("sdkVersion", ver.sdkVersion);
4193 pw.printPair("databaseVersion", ver.databaseVersion);
4194 pw.println();
4195 pw.printPair("fingerprint", ver.fingerprint);
4196 pw.println();
4197 pw.decreaseIndent();
4198 }
4199 pw.decreaseIndent();
4200 }
4201
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004202 void dumpPackageLPr(PrintWriter pw, String prefix, String checkinTag,
4203 ArraySet<String> permissionNames, PackageSetting ps, SimpleDateFormat sdf,
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004204 Date date, List<UserInfo> users, boolean dumpAll) {
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004205 if (checkinTag != null) {
4206 pw.print(checkinTag);
4207 pw.print(",");
4208 pw.print(ps.realName != null ? ps.realName : ps.name);
4209 pw.print(",");
4210 pw.print(ps.appId);
4211 pw.print(",");
4212 pw.print(ps.versionCode);
4213 pw.print(",");
4214 pw.print(ps.firstInstallTime);
4215 pw.print(",");
4216 pw.print(ps.lastUpdateTime);
4217 pw.print(",");
4218 pw.print(ps.installerPackageName != null ? ps.installerPackageName : "?");
4219 pw.println();
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004220 if (ps.pkg != null) {
4221 pw.print(checkinTag); pw.print("-"); pw.print("splt,");
4222 pw.print("base,");
4223 pw.println(ps.pkg.baseRevisionCode);
4224 if (ps.pkg.splitNames != null) {
4225 for (int i = 0; i < ps.pkg.splitNames.length; i++) {
4226 pw.print(checkinTag); pw.print("-"); pw.print("splt,");
4227 pw.print(ps.pkg.splitNames[i]); pw.print(",");
4228 pw.println(ps.pkg.splitRevisionCodes[i]);
4229 }
4230 }
4231 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004232 for (UserInfo user : users) {
4233 pw.print(checkinTag);
4234 pw.print("-");
4235 pw.print("usr");
4236 pw.print(",");
4237 pw.print(user.id);
4238 pw.print(",");
4239 pw.print(ps.getInstalled(user.id) ? "I" : "i");
Amith Yamasanie5bcff62014-07-19 15:44:09 -07004240 pw.print(ps.getHidden(user.id) ? "B" : "b");
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00004241 pw.print(ps.getSuspended(user.id) ? "SU" : "su");
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004242 pw.print(ps.getStopped(user.id) ? "S" : "s");
4243 pw.print(ps.getNotLaunched(user.id) ? "l" : "L");
4244 pw.print(",");
4245 pw.print(ps.getEnabled(user.id));
4246 String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id);
4247 pw.print(",");
4248 pw.print(lastDisabledAppCaller != null ? lastDisabledAppCaller : "?");
4249 pw.println();
4250 }
4251 return;
4252 }
4253
Dianne Hackbornc895be72013-03-11 17:48:43 -07004254 pw.print(prefix); pw.print("Package [");
4255 pw.print(ps.realName != null ? ps.realName : ps.name);
4256 pw.print("] (");
4257 pw.print(Integer.toHexString(System.identityHashCode(ps)));
4258 pw.println("):");
4259
Jeff Sharkeye31b8202015-04-29 21:44:57 -07004260 if (ps.frozen) {
4261 pw.print(prefix); pw.println(" FROZEN!");
4262 }
4263
Dianne Hackbornc895be72013-03-11 17:48:43 -07004264 if (ps.realName != null) {
4265 pw.print(prefix); pw.print(" compat name=");
4266 pw.println(ps.name);
4267 }
4268
Svetoslavc6d1c342015-02-26 14:44:43 -08004269 pw.print(prefix); pw.print(" userId="); pw.println(ps.appId);
4270
Dianne Hackbornc895be72013-03-11 17:48:43 -07004271 if (ps.sharedUser != null) {
4272 pw.print(prefix); pw.print(" sharedUser="); pw.println(ps.sharedUser);
4273 }
4274 pw.print(prefix); pw.print(" pkg="); pw.println(ps.pkg);
4275 pw.print(prefix); pw.print(" codePath="); pw.println(ps.codePathString);
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004276 if (permissionNames == null) {
4277 pw.print(prefix); pw.print(" resourcePath="); pw.println(ps.resourcePathString);
4278 pw.print(prefix); pw.print(" legacyNativeLibraryDir=");
4279 pw.println(ps.legacyNativeLibraryPathString);
4280 pw.print(prefix); pw.print(" primaryCpuAbi="); pw.println(ps.primaryCpuAbiString);
4281 pw.print(prefix); pw.print(" secondaryCpuAbi="); pw.println(ps.secondaryCpuAbiString);
4282 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004283 pw.print(prefix); pw.print(" versionCode="); pw.print(ps.versionCode);
4284 if (ps.pkg != null) {
4285 pw.print(" targetSdk="); pw.print(ps.pkg.applicationInfo.targetSdkVersion);
4286 }
4287 pw.println();
4288 if (ps.pkg != null) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004289 if (ps.pkg.parentPackage != null) {
4290 PackageParser.Package parentPkg = ps.pkg.parentPackage;
4291 PackageSetting pps = mPackages.get(parentPkg.packageName);
4292 if (pps == null || !pps.codePathString.equals(parentPkg.codePath)) {
4293 pps = mDisabledSysPackages.get(parentPkg.packageName);
4294 }
4295 if (pps != null) {
4296 pw.print(prefix); pw.print(" parentPackage=");
4297 pw.println(pps.realName != null ? pps.realName : pps.name);
4298 }
4299 } else if (ps.pkg.childPackages != null) {
4300 pw.print(prefix); pw.print(" childPackages=[");
4301 final int childCount = ps.pkg.childPackages.size();
4302 for (int i = 0; i < childCount; i++) {
4303 PackageParser.Package childPkg = ps.pkg.childPackages.get(i);
4304 PackageSetting cps = mPackages.get(childPkg.packageName);
4305 if (cps == null || !cps.codePathString.equals(childPkg.codePath)) {
4306 cps = mDisabledSysPackages.get(childPkg.packageName);
4307 }
4308 if (cps != null) {
4309 if (i > 0) {
4310 pw.print(", ");
4311 }
4312 pw.print(cps.realName != null ? cps.realName : cps.name);
4313 }
4314 }
4315 pw.println("]");
4316 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004317 pw.print(prefix); pw.print(" versionName="); pw.println(ps.pkg.mVersionName);
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004318 pw.print(prefix); pw.print(" splits="); dumpSplitNames(pw, ps.pkg); pw.println();
Dianne Hackbornc895be72013-03-11 17:48:43 -07004319 pw.print(prefix); pw.print(" applicationInfo=");
4320 pw.println(ps.pkg.applicationInfo.toString());
4321 pw.print(prefix); pw.print(" flags="); printFlags(pw, ps.pkg.applicationInfo.flags,
4322 FLAG_DUMP_SPEC); pw.println();
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004323 if (ps.pkg.applicationInfo.privateFlags != 0) {
4324 pw.print(prefix); pw.print(" privateFlags="); printFlags(pw,
4325 ps.pkg.applicationInfo.privateFlags, PRIVATE_FLAG_DUMP_SPEC); pw.println();
4326 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004327 pw.print(prefix); pw.print(" dataDir="); pw.println(ps.pkg.applicationInfo.dataDir);
Dianne Hackbornc895be72013-03-11 17:48:43 -07004328 pw.print(prefix); pw.print(" supportsScreens=[");
4329 boolean first = true;
4330 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS) != 0) {
4331 if (!first)
4332 pw.print(", ");
4333 first = false;
4334 pw.print("small");
4335 }
4336 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS) != 0) {
4337 if (!first)
4338 pw.print(", ");
4339 first = false;
4340 pw.print("medium");
4341 }
4342 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0) {
4343 if (!first)
4344 pw.print(", ");
4345 first = false;
4346 pw.print("large");
4347 }
4348 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS) != 0) {
4349 if (!first)
4350 pw.print(", ");
4351 first = false;
4352 pw.print("xlarge");
4353 }
4354 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS) != 0) {
4355 if (!first)
4356 pw.print(", ");
4357 first = false;
4358 pw.print("resizeable");
4359 }
4360 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES) != 0) {
4361 if (!first)
4362 pw.print(", ");
4363 first = false;
4364 pw.print("anyDensity");
4365 }
4366 pw.println("]");
4367 if (ps.pkg.libraryNames != null && ps.pkg.libraryNames.size() > 0) {
4368 pw.print(prefix); pw.println(" libraries:");
4369 for (int i=0; i<ps.pkg.libraryNames.size(); i++) {
4370 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.libraryNames.get(i));
4371 }
4372 }
4373 if (ps.pkg.usesLibraries != null && ps.pkg.usesLibraries.size() > 0) {
4374 pw.print(prefix); pw.println(" usesLibraries:");
4375 for (int i=0; i<ps.pkg.usesLibraries.size(); i++) {
4376 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.usesLibraries.get(i));
4377 }
4378 }
4379 if (ps.pkg.usesOptionalLibraries != null
4380 && ps.pkg.usesOptionalLibraries.size() > 0) {
4381 pw.print(prefix); pw.println(" usesOptionalLibraries:");
4382 for (int i=0; i<ps.pkg.usesOptionalLibraries.size(); i++) {
4383 pw.print(prefix); pw.print(" ");
4384 pw.println(ps.pkg.usesOptionalLibraries.get(i));
4385 }
4386 }
4387 if (ps.pkg.usesLibraryFiles != null
4388 && ps.pkg.usesLibraryFiles.length > 0) {
4389 pw.print(prefix); pw.println(" usesLibraryFiles:");
4390 for (int i=0; i<ps.pkg.usesLibraryFiles.length; i++) {
4391 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.usesLibraryFiles[i]);
4392 }
4393 }
4394 }
4395 pw.print(prefix); pw.print(" timeStamp=");
4396 date.setTime(ps.timeStamp);
4397 pw.println(sdf.format(date));
4398 pw.print(prefix); pw.print(" firstInstallTime=");
4399 date.setTime(ps.firstInstallTime);
4400 pw.println(sdf.format(date));
4401 pw.print(prefix); pw.print(" lastUpdateTime=");
4402 date.setTime(ps.lastUpdateTime);
4403 pw.println(sdf.format(date));
4404 if (ps.installerPackageName != null) {
4405 pw.print(prefix); pw.print(" installerPackageName=");
4406 pw.println(ps.installerPackageName);
4407 }
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07004408 if (ps.volumeUuid != null) {
4409 pw.print(prefix); pw.print(" volumeUuid=");
4410 pw.println(ps.volumeUuid);
4411 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004412 pw.print(prefix); pw.print(" signatures="); pw.println(ps.signatures);
Svetoslavcf959f62015-03-26 20:53:34 -07004413 pw.print(prefix); pw.print(" installPermissionsFixed=");
4414 pw.print(ps.installPermissionsFixed);
Dianne Hackbornc895be72013-03-11 17:48:43 -07004415 pw.print(" installStatus="); pw.println(ps.installStatus);
4416 pw.print(prefix); pw.print(" pkgFlags="); printFlags(pw, ps.pkgFlags, FLAG_DUMP_SPEC);
4417 pw.println();
Svetoslavc6d1c342015-02-26 14:44:43 -08004418
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004419 if (ps.pkg != null && ps.pkg.permissions != null && ps.pkg.permissions.size() > 0) {
4420 final ArrayList<PackageParser.Permission> perms = ps.pkg.permissions;
4421 pw.print(prefix); pw.println(" declared permissions:");
4422 for (int i=0; i<perms.size(); i++) {
4423 PackageParser.Permission perm = perms.get(i);
4424 if (permissionNames != null
4425 && !permissionNames.contains(perm.info.name)) {
4426 continue;
4427 }
4428 pw.print(prefix); pw.print(" "); pw.print(perm.info.name);
4429 pw.print(": prot=");
4430 pw.print(PermissionInfo.protectionToString(perm.info.protectionLevel));
4431 if ((perm.info.flags&PermissionInfo.FLAG_COSTS_MONEY) != 0) {
4432 pw.print(", COSTS_MONEY");
4433 }
Svet Ganov2a1376d2016-02-22 17:20:35 -08004434 if ((perm.info.flags&PermissionInfo.FLAG_REMOVED) != 0) {
Svet Ganov52153f42015-08-11 08:59:12 -07004435 pw.print(", HIDDEN");
Svet Ganov3e0be742015-08-07 23:06:00 -07004436 }
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004437 if ((perm.info.flags&PermissionInfo.FLAG_INSTALLED) != 0) {
4438 pw.print(", INSTALLED");
4439 }
4440 pw.println();
4441 }
4442 }
4443
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004444 if ((permissionNames != null || dumpAll) && ps.pkg.requestedPermissions != null
4445 && ps.pkg.requestedPermissions.size() > 0) {
4446 final ArrayList<String> perms = ps.pkg.requestedPermissions;
4447 pw.print(prefix); pw.println(" requested permissions:");
4448 for (int i=0; i<perms.size(); i++) {
4449 String perm = perms.get(i);
4450 if (permissionNames != null
4451 && !permissionNames.contains(perm)) {
4452 continue;
4453 }
4454 pw.print(prefix); pw.print(" "); pw.println(perm);
4455 }
4456 }
4457
4458 if (ps.sharedUser == null || permissionNames != null || dumpAll) {
Svetoslavc6d1c342015-02-26 14:44:43 -08004459 PermissionsState permissionsState = ps.getPermissionsState();
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004460 dumpInstallPermissionsLPr(pw, prefix + " ", permissionNames, permissionsState);
Svetoslavc6d1c342015-02-26 14:44:43 -08004461 }
4462
Dianne Hackbornc895be72013-03-11 17:48:43 -07004463 for (UserInfo user : users) {
4464 pw.print(prefix); pw.print(" User "); pw.print(user.id); pw.print(": ");
4465 pw.print(" installed=");
4466 pw.print(ps.getInstalled(user.id));
Amith Yamasanie5bcff62014-07-19 15:44:09 -07004467 pw.print(" hidden=");
4468 pw.print(ps.getHidden(user.id));
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00004469 pw.print(" suspended=");
4470 pw.print(ps.getSuspended(user.id));
Dianne Hackbornc895be72013-03-11 17:48:43 -07004471 pw.print(" stopped=");
4472 pw.print(ps.getStopped(user.id));
4473 pw.print(" notLaunched=");
4474 pw.print(ps.getNotLaunched(user.id));
4475 pw.print(" enabled=");
4476 pw.println(ps.getEnabled(user.id));
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07004477 String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id);
4478 if (lastDisabledAppCaller != null) {
4479 pw.print(prefix); pw.print(" lastDisabledCaller: ");
4480 pw.println(lastDisabledAppCaller);
4481 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004482
4483 if (ps.sharedUser == null) {
4484 PermissionsState permissionsState = ps.getPermissionsState();
4485 dumpGidsLPr(pw, prefix + " ", permissionsState.computeGids(user.id));
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004486 dumpRuntimePermissionsLPr(pw, prefix + " ", permissionNames, permissionsState
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004487 .getRuntimePermissionStates(user.id), dumpAll);
Svetoslavc6d1c342015-02-26 14:44:43 -08004488 }
4489
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004490 if (permissionNames == null) {
4491 ArraySet<String> cmp = ps.getDisabledComponents(user.id);
4492 if (cmp != null && cmp.size() > 0) {
4493 pw.print(prefix); pw.println(" disabledComponents:");
4494 for (String s : cmp) {
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004495 pw.print(prefix); pw.print(" "); pw.println(s);
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004496 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004497 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004498 cmp = ps.getEnabledComponents(user.id);
4499 if (cmp != null && cmp.size() > 0) {
4500 pw.print(prefix); pw.println(" enabledComponents:");
4501 for (String s : cmp) {
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004502 pw.print(prefix); pw.print(" "); pw.println(s);
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004503 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004504 }
4505 }
4506 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07004507 }
4508
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004509 void dumpPackagesLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames,
4510 DumpState dumpState, boolean checkin) {
Kenny Root447106f2011-03-23 11:00:15 -07004511 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
4512 final Date date = new Date();
4513 boolean printedSomething = false;
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07004514 List<UserInfo> users = getAllUsers();
Kenny Root447106f2011-03-23 11:00:15 -07004515 for (final PackageSetting ps : mPackages.values()) {
4516 if (packageName != null && !packageName.equals(ps.realName)
4517 && !packageName.equals(ps.name)) {
4518 continue;
4519 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004520 if (permissionNames != null
4521 && !ps.getPermissionsState().hasRequestedPermission(permissionNames)) {
4522 continue;
4523 }
Kenny Root447106f2011-03-23 11:00:15 -07004524
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004525 if (!checkin && packageName != null) {
Kenny Root447106f2011-03-23 11:00:15 -07004526 dumpState.setSharedUser(ps.sharedUser);
4527 }
4528
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004529 if (!checkin && !printedSomething) {
Kenny Root447106f2011-03-23 11:00:15 -07004530 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004531 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07004532 pw.println("Packages:");
4533 printedSomething = true;
4534 }
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004535 dumpPackageLPr(pw, " ", checkin ? "pkg" : null, permissionNames, ps, sdf, date, users,
4536 packageName != null);
Kenny Root447106f2011-03-23 11:00:15 -07004537 }
4538
4539 printedSomething = false;
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004540 if (mRenamedPackages.size() > 0 && permissionNames == null) {
Andy McFadden2f362292012-01-20 14:43:38 -08004541 for (final Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
Kenny Root447106f2011-03-23 11:00:15 -07004542 if (packageName != null && !packageName.equals(e.getKey())
4543 && !packageName.equals(e.getValue())) {
4544 continue;
4545 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004546 if (!checkin) {
4547 if (!printedSomething) {
4548 if (dumpState.onTitlePrinted())
4549 pw.println();
4550 pw.println("Renamed packages:");
4551 printedSomething = true;
4552 }
4553 pw.print(" ");
4554 } else {
4555 pw.print("ren,");
Kenny Root447106f2011-03-23 11:00:15 -07004556 }
Kenny Root447106f2011-03-23 11:00:15 -07004557 pw.print(e.getKey());
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004558 pw.print(checkin ? " -> " : ",");
Kenny Root447106f2011-03-23 11:00:15 -07004559 pw.println(e.getValue());
4560 }
4561 }
4562
4563 printedSomething = false;
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004564 if (mDisabledSysPackages.size() > 0 && permissionNames == null) {
Kenny Root447106f2011-03-23 11:00:15 -07004565 for (final PackageSetting ps : mDisabledSysPackages.values()) {
4566 if (packageName != null && !packageName.equals(ps.realName)
4567 && !packageName.equals(ps.name)) {
4568 continue;
4569 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08004570 if (!checkin && !printedSomething) {
Kenny Root447106f2011-03-23 11:00:15 -07004571 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004572 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07004573 pw.println("Hidden system packages:");
4574 printedSomething = true;
4575 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004576 dumpPackageLPr(pw, " ", checkin ? "dis" : null, permissionNames, ps, sdf, date,
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004577 users, packageName != null);
Kenny Root447106f2011-03-23 11:00:15 -07004578 }
4579 }
4580 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07004581
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004582 void dumpPermissionsLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames,
4583 DumpState dumpState) {
Kenny Root447106f2011-03-23 11:00:15 -07004584 boolean printedSomething = false;
4585 for (BasePermission p : mPermissions.values()) {
4586 if (packageName != null && !packageName.equals(p.sourcePackage)) {
4587 continue;
4588 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004589 if (permissionNames != null && !permissionNames.contains(p.name)) {
4590 continue;
4591 }
Kenny Root447106f2011-03-23 11:00:15 -07004592 if (!printedSomething) {
4593 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004594 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07004595 pw.println("Permissions:");
4596 printedSomething = true;
4597 }
4598 pw.print(" Permission ["); pw.print(p.name); pw.print("] (");
4599 pw.print(Integer.toHexString(System.identityHashCode(p)));
4600 pw.println("):");
4601 pw.print(" sourcePackage="); pw.println(p.sourcePackage);
4602 pw.print(" uid="); pw.print(p.uid);
Jeff Sharkey00f39042015-03-23 16:51:22 -07004603 pw.print(" gids="); pw.print(Arrays.toString(
Xiaohui Chen594f2082015-08-18 11:04:20 -07004604 p.computeGids(UserHandle.USER_SYSTEM)));
Kenny Root447106f2011-03-23 11:00:15 -07004605 pw.print(" type="); pw.print(p.type);
Dianne Hackborne639da72012-02-21 15:11:13 -08004606 pw.print(" prot=");
4607 pw.println(PermissionInfo.protectionToString(p.protectionLevel));
Kenny Root447106f2011-03-23 11:00:15 -07004608 if (p.perm != null) {
4609 pw.print(" perm="); pw.println(p.perm);
Svet Ganov3e0be742015-08-07 23:06:00 -07004610 if ((p.perm.info.flags & PermissionInfo.FLAG_INSTALLED) == 0
Svet Ganov2a1376d2016-02-22 17:20:35 -08004611 || (p.perm.info.flags & PermissionInfo.FLAG_REMOVED) != 0) {
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004612 pw.print(" flags=0x"); pw.println(Integer.toHexString(p.perm.info.flags));
4613 }
4614 }
4615 if (p.packageSetting != null) {
4616 pw.print(" packageSetting="); pw.println(p.packageSetting);
Kenny Root447106f2011-03-23 11:00:15 -07004617 }
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004618 if (READ_EXTERNAL_STORAGE.equals(p.name)) {
Jeff Sharkey5d32e772012-04-12 15:59:23 -07004619 pw.print(" enforced=");
4620 pw.println(mReadExternalStorageEnforced);
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004621 }
Kenny Root447106f2011-03-23 11:00:15 -07004622 }
4623 }
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004624
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004625 void dumpSharedUsersLPr(PrintWriter pw, String packageName, ArraySet<String> permissionNames,
4626 DumpState dumpState, boolean checkin) {
Kenny Root447106f2011-03-23 11:00:15 -07004627 boolean printedSomething = false;
4628 for (SharedUserSetting su : mSharedUsers.values()) {
4629 if (packageName != null && su != dumpState.getSharedUser()) {
4630 continue;
4631 }
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004632 if (permissionNames != null
4633 && !su.getPermissionsState().hasRequestedPermission(permissionNames)) {
4634 continue;
4635 }
Dianne Hackbornd052a942014-11-21 15:23:13 -08004636 if (!checkin) {
4637 if (!printedSomething) {
4638 if (dumpState.onTitlePrinted())
4639 pw.println();
4640 pw.println("Shared users:");
4641 printedSomething = true;
4642 }
4643 pw.print(" SharedUser [");
4644 pw.print(su.name);
4645 pw.print("] (");
4646 pw.print(Integer.toHexString(System.identityHashCode(su)));
4647 pw.println("):");
Svetoslavc6d1c342015-02-26 14:44:43 -08004648
4649 String prefix = " ";
4650 pw.print(prefix); pw.print("userId="); pw.println(su.userId);
4651
4652 PermissionsState permissionsState = su.getPermissionsState();
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004653 dumpInstallPermissionsLPr(pw, prefix, permissionNames, permissionsState);
Svetoslavc6d1c342015-02-26 14:44:43 -08004654
4655 for (int userId : UserManagerService.getInstance().getUserIds()) {
4656 final int[] gids = permissionsState.computeGids(userId);
Svet Ganov8c7f7002015-05-07 10:48:44 -07004657 List<PermissionState> permissions = permissionsState
4658 .getRuntimePermissionStates(userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08004659 if (!ArrayUtils.isEmpty(gids) || !permissions.isEmpty()) {
4660 pw.print(prefix); pw.print("User "); pw.print(userId); pw.println(": ");
4661 dumpGidsLPr(pw, prefix + " ", gids);
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004662 dumpRuntimePermissionsLPr(pw, prefix + " ", permissionNames, permissions,
4663 packageName != null);
Svetoslavc6d1c342015-02-26 14:44:43 -08004664 }
Dianne Hackbornd052a942014-11-21 15:23:13 -08004665 }
4666 } else {
4667 pw.print("suid,"); pw.print(su.userId); pw.print(","); pw.println(su.name);
Kenny Root447106f2011-03-23 11:00:15 -07004668 }
4669 }
4670 }
4671
4672 void dumpReadMessagesLPr(PrintWriter pw, DumpState dumpState) {
4673 pw.println("Settings parse messages:");
4674 pw.print(mReadMessages.toString());
4675 }
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004676
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07004677 void dumpRestoredPermissionGrantsLPr(PrintWriter pw, DumpState dumpState) {
4678 if (mRestoredUserGrants.size() > 0) {
4679 pw.println();
4680 pw.println("Restored (pending) permission grants:");
4681 for (int userIndex = 0; userIndex < mRestoredUserGrants.size(); userIndex++) {
4682 ArrayMap<String, ArraySet<RestoredPermissionGrant>> grantsByPackage =
4683 mRestoredUserGrants.valueAt(userIndex);
4684 if (grantsByPackage != null && grantsByPackage.size() > 0) {
4685 final int userId = mRestoredUserGrants.keyAt(userIndex);
4686 pw.print(" User "); pw.println(userId);
4687
4688 for (int pkgIndex = 0; pkgIndex < grantsByPackage.size(); pkgIndex++) {
4689 ArraySet<RestoredPermissionGrant> grants = grantsByPackage.valueAt(pkgIndex);
4690 if (grants != null && grants.size() > 0) {
4691 final String pkgName = grantsByPackage.keyAt(pkgIndex);
4692 pw.print(" "); pw.print(pkgName); pw.println(" :");
4693
4694 for (RestoredPermissionGrant g : grants) {
4695 pw.print(" ");
4696 pw.print(g.permissionName);
4697 if (g.granted) {
4698 pw.print(" GRANTED");
4699 }
4700 if ((g.grantBits&FLAG_PERMISSION_USER_SET) != 0) {
4701 pw.print(" user_set");
4702 }
4703 if ((g.grantBits&FLAG_PERMISSION_USER_FIXED) != 0) {
4704 pw.print(" user_fixed");
4705 }
4706 if ((g.grantBits&FLAG_PERMISSION_REVOKE_ON_UPGRADE) != 0) {
4707 pw.print(" revoke_on_upgrade");
4708 }
4709 pw.println();
4710 }
4711 }
4712 }
4713 }
4714 }
4715 pw.println();
4716 }
4717 }
4718
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004719 private static void dumpSplitNames(PrintWriter pw, PackageParser.Package pkg) {
4720 if (pkg == null) {
4721 pw.print("unknown");
4722 } else {
4723 // [base:10, config.mdpi, config.xhdpi:12]
4724 pw.print("[");
4725 pw.print("base");
4726 if (pkg.baseRevisionCode != 0) {
4727 pw.print(":"); pw.print(pkg.baseRevisionCode);
4728 }
4729 if (pkg.splitNames != null) {
4730 for (int i = 0; i < pkg.splitNames.length; i++) {
4731 pw.print(", ");
4732 pw.print(pkg.splitNames[i]);
4733 if (pkg.splitRevisionCodes[i] != 0) {
4734 pw.print(":"); pw.print(pkg.splitRevisionCodes[i]);
4735 }
4736 }
4737 }
4738 pw.print("]");
4739 }
4740 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004741
4742 void dumpGidsLPr(PrintWriter pw, String prefix, int[] gids) {
4743 if (!ArrayUtils.isEmpty(gids)) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07004744 pw.print(prefix);
4745 pw.print("gids="); pw.println(
Svetoslavc6d1c342015-02-26 14:44:43 -08004746 PackageManagerService.arrayToString(gids));
4747 }
4748 }
4749
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004750 void dumpRuntimePermissionsLPr(PrintWriter pw, String prefix, ArraySet<String> permissionNames,
Dianne Hackborn9f5b0a22015-08-13 18:25:20 -07004751 List<PermissionState> permissionStates, boolean dumpAll) {
4752 if (!permissionStates.isEmpty() || dumpAll) {
Svetoslavc6d1c342015-02-26 14:44:43 -08004753 pw.print(prefix); pw.println("runtime permissions:");
Svet Ganov8c7f7002015-05-07 10:48:44 -07004754 for (PermissionState permissionState : permissionStates) {
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004755 if (permissionNames != null
4756 && !permissionNames.contains(permissionState.getName())) {
4757 continue;
4758 }
Svet Ganov8c7f7002015-05-07 10:48:44 -07004759 pw.print(prefix); pw.print(" "); pw.print(permissionState.getName());
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004760 pw.print(": granted="); pw.print(permissionState.isGranted());
4761 pw.println(permissionFlagsToString(", flags=",
4762 permissionState.getFlags()));
Svetoslavc6d1c342015-02-26 14:44:43 -08004763 }
4764 }
4765 }
4766
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004767 private static String permissionFlagsToString(String prefix, int flags) {
4768 StringBuilder flagsString = null;
Svet Ganov77ab6a82015-07-03 12:03:02 -07004769 while (flags != 0) {
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004770 if (flagsString == null) {
4771 flagsString = new StringBuilder();
4772 flagsString.append(prefix);
4773 flagsString.append("[ ");
4774 }
Svet Ganov77ab6a82015-07-03 12:03:02 -07004775 final int flag = 1 << Integer.numberOfTrailingZeros(flags);
4776 flags &= ~flag;
4777 flagsString.append(PackageManager.permissionFlagToString(flag));
4778 flagsString.append(' ');
4779 }
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004780 if (flagsString != null) {
4781 flagsString.append(']');
4782 return flagsString.toString();
4783 } else {
4784 return "";
4785 }
Svet Ganov77ab6a82015-07-03 12:03:02 -07004786 }
4787
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004788 void dumpInstallPermissionsLPr(PrintWriter pw, String prefix, ArraySet<String> permissionNames,
Svetoslavc6d1c342015-02-26 14:44:43 -08004789 PermissionsState permissionsState) {
Svet Ganov8c7f7002015-05-07 10:48:44 -07004790 List<PermissionState> permissionStates = permissionsState.getInstallPermissionStates();
4791 if (!permissionStates.isEmpty()) {
Svetoslavc6d1c342015-02-26 14:44:43 -08004792 pw.print(prefix); pw.println("install permissions:");
Svet Ganov8c7f7002015-05-07 10:48:44 -07004793 for (PermissionState permissionState : permissionStates) {
Dianne Hackbornca8e6da2015-06-24 15:19:17 -07004794 if (permissionNames != null
4795 && !permissionNames.contains(permissionState.getName())) {
4796 continue;
4797 }
Svet Ganov8c7f7002015-05-07 10:48:44 -07004798 pw.print(prefix); pw.print(" "); pw.print(permissionState.getName());
Dianne Hackborncfbfafe2015-07-21 16:57:51 -07004799 pw.print(": granted="); pw.print(permissionState.isGranted());
4800 pw.println(permissionFlagsToString(", flags=",
Svet Ganov8c7f7002015-05-07 10:48:44 -07004801 permissionState.getFlags()));
Svetoslavc6d1c342015-02-26 14:44:43 -08004802 }
4803 }
4804 }
4805
4806 public void writeRuntimePermissionsForUserLPr(int userId, boolean sync) {
4807 if (sync) {
4808 mRuntimePermissionsPersistence.writePermissionsForUserSyncLPr(userId);
4809 } else {
4810 mRuntimePermissionsPersistence.writePermissionsForUserAsyncLPr(userId);
4811 }
4812 }
4813
4814 private final class RuntimePermissionPersistence {
4815 private static final long WRITE_PERMISSIONS_DELAY_MILLIS = 200;
Svetoslavc6d1c342015-02-26 14:44:43 -08004816 private static final long MAX_WRITE_PERMISSIONS_DELAY_MILLIS = 2000;
4817
4818 private final Handler mHandler = new MyHandler();
4819
4820 private final Object mLock;
4821
4822 @GuardedBy("mLock")
Svet Ganovba3ba812015-06-26 10:54:06 -07004823 private final SparseBooleanArray mWriteScheduled = new SparseBooleanArray();
Svetoslavc6d1c342015-02-26 14:44:43 -08004824
4825 @GuardedBy("mLock")
Svet Ganovba3ba812015-06-26 10:54:06 -07004826 // The mapping keys are user ids.
4827 private final SparseLongArray mLastNotWrittenMutationTimesMillis = new SparseLongArray();
4828
4829 @GuardedBy("mLock")
4830 // The mapping keys are user ids.
4831 private final SparseArray<String> mFingerprints = new SparseArray<>();
4832
4833 @GuardedBy("mLock")
4834 // The mapping keys are user ids.
4835 private final SparseBooleanArray mDefaultPermissionsGranted = new SparseBooleanArray();
Svetoslavc6d1c342015-02-26 14:44:43 -08004836
4837 public RuntimePermissionPersistence(Object lock) {
4838 mLock = lock;
4839 }
4840
Svet Ganovba3ba812015-06-26 10:54:06 -07004841 public boolean areDefaultRuntimPermissionsGrantedLPr(int userId) {
4842 return mDefaultPermissionsGranted.get(userId);
4843 }
4844
4845 public void onDefaultRuntimePermissionsGrantedLPr(int userId) {
4846 mFingerprints.put(userId, Build.FINGERPRINT);
4847 writePermissionsForUserAsyncLPr(userId);
4848 }
4849
Svetoslavc6d1c342015-02-26 14:44:43 -08004850 public void writePermissionsForUserSyncLPr(int userId) {
4851 mHandler.removeMessages(userId);
4852 writePermissionsSync(userId);
4853 }
4854
4855 public void writePermissionsForUserAsyncLPr(int userId) {
4856 final long currentTimeMillis = SystemClock.uptimeMillis();
4857
4858 if (mWriteScheduled.get(userId)) {
4859 mHandler.removeMessages(userId);
4860
4861 // If enough time passed, write without holding off anymore.
4862 final long lastNotWrittenMutationTimeMillis = mLastNotWrittenMutationTimesMillis
4863 .get(userId);
4864 final long timeSinceLastNotWrittenMutationMillis = currentTimeMillis
4865 - lastNotWrittenMutationTimeMillis;
4866 if (timeSinceLastNotWrittenMutationMillis >= MAX_WRITE_PERMISSIONS_DELAY_MILLIS) {
4867 mHandler.obtainMessage(userId).sendToTarget();
4868 return;
4869 }
4870
4871 // Hold off a bit more as settings are frequently changing.
4872 final long maxDelayMillis = Math.max(lastNotWrittenMutationTimeMillis
4873 + MAX_WRITE_PERMISSIONS_DELAY_MILLIS - currentTimeMillis, 0);
4874 final long writeDelayMillis = Math.min(WRITE_PERMISSIONS_DELAY_MILLIS,
4875 maxDelayMillis);
4876
4877 Message message = mHandler.obtainMessage(userId);
4878 mHandler.sendMessageDelayed(message, writeDelayMillis);
4879 } else {
4880 mLastNotWrittenMutationTimesMillis.put(userId, currentTimeMillis);
4881 Message message = mHandler.obtainMessage(userId);
4882 mHandler.sendMessageDelayed(message, WRITE_PERMISSIONS_DELAY_MILLIS);
4883 mWriteScheduled.put(userId, true);
4884 }
4885 }
4886
4887 private void writePermissionsSync(int userId) {
4888 AtomicFile destination = new AtomicFile(getUserRuntimePermissionsFile(userId));
4889
Svet Ganov8c7f7002015-05-07 10:48:44 -07004890 ArrayMap<String, List<PermissionState>> permissionsForPackage = new ArrayMap<>();
4891 ArrayMap<String, List<PermissionState>> permissionsForSharedUser = new ArrayMap<>();
Svetoslavc6d1c342015-02-26 14:44:43 -08004892
4893 synchronized (mLock) {
4894 mWriteScheduled.delete(userId);
4895
4896 final int packageCount = mPackages.size();
4897 for (int i = 0; i < packageCount; i++) {
4898 String packageName = mPackages.keyAt(i);
4899 PackageSetting packageSetting = mPackages.valueAt(i);
4900 if (packageSetting.sharedUser == null) {
4901 PermissionsState permissionsState = packageSetting.getPermissionsState();
Svet Ganov8c7f7002015-05-07 10:48:44 -07004902 List<PermissionState> permissionsStates = permissionsState
4903 .getRuntimePermissionStates(userId);
4904 if (!permissionsStates.isEmpty()) {
4905 permissionsForPackage.put(packageName, permissionsStates);
Svetoslavc6d1c342015-02-26 14:44:43 -08004906 }
4907 }
4908 }
4909
4910 final int sharedUserCount = mSharedUsers.size();
4911 for (int i = 0; i < sharedUserCount; i++) {
4912 String sharedUserName = mSharedUsers.keyAt(i);
4913 SharedUserSetting sharedUser = mSharedUsers.valueAt(i);
4914 PermissionsState permissionsState = sharedUser.getPermissionsState();
Svet Ganov8c7f7002015-05-07 10:48:44 -07004915 List<PermissionState> permissionsStates = permissionsState
4916 .getRuntimePermissionStates(userId);
4917 if (!permissionsStates.isEmpty()) {
4918 permissionsForSharedUser.put(sharedUserName, permissionsStates);
Svetoslavc6d1c342015-02-26 14:44:43 -08004919 }
4920 }
4921 }
4922
4923 FileOutputStream out = null;
4924 try {
4925 out = destination.startWrite();
4926
4927 XmlSerializer serializer = Xml.newSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004928 serializer.setOutput(out, StandardCharsets.UTF_8.name());
Svetoslavc6d1c342015-02-26 14:44:43 -08004929 serializer.setFeature(
4930 "http://xmlpull.org/v1/doc/features.html#indent-output", true);
4931 serializer.startDocument(null, true);
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07004932
Svetoslavc6d1c342015-02-26 14:44:43 -08004933 serializer.startTag(null, TAG_RUNTIME_PERMISSIONS);
4934
Svet Ganovba3ba812015-06-26 10:54:06 -07004935 String fingerprint = mFingerprints.get(userId);
Svetoslavcdfd2302015-06-25 19:07:31 -07004936 if (fingerprint != null) {
4937 serializer.attribute(null, ATTR_FINGERPRINT, fingerprint);
4938 }
Svet Ganovba3ba812015-06-26 10:54:06 -07004939
Svetoslavc6d1c342015-02-26 14:44:43 -08004940 final int packageCount = permissionsForPackage.size();
4941 for (int i = 0; i < packageCount; i++) {
4942 String packageName = permissionsForPackage.keyAt(i);
Svet Ganov8c7f7002015-05-07 10:48:44 -07004943 List<PermissionState> permissionStates = permissionsForPackage.valueAt(i);
Svetoslavc6d1c342015-02-26 14:44:43 -08004944 serializer.startTag(null, TAG_PACKAGE);
4945 serializer.attribute(null, ATTR_NAME, packageName);
Svet Ganov8c7f7002015-05-07 10:48:44 -07004946 writePermissions(serializer, permissionStates);
Svetoslavc6d1c342015-02-26 14:44:43 -08004947 serializer.endTag(null, TAG_PACKAGE);
4948 }
4949
4950 final int sharedUserCount = permissionsForSharedUser.size();
4951 for (int i = 0; i < sharedUserCount; i++) {
4952 String packageName = permissionsForSharedUser.keyAt(i);
Svet Ganov8c7f7002015-05-07 10:48:44 -07004953 List<PermissionState> permissionStates = permissionsForSharedUser.valueAt(i);
Svetoslavc6d1c342015-02-26 14:44:43 -08004954 serializer.startTag(null, TAG_SHARED_USER);
4955 serializer.attribute(null, ATTR_NAME, packageName);
Svet Ganov8c7f7002015-05-07 10:48:44 -07004956 writePermissions(serializer, permissionStates);
Svetoslavc6d1c342015-02-26 14:44:43 -08004957 serializer.endTag(null, TAG_SHARED_USER);
4958 }
4959
4960 serializer.endTag(null, TAG_RUNTIME_PERMISSIONS);
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07004961
4962 // Now any restored permission grants that are waiting for the apps
4963 // in question to be installed. These are stored as per-package
4964 // TAG_RESTORED_RUNTIME_PERMISSIONS blocks, each containing some
4965 // number of individual permission grant entities.
4966 if (mRestoredUserGrants.get(userId) != null) {
4967 ArrayMap<String, ArraySet<RestoredPermissionGrant>> restoredGrants =
4968 mRestoredUserGrants.get(userId);
4969 if (restoredGrants != null) {
4970 final int pkgCount = restoredGrants.size();
4971 for (int i = 0; i < pkgCount; i++) {
4972 final ArraySet<RestoredPermissionGrant> pkgGrants =
4973 restoredGrants.valueAt(i);
4974 if (pkgGrants != null && pkgGrants.size() > 0) {
4975 final String pkgName = restoredGrants.keyAt(i);
4976 serializer.startTag(null, TAG_RESTORED_RUNTIME_PERMISSIONS);
4977 serializer.attribute(null, ATTR_PACKAGE_NAME, pkgName);
4978
4979 final int N = pkgGrants.size();
4980 for (int z = 0; z < N; z++) {
4981 RestoredPermissionGrant g = pkgGrants.valueAt(z);
4982 serializer.startTag(null, TAG_PERMISSION_ENTRY);
4983 serializer.attribute(null, ATTR_NAME, g.permissionName);
4984
4985 if (g.granted) {
4986 serializer.attribute(null, ATTR_GRANTED, "true");
4987 }
4988
4989 if ((g.grantBits&FLAG_PERMISSION_USER_SET) != 0) {
4990 serializer.attribute(null, ATTR_USER_SET, "true");
4991 }
4992 if ((g.grantBits&FLAG_PERMISSION_USER_FIXED) != 0) {
4993 serializer.attribute(null, ATTR_USER_FIXED, "true");
4994 }
4995 if ((g.grantBits&FLAG_PERMISSION_REVOKE_ON_UPGRADE) != 0) {
4996 serializer.attribute(null, ATTR_REVOKE_ON_UPGRADE, "true");
4997 }
4998 serializer.endTag(null, TAG_PERMISSION_ENTRY);
4999 }
5000 serializer.endTag(null, TAG_RESTORED_RUNTIME_PERMISSIONS);
5001 }
5002 }
5003 }
5004 }
5005
Svetoslavc6d1c342015-02-26 14:44:43 -08005006 serializer.endDocument();
5007 destination.finishWrite(out);
Svet Ganovba0821e2015-04-22 13:34:31 -07005008
Svet Ganovba3ba812015-06-26 10:54:06 -07005009 if (Build.FINGERPRINT.equals(fingerprint)) {
5010 mDefaultPermissionsGranted.put(userId, true);
5011 }
5012 // Any error while writing is fatal.
Svet Ganovba0821e2015-04-22 13:34:31 -07005013 } catch (Throwable t) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005014 Slog.wtf(PackageManagerService.TAG,
Svet Ganovba0821e2015-04-22 13:34:31 -07005015 "Failed to write settings, restoring backup", t);
Svetoslavc6d1c342015-02-26 14:44:43 -08005016 destination.failWrite(out);
5017 } finally {
5018 IoUtils.closeQuietly(out);
5019 }
5020 }
5021
5022 private void onUserRemoved(int userId) {
5023 // Make sure we do not
5024 mHandler.removeMessages(userId);
5025
5026 for (SettingBase sb : mPackages.values()) {
Svet Ganov8c7f7002015-05-07 10:48:44 -07005027 revokeRuntimePermissionsAndClearFlags(sb, userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08005028 }
5029
5030 for (SettingBase sb : mSharedUsers.values()) {
Svet Ganov8c7f7002015-05-07 10:48:44 -07005031 revokeRuntimePermissionsAndClearFlags(sb, userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08005032 }
5033 }
5034
Svet Ganov8c7f7002015-05-07 10:48:44 -07005035 private void revokeRuntimePermissionsAndClearFlags(SettingBase sb, int userId) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005036 PermissionsState permissionsState = sb.getPermissionsState();
Svet Ganov8c7f7002015-05-07 10:48:44 -07005037 for (PermissionState permissionState
5038 : permissionsState.getRuntimePermissionStates(userId)) {
5039 BasePermission bp = mPermissions.get(permissionState.getName());
Svetoslavc6d1c342015-02-26 14:44:43 -08005040 if (bp != null) {
5041 permissionsState.revokeRuntimePermission(bp, userId);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005042 permissionsState.updatePermissionFlags(bp, userId,
5043 PackageManager.MASK_PERMISSION_FLAGS, 0);
Svetoslavc6d1c342015-02-26 14:44:43 -08005044 }
5045 }
5046 }
5047
Svet Ganovadc1cf42015-06-15 16:36:24 -07005048 public void deleteUserRuntimePermissionsFile(int userId) {
5049 getUserRuntimePermissionsFile(userId).delete();
5050 }
5051
Svetoslavc6d1c342015-02-26 14:44:43 -08005052 public void readStateForUserSyncLPr(int userId) {
5053 File permissionsFile = getUserRuntimePermissionsFile(userId);
5054 if (!permissionsFile.exists()) {
5055 return;
5056 }
5057
5058 FileInputStream in;
5059 try {
Svetoslav3dcdd372015-05-29 13:00:32 -07005060 in = new AtomicFile(permissionsFile).openRead();
Svetoslavc6d1c342015-02-26 14:44:43 -08005061 } catch (FileNotFoundException fnfe) {
5062 Slog.i(PackageManagerService.TAG, "No permissions state");
5063 return;
5064 }
5065
5066 try {
5067 XmlPullParser parser = Xml.newPullParser();
5068 parser.setInput(in, null);
5069 parseRuntimePermissionsLPr(parser, userId);
Svet Ganovba0821e2015-04-22 13:34:31 -07005070
Svet Ganove723e542015-04-23 11:58:26 -07005071 } catch (XmlPullParserException | IOException e) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005072 throw new IllegalStateException("Failed parsing permissions file: "
Svet Ganove723e542015-04-23 11:58:26 -07005073 + permissionsFile , e);
Svetoslavc6d1c342015-02-26 14:44:43 -08005074 } finally {
5075 IoUtils.closeQuietly(in);
5076 }
5077 }
5078
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07005079 // Backup/restore support
5080
5081 public void rememberRestoredUserGrantLPr(String pkgName, String permission,
5082 boolean isGranted, int restoredFlagSet, int userId) {
5083 // This change will be remembered at write-settings time
5084 ArrayMap<String, ArraySet<RestoredPermissionGrant>> grantsByPackage =
5085 mRestoredUserGrants.get(userId);
5086 if (grantsByPackage == null) {
5087 grantsByPackage = new ArrayMap<String, ArraySet<RestoredPermissionGrant>>();
5088 mRestoredUserGrants.put(userId, grantsByPackage);
5089 }
5090
5091 ArraySet<RestoredPermissionGrant> grants = grantsByPackage.get(pkgName);
5092 if (grants == null) {
5093 grants = new ArraySet<RestoredPermissionGrant>();
5094 grantsByPackage.put(pkgName, grants);
5095 }
5096
5097 RestoredPermissionGrant grant = new RestoredPermissionGrant(permission,
5098 isGranted, restoredFlagSet);
5099 grants.add(grant);
5100 }
5101
5102 // Private internals
5103
Svetoslavc6d1c342015-02-26 14:44:43 -08005104 private void parseRuntimePermissionsLPr(XmlPullParser parser, int userId)
5105 throws IOException, XmlPullParserException {
Svet Ganov12a692a2015-03-28 19:34:15 -07005106 final int outerDepth = parser.getDepth();
5107 int type;
5108 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
5109 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
5110 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
5111 continue;
5112 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005113
Svet Ganov12a692a2015-03-28 19:34:15 -07005114 switch (parser.getName()) {
Svet Ganovba3ba812015-06-26 10:54:06 -07005115 case TAG_RUNTIME_PERMISSIONS: {
5116 String fingerprint = parser.getAttributeValue(null, ATTR_FINGERPRINT);
5117 mFingerprints.put(userId, fingerprint);
5118 final boolean defaultsGranted = Build.FINGERPRINT.equals(fingerprint);
5119 mDefaultPermissionsGranted.put(userId, defaultsGranted);
5120 } break;
5121
Svet Ganov12a692a2015-03-28 19:34:15 -07005122 case TAG_PACKAGE: {
5123 String name = parser.getAttributeValue(null, ATTR_NAME);
5124 PackageSetting ps = mPackages.get(name);
5125 if (ps == null) {
5126 Slog.w(PackageManagerService.TAG, "Unknown package:" + name);
5127 XmlUtils.skipCurrentTag(parser);
5128 continue;
5129 }
5130 parsePermissionsLPr(parser, ps.getPermissionsState(), userId);
5131 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08005132
Svet Ganov12a692a2015-03-28 19:34:15 -07005133 case TAG_SHARED_USER: {
5134 String name = parser.getAttributeValue(null, ATTR_NAME);
5135 SharedUserSetting sus = mSharedUsers.get(name);
5136 if (sus == null) {
5137 Slog.w(PackageManagerService.TAG, "Unknown shared user:" + name);
5138 XmlUtils.skipCurrentTag(parser);
5139 continue;
5140 }
5141 parsePermissionsLPr(parser, sus.getPermissionsState(), userId);
5142 } break;
Christopher Tatee9fd1fa2015-09-15 16:43:07 -07005143
5144 case TAG_RESTORED_RUNTIME_PERMISSIONS: {
5145 final String pkgName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
5146 parseRestoredRuntimePermissionsLPr(parser, pkgName, userId);
5147 } break;
5148 }
5149 }
5150 }
5151
5152 private void parseRestoredRuntimePermissionsLPr(XmlPullParser parser,
5153 final String pkgName, final int userId) throws IOException, XmlPullParserException {
5154 final int outerDepth = parser.getDepth();
5155 int type;
5156 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
5157 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
5158 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
5159 continue;
5160 }
5161
5162 switch (parser.getName()) {
5163 case TAG_PERMISSION_ENTRY: {
5164 final String permName = parser.getAttributeValue(null, ATTR_NAME);
5165 final boolean isGranted = "true".equals(
5166 parser.getAttributeValue(null, ATTR_GRANTED));
5167
5168 int permBits = 0;
5169 if ("true".equals(parser.getAttributeValue(null, ATTR_USER_SET))) {
5170 permBits |= FLAG_PERMISSION_USER_SET;
5171 }
5172 if ("true".equals(parser.getAttributeValue(null, ATTR_USER_FIXED))) {
5173 permBits |= FLAG_PERMISSION_USER_FIXED;
5174 }
5175 if ("true".equals(parser.getAttributeValue(null, ATTR_REVOKE_ON_UPGRADE))) {
5176 permBits |= FLAG_PERMISSION_REVOKE_ON_UPGRADE;
5177 }
5178
5179 if (isGranted || permBits != 0) {
5180 rememberRestoredUserGrantLPr(pkgName, permName, isGranted, permBits, userId);
5181 }
5182 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08005183 }
5184 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005185 }
5186
Svet Ganov12a692a2015-03-28 19:34:15 -07005187 private void parsePermissionsLPr(XmlPullParser parser, PermissionsState permissionsState,
Svetoslavc6d1c342015-02-26 14:44:43 -08005188 int userId) throws IOException, XmlPullParserException {
Svet Ganov12a692a2015-03-28 19:34:15 -07005189 final int outerDepth = parser.getDepth();
5190 int type;
5191 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
5192 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
5193 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
5194 continue;
Svetoslavc6d1c342015-02-26 14:44:43 -08005195 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005196
Svet Ganov12a692a2015-03-28 19:34:15 -07005197 switch (parser.getName()) {
5198 case TAG_ITEM: {
5199 String name = parser.getAttributeValue(null, ATTR_NAME);
5200 BasePermission bp = mPermissions.get(name);
5201 if (bp == null) {
5202 Slog.w(PackageManagerService.TAG, "Unknown permission:" + name);
5203 XmlUtils.skipCurrentTag(parser);
5204 continue;
5205 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005206
Svet Ganov8c7f7002015-05-07 10:48:44 -07005207 String grantedStr = parser.getAttributeValue(null, ATTR_GRANTED);
5208 final boolean granted = grantedStr == null
5209 || Boolean.parseBoolean(grantedStr);
5210
5211 String flagsStr = parser.getAttributeValue(null, ATTR_FLAGS);
5212 final int flags = (flagsStr != null)
5213 ? Integer.parseInt(flagsStr, 16) : 0;
5214
5215 if (granted) {
Svet Ganovadc1cf42015-06-15 16:36:24 -07005216 permissionsState.grantRuntimePermission(bp, userId);
5217 permissionsState.updatePermissionFlags(bp, userId,
Svet Ganov8c7f7002015-05-07 10:48:44 -07005218 PackageManager.MASK_PERMISSION_FLAGS, flags);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005219 } else {
Svet Ganovadc1cf42015-06-15 16:36:24 -07005220 permissionsState.updatePermissionFlags(bp, userId,
5221 PackageManager.MASK_PERMISSION_FLAGS, flags);
Svet Ganov12a692a2015-03-28 19:34:15 -07005222 }
Svet Ganov8c7f7002015-05-07 10:48:44 -07005223
Svet Ganov12a692a2015-03-28 19:34:15 -07005224 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08005225 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005226 }
Svetoslavc6d1c342015-02-26 14:44:43 -08005227 }
5228
Svet Ganov8c7f7002015-05-07 10:48:44 -07005229 private void writePermissions(XmlSerializer serializer,
5230 List<PermissionState> permissionStates) throws IOException {
5231 for (PermissionState permissionState : permissionStates) {
Svetoslavc6d1c342015-02-26 14:44:43 -08005232 serializer.startTag(null, TAG_ITEM);
Svet Ganov8c7f7002015-05-07 10:48:44 -07005233 serializer.attribute(null, ATTR_NAME,permissionState.getName());
5234 serializer.attribute(null, ATTR_GRANTED,
5235 String.valueOf(permissionState.isGranted()));
5236 serializer.attribute(null, ATTR_FLAGS,
5237 Integer.toHexString(permissionState.getFlags()));
Svetoslavc6d1c342015-02-26 14:44:43 -08005238 serializer.endTag(null, TAG_ITEM);
5239 }
5240 }
5241
5242 private final class MyHandler extends Handler {
5243 public MyHandler() {
5244 super(BackgroundThread.getHandler().getLooper());
5245 }
5246
5247 @Override
5248 public void handleMessage(Message message) {
5249 final int userId = message.what;
5250 Runnable callback = (Runnable) message.obj;
5251 writePermissionsSync(userId);
5252 if (callback != null) {
5253 callback.run();
5254 }
5255 }
5256 }
5257 }
Andy McFadden2f362292012-01-20 14:43:38 -08005258}