Final push of installd to Binder; goodbye socket!

Move last two straggling installd commands to Binder and destroy the
socket-based communication channel forever.

Test: builds, boots, apps install fine, pre-OTA dexopt works
Bug: 13758960, 30944031
Change-Id: I9afb9f71858febde34a94f53839b2986493b68a0
diff --git a/services/core/java/com/android/server/pm/Installer.java b/services/core/java/com/android/server/pm/Installer.java
index f00065f..f144761 100644
--- a/services/core/java/com/android/server/pm/Installer.java
+++ b/services/core/java/com/android/server/pm/Installer.java
@@ -26,14 +26,10 @@
 import android.os.ServiceSpecificException;
 import android.util.Slog;
 
-import com.android.internal.os.InstallerConnection;
-import com.android.internal.os.InstallerConnection.InstallerException;
 import com.android.server.SystemService;
 
 import dalvik.system.VMRuntime;
 
-import java.util.Arrays;
-
 public class Installer extends SystemService {
     private static final String TAG = "Installer";
 
@@ -61,9 +57,7 @@
     private final boolean mIsolated;
 
     // TODO: reconnect if installd restarts
-    private final InstallerConnection mInstaller;
-    private final IInstalld mInstalld;
-
+    private volatile IInstalld mInstalld;
     private volatile Object mWarnIfHeld;
 
     public Installer(Context context) {
@@ -78,13 +72,6 @@
     public Installer(Context context, boolean isolated) {
         super(context);
         mIsolated = isolated;
-        if (isolated) {
-            mInstaller = null;
-            mInstalld = null;
-        } else {
-            mInstaller = new InstallerConnection();
-            mInstalld = IInstalld.Stub.asInterface(ServiceManager.getService("installd"));
-        }
     }
 
     /**
@@ -92,17 +79,15 @@
      * the given object.
      */
     public void setWarnIfHeld(Object warnIfHeld) {
-        if (mInstaller != null) {
-            mInstaller.setWarnIfHeld(warnIfHeld);
-        }
         mWarnIfHeld = warnIfHeld;
     }
 
     @Override
     public void onStart() {
-        if (mInstaller != null) {
-            Slog.i(TAG, "Waiting for installd to be ready.");
-            mInstaller.waitForConnection();
+        if (mIsolated) {
+            mInstalld = null;
+        } else {
+            mInstalld = IInstalld.Stub.asInterface(ServiceManager.getService("installd"));
         }
     }
 
@@ -131,7 +116,7 @@
             mInstalld.createAppData(uuid, packageName, userId, flags, appId, seInfo,
                     targetSdkVersion);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -141,7 +126,7 @@
         try {
             mInstalld.restoreconAppData(uuid, packageName, userId, flags, appId, seInfo);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -151,7 +136,7 @@
         try {
             mInstalld.migrateAppData(uuid, packageName, userId, flags);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -161,7 +146,7 @@
         try {
             mInstalld.clearAppData(uuid, packageName, userId, flags, ceDataInode);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -171,7 +156,7 @@
         try {
             mInstalld.destroyAppData(uuid, packageName, userId, flags, ceDataInode);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -183,21 +168,21 @@
             mInstalld.moveCompleteApp(fromUuid, toUuid, packageName, dataAppName, appId, seInfo,
                     targetSdkVersion);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
-    public void getAppSize(String uuid, String pkgname, int userid, int flags, long ceDataInode,
+    public void getAppSize(String uuid, String packageName, int userId, int flags, long ceDataInode,
             String codePath, PackageStats stats) throws InstallerException {
         if (!checkBeforeRemote()) return;
-        final String[] res = mInstaller.execute("get_app_size", uuid, pkgname, userid, flags,
-                ceDataInode, codePath);
         try {
-            stats.codeSize += Long.parseLong(res[1]);
-            stats.dataSize += Long.parseLong(res[2]);
-            stats.cacheSize += Long.parseLong(res[3]);
-        } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
-            throw new InstallerException("Invalid size result: " + Arrays.toString(res));
+            final long[] res = mInstalld.getAppSize(uuid, packageName, userId, flags, ceDataInode,
+                    codePath);
+            stats.codeSize += res[0];
+            stats.dataSize += res[1];
+            stats.cacheSize += res[2];
+        } catch (RemoteException | ServiceSpecificException e) {
+            throw InstallerException.from(e);
         }
     }
 
@@ -207,7 +192,7 @@
         try {
             return mInstalld.getAppDataInode(uuid, packageName, userId, flags);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -217,8 +202,12 @@
             throws InstallerException {
         assertValidInstructionSet(instructionSet);
         if (!checkBeforeRemote()) return;
-        mInstaller.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded,
-                outputPath, dexFlags, compilerFilter, volumeUuid, sharedLibraries);
+        try {
+            mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath,
+                    dexFlags, compilerFilter, volumeUuid, sharedLibraries);
+        } catch (RemoteException | ServiceSpecificException e) {
+            throw InstallerException.from(e);
+        }
     }
 
     public boolean mergeProfiles(int uid, String packageName) throws InstallerException {
@@ -226,7 +215,7 @@
         try {
             return mInstalld.mergeProfiles(uid, packageName);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -236,7 +225,7 @@
         try {
             return mInstalld.dumpProfiles(uid, packageName, codePaths);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -246,7 +235,7 @@
         try {
             mInstalld.idmap(targetApkPath, overlayApkPath, uid);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -256,7 +245,7 @@
         try {
             mInstalld.rmdex(codePath, instructionSet);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -265,7 +254,7 @@
         try {
             mInstalld.rmPackageDir(packageDir);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -274,7 +263,7 @@
         try {
             mInstalld.clearAppProfiles(packageName);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -283,7 +272,7 @@
         try {
             mInstalld.destroyAppProfiles(packageName);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -293,7 +282,7 @@
         try {
             mInstalld.createUserData(uuid, userId, userSerial, flags);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -302,7 +291,7 @@
         try {
             mInstalld.destroyUserData(uuid, userId, flags);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -312,7 +301,7 @@
         try {
             mInstalld.markBootComplete(instructionSet);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -321,7 +310,7 @@
         try {
             mInstalld.freeCache(uuid, freeStorageSize);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -336,7 +325,7 @@
         try {
             mInstalld.linkNativeLibraryDirectory(uuid, packageName, nativeLibPath32, userId);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -346,7 +335,7 @@
         try {
             mInstalld.createOatDir(oatDir, dexInstructionSet);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -356,7 +345,7 @@
         try {
             mInstalld.linkFile(relativePath, fromBase, toBase);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -366,7 +355,7 @@
         try {
             mInstalld.moveAb(apkPath, instructionSet, outputPath);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -376,7 +365,7 @@
         try {
             mInstalld.deleteOdex(apkPath, instructionSet, outputPath);
         } catch (RemoteException | ServiceSpecificException e) {
-            throw new InstallerException(e.getMessage());
+            throw InstallerException.from(e);
         }
     }
 
@@ -389,4 +378,14 @@
         }
         throw new InstallerException("Invalid instruction set: " + instructionSet);
     }
+
+    public static class InstallerException extends Exception {
+        public InstallerException(String detailMessage) {
+            super(detailMessage);
+        }
+
+        public static InstallerException from(Exception e) throws InstallerException {
+            throw new InstallerException(e.getMessage());
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/pm/OtaDexoptService.java b/services/core/java/com/android/server/pm/OtaDexoptService.java
index c0ae272..7b078ca1 100644
--- a/services/core/java/com/android/server/pm/OtaDexoptService.java
+++ b/services/core/java/com/android/server/pm/OtaDexoptService.java
@@ -35,7 +35,7 @@
 import android.util.Slog;
 
 import com.android.internal.logging.MetricsLogger;
-import com.android.internal.os.InstallerConnection.InstallerException;
+import com.android.server.pm.Installer.InstallerException;
 
 import java.io.File;
 import java.io.FileDescriptor;
diff --git a/services/core/java/com/android/server/pm/PackageDexOptimizer.java b/services/core/java/com/android/server/pm/PackageDexOptimizer.java
index acdcc72..30ff32b 100644
--- a/services/core/java/com/android/server/pm/PackageDexOptimizer.java
+++ b/services/core/java/com/android/server/pm/PackageDexOptimizer.java
@@ -27,8 +27,8 @@
 import android.util.Log;
 import android.util.Slog;
 
-import com.android.internal.os.InstallerConnection.InstallerException;
 import com.android.internal.util.IndentingPrintWriter;
+import com.android.server.pm.Installer.InstallerException;
 
 import java.io.File;
 import java.io.IOException;
diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java
index 2ece99f..c85e1d8 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerSession.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java
@@ -72,10 +72,10 @@
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.content.NativeLibraryHelper;
 import com.android.internal.content.PackageHelper;
-import com.android.internal.os.InstallerConnection.InstallerException;
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.internal.util.Preconditions;
+import com.android.server.pm.Installer.InstallerException;
 import com.android.server.pm.PackageInstallerService.PackageInstallObserverAdapter;
 
 import java.io.File;
diff --git a/services/core/java/com/android/server/pm/PackageManagerException.java b/services/core/java/com/android/server/pm/PackageManagerException.java
index f243e63..0e3f173 100644
--- a/services/core/java/com/android/server/pm/PackageManagerException.java
+++ b/services/core/java/com/android/server/pm/PackageManagerException.java
@@ -19,7 +19,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.PackageParser.PackageParserException;
 
-import com.android.internal.os.InstallerConnection.InstallerException;
+import com.android.server.pm.Installer.InstallerException;
 
 /** {@hide} */
 public class PackageManagerException extends Exception {
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index 81c31b8..87eafd7 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -231,7 +231,6 @@
 import com.android.internal.content.PackageHelper;
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.os.IParcelFileDescriptorFactory;
-import com.android.internal.os.InstallerConnection.InstallerException;
 import com.android.internal.os.SomeArgs;
 import com.android.internal.os.Zygote;
 import com.android.internal.telephony.CarrierAppUtils;
@@ -250,6 +249,7 @@
 import com.android.server.SystemConfig;
 import com.android.server.Watchdog;
 import com.android.server.net.NetworkPolicyManagerInternal;
+import com.android.server.pm.Installer.InstallerException;
 import com.android.server.pm.PermissionsState.PermissionState;
 import com.android.server.pm.Settings.DatabaseVersion;
 import com.android.server.pm.Settings.VersionInfo;
diff --git a/services/core/java/com/android/server/pm/Settings.java b/services/core/java/com/android/server/pm/Settings.java
index 335af08..39cae37 100644
--- a/services/core/java/com/android/server/pm/Settings.java
+++ b/services/core/java/com/android/server/pm/Settings.java
@@ -77,13 +77,13 @@
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.os.BackgroundThread;
-import com.android.internal.os.InstallerConnection.InstallerException;
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.FastXmlSerializer;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.internal.util.JournaledFile;
 import com.android.internal.util.XmlUtils;
 import com.android.server.backup.PreferredActivityBackupHelper;
+import com.android.server.pm.Installer.InstallerException;
 import com.android.server.pm.PackageManagerService.DumpState;
 import com.android.server.pm.PermissionsState.PermissionState;