Merge "Fix BTHS is still in pairing list after BTHS is connected"
This commit is contained in:
committed by
Android (Google) Code Review
commit
7d744c8f18
@@ -29,9 +29,9 @@ import android.widget.Toast;
|
|||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.search.Indexable;
|
|
||||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
|
import com.android.settingslib.search.Indexable;
|
||||||
import com.android.settingslib.widget.FooterPreference;
|
import com.android.settingslib.widget.FooterPreference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -191,12 +191,19 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
|
public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
|
||||||
if (mSelectedDevice != null) {
|
int bluetoothProfile) {
|
||||||
BluetoothDevice device = cachedDevice.getDevice();
|
// This callback is used to handle the case that bonded device is connected in pairing list.
|
||||||
if (device != null && mSelectedDevice.equals(device)
|
// 1. If user selected multiple bonded devices in pairing list, after connected
|
||||||
&& state == BluetoothAdapter.STATE_CONNECTED) {
|
// finish this page.
|
||||||
|
// 2. If the bonded devices auto connected in paring list, after connected it will be
|
||||||
|
// removed from paring list.
|
||||||
|
if (cachedDevice != null && cachedDevice.isConnected()) {
|
||||||
|
final BluetoothDevice device = cachedDevice.getDevice();
|
||||||
|
if (device != null && mSelectedList.contains(device)) {
|
||||||
finish();
|
finish();
|
||||||
|
} else if (mDevicePreferenceMap.containsKey(cachedDevice)) {
|
||||||
|
onDeviceDeleted(cachedDevice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -35,9 +35,10 @@ import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
|||||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.WeakHashMap;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parent class for settings fragments that contain a list of Bluetooth
|
* Parent class for settings fragments that contain a list of Bluetooth
|
||||||
@@ -72,6 +73,7 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
|
|
||||||
final HashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
|
final HashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
|
||||||
new HashMap<>();
|
new HashMap<>();
|
||||||
|
final List<BluetoothDevice> mSelectedList = new ArrayList<>();
|
||||||
|
|
||||||
boolean mShowDevicesWithoutNames;
|
boolean mShowDevicesWithoutNames;
|
||||||
|
|
||||||
@@ -154,6 +156,7 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
BluetoothDevicePreference btPreference = (BluetoothDevicePreference) preference;
|
BluetoothDevicePreference btPreference = (BluetoothDevicePreference) preference;
|
||||||
CachedBluetoothDevice device = btPreference.getCachedDevice();
|
CachedBluetoothDevice device = btPreference.getCachedDevice();
|
||||||
mSelectedDevice = device.getDevice();
|
mSelectedDevice = device.getDevice();
|
||||||
|
mSelectedList.add(mSelectedDevice);
|
||||||
onDevicePreferenceClick(btPreference);
|
onDevicePreferenceClick(btPreference);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -23,13 +23,16 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
|
|||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.Mockito.doNothing;
|
import static org.mockito.Mockito.doNothing;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
|
import android.bluetooth.BluetoothProfile;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
|
||||||
@@ -57,6 +60,7 @@ import org.robolectric.shadow.api.Shadow;
|
|||||||
@Config(shadows = {ShadowBluetoothAdapter.class})
|
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||||
public class BluetoothPairingDetailTest {
|
public class BluetoothPairingDetailTest {
|
||||||
private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
|
private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
|
||||||
|
private static final String TEST_DEVICE_ADDRESS_B = "00:B1:B1:B1:B1:B1";
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private Resources mResource;
|
private Resources mResource;
|
||||||
@@ -82,6 +86,7 @@ public class BluetoothPairingDetailTest {
|
|||||||
mFragment = spy(new BluetoothPairingDetail());
|
mFragment = spy(new BluetoothPairingDetail());
|
||||||
doReturn(mContext).when(mFragment).getContext();
|
doReturn(mContext).when(mFragment).getContext();
|
||||||
doReturn(mResource).when(mFragment).getResources();
|
doReturn(mResource).when(mFragment).getResources();
|
||||||
|
when(mCachedBluetoothDevice.getAddress()).thenReturn(TEST_DEVICE_ADDRESS);
|
||||||
|
|
||||||
mAvailableDevicesCategory = spy(new BluetoothProgressCategory(mContext));
|
mAvailableDevicesCategory = spy(new BluetoothProgressCategory(mContext));
|
||||||
mFooterPreference = new FooterPreference(mContext);
|
mFooterPreference = new FooterPreference(mContext);
|
||||||
@@ -218,13 +223,85 @@ public class BluetoothPairingDetailTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onConnectionStateChanged_connected_finish() {
|
public void onProfileConnectionStateChanged_deviceInSelectedListAndConnected_finish() {
|
||||||
mFragment.mSelectedDevice = mBluetoothDevice;
|
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_B);
|
||||||
doReturn(mBluetoothDevice).when(mCachedBluetoothDevice).getDevice();
|
mFragment.mSelectedList.add(mBluetoothDevice);
|
||||||
|
mFragment.mSelectedList.add(device);
|
||||||
|
|
||||||
mFragment.onConnectionStateChanged(mCachedBluetoothDevice,
|
when(mCachedBluetoothDevice.isConnected()).thenReturn(true);
|
||||||
BluetoothAdapter.STATE_CONNECTED);
|
when(mCachedBluetoothDevice.getDevice()).thenReturn(device);
|
||||||
|
|
||||||
|
mFragment.onProfileConnectionStateChanged(mCachedBluetoothDevice,
|
||||||
|
BluetoothProfile.A2DP, BluetoothAdapter.STATE_CONNECTED);
|
||||||
|
|
||||||
verify(mFragment).finish();
|
verify(mFragment).finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onProfileConnectionStateChanged_deviceNotInSelectedList_doNothing() {
|
||||||
|
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_B);
|
||||||
|
mFragment.mSelectedList.add(device);
|
||||||
|
|
||||||
|
when(mCachedBluetoothDevice.isConnected()).thenReturn(true);
|
||||||
|
when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
|
||||||
|
|
||||||
|
mFragment.onProfileConnectionStateChanged(mCachedBluetoothDevice,
|
||||||
|
BluetoothProfile.A2DP, BluetoothAdapter.STATE_CONNECTED);
|
||||||
|
|
||||||
|
// not crash
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onProfileConnectionStateChanged_deviceDisconnected_doNothing() {
|
||||||
|
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_B);
|
||||||
|
mFragment.mSelectedList.add(mBluetoothDevice);
|
||||||
|
mFragment.mSelectedList.add(device);
|
||||||
|
|
||||||
|
when(mCachedBluetoothDevice.isConnected()).thenReturn(false);
|
||||||
|
when(mCachedBluetoothDevice.getDevice()).thenReturn(device);
|
||||||
|
|
||||||
|
mFragment.onProfileConnectionStateChanged(mCachedBluetoothDevice,
|
||||||
|
BluetoothProfile.A2DP, BluetoothAdapter.STATE_DISCONNECTED);
|
||||||
|
|
||||||
|
// not crash
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onProfileConnectionStateChanged_deviceInPreferenceMapAndConnected_removed() {
|
||||||
|
final BluetoothDevicePreference preference =
|
||||||
|
new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
|
||||||
|
true, BluetoothDevicePreference.SortType.TYPE_FIFO);
|
||||||
|
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS);
|
||||||
|
mFragment.mDevicePreferenceMap.put(mCachedBluetoothDevice, preference);
|
||||||
|
|
||||||
|
when(mCachedBluetoothDevice.isConnected()).thenReturn(true);
|
||||||
|
when(mCachedBluetoothDevice.getDevice()).thenReturn(device);
|
||||||
|
|
||||||
|
mFragment.onProfileConnectionStateChanged(mCachedBluetoothDevice,
|
||||||
|
BluetoothProfile.A2DP, BluetoothAdapter.STATE_CONNECTED);
|
||||||
|
|
||||||
|
assertThat(mFragment.mDevicePreferenceMap.size()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onProfileConnectionStateChanged_deviceNotInPreferenceMap_doNothing() {
|
||||||
|
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
|
||||||
|
final BluetoothDevicePreference preference =
|
||||||
|
new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
|
||||||
|
true, BluetoothDevicePreference.SortType.TYPE_FIFO);
|
||||||
|
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS);
|
||||||
|
final BluetoothDevice device2 = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_B);
|
||||||
|
mFragment.mDevicePreferenceMap.put(mCachedBluetoothDevice, preference);
|
||||||
|
|
||||||
|
when(mCachedBluetoothDevice.isConnected()).thenReturn(true);
|
||||||
|
when(mCachedBluetoothDevice.getDevice()).thenReturn(device);
|
||||||
|
when(cachedDevice.isConnected()).thenReturn(true);
|
||||||
|
when(cachedDevice.getDevice()).thenReturn(device2);
|
||||||
|
when(cachedDevice.getAddress()).thenReturn(TEST_DEVICE_ADDRESS_B);
|
||||||
|
|
||||||
|
mFragment.onProfileConnectionStateChanged(cachedDevice, BluetoothProfile.A2DP,
|
||||||
|
BluetoothAdapter.STATE_CONNECTED);
|
||||||
|
|
||||||
|
// not crash
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user