diff options
author | Arc Wang <arcwang@google.com> | 2020-04-28 01:09:26 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-04-28 01:09:26 +0000 |
commit | 32487070589855d410f68619edc3f04572c2777e (patch) | |
tree | 4acbd18c2f6a8db4adebabdf0f862fd2692ed1d0 /libs | |
parent | fb2e4f6bbecfb571797ec7c64d7c2ac36ee6f62e (diff) | |
parent | 06c4ec9a1638a0b57d68bc023d4ab000f7cdde63 (diff) |
Merge "Add Wi-Fi network selection verbose logging" into rvc-dev
Diffstat (limited to 'libs')
7 files changed, 121 insertions, 1 deletions
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java index d659151c2..9b923fa07 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java @@ -554,4 +554,9 @@ public class PasspointWifiEntry extends WifiEntry { // TODO(b/70983952): Fill this method in. return ""; } + + @Override + String getNetworkSelectionDescription() { + return Utils.getNetworkSelectionDescription(mWifiConfig); + } } diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java index b6afa984a..965a1c21d 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java @@ -852,4 +852,9 @@ public class StandardWifiEntry extends WifiEntry { description.append("}"); return description.toString(); } + + @Override + String getNetworkSelectionDescription() { + return Utils.getNetworkSelectionDescription(getWifiConfiguration()); + } } diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java index 5cdecd48e..00a8dcf30 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java @@ -41,6 +41,7 @@ import android.net.NetworkInfo; import android.net.NetworkInfo.DetailedState; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; import android.os.PersistableBundle; import android.os.RemoteException; import android.os.UserHandle; @@ -52,6 +53,7 @@ import android.text.Annotation; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.TextUtils; +import android.text.format.DateUtils; import android.text.style.ClickableSpan; import android.view.View; @@ -443,9 +445,47 @@ class Utils { sj.add(scanResultsDescription); } + final String networkSelectionDescription = wifiEntry.getNetworkSelectionDescription(); + if (!TextUtils.isEmpty(networkSelectionDescription)) { + sj.add(networkSelectionDescription); + } + return sj.toString(); } + static String getNetworkSelectionDescription(WifiConfiguration wifiConfig) { + if (wifiConfig == null) { + return ""; + } + + StringBuilder description = new StringBuilder(); + NetworkSelectionStatus networkSelectionStatus = wifiConfig.getNetworkSelectionStatus(); + + if (networkSelectionStatus.getNetworkSelectionStatus() != NETWORK_SELECTION_ENABLED) { + description.append(" (" + networkSelectionStatus.getNetworkStatusString()); + if (networkSelectionStatus.getDisableTime() > 0) { + long now = System.currentTimeMillis(); + long elapsedSeconds = (now - networkSelectionStatus.getDisableTime()) / 1000; + description.append(" " + DateUtils.formatElapsedTime(elapsedSeconds)); + } + description.append(")"); + } + + int maxNetworkSelectionDisableReason = + NetworkSelectionStatus.getMaxNetworkSelectionDisableReason(); + for (int reason = 0; reason <= maxNetworkSelectionDisableReason; reason++) { + int disableReasonCounter = networkSelectionStatus.getDisableReasonCounter(reason); + if (disableReasonCounter == 0) { + continue; + } + description.append(" ") + .append(NetworkSelectionStatus.getNetworkSelectionDisableReasonString(reason)) + .append("=") + .append(disableReasonCounter); + } + return description.toString(); + } + static String getCurrentNetworkCapabilitiesInformation(Context context, NetworkCapabilities networkCapabilities) { if (context == null || networkCapabilities == null) { diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java index 989c764aa..59062505f 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiEntry.java @@ -428,6 +428,11 @@ public abstract class WifiEntry implements Comparable<WifiEntry> { /** Returns the ScanResult information of a WifiEntry */ abstract String getScanResultDescription(); + /** Returns the network selection information of a WifiEntry */ + String getNetworkSelectionDescription() { + return ""; + } + /** * Sets the callback listener for WifiEntryCallback methods. * Subsequent calls will overwrite the previous listener. diff --git a/libs/WifiTrackerLib/tests/Android.bp b/libs/WifiTrackerLib/tests/Android.bp index 0abd40148..a9c51e7a8 100644 --- a/libs/WifiTrackerLib/tests/Android.bp +++ b/libs/WifiTrackerLib/tests/Android.bp @@ -22,6 +22,7 @@ android_test { "mockito-target-minus-junit4", "truth-prebuilt", "WifiTrackerLib", + "Robolectric_all-target", ], libs: [ diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java index 4d9121239..0ee26dafa 100644 --- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java +++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java @@ -25,6 +25,7 @@ import static com.android.wifitrackerlib.Utils.getBestScanResultByLevel; import static com.android.wifitrackerlib.Utils.getCarrierNameForSubId; import static com.android.wifitrackerlib.Utils.getImsiProtectionDescription; import static com.android.wifitrackerlib.Utils.getMeteredDescription; +import static com.android.wifitrackerlib.Utils.getNetworkSelectionDescription; import static com.android.wifitrackerlib.Utils.getSubIdForConfig; import static com.android.wifitrackerlib.Utils.isImsiPrivacyProtectionProvided; import static com.android.wifitrackerlib.Utils.isSimPresent; @@ -35,9 +36,10 @@ import static com.android.wifitrackerlib.WifiEntry.SECURITY_PSK; import static com.google.common.truth.Truth.assertThat; - import static org.junit.Assert.*; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; @@ -48,6 +50,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.WifiEnterpriseConfig; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; @@ -64,17 +67,21 @@ import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.ClickableSpan; +import com.android.wifitrackerlib.shadow.ShadowSystem; + import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; +@Config(shadows = {ShadowSystem.class}) public class UtilsTest { private static final String LABEL_AUTO_CONNECTION_DISABLED = "Auto-Connection disabled"; private static final String LABEL_METERED = "Metered"; @@ -416,6 +423,26 @@ public class UtilsTest { ClickableSpan.class).length, 1); } + @Test + public void testGetNetworkSelectionDescription_disabledWrongPassword_showsWrongPasswordLabel() { + String expected = " (NETWORK_SELECTION_TEMPORARY_DISABLED 1:02:03) " + + "NETWORK_SELECTION_DISABLED_BY_WRONG_PASSWORD=2"; + WifiConfiguration wifiConfig = spy(new WifiConfiguration()); + NetworkSelectionStatus.Builder statusBuilder = new NetworkSelectionStatus.Builder(); + NetworkSelectionStatus networkSelectionStatus = spy(statusBuilder.setNetworkSelectionStatus( + NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED) + .setNetworkSelectionDisableReason(NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD) + .build()); + doReturn(2).when(networkSelectionStatus).getDisableReasonCounter( + NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD); + long now = System.currentTimeMillis(); + // Network selection disable time is 1:02:03 ago. + doReturn(now - (60 * 60 + 2 * 60 + 3) * 1000).when(networkSelectionStatus).getDisableTime(); + when(wifiConfig.getNetworkSelectionStatus()).thenReturn(networkSelectionStatus); + + assertThat(getNetworkSelectionDescription(wifiConfig)).isEqualTo(expected); + } + private StandardWifiEntry getStandardWifiEntry(WifiConfiguration config) { final WifiManager mockWifiManager = mock(WifiManager.class); final StandardWifiEntry entry = new StandardWifiEntry(mMockContext, mTestHandler, diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/shadows/ShadowSystem.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/shadows/ShadowSystem.java new file mode 100644 index 000000000..1ef4958ce --- /dev/null +++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/shadows/ShadowSystem.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2020 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.wifitrackerlib.shadow; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +/** + * A shadow object of {@link System}. + */ +@Implements(System.class) +public class ShadowSystem { + /** + * Implements {@link System#currentTimeMillis}. + * + * @return a fixed time milli throughout tests. + */ + @Implementation + public static long currentTimeMillis() { + // It's 2100/01/02 00:00:00. + return 4102531200000L; + } +} |