Skip to content

Commit

Permalink
Allow more intent receivers when importing recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
caseydavenport committed Sep 9, 2015
1 parent 6bf74a7 commit a224514
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
31 changes: 20 additions & 11 deletions src/com/biermacht/brews/frontend/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
Expand Down Expand Up @@ -256,10 +257,12 @@ private AlertDialog.Builder importRecipeAlert() {

public void onClick(DialogInterface dialog, int which) {
try {
Intent i3 = new Intent(Intent.ACTION_GET_CONTENT);
i3.setType("file/*");
startActivityForResult(i3, 1);
} catch (android.content.ActivityNotFoundException e){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
String[] mimeTypes = {"file/*.xml", "file/*.bsmx", "text/*"};
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, Constants.REQUEST_IMPORT_FILE);
} catch (android.content.ActivityNotFoundException e) {
new AlertDialog.Builder(getApplicationContext())
.setTitle("No File Browser Found")
.setMessage("Please install a file browser from the Play Store")
Expand All @@ -277,16 +280,21 @@ public void onClick(DialogInterface dialog, int which) {
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
if (requestCode == Constants.REQUEST_IMPORT_FILE) {
if (resultCode == RESULT_OK) {
String path = data.getData().getPath().toString();
Log.d("MainActivity::onActivityResult", "User selected a file.");
Uri uri = data.getData();
String path = uri.getPath().toString();
Log.d("MainActivity::onActivityResult", "URI: " + uri.toString());
Log.d("MainActivity::onActivityResult", "Path: " + path);

if (path != null) {
new LoadRecipes(this, path, ingredientHandler).execute("");
new LoadRecipes(this, uri, ingredientHandler).execute("");
}
}
if (resultCode == RESULT_CANCELED) {
// Action to get recipes was cancelled - do nothing.
Log.d("MainActivity::onActivityResult", "Load recipes from file cancelled by user");
}
}
}
Expand Down Expand Up @@ -357,14 +365,15 @@ public void setImportedRecipes(ArrayList<Recipe> recipeList) {
*/
private class LoadRecipes extends AsyncTask<String, Void, String> {

private String path;
private Uri uri;
private IngredientHandler ingredientHandler;
private Context context;
private ProgressDialog progress;
private ArrayList<Recipe> importedRecipes;

public LoadRecipes(Context c, String path, IngredientHandler i) {
this.path = path;
public LoadRecipes(Context c, Uri uri, IngredientHandler i) {
Log.d("MainActivity", "Loading URI: " + uri.toString());
this.uri = uri;
this.ingredientHandler = i;
this.context = c;
this.importedRecipes = new ArrayList<Recipe>();
Expand All @@ -373,7 +382,7 @@ public LoadRecipes(Context c, String path, IngredientHandler i) {
@Override
protected String doInBackground(String... params) {
try {
importedRecipes = ingredientHandler.getRecipesFromXml(path);
importedRecipes = ingredientHandler.getRecipesFromXml(uri);
} catch (IOException e) {
Log.e("LoadRecipes", e.toString());
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/biermacht/brews/ingredient/Ingredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void setVersion(int version) {
*/
public int compareTo(Ingredient other) {
// If they are not the same type, sort based on type.
if (!this.getType().equals(other.getType())) {
if (! this.getType().equals(other.getType())) {
return this.getType().compareTo(other.getType());
}

Expand Down
6 changes: 3 additions & 3 deletions src/com/biermacht/brews/recipe/MashStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ public int describeContents() {
@Override
public boolean equals(Object o) {
// Non-MashStep objects cannot equal a MashStep.
if (!(o instanceof MashStep)) {
if (! (o instanceof MashStep)) {
return false;
}

// Comparing to a MashStep - cast the given Object.
MashStep other = (MashStep) o;

// Both are MashStep objects - compare important fields.
if (!this.getName().equals(other.getName())) {
if (! this.getName().equals(other.getName())) {
Log.d("MashStep", "MashStep.equals(): " + this.getName() + " != " + other.getName());
return false;
}
Expand All @@ -171,7 +171,7 @@ else if (this.getBeerXmlStandardStepTemp() != other.getBeerXmlStandardStepTemp()
Log.d("MashStep", "MashStep.equals(): " + this.getBeerXmlStandardStepTemp() + " != " + other.getBeerXmlStandardStepTemp());
return false;
}
else if (!this.getType().equals(other.getType())) {
else if (! this.getType().equals(other.getType())) {
Log.d("MashStep", "MashStep.equals(): " + this.getType() + " != " + other.getType());
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/com/biermacht/brews/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class Constants {
public static final int REQUEST_NEW_MASH_STEP = 1;
public static final int REQUEST_EDIT_MASH_STEP = 2;
public static final int REQUEST_EDIT_RECIPE = 3;
public static final int REQUEST_IMPORT_FILE = 4;

// Possible timer states
public static int PAUSED = 0;
Expand Down
9 changes: 5 additions & 4 deletions src/com/biermacht/brews/utils/IngredientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,20 +344,21 @@ private ArrayList<Ingredient> getMiscsFromXml() throws IOException {
* @return ArrayList of Ingredient Objects
* @throws IOException
*/
public ArrayList<Recipe> getRecipesFromXml(String s) throws IOException {
public ArrayList<Recipe> getRecipesFromXml(Uri uri) throws IOException {
ArrayList<Recipe> retlist = new ArrayList<Recipe>();
ArrayList<Recipe> list;
BeerXmlReader beerXmlReader = new BeerXmlReader();
BsmxXmlReader bsmxXmlReader = new BsmxXmlReader();
SAXParserFactory spf = SAXParserFactory.newInstance();
String path = uri.getPath().toString();

try {
ContentResolver cr = mContext.getContentResolver();
SAXParser sp = spf.newSAXParser();
InputStream is = cr.openInputStream((Uri.parse("file://" + s)));
InputStream is = cr.openInputStream(uri);

// If this is a .bsmx, use the .bsmx parser. Otherwise, assume it is BeerXML.
if (s.endsWith(".bsmx")) {
if (path.endsWith(".bsmx")) {
Log.d("IngredientHandler", "Attempting to parse .bsmx file");
// .bsmx files generated by BeerSmith include HTML encoded characters without declaring
// the encoding - pull in the whole file as a string and escape any HTML entities before
Expand All @@ -382,7 +383,7 @@ public ArrayList<Recipe> getRecipesFromXml(String s) throws IOException {
retlist.addAll(list);
} catch (Exception e) {
e.printStackTrace();
Log.e("getRecipesFromXml", "Failed to load file '" + s + "' : " + e.toString());
Log.e("getRecipesFromXml", "Failed to load file '" + path + "' : " + e.toString());
}

return retlist;
Expand Down
1 change: 0 additions & 1 deletion src/com/biermacht/brews/xml/BsmxXmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
Expand Down

0 comments on commit a224514

Please sign in to comment.