Update bluetooth summary text when state changes.

- Refresh connected state before setting summary text.
- Detect inconsistent state (BT manager says connected but doesn't
  provide a connected device)

(This basically syncs implementation between settings and QS tile)

Change-Id: Id23138f8432b9aecd194f5016bf2576e33e8ca98
Fixes: 33341275
Test: RunSettingsRoboTests
This commit is contained in:
Fan Zhang
2016-12-27 13:56:42 -08:00
parent 68220c6d74
commit 00d617df0b
3 changed files with 261 additions and 47 deletions

View File

@@ -27,7 +27,7 @@ import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
@@ -58,6 +58,7 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@@ -438,8 +439,9 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
super.onBluetoothStateChanged(bluetoothState);
// If BT is turned off/on staying in the same BT Settings screen
// discoverability to be set again
if (BluetoothAdapter.STATE_ON == bluetoothState)
if (BluetoothAdapter.STATE_ON == bluetoothState) {
mInitiateDiscoverable = true;
}
updateContent(bluetoothState);
}
@@ -481,6 +483,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
/**
* Add a listener, which enables the advanced settings icon.
*
* @param preference the newly added preference
*/
@Override
@@ -497,7 +500,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
return R.string.help_url_bluetooth;
}
private static class SummaryProvider
@VisibleForTesting
static class SummaryProvider
implements SummaryLoader.SummaryProvider, BluetoothCallback {
private final LocalBluetoothManager mBluetoothManager;
@@ -505,10 +509,11 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
private final SummaryLoader mSummaryLoader;
private boolean mEnabled;
private boolean mConnected;
private int mConnectionState;
public SummaryProvider(Context context, SummaryLoader summaryLoader) {
mBluetoothManager = Utils.getLocalBtManager(context);
public SummaryProvider(Context context, SummaryLoader summaryLoader,
LocalBluetoothManager bluetoothManager) {
mBluetoothManager = bluetoothManager;
mContext = context;
mSummaryLoader = summaryLoader;
}
@@ -519,8 +524,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
if (defaultAdapter == null) return;
if (listening) {
mEnabled = defaultAdapter.isEnabled();
mConnected =
defaultAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED;
mConnectionState = defaultAdapter.getConnectionState();
mSummaryLoader.setSummary(this, getSummary());
mBluetoothManager.getEventManager().registerCallback(this);
} else {
@@ -529,20 +533,26 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
}
private CharSequence getSummary() {
return mContext.getString(!mEnabled ? R.string.bluetooth_disabled
: mConnected ? R.string.bluetooth_connected
: R.string.bluetooth_disconnected);
if (!mEnabled) {
return mContext.getString(R.string.bluetooth_disabled);
} else if (mConnectionState == BluetoothAdapter.STATE_CONNECTED) {
return mContext.getString(R.string.bluetooth_connected);
} else {
return mContext.getString(R.string.bluetooth_disconnected);
}
}
@Override
public void onBluetoothStateChanged(int bluetoothState) {
mEnabled = bluetoothState == BluetoothAdapter.STATE_ON;
mEnabled = bluetoothState == BluetoothAdapter.STATE_ON
|| bluetoothState == BluetoothAdapter.STATE_TURNING_ON;
mSummaryLoader.setSummary(this, getSummary());
}
@Override
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
mConnected = state == BluetoothAdapter.STATE_CONNECTED;
mConnectionState = state;
updateConnected();
mSummaryLoader.setSummary(this, getSummary());
}
@@ -565,6 +575,39 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
}
private void updateConnected() {
// Make sure our connection state is up to date.
int state = mBluetoothManager.getBluetoothAdapter().getConnectionState();
if (state != mConnectionState) {
mConnectionState = state;
return;
}
final Collection<CachedBluetoothDevice> devices = getDevices();
if (devices == null) {
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
return;
}
if (mConnectionState == BluetoothAdapter.STATE_CONNECTED) {
CachedBluetoothDevice connectedDevice = null;
for (CachedBluetoothDevice device : devices) {
if (device.isConnected()) {
connectedDevice = device;
}
}
if (connectedDevice == null) {
// If somehow we think we are connected, but have no connected devices, we
// aren't connected.
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
}
}
}
private Collection<CachedBluetoothDevice> getDevices() {
return mBluetoothManager != null
? mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()
: null;
}
}
public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
@@ -572,14 +615,16 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
@Override
public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
SummaryLoader summaryLoader) {
return new SummaryProvider(activity, summaryLoader);
return new SummaryProvider(activity, summaryLoader, Utils.getLocalBtManager(activity));
}
};
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
public List<SearchIndexableRaw> getRawDataToIndex(Context context,
boolean enabled) {
final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();

View File

@@ -330,5 +330,6 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
mDashboardTilePrefKeys.remove(key);
mProgressiveDisclosureMixin.removePreference(screen, key);
}
mSummaryLoader.setListening(true);
}
}

