diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2020-03-02 10:45:22 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-03-02 10:45:22 +0000 |
commit | 45d7afa8d4204462c4a5a5cbd118a97d9127d025 (patch) | |
tree | 942567babca71b8ad891df50eb892377598eb10a /libs | |
parent | bc7f5d85c39ec679143a2598fbd4d4d5822537f1 (diff) | |
parent | ca22c375ef161097d7a55ebf7892111bbaac6bf5 (diff) |
Merge "[WifiTrackerLib] Implement PasspointWifiEntry#getSummary" into rvc-dev
Diffstat (limited to 'libs')
6 files changed, 207 insertions, 93 deletions
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/OsuWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/OsuWifiEntry.java index 97467d559..9aaf346d7 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/OsuWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/OsuWifiEntry.java @@ -86,11 +86,6 @@ class OsuWifiEntry extends WifiEntry { } @Override - public String getSummary() { - return getSummary(true /* concise */); - } - - @Override public String getSummary(boolean concise) { // TODO(b/70983952): Add verbose summary return mOsuStatusString != null diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java index 39055d663..ac7dd14c5 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java @@ -20,8 +20,15 @@ import static android.net.wifi.WifiInfo.sanitizeSsid; import static androidx.core.util.Preconditions.checkNotNull; +import static com.android.wifitrackerlib.Utils.getAutoConnectDescription; import static com.android.wifitrackerlib.Utils.getBestScanResultByLevel; +import static com.android.wifitrackerlib.Utils.getCurrentNetworkCapabilitiesInformation; +import static com.android.wifitrackerlib.Utils.getDisconnectedStateDescription; +import static com.android.wifitrackerlib.Utils.getMeteredDescription; +import static com.android.wifitrackerlib.Utils.getNetworkDetailedState; import static com.android.wifitrackerlib.Utils.getSecurityTypeFromWifiConfiguration; +import static com.android.wifitrackerlib.Utils.getSpeedDescription; +import static com.android.wifitrackerlib.Utils.getVerboseLoggingDescription; import android.content.Context; import android.net.NetworkInfo; @@ -41,6 +48,7 @@ import com.android.internal.annotations.VisibleForTesting; import java.util.ArrayList; import java.util.List; +import java.util.StringJoiner; /** * WifiEntry representation of a subscribed Passpoint network, uniquely identified by FQDN. @@ -102,18 +110,67 @@ public class PasspointWifiEntry extends WifiEntry { } @Override - public String getSummary() { - return getSummary(true /* concise */); - } - - @Override public String getSummary(boolean concise) { if (isExpired()) { return mContext.getString(R.string.wifi_passpoint_expired); } - // TODO(b/70983952): Fill this method in - return "Passpoint (Placeholder Text)"; // Placeholder string + StringJoiner sj = new StringJoiner(mContext.getString(R.string.summary_separator)); + + // TODO(b/70983952): Check if it's necessary to add speend information here. + String speedDescription = getSpeedDescription(mContext, this); + if (!TextUtils.isEmpty(speedDescription)) { + sj.add(speedDescription); + } + + if (getConnectedState() == CONNECTED_STATE_DISCONNECTED) { + String disconnectDescription = getDisconnectedStateDescription(mContext, this); + if (TextUtils.isEmpty(disconnectDescription)) { + if (concise) { + sj.add(mContext.getString(R.string.wifi_disconnected)); + } else if (!mForSavedNetworksPage) { + sj.add(mContext.getString(R.string.wifi_remembered)); + } + } else { + sj.add(disconnectDescription); + } + } else { + String connectDescription = getConnectStateDescription(); + if (!TextUtils.isEmpty(connectDescription)) { + sj.add(connectDescription); + } + } + + String autoConnectDescription = getAutoConnectDescription(mContext, this); + if (!TextUtils.isEmpty(autoConnectDescription)) { + sj.add(autoConnectDescription); + } + + String meteredDescription = getMeteredDescription(mContext, this); + if (!TextUtils.isEmpty(meteredDescription)) { + sj.add(meteredDescription); + } + + if (!concise) { + String verboseLoggingDescription = getVerboseLoggingDescription(this); + if (!TextUtils.isEmpty(verboseLoggingDescription)) { + sj.add(verboseLoggingDescription); + } + } + + return sj.toString(); + } + + private String getConnectStateDescription() { + if (getConnectedState() == CONNECTED_STATE_CONNECTED) { + String networkCapabilitiesinformation = + getCurrentNetworkCapabilitiesInformation(mContext, mNetworkCapabilities); + if (!TextUtils.isEmpty(networkCapabilitiesinformation)) { + return networkCapabilitiesinformation; + } + } + + return getNetworkDetailedState(mContext, mNetworkInfo); } @Override diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java index eab826ed3..1a71efee5 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java @@ -16,8 +16,6 @@ package com.android.wifitrackerlib; -import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED; -import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED; import static android.net.wifi.WifiInfo.sanitizeSsid; import static androidx.core.util.Preconditions.checkNotNull; @@ -26,7 +24,10 @@ import static com.android.wifitrackerlib.Utils.getAppLabel; import static com.android.wifitrackerlib.Utils.getAppLabelForSavedNetwork; import static com.android.wifitrackerlib.Utils.getAutoConnectDescription; import static com.android.wifitrackerlib.Utils.getBestScanResultByLevel; +import static com.android.wifitrackerlib.Utils.getCurrentNetworkCapabilitiesInformation; +import static com.android.wifitrackerlib.Utils.getDisconnectedStateDescription; import static com.android.wifitrackerlib.Utils.getMeteredDescription; +import static com.android.wifitrackerlib.Utils.getNetworkDetailedState; import static com.android.wifitrackerlib.Utils.getSecurityTypeFromWifiConfiguration; import static com.android.wifitrackerlib.Utils.getSpeedDescription; import static com.android.wifitrackerlib.Utils.getVerboseLoggingDescription; @@ -35,7 +36,6 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkCapabilities; import android.net.NetworkInfo; -import android.net.NetworkInfo.DetailedState; import android.net.NetworkScoreManager; import android.net.NetworkScorerAppData; import android.net.wifi.ScanResult; @@ -169,11 +169,6 @@ public class StandardWifiEntry extends WifiEntry { } @Override - public String getSummary() { - return getSummary(true /* concise */); - } - - @Override public String getSummary(boolean concise) { StringJoiner sj = new StringJoiner(mContext.getString(R.string.summary_separator)); @@ -190,7 +185,7 @@ public class StandardWifiEntry extends WifiEntry { } if (getConnectedState() == CONNECTED_STATE_DISCONNECTED) { - String disconnectDescription = getDisconnectedStateDescription(); + String disconnectDescription = getDisconnectedStateDescription(mContext, this); if (TextUtils.isEmpty(disconnectDescription)) { if (concise) { sj.add(mContext.getString(R.string.wifi_disconnected)); @@ -246,76 +241,14 @@ public class StandardWifiEntry extends WifiEntry { return mContext.getString(R.string.connected_via_network_scorer_default); } - // Check NetworkCapabilities. - if (mNetworkCapabilities != null) { - if (mNetworkCapabilities.hasCapability( - NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL)) { - return mContext.getString(mContext.getResources() - .getIdentifier("network_available_sign_in", "string", "android")); - } - - if (mNetworkCapabilities.hasCapability( - NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY)) { - return mContext.getString(R.string.wifi_limited_connection); - } - - if (!mNetworkCapabilities.hasCapability( - NetworkCapabilities.NET_CAPABILITY_VALIDATED)) { - if (mNetworkCapabilities.isPrivateDnsBroken()) { - return mContext.getString(R.string.private_dns_broken); - } - return mContext.getString(R.string.wifi_connected_no_internet); - } + String networkCapabilitiesinformation = + getCurrentNetworkCapabilitiesInformation(mContext, mNetworkCapabilities); + if (!TextUtils.isEmpty(networkCapabilitiesinformation)) { + return networkCapabilitiesinformation; } } - if (mNetworkInfo == null) { - return ""; - } - final DetailedState detailState = mNetworkInfo.getDetailedState(); - if (detailState == null) { - return ""; - } - - final String[] wifiStatusArray = mContext.getResources() - .getStringArray(R.array.wifi_status); - final int index = detailState.ordinal(); - return index >= wifiStatusArray.length ? "" : wifiStatusArray[index]; - } - - private String getDisconnectedStateDescription() { - if (isSaved() && mWifiConfig.hasNoInternetAccess()) { - final int messageID = - mWifiConfig.getNetworkSelectionStatus().getNetworkSelectionStatus() - == NETWORK_SELECTION_PERMANENTLY_DISABLED - ? R.string.wifi_no_internet_no_reconnect : R.string.wifi_no_internet; - return mContext.getString(messageID); - } else if (isSaved() - && (mWifiConfig.getNetworkSelectionStatus().getNetworkSelectionStatus() - != NETWORK_SELECTION_ENABLED)) { - final WifiConfiguration.NetworkSelectionStatus networkStatus = - mWifiConfig.getNetworkSelectionStatus(); - switch (networkStatus.getNetworkSelectionDisableReason()) { - case WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE: - return mContext.getString(R.string.wifi_disabled_password_failure); - case WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD: - return mContext.getString(R.string.wifi_check_password_try_again); - case WifiConfiguration.NetworkSelectionStatus.DISABLED_DHCP_FAILURE: - return mContext.getString(R.string.wifi_disabled_network_failure); - case WifiConfiguration.NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION: - return mContext.getString(R.string.wifi_disabled_generic); - default: - break; - } - } else if (getLevel() == WIFI_LEVEL_UNREACHABLE) { - // Do nothing because users know it by signal icon. - } else { // In range, not disabled. - if (mWifiConfig != null && mWifiConfig.getRecentFailureReason() - == WifiConfiguration.RECENT_FAILURE_AP_UNABLE_TO_HANDLE_NEW_STA) { - return mContext.getString(R.string.wifi_ap_unable_to_handle_new_sta); - } - } - return ""; + return getNetworkDetailedState(mContext, mNetworkInfo); } @Override diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java index 28456efa1..ef658f0cd 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java @@ -16,6 +16,9 @@ package com.android.wifitrackerlib; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED; +import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED; + import static com.android.wifitrackerlib.StandardWifiEntry.ssidAndSecurityToStandardWifiEntryKey; import static com.android.wifitrackerlib.WifiEntry.SECURITY_EAP; import static com.android.wifitrackerlib.WifiEntry.SECURITY_EAP_SUITE_B; @@ -33,6 +36,9 @@ import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.IPackageManager; import android.content.pm.PackageManager; +import android.net.NetworkCapabilities; +import android.net.NetworkInfo; +import android.net.NetworkInfo.DetailedState; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.os.PersistableBundle; @@ -328,9 +334,51 @@ class Utils { } } + static String getDisconnectedStateDescription(Context context, WifiEntry wifiEntry) { + if (context == null || wifiEntry == null) { + return ""; + } + WifiConfiguration wifiConfiguration = wifiEntry.getWifiConfiguration(); + if (wifiConfiguration == null) { + return null; + } + + if (wifiConfiguration.hasNoInternetAccess()) { + int messageID = + wifiConfiguration.getNetworkSelectionStatus().getNetworkSelectionStatus() + == NETWORK_SELECTION_PERMANENTLY_DISABLED + ? R.string.wifi_no_internet_no_reconnect : R.string.wifi_no_internet; + return context.getString(messageID); + } else if (wifiConfiguration.getNetworkSelectionStatus().getNetworkSelectionStatus() + != NETWORK_SELECTION_ENABLED) { + WifiConfiguration.NetworkSelectionStatus networkStatus = + wifiConfiguration.getNetworkSelectionStatus(); + switch (networkStatus.getNetworkSelectionDisableReason()) { + case WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE: + return context.getString(R.string.wifi_disabled_password_failure); + case WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD: + return context.getString(R.string.wifi_check_password_try_again); + case WifiConfiguration.NetworkSelectionStatus.DISABLED_DHCP_FAILURE: + return context.getString(R.string.wifi_disabled_network_failure); + case WifiConfiguration.NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION: + return context.getString(R.string.wifi_disabled_generic); + default: + break; + } + } else if (wifiEntry.getLevel() == WifiEntry.WIFI_LEVEL_UNREACHABLE) { + // Do nothing because users know it by signal icon. + } else { // In range, not disabled. + if (wifiConfiguration.getRecentFailureReason() + == WifiConfiguration.RECENT_FAILURE_AP_UNABLE_TO_HANDLE_NEW_STA) { + return context.getString(R.string.wifi_ap_unable_to_handle_new_sta); + } + } + return ""; + } + static String getAutoConnectDescription(@NonNull Context context, @NonNull WifiEntry wifiEntry) { - if (context == null || wifiEntry == null || !wifiEntry.isSaved()) { + if (context == null || wifiEntry == null || !wifiEntry.canSetAutoJoinEnabled()) { return ""; } @@ -339,8 +387,12 @@ class Utils { } static String getMeteredDescription(@NonNull Context context, @Nullable WifiEntry wifiEntry) { - final WifiConfiguration config = wifiEntry.getWifiConfiguration(); - if (context == null || wifiEntry == null || config == null) { + if (context == null || wifiEntry == null) { + return ""; + } + + if (!wifiEntry.canSetMeteredChoice() + && wifiEntry.getMeteredChoice() != WifiEntry.METERED_CHOICE_METERED) { return ""; } @@ -381,6 +433,46 @@ class Utils { return sj.toString(); } + static String getCurrentNetworkCapabilitiesInformation(Context context, + NetworkCapabilities networkCapabilities) { + if (context == null || networkCapabilities == null) { + return ""; + } + + if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL)) { + return context.getString(context.getResources() + .getIdentifier("network_available_sign_in", "string", "android")); + } + + if (networkCapabilities.hasCapability( + NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY)) { + return context.getString(R.string.wifi_limited_connection); + } + + if (!networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) { + if (networkCapabilities.isPrivateDnsBroken()) { + return context.getString(R.string.private_dns_broken); + } + return context.getString(R.string.wifi_connected_no_internet); + } + return ""; + } + + static String getNetworkDetailedState(Context context, NetworkInfo networkInfo) { + if (context == null || networkInfo == null) { + return ""; + } + DetailedState detailState = networkInfo.getDetailedState(); + if (detailState == null) { + return ""; + } + + String[] wifiStatusArray = context.getResources() + .getStringArray(R.array.wifi_status); + int index = detailState.ordinal(); + return index >= wifiStatusArray.length ? "" : wifiStatusArray[index]; + } + /** * Check if the SIM is present for target carrier Id. */ diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java index 34bac2ab5..ff230e76c 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java @@ -242,7 +242,9 @@ public abstract class WifiEntry implements Comparable<WifiEntry> { public abstract String getTitle(); /** Returns the display summary, it's a concise summary. */ - public abstract String getSummary(); + public String getSummary() { + return getSummary(true /* concise */); + } /** * Returns the display summary. diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java index 0759f5e42..85bad0d39 100644 --- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java +++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/PasspointWifiEntryTest.java @@ -18,11 +18,15 @@ package com.android.wifitrackerlib; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; import android.content.res.Resources; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.hotspot2.PasspointConfiguration; import android.net.wifi.hotspot2.pps.Credential; @@ -43,6 +47,8 @@ public class PasspointWifiEntryTest { private TestLooper mTestLooper; private Handler mTestHandler; + private static final String FQDN = "fqdn"; + @Before public void setUp() { MockitoAnnotations.initMocks(this); @@ -51,6 +57,7 @@ public class PasspointWifiEntryTest { mTestHandler = new Handler(mTestLooper.getLooper()); when(mMockContext.getResources()).thenReturn(mMockResources); + when(mMockResources.getString(R.string.summary_separator)).thenReturn("/"); } @Test @@ -82,7 +89,7 @@ public class PasspointWifiEntryTest { private PasspointConfiguration getPasspointConfiguration() { PasspointConfiguration passpointConfiguration = new PasspointConfiguration(); HomeSp homeSp = new HomeSp(); - homeSp.setFqdn("fqdn"); + homeSp.setFqdn(FQDN); passpointConfiguration.setHomeSp(homeSp); passpointConfiguration.setCredential(new Credential()); return passpointConfiguration; @@ -97,4 +104,32 @@ public class PasspointWifiEntryTest { assertThat(entry.getMeteredChoice()).isEqualTo(WifiEntry.METERED_CHOICE_UNMETERED); } + + @Test + public void testGetSummary_connectedWifiNetwork_showsConnected() { + String summarySeparator = " / "; + String[] wifiStatusArray = new String[]{"", "Scanning", "Connecting", + "Authenticating", "Obtaining IP address", "Connected"}; + + Resources mockResources = mock(Resources.class); + when(mMockContext.getResources()).thenReturn(mockResources); + when(mockResources.getString(R.string.summary_separator)).thenReturn(summarySeparator); + when(mockResources.getStringArray(R.array.wifi_status)).thenReturn(wifiStatusArray); + ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class); + when(mMockContext.getSystemService(Context.CONNECTIVITY_SERVICE)) + .thenReturn(mockConnectivityManager); + + WifiInfo wifiInfo = mock(WifiInfo.class); + when(wifiInfo.isPasspointAp()).thenReturn(true); + when(wifiInfo.getPasspointFqdn()).thenReturn(FQDN); + NetworkInfo networkInfo = + new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", ""); + networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "", ""); + + PasspointWifiEntry entry = new PasspointWifiEntry(mMockContext, mTestHandler, + getPasspointConfiguration(), mMockWifiManager, false /* forSavedNetworksPage */); + entry.updateConnectionInfo(wifiInfo, networkInfo); + + assertThat(entry.getSummary()).isEqualTo("Connected"); + } } |