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