Clean up app following death when creating service

If an app has died, run through the cleanup before relaunching its
service.

Also a little simplifying refactor.

Fixes bug 16979752.

Change-Id: I376cbef2ea00fc626588386317f092cc6dea0bdc
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index d6e51f3..0bdb964 100755
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -24,6 +24,7 @@
 import java.util.Iterator;
 import java.util.List;
 
+import android.os.DeadObjectException;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.SystemProperties;
@@ -1424,6 +1425,9 @@
                     app.repProcState);
             r.postNotification();
             created = true;
+        } catch (DeadObjectException e) {
+            Slog.w(TAG, "Application dead when creating service " + r);
+            mAm.appDiedLocked(app);
         } finally {
             if (!created) {
                 app.services.remove(r);
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 6054401..34b2738 100755
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -4201,6 +4201,10 @@
         }
     }
 
+    final void appDiedLocked(ProcessRecord app) {
+       appDiedLocked(app, app.pid, app.thread);
+    }
+
     final void appDiedLocked(ProcessRecord app, int pid,
             IApplicationThread thread) {
 
@@ -8478,7 +8482,7 @@
                                 "Existing provider " + cpr.name.flattenToShortString()
                                 + " is crashing; detaching " + r);
                         boolean lastRef = decProviderCountLocked(conn, cpr, token, stable);
-                        appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread);
+                        appDiedLocked(cpr.proc);
                         if (!lastRef) {
                             // This wasn't the last ref our process had on
                             // the provider...  we have now been killed, bail.
@@ -8928,7 +8932,7 @@
                     + ") early provider death");
             final long ident = Binder.clearCallingIdentity();
             try {
-                appDiedLocked(proc, proc.pid, proc.thread);
+                appDiedLocked(proc);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 3d23cb7..88678cb 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -1165,7 +1165,7 @@
                 Slog.e(TAG, "Second failure launching "
                       + r.intent.getComponent().flattenToShortString()
                       + ", giving up", e);
-                mService.appDiedLocked(app, app.pid, app.thread);
+                mService.appDiedLocked(app);
                 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
                         "2nd-crash", false);
                 return false;