66import android .os .Looper ;
77import android .text .TextUtils ;
88
9- import com .codingcoderscode .evolving .net .cache .mode .CCCMode ;
109import com .codingcoderscode .evolving .net .request .api .CCNetApiService ;
11- import com .codingcoderscode .evolving .net .request .base .CCRequest ;
12- import com .codingcoderscode .evolving .net .request .callback .CCDownloadFileWriteListener ;
13- import com .codingcoderscode .evolving .net .request .callback . CCDownloadProgressListener ;
10+ import com .codingcoderscode .evolving .net .request .base .CCSimpleDownloadRequest ;
11+ import com .codingcoderscode .evolving .net .request .listener .CCDownloadFileWriteListener ;
12+ import com .codingcoderscode .evolving .net .request .listener . CCSingleDownloadProgressListener ;
1413import com .codingcoderscode .evolving .net .request .entity .CCDownloadTask ;
1514import com .codingcoderscode .evolving .net .request .exception .CCUnExpectedException ;
1615import com .codingcoderscode .evolving .net .request .exception .NoEnoughSpaceException ;
1716import com .codingcoderscode .evolving .net .request .exception .NoResponseBodyDataException ;
18- import com .codingcoderscode .evolving .net .request .method .CCHttpMethod ;
1917import com .codingcoderscode .evolving .net .request .retry .FlowableRetryWithDelay ;
2018import com .codingcoderscode .evolving .net .response .CCBaseResponse ;
2119import com .codingcoderscode .evolving .net .util .CCFileUtils ;
20+ import com .codingcoderscode .evolving .net .util .CCLogUtil ;
2221import com .codingcoderscode .evolving .net .util .CCNetUtil ;
23- import com .codingcoderscode .evolving .net .util .NetLogUtil ;
24- import com .codingcoderscode .evolving .net .util .SDCardUtil ;
22+ import com .codingcoderscode .evolving .net .util .CCSDCardUtil ;
2523
2624import org .reactivestreams .Publisher ;
25+ import org .reactivestreams .Subscription ;
2726
2827import java .io .File ;
2928import java .io .InputStream ;
4645 * 下载文件请求类
4746 */
4847
49- public class CCDownloadRequest <T > extends CCRequest < T , CCDownloadRequest < T > > {
48+ public class CCDownloadRequest <T > extends CCSimpleDownloadRequest < T > {
5049
5150 //进度回调
52- // private CCDownloadProgressListener ccDownloadProgressCallback ;
51+ private CCSingleDownloadProgressListener mSingleDownloadProgressListener ;
5352 //下载文件的本地保存路径
5453 private String fileSavePath ;
5554 //下载文件的本地名称,含后缀
@@ -86,8 +85,6 @@ public class CCDownloadRequest<T> extends CCRequest<T, CCDownloadRequest<T>> {
8685
8786 private CCDownloadFileWriteListener mDownloadFileWriteListener ;
8887
89- private CCDownloadProgressListener mDownloadProgressListener ;
90-
9188 private final int DEFAULT_BUFFER_SIZE = 8 * 1024 ;
9289
9390 private CCDownloadTask mDownloadTask ;
@@ -97,21 +94,6 @@ public CCDownloadRequest(String url, CCNetApiService apiService) {
9794 this .supportRage = false ;
9895 }
9996
100- @ Override
101- protected int getHttpMethod () {
102- return CCHttpMethod .GET ;
103- }
104-
105- @ Override
106- public int getCacheQueryMode () {
107- return CCCMode .QueryMode .MODE_NET ;
108- }
109-
110- @ Override
111- public int getCacheSaveMode () {
112- return CCCMode .SaveMode .MODE_NONE ;
113- }
114-
11597 @ Override
11698 protected Flowable <CCBaseResponse <T >> getRequestFlowable () {
11799
@@ -181,6 +163,54 @@ protected Call<ResponseBody> getRequestCall() {
181163 return call ;
182164 }
183165
166+ @ Override
167+ protected void onSubscribeLocal (Subscription s ) {
168+ super .onSubscribeLocal (s );
169+ try {
170+ if (getDownloadProgressListener () != null && isRequestRunning () && !isForceCanceled ()) {
171+ getDownloadProgressListener ().<T >onStart (getReqTag (), this .mDownloadTask , getNetCCCanceler ());
172+ }
173+ } catch (Exception e ) {
174+ e .printStackTrace ();
175+ }
176+ }
177+
178+ @ Override
179+ protected void onNextLocal (CCBaseResponse <T > tccBaseResponse ) {
180+ super .onNextLocal (tccBaseResponse );
181+ try {
182+ if (getDownloadProgressListener () != null && isRequestRunning () && !isForceCanceled ()) {
183+ getDownloadProgressListener ().<T >onSuccess (getReqTag (), this .mDownloadTask );
184+ }
185+ } catch (Exception e ) {
186+ e .printStackTrace ();
187+ }
188+ }
189+
190+ @ Override
191+ public void onErrorLocal (Throwable t ) {
192+ super .onErrorLocal (t );
193+ try {
194+ if (getDownloadProgressListener () != null && isRequestRunning () && !isForceCanceled ()) {
195+ getDownloadProgressListener ().<T >onError (getReqTag (), this .mDownloadTask , t );
196+ }
197+ } catch (Exception e ) {
198+ e .printStackTrace ();
199+ }
200+ }
201+
202+ @ Override
203+ public void onCompleteLocal () {
204+ super .onCompleteLocal ();
205+ try {
206+ if (getDownloadProgressListener () != null && isRequestRunning () && !isForceCanceled ()) {
207+ getDownloadProgressListener ().<T >onComplete (getReqTag (), this .mDownloadTask );
208+ }
209+ } catch (Exception e ) {
210+ e .printStackTrace ();
211+ }
212+ }
213+
184214 private long convertStringToLong (String source ) {
185215 long result = 0 ;
186216 try {
@@ -209,7 +239,7 @@ protected void onTryToWriteFileToSDCard(Response<ResponseBody> retrofitResponse,
209239 } else {
210240 long contentLengthLong = convertStringToLong (contentLengthStr );
211241
212- if (contentLengthLong > SDCardUtil .getSDCardAvailableSize () * 1024 * 1024 ) {
242+ if (contentLengthLong > CCSDCardUtil .getSDCardAvailableSize () * 1024 * 1024 ) {
213243 throw new NoEnoughSpaceException ("write failed: ENOSPC (No space left on device)" );
214244 }
215245
@@ -307,7 +337,7 @@ public void run() {
307337 }
308338 } catch (Exception exception ) {
309339 if ((exception instanceof java .io .InterruptedIOException ) && isForceCanceled ()) {
310- NetLogUtil .printLog ("e" , "CCDownloadRequest" , "当前异常类型是java.io.InterruptedIOException,且是强制中断,为用户主动取消网络传输" );
340+ CCLogUtil .printLog ("e" , "CCDownloadRequest" , "当前异常类型是java.io.InterruptedIOException,且是强制中断,为用户主动取消网络传输" );
311341 } else {
312342 throw new CCUnExpectedException (exception );
313343 }
@@ -324,7 +354,7 @@ private void onCloseInputStream(InputStream inputStream) {
324354 inputStream .close ();
325355 }
326356 } catch (Exception e ) {
327- NetLogUtil .printLog ("e" , getClass ().getCanonicalName (), "发生异常" , e );
357+ CCLogUtil .printLog ("e" , getClass ().getCanonicalName (), "发生异常" , e );
328358 }
329359 }
330360
@@ -334,7 +364,7 @@ private void onCloseOutputStream(OutputStream outputStream) {
334364 outputStream .close ();
335365 }
336366 } catch (Exception e ) {
337- NetLogUtil .printLog ("e" , getClass ().getCanonicalName (), "发生异常" , e );
367+ CCLogUtil .printLog ("e" , getClass ().getCanonicalName (), "发生异常" , e );
338368 }
339369 }
340370
@@ -344,7 +374,7 @@ private void onCloseRandomAccessFile(RandomAccessFile rafFile) {
344374 rafFile .close ();
345375 }
346376 } catch (Exception e ) {
347- NetLogUtil .printLog ("e" , getClass ().getCanonicalName (), "发生异常" , e );
377+ CCLogUtil .printLog ("e" , getClass ().getCanonicalName (), "发生异常" , e );
348378 }
349379 }
350380
@@ -386,7 +416,7 @@ private void onFileSaveCheck(boolean supportRage) {
386416 file .createNewFile ();
387417
388418 } catch (Exception exception ) {
389- NetLogUtil .printLog ("e" , getClass ().getCanonicalName (), "发生异常" , exception );
419+ CCLogUtil .printLog ("e" , getClass ().getCanonicalName (), "发生异常" , exception );
390420 }
391421 }
392422
@@ -438,12 +468,12 @@ public CCDownloadRequest<T> setCCDownloadFileWriteListener(CCDownloadFileWriteLi
438468 return this ;
439469 }
440470
441- public CCDownloadProgressListener getDownloadProgressListener () {
442- return mDownloadProgressListener ;
471+ public CCSingleDownloadProgressListener getDownloadProgressListener () {
472+ return mSingleDownloadProgressListener ;
443473 }
444474
445- public CCDownloadRequest <T > setDownloadProgressListener (CCDownloadProgressListener downloadProgressListener ) {
446- this .mDownloadProgressListener = downloadProgressListener ;
475+ public CCDownloadRequest <T > setDownloadProgressListener (CCSingleDownloadProgressListener downloadProgressListener ) {
476+ this .mSingleDownloadProgressListener = downloadProgressListener ;
447477 return this ;
448478 }
449479}
0 commit comments