Don't display gateway number on bluetooth

Display actual contact phone number instead of the Google voice
gateway number when there is a bluetooth connection with eg a car display.

Bug:11881222
Change-Id: I1a5b9d3dee4a490905e6e4b52745972fd63d81a1
(cherry picked from commit 21a7534fa69c1cc10809ae33fa0bfa84c5ac1309)
diff --git a/src/com/android/phone/BluetoothPhoneService.java b/src/com/android/phone/BluetoothPhoneService.java
index ed6f37e..be6c1cc 100644
--- a/src/com/android/phone/BluetoothPhoneService.java
+++ b/src/com/android/phone/BluetoothPhoneService.java
@@ -42,6 +42,8 @@
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.telephony.CallManager;
 
+import com.android.phone.CallGatewayManager.RawGatewayInfo;
+
 import java.io.IOException;
 import java.util.LinkedList;
 import java.util.List;
@@ -60,6 +62,7 @@
 
     private BluetoothAdapter mAdapter;
     private CallManager mCM;
+    private CallGatewayManager mCallGatewayManager;
 
     private BluetoothHeadset mBluetoothHeadset;
 
@@ -104,6 +107,7 @@
             if (VDBG) Log.d(TAG, "mAdapter null");
             return;
         }
+        mCallGatewayManager = CallGatewayManager.getInstance();
 
         mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
         mStartCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
@@ -521,16 +525,26 @@
             mpty = call.isMultiparty();
         }
 
-        int direction = connection.isIncoming() ? 1 : 0;
+        boolean isIncoming = connection.isIncoming();
 
+        // For GV outgoing calls send the contact phone #, not the gateway #.
         String number = connection.getAddress();
+        if (!isIncoming) {
+            RawGatewayInfo rawInfo = mCallGatewayManager.getGatewayInfo(connection);
+            if (!rawInfo.isEmpty()) {
+                number = rawInfo.trueNumber;
+            }
+        }
         int type = -1;
         if (number != null) {
             type = PhoneNumberUtils.toaFromString(number);
+        } else {
+            number = "";
         }
 
         if (mBluetoothHeadset != null) {
-            mBluetoothHeadset.clccResponse(index + 1, direction, state, 0, mpty, number, type);
+            mBluetoothHeadset.clccResponse(index + 1, isIncoming ? 1 : 0,
+                    state, 0, mpty, number, type);
         }
     }
 
@@ -654,9 +668,16 @@
                 // as per Bluetooth SIG PTS
         }
 
-        int direction = connection.isIncoming() ? 1 : 0;
+        boolean isIncoming = connection.isIncoming();
 
+        // For GV outgoing calls send the contact phone #, not the gateway #.
         String number = connection.getAddress();
+        if (!isIncoming) {
+            RawGatewayInfo rawInfo = mCallGatewayManager.getGatewayInfo(connection);
+            if (!rawInfo.isEmpty()) {
+                number = rawInfo.trueNumber;
+            }
+        }
         int type = -1;
         if (number != null) {
             type = PhoneNumberUtils.toaFromString(number);
@@ -665,7 +686,8 @@
         }
 
         if (mBluetoothHeadset != null) {
-            mBluetoothHeadset.clccResponse(index + 1, direction, state, 0, mpty, number, type);
+            mBluetoothHeadset.clccResponse(index + 1, isIncoming ? 1 : 0,
+                    state, 0, mpty, number, type);
         }
     }
 
diff --git a/src/com/android/phone/CallGatewayManager.java b/src/com/android/phone/CallGatewayManager.java
index 6fe2444..81bee07 100644
--- a/src/com/android/phone/CallGatewayManager.java
+++ b/src/com/android/phone/CallGatewayManager.java
@@ -25,7 +25,7 @@
 import com.android.internal.telephony.Connection;
 import com.google.android.collect.Maps;
 
-import java.util.HashMap;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * This class manages gateway information for outgoing calls. When calls are made, they may contain
@@ -68,9 +68,19 @@
 
     public static final RawGatewayInfo EMPTY_INFO = new RawGatewayInfo(null, null, null);
 
-    private final HashMap<Connection, RawGatewayInfo> mMap = Maps.newHashMap();
+    private final ConcurrentHashMap<Connection, RawGatewayInfo> mMap =
+        new ConcurrentHashMap<Connection, RawGatewayInfo>(4, 0.9f, 1);
 
-    public CallGatewayManager() {
+    private static CallGatewayManager sSingleton;
+
+    public static synchronized CallGatewayManager getInstance() {
+        if (sSingleton == null) {
+            sSingleton = new CallGatewayManager();
+        }
+        return sSingleton;
+    }
+
+    private CallGatewayManager() {
     }
 
     /**
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index 3f35900..1ce99a3 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -447,7 +447,7 @@
 
             CallLogger callLogger = new CallLogger(this, new CallLogAsync());
 
-            callGatewayManager = new CallGatewayManager();
+            callGatewayManager = CallGatewayManager.getInstance();
 
             // Create the CallController singleton, which is the interface
             // to the telephony layer for user-initiated telephony functionality