Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 75c603ce authored by arangelov's avatar arangelov
Browse files

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
parent a0cb9280
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -20,13 +20,14 @@
    android:key="connected_devices_screen"
    android:title="@string/connected_device_connections_title">

    <SwitchPreference
    <com.android.settingslib.RestrictedSwitchPreference
        android:key="toggle_bluetooth_switch"
        android:title="@string/bluetooth_settings_title"
        android:icon="@drawable/ic_settings_bluetooth"
        android:summary="@string/bluetooth_pref_summary"
        android:order="-7"
        settings:controller="com.android.settings.bluetooth.BluetoothSwitchPreferenceController"
        settings:userRestriction="no_bluetooth"
        settings:platform_slice="true"/>

    <SwitchPreference
+2 −2
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ public class BluetoothSwitchPreferenceController extends TogglePreferenceControl
     * Control the switch inside {@link SwitchPreference}
     */
    @VisibleForTesting
    class SwitchController extends SwitchWidgetController implements
    static class SwitchController extends SwitchWidgetController implements
            Preference.OnPreferenceChangeListener {
        private SwitchPreference mSwitchPreference;

@@ -156,7 +156,7 @@ public class BluetoothSwitchPreferenceController extends TogglePreferenceControl

        @Override
        public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
            mBtPreference.setEnabled(admin == null);
            mSwitchPreference.setEnabled(admin == null);
        }
    }
}
+30 −34
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.settings.bluetooth;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.mock;
@@ -36,11 +37,11 @@ import android.view.View;
import android.widget.Switch;

import com.android.settings.R;
import com.android.settings.bluetooth.BluetoothSwitchPreferenceController.SwitchController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
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.RestrictedSwitchPreference;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
@@ -60,6 +61,8 @@ import org.robolectric.annotation.Config;
public class BluetoothEnablerTest {

    private static EnforcedAdmin sFakeEnforcedAdmin;
    private PreferenceViewHolder mHolder;
    private RestrictedSwitchPreference mRestrictedSwitchPreference;

