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

Commit bd8c8fb8 authored by hjchangliao's avatar hjchangliao Committed by HJ ChangLiao
Browse files

Remove wrapper for BluetoothA2dp

Remove BluetoothA2dpWrapper,
Use BluetoothA2dp to replace all of them.

Bug: 76167422
Change-Id: I2a55677eb8ce4ef62008aa06929b0b78515b28e5
Test: RunSettingsLibRoboTests
parent 8e0b2a10
Loading
Loading
Loading
Loading
+7 −17
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothCodecConfig;
import android.bluetooth.BluetoothCodecStatus;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
@@ -28,9 +27,7 @@ import android.content.Context;
import android.os.ParcelUuid;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settingslib.R;
import com.android.settingslib.wrapper.BluetoothA2dpWrapper;

import java.util.ArrayList;
import java.util.Arrays;
@@ -43,7 +40,6 @@ public class A2dpProfile implements LocalBluetoothProfile {
    private Context mContext;

    private BluetoothA2dp mService;
    private BluetoothA2dpWrapper mServiceWrapper;
    private boolean mIsProfileReady;

    private final LocalBluetoothAdapter mLocalAdapter;
@@ -67,7 +63,6 @@ public class A2dpProfile implements LocalBluetoothProfile {
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (V) Log.d(TAG,"Bluetooth service connected");
            mService = (BluetoothA2dp) proxy;
            mServiceWrapper = new BluetoothA2dpWrapper(mService);
            // We just bound to the service, so refresh the UI for any connected A2DP devices.
            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
            while (!deviceList.isEmpty()) {
@@ -105,11 +100,6 @@ public class A2dpProfile implements LocalBluetoothProfile {
                BluetoothProfile.A2DP);
    }

    @VisibleForTesting
    void setBluetoothA2dpWrapper(BluetoothA2dpWrapper wrapper) {
        mServiceWrapper = wrapper;
    }

    public boolean isConnectable() {
        return true;
    }
@@ -203,12 +193,12 @@ public class A2dpProfile implements LocalBluetoothProfile {
    }

    public boolean supportsHighQualityAudio(BluetoothDevice device) {
        int support = mServiceWrapper.supportsOptionalCodecs(device);
        int support = mService.supportsOptionalCodecs(device);
        return support == BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED;
    }

    public boolean isHighQualityAudioEnabled(BluetoothDevice device) {
        int enabled = mServiceWrapper.getOptionalCodecsEnabled(device);
        int enabled = mService.getOptionalCodecsEnabled(device);
        if (enabled != BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN) {
            return enabled == BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED;
        } else if (getConnectionStatus(device) != BluetoothProfile.STATE_CONNECTED &&
@@ -219,8 +209,8 @@ public class A2dpProfile implements LocalBluetoothProfile {
            return true;
        }
        BluetoothCodecConfig codecConfig = null;
        if (mServiceWrapper.getCodecStatus(device) != null) {
            codecConfig = mServiceWrapper.getCodecStatus(device).getCodecConfig();
        if (mService.getCodecStatus(device) != null) {
            codecConfig = mService.getCodecStatus(device).getCodecConfig();
        }
        if (codecConfig != null)  {
            return !codecConfig.isMandatoryCodec();
@@ -233,7 +223,7 @@ public class A2dpProfile implements LocalBluetoothProfile {
        int prefValue = enabled
                ? BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED
                : BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED;
        mServiceWrapper.setOptionalCodecsEnabled(device, prefValue);
        mService.setOptionalCodecsEnabled(device, prefValue);
        if (getConnectionStatus(device) != BluetoothProfile.STATE_CONNECTED) {
            return;
        }
@@ -253,8 +243,8 @@ public class A2dpProfile implements LocalBluetoothProfile {
        // We want to get the highest priority codec, since that's the one that will be used with
        // this device, and see if it is high-quality (ie non-mandatory).
        BluetoothCodecConfig[] selectable = null;
        if (mServiceWrapper.getCodecStatus(device) != null) {
            selectable = mServiceWrapper.getCodecStatus(device).getCodecsSelectableCapabilities();
        if (mService.getCodecStatus(device) != null) {
            selectable = mService.getCodecStatus(device).getCodecsSelectableCapabilities();
            // To get the highest priority, we sort in reverse.
            Arrays.sort(selectable,
                    (a, b) -> {
+0 −70
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.wrapper;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothCodecStatus;
import android.bluetooth.BluetoothDevice;

/**
 * This class replicates some methods of android.bluetooth.BluetoothA2dp that are new and not
 * yet available in our current version of Robolectric. It provides a thin wrapper to call the real
 * methods in production and a mock in tests.
 */
public class BluetoothA2dpWrapper {

    private BluetoothA2dp mService;

    public BluetoothA2dpWrapper(BluetoothA2dp service) {
        mService = service;
    }

    /**
     * @return the real {@code BluetoothA2dp} object
     */
    public BluetoothA2dp getService() {
        return mService;
    }

    /**
     * Wraps {@code BluetoothA2dp.getCodecStatus}
     */
    public BluetoothCodecStatus getCodecStatus(BluetoothDevice device) {
        return mService.getCodecStatus(device);
    }

    /**
     * Wraps {@code BluetoothA2dp.supportsOptionalCodecs}
     */
    public int supportsOptionalCodecs(BluetoothDevice device) {
        return mService.supportsOptionalCodecs(device);
    }

    /**
     * Wraps {@code BluetoothA2dp.getOptionalCodecsEnabled}
     */
    public int getOptionalCodecsEnabled(BluetoothDevice device) {
        return mService.getOptionalCodecsEnabled(device);
    }

    /**
     * Wraps {@code BluetoothA2dp.setOptionalCodecsEnabled}
     */
    public void setOptionalCodecsEnabled(BluetoothDevice device, int value) {
        mService.setOptionalCodecsEnabled(device, value);
    }
}
+13 −16
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import android.content.Context;
import android.content.res.Resources;

import com.android.settingslib.R;
import com.android.settingslib.wrapper.BluetoothA2dpWrapper;

import org.junit.Before;
import org.junit.Test;
@@ -49,7 +48,6 @@ public class A2dpProfileTest {
    @Mock LocalBluetoothProfileManager mProfileManager;
    @Mock BluetoothDevice mDevice;
    @Mock BluetoothA2dp mBluetoothA2dp;
    @Mock BluetoothA2dpWrapper mBluetoothA2dpWrapper;
    BluetoothProfile.ServiceListener mServiceListener;

    A2dpProfile mProfile;
@@ -68,31 +66,30 @@ public class A2dpProfileTest {

        mProfile = new A2dpProfile(mContext, mAdapter, mDeviceManager, mProfileManager);
        mServiceListener.onServiceConnected(BluetoothProfile.A2DP, mBluetoothA2dp);
        mProfile.setBluetoothA2dpWrapper(mBluetoothA2dpWrapper);
    }

    @Test
    public void supportsHighQualityAudio() {
        when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
        when(mBluetoothA2dp.supportsOptionalCodecs(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
        assertThat(mProfile.supportsHighQualityAudio(mDevice)).isTrue();

        when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
        when(mBluetoothA2dp.supportsOptionalCodecs(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
        assertThat(mProfile.supportsHighQualityAudio(mDevice)).isFalse();

        when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
        when(mBluetoothA2dp.supportsOptionalCodecs(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_SUPPORT_UNKNOWN);
        assertThat(mProfile.supportsHighQualityAudio(mDevice)).isFalse();
    }

    @Test
    public void isHighQualityAudioEnabled() {
        when(mBluetoothA2dpWrapper.getOptionalCodecsEnabled(any())).thenReturn(
        when(mBluetoothA2dp.getOptionalCodecsEnabled(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED);
        assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue();

        when(mBluetoothA2dpWrapper.getOptionalCodecsEnabled(any())).thenReturn(
        when(mBluetoothA2dp.getOptionalCodecsEnabled(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED);
        assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse();

@@ -100,23 +97,23 @@ public class A2dpProfileTest {
        // then isHighQualityAudioEnabled() should return true or false based on whether optional
        // codecs are supported. If the device is connected then we should ask it directly, but if
        // the device isn't connected then rely on the stored pref about such support.
        when(mBluetoothA2dpWrapper.getOptionalCodecsEnabled(any())).thenReturn(
        when(mBluetoothA2dp.getOptionalCodecsEnabled(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);
        when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
                BluetoothProfile.STATE_DISCONNECTED);

        when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
        when(mBluetoothA2dp.supportsOptionalCodecs(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
        assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse();

        when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
        when(mBluetoothA2dp.supportsOptionalCodecs(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
        assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue();

        when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
                BluetoothProfile.STATE_CONNECTED);
        BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
        when(mBluetoothA2dpWrapper.getCodecStatus(mDevice)).thenReturn(status);
        when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status);
        BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
        when(status.getCodecConfig()).thenReturn(config);
        when(config.isMandatoryCodec()).thenReturn(false);
@@ -151,14 +148,14 @@ public class A2dpProfileTest {

        // Most tests want to simulate optional codecs being supported by the device, so do that
        // by default here.
        when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
        when(mBluetoothA2dp.supportsOptionalCodecs(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
    }

    @Test
    public void getLableCodecsNotSupported() {
        setupLabelTest();
        when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
        when(mBluetoothA2dp.supportsOptionalCodecs(any())).thenReturn(
                BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
        assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(UNKNOWN_CODEC_LABEL);
    }
@@ -179,7 +176,7 @@ public class A2dpProfileTest {
        BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
        BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
        BluetoothCodecConfig[] configs = {config};
        when(mBluetoothA2dpWrapper.getCodecStatus(mDevice)).thenReturn(status);
        when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status);
        when(status.getCodecsSelectableCapabilities()).thenReturn(configs);

        when(config.isMandatoryCodec()).thenReturn(true);
@@ -194,7 +191,7 @@ public class A2dpProfileTest {
        BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
        BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
        BluetoothCodecConfig[] configs = {config};
        when(mBluetoothA2dpWrapper.getCodecStatus(mDevice)).thenReturn(status);
        when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status);
        when(status.getCodecsSelectableCapabilities()).thenReturn(configs);

        when(config.isMandatoryCodec()).thenReturn(false);