1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.downloader; 17 18 import com.google.auto.value.AutoValue; 19 import com.google.common.collect.ImmutableSet; 20 import com.google.errorprone.annotations.CanIgnoreReturnValue; 21 import java.util.EnumSet; 22 import java.util.Set; 23 24 /** Possible constraints that a download request requires. */ 25 @AutoValue 26 public abstract class DownloadConstraints { 27 DownloadConstraints()28 DownloadConstraints() {} 29 30 /** 31 * Special value of {@code DownloadConstraints}. If this value is specified, no constraint checks 32 * are performed, even if no network is present at all! 33 */ 34 public static final DownloadConstraints NONE = 35 DownloadConstraints.builder() 36 .setRequiredNetworkTypes(EnumSet.noneOf(NetworkType.class)) 37 .setRequireUnmeteredNetwork(false) 38 .build(); 39 40 /** 41 * Common value to indicate that the active network must be unmetered. This value permits any 42 * network type as long as it doesn't indicate it is metered in some way. 43 */ 44 public static final DownloadConstraints NETWORK_UNMETERED = 45 DownloadConstraints.builder() 46 .setRequiredNetworkTypes(EnumSet.of(NetworkType.ANY)) 47 .setRequireUnmeteredNetwork(true) 48 .build(); 49 50 /** 51 * Common value to indicate that the required network must simply be connected in some way, and 52 * otherwise doesn't have any restrictions. Any network type is allowed. 53 * 54 * <p>This is the default value for download requests. 55 */ 56 public static final DownloadConstraints NETWORK_CONNECTED = 57 DownloadConstraints.builder() 58 .setRequiredNetworkTypes(EnumSet.of(NetworkType.ANY)) 59 .setRequireUnmeteredNetwork(false) 60 .build(); 61 62 /** 63 * The type of network that is required. This is a subset of the network types enumerated by 64 * {@link android.net.ConnectivityManager}. 65 */ 66 public enum NetworkType { 67 /** Special network type to allow any type of network, even if it not one of the known types. */ 68 ANY, 69 /** Equivalent to {@link android.net.NetworkCapabilities#TRANSPORT_BLUETOOTH} */ 70 BLUETOOTH, 71 /** Equivalent to {@link android.net.NetworkCapabilities#TRANSPORT_ETHERNET} */ 72 ETHERNET, 73 /** Equivalent to {@link android.net.NetworkCapabilities#TRANSPORT_CELLULAR} */ 74 CELLULAR, 75 /** Equivalent to {@link android.net.NetworkCapabilities#TRANSPORT_WIFI} */ 76 WIFI, 77 } 78 79 /** 80 * Whether the connection must be unmetered to pass connectivity checks. See {@link 81 * androidx.core.net.ConnectivityManagerCompat#isActiveNetworkMetered} for more details on this 82 * variable. 83 * 84 * <p>False by default. 85 */ requireUnmeteredNetwork()86 public abstract boolean requireUnmeteredNetwork(); 87 88 /** 89 * The types of networks that are allowed for the request to pass connectivity checks. The 90 * currently active network type must be one of the values in this set. This set may not be empty. 91 */ requiredNetworkTypes()92 public abstract ImmutableSet<NetworkType> requiredNetworkTypes(); 93 94 /** Creates a {@code DownloadConstraints.Builder} instance. */ builder()95 public static Builder builder() { 96 return new AutoValue_DownloadConstraints.Builder().setRequireUnmeteredNetwork(false); 97 } 98 99 /** Converts this instance to a builder for modifications. */ toBuilder()100 public abstract Builder toBuilder(); 101 102 /** Builder for creating instances of {@link DownloadConstraints}. */ 103 @AutoValue.Builder 104 public abstract static class Builder { Builder()105 Builder() {} 106 setRequiredNetworkTypes(Set<NetworkType> networkTypes)107 public abstract Builder setRequiredNetworkTypes(Set<NetworkType> networkTypes); 108 requiredNetworkTypesBuilder()109 abstract ImmutableSet.Builder<NetworkType> requiredNetworkTypesBuilder(); 110 111 @CanIgnoreReturnValue addRequiredNetworkType(NetworkType networkType)112 public final Builder addRequiredNetworkType(NetworkType networkType) { 113 requiredNetworkTypesBuilder().add(networkType); 114 return this; 115 } 116 setRequireUnmeteredNetwork(boolean requireUnmeteredNetwork)117 public abstract Builder setRequireUnmeteredNetwork(boolean requireUnmeteredNetwork); 118 build()119 public abstract DownloadConstraints build(); 120 } 121 } 122