summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/asynctask/OpmlExportWorker.java
blob: 8f866f4ff208e18e0bf69c27098578f172a357e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package de.danoeh.antennapod.asynctask;

import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import de.danoeh.antennapod.core.opml.OpmlWriter;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.LangUtils;
import rx.Observable;

/**
 * Writes an OPML file into the export directory in the background.
 */
public class OpmlExportWorker {

    public static final String EXPORT_DIR = "export/";
    private static final String TAG = "OpmlExportWorker";
    private static final String DEFAULT_OUTPUT_NAME = "antennapod-feeds.opml";
    private File output;

    public OpmlExportWorker() {
        this(new File(UserPreferences.getDataFolder(EXPORT_DIR), DEFAULT_OUTPUT_NAME));
    }

    public OpmlExportWorker(File output) {
        this.output = output;
    }

    public Observable<File> exportObservable() {
        if (output.exists()) {
            Log.w(TAG, "Overwriting previously exported file.");
            output.delete();
        }
        OpmlWriter opmlWriter = new OpmlWriter();
        return Observable.create(subscriber -> {
            OutputStreamWriter writer = null;
            try {
                writer = new OutputStreamWriter(new FileOutputStream(output), LangUtils.UTF_8);
                opmlWriter.writeDocument(DBReader.getFeedList(), writer);
                subscriber.onNext(output);
            } catch (IOException e) {
                subscriber.onError(e);
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e) {
                        subscriber.onError(e);
                    }
                }
                subscriber.onCompleted();
            }
        });
    }

}