From f2baeb259c0305a9d59fff11a9bd4c7ab6072077 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Fri, 17 Feb 2012 16:52:50 -0800 Subject: [PATCH] Validate email addresses when saving list sharing settings --- .../astrid/actfm/EditPeopleControlSet.java | 48 +++---------------- .../astrid/actfm/TagSettingsActivity.java | 22 ++++++++- .../todoroo/astrid/ui/PeopleContainer.java | 43 +++++++++++++++++ 3 files changed, 71 insertions(+), 42 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java b/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java index e4294e46e..838b0728d 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java @@ -58,6 +58,7 @@ import com.todoroo.astrid.service.ThemeService; import com.todoroo.astrid.tags.TagService; import com.todoroo.astrid.ui.PeopleContainer; import com.todoroo.astrid.ui.PeopleContainer.OnAddNewPersonListener; +import com.todoroo.astrid.ui.PeopleContainer.ParseSharedException; import com.todoroo.astrid.ui.PopupControlSet; public class EditPeopleControlSet extends PopupControlSet { @@ -622,7 +623,12 @@ public class EditPeopleControlSet extends PopupControlSet { } } - JSONObject sharedWith = parseSharedWithAndTags(); + JSONObject sharedWith = sharedWithContainer.parseSharedWithAndTags(activity, false); + if(cbFacebook.isChecked()) + sharedWith.put("fb", true); + if(cbTwitter.isChecked()) + sharedWith.put("tw", true); + dirty = dirty || sharedWith.has("p"); if(dirty && !actFmPreferenceService.isLoggedIn()) { DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() { @@ -686,46 +692,6 @@ public class EditPeopleControlSet extends PopupControlSet { Toast.makeText(activity, saveToast, length).show(); } - private class ParseSharedException extends Exception { - private static final long serialVersionUID = -4135848250086302970L; - public TextView view; - public String message; - - public ParseSharedException(TextView view, String message) { - this.view = view; - this.message = message; - } - } - - @SuppressWarnings("nls") - private JSONObject parseSharedWithAndTags() throws - JSONException, ParseSharedException { - JSONObject sharedWith = new JSONObject(); - if(cbFacebook.isChecked()) - sharedWith.put("fb", true); - if(cbTwitter.isChecked()) - sharedWith.put("tw", true); - - JSONArray peopleList = new JSONArray(); - for(int i = 0; i < sharedWithContainer.getChildCount(); i++) { - TextView textView = sharedWithContainer.getTextView(i); - textView.setTextAppearance(activity, android.R.style.TextAppearance_Medium_Inverse); - String text = textView.getText().toString(); - - if(text.length() == 0) - continue; - - if(text.indexOf('@') == -1) - throw new ParseSharedException(textView, - activity.getString(R.string.actfm_EPA_invalid_email, text)); - peopleList.put(text); - } - if(peopleList.length() > 0) - sharedWith.put("p", peopleList); - - return sharedWith; - } - @SuppressWarnings("nls") protected Object[] buildSharingArgs(JSONArray emails) throws JSONException { ArrayList values = new ArrayList(); diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java b/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java index 94536df7d..57f5ed40a 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java @@ -13,6 +13,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; +import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.ActionBar; import android.support.v4.app.FragmentActivity; @@ -31,6 +32,7 @@ import android.widget.Toast; import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; +import com.todoroo.andlib.service.ExceptionService; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.andlib.utility.Preferences; @@ -48,6 +50,7 @@ import com.todoroo.astrid.service.ThemeService; import com.todoroo.astrid.tags.TagFilterExposer; import com.todoroo.astrid.tags.TagService; import com.todoroo.astrid.ui.PeopleContainer; +import com.todoroo.astrid.ui.PeopleContainer.ParseSharedException; import com.todoroo.astrid.utility.Flags; import com.todoroo.astrid.welcome.HelpInfoPopover; @@ -71,6 +74,8 @@ public class TagSettingsActivity extends FragmentActivity { @Autowired ActFmPreferenceService actFmPreferenceService; + @Autowired ExceptionService exceptionService; + private PeopleContainer tagMembers; private AsyncImageView picture; private EditText tagName; @@ -227,7 +232,22 @@ public class TagSettingsActivity extends FragmentActivity { tagData.setValue(TagData.TAG_DESCRIPTION, newDesc); - JSONArray members = tagMembers.toJSONArray(); + JSONArray members; + try { + members = tagMembers.parseSharedWithAndTags(this, true).optJSONArray("p"); + } catch (JSONException e) { + exceptionService.displayAndReportError(this, "save-people", e); + return; + } catch (ParseSharedException e) { + if(e.view != null) { + e.view.setTextColor(Color.RED); + e.view.requestFocus(); + } + DialogUtilities.okDialog(this, e.message, null); + return; + } + if (members == null) + members = new JSONArray(); if(members.length() > 0 && !actFmPreferenceService.isLoggedIn()) { if(newName.length() > 0 && oldName.length() == 0) { diff --git a/astrid/src/com/todoroo/astrid/ui/PeopleContainer.java b/astrid/src/com/todoroo/astrid/ui/PeopleContainer.java index 173570f3a..5de4d2ce3 100644 --- a/astrid/src/com/todoroo/astrid/ui/PeopleContainer.java +++ b/astrid/src/com/todoroo/astrid/ui/PeopleContainer.java @@ -4,6 +4,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.text.Editable; @@ -170,6 +171,48 @@ public class PeopleContainer extends LinearLayout { return people; } + @SuppressWarnings("nls") + public JSONObject parseSharedWithAndTags(Activity activity, boolean peopleAsJSON) throws + JSONException, ParseSharedException { + JSONObject sharedWith = new JSONObject(); + + JSONArray peopleList = new JSONArray(); + for(int i = 0; i < getChildCount(); i++) { + TextView textView = getTextView(i); + textView.setTextAppearance(activity, android.R.style.TextAppearance_Medium_Inverse); + String text = textView.getText().toString(); + + if(text.length() == 0) + continue; + + if(text.indexOf('@') == -1) + throw new ParseSharedException(textView, + activity.getString(R.string.actfm_EPA_invalid_email, text)); + if (peopleAsJSON) { + JSONObject person = PeopleContainer.createUserJson(textView); + if (person != null) + peopleList.put(person); + } else { + peopleList.put(text); + } + } + if(peopleList.length() > 0) + sharedWith.put("p", peopleList); + + return sharedWith; + } + + public static class ParseSharedException extends Exception { + private static final long serialVersionUID = -4135848250086302970L; + public TextView view; + public String message; + + public ParseSharedException(TextView view, String message) { + this.view = view; + this.message = message; + } + } + /** * Add people from JSON Array * @param people