Browse Source

Add a temporary way to select download directory

inorichi 9 years ago
parent
commit
62ae572c72

+ 11 - 9
app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java

@@ -24,10 +24,12 @@ public class DownloadManager {
 
     private Context context;
     private SourceManager sourceManager;
+    private PreferencesHelper preferences;
 
-    public DownloadManager(Context context, SourceManager sourceManager) {
+    public DownloadManager(Context context, SourceManager sourceManager, PreferencesHelper preferences) {
         this.context = context;
         this.sourceManager = sourceManager;
+        this.preferences = preferences;
 
         initializeDownloadSubscription();
     }
@@ -48,7 +50,8 @@ public class DownloadManager {
 
     private Observable<Page> downloadChapter(Manga manga, Chapter chapter) {
         final Source source = sourceManager.get(manga.source);
-        final File chapterDirectory = new File(getDownloadsDirectory(), getChapterDirectory(chapter));
+        final File chapterDirectory = new File(
+                preferences.getDownloadsDirectory(), getChapterDirectory(source, manga, chapter));
 
         return source
                 .pullPageListFromNetwork(chapter.url)
@@ -62,13 +65,12 @@ public class DownloadManager {
                 .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 getChapterDirectory(Source source, Manga manga, Chapter chapter) {
+        return source.getName() +
+                File.separator +
+                manga.title.replaceAll("[^a-zA-Z0-9.-]", "_") +
+                File.separator +
+                chapter.name.replaceAll("[^a-zA-Z0-9.-]", "_");
     }
 
     private String getImageFilename(Page page) {

+ 2 - 0
app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java

@@ -19,6 +19,7 @@ import eu.kanade.mangafeed.presenter.SourcePresenter;
 import eu.kanade.mangafeed.sources.base.Source;
 import eu.kanade.mangafeed.ui.activity.ReaderActivity;
 import eu.kanade.mangafeed.ui.fragment.SettingsAccountsFragment;
+import eu.kanade.mangafeed.ui.fragment.SettingsDownloadsFragment;
 
 @Singleton
 @Component(
@@ -39,6 +40,7 @@ public interface AppComponent {
 
     void inject(ReaderActivity readerActivity);
     void inject(SettingsAccountsFragment settingsAccountsFragment);
+    void inject(SettingsDownloadsFragment settingsDownloadsFragment);
 
     void inject(Source source);
 

+ 3 - 2
app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java

@@ -51,8 +51,9 @@ public class DataModule {
 
     @Provides
     @Singleton
-    DownloadManager provideDownloadManager(Application app, SourceManager sourceManager) {
-        return new DownloadManager(app, sourceManager);
+    DownloadManager provideDownloadManager(
+            Application app, SourceManager sourceManager, PreferencesHelper preferences) {
+        return new DownloadManager(app, sourceManager, preferences);
     }
 
 }

+ 56 - 0
app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsDownloadsFragment.java

@@ -0,0 +1,56 @@
+package eu.kanade.mangafeed.ui.fragment;
+
+import android.os.Bundle;
+import android.preference.ListPreference;
+import android.preference.PreferenceFragment;
+import android.preference.PreferenceScreen;
+
+import javax.inject.Inject;
+
+import eu.kanade.mangafeed.App;
+import eu.kanade.mangafeed.R;
+import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
+import eu.kanade.mangafeed.ui.activity.base.BaseActivity;
+import eu.kanade.mangafeed.util.DiskUtils;
+
+public class SettingsDownloadsFragment extends PreferenceFragment {
+
+    @Inject PreferencesHelper preferences;
+
+    public static SettingsDownloadsFragment newInstance() {
+        return new SettingsDownloadsFragment();
+    }
+
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        App.get(getActivity()).getComponent().inject(this);
+
+        addPreferencesFromResource(R.xml.pref_downloads);
+
+        PreferenceScreen screen = getPreferenceScreen();
+
+        ListPreference directoriesPref = new ListPreference(getActivity(), null);
+
+        String[] externalDirs = DiskUtils.getStorageDirectories(getActivity());
+        directoriesPref.setKey(getString(R.string.pref_download_directory_key));
+        directoriesPref.setTitle(R.string.pref_download_directory);
+        directoriesPref.setEntryValues(externalDirs);
+        directoriesPref.setEntries(externalDirs);
+        directoriesPref.setSummary(preferences.getDownloadsDirectory());
+
+        directoriesPref.setOnPreferenceChangeListener((preference, newValue) -> {
+            preference.setSummary(newValue.toString());
+            return true;
+        });
+
+        screen.addPreference(directoriesPref);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        ((BaseActivity)getActivity())
+                .setToolbarTitle(getString(R.string.pref_category_downloads));
+    }
+
+}

+ 3 - 0
app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsMainFragment.java

@@ -17,6 +17,9 @@ public class SettingsMainFragment extends PreferenceFragment {
                 SettingsNestedFragment.newInstance(
                         R.xml.pref_reader, R.string.pref_category_reader));
 
+        registerSubpreference(R.string.pref_category_downloads_key,
+                SettingsDownloadsFragment.newInstance());
+
         registerSubpreference(R.string.pref_category_accounts_key,
                 SettingsAccountsFragment.newInstance());
     }

+ 1 - 0
app/src/main/res/values/keys.xml

@@ -2,6 +2,7 @@
 <resources>
     <string name="pref_category_reader_key">pref_category_reader_key</string>
     <string name="pref_category_accounts_key">pref_category_accounts_key</string>
+    <string name="pref_category_downloads_key">pref_category_downloads_key</string>
     <string name="pref_fullscreen_key">pref_fullscreen_key</string>
     <string name="pref_default_viewer_key">pref_default_viewer_key</string>
     <string name="pref_download_directory_key">pref_download_directory_key</string>

+ 6 - 2
app/src/main/res/values/strings.xml

@@ -52,6 +52,7 @@
     <string name="title_activity_settings">Settings</string>
     <string name="pref_category_reader">Reader</string>
     <string name="pref_category_accounts">Accounts</string>
+    <string name="pref_category_downloads">Downloads</string>
 
     <string name="pref_fullscreen_mode">Read in fullscreen</string>
 
@@ -61,6 +62,8 @@
     <string name="vertical_viewer">Vertical</string>
     <string name="webtoon_viewer">Webtoon (experimental)</string>
 
+    <string name="pref_download_directory">Downloads directory</string>
+
     <string name="accounts_login_title">Login for %1$s</string>
     <string name="username">Username</string>
     <string name="password">Password</string>
@@ -70,6 +73,8 @@
     <string name="success">Success</string>
     <string name="invalid_login">Login error</string>
     <string name="loading">Loading…</string>
+
+
     <string name="action_favorite">Add to favorites</string>
     <string name="action_remove_favorite">Remove from favorites</string>
     <string name="downloading">Downloading…</string>
@@ -80,13 +85,12 @@
     <string name="action_select_all">Select all</string>
     <string name="action_mark_as_read">Mark as read</string>
     <string name="action_mark_as_unread">Mark as unread</string>
+    <string name="action_download">Download</string>
     <string name="selected_chapters_title">Selected chapters: %1$d</string>
 
     <string name="notification_progress">Update progress: %1$d/%2$d</string>
     <string name="notification_completed">Update completed</string>
     <string name="notification_no_new_chapters">No new chapters found</string>
     <string name="notification_new_chapters">Found new chapters for:</string>
-    <string name="action_download">Download</string>
-
 
 </resources>

+ 5 - 0
app/src/main/res/xml/pref_downloads.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orderingFromXml="true">
+
+</PreferenceScreen>

+ 5 - 0
app/src/main/res/xml/pref_main.xml

@@ -6,6 +6,11 @@
         android:persistent="false"
         android:title="@string/pref_category_reader" />
 
+    <Preference
+        android:key="@string/pref_category_downloads_key"
+        android:persistent="false"
+        android:title="@string/pref_category_downloads" />
+    
     <Preference
         android:key="@string/pref_category_accounts_key"
         android:persistent="false"