Search in sources :

Example 1 with DownloadTask

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());
}
Also used : Call(okhttp3.Call) ObservableOnSubscribe(io.reactivex.rxjava3.core.ObservableOnSubscribe) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Request(okhttp3.Request) DownloadTask(com.xxf.arch.model.DownloadTask) Response(okhttp3.Response) NonNull(io.reactivex.rxjava3.annotations.NonNull) FileOutputStream(java.io.FileOutputStream) OkHttpClientBuilder(com.xxf.arch.http.OkHttpClientBuilder) ObservableEmitter(io.reactivex.rxjava3.core.ObservableEmitter)

Example 2 with DownloadTask

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);
                }
            });
        }
    });
}
Also used : DividerDecoration(com.xxf.view.recyclerview.itemdecorations.DividerDecoration) ArrayList(java.util.ArrayList) DownloadTask(com.xxf.arch.model.DownloadTask) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) NotNull(org.jetbrains.annotations.NotNull) Observable(io.reactivex.rxjava3.core.Observable) Callable(java.util.concurrent.Callable) Bitmap(android.graphics.Bitmap) Consumer(io.reactivex.rxjava3.functions.Consumer) File(java.io.File)

Aggregations

DownloadTask (com.xxf.arch.model.DownloadTask)2 Bitmap (android.graphics.Bitmap)1 View (android.view.View)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 OkHttpClientBuilder (com.xxf.arch.http.OkHttpClientBuilder)1 DividerDecoration (com.xxf.view.recyclerview.itemdecorations.DividerDecoration)1 NonNull (io.reactivex.rxjava3.annotations.NonNull)1 Observable (io.reactivex.rxjava3.core.Observable)1 ObservableEmitter (io.reactivex.rxjava3.core.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.rxjava3.core.ObservableOnSubscribe)1 Consumer (io.reactivex.rxjava3.functions.Consumer)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1