Handle Bluetooth is not supported on emulator

- Disable audio swicher while Bluetooth feature is not supported
- Error handle before using LocalBluetoothManager in the constructor

Bug: 80491267
Test: make RunSettingsRoboTests ROBOTEST_FILTER="AudioOutputSwitchPreferenceControllerTest" -j42

Change-Id: I971f31cd08dd0a2778548f6d1d675f279d92ef8e
This commit is contained in:
ryanywlin
2018-05-31 09:37:20 +08:00
parent ce7d1ab3bb
commit d67314e771
2 changed files with 49 additions and 4 deletions

View File

@@ -29,6 +29,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.media.AudioDeviceCallback;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
@@ -113,6 +114,10 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
Log.w(TAG, "Error getting LocalBluetoothManager.", e);
return;
}
if (mLocalBluetoothManager == null) {
Log.e(TAG, "Bluetooth is not supported on this device");
return;
}
mLocalBluetoothManager.setForegroundActivity(mContext);
mProfileManager = mLocalBluetoothManager.getProfileManager();
}
@@ -123,7 +128,8 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
*/
@Override
public final int getAvailabilityStatus() {
return FeatureFlagUtils.isEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS)
return FeatureFlagUtils.isEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS) &&
mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}

View File

@@ -42,6 +42,7 @@ import static org.robolectric.Shadows.shadowOf;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.pm.PackageManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
@@ -72,7 +73,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowBluetoothDevice;
import org.robolectric.shadows.ShadowPackageManager;
import java.util.ArrayList;
import java.util.List;
@@ -122,6 +125,7 @@ public class AudioOutputSwitchPreferenceControllerTest {
private List<BluetoothDevice> mProfileConnectedDevices;
private List<BluetoothDevice> mHearingAidActiveDevices;
private List<BluetoothDevice> mEmptyDevices;
private ShadowPackageManager mPackageManager;
@Before
public void setUp() {
@@ -139,6 +143,8 @@ public class AudioOutputSwitchPreferenceControllerTest {
when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
mPackageManager = Shadow.extract(mContext.getPackageManager());
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
mBluetoothManager = new BluetoothManager(mContext);
mBluetoothAdapter = mBluetoothManager.getAdapter();
@@ -174,13 +180,46 @@ public class AudioOutputSwitchPreferenceControllerTest {
}
@Test
public void getAvailabilityStatus_byDefault_isAvailable() {
public void constructor_notSupportBluetooth_shouldReturnBeforeUsingLocalBluetoothManager() {
ShadowBluetoothUtils.reset();
mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
AudioSwitchPreferenceController controller = new AudioSwitchPreferenceControllerTestable(
mContext, TEST_KEY);
assertThat(mLocalBluetoothManager).isNull();
}
@Test
public void getAvailabilityStatus_disableFlagNoBluetoothFeature_returnUnavailable() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_disableFlagWithBluetoothFeature_returnUnavailable() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_enableFlagWithBluetoothFeature_returnAvailable() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, true);
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_whenNotVisible_isDisable() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
public void getAvailabilityStatus_enableFlagNoBluetoothFeature_returnUnavailable() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, true);
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}