Step 7. PackageManagerService.main
-
class PackageManagerService extends IPackageManager.Stub {
- ......
-
public static final IPackageManager main(Context context, boolean factoryTest) {
-
PackageManagerService m = new PackageManagerService(context, factoryTest);
-
ServiceManager.addService("package", m);
-
return m;
- }
- ......
- }
-
class PackageManagerService extends IPackageManager.Stub {
- ......
-
public PackageManagerService(Context context, boolean factoryTest) {
- ......
-
synchronized (mInstallLock) {
-
synchronized (mPackages) {
- ......
- File dataDir = Environment.getDataDirectory();
-
mAppDataDir = new File(dataDir, "data");
-
mSecureAppDataDir = new File(dataDir, "secure/data");
-
mDrmAppPrivateInstallDir = new File(dataDir, "app-private");
- ......
-
mFrameworkDir = new File(Environment.getRootDirectory(), "framework");
-
mDalvikCacheDir = new File(dataDir, "dalvik-cache");
- ......
-
// Find base frameworks (resource packages without code).
-
mFrameworkInstallObserver = new AppDirObserver(
-
mFrameworkDir.getPath(), OBSERVER_EVENTS, true);
- mFrameworkInstallObserver.startWatching();
- scanDirLI(mFrameworkDir, PackageParser.PARSE_IS_SYSTEM
- | PackageParser.PARSE_IS_SYSTEM_DIR,
-
scanMode | SCAN_NO_DEX, 0);
-
// Collect all system packages.
-
mSystemAppDir = new File(Environment.getRootDirectory(), "app");
-
mSystemInstallObserver = new AppDirObserver(
-
mSystemAppDir.getPath(), OBSERVER_EVENTS, true);
- mSystemInstallObserver.startWatching();
- scanDirLI(mSystemAppDir, PackageParser.PARSE_IS_SYSTEM
-
| PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0);
-
// Collect all vendor packages.
-
mVendorAppDir = new File("/vendor/app");
-
mVendorInstallObserver = new AppDirObserver(
-
mVendorAppDir.getPath(), OBSERVER_EVENTS, true);
- mVendorInstallObserver.startWatching();
- scanDirLI(mVendorAppDir, PackageParser.PARSE_IS_SYSTEM
-
| PackageParser.PARSE_IS_SYSTEM_DIR, scanMode, 0);
-
mAppInstallObserver = new AppDirObserver(
-
mAppInstallDir.getPath(), OBSERVER_EVENTS, false);
- mAppInstallObserver.startWatching();
-
scanDirLI(mAppInstallDir, 0, scanMode, 0);
-
mDrmAppInstallObserver = new AppDirObserver(
-
mDrmAppPrivateInstallDir.getPath(), OBSERVER_EVENTS, false);
- mDrmAppInstallObserver.startWatching();
- scanDirLI(mDrmAppPrivateInstallDir, PackageParser.PARSE_FORWARD_LOCK,
-
scanMode, 0);
- ......
- }
- }
- }
- ......
- }
这个函数定义在frameworks/base/services/java/com/android/server/PackageManagerService.java文件中:
-
class PackageManagerService extends IPackageManager.Stub {
- ......
-
private void scanDirLI(File dir, int flags, int scanMode, long currentTime) {
- String[] files = dir.list();
- ......
-
int i;
-
for (i=0; i<files.length; i++) {
-
File file = new File(dir, files[i]);
-
if (!isPackageFilename(files[i])) {
-
// Ignore entries which are not apk's
-
continue;
- }
- PackageParser.Package pkg = scanPackageLI(file,
- flags|PackageParser.PARSE_MUST_BE_APK, scanMode, currentTime);
-
// Don't mess around with apps in system partition.
-
if (pkg == null && (flags & PackageParser.PARSE_IS_SYSTEM) == 0 &&
- mLastScanError == PackageManager.INSTALL_FAILED_INVALID_APK) {
-
// Delete the apk
-
Slog.w(TAG, "Cleaning up failed install of " + file);
- file.delete();
- }
- }
- }
- ......
- }
这个函数定义在frameworks/base/services/java/com/android/server/PackageManagerService.java文件中:
-
class PackageManagerService extends IPackageManager.Stub {
- ......
-
private PackageParser.Package scanPackageLI(File scanFile,
-
int parseFlags, int scanMode, long currentTime) {
- ......
- String scanPath = scanFile.getPath();
- parseFlags |= mDefParseFlags;
-
PackageParser pp = new PackageParser(scanPath);
- ......
-
final PackageParser.Package pkg = pp.parsePackage(scanFile,
- scanPath, mMetrics, parseFlags);
- ......
-
return scanPackageLI(pkg, parseFlags, scanMode | SCAN_UPDATE_SIGNATURE, currentTime);
- }
- ......
- }