Merge "Show bonded devices in "Pair new device"(2/2)"

This commit is contained in:
TreeHugger Robot
2018-08-28 14:32:04 +00:00
committed by Android (Google) Code Review
3 changed files with 41 additions and 10 deletions

View File

@@ -152,7 +152,7 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
addDeviceCategory(mAvailableDevicesCategory, addDeviceCategory(mAvailableDevicesCategory,
R.string.bluetooth_preference_found_media_devices, R.string.bluetooth_preference_found_media_devices,
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted); BluetoothDeviceFilter.ALL_FILTER, mInitialScanStarted);
updateFooterPreference(mFooterPreference); updateFooterPreference(mFooterPreference);
mAlwaysDiscoverable.start(); mAlwaysDiscoverable.start();
enableScanning(); enableScanning();
@@ -190,6 +190,17 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
} }
} }
@Override
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
if (mSelectedDevice != null) {
BluetoothDevice device = cachedDevice.getDevice();
if (device != null && mSelectedDevice.equals(device)
&& state == BluetoothAdapter.STATE_CONNECTED) {
finish();
}
}
}
@Override @Override
public int getHelpResource() { public int getHelpResource() {
return R.string.help_url_bluetooth; return R.string.help_url_bluetooth;

View File

@@ -192,6 +192,8 @@ public abstract class DeviceListPreferenceFragment extends
preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice, preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice,
mShowDevicesWithoutNames); mShowDevicesWithoutNames);
preference.setKey(key); preference.setKey(key);
//Set hideSecondTarget is true if it's bonded device.
preference.hideSecondTarget(true);
mDeviceListGroup.addPreference(preference); mDeviceListGroup.addPreference(preference);
} else { } else {
// Tell the preference it is being re-used in case there is new info in the // Tell the preference it is being re-used in case there is new info in the

View File

@@ -28,6 +28,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
@@ -35,6 +36,7 @@ import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.BluetoothDeviceFilter; import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.widget.FooterPreference; import com.android.settingslib.widget.FooterPreference;
@@ -53,6 +55,7 @@ import androidx.preference.PreferenceGroup;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@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";
@Mock @Mock
private Resources mResource; private Resources mResource;
@@ -60,12 +63,15 @@ public class BluetoothPairingDetailTest {
private LocalBluetoothManager mLocalManager; private LocalBluetoothManager mLocalManager;
@Mock @Mock
private PreferenceGroup mPreferenceGroup; private PreferenceGroup mPreferenceGroup;
@Mock
private CachedBluetoothDevice mCachedBluetoothDevice;
private BluetoothPairingDetail mFragment; private BluetoothPairingDetail mFragment;
private Context mContext; private Context mContext;
private BluetoothProgressCategory mAvailableDevicesCategory; private BluetoothProgressCategory mAvailableDevicesCategory;
private FooterPreference mFooterPreference; private FooterPreference mFooterPreference;
private BluetoothAdapter mBluetoothAdapter; private BluetoothAdapter mBluetoothAdapter;
private ShadowBluetoothAdapter mShadowBluetoothAdapter; private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private BluetoothDevice mBluetoothDevice;
@Before @Before
public void setUp() { public void setUp() {
@@ -80,6 +86,7 @@ public class BluetoothPairingDetailTest {
mFooterPreference = new FooterPreference(mContext); mFooterPreference = new FooterPreference(mContext);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS);
mFragment.mBluetoothAdapter = mBluetoothAdapter; mFragment.mBluetoothAdapter = mBluetoothAdapter;
mFragment.mLocalManager = mLocalManager; mFragment.mLocalManager = mLocalManager;
@@ -88,7 +95,7 @@ public class BluetoothPairingDetailTest {
} }
@Test @Test
public void testInitPreferencesFromPreferenceScreen_findPreferences() { public void initPreferencesFromPreferenceScreen_findPreferences() {
doReturn(mAvailableDevicesCategory).when(mFragment) doReturn(mAvailableDevicesCategory).when(mFragment)
.findPreference(BluetoothPairingDetail.KEY_AVAIL_DEVICES); .findPreference(BluetoothPairingDetail.KEY_AVAIL_DEVICES);
doReturn(mFooterPreference).when(mFragment) doReturn(mFooterPreference).when(mFragment)
@@ -101,7 +108,7 @@ public class BluetoothPairingDetailTest {
} }
@Test @Test
public void testStartScanning_startScanAndRemoveDevices() { public void startScanning_startScanAndRemoveDevices() {
mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory; mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
mFragment.mDeviceListGroup = mAvailableDevicesCategory; mFragment.mDeviceListGroup = mAvailableDevicesCategory;
@@ -112,7 +119,7 @@ public class BluetoothPairingDetailTest {
} }
@Test @Test
public void testUpdateContent_stateOn_addDevices() { public void updateContent_stateOn_addDevices() {
mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory; mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
mFragment.mFooterPreference = mFooterPreference; mFragment.mFooterPreference = mFooterPreference;
doNothing().when(mFragment).addDeviceCategory(any(), anyInt(), any(), anyBoolean()); doNothing().when(mFragment).addDeviceCategory(any(), anyInt(), any(), anyBoolean());
@@ -121,20 +128,20 @@ public class BluetoothPairingDetailTest {
verify(mFragment).addDeviceCategory(mAvailableDevicesCategory, verify(mFragment).addDeviceCategory(mAvailableDevicesCategory,
R.string.bluetooth_preference_found_media_devices, R.string.bluetooth_preference_found_media_devices,
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, false); BluetoothDeviceFilter.ALL_FILTER, false);
assertThat(mBluetoothAdapter.getScanMode()) assertThat(mBluetoothAdapter.getScanMode())
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE); .isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
} }
@Test @Test
public void testUpdateContent_stateOff_finish() { public void updateContent_stateOff_finish() {
mFragment.updateContent(BluetoothAdapter.STATE_OFF); mFragment.updateContent(BluetoothAdapter.STATE_OFF);
verify(mFragment).finish(); verify(mFragment).finish();
} }
@Test @Test
public void testUpdateBluetooth_bluetoothOff_turnOnBluetooth() { public void updateBluetooth_bluetoothOff_turnOnBluetooth() {
mShadowBluetoothAdapter.setEnabled(false); mShadowBluetoothAdapter.setEnabled(false);
mFragment.updateBluetooth(); mFragment.updateBluetooth();
@@ -143,7 +150,7 @@ public class BluetoothPairingDetailTest {
} }
@Test @Test
public void testUpdateBluetooth_bluetoothOn_updateState() { public void updateBluetooth_bluetoothOn_updateState() {
mShadowBluetoothAdapter.setEnabled(true); mShadowBluetoothAdapter.setEnabled(true);
doNothing().when(mFragment).updateContent(anyInt()); doNothing().when(mFragment).updateContent(anyInt());
@@ -153,7 +160,7 @@ public class BluetoothPairingDetailTest {
} }
@Test @Test
public void testOnScanningStateChanged_restartScanAfterInitialScanning() { public void onScanningStateChanged_restartScanAfterInitialScanning() {
mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory; mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
mFragment.mFooterPreference = mFooterPreference; mFragment.mFooterPreference = mFooterPreference;
mFragment.mDeviceListGroup = mAvailableDevicesCategory; mFragment.mDeviceListGroup = mAvailableDevicesCategory;
@@ -208,4 +215,15 @@ public class BluetoothPairingDetailTest {
verify(mFragment).showBluetoothTurnedOnToast(); verify(mFragment).showBluetoothTurnedOnToast();
} }
@Test
public void onConnectionStateChanged_connected_finish() {
mFragment.mSelectedDevice = mBluetoothDevice;
doReturn(mBluetoothDevice).when(mCachedBluetoothDevice).getDevice();
mFragment.onConnectionStateChanged(mCachedBluetoothDevice,
BluetoothAdapter.STATE_CONNECTED);
verify(mFragment).finish();
}
} }