Delay setting up SCO.

Ideally, we would like to set up the SCO channel
before sending the ring() so that we don't miss any
incall audio. However, some headsets don't play the
ringtone in such scenarios. So send the 2 ring()s first
and then setup SCO after a delay of 2 seconds.

If we still find that headsets don't play ringtones, we will back
out the change for opening up SCO early.

Dr No: Eastham
Bug: 2181474
diff --git a/src/com/android/phone/BluetoothHandsfree.java b/src/com/android/phone/BluetoothHandsfree.java
index 65ebbb4..9a89984 100644
--- a/src/com/android/phone/BluetoothHandsfree.java
+++ b/src/com/android/phone/BluetoothHandsfree.java
@@ -90,6 +90,7 @@
     private boolean mPendingSco;  // waiting for a2dp sink to suspend before establishing SCO
     private boolean mA2dpSuspended;
     private boolean mUserWantsAudio;
+    private boolean mAttemptDelayedScoConnection;
     private WakeLock mStartCallWakeLock;  // held while waiting for the intent to start call
     private WakeLock mStartVoiceRecognitionWakeLock;  // held while waiting for voice recognition
 
@@ -356,6 +357,16 @@
                     if (result != null) {
                         sendURC(result.toString());
                     }
+                    // Ideally, we would like to set up the SCO channel
+                    // before sending the ring() so that we don't miss any
+                    // incall audio. However, some headsets don't play the
+                    // ringtone in such scenarios. So send the 2 ring()s first
+                    // and then setup SCO after a delay of 2 seconds.
+                    if (mAttemptDelayedScoConnection) {
+                        mAttemptDelayedScoConnection = false;
+                        Message scoMsg = mHandler.obtainMessage(DELAYED_SCO_FOR_RINGTONE);
+                        mHandler.sendMessageDelayed(scoMsg, 2000);
+                    }
                     break;
                 case SERVICE_STATE_CHANGED:
                     ServiceState state = (ServiceState) ((AsyncResult) msg.obj).result;
@@ -835,16 +846,8 @@
                     mIgnoreRing = false;
                     mStopRing = false;
 
-                    // Ideally, we would like to set up the SCO channel
-                    // before sending the ring() so that we don't miss any
-                    // incall audio. However, some headsets don't play the
-                    // ringtone in such scenarios. So send the ring() first
-                    // and then setup SCO after a delay of 1 second.
+                    mAttemptDelayedScoConnection = true;
                     result.addResult(ring());
-
-                    Message msg = mHandler.obtainMessage(DELAYED_SCO_FOR_RINGTONE);
-                    mHandler.sendMessageDelayed(msg, 1000);
-
                 }
             }
             sendURC(result.toString());