diff --git a/res/values/strings.xml b/res/values/strings.xml index da70992e3..944d17bb7 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -147,6 +147,12 @@ Search Found in title Choose file to import + OPML export + Exporting... + You have no feeds + Export error + Opml export successful. + The .opml file was written to:\u0020 \ No newline at end of file diff --git a/src/de/danoeh/antennapod/PodcastApp.java b/src/de/danoeh/antennapod/PodcastApp.java index 4cd88ff39..48afb7862 100644 --- a/src/de/danoeh/antennapod/PodcastApp.java +++ b/src/de/danoeh/antennapod/PodcastApp.java @@ -21,6 +21,7 @@ public class PodcastApp extends Application implements private static final String TAG = "PodcastApp"; public static final String PREF_NAME = "AntennapodPrefs"; + public static final String EXPORT_DIR = "export/"; public static final String PREF_PAUSE_ON_HEADSET_DISCONNECT = "prefPauseOnHeadsetDisconnect"; public static final String PREF_FOLLOW_QUEUE = "prefFollowQueue"; diff --git a/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java b/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java new file mode 100644 index 000000000..60493169a --- /dev/null +++ b/src/de/danoeh/antennapod/asynctask/OpmlExportWorker.java @@ -0,0 +1,104 @@ +package de.danoeh.antennapod.asynctask; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import android.annotation.SuppressLint; +import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.AsyncTask; +import android.util.Log; +import de.danoeh.antennapod.PodcastApp; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.feed.FeedManager; +import de.danoeh.antennapod.opml.OpmlWriter; + +/** Writes an OPML file into the export directory in the background. */ +public class OpmlExportWorker extends AsyncTask { + private static final String TAG = "OpmlExportWorker"; + private static final String DEFAULT_OUTPUT_NAME = "AntennaPod-Feeds.opml"; + private Context context; + private File output; + + private ProgressDialog progDialog; + private Exception exception; + + public OpmlExportWorker(Context context, File output) { + this.context = context; + this.output = output; + } + + public OpmlExportWorker(Context context) { + this.context = context; + } + + @Override + protected Void doInBackground(Void... params) { + OpmlWriter opmlWriter = new OpmlWriter(); + if (output == null) { + output = new File( + context.getExternalFilesDir(PodcastApp.EXPORT_DIR), + DEFAULT_OUTPUT_NAME); + if (output.exists()) { + Log.w(TAG, "Overwriting previously exported file."); + output.delete(); + } + } + try { + FileWriter writer = new FileWriter(output); + opmlWriter.writeDocument(FeedManager.getInstance().getFeeds(), + writer); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + exception = e; + } + return null; + } + + @Override + protected void onPostExecute(Void result) { + progDialog.dismiss(); + AlertDialog.Builder alert = new AlertDialog.Builder(context) + .setNeutralButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, + int which) { + dialog.dismiss(); + } + }); + if (exception != null) { + alert.setTitle(R.string.export_error_label); + alert.setMessage(exception.getMessage()); + } else { + alert.setTitle(R.string.opml_export_success_title); + alert.setMessage(context + .getString(R.string.opml_export_success_sum) + + output.toString()); + } + alert.create().show(); + } + + @Override + protected void onPreExecute() { + progDialog = new ProgressDialog(context); + progDialog.setMessage(context.getString(R.string.exporting_label)); + progDialog.setIndeterminate(true); + progDialog.show(); + } + + @SuppressLint("NewApi") + public void executeAsync() { + if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { + executeOnExecutor(THREAD_POOL_EXECUTOR); + } else { + execute(); + } + } + +}