Bluetooth indicator should depends on call state

Add CallModel listener to BluetoothManager so that it can alter the
bluetooth state when calls change.

Change-Id: Idf0b9a4b8b2247ac5ddf1fcc1d8359c85bb299da
(cherry picked from commit 9f8953efa3983a1cd3f8411636300deab6ad3789)
diff --git a/src/com/android/phone/AudioRouter.java b/src/com/android/phone/AudioRouter.java
index 812d92c..517f777 100644
--- a/src/com/android/phone/AudioRouter.java
+++ b/src/com/android/phone/AudioRouter.java
@@ -253,7 +253,7 @@
         int mode = AudioMode.EARPIECE;
 
         // There is a very specific ordering here
-        if (mBluetoothManager.isBluetoothAudioConnectedOrPending()) {
+        if (mBluetoothManager.showBluetoothIndication()) {
             mode = AudioMode.BLUETOOTH;
         } else if (PhoneUtils.isSpeakerOn(mContext)) {
             mode = AudioMode.SPEAKER;
@@ -287,13 +287,12 @@
         // only update if it really changed.
         if (mAudioMode != mode) {
             Log.i(LOG_TAG, "Audio mode changing to " + AudioMode.toString(mode));
-
-            mPreviousMode = mAudioMode;
-            mAudioMode = mode;
-
             doNotify = true;
         }
 
+        mPreviousMode = mAudioMode;
+        mAudioMode = mode;
+
         if (doNotify) {
             notifyListeners();
         }
diff --git a/src/com/android/phone/BluetoothManager.java b/src/com/android/phone/BluetoothManager.java
index 6f26b95..f5106c8 100644
--- a/src/com/android/phone/BluetoothManager.java
+++ b/src/com/android/phone/BluetoothManager.java
@@ -34,7 +34,9 @@
 import android.util.Log;
 
 import com.android.internal.telephony.CallManager;
+import com.android.services.telephony.common.Call;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -42,7 +44,7 @@
  * overall audio state for use in the UI layer. Also provides method for connecting the bluetooth
  * headset to the phone call.
  */
-public class BluetoothManager {
+public class BluetoothManager implements CallModeler.Listener {
     private static final String LOG_TAG = BluetoothManager.class.getSimpleName();
     private static final boolean DBG =
             (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
@@ -51,6 +53,7 @@
     private final BluetoothAdapter mBluetoothAdapter;
     private final CallManager mCallManager;
     private final Context mContext;
+    private final CallModeler mCallModeler;
 
     private BluetoothHeadset mBluetoothHeadset;
     private int mBluetoothHeadsetState = BluetoothProfile.STATE_DISCONNECTED;
@@ -64,9 +67,10 @@
 
     private final List<BluetoothIndicatorListener> mListeners = Lists.newArrayList();
 
-    public BluetoothManager(Context context, CallManager callManager) {
+    public BluetoothManager(Context context, CallManager callManager, CallModeler callModeler) {
         mContext = context;
         mCallManager = callManager;
+        mCallModeler = callModeler;
         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 
         init(mContext);
@@ -251,6 +255,8 @@
                 new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
         intentFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
         context.registerReceiver(mReceiver, intentFilter);
+
+        mCallModeler.addListener(this);
     }
 
     private void tearDown() {
@@ -396,6 +402,23 @@
         }
     }
 
+    @Override
+    public void onDisconnect(Call call) {
+        updateBluetoothIndication();
+    }
+
+    @Override
+    public void onIncoming(Call call, ArrayList<String> messages) {
+        // An incoming call can affect bluetooth indicator, so we update it whenever there is
+        // a change to any of the calls.
+        updateBluetoothIndication();
+    }
+
+    @Override
+    public void onUpdate(List<Call> calls, boolean fullUpdate) {
+        updateBluetoothIndication();
+    }
+
     private void log(String msg) {
         Log.d(LOG_TAG, msg);
     }
diff --git a/src/com/android/phone/CallCard.java b/src/com/android/phone/CallCard.java
index 912af73..fcef7f3 100644
--- a/src/com/android/phone/CallCard.java
+++ b/src/com/android/phone/CallCard.java
@@ -809,6 +809,7 @@
 
         // Check a couple of other special cases (these are all CDMA-specific).
 
+        // TODO(klp): This code should go into the CallModeler logic instead of the UI.
         if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
             if ((state == Call.State.ACTIVE)
                 && mApplication.cdmaPhoneCallState.IsThreeWayCallOrigStateDialing()) {
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index b09789a..b9cd6c3 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -463,12 +463,6 @@
                 mBluetoothPhone = null;
             }
 
-            // Bluetooth manager
-            bluetoothManager = new BluetoothManager(this, mCM);
-            bluetoothManager.addBluetoothIndicatorListener(this);
-
-            ringer = Ringer.init(this, bluetoothManager);
-
             // before registering for phone state changes
             mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
             mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, LOG_TAG);
@@ -533,6 +527,12 @@
             wiredHeadsetManager = new WiredHeadsetManager(this);
             wiredHeadsetManager.addWiredHeadsetListener(this);
 
+            // Bluetooth manager
+            bluetoothManager = new BluetoothManager(this, mCM, callModeler);
+            bluetoothManager.addBluetoothIndicatorListener(this);
+
+            ringer = Ringer.init(this, bluetoothManager);
+
             // Audio router
             audioRouter = new AudioRouter(this, bluetoothManager, wiredHeadsetManager, mCM);