Use BluetoothAdapter instead of LocalBluetoothAdapter

LocalBluetoothAdapter only has a few APIs that is not supported
by BluetoothAdapter, and lots of LocalBluetoothAdapter function
pass parameter to BluetoothAdapter directly.
Do the refactor in Settings, use BluetoothAdapter instead of
LocalBluetoothAdapter.

Bug: 111769754
Test: make -j42 RunSettingsRoboTests
Change-Id: I88e5a8377b5d1106c7679e6a8c3fd1ca1a80ea6f
This commit is contained in:
hughchen
2018-07-26 11:22:01 +08:00
parent 75bafefa49
commit e94b02206e
32 changed files with 327 additions and 341 deletions

View File

@@ -29,8 +29,6 @@ import com.android.settings.R;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.WirelessUtils;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import androidx.annotation.VisibleForTesting;
@@ -45,7 +43,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
private final MetricsFeatureProvider mMetricsFeatureProvider;
private Context mContext;
private boolean mValidListener;
private final LocalBluetoothAdapter mLocalAdapter;
private final BluetoothAdapter mBluetoothAdapter;
private final IntentFilter mIntentFilter;
private final RestrictionUtils mRestrictionUtils;
private SwitchWidgetController.OnSwitchChangeListener mCallback;
@@ -65,15 +63,14 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
};
public BluetoothEnabler(Context context, SwitchWidgetController switchController,
MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager,
int metricsEvent) {
this(context, switchController, metricsFeatureProvider, manager, metricsEvent,
MetricsFeatureProvider metricsFeatureProvider, int metricsEvent) {
this(context, switchController, metricsFeatureProvider, metricsEvent,
new RestrictionUtils());
}
public BluetoothEnabler(Context context, SwitchWidgetController switchController,
MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager,
int metricsEvent, RestrictionUtils restrictionUtils) {
MetricsFeatureProvider metricsFeatureProvider, int metricsEvent,
RestrictionUtils restrictionUtils) {
mContext = context;
mMetricsFeatureProvider = metricsFeatureProvider;
mSwitchController = switchController;
@@ -81,12 +78,10 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
mValidListener = false;
mMetricsEvent = metricsEvent;
if (manager == null) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Bluetooth is not supported
mLocalAdapter = null;
mSwitchController.setEnabled(false);
} else {
mLocalAdapter = manager.getBluetoothAdapter();
}
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
mRestrictionUtils = restrictionUtils;
@@ -107,14 +102,14 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
final boolean restricted = maybeEnforceRestrictions();
if (mLocalAdapter == null) {
if (mBluetoothAdapter == null) {
mSwitchController.setEnabled(false);
return;
}
// Bluetooth state is not sticky, so set it manually
if (!restricted) {
handleStateChanged(mLocalAdapter.getBluetoothState());
handleStateChanged(mBluetoothAdapter.getState());
}
mSwitchController.startListening();
@@ -123,7 +118,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
}
public void pause() {
if (mLocalAdapter == null) {
if (mBluetoothAdapter == null) {
return;
}
if (mValidListener) {
@@ -188,8 +183,8 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
mMetricsFeatureProvider.action(mContext, mMetricsEvent, isChecked);
if (mLocalAdapter != null) {
boolean status = mLocalAdapter.setBluetoothEnabled(isChecked);
if (mBluetoothAdapter != null) {
boolean status = setBluetoothEnabled(isChecked);
// If we cannot toggle it ON then reset the UI assets:
// a) The switch should be OFF but it should still be togglable (enabled = True)
// b) The switch bar should have OFF text.
@@ -249,4 +244,8 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
mCallback.onSwitchToggled(isChecked);
}
}
private boolean setBluetoothEnabled(boolean isEnabled) {
return isEnabled ? mBluetoothAdapter.enable() : mBluetoothAdapter.disable();
}
}