Bluetooth: reset mConnectionState when adapter is OFF
* No device is connected when Bluetooth adapter is OFF * BluetoothSummaryUpdater should reset its connection state tracker in order to display the correct summary message on ConnectedDevice preference * Otherwise, "Connected to null" will be shown because no device is connected while BluetoothSummaryUpdater is still in CONNECTED state * Removed unused imports from BluetoothSummaryUpdater * Write additional unit test to verify the above behaviour * Add additional logging when deviceName is null in CONNECTED state Bug: 62492716 Test: Pair and connect to Bluetooth device, turning Bluetooth ON/OFF, unit tests Change-Id: I30726636f5678d61d6052f5b8d211aa20f26f409
This commit is contained in:
@@ -20,7 +20,7 @@ import android.bluetooth.BluetoothAdapter;
|
|||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.text.TextUtils;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.widget.SummaryUpdater;
|
import com.android.settings.widget.SummaryUpdater;
|
||||||
@@ -29,9 +29,7 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
|||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
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.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,6 +37,7 @@ import java.util.Set;
|
|||||||
* bluetooth summary info.
|
* bluetooth summary info.
|
||||||
*/
|
*/
|
||||||
public final class BluetoothSummaryUpdater extends SummaryUpdater implements BluetoothCallback {
|
public final class BluetoothSummaryUpdater extends SummaryUpdater implements BluetoothCallback {
|
||||||
|
private static final String TAG = "BluetoothSummaryUpdater";
|
||||||
|
|
||||||
private final LocalBluetoothManager mBluetoothManager;
|
private final LocalBluetoothManager mBluetoothManager;
|
||||||
private final LocalBluetoothAdapter mBluetoothAdapter;
|
private final LocalBluetoothAdapter mBluetoothAdapter;
|
||||||
@@ -58,6 +57,9 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu
|
|||||||
public void onBluetoothStateChanged(int bluetoothState) {
|
public void onBluetoothStateChanged(int bluetoothState) {
|
||||||
mEnabled = bluetoothState == BluetoothAdapter.STATE_ON
|
mEnabled = bluetoothState == BluetoothAdapter.STATE_ON
|
||||||
|| bluetoothState == BluetoothAdapter.STATE_TURNING_ON;
|
|| bluetoothState == BluetoothAdapter.STATE_TURNING_ON;
|
||||||
|
if (!mEnabled) {
|
||||||
|
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
|
||||||
|
}
|
||||||
notifyChangeIfNeeded();
|
notifyChangeIfNeeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +163,6 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu
|
|||||||
if (devices == null || devices.isEmpty()) {
|
if (devices == null || devices.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (BluetoothDevice device : devices) {
|
for (BluetoothDevice device : devices) {
|
||||||
if (device.isConnected()) {
|
if (device.isConnected()) {
|
||||||
deviceName = device.getName();
|
deviceName = device.getName();
|
||||||
@@ -171,7 +172,14 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (deviceName == null) {
|
||||||
|
Log.w(TAG, "getConnectedDeviceSummary, deviceName is null, numBondedDevices="
|
||||||
|
+ devices.size());
|
||||||
|
for (BluetoothDevice device : devices) {
|
||||||
|
Log.w(TAG, "getConnectedDeviceSummary, device=" + device.getName() + "["
|
||||||
|
+ device.getAddress() + "]" + ", isConnected=" + device.isConnected());
|
||||||
|
}
|
||||||
|
}
|
||||||
return count > 1 ? mContext.getString(R.string.bluetooth_connected_multiple_devices_summary)
|
return count > 1 ? mContext.getString(R.string.bluetooth_connected_multiple_devices_summary)
|
||||||
: mContext.getString(R.string.bluetooth_connected_summary, deviceName);
|
: mContext.getString(R.string.bluetooth_connected_summary, deviceName);
|
||||||
}
|
}
|
||||||
|
@@ -44,8 +44,11 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.anyString;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
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 static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -132,6 +135,36 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
mContext.getString(R.string.disconnected));
|
mContext.getString(R.string.disconnected));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onBluetoothStateChanged_ConnectedDisabledEnabled_shouldSendDisconnectedSummary() {
|
||||||
|
final boolean[] connected = {false};
|
||||||
|
final List<CachedBluetoothDevice> devices = new ArrayList<>();
|
||||||
|
devices.add(mock(CachedBluetoothDevice.class));
|
||||||
|
doAnswer(invocation -> connected[0]).when(devices.get(0)).isConnected();
|
||||||
|
when(mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy())
|
||||||
|
.thenReturn(devices);
|
||||||
|
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
|
||||||
|
prepareConnectedDevice(false);
|
||||||
|
|
||||||
|
mSummaryUpdater.register(true);
|
||||||
|
verify(mListener).onSummaryChanged(mContext.getString(R.string.disconnected));
|
||||||
|
|
||||||
|
connected[0] = true;
|
||||||
|
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED);
|
||||||
|
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||||
|
BluetoothAdapter.STATE_CONNECTED);
|
||||||
|
verify(mListener).onSummaryChanged(
|
||||||
|
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME));
|
||||||
|
|
||||||
|
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
|
||||||
|
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
|
||||||
|
|
||||||
|
connected[0] = false;
|
||||||
|
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_ON);
|
||||||
|
verify(mListener, times(2)).onSummaryChanged(mContext.getString(R.string.disconnected));
|
||||||
|
verify(mListener, times(4)).onSummaryChanged(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onConnectionStateChanged_connected_shouldSendConnectedMessage() {
|
public void onConnectionStateChanged_connected_shouldSendConnectedMessage() {
|
||||||
final List<CachedBluetoothDevice> devices = new ArrayList<>();
|
final List<CachedBluetoothDevice> devices = new ArrayList<>();
|
||||||
|
Reference in New Issue
Block a user