1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.vcn; 18 19 import android.annotation.NonNull; 20 import android.net.NetworkSpecifier; 21 import android.net.TelephonyNetworkSpecifier; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 import com.android.internal.annotations.VisibleForTesting.Visibility; 27 28 import java.util.Arrays; 29 import java.util.Objects; 30 31 /** 32 * NetworkSpecifier object for VCN underlying network requests. 33 * 34 * <p>This matches any underlying network with the appropriate subIds. 35 * 36 * @hide 37 */ 38 public final class VcnUnderlyingNetworkSpecifier extends NetworkSpecifier implements Parcelable { 39 @NonNull private final int[] mSubIds; 40 41 /** 42 * Builds a new VcnUnderlyingNetworkSpecifier with the given list of subIds 43 * 44 * @hide 45 */ VcnUnderlyingNetworkSpecifier(@onNull int[] subIds)46 public VcnUnderlyingNetworkSpecifier(@NonNull int[] subIds) { 47 mSubIds = Objects.requireNonNull(subIds, "subIds were null"); 48 } 49 50 /** 51 * Retrieves the list of subIds supported by this VcnUnderlyingNetworkSpecifier 52 * 53 * @hide 54 */ 55 @NonNull 56 @VisibleForTesting(visibility = Visibility.PRIVATE) getSubIds()57 public int[] getSubIds() { 58 return mSubIds; 59 } 60 61 public static final @NonNull Creator<VcnUnderlyingNetworkSpecifier> CREATOR = 62 new Creator<VcnUnderlyingNetworkSpecifier>() { 63 @Override 64 public VcnUnderlyingNetworkSpecifier createFromParcel(Parcel in) { 65 int[] subIds = in.createIntArray(); 66 return new VcnUnderlyingNetworkSpecifier(subIds); 67 } 68 69 @Override 70 public VcnUnderlyingNetworkSpecifier[] newArray(int size) { 71 return new VcnUnderlyingNetworkSpecifier[size]; 72 } 73 }; 74 75 @Override describeContents()76 public int describeContents() { 77 return 0; 78 } 79 80 @Override writeToParcel(@onNull Parcel dest, int flags)81 public void writeToParcel(@NonNull Parcel dest, int flags) { 82 dest.writeIntArray(mSubIds); 83 } 84 85 @Override hashCode()86 public int hashCode() { 87 return Arrays.hashCode(mSubIds); 88 } 89 90 @Override equals(Object obj)91 public boolean equals(Object obj) { 92 if (this == obj) { 93 return true; 94 } 95 if (!(obj instanceof VcnUnderlyingNetworkSpecifier)) { 96 return false; 97 } 98 99 VcnUnderlyingNetworkSpecifier lhs = (VcnUnderlyingNetworkSpecifier) obj; 100 return Arrays.equals(mSubIds, lhs.mSubIds); 101 } 102 103 @Override toString()104 public String toString() { 105 return new StringBuilder() 106 .append("VcnUnderlyingNetworkSpecifier [") 107 .append("mSubIds = ").append(Arrays.toString(mSubIds)) 108 .append("]") 109 .toString(); 110 } 111 112 /** @hide */ 113 @Override canBeSatisfiedBy(NetworkSpecifier other)114 public boolean canBeSatisfiedBy(NetworkSpecifier other) { 115 if (other instanceof TelephonyNetworkSpecifier) { 116 final int targetSubId = ((TelephonyNetworkSpecifier) other).getSubscriptionId(); 117 for (int subId : mSubIds) { 118 if (targetSubId == subId) { 119 return true; 120 } 121 } 122 return false; 123 } 124 // TODO(b/180140053): Allow matching against WifiNetworkAgentSpecifier 125 126 // MatchAllNetworkSpecifier matched in NetworkCapabilities. 127 return equals(other); 128 } 129 } 130