implemented import worker

This commit is contained in:
daniel oeh 2012-07-23 14:10:48 +02:00
parent 7ce5cfc513
commit 85e171ed99
2 changed files with 105 additions and 0 deletions

View File

@ -131,5 +131,7 @@
<string name="start_import_label">Start import</string>
<string name="opml_import_label">OPML import</string>
<string name="opml_directory_error">ERROR!</string>
<string name="reading_opml_label">Reading OPML file</string>
<string name="opml_reader_error">An error has occured while reading the opml document:</string>
</resources>

View File

@ -0,0 +1,103 @@
package de.danoeh.antennapod.asynctask;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParserException;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.opml.OpmlElement;
import de.danoeh.antennapod.opml.OpmlReader;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.util.Log;
public class OpmlImportWorker extends
AsyncTask<Void, Void, ArrayList<OpmlElement>> {
private static final String TAG = "OpmlImportWorker";
private Context context;
private File file; // path to opml file
private Exception exception;
private ProgressDialog progDialog;
public OpmlImportWorker(Context context, File file) {
super();
this.context = context;
this.file = file;
}
@Override
protected ArrayList<OpmlElement> doInBackground(Void... params) {
if (AppConfig.DEBUG)
Log.d(TAG, "Starting background work");
FileReader reader = null;
// Create reader
try {
reader = new FileReader(file);
if (AppConfig.DEBUG) Log.d(TAG, "Parsing " + file.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
exception = e;
return null;
}
OpmlReader opmlReader = new OpmlReader();
try {
ArrayList<OpmlElement> result = opmlReader.readDocument(reader);
reader.close();
return result;
} catch (XmlPullParserException e) {
e.printStackTrace();
exception = e;
return null;
} catch (IOException e) {
e.printStackTrace();
exception = e;
return null;
}
}
@Override
protected void onPostExecute(ArrayList<OpmlElement> result) {
progDialog.dismiss();
if (exception != null) {
if (AppConfig.DEBUG)
Log.d(TAG,
"An error occured while trying to parse the opml document");
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(R.string.error_label);
alert.setMessage(context.getString(R.string.opml_reader_error)
+ exception.getMessage());
alert.setNeutralButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
}
@Override
protected void onPreExecute() {
progDialog = new ProgressDialog(context);
progDialog.setMessage(context.getString(R.string.reading_opml_label));
progDialog.setIndeterminate(true);
progDialog.setCancelable(false);
progDialog.show();
}
}