From 53184b75236df66d2a90970827a25b738fa3d61c Mon Sep 17 00:00:00 2001 From: changbetty Date: Wed, 15 Dec 2021 15:13:38 +0000 Subject: [PATCH] To add restriction check for Wi-Fi Tethering Bug: 203168953 Test: make RunSettingsLibRoboTests ROBOTEST_FILTER=WifiEnterpriseRestrictionUtilsTest Change-Id: I49f3197ce79c2ef60b73b6ff079a8aa7f86356cc --- .../wifi/WifiEnterpriseRestrictionUtils.java | 51 ++++++++++++ .../WifiEnterpriseRestrictionUtilsTest.java | 80 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 packages/SettingsLib/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtils.java create mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtilsTest.java diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtils.java b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtils.java new file mode 100644 index 0000000000000..24233729ad450 --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtils.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settingslib.wifi; + +import android.content.Context; +import android.os.Build; +import android.os.Bundle; +import android.os.UserManager; +import android.util.Log; + +import androidx.annotation.ChecksSdkIntAtLeast; + +/* Utility class is to confirm the Wi-Fi function is available by enterprise restriction */ +public class WifiEnterpriseRestrictionUtils { + private static final String TAG = "WifiEntResUtils"; + + /** + * Confirm Wi-Fi tethering is available according to whether user restriction is set + * + * @param context A context + * @return whether the device is permitted to use Wi-Fi Tethering + */ + public static boolean isWifiTetheringAllowed(Context context) { + final UserManager userManager = context.getSystemService(UserManager.class); + final Bundle restrictions = userManager.getUserRestrictions(); + if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)) { + Log.i(TAG, "Wi-Fi Tethering isn't available due to user restriction."); + return false; + } + return true; + } + + @ChecksSdkIntAtLeast(api=Build.VERSION_CODES.TIRAMISU) + private static boolean isAtLeastT() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU; + } +} diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtilsTest.java new file mode 100644 index 0000000000000..455dc64fa97d9 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtilsTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settingslib.wifi; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.os.Build; +import android.os.Bundle; +import android.os.UserManager; + +import androidx.test.core.app.ApplicationProvider; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.util.ReflectionHelpers; + +@RunWith(RobolectricTestRunner.class) +public class WifiEnterpriseRestrictionUtilsTest { + + private Context mContext; + @Mock + private UserManager mUserManager; + @Mock + private Bundle mBundle; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = spy(ApplicationProvider.getApplicationContext()); + when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); + when(mUserManager.getUserRestrictions()).thenReturn(mBundle); + } + + @Test + public void isWifiTetheringAllowed_setSDKForS_shouldReturnTrue() { + ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); + when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(true); + + assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue(); + } + + @Test + public void isWifiTetheringAllowed_setSDKForTAndDisallowForRestriction_shouldReturnFalse() { + ReflectionHelpers.setStaticField( + Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); + when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(true); + + assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isFalse(); + } + + @Test + public void isWifiTetheringAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { + ReflectionHelpers.setStaticField( + Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); + when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(false); + + assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue(); + } +}