Search in sources :

Example 1 with DownloadMission

use of zlc.season.rxdownload2.entity.DownloadMission in project RxDownload by ssseasonnn.

the class DownloadService method startDispatch.

/**
     * start dispatch download queue.
     */
private void startDispatch() {
    disposable = Observable.create(new ObservableOnSubscribe<DownloadMission>() {

        @Override
        public void subscribe(ObservableEmitter<DownloadMission> emitter) throws Exception {
            DownloadMission mission;
            while (!emitter.isDisposed()) {
                try {
                    log(WAITING_FOR_MISSION_COME);
                    mission = downloadQueue.take();
                    log(Constant.MISSION_COMING);
                } catch (InterruptedException e) {
                    log("Interrupt blocking queue.");
                    continue;
                }
                emitter.onNext(mission);
            }
            emitter.onComplete();
        }
    }).subscribeOn(Schedulers.newThread()).subscribe(new Consumer<DownloadMission>() {

        @Override
        public void accept(DownloadMission mission) throws Exception {
            mission.start(semaphore);
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            log(throwable);
        }
    });
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Consumer(io.reactivex.functions.Consumer) DownloadMission(zlc.season.rxdownload2.entity.DownloadMission) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 2 with DownloadMission

use of zlc.season.rxdownload2.entity.DownloadMission in project RxDownload by ssseasonnn.

the class DownloadService method destroy.

/**
     * Call when service is onDestroy.
     */
private void destroy() {
    dispose(disposable);
    for (DownloadMission each : missionMap.values()) {
        each.pause(dataBaseHelper);
    }
    downloadQueue.clear();
}
Also used : DownloadMission(zlc.season.rxdownload2.entity.DownloadMission)

Example 3 with DownloadMission

use of zlc.season.rxdownload2.entity.DownloadMission in project RxDownload by ssseasonnn.

the class DownloadService method deleteAll.

/**
     * Delete all mission which associate with missionId.
     *
     * @param missionId  missionId
     * @param deleteFile deleteFile?
     */
public void deleteAll(String missionId, boolean deleteFile) {
    DownloadMission mission = missionMap.get(missionId);
    if (mission != null && mission instanceof MultiMission) {
        mission.delete(dataBaseHelper, deleteFile);
        missionMap.remove(missionId);
    } else {
        createProcessor(missionId, processorMap).onNext(normal(null));
        if (deleteFile) {
            List<DownloadRecord> list = dataBaseHelper.readMissionsRecord(missionId);
            for (DownloadRecord each : list) {
                deleteFiles(getFiles(each.getSaveName(), each.getSavePath()));
                dataBaseHelper.deleteRecord(each.getUrl());
            }
        }
    }
}
Also used : MultiMission(zlc.season.rxdownload2.entity.MultiMission) DownloadMission(zlc.season.rxdownload2.entity.DownloadMission) DownloadRecord(zlc.season.rxdownload2.entity.DownloadRecord)

Example 4 with DownloadMission

use of zlc.season.rxdownload2.entity.DownloadMission in project RxDownload by ssseasonnn.

the class DownloadService method receiveDownloadEvent.

/**
     * Receive the url download event.
     * <p>
     * Will receive the following event:
     * {@link DownloadFlag#NORMAL}、{@link DownloadFlag#WAITING}、
     * {@link DownloadFlag#STARTED}、{@link DownloadFlag#PAUSED}、
     * {@link DownloadFlag#COMPLETED}、{@link DownloadFlag#FAILED};
     * <p>
     * Every event has {@link DownloadStatus}, you can get it and display it on the interface.
     *
     * @param url url
     * @return DownloadEvent
     */
public FlowableProcessor<DownloadEvent> receiveDownloadEvent(String url) {
    FlowableProcessor<DownloadEvent> processor = createProcessor(url, processorMap);
    DownloadMission mission = missionMap.get(url);
    if (mission == null) {
        //Not yet add this url mission.
        DownloadRecord record = dataBaseHelper.readSingleRecord(url);
        if (record == null) {
            processor.onNext(normal(null));
        } else {
            File file = getFiles(record.getSaveName(), record.getSavePath())[0];
            if (file.exists()) {
                processor.onNext(createEvent(record.getFlag(), record.getStatus()));
            } else {
                processor.onNext(normal(null));
            }
        }
    }
    return processor;
}
Also used : DownloadEvent(zlc.season.rxdownload2.entity.DownloadEvent) DownloadMission(zlc.season.rxdownload2.entity.DownloadMission) File(java.io.File) DownloadRecord(zlc.season.rxdownload2.entity.DownloadRecord)

Example 5 with DownloadMission

use of zlc.season.rxdownload2.entity.DownloadMission in project RxDownload by ssseasonnn.

the class DownloadService method deleteDownload.

/**
     * Delete download.
     * <p>
     * Delete a url or all tasks belonging to missionId.
     *
     * @param url        url or missionId
     * @param deleteFile whether delete file
     */
public void deleteDownload(String url, boolean deleteFile) {
    DownloadMission mission = missionMap.get(url);
    if (mission != null && mission instanceof SingleMission) {
        mission.delete(dataBaseHelper, deleteFile);
        missionMap.remove(url);
    } else {
        createProcessor(url, processorMap).onNext(normal(null));
        if (deleteFile) {
            DownloadRecord record = dataBaseHelper.readSingleRecord(url);
            if (record != null) {
                deleteFiles(getFiles(record.getSaveName(), record.getSavePath()));
            }
        }
        dataBaseHelper.deleteRecord(url);
    }
}
Also used : SingleMission(zlc.season.rxdownload2.entity.SingleMission) DownloadMission(zlc.season.rxdownload2.entity.DownloadMission) DownloadRecord(zlc.season.rxdownload2.entity.DownloadRecord)

Aggregations

DownloadMission (zlc.season.rxdownload2.entity.DownloadMission)5 DownloadRecord (zlc.season.rxdownload2.entity.DownloadRecord)3 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Consumer (io.reactivex.functions.Consumer)1 File (java.io.File)1 DownloadEvent (zlc.season.rxdownload2.entity.DownloadEvent)1 MultiMission (zlc.season.rxdownload2.entity.MultiMission)1 SingleMission (zlc.season.rxdownload2.entity.SingleMission)1