|
|
@@ -1,5 +1,7 @@
|
|
|
package com.atmob.voiceai.utils;
|
|
|
|
|
|
+import android.os.CancellationSignal;
|
|
|
+
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
|
|
|
@@ -7,8 +9,11 @@ import atmob.okhttp3.OkHttpClient;
|
|
|
import atmob.okhttp3.Request;
|
|
|
import atmob.okhttp3.Response;
|
|
|
import atmob.okhttp3.ResponseBody;
|
|
|
+import atmob.reactivex.rxjava3.annotations.NonNull;
|
|
|
import atmob.reactivex.rxjava3.core.Observable;
|
|
|
import atmob.reactivex.rxjava3.core.ObservableOnSubscribe;
|
|
|
+import atmob.reactivex.rxjava3.core.Observer;
|
|
|
+import atmob.reactivex.rxjava3.disposables.Disposable;
|
|
|
import atmob.reactivex.rxjava3.schedulers.Schedulers;
|
|
|
|
|
|
public class DownloadUtils {
|
|
|
@@ -22,8 +27,9 @@ public class DownloadUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static void downLoad(OkHttpClient client, String url, final String filePath, final String fileName,
|
|
|
- final FileDownLoadObserver<File> fileDownLoadObserver) {
|
|
|
+ public static CancellationSignal downLoad(OkHttpClient client, String url, final String filePath, final String fileName,
|
|
|
+ final FileDownLoadObserver<File> fileDownLoadObserver) {
|
|
|
+ CancellationSignal cancellationSignal = new CancellationSignal();
|
|
|
Observable.create((ObservableOnSubscribe<ResponseBody>) emitter -> {
|
|
|
Request request = new Request.Builder()
|
|
|
.url(url)
|
|
|
@@ -45,7 +51,7 @@ public class DownloadUtils {
|
|
|
emitter.onError(e);
|
|
|
}
|
|
|
})
|
|
|
- .map(responseBody -> fileDownLoadObserver.saveFile(responseBody,
|
|
|
+ .map(responseBody -> fileDownLoadObserver.saveFile(cancellationSignal, responseBody,
|
|
|
filePath, fileName))
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
.observeOn(Schedulers.io())
|
|
|
@@ -55,7 +61,37 @@ public class DownloadUtils {
|
|
|
}
|
|
|
return file;
|
|
|
})
|
|
|
- .subscribe(fileDownLoadObserver);
|
|
|
+ .subscribe(new Observer<File>() {
|
|
|
+ @Override
|
|
|
+ public void onSubscribe(@NonNull Disposable d) {
|
|
|
+ if (cancellationSignal.isCanceled()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fileDownLoadObserver.onDownLoadStart();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onNext(@NonNull File file) {
|
|
|
+ if (cancellationSignal.isCanceled()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fileDownLoadObserver.onDownLoadSuccess(file);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable e) {
|
|
|
+ if (cancellationSignal.isCanceled()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fileDownLoadObserver.onDownLoadFail(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onComplete() {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return cancellationSignal;
|
|
|
}
|
|
|
|
|
|
|