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

Commit fe76826c authored by Betty Chang's avatar Betty Chang Committed by Android (Google) Code Review
Browse files

Merge "To disable Wi-Fi Direct when user restriction is set"

parents 03f3d9cb a639d266
Loading
Loading
Loading
Loading
+10 −19
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
import com.android.settingslib.wifi.WifiEnterpriseRestrictionUtils;

/**
 * {@link PreferenceControllerMixin} to toggle Wifi Direct preference on Wi-Fi state.
@@ -51,27 +52,17 @@ public class WifiP2pPreferenceController extends AbstractPreferenceController
        }
    };
    private final IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
    private final LocationManager mLocationManager;
    @VisibleForTesting
    final BroadcastReceiver mLocationReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mWifiDirectPref != null) {
                updateState(mWifiDirectPref);
            }
        }
    };
    private final IntentFilter mLocationFilter =
            new IntentFilter(LocationManager.MODE_CHANGED_ACTION);

    private Preference mWifiDirectPref;
    @VisibleForTesting
    boolean mIsWifiDirectAllow;

    public WifiP2pPreferenceController(
            Context context, Lifecycle lifecycle, WifiManager wifiManager) {
        super(context);
        mWifiManager = wifiManager;
        mIsWifiDirectAllow = WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(context);
        lifecycle.addObserver(this);
        mLocationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE);
    }

    @Override
@@ -84,19 +75,17 @@ public class WifiP2pPreferenceController extends AbstractPreferenceController
    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        preference.setEnabled(mLocationManager.isLocationEnabled() && mWifiManager.isWifiEnabled());
        preference.setEnabled(isWifiP2pAvailable());
    }

    @Override
    public void onResume() {
        mContext.registerReceiver(mReceiver, mFilter);
        mContext.registerReceiver(mLocationReceiver, mLocationFilter);
    }

    @Override
    public void onPause() {
        mContext.unregisterReceiver(mReceiver);
        mContext.unregisterReceiver(mLocationReceiver);
    }

    @Override
@@ -111,9 +100,11 @@ public class WifiP2pPreferenceController extends AbstractPreferenceController

    private void togglePreferences() {
        if (mWifiDirectPref != null) {
            mWifiDirectPref.setEnabled(
                    mWifiManager.isWifiEnabled()
                    && mLocationManager.isLocationEnabled());
            mWifiDirectPref.setEnabled(isWifiP2pAvailable());
        }
    }
    private boolean isWifiP2pAvailable() {
        return mWifiManager.isWifiEnabled() && mIsWifiDirectAllow;
    }

}
+26 −18
Original line number Diff line number Diff line
@@ -32,8 +32,9 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.UserManager;

import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
@@ -56,12 +57,15 @@ public class WifiP2PPreferenceControllerTest {
    private Context mContext;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private WifiManager mWifiManager;
    @Mock
    private UserManager mUserManager;
    @Mock
    private Bundle mBundle;

    @Mock
    private PreferenceScreen mScreen;
    @Mock
    private Preference mWifiDirectPreference;
    @Mock
    private LocationManager mLocationManager;

    private Lifecycle mLifecycle;
    private LifecycleOwner mLifecycleOwner;
@@ -74,8 +78,11 @@ public class WifiP2PPreferenceControllerTest {
        mLifecycle = new Lifecycle(mLifecycleOwner);
        when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
        when(mScreen.findPreference(anyString())).thenReturn(mWifiDirectPreference);
        when(mContext.getSystemService(eq(Service.LOCATION_SERVICE))).thenReturn(mLocationManager);
        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
        when(mUserManager.getUserRestrictions()).thenReturn(mBundle);
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        mController = new WifiP2pPreferenceController(mContext, mLifecycle, mWifiManager);
        mController.mIsWifiDirectAllow = true;
    }

    @Test
@@ -86,21 +93,19 @@ public class WifiP2PPreferenceControllerTest {
    @Test
    public void testOnResume_shouldRegisterListener() {
        mLifecycle.handleLifecycleEvent(ON_RESUME);
        verify(mContext, times(2)).registerReceiver(
                any(BroadcastReceiver.class), any(IntentFilter.class));
        verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
    }

    @Test
    public void testOnPause_shouldUnregisterListener() {
        mLifecycle.handleLifecycleEvent(ON_RESUME);
        mLifecycle.handleLifecycleEvent(ON_PAUSE);
        verify(mContext, times(2)).unregisterReceiver(any(BroadcastReceiver.class));
        verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
    }

    @Test
    public void testWifiStateChange_shouldToggleEnabledState() {
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mLocationManager.isLocationEnabled()).thenReturn(true);

        //Sets the preferences.
        mController.displayPreference(mScreen);
@@ -118,7 +123,6 @@ public class WifiP2PPreferenceControllerTest {
    @Test
    public void testDisplayPreference_shouldToggleEnabledState() {
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mLocationManager.isLocationEnabled()).thenReturn(true);
        mController.displayPreference(mScreen);
        verify(mWifiDirectPreference).setEnabled(true);

@@ -127,21 +131,25 @@ public class WifiP2PPreferenceControllerTest {
        verify(mWifiDirectPreference).setEnabled(false);

        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mLocationManager.isLocationEnabled()).thenReturn(false);
        mController.displayPreference(mScreen);
        verify(mWifiDirectPreference, times(2)).setEnabled(false);
        verify(mWifiDirectPreference).setEnabled(false);
    }

    @Test
    public void updateState_withLocationDisabled_preferenceShouldBeDisable() {
        when(mWifiManager.isWifiEnabled()).thenReturn(true);
        when(mLocationManager.isLocationEnabled()).thenReturn(true);
        Intent fakeIntent = new Intent();
    public void displayPreference_wifiDirectNotAllowed_shouldDisable() {
        mController.mIsWifiDirectAllow = false;

        mController.displayPreference(mScreen);
        verify(mWifiDirectPreference).setEnabled(true);

        when(mLocationManager.isLocationEnabled()).thenReturn(false);
        mController.mLocationReceiver.onReceive(mContext, fakeIntent);
        verify(mWifiDirectPreference).setEnabled(false);
    }

    @Test
    public void displayPreference_wifiDirectNotAllowed_shouldEnable() {
        mController.mIsWifiDirectAllow = true;

        mController.displayPreference(mScreen);

        verify(mWifiDirectPreference).setEnabled(true);
    }
}