From 39fdf4a333378affcca360886701851d84dd9e0b Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 23 Jan 2016 12:44:08 +0100 Subject: [PATCH] added support for field types jid-single and text-private --- .../ui/forms/FormFieldFactory.java | 5 +++ .../ui/forms/FormFieldWrapper.java | 8 ++++- .../ui/forms/FormJidSingleFieldWrapper.java | 31 +++++++++++++++++++ .../ui/forms/FormTextFieldWrapper.java | 19 ++++++++++-- 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/forms/FormJidSingleFieldWrapper.java diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java index 9e54678a..2e7c17dc 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldFactory.java @@ -15,10 +15,15 @@ public class FormFieldFactory { static { typeTable.put("text-single", FormTextFieldWrapper.class); typeTable.put("text-multi", FormTextFieldWrapper.class); + typeTable.put("text-private", FormTextFieldWrapper.class); + typeTable.put("jid-single", FormJidSingleFieldWrapper.class); } public static FormFieldWrapper createFromField(Context context, Field field) { Class clazz = typeTable.get(field.getType()); + if (clazz == null) { + clazz = FormTextFieldWrapper.class; + } return FormFieldWrapper.createFromField(clazz, context, field); } } diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java index 3af37536..a0ed371a 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java @@ -19,7 +19,11 @@ public abstract class FormFieldWrapper { this.field = field; LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.view = inflater.inflate(getLayoutResource(), null); - setLabel(field.getLabel(), field.isRequired()); + String label = field.getLabel(); + if (label == null) { + label = field.getFieldName(); + } + setLabel(label, field.isRequired()); } public void submit() { @@ -34,6 +38,8 @@ public abstract class FormFieldWrapper { abstract List getValues(); + abstract boolean validates(); + abstract protected int getLayoutResource(); protected static FormFieldWrapper createFromField(Class c, Context context, Field field) { diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormJidSingleFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormJidSingleFieldWrapper.java new file mode 100644 index 00000000..b54940f6 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormJidSingleFieldWrapper.java @@ -0,0 +1,31 @@ +package eu.siacs.conversations.ui.forms; + +import android.content.Context; +import android.text.InputType; + +import eu.siacs.conversations.R; +import eu.siacs.conversations.xmpp.forms.Field; +import eu.siacs.conversations.xmpp.jid.InvalidJidException; +import eu.siacs.conversations.xmpp.jid.Jid; + +public class FormJidSingleFieldWrapper extends FormTextFieldWrapper { + + protected FormJidSingleFieldWrapper(Context context, Field field) { + super(context, field); + editText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); + editText.setHint(R.string.account_settings_example_jabber_id); + } + + @Override + public boolean validates() { + String value = getValue(); + if (!value.isEmpty()) { + try { + Jid.fromString(value); + } catch (InvalidJidException e) { + return false; + } + } + return super.validates(); + } +} diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java index 8f70dd37..71ed6e4a 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.ui.forms; import android.content.Context; +import android.text.InputType; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; @@ -20,7 +21,10 @@ public class FormTextFieldWrapper extends FormFieldWrapper { protected FormTextFieldWrapper(Context context, Field field) { super(context, field); editText = (EditText) view.findViewById(R.id.field); - editText.setSingleLine("text-single".equals(field.getType())); + editText.setSingleLine(!"text-multi".equals(field.getType())); + if ("text-private".equals(field.getType())) { + editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + } } @Override @@ -36,15 +40,24 @@ public class FormTextFieldWrapper extends FormFieldWrapper { textView.setText(spannableString); } + protected String getValue() { + return editText.getText().toString(); + } + @Override - List getValues() { + public List getValues() { List values = new ArrayList<>(); - for (String line : editText.getText().toString().split("\\n")) { + for (String line : getValue().split("\\n")) { values.add(line); } return values; } + @Override + public boolean validates() { + return getValue().trim().length() > 0 || !field.isRequired(); + } + @Override protected int getLayoutResource() { return R.layout.form_text;