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

Commit 2bf7de91 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Add DISALLOW_CHANGE_WIFI_STATE user restriction" into tm-dev am: 2bc6dae0

parents 3c7597a1 2bc6dae0
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import android.util.Log;

import androidx.annotation.ChecksSdkIntAtLeast;

import com.android.internal.annotations.VisibleForTesting;

/* Utility class is to confirm the Wi-Fi function is available by enterprise restriction */
public class WifiEnterpriseRestrictionUtils {
    private static final String TAG = "WifiEntResUtils";
@@ -76,6 +78,26 @@ public class WifiEnterpriseRestrictionUtils {
        return true;
    }

    /**
     * Confirm Wi-Fi state is allowed to change to whether user restriction is set
     *
     * @param context A context
     * @return whether the device is permitted to change Wi-Fi state
     */
    public static boolean isChangeWifiStateAllowed(Context context) {
        if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_CHANGE_WIFI_STATE)) return true;
        Log.w(TAG, "WI-FI state isn't allowed to change due to user restriction.");
        return false;
    }

    @VisibleForTesting
    static boolean hasUserRestrictionFromT(Context context, String restrictionKey) {
        if (!isAtLeastT()) return false;
        final UserManager userManager = context.getSystemService(UserManager.class);
        if (userManager == null) return false;
        return userManager.hasUserRestriction(restrictionKey);
    }

    @ChecksSdkIntAtLeast(api=Build.VERSION_CODES.TIRAMISU)
    private static boolean isAtLeastT() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU;
+42 −0
Original line number Diff line number Diff line
@@ -15,8 +15,11 @@
 */
package com.android.settingslib.wifi;

import static android.os.UserManager.DISALLOW_CHANGE_WIFI_STATE;

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

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

@@ -50,6 +53,8 @@ public class WifiEnterpriseRestrictionUtilsTest {
        mContext = spy(ApplicationProvider.getApplicationContext());
        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
        when(mUserManager.getUserRestrictions()).thenReturn(mBundle);
        ReflectionHelpers.setStaticField(
                Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU);
    }

    @Test
@@ -129,4 +134,41 @@ public class WifiEnterpriseRestrictionUtilsTest {

        assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isTrue();
    }

    @Test
    public void isChangeWifiStateAllowed_hasDisallowRestriction_shouldReturnFalse() {
        when(mUserManager.hasUserRestriction(DISALLOW_CHANGE_WIFI_STATE)).thenReturn(true);

        assertThat(WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed(mContext)).isFalse();
    }

    @Test
    public void isChangeWifiStateAllowed_hasNoDisallowRestriction_shouldReturnTrue() {
        when(mUserManager.hasUserRestriction(DISALLOW_CHANGE_WIFI_STATE)).thenReturn(false);

        assertThat(WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed(mContext)).isTrue();
    }

    @Test
    public void hasUserRestrictionFromT_setSDKForS_shouldReturnTrue() {
        ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S);

        assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key"))
                .isFalse();
    }

    @Test
    public void hasUserRestrictionFromT_setSDKForT_shouldReturnHasUserRestriction() {
        ReflectionHelpers.setStaticField(
                Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU);
        when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);

        assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key"))
                .isFalse();

        when(mUserManager.hasUserRestriction(anyString())).thenReturn(true);

        assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key"))
                .isTrue();
    }
}