| 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.TRANSPORT_CELLULAR; |
| 19 |
import static android.net.NetworkCapabilities.TRANSPORT_TEST; |
| 20 |
import static android.net.NetworkCapabilities.TRANSPORT_WIFI; |
| 21 |
|
| 22 |
import static com.android.internal.annotations.VisibleForTesting.Visibility; |
| 23 |
import static com.android.server.vcn.util.PersistableBundleUtils.INTEGER_DESERIALIZER; |
| 24 |
import static com.android.server.vcn.util.PersistableBundleUtils.INTEGER_SERIALIZER; |
| 25 |
|
| 26 |
import android.annotation.IntDef; |
| 27 |
import android.annotation.NonNull; |
| 28 |
import android.annotation.Nullable; |
| 29 |
import android.content.Context; |
| 30 |
import android.net.NetworkCapabilities; |
| 31 |
import android.net.NetworkRequest; |
| 32 |
import android.os.Parcel; |
| 33 |
import android.os.Parcelable; |
| 34 |
import android.os.PersistableBundle; |
| 35 |
import android.util.ArraySet; |
| 36 |
import android.util.Log; |
| 37 |
|
| 38 |
import com.android.internal.annotations.VisibleForTesting; |
| 39 |
import com.android.internal.util.Preconditions; |
| 40 |
import com.android.server.vcn.util.PersistableBundleUtils; |
| 41 |
|
| 42 |
import java.lang.annotation.ElementType; |
| 43 |
import java.lang.annotation.Retention; |
| 44 |
import java.lang.annotation.RetentionPolicy; |
| 45 |
import java.lang.annotation.Target; |
| 46 |
import java.util.ArrayList; |
| 47 |
import java.util.Collections; |
| 48 |
import java.util.Iterator; |
| 49 |
import java.util.Objects; |
| 50 |
import java.util.Set; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public final class VcnConfig implements Parcelable { |
| 61 |
@NonNull private static final String TAG = VcnConfig.class.getSimpleName(); |
| 62 |
|
| 63 |
|
| 64 |
@Retention(RetentionPolicy.SOURCE) |
| 65 |
@IntDef( |
| 66 |
prefix = {"TRANSPORT_"}, |
| 67 |
value = { |
| 68 |
NetworkCapabilities.TRANSPORT_CELLULAR, |
| 69 |
NetworkCapabilities.TRANSPORT_WIFI, |
| 70 |
}) |
| 71 |
@Target({ElementType.TYPE_USE}) |
| 72 |
public @interface VcnUnderlyingNetworkTransport {} |
| 73 |
|
| 74 |
private static final Set<Integer> ALLOWED_TRANSPORTS = new ArraySet<>(); |
| 75 |
|
| 76 |
static { |
| 77 |
ALLOWED_TRANSPORTS.add(TRANSPORT_WIFI); |
| 78 |
ALLOWED_TRANSPORTS.add(TRANSPORT_CELLULAR); |
| 79 |
ALLOWED_TRANSPORTS.add(TRANSPORT_TEST); |
| 80 |
} |
| 81 |
|
| 82 |
private static final String PACKAGE_NAME_KEY = "mPackageName"; |
| 83 |
@NonNull private final String mPackageName; |
| 84 |
|
| 85 |
private static final String GATEWAY_CONNECTION_CONFIGS_KEY = "mGatewayConnectionConfigs"; |
| 86 |
@NonNull private final Set<VcnGatewayConnectionConfig> mGatewayConnectionConfigs; |
| 87 |
|
| 88 |
private static final Set<Integer> RESTRICTED_TRANSPORTS_DEFAULT = |
| 89 |
Collections.singleton(TRANSPORT_WIFI); |
| 90 |
private static final String RESTRICTED_TRANSPORTS_KEY = "mRestrictedTransports"; |
| 91 |
@NonNull private final Set<Integer> mRestrictedTransports; |
| 92 |
|
| 93 |
private static final String IS_TEST_MODE_PROFILE_KEY = "mIsTestModeProfile"; |
| 94 |
private final boolean mIsTestModeProfile; |
| 95 |
|
| 96 |
private VcnConfig( |
| 97 |
@NonNull String packageName, |
| 98 |
@NonNull Set<VcnGatewayConnectionConfig> gatewayConnectionConfigs, |
| 99 |
@NonNull Set<Integer> restrictedTransports, |
| 100 |
boolean isTestModeProfile) { |
| 101 |
mPackageName = packageName; |
| 102 |
mGatewayConnectionConfigs = |
| 103 |
Collections.unmodifiableSet(new ArraySet<>(gatewayConnectionConfigs)); |
| 104 |
mRestrictedTransports = Collections.unmodifiableSet(new ArraySet<>(restrictedTransports)); |
| 105 |
mIsTestModeProfile = isTestModeProfile; |
| 106 |
|
| 107 |
validate(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
@VisibleForTesting(visibility = Visibility.PRIVATE) |
| 116 |
public VcnConfig(@NonNull PersistableBundle in) { |
| 117 |
mPackageName = in.getString(PACKAGE_NAME_KEY); |
| 118 |
|
| 119 |
final PersistableBundle gatewayConnectionConfigsBundle = |
| 120 |
in.getPersistableBundle(GATEWAY_CONNECTION_CONFIGS_KEY); |
| 121 |
mGatewayConnectionConfigs = |
| 122 |
new ArraySet<>( |
| 123 |
PersistableBundleUtils.toList( |
| 124 |
gatewayConnectionConfigsBundle, VcnGatewayConnectionConfig::new)); |
| 125 |
|
| 126 |
final PersistableBundle restrictedTransportsBundle = |
| 127 |
in.getPersistableBundle(RESTRICTED_TRANSPORTS_KEY); |
| 128 |
if (restrictedTransportsBundle == null) { |
| 129 |
|
| 130 |
|
| 131 |
mRestrictedTransports = RESTRICTED_TRANSPORTS_DEFAULT; |
| 132 |
} else { |
| 133 |
mRestrictedTransports = |
| 134 |
new ArraySet<Integer>( |
| 135 |
PersistableBundleUtils.toList( |
| 136 |
restrictedTransportsBundle, INTEGER_DESERIALIZER)); |
| 137 |
} |
| 138 |
|
| 139 |
mIsTestModeProfile = in.getBoolean(IS_TEST_MODE_PROFILE_KEY); |
| 140 |
|
| 141 |
validate(); |
| 142 |
} |
| 143 |
|
| 144 |
private void validate() { |
| 145 |
Objects.requireNonNull(mPackageName, "packageName was null"); |
| 146 |
Preconditions.checkCollectionNotEmpty( |
| 147 |
mGatewayConnectionConfigs, "gatewayConnectionConfigs was empty"); |
| 148 |
|
| 149 |
final Iterator<Integer> iterator = mRestrictedTransports.iterator(); |
| 150 |
while (iterator.hasNext()) { |
| 151 |
final int transport = iterator.next(); |
| 152 |
if (!ALLOWED_TRANSPORTS.contains(transport)) { |
| 153 |
iterator.remove(); |
| 154 |
Log.w( |
| 155 |
TAG, |
| 156 |
"Found invalid transport " |
| 157 |
+ transport |
| 158 |
+ " which might be from a new version of VcnConfig"); |
| 159 |
} |
| 160 |
|
| 161 |
if (transport == TRANSPORT_TEST && !mIsTestModeProfile) { |
| 162 |
throw new IllegalArgumentException( |
| 163 |
"Found TRANSPORT_TEST in a non-test-mode profile"); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
@NonNull |
| 174 |
public String getProvisioningPackageName() { |
| 175 |
return mPackageName; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
@NonNull |
| 180 |
public Set<VcnGatewayConnectionConfig> getGatewayConnectionConfigs() { |
| 181 |
return Collections.unmodifiableSet(mGatewayConnectionConfigs); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
@NonNull |
| 190 |
public Set<@VcnUnderlyingNetworkTransport Integer> getRestrictedUnderlyingNetworkTransports() { |
| 191 |
return Collections.unmodifiableSet(mRestrictedTransports); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public boolean isTestModeProfile() { |
| 200 |
return mIsTestModeProfile; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
@NonNull |
| 209 |
public PersistableBundle toPersistableBundle() { |
| 210 |
final PersistableBundle result = new PersistableBundle(); |
| 211 |
|
| 212 |
result.putString(PACKAGE_NAME_KEY, mPackageName); |
| 213 |
|
| 214 |
final PersistableBundle gatewayConnectionConfigsBundle = |
| 215 |
PersistableBundleUtils.fromList( |
| 216 |
new ArrayList<>(mGatewayConnectionConfigs), |
| 217 |
VcnGatewayConnectionConfig::toPersistableBundle); |
| 218 |
result.putPersistableBundle(GATEWAY_CONNECTION_CONFIGS_KEY, gatewayConnectionConfigsBundle); |
| 219 |
|
| 220 |
final PersistableBundle restrictedTransportsBundle = |
| 221 |
PersistableBundleUtils.fromList( |
| 222 |
new ArrayList<>(mRestrictedTransports), INTEGER_SERIALIZER); |
| 223 |
result.putPersistableBundle(RESTRICTED_TRANSPORTS_KEY, restrictedTransportsBundle); |
| 224 |
|
| 225 |
result.putBoolean(IS_TEST_MODE_PROFILE_KEY, mIsTestModeProfile); |
| 226 |
|
| 227 |
return result; |
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public int hashCode() { |
| 232 |
return Objects.hash( |
| 233 |
mPackageName, mGatewayConnectionConfigs, mRestrictedTransports, mIsTestModeProfile); |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public boolean equals(@Nullable Object other) { |
| 238 |
if (!(other instanceof VcnConfig)) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
final VcnConfig rhs = (VcnConfig) other; |
| 243 |
return mPackageName.equals(rhs.mPackageName) |
| 244 |
&& mGatewayConnectionConfigs.equals(rhs.mGatewayConnectionConfigs) |
| 245 |
&& mRestrictedTransports.equals(rhs.mRestrictedTransports) |
| 246 |
&& mIsTestModeProfile == rhs.mIsTestModeProfile; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
@Override |
| 252 |
public int describeContents() { |
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
@Override |
| 257 |
public void writeToParcel(@NonNull Parcel out, int flags) { |
| 258 |
out.writeParcelable(toPersistableBundle(), flags); |
| 259 |
} |
| 260 |
|
| 261 |
@NonNull |
| 262 |
public static final Parcelable.Creator<VcnConfig> CREATOR = |
| 263 |
new Parcelable.Creator<VcnConfig>() { |
| 264 |
@NonNull |
| 265 |
public VcnConfig createFromParcel(Parcel in) { |
| 266 |
return new VcnConfig((PersistableBundle) in.readParcelable(null, android.os.PersistableBundle.class)); |
| 267 |
} |
| 268 |
|
| 269 |
@NonNull |
| 270 |
public VcnConfig[] newArray(int size) { |
| 271 |
return new VcnConfig[size]; |
| 272 |
} |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
public static final class Builder { |
| 277 |
@NonNull private final String mPackageName; |
| 278 |
|
| 279 |
@NonNull |
| 280 |
private final Set<VcnGatewayConnectionConfig> mGatewayConnectionConfigs = new ArraySet<>(); |
| 281 |
|
| 282 |
@NonNull private final Set<Integer> mRestrictedTransports = new ArraySet<>(); |
| 283 |
|
| 284 |
private boolean mIsTestModeProfile = false; |
| 285 |
|
| 286 |
public Builder(@NonNull Context context) { |
| 287 |
Objects.requireNonNull(context, "context was null"); |
| 288 |
|
| 289 |
mPackageName = context.getOpPackageName(); |
| 290 |
mRestrictedTransports.addAll(RESTRICTED_TRANSPORTS_DEFAULT); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
@NonNull |
| 303 |
public Builder addGatewayConnectionConfig( |
| 304 |
@NonNull VcnGatewayConnectionConfig gatewayConnectionConfig) { |
| 305 |
Objects.requireNonNull(gatewayConnectionConfig, "gatewayConnectionConfig was null"); |
| 306 |
|
| 307 |
for (final VcnGatewayConnectionConfig vcnGatewayConnectionConfig : |
| 308 |
mGatewayConnectionConfigs) { |
| 309 |
if (vcnGatewayConnectionConfig |
| 310 |
.getGatewayConnectionName() |
| 311 |
.equals(gatewayConnectionConfig.getGatewayConnectionName())) { |
| 312 |
throw new IllegalArgumentException( |
| 313 |
"GatewayConnection for specified name already exists"); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
mGatewayConnectionConfigs.add(gatewayConnectionConfig); |
| 318 |
return this; |
| 319 |
} |
| 320 |
|
| 321 |
private void validateRestrictedTransportsOrThrow(Set<Integer> restrictedTransports) { |
| 322 |
Objects.requireNonNull(restrictedTransports, "transports was null"); |
| 323 |
|
| 324 |
for (int transport : restrictedTransports) { |
| 325 |
if (!ALLOWED_TRANSPORTS.contains(transport)) { |
| 326 |
throw new IllegalArgumentException("Invalid transport " + transport); |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
@NonNull |
| 346 |
public Builder setRestrictedUnderlyingNetworkTransports( |
| 347 |
@NonNull Set<@VcnUnderlyingNetworkTransport Integer> transports) { |
| 348 |
validateRestrictedTransportsOrThrow(transports); |
| 349 |
|
| 350 |
mRestrictedTransports.clear(); |
| 351 |
mRestrictedTransports.addAll(transports); |
| 352 |
return this; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
@NonNull |
| 366 |
public Builder setIsTestModeProfile() { |
| 367 |
mIsTestModeProfile = true; |
| 368 |
return this; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
@NonNull |
| 377 |
public VcnConfig build() { |
| 378 |
return new VcnConfig( |
| 379 |
mPackageName, |
| 380 |
mGatewayConnectionConfigs, |
| 381 |
mRestrictedTransports, |
| 382 |
mIsTestModeProfile); |
| 383 |
} |
| 384 |
} |
| 385 |
} |