From 28be567a83b01c5818775164a7dc895bec7c7aa5 Mon Sep 17 00:00:00 2001 From: Weng Su Date: Fri, 21 Jul 2023 19:31:29 +0800 Subject: [PATCH] Show policy transparency dialog for Wi-Fi network restrictions - In order to support various user restrictions of individual Wi-Fi networks, WifiEntry provides the hasAdminRestrictions method for unified management. - Settings will refer to WifiEntry#hasAdminRestrictions to restrict individual Wi-Fi networks. Bug: 289448751 Bug: 289951241 Test: manual test atest -c LongPressWifiEntryPreferenceTest Change-Id: Iae5996a87ee72a3073300c7f62dfa14a9f31c21d --- .../wifi/LongPressWifiEntryPreference.java | 21 +++++++++++++++- .../LongPressWifiEntryPreferenceTest.java | 25 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/com/android/settings/wifi/LongPressWifiEntryPreference.java b/src/com/android/settings/wifi/LongPressWifiEntryPreference.java index 6343e06e074..ec94e74e0ff 100644 --- a/src/com/android/settings/wifi/LongPressWifiEntryPreference.java +++ b/src/com/android/settings/wifi/LongPressWifiEntryPreference.java @@ -22,6 +22,7 @@ import androidx.annotation.VisibleForTesting; import androidx.fragment.app.Fragment; import androidx.preference.PreferenceViewHolder; +import com.android.settingslib.RestrictedLockUtils; import com.android.wifitrackerlib.WifiEntry; /** @@ -34,7 +35,7 @@ public class LongPressWifiEntryPreference extends WifiEntryPreference { public LongPressWifiEntryPreference(Context context, WifiEntry wifiEntry, Fragment fragment) { super(context, wifiEntry); mFragment = fragment; - checkRestrictionAndSetDisabled(UserManager.DISALLOW_ADD_WIFI_CONFIG); + checkRestrictionAndSetDisabled(); } @Override @@ -65,4 +66,22 @@ public class LongPressWifiEntryPreference extends WifiEntryPreference { } return enabled; } + + @VisibleForTesting + void checkRestrictionAndSetDisabled() { + if (!getWifiEntry().hasAdminRestrictions()) { + return; + } + RestrictedLockUtils.EnforcedAdmin admin = null; + Context context = getContext(); + if (context != null) { + admin = RestrictedLockUtils.getProfileOrDeviceOwner(context, context.getUser()); + } + if (admin == null) { + // Use UserManager.DISALLOW_ADD_WIFI_CONFIG as default Wi-Fi network restriction. + admin = RestrictedLockUtils.EnforcedAdmin.createDefaultEnforcedAdminWithRestriction( + UserManager.DISALLOW_ADD_WIFI_CONFIG); + } + setDisabledByAdmin(admin); + } } diff --git a/tests/robotests/src/com/android/settings/wifi/LongPressWifiEntryPreferenceTest.java b/tests/robotests/src/com/android/settings/wifi/LongPressWifiEntryPreferenceTest.java index efc2018ec85..457d9ab4113 100644 --- a/tests/robotests/src/com/android/settings/wifi/LongPressWifiEntryPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/wifi/LongPressWifiEntryPreferenceTest.java @@ -18,6 +18,10 @@ package com.android.settings.wifi; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.Context; @@ -60,7 +64,7 @@ public class LongPressWifiEntryPreferenceTest { when(mWifiEntry.canDisconnect()).thenReturn(false); when(mWifiEntry.isSaved()).thenReturn(false); - mPreference = new LongPressWifiEntryPreference(mContext, mWifiEntry, mFragment); + mPreference = spy(new LongPressWifiEntryPreference(mContext, mWifiEntry, mFragment)); } @Test @@ -106,4 +110,23 @@ public class LongPressWifiEntryPreferenceTest { assertThat(mPreference.shouldEnabled()).isTrue(); } + + @Test + public void checkRestrictionAndSetDisabled_hasAdminRestrictions_doSetDisabledByAdmin() { + when(mContext.getUser()).thenReturn(null); + when(mWifiEntry.hasAdminRestrictions()).thenReturn(true); + + mPreference.checkRestrictionAndSetDisabled(); + + verify(mPreference).setDisabledByAdmin(any()); + } + + @Test + public void checkRestrictionAndSetDisabled_noAdminRestrictions_doNotSetDisabledByAdmin() { + when(mWifiEntry.hasAdminRestrictions()).thenReturn(false); + + mPreference.checkRestrictionAndSetDisabled(); + + verify(mPreference, never()).setDisabledByAdmin(any()); + } }