From 23ebda4c1210ffb48d6e5721f1641bc431b1573a Mon Sep 17 00:00:00 2001 From: William Escande Date: Mon, 20 Dec 2021 13:45:47 +0100 Subject: [PATCH 1/4] Add QUERY_AUDIO_STATE permission This permission is added to getLastAudibleStream Bug: 190422401 Tag: #refactor Test: Build + start a2dp + play music + change volume from DUT/speaker Change-Id: I65c5e6f50e7b440af57b2a0bd6656db91446578b --- AndroidManifest.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index b9d62f35879..350ddd6e66b 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -25,6 +25,7 @@ + From 26a0fdb4f6947635becc11e4bd8015b074f1905d Mon Sep 17 00:00:00 2001 From: Weng Su Date: Fri, 24 Dec 2021 09:36:26 +0800 Subject: [PATCH 2/4] Check WiFi restrictions for WiFi QR code intent - If WiFi configuration is not allowed, the WiFi QR code intent is ignored. - Add SafetyNet Logging to b/202017876. Bug: 202017876 Test: manual test make RunSettingsRoboTests ROBOTEST_FILTER=WifiDppEnrolleeActivityTest Change-Id: I147d2f4f4fabe2e24d5d3eaaad701b81059e8eee --- .../wifi/dpp/WifiDppEnrolleeActivity.java | 24 ++++++- .../wifi/dpp/WifiDppEnrolleeActivityTest.java | 66 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivity.java b/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivity.java index c5f1e81f80d..3ce244cea30 100644 --- a/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivity.java +++ b/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivity.java @@ -18,11 +18,14 @@ package com.android.settings.wifi.dpp; import android.app.settings.SettingsEnums; import android.content.Intent; +import android.util.EventLog; import android.util.Log; +import androidx.annotation.VisibleForTesting; import androidx.fragment.app.FragmentTransaction; import com.android.settings.R; +import com.android.settingslib.wifi.WifiRestrictionsCache; /** * To provision "this" device with specified Wi-Fi network. @@ -37,6 +40,9 @@ public class WifiDppEnrolleeActivity extends WifiDppBaseActivity implements static final String ACTION_ENROLLEE_QR_CODE_SCANNER = "android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER"; + @VisibleForTesting + protected WifiRestrictionsCache mWifiRestrictionsCache; + @Override public int getMetricsCategory() { return SettingsEnums.SETTINGS_WIFI_DPP_ENROLLEE; @@ -50,6 +56,14 @@ public class WifiDppEnrolleeActivity extends WifiDppBaseActivity implements return; } + if (!isWifiConfigAllowed()) { + Log.e(TAG, "The user is not allowed to configure Wi-Fi."); + finish(); + EventLog.writeEvent(0x534e4554, "202017876", getApplicationContext().getUserId(), + "The user is not allowed to configure Wi-Fi."); + return; + } + switch (action) { case ACTION_ENROLLEE_QR_CODE_SCANNER: String ssid = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID); @@ -61,7 +75,15 @@ public class WifiDppEnrolleeActivity extends WifiDppBaseActivity implements } } - private void showQrCodeScannerFragment(String ssid) { + private boolean isWifiConfigAllowed() { + if (mWifiRestrictionsCache == null) { + mWifiRestrictionsCache = WifiRestrictionsCache.getInstance(getApplicationContext()); + } + return mWifiRestrictionsCache.isConfigWifiAllowed(); + } + + @VisibleForTesting + protected void showQrCodeScannerFragment(String ssid) { WifiDppQrCodeScannerFragment fragment = (WifiDppQrCodeScannerFragment) mFragmentManager.findFragmentByTag( WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER); diff --git a/tests/robotests/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivityTest.java b/tests/robotests/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivityTest.java index 819b7f4331d..67d467803b2 100644 --- a/tests/robotests/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivityTest.java +++ b/tests/robotests/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivityTest.java @@ -16,16 +16,82 @@ package com.android.settings.wifi.dpp; +import static com.android.settings.wifi.dpp.WifiDppEnrolleeActivity.ACTION_ENROLLEE_QR_CODE_SCANNER; + +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Intent; + +import com.android.settingslib.wifi.WifiRestrictionsCache; + +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; @RunWith(RobolectricTestRunner.class) public class WifiDppEnrolleeActivityTest { + + private static final String WIFI_SSID = "wifi-ssid"; + + @Rule + public final MockitoRule mMockitoRule = MockitoJUnit.rule(); + @Mock + WifiRestrictionsCache mWifiRestrictionsCache; + @Mock + Intent mIntent; + + WifiDppEnrolleeActivity mActivity; + + @Before + public void setUp() { + when(mWifiRestrictionsCache.isConfigWifiAllowed()).thenReturn(true); + when(mIntent.getAction()).thenReturn(ACTION_ENROLLEE_QR_CODE_SCANNER); + when(mIntent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID)).thenReturn(WIFI_SSID); + + mActivity = spy(Robolectric.setupActivity(WifiDppEnrolleeActivity.class)); + mActivity.mWifiRestrictionsCache = mWifiRestrictionsCache; + } + @Test public void launchActivity_noIntentAction_shouldNotFatalException() { WifiDppEnrolleeActivity wifiDppEnrolleeActivity = Robolectric.setupActivity(WifiDppEnrolleeActivity.class); } + + @Test + public void handleIntent_noIntentAction_shouldFinish() { + when(mIntent.getAction()).thenReturn(null); + + mActivity.handleIntent(mIntent); + + verify(mActivity).finish(); + } + + @Test + public void handleIntent_notAllowedConfigWifi_shouldFinish() { + when(mWifiRestrictionsCache.isConfigWifiAllowed()).thenReturn(false); + + mActivity.handleIntent(mIntent); + + verify(mActivity).finish(); + } + + @Test + public void handleIntent_hasIntentDataAndAllowedConfigWifi_shouldShowFragment() { + when(mWifiRestrictionsCache.isConfigWifiAllowed()).thenReturn(true); + doNothing().when(mActivity).showQrCodeScannerFragment(WIFI_SSID); + + mActivity.handleIntent(mIntent); + + verify(mActivity).showQrCodeScannerFragment(WIFI_SSID); + } } From d29ead1824fa198c4c86845a5adfbd47341af6a2 Mon Sep 17 00:00:00 2001 From: Mill Chen Date: Tue, 28 Dec 2021 18:34:36 +0800 Subject: [PATCH 3/4] Enable nested scrolling for Trusted credentials The list of trusted root certs cannot be scrolled to the end. This is caused by the disabled nested scrolling. To ensure the CollapsingToolbarLayout works well with the list, the nested scrolling feature of ListView needs to be enabled. Bug: 191011957 Test: manual test Change-Id: Ic7a41377f983e90c85ca304798851112aebcc349 --- res/layout/trusted_credential_list_container.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/res/layout/trusted_credential_list_container.xml b/res/layout/trusted_credential_list_container.xml index 42dbfd9755b..5f8b7b3a654 100644 --- a/res/layout/trusted_credential_list_container.xml +++ b/res/layout/trusted_credential_list_container.xml @@ -53,6 +53,7 @@ From a639d2666012e2eaaeb1bec09b9bcc7d89c74925 Mon Sep 17 00:00:00 2001 From: changbetty Date: Tue, 21 Dec 2021 02:49:03 +0000 Subject: [PATCH 4/4] To disable Wi-Fi Direct when user restriction is set - Remove the Location check for Wi-Fi Direct - Add user restriction check for Wi-Fi Direct Bug: 204288318 Bug: 203170358 Test: make RunSettingsRoboTests ROBOTEST_FILTER=WifiP2PPreferenceControllerTest Change-Id: I64dab964b485665dd42c933d8974aeda47f178fe --- .../wifi/p2p/WifiP2pPreferenceController.java | 29 ++++-------- .../p2p/WifiP2PPreferenceControllerTest.java | 46 +++++++++++-------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/com/android/settings/wifi/p2p/WifiP2pPreferenceController.java b/src/com/android/settings/wifi/p2p/WifiP2pPreferenceController.java index 96044100647..db150dab3b5 100644 --- a/src/com/android/settings/wifi/p2p/WifiP2pPreferenceController.java +++ b/src/com/android/settings/wifi/p2p/WifiP2pPreferenceController.java @@ -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; + } + } diff --git a/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2PPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2PPreferenceControllerTest.java index 10d76d39305..a9d5611918d 100644 --- a/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2PPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2PPreferenceControllerTest.java @@ -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(); - mController.displayPreference(mScreen); - verify(mWifiDirectPreference).setEnabled(true); + public void displayPreference_wifiDirectNotAllowed_shouldDisable() { + mController.mIsWifiDirectAllow = false; + + mController.displayPreference(mScreen); - 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); + } }