Fix resource code and version attributes
Create a new package setting object for updated system apps rather than moving
around the same setting. This updates the resource, code and version correctly.
For updating system packages, disable the package first which removes the entry
from internal structures, create a new package setting, add it to list of user id's
then rest of installation steps, kill the process if needed via ActivityManager
then add this setting if everything was successful. This also fixes issues with
updating values prematurely.

When a new version of system package is available via OTA, just physically remove
entries for pkg. Note that the component and other info will be eventually updated
later on when scanning the package.
Also move certificate verification slightly ahead before scanning packages.

Some null checks

New api's in ActivityManager to kill an application pkg before finishing installation
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 81a715d..5ea59ca 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -4736,7 +4736,30 @@
             Binder.restoreCallingIdentity(callingId);
         }
     }
-    
+
+    /*
+     * The pkg name and uid have to be specified.
+     * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int)
+     */
+    public void killApplicationWithUid(String pkg, int uid) {
+        if (pkg == null) {
+            return;
+        }
+        // Make sure the uid is valid.
+        if (uid < 0) {
+            Log.w(TAG, "Invalid uid specified for pkg : " + pkg);
+            return;
+        }
+        int callerUid = Binder.getCallingUid();
+        // Only the system server can kill an application
+        if (callerUid == Process.SYSTEM_UID) {
+            uninstallPackageLocked(pkg, uid, false);
+        } else {
+            throw new SecurityException(callerUid + " cannot kill pkg: " +
+                    pkg);
+        }
+    }
+
     private void restartPackageLocked(final String packageName, int uid) {
         uninstallPackageLocked(packageName, uid, false);
         Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,