From 60546a7bd74357726d025123449254cb4fd216b5 Mon Sep 17 00:00:00 2001 From: ligi Date: Wed, 23 Jan 2013 14:44:28 +0100 Subject: make room for another way of Opml import --- .../antennapod/activity/AddFeedActivity.java | 2 +- .../activity/OpmlFeedChooserActivity.java | 4 +- .../antennapod/activity/OpmlImportActivity.java | 233 --------------------- .../activity/OpmlImportFromPathActivity.java | 228 ++++++++++++++++++++ .../antennapod/activity/OpmlImportHolder.java | 29 +++ .../antennapod/asynctask/OpmlFeedQueuer.java | 5 +- .../antennapod/asynctask/OpmlImportWorker.java | 37 ++-- 7 files changed, 282 insertions(+), 256 deletions(-) delete mode 100644 src/de/danoeh/antennapod/activity/OpmlImportActivity.java create mode 100644 src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java create mode 100644 src/de/danoeh/antennapod/activity/OpmlImportHolder.java (limited to 'src/de') diff --git a/src/de/danoeh/antennapod/activity/AddFeedActivity.java b/src/de/danoeh/antennapod/activity/AddFeedActivity.java index e90387cd3..44486b5ef 100644 --- a/src/de/danoeh/antennapod/activity/AddFeedActivity.java +++ b/src/de/danoeh/antennapod/activity/AddFeedActivity.java @@ -73,7 +73,7 @@ public class AddFeedActivity extends SherlockActivity { @Override public void onClick(View v) { startActivity(new Intent(AddFeedActivity.this, - OpmlImportActivity.class)); + OpmlImportFromPathActivity.class)); } }); diff --git a/src/de/danoeh/antennapod/activity/OpmlFeedChooserActivity.java b/src/de/danoeh/antennapod/activity/OpmlFeedChooserActivity.java index 582673dbb..2301d2f0f 100644 --- a/src/de/danoeh/antennapod/activity/OpmlFeedChooserActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlFeedChooserActivity.java @@ -90,8 +90,8 @@ public class OpmlFeedChooserActivity extends SherlockActivity { private List getTitleList() { List result = new ArrayList(); - if (OpmlImportActivity.getReadElements() != null) { - for (OpmlElement element : OpmlImportActivity.getReadElements()) { + if (OpmlImportHolder.getReadElements() != null) { + for (OpmlElement element : OpmlImportHolder.getReadElements()) { result.add(element.getText()); } diff --git a/src/de/danoeh/antennapod/activity/OpmlImportActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportActivity.java deleted file mode 100644 index 5ec2e8538..000000000 --- a/src/de/danoeh/antennapod/activity/OpmlImportActivity.java +++ /dev/null @@ -1,233 +0,0 @@ -package de.danoeh.antennapod.activity; - -import java.io.File; -import java.util.ArrayList; - -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; - -/** Lets the user start the OPML-import process. */ -public class OpmlImportActivity extends SherlockActivity { - private static final String TAG = "OpmlImportActivity"; - - public static final String IMPORT_DIR = "import/"; - - private TextView txtvPath; - private Button butStart; - private String importPath; - - private OpmlImportWorker importWorker; - - private static ArrayList readElements; - - @Override - protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); - super.onCreate(savedInstanceState); - - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setContentView(R.layout.opml_import); - - txtvPath = (TextView) findViewById(R.id.txtvPath); - butStart = (Button) findViewById(R.id.butStartImport); - - butStart.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - checkFolderForFiles(); - } - - }); - } - - @Override - protected void onResume() { - super.onResume(); - StorageUtils.checkStorageAvailability(this); - setImportPath(); - } - - /** - * Sets the importPath variable and makes txtvPath display the import - * directory. - */ - private void setImportPath() { - File importDir = PodcastApp.getDataFolder(this, IMPORT_DIR); - boolean success = true; - if (!importDir.exists()) { - if (AppConfig.DEBUG) - Log.d(TAG, "Import directory doesn't exist. Creating..."); - success = importDir.mkdir(); - if (!success) { - Log.e(TAG, "Could not create directory"); - } - } - if (success) { - txtvPath.setText(importDir.toString()); - importPath = importDir.toString(); - } else { - txtvPath.setText(R.string.opml_directory_error); - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - default: - return false; - } - } - - /** - * Looks at the contents of the import directory and decides what to do. If - * more than one file is in the directory, a dialog will be created to let - * the user choose which item to import - * */ - private void checkFolderForFiles() { - File dir = new File(importPath); - if (dir.isDirectory()) { - File[] fileList = dir.listFiles(); - if (fileList.length == 1) { - if (AppConfig.DEBUG) - Log.d(TAG, "Found one file, choosing that one."); - startImport(fileList[0]); - } else if (fileList.length > 1) { - Log.w(TAG, "Import directory contains more than one file."); - askForFile(dir); - } else { - Log.e(TAG, "Import directory is empty"); - Toast toast = Toast - .makeText(this, R.string.opml_import_error_dir_empty, - Toast.LENGTH_LONG); - toast.show(); - } - } - } - - /** Starts the import process. */ - private void startImport(File file) { - - if (file != null) { - importWorker = new OpmlImportWorker(this, file) { - - @Override - protected void onPostExecute(ArrayList result) { - super.onPostExecute(result); - if (result != null) { - if (AppConfig.DEBUG) - Log.d(TAG, "Parsing was successful"); - readElements = result; - startActivityForResult(new Intent( - OpmlImportActivity.this, - OpmlFeedChooserActivity.class), 0); - } else { - if (AppConfig.DEBUG) - Log.d(TAG, "Parser error occured"); - } - } - }; - importWorker.executeAsync(); - } - } - - /** - * Asks the user to choose from a list of files in a directory and returns - * his choice. - */ - private void askForFile(File dir) { - final File[] fileList = dir.listFiles(); - String[] fileNames = dir.list(); - - AlertDialog.Builder dialog = new AlertDialog.Builder(this); - dialog.setTitle(R.string.choose_file_to_import_label); - dialog.setNeutralButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (AppConfig.DEBUG) - Log.d(TAG, "Dialog was cancelled"); - dialog.dismiss(); - } - }); - dialog.setItems(fileNames, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (AppConfig.DEBUG) - Log.d(TAG, "File at index " + which + " was chosen"); - dialog.dismiss(); - startImport(fileList[which]); - } - }); - 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(OpmlImportActivity.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"); - } - } - } - - public static ArrayList getReadElements() { - return readElements; - } - -} diff --git a/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java new file mode 100644 index 000000000..a02f0e67f --- /dev/null +++ b/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java @@ -0,0 +1,228 @@ +package de.danoeh.antennapod.activity; + +import java.io.File; +import java.util.ArrayList; + +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; + +/** Lets the user start the OPML-import process. */ +public class OpmlImportFromPathActivity extends SherlockActivity { + private static final String TAG = "OpmlImportActivity"; + + public static final String IMPORT_DIR = "import/"; + + private TextView txtvPath; + private Button butStart; + private String importPath; + + private OpmlImportWorker importWorker; + + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(PodcastApp.getThemeResourceId()); + super.onCreate(savedInstanceState); + + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setContentView(R.layout.opml_import); + + txtvPath = (TextView) findViewById(R.id.txtvPath); + butStart = (Button) findViewById(R.id.butStartImport); + + butStart.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + checkFolderForFiles(); + } + + }); + } + + @Override + protected void onResume() { + super.onResume(); + StorageUtils.checkStorageAvailability(this); + setImportPath(); + } + + /** + * Sets the importPath variable and makes txtvPath display the import + * directory. + */ + private void setImportPath() { + File importDir = PodcastApp.getDataFolder(this, IMPORT_DIR); + boolean success = true; + if (!importDir.exists()) { + if (AppConfig.DEBUG) + Log.d(TAG, "Import directory doesn't exist. Creating..."); + success = importDir.mkdir(); + if (!success) { + Log.e(TAG, "Could not create directory"); + } + } + if (success) { + txtvPath.setText(importDir.toString()); + importPath = importDir.toString(); + } else { + txtvPath.setText(R.string.opml_directory_error); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + default: + return false; + } + } + + /** + * Looks at the contents of the import directory and decides what to do. If + * more than one file is in the directory, a dialog will be created to let + * the user choose which item to import + * */ + private void checkFolderForFiles() { + File dir = new File(importPath); + if (dir.isDirectory()) { + File[] fileList = dir.listFiles(); + if (fileList.length == 1) { + if (AppConfig.DEBUG) + Log.d(TAG, "Found one file, choosing that one."); + startImport(fileList[0]); + } else if (fileList.length > 1) { + Log.w(TAG, "Import directory contains more than one file."); + askForFile(dir); + } else { + Log.e(TAG, "Import directory is empty"); + Toast toast = Toast + .makeText(this, R.string.opml_import_error_dir_empty, + Toast.LENGTH_LONG); + toast.show(); + } + } + } + + /** Starts the import process. */ + private void startImport(File file) { + + if (file != null) { + importWorker = new OpmlImportWorker(this, file) { + + @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( + OpmlImportFromPathActivity.this, + OpmlFeedChooserActivity.class), 0); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Parser error occured"); + } + } + }; + importWorker.executeAsync(); + } + } + + /** + * Asks the user to choose from a list of files in a directory and returns + * his choice. + */ + private void askForFile(File dir) { + final File[] fileList = dir.listFiles(); + String[] fileNames = dir.list(); + + AlertDialog.Builder dialog = new AlertDialog.Builder(this); + dialog.setTitle(R.string.choose_file_to_import_label); + dialog.setNeutralButton(android.R.string.cancel, + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + if (AppConfig.DEBUG) + Log.d(TAG, "Dialog was cancelled"); + dialog.dismiss(); + } + }); + dialog.setItems(fileNames, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + if (AppConfig.DEBUG) + Log.d(TAG, "File at index " + which + " was chosen"); + dialog.dismiss(); + startImport(fileList[which]); + } + }); + 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"); + } + } + } + + +} diff --git a/src/de/danoeh/antennapod/activity/OpmlImportHolder.java b/src/de/danoeh/antennapod/activity/OpmlImportHolder.java new file mode 100644 index 000000000..ec53ed7b6 --- /dev/null +++ b/src/de/danoeh/antennapod/activity/OpmlImportHolder.java @@ -0,0 +1,29 @@ +package de.danoeh.antennapod.activity; + +import de.danoeh.antennapod.opml.OpmlElement; + +import java.util.ArrayList; + +/** + * Hold infos gathered by Ompl-Import + *

+ * Created with IntelliJ IDEA. + * User: ligi + * Date: 1/23/13 + * Time: 2:15 PM + */ +public class OpmlImportHolder { + + private static ArrayList readElements; + + public static ArrayList getReadElements() { + return readElements; + } + + public static void setReadElements(ArrayList _readElements) { + readElements = _readElements; + } + + +} + diff --git a/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java b/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java index 08c038206..a3652e05d 100644 --- a/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java +++ b/src/de/danoeh/antennapod/asynctask/OpmlFeedQueuer.java @@ -7,7 +7,8 @@ import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.activity.OpmlImportActivity; +import de.danoeh.antennapod.activity.OpmlImportFromPathActivity; +import de.danoeh.antennapod.activity.OpmlImportHolder; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.opml.OpmlElement; import de.danoeh.antennapod.storage.DownloadRequestException; @@ -43,7 +44,7 @@ public class OpmlFeedQueuer extends AsyncTask { protected Void doInBackground(Void... params) { DownloadRequester requester = DownloadRequester.getInstance(); for (int idx = 0; idx < selection.length; idx++) { - OpmlElement element = OpmlImportActivity.getReadElements().get( + OpmlElement element = OpmlImportHolder.getReadElements().get( selection[idx]); Feed feed = new Feed(element.getXmlUrl(), new Date(), element.getText()); diff --git a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java index 7498b93de..f9b199b63 100644 --- a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java +++ b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java @@ -1,9 +1,6 @@ package de.danoeh.antennapod.asynctask; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import org.xmlpull.v1.XmlPullParserException; @@ -26,35 +23,39 @@ public class OpmlImportWorker extends private static final String TAG = "OpmlImportWorker"; private Context context; - private File file; // path to opml file private Exception exception; private ProgressDialog progDialog; + private Reader mReader; + public OpmlImportWorker(Context context, File file) { super(); this.context = context; - this.file = file; + + // Create reader + try { + mReader = new FileReader(file); + if (AppConfig.DEBUG) Log.d(TAG, "Parsing " + file.toString()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + exception = e; + } } @Override protected ArrayList 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; - } + + if (mReader==null) { + return null; + } + OpmlReader opmlReader = new OpmlReader(); try { - ArrayList result = opmlReader.readDocument(reader); - reader.close(); + ArrayList result = opmlReader.readDocument(mReader); + mReader.close(); return result; } catch (XmlPullParserException e) { e.printStackTrace(); -- cgit v1.2.3 From eb11da53c7404f83c32b0a1190d59eada37b0d02 Mon Sep 17 00:00:00 2001 From: ligi Date: Wed, 23 Jan 2013 15:50:51 +0100 Subject: add constructor to construct with reader and not only file - needed for URL imort later --- src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/de') diff --git a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java index f9b199b63..6d66b0fa0 100644 --- a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java +++ b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java @@ -43,6 +43,12 @@ public class OpmlImportWorker extends } } + public OpmlImportWorker(Context context, Reader reader) { + super(); + this.context = context; + this.mReader=reader; + } + @Override protected ArrayList doInBackground(Void... params) { if (AppConfig.DEBUG) -- cgit v1.2.3 From e54dbbcdce01d3180fe1cdbf245fd6ea2dfafcd8 Mon Sep 17 00:00:00 2001 From: ligi Date: Wed, 23 Jan 2013 16:01:31 +0100 Subject: import via intent working initially --- .../activity/OpmlImportBaseActivity.java | 76 ++++++++++++++++++++ .../activity/OpmlImportFromIntentActivity.java | 84 ++++++++++++++++++++++ .../activity/OpmlImportFromPathActivity.java | 39 +--------- 3 files changed, 162 insertions(+), 37 deletions(-) create mode 100644 src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java create mode 100644 src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java (limited to 'src/de') 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"); - } - } - } - } -- cgit v1.2.3 From ab9b244af45cffb568794232a6e1f82912d5bdb0 Mon Sep 17 00:00:00 2001 From: ligi Date: Wed, 23 Jan 2013 16:13:09 +0100 Subject: deduplication and cleanup -> preparing a pull request --- src/de/danoeh/antennapod/PodcastApp.java | 5 +- .../activity/OpmlImportBaseActivity.java | 45 +-- .../activity/OpmlImportFromIntentActivity.java | 50 ---- .../activity/OpmlImportFromPathActivity.java | 320 ++++++++++----------- .../antennapod/asynctask/OpmlImportWorker.java | 14 - 5 files changed, 180 insertions(+), 254 deletions(-) (limited to 'src/de') diff --git a/src/de/danoeh/antennapod/PodcastApp.java b/src/de/danoeh/antennapod/PodcastApp.java index 58dbf2a23..0e3fab80d 100644 --- a/src/de/danoeh/antennapod/PodcastApp.java +++ b/src/de/danoeh/antennapod/PodcastApp.java @@ -11,10 +11,9 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; -import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.util.Log; -import de.danoeh.antennapod.activity.OpmlImportActivity; +import de.danoeh.antennapod.activity.OpmlImportFromPathActivity; import de.danoeh.antennapod.asynctask.FeedImageLoader; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.feed.FeedMedia; @@ -94,7 +93,7 @@ public class PodcastApp extends Application implements * available */ private void createImportDirectory() { - File importDir = getDataFolder(this, OpmlImportActivity.IMPORT_DIR); + File importDir = getDataFolder(this, OpmlImportFromPathActivity.IMPORT_DIR); if (importDir != null) { if (importDir.exists()) { if (AppConfig.DEBUG) diff --git a/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java index d285cbe15..012f51c6d 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java @@ -1,27 +1,13 @@ 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.io.Reader; import java.util.ArrayList; /** @@ -30,8 +16,9 @@ import java.util.ArrayList; public class OpmlImportBaseActivity extends SherlockActivity { private static final String TAG = "OpmlImportBaseActivity"; + private OpmlImportWorker importWorker; - /** + /** * Handles the choices made by the user in the OpmlFeedChooserActivity and * starts the OpmlFeedQueuer if necessary. */ @@ -68,6 +55,32 @@ public class OpmlImportBaseActivity extends SherlockActivity { } } + /** Starts the import process. */ + protected 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( + OpmlImportBaseActivity.this, + OpmlFeedChooserActivity.class), 0); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Parser error occured"); + } + } + }; + importWorker.executeAsync(); + } + } + protected boolean finishWhenCanceled() { return false; } diff --git a/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java index 7366391d5..cbe69d48c 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportFromIntentActivity.java @@ -1,36 +1,13 @@ 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) { @@ -49,33 +26,6 @@ public class OpmlImportFromIntentActivity extends OpmlImportBaseActivity { } - - /** 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 374612a1b..bb5734b57 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java @@ -1,11 +1,7 @@ package de.danoeh.antennapod.activity; -import java.io.File; -import java.util.ArrayList; - import android.app.AlertDialog; import android.content.DialogInterface; -import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -13,181 +9,163 @@ 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; -/** Lets the user start the OPML-import process. */ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.Reader; + +/** + * Lets the user start the OPML-import process from a path + */ public class OpmlImportFromPathActivity extends OpmlImportBaseActivity { - private static final String TAG = "OpmlImportFromPathActivity"; - - public static final String IMPORT_DIR = "import/"; - - private TextView txtvPath; - private Button butStart; - private String importPath; - - private OpmlImportWorker importWorker; - - @Override - protected void onCreate(Bundle savedInstanceState) { - setTheme(PodcastApp.getThemeResourceId()); - super.onCreate(savedInstanceState); - - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setContentView(R.layout.opml_import); - - txtvPath = (TextView) findViewById(R.id.txtvPath); - butStart = (Button) findViewById(R.id.butStartImport); - - butStart.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - checkFolderForFiles(); - } - - }); - } - - @Override - protected void onResume() { - super.onResume(); - StorageUtils.checkStorageAvailability(this); - setImportPath(); - } - - /** - * Sets the importPath variable and makes txtvPath display the import - * directory. - */ - private void setImportPath() { - File importDir = PodcastApp.getDataFolder(this, IMPORT_DIR); - boolean success = true; - if (!importDir.exists()) { - if (AppConfig.DEBUG) - Log.d(TAG, "Import directory doesn't exist. Creating..."); - success = importDir.mkdir(); - if (!success) { - Log.e(TAG, "Could not create directory"); - } - } - if (success) { - txtvPath.setText(importDir.toString()); - importPath = importDir.toString(); - } else { - txtvPath.setText(R.string.opml_directory_error); - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - default: - return false; - } - } - - /** - * Looks at the contents of the import directory and decides what to do. If - * more than one file is in the directory, a dialog will be created to let - * the user choose which item to import - * */ - private void checkFolderForFiles() { - File dir = new File(importPath); - if (dir.isDirectory()) { - File[] fileList = dir.listFiles(); - if (fileList.length == 1) { - if (AppConfig.DEBUG) - Log.d(TAG, "Found one file, choosing that one."); - startImport(fileList[0]); - } else if (fileList.length > 1) { - Log.w(TAG, "Import directory contains more than one file."); - askForFile(dir); - } else { - Log.e(TAG, "Import directory is empty"); - Toast toast = Toast - .makeText(this, R.string.opml_import_error_dir_empty, - Toast.LENGTH_LONG); - toast.show(); - } - } - } - - /** Starts the import process. */ - private void startImport(File file) { - - if (file != null) { - importWorker = new OpmlImportWorker(this, file) { - - @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( - OpmlImportFromPathActivity.this, - OpmlFeedChooserActivity.class), 0); - } else { - if (AppConfig.DEBUG) - Log.d(TAG, "Parser error occured"); - } - } - }; - importWorker.executeAsync(); - } - } - - /** - * Asks the user to choose from a list of files in a directory and returns - * his choice. - */ - private void askForFile(File dir) { - final File[] fileList = dir.listFiles(); - String[] fileNames = dir.list(); - - AlertDialog.Builder dialog = new AlertDialog.Builder(this); - dialog.setTitle(R.string.choose_file_to_import_label); - dialog.setNeutralButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (AppConfig.DEBUG) - Log.d(TAG, "Dialog was cancelled"); - dialog.dismiss(); - } - }); - dialog.setItems(fileNames, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (AppConfig.DEBUG) - Log.d(TAG, "File at index " + which + " was chosen"); - dialog.dismiss(); - startImport(fileList[which]); - } - }); - dialog.create().show(); - } + public static final String IMPORT_DIR = "import/"; + private static final String TAG = "OpmlImportFromPathActivity"; + private TextView txtvPath; + private Button butStart; + private String importPath; + + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(PodcastApp.getThemeResourceId()); + super.onCreate(savedInstanceState); + + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setContentView(R.layout.opml_import); + + txtvPath = (TextView) findViewById(R.id.txtvPath); + butStart = (Button) findViewById(R.id.butStartImport); + + butStart.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + checkFolderForFiles(); + } + + }); + } + + @Override + protected void onResume() { + super.onResume(); + StorageUtils.checkStorageAvailability(this); + setImportPath(); + } + + /** + * Sets the importPath variable and makes txtvPath display the import + * directory. + */ + private void setImportPath() { + File importDir = PodcastApp.getDataFolder(this, IMPORT_DIR); + boolean success = true; + if (!importDir.exists()) { + if (AppConfig.DEBUG) + Log.d(TAG, "Import directory doesn't exist. Creating..."); + success = importDir.mkdir(); + if (!success) { + Log.e(TAG, "Could not create directory"); + } + } + if (success) { + txtvPath.setText(importDir.toString()); + importPath = importDir.toString(); + } else { + txtvPath.setText(R.string.opml_directory_error); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + default: + return false; + } + } + + /** + * Looks at the contents of the import directory and decides what to do. If + * more than one file is in the directory, a dialog will be created to let + * the user choose which item to import + */ + private void checkFolderForFiles() { + File dir = new File(importPath); + if (dir.isDirectory()) { + File[] fileList = dir.listFiles(); + if (fileList.length == 1) { + if (AppConfig.DEBUG) + Log.d(TAG, "Found one file, choosing that one."); + startImport(fileList[0]); + } else if (fileList.length > 1) { + Log.w(TAG, "Import directory contains more than one file."); + askForFile(dir); + } else { + Log.e(TAG, "Import directory is empty"); + Toast toast = Toast + .makeText(this, R.string.opml_import_error_dir_empty, + Toast.LENGTH_LONG); + toast.show(); + } + } + } + + private void startImport(File file) { + try { + Reader mReader = new FileReader(file); + if (AppConfig.DEBUG) Log.d(TAG, "Parsing " + file.toString()); + startImport(mReader); + } catch (FileNotFoundException e) { + Log.d(TAG, "File not found which really should be there"); + // this should never happen as it is a file we have just chosen + } + } + + /** + * Asks the user to choose from a list of files in a directory and returns + * his choice. + */ + private void askForFile(File dir) { + final File[] fileList = dir.listFiles(); + String[] fileNames = dir.list(); + + AlertDialog.Builder dialog = new AlertDialog.Builder(this); + dialog.setTitle(R.string.choose_file_to_import_label); + dialog.setNeutralButton(android.R.string.cancel, + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + if (AppConfig.DEBUG) + Log.d(TAG, "Dialog was cancelled"); + dialog.dismiss(); + } + }); + dialog.setItems(fileNames, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + if (AppConfig.DEBUG) + Log.d(TAG, "File at index " + which + " was chosen"); + dialog.dismiss(); + startImport(fileList[which]); + } + }); + dialog.create().show(); + } } diff --git a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java index 6d66b0fa0..2c1d30bdb 100644 --- a/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java +++ b/src/de/danoeh/antennapod/asynctask/OpmlImportWorker.java @@ -29,20 +29,6 @@ public class OpmlImportWorker extends private Reader mReader; - public OpmlImportWorker(Context context, File file) { - super(); - this.context = context; - - // Create reader - try { - mReader = new FileReader(file); - if (AppConfig.DEBUG) Log.d(TAG, "Parsing " + file.toString()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - exception = e; - } - } - public OpmlImportWorker(Context context, Reader reader) { super(); this.context = context; -- cgit v1.2.3 From 799f2e203bacfff340da654d50fec7d07f0ecd08 Mon Sep 17 00:00:00 2001 From: ligi Date: Wed, 23 Jan 2013 16:14:10 +0100 Subject: rm typo --- src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/de') diff --git a/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java b/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java index 012f51c6d..cf3028307 100644 --- a/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java +++ b/src/de/danoeh/antennapod/activity/OpmlImportBaseActivity.java @@ -73,7 +73,7 @@ public class OpmlImportBaseActivity extends SherlockActivity { OpmlFeedChooserActivity.class), 0); } else { if (AppConfig.DEBUG) - Log.d(TAG, "Parser error occured"); + Log.d(TAG, "Parser error occurred"); } } }; -- cgit v1.2.3