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:
@@ -22,8 +22,6 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
/** Helper class, intended to be used by an Activity, to keep the local Bluetooth adapter in
|
/** Helper class, intended to be used by an Activity, to keep the local Bluetooth adapter in
|
||||||
@@ -35,15 +33,15 @@ public class AlwaysDiscoverable extends BroadcastReceiver {
|
|||||||
private static final String TAG = "AlwaysDiscoverable";
|
private static final String TAG = "AlwaysDiscoverable";
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
private IntentFilter mIntentFilter;
|
private IntentFilter mIntentFilter;
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
boolean mStarted;
|
boolean mStarted;
|
||||||
|
|
||||||
public AlwaysDiscoverable(Context context, LocalBluetoothAdapter localAdapter) {
|
public AlwaysDiscoverable(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mLocalAdapter = localAdapter;
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
mIntentFilter = new IntentFilter();
|
mIntentFilter = new IntentFilter();
|
||||||
mIntentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
|
mIntentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
|
||||||
}
|
}
|
||||||
@@ -56,8 +54,9 @@ public class AlwaysDiscoverable extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
mContext.registerReceiver(this, mIntentFilter);
|
mContext.registerReceiver(this, mIntentFilter);
|
||||||
mStarted = true;
|
mStarted = true;
|
||||||
if (mLocalAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
if (mBluetoothAdapter.getScanMode()
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
!= BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
||||||
|
mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +66,7 @@ public class AlwaysDiscoverable extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
mContext.unregisterReceiver(this);
|
mContext.unregisterReceiver(this);
|
||||||
mStarted = false;
|
mStarted = false;
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -76,8 +75,9 @@ public class AlwaysDiscoverable extends BroadcastReceiver {
|
|||||||
if (action != BluetoothAdapter.ACTION_SCAN_MODE_CHANGED) {
|
if (action != BluetoothAdapter.ACTION_SCAN_MODE_CHANGED) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (mLocalAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
if (mBluetoothAdapter.getScanMode()
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
!= BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
||||||
|
mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -27,8 +27,6 @@ import android.util.Log;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
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.OnStart;
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||||
@@ -46,8 +44,7 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
Preference mPreference;
|
Preference mPreference;
|
||||||
private LocalBluetoothManager mLocalManager;
|
protected BluetoothAdapter mBluetoothAdapter;
|
||||||
protected LocalBluetoothAdapter mLocalAdapter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor exclusively used for Slice.
|
* Constructor exclusively used for Slice.
|
||||||
@@ -55,19 +52,11 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
|||||||
public BluetoothDeviceNamePreferenceController(Context context, String preferenceKey) {
|
public BluetoothDeviceNamePreferenceController(Context context, String preferenceKey) {
|
||||||
super(context, preferenceKey);
|
super(context, preferenceKey);
|
||||||
|
|
||||||
mLocalManager = Utils.getLocalBtManager(context);
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (mLocalManager == null) {
|
if (mBluetoothAdapter == null) {
|
||||||
Log.e(TAG, "Bluetooth is not supported on this device");
|
Log.e(TAG, "Bluetooth is not supported on this device");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mLocalAdapter = mLocalManager.getBluetoothAdapter();
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
BluetoothDeviceNamePreferenceController(Context context, LocalBluetoothAdapter localAdapter,
|
|
||||||
String preferenceKey) {
|
|
||||||
super(context, preferenceKey);
|
|
||||||
mLocalAdapter = localAdapter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -91,7 +80,7 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAvailabilityStatus() {
|
public int getAvailabilityStatus() {
|
||||||
return mLocalAdapter != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
return mBluetoothAdapter != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -138,7 +127,7 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String getDeviceName() {
|
protected String getDeviceName() {
|
||||||
return mLocalAdapter.getName();
|
return mBluetoothAdapter.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -152,7 +141,8 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
|||||||
final String action = intent.getAction();
|
final String action = intent.getAction();
|
||||||
|
|
||||||
if (TextUtils.equals(action, BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) {
|
if (TextUtils.equals(action, BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) {
|
||||||
if (mPreference != null && mLocalAdapter != null && mLocalAdapter.isEnabled()) {
|
if (mPreference != null && mBluetoothAdapter != null
|
||||||
|
&& mBluetoothAdapter.isEnabled()) {
|
||||||
updatePreferenceState(mPreference);
|
updatePreferenceState(mPreference);
|
||||||
}
|
}
|
||||||
} else if (TextUtils.equals(action, BluetoothAdapter.ACTION_STATE_CHANGED)) {
|
} else if (TextUtils.equals(action, BluetoothAdapter.ACTION_STATE_CHANGED)) {
|
||||||
|
@@ -16,12 +16,12 @@
|
|||||||
|
|
||||||
package com.android.settings.bluetooth;
|
package com.android.settings.bluetooth;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto;
|
import com.android.internal.logging.nano.MetricsProto;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
@@ -42,13 +42,6 @@ public class BluetoothDeviceRenamePreferenceController extends
|
|||||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
BluetoothDeviceRenamePreferenceController(Context context, LocalBluetoothAdapter localAdapter,
|
|
||||||
String preferenceKey) {
|
|
||||||
super(context, localAdapter, preferenceKey);
|
|
||||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the {@link Fragment} that used to show {@link LocalDeviceNameDialogFragment}
|
* Set the {@link Fragment} that used to show {@link LocalDeviceNameDialogFragment}
|
||||||
* in {@code handlePreferenceTreeClick}
|
* in {@code handlePreferenceTreeClick}
|
||||||
@@ -61,7 +54,7 @@ public class BluetoothDeviceRenamePreferenceController extends
|
|||||||
@Override
|
@Override
|
||||||
protected void updatePreferenceState(final Preference preference) {
|
protected void updatePreferenceState(final Preference preference) {
|
||||||
preference.setSummary(getSummary());
|
preference.setSummary(getSummary());
|
||||||
preference.setVisible(mLocalAdapter != null && mLocalAdapter.isEnabled());
|
preference.setVisible(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -28,7 +28,6 @@ import android.util.Log;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
|
import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
|
|
||||||
@@ -64,7 +63,7 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
private final Handler mUiHandler;
|
private final Handler mUiHandler;
|
||||||
private final Preference mDiscoveryPreference;
|
private final Preference mDiscoveryPreference;
|
||||||
|
|
||||||
private final LocalBluetoothAdapter mLocalAdapter;
|
private final BluetoothAdapter mBluetoothAdapter;
|
||||||
|
|
||||||
private final SharedPreferences mSharedPreferences;
|
private final SharedPreferences mSharedPreferences;
|
||||||
|
|
||||||
@@ -92,17 +91,16 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
BluetoothDiscoverableEnabler(LocalBluetoothAdapter adapter,
|
BluetoothDiscoverableEnabler(Preference discoveryPreference) {
|
||||||
Preference discoveryPreference) {
|
|
||||||
mUiHandler = new Handler();
|
mUiHandler = new Handler();
|
||||||
mLocalAdapter = adapter;
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
mDiscoveryPreference = discoveryPreference;
|
mDiscoveryPreference = discoveryPreference;
|
||||||
mSharedPreferences = discoveryPreference.getSharedPreferences();
|
mSharedPreferences = discoveryPreference.getSharedPreferences();
|
||||||
discoveryPreference.setPersistent(false);
|
discoveryPreference.setPersistent(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resume(Context context) {
|
public void resume(Context context) {
|
||||||
if (mLocalAdapter == null) {
|
if (mBluetoothAdapter == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,11 +111,11 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
|
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
|
||||||
mContext.registerReceiver(mReceiver, filter);
|
mContext.registerReceiver(mReceiver, filter);
|
||||||
mDiscoveryPreference.setOnPreferenceClickListener(this);
|
mDiscoveryPreference.setOnPreferenceClickListener(this);
|
||||||
handleModeChanged(mLocalAdapter.getScanMode());
|
handleModeChanged(mBluetoothAdapter.getScanMode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pause() {
|
public void pause() {
|
||||||
if (mLocalAdapter == null) {
|
if (mBluetoothAdapter == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +137,8 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
long endTimestamp = System.currentTimeMillis() + timeout * 1000L;
|
long endTimestamp = System.currentTimeMillis() + timeout * 1000L;
|
||||||
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
|
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
|
||||||
|
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
|
mBluetoothAdapter
|
||||||
|
.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
|
||||||
updateCountdownSummary();
|
updateCountdownSummary();
|
||||||
|
|
||||||
Log.d(TAG, "setEnabled(): enabled = " + enable + "timeout = " + timeout);
|
Log.d(TAG, "setEnabled(): enabled = " + enable + "timeout = " + timeout);
|
||||||
@@ -151,7 +150,7 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
||||||
BluetoothDiscoverableTimeoutReceiver.cancelDiscoverableAlarm(mContext);
|
BluetoothDiscoverableTimeoutReceiver.cancelDiscoverableAlarm(mContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,7 +249,7 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
|
|
||||||
void setNumberOfPairedDevices(int pairedDevices) {
|
void setNumberOfPairedDevices(int pairedDevices) {
|
||||||
mNumberOfPairedDevices = pairedDevices;
|
mNumberOfPairedDevices = pairedDevices;
|
||||||
handleModeChanged(mLocalAdapter.getScanMode());
|
handleModeChanged(mBluetoothAdapter.getScanMode());
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleModeChanged(int mode) {
|
void handleModeChanged(int mode) {
|
||||||
@@ -273,7 +272,7 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateCountdownSummary() {
|
private void updateCountdownSummary() {
|
||||||
int mode = mLocalAdapter.getScanMode();
|
int mode = mBluetoothAdapter.getScanMode();
|
||||||
if (mode != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
if (mode != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@@ -29,8 +29,6 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.widget.SwitchWidgetController;
|
import com.android.settings.widget.SwitchWidgetController;
|
||||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||||
import com.android.settingslib.WirelessUtils;
|
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 com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
@@ -45,7 +43,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private boolean mValidListener;
|
private boolean mValidListener;
|
||||||
private final LocalBluetoothAdapter mLocalAdapter;
|
private final BluetoothAdapter mBluetoothAdapter;
|
||||||
private final IntentFilter mIntentFilter;
|
private final IntentFilter mIntentFilter;
|
||||||
private final RestrictionUtils mRestrictionUtils;
|
private final RestrictionUtils mRestrictionUtils;
|
||||||
private SwitchWidgetController.OnSwitchChangeListener mCallback;
|
private SwitchWidgetController.OnSwitchChangeListener mCallback;
|
||||||
@@ -65,15 +63,14 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
};
|
};
|
||||||
|
|
||||||
public BluetoothEnabler(Context context, SwitchWidgetController switchController,
|
public BluetoothEnabler(Context context, SwitchWidgetController switchController,
|
||||||
MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager,
|
MetricsFeatureProvider metricsFeatureProvider, int metricsEvent) {
|
||||||
int metricsEvent) {
|
this(context, switchController, metricsFeatureProvider, metricsEvent,
|
||||||
this(context, switchController, metricsFeatureProvider, manager, metricsEvent,
|
|
||||||
new RestrictionUtils());
|
new RestrictionUtils());
|
||||||
}
|
}
|
||||||
|
|
||||||
public BluetoothEnabler(Context context, SwitchWidgetController switchController,
|
public BluetoothEnabler(Context context, SwitchWidgetController switchController,
|
||||||
MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager,
|
MetricsFeatureProvider metricsFeatureProvider, int metricsEvent,
|
||||||
int metricsEvent, RestrictionUtils restrictionUtils) {
|
RestrictionUtils restrictionUtils) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mMetricsFeatureProvider = metricsFeatureProvider;
|
mMetricsFeatureProvider = metricsFeatureProvider;
|
||||||
mSwitchController = switchController;
|
mSwitchController = switchController;
|
||||||
@@ -81,12 +78,10 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
mValidListener = false;
|
mValidListener = false;
|
||||||
mMetricsEvent = metricsEvent;
|
mMetricsEvent = metricsEvent;
|
||||||
|
|
||||||
if (manager == null) {
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
if (mBluetoothAdapter == null) {
|
||||||
// Bluetooth is not supported
|
// Bluetooth is not supported
|
||||||
mLocalAdapter = null;
|
|
||||||
mSwitchController.setEnabled(false);
|
mSwitchController.setEnabled(false);
|
||||||
} else {
|
|
||||||
mLocalAdapter = manager.getBluetoothAdapter();
|
|
||||||
}
|
}
|
||||||
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
|
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
|
||||||
mRestrictionUtils = restrictionUtils;
|
mRestrictionUtils = restrictionUtils;
|
||||||
@@ -107,14 +102,14 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
|
|
||||||
final boolean restricted = maybeEnforceRestrictions();
|
final boolean restricted = maybeEnforceRestrictions();
|
||||||
|
|
||||||
if (mLocalAdapter == null) {
|
if (mBluetoothAdapter == null) {
|
||||||
mSwitchController.setEnabled(false);
|
mSwitchController.setEnabled(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bluetooth state is not sticky, so set it manually
|
// Bluetooth state is not sticky, so set it manually
|
||||||
if (!restricted) {
|
if (!restricted) {
|
||||||
handleStateChanged(mLocalAdapter.getBluetoothState());
|
handleStateChanged(mBluetoothAdapter.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
mSwitchController.startListening();
|
mSwitchController.startListening();
|
||||||
@@ -123,7 +118,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void pause() {
|
public void pause() {
|
||||||
if (mLocalAdapter == null) {
|
if (mBluetoothAdapter == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (mValidListener) {
|
if (mValidListener) {
|
||||||
@@ -188,8 +183,8 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
|
|
||||||
mMetricsFeatureProvider.action(mContext, mMetricsEvent, isChecked);
|
mMetricsFeatureProvider.action(mContext, mMetricsEvent, isChecked);
|
||||||
|
|
||||||
if (mLocalAdapter != null) {
|
if (mBluetoothAdapter != null) {
|
||||||
boolean status = mLocalAdapter.setBluetoothEnabled(isChecked);
|
boolean status = setBluetoothEnabled(isChecked);
|
||||||
// If we cannot toggle it ON then reset the UI assets:
|
// 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)
|
// a) The switch should be OFF but it should still be togglable (enabled = True)
|
||||||
// b) The switch bar should have OFF text.
|
// b) The switch bar should have OFF text.
|
||||||
@@ -249,4 +244,8 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
|
|||||||
mCallback.onSwitchToggled(isChecked);
|
mCallback.onSwitchToggled(isChecked);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean setBluetoothEnabled(boolean isEnabled) {
|
||||||
|
return isEnabled ? mBluetoothAdapter.enable() : mBluetoothAdapter.disable();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -63,7 +63,7 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
|||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
mInitialScanStarted = false;
|
mInitialScanStarted = false;
|
||||||
mAlwaysDiscoverable = new AlwaysDiscoverable(getContext(), mLocalAdapter);
|
mAlwaysDiscoverable = new AlwaysDiscoverable(getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,7 +74,7 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
updateBluetooth();
|
updateBluetooth();
|
||||||
mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering());
|
mAvailableDevicesCategory.setProgress(mBluetoothAdapter.isDiscovering());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -85,11 +85,11 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void updateBluetooth() {
|
void updateBluetooth() {
|
||||||
if (mLocalAdapter.isEnabled()) {
|
if (mBluetoothAdapter.isEnabled()) {
|
||||||
updateContent(mLocalAdapter.getBluetoothState());
|
updateContent(mBluetoothAdapter.getState());
|
||||||
} else {
|
} else {
|
||||||
// Turn on bluetooth if it is disabled
|
// Turn on bluetooth if it is disabled
|
||||||
mLocalAdapter.enable();
|
mBluetoothAdapter.enable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +148,7 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
|||||||
switch (bluetoothState) {
|
switch (bluetoothState) {
|
||||||
case BluetoothAdapter.STATE_ON:
|
case BluetoothAdapter.STATE_ON:
|
||||||
mDevicePreferenceMap.clear();
|
mDevicePreferenceMap.clear();
|
||||||
mLocalAdapter.setBluetoothEnabled(true);
|
mBluetoothAdapter.enable();
|
||||||
|
|
||||||
addDeviceCategory(mAvailableDevicesCategory,
|
addDeviceCategory(mAvailableDevicesCategory,
|
||||||
R.string.bluetooth_preference_found_media_devices,
|
R.string.bluetooth_preference_found_media_devices,
|
||||||
|
@@ -33,8 +33,6 @@ import com.android.settings.SubSettings;
|
|||||||
import com.android.settings.connecteddevice.BluetoothDashboardFragment;
|
import com.android.settings.connecteddevice.BluetoothDashboardFragment;
|
||||||
import com.android.settings.slices.SliceBroadcastReceiver;
|
import com.android.settings.slices.SliceBroadcastReceiver;
|
||||||
import com.android.settings.slices.SliceBuilderUtils;
|
import com.android.settings.slices.SliceBuilderUtils;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
|
|
||||||
import androidx.core.graphics.drawable.IconCompat;
|
import androidx.core.graphics.drawable.IconCompat;
|
||||||
import androidx.slice.Slice;
|
import androidx.slice.Slice;
|
||||||
@@ -119,10 +117,13 @@ public class BluetoothSliceBuilder {
|
|||||||
*/
|
*/
|
||||||
public static void handleUriChange(Context context, Intent intent) {
|
public static void handleUriChange(Context context, Intent intent) {
|
||||||
final boolean newBluetoothState = intent.getBooleanExtra(EXTRA_TOGGLE_STATE, false);
|
final boolean newBluetoothState = intent.getBooleanExtra(EXTRA_TOGGLE_STATE, false);
|
||||||
final LocalBluetoothAdapter adapter = LocalBluetoothManager.getInstance(context,
|
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
null /* callback */).getBluetoothAdapter();
|
|
||||||
|
|
||||||
adapter.setBluetoothEnabled(newBluetoothState);
|
if (newBluetoothState) {
|
||||||
|
adapter.enable();
|
||||||
|
} else {
|
||||||
|
adapter.disable();
|
||||||
|
}
|
||||||
// Do not notifyChange on Uri. The service takes longer to update the current value than it
|
// Do not notifyChange on Uri. The service takes longer to update the current value than it
|
||||||
// does for the Slice to check the current value again. Let {@link SliceBroadcastRelay}
|
// does for the Slice to check the current value again. Let {@link SliceBroadcastRelay}
|
||||||
// handle it.
|
// handle it.
|
||||||
|
@@ -25,7 +25,6 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.widget.SummaryUpdater;
|
import com.android.settings.widget.SummaryUpdater;
|
||||||
import com.android.settingslib.bluetooth.BluetoothCallback;
|
import com.android.settingslib.bluetooth.BluetoothCallback;
|
||||||
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 java.util.Set;
|
import java.util.Set;
|
||||||
@@ -39,15 +38,14 @@ import androidx.annotation.VisibleForTesting;
|
|||||||
public final class BluetoothSummaryUpdater extends SummaryUpdater implements BluetoothCallback {
|
public final class BluetoothSummaryUpdater extends SummaryUpdater implements BluetoothCallback {
|
||||||
private static final String TAG = "BluetoothSummaryUpdater";
|
private static final String TAG = "BluetoothSummaryUpdater";
|
||||||
|
|
||||||
|
private final BluetoothAdapter mBluetoothAdapter;
|
||||||
private final LocalBluetoothManager mBluetoothManager;
|
private final LocalBluetoothManager mBluetoothManager;
|
||||||
private final LocalBluetoothAdapter mBluetoothAdapter;
|
|
||||||
|
|
||||||
public BluetoothSummaryUpdater(Context context, OnSummaryChangeListener listener,
|
public BluetoothSummaryUpdater(Context context, OnSummaryChangeListener listener,
|
||||||
LocalBluetoothManager bluetoothManager) {
|
LocalBluetoothManager bluetoothManager) {
|
||||||
super(context, listener);
|
super(context, listener);
|
||||||
mBluetoothManager = bluetoothManager;
|
mBluetoothManager = bluetoothManager;
|
||||||
mBluetoothAdapter = mBluetoothManager != null
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
? mBluetoothManager.getBluetoothAdapter() : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -26,8 +26,6 @@ import com.android.settings.location.ScanningSettings;
|
|||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
import com.android.settings.utils.AnnotationSpan;
|
import com.android.settings.utils.AnnotationSpan;
|
||||||
import com.android.settings.widget.SwitchWidgetController;
|
import com.android.settings.widget.SwitchWidgetController;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
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.OnStart;
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||||
@@ -43,10 +41,6 @@ public class BluetoothSwitchPreferenceController
|
|||||||
implements LifecycleObserver, OnStart, OnStop,
|
implements LifecycleObserver, OnStart, OnStop,
|
||||||
SwitchWidgetController.OnSwitchChangeListener, View.OnClickListener {
|
SwitchWidgetController.OnSwitchChangeListener, View.OnClickListener {
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
LocalBluetoothAdapter mBluetoothAdapter;
|
|
||||||
|
|
||||||
private LocalBluetoothManager mBluetoothManager;
|
|
||||||
private BluetoothEnabler mBluetoothEnabler;
|
private BluetoothEnabler mBluetoothEnabler;
|
||||||
private RestrictionUtils mRestrictionUtils;
|
private RestrictionUtils mRestrictionUtils;
|
||||||
private SwitchWidgetController mSwitch;
|
private SwitchWidgetController mSwitch;
|
||||||
@@ -56,15 +50,12 @@ public class BluetoothSwitchPreferenceController
|
|||||||
public BluetoothSwitchPreferenceController(Context context,
|
public BluetoothSwitchPreferenceController(Context context,
|
||||||
SwitchWidgetController switchController,
|
SwitchWidgetController switchController,
|
||||||
FooterPreference footerPreference) {
|
FooterPreference footerPreference) {
|
||||||
this(context, Utils.getLocalBtManager(context), new RestrictionUtils(), switchController,
|
this(context, new RestrictionUtils(), switchController, footerPreference);
|
||||||
footerPreference);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public BluetoothSwitchPreferenceController(Context context,
|
public BluetoothSwitchPreferenceController(Context context, RestrictionUtils restrictionUtils,
|
||||||
LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils,
|
|
||||||
SwitchWidgetController switchController, FooterPreference footerPreference) {
|
SwitchWidgetController switchController, FooterPreference footerPreference) {
|
||||||
mBluetoothManager = bluetoothManager;
|
|
||||||
mRestrictionUtils = restrictionUtils;
|
mRestrictionUtils = restrictionUtils;
|
||||||
mSwitch = switchController;
|
mSwitch = switchController;
|
||||||
mContext = context;
|
mContext = context;
|
||||||
@@ -73,12 +64,9 @@ public class BluetoothSwitchPreferenceController
|
|||||||
mSwitch.setupView();
|
mSwitch.setupView();
|
||||||
updateText(mSwitch.isChecked());
|
updateText(mSwitch.isChecked());
|
||||||
|
|
||||||
if (mBluetoothManager != null) {
|
|
||||||
mBluetoothAdapter = mBluetoothManager.getBluetoothAdapter();
|
|
||||||
}
|
|
||||||
mBluetoothEnabler = new BluetoothEnabler(context,
|
mBluetoothEnabler = new BluetoothEnabler(context,
|
||||||
switchController,
|
switchController,
|
||||||
FeatureFactory.getFactory(context).getMetricsFeatureProvider(), mBluetoothManager,
|
FeatureFactory.getFactory(context).getMetricsFeatureProvider(),
|
||||||
MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE,
|
MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE,
|
||||||
mRestrictionUtils);
|
mRestrictionUtils);
|
||||||
mBluetoothEnabler.setToggleCallback(this);
|
mBluetoothEnabler.setToggleCallback(this);
|
||||||
|
@@ -28,7 +28,6 @@ import com.android.settings.dashboard.RestrictedDashboardFragment;
|
|||||||
import com.android.settingslib.bluetooth.BluetoothCallback;
|
import com.android.settingslib.bluetooth.BluetoothCallback;
|
||||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||||
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 java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -64,7 +63,7 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
|
|
||||||
BluetoothDevice mSelectedDevice;
|
BluetoothDevice mSelectedDevice;
|
||||||
|
|
||||||
LocalBluetoothAdapter mLocalAdapter;
|
BluetoothAdapter mBluetoothAdapter;
|
||||||
LocalBluetoothManager mLocalManager;
|
LocalBluetoothManager mLocalManager;
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -97,7 +96,7 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
Log.e(TAG, "Bluetooth is not supported on this device");
|
Log.e(TAG, "Bluetooth is not supported on this device");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mLocalAdapter = mLocalManager.getBluetoothAdapter();
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
mShowDevicesWithoutNames = SystemProperties.getBoolean(
|
mShowDevicesWithoutNames = SystemProperties.getBoolean(
|
||||||
BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY, false);
|
BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY, false);
|
||||||
|
|
||||||
@@ -146,7 +145,7 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceTreeClick(Preference preference) {
|
public boolean onPreferenceTreeClick(Preference preference) {
|
||||||
if (KEY_BT_SCAN.equals(preference.getKey())) {
|
if (KEY_BT_SCAN.equals(preference.getKey())) {
|
||||||
mLocalAdapter.startScanning(true);
|
startScanning();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +171,7 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prevent updates while the list shows one of the state messages
|
// Prevent updates while the list shows one of the state messages
|
||||||
if (mLocalAdapter.getBluetoothState() != BluetoothAdapter.STATE_ON) return;
|
if (mBluetoothAdapter.getState() != BluetoothAdapter.STATE_ON) return;
|
||||||
|
|
||||||
if (mFilter.matches(cachedDevice.getDevice())) {
|
if (mFilter.matches(cachedDevice.getDevice())) {
|
||||||
createDevicePreference(cachedDevice);
|
createDevicePreference(cachedDevice);
|
||||||
@@ -214,7 +213,7 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
|
|
||||||
myDevicePreference.setTitle(getString(
|
myDevicePreference.setTitle(getString(
|
||||||
R.string.bluetooth_footer_mac_message,
|
R.string.bluetooth_footer_mac_message,
|
||||||
bidiFormatter.unicodeWrap(mLocalAdapter.getAddress())));
|
bidiFormatter.unicodeWrap(mBluetoothAdapter.getAddress())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -227,21 +226,21 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void enableScanning() {
|
void enableScanning() {
|
||||||
// LocalBluetoothAdapter already handles repeated scan requests
|
// BluetoothAdapter already handles repeated scan requests
|
||||||
mLocalAdapter.startScanning(true);
|
startScanning();
|
||||||
mScanEnabled = true;
|
mScanEnabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void disableScanning() {
|
void disableScanning() {
|
||||||
mLocalAdapter.stopScanning();
|
stopScanning();
|
||||||
mScanEnabled = false;
|
mScanEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onScanningStateChanged(boolean started) {
|
public void onScanningStateChanged(boolean started) {
|
||||||
if (!started && mScanEnabled) {
|
if (!started && mScanEnabled) {
|
||||||
mLocalAdapter.startScanning(true);
|
startScanning();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,4 +286,16 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
public boolean shouldShowDevicesWithoutNames() {
|
public boolean shouldShowDevicesWithoutNames() {
|
||||||
return mShowDevicesWithoutNames;
|
return mShowDevicesWithoutNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void startScanning() {
|
||||||
|
if (!mBluetoothAdapter.isDiscovering()) {
|
||||||
|
mBluetoothAdapter.startDiscovery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void stopScanning() {
|
||||||
|
if (mBluetoothAdapter.isDiscovering()) {
|
||||||
|
mBluetoothAdapter.cancelDiscovery();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -94,7 +94,7 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
|
|||||||
mSelectedDevice = null;
|
mSelectedDevice = null;
|
||||||
if (mScanAllowed) {
|
if (mScanAllowed) {
|
||||||
enableScanning();
|
enableScanning();
|
||||||
mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering());
|
mAvailableDevicesCategory.setProgress(mBluetoothAdapter.isDiscovering());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,13 +16,13 @@
|
|||||||
|
|
||||||
package com.android.settings.bluetooth;
|
package com.android.settings.bluetooth;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,7 +89,7 @@ final class LocalBluetoothPreferences {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If the device was discoverING recently
|
// If the device was discoverING recently
|
||||||
LocalBluetoothAdapter adapter = manager.getBluetoothAdapter();
|
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (adapter != null) {
|
if (adapter != null) {
|
||||||
if (adapter.isDiscovering()) {
|
if (adapter.isDiscovering()) {
|
||||||
return true;
|
return true;
|
||||||
|
@@ -25,13 +25,11 @@ import android.os.Bundle;
|
|||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto;
|
import com.android.internal.logging.nano.MetricsProto;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
|
|
||||||
/** Provides a dialog for changing the advertised name of the local bluetooth adapter. */
|
/** Provides a dialog for changing the advertised name of the local bluetooth adapter. */
|
||||||
public class LocalDeviceNameDialogFragment extends BluetoothNameDialogFragment {
|
public class LocalDeviceNameDialogFragment extends BluetoothNameDialogFragment {
|
||||||
public static final String TAG = "LocalAdapterName";
|
public static final String TAG = "LocalAdapterName";
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
|
|
||||||
public static LocalDeviceNameDialogFragment newInstance() {
|
public static LocalDeviceNameDialogFragment newInstance() {
|
||||||
return new LocalDeviceNameDialogFragment();
|
return new LocalDeviceNameDialogFragment();
|
||||||
@@ -53,8 +51,7 @@ public class LocalDeviceNameDialogFragment extends BluetoothNameDialogFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
LocalBluetoothManager localManager = Utils.getLocalBtManager(getActivity());
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
mLocalAdapter = localManager.getBluetoothAdapter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -84,14 +81,14 @@ public class LocalDeviceNameDialogFragment extends BluetoothNameDialogFragment {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getDeviceName() {
|
protected String getDeviceName() {
|
||||||
if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
|
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
|
||||||
return mLocalAdapter.getName();
|
return mBluetoothAdapter.getName();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setDeviceName(String deviceName) {
|
protected void setDeviceName(String deviceName) {
|
||||||
mLocalAdapter.setName(deviceName);
|
mBluetoothAdapter.setName(deviceName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -34,8 +34,6 @@ import android.util.Log;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
|
import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
|
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
|
||||||
@@ -58,7 +56,7 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
static final int REQUEST_ENABLE_DISCOVERABLE = 2;
|
static final int REQUEST_ENABLE_DISCOVERABLE = 2;
|
||||||
static final int REQUEST_DISABLE = 3;
|
static final int REQUEST_DISABLE = 3;
|
||||||
|
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
|
|
||||||
private int mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT;
|
private int mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT;
|
||||||
|
|
||||||
@@ -76,13 +74,13 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
|
|
||||||
setResult(Activity.RESULT_CANCELED);
|
setResult(Activity.RESULT_CANCELED);
|
||||||
|
|
||||||
// Note: initializes mLocalAdapter and returns true on error
|
// Note: initializes mBluetoothAdapter and returns true on error
|
||||||
if (parseIntent()) {
|
if (parseIntent()) {
|
||||||
finish();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int btState = mLocalAdapter.getState();
|
int btState = mBluetoothAdapter.getState();
|
||||||
|
|
||||||
if (mRequest == REQUEST_DISABLE) {
|
if (mRequest == REQUEST_DISABLE) {
|
||||||
switch (btState) {
|
switch (btState) {
|
||||||
@@ -204,7 +202,7 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
switch (mRequest) {
|
switch (mRequest) {
|
||||||
case REQUEST_ENABLE:
|
case REQUEST_ENABLE:
|
||||||
case REQUEST_ENABLE_DISCOVERABLE: {
|
case REQUEST_ENABLE_DISCOVERABLE: {
|
||||||
if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
|
if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
|
||||||
proceedAndFinish();
|
proceedAndFinish();
|
||||||
} else {
|
} else {
|
||||||
// If BT is not up yet, show "Turning on Bluetooth..."
|
// If BT is not up yet, show "Turning on Bluetooth..."
|
||||||
@@ -216,7 +214,7 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
} break;
|
} break;
|
||||||
|
|
||||||
case REQUEST_DISABLE: {
|
case REQUEST_DISABLE: {
|
||||||
if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_OFF) {
|
if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
|
||||||
proceedAndFinish();
|
proceedAndFinish();
|
||||||
} else {
|
} else {
|
||||||
// If BT is not up yet, show "Turning off Bluetooth..."
|
// If BT is not up yet, show "Turning off Bluetooth..."
|
||||||
@@ -252,7 +250,7 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
if (mRequest == REQUEST_ENABLE || mRequest == REQUEST_DISABLE) {
|
if (mRequest == REQUEST_ENABLE || mRequest == REQUEST_DISABLE) {
|
||||||
// BT toggled. Done
|
// BT toggled. Done
|
||||||
returnCode = RESULT_OK;
|
returnCode = RESULT_OK;
|
||||||
} else if (mLocalAdapter.setScanMode(
|
} else if (mBluetoothAdapter.setScanMode(
|
||||||
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
||||||
// If already in discoverable mode, this will extend the timeout.
|
// If already in discoverable mode, this will extend the timeout.
|
||||||
long endTime = System.currentTimeMillis() + (long) mTimeout * 1000;
|
long endTime = System.currentTimeMillis() + (long) mTimeout * 1000;
|
||||||
@@ -284,7 +282,7 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the received Intent and initialize mLocalBluetoothAdapter.
|
* Parse the received Intent and initialize mBluetoothAdapter.
|
||||||
* @return true if an error occurred; false otherwise
|
* @return true if an error occurred; false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean parseIntent() {
|
private boolean parseIntent() {
|
||||||
@@ -314,13 +312,6 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalBluetoothManager manager = Utils.getLocalBtManager(this);
|
|
||||||
if (manager == null) {
|
|
||||||
Log.e(TAG, "Error: there's a problem starting Bluetooth");
|
|
||||||
setResult(RESULT_CANCELED);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
String packageName = getCallingPackage();
|
String packageName = getCallingPackage();
|
||||||
if (TextUtils.isEmpty(packageName)) {
|
if (TextUtils.isEmpty(packageName)) {
|
||||||
packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
|
packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
|
||||||
@@ -340,7 +331,12 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mLocalAdapter = manager.getBluetoothAdapter();
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
if (mBluetoothAdapter == null) {
|
||||||
|
Log.e(TAG, "Error: there's a problem starting Bluetooth");
|
||||||
|
setResult(RESULT_CANCELED);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -28,8 +28,6 @@ import android.util.Log;
|
|||||||
import com.android.internal.app.AlertActivity;
|
import com.android.internal.app.AlertActivity;
|
||||||
import com.android.internal.app.AlertController;
|
import com.android.internal.app.AlertController;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RequestPermissionHelperActivity asks the user whether to toggle Bluetooth.
|
* RequestPermissionHelperActivity asks the user whether to toggle Bluetooth.
|
||||||
@@ -49,7 +47,7 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
|||||||
public static final String EXTRA_APP_LABEL =
|
public static final String EXTRA_APP_LABEL =
|
||||||
"com.android.settings.bluetooth.extra.APP_LABEL";
|
"com.android.settings.bluetooth.extra.APP_LABEL";
|
||||||
|
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
|
|
||||||
private CharSequence mAppLabel;
|
private CharSequence mAppLabel;
|
||||||
|
|
||||||
@@ -63,7 +61,7 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
|||||||
|
|
||||||
setResult(RESULT_CANCELED);
|
setResult(RESULT_CANCELED);
|
||||||
|
|
||||||
// Note: initializes mLocalAdapter and returns true on error
|
// Note: initializes mBluetoothAdapter and returns true on error
|
||||||
if (!parseIntent()) {
|
if (!parseIntent()) {
|
||||||
finish();
|
finish();
|
||||||
return;
|
return;
|
||||||
@@ -131,20 +129,20 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
|||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mLocalAdapter.enable();
|
mBluetoothAdapter.enable();
|
||||||
setResult(Activity.RESULT_OK);
|
setResult(Activity.RESULT_OK);
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case RequestPermissionActivity.REQUEST_DISABLE: {
|
case RequestPermissionActivity.REQUEST_DISABLE: {
|
||||||
mLocalAdapter.disable();
|
mBluetoothAdapter.disable();
|
||||||
setResult(Activity.RESULT_OK);
|
setResult(Activity.RESULT_OK);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the received Intent and initialize mLocalBluetoothAdapter.
|
* Parse the received Intent and initialize mBluetoothAdapter.
|
||||||
* @return true if an error occurred; false otherwise
|
* @return true if an error occurred; false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean parseIntent() {
|
private boolean parseIntent() {
|
||||||
@@ -167,16 +165,14 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalBluetoothManager manager = Utils.getLocalBtManager(this);
|
mAppLabel = getIntent().getCharSequenceExtra(EXTRA_APP_LABEL);
|
||||||
if (manager == null) {
|
|
||||||
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
if (mBluetoothAdapter == null) {
|
||||||
Log.e(TAG, "Error: there's a problem starting Bluetooth");
|
Log.e(TAG, "Error: there's a problem starting Bluetooth");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mAppLabel = getIntent().getCharSequenceExtra(EXTRA_APP_LABEL);
|
|
||||||
|
|
||||||
mLocalAdapter = manager.getBluetoothAdapter();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -30,7 +30,6 @@ import com.android.settings.bluetooth.AlwaysDiscoverable;
|
|||||||
import com.android.settings.bluetooth.Utils;
|
import com.android.settings.bluetooth.Utils;
|
||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.dashboard.DashboardFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
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;
|
||||||
@@ -54,7 +53,7 @@ public class DiscoverableFooterPreferenceController extends BasePreferenceContro
|
|||||||
LocalBluetoothManager mLocalManager;
|
LocalBluetoothManager mLocalManager;
|
||||||
private FooterPreferenceMixinCompat mFooterPreferenceMixin;
|
private FooterPreferenceMixinCompat mFooterPreferenceMixin;
|
||||||
private FooterPreference mPreference;
|
private FooterPreference mPreference;
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
private AlwaysDiscoverable mAlwaysDiscoverable;
|
private AlwaysDiscoverable mAlwaysDiscoverable;
|
||||||
|
|
||||||
public DiscoverableFooterPreferenceController(Context context) {
|
public DiscoverableFooterPreferenceController(Context context) {
|
||||||
@@ -63,8 +62,8 @@ public class DiscoverableFooterPreferenceController extends BasePreferenceContro
|
|||||||
if (mLocalManager == null) {
|
if (mLocalManager == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mLocalAdapter = mLocalManager.getBluetoothAdapter();
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
mAlwaysDiscoverable = new AlwaysDiscoverable(context, mLocalAdapter);
|
mAlwaysDiscoverable = new AlwaysDiscoverable(context);
|
||||||
initReceiver();
|
initReceiver();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +120,7 @@ public class DiscoverableFooterPreferenceController extends BasePreferenceContro
|
|||||||
mContext.registerReceiver(mBluetoothChangedReceiver,
|
mContext.registerReceiver(mBluetoothChangedReceiver,
|
||||||
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
|
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
|
||||||
mAlwaysDiscoverable.start();
|
mAlwaysDiscoverable.start();
|
||||||
updateFooterPreferenceTitle(mLocalAdapter.getState());
|
updateFooterPreferenceTitle(mBluetoothAdapter.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -142,7 +141,7 @@ public class DiscoverableFooterPreferenceController extends BasePreferenceContro
|
|||||||
}
|
}
|
||||||
|
|
||||||
private CharSequence getPreferenceTitle() {
|
private CharSequence getPreferenceTitle() {
|
||||||
final String deviceName = mLocalAdapter.getName();
|
final String deviceName = mBluetoothAdapter.getName();
|
||||||
if (TextUtils.isEmpty(deviceName)) {
|
if (TextUtils.isEmpty(deviceName)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
package com.android.settings.deviceinfo;
|
package com.android.settings.deviceinfo;
|
||||||
|
|
||||||
import android.annotation.Nullable;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.wifi.WifiConfiguration;
|
import android.net.wifi.WifiConfiguration;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
@@ -29,8 +29,6 @@ import com.android.settings.bluetooth.BluetoothLengthDeviceNameFilter;
|
|||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.widget.ValidatedEditTextPreference;
|
import com.android.settings.widget.ValidatedEditTextPreference;
|
||||||
import com.android.settings.wifi.tether.WifiDeviceNameTextValidator;
|
import com.android.settings.wifi.tether.WifiDeviceNameTextValidator;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
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.OnCreate;
|
import com.android.settingslib.core.lifecycle.events.OnCreate;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
|
import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
|
||||||
@@ -49,10 +47,9 @@ public class DeviceNamePreferenceController extends BasePreferenceController
|
|||||||
private static final String KEY_PENDING_DEVICE_NAME = "key_pending_device_name";
|
private static final String KEY_PENDING_DEVICE_NAME = "key_pending_device_name";
|
||||||
private String mDeviceName;
|
private String mDeviceName;
|
||||||
protected WifiManager mWifiManager;
|
protected WifiManager mWifiManager;
|
||||||
|
private final BluetoothAdapter mBluetoothAdapter;
|
||||||
private final WifiDeviceNameTextValidator mWifiDeviceNameTextValidator;
|
private final WifiDeviceNameTextValidator mWifiDeviceNameTextValidator;
|
||||||
private ValidatedEditTextPreference mPreference;
|
private ValidatedEditTextPreference mPreference;
|
||||||
@Nullable
|
|
||||||
private LocalBluetoothManager mBluetoothManager;
|
|
||||||
private DeviceNamePreferenceHost mHost;
|
private DeviceNamePreferenceHost mHost;
|
||||||
private String mPendingDeviceName;
|
private String mPendingDeviceName;
|
||||||
|
|
||||||
@@ -61,6 +58,7 @@ public class DeviceNamePreferenceController extends BasePreferenceController
|
|||||||
|
|
||||||
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
||||||
mWifiDeviceNameTextValidator = new WifiDeviceNameTextValidator();
|
mWifiDeviceNameTextValidator = new WifiDeviceNameTextValidator();
|
||||||
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
|
||||||
initializeDeviceName();
|
initializeDeviceName();
|
||||||
}
|
}
|
||||||
@@ -115,10 +113,6 @@ public class DeviceNamePreferenceController extends BasePreferenceController
|
|||||||
return mWifiDeviceNameTextValidator.isTextValid(deviceName);
|
return mWifiDeviceNameTextValidator.isTextValid(deviceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLocalBluetoothManager(LocalBluetoothManager localBluetoothManager) {
|
|
||||||
mBluetoothManager = localBluetoothManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void confirmDeviceName() {
|
public void confirmDeviceName() {
|
||||||
if (mPendingDeviceName != null) {
|
if (mPendingDeviceName != null) {
|
||||||
setDeviceName(mPendingDeviceName);
|
setDeviceName(mPendingDeviceName);
|
||||||
@@ -146,14 +140,8 @@ public class DeviceNamePreferenceController extends BasePreferenceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setBluetoothDeviceName(String deviceName) {
|
private void setBluetoothDeviceName(String deviceName) {
|
||||||
// Bluetooth manager doesn't exist for certain devices.
|
if (mBluetoothAdapter != null) {
|
||||||
if (mBluetoothManager == null) {
|
mBluetoothAdapter.setName(getFilteredBluetoothString(deviceName));
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final LocalBluetoothAdapter localBluetoothAdapter = mBluetoothManager.getBluetoothAdapter();
|
|
||||||
if (localBluetoothAdapter != null) {
|
|
||||||
localBluetoothAdapter.setName(getFilteredBluetoothString(deviceName));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package com.android.settings.deviceinfo.aboutphone;
|
package com.android.settings.deviceinfo.aboutphone;
|
||||||
|
|
||||||
import static com.android.settings.bluetooth.Utils.getLocalBtManager;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -116,7 +114,6 @@ public class MyDeviceInfoFragment extends DashboardFragment
|
|||||||
controllers.add(new BrandedAccountPreferenceController(context));
|
controllers.add(new BrandedAccountPreferenceController(context));
|
||||||
DeviceNamePreferenceController deviceNamePreferenceController =
|
DeviceNamePreferenceController deviceNamePreferenceController =
|
||||||
new DeviceNamePreferenceController(context);
|
new DeviceNamePreferenceController(context);
|
||||||
deviceNamePreferenceController.setLocalBluetoothManager(getLocalBtManager(context));
|
|
||||||
deviceNamePreferenceController.setHost(fragment);
|
deviceNamePreferenceController.setHost(fragment);
|
||||||
if (lifecycle != null) {
|
if (lifecycle != null) {
|
||||||
lifecycle.addObserver(deviceNamePreferenceController);
|
lifecycle.addObserver(deviceNamePreferenceController);
|
||||||
|
@@ -40,9 +40,6 @@ import android.util.Log;
|
|||||||
import android.widget.RemoteViews;
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.bluetooth.Utils;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides control of power-related settings from a widget.
|
* Provides control of power-related settings from a widget.
|
||||||
@@ -54,7 +51,7 @@ public class SettingsAppWidgetProvider extends AppWidgetProvider {
|
|||||||
new ComponentName("com.android.settings",
|
new ComponentName("com.android.settings",
|
||||||
"com.android.settings.widget.SettingsAppWidgetProvider");
|
"com.android.settings.widget.SettingsAppWidgetProvider");
|
||||||
|
|
||||||
private static LocalBluetoothAdapter sLocalBluetoothAdapter = null;
|
private static BluetoothAdapter sBluetoothAdapter = null;
|
||||||
|
|
||||||
private static final int BUTTON_WIFI = 0;
|
private static final int BUTTON_WIFI = 0;
|
||||||
private static final int BUTTON_BRIGHTNESS = 1;
|
private static final int BUTTON_BRIGHTNESS = 1;
|
||||||
@@ -450,23 +447,19 @@ public class SettingsAppWidgetProvider extends AppWidgetProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getActualState(Context context) {
|
public int getActualState(Context context) {
|
||||||
if (sLocalBluetoothAdapter == null) {
|
if (sBluetoothAdapter == null) {
|
||||||
LocalBluetoothManager manager = Utils.getLocalBtManager(context);
|
sBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (manager == null) {
|
if (sBluetoothAdapter == null) {
|
||||||
return STATE_UNKNOWN; // On emulator?
|
|
||||||
}
|
|
||||||
sLocalBluetoothAdapter = manager.getBluetoothAdapter();
|
|
||||||
if (sLocalBluetoothAdapter == null) {
|
|
||||||
return STATE_UNKNOWN; // On emulator?
|
return STATE_UNKNOWN; // On emulator?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bluetoothStateToFiveState(sLocalBluetoothAdapter.getBluetoothState());
|
return bluetoothStateToFiveState(sBluetoothAdapter.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void requestStateChange(Context context, final boolean desiredState) {
|
protected void requestStateChange(Context context, final boolean desiredState) {
|
||||||
if (sLocalBluetoothAdapter == null) {
|
if (sBluetoothAdapter == null) {
|
||||||
Log.d(TAG, "No LocalBluetoothManager");
|
Log.d(TAG, "No BluetoothAdapter");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Actually request the Bluetooth change and persistent
|
// Actually request the Bluetooth change and persistent
|
||||||
@@ -476,7 +469,11 @@ public class SettingsAppWidgetProvider extends AppWidgetProvider {
|
|||||||
new AsyncTask<Void, Void, Void>() {
|
new AsyncTask<Void, Void, Void>() {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... args) {
|
protected Void doInBackground(Void... args) {
|
||||||
sLocalBluetoothAdapter.setBluetoothEnabled(desiredState);
|
if (desiredState) {
|
||||||
|
sBluetoothAdapter.enable();
|
||||||
|
} else {
|
||||||
|
sBluetoothAdapter.disable();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}.execute();
|
}.execute();
|
||||||
|
@@ -17,41 +17,41 @@ package com.android.settings.bluetooth;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.anyInt;
|
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.never;
|
|
||||||
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 android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||||
public class AlwaysDiscoverableTest {
|
public class AlwaysDiscoverableTest {
|
||||||
|
|
||||||
@Mock
|
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
private AlwaysDiscoverable mAlwaysDiscoverable;
|
private AlwaysDiscoverable mAlwaysDiscoverable;
|
||||||
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
|
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mAlwaysDiscoverable = new AlwaysDiscoverable(mContext, mLocalAdapter);
|
mAlwaysDiscoverable = new AlwaysDiscoverable(mContext);
|
||||||
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -76,14 +76,15 @@ public class AlwaysDiscoverableTest {
|
|||||||
public void stopWithoutStart() {
|
public void stopWithoutStart() {
|
||||||
mAlwaysDiscoverable.stop();
|
mAlwaysDiscoverable.stop();
|
||||||
// expect no crash
|
// expect no crash
|
||||||
verify(mLocalAdapter, never()).setScanMode(anyInt());
|
mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startSetsModeAndRegistersReceiver() {
|
public void startSetsModeAndRegistersReceiver() {
|
||||||
when(mLocalAdapter.getScanMode()).thenReturn(BluetoothAdapter.SCAN_MODE_NONE);
|
mShadowBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_NONE);
|
||||||
mAlwaysDiscoverable.start();
|
mAlwaysDiscoverable.start();
|
||||||
verify(mLocalAdapter).setScanMode(eq(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
|
assertThat(mBluetoothAdapter.getScanMode())
|
||||||
|
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
verify(mContext).registerReceiver(eq(mAlwaysDiscoverable), any());
|
verify(mContext).registerReceiver(eq(mAlwaysDiscoverable), any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,18 +98,18 @@ public class AlwaysDiscoverableTest {
|
|||||||
@Test
|
@Test
|
||||||
public void resetsToDiscoverableModeWhenScanModeChanges() {
|
public void resetsToDiscoverableModeWhenScanModeChanges() {
|
||||||
mAlwaysDiscoverable.start();
|
mAlwaysDiscoverable.start();
|
||||||
verify(mLocalAdapter, times(1))
|
assertThat(mBluetoothAdapter.getScanMode())
|
||||||
.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
|
|
||||||
sendScanModeChangedIntent(BluetoothAdapter.SCAN_MODE_CONNECTABLE,
|
sendScanModeChangedIntent(BluetoothAdapter.SCAN_MODE_CONNECTABLE,
|
||||||
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
|
|
||||||
verify(mLocalAdapter, times(2))
|
assertThat(mBluetoothAdapter.getScanMode())
|
||||||
.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendScanModeChangedIntent(int newMode, int previousMode) {
|
private void sendScanModeChangedIntent(int newMode, int previousMode) {
|
||||||
when(mLocalAdapter.getScanMode()).thenReturn(newMode);
|
mShadowBluetoothAdapter.setScanMode(newMode);
|
||||||
Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
|
Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
|
||||||
intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, newMode);
|
intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, newMode);
|
||||||
intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, previousMode);
|
intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, previousMode);
|
||||||
|
@@ -24,22 +24,25 @@ import static org.mockito.Mockito.spy;
|
|||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
|
|
||||||
|
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
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 androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||||
public class BluetoothDeviceNamePreferenceControllerTest {
|
public class BluetoothDeviceNamePreferenceControllerTest {
|
||||||
|
|
||||||
private static final String DEVICE_NAME = "Nightshade";
|
private static final String DEVICE_NAME = "Nightshade";
|
||||||
@@ -48,8 +51,6 @@ public class BluetoothDeviceNamePreferenceControllerTest {
|
|||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
@Mock
|
@Mock
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
|
||||||
@Mock
|
|
||||||
private PreferenceScreen mPreferenceScreen;
|
private PreferenceScreen mPreferenceScreen;
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
|
|
||||||
@@ -64,8 +65,7 @@ public class BluetoothDeviceNamePreferenceControllerTest {
|
|||||||
when(mPreferenceScreen.getContext()).thenReturn(mContext);
|
when(mPreferenceScreen.getContext()).thenReturn(mContext);
|
||||||
mPreference = new Preference(mContext);
|
mPreference = new Preference(mContext);
|
||||||
mPreference.setKey(KEY_DEVICE_NAME);
|
mPreference.setKey(KEY_DEVICE_NAME);
|
||||||
mController = spy(new BluetoothDeviceNamePreferenceController(mContext, mLocalAdapter,
|
mController = spy(new BluetoothDeviceNamePreferenceController(mContext, KEY_DEVICE_NAME));
|
||||||
KEY_DEVICE_NAME));
|
|
||||||
doReturn(DEVICE_NAME).when(mController).getDeviceName();
|
doReturn(DEVICE_NAME).when(mController).getDeviceName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -24,11 +24,11 @@ import static org.mockito.Mockito.spy;
|
|||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -41,15 +41,16 @@ import androidx.fragment.app.Fragment;
|
|||||||
import androidx.fragment.app.FragmentTransaction;
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||||
public class BluetoothDeviceRenamePreferenceControllerTest {
|
public class BluetoothDeviceRenamePreferenceControllerTest {
|
||||||
|
|
||||||
private static final String DEVICE_NAME = "Nightshade";
|
private static final String DEVICE_NAME = "Nightshade";
|
||||||
private static final String PREF_KEY = "bt_rename_devices";
|
private static final String PREF_KEY = "bt_rename_devices";
|
||||||
|
|
||||||
@Mock
|
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private Fragment mFragment;
|
private Fragment mFragment;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -59,6 +60,8 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
|||||||
private Context mContext;
|
private Context mContext;
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
private BluetoothDeviceRenamePreferenceController mController;
|
private BluetoothDeviceRenamePreferenceController mController;
|
||||||
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
|
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -67,9 +70,10 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
|||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mPreference = new Preference(mContext);
|
mPreference = new Preference(mContext);
|
||||||
mPreference.setKey(PREF_KEY);
|
mPreference.setKey(PREF_KEY);
|
||||||
|
mBluetoothAdapter = ShadowBluetoothAdapter.getDefaultAdapter();
|
||||||
|
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||||
|
|
||||||
mController = spy(new BluetoothDeviceRenamePreferenceController(mContext, mLocalAdapter,
|
mController = spy(new BluetoothDeviceRenamePreferenceController(mContext, PREF_KEY));
|
||||||
PREF_KEY));
|
|
||||||
mController.setFragment(mFragment);
|
mController.setFragment(mFragment);
|
||||||
doReturn(DEVICE_NAME).when(mController).getDeviceName();
|
doReturn(DEVICE_NAME).when(mController).getDeviceName();
|
||||||
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
||||||
@@ -102,7 +106,7 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updatePreferenceState_whenBTisOnPreferenceShouldBeVisible() {
|
public void updatePreferenceState_whenBTisOnPreferenceShouldBeVisible() {
|
||||||
when(mLocalAdapter.isEnabled()).thenReturn(true);
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
|
|
||||||
mController.updatePreferenceState(mPreference);
|
mController.updatePreferenceState(mPreference);
|
||||||
|
|
||||||
@@ -111,7 +115,7 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updatePreferenceState_whenBTisOffPreferenceShouldBeHide() {
|
public void updatePreferenceState_whenBTisOffPreferenceShouldBeHide() {
|
||||||
when(mLocalAdapter.isEnabled()).thenReturn(false);
|
mShadowBluetoothAdapter.setEnabled(false);
|
||||||
|
|
||||||
mController.updatePreferenceState(mPreference);
|
mController.updatePreferenceState(mPreference);
|
||||||
|
|
||||||
|
@@ -35,13 +35,12 @@ import android.view.View;
|
|||||||
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||||
import com.android.settings.widget.SwitchBar;
|
import com.android.settings.widget.SwitchBar;
|
||||||
import com.android.settings.widget.SwitchBarController;
|
import com.android.settings.widget.SwitchBarController;
|
||||||
import com.android.settings.widget.SwitchWidgetController;
|
import com.android.settings.widget.SwitchWidgetController;
|
||||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||||
import com.android.settingslib.RestrictedSwitchPreference;
|
import com.android.settingslib.RestrictedSwitchPreference;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -53,11 +52,12 @@ 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 androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(shadows = SettingsShadowResources.SettingsShadowTheme.class)
|
@Config(shadows = {SettingsShadowResources.SettingsShadowTheme.class, ShadowBluetoothAdapter.class})
|
||||||
public class BluetoothEnablerTest {
|
public class BluetoothEnablerTest {
|
||||||
|
|
||||||
private static EnforcedAdmin sFakeEnforcedAdmin;
|
private static EnforcedAdmin sFakeEnforcedAdmin;
|
||||||
@@ -74,21 +74,17 @@ public class BluetoothEnablerTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private RestrictionUtils mRestrictionUtils;
|
private RestrictionUtils mRestrictionUtils;
|
||||||
@Mock
|
@Mock
|
||||||
private LocalBluetoothManager mBluetoothManager;
|
|
||||||
@Mock
|
|
||||||
private LocalBluetoothAdapter mBluetoothAdapter;
|
|
||||||
@Mock
|
|
||||||
private SwitchWidgetController.OnSwitchChangeListener mCallback;
|
private SwitchWidgetController.OnSwitchChangeListener mCallback;
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private SwitchWidgetController mSwitchController;
|
private SwitchWidgetController mSwitchController;
|
||||||
private BluetoothEnabler mBluetoothEnabler;
|
private BluetoothEnabler mBluetoothEnabler;
|
||||||
|
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBluetoothAdapter);
|
|
||||||
|
|
||||||
mRestrictedSwitchPreference = new RestrictedSwitchPreference(mContext);
|
mRestrictedSwitchPreference = new RestrictedSwitchPreference(mContext);
|
||||||
mSwitchController = spy(new SwitchBarController(new SwitchBar(mContext)));
|
mSwitchController = spy(new SwitchBarController(new SwitchBar(mContext)));
|
||||||
@@ -96,12 +92,12 @@ public class BluetoothEnablerTest {
|
|||||||
mContext,
|
mContext,
|
||||||
mSwitchController,
|
mSwitchController,
|
||||||
mMetricsFeatureProvider,
|
mMetricsFeatureProvider,
|
||||||
mBluetoothManager,
|
|
||||||
123,
|
123,
|
||||||
mRestrictionUtils);
|
mRestrictionUtils);
|
||||||
mHolder = PreferenceViewHolder.createInstanceForTests(mock(View.class));
|
mHolder = PreferenceViewHolder.createInstanceForTests(mock(View.class));
|
||||||
mRestrictedSwitchPreference.onBindViewHolder(mHolder);
|
mRestrictedSwitchPreference.onBindViewHolder(mHolder);
|
||||||
mBluetoothEnabler.setToggleCallback(mCallback);
|
mBluetoothEnabler.setToggleCallback(mCallback);
|
||||||
|
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -191,7 +187,7 @@ public class BluetoothEnablerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startWithBluetoothOff_switchIsOff() {
|
public void startWithBluetoothOff_switchIsOff() {
|
||||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
|
mShadowBluetoothAdapter.setState(BluetoothAdapter.STATE_OFF);
|
||||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
verify(mSwitchController, never()).setChecked(true);
|
verify(mSwitchController, never()).setChecked(true);
|
||||||
@@ -199,7 +195,7 @@ public class BluetoothEnablerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startWithBluetoothOn_switchIsOn() {
|
public void startWithBluetoothOn_switchIsOn() {
|
||||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
mShadowBluetoothAdapter.setState(BluetoothAdapter.STATE_ON);
|
||||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
verify(mSwitchController, never()).setChecked(false);
|
verify(mSwitchController, never()).setChecked(false);
|
||||||
@@ -211,7 +207,7 @@ public class BluetoothEnablerTest {
|
|||||||
// Start up with bluetooth turned on. The switch should get turned on.
|
// Start up with bluetooth turned on. The switch should get turned on.
|
||||||
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||||
when(mContext.registerReceiver(captor.capture(), any(IntentFilter.class))).thenReturn(null);
|
when(mContext.registerReceiver(captor.capture(), any(IntentFilter.class))).thenReturn(null);
|
||||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
mShadowBluetoothAdapter.setState(BluetoothAdapter.STATE_ON);
|
||||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
verify(mSwitchController, never()).setChecked(false);
|
verify(mSwitchController, never()).setChecked(false);
|
||||||
@@ -235,7 +231,7 @@ public class BluetoothEnablerTest {
|
|||||||
// Start up with bluetooth turned on. The switch should be left off.
|
// Start up with bluetooth turned on. The switch should be left off.
|
||||||
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||||
when(mContext.registerReceiver(captor.capture(), any(IntentFilter.class))).thenReturn(null);
|
when(mContext.registerReceiver(captor.capture(), any(IntentFilter.class))).thenReturn(null);
|
||||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
|
mShadowBluetoothAdapter.setState(BluetoothAdapter.STATE_OFF);
|
||||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
|
@@ -33,8 +33,8 @@ import android.content.res.Resources;
|
|||||||
|
|
||||||
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.ShadowBluetoothAdapter;
|
||||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
import com.android.settingslib.widget.FooterPreference;
|
import com.android.settingslib.widget.FooterPreference;
|
||||||
|
|
||||||
@@ -45,16 +45,17 @@ import org.mockito.Answers;
|
|||||||
import org.mockito.Mock;
|
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.shadow.api.Shadow;
|
||||||
|
|
||||||
import androidx.preference.PreferenceGroup;
|
import androidx.preference.PreferenceGroup;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||||
public class BluetoothPairingDetailTest {
|
public class BluetoothPairingDetailTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private Resources mResource;
|
private Resources mResource;
|
||||||
@Mock
|
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private LocalBluetoothManager mLocalManager;
|
private LocalBluetoothManager mLocalManager;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -63,6 +64,8 @@ public class BluetoothPairingDetailTest {
|
|||||||
private Context mContext;
|
private Context mContext;
|
||||||
private BluetoothProgressCategory mAvailableDevicesCategory;
|
private BluetoothProgressCategory mAvailableDevicesCategory;
|
||||||
private FooterPreference mFooterPreference;
|
private FooterPreference mFooterPreference;
|
||||||
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
|
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -75,11 +78,13 @@ public class BluetoothPairingDetailTest {
|
|||||||
|
|
||||||
mAvailableDevicesCategory = spy(new BluetoothProgressCategory(mContext));
|
mAvailableDevicesCategory = spy(new BluetoothProgressCategory(mContext));
|
||||||
mFooterPreference = new FooterPreference(mContext);
|
mFooterPreference = new FooterPreference(mContext);
|
||||||
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||||
|
|
||||||
mFragment.mLocalAdapter = mLocalAdapter;
|
mFragment.mBluetoothAdapter = mBluetoothAdapter;
|
||||||
mFragment.mLocalManager = mLocalManager;
|
mFragment.mLocalManager = mLocalManager;
|
||||||
mFragment.mDeviceListGroup = mPreferenceGroup;
|
mFragment.mDeviceListGroup = mPreferenceGroup;
|
||||||
mFragment.mAlwaysDiscoverable = new AlwaysDiscoverable(mContext, mLocalAdapter);
|
mFragment.mAlwaysDiscoverable = new AlwaysDiscoverable(mContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -102,7 +107,7 @@ public class BluetoothPairingDetailTest {
|
|||||||
|
|
||||||
mFragment.enableScanning();
|
mFragment.enableScanning();
|
||||||
|
|
||||||
verify(mLocalAdapter).startScanning(true);
|
verify(mFragment).startScanning();
|
||||||
verify(mAvailableDevicesCategory).removeAll();
|
verify(mAvailableDevicesCategory).removeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +122,8 @@ public class BluetoothPairingDetailTest {
|
|||||||
verify(mFragment).addDeviceCategory(mAvailableDevicesCategory,
|
verify(mFragment).addDeviceCategory(mAvailableDevicesCategory,
|
||||||
R.string.bluetooth_preference_found_media_devices,
|
R.string.bluetooth_preference_found_media_devices,
|
||||||
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, false);
|
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, false);
|
||||||
verify(mLocalAdapter).setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
assertThat(mBluetoothAdapter.getScanMode())
|
||||||
|
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -129,16 +135,16 @@ public class BluetoothPairingDetailTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateBluetooth_bluetoothOff_turnOnBluetooth() {
|
public void testUpdateBluetooth_bluetoothOff_turnOnBluetooth() {
|
||||||
doReturn(false).when(mLocalAdapter).isEnabled();
|
mShadowBluetoothAdapter.setEnabled(false);
|
||||||
|
|
||||||
mFragment.updateBluetooth();
|
mFragment.updateBluetooth();
|
||||||
|
|
||||||
verify(mLocalAdapter).enable();
|
assertThat(mBluetoothAdapter.isEnabled()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateBluetooth_bluetoothOn_updateState() {
|
public void testUpdateBluetooth_bluetoothOn_updateState() {
|
||||||
doReturn(true).when(mLocalAdapter).isEnabled();
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
doNothing().when(mFragment).updateContent(anyInt());
|
doNothing().when(mFragment).updateContent(anyInt());
|
||||||
|
|
||||||
mFragment.updateBluetooth();
|
mFragment.updateBluetooth();
|
||||||
@@ -157,27 +163,27 @@ public class BluetoothPairingDetailTest {
|
|||||||
mFragment.updateContent(BluetoothAdapter.STATE_ON);
|
mFragment.updateContent(BluetoothAdapter.STATE_ON);
|
||||||
verify(mFragment).enableScanning();
|
verify(mFragment).enableScanning();
|
||||||
assertThat(mAvailableDevicesCategory.getPreferenceCount()).isEqualTo(0);
|
assertThat(mAvailableDevicesCategory.getPreferenceCount()).isEqualTo(0);
|
||||||
verify(mLocalAdapter).startScanning(true);
|
verify(mFragment).startScanning();
|
||||||
|
|
||||||
// Subsequent scan started event will not trigger start/stop nor list clear
|
// Subsequent scan started event will not trigger start/stop nor list clear
|
||||||
mFragment.onScanningStateChanged(true);
|
mFragment.onScanningStateChanged(true);
|
||||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
verify(mFragment, times(1)).startScanning();
|
||||||
verify(mAvailableDevicesCategory, times(1)).setProgress(true);
|
verify(mAvailableDevicesCategory, times(1)).setProgress(true);
|
||||||
|
|
||||||
// Subsequent scan finished event will trigger scan start without list clean
|
// Subsequent scan finished event will trigger scan start without list clean
|
||||||
mFragment.onScanningStateChanged(false);
|
mFragment.onScanningStateChanged(false);
|
||||||
verify(mLocalAdapter, times(2)).startScanning(true);
|
verify(mFragment, times(2)).startScanning();
|
||||||
verify(mAvailableDevicesCategory, times(2)).setProgress(true);
|
verify(mAvailableDevicesCategory, times(2)).setProgress(true);
|
||||||
|
|
||||||
// Subsequent scan started event will not trigger any change
|
// Subsequent scan started event will not trigger any change
|
||||||
mFragment.onScanningStateChanged(true);
|
mFragment.onScanningStateChanged(true);
|
||||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
verify(mFragment, times(2)).startScanning();
|
||||||
verify(mAvailableDevicesCategory, times(3)).setProgress(true);
|
verify(mAvailableDevicesCategory, times(3)).setProgress(true);
|
||||||
verify(mLocalAdapter, never()).stopScanning();
|
verify(mFragment, never()).stopScanning();
|
||||||
|
|
||||||
// Disable scanning will trigger scan stop
|
// Disable scanning will trigger scan stop
|
||||||
mFragment.disableScanning();
|
mFragment.disableScanning();
|
||||||
verify(mLocalAdapter, times(1)).stopScanning();
|
verify(mFragment, times(1)).stopScanning();
|
||||||
|
|
||||||
// Subsequent scan start event will not trigger any change besides progress circle
|
// Subsequent scan start event will not trigger any change besides progress circle
|
||||||
mFragment.onScanningStateChanged(true);
|
mFragment.onScanningStateChanged(true);
|
||||||
@@ -187,8 +193,8 @@ public class BluetoothPairingDetailTest {
|
|||||||
// progress circle from spinning
|
// progress circle from spinning
|
||||||
mFragment.onScanningStateChanged(false);
|
mFragment.onScanningStateChanged(false);
|
||||||
verify(mAvailableDevicesCategory, times(1)).setProgress(false);
|
verify(mAvailableDevicesCategory, times(1)).setProgress(false);
|
||||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
verify(mFragment, times(2)).startScanning();
|
||||||
verify(mLocalAdapter, times(1)).stopScanning();
|
verify(mFragment, times(1)).stopScanning();
|
||||||
|
|
||||||
// Verify that clean up only happen once at initialization
|
// Verify that clean up only happen once at initialization
|
||||||
verify(mAvailableDevicesCategory, times(1)).removeAll();
|
verify(mAvailableDevicesCategory, times(1)).removeAll();
|
||||||
|
@@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.anyInt;
|
|||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
@@ -29,10 +30,7 @@ import android.content.res.Resources;
|
|||||||
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.SliceTester;
|
import com.android.settings.testutils.SliceTester;
|
||||||
import com.android.settings.testutils.shadow.ShadowLocalBluetoothAdapter;
|
|
||||||
import com.android.settings.testutils.shadow.ShadowLocalBluetoothProfileManager;
|
import com.android.settings.testutils.shadow.ShadowLocalBluetoothProfileManager;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -52,7 +50,7 @@ import androidx.slice.core.SliceAction;
|
|||||||
import androidx.slice.widget.SliceLiveData;
|
import androidx.slice.widget.SliceLiveData;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(shadows = {ShadowLocalBluetoothAdapter.class, ShadowLocalBluetoothProfileManager.class})
|
@Config(shadows = {ShadowLocalBluetoothProfileManager.class})
|
||||||
public class BluetoothSliceBuilderTest {
|
public class BluetoothSliceBuilderTest {
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
@@ -90,11 +88,10 @@ public class BluetoothSliceBuilderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void handleUriChange_updatesBluetooth() {
|
public void handleUriChange_updatesBluetooth() {
|
||||||
final LocalBluetoothAdapter adapter = LocalBluetoothManager.getInstance(mContext,
|
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
null /* callback */).getBluetoothAdapter();
|
|
||||||
final Intent intent = new Intent();
|
final Intent intent = new Intent();
|
||||||
intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true);
|
intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true);
|
||||||
adapter.setBluetoothEnabled(false /* enabled */);
|
adapter.disable()/* enabled */;
|
||||||
|
|
||||||
BluetoothSliceBuilder.handleUriChange(mContext, intent);
|
BluetoothSliceBuilder.handleUriChange(mContext, intent);
|
||||||
|
|
||||||
|
@@ -23,7 +23,6 @@ import static org.mockito.Mockito.doCallRealMethod;
|
|||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.times;
|
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 android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
@@ -31,8 +30,8 @@ import android.content.Context;
|
|||||||
|
|
||||||
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.ShadowBluetoothAdapter;
|
||||||
import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
|
import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -42,11 +41,14 @@ import org.mockito.Answers;
|
|||||||
import org.mockito.Mock;
|
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.shadow.api.Shadow;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||||
public class BluetoothSummaryUpdaterTest {
|
public class BluetoothSummaryUpdaterTest {
|
||||||
|
|
||||||
private static final String DEVICE_NAME = "Nightshade";
|
private static final String DEVICE_NAME = "Nightshade";
|
||||||
@@ -56,8 +58,6 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private LocalBluetoothManager mBluetoothManager;
|
private LocalBluetoothManager mBluetoothManager;
|
||||||
@Mock
|
@Mock
|
||||||
private LocalBluetoothAdapter mBtAdapter;
|
|
||||||
@Mock
|
|
||||||
private BluetoothDevice mConnectedDevice;
|
private BluetoothDevice mConnectedDevice;
|
||||||
@Mock
|
@Mock
|
||||||
private BluetoothDevice mConnectedKeyBoardDevice;
|
private BluetoothDevice mConnectedKeyBoardDevice;
|
||||||
@@ -72,16 +72,13 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
private final boolean[] mDeviceConnected = {false, false};
|
private final boolean[] mDeviceConnected = {false, false};
|
||||||
private final Set<BluetoothDevice> mBondedDevices = new HashSet<>();
|
private final Set<BluetoothDevice> mBondedDevices = new HashSet<>();
|
||||||
private BluetoothSummaryUpdater mSummaryUpdater;
|
private BluetoothSummaryUpdater mSummaryUpdater;
|
||||||
|
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = RuntimeEnvironment.application.getApplicationContext();
|
mContext = RuntimeEnvironment.application.getApplicationContext();
|
||||||
doCallRealMethod().when(mListener).onSummaryChanged(anyString());
|
doCallRealMethod().when(mListener).onSummaryChanged(anyString());
|
||||||
// Setup mock adapter
|
|
||||||
when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBtAdapter);
|
|
||||||
doAnswer(invocation -> mAdapterEnabled[0]).when(mBtAdapter).isEnabled();
|
|
||||||
doAnswer(invocation -> mAdapterConnectionState[0]).when(mBtAdapter).getConnectionState();
|
|
||||||
mSummaryUpdater = new BluetoothSummaryUpdater(mContext, mListener, mBluetoothManager);
|
mSummaryUpdater = new BluetoothSummaryUpdater(mContext, mListener, mBluetoothManager);
|
||||||
// Setup first device
|
// Setup first device
|
||||||
doReturn(DEVICE_NAME).when(mConnectedDevice).getName();
|
doReturn(DEVICE_NAME).when(mConnectedDevice).getName();
|
||||||
@@ -89,7 +86,11 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
// Setup second device
|
// Setup second device
|
||||||
doReturn(DEVICE_KEYBOARD_NAME).when(mConnectedKeyBoardDevice).getName();
|
doReturn(DEVICE_KEYBOARD_NAME).when(mConnectedKeyBoardDevice).getName();
|
||||||
doAnswer(invocation -> mDeviceConnected[1]).when(mConnectedKeyBoardDevice).isConnected();
|
doAnswer(invocation -> mDeviceConnected[1]).when(mConnectedKeyBoardDevice).isConnected();
|
||||||
doReturn(mBondedDevices).when(mBtAdapter).getBondedDevices();
|
// Setup BluetoothAdapter
|
||||||
|
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||||
|
mShadowBluetoothAdapter.setEnabled(mAdapterEnabled[0]);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
|
mShadowBluetoothAdapter.setConnectionState(mAdapterConnectionState[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -108,9 +109,10 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void register_true_shouldSendSummaryChange() {
|
public void register_true_shouldSendSummaryChange() {
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
mDeviceConnected[0] = true;
|
mDeviceConnected[0] = true;
|
||||||
|
|
||||||
mSummaryUpdater.register(true);
|
mSummaryUpdater.register(true);
|
||||||
@@ -122,8 +124,9 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void onBluetoothStateChanged_btDisabled_shouldSendDisabledSummary() {
|
public void onBluetoothStateChanged_btDisabled_shouldSendDisabledSummary() {
|
||||||
// These states should be ignored
|
// These states should be ignored
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
mDeviceConnected[0] = true;
|
mDeviceConnected[0] = true;
|
||||||
|
|
||||||
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
|
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
|
||||||
@@ -133,9 +136,10 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onBluetoothStateChanged_btEnabled_connected_shouldSendConnectedSummary() {
|
public void onBluetoothStateChanged_btEnabled_connected_shouldSendConnectedSummary() {
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
mDeviceConnected[0] = true;
|
mDeviceConnected[0] = true;
|
||||||
|
|
||||||
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
|
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
|
||||||
@@ -146,9 +150,10 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onBluetoothStateChanged_btEnabled_connectedMisMatch_shouldSendNotConnected() {
|
public void onBluetoothStateChanged_btEnabled_connectedMisMatch_shouldSendNotConnected() {
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
// State mismatch
|
// State mismatch
|
||||||
mDeviceConnected[0] = false;
|
mDeviceConnected[0] = false;
|
||||||
|
|
||||||
@@ -159,9 +164,10 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onBluetoothStateChanged_btEnabled_notConnected_shouldSendDisconnectedMessage() {
|
public void onBluetoothStateChanged_btEnabled_notConnected_shouldSendDisconnectedMessage() {
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_DISCONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_DISCONNECTED);
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
// This should be ignored
|
// This should be ignored
|
||||||
mDeviceConnected[0] = true;
|
mDeviceConnected[0] = true;
|
||||||
|
|
||||||
@@ -172,22 +178,23 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onBluetoothStateChanged_ConnectedDisabledEnabled_shouldSendDisconnectedSummary() {
|
public void onBluetoothStateChanged_ConnectedDisabledEnabled_shouldSendDisconnectedSummary() {
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_DISCONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_DISCONNECTED);
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
mDeviceConnected[0] = false;
|
mDeviceConnected[0] = false;
|
||||||
|
|
||||||
mSummaryUpdater.register(true);
|
mSummaryUpdater.register(true);
|
||||||
verify(mListener).onSummaryChanged(mContext.getString(R.string.disconnected));
|
verify(mListener).onSummaryChanged(mContext.getString(R.string.disconnected));
|
||||||
|
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||||
mDeviceConnected[0] = true;
|
mDeviceConnected[0] = true;
|
||||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||||
BluetoothAdapter.STATE_CONNECTED);
|
BluetoothAdapter.STATE_CONNECTED);
|
||||||
verify(mListener).onSummaryChanged(
|
verify(mListener).onSummaryChanged(
|
||||||
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME));
|
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME));
|
||||||
|
|
||||||
mAdapterEnabled[0] = false;
|
mShadowBluetoothAdapter.setEnabled(false);
|
||||||
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
|
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
|
||||||
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
|
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
|
||||||
|
|
||||||
@@ -196,7 +203,7 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
// There should still be only one invocation of disabled message
|
// There should still be only one invocation of disabled message
|
||||||
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
|
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
|
||||||
|
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mDeviceConnected[0] = false;
|
mDeviceConnected[0] = false;
|
||||||
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
|
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
|
||||||
verify(mListener, times(2)).onSummaryChanged(mContext.getString(R.string.disconnected));
|
verify(mListener, times(2)).onSummaryChanged(mContext.getString(R.string.disconnected));
|
||||||
@@ -205,9 +212,10 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onConnectionStateChanged_connected_shouldSendConnectedMessage() {
|
public void onConnectionStateChanged_connected_shouldSendConnectedMessage() {
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
mDeviceConnected[0] = true;
|
mDeviceConnected[0] = true;
|
||||||
|
|
||||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||||
@@ -219,9 +227,10 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onConnectionStateChanged_inconsistentState_shouldSendDisconnectedMessage() {
|
public void onConnectionStateChanged_inconsistentState_shouldSendDisconnectedMessage() {
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_DISCONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_DISCONNECTED);
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
mDeviceConnected[0] = false;
|
mDeviceConnected[0] = false;
|
||||||
|
|
||||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||||
@@ -232,8 +241,8 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onConnectionStateChanged_noBondedDevice_shouldSendDisconnectedMessage() {
|
public void onConnectionStateChanged_noBondedDevice_shouldSendDisconnectedMessage() {
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||||
|
|
||||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||||
BluetoothAdapter.STATE_CONNECTED);
|
BluetoothAdapter.STATE_CONNECTED);
|
||||||
@@ -244,8 +253,8 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void onConnectionStateChanged_connecting_shouldSendConnectingMessage() {
|
public void onConnectionStateChanged_connecting_shouldSendConnectingMessage() {
|
||||||
// No need for bonded devices
|
// No need for bonded devices
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTING;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTING);
|
||||||
|
|
||||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||||
BluetoothAdapter.STATE_CONNECTING);
|
BluetoothAdapter.STATE_CONNECTING);
|
||||||
@@ -256,8 +265,8 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void onConnectionStateChanged_disconnecting_shouldSendDisconnectingMessage() {
|
public void onConnectionStateChanged_disconnecting_shouldSendDisconnectingMessage() {
|
||||||
// No need for bonded devices
|
// No need for bonded devices
|
||||||
mAdapterEnabled[0] = true;
|
mShadowBluetoothAdapter.setEnabled(true);
|
||||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_DISCONNECTING;
|
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_DISCONNECTING);
|
||||||
|
|
||||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||||
BluetoothAdapter.STATE_DISCONNECTING);
|
BluetoothAdapter.STATE_DISCONNECTING);
|
||||||
@@ -268,6 +277,7 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void getConnectedDeviceSummary_hasConnectedDevice_returnOneDeviceSummary() {
|
public void getConnectedDeviceSummary_hasConnectedDevice_returnOneDeviceSummary() {
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
mDeviceConnected[0] = true;
|
mDeviceConnected[0] = true;
|
||||||
final String expectedSummary =
|
final String expectedSummary =
|
||||||
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME);
|
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME);
|
||||||
@@ -279,6 +289,8 @@ public class BluetoothSummaryUpdaterTest {
|
|||||||
public void getConnectedDeviceSummary_multipleDevices_returnMultipleDevicesSummary() {
|
public void getConnectedDeviceSummary_multipleDevices_returnMultipleDevicesSummary() {
|
||||||
mBondedDevices.add(mConnectedDevice);
|
mBondedDevices.add(mConnectedDevice);
|
||||||
mBondedDevices.add(mConnectedKeyBoardDevice);
|
mBondedDevices.add(mConnectedKeyBoardDevice);
|
||||||
|
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||||
|
|
||||||
mDeviceConnected[0] = true;
|
mDeviceConnected[0] = true;
|
||||||
mDeviceConnected[1] = true;
|
mDeviceConnected[1] = true;
|
||||||
final String expectedSummary =
|
final String expectedSummary =
|
||||||
|
@@ -43,8 +43,6 @@ public class BluetoothSwitchPreferenceControllerTest {
|
|||||||
|
|
||||||
public static final String BLUETOOTH_INFO_STRING = "When Bluetooth is turned on, your device"
|
public static final String BLUETOOTH_INFO_STRING = "When Bluetooth is turned on, your device"
|
||||||
+ " can communicate with other nearby Bluetooth devices.";
|
+ " can communicate with other nearby Bluetooth devices.";
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
|
||||||
private LocalBluetoothManager mBluetoothManager;
|
|
||||||
@Mock
|
@Mock
|
||||||
private RestrictionUtils mRestrictionUtils;
|
private RestrictionUtils mRestrictionUtils;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -62,7 +60,7 @@ public class BluetoothSwitchPreferenceControllerTest {
|
|||||||
FakeFeatureFactory.setupForTest();
|
FakeFeatureFactory.setupForTest();
|
||||||
|
|
||||||
mController =
|
mController =
|
||||||
new BluetoothSwitchPreferenceController(mContext, mBluetoothManager, mRestrictionUtils,
|
new BluetoothSwitchPreferenceController(mContext, mRestrictionUtils,
|
||||||
mSwitchController, mFooterPreference);
|
mSwitchController, mFooterPreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,20 +18,20 @@ package com.android.settings.bluetooth;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.anyBoolean;
|
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
|
||||||
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.ShadowBluetoothAdapter;
|
||||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -40,12 +40,14 @@ import org.junit.runner.RunWith;
|
|||||||
import org.mockito.Mock;
|
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 java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||||
public class DeviceListPreferenceFragmentTest {
|
public class DeviceListPreferenceFragmentTest {
|
||||||
|
|
||||||
private static final String FOOTAGE_MAC_STRING = "Bluetooth mac: xxxx";
|
private static final String FOOTAGE_MAC_STRING = "Bluetooth mac: xxxx";
|
||||||
@@ -54,8 +56,7 @@ public class DeviceListPreferenceFragmentTest {
|
|||||||
private Resources mResource;
|
private Resources mResource;
|
||||||
@Mock
|
@Mock
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
@Mock
|
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
|
||||||
private TestFragment mFragment;
|
private TestFragment mFragment;
|
||||||
private Preference mMyDevicePreference;
|
private Preference mMyDevicePreference;
|
||||||
|
|
||||||
@@ -66,7 +67,7 @@ public class DeviceListPreferenceFragmentTest {
|
|||||||
mFragment = spy(new TestFragment());
|
mFragment = spy(new TestFragment());
|
||||||
doReturn(mContext).when(mFragment).getContext();
|
doReturn(mContext).when(mFragment).getContext();
|
||||||
doReturn(mResource).when(mFragment).getResources();
|
doReturn(mResource).when(mFragment).getResources();
|
||||||
mFragment.mLocalAdapter = mLocalAdapter;
|
mFragment.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
|
||||||
mMyDevicePreference = new Preference(RuntimeEnvironment.application);
|
mMyDevicePreference = new Preference(RuntimeEnvironment.application);
|
||||||
}
|
}
|
||||||
@@ -84,11 +85,11 @@ public class DeviceListPreferenceFragmentTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testEnableDisableScanning_testStateAfterEanbleDisable() {
|
public void testEnableDisableScanning_testStateAfterEanbleDisable() {
|
||||||
mFragment.enableScanning();
|
mFragment.enableScanning();
|
||||||
verify(mLocalAdapter).startScanning(true);
|
verify(mFragment).startScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isTrue();
|
assertThat(mFragment.mScanEnabled).isTrue();
|
||||||
|
|
||||||
mFragment.disableScanning();
|
mFragment.disableScanning();
|
||||||
verify(mLocalAdapter).stopScanning();
|
verify(mFragment).stopScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isFalse();
|
assertThat(mFragment.mScanEnabled).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,21 +97,21 @@ public class DeviceListPreferenceFragmentTest {
|
|||||||
public void testScanningStateChanged_testScanStarted() {
|
public void testScanningStateChanged_testScanStarted() {
|
||||||
mFragment.enableScanning();
|
mFragment.enableScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isTrue();
|
assertThat(mFragment.mScanEnabled).isTrue();
|
||||||
verify(mLocalAdapter).startScanning(true);
|
verify(mFragment).startScanning();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(true);
|
mFragment.onScanningStateChanged(true);
|
||||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
verify(mFragment, times(1)).startScanning();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testScanningStateChanged_testScanFinished() {
|
public void testScanningStateChanged_testScanFinished() {
|
||||||
// Could happen when last scanning not done while current scan gets enabled
|
// Could happen when last scanning not done while current scan gets enabled
|
||||||
mFragment.enableScanning();
|
mFragment.enableScanning();
|
||||||
verify(mLocalAdapter).startScanning(true);
|
verify(mFragment).startScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isTrue();
|
assertThat(mFragment.mScanEnabled).isTrue();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(false);
|
mFragment.onScanningStateChanged(false);
|
||||||
verify(mLocalAdapter, times(2)).startScanning(true);
|
verify(mFragment, times(2)).startScanning();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -118,53 +119,53 @@ public class DeviceListPreferenceFragmentTest {
|
|||||||
// Could happen when last scanning not done while current scan gets enabled
|
// Could happen when last scanning not done while current scan gets enabled
|
||||||
mFragment.enableScanning();
|
mFragment.enableScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isTrue();
|
assertThat(mFragment.mScanEnabled).isTrue();
|
||||||
verify(mLocalAdapter).startScanning(true);
|
verify(mFragment).startScanning();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(true);
|
mFragment.onScanningStateChanged(true);
|
||||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
verify(mFragment, times(1)).startScanning();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(false);
|
mFragment.onScanningStateChanged(false);
|
||||||
verify(mLocalAdapter, times(2)).startScanning(true);
|
verify(mFragment, times(2)).startScanning();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(true);
|
mFragment.onScanningStateChanged(true);
|
||||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
verify(mFragment, times(2)).startScanning();
|
||||||
|
|
||||||
mFragment.disableScanning();
|
mFragment.disableScanning();
|
||||||
verify(mLocalAdapter).stopScanning();
|
verify(mFragment).stopScanning();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(false);
|
mFragment.onScanningStateChanged(false);
|
||||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
verify(mFragment, times(2)).startScanning();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(true);
|
mFragment.onScanningStateChanged(true);
|
||||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
verify(mFragment, times(2)).startScanning();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testScanningStateChanged_testScanFinishedAfterDisable() {
|
public void testScanningStateChanged_testScanFinishedAfterDisable() {
|
||||||
mFragment.enableScanning();
|
mFragment.enableScanning();
|
||||||
verify(mLocalAdapter).startScanning(true);
|
verify(mFragment).startScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isTrue();
|
assertThat(mFragment.mScanEnabled).isTrue();
|
||||||
|
|
||||||
mFragment.disableScanning();
|
mFragment.disableScanning();
|
||||||
verify(mLocalAdapter).stopScanning();
|
verify(mFragment).stopScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isFalse();
|
assertThat(mFragment.mScanEnabled).isFalse();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(false);
|
mFragment.onScanningStateChanged(false);
|
||||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
verify(mFragment, times(1)).startScanning();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testScanningStateChanged_testScanStartedAfterDisable() {
|
public void testScanningStateChanged_testScanStartedAfterDisable() {
|
||||||
mFragment.enableScanning();
|
mFragment.enableScanning();
|
||||||
verify(mLocalAdapter).startScanning(true);
|
verify(mFragment).startScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isTrue();
|
assertThat(mFragment.mScanEnabled).isTrue();
|
||||||
|
|
||||||
mFragment.disableScanning();
|
mFragment.disableScanning();
|
||||||
verify(mLocalAdapter).stopScanning();
|
verify(mFragment).stopScanning();
|
||||||
assertThat(mFragment.mScanEnabled).isFalse();
|
assertThat(mFragment.mScanEnabled).isFalse();
|
||||||
|
|
||||||
mFragment.onScanningStateChanged(true);
|
mFragment.onScanningStateChanged(true);
|
||||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
verify(mFragment, times(1)).startScanning();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -30,7 +30,6 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
|
import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
|
||||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
import com.android.settingslib.testutils.FragmentTestUtils;
|
import com.android.settingslib.testutils.FragmentTestUtils;
|
||||||
|
|
||||||
@@ -50,10 +49,6 @@ import androidx.appcompat.app.AlertDialog;
|
|||||||
@Config(shadows = {ShadowAlertDialogCompat.class, SettingsShadowResourcesImpl.class})
|
@Config(shadows = {ShadowAlertDialogCompat.class, SettingsShadowResourcesImpl.class})
|
||||||
public class LocalDeviceNameDialogFragmentTest {
|
public class LocalDeviceNameDialogFragmentTest {
|
||||||
|
|
||||||
@Mock
|
|
||||||
private LocalBluetoothManager mManager;
|
|
||||||
@Mock
|
|
||||||
private LocalBluetoothAdapter mAdapter;
|
|
||||||
@Mock
|
@Mock
|
||||||
private InputMethodManager mInputMethodManager;
|
private InputMethodManager mInputMethodManager;
|
||||||
|
|
||||||
@@ -65,8 +60,6 @@ public class LocalDeviceNameDialogFragmentTest {
|
|||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
doReturn(mInputMethodManager).when(mContext).getSystemService(Context.INPUT_METHOD_SERVICE);
|
doReturn(mInputMethodManager).when(mContext).getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||||
ReflectionHelpers.setStaticField(LocalBluetoothManager.class, "sInstance", mManager);
|
|
||||||
when(mManager.getBluetoothAdapter()).thenReturn(mAdapter);
|
|
||||||
|
|
||||||
mFragment = spy(LocalDeviceNameDialogFragment.newInstance());
|
mFragment = spy(LocalDeviceNameDialogFragment.newInstance());
|
||||||
when(mFragment.getContext()).thenReturn(mContext);
|
when(mFragment.getContext()).thenReturn(mContext);
|
||||||
|
@@ -35,7 +35,6 @@ import com.android.settings.core.BasePreferenceController;
|
|||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||||
import com.android.settings.testutils.shadow.ShadowBluetoothPan;
|
import com.android.settings.testutils.shadow.ShadowBluetoothPan;
|
||||||
import com.android.settings.testutils.shadow.ShadowLocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.widget.FooterPreference;
|
import com.android.settingslib.widget.FooterPreference;
|
||||||
import com.android.settingslib.widget.FooterPreferenceMixinCompat;
|
import com.android.settingslib.widget.FooterPreferenceMixinCompat;
|
||||||
|
|
||||||
@@ -47,6 +46,7 @@ import org.mockito.MockitoAnnotations;
|
|||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.Shadows;
|
import org.robolectric.Shadows;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.shadows.ShadowApplication;
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -55,8 +55,7 @@ import java.util.List;
|
|||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(shadows = {ShadowBluetoothPan.class, ShadowBluetoothAdapter.class,
|
@Config(shadows = {ShadowBluetoothPan.class, ShadowBluetoothAdapter.class})
|
||||||
ShadowLocalBluetoothAdapter.class})
|
|
||||||
public class DiscoverableFooterPreferenceControllerTest {
|
public class DiscoverableFooterPreferenceControllerTest {
|
||||||
private static final String DEVICE_NAME = "device name";
|
private static final String DEVICE_NAME = "device name";
|
||||||
private static final String KEY = "discoverable_footer_preference";
|
private static final String KEY = "discoverable_footer_preference";
|
||||||
@@ -75,6 +74,7 @@ public class DiscoverableFooterPreferenceControllerTest {
|
|||||||
private DiscoverableFooterPreferenceController mDiscoverableFooterPreferenceController;
|
private DiscoverableFooterPreferenceController mDiscoverableFooterPreferenceController;
|
||||||
private BroadcastReceiver mBluetoothChangedReceiver;
|
private BroadcastReceiver mBluetoothChangedReceiver;
|
||||||
private ShadowApplication mShadowApplication;
|
private ShadowApplication mShadowApplication;
|
||||||
|
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -90,6 +90,7 @@ public class DiscoverableFooterPreferenceControllerTest {
|
|||||||
mAlwaysDiscoverable);
|
mAlwaysDiscoverable);
|
||||||
mBluetoothChangedReceiver = mDiscoverableFooterPreferenceController
|
mBluetoothChangedReceiver = mDiscoverableFooterPreferenceController
|
||||||
.mBluetoothChangedReceiver;
|
.mBluetoothChangedReceiver;
|
||||||
|
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -135,7 +136,7 @@ public class DiscoverableFooterPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onBluetoothStateChanged_bluetoothOn_updateTitle() {
|
public void onBluetoothStateChanged_bluetoothOn_updateTitle() {
|
||||||
ShadowLocalBluetoothAdapter.setName(DEVICE_NAME);
|
mShadowBluetoothAdapter.setName(DEVICE_NAME);
|
||||||
sendBluetoothStateChangedIntent(BluetoothAdapter.STATE_ON);
|
sendBluetoothStateChangedIntent(BluetoothAdapter.STATE_ON);
|
||||||
|
|
||||||
assertThat(mPreference.getTitle()).isEqualTo(generateTitle(DEVICE_NAME));
|
assertThat(mPreference.getTitle()).isEqualTo(generateTitle(DEVICE_NAME));
|
||||||
@@ -143,7 +144,7 @@ public class DiscoverableFooterPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onBluetoothStateChanged_bluetoothOff_updateTitle(){
|
public void onBluetoothStateChanged_bluetoothOff_updateTitle(){
|
||||||
ShadowLocalBluetoothAdapter.setName(DEVICE_NAME);
|
mShadowBluetoothAdapter.setName(DEVICE_NAME);
|
||||||
sendBluetoothStateChangedIntent(BluetoothAdapter.STATE_OFF);
|
sendBluetoothStateChangedIntent(BluetoothAdapter.STATE_OFF);
|
||||||
|
|
||||||
assertThat(mPreference.getTitle()).isEqualTo(generateTitle(null));
|
assertThat(mPreference.getTitle()).isEqualTo(generateTitle(null));
|
||||||
|
@@ -18,11 +18,10 @@ package com.android.settings.deviceinfo;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.Mockito.never;
|
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.wifi.WifiConfiguration;
|
import android.net.wifi.WifiConfiguration;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
@@ -30,29 +29,25 @@ import android.os.Build;
|
|||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||||
import com.android.settings.widget.ValidatedEditTextPreference;
|
import com.android.settings.widget.ValidatedEditTextPreference;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Answers;
|
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
import org.robolectric.shadows.ShadowApplication;
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
|
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||||
public class DeviceNamePreferenceControllerTest {
|
public class DeviceNamePreferenceControllerTest {
|
||||||
private static final String TESTING_STRING = "Testing";
|
private static final String TESTING_STRING = "Testing";
|
||||||
|
|
||||||
@Mock
|
|
||||||
private LocalBluetoothAdapter mBluetoothAdapter;
|
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
|
||||||
private LocalBluetoothManager mBluetoothManager;
|
|
||||||
@Mock
|
@Mock
|
||||||
private WifiManager mWifiManager;
|
private WifiManager mWifiManager;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -60,6 +55,7 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
private ValidatedEditTextPreference mPreference;
|
private ValidatedEditTextPreference mPreference;
|
||||||
private DeviceNamePreferenceController mController;
|
private DeviceNamePreferenceController mController;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -69,14 +65,13 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
shadowApplication.setSystemService(Context.WIFI_SERVICE, mWifiManager);
|
shadowApplication.setSystemService(Context.WIFI_SERVICE, mWifiManager);
|
||||||
mContext = shadowApplication.getApplicationContext();
|
mContext = shadowApplication.getApplicationContext();
|
||||||
mPreference = new ValidatedEditTextPreference(mContext);
|
mPreference = new ValidatedEditTextPreference(mContext);
|
||||||
when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBluetoothAdapter);
|
|
||||||
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
||||||
final WifiConfiguration configuration = new WifiConfiguration();
|
final WifiConfiguration configuration = new WifiConfiguration();
|
||||||
configuration.SSID = "test-ap";
|
configuration.SSID = "test-ap";
|
||||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(configuration);
|
when(mWifiManager.getWifiApConfiguration()).thenReturn(configuration);
|
||||||
|
|
||||||
mController = new DeviceNamePreferenceController(mContext);
|
mController = new DeviceNamePreferenceController(mContext);
|
||||||
mController.setLocalBluetoothManager(mBluetoothManager);
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -89,7 +84,6 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
Settings.Global.putString(
|
Settings.Global.putString(
|
||||||
mContext.getContentResolver(), Settings.Global.DEVICE_NAME, "Test");
|
mContext.getContentResolver(), Settings.Global.DEVICE_NAME, "Test");
|
||||||
mController = new DeviceNamePreferenceController(mContext);
|
mController = new DeviceNamePreferenceController(mContext);
|
||||||
mController.setLocalBluetoothManager(mBluetoothManager);
|
|
||||||
assertThat(mController.getSummary()).isEqualTo("Test");
|
assertThat(mController.getSummary()).isEqualTo("Test");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +112,7 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
mController.displayPreference(mScreen);
|
mController.displayPreference(mScreen);
|
||||||
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||||
|
|
||||||
verify(mBluetoothAdapter).setName(eq(TESTING_STRING));
|
assertThat(mBluetoothAdapter.getName()).isEqualTo(TESTING_STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -141,10 +135,11 @@ public class DeviceNamePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setDeviceName_ignoresIfCancelPressed() {
|
public void setDeviceName_ignoresIfCancelPressed() {
|
||||||
|
forceAcceptDeviceName();
|
||||||
mController.displayPreference(mScreen);
|
mController.displayPreference(mScreen);
|
||||||
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||||
|
|
||||||
verify(mBluetoothAdapter, never()).setName(eq(TESTING_STRING));
|
assertThat(mBluetoothAdapter.getName()).isEqualTo(TESTING_STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void forceAcceptDeviceName() {
|
private void forceAcceptDeviceName() {
|
||||||
|
@@ -26,6 +26,11 @@ import java.util.List;
|
|||||||
|
|
||||||
@Implements(value = BluetoothAdapter.class, inheritImplementationMethods = true)
|
@Implements(value = BluetoothAdapter.class, inheritImplementationMethods = true)
|
||||||
public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBluetoothAdapter {
|
public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBluetoothAdapter {
|
||||||
|
|
||||||
|
private String mName;
|
||||||
|
private int mScanMode;
|
||||||
|
private int mState;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Do nothing, implement it to avoid null pointer error inside BluetoothAdapter
|
* Do nothing, implement it to avoid null pointer error inside BluetoothAdapter
|
||||||
*/
|
*/
|
||||||
@@ -33,4 +38,32 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto
|
|||||||
public List<Integer> getSupportedProfiles() {
|
public List<Integer> getSupportedProfiles() {
|
||||||
return new ArrayList<Integer>();
|
return new ArrayList<Integer>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
mName = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public String getName() {
|
||||||
|
return mName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public void setScanMode(int scanMode) {
|
||||||
|
mScanMode = scanMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public int getScanMode() {
|
||||||
|
return mScanMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public int getConnectionState() {
|
||||||
|
return mState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnectionState(int state) {
|
||||||
|
mState = state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user