Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 77e77ed4 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add new internal API: enableOptionalCodecs()/disableOptionalCodecs()"

parents 6c47eb9a 4316545f
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -237,6 +237,18 @@ public class A2dpService extends ProfileService {
        mStateMachine.setCodecConfigPreference(codecConfig);
    }

    public void enableOptionalCodecs() {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (DBG) Log.d(TAG, "enableOptionalCodecs()");
        mStateMachine.enableOptionalCodecs();
    }

    public void disableOptionalCodecs() {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (DBG) Log.d(TAG, "disableOptionalCodecs()");
        mStateMachine.disableOptionalCodecs();
    }

    //Binder object: Must be static class or memory leak may occur 
    private static class BluetoothA2dpBinder extends IBluetoothA2dp.Stub 
        implements IProfileServiceBinder {
@@ -340,6 +352,18 @@ public class A2dpService extends ProfileService {
            if (service == null) return;
            service.setCodecConfigPreference(codecConfig);
        }

        public void enableOptionalCodecs() {
            A2dpService service = getService();
            if (service == null) return;
            service.enableOptionalCodecs();
        }

        public void disableOptionalCodecs() {
            A2dpService service = getService();
            if (service == null) return;
            service.disableOptionalCodecs();
        }
    };

    @Override
+57 −17
Original line number Diff line number Diff line
@@ -125,7 +125,7 @@ final class A2dpStateMachine extends StateMachine {
        mService = svc;
        mContext = context;
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mCodecConfigPriorities = assignCodecConfigPriorities(context.getResources());
        mCodecConfigPriorities = assignCodecConfigPriorities();

        initNative(mCodecConfigPriorities);

@@ -155,7 +155,8 @@ final class A2dpStateMachine extends StateMachine {
    }

    // Assign the A2DP Source codec config priorities
    private BluetoothCodecConfig[] assignCodecConfigPriorities(Resources resources) {
    private BluetoothCodecConfig[] assignCodecConfigPriorities() {
        Resources resources = mContext.getResources();
        if (resources == null) {
            return null;
        }
@@ -215,29 +216,34 @@ final class A2dpStateMachine extends StateMachine {
        BluetoothCodecConfig[] codecConfigArray =
                new BluetoothCodecConfig[BluetoothCodecConfig.SOURCE_CODEC_TYPE_MAX];
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                mA2dpSourceCodecPrioritySbc, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPrioritySbc, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[0] = codecConfig;
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC,
                mA2dpSourceCodecPriorityAac, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPriorityAac, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[1] = codecConfig;
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX,
                mA2dpSourceCodecPriorityAptx, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPriorityAptx, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[2] = codecConfig;
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD,
                mA2dpSourceCodecPriorityAptxHd, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPriorityAptxHd, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[3] = codecConfig;
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC,
                mA2dpSourceCodecPriorityLdac, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPriorityLdac, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[4] = codecConfig;

        return codecConfigArray;
@@ -803,6 +809,40 @@ final class A2dpStateMachine extends StateMachine {
        setCodecConfigPreferenceNative(codecConfigArray);
    }

    void enableOptionalCodecs() {
        BluetoothCodecConfig[] codecConfigArray = assignCodecConfigPriorities();
        if (codecConfigArray == null) {
            return;
        }

        // Set the mandatory codec's priority to default, and remove the rest
        for (int i = 0; i < codecConfigArray.length; i++) {
            BluetoothCodecConfig codecConfig = codecConfigArray[i];
            if (!codecConfig.isMandatoryCodec()) {
                codecConfigArray[i] = null;
            }
        }

        setCodecConfigPreferenceNative(codecConfigArray);
    }

    void disableOptionalCodecs() {
        BluetoothCodecConfig[] codecConfigArray = assignCodecConfigPriorities();
        if (codecConfigArray == null) {
            return;
        }
        // Set the mandatory codec's priority to highest, and ignore the rest
        for (int i = 0; i < codecConfigArray.length; i++) {
            BluetoothCodecConfig codecConfig = codecConfigArray[i];
            if (codecConfig.isMandatoryCodec()) {
                codecConfig.setCodecPriority(BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST);
            } else {
                codecConfigArray[i] = null;
            }
        }
        setCodecConfigPreferenceNative(codecConfigArray);
    }

    boolean okToConnect(BluetoothDevice device) {
        AdapterService adapterService = AdapterService.getAdapterService();
        int priority = mService.getPriority(device);