Fiddle system boot ordering.

This makes the system a little more careful to not start third party
code until it is ready to.

Also fix a little bug in SyncManager that would cause it to crash
during boot if sync was in a failure state.

Change-Id: Ib2d287d8441d155d393fe740a5f98690895fd358
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 8ea6699..03ebf0c 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -8188,7 +8188,7 @@
             }
         }
 
-        systemReady();
+        systemReady(null);
     }
 
     private void retrieveSettings() {
@@ -8218,7 +8218,7 @@
         return mSystemReady;
     }
     
-    public void systemReady() {
+    public void systemReady(final Runnable goingCallback) {
         // In the simulator, startRunning will never have been called, which
         // normally sets a few crucial variables. Do it here instead.
         if (!Process.supportsProcesses()) {
@@ -8228,6 +8228,7 @@
 
         synchronized(this) {
             if (mSystemReady) {
+                if (goingCallback != null) goingCallback.run();
                 return;
             }
             
@@ -8263,7 +8264,7 @@
                                     synchronized (ActivityManagerService.this) {
                                         mDidUpdate = true;
                                     }
-                                    systemReady();
+                                    systemReady(goingCallback);
                                 }
                             };
                         }
@@ -8310,7 +8311,7 @@
             }
         }
         
-        if (Config.LOGD) Log.d(TAG, "Start running!");
+        Log.i(TAG, "System now ready");
         EventLog.writeEvent(LOG_BOOT_PROGRESS_AMS_READY,
             SystemClock.uptimeMillis());
 
@@ -8352,6 +8353,8 @@
 
         retrieveSettings();
 
+        if (goingCallback != null) goingCallback.run();
+        
         synchronized (this) {
             if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
                 try {