• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
17 
18 import android.net.Uri;
19 import android.util.Pair;
20 import com.google.android.libraries.mobiledatadownload.downloader.DownloadConstraints;
21 import com.google.auto.value.AutoValue;
22 import com.google.common.base.Optional;
23 import com.google.common.collect.ImmutableList;
24 
25 /**
26  * Request to download a single file.
27  *
28  * <p>This differs from {@link DownloadFileGroupRequest} in two main ways:
29  *
30  * <p>1) Only a single file is downloaded rather than a group of files.
31  *
32  * <p>2) MDD does NOT manage the file after download. The caller specifies that destination of the
33  * download and is responsible for managing the file after download.
34  */
35 @AutoValue
36 public abstract class SingleFileDownloadRequest {
37 
38   // Default value for Traffic Tag if not set by clients.
39   // MDD will not tag the traffic if the TrafficTag is not set to a valid value (>0).
40   private static final int UNSPECIFIED_TRAFFIC_TAG = -1;
41 
SingleFileDownloadRequest()42   SingleFileDownloadRequest() {}
43 
44   // The Destination File Uri to download the file at.
destinationFileUri()45   public abstract Uri destinationFileUri();
46 
47   // The url to download the file from.
urlToDownload()48   public abstract String urlToDownload();
49 
50   // Conditions under which this file should be downloaded.
downloadConstraints()51   public abstract DownloadConstraints downloadConstraints();
52 
53   /** If present, will receive download progress update. */
listenerOptional()54   public abstract Optional<SingleFileDownloadListener> listenerOptional();
55 
56   // Traffic tag used for this request.
57   // If not set, it will take the default value of UNSPECIFIED_TRAFFIC_TAG and MDD will not tag the
58   // traffic.
trafficTag()59   public abstract int trafficTag();
60 
61   // The extra HTTP headers for this request.
extraHttpHeaders()62   public abstract ImmutableList<Pair<String, String>> extraHttpHeaders();
63 
64   // The size of the being downloaded file in bytes.
65   // This is used to display the progressbar.
66   // If not specified, an indeterminate progressbar will be displayed.
67   // https://developer.android.com/reference/android/app/Notification.Builder.html#setProgress(int,%20int,%20boolean)
fileSizeBytes()68   public abstract int fileSizeBytes();
69 
70   // Used only by Foreground download.
71   // The Content Title of the associated Notification for this download.
notificationContentTitle()72   public abstract String notificationContentTitle();
73 
74   // Used only by Foreground download.
75   // If Present, the Content Text (description) of the associated Notification for this download.
76   // Otherwise, the Content Text will be the url to download.
notificationContentTextOptional()77   public abstract Optional<String> notificationContentTextOptional();
78 
79   // Whether to show the downloaded notification. If false, MDD will automatically remove this
80   // notification when the download finished.
showDownloadedNotification()81   public abstract boolean showDownloadedNotification();
82 
newBuilder()83   public static Builder newBuilder() {
84     return new AutoValue_SingleFileDownloadRequest.Builder()
85         .setTrafficTag(UNSPECIFIED_TRAFFIC_TAG)
86         .setExtraHttpHeaders(ImmutableList.of())
87         .setFileSizeBytes(0)
88         .setShowDownloadedNotification(true)
89         .setDownloadConstraints(DownloadConstraints.NONE);
90   }
91 
92   /** Builder for {@link DownloadRequest}. */
93   @AutoValue.Builder
94   public abstract static class Builder {
Builder()95     Builder() {}
96 
97     /** Sets the destination file uri. */
setDestinationFileUri(Uri fileUri)98     public abstract Builder setDestinationFileUri(Uri fileUri);
99 
100     /** Sets the url to download. */
setUrlToDownload(String urlToDownload)101     public abstract Builder setUrlToDownload(String urlToDownload);
102 
103     /** Sets the DowloadConstraints. */
setDownloadConstraints(DownloadConstraints downloadConstraints)104     public abstract Builder setDownloadConstraints(DownloadConstraints downloadConstraints);
105 
106     /** Sets the optional download listener. If present, will receive download progress update. */
setListenerOptional( Optional<SingleFileDownloadListener> listenerOptional)107     public abstract Builder setListenerOptional(
108         Optional<SingleFileDownloadListener> listenerOptional);
109 
110     /** Sets the traffic tag for this request. */
setTrafficTag(int trafficTag)111     public abstract Builder setTrafficTag(int trafficTag);
112 
113     /** Sets the extra HTTP headers for this request. */
setExtraHttpHeaders( ImmutableList<Pair<String, String>> extraHttpHeaders)114     public abstract Builder setExtraHttpHeaders(
115         ImmutableList<Pair<String, String>> extraHttpHeaders);
116 
117     /**
118      * The size of the being downloaded file in bytes. This is used to display the progressbar. If
119      * not specified, a indeterminate progressbar will be displayed.
120      * https://developer.android.com/reference/android/app/Notification.Builder.html#setProgress(int,%20int,%20boolean)
121      */
setFileSizeBytes(int fileSizeBytes)122     public abstract Builder setFileSizeBytes(int fileSizeBytes);
123 
124     /** Sets the Notification Content Tile which will be used for foreground download */
setNotificationContentTitle(String notificationContentTitle)125     public abstract Builder setNotificationContentTitle(String notificationContentTitle);
126 
127     /**
128      * Sets the Notification Context Text which will be used for foreground downloads.
129      *
130      * <p>If not set, the url to download will be used instead.
131      */
setNotificationContentTextOptional( Optional<String> notificationContentTextOptional)132     public abstract Builder setNotificationContentTextOptional(
133         Optional<String> notificationContentTextOptional);
134 
135     /**
136      * Sets to show Downloaded Notification after the download finished successfully. This is only
137      * be used for foreground download. Default value is to show the downloaded notification.
138      */
setShowDownloadedNotification(boolean showDownloadedNotification)139     public abstract Builder setShowDownloadedNotification(boolean showDownloadedNotification);
140 
141     /** Builds {@link SingleFileDownloadRequest}. */
build()142     public final SingleFileDownloadRequest build() {
143       // If notification content title is not provided, use urlToDownload as a fallback
144       if (!notificationContentTitle().isPresent()) {
145         setNotificationContentTitle(urlToDownload());
146       }
147       // Use AutoValue's generated build to finish building.
148       return autoBuild();
149     }
150 
151     // private getter generated by AutoValue for access in build().
urlToDownload()152     abstract String urlToDownload();
153 
154     // private getter generated by AutoValue for access in build().
notificationContentTitle()155     abstract Optional<String> notificationContentTitle();
156 
157     // private build method to be generated by AutoValue.
autoBuild()158     abstract SingleFileDownloadRequest autoBuild();
159   }
160 }
161