mirror of
https://github.com/mihonapp/mihon.git
synced 2025-06-25 10:37:51 +02:00
Initial download manager
This commit is contained in:
@ -0,0 +1,128 @@
|
||||
package eu.kanade.mangafeed.data.helpers;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import eu.kanade.mangafeed.data.models.Chapter;
|
||||
import eu.kanade.mangafeed.data.models.Manga;
|
||||
import eu.kanade.mangafeed.data.models.Page;
|
||||
import eu.kanade.mangafeed.events.DownloadChapterEvent;
|
||||
import eu.kanade.mangafeed.sources.base.Source;
|
||||
import eu.kanade.mangafeed.util.DiskUtils;
|
||||
import rx.Observable;
|
||||
import rx.Subscription;
|
||||
import rx.schedulers.Schedulers;
|
||||
import rx.subjects.PublishSubject;
|
||||
|
||||
public class DownloadManager {
|
||||
|
||||
private PublishSubject<DownloadChapterEvent> downloadsSubject;
|
||||
private Subscription downloadsSubscription;
|
||||
|
||||
private Context context;
|
||||
private SourceManager sourceManager;
|
||||
|
||||
public DownloadManager(Context context, SourceManager sourceManager) {
|
||||
this.context = context;
|
||||
this.sourceManager = sourceManager;
|
||||
|
||||
initializeDownloadSubscription();
|
||||
}
|
||||
|
||||
private void initializeDownloadSubscription() {
|
||||
if (downloadsSubscription != null && !downloadsSubscription.isUnsubscribed()) {
|
||||
downloadsSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
downloadsSubject = PublishSubject.create();
|
||||
|
||||
downloadsSubscription = downloadsSubject
|
||||
.subscribeOn(Schedulers.io())
|
||||
.concatMap(event -> downloadChapter(event.getManga(), event.getChapter()))
|
||||
.onBackpressureBuffer()
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private Observable<Page> downloadChapter(Manga manga, Chapter chapter) {
|
||||
final Source source = sourceManager.get(manga.source);
|
||||
final File chapterDirectory = new File(getDownloadsDirectory(), getChapterDirectory(chapter));
|
||||
|
||||
return source
|
||||
.pullPageListFromNetwork(chapter.url)
|
||||
// Ensure we don't download a chapter already downloaded
|
||||
.filter(pages -> !isChapterDownloaded(chapterDirectory, pages))
|
||||
// Get all the URLs to the source images, fetch pages if necessary
|
||||
.flatMap(pageList -> Observable.merge(
|
||||
Observable.from(pageList).filter(page -> page.getImageUrl() != null),
|
||||
source.getRemainingImageUrlsFromPageList(pageList)))
|
||||
// Start downloading images
|
||||
.flatMap(page -> getDownloadedImage(page, source, chapterDirectory));
|
||||
}
|
||||
|
||||
private File getDownloadsDirectory() {
|
||||
// TODO
|
||||
return new File(DiskUtils.getStorageDirectories(context)[0]);
|
||||
}
|
||||
|
||||
private String getChapterDirectory(Chapter chapter) {
|
||||
return chapter.name.replaceAll("[^a-zA-Z0-9.-]", "_");
|
||||
}
|
||||
|
||||
private String getImageFilename(Page page) {
|
||||
return page.getImageUrl().substring(
|
||||
page.getImageUrl().lastIndexOf("/") + 1,
|
||||
page.getImageUrl().length());
|
||||
}
|
||||
|
||||
private boolean isChapterDownloaded(File chapterDir, List<Page> pages) {
|
||||
return chapterDir.exists() && chapterDir.listFiles().length == pages.size();
|
||||
}
|
||||
|
||||
private boolean isImageDownloaded(File imagePath) {
|
||||
return imagePath.exists() && !imagePath.isDirectory();
|
||||
}
|
||||
|
||||
public Observable<Page> getDownloadedImage(final Page page, Source source, File chapterDir) {
|
||||
Observable<Page> obs = Observable.just(page);
|
||||
if (page.getImageUrl() == null)
|
||||
return obs;
|
||||
|
||||
String imageFilename = getImageFilename(page);
|
||||
File imagePath = new File(chapterDir, imageFilename);
|
||||
|
||||
if (!isImageDownloaded(imagePath)) {
|
||||
page.setStatus(Page.DOWNLOAD_IMAGE);
|
||||
obs = downloadImage(page, source, chapterDir, imageFilename);
|
||||
}
|
||||
|
||||
return obs.flatMap(p -> {
|
||||
page.setImagePath(imagePath.getAbsolutePath());
|
||||
page.setStatus(Page.READY);
|
||||
return Observable.just(page);
|
||||
}).onErrorResumeNext(e -> {
|
||||
page.setStatus(Page.ERROR);
|
||||
return Observable.just(page);
|
||||
});
|
||||
}
|
||||
|
||||
private Observable<Page> downloadImage(final Page page, Source source, File chapterDir, String imageFilename) {
|
||||
return source.getImageProgressResponse(page)
|
||||
.flatMap(resp -> {
|
||||
try {
|
||||
DiskUtils.saveBufferedSourceToDirectory(resp.body().source(), chapterDir, imageFilename);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new IllegalStateException("Unable to save image");
|
||||
}
|
||||
return Observable.just(page);
|
||||
});
|
||||
}
|
||||
|
||||
public PublishSubject<DownloadChapterEvent> getDownloadsSubject() {
|
||||
return downloadsSubject;
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,7 @@ import android.preference.PreferenceManager;
|
||||
|
||||
import eu.kanade.mangafeed.R;
|
||||
import eu.kanade.mangafeed.sources.base.Source;
|
||||
import eu.kanade.mangafeed.util.DiskUtils;
|
||||
|
||||
public class PreferencesHelper {
|
||||
|
||||
@ -53,4 +54,9 @@ public class PreferencesHelper {
|
||||
.apply();
|
||||
}
|
||||
|
||||
public String getDownloadsDirectory() {
|
||||
return mPref.getString(getKey(R.string.pref_download_directory_key),
|
||||
DiskUtils.getStorageDirectories(context)[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,58 @@
|
||||
package eu.kanade.mangafeed.data.services;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
import eu.kanade.mangafeed.App;
|
||||
import eu.kanade.mangafeed.data.helpers.DownloadManager;
|
||||
import eu.kanade.mangafeed.events.DownloadChapterEvent;
|
||||
import eu.kanade.mangafeed.util.AndroidComponentUtil;
|
||||
import eu.kanade.mangafeed.util.EventBusHook;
|
||||
|
||||
public class DownloadService extends Service {
|
||||
|
||||
@Inject DownloadManager downloadManager;
|
||||
|
||||
public static Intent getStartIntent(Context context) {
|
||||
return new Intent(context, DownloadService.class);
|
||||
}
|
||||
|
||||
public static boolean isRunning(Context context) {
|
||||
return AndroidComponentUtil.isServiceRunning(context, DownloadService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
App.get(this).getComponent().inject(this);
|
||||
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@EventBusHook
|
||||
public void onEvent(DownloadChapterEvent event) {
|
||||
downloadManager.getDownloadsSubject().onNext(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
EventBus.getDefault().unregister(this);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package eu.kanade.mangafeed.events;
|
||||
|
||||
import eu.kanade.mangafeed.data.models.Chapter;
|
||||
import eu.kanade.mangafeed.data.models.Manga;
|
||||
|
||||
public class DownloadChapterEvent {
|
||||
private Manga manga;
|
||||
private Chapter chapter;
|
||||
|
||||
public DownloadChapterEvent(Manga manga, Chapter chapter) {
|
||||
this.manga = manga;
|
||||
this.chapter = chapter;
|
||||
}
|
||||
|
||||
public Manga getManga() {
|
||||
return manga;
|
||||
}
|
||||
|
||||
public Chapter getChapter() {
|
||||
return chapter;
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import android.app.Application;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Component;
|
||||
import eu.kanade.mangafeed.data.services.DownloadService;
|
||||
import eu.kanade.mangafeed.data.services.LibraryUpdateService;
|
||||
import eu.kanade.mangafeed.injection.module.AppModule;
|
||||
import eu.kanade.mangafeed.injection.module.DataModule;
|
||||
@ -42,6 +43,7 @@ public interface AppComponent {
|
||||
void inject(Source source);
|
||||
|
||||
void inject(LibraryUpdateService libraryUpdateService);
|
||||
void inject(DownloadService downloadService);
|
||||
|
||||
Application application();
|
||||
|
||||
|
@ -8,11 +8,10 @@ import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import eu.kanade.mangafeed.data.caches.CacheManager;
|
||||
import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
|
||||
import eu.kanade.mangafeed.data.helpers.DownloadManager;
|
||||
import eu.kanade.mangafeed.data.helpers.NetworkHelper;
|
||||
import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
|
||||
import eu.kanade.mangafeed.data.helpers.SourceManager;
|
||||
import rx.Scheduler;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* Provide dependencies to the DataManager, mainly Helper classes and Retrofit services.
|
||||
@ -32,12 +31,6 @@ public class DataModule {
|
||||
return new DatabaseHelper(app);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
Scheduler provideSubscribeScheduler() {
|
||||
return Schedulers.io();
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
CacheManager provideCacheManager(Application app) {
|
||||
@ -56,4 +49,10 @@ public class DataModule {
|
||||
return new SourceManager(app);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
DownloadManager provideDownloadManager(Application app, SourceManager sourceManager) {
|
||||
return new DownloadManager(app, sourceManager);
|
||||
}
|
||||
|
||||
}
|
@ -85,6 +85,10 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
|
||||
}
|
||||
}
|
||||
|
||||
public Manga getManga() {
|
||||
return manga;
|
||||
}
|
||||
|
||||
public void refreshChapters() {
|
||||
if (getView() != null)
|
||||
getView().setSwipeRefreshing();
|
||||
|
@ -4,6 +4,7 @@ package eu.kanade.mangafeed.sources.base;
|
||||
import android.content.Context;
|
||||
|
||||
import com.squareup.okhttp.Headers;
|
||||
import com.squareup.okhttp.Response;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -123,7 +124,7 @@ public abstract class Source extends BaseSource {
|
||||
}
|
||||
|
||||
private Observable<Page> cacheImage(final Page page) {
|
||||
return mNetworkService.getProgressResponse(page.getImageUrl(), mRequestHeaders, page)
|
||||
return getImageProgressResponse(page)
|
||||
.flatMap(resp -> {
|
||||
if (!mCacheManager.putImageToDiskCache(page.getImageUrl(), resp)) {
|
||||
throw new IllegalStateException("Unable to save image");
|
||||
@ -132,6 +133,10 @@ public abstract class Source extends BaseSource {
|
||||
});
|
||||
}
|
||||
|
||||
public Observable<Response> getImageProgressResponse(final Page page) {
|
||||
return mNetworkService.getProgressResponse(page.getImageUrl(), mRequestHeaders, page);
|
||||
}
|
||||
|
||||
public void savePageList(String chapterUrl, List<Page> pages) {
|
||||
if (pages != null)
|
||||
mCacheManager.putPageUrlsToDiskCache(chapterUrl, pages);
|
||||
|
@ -18,8 +18,11 @@ import java.util.List;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import de.greenrobot.event.EventBus;
|
||||
import eu.kanade.mangafeed.R;
|
||||
import eu.kanade.mangafeed.data.models.Chapter;
|
||||
import eu.kanade.mangafeed.data.services.DownloadService;
|
||||
import eu.kanade.mangafeed.events.DownloadChapterEvent;
|
||||
import eu.kanade.mangafeed.presenter.MangaChaptersPresenter;
|
||||
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
|
||||
import eu.kanade.mangafeed.ui.activity.ReaderActivity;
|
||||
@ -28,6 +31,8 @@ import eu.kanade.mangafeed.ui.adapter.ChaptersAdapter;
|
||||
import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment;
|
||||
import nucleus.factory.RequiresPresenter;
|
||||
import rx.Observable;
|
||||
import rx.Subscription;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
@RequiresPresenter(MangaChaptersPresenter.class)
|
||||
public class MangaChaptersFragment extends BaseRxFragment<MangaChaptersPresenter> implements
|
||||
@ -39,6 +44,7 @@ public class MangaChaptersFragment extends BaseRxFragment<MangaChaptersPresenter
|
||||
private ChaptersAdapter adapter;
|
||||
|
||||
private ActionMode actionMode;
|
||||
private Subscription downloadSubscription;
|
||||
|
||||
public static Fragment newInstance() {
|
||||
return new MangaChaptersFragment();
|
||||
@ -64,6 +70,15 @@ public class MangaChaptersFragment extends BaseRxFragment<MangaChaptersPresenter
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
if (!DownloadService.isRunning(getActivity())) {
|
||||
Intent intent = DownloadService.getStartIntent(getActivity());
|
||||
getActivity().startService(intent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.chapters, menu);
|
||||
@ -130,6 +145,9 @@ public class MangaChaptersFragment extends BaseRxFragment<MangaChaptersPresenter
|
||||
case R.id.action_mark_as_unread:
|
||||
getPresenter().markChaptersRead(getSelectedChapters(), false);
|
||||
return true;
|
||||
case R.id.action_download:
|
||||
onDownloadChapters();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -188,4 +206,20 @@ public class MangaChaptersFragment extends BaseRxFragment<MangaChaptersPresenter
|
||||
private void setContextTitle(int count) {
|
||||
actionMode.setTitle(getString(R.string.selected_chapters_title, count));
|
||||
}
|
||||
|
||||
private void onDownloadChapters() {
|
||||
if (downloadSubscription != null && !downloadSubscription.isUnsubscribed()) {
|
||||
downloadSubscription.unsubscribe();
|
||||
downloadSubscription = null;
|
||||
}
|
||||
|
||||
downloadSubscription = getSelectedChapters()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(chapter -> {
|
||||
EventBus.getDefault().post(
|
||||
new DownloadChapterEvent(getPresenter().getManga(), chapter));
|
||||
downloadSubscription.unsubscribe();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import okio.BufferedSource;
|
||||
import okio.Okio;
|
||||
|
||||
public final class DiskUtils {
|
||||
private static final Pattern DIR_SEPORATOR = Pattern.compile("/");
|
||||
private static final Pattern DIR_SEPARATOR = Pattern.compile("/");
|
||||
|
||||
private DiskUtils() {
|
||||
throw new AssertionError();
|
||||
@ -58,7 +58,7 @@ public final class DiskUtils {
|
||||
rawUserId = "";
|
||||
} else {
|
||||
final String path = Environment.getExternalStorageDirectory().getAbsolutePath();
|
||||
final String[] folders = DIR_SEPORATOR.split(path);
|
||||
final String[] folders = DIR_SEPARATOR.split(path);
|
||||
final String lastFolder = folders[folders.length - 1];
|
||||
boolean isDigit = false;
|
||||
|
||||
@ -114,18 +114,17 @@ public final class DiskUtils {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static File saveBufferedSourceToDirectory(BufferedSource bufferedSource, String directory, String name) throws IOException {
|
||||
File fileDirectory = new File(directory);
|
||||
if (!fileDirectory.exists()) {
|
||||
if (!fileDirectory.mkdirs()) {
|
||||
public static File saveBufferedSourceToDirectory(BufferedSource bufferedSource, File directory, String name) throws IOException {
|
||||
if (!directory.exists()) {
|
||||
if (!directory.mkdirs()) {
|
||||
throw new IOException("Failed Creating Directory");
|
||||
}
|
||||
}
|
||||
|
||||
File writeFile = new File(fileDirectory, name);
|
||||
File writeFile = new File(directory, name);
|
||||
if (writeFile.exists()) {
|
||||
if (writeFile.delete()) {
|
||||
writeFile = new File(fileDirectory, name);
|
||||
writeFile = new File(directory, name);
|
||||
} else {
|
||||
throw new IOException("Failed Deleting Existing File for Overwrite");
|
||||
}
|
||||
|
Reference in New Issue
Block a user