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 static com.google.android.libraries.mobiledatadownload.internal.MddConstants.INLINE_FILE_URL_SCHEME; 19 import static com.google.common.base.Preconditions.checkArgument; 20 21 import android.net.Uri; 22 import android.util.Pair; 23 import com.google.auto.value.AutoValue; 24 import com.google.common.base.Optional; 25 import com.google.common.collect.ImmutableList; 26 import javax.annotation.concurrent.Immutable; 27 28 /** Request to download a file. */ 29 @Immutable 30 @AutoValue 31 public abstract class DownloadRequest { 32 33 // Default value for Traffic Tag if not set by clients. 34 // MDD will not tag the traffic if the TrafficTag is not set to a valid value (>0). 35 private static final int UNSPECIFIED_TRAFFIC_TAG = -1; 36 DownloadRequest()37 DownloadRequest() {} 38 39 /** The File Uri to download the file at. */ fileUri()40 public abstract Uri fileUri(); 41 42 /** The url to download the file from. */ urlToDownload()43 public abstract String urlToDownload(); 44 45 /** 46 * Conditions under which this file should be downloaded. 47 * 48 * <p>These conditions relate to the type of network that should be used when downloading and must 49 * be provided when performing a network download. 50 * 51 * <p>When performing in-memory downloads (using the "inlinefile" url scheme), this will not be 52 * used. 53 */ downloadConstraints()54 public abstract DownloadConstraints downloadConstraints(); 55 56 /** 57 * Traffic tag used for this request. 58 * 59 * <p>If not set, it will take the default value of UNSPECIFIED_TRAFFIC_TAG and MDD will not tag 60 * the traffic. 61 */ trafficTag()62 public abstract int trafficTag(); 63 64 /** The extra HTTP headers for this request. */ extraHttpHeaders()65 public abstract ImmutableList<Pair<String, String>> extraHttpHeaders(); 66 67 /** 68 * Parameters for inline file downloads. 69 * 70 * <p>An instance of {@link InlineDownloadParams} must be included in the request to support 71 * in-memory downloads (see <internal> for more info on the "inlinefile" url scheme). 72 * 73 * <p>Implementations of {@link FileDownloader} that support downloading from an inline file 74 * should ensure that 1) the "inlinefile" url scheme is used and 2) an {@link 75 * InlineDownloadParams} is provided. 76 */ inlineDownloadParamsOptional()77 public abstract Optional<InlineDownloadParams> inlineDownloadParamsOptional(); 78 newBuilder()79 public static Builder newBuilder() { 80 return new AutoValue_DownloadRequest.Builder() 81 .setTrafficTag(UNSPECIFIED_TRAFFIC_TAG) 82 .setExtraHttpHeaders(ImmutableList.of()); 83 } 84 85 /** Builder for {@link DownloadRequest}. */ 86 @AutoValue.Builder 87 public abstract static class Builder { Builder()88 Builder() {} 89 90 /** Sets the on-device destination uri of the file. */ setFileUri(Uri fileUri)91 public abstract Builder setFileUri(Uri fileUri); 92 93 /** Sets the url from where file content should be downloaded. */ setUrlToDownload(String urlToDownload)94 public abstract Builder setUrlToDownload(String urlToDownload); 95 96 /** 97 * Sets the network constraints that should be used for the download. 98 * 99 * <p>Only required when performing network downloads. If performing an in-memory download, this 100 * is not required. 101 */ setDownloadConstraints(DownloadConstraints downloadConstraints)102 public abstract Builder setDownloadConstraints(DownloadConstraints downloadConstraints); 103 104 /** Sets the traffic tag for this request. */ setTrafficTag(int trafficTag)105 public abstract Builder setTrafficTag(int trafficTag); 106 107 /** Sets the extra HTTP headers for this request. */ setExtraHttpHeaders( ImmutableList<Pair<String, String>> extraHttpHeaders)108 public abstract Builder setExtraHttpHeaders( 109 ImmutableList<Pair<String, String>> extraHttpHeaders); 110 111 /** 112 * Sets the parameters for an inline file download. 113 * 114 * <p>Only required when performing an in-memory download (using an "inlinefile" url scheme). If 115 * performing a network download, this is not required. 116 */ setInlineDownloadParamsOptional( InlineDownloadParams inlineDownloadParams)117 public abstract Builder setInlineDownloadParamsOptional( 118 InlineDownloadParams inlineDownloadParams); 119 120 /** Builds a {@link DownloadRequest} and checks for correct data. */ build()121 public final DownloadRequest build() { 122 // Ensure that inlinefile: requests include InlineDownloadParams 123 if (urlToDownload().startsWith(INLINE_FILE_URL_SCHEME)) { 124 checkArgument( 125 inlineDownloadParamsOptional().isPresent(), 126 "InlineDownloadParams must be set when using inlinefile: scheme"); 127 128 // inline file request doesn't require download constraints, so set to NONE. 129 setDownloadConstraints(DownloadConstraints.NONE); 130 } 131 132 return autoBuild(); 133 } 134 135 /** Tells AutoValue to generate an automated builder used in {@link #build} */ autoBuild()136 abstract DownloadRequest autoBuild(); 137 urlToDownload()138 abstract String urlToDownload(); 139 inlineDownloadParamsOptional()140 abstract Optional<InlineDownloadParams> inlineDownloadParamsOptional(); 141 } 142 } 143