Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7db7ce17 authored by David Su's avatar David Su
Browse files

Expose WifiInfo & NetworkSelectionStatus Builders for SettingsUnitTests

SettingsUnitTests needs to set up WifiInfo &
NetworkSelectionStatus objects in a specific state
and parcel/unparcel them. Create @SystemApi
Builder classes in order to allow
SettingsUnitTests to create these objects while
maintaining the immutability of their APIs.

Bug: 138801922
Test: atest SettingsUnitTests
Change-Id: I40034d08396efc45bd24e16bd788e702366d5dd2
parent da340ae1
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -5918,6 +5918,7 @@ package android.net.wifi {
    method @Deprecated public static boolean isMetered(@Nullable android.net.wifi.WifiConfiguration, @Nullable android.net.wifi.WifiInfo);
    method @Deprecated public boolean isNoInternetAccessExpected();
    method @Deprecated public void setIpConfiguration(@Nullable android.net.IpConfiguration);
    method @Deprecated public void setNetworkSelectionStatus(@NonNull android.net.wifi.WifiConfiguration.NetworkSelectionStatus);
    method @Deprecated public void setProxy(@NonNull android.net.IpConfiguration.ProxySettings, @NonNull android.net.ProxyInfo);
    field @Deprecated public static final int AP_BAND_2GHZ = 0; // 0x0
    field @Deprecated public static final int AP_BAND_5GHZ = 1; // 0x1
@@ -5962,6 +5963,7 @@ package android.net.wifi {
    method @Deprecated public boolean getHasEverConnected();
    method @Deprecated @Nullable public static String getNetworkDisableReasonString(int);
    method @Deprecated public int getNetworkSelectionDisableReason();
    method @Deprecated public int getNetworkSelectionStatus();
    method @Deprecated @NonNull public String getNetworkStatusString();
    method @Deprecated public boolean isNetworkEnabled();
    method @Deprecated public boolean isNetworkPermanentlyDisabled();
@@ -5976,6 +5978,16 @@ package android.net.wifi {
    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_ENABLE = 0; // 0x0
    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
  }
  @Deprecated public static final class WifiConfiguration.NetworkSelectionStatus.Builder {
    ctor @Deprecated public WifiConfiguration.NetworkSelectionStatus.Builder();
    method @Deprecated @NonNull public android.net.wifi.WifiConfiguration.NetworkSelectionStatus build();
    method @Deprecated @NonNull public android.net.wifi.WifiConfiguration.NetworkSelectionStatus.Builder setNetworkSelectionDisableReason(int);
    method @Deprecated @NonNull public android.net.wifi.WifiConfiguration.NetworkSelectionStatus.Builder setNetworkSelectionStatus(int);
  }
  @Deprecated public static class WifiConfiguration.RecentFailure {
@@ -6020,6 +6032,15 @@ package android.net.wifi {
    field public static final int INVALID_RSSI = -127; // 0xffffff81
  }
  public static final class WifiInfo.Builder {
    ctor public WifiInfo.Builder();
    method @NonNull public android.net.wifi.WifiInfo build();
    method @NonNull public android.net.wifi.WifiInfo.Builder setBssid(@NonNull String);
    method @NonNull public android.net.wifi.WifiInfo.Builder setNetworkId(int);
    method @NonNull public android.net.wifi.WifiInfo.Builder setRssi(int);
    method @NonNull public android.net.wifi.WifiInfo.Builder setSsid(@NonNull byte[]);
  }
  public class WifiManager {
    method @RequiresPermission(android.Manifest.permission.WIFI_UPDATE_USABILITY_STATS_SCORE) public void addOnWifiUsabilityStatsListener(@NonNull java.util.concurrent.Executor, @NonNull android.net.wifi.WifiManager.OnWifiUsabilityStatsListener);
    method @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) public void allowAutojoin(int, boolean);
+76 −14
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import com.android.internal.annotations.VisibleForTesting;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
@@ -1208,20 +1210,25 @@ public class WifiConfiguration implements Parcelable {
     */
    @SystemApi
    public static class NetworkSelectionStatus {
        // Quality Network Selection Status enable, temporary disabled, permanently disabled
        /** @hide */
        @Retention(RetentionPolicy.SOURCE)
        @IntDef(prefix = "NETWORK_SELECTION_",
                value = {
                NETWORK_SELECTION_ENABLED,
                NETWORK_SELECTION_TEMPORARY_DISABLED,
                NETWORK_SELECTION_PERMANENTLY_DISABLED})
        public @interface NetworkEnabledStatus {}
        /**
         * This network is allowed to join Quality Network Selection
         * @hide
         * This network will be considered as a potential candidate to connect to during network
         * selection.
         */
        public static final int NETWORK_SELECTION_ENABLED = 0;
        /**
         * network was temporary disabled. Can be re-enabled after a time period expire
         * @hide
         * This network was temporary disabled. May be re-enabled after a time out.
         */
        public static final int NETWORK_SELECTION_TEMPORARY_DISABLED = 1;
        /**
         * network was permanently disabled.
         * @hide
         * This network was permanently disabled.
         */
        public static final int NETWORK_SELECTION_PERMANENTLY_DISABLED = 2;
        /**
@@ -1455,6 +1462,7 @@ public class WifiConfiguration implements Parcelable {
         * Network selection status, should be in one of three status: enable, temporaily disabled
         * or permanently disabled
         */
        @NetworkEnabledStatus
        private int mStatus;

        /**
@@ -1634,6 +1642,56 @@ public class WifiConfiguration implements Parcelable {
            mHasEverConnected = false;
        }

        /**
         * NetworkSelectionStatus exports an immutable public API.
         * However, test code has a need to construct a NetworkSelectionStatus in a specific state.
         * (Note that mocking using Mockito does not work if the object needs to be parceled and
         * unparceled.)
         * Export a @SystemApi Builder to allow tests to construct a NetworkSelectionStatus object
         * in the desired state, without sacrificing NetworkSelectionStatus's immutability.
         */
        @VisibleForTesting
        public static final class Builder {
            private final NetworkSelectionStatus mNetworkSelectionStatus =
                    new NetworkSelectionStatus();

            /**
             * Set the current network selection status.
             * One of:
             * {@link #NETWORK_SELECTION_ENABLED},
             * {@link #NETWORK_SELECTION_TEMPORARY_DISABLED},
             * {@link #NETWORK_SELECTION_PERMANENTLY_DISABLED}
             * @see NetworkSelectionStatus#getNetworkSelectionStatus()
             */
            @NonNull
            public Builder setNetworkSelectionStatus(@NetworkEnabledStatus int status) {
                mNetworkSelectionStatus.setNetworkSelectionStatus(status);
                return this;
            }

            /**
             *
             * Set the current network's disable reason.
             * One of the {@link #NETWORK_SELECTION_ENABLE} or DISABLED_* constants.
             * e.g. {@link #DISABLED_ASSOCIATION_REJECTION}.
             * @see NetworkSelectionStatus#getNetworkSelectionDisableReason()
             */
            @NonNull
            public Builder setNetworkSelectionDisableReason(
                    @NetworkSelectionDisableReason int reason) {
                mNetworkSelectionStatus.setNetworkSelectionDisableReason(reason);
                return this;
            }

            /**
             * Build a NetworkSelectionStatus object.
             */
            @NonNull
            public NetworkSelectionStatus build() {
                return mNetworkSelectionStatus;
            }
        }

        /**
         * Get the network disable reason string for a reason code (for debugging).
         * @param reason specific error reason. One of the {@link #NETWORK_SELECTION_ENABLE} or
@@ -1660,10 +1718,13 @@ public class WifiConfiguration implements Parcelable {
        }

        /**
         * get current network network selection status
         * @return return current network network selection status
         * @hide
         * Get the current network network selection status.
         * One of:
         * {@link #NETWORK_SELECTION_ENABLED},
         * {@link #NETWORK_SELECTION_TEMPORARY_DISABLED},
         * {@link #NETWORK_SELECTION_PERMANENTLY_DISABLED}
         */
        @NetworkEnabledStatus
        public int getNetworkSelectionStatus() {
            return mStatus;
        }
@@ -1965,10 +2026,11 @@ public class WifiConfiguration implements Parcelable {
    }

    /**
     * Set the network selection status
     * Set the network selection status.
     * @hide
     */
    public void setNetworkSelectionStatus(NetworkSelectionStatus status) {
    @SystemApi
    public void setNetworkSelectionStatus(@NonNull NetworkSelectionStatus status) {
        mNetworkSelectionStatus = status;
    }

+69 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.net.wifi;

import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage;
@@ -27,6 +28,8 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.android.internal.annotations.VisibleForTesting;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
@@ -356,6 +359,72 @@ public class WifiInfo implements Parcelable {
        }
    }

    /**
     * WifiInfo exports an immutable public API.
     * However, test code has a need to construct a WifiInfo in a specific state.
     * (Note that mocking using Mockito does not work if the object needs to be parceled and
     * unparceled.)
     * Export a @SystemApi Builder to allow tests to construct a WifiInfo object
     * in the desired state, without sacrificing WifiInfo's immutability.
     *
     * @hide
     */
    // This builder was not made public to reduce confusion for external developers as there are
    // no legitimate uses for this builder except for testing.
    @SystemApi
    @VisibleForTesting
    public static final class Builder {
        private final WifiInfo mWifiInfo = new WifiInfo();

        /**
         * Set the SSID, in the form of a raw byte array.
         * @see WifiInfo#getSSID()
         */
        @NonNull
        public Builder setSsid(@NonNull byte[] ssid) {
            mWifiInfo.setSSID(WifiSsid.createFromByteArray(ssid));
            return this;
        }

        /**
         * Set the BSSID.
         * @see WifiInfo#getBSSID()
         */
        @NonNull
        public Builder setBssid(@NonNull String bssid) {
            mWifiInfo.setBSSID(bssid);
            return this;
        }

        /**
         * Set the RSSI, in dBm.
         * @see WifiInfo#getRssi()
         */
        @NonNull
        public Builder setRssi(int rssi) {
            mWifiInfo.setRssi(rssi);
            return this;
        }

        /**
         * Set the network ID.
         * @see WifiInfo#getNetworkId()
         */
        @NonNull
        public Builder setNetworkId(int networkId) {
            mWifiInfo.setNetworkId(networkId);
            return this;
        }

        /**
         * Build a WifiInfo object.
         */
        @NonNull
        public WifiInfo build() {
            return mWifiInfo;
        }
    }

    /** @hide */
    public void setSSID(WifiSsid wifiSsid) {
        mWifiSsid = wifiSsid;