diff options
author | 2020-03-18 19:52:26 +0000 | |
---|---|---|
committer | 2020-03-18 19:52:26 +0000 | |
commit | 210bccd974291db2fa38ec4ba21f3f0ac024d658 (patch) | |
tree | dc8a12db55492aa9942004ce4ff7bfcb69a487f0 | |
parent | 4980c0a6cf72fd2ad0b9175e52033afc3a92dc36 (diff) | |
parent | 37bb944836d3c4350abbeb4ce4e149207708a396 (diff) |
Merge "API review: SubscriptionPlan#getNetworkTypes/setNetworkTypes" into rvc-dev
-rw-r--r-- | api/current.txt | 4 | ||||
-rw-r--r-- | core/java/android/telephony/SubscriptionPlan.java | 21 | ||||
-rw-r--r-- | services/core/java/com/android/server/net/NetworkPolicyManagerService.java | 45 |
3 files changed, 46 insertions, 24 deletions
diff --git a/api/current.txt b/api/current.txt index 3d734276d52d..61b427c1d50a 100644 --- a/api/current.txt +++ b/api/current.txt @@ -48110,7 +48110,7 @@ package android.telephony { method public long getDataLimitBytes(); method public long getDataUsageBytes(); method public long getDataUsageTime(); - method @Nullable public int[] getNetworkTypes(); + method @NonNull public int[] getNetworkTypes(); method @Nullable public CharSequence getSummary(); method @Nullable public CharSequence getTitle(); method public void writeToParcel(android.os.Parcel, int); @@ -48130,7 +48130,7 @@ package android.telephony { method public static android.telephony.SubscriptionPlan.Builder createRecurring(java.time.ZonedDateTime, java.time.Period); method public android.telephony.SubscriptionPlan.Builder setDataLimit(long, int); method public android.telephony.SubscriptionPlan.Builder setDataUsage(long, long); - method @NonNull public android.telephony.SubscriptionPlan.Builder setNetworkTypes(@Nullable int[]); + method @NonNull public android.telephony.SubscriptionPlan.Builder setNetworkTypes(@NonNull int[]); method public android.telephony.SubscriptionPlan.Builder setSummary(@Nullable CharSequence); method public android.telephony.SubscriptionPlan.Builder setTitle(@Nullable CharSequence); } diff --git a/core/java/android/telephony/SubscriptionPlan.java b/core/java/android/telephony/SubscriptionPlan.java index ff2f4ad5378c..901957f6ca4a 100644 --- a/core/java/android/telephony/SubscriptionPlan.java +++ b/core/java/android/telephony/SubscriptionPlan.java @@ -91,10 +91,11 @@ public final class SubscriptionPlan implements Parcelable { private long dataUsageBytes = BYTES_UNKNOWN; private long dataUsageTime = TIME_UNKNOWN; private @NetworkType int[] networkTypes; - private long networkTypesBitMask; private SubscriptionPlan(RecurrenceRule cycleRule) { this.cycleRule = Preconditions.checkNotNull(cycleRule); + this.networkTypes = Arrays.copyOf(TelephonyManager.getAllNetworkTypes(), + TelephonyManager.getAllNetworkTypes().length); } private SubscriptionPlan(Parcel source) { @@ -221,10 +222,10 @@ public final class SubscriptionPlan implements Parcelable { /** * Return an array containing all {@link NetworkType}s this SubscriptionPlan applies to. - * A null value means this SubscriptionPlan applies to all network types. + * @see TelephonyManager for network types values */ - public @Nullable @NetworkType int[] getNetworkTypes() { - return networkTypes; + public @NonNull @NetworkType int[] getNetworkTypes() { + return Arrays.copyOf(networkTypes, networkTypes.length); } /** @@ -361,14 +362,14 @@ public final class SubscriptionPlan implements Parcelable { } /** - * Set the network types this SubscriptionPlan applies to. + * Set the network types this SubscriptionPlan applies to. By default the plan will apply + * to all network types. An empty array means this plan applies to no network types. * - * @param networkTypes a set of all {@link NetworkType}s that apply to this plan. - * A null value means the plan applies to all network types, - * and an empty array means the plan applies to no network types. + * @param networkTypes an array of all {@link NetworkType}s that apply to this plan. + * @see TelephonyManager for network type values */ - public @NonNull Builder setNetworkTypes(@Nullable @NetworkType int[] networkTypes) { - plan.networkTypes = networkTypes; + public @NonNull Builder setNetworkTypes(@NonNull @NetworkType int[] networkTypes) { + plan.networkTypes = Arrays.copyOf(networkTypes, networkTypes.length); return this; } } diff --git a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java index caf29146e9db..c5c136377a1a 100644 --- a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java +++ b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java @@ -3087,17 +3087,38 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { private void enforceSubscriptionPlanValidity(SubscriptionPlan[] plans) { // nothing to check if no plans if (plans.length == 0) { + Log.d(TAG, "Received empty plans list. Clearing existing SubscriptionPlans."); return; } - final ArraySet<Integer> applicableNetworkTypes = new ArraySet<Integer>(); - boolean allNetworks = false; - for (SubscriptionPlan plan : plans) { - if (plan.getNetworkTypes() == null) { - allNetworks = true; + final int[] allNetworkTypes = TelephonyManager.getAllNetworkTypes(); + final ArraySet<Integer> allNetworksSet = new ArraySet<>(); + addAll(allNetworksSet, allNetworkTypes); + + final ArraySet<Integer> applicableNetworkTypes = new ArraySet<>(); + boolean hasGeneralPlan = false; + for (int i = 0; i < plans.length; i++) { + final int[] planNetworkTypes = plans[i].getNetworkTypes(); + final ArraySet<Integer> planNetworksSet = new ArraySet<>(); + for (int j = 0; j < planNetworkTypes.length; j++) { + // ensure all network types are valid + if (allNetworksSet.contains(planNetworkTypes[j])) { + // ensure no duplicate network types in the same SubscriptionPlan + if (!planNetworksSet.add(planNetworkTypes[j])) { + throw new IllegalArgumentException( + "Subscription plan contains duplicate network types."); + } + } else { + throw new IllegalArgumentException("Invalid network type: " + + planNetworkTypes[j]); + } + } + + if (planNetworkTypes.length == allNetworkTypes.length) { + hasGeneralPlan = true; } else { - final int[] networkTypes = plan.getNetworkTypes(); - if (!addAll(applicableNetworkTypes, networkTypes)) { + // ensure no network type applies to multiple plans + if (!addAll(applicableNetworkTypes, planNetworkTypes)) { throw new IllegalArgumentException( "Multiple subscription plans defined for a single network type."); } @@ -3105,7 +3126,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { } // ensure at least one plan applies for every network type - if (!allNetworks) { + if (!hasGeneralPlan) { throw new IllegalArgumentException( "No generic subscription plan that applies to all network types."); } @@ -3114,12 +3135,12 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { /** * Adds all of the {@code elements} to the {@code set}. * - * @return {@code false} if any element is not added because the set already have the value. + * @return {@code false} if any element is not added because the set already has the value. */ - private static boolean addAll(@NonNull Set<Integer> set, @NonNull int... elements) { + private static boolean addAll(@NonNull ArraySet<Integer> set, @NonNull int... elements) { boolean result = true; - for (int element : elements) { - result &= set.add(element); + for (int i = 0; i < elements.length; i++) { + result &= set.add(elements[i]); } return result; } |