View File

@@ -0,0 +1,168 @@
/*
* Copyright (C) 2016 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.settings.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowBluetoothAdapter;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BluetoothSettingsSummaryProviderTest {
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private LocalBluetoothManager mBluetoothManager;
@Mock
private SummaryLoader mSummaryLoader;
private BluetoothSettings.SummaryProvider mSummaryProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application.getApplicationContext();
mSummaryProvider = new BluetoothSettings.SummaryProvider(mContext, mSummaryLoader,
mBluetoothManager);
}
@Test
public void setListening_shouldUpdateSummary() {
mSummaryProvider.setListening(true);
verify(mBluetoothManager.getEventManager()).registerCallback(mSummaryProvider);
verify(mSummaryLoader).setSummary(eq(mSummaryProvider), anyString());
}
@Test
public void setNotListening_shouldUnregister() {
mSummaryProvider.setListening(false);
verify(mBluetoothManager.getEventManager()).unregisterCallback(mSummaryProvider);
}
@Test
public void updateSummary_btDisabled_shouldShowDisabledMessage() {
ShadowBluetoothAdapter.getDefaultAdapter().disable();
mSummaryProvider.setListening(true);
verify(mSummaryLoader).setSummary(mSummaryProvider,
mContext.getString(R.string.bluetooth_disabled));
}
@Test
public void updateSummary_btEnabled_noDevice_shouldShowDisconnectedMessage() {
ShadowBluetoothAdapter.getDefaultAdapter().enable();
mSummaryProvider.setListening(true);
verify(mSummaryLoader).setSummary(mSummaryProvider,
mContext.getString(R.string.bluetooth_disconnected));
}
@Test
public void updateState_btEnabled_noDevice_shouldShowDisconnectedMessage() {
ShadowBluetoothAdapter.getDefaultAdapter().enable();
mSummaryProvider.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_ON);
verify(mSummaryLoader).setSummary(mSummaryProvider,
mContext.getString(R.string.bluetooth_disconnected));
}
@Test
public void updateState_btDisabled_shouldShowDisabledMessage() {
ShadowBluetoothAdapter.getDefaultAdapter().enable();
mSummaryProvider.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_OFF);
verify(mSummaryLoader).setSummary(mSummaryProvider,
mContext.getString(R.string.bluetooth_disabled));
}
@Test
public void updateConnectionState_disconnected_shouldShowDisconnectedMessage() {
ShadowBluetoothAdapter.getDefaultAdapter().enable();
when(mBluetoothManager.getBluetoothAdapter().getConnectionState())
.thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
mSummaryProvider.setListening(true);
mSummaryProvider.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_DISCONNECTED);
verify(mSummaryLoader, times(2)).setSummary(mSummaryProvider,
mContext.getString(R.string.bluetooth_disconnected));
}
@Test
public void updateConnectionState_connected_shouldShowConnectedMessage() {
ShadowBluetoothAdapter.getDefaultAdapter().enable();
when(mBluetoothManager.getBluetoothAdapter().getConnectionState())
.thenReturn(BluetoothAdapter.STATE_CONNECTED);
final List<CachedBluetoothDevice> devices = new ArrayList<>();
devices.add(mock(CachedBluetoothDevice.class));
when(devices.get(0).isConnected()).thenReturn(true);
when(mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy())
.thenReturn(devices);
mSummaryProvider.setListening(true);
mSummaryProvider.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTED);
verify(mSummaryLoader).setSummary(mSummaryProvider,
mContext.getString(R.string.bluetooth_connected));
}
@Test
public void updateConnectionState_inconsistentState_shouldShowDisconnectedMessage() {
ShadowBluetoothAdapter.getDefaultAdapter().enable();
when(mBluetoothManager.getBluetoothAdapter().getConnectionState())
.thenReturn(BluetoothAdapter.STATE_CONNECTED);
mSummaryProvider.setListening(true);
mSummaryProvider.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTED);
verify(mSummaryLoader, times(2)).setSummary(mSummaryProvider,
mContext.getString(R.string.bluetooth_disconnected));
}
}