• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.google.android.downloader;
16 
17 import com.google.auto.value.AutoValue;
18 import com.google.common.collect.ImmutableSet;
19 import java.util.Set;
20 
21 /**
22  * Possible constraints that a download request requires. Primarily used by {@link
23  * ConnectivityHandler} to determine if sufficient connectivity is available.
24  */
25 @AutoValue
26 public abstract class DownloadConstraints {
27   /**
28    * Special value of {@code DownloadConstraints}. If this value is specified, then the {@link
29    * ConnectivityHandler} short-circuits and no constraint checks are performed, even if no network
30    * is present at all!
31    */
32   public static final DownloadConstraints NONE =
33       DownloadConstraints.builder()
34           // Note: Can't use EnumSet because it relies on reflection over the enum class, which
35           // breaks unless the right proguard configuration is applied.
36           .setRequiredNetworkTypes(ImmutableSet.of())
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           // Note: Can't use EnumSet because it relies on reflection over the enum class, which
47           // breaks unless the right proguard configuration is applied.
48           .setRequiredNetworkTypes(ImmutableSet.of(NetworkType.ANY))
49           .setRequireUnmeteredNetwork(true)
50           .build();
51 
52   /**
53    * Common value to indicate that the required network must simply be connected in some way, and
54    * otherwise doesn't have any restrictions. Any network type is allowed.
55    *
56    * <p>This is the default value for download requests.
57    */
58   public static final DownloadConstraints NETWORK_CONNECTED =
59       DownloadConstraints.builder()
60           // Note: Can't use EnumSet because it relies on reflection over the enum class, which
61           // breaks unless the right proguard configuration is applied.
62           .setRequiredNetworkTypes(ImmutableSet.of(NetworkType.ANY))
63           .setRequireUnmeteredNetwork(false)
64           .build();
65 
66   /**
67    * The type of network that is required. This is a subset of the network types enumerated by
68    * {@link android.net.ConnectivityManager}.
69    */
70   public enum NetworkType {
71     /** Special network type to allow any type of network, even if it not one of the known types. */
72     ANY,
73     /** Equivalent to {@link android.net.NetworkCapabilities#TRANSPORT_BLUETOOTH} */
74     BLUETOOTH,
75     /** Equivalent to {@link android.net.NetworkCapabilities#TRANSPORT_ETHERNET} */
76     ETHERNET,
77     /** Equivalent to {@link android.net.NetworkCapabilities#TRANSPORT_CELLULAR} */
78     CELLULAR,
79     /** Equivalent to {@link android.net.NetworkCapabilities#TRANSPORT_WIFI} */
80     WIFI,
81   }
82 
83   /**
84    * Whether the connection must be unmetered to pass connectivity checks. See {@link
85    * androidx.core.net.ConnectivityManagerCompat#isActiveNetworkMetered} for more details on this
86    * variable.
87    *
88    * <p>False by default.
89    */
requireUnmeteredNetwork()90   public abstract boolean requireUnmeteredNetwork();
91 
92   /**
93    * The types of networks that are allowed for the request to pass connectivity checks. The
94    * currently active network type must be one of the values in this set. This set may not be empty.
95    */
requiredNetworkTypes()96   public abstract ImmutableSet<NetworkType> requiredNetworkTypes();
97 
98   /** Creates a {@code DownloadConstraints.Builder} instance. */
builder()99   public static Builder builder() {
100     return new AutoValue_DownloadConstraints.Builder().setRequireUnmeteredNetwork(false);
101   }
102 
103   /** Converts this instance to a builder for modifications. */
toBuilder()104   public abstract Builder toBuilder();
105 
106   /** Builder for creating instances of {@link DownloadConstraints}. */
107   @AutoValue.Builder
108   public abstract static class Builder {
setRequiredNetworkTypes(Set<NetworkType> networkTypes)109     public abstract Builder setRequiredNetworkTypes(Set<NetworkType> networkTypes);
110 
requiredNetworkTypesBuilder()111     abstract ImmutableSet.Builder<NetworkType> requiredNetworkTypesBuilder();
112 
addRequiredNetworkType(NetworkType networkType)113     public Builder addRequiredNetworkType(NetworkType networkType) {
114       requiredNetworkTypesBuilder().add(networkType);
115       return this;
116     }
117 
setRequireUnmeteredNetwork(boolean requireUnmeteredNetwork)118     public abstract Builder setRequireUnmeteredNetwork(boolean requireUnmeteredNetwork);
119 
build()120     public abstract DownloadConstraints build();
121   }
122 }
123