Remove BluetoothMediaManager
Remove BluetoothMediaManager from SettingsLib, because we use Media router to get bluetooth device. Bug: 152722071 Test: Build pass Change-Id: I72d5e10f1aa359b966a88b231da9b5b41efcd886
This commit is contained in:
@@ -1,348 +0,0 @@
|
||||
/*
|
||||
* Copyright 2018 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.media;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settingslib.bluetooth.A2dpProfile;
|
||||
import com.android.settingslib.bluetooth.BluetoothCallback;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
|
||||
import com.android.settingslib.bluetooth.HearingAidProfile;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* BluetoothMediaManager provide interface to get Bluetooth device list.
|
||||
*/
|
||||
public class BluetoothMediaManager extends MediaManager implements BluetoothCallback,
|
||||
LocalBluetoothProfileManager.ServiceListener {
|
||||
|
||||
private static final String TAG = "BluetoothMediaManager";
|
||||
|
||||
private final DeviceAttributeChangeCallback mDeviceAttributeChangeCallback =
|
||||
new DeviceAttributeChangeCallback();
|
||||
|
||||
private LocalBluetoothManager mLocalBluetoothManager;
|
||||
private LocalBluetoothProfileManager mProfileManager;
|
||||
private CachedBluetoothDeviceManager mCachedBluetoothDeviceManager;
|
||||
|
||||
private MediaDevice mLastAddedDevice;
|
||||
private MediaDevice mLastRemovedDevice;
|
||||
|
||||
private boolean mIsA2dpProfileReady = false;
|
||||
private boolean mIsHearingAidProfileReady = false;
|
||||
|
||||
BluetoothMediaManager(Context context, LocalBluetoothManager localBluetoothManager,
|
||||
Notification notification) {
|
||||
super(context, notification);
|
||||
|
||||
mLocalBluetoothManager = localBluetoothManager;
|
||||
mProfileManager = mLocalBluetoothManager.getProfileManager();
|
||||
mCachedBluetoothDeviceManager = mLocalBluetoothManager.getCachedDeviceManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startScan() {
|
||||
mLocalBluetoothManager.getEventManager().registerCallback(this);
|
||||
buildBluetoothDeviceList();
|
||||
dispatchDeviceListAdded();
|
||||
addServiceListenerIfNecessary();
|
||||
}
|
||||
|
||||
private void addServiceListenerIfNecessary() {
|
||||
// The profile may not ready when calling startScan().
|
||||
// Device status are all disconnected since profiles are not ready to connected.
|
||||
// In this case, we observe onServiceConnected() in LocalBluetoothProfileManager.
|
||||
// When A2dpProfile or HearingAidProfile is connected will call buildBluetoothDeviceList()
|
||||
// again to find the connected devices.
|
||||
if (!mIsA2dpProfileReady || !mIsHearingAidProfileReady) {
|
||||
mProfileManager.addServiceListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void buildBluetoothDeviceList() {
|
||||
mMediaDevices.clear();
|
||||
addConnectableA2dpDevices();
|
||||
addConnectableHearingAidDevices();
|
||||
}
|
||||
|
||||
private void addConnectableA2dpDevices() {
|
||||
final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
|
||||
if (a2dpProfile == null) {
|
||||
Log.w(TAG, "addConnectableA2dpDevices() a2dp profile is null!");
|
||||
return;
|
||||
}
|
||||
|
||||
final List<BluetoothDevice> devices = a2dpProfile.getConnectableDevices();
|
||||
|
||||
for (BluetoothDevice device : devices) {
|
||||
final CachedBluetoothDevice cachedDevice =
|
||||
mCachedBluetoothDeviceManager.findDevice(device);
|
||||
|
||||
if (cachedDevice == null) {
|
||||
Log.w(TAG, "Can't found CachedBluetoothDevice : " + device.getName());
|
||||
continue;
|
||||
}
|
||||
|
||||
Log.d(TAG, "addConnectableA2dpDevices() device : " + cachedDevice.getName()
|
||||
+ ", is connected : " + cachedDevice.isConnected()
|
||||
+ ", is enabled : " + a2dpProfile.isEnabled(device));
|
||||
|
||||
if (a2dpProfile.isEnabled(device)
|
||||
&& BluetoothDevice.BOND_BONDED == cachedDevice.getBondState()) {
|
||||
addMediaDevice(cachedDevice);
|
||||
}
|
||||
}
|
||||
|
||||
mIsA2dpProfileReady = a2dpProfile.isProfileReady();
|
||||
}
|
||||
|
||||
private void addConnectableHearingAidDevices() {
|
||||
final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
|
||||
if (hapProfile == null) {
|
||||
Log.w(TAG, "addConnectableHearingAidDevices() hap profile is null!");
|
||||
return;
|
||||
}
|
||||
|
||||
final List<BluetoothDevice> devices = hapProfile.getConnectableDevices();
|
||||
for (BluetoothDevice device : devices) {
|
||||
// Only add master HearingAid device, ignore sub
|
||||
if (mCachedBluetoothDeviceManager.isSubDevice(device)) {
|
||||
Log.w(TAG, "Sub hearingAid device: " + device.getName());
|
||||
continue;
|
||||
}
|
||||
final CachedBluetoothDevice cachedDevice =
|
||||
mCachedBluetoothDeviceManager.findDevice(device);
|
||||
|
||||
if (cachedDevice == null) {
|
||||
Log.w(TAG, "Can't found CachedBluetoothDevice : " + device.getName());
|
||||
continue;
|
||||
}
|
||||
|
||||
Log.d(TAG, "addConnectableHearingAidDevices() device : " + cachedDevice.getName()
|
||||
+ ", is connected : " + cachedDevice.isConnected()
|
||||
+ ", is enabled : " + hapProfile.isEnabled(device));
|
||||
|
||||
if (hapProfile.isEnabled(device)
|
||||
&& BluetoothDevice.BOND_BONDED == cachedDevice.getBondState()) {
|
||||
addMediaDevice(cachedDevice);
|
||||
}
|
||||
}
|
||||
|
||||
mIsHearingAidProfileReady = hapProfile.isProfileReady();
|
||||
}
|
||||
|
||||
private void addMediaDevice(CachedBluetoothDevice cachedDevice) {
|
||||
MediaDevice mediaDevice = findMediaDevice(MediaDeviceUtils.getId(cachedDevice));
|
||||
if (mediaDevice == null) {
|
||||
mediaDevice = new BluetoothMediaDevice(mContext, cachedDevice, null, null, null);
|
||||
cachedDevice.registerCallback(mDeviceAttributeChangeCallback);
|
||||
mLastAddedDevice = mediaDevice;
|
||||
mMediaDevices.add(mediaDevice);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopScan() {
|
||||
mLocalBluetoothManager.getEventManager().unregisterCallback(this);
|
||||
unregisterDeviceAttributeChangeCallback();
|
||||
}
|
||||
|
||||
private void unregisterDeviceAttributeChangeCallback() {
|
||||
for (MediaDevice device : mMediaDevices) {
|
||||
((BluetoothMediaDevice) device).getCachedDevice()
|
||||
.unregisterCallback(mDeviceAttributeChangeCallback);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBluetoothStateChanged(int bluetoothState) {
|
||||
if (BluetoothAdapter.STATE_ON == bluetoothState) {
|
||||
buildBluetoothDeviceList();
|
||||
dispatchDeviceListAdded();
|
||||
addServiceListenerIfNecessary();
|
||||
} else if (BluetoothAdapter.STATE_OFF == bluetoothState) {
|
||||
final List<MediaDevice> removeDevicesList = new ArrayList<>();
|
||||
for (MediaDevice device : mMediaDevices) {
|
||||
((BluetoothMediaDevice) device).getCachedDevice()
|
||||
.unregisterCallback(mDeviceAttributeChangeCallback);
|
||||
removeDevicesList.add(device);
|
||||
}
|
||||
mMediaDevices.removeAll(removeDevicesList);
|
||||
dispatchDeviceListRemoved(removeDevicesList);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAudioModeChanged() {
|
||||
dispatchDataChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
|
||||
if (isCachedDeviceConnected(cachedDevice)) {
|
||||
addMediaDevice(cachedDevice);
|
||||
dispatchDeviceAdded(cachedDevice);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isCachedDeviceConnected(CachedBluetoothDevice cachedDevice) {
|
||||
final boolean isConnectedHearingAidDevice = cachedDevice.isConnectedHearingAidDevice();
|
||||
final boolean isConnectedA2dpDevice = cachedDevice.isConnectedA2dpDevice();
|
||||
Log.d(TAG, "isCachedDeviceConnected() cachedDevice : " + cachedDevice
|
||||
+ ", is hearing aid connected : " + isConnectedHearingAidDevice
|
||||
+ ", is a2dp connected : " + isConnectedA2dpDevice);
|
||||
|
||||
return isConnectedHearingAidDevice || isConnectedA2dpDevice;
|
||||
}
|
||||
|
||||
private void dispatchDeviceAdded(CachedBluetoothDevice cachedDevice) {
|
||||
if (mLastAddedDevice != null
|
||||
&& MediaDeviceUtils.getId(cachedDevice) == mLastAddedDevice.getId()) {
|
||||
dispatchDeviceAdded(mLastAddedDevice);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
|
||||
if (!isCachedDeviceConnected(cachedDevice)) {
|
||||
removeMediaDevice(cachedDevice);
|
||||
dispatchDeviceRemoved(cachedDevice);
|
||||
}
|
||||
}
|
||||
|
||||
private void removeMediaDevice(CachedBluetoothDevice cachedDevice) {
|
||||
final MediaDevice mediaDevice = findMediaDevice(MediaDeviceUtils.getId(cachedDevice));
|
||||
if (mediaDevice != null) {
|
||||
cachedDevice.unregisterCallback(mDeviceAttributeChangeCallback);
|
||||
mLastRemovedDevice = mediaDevice;
|
||||
mMediaDevices.remove(mediaDevice);
|
||||
}
|
||||
}
|
||||
|
||||
void dispatchDeviceRemoved(CachedBluetoothDevice cachedDevice) {
|
||||
if (mLastRemovedDevice != null
|
||||
&& MediaDeviceUtils.getId(cachedDevice) == mLastRemovedDevice.getId()) {
|
||||
dispatchDeviceRemoved(mLastRemovedDevice);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
|
||||
int bluetoothProfile) {
|
||||
Log.d(TAG, "onProfileConnectionStateChanged() device: " + cachedDevice
|
||||
+ ", state: " + state + ", bluetoothProfile: " + bluetoothProfile);
|
||||
|
||||
updateMediaDeviceListIfNecessary(cachedDevice);
|
||||
}
|
||||
|
||||
private void updateMediaDeviceListIfNecessary(CachedBluetoothDevice cachedDevice) {
|
||||
if (BluetoothDevice.BOND_NONE == cachedDevice.getBondState()) {
|
||||
removeMediaDevice(cachedDevice);
|
||||
dispatchDeviceRemoved(cachedDevice);
|
||||
} else {
|
||||
if (findMediaDevice(MediaDeviceUtils.getId(cachedDevice)) != null) {
|
||||
dispatchDataChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAclConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
|
||||
Log.d(TAG, "onAclConnectionStateChanged() device: " + cachedDevice + ", state: " + state);
|
||||
|
||||
updateMediaDeviceListIfNecessary(cachedDevice);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
|
||||
Log.d(TAG, "onActiveDeviceChanged : device : "
|
||||
+ activeDevice + ", profile : " + bluetoothProfile);
|
||||
|
||||
if (BluetoothProfile.HEARING_AID == bluetoothProfile) {
|
||||
dispatchConnectedDeviceChanged(activeDevice == null
|
||||
? PhoneMediaDevice.ID : MediaDeviceUtils.getId(activeDevice));
|
||||
} else if (BluetoothProfile.A2DP == bluetoothProfile) {
|
||||
// When active device change to Hearing Aid,
|
||||
// BluetoothEventManager also send onActiveDeviceChanged() to notify that active device
|
||||
// of A2DP profile is null. To handle this case, check hearing aid device
|
||||
// is active device or not
|
||||
final MediaDevice activeHearingAidDevice = findActiveHearingAidDevice();
|
||||
final String id = activeDevice == null
|
||||
? activeHearingAidDevice == null
|
||||
? PhoneMediaDevice.ID : activeHearingAidDevice.getId()
|
||||
: MediaDeviceUtils.getId(activeDevice);
|
||||
dispatchConnectedDeviceChanged(id);
|
||||
}
|
||||
}
|
||||
|
||||
private MediaDevice findActiveHearingAidDevice() {
|
||||
final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
|
||||
|
||||
if (hearingAidProfile == null) {
|
||||
Log.e(TAG, "findActiveHearingAidDevice: hearingAidProfile == null");
|
||||
return null;
|
||||
}
|
||||
final List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
|
||||
for (BluetoothDevice btDevice : activeDevices) {
|
||||
final MediaDevice mediaDevice =
|
||||
findMediaDevice(Long.toString(hearingAidProfile.getHiSyncId(btDevice)));
|
||||
if (mediaDevice != null) {
|
||||
return mediaDevice;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceConnected() {
|
||||
if (!mIsA2dpProfileReady || !mIsHearingAidProfileReady) {
|
||||
buildBluetoothDeviceList();
|
||||
dispatchDeviceListAdded();
|
||||
}
|
||||
|
||||
//Remove the listener once a2dpProfile and hearingAidProfile are ready.
|
||||
if (mIsA2dpProfileReady && mIsHearingAidProfileReady) {
|
||||
mProfileManager.removeServiceListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This callback is for update {@link BluetoothMediaDevice} summary when
|
||||
* {@link CachedBluetoothDevice} connection state is changed.
|
||||
*/
|
||||
private class DeviceAttributeChangeCallback implements CachedBluetoothDevice.Callback {
|
||||
|
||||
@Override
|
||||
public void onDeviceAttributesChanged() {
|
||||
dispatchDataChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,467 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 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.media;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settingslib.bluetooth.A2dpProfile;
|
||||
import com.android.settingslib.bluetooth.BluetoothEventManager;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
|
||||
import com.android.settingslib.bluetooth.HearingAidProfile;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BluetoothMediaManagerTest {
|
||||
|
||||
private static final String TEST_ADDRESS = "11:22:33:44:55:66";
|
||||
|
||||
@Mock
|
||||
private LocalBluetoothManager mLocalBluetoothManager;
|
||||
@Mock
|
||||
private LocalBluetoothProfileManager mProfileManager;
|
||||
@Mock
|
||||
private A2dpProfile mA2dpProfile;
|
||||
@Mock
|
||||
private HearingAidProfile mHapProfile;
|
||||
@Mock
|
||||
private CachedBluetoothDeviceManager mCachedDeviceManager;
|
||||
@Mock
|
||||
private BluetoothEventManager mEventManager;
|
||||
@Mock
|
||||
private MediaManager.MediaDeviceCallback mCallback;
|
||||
|
||||
private BluetoothMediaManager mMediaManager;
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
|
||||
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
|
||||
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
|
||||
when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
|
||||
when(mProfileManager.getHearingAidProfile()).thenReturn(mHapProfile);
|
||||
when(mLocalBluetoothManager.getEventManager()).thenReturn(mEventManager);
|
||||
|
||||
mMediaManager = new BluetoothMediaManager(mContext, mLocalBluetoothManager, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startScan_haveA2dpProfileDeviceIsPreferredAndBonded_shouldAddDevice() {
|
||||
final List<BluetoothDevice> devices = new ArrayList<>();
|
||||
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
|
||||
devices.add(bluetoothDevice);
|
||||
|
||||
when(mA2dpProfile.getConnectableDevices()).thenReturn(devices);
|
||||
when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice);
|
||||
when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
|
||||
when(mA2dpProfile.isEnabled(bluetoothDevice)).thenReturn(true);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
mMediaManager.startScan();
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(devices.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startScan_haveA2dpProfileDeviceIsPreferredAndBondNone_shouldNotAddDevice() {
|
||||
final List<BluetoothDevice> devices = new ArrayList<>();
|
||||
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
|
||||
devices.add(bluetoothDevice);
|
||||
|
||||
when(mA2dpProfile.getConnectableDevices()).thenReturn(devices);
|
||||
when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice);
|
||||
when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
|
||||
when(mA2dpProfile.isEnabled(bluetoothDevice)).thenReturn(true);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
mMediaManager.startScan();
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startScan_noA2dpProfileBluetoothDevice_shouldNotAddDevice() {
|
||||
final List<BluetoothDevice> devices = new ArrayList<>();
|
||||
|
||||
when(mA2dpProfile.getConnectableDevices()).thenReturn(devices);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
mMediaManager.startScan();
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startScan_haveHapProfileDeviceIsPreferredAndBonded_shouldAddDevice() {
|
||||
final List<BluetoothDevice> devices = new ArrayList<>();
|
||||
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
|
||||
devices.add(bluetoothDevice);
|
||||
|
||||
when(mHapProfile.getConnectableDevices()).thenReturn(devices);
|
||||
when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice);
|
||||
when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
|
||||
when(mHapProfile.isEnabled(bluetoothDevice)).thenReturn(true);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
mMediaManager.startScan();
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(devices.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startScan_noHapProfileBluetoothDevice_shouldNotAddDevice() {
|
||||
final List<BluetoothDevice> devices = new ArrayList<>();
|
||||
|
||||
when(mHapProfile.getConnectableDevices()).thenReturn(devices);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
mMediaManager.startScan();
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void starScan_a2dpAndHapProfileNotReady_shouldRegisterCallback() {
|
||||
final Collection<CachedBluetoothDevice> mDevices = new ArrayList<>();
|
||||
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
|
||||
mDevices.add(cachedDevice);
|
||||
|
||||
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mDevices);
|
||||
when(mA2dpProfile.isProfileReady()).thenReturn(false);
|
||||
when(mHapProfile.isProfileReady()).thenReturn(false);
|
||||
|
||||
mMediaManager.startScan();
|
||||
|
||||
verify(mProfileManager).addServiceListener(mMediaManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void starScan_a2dpAndHapProfileReady_shouldNotRegisterCallback() {
|
||||
final Collection<CachedBluetoothDevice> mDevices = new ArrayList<>();
|
||||
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
|
||||
mDevices.add(cachedDevice);
|
||||
|
||||
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mDevices);
|
||||
when(mA2dpProfile.isProfileReady()).thenReturn(true);
|
||||
when(mHapProfile.isProfileReady()).thenReturn(true);
|
||||
|
||||
mMediaManager.startScan();
|
||||
|
||||
verify(mProfileManager, never()).addServiceListener(mMediaManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onServiceConnected_a2dpAndHapProfileNotReady_doNothing() {
|
||||
final Collection<CachedBluetoothDevice> mDevices = new ArrayList<>();
|
||||
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
|
||||
mDevices.add(cachedDevice);
|
||||
|
||||
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mDevices);
|
||||
when(mA2dpProfile.isProfileReady()).thenReturn(false);
|
||||
when(mHapProfile.isProfileReady()).thenReturn(false);
|
||||
|
||||
mMediaManager.startScan();
|
||||
mMediaManager.onServiceConnected();
|
||||
|
||||
verify(mProfileManager, never()).removeServiceListener(mMediaManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceAttributesChanged_a2dpAndHapProfileReady_shouldUnregisterCallback() {
|
||||
final Collection<CachedBluetoothDevice> mDevices = new ArrayList<>();
|
||||
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
|
||||
mDevices.add(cachedDevice);
|
||||
|
||||
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mDevices);
|
||||
when(mA2dpProfile.isProfileReady()).thenReturn(true);
|
||||
when(mHapProfile.isProfileReady()).thenReturn(true);
|
||||
|
||||
mMediaManager.startScan();
|
||||
mMediaManager.onServiceConnected();
|
||||
|
||||
verify(mProfileManager).removeServiceListener(mMediaManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBluetoothStateChanged_bluetoothStateIsOn_callOnDeviceListAdded() {
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
|
||||
|
||||
verify(mCallback).onDeviceListAdded(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBluetoothStateChanged_bluetoothStateIsOff_callOnDeviceListRemoved() {
|
||||
final BluetoothMediaDevice device1 = mock(BluetoothMediaDevice.class);
|
||||
final BluetoothMediaDevice device2 = mock(BluetoothMediaDevice.class);
|
||||
final CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
|
||||
final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
|
||||
mMediaManager.mMediaDevices.add(device1);
|
||||
mMediaManager.mMediaDevices.add(device2);
|
||||
|
||||
when(device1.getCachedDevice()).thenReturn(cachedDevice1);
|
||||
when(device2.getCachedDevice()).thenReturn(cachedDevice2);
|
||||
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
verify(mCallback).onDeviceListRemoved(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceAdded_cachedDeviceIsConnected_callOnDeviceAdded() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
|
||||
when(device.isConnectedHearingAidDevice()).thenReturn(true);
|
||||
when(device.isConnectedA2dpDevice()).thenReturn(true);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onDeviceAdded(device);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
verify(mCallback).onDeviceAdded(any());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceAdded_cachedDeviceIsDisconnected_doNothing() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
|
||||
when(device.isConnectedHearingAidDevice()).thenReturn(false);
|
||||
when(device.isConnectedA2dpDevice()).thenReturn(false);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onDeviceAdded(device);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
verify(mCallback, never()).onDeviceAdded(any());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceDeleted_cachedDeviceIsConnected_doNothing() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
|
||||
mMediaManager.mMediaDevices.add(bluetoothMediaDevice);
|
||||
|
||||
when(device.isConnectedHearingAidDevice()).thenReturn(true);
|
||||
when(device.isConnectedA2dpDevice()).thenReturn(true);
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
when(bluetoothMediaDevice.getId()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onDeviceDeleted(device);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
verify(mCallback, never()).onDeviceRemoved(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceDeleted_cachedDeviceIsDisconnected_callOnDeviceRemoved() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
|
||||
mMediaManager.mMediaDevices.add(bluetoothMediaDevice);
|
||||
|
||||
when(device.isConnectedHearingAidDevice()).thenReturn(false);
|
||||
when(device.isConnectedA2dpDevice()).thenReturn(false);
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
when(bluetoothMediaDevice.getId()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onDeviceDeleted(device);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
verify(mCallback).onDeviceRemoved(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_cachedDeviceIsBonded_callDeviceAttributesChanged() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
|
||||
mMediaManager.mMediaDevices.add(bluetoothMediaDevice);
|
||||
|
||||
when(device.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
when(bluetoothMediaDevice.getId()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onProfileConnectionStateChanged(device, 0, 0);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
verify(mCallback).onDeviceAttributesChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_cachedDeviceIsBondNone_callOnDeviceRemoved() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
|
||||
mMediaManager.mMediaDevices.add(bluetoothMediaDevice);
|
||||
|
||||
when(device.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
when(bluetoothMediaDevice.getId()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onProfileConnectionStateChanged(device, 0, 0);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
verify(mCallback).onDeviceRemoved(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onAclConnectionStateChanged_cachedDeviceIsBonded_callDeviceAttributesChanged() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
|
||||
mMediaManager.mMediaDevices.add(bluetoothMediaDevice);
|
||||
|
||||
when(device.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
when(bluetoothMediaDevice.getId()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onAclConnectionStateChanged(device, 0);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
verify(mCallback).onDeviceAttributesChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onAclConnectionStateChanged_cachedDeviceIsBondNone_callOnDeviceRemoved() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
|
||||
mMediaManager.mMediaDevices.add(bluetoothMediaDevice);
|
||||
|
||||
when(device.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
when(bluetoothMediaDevice.getId()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).hasSize(1);
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onAclConnectionStateChanged(device, 0);
|
||||
|
||||
assertThat(mMediaManager.mMediaDevices).isEmpty();
|
||||
verify(mCallback).onDeviceRemoved(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onActiveDeviceChanged_isHapProfile_callOnActiveDeviceChanged() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onActiveDeviceChanged(device, BluetoothProfile.HEARING_AID);
|
||||
|
||||
verify(mCallback).onConnectedDeviceChanged(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onActiveDeviceChanged_isA2dpProfile_callOnActiveDeviceChanged() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onActiveDeviceChanged(device, BluetoothProfile.A2DP);
|
||||
|
||||
verify(mCallback).onConnectedDeviceChanged(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onActiveDeviceChanged_isNotA2dpAndHapProfile_doNothing() {
|
||||
final CachedBluetoothDevice device = mock(CachedBluetoothDevice.class);
|
||||
|
||||
when(device.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onActiveDeviceChanged(device, BluetoothProfile.HEALTH);
|
||||
|
||||
verify(mCallback, never()).onConnectedDeviceChanged(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onActiveDeviceChanged_hearingAidDeviceIsActive_returnHearingAidDeviceId() {
|
||||
final Long hiSyncId = Integer.toUnsignedLong(12345);
|
||||
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
|
||||
final List<BluetoothDevice> devices = new ArrayList<>();
|
||||
devices.add(bluetoothDevice);
|
||||
final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
|
||||
mMediaManager.mMediaDevices.add(bluetoothMediaDevice);
|
||||
|
||||
when(mHapProfile.getHiSyncId(bluetoothDevice)).thenReturn(hiSyncId);
|
||||
when(mHapProfile.getActiveDevices()).thenReturn(devices);
|
||||
when(bluetoothMediaDevice.getId()).thenReturn(Long.toString(hiSyncId));
|
||||
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onActiveDeviceChanged(null, BluetoothProfile.A2DP);
|
||||
|
||||
verify(mCallback).onConnectedDeviceChanged(Long.toString(hiSyncId));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onActiveDeviceChanged_hearingAidDeviceNotActive_returnPhoneDeviceId() {
|
||||
final List<BluetoothDevice> devices = new ArrayList<>();
|
||||
|
||||
when(mHapProfile.getActiveDevices()).thenReturn(devices);
|
||||
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onActiveDeviceChanged(null, BluetoothProfile.A2DP);
|
||||
|
||||
verify(mCallback).onConnectedDeviceChanged(PhoneMediaDevice.ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onAudioModeChanged_shouldCallOnDeviceAttributesChanged() {
|
||||
mMediaManager.registerCallback(mCallback);
|
||||
mMediaManager.onAudioModeChanged();
|
||||
|
||||
verify(mCallback).onDeviceAttributesChanged();
|
||||
}
|
||||
}
|
||||
@@ -66,8 +66,6 @@ public class LocalMediaManagerTest {
|
||||
private static final String TEST_CURRENT_DEVICE_ID = "currentDevice_id";
|
||||
private static final String TEST_PACKAGE_NAME = "com.test.playmusic";
|
||||
|
||||
@Mock
|
||||
private BluetoothMediaManager mBluetoothMediaManager;
|
||||
@Mock
|
||||
private InfoMediaManager mInfoMediaManager;
|
||||
@Mock
|
||||
|
||||
Reference in New Issue
Block a user