If the phone is idle, don't let the user get stuck on the in-call screen.

If the InCallScreen comes to the foreground but no call is active, that
can mean one of two things:

  (1) We had just tried to make an outgoing call, but failed (like if
      there was no service or we were in airplane mode.)

      In this case we display an error dialog.

  (2) We came to the foreground for some reason *other* than making a new
      call.  For example, if the user manually turned the screen off and
      then back on *after* a failed outgoing call.

      In this case we currently display the in-call screen in a
      useless blank state :-(

This change fixes case (2) to simply bail out, since there's no reason to
remain on the in-call screen if the phone is totally idle.  This change
has no effect on case (1).

(I originally developed+tested this in froyo, but decided to defer it to
gingerbread since we were pretty late in the froyo schedule, and this bug
only happens in really obscure scenarios anyway.)

Tested:
  (1) Scenarios from bug 2072046.  Error dialogs *are* visible when the
      error first occurs, but if you then turn the screen off and back on,
      the in-call UI dismisses itself.
  (2) Normal outgoing and incoming calls: no change.

Bug: 2072046
Change-Id: Id098c7ed3aec2c3c7f9586331ecf5e4f5200d0ee
diff --git a/src/com/android/phone/InCallScreen.java b/src/com/android/phone/InCallScreen.java
index 98037b3..230a480 100755
--- a/src/com/android/phone/InCallScreen.java
+++ b/src/com/android/phone/InCallScreen.java
@@ -684,6 +684,7 @@
 
         // Check for any failures that happened during onCreate() or onNewIntent().
         if (DBG) log("- onResume: initial status = " + mInCallInitialStatus);
+        boolean handledStartupError = false;
         if (mInCallInitialStatus != InCallInitStatus.SUCCESS) {
             if (DBG) log("- onResume: failure during startup: " + mInCallInitialStatus);
 
@@ -691,6 +692,7 @@
             // something more specific to let the user deal with the
             // problem.
             handleStartupError(mInCallInitialStatus);
+            handledStartupError = true;
 
             // But it *is* OK to continue with the rest of onResume(),
             // since any further setup steps (like updateScreen() and the
@@ -729,12 +731,37 @@
 
         InCallInitStatus status = syncWithPhoneState();
         if (status != InCallInitStatus.SUCCESS) {
-            if (DBG) log("- syncWithPhoneState failed! status = " + status);
+            if (DBG) log("- onResume: syncWithPhoneState failed! status = " + status);
             // Couldn't update the UI, presumably because the phone is totally
-            // idle.  But don't endInCallScreenSession immediately, since we might still
-            // have an error dialog up that the user needs to see.
-            // (And in that case, the error dialog is responsible for calling
-            // endInCallScreenSession when the user dismisses it.)
+            // idle.
+
+            if (handledStartupError) {
+                // Do NOT bail out of the in-call UI, since there's
+                // presumably a dialog visible right now (see the call to
+                // handleStartupError() above.)
+                //
+                // In this case, stay here for now, and we'll eventually
+                // leave the InCallScreen when the user presses the
+                // dialog's OK button (see bailOutAfterErrorDialog()).
+                if (DBG) log("  ==> syncWithPhoneState failed, but staying here anyway.");
+            } else {
+                // The phone is idle, and we did NOT handle a
+                // startup error during this pass thru onResume.
+                //
+                // This basically means that we're being resumed because of
+                // some action *other* than a new intent.  (For example,
+                // the user pressing POWER to wake up the device, causing
+                // the InCallScreen to come back to the foreground.)
+                //
+                // In this scenario we do NOT want to stay here on the
+                // InCallScreen: we're not showing any useful info to the
+                // user (like a dialog), and the in-call UI itself is
+                // useless if there's no active call.  So bail out.
+                if (DBG) log("  ==> syncWithPhoneState failed; bailing out!");
+                dismissAllDialogs();
+                endInCallScreenSession();
+                return;
+            }
         } else if (phoneIsCdma) {
             if (mInCallScreenMode == InCallScreenMode.OTA_NORMAL ||
                     mInCallScreenMode == InCallScreenMode.OTA_ENDED) {