use of com.xxf.arch.model.DownloadTask in project xxf_android by NBXXF.
the class XXFileServiceImpl method downloadTask.
@Override
public Observable<DownloadTask> downloadTask(String url, String filePath) {
return Observable.create(new ObservableOnSubscribe<DownloadTask>() {
@Override
public void subscribe(@NonNull ObservableEmitter<DownloadTask> emitter) throws Throwable {
Call call = new OkHttpClientBuilder().build().newCall(new Request.Builder().url(url).build());
Response response = call.execute();
long currentLength = 0;
OutputStream os = null;
InputStream is = null;
try {
is = response.body().byteStream();
long totalLength = response.body().contentLength();
FileUtils.createOrExistsFile(filePath);
os = new FileOutputStream(filePath);
int len;
long step = totalLength / 100;
if (step > 524288) {
step = 524288;
} else if (step < 0) {
step = totalLength;
}
byte[] buff = new byte[(int) step];
DownloadTask downloadTask = new DownloadTask(url, filePath, totalLength);
while ((len = is.read(buff)) != -1) {
if (emitter.isDisposed()) {
return;
}
os.write(buff, 0, len);
currentLength += len;
downloadTask.setCurrent(currentLength);
emitter.onNext(downloadTask.clone());
}
emitter.onComplete();
} finally {
if (os != null) {
try {
os.close();
} catch (Exception ignored) {
}
}
try {
is.close();
is = null;
} catch (Exception ignored) {
}
}
}
}).subscribeOn(Schedulers.io());
}
use of com.xxf.arch.model.DownloadTask in project xxf_android by NBXXF.
the class StateActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Observable<Boolean>> datas = new ArrayList<>();
datas.add(Observable.fromCallable(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
throw new RuntimeException("xxx");
}
}));
datas.add(Observable.fromCallable(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return true;
}
}));
Observable.concatDelayError(datas).observeOn(AndroidSchedulers.mainThread(), true).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Throwable {
Log.d("=======>data error:", "" + throwable);
}
}).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Throwable {
Log.d("=======>data:", "" + aBoolean);
}
});
stateBinding = ActivityStateBinding.inflate(getLayoutInflater(), null, false);
// stateBinding.grayLayout.setGrayColor(true);
setContentView(stateBinding.getRoot());
// TestViewModel viewModel = XXF.getViewModel(this, TestViewModel.class);
stateBinding.recyclerView.setAdapter(testAdaper = new TestAdaper());
// new ItemTouchHelper(new SimpleItemTouchHelperCallback(testAdaper)).attachToRecyclerView(stateBinding.recyclerView);
// stateBinding.recyclerView.addItemDecoration(new GridItemDecoration(DensityUtil.dip2px(5)));
stateBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
// stateBinding.recyclerView.addItemDecoration(ItemDecorationFactory.createHorizontalItemDecoration(new HorizontalDividerItemDecoration.Builder(this).size(DensityUtil.dip2px(20)).color(Color.RED)));
DividerDecoration dividerItemDecoration = new DividerDecoration(this, Color.YELLOW, DensityUtil.dip2px(20));
stateBinding.recyclerView.addItemDecoration(dividerItemDecoration);
stateBinding.btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ShareUtil.shareQQ(StateActivity.this, "xxxx");
SystemUtils.shareText(StateActivity.this, "xxxx", null).compose(XXF.bindToErrorNotice()).subscribe();
}
});
stateBinding.btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ScrollShotting(stateBinding.recyclerView, 200, Color.WHITE) {
@Override
public void onShot(@NotNull Bitmap bitmap) {
File my_images = new File(getApplication().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "image");
my_images.mkdirs();
File file = new File(new File(getApplication().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "image"), "default_image2.jpg");
boolean b = BitmapUtils.INSTANCE.bitmapToFile(bitmap, file);
ToastUtils.showToast("设置:" + b);
SystemUtils.shareFile(StateActivity.this, file.getAbsolutePath(), FileProvider7.INSTANCE.getAuthority(getApplication()), SystemUtils.SHARE_WEIBO_CIRCLE_COMPONENT).compose(XXF.bindToErrorNotice()).subscribe();
}
}.start();
}
});
testAdaper.bindData(true, new ArrayList<>());
loadData();
stateBinding.btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* XXF.getFileService()
.getPrivateFileDir()
.flatMap(new Function<File, ObservableSource<File>>() {
@Override
public ObservableSource<File> apply(File file) throws Throwable {
return XXF.getFileService()
.download("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4", new File(file, "test.mp4").getAbsolutePath());
}
})
.compose(XXF.bindToProgressHud())
.subscribe();*/
XXF.getFileService().getFilesDir(false, false).flatMap(new Function<File, Observable<DownloadTask>>() {
@Override
public Observable<DownloadTask> apply(File file) throws Throwable {
return XXF.getFileService().downloadTask("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4", new File(file, "test.mp4").getAbsolutePath());
}
}).compose(new ProgressHUDTransformerImpl<DownloadTask>(StateActivity.this) {
@Override
public void onNext(DownloadTask downloadTask) {
super.onNext(downloadTask);
getSafeProgressHUD().updateStateText((downloadTask.getCurrent() * 1.0f / downloadTask.getDuration()) * 100 + "%");
}
}.setDismissOnNext(false)).to(XXF.bindLifecycle(StateActivity.this)).subscribe(new Consumer<DownloadTask>() {
@Override
public void accept(DownloadTask downloadTask) throws Throwable {
Log.d("", "=========>task2:" + downloadTask);
}
});
}
});
}
Aggregations