Show policy transparency popup dialog for Bluetooth in Settings.
Bug: 70206452 Test: make ROBOTEST_FILTER=BluetoothEnablerTest -j40 RunSettingsRobotests Test: make ROBOTEST_FILTER=BluetoothSwitchPreferenceControllerTest -j40 RunSettingsRobotests Change-Id: I543cf69b754c44b02c66e101698f10906a1b957f
This commit is contained in:
@@ -20,14 +20,15 @@
|
|||||||
android:key="connected_devices_screen"
|
android:key="connected_devices_screen"
|
||||||
android:title="@string/connected_device_connections_title">
|
android:title="@string/connected_device_connections_title">
|
||||||
|
|
||||||
<SwitchPreference
|
<com.android.settingslib.RestrictedSwitchPreference
|
||||||
android:key="toggle_bluetooth_switch"
|
android:key="toggle_bluetooth_switch"
|
||||||
android:title="@string/bluetooth_settings_title"
|
android:title="@string/bluetooth_settings_title"
|
||||||
android:icon="@drawable/ic_settings_bluetooth"
|
android:icon="@drawable/ic_settings_bluetooth"
|
||||||
android:summary="@string/bluetooth_pref_summary"
|
android:summary="@string/bluetooth_pref_summary"
|
||||||
android:order="-7"
|
android:order="-7"
|
||||||
settings:controller="com.android.settings.bluetooth.BluetoothSwitchPreferenceController"
|
settings:controller="com.android.settings.bluetooth.BluetoothSwitchPreferenceController"
|
||||||
settings:platform_slice="true"/>
|
settings:userRestriction="no_bluetooth"
|
||||||
|
settings:platform_slice="true"/>
|
||||||
|
|
||||||
<SwitchPreference
|
<SwitchPreference
|
||||||
android:key="toggle_nfc"
|
android:key="toggle_nfc"
|
||||||
|
@@ -109,7 +109,7 @@ public class BluetoothSwitchPreferenceController extends TogglePreferenceControl
|
|||||||
* Control the switch inside {@link SwitchPreference}
|
* Control the switch inside {@link SwitchPreference}
|
||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
class SwitchController extends SwitchWidgetController implements
|
static class SwitchController extends SwitchWidgetController implements
|
||||||
Preference.OnPreferenceChangeListener {
|
Preference.OnPreferenceChangeListener {
|
||||||
private SwitchPreference mSwitchPreference;
|
private SwitchPreference mSwitchPreference;
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ public class BluetoothSwitchPreferenceController extends TogglePreferenceControl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
|
public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
|
||||||
mBtPreference.setEnabled(admin == null);
|
mSwitchPreference.setEnabled(admin == null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
package com.android.settings.bluetooth;
|
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.anyBoolean;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
@@ -36,11 +37,11 @@ import android.view.View;
|
|||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.bluetooth.BluetoothSwitchPreferenceController.SwitchController;
|
||||||
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.widget.MasterSwitchController;
|
|
||||||
import com.android.settings.widget.MasterSwitchPreference;
|
|
||||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||||
|
import com.android.settingslib.RestrictedSwitchPreference;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
@@ -60,6 +61,8 @@ import org.robolectric.annotation.Config;
|
|||||||
public class BluetoothEnablerTest {
|
public class BluetoothEnablerTest {
|
||||||
|
|
||||||
private static EnforcedAdmin sFakeEnforcedAdmin;
|
private static EnforcedAdmin sFakeEnforcedAdmin;
|
||||||
|
private PreferenceViewHolder mHolder;
|
||||||
|
private RestrictedSwitchPreference mRestrictedSwitchPreference;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() {
|
public static void beforeClass() {
|
||||||
@@ -76,8 +79,7 @@ public class BluetoothEnablerTest {
|
|||||||
private LocalBluetoothAdapter mBluetoothAdapter;
|
private LocalBluetoothAdapter mBluetoothAdapter;
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private Switch mSwitch;
|
private SwitchController mSwitchController;
|
||||||
private MasterSwitchController mMasterSwitchController;
|
|
||||||
private BluetoothEnabler mBluetoothEnabler;
|
private BluetoothEnabler mBluetoothEnabler;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -86,19 +88,17 @@ public class BluetoothEnablerTest {
|
|||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBluetoothAdapter);
|
when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBluetoothAdapter);
|
||||||
|
|
||||||
mSwitch = new Switch(mContext);
|
mRestrictedSwitchPreference = new RestrictedSwitchPreference(mContext);
|
||||||
MasterSwitchPreference masterSwitchPreference = new MasterSwitchPreference(mContext);
|
mSwitchController = spy(new SwitchController(mRestrictedSwitchPreference));
|
||||||
mMasterSwitchController = spy(new MasterSwitchController(masterSwitchPreference));
|
|
||||||
mBluetoothEnabler = new BluetoothEnabler(
|
mBluetoothEnabler = new BluetoothEnabler(
|
||||||
mContext,
|
mContext,
|
||||||
mMasterSwitchController,
|
mSwitchController,
|
||||||
mMetricsFeatureProvider,
|
mMetricsFeatureProvider,
|
||||||
mBluetoothManager,
|
mBluetoothManager,
|
||||||
123,
|
123,
|
||||||
mRestrictionUtils);
|
mRestrictionUtils);
|
||||||
PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(mock(View.class));
|
mHolder = PreferenceViewHolder.createInstanceForTests(mock(View.class));
|
||||||
when(holder.findViewById(R.id.switchWidget)).thenReturn(mSwitch);
|
mRestrictedSwitchPreference.onBindViewHolder(mHolder);
|
||||||
masterSwitchPreference.onBindViewHolder(holder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -121,9 +121,9 @@ public class BluetoothEnablerTest {
|
|||||||
assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isFalse();
|
assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isFalse();
|
||||||
|
|
||||||
// THEN a null EnfoceAdmin is set.
|
// THEN a null EnfoceAdmin is set.
|
||||||
verify(mMasterSwitchController).setDisabledByAdmin(null);
|
verify(mSwitchController).setDisabledByAdmin(null);
|
||||||
// THEN the state of the switch isn't changed.
|
// THEN the state of the switch isn't changed.
|
||||||
verify(mMasterSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -139,10 +139,10 @@ public class BluetoothEnablerTest {
|
|||||||
assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isTrue();
|
assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isTrue();
|
||||||
|
|
||||||
// THEN the expected EnfoceAdmin is set.
|
// THEN the expected EnfoceAdmin is set.
|
||||||
verify(mMasterSwitchController).setDisabledByAdmin(sFakeEnforcedAdmin);
|
verify(mSwitchController).setDisabledByAdmin(sFakeEnforcedAdmin);
|
||||||
|
|
||||||
// THEN the switch is unchecked.
|
// THEN the switch is unchecked.
|
||||||
verify(mMasterSwitchController).setChecked(false);
|
verify(mSwitchController).setChecked(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -158,10 +158,10 @@ public class BluetoothEnablerTest {
|
|||||||
assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isTrue();
|
assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isTrue();
|
||||||
|
|
||||||
// THEN the expected EnfoceAdmin is set.
|
// THEN the expected EnfoceAdmin is set.
|
||||||
verify(mMasterSwitchController).setDisabledByAdmin(sFakeEnforcedAdmin);
|
verify(mSwitchController).setDisabledByAdmin(sFakeEnforcedAdmin);
|
||||||
|
|
||||||
// THEN the switch is unchecked.
|
// THEN the switch is unchecked.
|
||||||
verify(mMasterSwitchController).setChecked(false);
|
verify(mSwitchController).setChecked(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -174,37 +174,36 @@ public class BluetoothEnablerTest {
|
|||||||
|
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
|
|
||||||
verify(mMasterSwitchController, never()).setEnabled(true);
|
verify(mSwitchController, never()).setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startWithBluetoothOff_switchIsOff() {
|
public void startWithBluetoothOff_switchIsOff() {
|
||||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
|
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
|
||||||
verify(mMasterSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
verify(mMasterSwitchController, never()).setChecked(true);
|
verify(mSwitchController, never()).setChecked(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startWithBluetoothOn_switchIsOn() {
|
public void startWithBluetoothOn_switchIsOn() {
|
||||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
||||||
verify(mMasterSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
verify(mMasterSwitchController, never()).setChecked(false);
|
verify(mSwitchController, never()).setChecked(false);
|
||||||
verify(mMasterSwitchController).setChecked(true);
|
verify(mSwitchController).setChecked(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bluetoothTurnsOff_switchTurnsOff() {
|
public void bluetoothTurnsOff_switchTurnsOff() {
|
||||||
// Start up with bluetooth turned on. The switch should get turned on.
|
// Start up with bluetooth turned on. The switch should get turned on.
|
||||||
assertThat(mSwitch.isChecked()).isFalse();
|
|
||||||
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);
|
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
||||||
verify(mMasterSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
verify(mMasterSwitchController, never()).setChecked(false);
|
verify(mSwitchController, never()).setChecked(false);
|
||||||
verify(mMasterSwitchController).setChecked(true);
|
verify(mSwitchController).setChecked(true);
|
||||||
|
|
||||||
// Now simulate bluetooth being turned off via an event.
|
// Now simulate bluetooth being turned off via an event.
|
||||||
BroadcastReceiver receiver = captor.getValue();
|
BroadcastReceiver receiver = captor.getValue();
|
||||||
@@ -216,20 +215,18 @@ public class BluetoothEnablerTest {
|
|||||||
receiver.onReceive(mContext, off);
|
receiver.onReceive(mContext, off);
|
||||||
|
|
||||||
// Make sure the switch was turned off.
|
// Make sure the switch was turned off.
|
||||||
verify(mMasterSwitchController).setChecked(false);
|
verify(mSwitchController).setChecked(false);
|
||||||
assertThat(mSwitch.isChecked()).isFalse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bluetoothTurnsOn_switchTurnsOn() {
|
public void bluetoothTurnsOn_switchTurnsOn() {
|
||||||
// Start up with bluetooth turned on. The switch should be left off.
|
// Start up with bluetooth turned on. The switch should be left off.
|
||||||
assertThat(mSwitch.isChecked()).isFalse();
|
|
||||||
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);
|
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
|
||||||
verify(mMasterSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
mBluetoothEnabler.resume(mContext);
|
mBluetoothEnabler.resume(mContext);
|
||||||
verify(mMasterSwitchController, never()).setChecked(anyBoolean());
|
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||||
|
|
||||||
// Now simulate bluetooth being turned on via an event.
|
// Now simulate bluetooth being turned on via an event.
|
||||||
BroadcastReceiver receiver = captor.getValue();
|
BroadcastReceiver receiver = captor.getValue();
|
||||||
@@ -241,7 +238,6 @@ public class BluetoothEnablerTest {
|
|||||||
receiver.onReceive(mContext, on);
|
receiver.onReceive(mContext, on);
|
||||||
|
|
||||||
// Make sure the switch was turned on.
|
// Make sure the switch was turned on.
|
||||||
verify(mMasterSwitchController).setChecked(true);
|
verify(mSwitchController).setChecked(true);
|
||||||
assertThat(mSwitch.isChecked()).isTrue();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user