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);
}
});
}
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();
}
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());
}
}
}
}
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;
}
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);
}
}
Aggregations