From 64c6943777eb720f2504791015eea54bc0d3cc20 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Tue, 4 Jan 2022 10:23:47 +0000 Subject: [PATCH] [MS09] Expose NetworkTemplate APIs which are needed by NetworkPolicy This change also remove getWifiNetworkKey which should not be exposed as system API. Test: atest NetworkTemplateTest Bug: 204830222 Change-Id: I924f62025a8672167f778ff790cd208c74eed878 --- core/api/module-lib-current.txt | 37 +++++++++++++++++++ core/api/system-current.txt | 4 ++ core/java/android/net/NetworkPolicy.java | 3 +- .../src/android/net/NetworkStats.java | 23 ++---------- .../src/android/net/NetworkTemplate.java | 23 +++++------- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt index 6343c67cebea9..d13170bf4a35d 100644 --- a/core/api/module-lib-current.txt +++ b/core/api/module-lib-current.txt @@ -237,6 +237,43 @@ package android.net { field @NonNull public static final android.os.Parcelable.Creator CREATOR; } + public final class NetworkTemplate implements android.os.Parcelable { + method public int describeContents(); + method public int getDefaultNetworkStatus(); + method public int getMatchRule(); + method public int getMeteredness(); + method public int getOemManaged(); + method public int getRatType(); + method public int getRoaming(); + method @NonNull public java.util.Set getSubscriberIds(); + method @NonNull public java.util.Set getWifiNetworkKeys(); + method public void writeToParcel(@NonNull android.os.Parcel, int); + field @NonNull public static final android.os.Parcelable.Creator CREATOR; + field public static final int MATCH_BLUETOOTH = 8; // 0x8 + field public static final int MATCH_CARRIER = 10; // 0xa + field public static final int MATCH_ETHERNET = 5; // 0x5 + field public static final int MATCH_MOBILE = 1; // 0x1 + field public static final int MATCH_WIFI = 4; // 0x4 + field public static final int NETWORK_TYPE_ALL = -1; // 0xffffffff + field public static final int OEM_MANAGED_ALL = -1; // 0xffffffff + field public static final int OEM_MANAGED_NO = 0; // 0x0 + field public static final int OEM_MANAGED_PAID = 1; // 0x1 + field public static final int OEM_MANAGED_PRIVATE = 2; // 0x2 + field public static final int OEM_MANAGED_YES = -2; // 0xfffffffe + } + + public static final class NetworkTemplate.Builder { + ctor public NetworkTemplate.Builder(int); + method @NonNull public android.net.NetworkTemplate build(); + method @NonNull public android.net.NetworkTemplate.Builder setDefaultNetworkStatus(int); + method @NonNull public android.net.NetworkTemplate.Builder setMeteredness(int); + method @NonNull public android.net.NetworkTemplate.Builder setOemManaged(int); + method @NonNull public android.net.NetworkTemplate.Builder setRatType(int); + method @NonNull public android.net.NetworkTemplate.Builder setRoaming(int); + method @NonNull public android.net.NetworkTemplate.Builder setSubscriberIds(@NonNull java.util.Set); + method @NonNull public android.net.NetworkTemplate.Builder setWifiNetworkKeys(@NonNull java.util.Set); + } + public class NetworkWatchlistManager { method @Nullable public byte[] getWatchlistConfigHash(); } diff --git a/core/api/system-current.txt b/core/api/system-current.txt index 6321aaa225b4c..8ce48ab8ce244 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -7508,13 +7508,17 @@ package android.net { method @NonNull public android.net.NetworkStats subtract(@NonNull android.net.NetworkStats); method public void writeToParcel(@NonNull android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; + field public static final int DEFAULT_NETWORK_ALL = -1; // 0xffffffff field public static final int DEFAULT_NETWORK_NO = 0; // 0x0 field public static final int DEFAULT_NETWORK_YES = 1; // 0x1 field public static final String IFACE_VT = "vt_data0"; + field public static final int METERED_ALL = -1; // 0xffffffff field public static final int METERED_NO = 0; // 0x0 field public static final int METERED_YES = 1; // 0x1 + field public static final int ROAMING_ALL = -1; // 0xffffffff field public static final int ROAMING_NO = 0; // 0x0 field public static final int ROAMING_YES = 1; // 0x1 + field public static final int SET_ALL = -1; // 0xffffffff field public static final int SET_DEFAULT = 0; // 0x0 field public static final int SET_FOREGROUND = 1; // 0x1 field public static final int TAG_NONE = 0; // 0x0 diff --git a/core/java/android/net/NetworkPolicy.java b/core/java/android/net/NetworkPolicy.java index 8b3d49ef49642..ab1f5420fb3fe 100644 --- a/core/java/android/net/NetworkPolicy.java +++ b/core/java/android/net/NetworkPolicy.java @@ -339,7 +339,8 @@ public class NetworkPolicy implements Parcelable, Comparable { out.writeInt(template.getMatchRule()); BackupUtils.writeString(out, template.getSubscriberIds().iterator().next()); - BackupUtils.writeString(out, template.getWifiNetworkKey()); + BackupUtils.writeString(out, template.getWifiNetworkKeys().isEmpty() + ? null : template.getWifiNetworkKeys().iterator().next()); out.writeInt(template.getMeteredness()); return baos.toByteArray(); diff --git a/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java b/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java index 181a594cbcf9f..b00fea4de2692 100644 --- a/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java +++ b/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java @@ -83,10 +83,7 @@ public final class NetworkStats implements Parcelable { */ // TODO: Rename TAG_ALL to TAG_ANY. public static final int TAG_ALL = -1; - /** - * {@link #set} value for all sets combined, not including debug sets. - * @hide - */ + /** {@link #set} value for all sets combined, not including debug sets. */ public static final int SET_ALL = -1; /** {@link #set} value where background data is accounted. */ public static final int SET_DEFAULT = 0; @@ -114,9 +111,6 @@ public final class NetworkStats implements Parcelable { SET_ALL, SET_DEFAULT, SET_FOREGROUND, - SET_DEBUG_START, - SET_DBG_VPN_IN, - SET_DBG_VPN_OUT }) public @interface State { } @@ -131,10 +125,7 @@ public final class NetworkStats implements Parcelable { // TODO: Rename TAG_NONE to TAG_ALL. public static final int TAG_NONE = 0; - /** - * {@link #metered} value to account for all metered states. - * @hide - */ + /** {@link #metered} value to account for all metered states. */ public static final int METERED_ALL = -1; /** {@link #metered} value where native, unmetered data is accounted. */ public static final int METERED_NO = 0; @@ -152,10 +143,7 @@ public final class NetworkStats implements Parcelable { } - /** - * {@link #roaming} value to account for all roaming states. - * @hide - */ + /** {@link #roaming} value to account for all roaming states. */ public static final int ROAMING_ALL = -1; /** {@link #roaming} value where native, non-roaming data is accounted. */ public static final int ROAMING_NO = 0; @@ -172,10 +160,7 @@ public final class NetworkStats implements Parcelable { public @interface Roaming { } - /** - * {@link #onDefaultNetwork} value to account for all default network states. - * @hide - */ + /** {@link #onDefaultNetwork} value to account for all default network states. */ public static final int DEFAULT_NETWORK_ALL = -1; /** {@link #onDefaultNetwork} value to account for usage while not the default network. */ public static final int DEFAULT_NETWORK_NO = 0; diff --git a/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java b/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java index 659ad06039b80..e9084b019668d 100644 --- a/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java +++ b/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java @@ -16,6 +16,7 @@ package android.net; +import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; import static android.net.ConnectivityManager.TYPE_BLUETOOTH; import static android.net.ConnectivityManager.TYPE_ETHERNET; import static android.net.ConnectivityManager.TYPE_MOBILE; @@ -39,7 +40,9 @@ import static android.net.NetworkStats.ROAMING_YES; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; +import android.annotation.SystemApi; import android.compat.annotation.UnsupportedAppUsage; +import android.net.wifi.WifiInfo; import android.os.Build; import android.os.Parcel; import android.os.Parcelable; @@ -70,7 +73,7 @@ import java.util.TreeSet; * * @hide */ -// @SystemApi(client = MODULE_LIBRARIES) +@SystemApi(client = MODULE_LIBRARIES) public final class NetworkTemplate implements Parcelable { /** @hide */ @Retention(RetentionPolicy.SOURCE) @@ -572,6 +575,7 @@ public final class NetworkTemplate implements Parcelable { /** * Get subscriber Id of the template. + * @hide */ @Nullable @UnsupportedAppUsage @@ -588,26 +592,19 @@ public final class NetworkTemplate implements Parcelable { } /** - * Get Wifi Network Key of the template. See {@link WifiInfo#getCurrentNetworkKey()}. + * Get the set of Wifi Network Keys of the template. + * See {@link WifiInfo#getCurrentNetworkKey()}. */ - @Nullable - public String getWifiNetworkKey() { - return CollectionUtils.isEmpty(mMatchWifiNetworkKeys) ? null : mMatchWifiNetworkKeys[0]; - } - - /** - * Get set of Wifi Network Keys of the template. - */ - @Nullable + @NonNull public Set getWifiNetworkKeys() { return new ArraySet<>(Arrays.asList(mMatchWifiNetworkKeys)); } /** @hide */ - // TODO: Remove this and replace all callers with {@link #getWifiNetworkKey()}. + // TODO: Remove this and replace all callers with {@link #getWifiNetworkKeys()}. @Nullable public String getNetworkId() { - return getWifiNetworkKey(); + return getWifiNetworkKeys().isEmpty() ? null : getWifiNetworkKeys().iterator().next(); } /**