Replace LocalBluetoothAdapter with BluetoothAdapter

LocalBluetoothAdapter is obsolete, use BluetoothAdapter instead.

Bug: 111810977
Test: make -j50 RunSettingsRoboTests
Change-Id: I5109a0296c1006a3c2e346bf966ef8901c101e30
This commit is contained in:
Chienyuan
2018-08-08 15:44:42 +08:00
parent fbc325a9aa
commit fca684aab6
3 changed files with 23 additions and 20 deletions

View File

@@ -39,7 +39,6 @@ import com.android.settings.bluetooth.BluetoothDeviceDetailsFragment;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.SubSettingLauncher; import com.android.settings.core.SubSettingLauncher;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause; import com.android.settingslib.core.lifecycle.events.OnPause;
@@ -82,6 +81,7 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
}; };
private final LocalBluetoothManager mLocalBluetoothManager; private final LocalBluetoothManager mLocalBluetoothManager;
private final BluetoothAdapter mBluetoothAdapter;
//cache value of supporting hearing aid or not //cache value of supporting hearing aid or not
private boolean mHearingAidProfileSupported; private boolean mHearingAidProfileSupported;
private FragmentManager mFragmentManager; private FragmentManager mFragmentManager;
@@ -89,6 +89,7 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
public AccessibilityHearingAidPreferenceController(Context context, String preferenceKey) { public AccessibilityHearingAidPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
mLocalBluetoothManager = getLocalBluetoothManager(); mLocalBluetoothManager = getLocalBluetoothManager();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mHearingAidProfileSupported = isHearingAidProfileSupported(); mHearingAidProfileSupported = isHearingAidProfileSupported();
} }
@@ -151,8 +152,7 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
if (!mHearingAidProfileSupported) { if (!mHearingAidProfileSupported) {
return null; return null;
} }
final LocalBluetoothAdapter localAdapter = mLocalBluetoothManager.getBluetoothAdapter(); if (!mBluetoothAdapter.isEnabled()) {
if (!localAdapter.isEnabled()) {
return null; return null;
} }
final List<BluetoothDevice> deviceList = mLocalBluetoothManager.getProfileManager() final List<BluetoothDevice> deviceList = mLocalBluetoothManager.getProfileManager()
@@ -166,8 +166,7 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
} }
private boolean isHearingAidProfileSupported() { private boolean isHearingAidProfileSupported() {
final LocalBluetoothAdapter localAdapter = mLocalBluetoothManager.getBluetoothAdapter(); final List<Integer> supportedList = mBluetoothAdapter.getSupportedProfiles();
final List<Integer> supportedList = localAdapter.getSupportedProfiles();
if (supportedList.contains(BluetoothProfile.HEARING_AID)) { if (supportedList.contains(BluetoothProfile.HEARING_AID)) {
return true; return true;
} }

View File

@@ -40,10 +40,10 @@ import androidx.preference.Preference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils; import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.HearingAidProfile; import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
@@ -55,23 +55,24 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothUtils.class}) @Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothUtils.class})
public class AccessibilityHearingAidPreferenceControllerTest { public class AccessibilityHearingAidPreferenceControllerTest {
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_NAME = "TEST_HEARING_AID_BT_DEVICE_NAME"; private static final String TEST_DEVICE_NAME = "TEST_HEARING_AID_BT_DEVICE_NAME";
private static final String HEARING_AID_PREFERENCE = "hearing_aid_preference"; private static final String HEARING_AID_PREFERENCE = "hearing_aid_preference";
private BluetoothAdapter mBluetoothAdapter; private BluetoothAdapter mBluetoothAdapter;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private BluetoothManager mBluetoothManager; private BluetoothManager mBluetoothManager;
private BluetoothDevice mBluetoothDevice; private BluetoothDevice mBluetoothDevice;
private Context mContext; private Context mContext;
private Preference mHearingAidPreference; private Preference mHearingAidPreference;
private List<Integer> mProfileSupportedList;
private AccessibilityHearingAidPreferenceController mPreferenceController; private AccessibilityHearingAidPreferenceController mPreferenceController;
@Mock @Mock
@@ -79,8 +80,6 @@ public class AccessibilityHearingAidPreferenceControllerTest {
@Mock @Mock
private CachedBluetoothDeviceManager mCachedDeviceManager; private CachedBluetoothDeviceManager mCachedDeviceManager;
@Mock @Mock
private LocalBluetoothAdapter mLocalBluetoothAdapter;
@Mock
private LocalBluetoothManager mLocalBluetoothManager; private LocalBluetoothManager mLocalBluetoothManager;
@Mock @Mock
private LocalBluetoothProfileManager mLocalBluetoothProfileManager; private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
@@ -161,7 +160,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
@Test @Test
public void onNotSupportHearingAidProfile_doNotDoReceiverOperation() { public void onNotSupportHearingAidProfile_doNotDoReceiverOperation() {
//clear bluetooth supported profile //clear bluetooth supported profile
mProfileSupportedList.clear(); mShadowBluetoothAdapter.clearSupportedProfiles();
mPreferenceController = new AccessibilityHearingAidPreferenceController(mContext, HEARING_AID_PREFERENCE); mPreferenceController = new AccessibilityHearingAidPreferenceController(mContext, HEARING_AID_PREFERENCE);
mPreferenceController.setPreference(mHearingAidPreference); mPreferenceController.setPreference(mHearingAidPreference);
//not call registerReceiver() //not call registerReceiver()
@@ -178,18 +177,17 @@ public class AccessibilityHearingAidPreferenceControllerTest {
mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext); mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
mBluetoothManager = new BluetoothManager(mContext); mBluetoothManager = new BluetoothManager(mContext);
mBluetoothAdapter = mBluetoothManager.getAdapter(); mBluetoothAdapter = mBluetoothManager.getAdapter();
when(mLocalBluetoothManager.getBluetoothAdapter()).thenReturn(mLocalBluetoothAdapter);
when(mLocalBluetoothAdapter.isEnabled()).thenReturn(true);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager); when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile); when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
} }
private void setupHearingAidEnvironment() { private void setupHearingAidEnvironment() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS); mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS);
mProfileSupportedList = new ArrayList<Integer>(); mShadowBluetoothAdapter.enable();
mProfileSupportedList.add(BluetoothProfile.HEARING_AID); mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID);
when(mLocalBluetoothAdapter.getSupportedProfiles()).thenReturn(mProfileSupportedList);
when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
when(mCachedBluetoothDevice.getName()).thenReturn(TEST_DEVICE_NAME); when(mCachedBluetoothDevice.getName()).thenReturn(TEST_DEVICE_NAME);
when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true);

View File

@@ -30,13 +30,19 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto
private String mName; private String mName;
private int mScanMode; private int mScanMode;
private int mState; private int mState;
private List<Integer> mSupportedProfiles = new ArrayList<Integer>();
/**
* Do nothing, implement it to avoid null pointer error inside BluetoothAdapter
*/
@Implementation @Implementation
public List<Integer> getSupportedProfiles() { public List<Integer> getSupportedProfiles() {
return new ArrayList<Integer>(); return mSupportedProfiles;
}
public void addSupportedProfiles(int profile) {
mSupportedProfiles.add(profile);
}
public void clearSupportedProfiles() {
mSupportedProfiles.clear();
} }
public void setName(String name) { public void setName(String name) {