Merge "Bluetooth: reset mConnectionState when adapter is OFF" into oc-dr1-dev

am: 0c6f001c09

Change-Id: I2a161d403cf0c8e34a16b1ac9328135c40f7e50a
This commit is contained in:
Jack He
2017-07-26 06:34:30 +00:00
committed by android-build-merger
2 changed files with 46 additions and 5 deletions

View File

@@ -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);
} }

View File

@@ -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<>();