Implement setting/getting whether audio can be routed to the HFP HF device.

Bug: 25332357
Change-Id: I2d37d082673255a165dad955aa1ca4b9f00d599e
diff --git a/res/values/config.xml b/res/values/config.xml
index 79fe9ce..42ab4b9 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -41,4 +41,6 @@
     <integer name="gatt_high_priority_max_interval">12</integer>
     <integer name="gatt_low_power_min_interval">80</integer>
     <integer name="gatt_low_power_max_interval">100</integer>
+
+    <bool name="headset_client_initial_audio_route_allowed">true</bool>
 </resources>
diff --git a/src/com/android/bluetooth/hfpclient/HeadsetClientService.java b/src/com/android/bluetooth/hfpclient/HeadsetClientService.java
index d7eb12d..b0e026b 100644
--- a/src/com/android/bluetooth/hfpclient/HeadsetClientService.java
+++ b/src/com/android/bluetooth/hfpclient/HeadsetClientService.java
@@ -253,6 +253,24 @@
         }
 
         @Override
+        public void setAudioRouteAllowed(boolean allowed) {
+            HeadsetClientService service = getService();
+            if (service != null) {
+                service.setAudioRouteAllowed(allowed);
+            }
+        }
+
+        @Override
+        public boolean getAudioRouteAllowed() {
+            HeadsetClientService service = getService();
+            if (service != null) {
+                return service.getAudioRouteAllowed();
+            }
+
+            return false;
+        }
+
+        @Override
         public boolean connectAudio() {
             HeadsetClientService service = getService();
             if (service == null) {
@@ -543,6 +561,14 @@
         return mStateMachine.getAudioState(device);
     }
 
+    public void setAudioRouteAllowed(boolean allowed) {
+        mStateMachine.setAudioRouteAllowed(allowed);
+    }
+
+    public boolean getAudioRouteAllowed() {
+        return mStateMachine.getAudioRouteAllowed();
+    }
+
     boolean connectAudio() {
         enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission");
         if (!mStateMachine.isConnected()) {
diff --git a/src/com/android/bluetooth/hfpclient/HeadsetClientStateMachine.java b/src/com/android/bluetooth/hfpclient/HeadsetClientStateMachine.java
index f9cadab..9ae7ba3 100644
--- a/src/com/android/bluetooth/hfpclient/HeadsetClientStateMachine.java
+++ b/src/com/android/bluetooth/hfpclient/HeadsetClientStateMachine.java
@@ -68,6 +68,8 @@
 import java.util.Queue;
 import java.util.Set;
 
+import com.android.bluetooth.R;
+
 final class HeadsetClientStateMachine extends StateMachine {
     private static final String TAG = "HeadsetClientStateMachine";
     private static final boolean DBG = false;
@@ -142,6 +144,8 @@
 
     private final AudioManager mAudioManager;
     private int mAudioState;
+    // Indicates whether audio can be routed to the device.
+    private boolean mAudioRouteAllowed;
     private boolean mAudioWbs;
     private final BluetoothAdapter mAdapter;
     private boolean mNativeAvailable;
@@ -1210,6 +1214,9 @@
         mAudioState = BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED;
         mAudioWbs = false;
 
+        mAudioRouteAllowed = context.getResources().getBoolean(
+                R.bool.headset_client_initial_audio_route_allowed);
+
         mIndicatorNetworkState = HeadsetClientHalConstants.NETWORK_STATE_NOT_AVAILABLE;
         mIndicatorNetworkType = HeadsetClientHalConstants.SERVICE_TYPE_HOME;
         mIndicatorNetworkSignal = 0;
@@ -2016,6 +2023,11 @@
                     mAudioWbs = true;
                     // fall through
                 case HeadsetClientHalConstants.AUDIO_STATE_CONNECTED:
+                    if (!mAudioRouteAllowed) {
+                        sendMessage(HeadsetClientStateMachine.DISCONNECT_AUDIO);
+                        break;
+                    }
+
                     mAudioState = BluetoothHeadsetClient.STATE_AUDIO_CONNECTED;
                     // request audio focus for call
                     int newAudioMode = AudioManager.MODE_IN_CALL;
@@ -2365,6 +2377,14 @@
         return (getCurrentState() == mAudioOn);
     }
 
+    public void setAudioRouteAllowed(boolean allowed) {
+        mAudioRouteAllowed = allowed;
+    }
+
+    public boolean getAudioRouteAllowed() {
+        return mAudioRouteAllowed;
+    }
+
     synchronized int getAudioState(BluetoothDevice device) {
         if (mCurrentDevice == null || !mCurrentDevice.equals(device)) {
             return BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED;