From e6d6949b9aece62bc0a9736c0ad10de0d6ce0565 Mon Sep 17 00:00:00 2001 From: David Su Date: Tue, 4 Feb 2020 17:26:46 -0800 Subject: [PATCH] Wifi: Hide NETWORK_SELECTION_DISABLED_MAX NETWORK_SELECTION_DISABLED_MAX is a constant, but it could change if a new reason is added. Instead expose a static method getMaxNetworkSelectionDisableReason(). Bug: 146046526 Test: atest FrameworksWifiApiTests Change-Id: I2fde8a0207b12d9055ed0cd4b6346b678a2819bf --- api/system-current.txt | 2 +- .../android/net/wifi/WifiConfiguration.java | 26 ++++++++++++++++++- .../net/wifi/WifiConfigurationTest.java | 14 ++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/api/system-current.txt b/api/system-current.txt index 23950295352db..f990b0bf8000c 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -7559,6 +7559,7 @@ package android.net.wifi { @Deprecated public static class WifiConfiguration.NetworkSelectionStatus { method @Deprecated public int getDisableReasonCounter(int); method @Deprecated public long getDisableTime(); + method @Deprecated public static int getMaxNetworkSelectionDisableReason(); method @Deprecated @Nullable public static String getNetworkDisableReasonString(int); method @Deprecated public int getNetworkSelectionDisableReason(); method @Deprecated public int getNetworkSelectionStatus(); @@ -7574,7 +7575,6 @@ package android.net.wifi { field @Deprecated public static final int DISABLED_NONE = 0; // 0x0 field @Deprecated public static final int DISABLED_NO_INTERNET_PERMANENT = 6; // 0x6 field @Deprecated public static final int DISABLED_NO_INTERNET_TEMPORARY = 4; // 0x4 - field @Deprecated public static final int NETWORK_SELECTION_DISABLED_MAX = 10; // 0xa field @Deprecated public static final int NETWORK_SELECTION_ENABLED = 0; // 0x0 field @Deprecated public static final int NETWORK_SELECTION_PERMANENTLY_DISABLED = 2; // 0x2 field @Deprecated public static final int NETWORK_SELECTION_TEMPORARY_DISABLED = 1; // 0x1 diff --git a/wifi/java/android/net/wifi/WifiConfiguration.java b/wifi/java/android/net/wifi/WifiConfiguration.java index 87c2ce9aa3074..1906e249c6d95 100644 --- a/wifi/java/android/net/wifi/WifiConfiguration.java +++ b/wifi/java/android/net/wifi/WifiConfiguration.java @@ -1304,9 +1304,33 @@ public class WifiConfiguration implements Parcelable { public static final int DISABLED_BY_WRONG_PASSWORD = 8; /** This network is disabled because service is not subscribed. */ public static final int DISABLED_AUTHENTICATION_NO_SUBSCRIPTION = 9; - /** All other disable reasons should be strictly less than this value. */ + /** + * All other disable reasons should be strictly less than this value. + * @hide + */ public static final int NETWORK_SELECTION_DISABLED_MAX = 10; + /** + * Get an integer that is equal to the maximum integer value of all the + * DISABLED_* reasons + * e.g. {@link #DISABLED_NONE}, {@link #DISABLED_ASSOCIATION_REJECTION}, etc. + * + * All DISABLED_* constants will be contiguous in the range + * 0, 1, 2, 3, ..., getMaxNetworkSelectionDisableReasons() + * + *
+ * For example, this can be used to iterate through all the network selection + * disable reasons like so: + *
{@code
+         * for (int reason = 0; reason <= getMaxNetworkSelectionDisableReasons(); reason++) {
+         *     ...
+         * }
+         * }
+ */ + public static int getMaxNetworkSelectionDisableReason() { + return NETWORK_SELECTION_DISABLED_MAX - 1; + } + /** * Contains info about disable reasons. * @hide diff --git a/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java b/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java index 8023160a811e4..05a3dce44022a 100644 --- a/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java +++ b/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java @@ -335,6 +335,20 @@ public class WifiConfigurationTest { } } + /** + * Ensure that {@link NetworkSelectionStatus#getMaxNetworkSelectionDisableReason()} returns + * the maximum disable reason. + */ + @Test + public void testNetworkSelectionGetMaxNetworkSelectionDisableReason() { + int maxReason = Integer.MIN_VALUE; + for (int i = 0; i < NetworkSelectionStatus.DISABLE_REASON_INFOS.size(); i++) { + int reason = NetworkSelectionStatus.DISABLE_REASON_INFOS.keyAt(i); + maxReason = Math.max(maxReason, reason); + } + assertEquals(maxReason, NetworkSelectionStatus.getMaxNetworkSelectionDisableReason()); + } + /** * Ensure that {@link WifiConfiguration#setSecurityParams(int)} sets up the * {@link WifiConfiguration} object correctly for SAE security type.