diff --git a/AndroidManifest.xml b/AndroidManifest.xml index d223e174e..a0e277a85 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -166,6 +166,63 @@ android:configChanges="keyboardHidden|orientation" android:label="@string/opml_import_label" > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java new file mode 100644 index 000000000..d285cbe15 --- /dev/null +++ b/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java @@ -0,0 +1,76 @@ +package de.danoeh.antennapod.activity; + +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; +import com.actionbarsherlock.app.SherlockActivity; +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuItem; +import de.danoeh.antennapod.AppConfig; +import de.danoeh.antennapod.PodcastApp; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.asynctask.OpmlFeedQueuer; +import de.danoeh.antennapod.asynctask.OpmlImportWorker; +import de.danoeh.antennapod.opml.OpmlElement; +import de.danoeh.antennapod.util.StorageUtils; + +import java.io.File; +import java.util.ArrayList; + +/** + * Base activity for Opml Import - e.g. with code what to do afterwards + * */ +public class OpmlImportBaseActivity extends SherlockActivity { + + private static final String TAG = "OpmlImportBaseActivity"; + + /** + * Handles the choices made by the user in the OpmlFeedChooserActivity and + * starts the OpmlFeedQueuer if necessary. + */ + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (AppConfig.DEBUG) + Log.d(TAG, "Received result"); + if (resultCode == RESULT_CANCELED) { + if (AppConfig.DEBUG) + Log.d(TAG, "Activity was cancelled"); + if (finishWhenCanceled()) + finish(); + } else { + int[] selected = data + .getIntArrayExtra(OpmlFeedChooserActivity.EXTRA_SELECTED_ITEMS); + if (selected != null && selected.length > 0) { + OpmlFeedQueuer queuer = new OpmlFeedQueuer(this, selected) { + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + Intent intent = new Intent(OpmlImportBaseActivity.this, MainActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP + | Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + } + + }; + queuer.executeAsync(); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "No items were selected"); + } + } + } + + protected boolean finishWhenCanceled() { + return false; + } + + +} diff --git a/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java new file mode 100644 index 000000000..7366391d5 --- /dev/null +++ b/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java @@ -0,0 +1,84 @@ +package de.danoeh.antennapod.activity; + +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuItem; +import de.danoeh.antennapod.AppConfig; +import de.danoeh.antennapod.PodcastApp; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.asynctask.OpmlImportWorker; +import de.danoeh.antennapod.opml.OpmlElement; +import de.danoeh.antennapod.util.StorageUtils; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; + +/** Lets the user start the OPML-import process. */ +public class OpmlImportFromIntentActivity extends OpmlImportBaseActivity { + private static final String TAG = "OpmlImportFromPathActivity"; + + public static final String IMPORT_DIR = "import/"; + + private OpmlImportWorker importWorker; + + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(PodcastApp.getThemeResourceId()); + super.onCreate(savedInstanceState); + + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + try { + URL mOpmlURL = new URL(getIntent().getData().toString()); + BufferedReader in = new BufferedReader(new InputStreamReader(mOpmlURL.openStream())); + startImport(in); + } catch (Exception e) { + new AlertDialog.Builder(this).setMessage("Cannot open XML - Reason: " + e.getMessage()).show(); + } + + } + + + /** Starts the import process. */ + private void startImport(Reader reader) { + + if (reader != null) { + importWorker = new OpmlImportWorker(this, reader) { + + @Override + protected void onPostExecute(ArrayList result) { + super.onPostExecute(result); + if (result != null) { + if (AppConfig.DEBUG) + Log.d(TAG, "Parsing was successful"); + OpmlImportHolder.setReadElements(result); + startActivityForResult(new Intent( + OpmlImportFromIntentActivity.this, + OpmlFeedChooserActivity.class), 0); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Parser error occured"); + } + } + }; + importWorker.executeAsync(); + } + } + + @Override + protected boolean finishWhenCanceled() { + return true; + } + +} diff --git a/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java index a02f0e67f..374612a1b 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java @@ -27,8 +27,8 @@ import de.danoeh.antennapod.opml.OpmlElement; import de.danoeh.antennapod.util.StorageUtils; /** Lets the user start the OPML-import process. */ -public class OpmlImportFromPathActivity extends SherlockActivity { - private static final String TAG = "OpmlImportActivity"; +public class OpmlImportFromPathActivity extends OpmlImportBaseActivity { + private static final String TAG = "OpmlImportFromPathActivity"; public static final String IMPORT_DIR = "import/"; @@ -189,40 +189,5 @@ public class OpmlImportFromPathActivity extends SherlockActivity { dialog.create().show(); } - /** - * Handles the choices made by the user in the OpmlFeedChooserActivity and - * starts the OpmlFeedQueuer if necessary. - */ - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (AppConfig.DEBUG) - Log.d(TAG, "Received result"); - if (resultCode == RESULT_CANCELED) { - if (AppConfig.DEBUG) - Log.d(TAG, "Activity was cancelled"); - } else { - int[] selected = data - .getIntArrayExtra(OpmlFeedChooserActivity.EXTRA_SELECTED_ITEMS); - if (selected != null && selected.length > 0) { - OpmlFeedQueuer queuer = new OpmlFeedQueuer(this, selected) { - - @Override - protected void onPostExecute(Void result) { - super.onPostExecute(result); - Intent intent = new Intent(OpmlImportFromPathActivity.this, MainActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP - | Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); - } - - }; - queuer.executeAsync(); - } else { - if (AppConfig.DEBUG) - Log.d(TAG, "No items were selected"); - } - } - } - }