Add API to get Subscription Id

Bug: 172867224
Test: atest android.net.wifi
Change-Id: I0534a71784060f1613b0b76602e9ed379ab3104e
diff --git a/wifi/api/system-current.txt b/wifi/api/system-current.txt
index 78d7d64..1d2bed8 100644
--- a/wifi/api/system-current.txt
+++ b/wifi/api/system-current.txt
@@ -427,6 +427,7 @@
     method @Nullable public String getRequestingPackageName();
     method public double getRetriedTxPacketsPerSecond();
     method public int getScore();
+    method public int getSubscriptionId();
     method public double getSuccessfulRxPacketsPerSecond();
     method public double getSuccessfulTxPacketsPerSecond();
     method public boolean isCarrierMerged();
diff --git a/wifi/java/android/net/wifi/WifiInfo.java b/wifi/java/android/net/wifi/WifiInfo.java
index 80f3c24..6bd1ce5 100644
--- a/wifi/java/android/net/wifi/WifiInfo.java
+++ b/wifi/java/android/net/wifi/WifiInfo.java
@@ -25,6 +25,7 @@
 import android.os.Build;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.telephony.SubscriptionManager;
 import android.text.TextUtils;
 
 import com.android.modules.utils.build.SdkLevel;
@@ -196,6 +197,11 @@
     private String mRequestingPackageName;
 
     /**
+     * Identify which Telephony subscription provides this network.
+     */
+    private int mSubscriptionId;
+
+    /**
      * Running total count of lost (not ACKed) transmitted unicast data packets.
      * @hide
      */
@@ -321,6 +327,7 @@
         mRssi = INVALID_RSSI;
         mLinkSpeed = LINK_SPEED_UNKNOWN;
         mFrequency = -1;
+        mSubscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
     }
 
     /** @hide */
@@ -347,6 +354,7 @@
         setFQDN(null);
         setProviderFriendlyName(null);
         setPasspointUniqueId(null);
+        setSubscriptionId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
         txBad = 0;
         txSuccess = 0;
         rxSuccess = 0;
@@ -386,6 +394,7 @@
             mOsuAp = source.mOsuAp;
             mFqdn = source.mFqdn;
             mProviderFriendlyName = source.mProviderFriendlyName;
+            mSubscriptionId = source.mSubscriptionId;
             txBad = source.txBad;
             txRetries = source.txRetries;
             txSuccess = source.txSuccess;
@@ -874,6 +883,28 @@
         return mRequestingPackageName;
     }
 
+    /** {@hide} */
+    public void setSubscriptionId(int subId) {
+        mSubscriptionId = subId;
+    }
+
+    /**
+     * If this network is provisioned by a carrier, returns subscription Id corresponding to the
+     * associated SIM on the device. If this network is not provisioned by a carrier, returns
+     * {@link android.telephony.SubscriptionManager#INVALID_SUBSCRIPTION_ID}
+     *
+     * @see WifiNetworkSuggestion.Builder#setSubscriptionId(int)
+     * @see android.telephony.SubscriptionInfo#getSubscriptionId()
+     * {@hide}
+     */
+    @SystemApi
+    public int getSubscriptionId() {
+        if (!SdkLevel.isAtLeastS()) {
+            throw new UnsupportedOperationException();
+        }
+        return mSubscriptionId;
+    }
+
 
     /** @hide */
     @UnsupportedAppUsage
@@ -1064,6 +1095,7 @@
         dest.writeInt(mMaxSupportedTxLinkSpeed);
         dest.writeInt(mMaxSupportedRxLinkSpeed);
         dest.writeString(mPasspointUniqueId);
+        dest.writeInt(mSubscriptionId);
     }
 
     /** Implement the Parcelable interface {@hide} */
@@ -1112,6 +1144,7 @@
                 info.mMaxSupportedTxLinkSpeed = in.readInt();
                 info.mMaxSupportedRxLinkSpeed = in.readInt();
                 info.mPasspointUniqueId = in.readString();
+                info.mSubscriptionId = in.readInt();
                 return info;
             }
 
diff --git a/wifi/tests/src/android/net/wifi/WifiInfoTest.java b/wifi/tests/src/android/net/wifi/WifiInfoTest.java
index d3d5bfd..33438ed 100644
--- a/wifi/tests/src/android/net/wifi/WifiInfoTest.java
+++ b/wifi/tests/src/android/net/wifi/WifiInfoTest.java
@@ -23,6 +23,7 @@
 import static org.junit.Assert.assertTrue;
 
 import android.os.Parcel;
+import android.telephony.SubscriptionManager;
 
 import androidx.test.filters.SmallTest;
 
@@ -50,6 +51,7 @@
     private static final int TEST_RSSI = -60;
     private static final int TEST_NETWORK_ID = 5;
     private static final int TEST_NETWORK_ID2 = 6;
+    private static final int TEST_SUB_ID = 1;
 
     /**
      *  Verify parcel write/read with WifiInfo.
@@ -72,6 +74,7 @@
         writeWifiInfo.setWifiStandard(TEST_WIFI_STANDARD);
         writeWifiInfo.setMaxSupportedTxLinkSpeedMbps(TEST_MAX_SUPPORTED_TX_LINK_SPEED_MBPS);
         writeWifiInfo.setMaxSupportedRxLinkSpeedMbps(TEST_MAX_SUPPORTED_RX_LINK_SPEED_MBPS);
+        writeWifiInfo.setSubscriptionId(TEST_SUB_ID);
 
         Parcel parcel = Parcel.obtain();
         writeWifiInfo.writeToParcel(parcel, 0);
@@ -98,6 +101,7 @@
                 readWifiInfo.getMaxSupportedTxLinkSpeedMbps());
         assertEquals(TEST_MAX_SUPPORTED_RX_LINK_SPEED_MBPS,
                 readWifiInfo.getMaxSupportedRxLinkSpeedMbps());
+        assertEquals(TEST_SUB_ID, readWifiInfo.getSubscriptionId());
     }
 
     @Test
@@ -118,6 +122,7 @@
         writeWifiInfo.setWifiStandard(TEST_WIFI_STANDARD);
         writeWifiInfo.setMaxSupportedTxLinkSpeedMbps(TEST_MAX_SUPPORTED_TX_LINK_SPEED_MBPS);
         writeWifiInfo.setMaxSupportedRxLinkSpeedMbps(TEST_MAX_SUPPORTED_RX_LINK_SPEED_MBPS);
+        writeWifiInfo.setSubscriptionId(TEST_SUB_ID);
 
         WifiInfo readWifiInfo = new WifiInfo(writeWifiInfo);
 
@@ -139,6 +144,7 @@
                 readWifiInfo.getMaxSupportedTxLinkSpeedMbps());
         assertEquals(TEST_MAX_SUPPORTED_RX_LINK_SPEED_MBPS,
                 readWifiInfo.getMaxSupportedRxLinkSpeedMbps());
+        assertEquals(TEST_SUB_ID, readWifiInfo.getSubscriptionId());
     }
 
     /**
@@ -159,6 +165,7 @@
         assertFalse(wifiInfo.isOemPaid());
         assertFalse(wifiInfo.isOemPrivate());
         assertFalse(wifiInfo.isCarrierMerged());
+        assertEquals(SubscriptionManager.INVALID_SUBSCRIPTION_ID, wifiInfo.getSubscriptionId());
     }
 
     /**