From 893e6a15cb6a891d915d531f89fcf67cb2126223 Mon Sep 17 00:00:00 2001 From: Arc Wang Date: Mon, 27 Apr 2020 19:39:04 +0800 Subject: Show Wi-Fi editor for authentication failures When the Wi-Fi network is disabled because of authentication failures, Wi-Fi picker should show editor before connecting or the network will always fail to connect. Bug: 112137755 Test: atest com.android.wifitrackerlib.StandardWifiEntryTest Change-Id: I6b9f737f8aea6ef60e92e043afd343e7b8a93ba7 --- .../android/wifitrackerlib/StandardWifiEntry.java | 33 +++++++ .../src/com/android/wifitrackerlib/WifiEntry.java | 9 ++ .../wifitrackerlib/StandardWifiEntryTest.java | 103 +++++++++++++++++++++ 3 files changed, 145 insertions(+) (limited to 'libs') diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java index b6afa984a..b65f65630 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java @@ -16,6 +16,10 @@ package com.android.wifitrackerlib; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_NO_CREDENTIALS; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED; import static android.net.wifi.WifiInfo.sanitizeSsid; import static androidx.core.util.Preconditions.checkNotNull; @@ -44,6 +48,7 @@ import android.net.NetworkScoreManager; import android.net.NetworkScorerAppData; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Handler; @@ -641,6 +646,34 @@ public class StandardWifiEntry extends WifiEntry { return false; } + @Override + public boolean shouldEditBeforeConnect() { + WifiConfiguration wifiConfig = getWifiConfiguration(); + if (wifiConfig == null) { + return false; + } + + // The secured Wi-Fi entry is never connected. + if (getSecurity() != SECURITY_NONE && getSecurity() != SECURITY_OWE + && !wifiConfig.getNetworkSelectionStatus().hasEverConnected()) { + return true; + } + + // The network is disabled because of one of the authentication problems. + NetworkSelectionStatus networkSelectionStatus = wifiConfig.getNetworkSelectionStatus(); + if (networkSelectionStatus.getNetworkSelectionStatus() != NETWORK_SELECTION_ENABLED) { + if (networkSelectionStatus.getDisableReasonCounter(DISABLED_AUTHENTICATION_FAILURE) > 0 + || networkSelectionStatus.getDisableReasonCounter( + DISABLED_BY_WRONG_PASSWORD) > 0 + || networkSelectionStatus.getDisableReasonCounter( + DISABLED_AUTHENTICATION_NO_CREDENTIALS) > 0) { + return true; + } + } + + return false; + } + @WorkerThread void updateScanResultInfo(@Nullable List scanResults) throws IllegalArgumentException { diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java index 989c764aa..29b638e59 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java @@ -428,6 +428,15 @@ public abstract class WifiEntry implements Comparable { /** Returns the ScanResult information of a WifiEntry */ abstract String getScanResultDescription(); + /** + * In Wi-Fi picker, when users click a saved network, it will connect to the Wi-Fi network. + * However, for some special cases, Wi-Fi picker should show Wi-Fi editor UI for users to edit + * security or password before connecting. Or users will always get connection fail results. + */ + public boolean shouldEditBeforeConnect() { + return false; + } + /** * Sets the callback listener for WifiEntryCallback methods. * Subsequent calls will overwrite the previous listener. diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java index e7d126097..63b096ecc 100644 --- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java +++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardWifiEntryTest.java @@ -16,6 +16,12 @@ package com.android.wifitrackerlib; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_NO_CREDENTIALS; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED; + import static com.android.wifitrackerlib.StandardWifiEntry.ssidAndSecurityToStandardWifiEntryKey; import static com.android.wifitrackerlib.StandardWifiEntry.wifiConfigToStandardWifiEntryKey; import static com.android.wifitrackerlib.TestUtils.buildScanResult; @@ -23,6 +29,7 @@ import static com.android.wifitrackerlib.WifiEntry.CONNECTED_STATE_CONNECTED; import static com.android.wifitrackerlib.WifiEntry.CONNECTED_STATE_DISCONNECTED; import static com.android.wifitrackerlib.WifiEntry.SECURITY_EAP; import static com.android.wifitrackerlib.WifiEntry.SECURITY_NONE; +import static com.android.wifitrackerlib.WifiEntry.SECURITY_OWE; import static com.android.wifitrackerlib.WifiEntry.SECURITY_PSK; import static com.android.wifitrackerlib.WifiEntry.SECURITY_WEP; import static com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_UNREACHABLE; @@ -31,6 +38,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -48,6 +56,7 @@ import android.net.NetworkInfo; import android.net.NetworkScoreManager; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Handler; @@ -664,4 +673,98 @@ public class StandardWifiEntryTest { verify(mMockConnectivityManager, times(1)).startCaptivePortalApp(any()); } + + @Test + public void testShouldEditBeforeConnect_nullWifiConfig_returnFalse() { + StandardWifiEntry entry = new StandardWifiEntry(mMockContext, mTestHandler, + ssidAndSecurityToStandardWifiEntryKey("ssid", SECURITY_EAP), + mMockWifiManager, false /* forSavedNetworksPage */); + + assertThat(entry.shouldEditBeforeConnect()).isFalse(); + } + + @Test + public void testShouldEditBeforeConnect_openNetwork_returnFalse() { + // Test open networks. + WifiConfiguration wifiConfig = new WifiConfiguration(); + wifiConfig.SSID = "\"ssid\""; + wifiConfig.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN); + StandardWifiEntry entry = new StandardWifiEntry(mMockContext, mTestHandler, + ssidAndSecurityToStandardWifiEntryKey("ssid", SECURITY_NONE), + wifiConfig, mMockWifiManager, false /* forSavedNetworksPage */); + + assertThat(entry.shouldEditBeforeConnect()).isFalse(); + + // Test enhanced open networks. + wifiConfig.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE); + entry = new StandardWifiEntry(mMockContext, mTestHandler, + ssidAndSecurityToStandardWifiEntryKey("ssid", SECURITY_OWE), + wifiConfig, mMockWifiManager, false /* forSavedNetworksPage */); + + assertThat(entry.shouldEditBeforeConnect()).isFalse(); + } + + @Test + public void testShouldEditBeforeConnect_securedNetwork_returnTrueIfNeverConnected() { + // Test never connected. + WifiConfiguration wifiConfig = spy(new WifiConfiguration()); + wifiConfig.SSID = "\"ssid\""; + wifiConfig.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + StandardWifiEntry entry = new StandardWifiEntry(mMockContext, mTestHandler, + ssidAndSecurityToStandardWifiEntryKey("ssid", SECURITY_PSK), + wifiConfig, mMockWifiManager, false /* forSavedNetworksPage */); + NetworkSelectionStatus networkSelectionStatus = + spy(new NetworkSelectionStatus.Builder().build()); + doReturn(networkSelectionStatus).when(wifiConfig).getNetworkSelectionStatus(); + + assertThat(entry.shouldEditBeforeConnect()).isTrue(); + + // Test ever connected. + doReturn(true).when(networkSelectionStatus).hasEverConnected(); + + assertThat(entry.shouldEditBeforeConnect()).isFalse(); + } + + @Test + public void testShouldEditBeforeConnect_authenticationFailure_returnTrue() { + // Test DISABLED_AUTHENTICATION_FAILURE. + WifiConfiguration wifiConfig = spy(new WifiConfiguration()); + wifiConfig.SSID = "\"ssid\""; + wifiConfig.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + StandardWifiEntry entry = new StandardWifiEntry(mMockContext, mTestHandler, + ssidAndSecurityToStandardWifiEntryKey("ssid", SECURITY_PSK), + wifiConfig, mMockWifiManager, false /* forSavedNetworksPage */); + NetworkSelectionStatus.Builder statusBuilder = new NetworkSelectionStatus.Builder(); + NetworkSelectionStatus networkSelectionStatus = spy(statusBuilder.setNetworkSelectionStatus( + NETWORK_SELECTION_TEMPORARY_DISABLED) + .setNetworkSelectionDisableReason( + DISABLED_AUTHENTICATION_FAILURE) + .build()); + doReturn(1).when(networkSelectionStatus).getDisableReasonCounter( + DISABLED_AUTHENTICATION_FAILURE); + doReturn(true).when(networkSelectionStatus).hasEverConnected(); + doReturn(networkSelectionStatus).when(wifiConfig).getNetworkSelectionStatus(); + + assertThat(entry.shouldEditBeforeConnect()).isTrue(); + + // Test DISABLED_BY_WRONG_PASSWORD. + networkSelectionStatus = spy(statusBuilder.setNetworkSelectionStatus( + NETWORK_SELECTION_PERMANENTLY_DISABLED) + .setNetworkSelectionDisableReason(DISABLED_BY_WRONG_PASSWORD) + .build()); + doReturn(1).when(networkSelectionStatus).getDisableReasonCounter( + DISABLED_BY_WRONG_PASSWORD); + + assertThat(entry.shouldEditBeforeConnect()).isTrue(); + + // Test DISABLED_AUTHENTICATION_NO_CREDENTIALS. + networkSelectionStatus = spy(statusBuilder.setNetworkSelectionStatus( + NETWORK_SELECTION_PERMANENTLY_DISABLED) + .setNetworkSelectionDisableReason(DISABLED_AUTHENTICATION_NO_CREDENTIALS) + .build()); + doReturn(1).when(networkSelectionStatus).getDisableReasonCounter( + DISABLED_AUTHENTICATION_NO_CREDENTIALS); + + assertThat(entry.shouldEditBeforeConnect()).isTrue(); + } } -- cgit v1.2.3