diff --git a/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportActivity.java index d236a7755..2b8064270 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportActivity.java @@ -21,8 +21,8 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.asynctask.OpmlImportWorker; import de.danoeh.antennapod.core.export.opml.OpmlElement; +import de.danoeh.antennapod.core.export.opml.OpmlReader; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.DownloadRequestException; @@ -30,6 +30,7 @@ import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.databinding.OpmlSelectionBinding; import de.danoeh.antennapod.model.feed.Feed; import io.reactivex.Completable; +import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import org.apache.commons.io.ByteOrderMark; @@ -53,6 +54,7 @@ public class OpmlImportActivity extends AppCompatActivity { private ArrayAdapter listAdapter; private MenuItem selectAll; private MenuItem deselectAll; + private ArrayList readElements; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -71,7 +73,7 @@ public class OpmlImportActivity extends AppCompatActivity { checkedCount++; } } - if(checkedCount == listAdapter.getCount()) { + if (checkedCount == listAdapter.getCount()) { selectAll.setVisible(false); deselectAll.setVisible(true); } else { @@ -92,7 +94,7 @@ public class OpmlImportActivity extends AppCompatActivity { if (!checked.valueAt(i)) { continue; } - OpmlElement element = OpmlImportHolder.getReadElements().get(checked.keyAt(i)); + OpmlElement element = readElements.get(checked.keyAt(i)); Feed feed = new Feed(element.getXmlUrl(), null, element.getText()); try { requester.downloadFeed(getApplicationContext(), feed); @@ -151,8 +153,8 @@ public class OpmlImportActivity extends AppCompatActivity { private List getTitleList() { List result = new ArrayList<>(); - if (OpmlImportHolder.getReadElements() != null) { - for (OpmlElement element : OpmlImportHolder.getReadElements()) { + if (readElements != null) { + for (OpmlElement element : readElements) { result.add(element.getText()); } } @@ -220,39 +222,36 @@ public class OpmlImportActivity extends AppCompatActivity { /** Starts the import process. */ private void startImport() { viewBinding.progressBar.setVisibility(View.VISIBLE); - try { + + Observable.fromCallable(() -> { InputStream opmlFileStream = getContentResolver().openInputStream(uri); BOMInputStream bomInputStream = new BOMInputStream(opmlFileStream); ByteOrderMark bom = bomInputStream.getBOM(); String charsetName = (bom == null) ? "UTF-8" : bom.getCharsetName(); Reader reader = new InputStreamReader(bomInputStream, charsetName); - - OpmlImportWorker importWorker = new OpmlImportWorker(this, reader) { - - @Override - protected void onPostExecute(ArrayList result) { - super.onPostExecute(result); - if (result != null) { - Log.d(TAG, "Parsing was successful"); - OpmlImportHolder.setReadElements(result); - listAdapter = new ArrayAdapter<>(OpmlImportActivity.this, - android.R.layout.simple_list_item_multiple_choice, - getTitleList()); - viewBinding.feedlist.setAdapter(listAdapter); - } else { - Log.d(TAG, "Parser error occurred"); - } - viewBinding.progressBar.setVisibility(View.GONE); - } - }; - importWorker.executeAsync(); - } catch (Exception e) { - Log.d(TAG, Log.getStackTraceString(e)); - String message = getString(R.string.opml_reader_error); - new AlertDialog.Builder(this) - .setMessage(message + " " + e.getMessage()) - .setPositiveButton(android.R.string.ok, null) - .show(); - } + OpmlReader opmlReader = new OpmlReader(); + ArrayList result = opmlReader.readDocument(reader); + reader.close(); + return result; + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + result -> { + viewBinding.progressBar.setVisibility(View.GONE); + Log.d(TAG, "Parsing was successful"); + readElements = result; + listAdapter = new ArrayAdapter<>(OpmlImportActivity.this, + android.R.layout.simple_list_item_multiple_choice, + getTitleList()); + viewBinding.feedlist.setAdapter(listAdapter); + }, e -> { + viewBinding.progressBar.setVisibility(View.GONE); + AlertDialog.Builder alert = new AlertDialog.Builder(this); + alert.setTitle(R.string.error_label); + alert.setMessage(getString(R.string.opml_reader_error) + e.getMessage()); + alert.setNeutralButton(android.R.string.ok, (dialog, which) -> dialog.dismiss()); + alert.create().show(); + }); } } diff --git a/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportHolder.java b/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportHolder.java deleted file mode 100644 index dc5570dc0..000000000 --- a/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportHolder.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.danoeh.antennapod.activity; - -import java.util.ArrayList; - -import de.danoeh.antennapod.core.export.opml.OpmlElement; - -/** - * Hold infos gathered by Ompl-Import - *

- * Created with IntelliJ IDEA. - * User: ligi - * Date: 1/23/13 - * Time: 2:15 PM - */ -public class OpmlImportHolder { - - private OpmlImportHolder(){} - - private static ArrayList readElements; - - public static ArrayList getReadElements() { - return readElements; - } - - public static void setReadElements(ArrayList _readElements) { - readElements = _readElements; - } - - -} - diff --git a/app/src/main/java/de/danoeh/antennapod/asynctask/OpmlImportWorker.java b/app/src/main/java/de/danoeh/antennapod/asynctask/OpmlImportWorker.java deleted file mode 100644 index e037eb392..000000000 --- a/app/src/main/java/de/danoeh/antennapod/asynctask/OpmlImportWorker.java +++ /dev/null @@ -1,93 +0,0 @@ -package de.danoeh.antennapod.asynctask; - -import android.app.ProgressDialog; -import android.content.Context; -import android.os.AsyncTask; -import androidx.appcompat.app.AlertDialog; -import android.util.Log; - -import org.xmlpull.v1.XmlPullParserException; - -import java.io.IOException; -import java.io.Reader; -import java.util.ArrayList; - -import de.danoeh.antennapod.core.R; -import de.danoeh.antennapod.core.export.opml.OpmlElement; -import de.danoeh.antennapod.core.export.opml.OpmlReader; - -public class OpmlImportWorker extends AsyncTask> { - private static final String TAG = "OpmlImportWorker"; - - private final Context context; - private Exception exception; - private ProgressDialog progDialog; - - private final Reader reader; - - public OpmlImportWorker(Context context, Reader reader) { - super(); - this.context = context; - this.reader = reader; - } - - @Override - protected ArrayList doInBackground(Void... params) { - Log.d(TAG, "Starting background work"); - - if (reader == null) { - return null; - } - - OpmlReader opmlReader = new OpmlReader(); - try { - ArrayList 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 result) { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - progDialog.dismiss(); - if (exception != null) { - Log.d(TAG, "An error occurred 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, (dialog, which) -> dialog.dismiss()); - alert.create().show(); - } - } - - @Override - protected void onPreExecute() { - progDialog = new ProgressDialog(context); - progDialog.setMessage(context.getString(R.string.please_wait)); - progDialog.setIndeterminate(true); - progDialog.setCancelable(false); - progDialog.show(); - } - - public void executeAsync() { - executeOnExecutor(THREAD_POOL_EXECUTOR); - } - -} diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 9523ff5f2..86f79318a 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -105,7 +105,6 @@ Shownotes swipe up to read shownotes \u0020episodes - Processing Close Retry Include in auto downloads