Add new activity manager method to get list of running applications installed on sdcard.
Use new method in UsbStorageActivity.
Fix moving dex files.
moveDex should be suffixed with LI since it uses Installer

Change-Id: Id5ef0254578e84b9aae2c2ac44f722eb5a0fda1c
diff --git a/services/java/com/android/server/PackageManagerService.java b/services/java/com/android/server/PackageManagerService.java
index 53415c7..4d7b393 100644
--- a/services/java/com/android/server/PackageManagerService.java
+++ b/services/java/com/android/server/PackageManagerService.java
@@ -5258,7 +5258,7 @@
             String sourceFile = getCodePath();
             // Remove dex file
             if (mInstaller != null) {
-                int retCode = mInstaller.rmdex(sourceFile.toString());
+                int retCode = mInstaller.rmdex(sourceFile);
                 if (retCode < 0) {
                     Slog.w(TAG, "Couldn't remove dex file for package: "
                             + " at location "
@@ -5613,7 +5613,7 @@
     }
 
     // Utility method used to move dex files during install.
-    private int moveDexFiles(PackageParser.Package newPackage) {
+    private int moveDexFilesLI(PackageParser.Package newPackage) {
         int retCode;
         if ((newPackage.applicationInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0) {
             retCode = mInstaller.movedex(newPackage.mScanPath, newPackage.mPath);
@@ -5636,7 +5636,7 @@
             mSettings.writeLP();
         }
 
-        if ((res.returnCode = moveDexFiles(newPackage))
+        if ((res.returnCode = moveDexFilesLI(newPackage))
                 != PackageManager.INSTALL_SUCCEEDED) {
             // Discontinue if moving dex files failed.
             return;
@@ -9697,41 +9697,43 @@
                    sendResourcesChangedBroadcast(false, pkgList, uidArr);
 
                    // Update package code and resource paths
-                   synchronized (mPackages) {
-                       PackageParser.Package pkg = mPackages.get(mp.packageName);
-                       if (pkg != null) {
-                           String oldCodePath = pkg.mPath;
-                           String newCodePath = mp.targetArgs.getCodePath();
-                           String newResPath = mp.targetArgs.getResourcePath();
-                           pkg.mPath = newCodePath;
-                           // Move dex files around
-                           if (moveDexFiles(pkg)
-                                   != PackageManager.INSTALL_SUCCEEDED) {
-                               // Moving of dex files failed. Set
-                               // error code and abort move.
-                               pkg.mPath = pkg.mScanPath;
-                               returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE;
-                               moveSucceeded = false;
-                           } else {
-                               pkg.mScanPath = newCodePath;
-                               pkg.applicationInfo.sourceDir = newCodePath;
-                               pkg.applicationInfo.publicSourceDir = newResPath;
-                               PackageSetting ps = (PackageSetting) pkg.mExtras;
-                               ps.codePath = new File(pkg.applicationInfo.sourceDir);
-                               ps.codePathString = ps.codePath.getPath();
-                               ps.resourcePath = new File(pkg.applicationInfo.publicSourceDir);
-                               ps.resourcePathString = ps.resourcePath.getPath();
-                               // Set the application info flag correctly.
-                               if ((mp.flags & PackageManager.INSTALL_EXTERNAL) != 0) {
-                                   pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE;
+                   synchronized (mInstallLock) {
+                       synchronized (mPackages) {
+                           PackageParser.Package pkg = mPackages.get(mp.packageName);
+                           if (pkg != null) {
+                               String oldCodePath = pkg.mPath;
+                               String newCodePath = mp.targetArgs.getCodePath();
+                               String newResPath = mp.targetArgs.getResourcePath();
+                               pkg.mPath = newCodePath;
+                               // Move dex files around
+                               if (moveDexFilesLI(pkg)
+                                       != PackageManager.INSTALL_SUCCEEDED) {
+                                   // Moving of dex files failed. Set
+                                   // error code and abort move.
+                                   pkg.mPath = pkg.mScanPath;
+                                   returnCode = PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE;
+                                   moveSucceeded = false;
                                } else {
-                                   pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_EXTERNAL_STORAGE;
+                                   pkg.mScanPath = newCodePath;
+                                   pkg.applicationInfo.sourceDir = newCodePath;
+                                   pkg.applicationInfo.publicSourceDir = newResPath;
+                                   PackageSetting ps = (PackageSetting) pkg.mExtras;
+                                   ps.codePath = new File(pkg.applicationInfo.sourceDir);
+                                   ps.codePathString = ps.codePath.getPath();
+                                   ps.resourcePath = new File(pkg.applicationInfo.publicSourceDir);
+                                   ps.resourcePathString = ps.resourcePath.getPath();
+                                   // Set the application info flag correctly.
+                                   if ((mp.flags & PackageManager.INSTALL_EXTERNAL) != 0) {
+                                       pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE;
+                                   } else {
+                                       pkg.applicationInfo.flags &= ~ApplicationInfo.FLAG_EXTERNAL_STORAGE;
+                                   }
+                                   ps.setFlags(pkg.applicationInfo.flags);
+                                   mAppDirs.remove(oldCodePath);
+                                   mAppDirs.put(newCodePath, pkg);
+                                   // Persist settings
+                                   mSettings.writeLP();
                                }
-                               ps.setFlags(pkg.applicationInfo.flags);
-                               mAppDirs.remove(oldCodePath);
-                               mAppDirs.put(newCodePath, pkg);
-                               // Persist settings
-                               mSettings.writeLP();
                            }
                        }
                    }