| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.net.vcn; |
| 17 |
|
| 18 |
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 19 |
import static android.net.vcn.VcnUnderlyingNetworkTemplate.MATCH_ANY; |
| 20 |
|
| 21 |
import static com.android.internal.annotations.VisibleForTesting.Visibility; |
| 22 |
import static com.android.server.vcn.util.PersistableBundleUtils.STRING_DESERIALIZER; |
| 23 |
import static com.android.server.vcn.util.PersistableBundleUtils.STRING_SERIALIZER; |
| 24 |
|
| 25 |
import android.annotation.NonNull; |
| 26 |
import android.annotation.Nullable; |
| 27 |
import android.annotation.SuppressLint; |
| 28 |
import android.net.NetworkCapabilities; |
| 29 |
import android.net.vcn.VcnUnderlyingNetworkTemplate.MatchCriteria; |
| 30 |
import android.os.PersistableBundle; |
| 31 |
import android.util.ArraySet; |
| 32 |
|
| 33 |
import com.android.internal.annotations.VisibleForTesting; |
| 34 |
import com.android.internal.util.IndentingPrintWriter; |
| 35 |
import com.android.server.vcn.util.PersistableBundleUtils; |
| 36 |
|
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.Collections; |
| 39 |
import java.util.Map; |
| 40 |
import java.util.Objects; |
| 41 |
import java.util.Set; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public final class VcnWifiUnderlyingNetworkTemplate extends VcnUnderlyingNetworkTemplate { |
| 50 |
private static final String SSIDS_KEY = "mSsids"; |
| 51 |
@Nullable private final Set<String> mSsids; |
| 52 |
|
| 53 |
private VcnWifiUnderlyingNetworkTemplate( |
| 54 |
int meteredMatchCriteria, |
| 55 |
int minEntryUpstreamBandwidthKbps, |
| 56 |
int minExitUpstreamBandwidthKbps, |
| 57 |
int minEntryDownstreamBandwidthKbps, |
| 58 |
int minExitDownstreamBandwidthKbps, |
| 59 |
Set<String> ssids) { |
| 60 |
super( |
| 61 |
NETWORK_PRIORITY_TYPE_WIFI, |
| 62 |
meteredMatchCriteria, |
| 63 |
minEntryUpstreamBandwidthKbps, |
| 64 |
minExitUpstreamBandwidthKbps, |
| 65 |
minEntryDownstreamBandwidthKbps, |
| 66 |
minExitDownstreamBandwidthKbps); |
| 67 |
mSsids = new ArraySet<>(ssids); |
| 68 |
|
| 69 |
validate(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
@Override |
| 74 |
protected void validate() { |
| 75 |
super.validate(); |
| 76 |
validateSsids(mSsids); |
| 77 |
} |
| 78 |
|
| 79 |
private static void validateSsids(Set<String> ssids) { |
| 80 |
Objects.requireNonNull(ssids, "ssids is null"); |
| 81 |
|
| 82 |
for (String ssid : ssids) { |
| 83 |
Objects.requireNonNull(ssid, "found null value ssid"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
@NonNull |
| 89 |
@VisibleForTesting(visibility = Visibility.PROTECTED) |
| 90 |
public static VcnWifiUnderlyingNetworkTemplate fromPersistableBundle( |
| 91 |
@NonNull PersistableBundle in) { |
| 92 |
Objects.requireNonNull(in, "PersistableBundle is null"); |
| 93 |
|
| 94 |
final int meteredMatchCriteria = in.getInt(METERED_MATCH_KEY); |
| 95 |
|
| 96 |
final int minEntryUpstreamBandwidthKbps = |
| 97 |
in.getInt(MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS_KEY, DEFAULT_MIN_BANDWIDTH_KBPS); |
| 98 |
final int minExitUpstreamBandwidthKbps = |
| 99 |
in.getInt(MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS_KEY, DEFAULT_MIN_BANDWIDTH_KBPS); |
| 100 |
final int minEntryDownstreamBandwidthKbps = |
| 101 |
in.getInt(MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS_KEY, DEFAULT_MIN_BANDWIDTH_KBPS); |
| 102 |
final int minExitDownstreamBandwidthKbps = |
| 103 |
in.getInt(MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS_KEY, DEFAULT_MIN_BANDWIDTH_KBPS); |
| 104 |
|
| 105 |
final PersistableBundle ssidsBundle = in.getPersistableBundle(SSIDS_KEY); |
| 106 |
Objects.requireNonNull(ssidsBundle, "ssidsBundle is null"); |
| 107 |
final Set<String> ssids = |
| 108 |
new ArraySet<String>( |
| 109 |
PersistableBundleUtils.toList(ssidsBundle, STRING_DESERIALIZER)); |
| 110 |
return new VcnWifiUnderlyingNetworkTemplate( |
| 111 |
meteredMatchCriteria, |
| 112 |
minEntryUpstreamBandwidthKbps, |
| 113 |
minExitUpstreamBandwidthKbps, |
| 114 |
minEntryDownstreamBandwidthKbps, |
| 115 |
minExitDownstreamBandwidthKbps, |
| 116 |
ssids); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
@Override |
| 121 |
@NonNull |
| 122 |
@VisibleForTesting(visibility = Visibility.PROTECTED) |
| 123 |
public PersistableBundle toPersistableBundle() { |
| 124 |
final PersistableBundle result = super.toPersistableBundle(); |
| 125 |
|
| 126 |
final PersistableBundle ssidsBundle = |
| 127 |
PersistableBundleUtils.fromList(new ArrayList<>(mSsids), STRING_SERIALIZER); |
| 128 |
result.putPersistableBundle(SSIDS_KEY, ssidsBundle); |
| 129 |
|
| 130 |
return result; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public int hashCode() { |
| 135 |
return Objects.hash(super.hashCode(), mSsids); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public boolean equals(@Nullable Object other) { |
| 140 |
if (!super.equals(other)) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
if (!(other instanceof VcnWifiUnderlyingNetworkTemplate)) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
final VcnWifiUnderlyingNetworkTemplate rhs = (VcnWifiUnderlyingNetworkTemplate) other; |
| 149 |
return mSsids.equals(rhs.mSsids); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
@Override |
| 154 |
void dumpTransportSpecificFields(IndentingPrintWriter pw) { |
| 155 |
if (!mSsids.isEmpty()) { |
| 156 |
pw.println("mSsids: " + mSsids); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
@NonNull |
| 166 |
public Set<String> getSsids() { |
| 167 |
return Collections.unmodifiableSet(mSsids); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
@Override |
| 172 |
public Map<Integer, Integer> getCapabilitiesMatchCriteria() { |
| 173 |
return Collections.singletonMap(NET_CAPABILITY_INTERNET, MATCH_REQUIRED); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
public static final class Builder { |
| 178 |
private int mMeteredMatchCriteria = MATCH_ANY; |
| 179 |
@NonNull private final Set<String> mSsids = new ArraySet<>(); |
| 180 |
|
| 181 |
private int mMinEntryUpstreamBandwidthKbps = DEFAULT_MIN_BANDWIDTH_KBPS; |
| 182 |
private int mMinExitUpstreamBandwidthKbps = DEFAULT_MIN_BANDWIDTH_KBPS; |
| 183 |
private int mMinEntryDownstreamBandwidthKbps = DEFAULT_MIN_BANDWIDTH_KBPS; |
| 184 |
private int mMinExitDownstreamBandwidthKbps = DEFAULT_MIN_BANDWIDTH_KBPS; |
| 185 |
|
| 186 |
|
| 187 |
public Builder() {} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
@SuppressLint("MissingGetterMatchingBuilder") |
| 203 |
@NonNull |
| 204 |
public Builder setMetered(@MatchCriteria int matchCriteria) { |
| 205 |
validateMatchCriteria(matchCriteria, "setMetered"); |
| 206 |
|
| 207 |
mMeteredMatchCriteria = matchCriteria; |
| 208 |
return this; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
@NonNull |
| 218 |
public Builder setSsids(@NonNull Set<String> ssids) { |
| 219 |
validateSsids(ssids); |
| 220 |
|
| 221 |
mSsids.clear(); |
| 222 |
mSsids.addAll(ssids); |
| 223 |
return this; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
@NonNull |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
@SuppressLint("MissingGetterMatchingBuilder") |
| 254 |
public Builder setMinUpstreamBandwidthKbps( |
| 255 |
int minEntryUpstreamBandwidthKbps, int minExitUpstreamBandwidthKbps) { |
| 256 |
validateMinBandwidthKbps(minEntryUpstreamBandwidthKbps, minExitUpstreamBandwidthKbps); |
| 257 |
|
| 258 |
mMinEntryUpstreamBandwidthKbps = minEntryUpstreamBandwidthKbps; |
| 259 |
mMinExitUpstreamBandwidthKbps = minExitUpstreamBandwidthKbps; |
| 260 |
|
| 261 |
return this; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
@NonNull |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
@SuppressLint("MissingGetterMatchingBuilder") |
| 292 |
public Builder setMinDownstreamBandwidthKbps( |
| 293 |
int minEntryDownstreamBandwidthKbps, int minExitDownstreamBandwidthKbps) { |
| 294 |
validateMinBandwidthKbps( |
| 295 |
minEntryDownstreamBandwidthKbps, minExitDownstreamBandwidthKbps); |
| 296 |
|
| 297 |
mMinEntryDownstreamBandwidthKbps = minEntryDownstreamBandwidthKbps; |
| 298 |
mMinExitDownstreamBandwidthKbps = minExitDownstreamBandwidthKbps; |
| 299 |
|
| 300 |
return this; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
@NonNull |
| 305 |
public VcnWifiUnderlyingNetworkTemplate build() { |
| 306 |
return new VcnWifiUnderlyingNetworkTemplate( |
| 307 |
mMeteredMatchCriteria, |
| 308 |
mMinEntryUpstreamBandwidthKbps, |
| 309 |
mMinExitUpstreamBandwidthKbps, |
| 310 |
mMinEntryDownstreamBandwidthKbps, |
| 311 |
mMinExitDownstreamBandwidthKbps, |
| 312 |
mSsids); |
| 313 |
} |
| 314 |
} |
| 315 |
} |