Maybe fix issue #17700474: manta: high occurrence of device booted...

...but dev.bootcomplete flag is not set

Rework things to address a few issues I found:

- When the activity goes idle, the way we were handling finishing the
  boot there was calling finishBooting() with the lock held, but it
  shouldn't.  We now dispatch that and turning on the screen together
  in a separate message.

- Make sure we don't try to start the home activity until we have
  reached the point of the system being ready and mBooting being set.
  This ensures we don't do any work prematurely.

Change-Id: If30c1f287af73bc2164e7aadbe98022ae42cc5e7
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index 01e80b7..f430c56 100755
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -1468,6 +1468,7 @@
                 app.services.remove(r);
                 r.app = null;
                 scheduleServiceRestartLocked(r, false);
+                return;
             }
         }
 
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 12c98c1..7fa000f 100755
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -27,6 +27,7 @@
 import static com.android.internal.util.XmlUtils.writeIntAttribute;
 import static com.android.internal.util.XmlUtils.writeLongAttribute;
 import static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
+import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
 import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
 import static org.xmlpull.v1.XmlPullParser.START_TAG;
 import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
@@ -1192,7 +1193,7 @@
     static final int SYSTEM_USER_START_MSG = 42;
     static final int SYSTEM_USER_CURRENT_MSG = 43;
     static final int ENTER_ANIMATION_COMPLETE_MSG = 44;
-    static final int ENABLE_SCREEN_AFTER_BOOT_MSG = 45;
+    static final int FINISH_BOOTING_MSG = 45;
     static final int START_USER_SWITCH_MSG = 46;
     static final int SEND_LOCALE_TO_MOUNT_DAEMON_MSG = 47;
 
@@ -1877,8 +1878,13 @@
                 }
                 break;
             }
-            case ENABLE_SCREEN_AFTER_BOOT_MSG: {
-                enableScreenAfterBoot();
+            case FINISH_BOOTING_MSG: {
+                if (msg.arg1 != 0) {
+                    finishBooting();
+                }
+                if (msg.arg2 != 0) {
+                    enableScreenAfterBoot();
+                }
                 break;
             }
             case SEND_LOCALE_TO_MOUNT_DAEMON_MSG: {
@@ -6258,8 +6264,9 @@
         Binder.restoreCallingIdentity(origId);
     }
 
-    void postEnableScreenAfterBootLocked() {
-        mHandler.sendEmptyMessage(ENABLE_SCREEN_AFTER_BOOT_MSG);
+    void postFinishBooting(boolean finishBooting, boolean enableScreen) {
+        mHandler.sendMessage(mHandler.obtainMessage(FINISH_BOOTING_MSG,
+                finishBooting? 1 : 0, enableScreen ? 1 : 0));
     }
 
     void enableScreenAfterBoot() {
@@ -11272,6 +11279,7 @@
 
             // Start up initial activity.
             mBooting = true;
+            startHomeActivityLocked(mCurrentUserId);
 
             try {
                 if (AppGlobals.getPackageManager().hasSystemUidErrors()) {
@@ -12505,7 +12513,7 @@
 
         boolean printedAnything = false;
 
-        if (mRecentTasks.size() > 0) {
+        if (mRecentTasks != null && mRecentTasks.size() > 0) {
             boolean printedHeader = false;
 
             final int N = mRecentTasks.size();
@@ -12904,10 +12912,12 @@
             if (dumpAll) {
                 pw.println("  Total persistent processes: " + numPers);
                 pw.println("  mProcessesReady=" + mProcessesReady
-                        + " mSystemReady=" + mSystemReady);
-                pw.println("  mBooting=" + mBooting
+                        + " mSystemReady=" + mSystemReady
                         + " mBooted=" + mBooted
                         + " mFactoryTest=" + mFactoryTest);
+                pw.println("  mBooting=" + mBooting
+                        + " mCallFinishBooting=" + mCallFinishBooting
+                        + " mBootAnimationComplete=" + mBootAnimationComplete);
                 pw.print("  mLastPowerCheckRealtime=");
                         TimeUtils.formatDuration(mLastPowerCheckRealtime, pw);
                         pw.println("");
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index 0646cce..c7b9d96 100755
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -1482,6 +1482,11 @@
     final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
         if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
 
+        if (!mService.mBooting && !mService.mBooted) {
+            // Not ready yet!
+            return false;
+        }
+
         ActivityRecord parent = mActivityContainer.mParentActivity;
         if ((parent != null && parent.state != ActivityState.RESUMED) ||
                 !mActivityContainer.isAttachedLocked()) {
@@ -3606,6 +3611,10 @@
 
         final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
         if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
+            if (!mService.mBooting && !mService.mBooted) {
+                // Not ready yet!
+                return false;
+            }
             final int taskToReturnTo = tr.getTaskToReturnTo();
             tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
             return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 83a8d45..e9f0558 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -421,6 +421,11 @@
     }
 
     boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
+        if (!mService.mBooting && !mService.mBooted) {
+            // Not ready yet!
+            return false;
+        }
+
         if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
             mWindowManager.showRecentApps();
             return false;
@@ -2295,9 +2300,7 @@
             activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
         }
 
-        if (booting) {
-            mService.finishBooting();
-        } else {
+        if (!booting) {
             // Complete user switch
             if (startingUsers != null) {
                 for (int i = 0; i < startingUsers.size(); i++) {
@@ -2318,8 +2321,8 @@
         //dump();
         //mWindowManager.dump();
 
-        if (enableScreen) {
-            mService.postEnableScreenAfterBootLocked();
+        if (booting || enableScreen) {
+            mService.postFinishBooting(booting, enableScreen);
         }
 
         if (activityRemoved) {
@@ -2584,7 +2587,6 @@
                         r.mLaunchTaskBehind);
             }
         }
-        resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
     }
 
     void moveTaskToStack(int taskId, int stackId, boolean toTop) {