    @BeforeClass
    public static void beforeClass() {
@@ -76,8 +79,7 @@ public class BluetoothEnablerTest {
    private LocalBluetoothAdapter mBluetoothAdapter;

    private Context mContext;
    private Switch mSwitch;
    private MasterSwitchController mMasterSwitchController;
    private SwitchController mSwitchController;
    private BluetoothEnabler mBluetoothEnabler;

    @Before
@@ -86,19 +88,17 @@ public class BluetoothEnablerTest {
        mContext = spy(RuntimeEnvironment.application);
        when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBluetoothAdapter);

        mSwitch = new Switch(mContext);
        MasterSwitchPreference masterSwitchPreference = new MasterSwitchPreference(mContext);
        mMasterSwitchController = spy(new MasterSwitchController(masterSwitchPreference));
        mRestrictedSwitchPreference = new RestrictedSwitchPreference(mContext);
        mSwitchController = spy(new SwitchController(mRestrictedSwitchPreference));
        mBluetoothEnabler = new BluetoothEnabler(
                mContext,
                mMasterSwitchController,
                mSwitchController,
                mMetricsFeatureProvider,
                mBluetoothManager,
                123,
                mRestrictionUtils);
        PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(mock(View.class));
        when(holder.findViewById(R.id.switchWidget)).thenReturn(mSwitch);
        masterSwitchPreference.onBindViewHolder(holder);
        mHolder = PreferenceViewHolder.createInstanceForTests(mock(View.class));
        mRestrictedSwitchPreference.onBindViewHolder(mHolder);
    }

    @Test
@@ -121,9 +121,9 @@ public class BluetoothEnablerTest {
        assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isFalse();

        // THEN a null EnfoceAdmin is set.
        verify(mMasterSwitchController).setDisabledByAdmin(null);
        verify(mSwitchController).setDisabledByAdmin(null);
        // THEN the state of the switch isn't changed.
        verify(mMasterSwitchController, never()).setChecked(anyBoolean());
        verify(mSwitchController, never()).setChecked(anyBoolean());
    }

    @Test
@@ -139,10 +139,10 @@ public class BluetoothEnablerTest {
        assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isTrue();

        // THEN the expected EnfoceAdmin is set.
        verify(mMasterSwitchController).setDisabledByAdmin(sFakeEnforcedAdmin);
        verify(mSwitchController).setDisabledByAdmin(sFakeEnforcedAdmin);

        // THEN the switch is unchecked.
        verify(mMasterSwitchController).setChecked(false);
        verify(mSwitchController).setChecked(false);
    }

    @Test
@@ -158,10 +158,10 @@ public class BluetoothEnablerTest {
        assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isTrue();

        // THEN the expected EnfoceAdmin is set.
        verify(mMasterSwitchController).setDisabledByAdmin(sFakeEnforcedAdmin);
        verify(mSwitchController).setDisabledByAdmin(sFakeEnforcedAdmin);

        // THEN the switch is unchecked.
        verify(mMasterSwitchController).setChecked(false);
        verify(mSwitchController).setChecked(false);
    }

    @Test
@@ -174,37 +174,36 @@ public class BluetoothEnablerTest {

        mBluetoothEnabler.resume(mContext);

        verify(mMasterSwitchController, never()).setEnabled(true);
        verify(mSwitchController, never()).setEnabled(true);
    }

    @Test
    public void startWithBluetoothOff_switchIsOff() {
        when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
        verify(mMasterSwitchController, never()).setChecked(anyBoolean());
        verify(mSwitchController, never()).setChecked(anyBoolean());
        mBluetoothEnabler.resume(mContext);
        verify(mMasterSwitchController, never()).setChecked(true);
        verify(mSwitchController, never()).setChecked(true);
    }

    @Test
    public void startWithBluetoothOn_switchIsOn() {
        when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
        verify(mMasterSwitchController, never()).setChecked(anyBoolean());
        verify(mSwitchController, never()).setChecked(anyBoolean());
        mBluetoothEnabler.resume(mContext);
        verify(mMasterSwitchController, never()).setChecked(false);
        verify(mMasterSwitchController).setChecked(true);
        verify(mSwitchController, never()).setChecked(false);
        verify(mSwitchController).setChecked(true);
    }

    @Test
    public void bluetoothTurnsOff_switchTurnsOff() {
        // Start up with bluetooth turned on. The switch should get turned on.
        assertThat(mSwitch.isChecked()).isFalse();
        ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
        when(mContext.registerReceiver(captor.capture(), any(IntentFilter.class))).thenReturn(null);
        when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
        verify(mMasterSwitchController, never()).setChecked(anyBoolean());
        verify(mSwitchController, never()).setChecked(anyBoolean());
        mBluetoothEnabler.resume(mContext);
        verify(mMasterSwitchController, never()).setChecked(false);
        verify(mMasterSwitchController).setChecked(true);
        verify(mSwitchController, never()).setChecked(false);
        verify(mSwitchController).setChecked(true);

        // Now simulate bluetooth being turned off via an event.
        BroadcastReceiver receiver = captor.getValue();
@@ -216,20 +215,18 @@ public class BluetoothEnablerTest {
        receiver.onReceive(mContext, off);

        // Make sure the switch was turned off.
        verify(mMasterSwitchController).setChecked(false);
        assertThat(mSwitch.isChecked()).isFalse();
        verify(mSwitchController).setChecked(false);
    }

    @Test
    public void bluetoothTurnsOn_switchTurnsOn() {
        // Start up with bluetooth turned on. The switch should be left off.
        assertThat(mSwitch.isChecked()).isFalse();
        ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
        when(mContext.registerReceiver(captor.capture(), any(IntentFilter.class))).thenReturn(null);
        when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
        verify(mMasterSwitchController, never()).setChecked(anyBoolean());
        verify(mSwitchController, never()).setChecked(anyBoolean());
        mBluetoothEnabler.resume(mContext);
        verify(mMasterSwitchController, never()).setChecked(anyBoolean());
        verify(mSwitchController, never()).setChecked(anyBoolean());

        // Now simulate bluetooth being turned on via an event.
        BroadcastReceiver receiver = captor.getValue();
@@ -241,7 +238,6 @@ public class BluetoothEnablerTest {
        receiver.onReceive(mContext, on);

        // Make sure the switch was turned on.
        verify(mMasterSwitchController).setChecked(true);
        assertThat(mSwitch.isChecked()).isTrue();
        verify(mSwitchController).setChecked(true);
    }
}