Add persistent state for Bluetooth high quality audio support

Defines the flags used in code and the Settings.Global key names for
persisting the following state for Bluetooth A2DP Sink devices:
-Whether the device supports optional codecs or not
-Whether optional codecs should be turned on for the device

For each of these two properties we model the state in the code as
yes/no/unknown, so that we can tailor the UI and behavior
accordingly.

Bug=37441685
Test: manually

Change-Id: I6bcd02fd7c95bef989575f3b13d4788dab61971a
diff --git a/core/java/android/bluetooth/BluetoothA2dp.java b/core/java/android/bluetooth/BluetoothA2dp.java
index d1ad8de..c58eaa1 100644
--- a/core/java/android/bluetooth/BluetoothA2dp.java
+++ b/core/java/android/bluetooth/BluetoothA2dp.java
@@ -136,6 +136,38 @@
      */
     public static final int STATE_NOT_PLAYING   =  11;
 
+    /**
+     * We don't have a stored preference for whether or not the given A2DP sink device supports
+     * optional codecs.
+     * @hide */
+    public static final int OPTIONAL_CODECS_SUPPORT_UNKNOWN = -1;
+
+    /**
+     * The given A2DP sink device does not support optional codecs.
+     * @hide */
+    public static final int OPTIONAL_CODECS_NOT_SUPPORTED = 0;
+
+    /**
+     * The given A2DP sink device does support optional codecs.
+     * @hide */
+    public static final int OPTIONAL_CODECS_SUPPORTED = 1;
+
+    /**
+     * We don't have a stored preference for whether optional codecs should be enabled or disabled
+     * for the given A2DP device.
+     * @hide */
+    public static final int OPTIONAL_CODECS_PREF_UNKNOWN = -1;
+
+    /**
+     * Optional codecs should be disabled for the given A2DP device.
+     * @hide */
+    public static final int OPTIONAL_CODECS_PREF_DISABLED = 0;
+
+    /**
+     *  Optional codecs should be enabled for the given A2DP device.
+     *  @hide */
+    public static final int OPTIONAL_CODECS_PREF_ENABLED = 1;
+
     private Context mContext;
     private ServiceListener mServiceListener;
     private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock();
@@ -655,6 +687,88 @@
     }
 
     /**
+     * Returns whether this device supports optional codecs.
+     *
+     * @param device The device to check
+     * @return one of OPTIONAL_CODECS_SUPPORT_UNKNOWN, OPTIONAL_CODECS_NOT_SUPPORTED, or
+     *         OPTIONAL_CODECS_SUPPORTED.
+     *
+     * @hide
+     */
+    public int supportsOptionalCodecs(BluetoothDevice device) {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled() && isValidDevice(device)) {
+                return mService.supportsOptionalCodecs(device);
+            }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return OPTIONAL_CODECS_SUPPORT_UNKNOWN;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error talking to BT service in getSupportsOptionalCodecs()", e);
+            return OPTIONAL_CODECS_SUPPORT_UNKNOWN;
+        } finally {
+            mServiceLock.readLock().unlock();
+        }
+    }
+
+    /**
+     * Returns whether this device should have optional codecs enabled.
+     *
+     * @param device The device in question.
+     * @return one of OPTIONAL_CODECS_PREF_UNKNOWN, OPTIONAL_CODECS_PREF_ENABLED, or
+     *         OPTIONAL_CODECS_PREF_DISABLED.
+     *
+     * @hide
+     */
+    public int getOptionalCodecsEnabled(BluetoothDevice device) {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled() && isValidDevice(device)) {
+                return mService.getOptionalCodecsEnabled(device);
+            }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return OPTIONAL_CODECS_PREF_UNKNOWN;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error talking to BT service in getSupportsOptionalCodecs()", e);
+            return OPTIONAL_CODECS_PREF_UNKNOWN;
+        } finally {
+            mServiceLock.readLock().unlock();
+        }
+    }
+
+    /**
+     * Sets a persistent preference for whether a given device should have optional codecs enabled.
+     *
+     * @param device The device to set this preference for.
+     * @param value Whether the optional codecs should be enabled for this device.  This should be
+     *              one of OPTIONAL_CODECS_PREF_UNKNOWN, OPTIONAL_CODECS_PREF_ENABLED, or
+     *              OPTIONAL_CODECS_PREF_DISABLED.
+     * @hide
+     */
+    public void setOptionalCodecsEnabled(BluetoothDevice device, int value) {
+        try {
+            if (value != BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN &&
+                    value != BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED &&
+                    value != BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED) {
+                Log.e(TAG, "Invalid value passed to setOptionalCodecsEnabled: " + value);
+                return;
+            }
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()
+                    && isValidDevice(device)) {
+                mService.setOptionalCodecsEnabled(device, value);
+            }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return;
+        } finally {
+            mServiceLock.readLock().unlock();
+        }
+    }
+
+    /**
      * Helper for converting a state to a string.
      *
      * For debug use only - strings are not internationalized.