• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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.exoplayer2.offline;
17 
18 import androidx.annotation.IntDef;
19 import com.google.android.exoplayer2.C;
20 import com.google.android.exoplayer2.util.Assertions;
21 import java.lang.annotation.Documented;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 
25 /** Represents state of a download. */
26 public final class Download {
27 
28   /**
29    * Download states. One of {@link #STATE_QUEUED}, {@link #STATE_STOPPED}, {@link
30    * #STATE_DOWNLOADING}, {@link #STATE_COMPLETED}, {@link #STATE_FAILED}, {@link #STATE_REMOVING}
31    * or {@link #STATE_RESTARTING}.
32    */
33   @Documented
34   @Retention(RetentionPolicy.SOURCE)
35   @IntDef({
36     STATE_QUEUED,
37     STATE_STOPPED,
38     STATE_DOWNLOADING,
39     STATE_COMPLETED,
40     STATE_FAILED,
41     STATE_REMOVING,
42     STATE_RESTARTING
43   })
44   public @interface State {}
45   // Important: These constants are persisted into DownloadIndex. Do not change them.
46   /**
47    * The download is waiting to be started. A download may be queued because the {@link
48    * DownloadManager}
49    *
50    * <ul>
51    *   <li>Is {@link DownloadManager#getDownloadsPaused() paused}
52    *   <li>Has {@link DownloadManager#getRequirements() Requirements} that are not met
53    *   <li>Has already started {@link DownloadManager#getMaxParallelDownloads()
54    *       maxParallelDownloads}
55    * </ul>
56    */
57   public static final int STATE_QUEUED = 0;
58   /** The download is stopped for a specified {@link #stopReason}. */
59   public static final int STATE_STOPPED = 1;
60   /** The download is currently started. */
61   public static final int STATE_DOWNLOADING = 2;
62   /** The download completed. */
63   public static final int STATE_COMPLETED = 3;
64   /** The download failed. */
65   public static final int STATE_FAILED = 4;
66   /** The download is being removed. */
67   public static final int STATE_REMOVING = 5;
68   /** The download will restart after all downloaded data is removed. */
69   public static final int STATE_RESTARTING = 7;
70 
71   /** Failure reasons. Either {@link #FAILURE_REASON_NONE} or {@link #FAILURE_REASON_UNKNOWN}. */
72   @Documented
73   @Retention(RetentionPolicy.SOURCE)
74   @IntDef({FAILURE_REASON_NONE, FAILURE_REASON_UNKNOWN})
75   public @interface FailureReason {}
76   /** The download isn't failed. */
77   public static final int FAILURE_REASON_NONE = 0;
78   /** The download is failed because of unknown reason. */
79   public static final int FAILURE_REASON_UNKNOWN = 1;
80 
81   /** The download isn't stopped. */
82   public static final int STOP_REASON_NONE = 0;
83 
84   /** The download request. */
85   public final DownloadRequest request;
86   /** The state of the download. */
87   @State public final int state;
88   /** The first time when download entry is created. */
89   public final long startTimeMs;
90   /** The last update time. */
91   public final long updateTimeMs;
92   /** The total size of the content in bytes, or {@link C#LENGTH_UNSET} if unknown. */
93   public final long contentLength;
94   /** The reason the download is stopped, or {@link #STOP_REASON_NONE}. */
95   public final int stopReason;
96   /**
97    * If {@link #state} is {@link #STATE_FAILED} then this is the cause, otherwise {@link
98    * #FAILURE_REASON_NONE}.
99    */
100   @FailureReason public final int failureReason;
101 
102   /* package */ final DownloadProgress progress;
103 
Download( DownloadRequest request, @State int state, long startTimeMs, long updateTimeMs, long contentLength, int stopReason, @FailureReason int failureReason)104   public Download(
105       DownloadRequest request,
106       @State int state,
107       long startTimeMs,
108       long updateTimeMs,
109       long contentLength,
110       int stopReason,
111       @FailureReason int failureReason) {
112     this(
113         request,
114         state,
115         startTimeMs,
116         updateTimeMs,
117         contentLength,
118         stopReason,
119         failureReason,
120         new DownloadProgress());
121   }
122 
Download( DownloadRequest request, @State int state, long startTimeMs, long updateTimeMs, long contentLength, int stopReason, @FailureReason int failureReason, DownloadProgress progress)123   public Download(
124       DownloadRequest request,
125       @State int state,
126       long startTimeMs,
127       long updateTimeMs,
128       long contentLength,
129       int stopReason,
130       @FailureReason int failureReason,
131       DownloadProgress progress) {
132     Assertions.checkNotNull(progress);
133     Assertions.checkArgument((failureReason == FAILURE_REASON_NONE) == (state != STATE_FAILED));
134     if (stopReason != 0) {
135       Assertions.checkArgument(state != STATE_DOWNLOADING && state != STATE_QUEUED);
136     }
137     this.request = request;
138     this.state = state;
139     this.startTimeMs = startTimeMs;
140     this.updateTimeMs = updateTimeMs;
141     this.contentLength = contentLength;
142     this.stopReason = stopReason;
143     this.failureReason = failureReason;
144     this.progress = progress;
145   }
146 
147   /** Returns whether the download is completed or failed. These are terminal states. */
isTerminalState()148   public boolean isTerminalState() {
149     return state == STATE_COMPLETED || state == STATE_FAILED;
150   }
151 
152   /** Returns the total number of downloaded bytes. */
getBytesDownloaded()153   public long getBytesDownloaded() {
154     return progress.bytesDownloaded;
155   }
156 
157   /**
158    * Returns the estimated download percentage, or {@link C#PERCENTAGE_UNSET} if no estimate is
159    * available.
160    */
getPercentDownloaded()161   public float getPercentDownloaded() {
162     return progress.percentDownloaded;
163   }
164 }
165