From b17ea56d30a7264e999f8005142a93ceaea03d5a Mon Sep 17 00:00:00 2001 From: junyulai Date: Fri, 29 Jan 2021 20:20:15 +0800 Subject: [PATCH] [FUI21] Move scrubSubscriberId to utils class This patch also removes the functionality of returning original subscriberId in eng build since it is not really useful. Test: atest NetworkIdentityUtilsTest Bug: 174123988 Change-Id: I721457e571aa9403a2200b3e4261b4895e4f7a3e --- .../app/usage/NetworkStatsManager.java | 4 +-- core/java/android/net/NetworkIdentity.java | 35 ++++--------------- core/java/android/net/NetworkTemplate.java | 5 +-- 3 files changed, 11 insertions(+), 33 deletions(-) diff --git a/core/java/android/app/usage/NetworkStatsManager.java b/core/java/android/app/usage/NetworkStatsManager.java index 1ddfe0d247..1d5dc1d5df 100644 --- a/core/java/android/app/usage/NetworkStatsManager.java +++ b/core/java/android/app/usage/NetworkStatsManager.java @@ -28,7 +28,6 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.DataUsageRequest; import android.net.INetworkStatsService; -import android.net.NetworkIdentity; import android.net.NetworkStack; import android.net.NetworkTemplate; import android.net.netstats.provider.INetworkStatsProviderCallback; @@ -47,6 +46,7 @@ import android.util.DataUnit; import android.util.Log; import com.android.internal.annotations.VisibleForTesting; +import com.android.net.module.util.NetworkIdentityUtils; import java.util.Objects; @@ -628,7 +628,7 @@ public class NetworkStatsManager { default: throw new IllegalArgumentException("Cannot create template for network type " + networkType + ", subscriberId '" - + NetworkIdentity.scrubSubscriberId(subscriberId) + "'."); + + NetworkIdentityUtils.scrubSubscriberId(subscriberId) + "'."); } return template; } diff --git a/core/java/android/net/NetworkIdentity.java b/core/java/android/net/NetworkIdentity.java index b644ed56ad..521ed9b194 100644 --- a/core/java/android/net/NetworkIdentity.java +++ b/core/java/android/net/NetworkIdentity.java @@ -21,11 +21,12 @@ import static android.net.ConnectivityManager.TYPE_WIFI; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; -import android.os.Build; import android.service.NetworkIdentityProto; import android.telephony.Annotation.NetworkType; import android.util.proto.ProtoOutputStream; +import com.android.net.module.util.NetworkIdentityUtils; + import java.util.Objects; /** @@ -89,7 +90,8 @@ public class NetworkIdentity implements Comparable { builder.append(mSubType); } if (mSubscriberId != null) { - builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId)); + builder.append(", subscriberId=") + .append(NetworkIdentityUtils.scrubSubscriberId(mSubscriberId)); } if (mNetworkId != null) { builder.append(", networkId=").append(mNetworkId); @@ -110,7 +112,8 @@ public class NetworkIdentity implements Comparable { // Not dumping mSubType, subtypes are no longer supported. if (mSubscriberId != null) { - proto.write(NetworkIdentityProto.SUBSCRIBER_ID, scrubSubscriberId(mSubscriberId)); + proto.write(NetworkIdentityProto.SUBSCRIBER_ID, + NetworkIdentityUtils.scrubSubscriberId(mSubscriberId)); } proto.write(NetworkIdentityProto.NETWORK_ID, mNetworkId); proto.write(NetworkIdentityProto.ROAMING, mRoaming); @@ -148,32 +151,6 @@ public class NetworkIdentity implements Comparable { return mDefaultNetwork; } - /** - * Scrub given IMSI on production builds. - */ - public static String scrubSubscriberId(String subscriberId) { - if (Build.IS_ENG) { - return subscriberId; - } else if (subscriberId != null) { - // TODO: parse this as MCC+MNC instead of hard-coding - return subscriberId.substring(0, Math.min(6, subscriberId.length())) + "..."; - } else { - return "null"; - } - } - - /** - * Scrub given IMSI on production builds. - */ - public static String[] scrubSubscriberId(String[] subscriberId) { - if (subscriberId == null) return null; - final String[] res = new String[subscriberId.length]; - for (int i = 0; i < res.length; i++) { - res[i] = NetworkIdentity.scrubSubscriberId(subscriberId[i]); - } - return res; - } - /** * Build a {@link NetworkIdentity} from the given {@link NetworkState} and {@code subType}, * assuming that any mobile networks are using the current IMSI. The subType if applicable, diff --git a/core/java/android/net/NetworkTemplate.java b/core/java/android/net/NetworkTemplate.java index 72be835a1c..2322048708 100644 --- a/core/java/android/net/NetworkTemplate.java +++ b/core/java/android/net/NetworkTemplate.java @@ -48,6 +48,7 @@ import android.util.Log; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.ArrayUtils; +import com.android.net.module.util.NetworkIdentityUtils; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; @@ -296,11 +297,11 @@ public class NetworkTemplate implements Parcelable { builder.append("matchRule=").append(getMatchRuleName(mMatchRule)); if (mSubscriberId != null) { builder.append(", subscriberId=").append( - NetworkIdentity.scrubSubscriberId(mSubscriberId)); + NetworkIdentityUtils.scrubSubscriberId(mSubscriberId)); } if (mMatchSubscriberIds != null) { builder.append(", matchSubscriberIds=").append( - Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds))); + Arrays.toString(NetworkIdentityUtils.scrubSubscriberIds(mMatchSubscriberIds))); } if (mNetworkId != null) { builder.append(", networkId=").append(mNetworkId);