use of org.kiwix.kiwixmobile.utils.Constants.EXTRA_ZIM_FILE in project kiwix-android by kiwix.
the class DownloadService method downloadBook.
private void downloadBook(String url, int notificationID, LibraryNetworkEntity.Book book) {
if (downloadFragment != null) {
downloadFragment.addDownload(notificationID, book, KIWIX_ROOT + StorageUtils.getFileNameFromUrl(book.getUrl()));
}
TestingUtils.bindResource(DownloadService.class);
if (book.file != null && (book.file.exists() || new File(book.file.getPath() + ".part").exists())) {
// Calculate initial download progress
int initial = (int) (getCurrentSize(book) / (Long.valueOf(book.getSize()) * BOOK_SIZE_OFFSET));
notification.get(notificationID).setProgress(100, initial, false);
updateDownloadFragmentProgress(initial, notificationID);
notificationManager.notify(notificationID, notification.get(notificationID).build());
}
kiwixService.getMetaLinks(url).retryWhen(errors -> errors.flatMap(error -> Observable.timer(5, TimeUnit.SECONDS))).subscribeOn(AndroidSchedulers.mainThread()).flatMap(metaLink -> getMetaLinkContentLength(metaLink.getRelevantUrl().getValue())).flatMap(pair -> Observable.fromIterable(ChunkUtils.getChunks(pair.first, pair.second, notificationID))).concatMap(this::downloadChunk).distinctUntilChanged().doOnComplete(() -> updateDownloadFragmentComplete(notificationID)).subscribe(progress -> {
if (progress == 100) {
notification.get(notificationID).setOngoing(false);
notification.get(notificationID).setContentTitle(notificationTitle + " " + getResources().getString(R.string.zim_file_downloaded));
notification.get(notificationID).setContentText(getString(R.string.zim_file_downloaded));
final Intent target = new Intent(this, KiwixMobileActivity.class);
target.putExtra(EXTRA_ZIM_FILE, KIWIX_ROOT + StorageUtils.getFileNameFromUrl(book.getUrl()));
target.putExtra(EXTRA_NOTIFICATION_ID, notificationID);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, target, PendingIntent.FLAG_CANCEL_CURRENT);
book.downloaded = true;
bookDao.deleteBook(book.id);
notification.get(notificationID).setContentIntent(pendingIntent);
notification.get(notificationID).mActions.clear();
TestingUtils.unbindResource(DownloadService.class);
}
notification.get(notificationID).setProgress(100, progress, false);
if (progress != 100 && timeRemaining.get(notificationID) != -1)
notification.get(notificationID).setContentText(DownloadFragment.toHumanReadableTime(timeRemaining.get(notificationID)));
notificationManager.notify(notificationID, notification.get(notificationID).build());
if (progress == 0 || progress == 100) {
// Tells android to not kill the service
updateForeground();
}
updateDownloadFragmentProgress(progress, notificationID);
if (progress == 100) {
stopSelf();
}
}, Throwable::printStackTrace);
}
Aggregations