Fix issue 2324029: In-call (and other) audio screwed up after using Pandora (ERD72).

The problem is due to spurious headset connection indications received from event observer when a 3.5mm headset w/o mic is connected.
In this particular case, The HeadsetObserver first received a valid indication of headset with mic connection, followed by a headset with mic disconnection and finally a headset w/o mic connection.
The HeadsetObserver delays the headset disconnection intent to leave time to music app to pause music before the output path is switched.
As the last headset w/o mic connection indication is received from the event observer before the intent corresponding to the spurious headset with mic disconnection is broadcast, the later is discarded. Results a state where the headset with mic is always considered as connected.

The fix consists in not canceling pending intents when a new headset state is received and carrying the HeadsetObserver state with the delayed message triggering the broacast of the disconnection intent.
diff --git a/services/java/com/android/server/HeadsetObserver.java b/services/java/com/android/server/HeadsetObserver.java
index 43de36b..a935131 100644
--- a/services/java/com/android/server/HeadsetObserver.java
+++ b/services/java/com/android/server/HeadsetObserver.java
@@ -49,7 +49,6 @@
     private int mHeadsetState;
     private int mPrevHeadsetState;
     private String mHeadsetName;
-    private boolean mPendingIntent;
 
     private final Context mContext;
     private final WakeLock mWakeLock;  // held while there is a pending route change
@@ -114,7 +113,6 @@
         mHeadsetName = newName;
         mPrevHeadsetState = mHeadsetState;
         mHeadsetState = headsetState;
-        mPendingIntent = true;
 
         if (headsetState == 0) {
             Intent intent = new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
@@ -126,25 +124,28 @@
             // This could be improved once the audio sub-system provides an
             // interface to clear the audio pipeline.
             mWakeLock.acquire();
-            mHandler.sendEmptyMessageDelayed(0, 1000);
+            mHandler.sendMessageDelayed(mHandler.obtainMessage(0,
+                                                               mHeadsetState,
+                                                               mPrevHeadsetState,
+                                                               mHeadsetName),
+                                        1000);
         } else {
-            sendIntents();
-            mPendingIntent = false;
+            sendIntents(mHeadsetState, mPrevHeadsetState, mHeadsetName);
         }
     }
 
-    private synchronized final void sendIntents() {
+    private synchronized final void sendIntents(int headsetState, int prevHeadsetState, String headsetName) {
         int allHeadsets = SUPPORTED_HEADSETS;
         for (int curHeadset = 1; allHeadsets != 0; curHeadset <<= 1) {
             if ((curHeadset & allHeadsets) != 0) {
-                sendIntent(curHeadset);
+                sendIntent(curHeadset, headsetState, prevHeadsetState, headsetName);
                 allHeadsets &= ~curHeadset;
             }
         }
     }
 
-    private final void sendIntent(int headset) {
-        if ((mHeadsetState & headset) != (mPrevHeadsetState & headset)) {
+    private final void sendIntent(int headset, int headsetState, int prevHeadsetState, String headsetName) {
+        if ((headsetState & headset) != (prevHeadsetState & headset)) {
             //  Pack up the values and broadcast them to everyone
             Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);
             intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
@@ -154,14 +155,14 @@
             if ((headset & HEADSETS_WITH_MIC) != 0) {
                 microphone = 1;
             }
-            if ((mHeadsetState & headset) != 0) {
+            if ((headsetState & headset) != 0) {
                 state = 1;
             }
             intent.putExtra("state", state);
-            intent.putExtra("name", mHeadsetName);
+            intent.putExtra("name", headsetName);
             intent.putExtra("microphone", microphone);
 
-            if (LOG) Log.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+mHeadsetName+" mic: "+microphone);
+            if (LOG) Log.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+headsetName+" mic: "+microphone);
             // TODO: Should we require a permission?
             ActivityManagerNative.broadcastStickyIntent(intent, null);
         }
@@ -170,12 +171,8 @@
     private final Handler mHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
-            if (mPendingIntent) {
-                sendIntents();
-                mPendingIntent = false;
-            }
+            sendIntents(msg.arg1, msg.arg2, (String)msg.obj);
             mWakeLock.release();
         }
     };
-
 }