blob: f3fdb0d409fdfad7be0d1c0b53e50440fa9ad0e0 [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
19import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
20import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -080021import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
Dianne Hackborn0ac30312011-06-17 14:49:23 -070022import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070023import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Jeff Sharkey1c27576a2012-04-11 19:07:08 -070024import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
Fabrice Di Meglio07885952015-04-06 19:41:28 -070025import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
26import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080027import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
Jeff Sharkey184a0102013-07-10 16:19:52 -070028import static android.os.Process.SYSTEM_UID;
29import static android.os.Process.PACKAGE_INFO_GID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070030
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080031import android.content.IntentFilter;
32import android.content.pm.ActivityInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080033import android.content.pm.IntentFilterVerificationInfo;
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080034import android.content.pm.ResolveInfo;
35import android.net.Uri;
Narayan Kamathff110bd2014-07-04 18:30:45 +010036import android.os.Binder;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -070037import android.os.Build;
Narayan Kamathff110bd2014-07-04 18:30:45 +010038import android.os.Environment;
39import android.os.FileUtils;
Svetoslavc6d1c342015-02-26 14:44:43 -080040import android.os.Handler;
41import android.os.Message;
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080042import android.os.PatternMatcher;
Narayan Kamathff110bd2014-07-04 18:30:45 +010043import android.os.Process;
Svetoslavc6d1c342015-02-26 14:44:43 -080044import android.os.SystemClock;
Narayan Kamathff110bd2014-07-04 18:30:45 +010045import android.os.UserHandle;
Amith Yamasani26f9ea32014-09-11 10:56:51 -070046import android.os.UserManager;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070047import android.os.storage.VolumeInfo;
Svetoslavc6d1c342015-02-26 14:44:43 -080048import android.util.AtomicFile;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080049import android.text.TextUtils;
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -080050import android.util.LogPrinter;
Svetoslavc6d1c342015-02-26 14:44:43 -080051import android.util.SparseBooleanArray;
52import android.util.SparseLongArray;
Christopher Tatee012a232015-04-01 17:18:50 -070053
Svetoslavc6d1c342015-02-26 14:44:43 -080054import com.android.internal.annotations.GuardedBy;
55import com.android.internal.os.BackgroundThread;
56import com.android.internal.util.ArrayUtils;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070057import com.android.internal.util.FastXmlSerializer;
58import com.android.internal.util.JournaledFile;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070059import com.android.internal.util.Preconditions;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070060import com.android.internal.util.XmlUtils;
Christopher Tatee012a232015-04-01 17:18:50 -070061import com.android.server.backup.PreferredActivityBackupHelper;
Kenny Root447106f2011-03-23 11:00:15 -070062import com.android.server.pm.PackageManagerService.DumpState;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070063
Svetoslavc6d1c342015-02-26 14:44:43 -080064import java.io.FileNotFoundException;
Narayan Kamathdf6d6dc2014-04-28 14:18:34 +010065import java.util.Collection;
Alexandra Gherghina6e2ae252014-06-12 16:03:58 +010066
Kenny Rootcf0b38c2011-03-22 14:17:59 -070067import org.xmlpull.v1.XmlPullParser;
68import org.xmlpull.v1.XmlPullParserException;
69import org.xmlpull.v1.XmlSerializer;
70
Kenny Root447106f2011-03-23 11:00:15 -070071import android.content.ComponentName;
72import android.content.Intent;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070073import android.content.pm.ApplicationInfo;
74import android.content.pm.ComponentInfo;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070075import android.content.pm.PackageCleanItem;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070076import android.content.pm.PackageManager;
77import android.content.pm.PackageParser;
78import android.content.pm.PermissionInfo;
79import android.content.pm.Signature;
Amith Yamasani483f3b02012-03-13 16:08:00 -070080import android.content.pm.UserInfo;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070081import android.content.pm.PackageUserState;
Kenny Root0aaa0d92011-09-12 16:42:55 -070082import android.content.pm.VerifierDeviceIdentity;
Jeff Sharkey9f837a92014-10-24 12:07:24 -070083import android.util.ArrayMap;
84import android.util.ArraySet;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070085import android.util.Log;
86import android.util.Slog;
87import android.util.SparseArray;
88import android.util.Xml;
89
90import java.io.BufferedOutputStream;
91import java.io.File;
92import java.io.FileInputStream;
93import java.io.FileOutputStream;
94import java.io.IOException;
95import java.io.PrintWriter;
Kenny Root447106f2011-03-23 11:00:15 -070096import java.text.SimpleDateFormat;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070097import java.util.ArrayList;
98import java.util.Arrays;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080099import java.util.Collections;
Kenny Root447106f2011-03-23 11:00:15 -0700100import java.util.Date;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700101import java.util.Iterator;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700102import java.util.List;
Andy McFadden2f362292012-01-20 14:43:38 -0800103import java.util.Map;
Jeff Sharkey84f12942014-07-10 17:48:11 -0700104import java.util.Objects;
Dianne Hackborn63092712012-10-07 14:45:35 -0700105import java.util.Set;
106import java.util.Map.Entry;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700107
Kenny Root62539e92011-11-10 09:35:38 -0800108import libcore.io.IoUtils;
109
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700110/**
111 * Holds information about dynamic settings.
112 */
113final class Settings {
Kenny Root447106f2011-03-23 11:00:15 -0700114 private static final String TAG = "PackageSettings";
115
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700116 /**
117 * Current version of the package database. Set it to the latest version in
118 * the {@link DatabaseVersion} class below to ensure the database upgrade
119 * doesn't happen repeatedly.
120 * <p>
121 * Note that care should be taken to make sure all database upgrades are
122 * idempotent.
123 */
Jeff Sharkeyd68b87c2014-11-12 12:18:11 -0800124 private static final int CURRENT_DATABASE_VERSION = DatabaseVersion.SIGNATURE_MALFORMED_RECOVER;
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700125
126 /**
127 * This class contains constants that can be referred to from upgrade code.
128 * Insert constant values here that describe the upgrade reason. The version
129 * code must be monotonically increasing.
130 */
131 public static class DatabaseVersion {
132 /**
133 * The initial version of the database.
134 */
135 public static final int FIRST_VERSION = 1;
Kenny Roota8e65fd2014-04-24 11:44:47 -0700136
137 /**
138 * Migrating the Signature array from the entire certificate chain to
139 * just the signing certificate.
140 */
141 public static final int SIGNATURE_END_ENTITY = 2;
Jeff Sharkeyd68b87c2014-11-12 12:18:11 -0800142
143 /**
144 * There was a window of time in
145 * {@link android.os.Build.VERSION_CODES#LOLLIPOP} where we persisted
146 * certificates after potentially mutating them. To switch back to the
147 * original untouched certificates, we need to force a collection pass.
148 */
149 public static final int SIGNATURE_MALFORMED_RECOVER = 3;
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700150 }
151
Kenny Root447106f2011-03-23 11:00:15 -0700152 private static final boolean DEBUG_STOPPED = false;
Amith Yamasani258848d2012-08-10 17:06:33 -0700153 private static final boolean DEBUG_MU = false;
Kenny Root447106f2011-03-23 11:00:15 -0700154
Svetoslavc6d1c342015-02-26 14:44:43 -0800155 private static final String RUNTIME_PERMISSIONS_FILE_NAME = "runtime-permissions.xml";
156
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -0700157 private static final String TAG_READ_EXTERNAL_STORAGE = "read-external-storage";
158 private static final String ATTR_ENFORCEMENT = "enforcement";
159
Amith Yamasani483f3b02012-03-13 16:08:00 -0700160 private static final String TAG_ITEM = "item";
161 private static final String TAG_DISABLED_COMPONENTS = "disabled-components";
162 private static final String TAG_ENABLED_COMPONENTS = "enabled-components";
163 private static final String TAG_PACKAGE_RESTRICTIONS = "package-restrictions";
164 private static final String TAG_PACKAGE = "pkg";
Svetoslavc6d1c342015-02-26 14:44:43 -0800165 private static final String TAG_SHARED_USER = "shared-user";
166 private static final String TAG_RUNTIME_PERMISSIONS = "runtime-permissions";
167 private static final String TAG_PERMISSIONS = "perms";
Sander Alewijnseaf597622014-03-20 18:44:57 +0000168 private static final String TAG_PERSISTENT_PREFERRED_ACTIVITIES =
169 "persistent-preferred-activities";
Nicolas Prevot81948992014-05-16 18:25:26 +0100170 static final String TAG_CROSS_PROFILE_INTENT_FILTERS =
171 "crossProfile-intent-filters";
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800172 public static final String TAG_DOMAIN_VERIFICATION = "domain-verification";
Fabrice Di Meglio62271722015-04-10 17:24:02 -0700173 public static final String TAG_DEFAULT_APPS= "default-apps";
174 public static final String TAG_DEFAULT_BROWSER= "default-browser";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700175
176 private static final String ATTR_NAME = "name";
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700177 private static final String ATTR_USER = "user";
178 private static final String ATTR_CODE = "code";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700179 private static final String ATTR_NOT_LAUNCHED = "nl";
180 private static final String ATTR_ENABLED = "enabled";
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -0700181 private static final String ATTR_ENABLED_CALLER = "enabledCaller";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700182 private static final String ATTR_STOPPED = "stopped";
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700183 // Legacy, here for reading older versions of the package-restrictions.
Amith Yamasani655d0e22013-06-12 14:19:10 -0700184 private static final String ATTR_BLOCKED = "blocked";
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700185 // New name for the above attribute.
186 private static final String ATTR_HIDDEN = "hidden";
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700187 private static final String ATTR_INSTALLED = "inst";
Kenny Guyc13053b2014-05-29 14:17:17 +0100188 private static final String ATTR_BLOCK_UNINSTALL = "blockUninstall";
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800189 private static final String ATTR_DOMAIN_VERIFICATON_STATE = "domainVerificationStatus";
Fabrice Di Meglio62271722015-04-10 17:24:02 -0700190 private static final String ATTR_PACKAGE_NAME= "packageName";
Amith Yamasani483f3b02012-03-13 16:08:00 -0700191
Svetoslavc6d1c342015-02-26 14:44:43 -0800192 private final Object mLock;
Svetoslavc6d1c342015-02-26 14:44:43 -0800193
194 private final RuntimePermissionPersistence mRuntimePermissionsPersistence;
195
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700196 private final File mSettingsFilename;
197 private final File mBackupSettingsFilename;
198 private final File mPackageListFilename;
199 private final File mStoppedPackagesFilename;
200 private final File mBackupStoppedPackagesFilename;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700201
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700202 final ArrayMap<String, PackageSetting> mPackages =
203 new ArrayMap<String, PackageSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700204 // List of replaced system applications
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700205 private final ArrayMap<String, PackageSetting> mDisabledSysPackages =
206 new ArrayMap<String, PackageSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700207
Geremy Condra12c18382013-03-06 16:49:06 -0800208 private static int mFirstAvailableUid = 0;
209
Jeff Sharkey529f91f2015-04-18 20:23:13 -0700210 // TODO: store SDK versions and fingerprint for each volume UUID
211
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700212 // These are the last platform API version we were using for
213 // the apps installed on internal and external storage. It is
214 // used to grant newer permissions one time during a system upgrade.
215 int mInternalSdkPlatform;
216 int mExternalSdkPlatform;
Kenny Root0aaa0d92011-09-12 16:42:55 -0700217
Kenny Rootc1c0d3c2014-04-24 13:36:40 -0700218 /**
219 * The current database version for apps on internal storage. This is
220 * used to upgrade the format of the packages.xml database not necessarily
221 * tied to an SDK version.
222 */
223 int mInternalDatabaseVersion;
224 int mExternalDatabaseVersion;
225
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700226 /**
227 * Last known value of {@link Build#FINGERPRINT}. Used to determine when an
228 * system update has occurred, meaning we need to clear code caches.
229 */
230 String mFingerprint;
231
Jeff Sharkeyf5385772012-05-11 14:04:41 -0700232 Boolean mReadExternalStorageEnforced;
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -0700233
Kenny Root0aaa0d92011-09-12 16:42:55 -0700234 /** Device identity for the purpose of package verification. */
235 private VerifierDeviceIdentity mVerifierDeviceIdentity;
236
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700237 // The user's preferred activities associated with particular intent
238 // filters.
Dianne Hackborn63092712012-10-07 14:45:35 -0700239 final SparseArray<PreferredIntentResolver> mPreferredActivities =
240 new SparseArray<PreferredIntentResolver>();
241
Sander Alewijnsef475ca32014-02-17 15:13:58 +0000242 // The persistent preferred activities of the user's profile/device owner
243 // associated with particular intent filters.
244 final SparseArray<PersistentPreferredIntentResolver> mPersistentPreferredActivities =
245 new SparseArray<PersistentPreferredIntentResolver>();
246
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000247 // For every user, it is used to find to which other users the intent can be forwarded.
Nicolas Prevot81948992014-05-16 18:25:26 +0100248 final SparseArray<CrossProfileIntentResolver> mCrossProfileIntentResolvers =
249 new SparseArray<CrossProfileIntentResolver>();
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000250
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700251 final ArrayMap<String, SharedUserSetting> mSharedUsers =
252 new ArrayMap<String, SharedUserSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700253 private final ArrayList<Object> mUserIds = new ArrayList<Object>();
254 private final SparseArray<Object> mOtherUserIds =
255 new SparseArray<Object>();
256
257 // For reading/writing settings file.
258 private final ArrayList<Signature> mPastSignatures =
259 new ArrayList<Signature>();
dcashman8c04fac2015-03-23 11:39:42 -0700260 private final ArrayMap<Long, Integer> mKeySetRefs =
261 new ArrayMap<Long, Integer>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700262
263 // Mapping from permission names to info about them.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700264 final ArrayMap<String, BasePermission> mPermissions =
265 new ArrayMap<String, BasePermission>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700266
267 // Mapping from permission tree names to info about them.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700268 final ArrayMap<String, BasePermission> mPermissionTrees =
269 new ArrayMap<String, BasePermission>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700270
271 // Packages that have been uninstalled and still need their external
272 // storage data deleted.
Jeff Sharkey752cd922012-09-23 16:25:12 -0700273 final ArrayList<PackageCleanItem> mPackagesToBeCleaned = new ArrayList<PackageCleanItem>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700274
275 // Packages that have been renamed since they were first installed.
276 // Keys are the new names of the packages, values are the original
277 // names. The packages appear everwhere else under their original
278 // names.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700279 final ArrayMap<String, String> mRenamedPackages = new ArrayMap<String, String>();
Fabrice Di Meglio62271722015-04-10 17:24:02 -0700280
281 // For every user, it is used to find the package name of the default Browser App.
282 final SparseArray<String> mDefaultBrowserApp = new SparseArray<String>();
283
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700284 final StringBuilder mReadMessages = new StringBuilder();
285
Kenny Root447106f2011-03-23 11:00:15 -0700286 /**
287 * Used to track packages that have a shared user ID that hasn't been read
288 * in yet.
289 * <p>
290 * TODO: make this just a local variable that is passed in during package
291 * scanning to make it less confusing.
292 */
293 private final ArrayList<PendingPackage> mPendingPackages = new ArrayList<PendingPackage>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700294
Amith Yamasani483f3b02012-03-13 16:08:00 -0700295 private final File mSystemDir;
Geremy Condraf1bcca82013-01-07 22:35:24 -0800296
dcashman55b10782014-04-09 14:20:38 -0700297 public final KeySetManagerService mKeySetManagerService = new KeySetManagerService(mPackages);
Geremy Condraf1bcca82013-01-07 22:35:24 -0800298
Svet Ganov12a692a2015-03-28 19:34:15 -0700299 Settings(Object lock) {
300 this(Environment.getDataDirectory(), lock);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700301 }
302
Svet Ganov12a692a2015-03-28 19:34:15 -0700303 Settings(File dataDir, Object lock) {
Svetoslavc6d1c342015-02-26 14:44:43 -0800304 mLock = lock;
305
306 mRuntimePermissionsPersistence = new RuntimePermissionPersistence(mLock);
307
Amith Yamasani483f3b02012-03-13 16:08:00 -0700308 mSystemDir = new File(dataDir, "system");
309 mSystemDir.mkdirs();
310 FileUtils.setPermissions(mSystemDir.toString(),
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700311 FileUtils.S_IRWXU|FileUtils.S_IRWXG
312 |FileUtils.S_IROTH|FileUtils.S_IXOTH,
313 -1, -1);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700314 mSettingsFilename = new File(mSystemDir, "packages.xml");
315 mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
316 mPackageListFilename = new File(mSystemDir, "packages.list");
Jeff Sharkeyaebb65c2014-11-24 15:00:13 -0800317 FileUtils.setPermissions(mPackageListFilename, 0640, SYSTEM_UID, PACKAGE_INFO_GID);
Jeff Sharkey184a0102013-07-10 16:19:52 -0700318
Amith Yamasani483f3b02012-03-13 16:08:00 -0700319 // Deprecated: Needed for migration
320 mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
321 mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700322 }
323
Kenny Root447106f2011-03-23 11:00:15 -0700324 PackageSetting getPackageLPw(PackageParser.Package pkg, PackageSetting origPackage,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700325 String realName, SharedUserSetting sharedUser, File codePath, File resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100326 String legacyNativeLibraryPathString, String primaryCpuAbi, String secondaryCpuAbi,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800327 int pkgFlags, int pkgPrivateFlags, UserHandle user, boolean add) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700328 final String name = pkg.packageName;
Kenny Root447106f2011-03-23 11:00:15 -0700329 PackageSetting p = getPackageLPw(name, origPackage, realName, sharedUser, codePath,
Jeff Sharkey84f12942014-07-10 17:48:11 -0700330 resourcePath, legacyNativeLibraryPathString, primaryCpuAbi, secondaryCpuAbi,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800331 pkg.mVersionCode, pkgFlags, pkgPrivateFlags, user, add, true /* allowInstall */);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700332 return p;
333 }
334
Kenny Root447106f2011-03-23 11:00:15 -0700335 PackageSetting peekPackageLPr(String name) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700336 return mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700337 }
338
339 void setInstallStatus(String pkgName, int status) {
340 PackageSetting p = mPackages.get(pkgName);
341 if(p != null) {
342 if(p.getInstallStatus() != status) {
343 p.setInstallStatus(status);
344 }
345 }
346 }
347
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -0700348 void setInstallerPackageName(String pkgName, String installerPkgName) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700349 PackageSetting p = mPackages.get(pkgName);
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -0700350 if (p != null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700351 p.setInstallerPackageName(installerPkgName);
352 }
353 }
354
Kenny Root447106f2011-03-23 11:00:15 -0700355 SharedUserSetting getSharedUserLPw(String name,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800356 int pkgFlags, int pkgPrivateFlags, boolean create) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700357 SharedUserSetting s = mSharedUsers.get(name);
358 if (s == null) {
359 if (!create) {
360 return null;
361 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800362 s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800363 s.userId = newUserIdLPw(s);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700364 Log.i(PackageManagerService.TAG, "New shared user " + name + ": id=" + s.userId);
365 // < 0 means we couldn't assign a userid; fall out and return
366 // s, which is currently null
367 if (s.userId >= 0) {
368 mSharedUsers.put(name, s);
369 }
370 }
371
372 return s;
373 }
374
Narayan Kamathdf6d6dc2014-04-28 14:18:34 +0100375 Collection<SharedUserSetting> getAllSharedUsersLPw() {
376 return mSharedUsers.values();
377 }
378
379
Kenny Root447106f2011-03-23 11:00:15 -0700380 boolean disableSystemPackageLPw(String name) {
381 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700382 if(p == null) {
383 Log.w(PackageManagerService.TAG, "Package:"+name+" is not an installed package");
384 return false;
385 }
Kenny Root447106f2011-03-23 11:00:15 -0700386 final PackageSetting dp = mDisabledSysPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700387 // always make sure the system package code and resource paths dont change
388 if (dp == null) {
389 if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
390 p.pkg.applicationInfo.flags |= ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
391 }
392 mDisabledSysPackages.put(name, p);
393
394 // a little trick... when we install the new package, we don't
395 // want to modify the existing PackageSetting for the built-in
396 // version. so at this point we need a new PackageSetting that
397 // is okay to muck with.
398 PackageSetting newp = new PackageSetting(p);
Kenny Root447106f2011-03-23 11:00:15 -0700399 replacePackageLPw(name, newp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700400 return true;
401 }
402 return false;
403 }
404
Kenny Root447106f2011-03-23 11:00:15 -0700405 PackageSetting enableSystemPackageLPw(String name) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700406 PackageSetting p = mDisabledSysPackages.get(name);
407 if(p == null) {
408 Log.w(PackageManagerService.TAG, "Package:"+name+" is not disabled");
409 return null;
410 }
411 // Reset flag in ApplicationInfo object
412 if((p.pkg != null) && (p.pkg.applicationInfo != null)) {
413 p.pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
414 }
Kenny Root447106f2011-03-23 11:00:15 -0700415 PackageSetting ret = addPackageLPw(name, p.realName, p.codePath, p.resourcePath,
Narayan Kamathff110bd2014-07-04 18:30:45 +0100416 p.legacyNativeLibraryPathString, p.primaryCpuAbiString,
Narayan Kamath4903f642014-08-11 13:33:45 +0100417 p.secondaryCpuAbiString, p.secondaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800418 p.appId, p.versionCode, p.pkgFlags, p.pkgPrivateFlags);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700419 mDisabledSysPackages.remove(name);
420 return ret;
421 }
422
Kenny Rootc52d6fd2012-05-07 23:04:52 -0700423 boolean isDisabledSystemPackageLPr(String name) {
424 return mDisabledSysPackages.containsKey(name);
425 }
426
427 void removeDisabledSystemPackageLPw(String name) {
428 mDisabledSysPackages.remove(name);
429 }
430
Kenny Root447106f2011-03-23 11:00:15 -0700431 PackageSetting addPackageLPw(String name, String realName, File codePath, File resourcePath,
Narayan Kamathff110bd2014-07-04 18:30:45 +0100432 String legacyNativeLibraryPathString, String primaryCpuAbiString, String secondaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800433 String cpuAbiOverrideString, int uid, int vc, int pkgFlags, int pkgPrivateFlags) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700434 PackageSetting p = mPackages.get(name);
435 if (p != null) {
Amith Yamasani13593602012-03-22 16:16:17 -0700436 if (p.appId == uid) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700437 return p;
438 }
439 PackageManagerService.reportSettingsProblem(Log.ERROR,
440 "Adding duplicate package, keeping first: " + name);
441 return null;
442 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100443 p = new PackageSetting(name, realName, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100444 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800445 cpuAbiOverrideString, vc, pkgFlags, pkgPrivateFlags);
Amith Yamasani13593602012-03-22 16:16:17 -0700446 p.appId = uid;
Kenny Root447106f2011-03-23 11:00:15 -0700447 if (addUserIdLPw(uid, p, name)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700448 mPackages.put(name, p);
449 return p;
450 }
451 return null;
452 }
453
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800454 SharedUserSetting addSharedUserLPw(String name, int uid, int pkgFlags, int pkgPrivateFlags) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700455 SharedUserSetting s = mSharedUsers.get(name);
456 if (s != null) {
457 if (s.userId == uid) {
458 return s;
459 }
460 PackageManagerService.reportSettingsProblem(Log.ERROR,
461 "Adding duplicate shared user, keeping first: " + name);
462 return null;
463 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800464 s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700465 s.userId = uid;
Kenny Root447106f2011-03-23 11:00:15 -0700466 if (addUserIdLPw(uid, s, name)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700467 mSharedUsers.put(name, s);
468 return s;
469 }
470 return null;
471 }
472
Christopher Tate86b391c2013-09-13 16:58:36 -0700473 void pruneSharedUsersLPw() {
474 ArrayList<String> removeStage = new ArrayList<String>();
475 for (Map.Entry<String,SharedUserSetting> entry : mSharedUsers.entrySet()) {
476 final SharedUserSetting sus = entry.getValue();
477 if (sus == null || sus.packages.size() == 0) {
478 removeStage.add(entry.getKey());
479 }
480 }
481 for (int i = 0; i < removeStage.size(); i++) {
482 mSharedUsers.remove(removeStage.get(i));
483 }
484 }
485
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700486 // Transfer ownership of permissions from one package to another.
Kenny Root447106f2011-03-23 11:00:15 -0700487 void transferPermissionsLPw(String origPkg, String newPkg) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700488 // Transfer ownership of permissions to the new package.
489 for (int i=0; i<2; i++) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700490 ArrayMap<String, BasePermission> permissions =
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700491 i == 0 ? mPermissionTrees : mPermissions;
492 for (BasePermission bp : permissions.values()) {
493 if (origPkg.equals(bp.sourcePackage)) {
494 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG,
495 "Moving permission " + bp.name
496 + " from pkg " + bp.sourcePackage
497 + " to " + newPkg);
498 bp.sourcePackage = newPkg;
499 bp.packageSetting = null;
500 bp.perm = null;
501 if (bp.pendingInfo != null) {
502 bp.pendingInfo.packageName = newPkg;
503 }
504 bp.uid = 0;
Jeff Sharkey00f39042015-03-23 16:51:22 -0700505 bp.setGids(null, false);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700506 }
507 }
508 }
509 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700510
Kenny Root447106f2011-03-23 11:00:15 -0700511 private PackageSetting getPackageLPw(String name, PackageSetting origPackage,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700512 String realName, SharedUserSetting sharedUser, File codePath, File resourcePath,
Svetoslavc6d1c342015-02-26 14:44:43 -0800513 String legacyNativeLibraryPathString, String primaryCpuAbiString,
514 String secondaryCpuAbiString, int vc, int pkgFlags, int pkgPrivateFlags,
515 UserHandle installUser, boolean add, boolean allowInstall) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700516 PackageSetting p = mPackages.get(name);
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700517 UserManagerService userManager = UserManagerService.getInstance();
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700518 if (p != null) {
Narayan Kamathff110bd2014-07-04 18:30:45 +0100519 p.primaryCpuAbiString = primaryCpuAbiString;
520 p.secondaryCpuAbiString = secondaryCpuAbiString;
Narayan Kamath4903f642014-08-11 13:33:45 +0100521
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700522 if (!p.codePath.equals(codePath)) {
523 // Check to see if its a disabled system app
524 if ((p.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0) {
525 // This is an updated system app with versions in both system
526 // and data partition. Just let the most recent version
527 // take precedence.
Amith Yamasani483f3b02012-03-13 16:08:00 -0700528 Slog.w(PackageManagerService.TAG, "Trying to update system app code path from "
529 + p.codePathString + " to " + codePath.toString());
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700530 } else {
531 // Just a change in the code path is not an issue, but
532 // let's log a message about it.
Amith Yamasani483f3b02012-03-13 16:08:00 -0700533 Slog.i(PackageManagerService.TAG, "Package " + name + " codePath changed from "
534 + p.codePath + " to " + codePath + "; Retaining data and using new");
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700535 /*
536 * Since we've changed paths, we need to prefer the new
537 * native library path over the one stored in the
538 * package settings since we might have moved from
539 * internal to external storage or vice versa.
540 */
Narayan Kamathff110bd2014-07-04 18:30:45 +0100541 p.legacyNativeLibraryPathString = legacyNativeLibraryPathString;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700542 }
543 }
544 if (p.sharedUser != sharedUser) {
545 PackageManagerService.reportSettingsProblem(Log.WARN,
546 "Package " + name + " shared user changed from "
547 + (p.sharedUser != null ? p.sharedUser.name : "<nothing>")
548 + " to "
549 + (sharedUser != null ? sharedUser.name : "<nothing>")
550 + "; replacing with new");
551 p = null;
552 } else {
Christopher Tate9f088202013-10-22 15:36:01 -0700553 // If what we are scanning is a system (and possibly privileged) package,
554 // then make it so, regardless of whether it was previously installed only
555 // in the data partition.
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800556 p.pkgFlags |= pkgFlags & ApplicationInfo.FLAG_SYSTEM;
557 p.pkgPrivateFlags |= pkgPrivateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700558 }
559 }
560 if (p == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700561 if (origPackage != null) {
562 // We are consuming the data from an existing package.
563 p = new PackageSetting(origPackage.name, name, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100564 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800565 null /* cpuAbiOverrideString */, vc, pkgFlags, pkgPrivateFlags);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700566 if (PackageManagerService.DEBUG_UPGRADE) Log.v(PackageManagerService.TAG, "Package "
567 + name + " is adopting original package " + origPackage.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700568 // Note that we will retain the new package's signature so
569 // that we can keep its data.
570 PackageSignatures s = p.signatures;
571 p.copyFrom(origPackage);
572 p.signatures = s;
573 p.sharedUser = origPackage.sharedUser;
Amith Yamasani13593602012-03-22 16:16:17 -0700574 p.appId = origPackage.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700575 p.origPackage = origPackage;
576 mRenamedPackages.put(name, origPackage.name);
577 name = origPackage.name;
578 // Update new package state.
579 p.setTimeStamp(codePath.lastModified());
580 } else {
581 p = new PackageSetting(name, realName, codePath, resourcePath,
Narayan Kamath4903f642014-08-11 13:33:45 +0100582 legacyNativeLibraryPathString, primaryCpuAbiString, secondaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800583 null /* cpuAbiOverrideString */, vc, pkgFlags, pkgPrivateFlags);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700584 p.setTimeStamp(codePath.lastModified());
585 p.sharedUser = sharedUser;
586 // If this is not a system app, it starts out stopped.
587 if ((pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0) {
Kenny Root447106f2011-03-23 11:00:15 -0700588 if (DEBUG_STOPPED) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700589 RuntimeException e = new RuntimeException("here");
590 e.fillInStackTrace();
591 Slog.i(PackageManagerService.TAG, "Stopping package " + name, e);
592 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700593 List<UserInfo> users = getAllUsers();
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700594 final int installUserId = installUser != null ? installUser.getIdentifier() : 0;
Amith Yamasanif031f232012-10-26 15:35:21 -0700595 if (users != null && allowInstall) {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700596 for (UserInfo user : users) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700597 // By default we consider this app to be installed
598 // for the user if no user has been specified (which
599 // means to leave it at its original value, and the
600 // original default value is true), or we are being
601 // asked to install for all users, or this is the
602 // user we are installing for.
603 final boolean installed = installUser == null
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700604 || (installUserId == UserHandle.USER_ALL
605 && !isAdbInstallDisallowed(userManager, user.id))
606 || installUserId == user.id;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700607 p.setUserState(user.id, COMPONENT_ENABLED_STATE_DEFAULT,
608 installed,
609 true, // stopped,
610 true, // notLaunched
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700611 false, // hidden
Kenny Guyc13053b2014-05-29 14:17:17 +0100612 null, null, null,
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800613 false, // blockUninstall
614 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700615 writePackageRestrictionsLPr(user.id);
616 }
617 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700618 }
619 if (sharedUser != null) {
Amith Yamasani13593602012-03-22 16:16:17 -0700620 p.appId = sharedUser.userId;
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800621 } else {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700622 // Clone the setting here for disabled system packages
623 PackageSetting dis = mDisabledSysPackages.get(name);
624 if (dis != null) {
625 // For disabled packages a new setting is created
626 // from the existing user id. This still has to be
627 // added to list of user id's
628 // Copy signatures from previous setting
629 if (dis.signatures.mSignatures != null) {
630 p.signatures.mSignatures = dis.signatures.mSignatures.clone();
631 }
Amith Yamasani13593602012-03-22 16:16:17 -0700632 p.appId = dis.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700633 // Clone permissions
Svetoslavc6d1c342015-02-26 14:44:43 -0800634 p.getPermissionsState().copyFrom(dis.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700635 // Clone component info
Amith Yamasani483f3b02012-03-13 16:08:00 -0700636 List<UserInfo> users = getAllUsers();
637 if (users != null) {
638 for (UserInfo user : users) {
639 int userId = user.id;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700640 p.setDisabledComponentsCopy(
641 dis.getDisabledComponents(userId), userId);
642 p.setEnabledComponentsCopy(
643 dis.getEnabledComponents(userId), userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700644 }
645 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700646 // Add new setting to list of user ids
Amith Yamasani13593602012-03-22 16:16:17 -0700647 addUserIdLPw(p.appId, p, name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700648 } else {
649 // Assign new user id
Amith Yamasani13593602012-03-22 16:16:17 -0700650 p.appId = newUserIdLPw(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700651 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700652 }
653 }
Amith Yamasani13593602012-03-22 16:16:17 -0700654 if (p.appId < 0) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700655 PackageManagerService.reportSettingsProblem(Log.WARN,
656 "Package " + name + " could not be assigned a valid uid");
657 return null;
658 }
659 if (add) {
660 // Finish adding new package by adding it and updating shared
661 // user preferences
Kenny Root447106f2011-03-23 11:00:15 -0700662 addPackageSettingLPw(p, name, sharedUser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700663 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700664 } else {
Amith Yamasanif031f232012-10-26 15:35:21 -0700665 if (installUser != null && allowInstall) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700666 // The caller has explicitly specified the user they want this
667 // package installed for, and the package already exists.
668 // Make sure it conforms to the new request.
669 List<UserInfo> users = getAllUsers();
670 if (users != null) {
671 for (UserInfo user : users) {
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700672 if ((installUser.getIdentifier() == UserHandle.USER_ALL
673 && !isAdbInstallDisallowed(userManager, user.id))
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700674 || installUser.getIdentifier() == user.id) {
675 boolean installed = p.getInstalled(user.id);
676 if (!installed) {
677 p.setInstalled(true, user.id);
678 writePackageRestrictionsLPr(user.id);
679 }
680 }
681 }
682 }
683 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700684 }
685 return p;
686 }
687
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700688 boolean isAdbInstallDisallowed(UserManagerService userManager, int userId) {
689 return userManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES,
690 userId);
691 }
692
Kenny Root447106f2011-03-23 11:00:15 -0700693 void insertPackageSettingLPw(PackageSetting p, PackageParser.Package pkg) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700694 p.pkg = pkg;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700695 // pkg.mSetEnabled = p.getEnabled(userId);
696 // pkg.mSetStopped = p.getStopped(userId);
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700697 final String volumeUuid = pkg.applicationInfo.volumeUuid;
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700698 final String codePath = pkg.applicationInfo.getCodePath();
699 final String resourcePath = pkg.applicationInfo.getResourcePath();
Jeff Sharkey84f12942014-07-10 17:48:11 -0700700 final String legacyNativeLibraryPath = pkg.applicationInfo.nativeLibraryRootDir;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700701 // Update volume if needed
702 if (!Objects.equals(volumeUuid, p.volumeUuid)) {
703 Slog.w(PackageManagerService.TAG, "Volume for " + p.pkg.packageName +
704 " changing from " + p.volumeUuid + " to " + volumeUuid);
705 p.volumeUuid = volumeUuid;
706 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700707 // Update code path if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700708 if (!Objects.equals(codePath, p.codePathString)) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700709 Slog.w(PackageManagerService.TAG, "Code path for " + p.pkg.packageName +
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700710 " changing from " + p.codePathString + " to " + codePath);
711 p.codePath = new File(codePath);
712 p.codePathString = codePath;
713 }
714 //Update resource path if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700715 if (!Objects.equals(resourcePath, p.resourcePathString)) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700716 Slog.w(PackageManagerService.TAG, "Resource path for " + p.pkg.packageName +
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700717 " changing from " + p.resourcePathString + " to " + resourcePath);
718 p.resourcePath = new File(resourcePath);
719 p.resourcePathString = resourcePath;
720 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100721 // Update the native library paths if needed
Jeff Sharkey84f12942014-07-10 17:48:11 -0700722 if (!Objects.equals(legacyNativeLibraryPath, p.legacyNativeLibraryPathString)) {
723 p.legacyNativeLibraryPathString = legacyNativeLibraryPath;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700724 }
Narayan Kamathff110bd2014-07-04 18:30:45 +0100725
Narayan Kamath9e289d72014-04-10 09:26:59 +0000726 // Update the required Cpu Abi
Narayan Kamathff110bd2014-07-04 18:30:45 +0100727 p.primaryCpuAbiString = pkg.applicationInfo.primaryCpuAbi;
728 p.secondaryCpuAbiString = pkg.applicationInfo.secondaryCpuAbi;
Narayan Kamath4903f642014-08-11 13:33:45 +0100729 p.cpuAbiOverrideString = pkg.cpuAbiOverride;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700730 // Update version code if needed
Amith Yamasani483f3b02012-03-13 16:08:00 -0700731 if (pkg.mVersionCode != p.versionCode) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700732 p.versionCode = pkg.mVersionCode;
733 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700734 // Update signatures if needed.
735 if (p.signatures.mSignatures == null) {
736 p.signatures.assignSignatures(pkg.mSignatures);
737 }
Kenny Root5455f682012-09-09 14:52:10 -0700738 // Update flags if needed.
739 if (pkg.applicationInfo.flags != p.pkgFlags) {
740 p.pkgFlags = pkg.applicationInfo.flags;
741 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700742 // If this app defines a shared user id initialize
743 // the shared user signatures as well.
744 if (p.sharedUser != null && p.sharedUser.signatures.mSignatures == null) {
745 p.sharedUser.signatures.assignSignatures(pkg.mSignatures);
746 }
Kenny Root447106f2011-03-23 11:00:15 -0700747 addPackageSettingLPw(p, pkg.packageName, p.sharedUser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700748 }
749
750 // Utility method that adds a PackageSetting to mPackages and
751 // completes updating the shared user attributes
Kenny Root447106f2011-03-23 11:00:15 -0700752 private void addPackageSettingLPw(PackageSetting p, String name,
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700753 SharedUserSetting sharedUser) {
754 mPackages.put(name, p);
755 if (sharedUser != null) {
756 if (p.sharedUser != null && p.sharedUser != sharedUser) {
757 PackageManagerService.reportSettingsProblem(Log.ERROR,
758 "Package " + p.name + " was user "
759 + p.sharedUser + " but is now " + sharedUser
760 + "; I am not changing its files so it will probably fail!");
Ben Gruverdd72c9e2013-08-06 12:34:17 -0700761 p.sharedUser.removePackage(p);
Amith Yamasani13593602012-03-22 16:16:17 -0700762 } else if (p.appId != sharedUser.userId) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700763 PackageManagerService.reportSettingsProblem(Log.ERROR,
Amith Yamasani13593602012-03-22 16:16:17 -0700764 "Package " + p.name + " was user id " + p.appId
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700765 + " but is now user " + sharedUser
766 + " with id " + sharedUser.userId
767 + "; I am not changing its files so it will probably fail!");
768 }
769
Ben Gruverdd72c9e2013-08-06 12:34:17 -0700770 sharedUser.addPackage(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700771 p.sharedUser = sharedUser;
Amith Yamasani13593602012-03-22 16:16:17 -0700772 p.appId = sharedUser.userId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700773 }
774 }
775
776 /*
777 * Update the shared user setting when a package using
778 * specifying the shared user id is removed. The gids
779 * associated with each permission of the deleted package
780 * are removed from the shared user's gid list only if its
781 * not in use by other permissions of packages in the
782 * shared user setting.
783 */
Svetoslavc6d1c342015-02-26 14:44:43 -0800784 int updateSharedUserPermsLPw(PackageSetting deletedPs, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -0700785 if ((deletedPs == null) || (deletedPs.pkg == null)) {
786 Slog.i(PackageManagerService.TAG,
787 "Trying to update info for null package. Just ignoring");
Svetoslavc6d1c342015-02-26 14:44:43 -0800788 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700789 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800790
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700791 // No sharedUserId
792 if (deletedPs.sharedUser == null) {
Svetoslavc6d1c342015-02-26 14:44:43 -0800793 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700794 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800795
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700796 SharedUserSetting sus = deletedPs.sharedUser;
Svetoslavc6d1c342015-02-26 14:44:43 -0800797
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700798 // Update permissions
Kenny Root447106f2011-03-23 11:00:15 -0700799 for (String eachPerm : deletedPs.pkg.requestedPermissions) {
Svetoslavc6d1c342015-02-26 14:44:43 -0800800 BasePermission bp = mPermissions.get(eachPerm);
801 if (bp == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700802 continue;
803 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800804
805 // If no user has the permission, nothing to remove.
806 if (!sus.getPermissionsState().hasPermission(bp.name, userId)) {
807 continue;
808 }
809
810 boolean used = false;
811
812 // Check if another package in the shared user needs the permission.
813 for (PackageSetting pkg : sus.packages) {
814 if (pkg.pkg != null
815 && !pkg.pkg.packageName.equals(deletedPs.pkg.packageName)
816 && pkg.pkg.requestedPermissions.contains(eachPerm)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700817 used = true;
818 break;
819 }
820 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800821
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700822 if (!used) {
Svetoslavc6d1c342015-02-26 14:44:43 -0800823 // Try to revoke as an install permission which is for all users.
824 if (sus.getPermissionsState().revokeInstallPermission(bp) ==
825 PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED) {
826 return UserHandle.USER_ALL;
827 }
828
829 // Try to revoke as an install permission which is per user.
830 if (sus.getPermissionsState().revokeRuntimePermission(bp, userId) ==
831 PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED) {
832 return userId;
833 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700834 }
835 }
Svetoslavc6d1c342015-02-26 14:44:43 -0800836
837 return UserHandle.USER_NULL;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700838 }
839
Kenny Root447106f2011-03-23 11:00:15 -0700840 int removePackageLPw(String name) {
841 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700842 if (p != null) {
843 mPackages.remove(name);
844 if (p.sharedUser != null) {
Ben Gruverdd72c9e2013-08-06 12:34:17 -0700845 p.sharedUser.removePackage(p);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700846 if (p.sharedUser.packages.size() == 0) {
847 mSharedUsers.remove(p.sharedUser.name);
Kenny Root447106f2011-03-23 11:00:15 -0700848 removeUserIdLPw(p.sharedUser.userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700849 return p.sharedUser.userId;
850 }
851 } else {
Amith Yamasani13593602012-03-22 16:16:17 -0700852 removeUserIdLPw(p.appId);
853 return p.appId;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700854 }
855 }
856 return -1;
857 }
858
Kenny Root447106f2011-03-23 11:00:15 -0700859 private void replacePackageLPw(String name, PackageSetting newp) {
860 final PackageSetting p = mPackages.get(name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700861 if (p != null) {
862 if (p.sharedUser != null) {
Ben Gruverdd72c9e2013-08-06 12:34:17 -0700863 p.sharedUser.removePackage(p);
864 p.sharedUser.addPackage(newp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700865 } else {
Amith Yamasani13593602012-03-22 16:16:17 -0700866 replaceUserIdLPw(p.appId, newp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700867 }
868 }
869 mPackages.put(name, newp);
870 }
871
Kenny Root447106f2011-03-23 11:00:15 -0700872 private boolean addUserIdLPw(int uid, Object obj, Object name) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800873 if (uid > Process.LAST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700874 return false;
875 }
876
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800877 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700878 int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800879 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700880 while (index >= N) {
881 mUserIds.add(null);
882 N++;
883 }
884 if (mUserIds.get(index) != null) {
885 PackageManagerService.reportSettingsProblem(Log.ERROR,
886 "Adding duplicate user id: " + uid
887 + " name=" + name);
888 return false;
889 }
890 mUserIds.set(index, obj);
891 } else {
892 if (mOtherUserIds.get(uid) != null) {
893 PackageManagerService.reportSettingsProblem(Log.ERROR,
894 "Adding duplicate shared id: " + uid
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800895 + " name=" + name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700896 return false;
897 }
898 mOtherUserIds.put(uid, obj);
899 }
900 return true;
901 }
902
Kenny Root447106f2011-03-23 11:00:15 -0700903 public Object getUserIdLPr(int uid) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800904 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700905 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800906 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700907 return index < N ? mUserIds.get(index) : null;
908 } else {
909 return mOtherUserIds.get(uid);
910 }
911 }
912
Kenny Root447106f2011-03-23 11:00:15 -0700913 private void removeUserIdLPw(int uid) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800914 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700915 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800916 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700917 if (index < N) mUserIds.set(index, null);
918 } else {
919 mOtherUserIds.remove(uid);
920 }
Geremy Condra12c18382013-03-06 16:49:06 -0800921 setFirstAvailableUid(uid+1);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700922 }
923
Kenny Root447106f2011-03-23 11:00:15 -0700924 private void replaceUserIdLPw(int uid, Object obj) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800925 if (uid >= Process.FIRST_APPLICATION_UID) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700926 final int N = mUserIds.size();
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800927 final int index = uid - Process.FIRST_APPLICATION_UID;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700928 if (index < N) mUserIds.set(index, obj);
929 } else {
930 mOtherUserIds.put(uid, obj);
931 }
932 }
933
Dianne Hackborn63092712012-10-07 14:45:35 -0700934 PreferredIntentResolver editPreferredActivitiesLPw(int userId) {
935 PreferredIntentResolver pir = mPreferredActivities.get(userId);
936 if (pir == null) {
937 pir = new PreferredIntentResolver();
938 mPreferredActivities.put(userId, pir);
939 }
940 return pir;
941 }
942
Sander Alewijnsef475ca32014-02-17 15:13:58 +0000943 PersistentPreferredIntentResolver editPersistentPreferredActivitiesLPw(int userId) {
944 PersistentPreferredIntentResolver ppir = mPersistentPreferredActivities.get(userId);
945 if (ppir == null) {
946 ppir = new PersistentPreferredIntentResolver();
947 mPersistentPreferredActivities.put(userId, ppir);
948 }
949 return ppir;
950 }
951
Nicolas Prevot81948992014-05-16 18:25:26 +0100952 CrossProfileIntentResolver editCrossProfileIntentResolverLPw(int userId) {
953 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(userId);
954 if (cpir == null) {
955 cpir = new CrossProfileIntentResolver();
956 mCrossProfileIntentResolvers.put(userId, cpir);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000957 }
Nicolas Prevot81948992014-05-16 18:25:26 +0100958 return cpir;
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000959 }
960
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800961 /**
962 * The following functions suppose that you have a lock for managing access to the
963 * mIntentFiltersVerifications map.
964 */
965
966 /* package protected */
967 IntentFilterVerificationInfo getIntentFilterVerificationLPr(String packageName) {
968 PackageSetting ps = mPackages.get(packageName);
969 if (ps == null) {
970 Slog.w(PackageManagerService.TAG, "No package known for name: " + packageName);
971 return null;
972 }
973 return ps.getIntentFilterVerificationInfo();
974 }
975
976 /* package protected */
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700977 IntentFilterVerificationInfo createIntentFilterVerificationIfNeededLPw(String packageName,
Fabrice Di Meglio07885952015-04-06 19:41:28 -0700978 ArrayList<String> domains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800979 PackageSetting ps = mPackages.get(packageName);
980 if (ps == null) {
981 Slog.w(PackageManagerService.TAG, "No package known for name: " + packageName);
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700982 return null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800983 }
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700984 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
985 if (ivi == null) {
986 ivi = new IntentFilterVerificationInfo(packageName, domains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800987 ps.setIntentFilterVerificationInfo(ivi);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800988 }
Fabrice Di Meglio7d014ce2015-04-08 16:17:46 -0700989 return ivi;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800990 }
991
992 int getIntentFilterVerificationStatusLPr(String packageName, int userId) {
993 PackageSetting ps = mPackages.get(packageName);
994 if (ps == null) {
995 Slog.w(PackageManagerService.TAG, "No package known for name: " + packageName);
996 return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
997 }
998 int status = ps.getDomainVerificationStatusForUser(userId);
999 if (status == INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED) {
1000 if (ps.getIntentFilterVerificationInfo() != null) {
1001 status = ps.getIntentFilterVerificationInfo().getStatus();
1002 }
1003 }
1004 return status;
1005 }
1006
1007 boolean updateIntentFilterVerificationStatusLPw(String packageName, int status, int userId) {
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001008 // Update the status for the current package
1009 PackageSetting current = mPackages.get(packageName);
1010 if (current == null) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001011 Slog.w(PackageManagerService.TAG, "No package known for name: " + packageName);
1012 return false;
1013 }
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001014 current.setDomainVerificationStatusForUser(status, userId);
1015
1016 if (current.getIntentFilterVerificationInfo() == null) {
1017 Slog.w(PackageManagerService.TAG,
1018 "No IntentFilterVerificationInfo known for name: " + packageName);
1019 return false;
1020 }
1021
1022 // Then, if we set a ALWAYS status, then put NEVER status for Apps whose IntentFilter
1023 // domains overlap the domains of the current package
1024 ArraySet<String> currentDomains = current.getIntentFilterVerificationInfo().getDomainsSet();
1025 if (status == INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS) {
1026 for (PackageSetting ps : mPackages.values()) {
1027 if (ps == null || ps.pkg.packageName.equals(packageName)) continue;
1028 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
1029 if (ivi == null) {
1030 continue;
1031 }
1032 ArraySet<String> set = ivi.getDomainsSet();
1033 set.retainAll(currentDomains);
1034 if (set.size() > 0) {
1035 ps.setDomainVerificationStatusForUser(
1036 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER, userId);
1037 }
1038 }
1039 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001040 return true;
1041 }
1042
1043 /**
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001044 * Used for Settings App and PackageManagerService dump. Should be read only.
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001045 */
1046 List<IntentFilterVerificationInfo> getIntentFilterVerificationsLPr(
1047 String packageName) {
1048 if (packageName == null) {
1049 return Collections.<IntentFilterVerificationInfo>emptyList();
1050 }
1051 ArrayList<IntentFilterVerificationInfo> result = new ArrayList<>();
1052 for (PackageSetting ps : mPackages.values()) {
1053 IntentFilterVerificationInfo ivi = ps.getIntentFilterVerificationInfo();
1054 if (ivi == null || TextUtils.isEmpty(ivi.getPackageName()) ||
1055 !ivi.getPackageName().equalsIgnoreCase(packageName)) {
1056 continue;
1057 }
1058 result.add(ivi);
1059 }
1060 return result;
1061 }
1062
1063 void removeIntentFilterVerificationLPw(String packageName, int userId) {
1064 PackageSetting ps = mPackages.get(packageName);
1065 if (ps == null) {
1066 Slog.w(PackageManagerService.TAG, "No package known for name: " + packageName);
1067 return;
1068 }
1069 ps.clearDomainVerificationStatusForUser(userId);
1070 }
1071
1072 void removeIntentFilterVerificationLPw(String packageName, int[] userIds) {
1073 for (int userId : userIds) {
1074 removeIntentFilterVerificationLPw(packageName, userId);
1075 }
1076 }
1077
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001078 boolean setDefaultBrowserPackageNameLPr(String packageName, int userId) {
1079 if (userId == UserHandle.USER_ALL) {
1080 return false;
1081 }
1082 mDefaultBrowserApp.put(userId, packageName);
1083 writePackageRestrictionsLPr(userId);
1084 return true;
1085 }
1086
1087 String getDefaultBrowserPackageNameLPw(int userId) {
1088 return (userId == UserHandle.USER_ALL) ? null : mDefaultBrowserApp.get(userId);
1089 }
1090
Amith Yamasani483f3b02012-03-13 16:08:00 -07001091 private File getUserPackagesStateFile(int userId) {
Svetoslavc6d1c342015-02-26 14:44:43 -08001092 // TODO: Implement a cleaner solution when adding tests.
1093 // This instead of Environment.getUserSystemDirectory(userId) to support testing.
1094 File userDir = new File(new File(mSystemDir, "users"), Integer.toString(userId));
1095 return new File(userDir, "package-restrictions.xml");
1096 }
1097
1098 private File getUserRuntimePermissionsFile(int userId) {
1099 // TODO: Implement a cleaner solution when adding tests.
1100 // This instead of Environment.getUserSystemDirectory(userId) to support testing.
1101 File userDir = new File(new File(mSystemDir, "users"), Integer.toString(userId));
1102 return new File(userDir, RUNTIME_PERMISSIONS_FILE_NAME);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001103 }
1104
Svet Ganov12a692a2015-03-28 19:34:15 -07001105 boolean isFirstRuntimePermissionsBoot() {
1106 return !getUserRuntimePermissionsFile(UserHandle.USER_OWNER).exists();
1107 }
1108
1109 void deleteRuntimePermissionsFiles() {
1110 for (int userId : UserManagerService.getInstance().getUserIds()) {
1111 File file = getUserRuntimePermissionsFile(userId);
1112 file.delete();
1113 }
1114 }
1115
Amith Yamasani483f3b02012-03-13 16:08:00 -07001116 private File getUserPackagesStateBackupFile(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -07001117 return new File(Environment.getUserSystemDirectory(userId),
1118 "package-restrictions-backup.xml");
Amith Yamasani483f3b02012-03-13 16:08:00 -07001119 }
1120
1121 void writeAllUsersPackageRestrictionsLPr() {
1122 List<UserInfo> users = getAllUsers();
1123 if (users == null) return;
1124
1125 for (UserInfo user : users) {
1126 writePackageRestrictionsLPr(user.id);
1127 }
1128 }
1129
Svetoslavc6d1c342015-02-26 14:44:43 -08001130 void writeAllRuntimePermissionsLPr() {
1131 for (int userId : UserManagerService.getInstance().getUserIds()) {
1132 mRuntimePermissionsPersistence.writePermissionsForUserAsyncLPr(userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001133 }
1134 }
1135
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001136 /**
1137 * Returns whether the current database has is older than {@code version}
1138 * for apps on internal storage.
1139 */
1140 public boolean isInternalDatabaseVersionOlderThan(int version) {
1141 return mInternalDatabaseVersion < version;
1142 }
1143
1144 /**
1145 * Returns whether the current database has is older than {@code version}
1146 * for apps on external storage.
1147 */
1148 public boolean isExternalDatabaseVersionOlderThan(int version) {
1149 return mExternalDatabaseVersion < version;
1150 }
1151
1152 /**
1153 * Updates the database version for apps on internal storage. Called after
1154 * call the updates to the database format are done for apps on internal
1155 * storage after the initial start-up scan.
1156 */
1157 public void updateInternalDatabaseVersion() {
1158 mInternalDatabaseVersion = CURRENT_DATABASE_VERSION;
1159 }
1160
1161 /**
1162 * Updates the database version for apps on internal storage. Called after
1163 * call the updates to the database format are done for apps on internal
1164 * storage after the initial start-up scan.
1165 */
1166 public void updateExternalDatabaseVersion() {
1167 mExternalDatabaseVersion = CURRENT_DATABASE_VERSION;
1168 }
1169
Christopher Tatee012a232015-04-01 17:18:50 -07001170 /**
1171 * Applies the preferred activity state described by the given XML. This code
1172 * also supports the restore-from-backup code path.
1173 *
1174 * @see PreferredActivityBackupHelper
1175 */
1176 void readPreferredActivitiesLPw(XmlPullParser parser, int userId)
Dianne Hackborn63092712012-10-07 14:45:35 -07001177 throws XmlPullParserException, IOException {
1178 int outerDepth = parser.getDepth();
1179 int type;
1180 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1181 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1182 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1183 continue;
1184 }
1185
1186 String tagName = parser.getName();
1187 if (tagName.equals(TAG_ITEM)) {
1188 PreferredActivity pa = new PreferredActivity(parser);
1189 if (pa.mPref.getParseError() == null) {
1190 editPreferredActivitiesLPw(userId).addFilter(pa);
1191 } else {
1192 PackageManagerService.reportSettingsProblem(Log.WARN,
1193 "Error in package manager settings: <preferred-activity> "
1194 + pa.mPref.getParseError() + " at "
1195 + parser.getPositionDescription());
1196 }
1197 } else {
1198 PackageManagerService.reportSettingsProblem(Log.WARN,
1199 "Unknown element under <preferred-activities>: " + parser.getName());
1200 XmlUtils.skipCurrentTag(parser);
1201 }
1202 }
1203 }
1204
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001205 private void readPersistentPreferredActivitiesLPw(XmlPullParser parser, int userId)
1206 throws XmlPullParserException, IOException {
1207 int outerDepth = parser.getDepth();
1208 int type;
1209 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1210 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1211 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1212 continue;
1213 }
1214 String tagName = parser.getName();
1215 if (tagName.equals(TAG_ITEM)) {
1216 PersistentPreferredActivity ppa = new PersistentPreferredActivity(parser);
1217 editPersistentPreferredActivitiesLPw(userId).addFilter(ppa);
1218 } else {
1219 PackageManagerService.reportSettingsProblem(Log.WARN,
Sander Alewijnseaf597622014-03-20 18:44:57 +00001220 "Unknown element under <" + TAG_PERSISTENT_PREFERRED_ACTIVITIES + ">: "
1221 + parser.getName());
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001222 XmlUtils.skipCurrentTag(parser);
1223 }
1224 }
1225 }
1226
Nicolas Prevot81948992014-05-16 18:25:26 +01001227 private void readCrossProfileIntentFiltersLPw(XmlPullParser parser, int userId)
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001228 throws XmlPullParserException, IOException {
1229 int outerDepth = parser.getDepth();
1230 int type;
1231 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1232 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1233 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1234 continue;
1235 }
1236 String tagName = parser.getName();
1237 if (tagName.equals(TAG_ITEM)) {
Nicolas Prevot81948992014-05-16 18:25:26 +01001238 CrossProfileIntentFilter cpif = new CrossProfileIntentFilter(parser);
1239 editCrossProfileIntentResolverLPw(userId).addFilter(cpif);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001240 } else {
Nicolas Prevot81948992014-05-16 18:25:26 +01001241 String msg = "Unknown element under " + TAG_CROSS_PROFILE_INTENT_FILTERS + ": " +
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001242 parser.getName();
1243 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
1244 XmlUtils.skipCurrentTag(parser);
1245 }
1246 }
1247 }
1248
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001249 private void readDomainVerificationLPw(XmlPullParser parser, PackageSettingBase packageSetting)
1250 throws XmlPullParserException, IOException {
1251 IntentFilterVerificationInfo ivi = new IntentFilterVerificationInfo(parser);
1252 packageSetting.setIntentFilterVerificationInfo(ivi);
1253 Log.d(TAG, "Read domain verification for package:" + ivi.getPackageName());
1254 }
1255
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001256 private void readDefaultAppsLPw(XmlPullParser parser, int userId)
1257 throws XmlPullParserException, IOException {
1258 int outerDepth = parser.getDepth();
1259 int type;
1260 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1261 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1262 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1263 continue;
1264 }
1265 String tagName = parser.getName();
1266 if (tagName.equals(TAG_DEFAULT_BROWSER)) {
1267 String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
1268 mDefaultBrowserApp.put(userId, packageName);
1269 } else {
1270 String msg = "Unknown element under " + TAG_DEFAULT_APPS + ": " +
1271 parser.getName();
1272 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
1273 XmlUtils.skipCurrentTag(parser);
1274 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001275 }
1276 }
1277
Amith Yamasani483f3b02012-03-13 16:08:00 -07001278 void readPackageRestrictionsLPr(int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001279 if (DEBUG_MU) {
1280 Log.i(TAG, "Reading package restrictions for user=" + userId);
1281 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07001282 FileInputStream str = null;
1283 File userPackagesStateFile = getUserPackagesStateFile(userId);
1284 File backupFile = getUserPackagesStateBackupFile(userId);
1285 if (backupFile.exists()) {
1286 try {
1287 str = new FileInputStream(backupFile);
1288 mReadMessages.append("Reading from backup stopped packages file\n");
1289 PackageManagerService.reportSettingsProblem(Log.INFO,
1290 "Need to read from backup stopped packages file");
1291 if (userPackagesStateFile.exists()) {
1292 // If both the backup and normal file exist, we
1293 // ignore the normal one since it might have been
1294 // corrupted.
1295 Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file "
1296 + userPackagesStateFile);
1297 userPackagesStateFile.delete();
1298 }
1299 } catch (java.io.IOException e) {
1300 // We'll try for the normal settings file.
1301 }
1302 }
1303
1304 try {
1305 if (str == null) {
1306 if (!userPackagesStateFile.exists()) {
1307 mReadMessages.append("No stopped packages file found\n");
1308 PackageManagerService.reportSettingsProblem(Log.INFO,
1309 "No stopped packages file; "
1310 + "assuming all started");
1311 // At first boot, make sure no packages are stopped.
1312 // We usually want to have third party apps initialize
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001313 // in the stopped state, but not at first boot. Also
1314 // consider all applications to be installed.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001315 for (PackageSetting pkg : mPackages.values()) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001316 pkg.setUserState(userId, COMPONENT_ENABLED_STATE_DEFAULT,
1317 true, // installed
1318 false, // stopped
1319 false, // notLaunched
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001320 false, // hidden
Kenny Guyc13053b2014-05-29 14:17:17 +01001321 null, null, null,
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001322 false, // blockUninstall
1323 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001324 }
1325 return;
1326 }
1327 str = new FileInputStream(userPackagesStateFile);
1328 }
1329 final XmlPullParser parser = Xml.newPullParser();
1330 parser.setInput(str, null);
1331
1332 int type;
1333 while ((type=parser.next()) != XmlPullParser.START_TAG
1334 && type != XmlPullParser.END_DOCUMENT) {
1335 ;
1336 }
1337
1338 if (type != XmlPullParser.START_TAG) {
1339 mReadMessages.append("No start tag found in package restrictions file\n");
1340 PackageManagerService.reportSettingsProblem(Log.WARN,
1341 "No start tag found in package manager stopped packages");
1342 return;
1343 }
1344
1345 int outerDepth = parser.getDepth();
1346 PackageSetting ps = null;
1347 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1348 && (type != XmlPullParser.END_TAG
1349 || parser.getDepth() > outerDepth)) {
1350 if (type == XmlPullParser.END_TAG
1351 || type == XmlPullParser.TEXT) {
1352 continue;
1353 }
1354
1355 String tagName = parser.getName();
1356 if (tagName.equals(TAG_PACKAGE)) {
1357 String name = parser.getAttributeValue(null, ATTR_NAME);
1358 ps = mPackages.get(name);
1359 if (ps == null) {
1360 Slog.w(PackageManagerService.TAG, "No package known for stopped package: "
1361 + name);
1362 XmlUtils.skipCurrentTag(parser);
1363 continue;
1364 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001365 final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED);
1366 final int enabled = enabledStr == null
1367 ? COMPONENT_ENABLED_STATE_DEFAULT : Integer.parseInt(enabledStr);
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001368 final String enabledCaller = parser.getAttributeValue(null,
1369 ATTR_ENABLED_CALLER);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001370 final String installedStr = parser.getAttributeValue(null, ATTR_INSTALLED);
1371 final boolean installed = installedStr == null
1372 ? true : Boolean.parseBoolean(installedStr);
1373 final String stoppedStr = parser.getAttributeValue(null, ATTR_STOPPED);
1374 final boolean stopped = stoppedStr == null
1375 ? false : Boolean.parseBoolean(stoppedStr);
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001376 // For backwards compatibility with the previous name of "blocked", which
1377 // now means hidden, read the old attribute as well.
Amith Yamasani655d0e22013-06-12 14:19:10 -07001378 final String blockedStr = parser.getAttributeValue(null, ATTR_BLOCKED);
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001379 boolean hidden = blockedStr == null
Amith Yamasani655d0e22013-06-12 14:19:10 -07001380 ? false : Boolean.parseBoolean(blockedStr);
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001381 final String hiddenStr = parser.getAttributeValue(null, ATTR_HIDDEN);
1382 hidden = hiddenStr == null
1383 ? hidden : Boolean.parseBoolean(hiddenStr);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001384 final String notLaunchedStr = parser.getAttributeValue(null, ATTR_NOT_LAUNCHED);
1385 final boolean notLaunched = stoppedStr == null
1386 ? false : Boolean.parseBoolean(notLaunchedStr);
Kenny Guyc13053b2014-05-29 14:17:17 +01001387 final String blockUninstallStr = parser.getAttributeValue(null,
1388 ATTR_BLOCK_UNINSTALL);
1389 final boolean blockUninstall = blockUninstallStr == null
1390 ? false : Boolean.parseBoolean(blockUninstallStr);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001391
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001392 final String verifStateStr =
1393 parser.getAttributeValue(null, ATTR_DOMAIN_VERIFICATON_STATE);
1394 final int verifState = (verifStateStr == null) ?
1395 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED :
1396 Integer.parseInt(verifStateStr);
1397
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001398 ArraySet<String> enabledComponents = null;
1399 ArraySet<String> disabledComponents = null;
Amith Yamasani483f3b02012-03-13 16:08:00 -07001400
1401 int packageDepth = parser.getDepth();
1402 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1403 && (type != XmlPullParser.END_TAG
1404 || parser.getDepth() > packageDepth)) {
1405 if (type == XmlPullParser.END_TAG
1406 || type == XmlPullParser.TEXT) {
1407 continue;
1408 }
1409 tagName = parser.getName();
1410 if (tagName.equals(TAG_ENABLED_COMPONENTS)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001411 enabledComponents = readComponentsLPr(parser);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001412 } else if (tagName.equals(TAG_DISABLED_COMPONENTS)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001413 disabledComponents = readComponentsLPr(parser);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001414 }
1415 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001416
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001417 ps.setUserState(userId, enabled, installed, stopped, notLaunched, hidden,
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001418 enabledCaller, enabledComponents, disabledComponents, blockUninstall,
1419 verifState);
Dianne Hackborn63092712012-10-07 14:45:35 -07001420 } else if (tagName.equals("preferred-activities")) {
1421 readPreferredActivitiesLPw(parser, userId);
Sander Alewijnseaf597622014-03-20 18:44:57 +00001422 } else if (tagName.equals(TAG_PERSISTENT_PREFERRED_ACTIVITIES)) {
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001423 readPersistentPreferredActivitiesLPw(parser, userId);
Nicolas Prevot29762c32014-07-29 18:51:32 +01001424 } else if (tagName.equals(TAG_CROSS_PROFILE_INTENT_FILTERS)) {
Nicolas Prevot81948992014-05-16 18:25:26 +01001425 readCrossProfileIntentFiltersLPw(parser, userId);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001426 } else if (tagName.equals(TAG_DEFAULT_APPS)) {
1427 readDefaultAppsLPw(parser, userId);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001428 } else {
1429 Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: "
1430 + parser.getName());
1431 XmlUtils.skipCurrentTag(parser);
1432 }
1433 }
1434
1435 str.close();
1436
1437 } catch (XmlPullParserException e) {
1438 mReadMessages.append("Error reading: " + e.toString());
1439 PackageManagerService.reportSettingsProblem(Log.ERROR,
1440 "Error reading stopped packages: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07001441 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
1442 e);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001443
1444 } catch (java.io.IOException e) {
1445 mReadMessages.append("Error reading: " + e.toString());
1446 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07001447 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
1448 e);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001449 }
1450 }
1451
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001452 private ArraySet<String> readComponentsLPr(XmlPullParser parser)
Amith Yamasani483f3b02012-03-13 16:08:00 -07001453 throws IOException, XmlPullParserException {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001454 ArraySet<String> components = null;
Amith Yamasani483f3b02012-03-13 16:08:00 -07001455 int type;
1456 int outerDepth = parser.getDepth();
1457 String tagName;
1458 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1459 && (type != XmlPullParser.END_TAG
1460 || parser.getDepth() > outerDepth)) {
1461 if (type == XmlPullParser.END_TAG
1462 || type == XmlPullParser.TEXT) {
1463 continue;
1464 }
1465 tagName = parser.getName();
1466 if (tagName.equals(TAG_ITEM)) {
1467 String componentName = parser.getAttributeValue(null, ATTR_NAME);
1468 if (componentName != null) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001469 if (components == null) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07001470 components = new ArraySet<String>();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001471 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07001472 components.add(componentName);
1473 }
1474 }
1475 }
1476 return components;
1477 }
1478
Christopher Tatee012a232015-04-01 17:18:50 -07001479 /**
1480 * Record the state of preferred activity configuration into XML. This is used both
1481 * for recording packages.xml internally and for supporting backup/restore of the
1482 * preferred activity configuration.
1483 */
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08001484 void writePreferredActivitiesLPr(XmlSerializer serializer, int userId, boolean full)
Dianne Hackborn63092712012-10-07 14:45:35 -07001485 throws IllegalArgumentException, IllegalStateException, IOException {
1486 serializer.startTag(null, "preferred-activities");
1487 PreferredIntentResolver pir = mPreferredActivities.get(userId);
1488 if (pir != null) {
1489 for (final PreferredActivity pa : pir.filterSet()) {
1490 serializer.startTag(null, TAG_ITEM);
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08001491 pa.writeToXml(serializer, full);
Dianne Hackborn63092712012-10-07 14:45:35 -07001492 serializer.endTag(null, TAG_ITEM);
1493 }
1494 }
1495 serializer.endTag(null, "preferred-activities");
1496 }
1497
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001498 void writePersistentPreferredActivitiesLPr(XmlSerializer serializer, int userId)
1499 throws IllegalArgumentException, IllegalStateException, IOException {
Sander Alewijnseaf597622014-03-20 18:44:57 +00001500 serializer.startTag(null, TAG_PERSISTENT_PREFERRED_ACTIVITIES);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001501 PersistentPreferredIntentResolver ppir = mPersistentPreferredActivities.get(userId);
1502 if (ppir != null) {
1503 for (final PersistentPreferredActivity ppa : ppir.filterSet()) {
1504 serializer.startTag(null, TAG_ITEM);
1505 ppa.writeToXml(serializer);
1506 serializer.endTag(null, TAG_ITEM);
1507 }
1508 }
Sander Alewijnseaf597622014-03-20 18:44:57 +00001509 serializer.endTag(null, TAG_PERSISTENT_PREFERRED_ACTIVITIES);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001510 }
1511
Nicolas Prevot81948992014-05-16 18:25:26 +01001512 void writeCrossProfileIntentFiltersLPr(XmlSerializer serializer, int userId)
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001513 throws IllegalArgumentException, IllegalStateException, IOException {
Nicolas Prevot81948992014-05-16 18:25:26 +01001514 serializer.startTag(null, TAG_CROSS_PROFILE_INTENT_FILTERS);
1515 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(userId);
1516 if (cpir != null) {
1517 for (final CrossProfileIntentFilter cpif : cpir.filterSet()) {
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001518 serializer.startTag(null, TAG_ITEM);
Nicolas Prevot81948992014-05-16 18:25:26 +01001519 cpif.writeToXml(serializer);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001520 serializer.endTag(null, TAG_ITEM);
1521 }
1522 }
Nicolas Prevot81948992014-05-16 18:25:26 +01001523 serializer.endTag(null, TAG_CROSS_PROFILE_INTENT_FILTERS);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001524 }
1525
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001526 void writeDomainVerificationsLPr(XmlSerializer serializer,
1527 IntentFilterVerificationInfo verificationInfo)
1528 throws IllegalArgumentException, IllegalStateException, IOException {
1529 if (verificationInfo != null && verificationInfo.getPackageName() != null) {
1530 serializer.startTag(null, TAG_DOMAIN_VERIFICATION);
1531 verificationInfo.writeToXml(serializer);
1532 Log.d(TAG, "Wrote domain verification for package: "
1533 + verificationInfo.getPackageName());
1534 serializer.endTag(null, TAG_DOMAIN_VERIFICATION);
1535 }
1536 }
1537
1538 void writeDefaultAppsLPr(XmlSerializer serializer, int userId)
1539 throws IllegalArgumentException, IllegalStateException, IOException {
1540 serializer.startTag(null, TAG_DEFAULT_APPS);
1541 String packageName = mDefaultBrowserApp.get(userId);
1542 if (!TextUtils.isEmpty(packageName)) {
1543 serializer.startTag(null, TAG_DEFAULT_BROWSER);
1544 serializer.attribute(null, ATTR_PACKAGE_NAME, packageName);
1545 serializer.endTag(null, TAG_DEFAULT_BROWSER);
1546 }
1547 serializer.endTag(null, TAG_DEFAULT_APPS);
1548 }
1549
Amith Yamasani483f3b02012-03-13 16:08:00 -07001550 void writePackageRestrictionsLPr(int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001551 if (DEBUG_MU) {
1552 Log.i(TAG, "Writing package restrictions for user=" + userId);
1553 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001554 // Keep the old stopped packages around until we know the new ones have
1555 // been successfully written.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001556 File userPackagesStateFile = getUserPackagesStateFile(userId);
1557 File backupFile = getUserPackagesStateBackupFile(userId);
1558 new File(userPackagesStateFile.getParent()).mkdirs();
1559 if (userPackagesStateFile.exists()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001560 // Presence of backup settings file indicates that we failed
1561 // to persist packages earlier. So preserve the older
1562 // backup for future reference since the current packages
1563 // might have been corrupted.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001564 if (!backupFile.exists()) {
1565 if (!userPackagesStateFile.renameTo(backupFile)) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001566 Slog.wtf(PackageManagerService.TAG,
1567 "Unable to backup user packages state file, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001568 + "current changes will be lost at reboot");
1569 return;
1570 }
1571 } else {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001572 userPackagesStateFile.delete();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001573 Slog.w(PackageManagerService.TAG, "Preserving older stopped packages backup");
1574 }
1575 }
1576
1577 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001578 final FileOutputStream fstr = new FileOutputStream(userPackagesStateFile);
Kenny Root447106f2011-03-23 11:00:15 -07001579 final BufferedOutputStream str = new BufferedOutputStream(fstr);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001580
Kenny Root447106f2011-03-23 11:00:15 -07001581 final XmlSerializer serializer = new FastXmlSerializer();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001582 serializer.setOutput(str, "utf-8");
1583 serializer.startDocument(null, true);
1584 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1585
Amith Yamasani483f3b02012-03-13 16:08:00 -07001586 serializer.startTag(null, TAG_PACKAGE_RESTRICTIONS);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001587
Kenny Root447106f2011-03-23 11:00:15 -07001588 for (final PackageSetting pkg : mPackages.values()) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001589 PackageUserState ustate = pkg.readUserState(userId);
1590 if (ustate.stopped || ustate.notLaunched || !ustate.installed
1591 || ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001592 || ustate.hidden
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001593 || (ustate.enabledComponents != null
1594 && ustate.enabledComponents.size() > 0)
1595 || (ustate.disabledComponents != null
Kenny Guyc13053b2014-05-29 14:17:17 +01001596 && ustate.disabledComponents.size() > 0)
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001597 || ustate.blockUninstall
1598 || (ustate.domainVerificationStatus !=
1599 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED)) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001600 serializer.startTag(null, TAG_PACKAGE);
1601 serializer.attribute(null, ATTR_NAME, pkg.name);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001602 if (DEBUG_MU) Log.i(TAG, " pkg=" + pkg.name + ", state=" + ustate.enabled);
Amith Yamasani483f3b02012-03-13 16:08:00 -07001603
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001604 if (!ustate.installed) {
1605 serializer.attribute(null, ATTR_INSTALLED, "false");
1606 }
1607 if (ustate.stopped) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001608 serializer.attribute(null, ATTR_STOPPED, "true");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001609 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001610 if (ustate.notLaunched) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001611 serializer.attribute(null, ATTR_NOT_LAUNCHED, "true");
1612 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001613 if (ustate.hidden) {
1614 serializer.attribute(null, ATTR_HIDDEN, "true");
Amith Yamasani655d0e22013-06-12 14:19:10 -07001615 }
Kenny Guyc13053b2014-05-29 14:17:17 +01001616 if (ustate.blockUninstall) {
1617 serializer.attribute(null, ATTR_BLOCK_UNINSTALL, "true");
1618 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001619 if (ustate.enabled != COMPONENT_ENABLED_STATE_DEFAULT) {
1620 serializer.attribute(null, ATTR_ENABLED,
1621 Integer.toString(ustate.enabled));
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001622 if (ustate.lastDisableAppCaller != null) {
1623 serializer.attribute(null, ATTR_ENABLED_CALLER,
1624 ustate.lastDisableAppCaller);
1625 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07001626 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001627 if (ustate.domainVerificationStatus !=
1628 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED) {
1629 serializer.attribute(null, ATTR_DOMAIN_VERIFICATON_STATE,
1630 Integer.toString(ustate.domainVerificationStatus));
1631 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001632 if (ustate.enabledComponents != null
1633 && ustate.enabledComponents.size() > 0) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001634 serializer.startTag(null, TAG_ENABLED_COMPONENTS);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001635 for (final String name : ustate.enabledComponents) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001636 serializer.startTag(null, TAG_ITEM);
1637 serializer.attribute(null, ATTR_NAME, name);
1638 serializer.endTag(null, TAG_ITEM);
1639 }
1640 serializer.endTag(null, TAG_ENABLED_COMPONENTS);
1641 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001642 if (ustate.disabledComponents != null
1643 && ustate.disabledComponents.size() > 0) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001644 serializer.startTag(null, TAG_DISABLED_COMPONENTS);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001645 for (final String name : ustate.disabledComponents) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001646 serializer.startTag(null, TAG_ITEM);
1647 serializer.attribute(null, ATTR_NAME, name);
1648 serializer.endTag(null, TAG_ITEM);
1649 }
1650 serializer.endTag(null, TAG_DISABLED_COMPONENTS);
1651 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001652
Amith Yamasani483f3b02012-03-13 16:08:00 -07001653 serializer.endTag(null, TAG_PACKAGE);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001654 }
1655 }
1656
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08001657 writePreferredActivitiesLPr(serializer, userId, true);
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001658 writePersistentPreferredActivitiesLPr(serializer, userId);
Nicolas Prevot81948992014-05-16 18:25:26 +01001659 writeCrossProfileIntentFiltersLPr(serializer, userId);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001660 writeDefaultAppsLPr(serializer, userId);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001661
Amith Yamasani483f3b02012-03-13 16:08:00 -07001662 serializer.endTag(null, TAG_PACKAGE_RESTRICTIONS);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001663
1664 serializer.endDocument();
1665
1666 str.flush();
1667 FileUtils.sync(fstr);
1668 str.close();
1669
1670 // New settings successfully written, old ones are no longer
1671 // needed.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001672 backupFile.delete();
1673 FileUtils.setPermissions(userPackagesStateFile.toString(),
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001674 FileUtils.S_IRUSR|FileUtils.S_IWUSR
Nick Kralevich70522ad2012-01-06 13:58:01 -08001675 |FileUtils.S_IRGRP|FileUtils.S_IWGRP,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001676 -1, -1);
1677
1678 // Done, all is good!
1679 return;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001680 } catch(java.io.IOException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001681 Slog.wtf(PackageManagerService.TAG,
Amith Yamasani483f3b02012-03-13 16:08:00 -07001682 "Unable to write package manager user packages state, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001683 + " current changes will be lost at reboot", e);
1684 }
1685
1686 // Clean up partially written files
Amith Yamasani483f3b02012-03-13 16:08:00 -07001687 if (userPackagesStateFile.exists()) {
1688 if (!userPackagesStateFile.delete()) {
1689 Log.i(PackageManagerService.TAG, "Failed to clean up mangled file: "
1690 + mStoppedPackagesFilename);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001691 }
1692 }
1693 }
1694
Svetoslavc6d1c342015-02-26 14:44:43 -08001695 void readInstallPermissionsLPr(XmlPullParser parser,
1696 PermissionsState permissionsState) throws IOException, XmlPullParserException {
1697 int outerDepth = parser.getDepth();
1698 int type;
1699 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1700 && (type != XmlPullParser.END_TAG
1701 || parser.getDepth() > outerDepth)) {
1702 if (type == XmlPullParser.END_TAG
1703 || type == XmlPullParser.TEXT) {
1704 continue;
1705 }
1706 String tagName = parser.getName();
1707 if (tagName.equals(TAG_ITEM)) {
1708 String name = parser.getAttributeValue(null, ATTR_NAME);
1709
1710 BasePermission bp = mPermissions.get(name);
1711 if (bp == null) {
1712 Slog.w(PackageManagerService.TAG, "Unknown permission: " + name);
1713 XmlUtils.skipCurrentTag(parser);
1714 continue;
1715 }
1716
1717 if (permissionsState.grantInstallPermission(bp) ==
1718 PermissionsState.PERMISSION_OPERATION_FAILURE) {
1719 Slog.w(PackageManagerService.TAG, "Permission already added: " + name);
1720 XmlUtils.skipCurrentTag(parser);
1721 }
1722 } else {
1723 Slog.w(PackageManagerService.TAG, "Unknown element under <permissions>: "
1724 + parser.getName());
1725 XmlUtils.skipCurrentTag(parser);
1726 }
1727 }
1728 }
1729
1730 void writePermissionsLPr(XmlSerializer serializer, Set<String> permissions)
1731 throws IOException {
1732 if (permissions.isEmpty()) {
1733 return;
1734 }
1735
1736 serializer.startTag(null, TAG_PERMISSIONS);
1737
1738 for (String permission : permissions) {
1739 serializer.startTag(null, TAG_ITEM);
1740 serializer.attribute(null, ATTR_NAME, permission);
1741 serializer.endTag(null, TAG_ITEM);
1742 }
1743
1744 serializer.endTag(null, TAG_PERMISSIONS);
1745 }
1746
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001747 // Note: assumed "stopped" field is already cleared in all packages.
Amith Yamasani483f3b02012-03-13 16:08:00 -07001748 // Legacy reader, used to read in the old file format after an upgrade. Not used after that.
Kenny Root447106f2011-03-23 11:00:15 -07001749 void readStoppedLPw() {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001750 FileInputStream str = null;
1751 if (mBackupStoppedPackagesFilename.exists()) {
1752 try {
1753 str = new FileInputStream(mBackupStoppedPackagesFilename);
1754 mReadMessages.append("Reading from backup stopped packages file\n");
Amith Yamasani483f3b02012-03-13 16:08:00 -07001755 PackageManagerService.reportSettingsProblem(Log.INFO,
1756 "Need to read from backup stopped packages file");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001757 if (mSettingsFilename.exists()) {
1758 // If both the backup and normal file exist, we
1759 // ignore the normal one since it might have been
1760 // corrupted.
1761 Slog.w(PackageManagerService.TAG, "Cleaning up stopped packages file "
1762 + mStoppedPackagesFilename);
1763 mStoppedPackagesFilename.delete();
1764 }
1765 } catch (java.io.IOException e) {
1766 // We'll try for the normal settings file.
1767 }
1768 }
1769
1770 try {
1771 if (str == null) {
1772 if (!mStoppedPackagesFilename.exists()) {
1773 mReadMessages.append("No stopped packages file found\n");
Amith Yamasani483f3b02012-03-13 16:08:00 -07001774 PackageManagerService.reportSettingsProblem(Log.INFO,
1775 "No stopped packages file file; assuming all started");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001776 // At first boot, make sure no packages are stopped.
1777 // We usually want to have third party apps initialize
1778 // in the stopped state, but not at first boot.
1779 for (PackageSetting pkg : mPackages.values()) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001780 pkg.setStopped(false, 0);
1781 pkg.setNotLaunched(false, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001782 }
1783 return;
1784 }
1785 str = new FileInputStream(mStoppedPackagesFilename);
1786 }
Kenny Root447106f2011-03-23 11:00:15 -07001787 final XmlPullParser parser = Xml.newPullParser();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001788 parser.setInput(str, null);
1789
1790 int type;
1791 while ((type=parser.next()) != XmlPullParser.START_TAG
1792 && type != XmlPullParser.END_DOCUMENT) {
1793 ;
1794 }
1795
1796 if (type != XmlPullParser.START_TAG) {
1797 mReadMessages.append("No start tag found in stopped packages file\n");
1798 PackageManagerService.reportSettingsProblem(Log.WARN,
1799 "No start tag found in package manager stopped packages");
1800 return;
1801 }
1802
1803 int outerDepth = parser.getDepth();
1804 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1805 && (type != XmlPullParser.END_TAG
1806 || parser.getDepth() > outerDepth)) {
1807 if (type == XmlPullParser.END_TAG
1808 || type == XmlPullParser.TEXT) {
1809 continue;
1810 }
1811
1812 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07001813 if (tagName.equals(TAG_PACKAGE)) {
1814 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001815 PackageSetting ps = mPackages.get(name);
1816 if (ps != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001817 ps.setStopped(true, 0);
1818 if ("1".equals(parser.getAttributeValue(null, ATTR_NOT_LAUNCHED))) {
1819 ps.setNotLaunched(true, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001820 }
1821 } else {
Amith Yamasani483f3b02012-03-13 16:08:00 -07001822 Slog.w(PackageManagerService.TAG,
1823 "No package known for stopped package: " + name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001824 }
1825 XmlUtils.skipCurrentTag(parser);
1826 } else {
1827 Slog.w(PackageManagerService.TAG, "Unknown element under <stopped-packages>: "
1828 + parser.getName());
1829 XmlUtils.skipCurrentTag(parser);
1830 }
1831 }
1832
1833 str.close();
1834
Amith Yamasani483f3b02012-03-13 16:08:00 -07001835 } catch (XmlPullParserException e) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001836 mReadMessages.append("Error reading: " + e.toString());
Amith Yamasani483f3b02012-03-13 16:08:00 -07001837 PackageManagerService.reportSettingsProblem(Log.ERROR,
1838 "Error reading stopped packages: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07001839 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
1840 e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001841
Amith Yamasani483f3b02012-03-13 16:08:00 -07001842 } catch (java.io.IOException e) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001843 mReadMessages.append("Error reading: " + e.toString());
1844 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07001845 Slog.wtf(PackageManagerService.TAG, "Error reading package manager stopped packages",
1846 e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001847
1848 }
1849 }
1850
Kenny Root447106f2011-03-23 11:00:15 -07001851 void writeLPr() {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001852 //Debug.startMethodTracing("/data/system/packageprof", 8 * 1024 * 1024);
1853
1854 // Keep the old settings around until we know the new ones have
1855 // been successfully written.
1856 if (mSettingsFilename.exists()) {
1857 // Presence of backup settings file indicates that we failed
1858 // to persist settings earlier. So preserve the older
1859 // backup for future reference since the current settings
1860 // might have been corrupted.
1861 if (!mBackupSettingsFilename.exists()) {
1862 if (!mSettingsFilename.renameTo(mBackupSettingsFilename)) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07001863 Slog.wtf(PackageManagerService.TAG,
1864 "Unable to backup package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001865 + " current changes will be lost at reboot");
1866 return;
1867 }
1868 } else {
1869 mSettingsFilename.delete();
1870 Slog.w(PackageManagerService.TAG, "Preserving older settings backup");
1871 }
1872 }
1873
1874 mPastSignatures.clear();
1875
1876 try {
1877 FileOutputStream fstr = new FileOutputStream(mSettingsFilename);
1878 BufferedOutputStream str = new BufferedOutputStream(fstr);
1879
1880 //XmlSerializer serializer = XmlUtils.serializerInstance();
1881 XmlSerializer serializer = new FastXmlSerializer();
1882 serializer.setOutput(str, "utf-8");
1883 serializer.startDocument(null, true);
1884 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1885
1886 serializer.startTag(null, "packages");
1887
1888 serializer.startTag(null, "last-platform-version");
1889 serializer.attribute(null, "internal", Integer.toString(mInternalSdkPlatform));
1890 serializer.attribute(null, "external", Integer.toString(mExternalSdkPlatform));
Jeff Sharkey4ed745d2014-07-15 20:39:15 -07001891 serializer.attribute(null, "fingerprint", mFingerprint);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001892 serializer.endTag(null, "last-platform-version");
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07001893
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07001894 serializer.startTag(null, "database-version");
1895 serializer.attribute(null, "internal", Integer.toString(mInternalDatabaseVersion));
1896 serializer.attribute(null, "external", Integer.toString(mExternalDatabaseVersion));
1897 serializer.endTag(null, "database-version");
1898
Kenny Root0aaa0d92011-09-12 16:42:55 -07001899 if (mVerifierDeviceIdentity != null) {
1900 serializer.startTag(null, "verifier");
1901 serializer.attribute(null, "device", mVerifierDeviceIdentity.toString());
1902 serializer.endTag(null, "verifier");
1903 }
1904
Jeff Sharkeyf5385772012-05-11 14:04:41 -07001905 if (mReadExternalStorageEnforced != null) {
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07001906 serializer.startTag(null, TAG_READ_EXTERNAL_STORAGE);
1907 serializer.attribute(
Jeff Sharkey5d32e772012-04-12 15:59:23 -07001908 null, ATTR_ENFORCEMENT, mReadExternalStorageEnforced ? "1" : "0");
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07001909 serializer.endTag(null, TAG_READ_EXTERNAL_STORAGE);
1910 }
1911
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001912 serializer.startTag(null, "permission-trees");
1913 for (BasePermission bp : mPermissionTrees.values()) {
Kenny Root447106f2011-03-23 11:00:15 -07001914 writePermissionLPr(serializer, bp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001915 }
1916 serializer.endTag(null, "permission-trees");
1917
1918 serializer.startTag(null, "permissions");
1919 for (BasePermission bp : mPermissions.values()) {
Kenny Root447106f2011-03-23 11:00:15 -07001920 writePermissionLPr(serializer, bp);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001921 }
1922 serializer.endTag(null, "permissions");
1923
Kenny Root447106f2011-03-23 11:00:15 -07001924 for (final PackageSetting pkg : mPackages.values()) {
1925 writePackageLPr(serializer, pkg);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001926 }
1927
Kenny Root447106f2011-03-23 11:00:15 -07001928 for (final PackageSetting pkg : mDisabledSysPackages.values()) {
1929 writeDisabledSysPackageLPr(serializer, pkg);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001930 }
1931
Kenny Root447106f2011-03-23 11:00:15 -07001932 for (final SharedUserSetting usr : mSharedUsers.values()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001933 serializer.startTag(null, "shared-user");
Amith Yamasani483f3b02012-03-13 16:08:00 -07001934 serializer.attribute(null, ATTR_NAME, usr.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001935 serializer.attribute(null, "userId",
1936 Integer.toString(usr.userId));
1937 usr.signatures.writeXml(serializer, "sigs", mPastSignatures);
Svetoslavc6d1c342015-02-26 14:44:43 -08001938 writePermissionsLPr(serializer, usr.getPermissionsState().getInstallPermissions());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001939 serializer.endTag(null, "shared-user");
1940 }
1941
1942 if (mPackagesToBeCleaned.size() > 0) {
Jeff Sharkey752cd922012-09-23 16:25:12 -07001943 for (PackageCleanItem item : mPackagesToBeCleaned) {
1944 final String userStr = Integer.toString(item.userId);
1945 serializer.startTag(null, "cleaning-package");
1946 serializer.attribute(null, ATTR_NAME, item.packageName);
1947 serializer.attribute(null, ATTR_CODE, item.andCode ? "true" : "false");
1948 serializer.attribute(null, ATTR_USER, userStr);
1949 serializer.endTag(null, "cleaning-package");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001950 }
1951 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001952
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001953 if (mRenamedPackages.size() > 0) {
Andy McFadden2f362292012-01-20 14:43:38 -08001954 for (Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001955 serializer.startTag(null, "renamed-package");
1956 serializer.attribute(null, "new", e.getKey());
1957 serializer.attribute(null, "old", e.getValue());
1958 serializer.endTag(null, "renamed-package");
1959 }
1960 }
Svetoslavc6d1c342015-02-26 14:44:43 -08001961
dcashman55b10782014-04-09 14:20:38 -07001962 mKeySetManagerService.writeKeySetManagerServiceLPr(serializer);
Geremy Condraf1bcca82013-01-07 22:35:24 -08001963
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001964 serializer.endTag(null, "packages");
1965
1966 serializer.endDocument();
1967
1968 str.flush();
1969 FileUtils.sync(fstr);
1970 str.close();
1971
1972 // New settings successfully written, old ones are no longer
1973 // needed.
1974 mBackupSettingsFilename.delete();
1975 FileUtils.setPermissions(mSettingsFilename.toString(),
1976 FileUtils.S_IRUSR|FileUtils.S_IWUSR
Nick Kralevich70522ad2012-01-06 13:58:01 -08001977 |FileUtils.S_IRGRP|FileUtils.S_IWGRP,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001978 -1, -1);
1979
1980 // Write package list file now, use a JournaledFile.
Jeff Sharkey184a0102013-07-10 16:19:52 -07001981 File tempFile = new File(mPackageListFilename.getAbsolutePath() + ".tmp");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001982 JournaledFile journal = new JournaledFile(mPackageListFilename, tempFile);
1983
Jeff Sharkey184a0102013-07-10 16:19:52 -07001984 final File writeTarget = journal.chooseForWrite();
1985 fstr = new FileOutputStream(writeTarget);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001986 str = new BufferedOutputStream(fstr);
1987 try {
Jeff Sharkeyaebb65c2014-11-24 15:00:13 -08001988 FileUtils.setPermissions(fstr.getFD(), 0640, SYSTEM_UID, PACKAGE_INFO_GID);
Jeff Sharkey184a0102013-07-10 16:19:52 -07001989
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001990 StringBuilder sb = new StringBuilder();
Kenny Root447106f2011-03-23 11:00:15 -07001991 for (final PackageSetting pkg : mPackages.values()) {
Jeff Sharkey5dea7d52013-09-03 12:01:20 -07001992 if (pkg.pkg == null || pkg.pkg.applicationInfo == null) {
1993 Slog.w(TAG, "Skipping " + pkg + " due to missing metadata");
1994 continue;
1995 }
1996
Jeff Sharkey02e4d16e2013-08-12 20:31:36 -07001997 final ApplicationInfo ai = pkg.pkg.applicationInfo;
1998 final String dataPath = ai.dataDir;
1999 final boolean isDebug = (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
Svetoslavc6d1c342015-02-26 14:44:43 -08002000 final int[] gids = pkg.getPermissionsState().computeGids();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002001
Jeff Sharkeycc8f7102013-09-10 16:57:24 -07002002 // Avoid any application that has a space in its path.
2003 if (dataPath.indexOf(" ") >= 0)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002004 continue;
2005
2006 // we store on each line the following information for now:
2007 //
2008 // pkgName - package name
2009 // userId - application-specific user id
2010 // debugFlag - 0 or 1 if the package is debuggable.
2011 // dataPath - path to package's data path
Robert Craig4a453732013-03-26 08:21:37 -04002012 // seinfo - seinfo label for the app (assigned at install time)
Jeff Sharkey02e4d16e2013-08-12 20:31:36 -07002013 // gids - supplementary gids this app launches with
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002014 //
2015 // NOTE: We prefer not to expose all ApplicationInfo flags for now.
2016 //
2017 // DO NOT MODIFY THIS FORMAT UNLESS YOU CAN ALSO MODIFY ITS USERS
2018 // FROM NATIVE CODE. AT THE MOMENT, LOOK AT THE FOLLOWING SOURCES:
Mark Salyzyn2018fd02015-03-12 09:18:26 -07002019 // system/core/logd/LogStatistics.cpp
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002020 // system/core/run-as/run-as.c
Jeff Sharkey184a0102013-07-10 16:19:52 -07002021 // system/core/sdcard/sdcard.c
Nick Kralevich32cc40a2014-03-26 15:55:41 +00002022 // external/libselinux/src/android.c:package_info_init()
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002023 //
2024 sb.setLength(0);
2025 sb.append(ai.packageName);
2026 sb.append(" ");
2027 sb.append((int)ai.uid);
2028 sb.append(isDebug ? " 1 " : " 0 ");
2029 sb.append(dataPath);
Robert Craig4a453732013-03-26 08:21:37 -04002030 sb.append(" ");
2031 sb.append(ai.seinfo);
Jeff Sharkey02e4d16e2013-08-12 20:31:36 -07002032 sb.append(" ");
2033 if (gids != null && gids.length > 0) {
2034 sb.append(gids[0]);
2035 for (int i = 1; i < gids.length; i++) {
2036 sb.append(",");
2037 sb.append(gids[i]);
2038 }
2039 } else {
2040 sb.append("none");
2041 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002042 sb.append("\n");
2043 str.write(sb.toString().getBytes());
2044 }
2045 str.flush();
2046 FileUtils.sync(fstr);
2047 str.close();
2048 journal.commit();
Kenny Root62539e92011-11-10 09:35:38 -08002049 } catch (Exception e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002050 Slog.wtf(TAG, "Failed to write packages.list", e);
Kenny Root62539e92011-11-10 09:35:38 -08002051 IoUtils.closeQuietly(str);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002052 journal.rollback();
2053 }
2054
Amith Yamasani483f3b02012-03-13 16:08:00 -07002055 writeAllUsersPackageRestrictionsLPr();
Svetoslavc6d1c342015-02-26 14:44:43 -08002056
2057 writeAllRuntimePermissionsLPr();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002058 return;
2059
2060 } catch(XmlPullParserException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002061 Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002062 + "current changes will be lost at reboot", e);
2063 } catch(java.io.IOException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002064 Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, "
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002065 + "current changes will be lost at reboot", e);
2066 }
2067 // Clean up partially written files
2068 if (mSettingsFilename.exists()) {
2069 if (!mSettingsFilename.delete()) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002070 Slog.wtf(PackageManagerService.TAG, "Failed to clean up mangled file: "
Amith Yamasani483f3b02012-03-13 16:08:00 -07002071 + mSettingsFilename);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002072 }
2073 }
2074 //Debug.stopMethodTracing();
2075 }
2076
Kenny Root447106f2011-03-23 11:00:15 -07002077 void writeDisabledSysPackageLPr(XmlSerializer serializer, final PackageSetting pkg)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002078 throws java.io.IOException {
2079 serializer.startTag(null, "updated-package");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002080 serializer.attribute(null, ATTR_NAME, pkg.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002081 if (pkg.realName != null) {
2082 serializer.attribute(null, "realName", pkg.realName);
2083 }
2084 serializer.attribute(null, "codePath", pkg.codePathString);
2085 serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp));
2086 serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime));
2087 serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime));
2088 serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
2089 if (!pkg.resourcePathString.equals(pkg.codePathString)) {
2090 serializer.attribute(null, "resourcePath", pkg.resourcePathString);
2091 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002092 if (pkg.legacyNativeLibraryPathString != null) {
2093 serializer.attribute(null, "nativeLibraryPath", pkg.legacyNativeLibraryPathString);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002094 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002095 if (pkg.primaryCpuAbiString != null) {
2096 serializer.attribute(null, "primaryCpuAbi", pkg.primaryCpuAbiString);
Narayan Kamath9e289d72014-04-10 09:26:59 +00002097 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002098 if (pkg.secondaryCpuAbiString != null) {
2099 serializer.attribute(null, "secondaryCpuAbi", pkg.secondaryCpuAbiString);
2100 }
Narayan Kamath4903f642014-08-11 13:33:45 +01002101 if (pkg.cpuAbiOverrideString != null) {
2102 serializer.attribute(null, "cpuAbiOverride", pkg.cpuAbiOverrideString);
2103 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002104
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002105 if (pkg.sharedUser == null) {
Amith Yamasani13593602012-03-22 16:16:17 -07002106 serializer.attribute(null, "userId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002107 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07002108 serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002109 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002110
2111 // If this is a shared user, the permissions will be written there.
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002112 if (pkg.sharedUser == null) {
Svetoslavc6d1c342015-02-26 14:44:43 -08002113 writePermissionsLPr(serializer, pkg.getPermissionsState().getInstallPermissions());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002114 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002115
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002116 serializer.endTag(null, "updated-package");
2117 }
2118
Kenny Root447106f2011-03-23 11:00:15 -07002119 void writePackageLPr(XmlSerializer serializer, final PackageSetting pkg)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002120 throws java.io.IOException {
2121 serializer.startTag(null, "package");
Amith Yamasani483f3b02012-03-13 16:08:00 -07002122 serializer.attribute(null, ATTR_NAME, pkg.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002123 if (pkg.realName != null) {
2124 serializer.attribute(null, "realName", pkg.realName);
2125 }
2126 serializer.attribute(null, "codePath", pkg.codePathString);
2127 if (!pkg.resourcePathString.equals(pkg.codePathString)) {
2128 serializer.attribute(null, "resourcePath", pkg.resourcePathString);
2129 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002130
2131 if (pkg.legacyNativeLibraryPathString != null) {
2132 serializer.attribute(null, "nativeLibraryPath", pkg.legacyNativeLibraryPathString);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002133 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002134 if (pkg.primaryCpuAbiString != null) {
2135 serializer.attribute(null, "primaryCpuAbi", pkg.primaryCpuAbiString);
Narayan Kamath9e289d72014-04-10 09:26:59 +00002136 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002137 if (pkg.secondaryCpuAbiString != null) {
2138 serializer.attribute(null, "secondaryCpuAbi", pkg.secondaryCpuAbiString);
2139 }
Narayan Kamath4903f642014-08-11 13:33:45 +01002140 if (pkg.cpuAbiOverrideString != null) {
2141 serializer.attribute(null, "cpuAbiOverride", pkg.cpuAbiOverrideString);
2142 }
Narayan Kamathff110bd2014-07-04 18:30:45 +01002143
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002144 serializer.attribute(null, "publicFlags", Integer.toString(pkg.pkgFlags));
2145 serializer.attribute(null, "privateFlags", Integer.toString(pkg.pkgPrivateFlags));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002146 serializer.attribute(null, "ft", Long.toHexString(pkg.timeStamp));
2147 serializer.attribute(null, "it", Long.toHexString(pkg.firstInstallTime));
2148 serializer.attribute(null, "ut", Long.toHexString(pkg.lastUpdateTime));
2149 serializer.attribute(null, "version", String.valueOf(pkg.versionCode));
2150 if (pkg.sharedUser == null) {
Amith Yamasani13593602012-03-22 16:16:17 -07002151 serializer.attribute(null, "userId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002152 } else {
Amith Yamasani13593602012-03-22 16:16:17 -07002153 serializer.attribute(null, "sharedUserId", Integer.toString(pkg.appId));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002154 }
2155 if (pkg.uidError) {
2156 serializer.attribute(null, "uidError", "true");
2157 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002158 if (pkg.installStatus == PackageSettingBase.PKG_INSTALL_INCOMPLETE) {
2159 serializer.attribute(null, "installStatus", "false");
2160 }
2161 if (pkg.installerPackageName != null) {
2162 serializer.attribute(null, "installer", pkg.installerPackageName);
2163 }
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07002164 if (pkg.volumeUuid != null) {
2165 serializer.attribute(null, "volumeUuid", pkg.volumeUuid);
2166 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002167 pkg.signatures.writeXml(serializer, "sigs", mPastSignatures);
2168 if ((pkg.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0) {
Svetoslavc6d1c342015-02-26 14:44:43 -08002169 writePermissionsLPr(serializer, pkg.getPermissionsState().getInstallPermissions());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002170 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002171
dcashman8c04fac2015-03-23 11:39:42 -07002172 writeSigningKeySetLPr(serializer, pkg.keySetData);
dcashman55b10782014-04-09 14:20:38 -07002173 writeUpgradeKeySetsLPr(serializer, pkg.keySetData);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002174 writeKeySetAliasesLPr(serializer, pkg.keySetData);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07002175 writeDomainVerificationsLPr(serializer, pkg.verificationInfo);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002176
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002177 serializer.endTag(null, "package");
2178 }
2179
dcashman8c04fac2015-03-23 11:39:42 -07002180 void writeSigningKeySetLPr(XmlSerializer serializer,
Geremy Condraf1bcca82013-01-07 22:35:24 -08002181 PackageKeySetData data) throws IOException {
dcashman8c04fac2015-03-23 11:39:42 -07002182 serializer.startTag(null, "proper-signing-keyset");
2183 serializer.attribute(null, "identifier",
2184 Long.toString(data.getProperSigningKeySet()));
2185 serializer.endTag(null, "proper-signing-keyset");
dcashman55b10782014-04-09 14:20:38 -07002186 }
2187
2188 void writeUpgradeKeySetsLPr(XmlSerializer serializer,
2189 PackageKeySetData data) throws IOException {
dcashman8c04fac2015-03-23 11:39:42 -07002190 long properSigning = data.getProperSigningKeySet();
dcashman55b10782014-04-09 14:20:38 -07002191 if (data.isUsingUpgradeKeySets()) {
2192 for (long id : data.getUpgradeKeySets()) {
2193 serializer.startTag(null, "upgrade-keyset");
2194 serializer.attribute(null, "identifier", Long.toString(id));
2195 serializer.endTag(null, "upgrade-keyset");
2196 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08002197 }
2198 }
2199
2200 void writeKeySetAliasesLPr(XmlSerializer serializer,
2201 PackageKeySetData data) throws IOException {
2202 for (Map.Entry<String, Long> e: data.getAliases().entrySet()) {
2203 serializer.startTag(null, "defined-keyset");
2204 serializer.attribute(null, "alias", e.getKey());
2205 serializer.attribute(null, "identifier", Long.toString(e.getValue()));
2206 serializer.endTag(null, "defined-keyset");
2207 }
2208 }
2209
Kenny Root447106f2011-03-23 11:00:15 -07002210 void writePermissionLPr(XmlSerializer serializer, BasePermission bp)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002211 throws XmlPullParserException, java.io.IOException {
2212 if (bp.type != BasePermission.TYPE_BUILTIN && bp.sourcePackage != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002213 serializer.startTag(null, TAG_ITEM);
2214 serializer.attribute(null, ATTR_NAME, bp.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002215 serializer.attribute(null, "package", bp.sourcePackage);
2216 if (bp.protectionLevel != PermissionInfo.PROTECTION_NORMAL) {
2217 serializer.attribute(null, "protection", Integer.toString(bp.protectionLevel));
2218 }
2219 if (PackageManagerService.DEBUG_SETTINGS)
2220 Log.v(PackageManagerService.TAG, "Writing perm: name=" + bp.name + " type="
2221 + bp.type);
2222 if (bp.type == BasePermission.TYPE_DYNAMIC) {
Kenny Root447106f2011-03-23 11:00:15 -07002223 final PermissionInfo pi = bp.perm != null ? bp.perm.info : bp.pendingInfo;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002224 if (pi != null) {
2225 serializer.attribute(null, "type", "dynamic");
2226 if (pi.icon != 0) {
2227 serializer.attribute(null, "icon", Integer.toString(pi.icon));
2228 }
2229 if (pi.nonLocalizedLabel != null) {
2230 serializer.attribute(null, "label", pi.nonLocalizedLabel.toString());
2231 }
2232 }
2233 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07002234 serializer.endTag(null, TAG_ITEM);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002235 }
2236 }
2237
Kenny Root447106f2011-03-23 11:00:15 -07002238 ArrayList<PackageSetting> getListOfIncompleteInstallPackagesLPr() {
Jeff Sharkey9f837a92014-10-24 12:07:24 -07002239 final ArraySet<String> kList = new ArraySet<String>(mPackages.keySet());
Kenny Root447106f2011-03-23 11:00:15 -07002240 final Iterator<String> its = kList.iterator();
2241 final ArrayList<PackageSetting> ret = new ArrayList<PackageSetting>();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002242 while (its.hasNext()) {
Kenny Root447106f2011-03-23 11:00:15 -07002243 final String key = its.next();
2244 final PackageSetting ps = mPackages.get(key);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002245 if (ps.getInstallStatus() == PackageSettingBase.PKG_INSTALL_INCOMPLETE) {
2246 ret.add(ps);
2247 }
2248 }
2249 return ret;
2250 }
2251
Jeff Sharkey752cd922012-09-23 16:25:12 -07002252 void addPackageToCleanLPw(PackageCleanItem pkg) {
2253 if (!mPackagesToBeCleaned.contains(pkg)) {
2254 mPackagesToBeCleaned.add(pkg);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002255 }
2256 }
2257
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08002258 boolean readLPw(PackageManagerService service, List<UserInfo> users, int sdkVersion,
2259 boolean onlyCore) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002260 FileInputStream str = null;
2261 if (mBackupSettingsFilename.exists()) {
2262 try {
2263 str = new FileInputStream(mBackupSettingsFilename);
2264 mReadMessages.append("Reading from backup settings file\n");
2265 PackageManagerService.reportSettingsProblem(Log.INFO,
2266 "Need to read from backup settings file");
2267 if (mSettingsFilename.exists()) {
2268 // If both the backup and settings file exist, we
2269 // ignore the settings since it might have been
2270 // corrupted.
2271 Slog.w(PackageManagerService.TAG, "Cleaning up settings file "
2272 + mSettingsFilename);
2273 mSettingsFilename.delete();
2274 }
2275 } catch (java.io.IOException e) {
2276 // We'll try for the normal settings file.
2277 }
2278 }
2279
Kenny Root447106f2011-03-23 11:00:15 -07002280 mPendingPackages.clear();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002281 mPastSignatures.clear();
dcashman8c04fac2015-03-23 11:39:42 -07002282 mKeySetRefs.clear();
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002283
2284 try {
2285 if (str == null) {
2286 if (!mSettingsFilename.exists()) {
2287 mReadMessages.append("No settings file found\n");
2288 PackageManagerService.reportSettingsProblem(Log.INFO,
2289 "No settings file; creating initial state");
Dianne Hackborn13579ed2012-11-28 18:05:36 -08002290 mInternalSdkPlatform = mExternalSdkPlatform = sdkVersion;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -07002291 mFingerprint = Build.FINGERPRINT;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002292 return false;
2293 }
2294 str = new FileInputStream(mSettingsFilename);
2295 }
2296 XmlPullParser parser = Xml.newPullParser();
2297 parser.setInput(str, null);
2298
2299 int type;
2300 while ((type = parser.next()) != XmlPullParser.START_TAG
2301 && type != XmlPullParser.END_DOCUMENT) {
2302 ;
2303 }
2304
2305 if (type != XmlPullParser.START_TAG) {
2306 mReadMessages.append("No start tag found in settings file\n");
2307 PackageManagerService.reportSettingsProblem(Log.WARN,
2308 "No start tag found in package manager settings");
Dianne Hackborn8d051722014-10-01 14:59:58 -07002309 Slog.wtf(PackageManagerService.TAG,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002310 "No start tag found in package manager settings");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002311 return false;
2312 }
2313
2314 int outerDepth = parser.getDepth();
2315 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2316 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2317 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2318 continue;
2319 }
2320
2321 String tagName = parser.getName();
2322 if (tagName.equals("package")) {
Kenny Root447106f2011-03-23 11:00:15 -07002323 readPackageLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002324 } else if (tagName.equals("permissions")) {
Kenny Root447106f2011-03-23 11:00:15 -07002325 readPermissionsLPw(mPermissions, parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002326 } else if (tagName.equals("permission-trees")) {
Kenny Root447106f2011-03-23 11:00:15 -07002327 readPermissionsLPw(mPermissionTrees, parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002328 } else if (tagName.equals("shared-user")) {
Kenny Root447106f2011-03-23 11:00:15 -07002329 readSharedUserLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002330 } else if (tagName.equals("preferred-packages")) {
2331 // no longer used.
2332 } else if (tagName.equals("preferred-activities")) {
Dianne Hackborn63092712012-10-07 14:45:35 -07002333 // Upgrading from old single-user implementation;
2334 // these are the preferred activities for user 0.
2335 readPreferredActivitiesLPw(parser, 0);
Sander Alewijnseaf597622014-03-20 18:44:57 +00002336 } else if (tagName.equals(TAG_PERSISTENT_PREFERRED_ACTIVITIES)) {
Sander Alewijnsef475ca32014-02-17 15:13:58 +00002337 // TODO: check whether this is okay! as it is very
2338 // similar to how preferred-activities are treated
2339 readPersistentPreferredActivitiesLPw(parser, 0);
Nicolas Prevot29762c32014-07-29 18:51:32 +01002340 } else if (tagName.equals(TAG_CROSS_PROFILE_INTENT_FILTERS)) {
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00002341 // TODO: check whether this is okay! as it is very
2342 // similar to how preferred-activities are treated
Nicolas Prevot81948992014-05-16 18:25:26 +01002343 readCrossProfileIntentFiltersLPw(parser, 0);
Fabrice Di Meglio62271722015-04-10 17:24:02 -07002344 } else if (tagName.equals(TAG_DEFAULT_BROWSER)) {
2345 readDefaultAppsLPw(parser, 0);
2346 } else if (tagName.equals("updated-package")) {
Kenny Root447106f2011-03-23 11:00:15 -07002347 readDisabledSysPackageLPw(parser);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002348 } else if (tagName.equals("cleaning-package")) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002349 String name = parser.getAttributeValue(null, ATTR_NAME);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002350 String userStr = parser.getAttributeValue(null, ATTR_USER);
2351 String codeStr = parser.getAttributeValue(null, ATTR_CODE);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002352 if (name != null) {
Jeff Sharkey752cd922012-09-23 16:25:12 -07002353 int userId = 0;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002354 boolean andCode = true;
2355 try {
2356 if (userStr != null) {
Jeff Sharkey752cd922012-09-23 16:25:12 -07002357 userId = Integer.parseInt(userStr);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07002358 }
2359 } catch (NumberFormatException e) {
2360 }
2361 if (codeStr != null) {
2362 andCode = Boolean.parseBoolean(codeStr);
2363 }
Jeff Sharkey752cd922012-09-23 16:25:12 -07002364 addPackageToCleanLPw(new PackageCleanItem(userId, name, andCode));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002365 }
2366 } else if (tagName.equals("renamed-package")) {
2367 String nname = parser.getAttributeValue(null, "new");
2368 String oname = parser.getAttributeValue(null, "old");
2369 if (nname != null && oname != null) {
2370 mRenamedPackages.put(nname, oname);
2371 }
2372 } else if (tagName.equals("last-platform-version")) {
2373 mInternalSdkPlatform = mExternalSdkPlatform = 0;
2374 try {
2375 String internal = parser.getAttributeValue(null, "internal");
2376 if (internal != null) {
2377 mInternalSdkPlatform = Integer.parseInt(internal);
2378 }
2379 String external = parser.getAttributeValue(null, "external");
2380 if (external != null) {
2381 mExternalSdkPlatform = Integer.parseInt(external);
2382 }
2383 } catch (NumberFormatException e) {
2384 }
Jeff Sharkey4ed745d2014-07-15 20:39:15 -07002385 mFingerprint = parser.getAttributeValue(null, "fingerprint");
Kenny Rootc1c0d3c2014-04-24 13:36:40 -07002386 } else if (tagName.equals("database-version")) {
2387 mInternalDatabaseVersion = mExternalDatabaseVersion = 0;
2388 try {
2389 String internalDbVersionString = parser.getAttributeValue(null, "internal");
2390 if (internalDbVersionString != null) {
2391 mInternalDatabaseVersion = Integer.parseInt(internalDbVersionString);
2392 }
2393 String externalDbVersionString = parser.getAttributeValue(null, "external");
2394 if (externalDbVersionString != null) {
2395 mExternalDatabaseVersion = Integer.parseInt(externalDbVersionString);
2396 }
2397 } catch (NumberFormatException ignored) {
2398 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002399 } else if (tagName.equals("verifier")) {
2400 final String deviceIdentity = parser.getAttributeValue(null, "device");
2401 try {
2402 mVerifierDeviceIdentity = VerifierDeviceIdentity.parse(deviceIdentity);
2403 } catch (IllegalArgumentException e) {
2404 Slog.w(PackageManagerService.TAG, "Discard invalid verifier device id: "
2405 + e.getMessage());
2406 }
Jeff Sharkeyedc84ee82012-03-19 16:52:26 -07002407 } else if (TAG_READ_EXTERNAL_STORAGE.equals(tagName)) {
2408 final String enforcement = parser.getAttributeValue(null, ATTR_ENFORCEMENT);
Jeff Sharkey5d32e772012-04-12 15:59:23 -07002409 mReadExternalStorageEnforced = "1".equals(enforcement);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002410 } else if (tagName.equals("keyset-settings")) {
dcashman8c04fac2015-03-23 11:39:42 -07002411 mKeySetManagerService.readKeySetsLPw(parser, mKeySetRefs);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002412 } else {
2413 Slog.w(PackageManagerService.TAG, "Unknown element under <packages>: "
2414 + parser.getName());
2415 XmlUtils.skipCurrentTag(parser);
2416 }
2417 }
2418
2419 str.close();
2420
2421 } catch (XmlPullParserException e) {
2422 mReadMessages.append("Error reading: " + e.toString());
2423 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002424 Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002425
2426 } catch (java.io.IOException e) {
2427 mReadMessages.append("Error reading: " + e.toString());
2428 PackageManagerService.reportSettingsProblem(Log.ERROR, "Error reading settings: " + e);
Dianne Hackborn8d051722014-10-01 14:59:58 -07002429 Slog.wtf(PackageManagerService.TAG, "Error reading package manager settings", e);
Amith Yamasani258848d2012-08-10 17:06:33 -07002430 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002431
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002432 final int N = mPendingPackages.size();
dcashman8c04fac2015-03-23 11:39:42 -07002433
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002434 for (int i = 0; i < N; i++) {
2435 final PendingPackage pp = mPendingPackages.get(i);
Kenny Root447106f2011-03-23 11:00:15 -07002436 Object idObj = getUserIdLPr(pp.sharedId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002437 if (idObj != null && idObj instanceof SharedUserSetting) {
Kenny Root447106f2011-03-23 11:00:15 -07002438 PackageSetting p = getPackageLPw(pp.name, null, pp.realName,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002439 (SharedUserSetting) idObj, pp.codePath, pp.resourcePath,
Narayan Kamathff110bd2014-07-04 18:30:45 +01002440 pp.legacyNativeLibraryPathString, pp.primaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002441 pp.secondaryCpuAbiString, pp.versionCode, pp.pkgFlags, pp.pkgPrivateFlags,
2442 null, true /* add */, false /* allowInstall */);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002443 if (p == null) {
2444 PackageManagerService.reportSettingsProblem(Log.WARN,
2445 "Unable to create application package for " + pp.name);
2446 continue;
2447 }
2448 p.copyFrom(pp);
2449 } else if (idObj != null) {
2450 String msg = "Bad package setting: package " + pp.name + " has shared uid "
2451 + pp.sharedId + " that is not a shared uid\n";
2452 mReadMessages.append(msg);
2453 PackageManagerService.reportSettingsProblem(Log.ERROR, msg);
2454 } else {
2455 String msg = "Bad package setting: package " + pp.name + " has shared uid "
2456 + pp.sharedId + " that is not defined\n";
2457 mReadMessages.append(msg);
2458 PackageManagerService.reportSettingsProblem(Log.ERROR, msg);
2459 }
2460 }
2461 mPendingPackages.clear();
2462
Amith Yamasanif031f232012-10-26 15:35:21 -07002463 if (mBackupStoppedPackagesFilename.exists()
2464 || mStoppedPackagesFilename.exists()) {
2465 // Read old file
2466 readStoppedLPw();
2467 mBackupStoppedPackagesFilename.delete();
2468 mStoppedPackagesFilename.delete();
2469 // Migrate to new file format
2470 writePackageRestrictionsLPr(0);
2471 } else {
2472 if (users == null) {
2473 readPackageRestrictionsLPr(0);
Svetoslavc6d1c342015-02-26 14:44:43 -08002474 mRuntimePermissionsPersistence.readStateForUserSyncLPr(UserHandle.USER_OWNER);
Amith Yamasanif031f232012-10-26 15:35:21 -07002475 } else {
2476 for (UserInfo user : users) {
2477 readPackageRestrictionsLPr(user.id);
Svetoslavc6d1c342015-02-26 14:44:43 -08002478 mRuntimePermissionsPersistence.readStateForUserSyncLPr(user.id);
Amith Yamasanif031f232012-10-26 15:35:21 -07002479 }
2480 }
2481 }
2482
Kenny Root1d1b4892011-04-08 14:25:24 -07002483 /*
2484 * Make sure all the updated system packages have their shared users
2485 * associated with them.
2486 */
2487 final Iterator<PackageSetting> disabledIt = mDisabledSysPackages.values().iterator();
2488 while (disabledIt.hasNext()) {
2489 final PackageSetting disabledPs = disabledIt.next();
Amith Yamasani13593602012-03-22 16:16:17 -07002490 final Object id = getUserIdLPr(disabledPs.appId);
Kenny Root1d1b4892011-04-08 14:25:24 -07002491 if (id != null && id instanceof SharedUserSetting) {
2492 disabledPs.sharedUser = (SharedUserSetting) id;
2493 }
2494 }
2495
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002496 mReadMessages.append("Read completed successfully: " + mPackages.size() + " packages, "
2497 + mSharedUsers.size() + " shared uids\n");
2498
2499 return true;
2500 }
2501
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08002502 void readDefaultPreferredAppsLPw(PackageManagerService service, int userId) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002503 // First pull data from any pre-installed apps.
2504 for (PackageSetting ps : mPackages.values()) {
2505 if ((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0 && ps.pkg != null
2506 && ps.pkg.preferredActivityFilters != null) {
2507 ArrayList<PackageParser.ActivityIntentInfo> intents
2508 = ps.pkg.preferredActivityFilters;
2509 for (int i=0; i<intents.size(); i++) {
2510 PackageParser.ActivityIntentInfo aii = intents.get(i);
2511 applyDefaultPreferredActivityLPw(service, aii, new ComponentName(
2512 ps.name, aii.activity.className), userId);
2513 }
2514 }
2515 }
2516
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07002517 // Read preferred apps from .../etc/preferred-apps directory.
2518 File preferredDir = new File(Environment.getRootDirectory(), "etc/preferred-apps");
2519 if (!preferredDir.exists() || !preferredDir.isDirectory()) {
2520 return;
2521 }
2522 if (!preferredDir.canRead()) {
2523 Slog.w(TAG, "Directory " + preferredDir + " cannot be read");
2524 return;
2525 }
2526
2527 // Iterate over the files in the directory and scan .xml files
2528 for (File f : preferredDir.listFiles()) {
2529 if (!f.getPath().endsWith(".xml")) {
2530 Slog.i(TAG, "Non-xml file " + f + " in " + preferredDir + " directory, ignoring");
2531 continue;
2532 }
2533 if (!f.canRead()) {
2534 Slog.w(TAG, "Preferred apps file " + f + " cannot be read");
2535 continue;
2536 }
2537
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08002538 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Reading default preferred " + f);
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07002539 FileInputStream str = null;
2540 try {
2541 str = new FileInputStream(f);
2542 XmlPullParser parser = Xml.newPullParser();
2543 parser.setInput(str, null);
2544
2545 int type;
2546 while ((type = parser.next()) != XmlPullParser.START_TAG
2547 && type != XmlPullParser.END_DOCUMENT) {
2548 ;
2549 }
2550
2551 if (type != XmlPullParser.START_TAG) {
2552 Slog.w(TAG, "Preferred apps file " + f + " does not have start tag");
2553 continue;
2554 }
2555 if (!"preferred-activities".equals(parser.getName())) {
2556 Slog.w(TAG, "Preferred apps file " + f
2557 + " does not start with 'preferred-activities'");
2558 continue;
2559 }
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08002560 readDefaultPreferredActivitiesLPw(service, parser, userId);
Dianne Hackbornfc8b7fe2012-06-18 15:38:12 -07002561 } catch (XmlPullParserException e) {
2562 Slog.w(TAG, "Error reading apps file " + f, e);
2563 } catch (IOException e) {
2564 Slog.w(TAG, "Error reading apps file " + f, e);
2565 } finally {
2566 if (str != null) {
2567 try {
2568 str.close();
2569 } catch (IOException e) {
2570 }
2571 }
2572 }
2573 }
2574 }
2575
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002576 private void applyDefaultPreferredActivityLPw(PackageManagerService service,
2577 IntentFilter tmpPa, ComponentName cn, int userId) {
2578 // The initial preferences only specify the target activity
2579 // component and intent-filter, not the set of matches. So we
2580 // now need to query for the matches to build the correct
2581 // preferred activity entry.
2582 if (PackageManagerService.DEBUG_PREFERRED) {
2583 Log.d(TAG, "Processing preferred:");
2584 tmpPa.dump(new LogPrinter(Log.DEBUG, TAG), " ");
2585 }
2586 Intent intent = new Intent();
2587 int flags = 0;
2588 intent.setAction(tmpPa.getAction(0));
2589 for (int i=0; i<tmpPa.countCategories(); i++) {
2590 String cat = tmpPa.getCategory(i);
2591 if (cat.equals(Intent.CATEGORY_DEFAULT)) {
2592 flags |= PackageManager.MATCH_DEFAULT_ONLY;
2593 } else {
2594 intent.addCategory(cat);
2595 }
2596 }
2597
2598 boolean doNonData = true;
Christopher Tate2298ef22013-11-04 17:02:10 -08002599 boolean hasSchemes = false;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002600
2601 for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) {
2602 boolean doScheme = true;
2603 String scheme = tmpPa.getDataScheme(ischeme);
Christopher Tate2298ef22013-11-04 17:02:10 -08002604 if (scheme != null && !scheme.isEmpty()) {
2605 hasSchemes = true;
2606 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002607 for (int issp=0; issp<tmpPa.countDataSchemeSpecificParts(); issp++) {
2608 Uri.Builder builder = new Uri.Builder();
2609 builder.scheme(scheme);
2610 PatternMatcher ssp = tmpPa.getDataSchemeSpecificPart(issp);
2611 builder.opaquePart(ssp.getPath());
2612 Intent finalIntent = new Intent(intent);
2613 finalIntent.setData(builder.build());
2614 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002615 scheme, ssp, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002616 doScheme = false;
2617 }
2618 for (int iauth=0; iauth<tmpPa.countDataAuthorities(); iauth++) {
2619 boolean doAuth = true;
2620 IntentFilter.AuthorityEntry auth = tmpPa.getDataAuthority(iauth);
2621 for (int ipath=0; ipath<tmpPa.countDataPaths(); ipath++) {
2622 Uri.Builder builder = new Uri.Builder();
2623 builder.scheme(scheme);
2624 if (auth.getHost() != null) {
2625 builder.authority(auth.getHost());
2626 }
2627 PatternMatcher path = tmpPa.getDataPath(ipath);
2628 builder.path(path.getPath());
2629 Intent finalIntent = new Intent(intent);
2630 finalIntent.setData(builder.build());
2631 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002632 scheme, null, auth, path, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002633 doAuth = doScheme = false;
2634 }
2635 if (doAuth) {
2636 Uri.Builder builder = new Uri.Builder();
2637 builder.scheme(scheme);
2638 if (auth.getHost() != null) {
2639 builder.authority(auth.getHost());
2640 }
2641 Intent finalIntent = new Intent(intent);
2642 finalIntent.setData(builder.build());
2643 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002644 scheme, null, auth, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002645 doScheme = false;
2646 }
2647 }
2648 if (doScheme) {
2649 Uri.Builder builder = new Uri.Builder();
2650 builder.scheme(scheme);
2651 Intent finalIntent = new Intent(intent);
2652 finalIntent.setData(builder.build());
2653 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002654 scheme, null, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002655 }
2656 doNonData = false;
2657 }
2658
2659 for (int idata=0; idata<tmpPa.countDataTypes(); idata++) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002660 String mimeType = tmpPa.getDataType(idata);
Christopher Tate2298ef22013-11-04 17:02:10 -08002661 if (hasSchemes) {
2662 Uri.Builder builder = new Uri.Builder();
2663 for (int ischeme=0; ischeme<tmpPa.countDataSchemes(); ischeme++) {
2664 String scheme = tmpPa.getDataScheme(ischeme);
2665 if (scheme != null && !scheme.isEmpty()) {
2666 Intent finalIntent = new Intent(intent);
2667 builder.scheme(scheme);
2668 finalIntent.setDataAndType(builder.build(), mimeType);
2669 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002670 scheme, null, null, null, userId);
Christopher Tate2298ef22013-11-04 17:02:10 -08002671 }
2672 }
2673 } else {
2674 Intent finalIntent = new Intent(intent);
2675 finalIntent.setType(mimeType);
2676 applyDefaultPreferredActivityLPw(service, finalIntent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002677 null, null, null, null, userId);
Christopher Tate2298ef22013-11-04 17:02:10 -08002678 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002679 doNonData = false;
2680 }
2681
2682 if (doNonData) {
2683 applyDefaultPreferredActivityLPw(service, intent, flags, cn,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002684 null, null, null, null, userId);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002685 }
2686 }
2687
2688 private void applyDefaultPreferredActivityLPw(PackageManagerService service,
2689 Intent intent, int flags, ComponentName cn, String scheme, PatternMatcher ssp,
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002690 IntentFilter.AuthorityEntry auth, PatternMatcher path, int userId) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002691 List<ResolveInfo> ri = service.mActivities.queryIntent(intent,
2692 intent.getType(), flags, 0);
2693 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Queried " + intent
2694 + " results: " + ri);
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07002695 int systemMatch = 0;
2696 int thirdPartyMatch = 0;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002697 if (ri != null && ri.size() > 1) {
2698 boolean haveAct = false;
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002699 ComponentName haveNonSys = null;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002700 ComponentName[] set = new ComponentName[ri.size()];
2701 for (int i=0; i<ri.size(); i++) {
2702 ActivityInfo ai = ri.get(i).activityInfo;
2703 set[i] = new ComponentName(ai.packageName, ai.name);
2704 if ((ai.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07002705 if (ri.get(i).match >= thirdPartyMatch) {
2706 // Keep track of the best match we find of all third
2707 // party apps, for use later to determine if we actually
2708 // want to set a preferred app for this intent.
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002709 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
2710 + ai.packageName + "/" + ai.name + ": non-system!");
2711 haveNonSys = set[i];
2712 break;
2713 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002714 } else if (cn.getPackageName().equals(ai.packageName)
2715 && cn.getClassName().equals(ai.name)) {
2716 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
2717 + ai.packageName + "/" + ai.name + ": default!");
2718 haveAct = true;
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07002719 systemMatch = ri.get(i).match;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002720 } else {
2721 if (PackageManagerService.DEBUG_PREFERRED) Log.d(TAG, "Result "
2722 + ai.packageName + "/" + ai.name + ": skipped");
2723 }
2724 }
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07002725 if (haveNonSys != null && thirdPartyMatch < systemMatch) {
2726 // If we have a matching third party app, but its match is not as
2727 // good as the built-in system app, then we don't want to actually
2728 // consider it a match because presumably the built-in app is still
2729 // the thing we want users to see by default.
2730 haveNonSys = null;
2731 }
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002732 if (haveAct && haveNonSys == null) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002733 IntentFilter filter = new IntentFilter();
2734 if (intent.getAction() != null) {
2735 filter.addAction(intent.getAction());
2736 }
Erin Dahlgren707a59d2013-10-24 15:13:39 -07002737 if (intent.getCategories() != null) {
2738 for (String cat : intent.getCategories()) {
2739 filter.addCategory(cat);
2740 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002741 }
2742 if ((flags&PackageManager.MATCH_DEFAULT_ONLY) != 0) {
2743 filter.addCategory(Intent.CATEGORY_DEFAULT);
2744 }
2745 if (scheme != null) {
2746 filter.addDataScheme(scheme);
2747 }
2748 if (ssp != null) {
2749 filter.addDataSchemeSpecificPart(ssp.getPath(), ssp.getType());
2750 }
2751 if (auth != null) {
2752 filter.addDataAuthority(auth);
2753 }
2754 if (path != null) {
2755 filter.addDataPath(path);
2756 }
Erin Dahlgren707a59d2013-10-24 15:13:39 -07002757 if (intent.getType() != null) {
2758 try {
2759 filter.addDataType(intent.getType());
2760 } catch (IntentFilter.MalformedMimeTypeException ex) {
2761 Slog.w(TAG, "Malformed mimetype " + intent.getType() + " for " + cn);
2762 }
2763 }
Dianne Hackbornf85e7af2014-10-14 10:43:43 -07002764 PreferredActivity pa = new PreferredActivity(filter, systemMatch, set, cn, true);
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002765 editPreferredActivitiesLPw(userId).addFilter(pa);
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002766 } else if (haveNonSys == null) {
2767 StringBuilder sb = new StringBuilder();
2768 sb.append("No component ");
2769 sb.append(cn.flattenToShortString());
2770 sb.append(" found setting preferred ");
2771 sb.append(intent);
2772 sb.append("; possible matches are ");
2773 for (int i=0; i<set.length; i++) {
2774 if (i > 0) sb.append(", ");
2775 sb.append(set[i].flattenToShortString());
2776 }
2777 Slog.w(TAG, sb.toString());
2778 } else {
2779 Slog.i(TAG, "Not setting preferred " + intent + "; found third party match "
2780 + haveNonSys.flattenToShortString());
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002781 }
Dianne Hackborneeb2c7e2014-09-23 16:45:39 -07002782 } else {
2783 Slog.w(TAG, "No potential matches found for " + intent + " while setting preferred "
2784 + cn.flattenToShortString());
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002785 }
2786 }
2787
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08002788 private void readDefaultPreferredActivitiesLPw(PackageManagerService service,
2789 XmlPullParser parser, int userId)
2790 throws XmlPullParserException, IOException {
2791 int outerDepth = parser.getDepth();
2792 int type;
2793 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2794 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2795 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2796 continue;
2797 }
2798
2799 String tagName = parser.getName();
2800 if (tagName.equals(TAG_ITEM)) {
2801 PreferredActivity tmpPa = new PreferredActivity(parser);
2802 if (tmpPa.mPref.getParseError() == null) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07002803 applyDefaultPreferredActivityLPw(service, tmpPa, tmpPa.mPref.mComponent,
2804 userId);
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08002805 } else {
2806 PackageManagerService.reportSettingsProblem(Log.WARN,
2807 "Error in package manager settings: <preferred-activity> "
2808 + tmpPa.mPref.getParseError() + " at "
2809 + parser.getPositionDescription());
2810 }
2811 } else {
2812 PackageManagerService.reportSettingsProblem(Log.WARN,
2813 "Unknown element under <preferred-activities>: " + parser.getName());
2814 XmlUtils.skipCurrentTag(parser);
2815 }
2816 }
2817 }
2818
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002819 private int readInt(XmlPullParser parser, String ns, String name, int defValue) {
2820 String v = parser.getAttributeValue(ns, name);
2821 try {
2822 if (v == null) {
2823 return defValue;
2824 }
2825 return Integer.parseInt(v);
2826 } catch (NumberFormatException e) {
2827 PackageManagerService.reportSettingsProblem(Log.WARN,
2828 "Error in package manager settings: attribute " + name
2829 + " has bad integer value " + v + " at "
2830 + parser.getPositionDescription());
2831 }
2832 return defValue;
2833 }
2834
Jeff Sharkey9f837a92014-10-24 12:07:24 -07002835 private void readPermissionsLPw(ArrayMap<String, BasePermission> out, XmlPullParser parser)
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002836 throws IOException, XmlPullParserException {
2837 int outerDepth = parser.getDepth();
2838 int type;
2839 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2840 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2841 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2842 continue;
2843 }
2844
Kenny Root447106f2011-03-23 11:00:15 -07002845 final String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07002846 if (tagName.equals(TAG_ITEM)) {
2847 final String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Root447106f2011-03-23 11:00:15 -07002848 final String sourcePackage = parser.getAttributeValue(null, "package");
2849 final String ptype = parser.getAttributeValue(null, "type");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002850 if (name != null && sourcePackage != null) {
Kenny Root447106f2011-03-23 11:00:15 -07002851 final boolean dynamic = "dynamic".equals(ptype);
Svetoslavc6d1c342015-02-26 14:44:43 -08002852 final BasePermission bp = new BasePermission(name.intern(), sourcePackage,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002853 dynamic ? BasePermission.TYPE_DYNAMIC : BasePermission.TYPE_NORMAL);
2854 bp.protectionLevel = readInt(parser, null, "protection",
2855 PermissionInfo.PROTECTION_NORMAL);
Dianne Hackborne639da72012-02-21 15:11:13 -08002856 bp.protectionLevel = PermissionInfo.fixProtectionLevel(bp.protectionLevel);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002857 if (dynamic) {
2858 PermissionInfo pi = new PermissionInfo();
2859 pi.packageName = sourcePackage.intern();
2860 pi.name = name.intern();
2861 pi.icon = readInt(parser, null, "icon", 0);
2862 pi.nonLocalizedLabel = parser.getAttributeValue(null, "label");
2863 pi.protectionLevel = bp.protectionLevel;
2864 bp.pendingInfo = pi;
2865 }
2866 out.put(bp.name, bp);
2867 } else {
2868 PackageManagerService.reportSettingsProblem(Log.WARN,
2869 "Error in package manager settings: permissions has" + " no name at "
2870 + parser.getPositionDescription());
2871 }
2872 } else {
2873 PackageManagerService.reportSettingsProblem(Log.WARN,
2874 "Unknown element reading permissions: " + parser.getName() + " at "
2875 + parser.getPositionDescription());
2876 }
2877 XmlUtils.skipCurrentTag(parser);
2878 }
2879 }
2880
Kenny Root447106f2011-03-23 11:00:15 -07002881 private void readDisabledSysPackageLPw(XmlPullParser parser) throws XmlPullParserException,
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002882 IOException {
Amith Yamasani483f3b02012-03-13 16:08:00 -07002883 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002884 String realName = parser.getAttributeValue(null, "realName");
2885 String codePathStr = parser.getAttributeValue(null, "codePath");
2886 String resourcePathStr = parser.getAttributeValue(null, "resourcePath");
Narayan Kamathff110bd2014-07-04 18:30:45 +01002887
2888 String legacyCpuAbiStr = parser.getAttributeValue(null, "requiredCpuAbi");
2889 String legacyNativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath");
2890
2891 String primaryCpuAbiStr = parser.getAttributeValue(null, "primaryCpuAbi");
2892 String secondaryCpuAbiStr = parser.getAttributeValue(null, "secondaryCpuAbi");
Narayan Kamath4903f642014-08-11 13:33:45 +01002893 String cpuAbiOverrideStr = parser.getAttributeValue(null, "cpuAbiOverride");
Narayan Kamathff110bd2014-07-04 18:30:45 +01002894
2895 if (primaryCpuAbiStr == null && legacyCpuAbiStr != null) {
2896 primaryCpuAbiStr = legacyCpuAbiStr;
2897 }
Narayan Kamath9e289d72014-04-10 09:26:59 +00002898
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002899 if (resourcePathStr == null) {
2900 resourcePathStr = codePathStr;
2901 }
2902 String version = parser.getAttributeValue(null, "version");
2903 int versionCode = 0;
2904 if (version != null) {
2905 try {
2906 versionCode = Integer.parseInt(version);
2907 } catch (NumberFormatException e) {
2908 }
2909 }
2910
2911 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002912 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002913 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
Christopher Tate628946a2013-10-18 18:11:05 -07002914 final File codePathFile = new File(codePathStr);
2915 if (PackageManagerService.locationIsPrivileged(codePathFile)) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002916 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Christopher Tate628946a2013-10-18 18:11:05 -07002917 }
2918 PackageSetting ps = new PackageSetting(name, realName, codePathFile,
Narayan Kamathff110bd2014-07-04 18:30:45 +01002919 new File(resourcePathStr), legacyNativeLibraryPathStr, primaryCpuAbiStr,
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002920 secondaryCpuAbiStr, cpuAbiOverrideStr, versionCode, pkgFlags, pkgPrivateFlags);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002921 String timeStampStr = parser.getAttributeValue(null, "ft");
2922 if (timeStampStr != null) {
2923 try {
2924 long timeStamp = Long.parseLong(timeStampStr, 16);
2925 ps.setTimeStamp(timeStamp);
2926 } catch (NumberFormatException e) {
2927 }
2928 } else {
2929 timeStampStr = parser.getAttributeValue(null, "ts");
2930 if (timeStampStr != null) {
2931 try {
2932 long timeStamp = Long.parseLong(timeStampStr);
2933 ps.setTimeStamp(timeStamp);
2934 } catch (NumberFormatException e) {
2935 }
2936 }
2937 }
2938 timeStampStr = parser.getAttributeValue(null, "it");
2939 if (timeStampStr != null) {
2940 try {
2941 ps.firstInstallTime = Long.parseLong(timeStampStr, 16);
2942 } catch (NumberFormatException e) {
2943 }
2944 }
2945 timeStampStr = parser.getAttributeValue(null, "ut");
2946 if (timeStampStr != null) {
2947 try {
2948 ps.lastUpdateTime = Long.parseLong(timeStampStr, 16);
2949 } catch (NumberFormatException e) {
2950 }
2951 }
2952 String idStr = parser.getAttributeValue(null, "userId");
Amith Yamasani13593602012-03-22 16:16:17 -07002953 ps.appId = idStr != null ? Integer.parseInt(idStr) : 0;
2954 if (ps.appId <= 0) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002955 String sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
Amith Yamasani13593602012-03-22 16:16:17 -07002956 ps.appId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002957 }
Svetoslavc6d1c342015-02-26 14:44:43 -08002958
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002959 int outerDepth = parser.getDepth();
2960 int type;
2961 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2962 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2963 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2964 continue;
2965 }
2966
Svetoslavc6d1c342015-02-26 14:44:43 -08002967 if (parser.getName().equals(TAG_PERMISSIONS)) {
2968 readInstallPermissionsLPr(parser, ps.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002969 } else {
2970 PackageManagerService.reportSettingsProblem(Log.WARN,
2971 "Unknown element under <updated-package>: " + parser.getName());
2972 XmlUtils.skipCurrentTag(parser);
2973 }
2974 }
Christopher Tate628946a2013-10-18 18:11:05 -07002975
Svet Ganov12a692a2015-03-28 19:34:15 -07002976 // We keep track for which users we granted permissions to be able
2977 // to grant runtime permissions to system apps for newly appeared
2978 // users or newly appeared system apps. If we supported runtime
2979 // permissions during the previous boot, then we already granted
2980 // permissions for all device users. In such a case we set the users
2981 // for which we granted permissions to avoid clobbering of runtime
2982 // permissions we granted to system apps but the user revoked later.
2983 if (!isFirstRuntimePermissionsBoot()) {
2984 final int[] userIds = UserManagerService.getInstance().getUserIds();
2985 ps.setPermissionsUpdatedForUserIds(userIds);
2986 }
2987
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002988 mDisabledSysPackages.put(name, ps);
2989 }
2990
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002991 private static int PRE_M_APP_INFO_FLAG_HIDDEN = 1<<27;
2992 private static int PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE = 1<<28;
2993 private static int PRE_M_APP_INFO_FLAG_FORWARD_LOCK = 1<<29;
2994 private static int PRE_M_APP_INFO_FLAG_PRIVILEGED = 1<<30;
2995
Kenny Root447106f2011-03-23 11:00:15 -07002996 private void readPackageLPw(XmlPullParser parser) throws XmlPullParserException, IOException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07002997 String name = null;
2998 String realName = null;
2999 String idStr = null;
3000 String sharedIdStr = null;
3001 String codePathStr = null;
3002 String resourcePathStr = null;
Narayan Kamathff110bd2014-07-04 18:30:45 +01003003 String legacyCpuAbiString = null;
3004 String legacyNativeLibraryPathStr = null;
3005 String primaryCpuAbiString = null;
3006 String secondaryCpuAbiString = null;
Narayan Kamath4903f642014-08-11 13:33:45 +01003007 String cpuAbiOverrideString = null;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003008 String systemStr = null;
3009 String installerPackageName = null;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003010 String volumeUuid = null;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003011 String uidError = null;
3012 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003013 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003014 long timeStamp = 0;
3015 long firstInstallTime = 0;
3016 long lastUpdateTime = 0;
3017 PackageSettingBase packageSetting = null;
3018 String version = null;
3019 int versionCode = 0;
3020 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003021 name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003022 realName = parser.getAttributeValue(null, "realName");
3023 idStr = parser.getAttributeValue(null, "userId");
3024 uidError = parser.getAttributeValue(null, "uidError");
3025 sharedIdStr = parser.getAttributeValue(null, "sharedUserId");
3026 codePathStr = parser.getAttributeValue(null, "codePath");
3027 resourcePathStr = parser.getAttributeValue(null, "resourcePath");
Narayan Kamath9e289d72014-04-10 09:26:59 +00003028
Narayan Kamathff110bd2014-07-04 18:30:45 +01003029 legacyCpuAbiString = parser.getAttributeValue(null, "requiredCpuAbi");
3030
3031 legacyNativeLibraryPathStr = parser.getAttributeValue(null, "nativeLibraryPath");
3032 primaryCpuAbiString = parser.getAttributeValue(null, "primaryCpuAbi");
3033 secondaryCpuAbiString = parser.getAttributeValue(null, "secondaryCpuAbi");
Narayan Kamath4903f642014-08-11 13:33:45 +01003034 cpuAbiOverrideString = parser.getAttributeValue(null, "cpuAbiOverride");
Narayan Kamathff110bd2014-07-04 18:30:45 +01003035
3036 if (primaryCpuAbiString == null && legacyCpuAbiString != null) {
3037 primaryCpuAbiString = legacyCpuAbiString;
3038 }
Svetoslavc6d1c342015-02-26 14:44:43 -08003039
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003040 version = parser.getAttributeValue(null, "version");
3041 if (version != null) {
3042 try {
3043 versionCode = Integer.parseInt(version);
3044 } catch (NumberFormatException e) {
3045 }
3046 }
3047 installerPackageName = parser.getAttributeValue(null, "installer");
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003048 volumeUuid = parser.getAttributeValue(null, "volumeUuid");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003049
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003050 systemStr = parser.getAttributeValue(null, "publicFlags");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003051 if (systemStr != null) {
3052 try {
3053 pkgFlags = Integer.parseInt(systemStr);
3054 } catch (NumberFormatException e) {
3055 }
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003056 systemStr = parser.getAttributeValue(null, "privateFlags");
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003057 if (systemStr != null) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003058 try {
3059 pkgPrivateFlags = Integer.parseInt(systemStr);
3060 } catch (NumberFormatException e) {
3061 }
3062 }
3063 } else {
3064 // Pre-M -- both public and private flags were stored in one "flags" field.
3065 systemStr = parser.getAttributeValue(null, "flags");
3066 if (systemStr != null) {
3067 try {
3068 pkgFlags = Integer.parseInt(systemStr);
3069 } catch (NumberFormatException e) {
3070 }
3071 if ((pkgFlags & PRE_M_APP_INFO_FLAG_HIDDEN) != 0) {
3072 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_HIDDEN;
3073 }
3074 if ((pkgFlags & PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE) != 0) {
3075 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE;
3076 }
3077 if ((pkgFlags & PRE_M_APP_INFO_FLAG_FORWARD_LOCK) != 0) {
3078 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK;
3079 }
3080 if ((pkgFlags & PRE_M_APP_INFO_FLAG_PRIVILEGED) != 0) {
3081 pkgPrivateFlags |= ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
3082 }
3083 pkgFlags &= ~(PRE_M_APP_INFO_FLAG_HIDDEN
3084 | PRE_M_APP_INFO_FLAG_CANT_SAVE_STATE
3085 | PRE_M_APP_INFO_FLAG_FORWARD_LOCK
3086 | PRE_M_APP_INFO_FLAG_PRIVILEGED);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003087 } else {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003088 // For backward compatibility
3089 systemStr = parser.getAttributeValue(null, "system");
3090 if (systemStr != null) {
3091 pkgFlags |= ("true".equalsIgnoreCase(systemStr)) ? ApplicationInfo.FLAG_SYSTEM
3092 : 0;
3093 } else {
3094 // Old settings that don't specify system... just treat
3095 // them as system, good enough.
3096 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
3097 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003098 }
3099 }
3100 String timeStampStr = parser.getAttributeValue(null, "ft");
3101 if (timeStampStr != null) {
3102 try {
3103 timeStamp = Long.parseLong(timeStampStr, 16);
3104 } catch (NumberFormatException e) {
3105 }
3106 } else {
3107 timeStampStr = parser.getAttributeValue(null, "ts");
3108 if (timeStampStr != null) {
3109 try {
3110 timeStamp = Long.parseLong(timeStampStr);
3111 } catch (NumberFormatException e) {
3112 }
3113 }
3114 }
3115 timeStampStr = parser.getAttributeValue(null, "it");
3116 if (timeStampStr != null) {
3117 try {
3118 firstInstallTime = Long.parseLong(timeStampStr, 16);
3119 } catch (NumberFormatException e) {
3120 }
3121 }
3122 timeStampStr = parser.getAttributeValue(null, "ut");
3123 if (timeStampStr != null) {
3124 try {
3125 lastUpdateTime = Long.parseLong(timeStampStr, 16);
3126 } catch (NumberFormatException e) {
3127 }
3128 }
3129 if (PackageManagerService.DEBUG_SETTINGS)
3130 Log.v(PackageManagerService.TAG, "Reading package: " + name + " userId=" + idStr
3131 + " sharedUserId=" + sharedIdStr);
3132 int userId = idStr != null ? Integer.parseInt(idStr) : 0;
3133 if (resourcePathStr == null) {
3134 resourcePathStr = codePathStr;
3135 }
3136 if (realName != null) {
3137 realName = realName.intern();
3138 }
3139 if (name == null) {
3140 PackageManagerService.reportSettingsProblem(Log.WARN,
3141 "Error in package manager settings: <package> has no name at "
3142 + parser.getPositionDescription());
3143 } else if (codePathStr == null) {
3144 PackageManagerService.reportSettingsProblem(Log.WARN,
3145 "Error in package manager settings: <package> has no codePath at "
3146 + parser.getPositionDescription());
3147 } else if (userId > 0) {
Kenny Root447106f2011-03-23 11:00:15 -07003148 packageSetting = addPackageLPw(name.intern(), realName, new File(codePathStr),
Narayan Kamathff110bd2014-07-04 18:30:45 +01003149 new File(resourcePathStr), legacyNativeLibraryPathStr, primaryCpuAbiString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003150 secondaryCpuAbiString, cpuAbiOverrideString, userId, versionCode, pkgFlags,
3151 pkgPrivateFlags);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003152 if (PackageManagerService.DEBUG_SETTINGS)
3153 Log.i(PackageManagerService.TAG, "Reading package " + name + ": userId="
3154 + userId + " pkg=" + packageSetting);
3155 if (packageSetting == null) {
3156 PackageManagerService.reportSettingsProblem(Log.ERROR, "Failure adding uid "
3157 + userId + " while parsing settings at "
3158 + parser.getPositionDescription());
3159 } else {
3160 packageSetting.setTimeStamp(timeStamp);
3161 packageSetting.firstInstallTime = firstInstallTime;
3162 packageSetting.lastUpdateTime = lastUpdateTime;
3163 }
3164 } else if (sharedIdStr != null) {
3165 userId = sharedIdStr != null ? Integer.parseInt(sharedIdStr) : 0;
3166 if (userId > 0) {
3167 packageSetting = new PendingPackage(name.intern(), realName, new File(
Narayan Kamathff110bd2014-07-04 18:30:45 +01003168 codePathStr), new File(resourcePathStr), legacyNativeLibraryPathStr,
Narayan Kamath4903f642014-08-11 13:33:45 +01003169 primaryCpuAbiString, secondaryCpuAbiString, cpuAbiOverrideString,
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003170 userId, versionCode, pkgFlags, pkgPrivateFlags);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003171 packageSetting.setTimeStamp(timeStamp);
3172 packageSetting.firstInstallTime = firstInstallTime;
3173 packageSetting.lastUpdateTime = lastUpdateTime;
3174 mPendingPackages.add((PendingPackage) packageSetting);
3175 if (PackageManagerService.DEBUG_SETTINGS)
3176 Log.i(PackageManagerService.TAG, "Reading package " + name
3177 + ": sharedUserId=" + userId + " pkg=" + packageSetting);
3178 } else {
3179 PackageManagerService.reportSettingsProblem(Log.WARN,
3180 "Error in package manager settings: package " + name
3181 + " has bad sharedId " + sharedIdStr + " at "
3182 + parser.getPositionDescription());
3183 }
3184 } else {
3185 PackageManagerService.reportSettingsProblem(Log.WARN,
3186 "Error in package manager settings: package " + name + " has bad userId "
3187 + idStr + " at " + parser.getPositionDescription());
3188 }
3189 } catch (NumberFormatException e) {
3190 PackageManagerService.reportSettingsProblem(Log.WARN,
3191 "Error in package manager settings: package " + name + " has bad userId "
3192 + idStr + " at " + parser.getPositionDescription());
3193 }
3194 if (packageSetting != null) {
3195 packageSetting.uidError = "true".equals(uidError);
3196 packageSetting.installerPackageName = installerPackageName;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003197 packageSetting.volumeUuid = volumeUuid;
Narayan Kamathff110bd2014-07-04 18:30:45 +01003198 packageSetting.legacyNativeLibraryPathString = legacyNativeLibraryPathStr;
3199 packageSetting.primaryCpuAbiString = primaryCpuAbiString;
3200 packageSetting.secondaryCpuAbiString = secondaryCpuAbiString;
Amith Yamasani483f3b02012-03-13 16:08:00 -07003201 // Handle legacy string here for single-user mode
3202 final String enabledStr = parser.getAttributeValue(null, ATTR_ENABLED);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003203 if (enabledStr != null) {
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003204 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003205 packageSetting.setEnabled(Integer.parseInt(enabledStr), 0 /* userId */, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003206 } catch (NumberFormatException e) {
3207 if (enabledStr.equalsIgnoreCase("true")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003208 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_ENABLED, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003209 } else if (enabledStr.equalsIgnoreCase("false")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003210 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DISABLED, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003211 } else if (enabledStr.equalsIgnoreCase("default")) {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003212 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null);
Dianne Hackbornb8f40002011-07-19 15:17:43 -07003213 } else {
3214 PackageManagerService.reportSettingsProblem(Log.WARN,
3215 "Error in package manager settings: package " + name
3216 + " has bad enabled value: " + idStr + " at "
3217 + parser.getPositionDescription());
3218 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003219 }
3220 } else {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003221 packageSetting.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, 0, null);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003222 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003223
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003224 final String installStatusStr = parser.getAttributeValue(null, "installStatus");
3225 if (installStatusStr != null) {
3226 if (installStatusStr.equalsIgnoreCase("false")) {
3227 packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_INCOMPLETE;
3228 } else {
3229 packageSetting.installStatus = PackageSettingBase.PKG_INSTALL_COMPLETE;
3230 }
3231 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003232 int outerDepth = parser.getDepth();
3233 int type;
3234 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3235 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3236 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3237 continue;
3238 }
3239
3240 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003241 // Legacy
3242 if (tagName.equals(TAG_DISABLED_COMPONENTS)) {
3243 readDisabledComponentsLPw(packageSetting, parser, 0);
3244 } else if (tagName.equals(TAG_ENABLED_COMPONENTS)) {
3245 readEnabledComponentsLPw(packageSetting, parser, 0);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003246 } else if (tagName.equals("sigs")) {
3247 packageSetting.signatures.readXml(parser, mPastSignatures);
Svetoslavc6d1c342015-02-26 14:44:43 -08003248 } else if (tagName.equals(TAG_PERMISSIONS)) {
3249 readInstallPermissionsLPr(parser,
3250 packageSetting.getPermissionsState());
Svetoslavcf959f62015-03-26 20:53:34 -07003251 packageSetting.installPermissionsFixed = true;
dcashman3a0dbfb2014-07-07 13:53:36 -07003252 } else if (tagName.equals("proper-signing-keyset")) {
3253 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
dcashman8c04fac2015-03-23 11:39:42 -07003254 Integer refCt = mKeySetRefs.get(id);
3255 if (refCt != null) {
3256 mKeySetRefs.put(id, refCt + 1);
3257 } else {
3258 mKeySetRefs.put(id, 1);
3259 }
dcashman3a0dbfb2014-07-07 13:53:36 -07003260 packageSetting.keySetData.setProperSigningKeySet(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -08003261 } else if (tagName.equals("signing-keyset")) {
dcashman8c04fac2015-03-23 11:39:42 -07003262 // from v1 of keysetmanagerservice - no longer used
dcashman55b10782014-04-09 14:20:38 -07003263 } else if (tagName.equals("upgrade-keyset")) {
3264 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
3265 packageSetting.keySetData.addUpgradeKeySetById(id);
Geremy Condraf1bcca82013-01-07 22:35:24 -08003266 } else if (tagName.equals("defined-keyset")) {
3267 long id = Long.parseLong(parser.getAttributeValue(null, "identifier"));
3268 String alias = parser.getAttributeValue(null, "alias");
dcashman8c04fac2015-03-23 11:39:42 -07003269 Integer refCt = mKeySetRefs.get(id);
3270 if (refCt != null) {
3271 mKeySetRefs.put(id, refCt + 1);
3272 } else {
3273 mKeySetRefs.put(id, 1);
3274 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08003275 packageSetting.keySetData.addDefinedKeySet(id, alias);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08003276 } else if (tagName.equals(TAG_DOMAIN_VERIFICATION)) {
3277 readDomainVerificationLPw(parser, packageSetting);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003278 } else {
3279 PackageManagerService.reportSettingsProblem(Log.WARN,
3280 "Unknown element under <package>: " + parser.getName());
3281 XmlUtils.skipCurrentTag(parser);
3282 }
3283 }
Svet Ganov12a692a2015-03-28 19:34:15 -07003284
3285 // We keep track for which users we granted permissions to be able
3286 // to grant runtime permissions to system apps for newly appeared
3287 // users or newly appeared system apps. If we supported runtime
3288 // permissions during the previous boot, then we already granted
3289 // permissions for all device users. In such a case we set the users
3290 // for which we granted permissions to avoid clobbering of runtime
3291 // permissions we granted to system apps but the user revoked later.
3292 if (!isFirstRuntimePermissionsBoot()) {
3293 final int[] userIds = UserManagerService.getInstance().getUserIds();
3294 packageSetting.setPermissionsUpdatedForUserIds(userIds);
3295 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003296 } else {
3297 XmlUtils.skipCurrentTag(parser);
3298 }
3299 }
3300
Amith Yamasani483f3b02012-03-13 16:08:00 -07003301 private void readDisabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser,
3302 int userId) throws IOException, XmlPullParserException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003303 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
3311 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003312 if (tagName.equals(TAG_ITEM)) {
3313 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003314 if (name != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003315 packageSetting.addDisabledComponent(name.intern(), userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003316 } else {
3317 PackageManagerService.reportSettingsProblem(Log.WARN,
3318 "Error in package manager settings: <disabled-components> has"
3319 + " no name at " + parser.getPositionDescription());
3320 }
3321 } else {
3322 PackageManagerService.reportSettingsProblem(Log.WARN,
3323 "Unknown element under <disabled-components>: " + parser.getName());
3324 }
3325 XmlUtils.skipCurrentTag(parser);
3326 }
3327 }
3328
Amith Yamasani483f3b02012-03-13 16:08:00 -07003329 private void readEnabledComponentsLPw(PackageSettingBase packageSetting, XmlPullParser parser,
3330 int userId) throws IOException, XmlPullParserException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003331 int outerDepth = parser.getDepth();
3332 int type;
3333 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3334 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3335 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3336 continue;
3337 }
3338
3339 String tagName = parser.getName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003340 if (tagName.equals(TAG_ITEM)) {
3341 String name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003342 if (name != null) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003343 packageSetting.addEnabledComponent(name.intern(), userId);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003344 } else {
3345 PackageManagerService.reportSettingsProblem(Log.WARN,
3346 "Error in package manager settings: <enabled-components> has"
3347 + " no name at " + parser.getPositionDescription());
3348 }
3349 } else {
3350 PackageManagerService.reportSettingsProblem(Log.WARN,
3351 "Unknown element under <enabled-components>: " + parser.getName());
3352 }
3353 XmlUtils.skipCurrentTag(parser);
3354 }
3355 }
3356
Amith Yamasani483f3b02012-03-13 16:08:00 -07003357 private void readSharedUserLPw(XmlPullParser parser) throws XmlPullParserException,IOException {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003358 String name = null;
3359 String idStr = null;
3360 int pkgFlags = 0;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003361 int pkgPrivateFlags = 0;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003362 SharedUserSetting su = null;
3363 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003364 name = parser.getAttributeValue(null, ATTR_NAME);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003365 idStr = parser.getAttributeValue(null, "userId");
3366 int userId = idStr != null ? Integer.parseInt(idStr) : 0;
3367 if ("true".equals(parser.getAttributeValue(null, "system"))) {
3368 pkgFlags |= ApplicationInfo.FLAG_SYSTEM;
3369 }
3370 if (name == null) {
3371 PackageManagerService.reportSettingsProblem(Log.WARN,
3372 "Error in package manager settings: <shared-user> has no name at "
3373 + parser.getPositionDescription());
3374 } else if (userId == 0) {
3375 PackageManagerService.reportSettingsProblem(Log.WARN,
3376 "Error in package manager settings: shared-user " + name
3377 + " has bad userId " + idStr + " at "
3378 + parser.getPositionDescription());
3379 } else {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003380 if ((su = addSharedUserLPw(name.intern(), userId, pkgFlags, pkgPrivateFlags))
3381 == null) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003382 PackageManagerService
3383 .reportSettingsProblem(Log.ERROR, "Occurred while parsing settings at "
3384 + parser.getPositionDescription());
3385 }
3386 }
3387 } catch (NumberFormatException e) {
3388 PackageManagerService.reportSettingsProblem(Log.WARN,
3389 "Error in package manager settings: package " + name + " has bad userId "
3390 + idStr + " at " + parser.getPositionDescription());
3391 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003392
3393 if (su != null) {
3394 int outerDepth = parser.getDepth();
3395 int type;
3396 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3397 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3398 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3399 continue;
3400 }
3401
3402 String tagName = parser.getName();
3403 if (tagName.equals("sigs")) {
3404 su.signatures.readXml(parser, mPastSignatures);
3405 } else if (tagName.equals("perms")) {
Svetoslavc6d1c342015-02-26 14:44:43 -08003406 readInstallPermissionsLPr(parser, su.getPermissionsState());
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003407 } else {
3408 PackageManagerService.reportSettingsProblem(Log.WARN,
3409 "Unknown element under <shared-user>: " + parser.getName());
3410 XmlUtils.skipCurrentTag(parser);
3411 }
3412 }
Svet Ganov12a692a2015-03-28 19:34:15 -07003413
3414 // We keep track for which users we granted permissions to be able
3415 // to grant runtime permissions to system apps for newly appeared
3416 // users or newly appeared system apps. If we supported runtime
3417 // permissions during the previous boot, then we already granted
3418 // permissions for all device users. In such a case we set the users
3419 // for which we granted permissions to avoid clobbering of runtime
3420 // permissions we granted to system apps but the user revoked later.
3421 if (!isFirstRuntimePermissionsBoot()) {
3422 final int[] userIds = UserManagerService.getInstance().getUserIds();
3423 su.setPermissionsUpdatedForUserIds(userIds);
3424 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003425 } else {
3426 XmlUtils.skipCurrentTag(parser);
3427 }
3428 }
3429
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003430 void createNewUserLILPw(PackageManagerService service, Installer installer,
3431 int userHandle, File path) {
Dianne Hackborn63092712012-10-07 14:45:35 -07003432 path.mkdir();
3433 FileUtils.setPermissions(path.toString(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
3434 | FileUtils.S_IXOTH, -1, -1);
3435 for (PackageSetting ps : mPackages.values()) {
Amith Yamasani41cd5772014-07-10 15:26:00 -07003436 if (ps.pkg == null || ps.pkg.applicationInfo == null) {
3437 continue;
3438 }
Dianne Hackborn63092712012-10-07 14:45:35 -07003439 // Only system apps are initially installed.
3440 ps.setInstalled((ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) != 0, userHandle);
3441 // Need to create a data directory for all apps under this user.
Jeff Sharkey529f91f2015-04-18 20:23:13 -07003442 installer.createUserData(ps.volumeUuid, ps.name,
Robert Craig8643dc62013-07-29 09:06:51 -04003443 UserHandle.getUid(userHandle, ps.appId), userHandle,
3444 ps.pkg.applicationInfo.seinfo);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003445 }
Dianne Hackborn8a2ed1d2013-01-29 15:18:29 -08003446 readDefaultPreferredAppsLPw(service, userHandle);
Dianne Hackborn63092712012-10-07 14:45:35 -07003447 writePackageRestrictionsLPr(userHandle);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003448 }
3449
Alexandra Gherghina539a7ef2014-07-07 12:27:54 +01003450 void removeUserLPw(int userId) {
Dianne Hackborn63092712012-10-07 14:45:35 -07003451 Set<Entry<String, PackageSetting>> entries = mPackages.entrySet();
3452 for (Entry<String, PackageSetting> entry : entries) {
3453 entry.getValue().removeUser(userId);
3454 }
3455 mPreferredActivities.remove(userId);
Amith Yamasani13593602012-03-22 16:16:17 -07003456 File file = getUserPackagesStateFile(userId);
3457 file.delete();
3458 file = getUserPackagesStateBackupFile(userId);
3459 file.delete();
Nicolas Prevote7024042014-07-08 15:47:17 +01003460 removeCrossProfileIntentFiltersLPw(userId);
Svetoslavc6d1c342015-02-26 14:44:43 -08003461
3462 mRuntimePermissionsPersistence.onUserRemoved(userId);
Nicolas Prevota0f48852014-05-27 11:21:11 +01003463 }
3464
Nicolas Prevote7024042014-07-08 15:47:17 +01003465 void removeCrossProfileIntentFiltersLPw(int userId) {
3466 synchronized (mCrossProfileIntentResolvers) {
3467 // userId is the source user
3468 if (mCrossProfileIntentResolvers.get(userId) != null) {
3469 mCrossProfileIntentResolvers.remove(userId);
3470 writePackageRestrictionsLPr(userId);
Nicolas Prevota0f48852014-05-27 11:21:11 +01003471 }
Nicolas Prevote7024042014-07-08 15:47:17 +01003472 // userId is the target user
3473 int count = mCrossProfileIntentResolvers.size();
3474 for (int i = 0; i < count; i++) {
3475 int sourceUserId = mCrossProfileIntentResolvers.keyAt(i);
3476 CrossProfileIntentResolver cpir = mCrossProfileIntentResolvers.get(sourceUserId);
3477 boolean needsWriting = false;
Jeff Sharkey9f837a92014-10-24 12:07:24 -07003478 ArraySet<CrossProfileIntentFilter> cpifs =
3479 new ArraySet<CrossProfileIntentFilter>(cpir.filterSet());
Nicolas Prevote7024042014-07-08 15:47:17 +01003480 for (CrossProfileIntentFilter cpif : cpifs) {
3481 if (cpif.getTargetUserId() == userId) {
3482 needsWriting = true;
3483 cpir.removeFilter(cpif);
3484 }
3485 }
3486 if (needsWriting) {
3487 writePackageRestrictionsLPr(sourceUserId);
3488 }
Nicolas Prevota0f48852014-05-27 11:21:11 +01003489 }
3490 }
Amith Yamasani13593602012-03-22 16:16:17 -07003491 }
3492
Geremy Condra12c18382013-03-06 16:49:06 -08003493 // This should be called (at least) whenever an application is removed
3494 private void setFirstAvailableUid(int uid) {
3495 if (uid > mFirstAvailableUid) {
3496 mFirstAvailableUid = uid;
3497 }
3498 }
3499
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003500 // Returns -1 if we could not find an available UserId to assign
Kenny Root447106f2011-03-23 11:00:15 -07003501 private int newUserIdLPw(Object obj) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003502 // Let's be stupidly inefficient for now...
3503 final int N = mUserIds.size();
Geremy Condra12c18382013-03-06 16:49:06 -08003504 for (int i = mFirstAvailableUid; i < N; i++) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003505 if (mUserIds.get(i) == null) {
3506 mUserIds.set(i, obj);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08003507 return Process.FIRST_APPLICATION_UID + i;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003508 }
3509 }
3510
3511 // None left?
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08003512 if (N > (Process.LAST_APPLICATION_UID-Process.FIRST_APPLICATION_UID)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003513 return -1;
3514 }
3515
3516 mUserIds.add(obj);
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08003517 return Process.FIRST_APPLICATION_UID + N;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003518 }
3519
Kenny Root0aaa0d92011-09-12 16:42:55 -07003520 public VerifierDeviceIdentity getVerifierDeviceIdentityLPw() {
3521 if (mVerifierDeviceIdentity == null) {
3522 mVerifierDeviceIdentity = VerifierDeviceIdentity.generate();
3523
3524 writeLPr();
3525 }
3526
3527 return mVerifierDeviceIdentity;
3528 }
3529
Kenny Root447106f2011-03-23 11:00:15 -07003530 public PackageSetting getDisabledSystemPkgLPr(String name) {
3531 PackageSetting ps = mDisabledSysPackages.get(name);
3532 return ps;
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003533 }
3534
Jeff Sharkey9f837a92014-10-24 12:07:24 -07003535 private String compToString(ArraySet<String> cmp) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003536 return cmp != null ? Arrays.toString(cmp.toArray()) : "[]";
3537 }
3538
Amith Yamasani483f3b02012-03-13 16:08:00 -07003539 boolean isEnabledLPr(ComponentInfo componentInfo, int flags, int userId) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003540 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
3541 return true;
3542 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003543 final String pkgName = componentInfo.packageName;
3544 final PackageSetting packageSettings = mPackages.get(pkgName);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003545 if (PackageManagerService.DEBUG_SETTINGS) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003546 Log.v(PackageManagerService.TAG, "isEnabledLock - packageName = "
3547 + componentInfo.packageName + " componentName = " + componentInfo.name);
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003548 Log.v(PackageManagerService.TAG, "enabledComponents: "
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003549 + compToString(packageSettings.getEnabledComponents(userId)));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003550 Log.v(PackageManagerService.TAG, "disabledComponents: "
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003551 + compToString(packageSettings.getDisabledComponents(userId)));
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003552 }
3553 if (packageSettings == null) {
3554 return false;
3555 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003556 PackageUserState ustate = packageSettings.readUserState(userId);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003557 if ((flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) != 0) {
3558 if (ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
3559 return true;
3560 }
3561 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003562 if (ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED
3563 || ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_USER
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003564 || ustate.enabled == COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003565 || (packageSettings.pkg != null && !packageSettings.pkg.applicationInfo.enabled
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003566 && ustate.enabled == COMPONENT_ENABLED_STATE_DEFAULT)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003567 return false;
3568 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003569 if (ustate.enabledComponents != null
3570 && ustate.enabledComponents.contains(componentInfo.name)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003571 return true;
3572 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003573 if (ustate.disabledComponents != null
3574 && ustate.disabledComponents.contains(componentInfo.name)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -07003575 return false;
3576 }
3577 return componentInfo.enabled;
3578 }
Kenny Root447106f2011-03-23 11:00:15 -07003579
3580 String getInstallerPackageNameLPr(String packageName) {
3581 final PackageSetting pkg = mPackages.get(packageName);
3582 if (pkg == null) {
3583 throw new IllegalArgumentException("Unknown package: " + packageName);
3584 }
3585 return pkg.installerPackageName;
3586 }
3587
Amith Yamasani483f3b02012-03-13 16:08:00 -07003588 int getApplicationEnabledSettingLPr(String packageName, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -07003589 final PackageSetting pkg = mPackages.get(packageName);
3590 if (pkg == null) {
3591 throw new IllegalArgumentException("Unknown package: " + packageName);
3592 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003593 return pkg.getEnabled(userId);
Kenny Root447106f2011-03-23 11:00:15 -07003594 }
3595
Amith Yamasani483f3b02012-03-13 16:08:00 -07003596 int getComponentEnabledSettingLPr(ComponentName componentName, int userId) {
Kenny Root447106f2011-03-23 11:00:15 -07003597 final String packageName = componentName.getPackageName();
3598 final PackageSetting pkg = mPackages.get(packageName);
3599 if (pkg == null) {
3600 throw new IllegalArgumentException("Unknown component: " + componentName);
3601 }
3602 final String classNameStr = componentName.getClassName();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003603 return pkg.getCurrentEnabledStateLPr(classNameStr, userId);
Kenny Root447106f2011-03-23 11:00:15 -07003604 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003605
Kenny Root447106f2011-03-23 11:00:15 -07003606 boolean setPackageStoppedStateLPw(String packageName, boolean stopped,
Amith Yamasani483f3b02012-03-13 16:08:00 -07003607 boolean allowedByPermission, int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003608 int appId = UserHandle.getAppId(uid);
Kenny Root447106f2011-03-23 11:00:15 -07003609 final PackageSetting pkgSetting = mPackages.get(packageName);
3610 if (pkgSetting == null) {
3611 throw new IllegalArgumentException("Unknown package: " + packageName);
3612 }
Amith Yamasani13593602012-03-22 16:16:17 -07003613 if (!allowedByPermission && (appId != pkgSetting.appId)) {
Kenny Root447106f2011-03-23 11:00:15 -07003614 throw new SecurityException(
3615 "Permission Denial: attempt to change stopped state from pid="
3616 + Binder.getCallingPid()
Amith Yamasani13593602012-03-22 16:16:17 -07003617 + ", uid=" + uid + ", package uid=" + pkgSetting.appId);
Kenny Root447106f2011-03-23 11:00:15 -07003618 }
3619 if (DEBUG_STOPPED) {
3620 if (stopped) {
3621 RuntimeException e = new RuntimeException("here");
3622 e.fillInStackTrace();
3623 Slog.i(TAG, "Stopping package " + packageName, e);
3624 }
3625 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003626 if (pkgSetting.getStopped(userId) != stopped) {
3627 pkgSetting.setStopped(stopped, userId);
3628 // pkgSetting.pkg.mSetStopped = stopped;
3629 if (pkgSetting.getNotLaunched(userId)) {
Kenny Root447106f2011-03-23 11:00:15 -07003630 if (pkgSetting.installerPackageName != null) {
3631 PackageManagerService.sendPackageBroadcast(Intent.ACTION_PACKAGE_FIRST_LAUNCH,
3632 pkgSetting.name, null,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003633 pkgSetting.installerPackageName, null, new int[] {userId});
Kenny Root447106f2011-03-23 11:00:15 -07003634 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003635 pkgSetting.setNotLaunched(false, userId);
Kenny Root447106f2011-03-23 11:00:15 -07003636 }
3637 return true;
3638 }
3639 return false;
3640 }
3641
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08003642 List<UserInfo> getAllUsers() {
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07003643 long id = Binder.clearCallingIdentity();
Amith Yamasani483f3b02012-03-13 16:08:00 -07003644 try {
Amith Yamasani920ace02012-09-20 22:15:37 -07003645 return UserManagerService.getInstance().getUsers(false);
Amith Yamasani483f3b02012-03-13 16:08:00 -07003646 } catch (NullPointerException npe) {
3647 // packagemanager not yet initialized
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07003648 } finally {
3649 Binder.restoreCallingIdentity(id);
Amith Yamasani483f3b02012-03-13 16:08:00 -07003650 }
3651 return null;
3652 }
3653
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003654 /**
3655 * Return all {@link PackageSetting} that are actively installed on the
3656 * given {@link VolumeInfo#fsUuid}.
3657 */
3658 List<PackageSetting> getVolumePackagesLPr(String volumeUuid) {
3659 Preconditions.checkNotNull(volumeUuid);
3660 ArrayList<PackageSetting> res = new ArrayList<>();
3661 for (int i = 0; i < mPackages.size(); i++) {
3662 final PackageSetting setting = mPackages.valueAt(i);
3663 if (Objects.equals(volumeUuid, setting.volumeUuid)) {
3664 res.add(setting);
3665 }
3666 }
3667 return res;
3668 }
3669
Svetoslavc6d1c342015-02-26 14:44:43 -08003670 static void printFlags(PrintWriter pw, int val, Object[] spec) {
Joe Onorato20963df2012-01-04 18:13:24 -08003671 pw.print("[ ");
3672 for (int i=0; i<spec.length; i+=2) {
3673 int mask = (Integer)spec[i];
3674 if ((val & mask) != 0) {
3675 pw.print(spec[i+1]);
3676 pw.print(" ");
3677 }
3678 }
3679 pw.print("]");
3680 }
3681
3682 static final Object[] FLAG_DUMP_SPEC = new Object[] {
3683 ApplicationInfo.FLAG_SYSTEM, "SYSTEM",
3684 ApplicationInfo.FLAG_DEBUGGABLE, "DEBUGGABLE",
3685 ApplicationInfo.FLAG_HAS_CODE, "HAS_CODE",
3686 ApplicationInfo.FLAG_PERSISTENT, "PERSISTENT",
3687 ApplicationInfo.FLAG_FACTORY_TEST, "FACTORY_TEST",
3688 ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING, "ALLOW_TASK_REPARENTING",
3689 ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA, "ALLOW_CLEAR_USER_DATA",
3690 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP, "UPDATED_SYSTEM_APP",
3691 ApplicationInfo.FLAG_TEST_ONLY, "TEST_ONLY",
3692 ApplicationInfo.FLAG_VM_SAFE_MODE, "VM_SAFE_MODE",
3693 ApplicationInfo.FLAG_ALLOW_BACKUP, "ALLOW_BACKUP",
3694 ApplicationInfo.FLAG_KILL_AFTER_RESTORE, "KILL_AFTER_RESTORE",
3695 ApplicationInfo.FLAG_RESTORE_ANY_VERSION, "RESTORE_ANY_VERSION",
3696 ApplicationInfo.FLAG_EXTERNAL_STORAGE, "EXTERNAL_STORAGE",
3697 ApplicationInfo.FLAG_LARGE_HEAP, "LARGE_HEAP",
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003698 };
3699
3700 static final Object[] PRIVATE_FLAG_DUMP_SPEC = new Object[] {
3701 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED, "PRIVILEGED",
3702 ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK, "FORWARD_LOCK",
3703 ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE, "CANT_SAVE_STATE",
Joe Onorato20963df2012-01-04 18:13:24 -08003704 };
3705
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003706 void dumpPackageLPr(PrintWriter pw, String prefix, String checkinTag, PackageSetting ps,
3707 SimpleDateFormat sdf, Date date, List<UserInfo> users) {
3708 if (checkinTag != null) {
3709 pw.print(checkinTag);
3710 pw.print(",");
3711 pw.print(ps.realName != null ? ps.realName : ps.name);
3712 pw.print(",");
3713 pw.print(ps.appId);
3714 pw.print(",");
3715 pw.print(ps.versionCode);
3716 pw.print(",");
3717 pw.print(ps.firstInstallTime);
3718 pw.print(",");
3719 pw.print(ps.lastUpdateTime);
3720 pw.print(",");
3721 pw.print(ps.installerPackageName != null ? ps.installerPackageName : "?");
3722 pw.println();
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08003723 if (ps.pkg != null) {
3724 pw.print(checkinTag); pw.print("-"); pw.print("splt,");
3725 pw.print("base,");
3726 pw.println(ps.pkg.baseRevisionCode);
3727 if (ps.pkg.splitNames != null) {
3728 for (int i = 0; i < ps.pkg.splitNames.length; i++) {
3729 pw.print(checkinTag); pw.print("-"); pw.print("splt,");
3730 pw.print(ps.pkg.splitNames[i]); pw.print(",");
3731 pw.println(ps.pkg.splitRevisionCodes[i]);
3732 }
3733 }
3734 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003735 for (UserInfo user : users) {
3736 pw.print(checkinTag);
3737 pw.print("-");
3738 pw.print("usr");
3739 pw.print(",");
3740 pw.print(user.id);
3741 pw.print(",");
3742 pw.print(ps.getInstalled(user.id) ? "I" : "i");
Amith Yamasanie5bcff62014-07-19 15:44:09 -07003743 pw.print(ps.getHidden(user.id) ? "B" : "b");
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003744 pw.print(ps.getStopped(user.id) ? "S" : "s");
3745 pw.print(ps.getNotLaunched(user.id) ? "l" : "L");
3746 pw.print(",");
3747 pw.print(ps.getEnabled(user.id));
3748 String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id);
3749 pw.print(",");
3750 pw.print(lastDisabledAppCaller != null ? lastDisabledAppCaller : "?");
3751 pw.println();
3752 }
3753 return;
3754 }
3755
Dianne Hackbornc895be72013-03-11 17:48:43 -07003756 pw.print(prefix); pw.print("Package [");
3757 pw.print(ps.realName != null ? ps.realName : ps.name);
3758 pw.print("] (");
3759 pw.print(Integer.toHexString(System.identityHashCode(ps)));
3760 pw.println("):");
3761
3762 if (ps.realName != null) {
3763 pw.print(prefix); pw.print(" compat name=");
3764 pw.println(ps.name);
3765 }
3766
Svetoslavc6d1c342015-02-26 14:44:43 -08003767 pw.print(prefix); pw.print(" userId="); pw.println(ps.appId);
3768
Dianne Hackbornc895be72013-03-11 17:48:43 -07003769 if (ps.sharedUser != null) {
3770 pw.print(prefix); pw.print(" sharedUser="); pw.println(ps.sharedUser);
3771 }
3772 pw.print(prefix); pw.print(" pkg="); pw.println(ps.pkg);
3773 pw.print(prefix); pw.print(" codePath="); pw.println(ps.codePathString);
3774 pw.print(prefix); pw.print(" resourcePath="); pw.println(ps.resourcePathString);
Narayan Kamathff110bd2014-07-04 18:30:45 +01003775 pw.print(prefix); pw.print(" legacyNativeLibraryDir="); pw.println(ps.legacyNativeLibraryPathString);
3776 pw.print(prefix); pw.print(" primaryCpuAbi="); pw.println(ps.primaryCpuAbiString);
3777 pw.print(prefix); pw.print(" secondaryCpuAbi="); pw.println(ps.secondaryCpuAbiString);
Dianne Hackbornc895be72013-03-11 17:48:43 -07003778 pw.print(prefix); pw.print(" versionCode="); pw.print(ps.versionCode);
3779 if (ps.pkg != null) {
3780 pw.print(" targetSdk="); pw.print(ps.pkg.applicationInfo.targetSdkVersion);
3781 }
3782 pw.println();
3783 if (ps.pkg != null) {
3784 pw.print(prefix); pw.print(" versionName="); pw.println(ps.pkg.mVersionName);
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08003785 pw.print(prefix); pw.print(" splits="); dumpSplitNames(pw, ps.pkg); pw.println();
Dianne Hackbornc895be72013-03-11 17:48:43 -07003786 pw.print(prefix); pw.print(" applicationInfo=");
3787 pw.println(ps.pkg.applicationInfo.toString());
3788 pw.print(prefix); pw.print(" flags="); printFlags(pw, ps.pkg.applicationInfo.flags,
3789 FLAG_DUMP_SPEC); pw.println();
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003790 pw.print(prefix); pw.print(" priavateFlags="); printFlags(pw,
3791 ps.pkg.applicationInfo.privateFlags, PRIVATE_FLAG_DUMP_SPEC); pw.println();
Dianne Hackbornc895be72013-03-11 17:48:43 -07003792 pw.print(prefix); pw.print(" dataDir="); pw.println(ps.pkg.applicationInfo.dataDir);
3793 if (ps.pkg.mOperationPending) {
3794 pw.print(prefix); pw.println(" mOperationPending=true");
3795 }
3796 pw.print(prefix); pw.print(" supportsScreens=[");
3797 boolean first = true;
3798 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS) != 0) {
3799 if (!first)
3800 pw.print(", ");
3801 first = false;
3802 pw.print("small");
3803 }
3804 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS) != 0) {
3805 if (!first)
3806 pw.print(", ");
3807 first = false;
3808 pw.print("medium");
3809 }
3810 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0) {
3811 if (!first)
3812 pw.print(", ");
3813 first = false;
3814 pw.print("large");
3815 }
3816 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS) != 0) {
3817 if (!first)
3818 pw.print(", ");
3819 first = false;
3820 pw.print("xlarge");
3821 }
3822 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS) != 0) {
3823 if (!first)
3824 pw.print(", ");
3825 first = false;
3826 pw.print("resizeable");
3827 }
3828 if ((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES) != 0) {
3829 if (!first)
3830 pw.print(", ");
3831 first = false;
3832 pw.print("anyDensity");
3833 }
3834 pw.println("]");
3835 if (ps.pkg.libraryNames != null && ps.pkg.libraryNames.size() > 0) {
3836 pw.print(prefix); pw.println(" libraries:");
3837 for (int i=0; i<ps.pkg.libraryNames.size(); i++) {
3838 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.libraryNames.get(i));
3839 }
3840 }
3841 if (ps.pkg.usesLibraries != null && ps.pkg.usesLibraries.size() > 0) {
3842 pw.print(prefix); pw.println(" usesLibraries:");
3843 for (int i=0; i<ps.pkg.usesLibraries.size(); i++) {
3844 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.usesLibraries.get(i));
3845 }
3846 }
3847 if (ps.pkg.usesOptionalLibraries != null
3848 && ps.pkg.usesOptionalLibraries.size() > 0) {
3849 pw.print(prefix); pw.println(" usesOptionalLibraries:");
3850 for (int i=0; i<ps.pkg.usesOptionalLibraries.size(); i++) {
3851 pw.print(prefix); pw.print(" ");
3852 pw.println(ps.pkg.usesOptionalLibraries.get(i));
3853 }
3854 }
3855 if (ps.pkg.usesLibraryFiles != null
3856 && ps.pkg.usesLibraryFiles.length > 0) {
3857 pw.print(prefix); pw.println(" usesLibraryFiles:");
3858 for (int i=0; i<ps.pkg.usesLibraryFiles.length; i++) {
3859 pw.print(prefix); pw.print(" "); pw.println(ps.pkg.usesLibraryFiles[i]);
3860 }
3861 }
3862 }
3863 pw.print(prefix); pw.print(" timeStamp=");
3864 date.setTime(ps.timeStamp);
3865 pw.println(sdf.format(date));
3866 pw.print(prefix); pw.print(" firstInstallTime=");
3867 date.setTime(ps.firstInstallTime);
3868 pw.println(sdf.format(date));
3869 pw.print(prefix); pw.print(" lastUpdateTime=");
3870 date.setTime(ps.lastUpdateTime);
3871 pw.println(sdf.format(date));
3872 if (ps.installerPackageName != null) {
3873 pw.print(prefix); pw.print(" installerPackageName=");
3874 pw.println(ps.installerPackageName);
3875 }
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07003876 if (ps.volumeUuid != null) {
3877 pw.print(prefix); pw.print(" volumeUuid=");
3878 pw.println(ps.volumeUuid);
3879 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07003880 pw.print(prefix); pw.print(" signatures="); pw.println(ps.signatures);
Svetoslavcf959f62015-03-26 20:53:34 -07003881 pw.print(prefix); pw.print(" installPermissionsFixed=");
3882 pw.print(ps.installPermissionsFixed);
Dianne Hackbornc895be72013-03-11 17:48:43 -07003883 pw.print(" installStatus="); pw.println(ps.installStatus);
3884 pw.print(prefix); pw.print(" pkgFlags="); printFlags(pw, ps.pkgFlags, FLAG_DUMP_SPEC);
3885 pw.println();
Svetoslavc6d1c342015-02-26 14:44:43 -08003886
3887 if (ps.sharedUser == null) {
3888 PermissionsState permissionsState = ps.getPermissionsState();
3889 dumpInstallPermissionsLPr(pw, prefix + " ", permissionsState);
3890 }
3891
Dianne Hackbornc895be72013-03-11 17:48:43 -07003892 for (UserInfo user : users) {
3893 pw.print(prefix); pw.print(" User "); pw.print(user.id); pw.print(": ");
3894 pw.print(" installed=");
3895 pw.print(ps.getInstalled(user.id));
Amith Yamasanie5bcff62014-07-19 15:44:09 -07003896 pw.print(" hidden=");
3897 pw.print(ps.getHidden(user.id));
Dianne Hackbornc895be72013-03-11 17:48:43 -07003898 pw.print(" stopped=");
3899 pw.print(ps.getStopped(user.id));
3900 pw.print(" notLaunched=");
3901 pw.print(ps.getNotLaunched(user.id));
3902 pw.print(" enabled=");
3903 pw.println(ps.getEnabled(user.id));
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07003904 String lastDisabledAppCaller = ps.getLastDisabledAppCaller(user.id);
3905 if (lastDisabledAppCaller != null) {
3906 pw.print(prefix); pw.print(" lastDisabledCaller: ");
3907 pw.println(lastDisabledAppCaller);
3908 }
Svetoslavc6d1c342015-02-26 14:44:43 -08003909
3910 if (ps.sharedUser == null) {
3911 PermissionsState permissionsState = ps.getPermissionsState();
3912 dumpGidsLPr(pw, prefix + " ", permissionsState.computeGids(user.id));
3913 dumpRuntimePermissionsLPr(pw, prefix + " ", permissionsState
3914 .getRuntimePermissions(user.id));
3915 }
3916
Jeff Sharkey9f837a92014-10-24 12:07:24 -07003917 ArraySet<String> cmp = ps.getDisabledComponents(user.id);
Dianne Hackbornc895be72013-03-11 17:48:43 -07003918 if (cmp != null && cmp.size() > 0) {
3919 pw.print(prefix); pw.println(" disabledComponents:");
3920 for (String s : cmp) {
3921 pw.print(prefix); pw.print(" "); pw.println(s);
3922 }
3923 }
3924 cmp = ps.getEnabledComponents(user.id);
3925 if (cmp != null && cmp.size() > 0) {
3926 pw.print(prefix); pw.println(" enabledComponents:");
3927 for (String s : cmp) {
3928 pw.print(prefix); pw.print(" "); pw.println(s);
3929 }
3930 }
3931 }
Dianne Hackbornc895be72013-03-11 17:48:43 -07003932 }
3933
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003934 void dumpPackagesLPr(PrintWriter pw, String packageName, DumpState dumpState, boolean checkin) {
Kenny Root447106f2011-03-23 11:00:15 -07003935 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
3936 final Date date = new Date();
3937 boolean printedSomething = false;
Amith Yamasani7ea3e7d2012-04-20 15:19:35 -07003938 List<UserInfo> users = getAllUsers();
Kenny Root447106f2011-03-23 11:00:15 -07003939 for (final PackageSetting ps : mPackages.values()) {
3940 if (packageName != null && !packageName.equals(ps.realName)
3941 && !packageName.equals(ps.name)) {
3942 continue;
3943 }
3944
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003945 if (!checkin && packageName != null) {
Kenny Root447106f2011-03-23 11:00:15 -07003946 dumpState.setSharedUser(ps.sharedUser);
3947 }
3948
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003949 if (!checkin && !printedSomething) {
Kenny Root447106f2011-03-23 11:00:15 -07003950 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003951 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07003952 pw.println("Packages:");
3953 printedSomething = true;
3954 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003955 dumpPackageLPr(pw, " ", checkin ? "pkg" : null, ps, sdf, date, users);
Kenny Root447106f2011-03-23 11:00:15 -07003956 }
3957
3958 printedSomething = false;
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003959 if (!checkin && mRenamedPackages.size() > 0) {
Andy McFadden2f362292012-01-20 14:43:38 -08003960 for (final Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
Kenny Root447106f2011-03-23 11:00:15 -07003961 if (packageName != null && !packageName.equals(e.getKey())
3962 && !packageName.equals(e.getValue())) {
3963 continue;
3964 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003965 if (!checkin) {
3966 if (!printedSomething) {
3967 if (dumpState.onTitlePrinted())
3968 pw.println();
3969 pw.println("Renamed packages:");
3970 printedSomething = true;
3971 }
3972 pw.print(" ");
3973 } else {
3974 pw.print("ren,");
Kenny Root447106f2011-03-23 11:00:15 -07003975 }
Kenny Root447106f2011-03-23 11:00:15 -07003976 pw.print(e.getKey());
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003977 pw.print(checkin ? " -> " : ",");
Kenny Root447106f2011-03-23 11:00:15 -07003978 pw.println(e.getValue());
3979 }
3980 }
3981
3982 printedSomething = false;
3983 if (mDisabledSysPackages.size() > 0) {
3984 for (final PackageSetting ps : mDisabledSysPackages.values()) {
3985 if (packageName != null && !packageName.equals(ps.realName)
3986 && !packageName.equals(ps.name)) {
3987 continue;
3988 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003989 if (!checkin && !printedSomething) {
Kenny Root447106f2011-03-23 11:00:15 -07003990 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003991 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07003992 pw.println("Hidden system packages:");
3993 printedSomething = true;
3994 }
Dianne Hackborn60dc0d92014-01-17 13:29:21 -08003995 dumpPackageLPr(pw, " ", checkin ? "dis" : null, ps, sdf, date, users);
Kenny Root447106f2011-03-23 11:00:15 -07003996 }
3997 }
3998 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07003999
Kenny Root447106f2011-03-23 11:00:15 -07004000 void dumpPermissionsLPr(PrintWriter pw, String packageName, DumpState dumpState) {
4001 boolean printedSomething = false;
4002 for (BasePermission p : mPermissions.values()) {
4003 if (packageName != null && !packageName.equals(p.sourcePackage)) {
4004 continue;
4005 }
4006 if (!printedSomething) {
4007 if (dumpState.onTitlePrinted())
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004008 pw.println();
Kenny Root447106f2011-03-23 11:00:15 -07004009 pw.println("Permissions:");
4010 printedSomething = true;
4011 }
4012 pw.print(" Permission ["); pw.print(p.name); pw.print("] (");
4013 pw.print(Integer.toHexString(System.identityHashCode(p)));
4014 pw.println("):");
4015 pw.print(" sourcePackage="); pw.println(p.sourcePackage);
4016 pw.print(" uid="); pw.print(p.uid);
Jeff Sharkey00f39042015-03-23 16:51:22 -07004017 pw.print(" gids="); pw.print(Arrays.toString(
4018 p.computeGids(UserHandle.USER_OWNER)));
Kenny Root447106f2011-03-23 11:00:15 -07004019 pw.print(" type="); pw.print(p.type);
Dianne Hackborne639da72012-02-21 15:11:13 -08004020 pw.print(" prot=");
4021 pw.println(PermissionInfo.protectionToString(p.protectionLevel));
Kenny Root447106f2011-03-23 11:00:15 -07004022 if (p.packageSetting != null) {
4023 pw.print(" packageSetting="); pw.println(p.packageSetting);
4024 }
4025 if (p.perm != null) {
4026 pw.print(" perm="); pw.println(p.perm);
4027 }
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004028 if (READ_EXTERNAL_STORAGE.equals(p.name)) {
Jeff Sharkey5d32e772012-04-12 15:59:23 -07004029 pw.print(" enforced=");
4030 pw.println(mReadExternalStorageEnforced);
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004031 }
Kenny Root447106f2011-03-23 11:00:15 -07004032 }
4033 }
Jeff Sharkey1c27576a2012-04-11 19:07:08 -07004034
Dianne Hackbornd052a942014-11-21 15:23:13 -08004035 void dumpSharedUsersLPr(PrintWriter pw, String packageName, DumpState dumpState,
4036 boolean checkin) {
Kenny Root447106f2011-03-23 11:00:15 -07004037 boolean printedSomething = false;
4038 for (SharedUserSetting su : mSharedUsers.values()) {
4039 if (packageName != null && su != dumpState.getSharedUser()) {
4040 continue;
4041 }
Dianne Hackbornd052a942014-11-21 15:23:13 -08004042 if (!checkin) {
4043 if (!printedSomething) {
4044 if (dumpState.onTitlePrinted())
4045 pw.println();
4046 pw.println("Shared users:");
4047 printedSomething = true;
4048 }
4049 pw.print(" SharedUser [");
4050 pw.print(su.name);
4051 pw.print("] (");
4052 pw.print(Integer.toHexString(System.identityHashCode(su)));
4053 pw.println("):");
Svetoslavc6d1c342015-02-26 14:44:43 -08004054
4055 String prefix = " ";
4056 pw.print(prefix); pw.print("userId="); pw.println(su.userId);
4057
4058 PermissionsState permissionsState = su.getPermissionsState();
4059 dumpInstallPermissionsLPr(pw, prefix, permissionsState);
4060
4061 for (int userId : UserManagerService.getInstance().getUserIds()) {
4062 final int[] gids = permissionsState.computeGids(userId);
4063 Set<String> permissions = permissionsState.getRuntimePermissions(userId);
4064 if (!ArrayUtils.isEmpty(gids) || !permissions.isEmpty()) {
4065 pw.print(prefix); pw.print("User "); pw.print(userId); pw.println(": ");
4066 dumpGidsLPr(pw, prefix + " ", gids);
4067 dumpRuntimePermissionsLPr(pw, prefix + " ", permissions);
4068 }
Dianne Hackbornd052a942014-11-21 15:23:13 -08004069 }
4070 } else {
4071 pw.print("suid,"); pw.print(su.userId); pw.print(","); pw.println(su.name);
Kenny Root447106f2011-03-23 11:00:15 -07004072 }
4073 }
4074 }
4075
4076 void dumpReadMessagesLPr(PrintWriter pw, DumpState dumpState) {
4077 pw.println("Settings parse messages:");
4078 pw.print(mReadMessages.toString());
4079 }
Jeff Sharkeyc4d05fc2014-12-01 16:24:01 -08004080
4081 private static void dumpSplitNames(PrintWriter pw, PackageParser.Package pkg) {
4082 if (pkg == null) {
4083 pw.print("unknown");
4084 } else {
4085 // [base:10, config.mdpi, config.xhdpi:12]
4086 pw.print("[");
4087 pw.print("base");
4088 if (pkg.baseRevisionCode != 0) {
4089 pw.print(":"); pw.print(pkg.baseRevisionCode);
4090 }
4091 if (pkg.splitNames != null) {
4092 for (int i = 0; i < pkg.splitNames.length; i++) {
4093 pw.print(", ");
4094 pw.print(pkg.splitNames[i]);
4095 if (pkg.splitRevisionCodes[i] != 0) {
4096 pw.print(":"); pw.print(pkg.splitRevisionCodes[i]);
4097 }
4098 }
4099 }
4100 pw.print("]");
4101 }
4102 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004103
4104 void dumpGidsLPr(PrintWriter pw, String prefix, int[] gids) {
4105 if (!ArrayUtils.isEmpty(gids)) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07004106 pw.print(prefix);
4107 pw.print("gids="); pw.println(
Svetoslavc6d1c342015-02-26 14:44:43 -08004108 PackageManagerService.arrayToString(gids));
4109 }
4110 }
4111
4112 void dumpRuntimePermissionsLPr(PrintWriter pw, String prefix, Set<String> permissions) {
4113 if (!permissions.isEmpty()) {
4114 pw.print(prefix); pw.println("runtime permissions:");
4115 for (String permission : permissions) {
4116 pw.print(prefix); pw.print(" "); pw.println(permission);
4117 }
4118 }
4119 }
4120
4121 void dumpInstallPermissionsLPr(PrintWriter pw, String prefix,
4122 PermissionsState permissionsState) {
4123 Set<String> permissions = permissionsState.getInstallPermissions();
4124 if (!permissions.isEmpty()) {
4125 pw.print(prefix); pw.println("install permissions:");
4126 for (String permission : permissions) {
4127 pw.print(prefix); pw.print(" "); pw.println(permission);
4128 }
4129 }
4130 }
4131
4132 public void writeRuntimePermissionsForUserLPr(int userId, boolean sync) {
4133 if (sync) {
4134 mRuntimePermissionsPersistence.writePermissionsForUserSyncLPr(userId);
4135 } else {
4136 mRuntimePermissionsPersistence.writePermissionsForUserAsyncLPr(userId);
4137 }
4138 }
4139
4140 private final class RuntimePermissionPersistence {
4141 private static final long WRITE_PERMISSIONS_DELAY_MILLIS = 200;
4142
4143 private static final long MAX_WRITE_PERMISSIONS_DELAY_MILLIS = 2000;
4144
4145 private final Handler mHandler = new MyHandler();
4146
4147 private final Object mLock;
4148
4149 @GuardedBy("mLock")
4150 private SparseBooleanArray mWriteScheduled = new SparseBooleanArray();
4151
4152 @GuardedBy("mLock")
4153 private SparseLongArray mLastNotWrittenMutationTimesMillis = new SparseLongArray();
4154
4155 public RuntimePermissionPersistence(Object lock) {
4156 mLock = lock;
4157 }
4158
4159 public void writePermissionsForUserSyncLPr(int userId) {
Svet Ganov12a692a2015-03-28 19:34:15 -07004160 if (!PackageManagerService.RUNTIME_PERMISSIONS_ENABLED) {
4161 return;
4162 }
4163
Svetoslavc6d1c342015-02-26 14:44:43 -08004164 mHandler.removeMessages(userId);
4165 writePermissionsSync(userId);
4166 }
4167
4168 public void writePermissionsForUserAsyncLPr(int userId) {
Svet Ganov12a692a2015-03-28 19:34:15 -07004169 if (!PackageManagerService.RUNTIME_PERMISSIONS_ENABLED) {
4170 return;
4171 }
4172
Svetoslavc6d1c342015-02-26 14:44:43 -08004173 final long currentTimeMillis = SystemClock.uptimeMillis();
4174
4175 if (mWriteScheduled.get(userId)) {
4176 mHandler.removeMessages(userId);
4177
4178 // If enough time passed, write without holding off anymore.
4179 final long lastNotWrittenMutationTimeMillis = mLastNotWrittenMutationTimesMillis
4180 .get(userId);
4181 final long timeSinceLastNotWrittenMutationMillis = currentTimeMillis
4182 - lastNotWrittenMutationTimeMillis;
4183 if (timeSinceLastNotWrittenMutationMillis >= MAX_WRITE_PERMISSIONS_DELAY_MILLIS) {
4184 mHandler.obtainMessage(userId).sendToTarget();
4185 return;
4186 }
4187
4188 // Hold off a bit more as settings are frequently changing.
4189 final long maxDelayMillis = Math.max(lastNotWrittenMutationTimeMillis
4190 + MAX_WRITE_PERMISSIONS_DELAY_MILLIS - currentTimeMillis, 0);
4191 final long writeDelayMillis = Math.min(WRITE_PERMISSIONS_DELAY_MILLIS,
4192 maxDelayMillis);
4193
4194 Message message = mHandler.obtainMessage(userId);
4195 mHandler.sendMessageDelayed(message, writeDelayMillis);
4196 } else {
4197 mLastNotWrittenMutationTimesMillis.put(userId, currentTimeMillis);
4198 Message message = mHandler.obtainMessage(userId);
4199 mHandler.sendMessageDelayed(message, WRITE_PERMISSIONS_DELAY_MILLIS);
4200 mWriteScheduled.put(userId, true);
4201 }
4202 }
4203
4204 private void writePermissionsSync(int userId) {
4205 AtomicFile destination = new AtomicFile(getUserRuntimePermissionsFile(userId));
4206
4207 ArrayMap<String, Set<String>> permissionsForPackage = new ArrayMap<>();
4208 ArrayMap<String, Set<String>> permissionsForSharedUser = new ArrayMap<>();
4209
4210 synchronized (mLock) {
4211 mWriteScheduled.delete(userId);
4212
4213 final int packageCount = mPackages.size();
4214 for (int i = 0; i < packageCount; i++) {
4215 String packageName = mPackages.keyAt(i);
4216 PackageSetting packageSetting = mPackages.valueAt(i);
4217 if (packageSetting.sharedUser == null) {
4218 PermissionsState permissionsState = packageSetting.getPermissionsState();
4219 Set<String> permissions = permissionsState.getRuntimePermissions(userId);
4220 if (!permissions.isEmpty()) {
4221 permissionsForPackage.put(packageName, permissions);
4222 }
4223 }
4224 }
4225
4226 final int sharedUserCount = mSharedUsers.size();
4227 for (int i = 0; i < sharedUserCount; i++) {
4228 String sharedUserName = mSharedUsers.keyAt(i);
4229 SharedUserSetting sharedUser = mSharedUsers.valueAt(i);
4230 PermissionsState permissionsState = sharedUser.getPermissionsState();
4231 Set<String> permissions = permissionsState.getRuntimePermissions(userId);
4232 if (!permissions.isEmpty()) {
4233 permissionsForSharedUser.put(sharedUserName, permissions);
4234 }
4235 }
4236 }
4237
4238 FileOutputStream out = null;
4239 try {
4240 out = destination.startWrite();
4241
4242 XmlSerializer serializer = Xml.newSerializer();
4243 serializer.setOutput(out, "utf-8");
4244 serializer.setFeature(
4245 "http://xmlpull.org/v1/doc/features.html#indent-output", true);
4246 serializer.startDocument(null, true);
4247 serializer.startTag(null, TAG_RUNTIME_PERMISSIONS);
4248
4249 final int packageCount = permissionsForPackage.size();
4250 for (int i = 0; i < packageCount; i++) {
4251 String packageName = permissionsForPackage.keyAt(i);
4252 Set<String> permissions = permissionsForPackage.valueAt(i);
4253 serializer.startTag(null, TAG_PACKAGE);
4254 serializer.attribute(null, ATTR_NAME, packageName);
4255 writePermissions(serializer, permissions);
4256 serializer.endTag(null, TAG_PACKAGE);
4257 }
4258
4259 final int sharedUserCount = permissionsForSharedUser.size();
4260 for (int i = 0; i < sharedUserCount; i++) {
4261 String packageName = permissionsForSharedUser.keyAt(i);
4262 Set<String> permissions = permissionsForSharedUser.valueAt(i);
4263 serializer.startTag(null, TAG_SHARED_USER);
4264 serializer.attribute(null, ATTR_NAME, packageName);
4265 writePermissions(serializer, permissions);
4266 serializer.endTag(null, TAG_SHARED_USER);
4267 }
4268
4269 serializer.endTag(null, TAG_RUNTIME_PERMISSIONS);
4270 serializer.endDocument();
4271 destination.finishWrite(out);
4272 } catch (IOException e) {
4273 Slog.wtf(PackageManagerService.TAG,
4274 "Failed to write settings, restoring backup", e);
4275 destination.failWrite(out);
4276 } finally {
4277 IoUtils.closeQuietly(out);
4278 }
4279 }
4280
4281 private void onUserRemoved(int userId) {
4282 // Make sure we do not
4283 mHandler.removeMessages(userId);
4284
4285 for (SettingBase sb : mPackages.values()) {
4286 revokeRuntimePermissions(sb, userId);
4287 }
4288
4289 for (SettingBase sb : mSharedUsers.values()) {
4290 revokeRuntimePermissions(sb, userId);
4291 }
4292 }
4293
4294 private void revokeRuntimePermissions(SettingBase sb, int userId) {
4295 PermissionsState permissionsState = sb.getPermissionsState();
4296 for (String permission : permissionsState.getRuntimePermissions(userId)) {
4297 BasePermission bp = mPermissions.get(permission);
4298 if (bp != null) {
4299 permissionsState.revokeRuntimePermission(bp, userId);
4300 }
4301 }
4302 }
4303
4304 public void readStateForUserSyncLPr(int userId) {
4305 File permissionsFile = getUserRuntimePermissionsFile(userId);
4306 if (!permissionsFile.exists()) {
4307 return;
4308 }
4309
4310 FileInputStream in;
4311 try {
4312 in = new FileInputStream(permissionsFile);
4313 } catch (FileNotFoundException fnfe) {
4314 Slog.i(PackageManagerService.TAG, "No permissions state");
4315 return;
4316 }
4317
4318 try {
4319 XmlPullParser parser = Xml.newPullParser();
4320 parser.setInput(in, null);
4321 parseRuntimePermissionsLPr(parser, userId);
4322 } catch (XmlPullParserException | IOException ise) {
4323 throw new IllegalStateException("Failed parsing permissions file: "
4324 + permissionsFile , ise);
4325 } finally {
4326 IoUtils.closeQuietly(in);
4327 }
4328 }
4329
4330 private void parseRuntimePermissionsLPr(XmlPullParser parser, int userId)
4331 throws IOException, XmlPullParserException {
Svet Ganov12a692a2015-03-28 19:34:15 -07004332 final int outerDepth = parser.getDepth();
4333 int type;
4334 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
4335 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
4336 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
4337 continue;
4338 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004339
Svet Ganov12a692a2015-03-28 19:34:15 -07004340 switch (parser.getName()) {
4341 case TAG_PACKAGE: {
4342 String name = parser.getAttributeValue(null, ATTR_NAME);
4343 PackageSetting ps = mPackages.get(name);
4344 if (ps == null) {
4345 Slog.w(PackageManagerService.TAG, "Unknown package:" + name);
4346 XmlUtils.skipCurrentTag(parser);
4347 continue;
4348 }
4349 parsePermissionsLPr(parser, ps.getPermissionsState(), userId);
4350 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08004351
Svet Ganov12a692a2015-03-28 19:34:15 -07004352 case TAG_SHARED_USER: {
4353 String name = parser.getAttributeValue(null, ATTR_NAME);
4354 SharedUserSetting sus = mSharedUsers.get(name);
4355 if (sus == null) {
4356 Slog.w(PackageManagerService.TAG, "Unknown shared user:" + name);
4357 XmlUtils.skipCurrentTag(parser);
4358 continue;
4359 }
4360 parsePermissionsLPr(parser, sus.getPermissionsState(), userId);
4361 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08004362 }
4363 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004364 }
4365
Svet Ganov12a692a2015-03-28 19:34:15 -07004366 private void parsePermissionsLPr(XmlPullParser parser, PermissionsState permissionsState,
Svetoslavc6d1c342015-02-26 14:44:43 -08004367 int userId) throws IOException, XmlPullParserException {
Svet Ganov12a692a2015-03-28 19:34:15 -07004368 final int outerDepth = parser.getDepth();
4369 int type;
4370 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
4371 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
4372 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
4373 continue;
Svetoslavc6d1c342015-02-26 14:44:43 -08004374 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004375
Svet Ganov12a692a2015-03-28 19:34:15 -07004376 switch (parser.getName()) {
4377 case TAG_ITEM: {
4378 String name = parser.getAttributeValue(null, ATTR_NAME);
4379 BasePermission bp = mPermissions.get(name);
4380 if (bp == null) {
4381 Slog.w(PackageManagerService.TAG, "Unknown permission:" + name);
4382 XmlUtils.skipCurrentTag(parser);
4383 continue;
4384 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004385
Svet Ganov12a692a2015-03-28 19:34:15 -07004386 if (permissionsState.grantRuntimePermission(bp, userId) ==
4387 PermissionsState.PERMISSION_OPERATION_FAILURE) {
4388 Slog.w(PackageManagerService.TAG, "Duplicate permission:" + name);
4389 }
4390 } break;
Svetoslavc6d1c342015-02-26 14:44:43 -08004391 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004392 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004393 }
4394
4395 private void writePermissions(XmlSerializer serializer, Set<String> permissions)
4396 throws IOException {
4397 for (String permission : permissions) {
4398 serializer.startTag(null, TAG_ITEM);
4399 serializer.attribute(null, ATTR_NAME, permission);
4400 serializer.endTag(null, TAG_ITEM);
4401 }
4402 }
4403
4404 private final class MyHandler extends Handler {
4405 public MyHandler() {
4406 super(BackgroundThread.getHandler().getLooper());
4407 }
4408
4409 @Override
4410 public void handleMessage(Message message) {
4411 final int userId = message.what;
4412 Runnable callback = (Runnable) message.obj;
4413 writePermissionsSync(userId);
4414 if (callback != null) {
4415 callback.run();
4416 }
4417 }
4418 }
4419 }
Andy McFadden2f362292012-01-20 14:43:38 -08004420}